4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Some corrections by tytso.
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
17 #include <linux/init.h>
18 #include <linux/export.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
22 #include <linux/namei.h>
23 #include <linux/pagemap.h>
24 #include <linux/fsnotify.h>
25 #include <linux/personality.h>
26 #include <linux/security.h>
27 #include <linux/ima.h>
28 #include <linux/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <linux/capability.h>
32 #include <linux/file.h>
33 #include <linux/fcntl.h>
34 #include <linux/device_cgroup.h>
35 #include <linux/fs_struct.h>
36 #include <linux/posix_acl.h>
37 #include <linux/hash.h>
38 #include <asm/uaccess.h>
43 /* [Feb-1997 T. Schoebel-Theuer]
44 * Fundamental changes in the pathname lookup mechanisms (namei)
45 * were necessary because of omirr. The reason is that omirr needs
46 * to know the _real_ pathname, not the user-supplied one, in case
47 * of symlinks (and also when transname replacements occur).
49 * The new code replaces the old recursive symlink resolution with
50 * an iterative one (in case of non-nested symlink chains). It does
51 * this with calls to <fs>_follow_link().
52 * As a side effect, dir_namei(), _namei() and follow_link() are now
53 * replaced with a single function lookup_dentry() that can handle all
54 * the special cases of the former code.
56 * With the new dcache, the pathname is stored at each inode, at least as
57 * long as the refcount of the inode is positive. As a side effect, the
58 * size of the dcache depends on the inode cache and thus is dynamic.
60 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
61 * resolution to correspond with current state of the code.
63 * Note that the symlink resolution is not *completely* iterative.
64 * There is still a significant amount of tail- and mid- recursion in
65 * the algorithm. Also, note that <fs>_readlink() is not used in
66 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
67 * may return different results than <fs>_follow_link(). Many virtual
68 * filesystems (including /proc) exhibit this behavior.
71 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
72 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
73 * and the name already exists in form of a symlink, try to create the new
74 * name indicated by the symlink. The old code always complained that the
75 * name already exists, due to not following the symlink even if its target
76 * is nonexistent. The new semantics affects also mknod() and link() when
77 * the name is a symlink pointing to a non-existent name.
79 * I don't know which semantics is the right one, since I have no access
80 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
81 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
82 * "old" one. Personally, I think the new semantics is much more logical.
83 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
84 * file does succeed in both HP-UX and SunOs, but not in Solaris
85 * and in the old Linux semantics.
88 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
89 * semantics. See the comments in "open_namei" and "do_link" below.
91 * [10-Sep-98 Alan Modra] Another symlink change.
94 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
95 * inside the path - always follow.
96 * in the last component in creation/removal/renaming - never follow.
97 * if LOOKUP_FOLLOW passed - follow.
98 * if the pathname has trailing slashes - follow.
99 * otherwise - don't follow.
100 * (applied in that order).
102 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
103 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
104 * During the 2.4 we need to fix the userland stuff depending on it -
105 * hopefully we will be able to get rid of that wart in 2.5. So far only
106 * XEmacs seems to be relying on it...
109 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
110 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
111 * any extra contention...
114 /* In order to reduce some races, while at the same time doing additional
115 * checking and hopefully speeding things up, we copy filenames to the
116 * kernel data space before using them..
118 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
119 * PATH_MAX includes the nul terminator --RR.
122 #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
125 getname_flags(const char __user
*filename
, int flags
, int *empty
)
127 struct filename
*result
;
131 result
= audit_reusename(filename
);
135 result
= __getname();
136 if (unlikely(!result
))
137 return ERR_PTR(-ENOMEM
);
140 * First, try to embed the struct filename inside the names_cache
143 kname
= (char *)result
->iname
;
144 result
->name
= kname
;
146 len
= strncpy_from_user(kname
, filename
, EMBEDDED_NAME_MAX
);
147 if (unlikely(len
< 0)) {
153 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
154 * separate struct filename so we can dedicate the entire
155 * names_cache allocation for the pathname, and re-do the copy from
158 if (unlikely(len
== EMBEDDED_NAME_MAX
)) {
159 const size_t size
= offsetof(struct filename
, iname
[1]);
160 kname
= (char *)result
;
163 * size is chosen that way we to guarantee that
164 * result->iname[0] is within the same object and that
165 * kname can't be equal to result->iname, no matter what.
167 result
= kzalloc(size
, GFP_KERNEL
);
168 if (unlikely(!result
)) {
170 return ERR_PTR(-ENOMEM
);
172 result
->name
= kname
;
173 len
= strncpy_from_user(kname
, filename
, PATH_MAX
);
174 if (unlikely(len
< 0)) {
179 if (unlikely(len
== PATH_MAX
)) {
182 return ERR_PTR(-ENAMETOOLONG
);
187 /* The empty path is special. */
188 if (unlikely(!len
)) {
191 if (!(flags
& LOOKUP_EMPTY
)) {
193 return ERR_PTR(-ENOENT
);
197 result
->uptr
= filename
;
198 result
->aname
= NULL
;
199 audit_getname(result
);
204 getname(const char __user
* filename
)
206 return getname_flags(filename
, 0, NULL
);
210 getname_kernel(const char * filename
)
212 struct filename
*result
;
213 int len
= strlen(filename
) + 1;
215 result
= __getname();
216 if (unlikely(!result
))
217 return ERR_PTR(-ENOMEM
);
219 if (len
<= EMBEDDED_NAME_MAX
) {
220 result
->name
= (char *)result
->iname
;
221 } else if (len
<= PATH_MAX
) {
222 struct filename
*tmp
;
224 tmp
= kmalloc(sizeof(*tmp
), GFP_KERNEL
);
225 if (unlikely(!tmp
)) {
227 return ERR_PTR(-ENOMEM
);
229 tmp
->name
= (char *)result
;
233 return ERR_PTR(-ENAMETOOLONG
);
235 memcpy((char *)result
->name
, filename
, len
);
237 result
->aname
= NULL
;
239 audit_getname(result
);
244 void putname(struct filename
*name
)
246 BUG_ON(name
->refcnt
<= 0);
248 if (--name
->refcnt
> 0)
251 if (name
->name
!= name
->iname
) {
252 __putname(name
->name
);
258 static int check_acl(struct inode
*inode
, int mask
)
260 #ifdef CONFIG_FS_POSIX_ACL
261 struct posix_acl
*acl
;
263 if (mask
& MAY_NOT_BLOCK
) {
264 acl
= get_cached_acl_rcu(inode
, ACL_TYPE_ACCESS
);
267 /* no ->get_acl() calls in RCU mode... */
268 if (acl
== ACL_NOT_CACHED
)
270 return posix_acl_permission(inode
, acl
, mask
& ~MAY_NOT_BLOCK
);
273 acl
= get_acl(inode
, ACL_TYPE_ACCESS
);
277 int error
= posix_acl_permission(inode
, acl
, mask
);
278 posix_acl_release(acl
);
287 * This does the basic permission checking
289 static int acl_permission_check(struct inode
*inode
, int mask
)
291 unsigned int mode
= inode
->i_mode
;
293 if (likely(uid_eq(current_fsuid(), inode
->i_uid
)))
296 if (IS_POSIXACL(inode
) && (mode
& S_IRWXG
)) {
297 int error
= check_acl(inode
, mask
);
298 if (error
!= -EAGAIN
)
302 if (in_group_p(inode
->i_gid
))
307 * If the DACs are ok we don't need any capability check.
309 if ((mask
& ~mode
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
)) == 0)
315 * generic_permission - check for access rights on a Posix-like filesystem
316 * @inode: inode to check access rights for
317 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
319 * Used to check for read/write/execute permissions on a file.
320 * We use "fsuid" for this, letting us set arbitrary permissions
321 * for filesystem access without changing the "normal" uids which
322 * are used for other things.
324 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
325 * request cannot be satisfied (eg. requires blocking or too much complexity).
326 * It would then be called again in ref-walk mode.
328 int generic_permission(struct inode
*inode
, int mask
)
333 * Do the basic permission checks.
335 ret
= acl_permission_check(inode
, mask
);
339 if (S_ISDIR(inode
->i_mode
)) {
340 /* DACs are overridable for directories */
341 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_OVERRIDE
))
343 if (!(mask
& MAY_WRITE
))
344 if (capable_wrt_inode_uidgid(inode
,
345 CAP_DAC_READ_SEARCH
))
350 * Read/write DACs are always overridable.
351 * Executable DACs are overridable when there is
352 * at least one exec bit set.
354 if (!(mask
& MAY_EXEC
) || (inode
->i_mode
& S_IXUGO
))
355 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_OVERRIDE
))
359 * Searching includes executable on directories, else just read.
361 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
362 if (mask
== MAY_READ
)
363 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_READ_SEARCH
))
368 EXPORT_SYMBOL(generic_permission
);
371 * We _really_ want to just do "generic_permission()" without
372 * even looking at the inode->i_op values. So we keep a cache
373 * flag in inode->i_opflags, that says "this has not special
374 * permission function, use the fast case".
376 static inline int do_inode_permission(struct inode
*inode
, int mask
)
378 if (unlikely(!(inode
->i_opflags
& IOP_FASTPERM
))) {
379 if (likely(inode
->i_op
->permission
))
380 return inode
->i_op
->permission(inode
, mask
);
382 /* This gets set once for the inode lifetime */
383 spin_lock(&inode
->i_lock
);
384 inode
->i_opflags
|= IOP_FASTPERM
;
385 spin_unlock(&inode
->i_lock
);
387 return generic_permission(inode
, mask
);
391 * __inode_permission - Check for access rights to a given inode
392 * @inode: Inode to check permission on
393 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
395 * Check for read/write/execute permissions on an inode.
397 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
399 * This does not check for a read-only file system. You probably want
400 * inode_permission().
402 int __inode_permission(struct inode
*inode
, int mask
)
406 if (unlikely(mask
& MAY_WRITE
)) {
408 * Nobody gets write access to an immutable file.
410 if (IS_IMMUTABLE(inode
))
414 retval
= do_inode_permission(inode
, mask
);
418 retval
= devcgroup_inode_permission(inode
, mask
);
422 return security_inode_permission(inode
, mask
);
424 EXPORT_SYMBOL(__inode_permission
);
427 * sb_permission - Check superblock-level permissions
428 * @sb: Superblock of inode to check permission on
429 * @inode: Inode to check permission on
430 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
432 * Separate out file-system wide checks from inode-specific permission checks.
434 static int sb_permission(struct super_block
*sb
, struct inode
*inode
, int mask
)
436 if (unlikely(mask
& MAY_WRITE
)) {
437 umode_t mode
= inode
->i_mode
;
439 /* Nobody gets write access to a read-only fs. */
440 if ((sb
->s_flags
& MS_RDONLY
) &&
441 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
448 * inode_permission - Check for access rights to a given inode
449 * @inode: Inode to check permission on
450 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
452 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
453 * this, letting us set arbitrary permissions for filesystem access without
454 * changing the "normal" UIDs which are used for other things.
456 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
458 int inode_permission(struct inode
*inode
, int mask
)
462 retval
= sb_permission(inode
->i_sb
, inode
, mask
);
465 return __inode_permission(inode
, mask
);
467 EXPORT_SYMBOL(inode_permission
);
470 * path_get - get a reference to a path
471 * @path: path to get the reference to
473 * Given a path increment the reference count to the dentry and the vfsmount.
475 void path_get(const struct path
*path
)
480 EXPORT_SYMBOL(path_get
);
483 * path_put - put a reference to a path
484 * @path: path to put the reference to
486 * Given a path decrement the reference count to the dentry and the vfsmount.
488 void path_put(const struct path
*path
)
493 EXPORT_SYMBOL(path_put
);
502 struct inode
*inode
; /* path.dentry.d_inode */
512 } stack
[MAX_NESTED_LINKS
+ 1];
516 * Path walking has 2 modes, rcu-walk and ref-walk (see
517 * Documentation/filesystems/path-lookup.txt). In situations when we can't
518 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
519 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
520 * mode. Refcounts are grabbed at the last known good point before rcu-walk
521 * got stuck, so ref-walk may continue from there. If this is not successful
522 * (eg. a seqcount has changed), then failure is returned and it's up to caller
523 * to restart the path walk from the beginning in ref-walk mode.
527 * unlazy_walk - try to switch to ref-walk mode.
528 * @nd: nameidata pathwalk data
529 * @dentry: child of nd->path.dentry or NULL
530 * Returns: 0 on success, -ECHILD on failure
532 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
533 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
534 * @nd or NULL. Must be called from rcu-walk context.
536 static int unlazy_walk(struct nameidata
*nd
, struct dentry
*dentry
)
538 struct fs_struct
*fs
= current
->fs
;
539 struct dentry
*parent
= nd
->path
.dentry
;
541 BUG_ON(!(nd
->flags
& LOOKUP_RCU
));
544 * After legitimizing the bastards, terminate_walk()
545 * will do the right thing for non-RCU mode, and all our
546 * subsequent exit cases should rcu_read_unlock()
547 * before returning. Do vfsmount first; if dentry
548 * can't be legitimized, just set nd->path.dentry to NULL
549 * and rely on dput(NULL) being a no-op.
551 if (!legitimize_mnt(nd
->path
.mnt
, nd
->m_seq
))
553 nd
->flags
&= ~LOOKUP_RCU
;
555 if (!lockref_get_not_dead(&parent
->d_lockref
)) {
556 nd
->path
.dentry
= NULL
;
561 * For a negative lookup, the lookup sequence point is the parents
562 * sequence point, and it only needs to revalidate the parent dentry.
564 * For a positive lookup, we need to move both the parent and the
565 * dentry from the RCU domain to be properly refcounted. And the
566 * sequence number in the dentry validates *both* dentry counters,
567 * since we checked the sequence number of the parent after we got
568 * the child sequence number. So we know the parent must still
569 * be valid if the child sequence number is still valid.
572 if (read_seqcount_retry(&parent
->d_seq
, nd
->seq
))
574 BUG_ON(nd
->inode
!= parent
->d_inode
);
576 if (!lockref_get_not_dead(&dentry
->d_lockref
))
578 if (read_seqcount_retry(&dentry
->d_seq
, nd
->seq
))
583 * Sequence counts matched. Now make sure that the root is
584 * still valid and get it if required.
586 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
587 spin_lock(&fs
->lock
);
588 if (nd
->root
.mnt
!= fs
->root
.mnt
|| nd
->root
.dentry
!= fs
->root
.dentry
)
589 goto unlock_and_drop_dentry
;
591 spin_unlock(&fs
->lock
);
597 unlock_and_drop_dentry
:
598 spin_unlock(&fs
->lock
);
606 if (!(nd
->flags
& LOOKUP_ROOT
))
611 static inline int d_revalidate(struct dentry
*dentry
, unsigned int flags
)
613 return dentry
->d_op
->d_revalidate(dentry
, flags
);
617 * complete_walk - successful completion of path walk
618 * @nd: pointer nameidata
620 * If we had been in RCU mode, drop out of it and legitimize nd->path.
621 * Revalidate the final result, unless we'd already done that during
622 * the path walk or the filesystem doesn't ask for it. Return 0 on
623 * success, -error on failure. In case of failure caller does not
624 * need to drop nd->path.
626 static int complete_walk(struct nameidata
*nd
)
628 struct dentry
*dentry
= nd
->path
.dentry
;
631 if (nd
->flags
& LOOKUP_RCU
) {
632 nd
->flags
&= ~LOOKUP_RCU
;
633 if (!(nd
->flags
& LOOKUP_ROOT
))
636 if (!legitimize_mnt(nd
->path
.mnt
, nd
->m_seq
)) {
640 if (unlikely(!lockref_get_not_dead(&dentry
->d_lockref
))) {
642 mntput(nd
->path
.mnt
);
645 if (read_seqcount_retry(&dentry
->d_seq
, nd
->seq
)) {
648 mntput(nd
->path
.mnt
);
654 if (likely(!(nd
->flags
& LOOKUP_JUMPED
)))
657 if (likely(!(dentry
->d_flags
& DCACHE_OP_WEAK_REVALIDATE
)))
660 status
= dentry
->d_op
->d_weak_revalidate(dentry
, nd
->flags
);
671 static __always_inline
void set_root(struct nameidata
*nd
)
673 get_fs_root(current
->fs
, &nd
->root
);
676 static __always_inline
unsigned set_root_rcu(struct nameidata
*nd
)
678 struct fs_struct
*fs
= current
->fs
;
682 seq
= read_seqcount_begin(&fs
->seq
);
684 res
= __read_seqcount_begin(&nd
->root
.dentry
->d_seq
);
685 } while (read_seqcount_retry(&fs
->seq
, seq
));
689 static void path_put_conditional(struct path
*path
, struct nameidata
*nd
)
692 if (path
->mnt
!= nd
->path
.mnt
)
696 static inline void path_to_nameidata(const struct path
*path
,
697 struct nameidata
*nd
)
699 if (!(nd
->flags
& LOOKUP_RCU
)) {
700 dput(nd
->path
.dentry
);
701 if (nd
->path
.mnt
!= path
->mnt
)
702 mntput(nd
->path
.mnt
);
704 nd
->path
.mnt
= path
->mnt
;
705 nd
->path
.dentry
= path
->dentry
;
709 * Helper to directly jump to a known parsed path from ->follow_link,
710 * caller must have taken a reference to path beforehand.
712 void nd_jump_link(struct nameidata
*nd
, struct path
*path
)
717 nd
->inode
= nd
->path
.dentry
->d_inode
;
718 nd
->flags
|= LOOKUP_JUMPED
;
721 static inline void put_link(struct nameidata
*nd
)
723 struct saved
*last
= nd
->stack
+ nd
->depth
;
724 struct inode
*inode
= last
->link
.dentry
->d_inode
;
725 if (last
->cookie
&& inode
->i_op
->put_link
)
726 inode
->i_op
->put_link(last
->link
.dentry
, last
->cookie
);
727 path_put(&last
->link
);
730 int sysctl_protected_symlinks __read_mostly
= 0;
731 int sysctl_protected_hardlinks __read_mostly
= 0;
734 * may_follow_link - Check symlink following for unsafe situations
735 * @link: The path of the symlink
736 * @nd: nameidata pathwalk data
738 * In the case of the sysctl_protected_symlinks sysctl being enabled,
739 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
740 * in a sticky world-writable directory. This is to protect privileged
741 * processes from failing races against path names that may change out
742 * from under them by way of other users creating malicious symlinks.
743 * It will permit symlinks to be followed only when outside a sticky
744 * world-writable directory, or when the uid of the symlink and follower
745 * match, or when the directory owner matches the symlink's owner.
747 * Returns 0 if following the symlink is allowed, -ve on error.
749 static inline int may_follow_link(struct path
*link
, struct nameidata
*nd
)
751 const struct inode
*inode
;
752 const struct inode
*parent
;
754 if (!sysctl_protected_symlinks
)
757 /* Allowed if owner and follower match. */
758 inode
= link
->dentry
->d_inode
;
759 if (uid_eq(current_cred()->fsuid
, inode
->i_uid
))
762 /* Allowed if parent directory not sticky and world-writable. */
763 parent
= nd
->path
.dentry
->d_inode
;
764 if ((parent
->i_mode
& (S_ISVTX
|S_IWOTH
)) != (S_ISVTX
|S_IWOTH
))
767 /* Allowed if parent directory and link owner match. */
768 if (uid_eq(parent
->i_uid
, inode
->i_uid
))
771 audit_log_link_denied("follow_link", link
);
772 path_put_conditional(link
, nd
);
778 * safe_hardlink_source - Check for safe hardlink conditions
779 * @inode: the source inode to hardlink from
781 * Return false if at least one of the following conditions:
782 * - inode is not a regular file
784 * - inode is setgid and group-exec
785 * - access failure for read and write
787 * Otherwise returns true.
789 static bool safe_hardlink_source(struct inode
*inode
)
791 umode_t mode
= inode
->i_mode
;
793 /* Special files should not get pinned to the filesystem. */
797 /* Setuid files should not get pinned to the filesystem. */
801 /* Executable setgid files should not get pinned to the filesystem. */
802 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
))
805 /* Hardlinking to unreadable or unwritable sources is dangerous. */
806 if (inode_permission(inode
, MAY_READ
| MAY_WRITE
))
813 * may_linkat - Check permissions for creating a hardlink
814 * @link: the source to hardlink from
816 * Block hardlink when all of:
817 * - sysctl_protected_hardlinks enabled
818 * - fsuid does not match inode
819 * - hardlink source is unsafe (see safe_hardlink_source() above)
822 * Returns 0 if successful, -ve on error.
824 static int may_linkat(struct path
*link
)
826 const struct cred
*cred
;
829 if (!sysctl_protected_hardlinks
)
832 cred
= current_cred();
833 inode
= link
->dentry
->d_inode
;
835 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
836 * otherwise, it must be a safe source.
838 if (uid_eq(cred
->fsuid
, inode
->i_uid
) || safe_hardlink_source(inode
) ||
842 audit_log_link_denied("linkat", link
);
846 static __always_inline
const char *
847 get_link(struct path
*link
, struct nameidata
*nd
, void **p
)
849 struct dentry
*dentry
= link
->dentry
;
850 struct inode
*inode
= dentry
->d_inode
;
854 BUG_ON(nd
->flags
& LOOKUP_RCU
);
856 if (link
->mnt
== nd
->path
.mnt
)
859 res
= ERR_PTR(-ELOOP
);
860 if (unlikely(current
->total_link_count
>= 40))
864 current
->total_link_count
++;
868 error
= security_inode_follow_link(dentry
);
869 res
= ERR_PTR(error
);
873 nd
->last_type
= LAST_BIND
;
877 res
= inode
->i_op
->follow_link(dentry
, p
, nd
);
887 static int follow_up_rcu(struct path
*path
)
889 struct mount
*mnt
= real_mount(path
->mnt
);
890 struct mount
*parent
;
891 struct dentry
*mountpoint
;
893 parent
= mnt
->mnt_parent
;
894 if (&parent
->mnt
== path
->mnt
)
896 mountpoint
= mnt
->mnt_mountpoint
;
897 path
->dentry
= mountpoint
;
898 path
->mnt
= &parent
->mnt
;
903 * follow_up - Find the mountpoint of path's vfsmount
905 * Given a path, find the mountpoint of its source file system.
906 * Replace @path with the path of the mountpoint in the parent mount.
909 * Return 1 if we went up a level and 0 if we were already at the
912 int follow_up(struct path
*path
)
914 struct mount
*mnt
= real_mount(path
->mnt
);
915 struct mount
*parent
;
916 struct dentry
*mountpoint
;
918 read_seqlock_excl(&mount_lock
);
919 parent
= mnt
->mnt_parent
;
921 read_sequnlock_excl(&mount_lock
);
924 mntget(&parent
->mnt
);
925 mountpoint
= dget(mnt
->mnt_mountpoint
);
926 read_sequnlock_excl(&mount_lock
);
928 path
->dentry
= mountpoint
;
930 path
->mnt
= &parent
->mnt
;
933 EXPORT_SYMBOL(follow_up
);
936 * Perform an automount
937 * - return -EISDIR to tell follow_managed() to stop and return the path we
940 static int follow_automount(struct path
*path
, unsigned flags
,
943 struct vfsmount
*mnt
;
946 if (!path
->dentry
->d_op
|| !path
->dentry
->d_op
->d_automount
)
949 /* We don't want to mount if someone's just doing a stat -
950 * unless they're stat'ing a directory and appended a '/' to
953 * We do, however, want to mount if someone wants to open or
954 * create a file of any type under the mountpoint, wants to
955 * traverse through the mountpoint or wants to open the
956 * mounted directory. Also, autofs may mark negative dentries
957 * as being automount points. These will need the attentions
958 * of the daemon to instantiate them before they can be used.
960 if (!(flags
& (LOOKUP_PARENT
| LOOKUP_DIRECTORY
|
961 LOOKUP_OPEN
| LOOKUP_CREATE
| LOOKUP_AUTOMOUNT
)) &&
962 path
->dentry
->d_inode
)
965 current
->total_link_count
++;
966 if (current
->total_link_count
>= 40)
969 mnt
= path
->dentry
->d_op
->d_automount(path
);
972 * The filesystem is allowed to return -EISDIR here to indicate
973 * it doesn't want to automount. For instance, autofs would do
974 * this so that its userspace daemon can mount on this dentry.
976 * However, we can only permit this if it's a terminal point in
977 * the path being looked up; if it wasn't then the remainder of
978 * the path is inaccessible and we should say so.
980 if (PTR_ERR(mnt
) == -EISDIR
&& (flags
& LOOKUP_PARENT
))
985 if (!mnt
) /* mount collision */
989 /* lock_mount() may release path->mnt on error */
993 err
= finish_automount(mnt
, path
);
997 /* Someone else made a mount here whilst we were busy */
1002 path
->dentry
= dget(mnt
->mnt_root
);
1011 * Handle a dentry that is managed in some way.
1012 * - Flagged for transit management (autofs)
1013 * - Flagged as mountpoint
1014 * - Flagged as automount point
1016 * This may only be called in refwalk mode.
1018 * Serialization is taken care of in namespace.c
1020 static int follow_managed(struct path
*path
, unsigned flags
)
1022 struct vfsmount
*mnt
= path
->mnt
; /* held by caller, must be left alone */
1024 bool need_mntput
= false;
1027 /* Given that we're not holding a lock here, we retain the value in a
1028 * local variable for each dentry as we look at it so that we don't see
1029 * the components of that value change under us */
1030 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
1031 managed
&= DCACHE_MANAGED_DENTRY
,
1032 unlikely(managed
!= 0)) {
1033 /* Allow the filesystem to manage the transit without i_mutex
1035 if (managed
& DCACHE_MANAGE_TRANSIT
) {
1036 BUG_ON(!path
->dentry
->d_op
);
1037 BUG_ON(!path
->dentry
->d_op
->d_manage
);
1038 ret
= path
->dentry
->d_op
->d_manage(path
->dentry
, false);
1043 /* Transit to a mounted filesystem. */
1044 if (managed
& DCACHE_MOUNTED
) {
1045 struct vfsmount
*mounted
= lookup_mnt(path
);
1050 path
->mnt
= mounted
;
1051 path
->dentry
= dget(mounted
->mnt_root
);
1056 /* Something is mounted on this dentry in another
1057 * namespace and/or whatever was mounted there in this
1058 * namespace got unmounted before lookup_mnt() could
1062 /* Handle an automount point */
1063 if (managed
& DCACHE_NEED_AUTOMOUNT
) {
1064 ret
= follow_automount(path
, flags
, &need_mntput
);
1070 /* We didn't change the current path point */
1074 if (need_mntput
&& path
->mnt
== mnt
)
1078 return ret
< 0 ? ret
: need_mntput
;
1081 int follow_down_one(struct path
*path
)
1083 struct vfsmount
*mounted
;
1085 mounted
= lookup_mnt(path
);
1089 path
->mnt
= mounted
;
1090 path
->dentry
= dget(mounted
->mnt_root
);
1095 EXPORT_SYMBOL(follow_down_one
);
1097 static inline int managed_dentry_rcu(struct dentry
*dentry
)
1099 return (dentry
->d_flags
& DCACHE_MANAGE_TRANSIT
) ?
1100 dentry
->d_op
->d_manage(dentry
, true) : 0;
1104 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1105 * we meet a managed dentry that would need blocking.
1107 static bool __follow_mount_rcu(struct nameidata
*nd
, struct path
*path
,
1108 struct inode
**inode
)
1111 struct mount
*mounted
;
1113 * Don't forget we might have a non-mountpoint managed dentry
1114 * that wants to block transit.
1116 switch (managed_dentry_rcu(path
->dentry
)) {
1126 if (!d_mountpoint(path
->dentry
))
1127 return !(path
->dentry
->d_flags
& DCACHE_NEED_AUTOMOUNT
);
1129 mounted
= __lookup_mnt(path
->mnt
, path
->dentry
);
1132 path
->mnt
= &mounted
->mnt
;
1133 path
->dentry
= mounted
->mnt
.mnt_root
;
1134 nd
->flags
|= LOOKUP_JUMPED
;
1135 nd
->seq
= read_seqcount_begin(&path
->dentry
->d_seq
);
1137 * Update the inode too. We don't need to re-check the
1138 * dentry sequence number here after this d_inode read,
1139 * because a mount-point is always pinned.
1141 *inode
= path
->dentry
->d_inode
;
1143 return !read_seqretry(&mount_lock
, nd
->m_seq
) &&
1144 !(path
->dentry
->d_flags
& DCACHE_NEED_AUTOMOUNT
);
1147 static int follow_dotdot_rcu(struct nameidata
*nd
)
1149 struct inode
*inode
= nd
->inode
;
1154 if (nd
->path
.dentry
== nd
->root
.dentry
&&
1155 nd
->path
.mnt
== nd
->root
.mnt
) {
1158 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
1159 struct dentry
*old
= nd
->path
.dentry
;
1160 struct dentry
*parent
= old
->d_parent
;
1163 inode
= parent
->d_inode
;
1164 seq
= read_seqcount_begin(&parent
->d_seq
);
1165 if (read_seqcount_retry(&old
->d_seq
, nd
->seq
))
1167 nd
->path
.dentry
= parent
;
1171 if (!follow_up_rcu(&nd
->path
))
1173 inode
= nd
->path
.dentry
->d_inode
;
1174 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1176 while (d_mountpoint(nd
->path
.dentry
)) {
1177 struct mount
*mounted
;
1178 mounted
= __lookup_mnt(nd
->path
.mnt
, nd
->path
.dentry
);
1181 nd
->path
.mnt
= &mounted
->mnt
;
1182 nd
->path
.dentry
= mounted
->mnt
.mnt_root
;
1183 inode
= nd
->path
.dentry
->d_inode
;
1184 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1185 if (read_seqretry(&mount_lock
, nd
->m_seq
))
1192 nd
->flags
&= ~LOOKUP_RCU
;
1193 if (!(nd
->flags
& LOOKUP_ROOT
))
1194 nd
->root
.mnt
= NULL
;
1200 * Follow down to the covering mount currently visible to userspace. At each
1201 * point, the filesystem owning that dentry may be queried as to whether the
1202 * caller is permitted to proceed or not.
1204 int follow_down(struct path
*path
)
1209 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
1210 unlikely(managed
& DCACHE_MANAGED_DENTRY
)) {
1211 /* Allow the filesystem to manage the transit without i_mutex
1214 * We indicate to the filesystem if someone is trying to mount
1215 * something here. This gives autofs the chance to deny anyone
1216 * other than its daemon the right to mount on its
1219 * The filesystem may sleep at this point.
1221 if (managed
& DCACHE_MANAGE_TRANSIT
) {
1222 BUG_ON(!path
->dentry
->d_op
);
1223 BUG_ON(!path
->dentry
->d_op
->d_manage
);
1224 ret
= path
->dentry
->d_op
->d_manage(
1225 path
->dentry
, false);
1227 return ret
== -EISDIR
? 0 : ret
;
1230 /* Transit to a mounted filesystem. */
1231 if (managed
& DCACHE_MOUNTED
) {
1232 struct vfsmount
*mounted
= lookup_mnt(path
);
1237 path
->mnt
= mounted
;
1238 path
->dentry
= dget(mounted
->mnt_root
);
1242 /* Don't handle automount points here */
1247 EXPORT_SYMBOL(follow_down
);
1250 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1252 static void follow_mount(struct path
*path
)
1254 while (d_mountpoint(path
->dentry
)) {
1255 struct vfsmount
*mounted
= lookup_mnt(path
);
1260 path
->mnt
= mounted
;
1261 path
->dentry
= dget(mounted
->mnt_root
);
1265 static void follow_dotdot(struct nameidata
*nd
)
1271 struct dentry
*old
= nd
->path
.dentry
;
1273 if (nd
->path
.dentry
== nd
->root
.dentry
&&
1274 nd
->path
.mnt
== nd
->root
.mnt
) {
1277 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
1278 /* rare case of legitimate dget_parent()... */
1279 nd
->path
.dentry
= dget_parent(nd
->path
.dentry
);
1283 if (!follow_up(&nd
->path
))
1286 follow_mount(&nd
->path
);
1287 nd
->inode
= nd
->path
.dentry
->d_inode
;
1291 * This looks up the name in dcache, possibly revalidates the old dentry and
1292 * allocates a new one if not found or not valid. In the need_lookup argument
1293 * returns whether i_op->lookup is necessary.
1295 * dir->d_inode->i_mutex must be held
1297 static struct dentry
*lookup_dcache(struct qstr
*name
, struct dentry
*dir
,
1298 unsigned int flags
, bool *need_lookup
)
1300 struct dentry
*dentry
;
1303 *need_lookup
= false;
1304 dentry
= d_lookup(dir
, name
);
1306 if (dentry
->d_flags
& DCACHE_OP_REVALIDATE
) {
1307 error
= d_revalidate(dentry
, flags
);
1308 if (unlikely(error
<= 0)) {
1311 return ERR_PTR(error
);
1313 d_invalidate(dentry
);
1322 dentry
= d_alloc(dir
, name
);
1323 if (unlikely(!dentry
))
1324 return ERR_PTR(-ENOMEM
);
1326 *need_lookup
= true;
1332 * Call i_op->lookup on the dentry. The dentry must be negative and
1335 * dir->d_inode->i_mutex must be held
1337 static struct dentry
*lookup_real(struct inode
*dir
, struct dentry
*dentry
,
1342 /* Don't create child dentry for a dead directory. */
1343 if (unlikely(IS_DEADDIR(dir
))) {
1345 return ERR_PTR(-ENOENT
);
1348 old
= dir
->i_op
->lookup(dir
, dentry
, flags
);
1349 if (unlikely(old
)) {
1356 static struct dentry
*__lookup_hash(struct qstr
*name
,
1357 struct dentry
*base
, unsigned int flags
)
1360 struct dentry
*dentry
;
1362 dentry
= lookup_dcache(name
, base
, flags
, &need_lookup
);
1366 return lookup_real(base
->d_inode
, dentry
, flags
);
1370 * It's more convoluted than I'd like it to be, but... it's still fairly
1371 * small and for now I'd prefer to have fast path as straight as possible.
1372 * It _is_ time-critical.
1374 static int lookup_fast(struct nameidata
*nd
,
1375 struct path
*path
, struct inode
**inode
)
1377 struct vfsmount
*mnt
= nd
->path
.mnt
;
1378 struct dentry
*dentry
, *parent
= nd
->path
.dentry
;
1384 * Rename seqlock is not required here because in the off chance
1385 * of a false negative due to a concurrent rename, we're going to
1386 * do the non-racy lookup, below.
1388 if (nd
->flags
& LOOKUP_RCU
) {
1391 dentry
= __d_lookup_rcu(parent
, &nd
->last
, &seq
);
1396 * This sequence count validates that the inode matches
1397 * the dentry name information from lookup.
1399 *inode
= dentry
->d_inode
;
1400 negative
= d_is_negative(dentry
);
1401 if (read_seqcount_retry(&dentry
->d_seq
, seq
))
1407 * This sequence count validates that the parent had no
1408 * changes while we did the lookup of the dentry above.
1410 * The memory barrier in read_seqcount_begin of child is
1411 * enough, we can use __read_seqcount_retry here.
1413 if (__read_seqcount_retry(&parent
->d_seq
, nd
->seq
))
1417 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
)) {
1418 status
= d_revalidate(dentry
, nd
->flags
);
1419 if (unlikely(status
<= 0)) {
1420 if (status
!= -ECHILD
)
1426 path
->dentry
= dentry
;
1427 if (likely(__follow_mount_rcu(nd
, path
, inode
)))
1430 if (unlazy_walk(nd
, dentry
))
1433 dentry
= __d_lookup(parent
, &nd
->last
);
1436 if (unlikely(!dentry
))
1439 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
) && need_reval
)
1440 status
= d_revalidate(dentry
, nd
->flags
);
1441 if (unlikely(status
<= 0)) {
1446 d_invalidate(dentry
);
1451 if (unlikely(d_is_negative(dentry
))) {
1456 path
->dentry
= dentry
;
1457 err
= follow_managed(path
, nd
->flags
);
1458 if (unlikely(err
< 0)) {
1459 path_put_conditional(path
, nd
);
1463 nd
->flags
|= LOOKUP_JUMPED
;
1464 *inode
= path
->dentry
->d_inode
;
1471 /* Fast lookup failed, do it the slow way */
1472 static int lookup_slow(struct nameidata
*nd
, struct path
*path
)
1474 struct dentry
*dentry
, *parent
;
1477 parent
= nd
->path
.dentry
;
1478 BUG_ON(nd
->inode
!= parent
->d_inode
);
1480 mutex_lock(&parent
->d_inode
->i_mutex
);
1481 dentry
= __lookup_hash(&nd
->last
, parent
, nd
->flags
);
1482 mutex_unlock(&parent
->d_inode
->i_mutex
);
1484 return PTR_ERR(dentry
);
1485 path
->mnt
= nd
->path
.mnt
;
1486 path
->dentry
= dentry
;
1487 err
= follow_managed(path
, nd
->flags
);
1488 if (unlikely(err
< 0)) {
1489 path_put_conditional(path
, nd
);
1493 nd
->flags
|= LOOKUP_JUMPED
;
1497 static inline int may_lookup(struct nameidata
*nd
)
1499 if (nd
->flags
& LOOKUP_RCU
) {
1500 int err
= inode_permission(nd
->inode
, MAY_EXEC
|MAY_NOT_BLOCK
);
1503 if (unlazy_walk(nd
, NULL
))
1506 return inode_permission(nd
->inode
, MAY_EXEC
);
1509 static inline int handle_dots(struct nameidata
*nd
, int type
)
1511 if (type
== LAST_DOTDOT
) {
1512 if (nd
->flags
& LOOKUP_RCU
) {
1513 if (follow_dotdot_rcu(nd
))
1521 static void terminate_walk(struct nameidata
*nd
)
1523 if (!(nd
->flags
& LOOKUP_RCU
)) {
1524 path_put(&nd
->path
);
1526 nd
->flags
&= ~LOOKUP_RCU
;
1527 if (!(nd
->flags
& LOOKUP_ROOT
))
1528 nd
->root
.mnt
= NULL
;
1534 * Do we need to follow links? We _really_ want to be able
1535 * to do this check without having to look at inode->i_op,
1536 * so we keep a cache of "no, this doesn't need follow_link"
1537 * for the common case.
1539 static inline int should_follow_link(struct dentry
*dentry
, int follow
)
1541 return unlikely(d_is_symlink(dentry
)) ? follow
: 0;
1544 static int walk_component(struct nameidata
*nd
, int follow
)
1547 struct inode
*inode
;
1550 * "." and ".." are special - ".." especially so because it has
1551 * to be able to know about the current root directory and
1552 * parent relationships.
1554 if (unlikely(nd
->last_type
!= LAST_NORM
))
1555 return handle_dots(nd
, nd
->last_type
);
1556 err
= lookup_fast(nd
, &path
, &inode
);
1557 if (unlikely(err
)) {
1561 err
= lookup_slow(nd
, &path
);
1565 inode
= path
.dentry
->d_inode
;
1567 if (d_is_negative(path
.dentry
))
1571 if (should_follow_link(path
.dentry
, follow
)) {
1572 if (nd
->flags
& LOOKUP_RCU
) {
1573 if (unlikely(nd
->path
.mnt
!= path
.mnt
||
1574 unlazy_walk(nd
, path
.dentry
))) {
1579 BUG_ON(inode
!= path
.dentry
->d_inode
);
1583 path_to_nameidata(&path
, nd
);
1588 path_to_nameidata(&path
, nd
);
1595 * We can do the critical dentry name comparison and hashing
1596 * operations one word at a time, but we are limited to:
1598 * - Architectures with fast unaligned word accesses. We could
1599 * do a "get_unaligned()" if this helps and is sufficiently
1602 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1603 * do not trap on the (extremely unlikely) case of a page
1604 * crossing operation.
1606 * - Furthermore, we need an efficient 64-bit compile for the
1607 * 64-bit case in order to generate the "number of bytes in
1608 * the final mask". Again, that could be replaced with a
1609 * efficient population count instruction or similar.
1611 #ifdef CONFIG_DCACHE_WORD_ACCESS
1613 #include <asm/word-at-a-time.h>
1617 static inline unsigned int fold_hash(unsigned long hash
)
1619 return hash_64(hash
, 32);
1622 #else /* 32-bit case */
1624 #define fold_hash(x) (x)
1628 unsigned int full_name_hash(const unsigned char *name
, unsigned int len
)
1630 unsigned long a
, mask
;
1631 unsigned long hash
= 0;
1634 a
= load_unaligned_zeropad(name
);
1635 if (len
< sizeof(unsigned long))
1639 name
+= sizeof(unsigned long);
1640 len
-= sizeof(unsigned long);
1644 mask
= bytemask_from_count(len
);
1647 return fold_hash(hash
);
1649 EXPORT_SYMBOL(full_name_hash
);
1652 * Calculate the length and hash of the path component, and
1653 * return the "hash_len" as the result.
1655 static inline u64
hash_name(const char *name
)
1657 unsigned long a
, b
, adata
, bdata
, mask
, hash
, len
;
1658 const struct word_at_a_time constants
= WORD_AT_A_TIME_CONSTANTS
;
1661 len
= -sizeof(unsigned long);
1663 hash
= (hash
+ a
) * 9;
1664 len
+= sizeof(unsigned long);
1665 a
= load_unaligned_zeropad(name
+len
);
1666 b
= a
^ REPEAT_BYTE('/');
1667 } while (!(has_zero(a
, &adata
, &constants
) | has_zero(b
, &bdata
, &constants
)));
1669 adata
= prep_zero_mask(a
, adata
, &constants
);
1670 bdata
= prep_zero_mask(b
, bdata
, &constants
);
1672 mask
= create_zero_mask(adata
| bdata
);
1674 hash
+= a
& zero_bytemask(mask
);
1675 len
+= find_zero(mask
);
1676 return hashlen_create(fold_hash(hash
), len
);
1681 unsigned int full_name_hash(const unsigned char *name
, unsigned int len
)
1683 unsigned long hash
= init_name_hash();
1685 hash
= partial_name_hash(*name
++, hash
);
1686 return end_name_hash(hash
);
1688 EXPORT_SYMBOL(full_name_hash
);
1691 * We know there's a real path component here of at least
1694 static inline u64
hash_name(const char *name
)
1696 unsigned long hash
= init_name_hash();
1697 unsigned long len
= 0, c
;
1699 c
= (unsigned char)*name
;
1702 hash
= partial_name_hash(c
, hash
);
1703 c
= (unsigned char)name
[len
];
1704 } while (c
&& c
!= '/');
1705 return hashlen_create(end_name_hash(hash
), len
);
1712 * This is the basic name resolution function, turning a pathname into
1713 * the final dentry. We expect 'base' to be positive and a directory.
1715 * Returns 0 and nd will have valid dentry and mnt on success.
1716 * Returns error and drops reference to input namei data on failure.
1718 static int link_path_walk(const char *name
, struct nameidata
*nd
)
1720 struct saved
*last
= nd
->stack
;
1728 /* At this point we know we have a real path component. */
1733 err
= may_lookup(nd
);
1737 hash_len
= hash_name(name
);
1740 if (name
[0] == '.') switch (hashlen_len(hash_len
)) {
1742 if (name
[1] == '.') {
1744 nd
->flags
|= LOOKUP_JUMPED
;
1750 if (likely(type
== LAST_NORM
)) {
1751 struct dentry
*parent
= nd
->path
.dentry
;
1752 nd
->flags
&= ~LOOKUP_JUMPED
;
1753 if (unlikely(parent
->d_flags
& DCACHE_OP_HASH
)) {
1754 struct qstr
this = { { .hash_len
= hash_len
}, .name
= name
};
1755 err
= parent
->d_op
->d_hash(parent
, &this);
1758 hash_len
= this.hash_len
;
1763 nd
->last
.hash_len
= hash_len
;
1764 nd
->last
.name
= name
;
1765 nd
->last_type
= type
;
1767 name
+= hashlen_len(hash_len
);
1771 * If it wasn't NUL, we know it was '/'. Skip that
1772 * slash, and continue until no more slashes.
1776 } while (unlikely(*name
== '/'));
1780 err
= walk_component(nd
, LOOKUP_FOLLOW
);
1788 if (unlikely(current
->link_count
>= MAX_NESTED_LINKS
)) {
1789 path_put_conditional(&nd
->link
, nd
);
1790 path_put(&nd
->path
);
1794 BUG_ON(nd
->depth
>= MAX_NESTED_LINKS
);
1797 current
->link_count
++;
1800 last
->link
= nd
->link
;
1801 s
= get_link(&last
->link
, nd
, &last
->cookie
);
1803 if (unlikely(IS_ERR(s
))) {
1805 current
->link_count
--;
1814 current
->link_count
--;
1821 path_put(&nd
->path
);
1822 nd
->path
= nd
->root
;
1823 path_get(&nd
->root
);
1824 nd
->flags
|= LOOKUP_JUMPED
;
1825 while (unlikely(*++s
== '/'))
1828 nd
->inode
= nd
->path
.dentry
->d_inode
;
1836 if (!d_can_lookup(nd
->path
.dentry
)) {
1843 while (unlikely(nd
->depth
)) {
1845 current
->link_count
--;
1851 if (unlikely(nd
->depth
)) {
1853 err
= walk_component(nd
, LOOKUP_FOLLOW
);
1855 current
->link_count
--;
1863 static int path_init(int dfd
, const struct filename
*name
, unsigned int flags
,
1864 struct nameidata
*nd
)
1867 const char *s
= name
->name
;
1869 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
1870 nd
->flags
= flags
| LOOKUP_JUMPED
| LOOKUP_PARENT
;
1873 if (flags
& LOOKUP_ROOT
) {
1874 struct dentry
*root
= nd
->root
.dentry
;
1875 struct inode
*inode
= root
->d_inode
;
1877 if (!d_can_lookup(root
))
1879 retval
= inode_permission(inode
, MAY_EXEC
);
1883 nd
->path
= nd
->root
;
1885 if (flags
& LOOKUP_RCU
) {
1887 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1888 nd
->m_seq
= read_seqbegin(&mount_lock
);
1890 path_get(&nd
->path
);
1895 nd
->root
.mnt
= NULL
;
1897 nd
->m_seq
= read_seqbegin(&mount_lock
);
1899 if (flags
& LOOKUP_RCU
) {
1901 nd
->seq
= set_root_rcu(nd
);
1904 path_get(&nd
->root
);
1906 nd
->path
= nd
->root
;
1907 } else if (dfd
== AT_FDCWD
) {
1908 if (flags
& LOOKUP_RCU
) {
1909 struct fs_struct
*fs
= current
->fs
;
1915 seq
= read_seqcount_begin(&fs
->seq
);
1917 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1918 } while (read_seqcount_retry(&fs
->seq
, seq
));
1920 get_fs_pwd(current
->fs
, &nd
->path
);
1923 /* Caller must check execute permissions on the starting path component */
1924 struct fd f
= fdget_raw(dfd
);
1925 struct dentry
*dentry
;
1930 dentry
= f
.file
->f_path
.dentry
;
1933 if (!d_can_lookup(dentry
)) {
1939 nd
->path
= f
.file
->f_path
;
1940 if (flags
& LOOKUP_RCU
) {
1941 if (f
.flags
& FDPUT_FPUT
)
1943 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1946 path_get(&nd
->path
);
1951 nd
->inode
= nd
->path
.dentry
->d_inode
;
1952 if (!(flags
& LOOKUP_RCU
))
1954 if (likely(!read_seqcount_retry(&nd
->path
.dentry
->d_seq
, nd
->seq
)))
1956 if (!(nd
->flags
& LOOKUP_ROOT
))
1957 nd
->root
.mnt
= NULL
;
1961 current
->total_link_count
= 0;
1962 return link_path_walk(s
, nd
);
1965 static void path_cleanup(struct nameidata
*nd
)
1967 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
1968 path_put(&nd
->root
);
1969 nd
->root
.mnt
= NULL
;
1971 if (unlikely(nd
->base
))
1975 static int trailing_symlink(struct nameidata
*nd
)
1978 int error
= may_follow_link(&nd
->link
, nd
);
1979 if (unlikely(error
))
1981 nd
->flags
|= LOOKUP_PARENT
;
1982 nd
->stack
[0].link
= nd
->link
;
1983 s
= get_link(&nd
->stack
[0].link
, nd
, &nd
->stack
[0].cookie
);
1984 if (unlikely(IS_ERR(s
)))
1991 path_put(&nd
->path
);
1992 nd
->path
= nd
->root
;
1993 path_get(&nd
->root
);
1994 nd
->flags
|= LOOKUP_JUMPED
;
1996 nd
->inode
= nd
->path
.dentry
->d_inode
;
1997 error
= link_path_walk(s
, nd
);
1998 if (unlikely(error
))
2003 static inline int lookup_last(struct nameidata
*nd
)
2005 if (nd
->last_type
== LAST_NORM
&& nd
->last
.name
[nd
->last
.len
])
2006 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
2008 nd
->flags
&= ~LOOKUP_PARENT
;
2009 return walk_component(nd
, nd
->flags
& LOOKUP_FOLLOW
);
2012 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2013 static int path_lookupat(int dfd
, const struct filename
*name
,
2014 unsigned int flags
, struct nameidata
*nd
)
2019 * Path walking is largely split up into 2 different synchronisation
2020 * schemes, rcu-walk and ref-walk (explained in
2021 * Documentation/filesystems/path-lookup.txt). These share much of the
2022 * path walk code, but some things particularly setup, cleanup, and
2023 * following mounts are sufficiently divergent that functions are
2024 * duplicated. Typically there is a function foo(), and its RCU
2025 * analogue, foo_rcu().
2027 * -ECHILD is the error number of choice (just to avoid clashes) that
2028 * is returned if some aspect of an rcu-walk fails. Such an error must
2029 * be handled by restarting a traditional ref-walk (which will always
2030 * be able to complete).
2032 err
= path_init(dfd
, name
, flags
, nd
);
2033 if (!err
&& !(flags
& LOOKUP_PARENT
)) {
2034 err
= lookup_last(nd
);
2036 err
= trailing_symlink(nd
);
2039 err
= lookup_last(nd
);
2045 err
= complete_walk(nd
);
2047 if (!err
&& nd
->flags
& LOOKUP_DIRECTORY
) {
2048 if (!d_can_lookup(nd
->path
.dentry
)) {
2049 path_put(&nd
->path
);
2058 static int filename_lookup(int dfd
, struct filename
*name
,
2059 unsigned int flags
, struct nameidata
*nd
)
2061 int retval
= path_lookupat(dfd
, name
, flags
| LOOKUP_RCU
, nd
);
2062 if (unlikely(retval
== -ECHILD
))
2063 retval
= path_lookupat(dfd
, name
, flags
, nd
);
2064 if (unlikely(retval
== -ESTALE
))
2065 retval
= path_lookupat(dfd
, name
, flags
| LOOKUP_REVAL
, nd
);
2067 if (likely(!retval
))
2068 audit_inode(name
, nd
->path
.dentry
, flags
& LOOKUP_PARENT
);
2072 /* does lookup, returns the object with parent locked */
2073 struct dentry
*kern_path_locked(const char *name
, struct path
*path
)
2075 struct filename
*filename
= getname_kernel(name
);
2076 struct nameidata nd
;
2080 if (IS_ERR(filename
))
2081 return ERR_CAST(filename
);
2083 err
= filename_lookup(AT_FDCWD
, filename
, LOOKUP_PARENT
, &nd
);
2088 if (nd
.last_type
!= LAST_NORM
) {
2090 d
= ERR_PTR(-EINVAL
);
2093 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2094 d
= __lookup_hash(&nd
.last
, nd
.path
.dentry
, 0);
2096 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2106 int kern_path(const char *name
, unsigned int flags
, struct path
*path
)
2108 struct nameidata nd
;
2109 struct filename
*filename
= getname_kernel(name
);
2110 int res
= PTR_ERR(filename
);
2112 if (!IS_ERR(filename
)) {
2113 res
= filename_lookup(AT_FDCWD
, filename
, flags
, &nd
);
2120 EXPORT_SYMBOL(kern_path
);
2123 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2124 * @dentry: pointer to dentry of the base directory
2125 * @mnt: pointer to vfs mount of the base directory
2126 * @name: pointer to file name
2127 * @flags: lookup flags
2128 * @path: pointer to struct path to fill
2130 int vfs_path_lookup(struct dentry
*dentry
, struct vfsmount
*mnt
,
2131 const char *name
, unsigned int flags
,
2134 struct filename
*filename
= getname_kernel(name
);
2135 int err
= PTR_ERR(filename
);
2137 BUG_ON(flags
& LOOKUP_PARENT
);
2139 /* the first argument of filename_lookup() is ignored with LOOKUP_ROOT */
2140 if (!IS_ERR(filename
)) {
2141 struct nameidata nd
;
2142 nd
.root
.dentry
= dentry
;
2144 err
= filename_lookup(AT_FDCWD
, filename
,
2145 flags
| LOOKUP_ROOT
, &nd
);
2152 EXPORT_SYMBOL(vfs_path_lookup
);
2155 * lookup_one_len - filesystem helper to lookup single pathname component
2156 * @name: pathname component to lookup
2157 * @base: base directory to lookup from
2158 * @len: maximum length @len should be interpreted to
2160 * Note that this routine is purely a helper for filesystem usage and should
2161 * not be called by generic code.
2163 struct dentry
*lookup_one_len(const char *name
, struct dentry
*base
, int len
)
2169 WARN_ON_ONCE(!mutex_is_locked(&base
->d_inode
->i_mutex
));
2173 this.hash
= full_name_hash(name
, len
);
2175 return ERR_PTR(-EACCES
);
2177 if (unlikely(name
[0] == '.')) {
2178 if (len
< 2 || (len
== 2 && name
[1] == '.'))
2179 return ERR_PTR(-EACCES
);
2183 c
= *(const unsigned char *)name
++;
2184 if (c
== '/' || c
== '\0')
2185 return ERR_PTR(-EACCES
);
2188 * See if the low-level filesystem might want
2189 * to use its own hash..
2191 if (base
->d_flags
& DCACHE_OP_HASH
) {
2192 int err
= base
->d_op
->d_hash(base
, &this);
2194 return ERR_PTR(err
);
2197 err
= inode_permission(base
->d_inode
, MAY_EXEC
);
2199 return ERR_PTR(err
);
2201 return __lookup_hash(&this, base
, 0);
2203 EXPORT_SYMBOL(lookup_one_len
);
2205 int user_path_at_empty(int dfd
, const char __user
*name
, unsigned flags
,
2206 struct path
*path
, int *empty
)
2208 struct nameidata nd
;
2209 struct filename
*tmp
= getname_flags(name
, flags
, empty
);
2210 int err
= PTR_ERR(tmp
);
2213 BUG_ON(flags
& LOOKUP_PARENT
);
2215 err
= filename_lookup(dfd
, tmp
, flags
, &nd
);
2223 int user_path_at(int dfd
, const char __user
*name
, unsigned flags
,
2226 return user_path_at_empty(dfd
, name
, flags
, path
, NULL
);
2228 EXPORT_SYMBOL(user_path_at
);
2231 * NB: most callers don't do anything directly with the reference to the
2232 * to struct filename, but the nd->last pointer points into the name string
2233 * allocated by getname. So we must hold the reference to it until all
2234 * path-walking is complete.
2236 static struct filename
*
2237 user_path_parent(int dfd
, const char __user
*path
,
2238 struct path
*parent
,
2243 struct nameidata nd
;
2244 struct filename
*s
= getname(path
);
2247 /* only LOOKUP_REVAL is allowed in extra flags */
2248 flags
&= LOOKUP_REVAL
;
2253 error
= filename_lookup(dfd
, s
, flags
| LOOKUP_PARENT
, &nd
);
2256 return ERR_PTR(error
);
2260 *type
= nd
.last_type
;
2266 * mountpoint_last - look up last component for umount
2267 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2268 * @path: pointer to container for result
2270 * This is a special lookup_last function just for umount. In this case, we
2271 * need to resolve the path without doing any revalidation.
2273 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2274 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2275 * in almost all cases, this lookup will be served out of the dcache. The only
2276 * cases where it won't are if nd->last refers to a symlink or the path is
2277 * bogus and it doesn't exist.
2280 * -error: if there was an error during lookup. This includes -ENOENT if the
2281 * lookup found a negative dentry. The nd->path reference will also be
2284 * 0: if we successfully resolved nd->path and found it to not to be a
2285 * symlink that needs to be followed. "path" will also be populated.
2286 * The nd->path reference will also be put.
2288 * 1: if we successfully resolved nd->last and found it to be a symlink
2289 * that needs to be followed. "path" will be populated with the path
2290 * to the link, and nd->path will *not* be put.
2293 mountpoint_last(struct nameidata
*nd
, struct path
*path
)
2296 struct dentry
*dentry
;
2297 struct dentry
*dir
= nd
->path
.dentry
;
2299 /* If we're in rcuwalk, drop out of it to handle last component */
2300 if (nd
->flags
& LOOKUP_RCU
) {
2301 if (unlazy_walk(nd
, NULL
)) {
2307 nd
->flags
&= ~LOOKUP_PARENT
;
2309 if (unlikely(nd
->last_type
!= LAST_NORM
)) {
2310 error
= handle_dots(nd
, nd
->last_type
);
2313 dentry
= dget(nd
->path
.dentry
);
2317 mutex_lock(&dir
->d_inode
->i_mutex
);
2318 dentry
= d_lookup(dir
, &nd
->last
);
2321 * No cached dentry. Mounted dentries are pinned in the cache,
2322 * so that means that this dentry is probably a symlink or the
2323 * path doesn't actually point to a mounted dentry.
2325 dentry
= d_alloc(dir
, &nd
->last
);
2328 mutex_unlock(&dir
->d_inode
->i_mutex
);
2331 dentry
= lookup_real(dir
->d_inode
, dentry
, nd
->flags
);
2332 error
= PTR_ERR(dentry
);
2333 if (IS_ERR(dentry
)) {
2334 mutex_unlock(&dir
->d_inode
->i_mutex
);
2338 mutex_unlock(&dir
->d_inode
->i_mutex
);
2341 if (d_is_negative(dentry
)) {
2346 path
->dentry
= dentry
;
2347 path
->mnt
= nd
->path
.mnt
;
2348 if (should_follow_link(dentry
, nd
->flags
& LOOKUP_FOLLOW
)) {
2361 * path_mountpoint - look up a path to be umounted
2362 * @dfd: directory file descriptor to start walk from
2363 * @name: full pathname to walk
2364 * @path: pointer to container for result
2365 * @flags: lookup flags
2367 * Look up the given name, but don't attempt to revalidate the last component.
2368 * Returns 0 and "path" will be valid on success; Returns error otherwise.
2371 path_mountpoint(int dfd
, const struct filename
*name
, struct path
*path
,
2372 struct nameidata
*nd
, unsigned int flags
)
2374 int err
= path_init(dfd
, name
, flags
, nd
);
2378 err
= mountpoint_last(nd
, path
);
2380 err
= trailing_symlink(nd
);
2383 err
= mountpoint_last(nd
, path
);
2392 filename_mountpoint(int dfd
, struct filename
*name
, struct path
*path
,
2395 struct nameidata nd
;
2398 return PTR_ERR(name
);
2399 error
= path_mountpoint(dfd
, name
, path
, &nd
, flags
| LOOKUP_RCU
);
2400 if (unlikely(error
== -ECHILD
))
2401 error
= path_mountpoint(dfd
, name
, path
, &nd
, flags
);
2402 if (unlikely(error
== -ESTALE
))
2403 error
= path_mountpoint(dfd
, name
, path
, &nd
, flags
| LOOKUP_REVAL
);
2405 audit_inode(name
, path
->dentry
, 0);
2411 * user_path_mountpoint_at - lookup a path from userland in order to umount it
2412 * @dfd: directory file descriptor
2413 * @name: pathname from userland
2414 * @flags: lookup flags
2415 * @path: pointer to container to hold result
2417 * A umount is a special case for path walking. We're not actually interested
2418 * in the inode in this situation, and ESTALE errors can be a problem. We
2419 * simply want track down the dentry and vfsmount attached at the mountpoint
2420 * and avoid revalidating the last component.
2422 * Returns 0 and populates "path" on success.
2425 user_path_mountpoint_at(int dfd
, const char __user
*name
, unsigned int flags
,
2428 return filename_mountpoint(dfd
, getname(name
), path
, flags
);
2432 kern_path_mountpoint(int dfd
, const char *name
, struct path
*path
,
2435 return filename_mountpoint(dfd
, getname_kernel(name
), path
, flags
);
2437 EXPORT_SYMBOL(kern_path_mountpoint
);
2439 int __check_sticky(struct inode
*dir
, struct inode
*inode
)
2441 kuid_t fsuid
= current_fsuid();
2443 if (uid_eq(inode
->i_uid
, fsuid
))
2445 if (uid_eq(dir
->i_uid
, fsuid
))
2447 return !capable_wrt_inode_uidgid(inode
, CAP_FOWNER
);
2449 EXPORT_SYMBOL(__check_sticky
);
2452 * Check whether we can remove a link victim from directory dir, check
2453 * whether the type of victim is right.
2454 * 1. We can't do it if dir is read-only (done in permission())
2455 * 2. We should have write and exec permissions on dir
2456 * 3. We can't remove anything from append-only dir
2457 * 4. We can't do anything with immutable dir (done in permission())
2458 * 5. If the sticky bit on dir is set we should either
2459 * a. be owner of dir, or
2460 * b. be owner of victim, or
2461 * c. have CAP_FOWNER capability
2462 * 6. If the victim is append-only or immutable we can't do antyhing with
2463 * links pointing to it.
2464 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2465 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2466 * 9. We can't remove a root or mountpoint.
2467 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2468 * nfs_async_unlink().
2470 static int may_delete(struct inode
*dir
, struct dentry
*victim
, bool isdir
)
2472 struct inode
*inode
= victim
->d_inode
;
2475 if (d_is_negative(victim
))
2479 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
2480 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
2482 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
2488 if (check_sticky(dir
, inode
) || IS_APPEND(inode
) ||
2489 IS_IMMUTABLE(inode
) || IS_SWAPFILE(inode
))
2492 if (!d_is_dir(victim
))
2494 if (IS_ROOT(victim
))
2496 } else if (d_is_dir(victim
))
2498 if (IS_DEADDIR(dir
))
2500 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
2505 /* Check whether we can create an object with dentry child in directory
2507 * 1. We can't do it if child already exists (open has special treatment for
2508 * this case, but since we are inlined it's OK)
2509 * 2. We can't do it if dir is read-only (done in permission())
2510 * 3. We should have write and exec permissions on dir
2511 * 4. We can't do it if dir is immutable (done in permission())
2513 static inline int may_create(struct inode
*dir
, struct dentry
*child
)
2515 audit_inode_child(dir
, child
, AUDIT_TYPE_CHILD_CREATE
);
2518 if (IS_DEADDIR(dir
))
2520 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
2524 * p1 and p2 should be directories on the same fs.
2526 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
2531 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2535 mutex_lock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
2537 p
= d_ancestor(p2
, p1
);
2539 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2540 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
2544 p
= d_ancestor(p1
, p2
);
2546 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2547 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
2551 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2552 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_PARENT2
);
2555 EXPORT_SYMBOL(lock_rename
);
2557 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
2559 mutex_unlock(&p1
->d_inode
->i_mutex
);
2561 mutex_unlock(&p2
->d_inode
->i_mutex
);
2562 mutex_unlock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
2565 EXPORT_SYMBOL(unlock_rename
);
2567 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2570 int error
= may_create(dir
, dentry
);
2574 if (!dir
->i_op
->create
)
2575 return -EACCES
; /* shouldn't it be ENOSYS? */
2578 error
= security_inode_create(dir
, dentry
, mode
);
2581 error
= dir
->i_op
->create(dir
, dentry
, mode
, want_excl
);
2583 fsnotify_create(dir
, dentry
);
2586 EXPORT_SYMBOL(vfs_create
);
2588 static int may_open(struct path
*path
, int acc_mode
, int flag
)
2590 struct dentry
*dentry
= path
->dentry
;
2591 struct inode
*inode
= dentry
->d_inode
;
2601 switch (inode
->i_mode
& S_IFMT
) {
2605 if (acc_mode
& MAY_WRITE
)
2610 if (path
->mnt
->mnt_flags
& MNT_NODEV
)
2619 error
= inode_permission(inode
, acc_mode
);
2624 * An append-only file must be opened in append mode for writing.
2626 if (IS_APPEND(inode
)) {
2627 if ((flag
& O_ACCMODE
) != O_RDONLY
&& !(flag
& O_APPEND
))
2633 /* O_NOATIME can only be set by the owner or superuser */
2634 if (flag
& O_NOATIME
&& !inode_owner_or_capable(inode
))
2640 static int handle_truncate(struct file
*filp
)
2642 struct path
*path
= &filp
->f_path
;
2643 struct inode
*inode
= path
->dentry
->d_inode
;
2644 int error
= get_write_access(inode
);
2648 * Refuse to truncate files with mandatory locks held on them.
2650 error
= locks_verify_locked(filp
);
2652 error
= security_path_truncate(path
);
2654 error
= do_truncate(path
->dentry
, 0,
2655 ATTR_MTIME
|ATTR_CTIME
|ATTR_OPEN
,
2658 put_write_access(inode
);
2662 static inline int open_to_namei_flags(int flag
)
2664 if ((flag
& O_ACCMODE
) == 3)
2669 static int may_o_create(struct path
*dir
, struct dentry
*dentry
, umode_t mode
)
2671 int error
= security_path_mknod(dir
, dentry
, mode
, 0);
2675 error
= inode_permission(dir
->dentry
->d_inode
, MAY_WRITE
| MAY_EXEC
);
2679 return security_inode_create(dir
->dentry
->d_inode
, dentry
, mode
);
2683 * Attempt to atomically look up, create and open a file from a negative
2686 * Returns 0 if successful. The file will have been created and attached to
2687 * @file by the filesystem calling finish_open().
2689 * Returns 1 if the file was looked up only or didn't need creating. The
2690 * caller will need to perform the open themselves. @path will have been
2691 * updated to point to the new dentry. This may be negative.
2693 * Returns an error code otherwise.
2695 static int atomic_open(struct nameidata
*nd
, struct dentry
*dentry
,
2696 struct path
*path
, struct file
*file
,
2697 const struct open_flags
*op
,
2698 bool got_write
, bool need_lookup
,
2701 struct inode
*dir
= nd
->path
.dentry
->d_inode
;
2702 unsigned open_flag
= open_to_namei_flags(op
->open_flag
);
2706 int create_error
= 0;
2707 struct dentry
*const DENTRY_NOT_SET
= (void *) -1UL;
2710 BUG_ON(dentry
->d_inode
);
2712 /* Don't create child dentry for a dead directory. */
2713 if (unlikely(IS_DEADDIR(dir
))) {
2719 if ((open_flag
& O_CREAT
) && !IS_POSIXACL(dir
))
2720 mode
&= ~current_umask();
2722 excl
= (open_flag
& (O_EXCL
| O_CREAT
)) == (O_EXCL
| O_CREAT
);
2724 open_flag
&= ~O_TRUNC
;
2727 * Checking write permission is tricky, bacuse we don't know if we are
2728 * going to actually need it: O_CREAT opens should work as long as the
2729 * file exists. But checking existence breaks atomicity. The trick is
2730 * to check access and if not granted clear O_CREAT from the flags.
2732 * Another problem is returing the "right" error value (e.g. for an
2733 * O_EXCL open we want to return EEXIST not EROFS).
2735 if (((open_flag
& (O_CREAT
| O_TRUNC
)) ||
2736 (open_flag
& O_ACCMODE
) != O_RDONLY
) && unlikely(!got_write
)) {
2737 if (!(open_flag
& O_CREAT
)) {
2739 * No O_CREATE -> atomicity not a requirement -> fall
2740 * back to lookup + open
2743 } else if (open_flag
& (O_EXCL
| O_TRUNC
)) {
2744 /* Fall back and fail with the right error */
2745 create_error
= -EROFS
;
2748 /* No side effects, safe to clear O_CREAT */
2749 create_error
= -EROFS
;
2750 open_flag
&= ~O_CREAT
;
2754 if (open_flag
& O_CREAT
) {
2755 error
= may_o_create(&nd
->path
, dentry
, mode
);
2757 create_error
= error
;
2758 if (open_flag
& O_EXCL
)
2760 open_flag
&= ~O_CREAT
;
2764 if (nd
->flags
& LOOKUP_DIRECTORY
)
2765 open_flag
|= O_DIRECTORY
;
2767 file
->f_path
.dentry
= DENTRY_NOT_SET
;
2768 file
->f_path
.mnt
= nd
->path
.mnt
;
2769 error
= dir
->i_op
->atomic_open(dir
, dentry
, file
, open_flag
, mode
,
2772 if (create_error
&& error
== -ENOENT
)
2773 error
= create_error
;
2777 if (error
) { /* returned 1, that is */
2778 if (WARN_ON(file
->f_path
.dentry
== DENTRY_NOT_SET
)) {
2782 if (file
->f_path
.dentry
) {
2784 dentry
= file
->f_path
.dentry
;
2786 if (*opened
& FILE_CREATED
)
2787 fsnotify_create(dir
, dentry
);
2788 if (!dentry
->d_inode
) {
2789 WARN_ON(*opened
& FILE_CREATED
);
2791 error
= create_error
;
2795 if (excl
&& !(*opened
& FILE_CREATED
)) {
2804 * We didn't have the inode before the open, so check open permission
2807 acc_mode
= op
->acc_mode
;
2808 if (*opened
& FILE_CREATED
) {
2809 WARN_ON(!(open_flag
& O_CREAT
));
2810 fsnotify_create(dir
, dentry
);
2811 acc_mode
= MAY_OPEN
;
2813 error
= may_open(&file
->f_path
, acc_mode
, open_flag
);
2823 dentry
= lookup_real(dir
, dentry
, nd
->flags
);
2825 return PTR_ERR(dentry
);
2828 int open_flag
= op
->open_flag
;
2830 error
= create_error
;
2831 if ((open_flag
& O_EXCL
)) {
2832 if (!dentry
->d_inode
)
2834 } else if (!dentry
->d_inode
) {
2836 } else if ((open_flag
& O_TRUNC
) &&
2840 /* will fail later, go on to get the right error */
2844 path
->dentry
= dentry
;
2845 path
->mnt
= nd
->path
.mnt
;
2850 * Look up and maybe create and open the last component.
2852 * Must be called with i_mutex held on parent.
2854 * Returns 0 if the file was successfully atomically created (if necessary) and
2855 * opened. In this case the file will be returned attached to @file.
2857 * Returns 1 if the file was not completely opened at this time, though lookups
2858 * and creations will have been performed and the dentry returned in @path will
2859 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2860 * specified then a negative dentry may be returned.
2862 * An error code is returned otherwise.
2864 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2865 * cleared otherwise prior to returning.
2867 static int lookup_open(struct nameidata
*nd
, struct path
*path
,
2869 const struct open_flags
*op
,
2870 bool got_write
, int *opened
)
2872 struct dentry
*dir
= nd
->path
.dentry
;
2873 struct inode
*dir_inode
= dir
->d_inode
;
2874 struct dentry
*dentry
;
2878 *opened
&= ~FILE_CREATED
;
2879 dentry
= lookup_dcache(&nd
->last
, dir
, nd
->flags
, &need_lookup
);
2881 return PTR_ERR(dentry
);
2883 /* Cached positive dentry: will open in f_op->open */
2884 if (!need_lookup
&& dentry
->d_inode
)
2887 if ((nd
->flags
& LOOKUP_OPEN
) && dir_inode
->i_op
->atomic_open
) {
2888 return atomic_open(nd
, dentry
, path
, file
, op
, got_write
,
2889 need_lookup
, opened
);
2893 BUG_ON(dentry
->d_inode
);
2895 dentry
= lookup_real(dir_inode
, dentry
, nd
->flags
);
2897 return PTR_ERR(dentry
);
2900 /* Negative dentry, just create the file */
2901 if (!dentry
->d_inode
&& (op
->open_flag
& O_CREAT
)) {
2902 umode_t mode
= op
->mode
;
2903 if (!IS_POSIXACL(dir
->d_inode
))
2904 mode
&= ~current_umask();
2906 * This write is needed to ensure that a
2907 * rw->ro transition does not occur between
2908 * the time when the file is created and when
2909 * a permanent write count is taken through
2910 * the 'struct file' in finish_open().
2916 *opened
|= FILE_CREATED
;
2917 error
= security_path_mknod(&nd
->path
, dentry
, mode
, 0);
2920 error
= vfs_create(dir
->d_inode
, dentry
, mode
,
2921 nd
->flags
& LOOKUP_EXCL
);
2926 path
->dentry
= dentry
;
2927 path
->mnt
= nd
->path
.mnt
;
2936 * Handle the last step of open()
2938 static int do_last(struct nameidata
*nd
,
2939 struct file
*file
, const struct open_flags
*op
,
2940 int *opened
, struct filename
*name
)
2942 struct dentry
*dir
= nd
->path
.dentry
;
2943 int open_flag
= op
->open_flag
;
2944 bool will_truncate
= (open_flag
& O_TRUNC
) != 0;
2945 bool got_write
= false;
2946 int acc_mode
= op
->acc_mode
;
2947 struct inode
*inode
;
2948 struct path save_parent
= { .dentry
= NULL
, .mnt
= NULL
};
2950 bool retried
= false;
2953 nd
->flags
&= ~LOOKUP_PARENT
;
2954 nd
->flags
|= op
->intent
;
2956 if (nd
->last_type
!= LAST_NORM
) {
2957 error
= handle_dots(nd
, nd
->last_type
);
2963 if (!(open_flag
& O_CREAT
)) {
2964 if (nd
->last
.name
[nd
->last
.len
])
2965 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
2966 /* we _can_ be in RCU mode here */
2967 error
= lookup_fast(nd
, &path
, &inode
);
2974 BUG_ON(nd
->inode
!= dir
->d_inode
);
2976 /* create side of things */
2978 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2979 * has been cleared when we got to the last component we are
2982 error
= complete_walk(nd
);
2986 audit_inode(name
, dir
, LOOKUP_PARENT
);
2988 /* trailing slashes? */
2989 if (nd
->last
.name
[nd
->last
.len
])
2994 if (op
->open_flag
& (O_CREAT
| O_TRUNC
| O_WRONLY
| O_RDWR
)) {
2995 error
= mnt_want_write(nd
->path
.mnt
);
2999 * do _not_ fail yet - we might not need that or fail with
3000 * a different error; let lookup_open() decide; we'll be
3001 * dropping this one anyway.
3004 mutex_lock(&dir
->d_inode
->i_mutex
);
3005 error
= lookup_open(nd
, &path
, file
, op
, got_write
, opened
);
3006 mutex_unlock(&dir
->d_inode
->i_mutex
);
3012 if ((*opened
& FILE_CREATED
) ||
3013 !S_ISREG(file_inode(file
)->i_mode
))
3014 will_truncate
= false;
3016 audit_inode(name
, file
->f_path
.dentry
, 0);
3020 if (*opened
& FILE_CREATED
) {
3021 /* Don't check for write permission, don't truncate */
3022 open_flag
&= ~O_TRUNC
;
3023 will_truncate
= false;
3024 acc_mode
= MAY_OPEN
;
3025 path_to_nameidata(&path
, nd
);
3026 goto finish_open_created
;
3030 * create/update audit record if it already exists.
3032 if (d_is_positive(path
.dentry
))
3033 audit_inode(name
, path
.dentry
, 0);
3036 * If atomic_open() acquired write access it is dropped now due to
3037 * possible mount and symlink following (this might be optimized away if
3041 mnt_drop_write(nd
->path
.mnt
);
3046 if ((open_flag
& (O_EXCL
| O_CREAT
)) == (O_EXCL
| O_CREAT
))
3049 error
= follow_managed(&path
, nd
->flags
);
3054 nd
->flags
|= LOOKUP_JUMPED
;
3056 BUG_ON(nd
->flags
& LOOKUP_RCU
);
3057 inode
= path
.dentry
->d_inode
;
3059 if (d_is_negative(path
.dentry
)) {
3060 path_to_nameidata(&path
, nd
);
3064 if (should_follow_link(path
.dentry
, nd
->flags
& LOOKUP_FOLLOW
)) {
3065 if (nd
->flags
& LOOKUP_RCU
) {
3066 if (unlikely(nd
->path
.mnt
!= path
.mnt
||
3067 unlazy_walk(nd
, path
.dentry
))) {
3072 BUG_ON(inode
!= path
.dentry
->d_inode
);
3077 if (unlikely(d_is_symlink(path
.dentry
)) && !(open_flag
& O_PATH
)) {
3078 path_to_nameidata(&path
, nd
);
3083 if ((nd
->flags
& LOOKUP_RCU
) || nd
->path
.mnt
!= path
.mnt
) {
3084 path_to_nameidata(&path
, nd
);
3086 save_parent
.dentry
= nd
->path
.dentry
;
3087 save_parent
.mnt
= mntget(path
.mnt
);
3088 nd
->path
.dentry
= path
.dentry
;
3092 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
3094 error
= complete_walk(nd
);
3096 path_put(&save_parent
);
3099 audit_inode(name
, nd
->path
.dentry
, 0);
3101 if ((open_flag
& O_CREAT
) && d_is_dir(nd
->path
.dentry
))
3104 if ((nd
->flags
& LOOKUP_DIRECTORY
) && !d_can_lookup(nd
->path
.dentry
))
3106 if (!d_is_reg(nd
->path
.dentry
))
3107 will_truncate
= false;
3109 if (will_truncate
) {
3110 error
= mnt_want_write(nd
->path
.mnt
);
3115 finish_open_created
:
3116 error
= may_open(&nd
->path
, acc_mode
, open_flag
);
3120 BUG_ON(*opened
& FILE_OPENED
); /* once it's opened, it's opened */
3121 error
= vfs_open(&nd
->path
, file
, current_cred());
3123 *opened
|= FILE_OPENED
;
3125 if (error
== -EOPENSTALE
)
3130 error
= open_check_o_direct(file
);
3133 error
= ima_file_check(file
, op
->acc_mode
, *opened
);
3137 if (will_truncate
) {
3138 error
= handle_truncate(file
);
3144 mnt_drop_write(nd
->path
.mnt
);
3145 path_put(&save_parent
);
3150 path_put_conditional(&path
, nd
);
3157 /* If no saved parent or already retried then can't retry */
3158 if (!save_parent
.dentry
|| retried
)
3161 BUG_ON(save_parent
.dentry
!= dir
);
3162 path_put(&nd
->path
);
3163 nd
->path
= save_parent
;
3164 nd
->inode
= dir
->d_inode
;
3165 save_parent
.mnt
= NULL
;
3166 save_parent
.dentry
= NULL
;
3168 mnt_drop_write(nd
->path
.mnt
);
3175 static int do_tmpfile(int dfd
, struct filename
*pathname
,
3176 struct nameidata
*nd
, int flags
,
3177 const struct open_flags
*op
,
3178 struct file
*file
, int *opened
)
3180 static const struct qstr name
= QSTR_INIT("/", 1);
3181 struct dentry
*dentry
, *child
;
3183 int error
= path_lookupat(dfd
, pathname
,
3184 flags
| LOOKUP_DIRECTORY
, nd
);
3185 if (unlikely(error
))
3187 error
= mnt_want_write(nd
->path
.mnt
);
3188 if (unlikely(error
))
3190 /* we want directory to be writable */
3191 error
= inode_permission(nd
->inode
, MAY_WRITE
| MAY_EXEC
);
3194 dentry
= nd
->path
.dentry
;
3195 dir
= dentry
->d_inode
;
3196 if (!dir
->i_op
->tmpfile
) {
3197 error
= -EOPNOTSUPP
;
3200 child
= d_alloc(dentry
, &name
);
3201 if (unlikely(!child
)) {
3205 nd
->flags
&= ~LOOKUP_DIRECTORY
;
3206 nd
->flags
|= op
->intent
;
3207 dput(nd
->path
.dentry
);
3208 nd
->path
.dentry
= child
;
3209 error
= dir
->i_op
->tmpfile(dir
, nd
->path
.dentry
, op
->mode
);
3212 audit_inode(pathname
, nd
->path
.dentry
, 0);
3213 /* Don't check for other permissions, the inode was just created */
3214 error
= may_open(&nd
->path
, MAY_OPEN
, op
->open_flag
);
3217 file
->f_path
.mnt
= nd
->path
.mnt
;
3218 error
= finish_open(file
, nd
->path
.dentry
, NULL
, opened
);
3221 error
= open_check_o_direct(file
);
3224 } else if (!(op
->open_flag
& O_EXCL
)) {
3225 struct inode
*inode
= file_inode(file
);
3226 spin_lock(&inode
->i_lock
);
3227 inode
->i_state
|= I_LINKABLE
;
3228 spin_unlock(&inode
->i_lock
);
3231 mnt_drop_write(nd
->path
.mnt
);
3233 path_put(&nd
->path
);
3237 static struct file
*path_openat(int dfd
, struct filename
*pathname
,
3238 struct nameidata
*nd
, const struct open_flags
*op
, int flags
)
3244 file
= get_empty_filp();
3248 file
->f_flags
= op
->open_flag
;
3250 if (unlikely(file
->f_flags
& __O_TMPFILE
)) {
3251 error
= do_tmpfile(dfd
, pathname
, nd
, flags
, op
, file
, &opened
);
3255 error
= path_init(dfd
, pathname
, flags
, nd
);
3256 if (unlikely(error
))
3259 error
= do_last(nd
, file
, op
, &opened
, pathname
);
3260 while (unlikely(error
> 0)) { /* trailing symlink */
3261 nd
->flags
&= ~(LOOKUP_OPEN
|LOOKUP_CREATE
|LOOKUP_EXCL
);
3262 error
= trailing_symlink(nd
);
3263 if (unlikely(error
))
3265 error
= do_last(nd
, file
, op
, &opened
, pathname
);
3271 if (!(opened
& FILE_OPENED
)) {
3275 if (unlikely(error
)) {
3276 if (error
== -EOPENSTALE
) {
3277 if (flags
& LOOKUP_RCU
)
3282 file
= ERR_PTR(error
);
3287 struct file
*do_filp_open(int dfd
, struct filename
*pathname
,
3288 const struct open_flags
*op
)
3290 struct nameidata nd
;
3291 int flags
= op
->lookup_flags
;
3294 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
| LOOKUP_RCU
);
3295 if (unlikely(filp
== ERR_PTR(-ECHILD
)))
3296 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
);
3297 if (unlikely(filp
== ERR_PTR(-ESTALE
)))
3298 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
| LOOKUP_REVAL
);
3302 struct file
*do_file_open_root(struct dentry
*dentry
, struct vfsmount
*mnt
,
3303 const char *name
, const struct open_flags
*op
)
3305 struct nameidata nd
;
3307 struct filename
*filename
;
3308 int flags
= op
->lookup_flags
| LOOKUP_ROOT
;
3311 nd
.root
.dentry
= dentry
;
3313 if (d_is_symlink(dentry
) && op
->intent
& LOOKUP_OPEN
)
3314 return ERR_PTR(-ELOOP
);
3316 filename
= getname_kernel(name
);
3317 if (unlikely(IS_ERR(filename
)))
3318 return ERR_CAST(filename
);
3320 file
= path_openat(-1, filename
, &nd
, op
, flags
| LOOKUP_RCU
);
3321 if (unlikely(file
== ERR_PTR(-ECHILD
)))
3322 file
= path_openat(-1, filename
, &nd
, op
, flags
);
3323 if (unlikely(file
== ERR_PTR(-ESTALE
)))
3324 file
= path_openat(-1, filename
, &nd
, op
, flags
| LOOKUP_REVAL
);
3329 static struct dentry
*filename_create(int dfd
, struct filename
*name
,
3330 struct path
*path
, unsigned int lookup_flags
)
3332 struct dentry
*dentry
= ERR_PTR(-EEXIST
);
3333 struct nameidata nd
;
3336 bool is_dir
= (lookup_flags
& LOOKUP_DIRECTORY
);
3339 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3340 * other flags passed in are ignored!
3342 lookup_flags
&= LOOKUP_REVAL
;
3344 error
= filename_lookup(dfd
, name
, LOOKUP_PARENT
|lookup_flags
, &nd
);
3346 return ERR_PTR(error
);
3349 * Yucky last component or no last component at all?
3350 * (foo/., foo/.., /////)
3352 if (nd
.last_type
!= LAST_NORM
)
3354 nd
.flags
&= ~LOOKUP_PARENT
;
3355 nd
.flags
|= LOOKUP_CREATE
| LOOKUP_EXCL
;
3357 /* don't fail immediately if it's r/o, at least try to report other errors */
3358 err2
= mnt_want_write(nd
.path
.mnt
);
3360 * Do the final lookup.
3362 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
3363 dentry
= __lookup_hash(&nd
.last
, nd
.path
.dentry
, nd
.flags
);
3368 if (d_is_positive(dentry
))
3372 * Special case - lookup gave negative, but... we had foo/bar/
3373 * From the vfs_mknod() POV we just have a negative dentry -
3374 * all is fine. Let's be bastards - you had / on the end, you've
3375 * been asking for (non-existent) directory. -ENOENT for you.
3377 if (unlikely(!is_dir
&& nd
.last
.name
[nd
.last
.len
])) {
3381 if (unlikely(err2
)) {
3389 dentry
= ERR_PTR(error
);
3391 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
3393 mnt_drop_write(nd
.path
.mnt
);
3399 struct dentry
*kern_path_create(int dfd
, const char *pathname
,
3400 struct path
*path
, unsigned int lookup_flags
)
3402 struct filename
*filename
= getname_kernel(pathname
);
3405 if (IS_ERR(filename
))
3406 return ERR_CAST(filename
);
3407 res
= filename_create(dfd
, filename
, path
, lookup_flags
);
3411 EXPORT_SYMBOL(kern_path_create
);
3413 void done_path_create(struct path
*path
, struct dentry
*dentry
)
3416 mutex_unlock(&path
->dentry
->d_inode
->i_mutex
);
3417 mnt_drop_write(path
->mnt
);
3420 EXPORT_SYMBOL(done_path_create
);
3422 struct dentry
*user_path_create(int dfd
, const char __user
*pathname
,
3423 struct path
*path
, unsigned int lookup_flags
)
3425 struct filename
*tmp
= getname(pathname
);
3428 return ERR_CAST(tmp
);
3429 res
= filename_create(dfd
, tmp
, path
, lookup_flags
);
3433 EXPORT_SYMBOL(user_path_create
);
3435 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t dev
)
3437 int error
= may_create(dir
, dentry
);
3442 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
3445 if (!dir
->i_op
->mknod
)
3448 error
= devcgroup_inode_mknod(mode
, dev
);
3452 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
3456 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
3458 fsnotify_create(dir
, dentry
);
3461 EXPORT_SYMBOL(vfs_mknod
);
3463 static int may_mknod(umode_t mode
)
3465 switch (mode
& S_IFMT
) {
3471 case 0: /* zero mode translates to S_IFREG */
3480 SYSCALL_DEFINE4(mknodat
, int, dfd
, const char __user
*, filename
, umode_t
, mode
,
3483 struct dentry
*dentry
;
3486 unsigned int lookup_flags
= 0;
3488 error
= may_mknod(mode
);
3492 dentry
= user_path_create(dfd
, filename
, &path
, lookup_flags
);
3494 return PTR_ERR(dentry
);
3496 if (!IS_POSIXACL(path
.dentry
->d_inode
))
3497 mode
&= ~current_umask();
3498 error
= security_path_mknod(&path
, dentry
, mode
, dev
);
3501 switch (mode
& S_IFMT
) {
3502 case 0: case S_IFREG
:
3503 error
= vfs_create(path
.dentry
->d_inode
,dentry
,mode
,true);
3505 case S_IFCHR
: case S_IFBLK
:
3506 error
= vfs_mknod(path
.dentry
->d_inode
,dentry
,mode
,
3507 new_decode_dev(dev
));
3509 case S_IFIFO
: case S_IFSOCK
:
3510 error
= vfs_mknod(path
.dentry
->d_inode
,dentry
,mode
,0);
3514 done_path_create(&path
, dentry
);
3515 if (retry_estale(error
, lookup_flags
)) {
3516 lookup_flags
|= LOOKUP_REVAL
;
3522 SYSCALL_DEFINE3(mknod
, const char __user
*, filename
, umode_t
, mode
, unsigned, dev
)
3524 return sys_mknodat(AT_FDCWD
, filename
, mode
, dev
);
3527 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
3529 int error
= may_create(dir
, dentry
);
3530 unsigned max_links
= dir
->i_sb
->s_max_links
;
3535 if (!dir
->i_op
->mkdir
)
3538 mode
&= (S_IRWXUGO
|S_ISVTX
);
3539 error
= security_inode_mkdir(dir
, dentry
, mode
);
3543 if (max_links
&& dir
->i_nlink
>= max_links
)
3546 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
3548 fsnotify_mkdir(dir
, dentry
);
3551 EXPORT_SYMBOL(vfs_mkdir
);
3553 SYSCALL_DEFINE3(mkdirat
, int, dfd
, const char __user
*, pathname
, umode_t
, mode
)
3555 struct dentry
*dentry
;
3558 unsigned int lookup_flags
= LOOKUP_DIRECTORY
;
3561 dentry
= user_path_create(dfd
, pathname
, &path
, lookup_flags
);
3563 return PTR_ERR(dentry
);
3565 if (!IS_POSIXACL(path
.dentry
->d_inode
))
3566 mode
&= ~current_umask();
3567 error
= security_path_mkdir(&path
, dentry
, mode
);
3569 error
= vfs_mkdir(path
.dentry
->d_inode
, dentry
, mode
);
3570 done_path_create(&path
, dentry
);
3571 if (retry_estale(error
, lookup_flags
)) {
3572 lookup_flags
|= LOOKUP_REVAL
;
3578 SYSCALL_DEFINE2(mkdir
, const char __user
*, pathname
, umode_t
, mode
)
3580 return sys_mkdirat(AT_FDCWD
, pathname
, mode
);
3584 * The dentry_unhash() helper will try to drop the dentry early: we
3585 * should have a usage count of 1 if we're the only user of this
3586 * dentry, and if that is true (possibly after pruning the dcache),
3587 * then we drop the dentry now.
3589 * A low-level filesystem can, if it choses, legally
3592 * if (!d_unhashed(dentry))
3595 * if it cannot handle the case of removing a directory
3596 * that is still in use by something else..
3598 void dentry_unhash(struct dentry
*dentry
)
3600 shrink_dcache_parent(dentry
);
3601 spin_lock(&dentry
->d_lock
);
3602 if (dentry
->d_lockref
.count
== 1)
3604 spin_unlock(&dentry
->d_lock
);
3606 EXPORT_SYMBOL(dentry_unhash
);
3608 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3610 int error
= may_delete(dir
, dentry
, 1);
3615 if (!dir
->i_op
->rmdir
)
3619 mutex_lock(&dentry
->d_inode
->i_mutex
);
3622 if (is_local_mountpoint(dentry
))
3625 error
= security_inode_rmdir(dir
, dentry
);
3629 shrink_dcache_parent(dentry
);
3630 error
= dir
->i_op
->rmdir(dir
, dentry
);
3634 dentry
->d_inode
->i_flags
|= S_DEAD
;
3636 detach_mounts(dentry
);
3639 mutex_unlock(&dentry
->d_inode
->i_mutex
);
3645 EXPORT_SYMBOL(vfs_rmdir
);
3647 static long do_rmdir(int dfd
, const char __user
*pathname
)
3650 struct filename
*name
;
3651 struct dentry
*dentry
;
3655 unsigned int lookup_flags
= 0;
3657 name
= user_path_parent(dfd
, pathname
,
3658 &path
, &last
, &type
, lookup_flags
);
3660 return PTR_ERR(name
);
3674 error
= mnt_want_write(path
.mnt
);
3678 mutex_lock_nested(&path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
3679 dentry
= __lookup_hash(&last
, path
.dentry
, lookup_flags
);
3680 error
= PTR_ERR(dentry
);
3683 if (!dentry
->d_inode
) {
3687 error
= security_path_rmdir(&path
, dentry
);
3690 error
= vfs_rmdir(path
.dentry
->d_inode
, dentry
);
3694 mutex_unlock(&path
.dentry
->d_inode
->i_mutex
);
3695 mnt_drop_write(path
.mnt
);
3699 if (retry_estale(error
, lookup_flags
)) {
3700 lookup_flags
|= LOOKUP_REVAL
;
3706 SYSCALL_DEFINE1(rmdir
, const char __user
*, pathname
)
3708 return do_rmdir(AT_FDCWD
, pathname
);
3712 * vfs_unlink - unlink a filesystem object
3713 * @dir: parent directory
3715 * @delegated_inode: returns victim inode, if the inode is delegated.
3717 * The caller must hold dir->i_mutex.
3719 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3720 * return a reference to the inode in delegated_inode. The caller
3721 * should then break the delegation on that inode and retry. Because
3722 * breaking a delegation may take a long time, the caller should drop
3723 * dir->i_mutex before doing so.
3725 * Alternatively, a caller may pass NULL for delegated_inode. This may
3726 * be appropriate for callers that expect the underlying filesystem not
3727 * to be NFS exported.
3729 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
, struct inode
**delegated_inode
)
3731 struct inode
*target
= dentry
->d_inode
;
3732 int error
= may_delete(dir
, dentry
, 0);
3737 if (!dir
->i_op
->unlink
)
3740 mutex_lock(&target
->i_mutex
);
3741 if (is_local_mountpoint(dentry
))
3744 error
= security_inode_unlink(dir
, dentry
);
3746 error
= try_break_deleg(target
, delegated_inode
);
3749 error
= dir
->i_op
->unlink(dir
, dentry
);
3752 detach_mounts(dentry
);
3757 mutex_unlock(&target
->i_mutex
);
3759 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3760 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
3761 fsnotify_link_count(target
);
3767 EXPORT_SYMBOL(vfs_unlink
);
3770 * Make sure that the actual truncation of the file will occur outside its
3771 * directory's i_mutex. Truncate can take a long time if there is a lot of
3772 * writeout happening, and we don't want to prevent access to the directory
3773 * while waiting on the I/O.
3775 static long do_unlinkat(int dfd
, const char __user
*pathname
)
3778 struct filename
*name
;
3779 struct dentry
*dentry
;
3783 struct inode
*inode
= NULL
;
3784 struct inode
*delegated_inode
= NULL
;
3785 unsigned int lookup_flags
= 0;
3787 name
= user_path_parent(dfd
, pathname
,
3788 &path
, &last
, &type
, lookup_flags
);
3790 return PTR_ERR(name
);
3793 if (type
!= LAST_NORM
)
3796 error
= mnt_want_write(path
.mnt
);
3800 mutex_lock_nested(&path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
3801 dentry
= __lookup_hash(&last
, path
.dentry
, lookup_flags
);
3802 error
= PTR_ERR(dentry
);
3803 if (!IS_ERR(dentry
)) {
3804 /* Why not before? Because we want correct error value */
3805 if (last
.name
[last
.len
])
3807 inode
= dentry
->d_inode
;
3808 if (d_is_negative(dentry
))
3811 error
= security_path_unlink(&path
, dentry
);
3814 error
= vfs_unlink(path
.dentry
->d_inode
, dentry
, &delegated_inode
);
3818 mutex_unlock(&path
.dentry
->d_inode
->i_mutex
);
3820 iput(inode
); /* truncate the inode here */
3822 if (delegated_inode
) {
3823 error
= break_deleg_wait(&delegated_inode
);
3827 mnt_drop_write(path
.mnt
);
3831 if (retry_estale(error
, lookup_flags
)) {
3832 lookup_flags
|= LOOKUP_REVAL
;
3839 if (d_is_negative(dentry
))
3841 else if (d_is_dir(dentry
))
3848 SYSCALL_DEFINE3(unlinkat
, int, dfd
, const char __user
*, pathname
, int, flag
)
3850 if ((flag
& ~AT_REMOVEDIR
) != 0)
3853 if (flag
& AT_REMOVEDIR
)
3854 return do_rmdir(dfd
, pathname
);
3856 return do_unlinkat(dfd
, pathname
);
3859 SYSCALL_DEFINE1(unlink
, const char __user
*, pathname
)
3861 return do_unlinkat(AT_FDCWD
, pathname
);
3864 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
3866 int error
= may_create(dir
, dentry
);
3871 if (!dir
->i_op
->symlink
)
3874 error
= security_inode_symlink(dir
, dentry
, oldname
);
3878 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
3880 fsnotify_create(dir
, dentry
);
3883 EXPORT_SYMBOL(vfs_symlink
);
3885 SYSCALL_DEFINE3(symlinkat
, const char __user
*, oldname
,
3886 int, newdfd
, const char __user
*, newname
)
3889 struct filename
*from
;
3890 struct dentry
*dentry
;
3892 unsigned int lookup_flags
= 0;
3894 from
= getname(oldname
);
3896 return PTR_ERR(from
);
3898 dentry
= user_path_create(newdfd
, newname
, &path
, lookup_flags
);
3899 error
= PTR_ERR(dentry
);
3903 error
= security_path_symlink(&path
, dentry
, from
->name
);
3905 error
= vfs_symlink(path
.dentry
->d_inode
, dentry
, from
->name
);
3906 done_path_create(&path
, dentry
);
3907 if (retry_estale(error
, lookup_flags
)) {
3908 lookup_flags
|= LOOKUP_REVAL
;
3916 SYSCALL_DEFINE2(symlink
, const char __user
*, oldname
, const char __user
*, newname
)
3918 return sys_symlinkat(oldname
, AT_FDCWD
, newname
);
3922 * vfs_link - create a new link
3923 * @old_dentry: object to be linked
3925 * @new_dentry: where to create the new link
3926 * @delegated_inode: returns inode needing a delegation break
3928 * The caller must hold dir->i_mutex
3930 * If vfs_link discovers a delegation on the to-be-linked file in need
3931 * of breaking, it will return -EWOULDBLOCK and return a reference to the
3932 * inode in delegated_inode. The caller should then break the delegation
3933 * and retry. Because breaking a delegation may take a long time, the
3934 * caller should drop the i_mutex before doing so.
3936 * Alternatively, a caller may pass NULL for delegated_inode. This may
3937 * be appropriate for callers that expect the underlying filesystem not
3938 * to be NFS exported.
3940 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
, struct inode
**delegated_inode
)
3942 struct inode
*inode
= old_dentry
->d_inode
;
3943 unsigned max_links
= dir
->i_sb
->s_max_links
;
3949 error
= may_create(dir
, new_dentry
);
3953 if (dir
->i_sb
!= inode
->i_sb
)
3957 * A link to an append-only or immutable file cannot be created.
3959 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
3961 if (!dir
->i_op
->link
)
3963 if (S_ISDIR(inode
->i_mode
))
3966 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
3970 mutex_lock(&inode
->i_mutex
);
3971 /* Make sure we don't allow creating hardlink to an unlinked file */
3972 if (inode
->i_nlink
== 0 && !(inode
->i_state
& I_LINKABLE
))
3974 else if (max_links
&& inode
->i_nlink
>= max_links
)
3977 error
= try_break_deleg(inode
, delegated_inode
);
3979 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
3982 if (!error
&& (inode
->i_state
& I_LINKABLE
)) {
3983 spin_lock(&inode
->i_lock
);
3984 inode
->i_state
&= ~I_LINKABLE
;
3985 spin_unlock(&inode
->i_lock
);
3987 mutex_unlock(&inode
->i_mutex
);
3989 fsnotify_link(dir
, inode
, new_dentry
);
3992 EXPORT_SYMBOL(vfs_link
);
3995 * Hardlinks are often used in delicate situations. We avoid
3996 * security-related surprises by not following symlinks on the
3999 * We don't follow them on the oldname either to be compatible
4000 * with linux 2.0, and to avoid hard-linking to directories
4001 * and other special files. --ADM
4003 SYSCALL_DEFINE5(linkat
, int, olddfd
, const char __user
*, oldname
,
4004 int, newdfd
, const char __user
*, newname
, int, flags
)
4006 struct dentry
*new_dentry
;
4007 struct path old_path
, new_path
;
4008 struct inode
*delegated_inode
= NULL
;
4012 if ((flags
& ~(AT_SYMLINK_FOLLOW
| AT_EMPTY_PATH
)) != 0)
4015 * To use null names we require CAP_DAC_READ_SEARCH
4016 * This ensures that not everyone will be able to create
4017 * handlink using the passed filedescriptor.
4019 if (flags
& AT_EMPTY_PATH
) {
4020 if (!capable(CAP_DAC_READ_SEARCH
))
4025 if (flags
& AT_SYMLINK_FOLLOW
)
4026 how
|= LOOKUP_FOLLOW
;
4028 error
= user_path_at(olddfd
, oldname
, how
, &old_path
);
4032 new_dentry
= user_path_create(newdfd
, newname
, &new_path
,
4033 (how
& LOOKUP_REVAL
));
4034 error
= PTR_ERR(new_dentry
);
4035 if (IS_ERR(new_dentry
))
4039 if (old_path
.mnt
!= new_path
.mnt
)
4041 error
= may_linkat(&old_path
);
4042 if (unlikely(error
))
4044 error
= security_path_link(old_path
.dentry
, &new_path
, new_dentry
);
4047 error
= vfs_link(old_path
.dentry
, new_path
.dentry
->d_inode
, new_dentry
, &delegated_inode
);
4049 done_path_create(&new_path
, new_dentry
);
4050 if (delegated_inode
) {
4051 error
= break_deleg_wait(&delegated_inode
);
4053 path_put(&old_path
);
4057 if (retry_estale(error
, how
)) {
4058 path_put(&old_path
);
4059 how
|= LOOKUP_REVAL
;
4063 path_put(&old_path
);
4068 SYSCALL_DEFINE2(link
, const char __user
*, oldname
, const char __user
*, newname
)
4070 return sys_linkat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
, 0);
4074 * vfs_rename - rename a filesystem object
4075 * @old_dir: parent of source
4076 * @old_dentry: source
4077 * @new_dir: parent of destination
4078 * @new_dentry: destination
4079 * @delegated_inode: returns an inode needing a delegation break
4080 * @flags: rename flags
4082 * The caller must hold multiple mutexes--see lock_rename()).
4084 * If vfs_rename discovers a delegation in need of breaking at either
4085 * the source or destination, it will return -EWOULDBLOCK and return a
4086 * reference to the inode in delegated_inode. The caller should then
4087 * break the delegation and retry. Because breaking a delegation may
4088 * take a long time, the caller should drop all locks before doing
4091 * Alternatively, a caller may pass NULL for delegated_inode. This may
4092 * be appropriate for callers that expect the underlying filesystem not
4093 * to be NFS exported.
4095 * The worst of all namespace operations - renaming directory. "Perverted"
4096 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4098 * a) we can get into loop creation.
4099 * b) race potential - two innocent renames can create a loop together.
4100 * That's where 4.4 screws up. Current fix: serialization on
4101 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
4103 * c) we have to lock _four_ objects - parents and victim (if it exists),
4104 * and source (if it is not a directory).
4105 * And that - after we got ->i_mutex on parents (until then we don't know
4106 * whether the target exists). Solution: try to be smart with locking
4107 * order for inodes. We rely on the fact that tree topology may change
4108 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
4109 * move will be locked. Thus we can rank directories by the tree
4110 * (ancestors first) and rank all non-directories after them.
4111 * That works since everybody except rename does "lock parent, lookup,
4112 * lock child" and rename is under ->s_vfs_rename_mutex.
4113 * HOWEVER, it relies on the assumption that any object with ->lookup()
4114 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4115 * we'd better make sure that there's no link(2) for them.
4116 * d) conversion from fhandle to dentry may come in the wrong moment - when
4117 * we are removing the target. Solution: we will have to grab ->i_mutex
4118 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4119 * ->i_mutex on parents, which works but leads to some truly excessive
4122 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
4123 struct inode
*new_dir
, struct dentry
*new_dentry
,
4124 struct inode
**delegated_inode
, unsigned int flags
)
4127 bool is_dir
= d_is_dir(old_dentry
);
4128 const unsigned char *old_name
;
4129 struct inode
*source
= old_dentry
->d_inode
;
4130 struct inode
*target
= new_dentry
->d_inode
;
4131 bool new_is_dir
= false;
4132 unsigned max_links
= new_dir
->i_sb
->s_max_links
;
4134 if (source
== target
)
4137 error
= may_delete(old_dir
, old_dentry
, is_dir
);
4142 error
= may_create(new_dir
, new_dentry
);
4144 new_is_dir
= d_is_dir(new_dentry
);
4146 if (!(flags
& RENAME_EXCHANGE
))
4147 error
= may_delete(new_dir
, new_dentry
, is_dir
);
4149 error
= may_delete(new_dir
, new_dentry
, new_is_dir
);
4154 if (!old_dir
->i_op
->rename
&& !old_dir
->i_op
->rename2
)
4157 if (flags
&& !old_dir
->i_op
->rename2
)
4161 * If we are going to change the parent - check write permissions,
4162 * we'll need to flip '..'.
4164 if (new_dir
!= old_dir
) {
4166 error
= inode_permission(source
, MAY_WRITE
);
4170 if ((flags
& RENAME_EXCHANGE
) && new_is_dir
) {
4171 error
= inode_permission(target
, MAY_WRITE
);
4177 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
,
4182 old_name
= fsnotify_oldname_init(old_dentry
->d_name
.name
);
4184 if (!is_dir
|| (flags
& RENAME_EXCHANGE
))
4185 lock_two_nondirectories(source
, target
);
4187 mutex_lock(&target
->i_mutex
);
4190 if (is_local_mountpoint(old_dentry
) || is_local_mountpoint(new_dentry
))
4193 if (max_links
&& new_dir
!= old_dir
) {
4195 if (is_dir
&& !new_is_dir
&& new_dir
->i_nlink
>= max_links
)
4197 if ((flags
& RENAME_EXCHANGE
) && !is_dir
&& new_is_dir
&&
4198 old_dir
->i_nlink
>= max_links
)
4201 if (is_dir
&& !(flags
& RENAME_EXCHANGE
) && target
)
4202 shrink_dcache_parent(new_dentry
);
4204 error
= try_break_deleg(source
, delegated_inode
);
4208 if (target
&& !new_is_dir
) {
4209 error
= try_break_deleg(target
, delegated_inode
);
4213 if (!old_dir
->i_op
->rename2
) {
4214 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
,
4215 new_dir
, new_dentry
);
4217 WARN_ON(old_dir
->i_op
->rename
!= NULL
);
4218 error
= old_dir
->i_op
->rename2(old_dir
, old_dentry
,
4219 new_dir
, new_dentry
, flags
);
4224 if (!(flags
& RENAME_EXCHANGE
) && target
) {
4226 target
->i_flags
|= S_DEAD
;
4227 dont_mount(new_dentry
);
4228 detach_mounts(new_dentry
);
4230 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
)) {
4231 if (!(flags
& RENAME_EXCHANGE
))
4232 d_move(old_dentry
, new_dentry
);
4234 d_exchange(old_dentry
, new_dentry
);
4237 if (!is_dir
|| (flags
& RENAME_EXCHANGE
))
4238 unlock_two_nondirectories(source
, target
);
4240 mutex_unlock(&target
->i_mutex
);
4243 fsnotify_move(old_dir
, new_dir
, old_name
, is_dir
,
4244 !(flags
& RENAME_EXCHANGE
) ? target
: NULL
, old_dentry
);
4245 if (flags
& RENAME_EXCHANGE
) {
4246 fsnotify_move(new_dir
, old_dir
, old_dentry
->d_name
.name
,
4247 new_is_dir
, NULL
, new_dentry
);
4250 fsnotify_oldname_free(old_name
);
4254 EXPORT_SYMBOL(vfs_rename
);
4256 SYSCALL_DEFINE5(renameat2
, int, olddfd
, const char __user
*, oldname
,
4257 int, newdfd
, const char __user
*, newname
, unsigned int, flags
)
4259 struct dentry
*old_dentry
, *new_dentry
;
4260 struct dentry
*trap
;
4261 struct path old_path
, new_path
;
4262 struct qstr old_last
, new_last
;
4263 int old_type
, new_type
;
4264 struct inode
*delegated_inode
= NULL
;
4265 struct filename
*from
;
4266 struct filename
*to
;
4267 unsigned int lookup_flags
= 0, target_flags
= LOOKUP_RENAME_TARGET
;
4268 bool should_retry
= false;
4271 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
4274 if ((flags
& (RENAME_NOREPLACE
| RENAME_WHITEOUT
)) &&
4275 (flags
& RENAME_EXCHANGE
))
4278 if ((flags
& RENAME_WHITEOUT
) && !capable(CAP_MKNOD
))
4281 if (flags
& RENAME_EXCHANGE
)
4285 from
= user_path_parent(olddfd
, oldname
,
4286 &old_path
, &old_last
, &old_type
, lookup_flags
);
4288 error
= PTR_ERR(from
);
4292 to
= user_path_parent(newdfd
, newname
,
4293 &new_path
, &new_last
, &new_type
, lookup_flags
);
4295 error
= PTR_ERR(to
);
4300 if (old_path
.mnt
!= new_path
.mnt
)
4304 if (old_type
!= LAST_NORM
)
4307 if (flags
& RENAME_NOREPLACE
)
4309 if (new_type
!= LAST_NORM
)
4312 error
= mnt_want_write(old_path
.mnt
);
4317 trap
= lock_rename(new_path
.dentry
, old_path
.dentry
);
4319 old_dentry
= __lookup_hash(&old_last
, old_path
.dentry
, lookup_flags
);
4320 error
= PTR_ERR(old_dentry
);
4321 if (IS_ERR(old_dentry
))
4323 /* source must exist */
4325 if (d_is_negative(old_dentry
))
4327 new_dentry
= __lookup_hash(&new_last
, new_path
.dentry
, lookup_flags
| target_flags
);
4328 error
= PTR_ERR(new_dentry
);
4329 if (IS_ERR(new_dentry
))
4332 if ((flags
& RENAME_NOREPLACE
) && d_is_positive(new_dentry
))
4334 if (flags
& RENAME_EXCHANGE
) {
4336 if (d_is_negative(new_dentry
))
4339 if (!d_is_dir(new_dentry
)) {
4341 if (new_last
.name
[new_last
.len
])
4345 /* unless the source is a directory trailing slashes give -ENOTDIR */
4346 if (!d_is_dir(old_dentry
)) {
4348 if (old_last
.name
[old_last
.len
])
4350 if (!(flags
& RENAME_EXCHANGE
) && new_last
.name
[new_last
.len
])
4353 /* source should not be ancestor of target */
4355 if (old_dentry
== trap
)
4357 /* target should not be an ancestor of source */
4358 if (!(flags
& RENAME_EXCHANGE
))
4360 if (new_dentry
== trap
)
4363 error
= security_path_rename(&old_path
, old_dentry
,
4364 &new_path
, new_dentry
, flags
);
4367 error
= vfs_rename(old_path
.dentry
->d_inode
, old_dentry
,
4368 new_path
.dentry
->d_inode
, new_dentry
,
4369 &delegated_inode
, flags
);
4375 unlock_rename(new_path
.dentry
, old_path
.dentry
);
4376 if (delegated_inode
) {
4377 error
= break_deleg_wait(&delegated_inode
);
4381 mnt_drop_write(old_path
.mnt
);
4383 if (retry_estale(error
, lookup_flags
))
4384 should_retry
= true;
4385 path_put(&new_path
);
4388 path_put(&old_path
);
4391 should_retry
= false;
4392 lookup_flags
|= LOOKUP_REVAL
;
4399 SYSCALL_DEFINE4(renameat
, int, olddfd
, const char __user
*, oldname
,
4400 int, newdfd
, const char __user
*, newname
)
4402 return sys_renameat2(olddfd
, oldname
, newdfd
, newname
, 0);
4405 SYSCALL_DEFINE2(rename
, const char __user
*, oldname
, const char __user
*, newname
)
4407 return sys_renameat2(AT_FDCWD
, oldname
, AT_FDCWD
, newname
, 0);
4410 int vfs_whiteout(struct inode
*dir
, struct dentry
*dentry
)
4412 int error
= may_create(dir
, dentry
);
4416 if (!dir
->i_op
->mknod
)
4419 return dir
->i_op
->mknod(dir
, dentry
,
4420 S_IFCHR
| WHITEOUT_MODE
, WHITEOUT_DEV
);
4422 EXPORT_SYMBOL(vfs_whiteout
);
4424 int readlink_copy(char __user
*buffer
, int buflen
, const char *link
)
4426 int len
= PTR_ERR(link
);
4431 if (len
> (unsigned) buflen
)
4433 if (copy_to_user(buffer
, link
, len
))
4438 EXPORT_SYMBOL(readlink_copy
);
4441 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4442 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4443 * using) it for any given inode is up to filesystem.
4445 int generic_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
4448 const char *link
= dentry
->d_inode
->i_link
;
4452 link
= dentry
->d_inode
->i_op
->follow_link(dentry
, &cookie
, NULL
);
4454 return PTR_ERR(link
);
4456 res
= readlink_copy(buffer
, buflen
, link
);
4457 if (cookie
&& dentry
->d_inode
->i_op
->put_link
)
4458 dentry
->d_inode
->i_op
->put_link(dentry
, cookie
);
4461 EXPORT_SYMBOL(generic_readlink
);
4463 /* get the link contents into pagecache */
4464 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
4468 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
4469 page
= read_mapping_page(mapping
, 0, NULL
);
4474 nd_terminate_link(kaddr
, dentry
->d_inode
->i_size
, PAGE_SIZE
- 1);
4478 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
4480 struct page
*page
= NULL
;
4481 int res
= readlink_copy(buffer
, buflen
, page_getlink(dentry
, &page
));
4484 page_cache_release(page
);
4488 EXPORT_SYMBOL(page_readlink
);
4490 const char *page_follow_link_light(struct dentry
*dentry
, void **cookie
, struct nameidata
*nd
)
4492 struct page
*page
= NULL
;
4493 char *res
= page_getlink(dentry
, &page
);
4498 EXPORT_SYMBOL(page_follow_link_light
);
4500 void page_put_link(struct dentry
*dentry
, void *cookie
)
4502 struct page
*page
= cookie
;
4504 page_cache_release(page
);
4506 EXPORT_SYMBOL(page_put_link
);
4509 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4511 int __page_symlink(struct inode
*inode
, const char *symname
, int len
, int nofs
)
4513 struct address_space
*mapping
= inode
->i_mapping
;
4518 unsigned int flags
= AOP_FLAG_UNINTERRUPTIBLE
;
4520 flags
|= AOP_FLAG_NOFS
;
4523 err
= pagecache_write_begin(NULL
, mapping
, 0, len
-1,
4524 flags
, &page
, &fsdata
);
4528 kaddr
= kmap_atomic(page
);
4529 memcpy(kaddr
, symname
, len
-1);
4530 kunmap_atomic(kaddr
);
4532 err
= pagecache_write_end(NULL
, mapping
, 0, len
-1, len
-1,
4539 mark_inode_dirty(inode
);
4544 EXPORT_SYMBOL(__page_symlink
);
4546 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
4548 return __page_symlink(inode
, symname
, len
,
4549 !(mapping_gfp_mask(inode
->i_mapping
) & __GFP_FS
));
4551 EXPORT_SYMBOL(page_symlink
);
4553 const struct inode_operations page_symlink_inode_operations
= {
4554 .readlink
= generic_readlink
,
4555 .follow_link
= page_follow_link_light
,
4556 .put_link
= page_put_link
,
4558 EXPORT_SYMBOL(page_symlink_inode_operations
);