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
, int *empty
)
143 result
= ERR_PTR(-ENOMEM
);
146 int retval
= do_getname(filename
, tmp
);
150 if (retval
== -ENOENT
&& empty
)
152 if (retval
!= -ENOENT
|| !(flags
& LOOKUP_EMPTY
)) {
154 result
= ERR_PTR(retval
);
158 audit_getname(result
);
162 char *getname(const char __user
* filename
)
164 return getname_flags(filename
, 0, 0);
167 #ifdef CONFIG_AUDITSYSCALL
168 void putname(const char *name
)
170 if (unlikely(!audit_dummy_context()))
175 EXPORT_SYMBOL(putname
);
179 * This does basic POSIX ACL permission checking
181 static int acl_permission_check(struct inode
*inode
, int mask
, unsigned int flags
,
182 int (*check_acl
)(struct inode
*inode
, int mask
, unsigned int flags
))
184 unsigned int mode
= inode
->i_mode
;
186 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
188 if (current_user_ns() != inode_userns(inode
))
191 if (current_fsuid() == inode
->i_uid
)
194 if (IS_POSIXACL(inode
) && (mode
& S_IRWXG
) && check_acl
) {
195 int error
= check_acl(inode
, mask
, flags
);
196 if (error
!= -EAGAIN
)
200 if (in_group_p(inode
->i_gid
))
206 * If the DACs are ok we don't need any capability check.
208 if ((mask
& ~mode
) == 0)
214 * generic_permission - check for access rights on a Posix-like filesystem
215 * @inode: inode to check access rights for
216 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
217 * @check_acl: optional callback to check for Posix ACLs
218 * @flags: IPERM_FLAG_ flags.
220 * Used to check for read/write/execute permissions on a file.
221 * We use "fsuid" for this, letting us set arbitrary permissions
222 * for filesystem access without changing the "normal" uids which
223 * are used for other things.
225 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
226 * request cannot be satisfied (eg. requires blocking or too much complexity).
227 * It would then be called again in ref-walk mode.
229 int generic_permission(struct inode
*inode
, int mask
, unsigned int flags
,
230 int (*check_acl
)(struct inode
*inode
, int mask
, unsigned int flags
))
235 * Do the basic POSIX ACL permission checks.
237 ret
= acl_permission_check(inode
, mask
, flags
, check_acl
);
242 * Read/write DACs are always overridable.
243 * Executable DACs are overridable for all directories and
244 * for non-directories that have least one exec bit set.
246 if (!(mask
& MAY_EXEC
) || execute_ok(inode
))
247 if (ns_capable(inode_userns(inode
), CAP_DAC_OVERRIDE
))
251 * Searching includes executable on directories, else just read.
253 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
254 if (mask
== MAY_READ
|| (S_ISDIR(inode
->i_mode
) && !(mask
& MAY_WRITE
)))
255 if (ns_capable(inode_userns(inode
), CAP_DAC_READ_SEARCH
))
262 * inode_permission - check for access rights to a given inode
263 * @inode: inode to check permission on
264 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
266 * Used to check for read/write/execute permissions on an inode.
267 * We use "fsuid" for this, letting us set arbitrary permissions
268 * for filesystem access without changing the "normal" uids which
269 * are used for other things.
271 int inode_permission(struct inode
*inode
, int mask
)
275 if (mask
& MAY_WRITE
) {
276 umode_t mode
= inode
->i_mode
;
279 * Nobody gets write access to a read-only fs.
281 if (IS_RDONLY(inode
) &&
282 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
286 * Nobody gets write access to an immutable file.
288 if (IS_IMMUTABLE(inode
))
292 if (inode
->i_op
->permission
)
293 retval
= inode
->i_op
->permission(inode
, mask
, 0);
295 retval
= generic_permission(inode
, mask
, 0,
296 inode
->i_op
->check_acl
);
301 retval
= devcgroup_inode_permission(inode
, mask
);
305 return security_inode_permission(inode
, mask
);
309 * file_permission - check for additional access rights to a given file
310 * @file: file to check access rights for
311 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
313 * Used to check for read/write/execute permissions on an already opened
317 * Do not use this function in new code. All access checks should
318 * be done using inode_permission().
320 int file_permission(struct file
*file
, int mask
)
322 return inode_permission(file
->f_path
.dentry
->d_inode
, mask
);
326 * get_write_access() gets write permission for a file.
327 * put_write_access() releases this write permission.
328 * This is used for regular files.
329 * We cannot support write (and maybe mmap read-write shared) accesses and
330 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
331 * can have the following values:
332 * 0: no writers, no VM_DENYWRITE mappings
333 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
334 * > 0: (i_writecount) users are writing to the file.
336 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
337 * except for the cases where we don't hold i_writecount yet. Then we need to
338 * use {get,deny}_write_access() - these functions check the sign and refuse
339 * to do the change if sign is wrong. Exclusion between them is provided by
340 * the inode->i_lock spinlock.
343 int get_write_access(struct inode
* inode
)
345 spin_lock(&inode
->i_lock
);
346 if (atomic_read(&inode
->i_writecount
) < 0) {
347 spin_unlock(&inode
->i_lock
);
350 atomic_inc(&inode
->i_writecount
);
351 spin_unlock(&inode
->i_lock
);
356 int deny_write_access(struct file
* file
)
358 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
360 spin_lock(&inode
->i_lock
);
361 if (atomic_read(&inode
->i_writecount
) > 0) {
362 spin_unlock(&inode
->i_lock
);
365 atomic_dec(&inode
->i_writecount
);
366 spin_unlock(&inode
->i_lock
);
372 * path_get - get a reference to a path
373 * @path: path to get the reference to
375 * Given a path increment the reference count to the dentry and the vfsmount.
377 void path_get(struct path
*path
)
382 EXPORT_SYMBOL(path_get
);
385 * path_put - put a reference to a path
386 * @path: path to put the reference to
388 * Given a path decrement the reference count to the dentry and the vfsmount.
390 void path_put(struct path
*path
)
395 EXPORT_SYMBOL(path_put
);
398 * Path walking has 2 modes, rcu-walk and ref-walk (see
399 * Documentation/filesystems/path-lookup.txt). In situations when we can't
400 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
401 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
402 * mode. Refcounts are grabbed at the last known good point before rcu-walk
403 * got stuck, so ref-walk may continue from there. If this is not successful
404 * (eg. a seqcount has changed), then failure is returned and it's up to caller
405 * to restart the path walk from the beginning in ref-walk mode.
409 * unlazy_walk - try to switch to ref-walk mode.
410 * @nd: nameidata pathwalk data
411 * @dentry: child of nd->path.dentry or NULL
412 * Returns: 0 on success, -ECHILD on failure
414 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
415 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
416 * @nd or NULL. Must be called from rcu-walk context.
418 static int unlazy_walk(struct nameidata
*nd
, struct dentry
*dentry
)
420 struct fs_struct
*fs
= current
->fs
;
421 struct dentry
*parent
= nd
->path
.dentry
;
424 BUG_ON(!(nd
->flags
& LOOKUP_RCU
));
425 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
427 spin_lock(&fs
->lock
);
428 if (nd
->root
.mnt
!= fs
->root
.mnt
||
429 nd
->root
.dentry
!= fs
->root
.dentry
)
432 spin_lock(&parent
->d_lock
);
434 if (!__d_rcu_to_refcount(parent
, nd
->seq
))
436 BUG_ON(nd
->inode
!= parent
->d_inode
);
438 if (dentry
->d_parent
!= parent
)
440 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
441 if (!__d_rcu_to_refcount(dentry
, nd
->seq
))
444 * If the sequence check on the child dentry passed, then
445 * the child has not been removed from its parent. This
446 * means the parent dentry must be valid and able to take
447 * a reference at this point.
449 BUG_ON(!IS_ROOT(dentry
) && dentry
->d_parent
!= parent
);
450 BUG_ON(!parent
->d_count
);
452 spin_unlock(&dentry
->d_lock
);
454 spin_unlock(&parent
->d_lock
);
457 spin_unlock(&fs
->lock
);
459 mntget(nd
->path
.mnt
);
462 br_read_unlock(vfsmount_lock
);
463 nd
->flags
&= ~LOOKUP_RCU
;
467 spin_unlock(&dentry
->d_lock
);
469 spin_unlock(&parent
->d_lock
);
472 spin_unlock(&fs
->lock
);
477 * release_open_intent - free up open intent resources
478 * @nd: pointer to nameidata
480 void release_open_intent(struct nameidata
*nd
)
482 struct file
*file
= nd
->intent
.open
.file
;
484 if (file
&& !IS_ERR(file
)) {
485 if (file
->f_path
.dentry
== NULL
)
492 static inline int d_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
494 return dentry
->d_op
->d_revalidate(dentry
, nd
);
497 static struct dentry
*
498 do_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
500 int status
= d_revalidate(dentry
, nd
);
501 if (unlikely(status
<= 0)) {
503 * The dentry failed validation.
504 * If d_revalidate returned 0 attempt to invalidate
505 * the dentry otherwise d_revalidate is asking us
506 * to return a fail status.
510 dentry
= ERR_PTR(status
);
511 } else if (!d_invalidate(dentry
)) {
520 * complete_walk - successful completion of path walk
521 * @nd: pointer nameidata
523 * If we had been in RCU mode, drop out of it and legitimize nd->path.
524 * Revalidate the final result, unless we'd already done that during
525 * the path walk or the filesystem doesn't ask for it. Return 0 on
526 * success, -error on failure. In case of failure caller does not
527 * need to drop nd->path.
529 static int complete_walk(struct nameidata
*nd
)
531 struct dentry
*dentry
= nd
->path
.dentry
;
534 if (nd
->flags
& LOOKUP_RCU
) {
535 nd
->flags
&= ~LOOKUP_RCU
;
536 if (!(nd
->flags
& LOOKUP_ROOT
))
538 spin_lock(&dentry
->d_lock
);
539 if (unlikely(!__d_rcu_to_refcount(dentry
, nd
->seq
))) {
540 spin_unlock(&dentry
->d_lock
);
542 br_read_unlock(vfsmount_lock
);
545 BUG_ON(nd
->inode
!= dentry
->d_inode
);
546 spin_unlock(&dentry
->d_lock
);
547 mntget(nd
->path
.mnt
);
549 br_read_unlock(vfsmount_lock
);
552 if (likely(!(nd
->flags
& LOOKUP_JUMPED
)))
555 if (likely(!(dentry
->d_flags
& DCACHE_OP_REVALIDATE
)))
558 if (likely(!(dentry
->d_sb
->s_type
->fs_flags
& FS_REVAL_DOT
)))
561 /* Note: we do not d_invalidate() */
562 status
= d_revalidate(dentry
, nd
);
574 * Short-cut version of permission(), for calling on directories
575 * during pathname resolution. Combines parts of permission()
576 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
578 * If appropriate, check DAC only. If not appropriate, or
579 * short-cut DAC fails, then call ->permission() to do more
580 * complete permission check.
582 static inline int exec_permission(struct inode
*inode
, unsigned int flags
)
585 struct user_namespace
*ns
= inode_userns(inode
);
587 if (inode
->i_op
->permission
) {
588 ret
= inode
->i_op
->permission(inode
, MAY_EXEC
, flags
);
590 ret
= acl_permission_check(inode
, MAY_EXEC
, flags
,
591 inode
->i_op
->check_acl
);
598 if (ns_capable(ns
, CAP_DAC_OVERRIDE
) ||
599 ns_capable(ns
, CAP_DAC_READ_SEARCH
))
604 return security_inode_exec_permission(inode
, flags
);
607 static __always_inline
void set_root(struct nameidata
*nd
)
610 get_fs_root(current
->fs
, &nd
->root
);
613 static int link_path_walk(const char *, struct nameidata
*);
615 static __always_inline
void set_root_rcu(struct nameidata
*nd
)
618 struct fs_struct
*fs
= current
->fs
;
622 seq
= read_seqcount_begin(&fs
->seq
);
624 nd
->seq
= __read_seqcount_begin(&nd
->root
.dentry
->d_seq
);
625 } while (read_seqcount_retry(&fs
->seq
, seq
));
629 static __always_inline
int __vfs_follow_link(struct nameidata
*nd
, const char *link
)
641 nd
->flags
|= LOOKUP_JUMPED
;
643 nd
->inode
= nd
->path
.dentry
->d_inode
;
645 ret
= link_path_walk(link
, nd
);
649 return PTR_ERR(link
);
652 static void path_put_conditional(struct path
*path
, struct nameidata
*nd
)
655 if (path
->mnt
!= nd
->path
.mnt
)
659 static inline void path_to_nameidata(const struct path
*path
,
660 struct nameidata
*nd
)
662 if (!(nd
->flags
& LOOKUP_RCU
)) {
663 dput(nd
->path
.dentry
);
664 if (nd
->path
.mnt
!= path
->mnt
)
665 mntput(nd
->path
.mnt
);
667 nd
->path
.mnt
= path
->mnt
;
668 nd
->path
.dentry
= path
->dentry
;
671 static inline void put_link(struct nameidata
*nd
, struct path
*link
, void *cookie
)
673 struct inode
*inode
= link
->dentry
->d_inode
;
674 if (!IS_ERR(cookie
) && inode
->i_op
->put_link
)
675 inode
->i_op
->put_link(link
->dentry
, nd
, cookie
);
679 static __always_inline
int
680 follow_link(struct path
*link
, struct nameidata
*nd
, void **p
)
683 struct dentry
*dentry
= link
->dentry
;
685 BUG_ON(nd
->flags
& LOOKUP_RCU
);
687 if (link
->mnt
== nd
->path
.mnt
)
690 if (unlikely(current
->total_link_count
>= 40)) {
691 *p
= ERR_PTR(-ELOOP
); /* no ->put_link(), please */
696 current
->total_link_count
++;
698 touch_atime(link
->mnt
, dentry
);
699 nd_set_link(nd
, NULL
);
701 error
= security_inode_follow_link(link
->dentry
, nd
);
703 *p
= ERR_PTR(error
); /* no ->put_link(), please */
708 nd
->last_type
= LAST_BIND
;
709 *p
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
712 char *s
= nd_get_link(nd
);
715 error
= __vfs_follow_link(nd
, s
);
716 else if (nd
->last_type
== LAST_BIND
) {
717 nd
->flags
|= LOOKUP_JUMPED
;
718 nd
->inode
= nd
->path
.dentry
->d_inode
;
719 if (nd
->inode
->i_op
->follow_link
) {
720 /* stepped on a _really_ weird one */
729 static int follow_up_rcu(struct path
*path
)
731 struct vfsmount
*parent
;
732 struct dentry
*mountpoint
;
734 parent
= path
->mnt
->mnt_parent
;
735 if (parent
== path
->mnt
)
737 mountpoint
= path
->mnt
->mnt_mountpoint
;
738 path
->dentry
= mountpoint
;
743 int follow_up(struct path
*path
)
745 struct vfsmount
*parent
;
746 struct dentry
*mountpoint
;
748 br_read_lock(vfsmount_lock
);
749 parent
= path
->mnt
->mnt_parent
;
750 if (parent
== path
->mnt
) {
751 br_read_unlock(vfsmount_lock
);
755 mountpoint
= dget(path
->mnt
->mnt_mountpoint
);
756 br_read_unlock(vfsmount_lock
);
758 path
->dentry
= mountpoint
;
765 * Perform an automount
766 * - return -EISDIR to tell follow_managed() to stop and return the path we
769 static int follow_automount(struct path
*path
, unsigned flags
,
772 struct vfsmount
*mnt
;
775 if (!path
->dentry
->d_op
|| !path
->dentry
->d_op
->d_automount
)
778 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
779 * and this is the terminal part of the path.
781 if ((flags
& LOOKUP_NO_AUTOMOUNT
) && !(flags
& LOOKUP_CONTINUE
))
782 return -EISDIR
; /* we actually want to stop here */
784 /* We don't want to mount if someone's just doing a stat -
785 * unless they're stat'ing a directory and appended a '/' to
788 * We do, however, want to mount if someone wants to open or
789 * create a file of any type under the mountpoint, wants to
790 * traverse through the mountpoint or wants to open the
791 * mounted directory. Also, autofs may mark negative dentries
792 * as being automount points. These will need the attentions
793 * of the daemon to instantiate them before they can be used.
795 if (!(flags
& (LOOKUP_CONTINUE
| LOOKUP_DIRECTORY
|
796 LOOKUP_OPEN
| LOOKUP_CREATE
| LOOKUP_AUTOMOUNT
)) &&
797 path
->dentry
->d_inode
)
800 current
->total_link_count
++;
801 if (current
->total_link_count
>= 40)
804 mnt
= path
->dentry
->d_op
->d_automount(path
);
807 * The filesystem is allowed to return -EISDIR here to indicate
808 * it doesn't want to automount. For instance, autofs would do
809 * this so that its userspace daemon can mount on this dentry.
811 * However, we can only permit this if it's a terminal point in
812 * the path being looked up; if it wasn't then the remainder of
813 * the path is inaccessible and we should say so.
815 if (PTR_ERR(mnt
) == -EISDIR
&& (flags
& LOOKUP_CONTINUE
))
820 if (!mnt
) /* mount collision */
824 /* lock_mount() may release path->mnt on error */
828 err
= finish_automount(mnt
, path
);
832 /* Someone else made a mount here whilst we were busy */
837 path
->dentry
= dget(mnt
->mnt_root
);
846 * Handle a dentry that is managed in some way.
847 * - Flagged for transit management (autofs)
848 * - Flagged as mountpoint
849 * - Flagged as automount point
851 * This may only be called in refwalk mode.
853 * Serialization is taken care of in namespace.c
855 static int follow_managed(struct path
*path
, unsigned flags
)
857 struct vfsmount
*mnt
= path
->mnt
; /* held by caller, must be left alone */
859 bool need_mntput
= false;
862 /* Given that we're not holding a lock here, we retain the value in a
863 * local variable for each dentry as we look at it so that we don't see
864 * the components of that value change under us */
865 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
866 managed
&= DCACHE_MANAGED_DENTRY
,
867 unlikely(managed
!= 0)) {
868 /* Allow the filesystem to manage the transit without i_mutex
870 if (managed
& DCACHE_MANAGE_TRANSIT
) {
871 BUG_ON(!path
->dentry
->d_op
);
872 BUG_ON(!path
->dentry
->d_op
->d_manage
);
873 ret
= path
->dentry
->d_op
->d_manage(path
->dentry
, false);
878 /* Transit to a mounted filesystem. */
879 if (managed
& DCACHE_MOUNTED
) {
880 struct vfsmount
*mounted
= lookup_mnt(path
);
886 path
->dentry
= dget(mounted
->mnt_root
);
891 /* Something is mounted on this dentry in another
892 * namespace and/or whatever was mounted there in this
893 * namespace got unmounted before we managed to get the
897 /* Handle an automount point */
898 if (managed
& DCACHE_NEED_AUTOMOUNT
) {
899 ret
= follow_automount(path
, flags
, &need_mntput
);
905 /* We didn't change the current path point */
909 if (need_mntput
&& path
->mnt
== mnt
)
913 return ret
< 0 ? ret
: need_mntput
;
916 int follow_down_one(struct path
*path
)
918 struct vfsmount
*mounted
;
920 mounted
= lookup_mnt(path
);
925 path
->dentry
= dget(mounted
->mnt_root
);
931 static inline bool managed_dentry_might_block(struct dentry
*dentry
)
933 return (dentry
->d_flags
& DCACHE_MANAGE_TRANSIT
&&
934 dentry
->d_op
->d_manage(dentry
, true) < 0);
938 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
939 * we meet a managed dentry that would need blocking.
941 static bool __follow_mount_rcu(struct nameidata
*nd
, struct path
*path
,
942 struct inode
**inode
)
945 struct vfsmount
*mounted
;
947 * Don't forget we might have a non-mountpoint managed dentry
948 * that wants to block transit.
950 if (unlikely(managed_dentry_might_block(path
->dentry
)))
953 if (!d_mountpoint(path
->dentry
))
956 mounted
= __lookup_mnt(path
->mnt
, path
->dentry
, 1);
960 path
->dentry
= mounted
->mnt_root
;
961 nd
->flags
|= LOOKUP_JUMPED
;
962 nd
->seq
= read_seqcount_begin(&path
->dentry
->d_seq
);
964 * Update the inode too. We don't need to re-check the
965 * dentry sequence number here after this d_inode read,
966 * because a mount-point is always pinned.
968 *inode
= path
->dentry
->d_inode
;
973 static void follow_mount_rcu(struct nameidata
*nd
)
975 while (d_mountpoint(nd
->path
.dentry
)) {
976 struct vfsmount
*mounted
;
977 mounted
= __lookup_mnt(nd
->path
.mnt
, nd
->path
.dentry
, 1);
980 nd
->path
.mnt
= mounted
;
981 nd
->path
.dentry
= mounted
->mnt_root
;
982 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
986 static int follow_dotdot_rcu(struct nameidata
*nd
)
991 if (nd
->path
.dentry
== nd
->root
.dentry
&&
992 nd
->path
.mnt
== nd
->root
.mnt
) {
995 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
996 struct dentry
*old
= nd
->path
.dentry
;
997 struct dentry
*parent
= old
->d_parent
;
1000 seq
= read_seqcount_begin(&parent
->d_seq
);
1001 if (read_seqcount_retry(&old
->d_seq
, nd
->seq
))
1003 nd
->path
.dentry
= parent
;
1007 if (!follow_up_rcu(&nd
->path
))
1009 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1011 follow_mount_rcu(nd
);
1012 nd
->inode
= nd
->path
.dentry
->d_inode
;
1016 nd
->flags
&= ~LOOKUP_RCU
;
1017 if (!(nd
->flags
& LOOKUP_ROOT
))
1018 nd
->root
.mnt
= NULL
;
1020 br_read_unlock(vfsmount_lock
);
1025 * Follow down to the covering mount currently visible to userspace. At each
1026 * point, the filesystem owning that dentry may be queried as to whether the
1027 * caller is permitted to proceed or not.
1029 int follow_down(struct path
*path
)
1034 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
1035 unlikely(managed
& DCACHE_MANAGED_DENTRY
)) {
1036 /* Allow the filesystem to manage the transit without i_mutex
1039 * We indicate to the filesystem if someone is trying to mount
1040 * something here. This gives autofs the chance to deny anyone
1041 * other than its daemon the right to mount on its
1044 * The filesystem may sleep at this point.
1046 if (managed
& DCACHE_MANAGE_TRANSIT
) {
1047 BUG_ON(!path
->dentry
->d_op
);
1048 BUG_ON(!path
->dentry
->d_op
->d_manage
);
1049 ret
= path
->dentry
->d_op
->d_manage(
1050 path
->dentry
, false);
1052 return ret
== -EISDIR
? 0 : ret
;
1055 /* Transit to a mounted filesystem. */
1056 if (managed
& DCACHE_MOUNTED
) {
1057 struct vfsmount
*mounted
= lookup_mnt(path
);
1062 path
->mnt
= mounted
;
1063 path
->dentry
= dget(mounted
->mnt_root
);
1067 /* Don't handle automount points here */
1074 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1076 static void follow_mount(struct path
*path
)
1078 while (d_mountpoint(path
->dentry
)) {
1079 struct vfsmount
*mounted
= lookup_mnt(path
);
1084 path
->mnt
= mounted
;
1085 path
->dentry
= dget(mounted
->mnt_root
);
1089 static void follow_dotdot(struct nameidata
*nd
)
1094 struct dentry
*old
= nd
->path
.dentry
;
1096 if (nd
->path
.dentry
== nd
->root
.dentry
&&
1097 nd
->path
.mnt
== nd
->root
.mnt
) {
1100 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
1101 /* rare case of legitimate dget_parent()... */
1102 nd
->path
.dentry
= dget_parent(nd
->path
.dentry
);
1106 if (!follow_up(&nd
->path
))
1109 follow_mount(&nd
->path
);
1110 nd
->inode
= nd
->path
.dentry
->d_inode
;
1114 * Allocate a dentry with name and parent, and perform a parent
1115 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1116 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1117 * have verified that no child exists while under i_mutex.
1119 static struct dentry
*d_alloc_and_lookup(struct dentry
*parent
,
1120 struct qstr
*name
, struct nameidata
*nd
)
1122 struct inode
*inode
= parent
->d_inode
;
1123 struct dentry
*dentry
;
1126 /* Don't create child dentry for a dead directory. */
1127 if (unlikely(IS_DEADDIR(inode
)))
1128 return ERR_PTR(-ENOENT
);
1130 dentry
= d_alloc(parent
, name
);
1131 if (unlikely(!dentry
))
1132 return ERR_PTR(-ENOMEM
);
1134 old
= inode
->i_op
->lookup(inode
, dentry
, nd
);
1135 if (unlikely(old
)) {
1143 * It's more convoluted than I'd like it to be, but... it's still fairly
1144 * small and for now I'd prefer to have fast path as straight as possible.
1145 * It _is_ time-critical.
1147 static int do_lookup(struct nameidata
*nd
, struct qstr
*name
,
1148 struct path
*path
, struct inode
**inode
)
1150 struct vfsmount
*mnt
= nd
->path
.mnt
;
1151 struct dentry
*dentry
, *parent
= nd
->path
.dentry
;
1157 * Rename seqlock is not required here because in the off chance
1158 * of a false negative due to a concurrent rename, we're going to
1159 * do the non-racy lookup, below.
1161 if (nd
->flags
& LOOKUP_RCU
) {
1164 dentry
= __d_lookup_rcu(parent
, name
, &seq
, inode
);
1168 /* Memory barrier in read_seqcount_begin of child is enough */
1169 if (__read_seqcount_retry(&parent
->d_seq
, nd
->seq
))
1173 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
)) {
1174 status
= d_revalidate(dentry
, nd
);
1175 if (unlikely(status
<= 0)) {
1176 if (status
!= -ECHILD
)
1182 path
->dentry
= dentry
;
1183 if (unlikely(!__follow_mount_rcu(nd
, path
, inode
)))
1185 if (unlikely(path
->dentry
->d_flags
& DCACHE_NEED_AUTOMOUNT
))
1189 if (unlazy_walk(nd
, dentry
))
1192 dentry
= __d_lookup(parent
, name
);
1196 if (unlikely(!dentry
)) {
1197 struct inode
*dir
= parent
->d_inode
;
1198 BUG_ON(nd
->inode
!= dir
);
1200 mutex_lock(&dir
->i_mutex
);
1201 dentry
= d_lookup(parent
, name
);
1202 if (likely(!dentry
)) {
1203 dentry
= d_alloc_and_lookup(parent
, name
, nd
);
1204 if (IS_ERR(dentry
)) {
1205 mutex_unlock(&dir
->i_mutex
);
1206 return PTR_ERR(dentry
);
1212 mutex_unlock(&dir
->i_mutex
);
1214 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
) && need_reval
)
1215 status
= d_revalidate(dentry
, nd
);
1216 if (unlikely(status
<= 0)) {
1221 if (!d_invalidate(dentry
)) {
1230 path
->dentry
= dentry
;
1231 err
= follow_managed(path
, nd
->flags
);
1232 if (unlikely(err
< 0)) {
1233 path_put_conditional(path
, nd
);
1237 nd
->flags
|= LOOKUP_JUMPED
;
1238 *inode
= path
->dentry
->d_inode
;
1242 static inline int may_lookup(struct nameidata
*nd
)
1244 if (nd
->flags
& LOOKUP_RCU
) {
1245 int err
= exec_permission(nd
->inode
, IPERM_FLAG_RCU
);
1248 if (unlazy_walk(nd
, NULL
))
1251 return exec_permission(nd
->inode
, 0);
1254 static inline int handle_dots(struct nameidata
*nd
, int type
)
1256 if (type
== LAST_DOTDOT
) {
1257 if (nd
->flags
& LOOKUP_RCU
) {
1258 if (follow_dotdot_rcu(nd
))
1266 static void terminate_walk(struct nameidata
*nd
)
1268 if (!(nd
->flags
& LOOKUP_RCU
)) {
1269 path_put(&nd
->path
);
1271 nd
->flags
&= ~LOOKUP_RCU
;
1272 if (!(nd
->flags
& LOOKUP_ROOT
))
1273 nd
->root
.mnt
= NULL
;
1275 br_read_unlock(vfsmount_lock
);
1279 static inline int walk_component(struct nameidata
*nd
, struct path
*path
,
1280 struct qstr
*name
, int type
, int follow
)
1282 struct inode
*inode
;
1285 * "." and ".." are special - ".." especially so because it has
1286 * to be able to know about the current root directory and
1287 * parent relationships.
1289 if (unlikely(type
!= LAST_NORM
))
1290 return handle_dots(nd
, type
);
1291 err
= do_lookup(nd
, name
, path
, &inode
);
1292 if (unlikely(err
)) {
1297 path_to_nameidata(path
, nd
);
1301 if (unlikely(inode
->i_op
->follow_link
) && follow
) {
1302 if (nd
->flags
& LOOKUP_RCU
) {
1303 if (unlikely(unlazy_walk(nd
, path
->dentry
))) {
1308 BUG_ON(inode
!= path
->dentry
->d_inode
);
1311 path_to_nameidata(path
, nd
);
1317 * This limits recursive symlink follows to 8, while
1318 * limiting consecutive symlinks to 40.
1320 * Without that kind of total limit, nasty chains of consecutive
1321 * symlinks can cause almost arbitrarily long lookups.
1323 static inline int nested_symlink(struct path
*path
, struct nameidata
*nd
)
1327 if (unlikely(current
->link_count
>= MAX_NESTED_LINKS
)) {
1328 path_put_conditional(path
, nd
);
1329 path_put(&nd
->path
);
1332 BUG_ON(nd
->depth
>= MAX_NESTED_LINKS
);
1335 current
->link_count
++;
1338 struct path link
= *path
;
1341 res
= follow_link(&link
, nd
, &cookie
);
1343 res
= walk_component(nd
, path
, &nd
->last
,
1344 nd
->last_type
, LOOKUP_FOLLOW
);
1345 put_link(nd
, &link
, cookie
);
1348 current
->link_count
--;
1355 * This is the basic name resolution function, turning a pathname into
1356 * the final dentry. We expect 'base' to be positive and a directory.
1358 * Returns 0 and nd will have valid dentry and mnt on success.
1359 * Returns error and drops reference to input namei data on failure.
1361 static int link_path_walk(const char *name
, struct nameidata
*nd
)
1365 unsigned int lookup_flags
= nd
->flags
;
1372 /* At this point we know we have a real path component. */
1379 nd
->flags
|= LOOKUP_CONTINUE
;
1381 err
= may_lookup(nd
);
1386 c
= *(const unsigned char *)name
;
1388 hash
= init_name_hash();
1391 hash
= partial_name_hash(c
, hash
);
1392 c
= *(const unsigned char *)name
;
1393 } while (c
&& (c
!= '/'));
1394 this.len
= name
- (const char *) this.name
;
1395 this.hash
= end_name_hash(hash
);
1398 if (this.name
[0] == '.') switch (this.len
) {
1400 if (this.name
[1] == '.') {
1402 nd
->flags
|= LOOKUP_JUMPED
;
1408 if (likely(type
== LAST_NORM
)) {
1409 struct dentry
*parent
= nd
->path
.dentry
;
1410 nd
->flags
&= ~LOOKUP_JUMPED
;
1411 if (unlikely(parent
->d_flags
& DCACHE_OP_HASH
)) {
1412 err
= parent
->d_op
->d_hash(parent
, nd
->inode
,
1419 /* remove trailing slashes? */
1421 goto last_component
;
1422 while (*++name
== '/');
1424 goto last_component
;
1426 err
= walk_component(nd
, &next
, &this, type
, LOOKUP_FOLLOW
);
1431 err
= nested_symlink(&next
, nd
);
1436 if (!nd
->inode
->i_op
->lookup
)
1439 /* here ends the main loop */
1442 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1443 nd
->flags
&= lookup_flags
| ~LOOKUP_CONTINUE
;
1445 nd
->last_type
= type
;
1452 static int path_init(int dfd
, const char *name
, unsigned int flags
,
1453 struct nameidata
*nd
, struct file
**fp
)
1459 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
1460 nd
->flags
= flags
| LOOKUP_JUMPED
;
1462 if (flags
& LOOKUP_ROOT
) {
1463 struct inode
*inode
= nd
->root
.dentry
->d_inode
;
1465 if (!inode
->i_op
->lookup
)
1467 retval
= inode_permission(inode
, MAY_EXEC
);
1471 nd
->path
= nd
->root
;
1473 if (flags
& LOOKUP_RCU
) {
1474 br_read_lock(vfsmount_lock
);
1476 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1478 path_get(&nd
->path
);
1483 nd
->root
.mnt
= NULL
;
1486 if (flags
& LOOKUP_RCU
) {
1487 br_read_lock(vfsmount_lock
);
1492 path_get(&nd
->root
);
1494 nd
->path
= nd
->root
;
1495 } else if (dfd
== AT_FDCWD
) {
1496 if (flags
& LOOKUP_RCU
) {
1497 struct fs_struct
*fs
= current
->fs
;
1500 br_read_lock(vfsmount_lock
);
1504 seq
= read_seqcount_begin(&fs
->seq
);
1506 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1507 } while (read_seqcount_retry(&fs
->seq
, seq
));
1509 get_fs_pwd(current
->fs
, &nd
->path
);
1512 struct dentry
*dentry
;
1514 file
= fget_raw_light(dfd
, &fput_needed
);
1519 dentry
= file
->f_path
.dentry
;
1523 if (!S_ISDIR(dentry
->d_inode
->i_mode
))
1526 retval
= file_permission(file
, MAY_EXEC
);
1531 nd
->path
= file
->f_path
;
1532 if (flags
& LOOKUP_RCU
) {
1535 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1536 br_read_lock(vfsmount_lock
);
1539 path_get(&file
->f_path
);
1540 fput_light(file
, fput_needed
);
1544 nd
->inode
= nd
->path
.dentry
->d_inode
;
1548 fput_light(file
, fput_needed
);
1553 static inline int lookup_last(struct nameidata
*nd
, struct path
*path
)
1555 if (nd
->last_type
== LAST_NORM
&& nd
->last
.name
[nd
->last
.len
])
1556 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
1558 nd
->flags
&= ~LOOKUP_PARENT
;
1559 return walk_component(nd
, path
, &nd
->last
, nd
->last_type
,
1560 nd
->flags
& LOOKUP_FOLLOW
);
1563 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1564 static int path_lookupat(int dfd
, const char *name
,
1565 unsigned int flags
, struct nameidata
*nd
)
1567 struct file
*base
= NULL
;
1572 * Path walking is largely split up into 2 different synchronisation
1573 * schemes, rcu-walk and ref-walk (explained in
1574 * Documentation/filesystems/path-lookup.txt). These share much of the
1575 * path walk code, but some things particularly setup, cleanup, and
1576 * following mounts are sufficiently divergent that functions are
1577 * duplicated. Typically there is a function foo(), and its RCU
1578 * analogue, foo_rcu().
1580 * -ECHILD is the error number of choice (just to avoid clashes) that
1581 * is returned if some aspect of an rcu-walk fails. Such an error must
1582 * be handled by restarting a traditional ref-walk (which will always
1583 * be able to complete).
1585 err
= path_init(dfd
, name
, flags
| LOOKUP_PARENT
, nd
, &base
);
1590 current
->total_link_count
= 0;
1591 err
= link_path_walk(name
, nd
);
1593 if (!err
&& !(flags
& LOOKUP_PARENT
)) {
1594 err
= lookup_last(nd
, &path
);
1597 struct path link
= path
;
1598 nd
->flags
|= LOOKUP_PARENT
;
1599 err
= follow_link(&link
, nd
, &cookie
);
1601 err
= lookup_last(nd
, &path
);
1602 put_link(nd
, &link
, cookie
);
1607 err
= complete_walk(nd
);
1609 if (!err
&& nd
->flags
& LOOKUP_DIRECTORY
) {
1610 if (!nd
->inode
->i_op
->lookup
) {
1611 path_put(&nd
->path
);
1619 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
1620 path_put(&nd
->root
);
1621 nd
->root
.mnt
= NULL
;
1626 static int do_path_lookup(int dfd
, const char *name
,
1627 unsigned int flags
, struct nameidata
*nd
)
1629 int retval
= path_lookupat(dfd
, name
, flags
| LOOKUP_RCU
, nd
);
1630 if (unlikely(retval
== -ECHILD
))
1631 retval
= path_lookupat(dfd
, name
, flags
, nd
);
1632 if (unlikely(retval
== -ESTALE
))
1633 retval
= path_lookupat(dfd
, name
, flags
| LOOKUP_REVAL
, nd
);
1635 if (likely(!retval
)) {
1636 if (unlikely(!audit_dummy_context())) {
1637 if (nd
->path
.dentry
&& nd
->inode
)
1638 audit_inode(name
, nd
->path
.dentry
);
1644 int kern_path_parent(const char *name
, struct nameidata
*nd
)
1646 return do_path_lookup(AT_FDCWD
, name
, LOOKUP_PARENT
, nd
);
1649 int kern_path(const char *name
, unsigned int flags
, struct path
*path
)
1651 struct nameidata nd
;
1652 int res
= do_path_lookup(AT_FDCWD
, name
, flags
, &nd
);
1659 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1660 * @dentry: pointer to dentry of the base directory
1661 * @mnt: pointer to vfs mount of the base directory
1662 * @name: pointer to file name
1663 * @flags: lookup flags
1664 * @nd: pointer to nameidata
1666 int vfs_path_lookup(struct dentry
*dentry
, struct vfsmount
*mnt
,
1667 const char *name
, unsigned int flags
,
1668 struct nameidata
*nd
)
1670 nd
->root
.dentry
= dentry
;
1672 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1673 return do_path_lookup(AT_FDCWD
, name
, flags
| LOOKUP_ROOT
, nd
);
1676 static struct dentry
*__lookup_hash(struct qstr
*name
,
1677 struct dentry
*base
, struct nameidata
*nd
)
1679 struct inode
*inode
= base
->d_inode
;
1680 struct dentry
*dentry
;
1683 err
= exec_permission(inode
, 0);
1685 return ERR_PTR(err
);
1688 * Don't bother with __d_lookup: callers are for creat as
1689 * well as unlink, so a lot of the time it would cost
1692 dentry
= d_lookup(base
, name
);
1694 if (dentry
&& (dentry
->d_flags
& DCACHE_OP_REVALIDATE
))
1695 dentry
= do_revalidate(dentry
, nd
);
1698 dentry
= d_alloc_and_lookup(base
, name
, nd
);
1704 * Restricted form of lookup. Doesn't follow links, single-component only,
1705 * needs parent already locked. Doesn't follow mounts.
1708 static struct dentry
*lookup_hash(struct nameidata
*nd
)
1710 return __lookup_hash(&nd
->last
, nd
->path
.dentry
, nd
);
1714 * lookup_one_len - filesystem helper to lookup single pathname component
1715 * @name: pathname component to lookup
1716 * @base: base directory to lookup from
1717 * @len: maximum length @len should be interpreted to
1719 * Note that this routine is purely a helper for filesystem usage and should
1720 * not be called by generic code. Also note that by using this function the
1721 * nameidata argument is passed to the filesystem methods and a filesystem
1722 * using this helper needs to be prepared for that.
1724 struct dentry
*lookup_one_len(const char *name
, struct dentry
*base
, int len
)
1730 WARN_ON_ONCE(!mutex_is_locked(&base
->d_inode
->i_mutex
));
1735 return ERR_PTR(-EACCES
);
1737 hash
= init_name_hash();
1739 c
= *(const unsigned char *)name
++;
1740 if (c
== '/' || c
== '\0')
1741 return ERR_PTR(-EACCES
);
1742 hash
= partial_name_hash(c
, hash
);
1744 this.hash
= end_name_hash(hash
);
1746 * See if the low-level filesystem might want
1747 * to use its own hash..
1749 if (base
->d_flags
& DCACHE_OP_HASH
) {
1750 int err
= base
->d_op
->d_hash(base
, base
->d_inode
, &this);
1752 return ERR_PTR(err
);
1755 return __lookup_hash(&this, base
, NULL
);
1758 int user_path_at_empty(int dfd
, const char __user
*name
, unsigned flags
,
1759 struct path
*path
, int *empty
)
1761 struct nameidata nd
;
1762 char *tmp
= getname_flags(name
, flags
, empty
);
1763 int err
= PTR_ERR(tmp
);
1766 BUG_ON(flags
& LOOKUP_PARENT
);
1768 err
= do_path_lookup(dfd
, tmp
, flags
, &nd
);
1776 int user_path_at(int dfd
, const char __user
*name
, unsigned flags
,
1779 return user_path_at_empty(dfd
, name
, flags
, path
, 0);
1782 static int user_path_parent(int dfd
, const char __user
*path
,
1783 struct nameidata
*nd
, char **name
)
1785 char *s
= getname(path
);
1791 error
= do_path_lookup(dfd
, s
, LOOKUP_PARENT
, nd
);
1801 * It's inline, so penalty for filesystems that don't use sticky bit is
1804 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
1806 uid_t fsuid
= current_fsuid();
1808 if (!(dir
->i_mode
& S_ISVTX
))
1810 if (current_user_ns() != inode_userns(inode
))
1812 if (inode
->i_uid
== fsuid
)
1814 if (dir
->i_uid
== fsuid
)
1818 return !ns_capable(inode_userns(inode
), CAP_FOWNER
);
1822 * Check whether we can remove a link victim from directory dir, check
1823 * whether the type of victim is right.
1824 * 1. We can't do it if dir is read-only (done in permission())
1825 * 2. We should have write and exec permissions on dir
1826 * 3. We can't remove anything from append-only dir
1827 * 4. We can't do anything with immutable dir (done in permission())
1828 * 5. If the sticky bit on dir is set we should either
1829 * a. be owner of dir, or
1830 * b. be owner of victim, or
1831 * c. have CAP_FOWNER capability
1832 * 6. If the victim is append-only or immutable we can't do antyhing with
1833 * links pointing to it.
1834 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1835 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1836 * 9. We can't remove a root or mountpoint.
1837 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1838 * nfs_async_unlink().
1840 static int may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
1844 if (!victim
->d_inode
)
1847 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
1848 audit_inode_child(victim
, dir
);
1850 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
1855 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
1856 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
1859 if (!S_ISDIR(victim
->d_inode
->i_mode
))
1861 if (IS_ROOT(victim
))
1863 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
1865 if (IS_DEADDIR(dir
))
1867 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
1872 /* Check whether we can create an object with dentry child in directory
1874 * 1. We can't do it if child already exists (open has special treatment for
1875 * this case, but since we are inlined it's OK)
1876 * 2. We can't do it if dir is read-only (done in permission())
1877 * 3. We should have write and exec permissions on dir
1878 * 4. We can't do it if dir is immutable (done in permission())
1880 static inline int may_create(struct inode
*dir
, struct dentry
*child
)
1884 if (IS_DEADDIR(dir
))
1886 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
1890 * p1 and p2 should be directories on the same fs.
1892 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
1897 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1901 mutex_lock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
1903 p
= d_ancestor(p2
, p1
);
1905 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1906 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1910 p
= d_ancestor(p1
, p2
);
1912 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1913 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1917 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1918 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1922 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
1924 mutex_unlock(&p1
->d_inode
->i_mutex
);
1926 mutex_unlock(&p2
->d_inode
->i_mutex
);
1927 mutex_unlock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
1931 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
1932 struct nameidata
*nd
)
1934 int error
= may_create(dir
, dentry
);
1939 if (!dir
->i_op
->create
)
1940 return -EACCES
; /* shouldn't it be ENOSYS? */
1943 error
= security_inode_create(dir
, dentry
, mode
);
1946 error
= dir
->i_op
->create(dir
, dentry
, mode
, nd
);
1948 fsnotify_create(dir
, dentry
);
1952 static int may_open(struct path
*path
, int acc_mode
, int flag
)
1954 struct dentry
*dentry
= path
->dentry
;
1955 struct inode
*inode
= dentry
->d_inode
;
1965 switch (inode
->i_mode
& S_IFMT
) {
1969 if (acc_mode
& MAY_WRITE
)
1974 if (path
->mnt
->mnt_flags
& MNT_NODEV
)
1983 error
= inode_permission(inode
, acc_mode
);
1988 * An append-only file must be opened in append mode for writing.
1990 if (IS_APPEND(inode
)) {
1991 if ((flag
& O_ACCMODE
) != O_RDONLY
&& !(flag
& O_APPEND
))
1997 /* O_NOATIME can only be set by the owner or superuser */
1998 if (flag
& O_NOATIME
&& !inode_owner_or_capable(inode
))
2002 * Ensure there are no outstanding leases on the file.
2004 return break_lease(inode
, flag
);
2007 static int handle_truncate(struct file
*filp
)
2009 struct path
*path
= &filp
->f_path
;
2010 struct inode
*inode
= path
->dentry
->d_inode
;
2011 int error
= get_write_access(inode
);
2015 * Refuse to truncate files with mandatory locks held on them.
2017 error
= locks_verify_locked(inode
);
2019 error
= security_path_truncate(path
);
2021 error
= do_truncate(path
->dentry
, 0,
2022 ATTR_MTIME
|ATTR_CTIME
|ATTR_OPEN
,
2025 put_write_access(inode
);
2030 * Note that while the flag value (low two bits) for sys_open means:
2035 * it is changed into
2036 * 00 - no permissions needed
2037 * 01 - read-permission
2038 * 10 - write-permission
2040 * for the internal routines (ie open_namei()/follow_link() etc)
2041 * This is more logical, and also allows the 00 "no perm needed"
2042 * to be used for symlinks (where the permissions are checked
2046 static inline int open_to_namei_flags(int flag
)
2048 if ((flag
+1) & O_ACCMODE
)
2054 * Handle the last step of open()
2056 static struct file
*do_last(struct nameidata
*nd
, struct path
*path
,
2057 const struct open_flags
*op
, const char *pathname
)
2059 struct dentry
*dir
= nd
->path
.dentry
;
2060 struct dentry
*dentry
;
2061 int open_flag
= op
->open_flag
;
2062 int will_truncate
= open_flag
& O_TRUNC
;
2064 int acc_mode
= op
->acc_mode
;
2068 nd
->flags
&= ~LOOKUP_PARENT
;
2069 nd
->flags
|= op
->intent
;
2071 switch (nd
->last_type
) {
2074 error
= handle_dots(nd
, nd
->last_type
);
2076 return ERR_PTR(error
);
2079 error
= complete_walk(nd
);
2081 return ERR_PTR(error
);
2082 audit_inode(pathname
, nd
->path
.dentry
);
2083 if (open_flag
& O_CREAT
) {
2089 error
= complete_walk(nd
);
2091 return ERR_PTR(error
);
2092 audit_inode(pathname
, dir
);
2096 if (!(open_flag
& O_CREAT
)) {
2098 if (nd
->last
.name
[nd
->last
.len
])
2099 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
2100 if (open_flag
& O_PATH
&& !(nd
->flags
& LOOKUP_FOLLOW
))
2102 /* we _can_ be in RCU mode here */
2103 error
= walk_component(nd
, path
, &nd
->last
, LAST_NORM
,
2106 return ERR_PTR(error
);
2107 if (error
) /* symlink */
2110 error
= complete_walk(nd
);
2112 return ERR_PTR(-ECHILD
);
2115 if (nd
->flags
& LOOKUP_DIRECTORY
) {
2116 if (!nd
->inode
->i_op
->lookup
)
2119 audit_inode(pathname
, nd
->path
.dentry
);
2123 /* create side of things */
2125 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
2126 * cleared when we got to the last component we are about to look up
2128 error
= complete_walk(nd
);
2130 return ERR_PTR(error
);
2132 audit_inode(pathname
, dir
);
2134 /* trailing slashes? */
2135 if (nd
->last
.name
[nd
->last
.len
])
2138 mutex_lock(&dir
->d_inode
->i_mutex
);
2140 dentry
= lookup_hash(nd
);
2141 error
= PTR_ERR(dentry
);
2142 if (IS_ERR(dentry
)) {
2143 mutex_unlock(&dir
->d_inode
->i_mutex
);
2147 path
->dentry
= dentry
;
2148 path
->mnt
= nd
->path
.mnt
;
2150 /* Negative dentry, just create the file */
2151 if (!dentry
->d_inode
) {
2152 int mode
= op
->mode
;
2153 if (!IS_POSIXACL(dir
->d_inode
))
2154 mode
&= ~current_umask();
2156 * This write is needed to ensure that a
2157 * rw->ro transition does not occur between
2158 * the time when the file is created and when
2159 * a permanent write count is taken through
2160 * the 'struct file' in nameidata_to_filp().
2162 error
= mnt_want_write(nd
->path
.mnt
);
2164 goto exit_mutex_unlock
;
2166 /* Don't check for write permission, don't truncate */
2167 open_flag
&= ~O_TRUNC
;
2169 acc_mode
= MAY_OPEN
;
2170 error
= security_path_mknod(&nd
->path
, dentry
, mode
, 0);
2172 goto exit_mutex_unlock
;
2173 error
= vfs_create(dir
->d_inode
, dentry
, mode
, nd
);
2175 goto exit_mutex_unlock
;
2176 mutex_unlock(&dir
->d_inode
->i_mutex
);
2177 dput(nd
->path
.dentry
);
2178 nd
->path
.dentry
= dentry
;
2183 * It already exists.
2185 mutex_unlock(&dir
->d_inode
->i_mutex
);
2186 audit_inode(pathname
, path
->dentry
);
2189 if (open_flag
& O_EXCL
)
2192 error
= follow_managed(path
, nd
->flags
);
2197 nd
->flags
|= LOOKUP_JUMPED
;
2200 if (!path
->dentry
->d_inode
)
2203 if (path
->dentry
->d_inode
->i_op
->follow_link
)
2206 path_to_nameidata(path
, nd
);
2207 nd
->inode
= path
->dentry
->d_inode
;
2208 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2209 error
= complete_walk(nd
);
2213 if (S_ISDIR(nd
->inode
->i_mode
))
2216 if (!S_ISREG(nd
->inode
->i_mode
))
2219 if (will_truncate
) {
2220 error
= mnt_want_write(nd
->path
.mnt
);
2226 error
= may_open(&nd
->path
, acc_mode
, open_flag
);
2229 filp
= nameidata_to_filp(nd
);
2230 if (!IS_ERR(filp
)) {
2231 error
= ima_file_check(filp
, op
->acc_mode
);
2234 filp
= ERR_PTR(error
);
2237 if (!IS_ERR(filp
)) {
2238 if (will_truncate
) {
2239 error
= handle_truncate(filp
);
2242 filp
= ERR_PTR(error
);
2248 mnt_drop_write(nd
->path
.mnt
);
2249 path_put(&nd
->path
);
2253 mutex_unlock(&dir
->d_inode
->i_mutex
);
2255 path_put_conditional(path
, nd
);
2257 filp
= ERR_PTR(error
);
2261 static struct file
*path_openat(int dfd
, const char *pathname
,
2262 struct nameidata
*nd
, const struct open_flags
*op
, int flags
)
2264 struct file
*base
= NULL
;
2269 filp
= get_empty_filp();
2271 return ERR_PTR(-ENFILE
);
2273 filp
->f_flags
= op
->open_flag
;
2274 nd
->intent
.open
.file
= filp
;
2275 nd
->intent
.open
.flags
= open_to_namei_flags(op
->open_flag
);
2276 nd
->intent
.open
.create_mode
= op
->mode
;
2278 error
= path_init(dfd
, pathname
, flags
| LOOKUP_PARENT
, nd
, &base
);
2279 if (unlikely(error
))
2282 current
->total_link_count
= 0;
2283 error
= link_path_walk(pathname
, nd
);
2284 if (unlikely(error
))
2287 filp
= do_last(nd
, &path
, op
, pathname
);
2288 while (unlikely(!filp
)) { /* trailing symlink */
2289 struct path link
= path
;
2291 if (!(nd
->flags
& LOOKUP_FOLLOW
)) {
2292 path_put_conditional(&path
, nd
);
2293 path_put(&nd
->path
);
2294 filp
= ERR_PTR(-ELOOP
);
2297 nd
->flags
|= LOOKUP_PARENT
;
2298 nd
->flags
&= ~(LOOKUP_OPEN
|LOOKUP_CREATE
|LOOKUP_EXCL
);
2299 error
= follow_link(&link
, nd
, &cookie
);
2300 if (unlikely(error
))
2301 filp
= ERR_PTR(error
);
2303 filp
= do_last(nd
, &path
, op
, pathname
);
2304 put_link(nd
, &link
, cookie
);
2307 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
))
2308 path_put(&nd
->root
);
2311 release_open_intent(nd
);
2315 filp
= ERR_PTR(error
);
2319 struct file
*do_filp_open(int dfd
, const char *pathname
,
2320 const struct open_flags
*op
, int flags
)
2322 struct nameidata nd
;
2325 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
| LOOKUP_RCU
);
2326 if (unlikely(filp
== ERR_PTR(-ECHILD
)))
2327 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
);
2328 if (unlikely(filp
== ERR_PTR(-ESTALE
)))
2329 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
| LOOKUP_REVAL
);
2333 struct file
*do_file_open_root(struct dentry
*dentry
, struct vfsmount
*mnt
,
2334 const char *name
, const struct open_flags
*op
, int flags
)
2336 struct nameidata nd
;
2340 nd
.root
.dentry
= dentry
;
2342 flags
|= LOOKUP_ROOT
;
2344 if (dentry
->d_inode
->i_op
->follow_link
&& op
->intent
& LOOKUP_OPEN
)
2345 return ERR_PTR(-ELOOP
);
2347 file
= path_openat(-1, name
, &nd
, op
, flags
| LOOKUP_RCU
);
2348 if (unlikely(file
== ERR_PTR(-ECHILD
)))
2349 file
= path_openat(-1, name
, &nd
, op
, flags
);
2350 if (unlikely(file
== ERR_PTR(-ESTALE
)))
2351 file
= path_openat(-1, name
, &nd
, op
, flags
| LOOKUP_REVAL
);
2356 * lookup_create - lookup a dentry, creating it if it doesn't exist
2357 * @nd: nameidata info
2358 * @is_dir: directory flag
2360 * Simple function to lookup and return a dentry and create it
2361 * if it doesn't exist. Is SMP-safe.
2363 * Returns with nd->path.dentry->d_inode->i_mutex locked.
2365 struct dentry
*lookup_create(struct nameidata
*nd
, int is_dir
)
2367 struct dentry
*dentry
= ERR_PTR(-EEXIST
);
2369 mutex_lock_nested(&nd
->path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2371 * Yucky last component or no last component at all?
2372 * (foo/., foo/.., /////)
2374 if (nd
->last_type
!= LAST_NORM
)
2376 nd
->flags
&= ~LOOKUP_PARENT
;
2377 nd
->flags
|= LOOKUP_CREATE
| LOOKUP_EXCL
;
2378 nd
->intent
.open
.flags
= O_EXCL
;
2381 * Do the final lookup.
2383 dentry
= lookup_hash(nd
);
2387 if (dentry
->d_inode
)
2390 * Special case - lookup gave negative, but... we had foo/bar/
2391 * From the vfs_mknod() POV we just have a negative dentry -
2392 * all is fine. Let's be bastards - you had / on the end, you've
2393 * been asking for (non-existent) directory. -ENOENT for you.
2395 if (unlikely(!is_dir
&& nd
->last
.name
[nd
->last
.len
])) {
2397 dentry
= ERR_PTR(-ENOENT
);
2402 dentry
= ERR_PTR(-EEXIST
);
2406 EXPORT_SYMBOL_GPL(lookup_create
);
2408 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
2410 int error
= may_create(dir
, dentry
);
2415 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) &&
2416 !ns_capable(inode_userns(dir
), CAP_MKNOD
))
2419 if (!dir
->i_op
->mknod
)
2422 error
= devcgroup_inode_mknod(mode
, dev
);
2426 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
2430 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
2432 fsnotify_create(dir
, dentry
);
2436 static int may_mknod(mode_t mode
)
2438 switch (mode
& S_IFMT
) {
2444 case 0: /* zero mode translates to S_IFREG */
2453 SYSCALL_DEFINE4(mknodat
, int, dfd
, const char __user
*, filename
, int, mode
,
2458 struct dentry
*dentry
;
2459 struct nameidata nd
;
2464 error
= user_path_parent(dfd
, filename
, &nd
, &tmp
);
2468 dentry
= lookup_create(&nd
, 0);
2469 if (IS_ERR(dentry
)) {
2470 error
= PTR_ERR(dentry
);
2473 if (!IS_POSIXACL(nd
.path
.dentry
->d_inode
))
2474 mode
&= ~current_umask();
2475 error
= may_mknod(mode
);
2478 error
= mnt_want_write(nd
.path
.mnt
);
2481 error
= security_path_mknod(&nd
.path
, dentry
, mode
, dev
);
2483 goto out_drop_write
;
2484 switch (mode
& S_IFMT
) {
2485 case 0: case S_IFREG
:
2486 error
= vfs_create(nd
.path
.dentry
->d_inode
,dentry
,mode
,&nd
);
2488 case S_IFCHR
: case S_IFBLK
:
2489 error
= vfs_mknod(nd
.path
.dentry
->d_inode
,dentry
,mode
,
2490 new_decode_dev(dev
));
2492 case S_IFIFO
: case S_IFSOCK
:
2493 error
= vfs_mknod(nd
.path
.dentry
->d_inode
,dentry
,mode
,0);
2497 mnt_drop_write(nd
.path
.mnt
);
2501 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2508 SYSCALL_DEFINE3(mknod
, const char __user
*, filename
, int, mode
, unsigned, dev
)
2510 return sys_mknodat(AT_FDCWD
, filename
, mode
, dev
);
2513 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2515 int error
= may_create(dir
, dentry
);
2520 if (!dir
->i_op
->mkdir
)
2523 mode
&= (S_IRWXUGO
|S_ISVTX
);
2524 error
= security_inode_mkdir(dir
, dentry
, mode
);
2528 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
2530 fsnotify_mkdir(dir
, dentry
);
2534 SYSCALL_DEFINE3(mkdirat
, int, dfd
, const char __user
*, pathname
, int, mode
)
2538 struct dentry
*dentry
;
2539 struct nameidata nd
;
2541 error
= user_path_parent(dfd
, pathname
, &nd
, &tmp
);
2545 dentry
= lookup_create(&nd
, 1);
2546 error
= PTR_ERR(dentry
);
2550 if (!IS_POSIXACL(nd
.path
.dentry
->d_inode
))
2551 mode
&= ~current_umask();
2552 error
= mnt_want_write(nd
.path
.mnt
);
2555 error
= security_path_mkdir(&nd
.path
, dentry
, mode
);
2557 goto out_drop_write
;
2558 error
= vfs_mkdir(nd
.path
.dentry
->d_inode
, dentry
, mode
);
2560 mnt_drop_write(nd
.path
.mnt
);
2564 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2571 SYSCALL_DEFINE2(mkdir
, const char __user
*, pathname
, int, mode
)
2573 return sys_mkdirat(AT_FDCWD
, pathname
, mode
);
2577 * The dentry_unhash() helper will try to drop the dentry early: we
2578 * should have a usage count of 2 if we're the only user of this
2579 * dentry, and if that is true (possibly after pruning the dcache),
2580 * then we drop the dentry now.
2582 * A low-level filesystem can, if it choses, legally
2585 * if (!d_unhashed(dentry))
2588 * if it cannot handle the case of removing a directory
2589 * that is still in use by something else..
2591 void dentry_unhash(struct dentry
*dentry
)
2593 shrink_dcache_parent(dentry
);
2594 spin_lock(&dentry
->d_lock
);
2595 if (dentry
->d_count
== 1)
2597 spin_unlock(&dentry
->d_lock
);
2600 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2602 int error
= may_delete(dir
, dentry
, 1);
2607 if (!dir
->i_op
->rmdir
)
2611 mutex_lock(&dentry
->d_inode
->i_mutex
);
2614 if (d_mountpoint(dentry
))
2617 error
= security_inode_rmdir(dir
, dentry
);
2621 shrink_dcache_parent(dentry
);
2622 error
= dir
->i_op
->rmdir(dir
, dentry
);
2626 dentry
->d_inode
->i_flags
|= S_DEAD
;
2630 mutex_unlock(&dentry
->d_inode
->i_mutex
);
2637 static long do_rmdir(int dfd
, const char __user
*pathname
)
2641 struct dentry
*dentry
;
2642 struct nameidata nd
;
2644 error
= user_path_parent(dfd
, pathname
, &nd
, &name
);
2648 switch(nd
.last_type
) {
2660 nd
.flags
&= ~LOOKUP_PARENT
;
2662 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2663 dentry
= lookup_hash(&nd
);
2664 error
= PTR_ERR(dentry
);
2667 if (!dentry
->d_inode
) {
2671 error
= mnt_want_write(nd
.path
.mnt
);
2674 error
= security_path_rmdir(&nd
.path
, dentry
);
2677 error
= vfs_rmdir(nd
.path
.dentry
->d_inode
, dentry
);
2679 mnt_drop_write(nd
.path
.mnt
);
2683 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2690 SYSCALL_DEFINE1(rmdir
, const char __user
*, pathname
)
2692 return do_rmdir(AT_FDCWD
, pathname
);
2695 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
2697 int error
= may_delete(dir
, dentry
, 0);
2702 if (!dir
->i_op
->unlink
)
2705 mutex_lock(&dentry
->d_inode
->i_mutex
);
2706 if (d_mountpoint(dentry
))
2709 error
= security_inode_unlink(dir
, dentry
);
2711 error
= dir
->i_op
->unlink(dir
, dentry
);
2716 mutex_unlock(&dentry
->d_inode
->i_mutex
);
2718 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2719 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
2720 fsnotify_link_count(dentry
->d_inode
);
2728 * Make sure that the actual truncation of the file will occur outside its
2729 * directory's i_mutex. Truncate can take a long time if there is a lot of
2730 * writeout happening, and we don't want to prevent access to the directory
2731 * while waiting on the I/O.
2733 static long do_unlinkat(int dfd
, const char __user
*pathname
)
2737 struct dentry
*dentry
;
2738 struct nameidata nd
;
2739 struct inode
*inode
= NULL
;
2741 error
= user_path_parent(dfd
, pathname
, &nd
, &name
);
2746 if (nd
.last_type
!= LAST_NORM
)
2749 nd
.flags
&= ~LOOKUP_PARENT
;
2751 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2752 dentry
= lookup_hash(&nd
);
2753 error
= PTR_ERR(dentry
);
2754 if (!IS_ERR(dentry
)) {
2755 /* Why not before? Because we want correct error value */
2756 if (nd
.last
.name
[nd
.last
.len
])
2758 inode
= dentry
->d_inode
;
2762 error
= mnt_want_write(nd
.path
.mnt
);
2765 error
= security_path_unlink(&nd
.path
, dentry
);
2768 error
= vfs_unlink(nd
.path
.dentry
->d_inode
, dentry
);
2770 mnt_drop_write(nd
.path
.mnt
);
2774 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2776 iput(inode
); /* truncate the inode here */
2783 error
= !dentry
->d_inode
? -ENOENT
:
2784 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
2788 SYSCALL_DEFINE3(unlinkat
, int, dfd
, const char __user
*, pathname
, int, flag
)
2790 if ((flag
& ~AT_REMOVEDIR
) != 0)
2793 if (flag
& AT_REMOVEDIR
)
2794 return do_rmdir(dfd
, pathname
);
2796 return do_unlinkat(dfd
, pathname
);
2799 SYSCALL_DEFINE1(unlink
, const char __user
*, pathname
)
2801 return do_unlinkat(AT_FDCWD
, pathname
);
2804 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
2806 int error
= may_create(dir
, dentry
);
2811 if (!dir
->i_op
->symlink
)
2814 error
= security_inode_symlink(dir
, dentry
, oldname
);
2818 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
2820 fsnotify_create(dir
, dentry
);
2824 SYSCALL_DEFINE3(symlinkat
, const char __user
*, oldname
,
2825 int, newdfd
, const char __user
*, newname
)
2830 struct dentry
*dentry
;
2831 struct nameidata nd
;
2833 from
= getname(oldname
);
2835 return PTR_ERR(from
);
2837 error
= user_path_parent(newdfd
, newname
, &nd
, &to
);
2841 dentry
= lookup_create(&nd
, 0);
2842 error
= PTR_ERR(dentry
);
2846 error
= mnt_want_write(nd
.path
.mnt
);
2849 error
= security_path_symlink(&nd
.path
, dentry
, from
);
2851 goto out_drop_write
;
2852 error
= vfs_symlink(nd
.path
.dentry
->d_inode
, dentry
, from
);
2854 mnt_drop_write(nd
.path
.mnt
);
2858 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2866 SYSCALL_DEFINE2(symlink
, const char __user
*, oldname
, const char __user
*, newname
)
2868 return sys_symlinkat(oldname
, AT_FDCWD
, newname
);
2871 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
2873 struct inode
*inode
= old_dentry
->d_inode
;
2879 error
= may_create(dir
, new_dentry
);
2883 if (dir
->i_sb
!= inode
->i_sb
)
2887 * A link to an append-only or immutable file cannot be created.
2889 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2891 if (!dir
->i_op
->link
)
2893 if (S_ISDIR(inode
->i_mode
))
2896 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
2900 mutex_lock(&inode
->i_mutex
);
2901 /* Make sure we don't allow creating hardlink to an unlinked file */
2902 if (inode
->i_nlink
== 0)
2905 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
2906 mutex_unlock(&inode
->i_mutex
);
2908 fsnotify_link(dir
, inode
, new_dentry
);
2913 * Hardlinks are often used in delicate situations. We avoid
2914 * security-related surprises by not following symlinks on the
2917 * We don't follow them on the oldname either to be compatible
2918 * with linux 2.0, and to avoid hard-linking to directories
2919 * and other special files. --ADM
2921 SYSCALL_DEFINE5(linkat
, int, olddfd
, const char __user
*, oldname
,
2922 int, newdfd
, const char __user
*, newname
, int, flags
)
2924 struct dentry
*new_dentry
;
2925 struct nameidata nd
;
2926 struct path old_path
;
2931 if ((flags
& ~(AT_SYMLINK_FOLLOW
| AT_EMPTY_PATH
)) != 0)
2934 * To use null names we require CAP_DAC_READ_SEARCH
2935 * This ensures that not everyone will be able to create
2936 * handlink using the passed filedescriptor.
2938 if (flags
& AT_EMPTY_PATH
) {
2939 if (!capable(CAP_DAC_READ_SEARCH
))
2944 if (flags
& AT_SYMLINK_FOLLOW
)
2945 how
|= LOOKUP_FOLLOW
;
2947 error
= user_path_at(olddfd
, oldname
, how
, &old_path
);
2951 error
= user_path_parent(newdfd
, newname
, &nd
, &to
);
2955 if (old_path
.mnt
!= nd
.path
.mnt
)
2957 new_dentry
= lookup_create(&nd
, 0);
2958 error
= PTR_ERR(new_dentry
);
2959 if (IS_ERR(new_dentry
))
2961 error
= mnt_want_write(nd
.path
.mnt
);
2964 error
= security_path_link(old_path
.dentry
, &nd
.path
, new_dentry
);
2966 goto out_drop_write
;
2967 error
= vfs_link(old_path
.dentry
, nd
.path
.dentry
->d_inode
, new_dentry
);
2969 mnt_drop_write(nd
.path
.mnt
);
2973 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2978 path_put(&old_path
);
2983 SYSCALL_DEFINE2(link
, const char __user
*, oldname
, const char __user
*, newname
)
2985 return sys_linkat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
, 0);
2989 * The worst of all namespace operations - renaming directory. "Perverted"
2990 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2992 * a) we can get into loop creation. Check is done in is_subdir().
2993 * b) race potential - two innocent renames can create a loop together.
2994 * That's where 4.4 screws up. Current fix: serialization on
2995 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
2997 * c) we have to lock _three_ objects - parents and victim (if it exists).
2998 * And that - after we got ->i_mutex on parents (until then we don't know
2999 * whether the target exists). Solution: try to be smart with locking
3000 * order for inodes. We rely on the fact that tree topology may change
3001 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
3002 * move will be locked. Thus we can rank directories by the tree
3003 * (ancestors first) and rank all non-directories after them.
3004 * That works since everybody except rename does "lock parent, lookup,
3005 * lock child" and rename is under ->s_vfs_rename_mutex.
3006 * HOWEVER, it relies on the assumption that any object with ->lookup()
3007 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3008 * we'd better make sure that there's no link(2) for them.
3009 * d) conversion from fhandle to dentry may come in the wrong moment - when
3010 * we are removing the target. Solution: we will have to grab ->i_mutex
3011 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3012 * ->i_mutex on parents, which works but leads to some truly excessive
3015 static int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
3016 struct inode
*new_dir
, struct dentry
*new_dentry
)
3019 struct inode
*target
= new_dentry
->d_inode
;
3022 * If we are going to change the parent - check write permissions,
3023 * we'll need to flip '..'.
3025 if (new_dir
!= old_dir
) {
3026 error
= inode_permission(old_dentry
->d_inode
, MAY_WRITE
);
3031 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3037 mutex_lock(&target
->i_mutex
);
3040 if (d_mountpoint(old_dentry
) || d_mountpoint(new_dentry
))
3044 shrink_dcache_parent(new_dentry
);
3045 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3050 target
->i_flags
|= S_DEAD
;
3051 dont_mount(new_dentry
);
3055 mutex_unlock(&target
->i_mutex
);
3058 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
))
3059 d_move(old_dentry
,new_dentry
);
3063 static int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
3064 struct inode
*new_dir
, struct dentry
*new_dentry
)
3066 struct inode
*target
= new_dentry
->d_inode
;
3069 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3075 mutex_lock(&target
->i_mutex
);
3078 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
3081 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3086 dont_mount(new_dentry
);
3087 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
))
3088 d_move(old_dentry
, new_dentry
);
3091 mutex_unlock(&target
->i_mutex
);
3096 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
3097 struct inode
*new_dir
, struct dentry
*new_dentry
)
3100 int is_dir
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
3101 const unsigned char *old_name
;
3103 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
3106 error
= may_delete(old_dir
, old_dentry
, is_dir
);
3110 if (!new_dentry
->d_inode
)
3111 error
= may_create(new_dir
, new_dentry
);
3113 error
= may_delete(new_dir
, new_dentry
, is_dir
);
3117 if (!old_dir
->i_op
->rename
)
3120 old_name
= fsnotify_oldname_init(old_dentry
->d_name
.name
);
3123 error
= vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
3125 error
= vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
3127 fsnotify_move(old_dir
, new_dir
, old_name
, is_dir
,
3128 new_dentry
->d_inode
, old_dentry
);
3129 fsnotify_oldname_free(old_name
);
3134 SYSCALL_DEFINE4(renameat
, int, olddfd
, const char __user
*, oldname
,
3135 int, newdfd
, const char __user
*, newname
)
3137 struct dentry
*old_dir
, *new_dir
;
3138 struct dentry
*old_dentry
, *new_dentry
;
3139 struct dentry
*trap
;
3140 struct nameidata oldnd
, newnd
;
3145 error
= user_path_parent(olddfd
, oldname
, &oldnd
, &from
);
3149 error
= user_path_parent(newdfd
, newname
, &newnd
, &to
);
3154 if (oldnd
.path
.mnt
!= newnd
.path
.mnt
)
3157 old_dir
= oldnd
.path
.dentry
;
3159 if (oldnd
.last_type
!= LAST_NORM
)
3162 new_dir
= newnd
.path
.dentry
;
3163 if (newnd
.last_type
!= LAST_NORM
)
3166 oldnd
.flags
&= ~LOOKUP_PARENT
;
3167 newnd
.flags
&= ~LOOKUP_PARENT
;
3168 newnd
.flags
|= LOOKUP_RENAME_TARGET
;
3170 trap
= lock_rename(new_dir
, old_dir
);
3172 old_dentry
= lookup_hash(&oldnd
);
3173 error
= PTR_ERR(old_dentry
);
3174 if (IS_ERR(old_dentry
))
3176 /* source must exist */
3178 if (!old_dentry
->d_inode
)
3180 /* unless the source is a directory trailing slashes give -ENOTDIR */
3181 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
3183 if (oldnd
.last
.name
[oldnd
.last
.len
])
3185 if (newnd
.last
.name
[newnd
.last
.len
])
3188 /* source should not be ancestor of target */
3190 if (old_dentry
== trap
)
3192 new_dentry
= lookup_hash(&newnd
);
3193 error
= PTR_ERR(new_dentry
);
3194 if (IS_ERR(new_dentry
))
3196 /* target should not be an ancestor of source */
3198 if (new_dentry
== trap
)
3201 error
= mnt_want_write(oldnd
.path
.mnt
);
3204 error
= security_path_rename(&oldnd
.path
, old_dentry
,
3205 &newnd
.path
, new_dentry
);
3208 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
3209 new_dir
->d_inode
, new_dentry
);
3211 mnt_drop_write(oldnd
.path
.mnt
);
3217 unlock_rename(new_dir
, old_dir
);
3219 path_put(&newnd
.path
);
3222 path_put(&oldnd
.path
);
3228 SYSCALL_DEFINE2(rename
, const char __user
*, oldname
, const char __user
*, newname
)
3230 return sys_renameat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
);
3233 int vfs_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
, const char *link
)
3237 len
= PTR_ERR(link
);
3242 if (len
> (unsigned) buflen
)
3244 if (copy_to_user(buffer
, link
, len
))
3251 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3252 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3253 * using) it for any given inode is up to filesystem.
3255 int generic_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
3257 struct nameidata nd
;
3262 cookie
= dentry
->d_inode
->i_op
->follow_link(dentry
, &nd
);
3264 return PTR_ERR(cookie
);
3266 res
= vfs_readlink(dentry
, buffer
, buflen
, nd_get_link(&nd
));
3267 if (dentry
->d_inode
->i_op
->put_link
)
3268 dentry
->d_inode
->i_op
->put_link(dentry
, &nd
, cookie
);
3272 int vfs_follow_link(struct nameidata
*nd
, const char *link
)
3274 return __vfs_follow_link(nd
, link
);
3277 /* get the link contents into pagecache */
3278 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
3282 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
3283 page
= read_mapping_page(mapping
, 0, NULL
);
3288 nd_terminate_link(kaddr
, dentry
->d_inode
->i_size
, PAGE_SIZE
- 1);
3292 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
3294 struct page
*page
= NULL
;
3295 char *s
= page_getlink(dentry
, &page
);
3296 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
3299 page_cache_release(page
);
3304 void *page_follow_link_light(struct dentry
*dentry
, struct nameidata
*nd
)
3306 struct page
*page
= NULL
;
3307 nd_set_link(nd
, page_getlink(dentry
, &page
));
3311 void page_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *cookie
)
3313 struct page
*page
= cookie
;
3317 page_cache_release(page
);
3322 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3324 int __page_symlink(struct inode
*inode
, const char *symname
, int len
, int nofs
)
3326 struct address_space
*mapping
= inode
->i_mapping
;
3331 unsigned int flags
= AOP_FLAG_UNINTERRUPTIBLE
;
3333 flags
|= AOP_FLAG_NOFS
;
3336 err
= pagecache_write_begin(NULL
, mapping
, 0, len
-1,
3337 flags
, &page
, &fsdata
);
3341 kaddr
= kmap_atomic(page
, KM_USER0
);
3342 memcpy(kaddr
, symname
, len
-1);
3343 kunmap_atomic(kaddr
, KM_USER0
);
3345 err
= pagecache_write_end(NULL
, mapping
, 0, len
-1, len
-1,
3352 mark_inode_dirty(inode
);
3358 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
3360 return __page_symlink(inode
, symname
, len
,
3361 !(mapping_gfp_mask(inode
->i_mapping
) & __GFP_FS
));
3364 const struct inode_operations page_symlink_inode_operations
= {
3365 .readlink
= generic_readlink
,
3366 .follow_link
= page_follow_link_light
,
3367 .put_link
= page_put_link
,
3370 EXPORT_SYMBOL(user_path_at
);
3371 EXPORT_SYMBOL(follow_down_one
);
3372 EXPORT_SYMBOL(follow_down
);
3373 EXPORT_SYMBOL(follow_up
);
3374 EXPORT_SYMBOL(get_write_access
); /* binfmt_aout */
3375 EXPORT_SYMBOL(getname
);
3376 EXPORT_SYMBOL(lock_rename
);
3377 EXPORT_SYMBOL(lookup_one_len
);
3378 EXPORT_SYMBOL(page_follow_link_light
);
3379 EXPORT_SYMBOL(page_put_link
);
3380 EXPORT_SYMBOL(page_readlink
);
3381 EXPORT_SYMBOL(__page_symlink
);
3382 EXPORT_SYMBOL(page_symlink
);
3383 EXPORT_SYMBOL(page_symlink_inode_operations
);
3384 EXPORT_SYMBOL(kern_path_parent
);
3385 EXPORT_SYMBOL(kern_path
);
3386 EXPORT_SYMBOL(vfs_path_lookup
);
3387 EXPORT_SYMBOL(inode_permission
);
3388 EXPORT_SYMBOL(file_permission
);
3389 EXPORT_SYMBOL(unlock_rename
);
3390 EXPORT_SYMBOL(vfs_create
);
3391 EXPORT_SYMBOL(vfs_follow_link
);
3392 EXPORT_SYMBOL(vfs_link
);
3393 EXPORT_SYMBOL(vfs_mkdir
);
3394 EXPORT_SYMBOL(vfs_mknod
);
3395 EXPORT_SYMBOL(generic_permission
);
3396 EXPORT_SYMBOL(vfs_readlink
);
3397 EXPORT_SYMBOL(vfs_rename
);
3398 EXPORT_SYMBOL(vfs_rmdir
);
3399 EXPORT_SYMBOL(vfs_symlink
);
3400 EXPORT_SYMBOL(vfs_unlink
);
3401 EXPORT_SYMBOL(dentry_unhash
);
3402 EXPORT_SYMBOL(generic_readlink
);