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/module.h>
19 #include <linux/slab.h>
21 #include <linux/namei.h>
22 #include <linux/pagemap.h>
23 #include <linux/fsnotify.h>
24 #include <linux/personality.h>
25 #include <linux/security.h>
26 #include <linux/ima.h>
27 #include <linux/syscalls.h>
28 #include <linux/mount.h>
29 #include <linux/audit.h>
30 #include <linux/capability.h>
31 #include <linux/file.h>
32 #include <linux/fcntl.h>
33 #include <linux/device_cgroup.h>
34 #include <linux/fs_struct.h>
35 #include <asm/uaccess.h>
39 /* [Feb-1997 T. Schoebel-Theuer]
40 * Fundamental changes in the pathname lookup mechanisms (namei)
41 * were necessary because of omirr. The reason is that omirr needs
42 * to know the _real_ pathname, not the user-supplied one, in case
43 * of symlinks (and also when transname replacements occur).
45 * The new code replaces the old recursive symlink resolution with
46 * an iterative one (in case of non-nested symlink chains). It does
47 * this with calls to <fs>_follow_link().
48 * As a side effect, dir_namei(), _namei() and follow_link() are now
49 * replaced with a single function lookup_dentry() that can handle all
50 * the special cases of the former code.
52 * With the new dcache, the pathname is stored at each inode, at least as
53 * long as the refcount of the inode is positive. As a side effect, the
54 * size of the dcache depends on the inode cache and thus is dynamic.
56 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57 * resolution to correspond with current state of the code.
59 * Note that the symlink resolution is not *completely* iterative.
60 * There is still a significant amount of tail- and mid- recursion in
61 * the algorithm. Also, note that <fs>_readlink() is not used in
62 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
63 * may return different results than <fs>_follow_link(). Many virtual
64 * filesystems (including /proc) exhibit this behavior.
67 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
68 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
69 * and the name already exists in form of a symlink, try to create the new
70 * name indicated by the symlink. The old code always complained that the
71 * name already exists, due to not following the symlink even if its target
72 * is nonexistent. The new semantics affects also mknod() and link() when
73 * the name is a symlink pointing to a non-existent name.
75 * I don't know which semantics is the right one, since I have no access
76 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78 * "old" one. Personally, I think the new semantics is much more logical.
79 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80 * file does succeed in both HP-UX and SunOs, but not in Solaris
81 * and in the old Linux semantics.
84 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85 * semantics. See the comments in "open_namei" and "do_link" below.
87 * [10-Sep-98 Alan Modra] Another symlink change.
90 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91 * inside the path - always follow.
92 * in the last component in creation/removal/renaming - never follow.
93 * if LOOKUP_FOLLOW passed - follow.
94 * if the pathname has trailing slashes - follow.
95 * otherwise - don't follow.
96 * (applied in that order).
98 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100 * During the 2.4 we need to fix the userland stuff depending on it -
101 * hopefully we will be able to get rid of that wart in 2.5. So far only
102 * XEmacs seems to be relying on it...
105 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
106 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
107 * any extra contention...
110 /* In order to reduce some races, while at the same time doing additional
111 * checking and hopefully speeding things up, we copy filenames to the
112 * kernel data space before using them..
114 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
115 * PATH_MAX includes the nul terminator --RR.
117 static int do_getname(const char __user
*filename
, char *page
)
120 unsigned long len
= PATH_MAX
;
122 if (!segment_eq(get_fs(), KERNEL_DS
)) {
123 if ((unsigned long) filename
>= TASK_SIZE
)
125 if (TASK_SIZE
- (unsigned long) filename
< PATH_MAX
)
126 len
= TASK_SIZE
- (unsigned long) filename
;
129 retval
= strncpy_from_user(page
, filename
, len
);
133 return -ENAMETOOLONG
;
139 static char *getname_flags(const char __user
* filename
, int flags
)
143 result
= ERR_PTR(-ENOMEM
);
146 int retval
= do_getname(filename
, tmp
);
150 if (retval
!= -ENOENT
|| !(flags
& LOOKUP_EMPTY
)) {
152 result
= ERR_PTR(retval
);
156 audit_getname(result
);
160 char *getname(const char __user
* filename
)
162 return getname_flags(filename
, 0);
165 #ifdef CONFIG_AUDITSYSCALL
166 void putname(const char *name
)
168 if (unlikely(!audit_dummy_context()))
173 EXPORT_SYMBOL(putname
);
177 * This does basic POSIX ACL permission checking
179 static int acl_permission_check(struct inode
*inode
, int mask
, unsigned int flags
,
180 int (*check_acl
)(struct inode
*inode
, int mask
, unsigned int flags
))
182 unsigned int mode
= inode
->i_mode
;
184 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
186 if (current_user_ns() != inode_userns(inode
))
189 if (current_fsuid() == inode
->i_uid
)
192 if (IS_POSIXACL(inode
) && (mode
& S_IRWXG
) && check_acl
) {
193 int error
= check_acl(inode
, mask
, flags
);
194 if (error
!= -EAGAIN
)
198 if (in_group_p(inode
->i_gid
))
204 * If the DACs are ok we don't need any capability check.
206 if ((mask
& ~mode
) == 0)
212 * generic_permission - check for access rights on a Posix-like filesystem
213 * @inode: inode to check access rights for
214 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
215 * @check_acl: optional callback to check for Posix ACLs
216 * @flags: IPERM_FLAG_ flags.
218 * Used to check for read/write/execute permissions on a file.
219 * We use "fsuid" for this, letting us set arbitrary permissions
220 * for filesystem access without changing the "normal" uids which
221 * are used for other things.
223 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
224 * request cannot be satisfied (eg. requires blocking or too much complexity).
225 * It would then be called again in ref-walk mode.
227 int generic_permission(struct inode
*inode
, int mask
, unsigned int flags
,
228 int (*check_acl
)(struct inode
*inode
, int mask
, unsigned int flags
))
233 * Do the basic POSIX ACL permission checks.
235 ret
= acl_permission_check(inode
, mask
, flags
, check_acl
);
240 * Read/write DACs are always overridable.
241 * Executable DACs are overridable for all directories and
242 * for non-directories that have least one exec bit set.
244 if (!(mask
& MAY_EXEC
) || execute_ok(inode
))
245 if (ns_capable(inode_userns(inode
), CAP_DAC_OVERRIDE
))
249 * Searching includes executable on directories, else just read.
251 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
252 if (mask
== MAY_READ
|| (S_ISDIR(inode
->i_mode
) && !(mask
& MAY_WRITE
)))
253 if (ns_capable(inode_userns(inode
), CAP_DAC_READ_SEARCH
))
260 * inode_permission - check for access rights to a given inode
261 * @inode: inode to check permission on
262 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
264 * Used to check for read/write/execute permissions on an inode.
265 * We use "fsuid" for this, letting us set arbitrary permissions
266 * for filesystem access without changing the "normal" uids which
267 * are used for other things.
269 int inode_permission(struct inode
*inode
, int mask
)
273 if (mask
& MAY_WRITE
) {
274 umode_t mode
= inode
->i_mode
;
277 * Nobody gets write access to a read-only fs.
279 if (IS_RDONLY(inode
) &&
280 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
284 * Nobody gets write access to an immutable file.
286 if (IS_IMMUTABLE(inode
))
290 if (inode
->i_op
->permission
)
291 retval
= inode
->i_op
->permission(inode
, mask
, 0);
293 retval
= generic_permission(inode
, mask
, 0,
294 inode
->i_op
->check_acl
);
299 retval
= devcgroup_inode_permission(inode
, mask
);
303 return security_inode_permission(inode
, mask
);
307 * get_write_access() gets write permission for a file.
308 * put_write_access() releases this write permission.
309 * This is used for regular files.
310 * We cannot support write (and maybe mmap read-write shared) accesses and
311 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
312 * can have the following values:
313 * 0: no writers, no VM_DENYWRITE mappings
314 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
315 * > 0: (i_writecount) users are writing to the file.
317 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
318 * except for the cases where we don't hold i_writecount yet. Then we need to
319 * use {get,deny}_write_access() - these functions check the sign and refuse
320 * to do the change if sign is wrong. Exclusion between them is provided by
321 * the inode->i_lock spinlock.
324 int get_write_access(struct inode
* inode
)
326 spin_lock(&inode
->i_lock
);
327 if (atomic_read(&inode
->i_writecount
) < 0) {
328 spin_unlock(&inode
->i_lock
);
331 atomic_inc(&inode
->i_writecount
);
332 spin_unlock(&inode
->i_lock
);
337 int deny_write_access(struct file
* file
)
339 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
341 spin_lock(&inode
->i_lock
);
342 if (atomic_read(&inode
->i_writecount
) > 0) {
343 spin_unlock(&inode
->i_lock
);
346 atomic_dec(&inode
->i_writecount
);
347 spin_unlock(&inode
->i_lock
);
353 * path_get - get a reference to a path
354 * @path: path to get the reference to
356 * Given a path increment the reference count to the dentry and the vfsmount.
358 void path_get(struct path
*path
)
363 EXPORT_SYMBOL(path_get
);
366 * path_put - put a reference to a path
367 * @path: path to put the reference to
369 * Given a path decrement the reference count to the dentry and the vfsmount.
371 void path_put(struct path
*path
)
376 EXPORT_SYMBOL(path_put
);
379 * Path walking has 2 modes, rcu-walk and ref-walk (see
380 * Documentation/filesystems/path-lookup.txt). In situations when we can't
381 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
382 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
383 * mode. Refcounts are grabbed at the last known good point before rcu-walk
384 * got stuck, so ref-walk may continue from there. If this is not successful
385 * (eg. a seqcount has changed), then failure is returned and it's up to caller
386 * to restart the path walk from the beginning in ref-walk mode.
390 * unlazy_walk - try to switch to ref-walk mode.
391 * @nd: nameidata pathwalk data
392 * @dentry: child of nd->path.dentry or NULL
393 * Returns: 0 on success, -ECHILD on failure
395 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
396 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
397 * @nd or NULL. Must be called from rcu-walk context.
399 static int unlazy_walk(struct nameidata
*nd
, struct dentry
*dentry
)
401 struct fs_struct
*fs
= current
->fs
;
402 struct dentry
*parent
= nd
->path
.dentry
;
405 BUG_ON(!(nd
->flags
& LOOKUP_RCU
));
406 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
408 spin_lock(&fs
->lock
);
409 if (nd
->root
.mnt
!= fs
->root
.mnt
||
410 nd
->root
.dentry
!= fs
->root
.dentry
)
413 spin_lock(&parent
->d_lock
);
415 if (!__d_rcu_to_refcount(parent
, nd
->seq
))
417 BUG_ON(nd
->inode
!= parent
->d_inode
);
419 if (dentry
->d_parent
!= parent
)
421 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
422 if (!__d_rcu_to_refcount(dentry
, nd
->seq
))
425 * If the sequence check on the child dentry passed, then
426 * the child has not been removed from its parent. This
427 * means the parent dentry must be valid and able to take
428 * a reference at this point.
430 BUG_ON(!IS_ROOT(dentry
) && dentry
->d_parent
!= parent
);
431 BUG_ON(!parent
->d_count
);
433 spin_unlock(&dentry
->d_lock
);
435 spin_unlock(&parent
->d_lock
);
438 spin_unlock(&fs
->lock
);
440 mntget(nd
->path
.mnt
);
443 br_read_unlock(vfsmount_lock
);
444 nd
->flags
&= ~LOOKUP_RCU
;
448 spin_unlock(&dentry
->d_lock
);
450 spin_unlock(&parent
->d_lock
);
453 spin_unlock(&fs
->lock
);
458 * release_open_intent - free up open intent resources
459 * @nd: pointer to nameidata
461 void release_open_intent(struct nameidata
*nd
)
463 struct file
*file
= nd
->intent
.open
.file
;
465 if (file
&& !IS_ERR(file
)) {
466 if (file
->f_path
.dentry
== NULL
)
473 static inline int d_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
475 return dentry
->d_op
->d_revalidate(dentry
, nd
);
478 static struct dentry
*
479 do_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
481 int status
= d_revalidate(dentry
, nd
);
482 if (unlikely(status
<= 0)) {
484 * The dentry failed validation.
485 * If d_revalidate returned 0 attempt to invalidate
486 * the dentry otherwise d_revalidate is asking us
487 * to return a fail status.
491 dentry
= ERR_PTR(status
);
492 } else if (!d_invalidate(dentry
)) {
501 * complete_walk - successful completion of path walk
502 * @nd: pointer nameidata
504 * If we had been in RCU mode, drop out of it and legitimize nd->path.
505 * Revalidate the final result, unless we'd already done that during
506 * the path walk or the filesystem doesn't ask for it. Return 0 on
507 * success, -error on failure. In case of failure caller does not
508 * need to drop nd->path.
510 static int complete_walk(struct nameidata
*nd
)
512 struct dentry
*dentry
= nd
->path
.dentry
;
515 if (nd
->flags
& LOOKUP_RCU
) {
516 nd
->flags
&= ~LOOKUP_RCU
;
517 if (!(nd
->flags
& LOOKUP_ROOT
))
519 spin_lock(&dentry
->d_lock
);
520 if (unlikely(!__d_rcu_to_refcount(dentry
, nd
->seq
))) {
521 spin_unlock(&dentry
->d_lock
);
523 br_read_unlock(vfsmount_lock
);
526 BUG_ON(nd
->inode
!= dentry
->d_inode
);
527 spin_unlock(&dentry
->d_lock
);
528 mntget(nd
->path
.mnt
);
530 br_read_unlock(vfsmount_lock
);
533 if (likely(!(nd
->flags
& LOOKUP_JUMPED
)))
536 if (likely(!(dentry
->d_flags
& DCACHE_OP_REVALIDATE
)))
539 if (likely(!(dentry
->d_sb
->s_type
->fs_flags
& FS_REVAL_DOT
)))
542 /* Note: we do not d_invalidate() */
543 status
= d_revalidate(dentry
, nd
);
555 * Short-cut version of permission(), for calling on directories
556 * during pathname resolution. Combines parts of permission()
557 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
559 * If appropriate, check DAC only. If not appropriate, or
560 * short-cut DAC fails, then call ->permission() to do more
561 * complete permission check.
563 static inline int exec_permission(struct inode
*inode
, unsigned int flags
)
566 struct user_namespace
*ns
= inode_userns(inode
);
568 if (inode
->i_op
->permission
) {
569 ret
= inode
->i_op
->permission(inode
, MAY_EXEC
, flags
);
573 ret
= acl_permission_check(inode
, MAY_EXEC
, flags
,
574 inode
->i_op
->check_acl
);
579 if (ns_capable(ns
, CAP_DAC_OVERRIDE
) ||
580 ns_capable(ns
, CAP_DAC_READ_SEARCH
))
585 return security_inode_exec_permission(inode
, flags
);
588 static __always_inline
void set_root(struct nameidata
*nd
)
591 get_fs_root(current
->fs
, &nd
->root
);
594 static int link_path_walk(const char *, struct nameidata
*);
596 static __always_inline
void set_root_rcu(struct nameidata
*nd
)
599 struct fs_struct
*fs
= current
->fs
;
603 seq
= read_seqcount_begin(&fs
->seq
);
605 nd
->seq
= __read_seqcount_begin(&nd
->root
.dentry
->d_seq
);
606 } while (read_seqcount_retry(&fs
->seq
, seq
));
610 static __always_inline
int __vfs_follow_link(struct nameidata
*nd
, const char *link
)
622 nd
->flags
|= LOOKUP_JUMPED
;
624 nd
->inode
= nd
->path
.dentry
->d_inode
;
626 ret
= link_path_walk(link
, nd
);
630 return PTR_ERR(link
);
633 static void path_put_conditional(struct path
*path
, struct nameidata
*nd
)
636 if (path
->mnt
!= nd
->path
.mnt
)
640 static inline void path_to_nameidata(const struct path
*path
,
641 struct nameidata
*nd
)
643 if (!(nd
->flags
& LOOKUP_RCU
)) {
644 dput(nd
->path
.dentry
);
645 if (nd
->path
.mnt
!= path
->mnt
)
646 mntput(nd
->path
.mnt
);
648 nd
->path
.mnt
= path
->mnt
;
649 nd
->path
.dentry
= path
->dentry
;
652 static inline void put_link(struct nameidata
*nd
, struct path
*link
, void *cookie
)
654 struct inode
*inode
= link
->dentry
->d_inode
;
655 if (!IS_ERR(cookie
) && inode
->i_op
->put_link
)
656 inode
->i_op
->put_link(link
->dentry
, nd
, cookie
);
660 static __always_inline
int
661 follow_link(struct path
*link
, struct nameidata
*nd
, void **p
)
664 struct dentry
*dentry
= link
->dentry
;
666 BUG_ON(nd
->flags
& LOOKUP_RCU
);
668 if (link
->mnt
== nd
->path
.mnt
)
671 if (unlikely(current
->total_link_count
>= 40)) {
672 *p
= ERR_PTR(-ELOOP
); /* no ->put_link(), please */
677 current
->total_link_count
++;
679 touch_atime(link
->mnt
, dentry
);
680 nd_set_link(nd
, NULL
);
682 error
= security_inode_follow_link(link
->dentry
, nd
);
684 *p
= ERR_PTR(error
); /* no ->put_link(), please */
689 nd
->last_type
= LAST_BIND
;
690 *p
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
693 char *s
= nd_get_link(nd
);
696 error
= __vfs_follow_link(nd
, s
);
697 else if (nd
->last_type
== LAST_BIND
) {
698 nd
->flags
|= LOOKUP_JUMPED
;
699 nd
->inode
= nd
->path
.dentry
->d_inode
;
700 if (nd
->inode
->i_op
->follow_link
) {
701 /* stepped on a _really_ weird one */
710 static int follow_up_rcu(struct path
*path
)
712 struct vfsmount
*parent
;
713 struct dentry
*mountpoint
;
715 parent
= path
->mnt
->mnt_parent
;
716 if (parent
== path
->mnt
)
718 mountpoint
= path
->mnt
->mnt_mountpoint
;
719 path
->dentry
= mountpoint
;
724 int follow_up(struct path
*path
)
726 struct vfsmount
*parent
;
727 struct dentry
*mountpoint
;
729 br_read_lock(vfsmount_lock
);
730 parent
= path
->mnt
->mnt_parent
;
731 if (parent
== path
->mnt
) {
732 br_read_unlock(vfsmount_lock
);
736 mountpoint
= dget(path
->mnt
->mnt_mountpoint
);
737 br_read_unlock(vfsmount_lock
);
739 path
->dentry
= mountpoint
;
746 * Perform an automount
747 * - return -EISDIR to tell follow_managed() to stop and return the path we
750 static int follow_automount(struct path
*path
, unsigned flags
,
753 struct vfsmount
*mnt
;
756 if (!path
->dentry
->d_op
|| !path
->dentry
->d_op
->d_automount
)
759 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
760 * and this is the terminal part of the path.
762 if ((flags
& LOOKUP_NO_AUTOMOUNT
) && !(flags
& LOOKUP_CONTINUE
))
763 return -EISDIR
; /* we actually want to stop here */
765 /* We want to mount if someone is trying to open/create a file of any
766 * type under the mountpoint, wants to traverse through the mountpoint
767 * or wants to open the mounted directory.
769 * We don't want to mount if someone's just doing a stat and they've
770 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
771 * appended a '/' to the name.
773 if (!(flags
& LOOKUP_FOLLOW
) &&
774 !(flags
& (LOOKUP_CONTINUE
| LOOKUP_DIRECTORY
|
775 LOOKUP_OPEN
| LOOKUP_CREATE
)))
778 current
->total_link_count
++;
779 if (current
->total_link_count
>= 40)
782 mnt
= path
->dentry
->d_op
->d_automount(path
);
785 * The filesystem is allowed to return -EISDIR here to indicate
786 * it doesn't want to automount. For instance, autofs would do
787 * this so that its userspace daemon can mount on this dentry.
789 * However, we can only permit this if it's a terminal point in
790 * the path being looked up; if it wasn't then the remainder of
791 * the path is inaccessible and we should say so.
793 if (PTR_ERR(mnt
) == -EISDIR
&& (flags
& LOOKUP_CONTINUE
))
798 if (!mnt
) /* mount collision */
802 /* lock_mount() may release path->mnt on error */
806 err
= finish_automount(mnt
, path
);
810 /* Someone else made a mount here whilst we were busy */
815 path
->dentry
= dget(mnt
->mnt_root
);
824 * Handle a dentry that is managed in some way.
825 * - Flagged for transit management (autofs)
826 * - Flagged as mountpoint
827 * - Flagged as automount point
829 * This may only be called in refwalk mode.
831 * Serialization is taken care of in namespace.c
833 static int follow_managed(struct path
*path
, unsigned flags
)
835 struct vfsmount
*mnt
= path
->mnt
; /* held by caller, must be left alone */
837 bool need_mntput
= false;
840 /* Given that we're not holding a lock here, we retain the value in a
841 * local variable for each dentry as we look at it so that we don't see
842 * the components of that value change under us */
843 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
844 managed
&= DCACHE_MANAGED_DENTRY
,
845 unlikely(managed
!= 0)) {
846 /* Allow the filesystem to manage the transit without i_mutex
848 if (managed
& DCACHE_MANAGE_TRANSIT
) {
849 BUG_ON(!path
->dentry
->d_op
);
850 BUG_ON(!path
->dentry
->d_op
->d_manage
);
851 ret
= path
->dentry
->d_op
->d_manage(path
->dentry
, false);
856 /* Transit to a mounted filesystem. */
857 if (managed
& DCACHE_MOUNTED
) {
858 struct vfsmount
*mounted
= lookup_mnt(path
);
864 path
->dentry
= dget(mounted
->mnt_root
);
869 /* Something is mounted on this dentry in another
870 * namespace and/or whatever was mounted there in this
871 * namespace got unmounted before we managed to get the
875 /* Handle an automount point */
876 if (managed
& DCACHE_NEED_AUTOMOUNT
) {
877 ret
= follow_automount(path
, flags
, &need_mntput
);
883 /* We didn't change the current path point */
887 if (need_mntput
&& path
->mnt
== mnt
)
894 int follow_down_one(struct path
*path
)
896 struct vfsmount
*mounted
;
898 mounted
= lookup_mnt(path
);
903 path
->dentry
= dget(mounted
->mnt_root
);
909 static inline bool managed_dentry_might_block(struct dentry
*dentry
)
911 return (dentry
->d_flags
& DCACHE_MANAGE_TRANSIT
&&
912 dentry
->d_op
->d_manage(dentry
, true) < 0);
916 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
917 * we meet a managed dentry that would need blocking.
919 static bool __follow_mount_rcu(struct nameidata
*nd
, struct path
*path
,
920 struct inode
**inode
)
923 struct vfsmount
*mounted
;
925 * Don't forget we might have a non-mountpoint managed dentry
926 * that wants to block transit.
928 if (unlikely(managed_dentry_might_block(path
->dentry
)))
931 if (!d_mountpoint(path
->dentry
))
934 mounted
= __lookup_mnt(path
->mnt
, path
->dentry
, 1);
938 path
->dentry
= mounted
->mnt_root
;
939 nd
->seq
= read_seqcount_begin(&path
->dentry
->d_seq
);
941 * Update the inode too. We don't need to re-check the
942 * dentry sequence number here after this d_inode read,
943 * because a mount-point is always pinned.
945 *inode
= path
->dentry
->d_inode
;
950 static void follow_mount_rcu(struct nameidata
*nd
)
952 while (d_mountpoint(nd
->path
.dentry
)) {
953 struct vfsmount
*mounted
;
954 mounted
= __lookup_mnt(nd
->path
.mnt
, nd
->path
.dentry
, 1);
957 nd
->path
.mnt
= mounted
;
958 nd
->path
.dentry
= mounted
->mnt_root
;
959 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
963 static int follow_dotdot_rcu(struct nameidata
*nd
)
968 if (nd
->path
.dentry
== nd
->root
.dentry
&&
969 nd
->path
.mnt
== nd
->root
.mnt
) {
972 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
973 struct dentry
*old
= nd
->path
.dentry
;
974 struct dentry
*parent
= old
->d_parent
;
977 seq
= read_seqcount_begin(&parent
->d_seq
);
978 if (read_seqcount_retry(&old
->d_seq
, nd
->seq
))
980 nd
->path
.dentry
= parent
;
984 if (!follow_up_rcu(&nd
->path
))
986 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
988 follow_mount_rcu(nd
);
989 nd
->inode
= nd
->path
.dentry
->d_inode
;
993 nd
->flags
&= ~LOOKUP_RCU
;
994 if (!(nd
->flags
& LOOKUP_ROOT
))
997 br_read_unlock(vfsmount_lock
);
1002 * Follow down to the covering mount currently visible to userspace. At each
1003 * point, the filesystem owning that dentry may be queried as to whether the
1004 * caller is permitted to proceed or not.
1006 int follow_down(struct path
*path
)
1011 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
1012 unlikely(managed
& DCACHE_MANAGED_DENTRY
)) {
1013 /* Allow the filesystem to manage the transit without i_mutex
1016 * We indicate to the filesystem if someone is trying to mount
1017 * something here. This gives autofs the chance to deny anyone
1018 * other than its daemon the right to mount on its
1021 * The filesystem may sleep at this point.
1023 if (managed
& DCACHE_MANAGE_TRANSIT
) {
1024 BUG_ON(!path
->dentry
->d_op
);
1025 BUG_ON(!path
->dentry
->d_op
->d_manage
);
1026 ret
= path
->dentry
->d_op
->d_manage(
1027 path
->dentry
, false);
1029 return ret
== -EISDIR
? 0 : ret
;
1032 /* Transit to a mounted filesystem. */
1033 if (managed
& DCACHE_MOUNTED
) {
1034 struct vfsmount
*mounted
= lookup_mnt(path
);
1039 path
->mnt
= mounted
;
1040 path
->dentry
= dget(mounted
->mnt_root
);
1044 /* Don't handle automount points here */
1051 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1053 static void follow_mount(struct path
*path
)
1055 while (d_mountpoint(path
->dentry
)) {
1056 struct vfsmount
*mounted
= lookup_mnt(path
);
1061 path
->mnt
= mounted
;
1062 path
->dentry
= dget(mounted
->mnt_root
);
1066 static void follow_dotdot(struct nameidata
*nd
)
1071 struct dentry
*old
= nd
->path
.dentry
;
1073 if (nd
->path
.dentry
== nd
->root
.dentry
&&
1074 nd
->path
.mnt
== nd
->root
.mnt
) {
1077 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
1078 /* rare case of legitimate dget_parent()... */
1079 nd
->path
.dentry
= dget_parent(nd
->path
.dentry
);
1083 if (!follow_up(&nd
->path
))
1086 follow_mount(&nd
->path
);
1087 nd
->inode
= nd
->path
.dentry
->d_inode
;
1091 * Allocate a dentry with name and parent, and perform a parent
1092 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1093 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1094 * have verified that no child exists while under i_mutex.
1096 static struct dentry
*d_alloc_and_lookup(struct dentry
*parent
,
1097 struct qstr
*name
, struct nameidata
*nd
)
1099 struct inode
*inode
= parent
->d_inode
;
1100 struct dentry
*dentry
;
1103 /* Don't create child dentry for a dead directory. */
1104 if (unlikely(IS_DEADDIR(inode
)))
1105 return ERR_PTR(-ENOENT
);
1107 dentry
= d_alloc(parent
, name
);
1108 if (unlikely(!dentry
))
1109 return ERR_PTR(-ENOMEM
);
1111 old
= inode
->i_op
->lookup(inode
, dentry
, nd
);
1112 if (unlikely(old
)) {
1120 * We already have a dentry, but require a lookup to be performed on the parent
1121 * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
1122 * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
1123 * child exists while under i_mutex.
1125 static struct dentry
*d_inode_lookup(struct dentry
*parent
, struct dentry
*dentry
,
1126 struct nameidata
*nd
)
1128 struct inode
*inode
= parent
->d_inode
;
1131 /* Don't create child dentry for a dead directory. */
1132 if (unlikely(IS_DEADDIR(inode
)))
1133 return ERR_PTR(-ENOENT
);
1135 old
= inode
->i_op
->lookup(inode
, dentry
, nd
);
1136 if (unlikely(old
)) {
1144 * It's more convoluted than I'd like it to be, but... it's still fairly
1145 * small and for now I'd prefer to have fast path as straight as possible.
1146 * It _is_ time-critical.
1148 static int do_lookup(struct nameidata
*nd
, struct qstr
*name
,
1149 struct path
*path
, struct inode
**inode
)
1151 struct vfsmount
*mnt
= nd
->path
.mnt
;
1152 struct dentry
*dentry
, *parent
= nd
->path
.dentry
;
1158 * Rename seqlock is not required here because in the off chance
1159 * of a false negative due to a concurrent rename, we're going to
1160 * do the non-racy lookup, below.
1162 if (nd
->flags
& LOOKUP_RCU
) {
1165 dentry
= __d_lookup_rcu(parent
, name
, &seq
, inode
);
1169 /* Memory barrier in read_seqcount_begin of child is enough */
1170 if (__read_seqcount_retry(&parent
->d_seq
, nd
->seq
))
1174 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
)) {
1175 status
= d_revalidate(dentry
, nd
);
1176 if (unlikely(status
<= 0)) {
1177 if (status
!= -ECHILD
)
1182 if (unlikely(d_need_lookup(dentry
)))
1185 path
->dentry
= dentry
;
1186 if (unlikely(!__follow_mount_rcu(nd
, path
, inode
)))
1188 if (unlikely(path
->dentry
->d_flags
& DCACHE_NEED_AUTOMOUNT
))
1192 if (unlazy_walk(nd
, dentry
))
1195 dentry
= __d_lookup(parent
, name
);
1198 if (dentry
&& unlikely(d_need_lookup(dentry
))) {
1203 if (unlikely(!dentry
)) {
1204 struct inode
*dir
= parent
->d_inode
;
1205 BUG_ON(nd
->inode
!= dir
);
1207 mutex_lock(&dir
->i_mutex
);
1208 dentry
= d_lookup(parent
, name
);
1209 if (likely(!dentry
)) {
1210 dentry
= d_alloc_and_lookup(parent
, name
, nd
);
1211 if (IS_ERR(dentry
)) {
1212 mutex_unlock(&dir
->i_mutex
);
1213 return PTR_ERR(dentry
);
1218 } else if (unlikely(d_need_lookup(dentry
))) {
1219 dentry
= d_inode_lookup(parent
, dentry
, nd
);
1220 if (IS_ERR(dentry
)) {
1221 mutex_unlock(&dir
->i_mutex
);
1222 return PTR_ERR(dentry
);
1228 mutex_unlock(&dir
->i_mutex
);
1230 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
) && need_reval
)
1231 status
= d_revalidate(dentry
, nd
);
1232 if (unlikely(status
<= 0)) {
1237 if (!d_invalidate(dentry
)) {
1246 path
->dentry
= dentry
;
1247 err
= follow_managed(path
, nd
->flags
);
1248 if (unlikely(err
< 0)) {
1249 path_put_conditional(path
, nd
);
1252 *inode
= path
->dentry
->d_inode
;
1256 static inline int may_lookup(struct nameidata
*nd
)
1258 if (nd
->flags
& LOOKUP_RCU
) {
1259 int err
= exec_permission(nd
->inode
, IPERM_FLAG_RCU
);
1262 if (unlazy_walk(nd
, NULL
))
1265 return exec_permission(nd
->inode
, 0);
1268 static inline int handle_dots(struct nameidata
*nd
, int type
)
1270 if (type
== LAST_DOTDOT
) {
1271 if (nd
->flags
& LOOKUP_RCU
) {
1272 if (follow_dotdot_rcu(nd
))
1280 static void terminate_walk(struct nameidata
*nd
)
1282 if (!(nd
->flags
& LOOKUP_RCU
)) {
1283 path_put(&nd
->path
);
1285 nd
->flags
&= ~LOOKUP_RCU
;
1286 if (!(nd
->flags
& LOOKUP_ROOT
))
1287 nd
->root
.mnt
= NULL
;
1289 br_read_unlock(vfsmount_lock
);
1293 static inline int walk_component(struct nameidata
*nd
, struct path
*path
,
1294 struct qstr
*name
, int type
, int follow
)
1296 struct inode
*inode
;
1299 * "." and ".." are special - ".." especially so because it has
1300 * to be able to know about the current root directory and
1301 * parent relationships.
1303 if (unlikely(type
!= LAST_NORM
))
1304 return handle_dots(nd
, type
);
1305 err
= do_lookup(nd
, name
, path
, &inode
);
1306 if (unlikely(err
)) {
1311 path_to_nameidata(path
, nd
);
1315 if (unlikely(inode
->i_op
->follow_link
) && follow
) {
1316 if (nd
->flags
& LOOKUP_RCU
) {
1317 if (unlikely(unlazy_walk(nd
, path
->dentry
))) {
1322 BUG_ON(inode
!= path
->dentry
->d_inode
);
1325 path_to_nameidata(path
, nd
);
1331 * This limits recursive symlink follows to 8, while
1332 * limiting consecutive symlinks to 40.
1334 * Without that kind of total limit, nasty chains of consecutive
1335 * symlinks can cause almost arbitrarily long lookups.
1337 static inline int nested_symlink(struct path
*path
, struct nameidata
*nd
)
1341 if (unlikely(current
->link_count
>= MAX_NESTED_LINKS
)) {
1342 path_put_conditional(path
, nd
);
1343 path_put(&nd
->path
);
1346 BUG_ON(nd
->depth
>= MAX_NESTED_LINKS
);
1349 current
->link_count
++;
1352 struct path link
= *path
;
1355 res
= follow_link(&link
, nd
, &cookie
);
1357 res
= walk_component(nd
, path
, &nd
->last
,
1358 nd
->last_type
, LOOKUP_FOLLOW
);
1359 put_link(nd
, &link
, cookie
);
1362 current
->link_count
--;
1369 * This is the basic name resolution function, turning a pathname into
1370 * the final dentry. We expect 'base' to be positive and a directory.
1372 * Returns 0 and nd will have valid dentry and mnt on success.
1373 * Returns error and drops reference to input namei data on failure.
1375 static int link_path_walk(const char *name
, struct nameidata
*nd
)
1379 unsigned int lookup_flags
= nd
->flags
;
1386 /* At this point we know we have a real path component. */
1393 nd
->flags
|= LOOKUP_CONTINUE
;
1395 err
= may_lookup(nd
);
1400 c
= *(const unsigned char *)name
;
1402 hash
= init_name_hash();
1405 hash
= partial_name_hash(c
, hash
);
1406 c
= *(const unsigned char *)name
;
1407 } while (c
&& (c
!= '/'));
1408 this.len
= name
- (const char *) this.name
;
1409 this.hash
= end_name_hash(hash
);
1412 if (this.name
[0] == '.') switch (this.len
) {
1414 if (this.name
[1] == '.') {
1416 nd
->flags
|= LOOKUP_JUMPED
;
1422 if (likely(type
== LAST_NORM
)) {
1423 struct dentry
*parent
= nd
->path
.dentry
;
1424 nd
->flags
&= ~LOOKUP_JUMPED
;
1425 if (unlikely(parent
->d_flags
& DCACHE_OP_HASH
)) {
1426 err
= parent
->d_op
->d_hash(parent
, nd
->inode
,
1433 /* remove trailing slashes? */
1435 goto last_component
;
1436 while (*++name
== '/');
1438 goto last_component
;
1440 err
= walk_component(nd
, &next
, &this, type
, LOOKUP_FOLLOW
);
1445 err
= nested_symlink(&next
, nd
);
1450 if (!nd
->inode
->i_op
->lookup
)
1453 /* here ends the main loop */
1456 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1457 nd
->flags
&= lookup_flags
| ~LOOKUP_CONTINUE
;
1459 nd
->last_type
= type
;
1466 static int path_init(int dfd
, const char *name
, unsigned int flags
,
1467 struct nameidata
*nd
, struct file
**fp
)
1473 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
1474 nd
->flags
= flags
| LOOKUP_JUMPED
;
1476 if (flags
& LOOKUP_ROOT
) {
1477 struct inode
*inode
= nd
->root
.dentry
->d_inode
;
1479 if (!inode
->i_op
->lookup
)
1481 retval
= inode_permission(inode
, MAY_EXEC
);
1485 nd
->path
= nd
->root
;
1487 if (flags
& LOOKUP_RCU
) {
1488 br_read_lock(vfsmount_lock
);
1490 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1492 path_get(&nd
->path
);
1497 nd
->root
.mnt
= NULL
;
1500 if (flags
& LOOKUP_RCU
) {
1501 br_read_lock(vfsmount_lock
);
1506 path_get(&nd
->root
);
1508 nd
->path
= nd
->root
;
1509 } else if (dfd
== AT_FDCWD
) {
1510 if (flags
& LOOKUP_RCU
) {
1511 struct fs_struct
*fs
= current
->fs
;
1514 br_read_lock(vfsmount_lock
);
1518 seq
= read_seqcount_begin(&fs
->seq
);
1520 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1521 } while (read_seqcount_retry(&fs
->seq
, seq
));
1523 get_fs_pwd(current
->fs
, &nd
->path
);
1526 struct dentry
*dentry
;
1528 file
= fget_raw_light(dfd
, &fput_needed
);
1533 dentry
= file
->f_path
.dentry
;
1537 if (!S_ISDIR(dentry
->d_inode
->i_mode
))
1540 retval
= exec_permission(dentry
->d_inode
, 0);
1545 nd
->path
= file
->f_path
;
1546 if (flags
& LOOKUP_RCU
) {
1549 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1550 br_read_lock(vfsmount_lock
);
1553 path_get(&file
->f_path
);
1554 fput_light(file
, fput_needed
);
1558 nd
->inode
= nd
->path
.dentry
->d_inode
;
1562 fput_light(file
, fput_needed
);
1567 static inline int lookup_last(struct nameidata
*nd
, struct path
*path
)
1569 if (nd
->last_type
== LAST_NORM
&& nd
->last
.name
[nd
->last
.len
])
1570 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
1572 nd
->flags
&= ~LOOKUP_PARENT
;
1573 return walk_component(nd
, path
, &nd
->last
, nd
->last_type
,
1574 nd
->flags
& LOOKUP_FOLLOW
);
1577 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1578 static int path_lookupat(int dfd
, const char *name
,
1579 unsigned int flags
, struct nameidata
*nd
)
1581 struct file
*base
= NULL
;
1586 * Path walking is largely split up into 2 different synchronisation
1587 * schemes, rcu-walk and ref-walk (explained in
1588 * Documentation/filesystems/path-lookup.txt). These share much of the
1589 * path walk code, but some things particularly setup, cleanup, and
1590 * following mounts are sufficiently divergent that functions are
1591 * duplicated. Typically there is a function foo(), and its RCU
1592 * analogue, foo_rcu().
1594 * -ECHILD is the error number of choice (just to avoid clashes) that
1595 * is returned if some aspect of an rcu-walk fails. Such an error must
1596 * be handled by restarting a traditional ref-walk (which will always
1597 * be able to complete).
1599 err
= path_init(dfd
, name
, flags
| LOOKUP_PARENT
, nd
, &base
);
1604 current
->total_link_count
= 0;
1605 err
= link_path_walk(name
, nd
);
1607 if (!err
&& !(flags
& LOOKUP_PARENT
)) {
1608 err
= lookup_last(nd
, &path
);
1611 struct path link
= path
;
1612 nd
->flags
|= LOOKUP_PARENT
;
1613 err
= follow_link(&link
, nd
, &cookie
);
1615 err
= lookup_last(nd
, &path
);
1616 put_link(nd
, &link
, cookie
);
1621 err
= complete_walk(nd
);
1623 if (!err
&& nd
->flags
& LOOKUP_DIRECTORY
) {
1624 if (!nd
->inode
->i_op
->lookup
) {
1625 path_put(&nd
->path
);
1633 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
1634 path_put(&nd
->root
);
1635 nd
->root
.mnt
= NULL
;
1640 static int do_path_lookup(int dfd
, const char *name
,
1641 unsigned int flags
, struct nameidata
*nd
)
1643 int retval
= path_lookupat(dfd
, name
, flags
| LOOKUP_RCU
, nd
);
1644 if (unlikely(retval
== -ECHILD
))
1645 retval
= path_lookupat(dfd
, name
, flags
, nd
);
1646 if (unlikely(retval
== -ESTALE
))
1647 retval
= path_lookupat(dfd
, name
, flags
| LOOKUP_REVAL
, nd
);
1649 if (likely(!retval
)) {
1650 if (unlikely(!audit_dummy_context())) {
1651 if (nd
->path
.dentry
&& nd
->inode
)
1652 audit_inode(name
, nd
->path
.dentry
);
1658 int kern_path_parent(const char *name
, struct nameidata
*nd
)
1660 return do_path_lookup(AT_FDCWD
, name
, LOOKUP_PARENT
, nd
);
1663 int kern_path(const char *name
, unsigned int flags
, struct path
*path
)
1665 struct nameidata nd
;
1666 int res
= do_path_lookup(AT_FDCWD
, name
, flags
, &nd
);
1673 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1674 * @dentry: pointer to dentry of the base directory
1675 * @mnt: pointer to vfs mount of the base directory
1676 * @name: pointer to file name
1677 * @flags: lookup flags
1678 * @nd: pointer to nameidata
1680 int vfs_path_lookup(struct dentry
*dentry
, struct vfsmount
*mnt
,
1681 const char *name
, unsigned int flags
,
1682 struct nameidata
*nd
)
1684 nd
->root
.dentry
= dentry
;
1686 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1687 return do_path_lookup(AT_FDCWD
, name
, flags
| LOOKUP_ROOT
, nd
);
1690 static struct dentry
*__lookup_hash(struct qstr
*name
,
1691 struct dentry
*base
, struct nameidata
*nd
)
1693 struct inode
*inode
= base
->d_inode
;
1694 struct dentry
*dentry
;
1697 err
= exec_permission(inode
, 0);
1699 return ERR_PTR(err
);
1702 * Don't bother with __d_lookup: callers are for creat as
1703 * well as unlink, so a lot of the time it would cost
1706 dentry
= d_lookup(base
, name
);
1708 if (dentry
&& d_need_lookup(dentry
)) {
1710 * __lookup_hash is called with the parent dir's i_mutex already
1711 * held, so we are good to go here.
1713 dentry
= d_inode_lookup(base
, dentry
, nd
);
1718 if (dentry
&& (dentry
->d_flags
& DCACHE_OP_REVALIDATE
))
1719 dentry
= do_revalidate(dentry
, nd
);
1722 dentry
= d_alloc_and_lookup(base
, name
, nd
);
1728 * Restricted form of lookup. Doesn't follow links, single-component only,
1729 * needs parent already locked. Doesn't follow mounts.
1732 static struct dentry
*lookup_hash(struct nameidata
*nd
)
1734 return __lookup_hash(&nd
->last
, nd
->path
.dentry
, nd
);
1738 * lookup_one_len - filesystem helper to lookup single pathname component
1739 * @name: pathname component to lookup
1740 * @base: base directory to lookup from
1741 * @len: maximum length @len should be interpreted to
1743 * Note that this routine is purely a helper for filesystem usage and should
1744 * not be called by generic code. Also note that by using this function the
1745 * nameidata argument is passed to the filesystem methods and a filesystem
1746 * using this helper needs to be prepared for that.
1748 struct dentry
*lookup_one_len(const char *name
, struct dentry
*base
, int len
)
1754 WARN_ON_ONCE(!mutex_is_locked(&base
->d_inode
->i_mutex
));
1759 return ERR_PTR(-EACCES
);
1761 hash
= init_name_hash();
1763 c
= *(const unsigned char *)name
++;
1764 if (c
== '/' || c
== '\0')
1765 return ERR_PTR(-EACCES
);
1766 hash
= partial_name_hash(c
, hash
);
1768 this.hash
= end_name_hash(hash
);
1770 * See if the low-level filesystem might want
1771 * to use its own hash..
1773 if (base
->d_flags
& DCACHE_OP_HASH
) {
1774 int err
= base
->d_op
->d_hash(base
, base
->d_inode
, &this);
1776 return ERR_PTR(err
);
1779 return __lookup_hash(&this, base
, NULL
);
1782 int user_path_at(int dfd
, const char __user
*name
, unsigned flags
,
1785 struct nameidata nd
;
1786 char *tmp
= getname_flags(name
, flags
);
1787 int err
= PTR_ERR(tmp
);
1790 BUG_ON(flags
& LOOKUP_PARENT
);
1792 err
= do_path_lookup(dfd
, tmp
, flags
, &nd
);
1800 static int user_path_parent(int dfd
, const char __user
*path
,
1801 struct nameidata
*nd
, char **name
)
1803 char *s
= getname(path
);
1809 error
= do_path_lookup(dfd
, s
, LOOKUP_PARENT
, nd
);
1819 * It's inline, so penalty for filesystems that don't use sticky bit is
1822 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
1824 uid_t fsuid
= current_fsuid();
1826 if (!(dir
->i_mode
& S_ISVTX
))
1828 if (current_user_ns() != inode_userns(inode
))
1830 if (inode
->i_uid
== fsuid
)
1832 if (dir
->i_uid
== fsuid
)
1836 return !ns_capable(inode_userns(inode
), CAP_FOWNER
);
1840 * Check whether we can remove a link victim from directory dir, check
1841 * whether the type of victim is right.
1842 * 1. We can't do it if dir is read-only (done in permission())
1843 * 2. We should have write and exec permissions on dir
1844 * 3. We can't remove anything from append-only dir
1845 * 4. We can't do anything with immutable dir (done in permission())
1846 * 5. If the sticky bit on dir is set we should either
1847 * a. be owner of dir, or
1848 * b. be owner of victim, or
1849 * c. have CAP_FOWNER capability
1850 * 6. If the victim is append-only or immutable we can't do antyhing with
1851 * links pointing to it.
1852 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1853 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1854 * 9. We can't remove a root or mountpoint.
1855 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1856 * nfs_async_unlink().
1858 static int may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
1862 if (!victim
->d_inode
)
1865 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
1866 audit_inode_child(victim
, dir
);
1868 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
1873 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
1874 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
1877 if (!S_ISDIR(victim
->d_inode
->i_mode
))
1879 if (IS_ROOT(victim
))
1881 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
1883 if (IS_DEADDIR(dir
))
1885 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
1890 /* Check whether we can create an object with dentry child in directory
1892 * 1. We can't do it if child already exists (open has special treatment for
1893 * this case, but since we are inlined it's OK)
1894 * 2. We can't do it if dir is read-only (done in permission())
1895 * 3. We should have write and exec permissions on dir
1896 * 4. We can't do it if dir is immutable (done in permission())
1898 static inline int may_create(struct inode
*dir
, struct dentry
*child
)
1902 if (IS_DEADDIR(dir
))
1904 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
1908 * p1 and p2 should be directories on the same fs.
1910 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
1915 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1919 mutex_lock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
1921 p
= d_ancestor(p2
, p1
);
1923 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1924 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1928 p
= d_ancestor(p1
, p2
);
1930 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1931 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1935 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1936 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1940 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
1942 mutex_unlock(&p1
->d_inode
->i_mutex
);
1944 mutex_unlock(&p2
->d_inode
->i_mutex
);
1945 mutex_unlock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
1949 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
1950 struct nameidata
*nd
)
1952 int error
= may_create(dir
, dentry
);
1957 if (!dir
->i_op
->create
)
1958 return -EACCES
; /* shouldn't it be ENOSYS? */
1961 error
= security_inode_create(dir
, dentry
, mode
);
1964 error
= dir
->i_op
->create(dir
, dentry
, mode
, nd
);
1966 fsnotify_create(dir
, dentry
);
1970 static int may_open(struct path
*path
, int acc_mode
, int flag
)
1972 struct dentry
*dentry
= path
->dentry
;
1973 struct inode
*inode
= dentry
->d_inode
;
1983 switch (inode
->i_mode
& S_IFMT
) {
1987 if (acc_mode
& MAY_WRITE
)
1992 if (path
->mnt
->mnt_flags
& MNT_NODEV
)
2001 error
= inode_permission(inode
, acc_mode
);
2006 * An append-only file must be opened in append mode for writing.
2008 if (IS_APPEND(inode
)) {
2009 if ((flag
& O_ACCMODE
) != O_RDONLY
&& !(flag
& O_APPEND
))
2015 /* O_NOATIME can only be set by the owner or superuser */
2016 if (flag
& O_NOATIME
&& !inode_owner_or_capable(inode
))
2020 * Ensure there are no outstanding leases on the file.
2022 return break_lease(inode
, flag
);
2025 static int handle_truncate(struct file
*filp
)
2027 struct path
*path
= &filp
->f_path
;
2028 struct inode
*inode
= path
->dentry
->d_inode
;
2029 int error
= get_write_access(inode
);
2033 * Refuse to truncate files with mandatory locks held on them.
2035 error
= locks_verify_locked(inode
);
2037 error
= security_path_truncate(path
);
2039 error
= do_truncate(path
->dentry
, 0,
2040 ATTR_MTIME
|ATTR_CTIME
|ATTR_OPEN
,
2043 put_write_access(inode
);
2048 * Note that while the flag value (low two bits) for sys_open means:
2053 * it is changed into
2054 * 00 - no permissions needed
2055 * 01 - read-permission
2056 * 10 - write-permission
2058 * for the internal routines (ie open_namei()/follow_link() etc)
2059 * This is more logical, and also allows the 00 "no perm needed"
2060 * to be used for symlinks (where the permissions are checked
2064 static inline int open_to_namei_flags(int flag
)
2066 if ((flag
+1) & O_ACCMODE
)
2072 * Handle the last step of open()
2074 static struct file
*do_last(struct nameidata
*nd
, struct path
*path
,
2075 const struct open_flags
*op
, const char *pathname
)
2077 struct dentry
*dir
= nd
->path
.dentry
;
2078 struct dentry
*dentry
;
2079 int open_flag
= op
->open_flag
;
2080 int will_truncate
= open_flag
& O_TRUNC
;
2082 int acc_mode
= op
->acc_mode
;
2086 nd
->flags
&= ~LOOKUP_PARENT
;
2087 nd
->flags
|= op
->intent
;
2089 switch (nd
->last_type
) {
2092 error
= handle_dots(nd
, nd
->last_type
);
2094 return ERR_PTR(error
);
2097 error
= complete_walk(nd
);
2099 return ERR_PTR(error
);
2100 audit_inode(pathname
, nd
->path
.dentry
);
2101 if (open_flag
& O_CREAT
) {
2107 error
= complete_walk(nd
);
2109 return ERR_PTR(error
);
2110 audit_inode(pathname
, dir
);
2114 if (!(open_flag
& O_CREAT
)) {
2116 if (nd
->last
.name
[nd
->last
.len
])
2117 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
2118 if (open_flag
& O_PATH
&& !(nd
->flags
& LOOKUP_FOLLOW
))
2120 /* we _can_ be in RCU mode here */
2121 error
= walk_component(nd
, path
, &nd
->last
, LAST_NORM
,
2124 return ERR_PTR(error
);
2125 if (error
) /* symlink */
2128 error
= complete_walk(nd
);
2130 return ERR_PTR(-ECHILD
);
2133 if (nd
->flags
& LOOKUP_DIRECTORY
) {
2134 if (!nd
->inode
->i_op
->lookup
)
2137 audit_inode(pathname
, nd
->path
.dentry
);
2141 /* create side of things */
2142 error
= complete_walk(nd
);
2144 return ERR_PTR(error
);
2146 audit_inode(pathname
, dir
);
2148 /* trailing slashes? */
2149 if (nd
->last
.name
[nd
->last
.len
])
2152 mutex_lock(&dir
->d_inode
->i_mutex
);
2154 dentry
= lookup_hash(nd
);
2155 error
= PTR_ERR(dentry
);
2156 if (IS_ERR(dentry
)) {
2157 mutex_unlock(&dir
->d_inode
->i_mutex
);
2161 path
->dentry
= dentry
;
2162 path
->mnt
= nd
->path
.mnt
;
2164 /* Negative dentry, just create the file */
2165 if (!dentry
->d_inode
) {
2166 int mode
= op
->mode
;
2167 if (!IS_POSIXACL(dir
->d_inode
))
2168 mode
&= ~current_umask();
2170 * This write is needed to ensure that a
2171 * rw->ro transition does not occur between
2172 * the time when the file is created and when
2173 * a permanent write count is taken through
2174 * the 'struct file' in nameidata_to_filp().
2176 error
= mnt_want_write(nd
->path
.mnt
);
2178 goto exit_mutex_unlock
;
2180 /* Don't check for write permission, don't truncate */
2181 open_flag
&= ~O_TRUNC
;
2183 acc_mode
= MAY_OPEN
;
2184 error
= security_path_mknod(&nd
->path
, dentry
, mode
, 0);
2186 goto exit_mutex_unlock
;
2187 error
= vfs_create(dir
->d_inode
, dentry
, mode
, nd
);
2189 goto exit_mutex_unlock
;
2190 mutex_unlock(&dir
->d_inode
->i_mutex
);
2191 dput(nd
->path
.dentry
);
2192 nd
->path
.dentry
= dentry
;
2197 * It already exists.
2199 mutex_unlock(&dir
->d_inode
->i_mutex
);
2200 audit_inode(pathname
, path
->dentry
);
2203 if (open_flag
& O_EXCL
)
2206 error
= follow_managed(path
, nd
->flags
);
2211 if (!path
->dentry
->d_inode
)
2214 if (path
->dentry
->d_inode
->i_op
->follow_link
)
2217 path_to_nameidata(path
, nd
);
2218 nd
->inode
= path
->dentry
->d_inode
;
2220 if (S_ISDIR(nd
->inode
->i_mode
))
2223 if (!S_ISREG(nd
->inode
->i_mode
))
2226 if (will_truncate
) {
2227 error
= mnt_want_write(nd
->path
.mnt
);
2233 error
= may_open(&nd
->path
, acc_mode
, open_flag
);
2236 filp
= nameidata_to_filp(nd
);
2237 if (!IS_ERR(filp
)) {
2238 error
= ima_file_check(filp
, op
->acc_mode
);
2241 filp
= ERR_PTR(error
);
2244 if (!IS_ERR(filp
)) {
2245 if (will_truncate
) {
2246 error
= handle_truncate(filp
);
2249 filp
= ERR_PTR(error
);
2255 mnt_drop_write(nd
->path
.mnt
);
2256 path_put(&nd
->path
);
2260 mutex_unlock(&dir
->d_inode
->i_mutex
);
2262 path_put_conditional(path
, nd
);
2264 filp
= ERR_PTR(error
);
2268 static struct file
*path_openat(int dfd
, const char *pathname
,
2269 struct nameidata
*nd
, const struct open_flags
*op
, int flags
)
2271 struct file
*base
= NULL
;
2276 filp
= get_empty_filp();
2278 return ERR_PTR(-ENFILE
);
2280 filp
->f_flags
= op
->open_flag
;
2281 nd
->intent
.open
.file
= filp
;
2282 nd
->intent
.open
.flags
= open_to_namei_flags(op
->open_flag
);
2283 nd
->intent
.open
.create_mode
= op
->mode
;
2285 error
= path_init(dfd
, pathname
, flags
| LOOKUP_PARENT
, nd
, &base
);
2286 if (unlikely(error
))
2289 current
->total_link_count
= 0;
2290 error
= link_path_walk(pathname
, nd
);
2291 if (unlikely(error
))
2294 filp
= do_last(nd
, &path
, op
, pathname
);
2295 while (unlikely(!filp
)) { /* trailing symlink */
2296 struct path link
= path
;
2298 if (!(nd
->flags
& LOOKUP_FOLLOW
)) {
2299 path_put_conditional(&path
, nd
);
2300 path_put(&nd
->path
);
2301 filp
= ERR_PTR(-ELOOP
);
2304 nd
->flags
|= LOOKUP_PARENT
;
2305 nd
->flags
&= ~(LOOKUP_OPEN
|LOOKUP_CREATE
|LOOKUP_EXCL
);
2306 error
= follow_link(&link
, nd
, &cookie
);
2307 if (unlikely(error
))
2308 filp
= ERR_PTR(error
);
2310 filp
= do_last(nd
, &path
, op
, pathname
);
2311 put_link(nd
, &link
, cookie
);
2314 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
))
2315 path_put(&nd
->root
);
2318 release_open_intent(nd
);
2322 filp
= ERR_PTR(error
);
2326 struct file
*do_filp_open(int dfd
, const char *pathname
,
2327 const struct open_flags
*op
, int flags
)
2329 struct nameidata nd
;
2332 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
| LOOKUP_RCU
);
2333 if (unlikely(filp
== ERR_PTR(-ECHILD
)))
2334 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
);
2335 if (unlikely(filp
== ERR_PTR(-ESTALE
)))
2336 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
| LOOKUP_REVAL
);
2340 struct file
*do_file_open_root(struct dentry
*dentry
, struct vfsmount
*mnt
,
2341 const char *name
, const struct open_flags
*op
, int flags
)
2343 struct nameidata nd
;
2347 nd
.root
.dentry
= dentry
;
2349 flags
|= LOOKUP_ROOT
;
2351 if (dentry
->d_inode
->i_op
->follow_link
&& op
->intent
& LOOKUP_OPEN
)
2352 return ERR_PTR(-ELOOP
);
2354 file
= path_openat(-1, name
, &nd
, op
, flags
| LOOKUP_RCU
);
2355 if (unlikely(file
== ERR_PTR(-ECHILD
)))
2356 file
= path_openat(-1, name
, &nd
, op
, flags
);
2357 if (unlikely(file
== ERR_PTR(-ESTALE
)))
2358 file
= path_openat(-1, name
, &nd
, op
, flags
| LOOKUP_REVAL
);
2363 * lookup_create - lookup a dentry, creating it if it doesn't exist
2364 * @nd: nameidata info
2365 * @is_dir: directory flag
2367 * Simple function to lookup and return a dentry and create it
2368 * if it doesn't exist. Is SMP-safe.
2370 * Returns with nd->path.dentry->d_inode->i_mutex locked.
2372 struct dentry
*lookup_create(struct nameidata
*nd
, int is_dir
)
2374 struct dentry
*dentry
= ERR_PTR(-EEXIST
);
2376 mutex_lock_nested(&nd
->path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2378 * Yucky last component or no last component at all?
2379 * (foo/., foo/.., /////)
2381 if (nd
->last_type
!= LAST_NORM
)
2383 nd
->flags
&= ~LOOKUP_PARENT
;
2384 nd
->flags
|= LOOKUP_CREATE
| LOOKUP_EXCL
;
2385 nd
->intent
.open
.flags
= O_EXCL
;
2388 * Do the final lookup.
2390 dentry
= lookup_hash(nd
);
2394 if (dentry
->d_inode
)
2397 * Special case - lookup gave negative, but... we had foo/bar/
2398 * From the vfs_mknod() POV we just have a negative dentry -
2399 * all is fine. Let's be bastards - you had / on the end, you've
2400 * been asking for (non-existent) directory. -ENOENT for you.
2402 if (unlikely(!is_dir
&& nd
->last
.name
[nd
->last
.len
])) {
2404 dentry
= ERR_PTR(-ENOENT
);
2409 dentry
= ERR_PTR(-EEXIST
);
2413 EXPORT_SYMBOL_GPL(lookup_create
);
2415 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
2417 int error
= may_create(dir
, dentry
);
2422 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) &&
2423 !ns_capable(inode_userns(dir
), CAP_MKNOD
))
2426 if (!dir
->i_op
->mknod
)
2429 error
= devcgroup_inode_mknod(mode
, dev
);
2433 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
2437 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
2439 fsnotify_create(dir
, dentry
);
2443 static int may_mknod(mode_t mode
)
2445 switch (mode
& S_IFMT
) {
2451 case 0: /* zero mode translates to S_IFREG */
2460 SYSCALL_DEFINE4(mknodat
, int, dfd
, const char __user
*, filename
, int, mode
,
2465 struct dentry
*dentry
;
2466 struct nameidata nd
;
2471 error
= user_path_parent(dfd
, filename
, &nd
, &tmp
);
2475 dentry
= lookup_create(&nd
, 0);
2476 if (IS_ERR(dentry
)) {
2477 error
= PTR_ERR(dentry
);
2480 if (!IS_POSIXACL(nd
.path
.dentry
->d_inode
))
2481 mode
&= ~current_umask();
2482 error
= may_mknod(mode
);
2485 error
= mnt_want_write(nd
.path
.mnt
);
2488 error
= security_path_mknod(&nd
.path
, dentry
, mode
, dev
);
2490 goto out_drop_write
;
2491 switch (mode
& S_IFMT
) {
2492 case 0: case S_IFREG
:
2493 error
= vfs_create(nd
.path
.dentry
->d_inode
,dentry
,mode
,&nd
);
2495 case S_IFCHR
: case S_IFBLK
:
2496 error
= vfs_mknod(nd
.path
.dentry
->d_inode
,dentry
,mode
,
2497 new_decode_dev(dev
));
2499 case S_IFIFO
: case S_IFSOCK
:
2500 error
= vfs_mknod(nd
.path
.dentry
->d_inode
,dentry
,mode
,0);
2504 mnt_drop_write(nd
.path
.mnt
);
2508 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2515 SYSCALL_DEFINE3(mknod
, const char __user
*, filename
, int, mode
, unsigned, dev
)
2517 return sys_mknodat(AT_FDCWD
, filename
, mode
, dev
);
2520 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2522 int error
= may_create(dir
, dentry
);
2527 if (!dir
->i_op
->mkdir
)
2530 mode
&= (S_IRWXUGO
|S_ISVTX
);
2531 error
= security_inode_mkdir(dir
, dentry
, mode
);
2535 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
2537 fsnotify_mkdir(dir
, dentry
);
2541 SYSCALL_DEFINE3(mkdirat
, int, dfd
, const char __user
*, pathname
, int, mode
)
2545 struct dentry
*dentry
;
2546 struct nameidata nd
;
2548 error
= user_path_parent(dfd
, pathname
, &nd
, &tmp
);
2552 dentry
= lookup_create(&nd
, 1);
2553 error
= PTR_ERR(dentry
);
2557 if (!IS_POSIXACL(nd
.path
.dentry
->d_inode
))
2558 mode
&= ~current_umask();
2559 error
= mnt_want_write(nd
.path
.mnt
);
2562 error
= security_path_mkdir(&nd
.path
, dentry
, mode
);
2564 goto out_drop_write
;
2565 error
= vfs_mkdir(nd
.path
.dentry
->d_inode
, dentry
, mode
);
2567 mnt_drop_write(nd
.path
.mnt
);
2571 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2578 SYSCALL_DEFINE2(mkdir
, const char __user
*, pathname
, int, mode
)
2580 return sys_mkdirat(AT_FDCWD
, pathname
, mode
);
2584 * The dentry_unhash() helper will try to drop the dentry early: we
2585 * should have a usage count of 2 if we're the only user of this
2586 * dentry, and if that is true (possibly after pruning the dcache),
2587 * then we drop the dentry now.
2589 * A low-level filesystem can, if it choses, legally
2592 * if (!d_unhashed(dentry))
2595 * if it cannot handle the case of removing a directory
2596 * that is still in use by something else..
2598 void dentry_unhash(struct dentry
*dentry
)
2600 shrink_dcache_parent(dentry
);
2601 spin_lock(&dentry
->d_lock
);
2602 if (dentry
->d_count
== 1)
2604 spin_unlock(&dentry
->d_lock
);
2607 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2609 int error
= may_delete(dir
, dentry
, 1);
2614 if (!dir
->i_op
->rmdir
)
2617 mutex_lock(&dentry
->d_inode
->i_mutex
);
2620 if (d_mountpoint(dentry
))
2623 error
= security_inode_rmdir(dir
, dentry
);
2627 shrink_dcache_parent(dentry
);
2628 error
= dir
->i_op
->rmdir(dir
, dentry
);
2632 dentry
->d_inode
->i_flags
|= S_DEAD
;
2636 mutex_unlock(&dentry
->d_inode
->i_mutex
);
2642 static long do_rmdir(int dfd
, const char __user
*pathname
)
2646 struct dentry
*dentry
;
2647 struct nameidata nd
;
2649 error
= user_path_parent(dfd
, pathname
, &nd
, &name
);
2653 switch(nd
.last_type
) {
2665 nd
.flags
&= ~LOOKUP_PARENT
;
2667 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2668 dentry
= lookup_hash(&nd
);
2669 error
= PTR_ERR(dentry
);
2672 if (!dentry
->d_inode
) {
2676 error
= mnt_want_write(nd
.path
.mnt
);
2679 error
= security_path_rmdir(&nd
.path
, dentry
);
2682 error
= vfs_rmdir(nd
.path
.dentry
->d_inode
, dentry
);
2684 mnt_drop_write(nd
.path
.mnt
);
2688 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2695 SYSCALL_DEFINE1(rmdir
, const char __user
*, pathname
)
2697 return do_rmdir(AT_FDCWD
, pathname
);
2700 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
2702 int error
= may_delete(dir
, dentry
, 0);
2707 if (!dir
->i_op
->unlink
)
2710 mutex_lock(&dentry
->d_inode
->i_mutex
);
2711 if (d_mountpoint(dentry
))
2714 error
= security_inode_unlink(dir
, dentry
);
2716 error
= dir
->i_op
->unlink(dir
, dentry
);
2721 mutex_unlock(&dentry
->d_inode
->i_mutex
);
2723 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2724 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
2725 fsnotify_link_count(dentry
->d_inode
);
2733 * Make sure that the actual truncation of the file will occur outside its
2734 * directory's i_mutex. Truncate can take a long time if there is a lot of
2735 * writeout happening, and we don't want to prevent access to the directory
2736 * while waiting on the I/O.
2738 static long do_unlinkat(int dfd
, const char __user
*pathname
)
2742 struct dentry
*dentry
;
2743 struct nameidata nd
;
2744 struct inode
*inode
= NULL
;
2746 error
= user_path_parent(dfd
, pathname
, &nd
, &name
);
2751 if (nd
.last_type
!= LAST_NORM
)
2754 nd
.flags
&= ~LOOKUP_PARENT
;
2756 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2757 dentry
= lookup_hash(&nd
);
2758 error
= PTR_ERR(dentry
);
2759 if (!IS_ERR(dentry
)) {
2760 /* Why not before? Because we want correct error value */
2761 if (nd
.last
.name
[nd
.last
.len
])
2763 inode
= dentry
->d_inode
;
2767 error
= mnt_want_write(nd
.path
.mnt
);
2770 error
= security_path_unlink(&nd
.path
, dentry
);
2773 error
= vfs_unlink(nd
.path
.dentry
->d_inode
, dentry
);
2775 mnt_drop_write(nd
.path
.mnt
);
2779 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2781 iput(inode
); /* truncate the inode here */
2788 error
= !dentry
->d_inode
? -ENOENT
:
2789 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
2793 SYSCALL_DEFINE3(unlinkat
, int, dfd
, const char __user
*, pathname
, int, flag
)
2795 if ((flag
& ~AT_REMOVEDIR
) != 0)
2798 if (flag
& AT_REMOVEDIR
)
2799 return do_rmdir(dfd
, pathname
);
2801 return do_unlinkat(dfd
, pathname
);
2804 SYSCALL_DEFINE1(unlink
, const char __user
*, pathname
)
2806 return do_unlinkat(AT_FDCWD
, pathname
);
2809 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
2811 int error
= may_create(dir
, dentry
);
2816 if (!dir
->i_op
->symlink
)
2819 error
= security_inode_symlink(dir
, dentry
, oldname
);
2823 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
2825 fsnotify_create(dir
, dentry
);
2829 SYSCALL_DEFINE3(symlinkat
, const char __user
*, oldname
,
2830 int, newdfd
, const char __user
*, newname
)
2835 struct dentry
*dentry
;
2836 struct nameidata nd
;
2838 from
= getname(oldname
);
2840 return PTR_ERR(from
);
2842 error
= user_path_parent(newdfd
, newname
, &nd
, &to
);
2846 dentry
= lookup_create(&nd
, 0);
2847 error
= PTR_ERR(dentry
);
2851 error
= mnt_want_write(nd
.path
.mnt
);
2854 error
= security_path_symlink(&nd
.path
, dentry
, from
);
2856 goto out_drop_write
;
2857 error
= vfs_symlink(nd
.path
.dentry
->d_inode
, dentry
, from
);
2859 mnt_drop_write(nd
.path
.mnt
);
2863 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2871 SYSCALL_DEFINE2(symlink
, const char __user
*, oldname
, const char __user
*, newname
)
2873 return sys_symlinkat(oldname
, AT_FDCWD
, newname
);
2876 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
2878 struct inode
*inode
= old_dentry
->d_inode
;
2884 error
= may_create(dir
, new_dentry
);
2888 if (dir
->i_sb
!= inode
->i_sb
)
2892 * A link to an append-only or immutable file cannot be created.
2894 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2896 if (!dir
->i_op
->link
)
2898 if (S_ISDIR(inode
->i_mode
))
2901 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
2905 mutex_lock(&inode
->i_mutex
);
2906 /* Make sure we don't allow creating hardlink to an unlinked file */
2907 if (inode
->i_nlink
== 0)
2910 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
2911 mutex_unlock(&inode
->i_mutex
);
2913 fsnotify_link(dir
, inode
, new_dentry
);
2918 * Hardlinks are often used in delicate situations. We avoid
2919 * security-related surprises by not following symlinks on the
2922 * We don't follow them on the oldname either to be compatible
2923 * with linux 2.0, and to avoid hard-linking to directories
2924 * and other special files. --ADM
2926 SYSCALL_DEFINE5(linkat
, int, olddfd
, const char __user
*, oldname
,
2927 int, newdfd
, const char __user
*, newname
, int, flags
)
2929 struct dentry
*new_dentry
;
2930 struct nameidata nd
;
2931 struct path old_path
;
2936 if ((flags
& ~(AT_SYMLINK_FOLLOW
| AT_EMPTY_PATH
)) != 0)
2939 * To use null names we require CAP_DAC_READ_SEARCH
2940 * This ensures that not everyone will be able to create
2941 * handlink using the passed filedescriptor.
2943 if (flags
& AT_EMPTY_PATH
) {
2944 if (!capable(CAP_DAC_READ_SEARCH
))
2949 if (flags
& AT_SYMLINK_FOLLOW
)
2950 how
|= LOOKUP_FOLLOW
;
2952 error
= user_path_at(olddfd
, oldname
, how
, &old_path
);
2956 error
= user_path_parent(newdfd
, newname
, &nd
, &to
);
2960 if (old_path
.mnt
!= nd
.path
.mnt
)
2962 new_dentry
= lookup_create(&nd
, 0);
2963 error
= PTR_ERR(new_dentry
);
2964 if (IS_ERR(new_dentry
))
2966 error
= mnt_want_write(nd
.path
.mnt
);
2969 error
= security_path_link(old_path
.dentry
, &nd
.path
, new_dentry
);
2971 goto out_drop_write
;
2972 error
= vfs_link(old_path
.dentry
, nd
.path
.dentry
->d_inode
, new_dentry
);
2974 mnt_drop_write(nd
.path
.mnt
);
2978 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2983 path_put(&old_path
);
2988 SYSCALL_DEFINE2(link
, const char __user
*, oldname
, const char __user
*, newname
)
2990 return sys_linkat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
, 0);
2994 * The worst of all namespace operations - renaming directory. "Perverted"
2995 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2997 * a) we can get into loop creation. Check is done in is_subdir().
2998 * b) race potential - two innocent renames can create a loop together.
2999 * That's where 4.4 screws up. Current fix: serialization on
3000 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3002 * c) we have to lock _three_ objects - parents and victim (if it exists).
3003 * And that - after we got ->i_mutex on parents (until then we don't know
3004 * whether the target exists). Solution: try to be smart with locking
3005 * order for inodes. We rely on the fact that tree topology may change
3006 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
3007 * move will be locked. Thus we can rank directories by the tree
3008 * (ancestors first) and rank all non-directories after them.
3009 * That works since everybody except rename does "lock parent, lookup,
3010 * lock child" and rename is under ->s_vfs_rename_mutex.
3011 * HOWEVER, it relies on the assumption that any object with ->lookup()
3012 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3013 * we'd better make sure that there's no link(2) for them.
3014 * d) conversion from fhandle to dentry may come in the wrong moment - when
3015 * we are removing the target. Solution: we will have to grab ->i_mutex
3016 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3017 * ->i_mutex on parents, which works but leads to some truly excessive
3020 static int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
3021 struct inode
*new_dir
, struct dentry
*new_dentry
)
3024 struct inode
*target
= new_dentry
->d_inode
;
3027 * If we are going to change the parent - check write permissions,
3028 * we'll need to flip '..'.
3030 if (new_dir
!= old_dir
) {
3031 error
= inode_permission(old_dentry
->d_inode
, MAY_WRITE
);
3036 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3041 mutex_lock(&target
->i_mutex
);
3044 if (d_mountpoint(old_dentry
) || d_mountpoint(new_dentry
))
3048 shrink_dcache_parent(new_dentry
);
3049 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3054 target
->i_flags
|= S_DEAD
;
3055 dont_mount(new_dentry
);
3059 mutex_unlock(&target
->i_mutex
);
3061 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
))
3062 d_move(old_dentry
,new_dentry
);
3066 static int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
3067 struct inode
*new_dir
, struct dentry
*new_dentry
)
3069 struct inode
*target
= new_dentry
->d_inode
;
3072 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3078 mutex_lock(&target
->i_mutex
);
3081 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
3084 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3089 dont_mount(new_dentry
);
3090 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
))
3091 d_move(old_dentry
, new_dentry
);
3094 mutex_unlock(&target
->i_mutex
);
3099 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
3100 struct inode
*new_dir
, struct dentry
*new_dentry
)
3103 int is_dir
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
3104 const unsigned char *old_name
;
3106 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
3109 error
= may_delete(old_dir
, old_dentry
, is_dir
);
3113 if (!new_dentry
->d_inode
)
3114 error
= may_create(new_dir
, new_dentry
);
3116 error
= may_delete(new_dir
, new_dentry
, is_dir
);
3120 if (!old_dir
->i_op
->rename
)
3123 old_name
= fsnotify_oldname_init(old_dentry
->d_name
.name
);
3126 error
= vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
3128 error
= vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
3130 fsnotify_move(old_dir
, new_dir
, old_name
, is_dir
,
3131 new_dentry
->d_inode
, old_dentry
);
3132 fsnotify_oldname_free(old_name
);
3137 SYSCALL_DEFINE4(renameat
, int, olddfd
, const char __user
*, oldname
,
3138 int, newdfd
, const char __user
*, newname
)
3140 struct dentry
*old_dir
, *new_dir
;
3141 struct dentry
*old_dentry
, *new_dentry
;
3142 struct dentry
*trap
;
3143 struct nameidata oldnd
, newnd
;
3148 error
= user_path_parent(olddfd
, oldname
, &oldnd
, &from
);
3152 error
= user_path_parent(newdfd
, newname
, &newnd
, &to
);
3157 if (oldnd
.path
.mnt
!= newnd
.path
.mnt
)
3160 old_dir
= oldnd
.path
.dentry
;
3162 if (oldnd
.last_type
!= LAST_NORM
)
3165 new_dir
= newnd
.path
.dentry
;
3166 if (newnd
.last_type
!= LAST_NORM
)
3169 oldnd
.flags
&= ~LOOKUP_PARENT
;
3170 newnd
.flags
&= ~LOOKUP_PARENT
;
3171 newnd
.flags
|= LOOKUP_RENAME_TARGET
;
3173 trap
= lock_rename(new_dir
, old_dir
);
3175 old_dentry
= lookup_hash(&oldnd
);
3176 error
= PTR_ERR(old_dentry
);
3177 if (IS_ERR(old_dentry
))
3179 /* source must exist */
3181 if (!old_dentry
->d_inode
)
3183 /* unless the source is a directory trailing slashes give -ENOTDIR */
3184 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
3186 if (oldnd
.last
.name
[oldnd
.last
.len
])
3188 if (newnd
.last
.name
[newnd
.last
.len
])
3191 /* source should not be ancestor of target */
3193 if (old_dentry
== trap
)
3195 new_dentry
= lookup_hash(&newnd
);
3196 error
= PTR_ERR(new_dentry
);
3197 if (IS_ERR(new_dentry
))
3199 /* target should not be an ancestor of source */
3201 if (new_dentry
== trap
)
3204 error
= mnt_want_write(oldnd
.path
.mnt
);
3207 error
= security_path_rename(&oldnd
.path
, old_dentry
,
3208 &newnd
.path
, new_dentry
);
3211 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
3212 new_dir
->d_inode
, new_dentry
);
3214 mnt_drop_write(oldnd
.path
.mnt
);
3220 unlock_rename(new_dir
, old_dir
);
3222 path_put(&newnd
.path
);
3225 path_put(&oldnd
.path
);
3231 SYSCALL_DEFINE2(rename
, const char __user
*, oldname
, const char __user
*, newname
)
3233 return sys_renameat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
);
3236 int vfs_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
, const char *link
)
3240 len
= PTR_ERR(link
);
3245 if (len
> (unsigned) buflen
)
3247 if (copy_to_user(buffer
, link
, len
))
3254 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3255 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3256 * using) it for any given inode is up to filesystem.
3258 int generic_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
3260 struct nameidata nd
;
3265 cookie
= dentry
->d_inode
->i_op
->follow_link(dentry
, &nd
);
3267 return PTR_ERR(cookie
);
3269 res
= vfs_readlink(dentry
, buffer
, buflen
, nd_get_link(&nd
));
3270 if (dentry
->d_inode
->i_op
->put_link
)
3271 dentry
->d_inode
->i_op
->put_link(dentry
, &nd
, cookie
);
3275 int vfs_follow_link(struct nameidata
*nd
, const char *link
)
3277 return __vfs_follow_link(nd
, link
);
3280 /* get the link contents into pagecache */
3281 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
3285 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
3286 page
= read_mapping_page(mapping
, 0, NULL
);
3291 nd_terminate_link(kaddr
, dentry
->d_inode
->i_size
, PAGE_SIZE
- 1);
3295 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
3297 struct page
*page
= NULL
;
3298 char *s
= page_getlink(dentry
, &page
);
3299 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
3302 page_cache_release(page
);
3307 void *page_follow_link_light(struct dentry
*dentry
, struct nameidata
*nd
)
3309 struct page
*page
= NULL
;
3310 nd_set_link(nd
, page_getlink(dentry
, &page
));
3314 void page_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *cookie
)
3316 struct page
*page
= cookie
;
3320 page_cache_release(page
);
3325 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3327 int __page_symlink(struct inode
*inode
, const char *symname
, int len
, int nofs
)
3329 struct address_space
*mapping
= inode
->i_mapping
;
3334 unsigned int flags
= AOP_FLAG_UNINTERRUPTIBLE
;
3336 flags
|= AOP_FLAG_NOFS
;
3339 err
= pagecache_write_begin(NULL
, mapping
, 0, len
-1,
3340 flags
, &page
, &fsdata
);
3344 kaddr
= kmap_atomic(page
, KM_USER0
);
3345 memcpy(kaddr
, symname
, len
-1);
3346 kunmap_atomic(kaddr
, KM_USER0
);
3348 err
= pagecache_write_end(NULL
, mapping
, 0, len
-1, len
-1,
3355 mark_inode_dirty(inode
);
3361 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
3363 return __page_symlink(inode
, symname
, len
,
3364 !(mapping_gfp_mask(inode
->i_mapping
) & __GFP_FS
));
3367 const struct inode_operations page_symlink_inode_operations
= {
3368 .readlink
= generic_readlink
,
3369 .follow_link
= page_follow_link_light
,
3370 .put_link
= page_put_link
,
3373 EXPORT_SYMBOL(user_path_at
);
3374 EXPORT_SYMBOL(follow_down_one
);
3375 EXPORT_SYMBOL(follow_down
);
3376 EXPORT_SYMBOL(follow_up
);
3377 EXPORT_SYMBOL(get_write_access
); /* binfmt_aout */
3378 EXPORT_SYMBOL(getname
);
3379 EXPORT_SYMBOL(lock_rename
);
3380 EXPORT_SYMBOL(lookup_one_len
);
3381 EXPORT_SYMBOL(page_follow_link_light
);
3382 EXPORT_SYMBOL(page_put_link
);
3383 EXPORT_SYMBOL(page_readlink
);
3384 EXPORT_SYMBOL(__page_symlink
);
3385 EXPORT_SYMBOL(page_symlink
);
3386 EXPORT_SYMBOL(page_symlink_inode_operations
);
3387 EXPORT_SYMBOL(kern_path_parent
);
3388 EXPORT_SYMBOL(kern_path
);
3389 EXPORT_SYMBOL(vfs_path_lookup
);
3390 EXPORT_SYMBOL(inode_permission
);
3391 EXPORT_SYMBOL(unlock_rename
);
3392 EXPORT_SYMBOL(vfs_create
);
3393 EXPORT_SYMBOL(vfs_follow_link
);
3394 EXPORT_SYMBOL(vfs_link
);
3395 EXPORT_SYMBOL(vfs_mkdir
);
3396 EXPORT_SYMBOL(vfs_mknod
);
3397 EXPORT_SYMBOL(generic_permission
);
3398 EXPORT_SYMBOL(vfs_readlink
);
3399 EXPORT_SYMBOL(vfs_rename
);
3400 EXPORT_SYMBOL(vfs_rmdir
);
3401 EXPORT_SYMBOL(vfs_symlink
);
3402 EXPORT_SYMBOL(vfs_unlink
);
3403 EXPORT_SYMBOL(dentry_unhash
);
3404 EXPORT_SYMBOL(generic_readlink
);