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.
121 void final_putname(struct filename
*name
)
123 if (name
->separate
) {
124 __putname(name
->name
);
131 #define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename))
134 getname_flags(const char __user
*filename
, int flags
, int *empty
)
136 struct filename
*result
, *err
;
141 result
= audit_reusename(filename
);
145 result
= __getname();
146 if (unlikely(!result
))
147 return ERR_PTR(-ENOMEM
);
150 * First, try to embed the struct filename inside the names_cache
153 kname
= (char *)result
+ sizeof(*result
);
154 result
->name
= kname
;
155 result
->separate
= false;
156 max
= EMBEDDED_NAME_MAX
;
159 len
= strncpy_from_user(kname
, filename
, max
);
160 if (unlikely(len
< 0)) {
166 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
167 * separate struct filename so we can dedicate the entire
168 * names_cache allocation for the pathname, and re-do the copy from
171 if (len
== EMBEDDED_NAME_MAX
&& max
== EMBEDDED_NAME_MAX
) {
172 kname
= (char *)result
;
174 result
= kzalloc(sizeof(*result
), GFP_KERNEL
);
176 err
= ERR_PTR(-ENOMEM
);
177 result
= (struct filename
*)kname
;
180 result
->name
= kname
;
181 result
->separate
= true;
186 /* The empty path is special. */
187 if (unlikely(!len
)) {
190 err
= ERR_PTR(-ENOENT
);
191 if (!(flags
& LOOKUP_EMPTY
))
195 err
= ERR_PTR(-ENAMETOOLONG
);
196 if (unlikely(len
>= PATH_MAX
))
199 result
->uptr
= filename
;
200 result
->aname
= NULL
;
201 audit_getname(result
);
205 final_putname(result
);
210 getname(const char __user
* filename
)
212 return getname_flags(filename
, 0, NULL
);
216 getname_kernel(const char * filename
)
218 struct filename
*result
;
219 int len
= strlen(filename
) + 1;
221 result
= __getname();
222 if (unlikely(!result
))
223 return ERR_PTR(-ENOMEM
);
225 if (len
<= EMBEDDED_NAME_MAX
) {
226 result
->name
= (char *)(result
) + sizeof(*result
);
227 result
->separate
= false;
228 } else if (len
<= PATH_MAX
) {
229 struct filename
*tmp
;
231 tmp
= kmalloc(sizeof(*tmp
), GFP_KERNEL
);
232 if (unlikely(!tmp
)) {
234 return ERR_PTR(-ENOMEM
);
236 tmp
->name
= (char *)result
;
237 tmp
->separate
= true;
241 return ERR_PTR(-ENAMETOOLONG
);
243 memcpy((char *)result
->name
, filename
, len
);
245 result
->aname
= NULL
;
246 audit_getname(result
);
251 #ifdef CONFIG_AUDITSYSCALL
252 void putname(struct filename
*name
)
254 if (unlikely(!audit_dummy_context()))
255 return audit_putname(name
);
260 static int check_acl(struct inode
*inode
, int mask
)
262 #ifdef CONFIG_FS_POSIX_ACL
263 struct posix_acl
*acl
;
265 if (mask
& MAY_NOT_BLOCK
) {
266 acl
= get_cached_acl_rcu(inode
, ACL_TYPE_ACCESS
);
269 /* no ->get_acl() calls in RCU mode... */
270 if (acl
== ACL_NOT_CACHED
)
272 return posix_acl_permission(inode
, acl
, mask
& ~MAY_NOT_BLOCK
);
275 acl
= get_acl(inode
, ACL_TYPE_ACCESS
);
279 int error
= posix_acl_permission(inode
, acl
, mask
);
280 posix_acl_release(acl
);
289 * This does the basic permission checking
291 static int acl_permission_check(struct inode
*inode
, int mask
)
293 unsigned int mode
= inode
->i_mode
;
295 if (likely(uid_eq(current_fsuid(), inode
->i_uid
)))
298 if (IS_POSIXACL(inode
) && (mode
& S_IRWXG
)) {
299 int error
= check_acl(inode
, mask
);
300 if (error
!= -EAGAIN
)
304 if (in_group_p(inode
->i_gid
))
309 * If the DACs are ok we don't need any capability check.
311 if ((mask
& ~mode
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
)) == 0)
317 * generic_permission - check for access rights on a Posix-like filesystem
318 * @inode: inode to check access rights for
319 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
321 * Used to check for read/write/execute permissions on a file.
322 * We use "fsuid" for this, letting us set arbitrary permissions
323 * for filesystem access without changing the "normal" uids which
324 * are used for other things.
326 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
327 * request cannot be satisfied (eg. requires blocking or too much complexity).
328 * It would then be called again in ref-walk mode.
330 int generic_permission(struct inode
*inode
, int mask
)
335 * Do the basic permission checks.
337 ret
= acl_permission_check(inode
, mask
);
341 if (S_ISDIR(inode
->i_mode
)) {
342 /* DACs are overridable for directories */
343 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_OVERRIDE
))
345 if (!(mask
& MAY_WRITE
))
346 if (capable_wrt_inode_uidgid(inode
,
347 CAP_DAC_READ_SEARCH
))
352 * Read/write DACs are always overridable.
353 * Executable DACs are overridable when there is
354 * at least one exec bit set.
356 if (!(mask
& MAY_EXEC
) || (inode
->i_mode
& S_IXUGO
))
357 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_OVERRIDE
))
361 * Searching includes executable on directories, else just read.
363 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
364 if (mask
== MAY_READ
)
365 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_READ_SEARCH
))
370 EXPORT_SYMBOL(generic_permission
);
373 * We _really_ want to just do "generic_permission()" without
374 * even looking at the inode->i_op values. So we keep a cache
375 * flag in inode->i_opflags, that says "this has not special
376 * permission function, use the fast case".
378 static inline int do_inode_permission(struct inode
*inode
, int mask
)
380 if (unlikely(!(inode
->i_opflags
& IOP_FASTPERM
))) {
381 if (likely(inode
->i_op
->permission
))
382 return inode
->i_op
->permission(inode
, mask
);
384 /* This gets set once for the inode lifetime */
385 spin_lock(&inode
->i_lock
);
386 inode
->i_opflags
|= IOP_FASTPERM
;
387 spin_unlock(&inode
->i_lock
);
389 return generic_permission(inode
, mask
);
393 * __inode_permission - Check for access rights to a given inode
394 * @inode: Inode to check permission on
395 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
397 * Check for read/write/execute permissions on an inode.
399 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
401 * This does not check for a read-only file system. You probably want
402 * inode_permission().
404 int __inode_permission(struct inode
*inode
, int mask
)
408 if (unlikely(mask
& MAY_WRITE
)) {
410 * Nobody gets write access to an immutable file.
412 if (IS_IMMUTABLE(inode
))
416 retval
= do_inode_permission(inode
, mask
);
420 retval
= devcgroup_inode_permission(inode
, mask
);
424 return security_inode_permission(inode
, mask
);
426 EXPORT_SYMBOL(__inode_permission
);
429 * sb_permission - Check superblock-level permissions
430 * @sb: Superblock of inode to check permission on
431 * @inode: Inode to check permission on
432 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
434 * Separate out file-system wide checks from inode-specific permission checks.
436 static int sb_permission(struct super_block
*sb
, struct inode
*inode
, int mask
)
438 if (unlikely(mask
& MAY_WRITE
)) {
439 umode_t mode
= inode
->i_mode
;
441 /* Nobody gets write access to a read-only fs. */
442 if ((sb
->s_flags
& MS_RDONLY
) &&
443 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
450 * inode_permission - Check for access rights to a given inode
451 * @inode: Inode to check permission on
452 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
454 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
455 * this, letting us set arbitrary permissions for filesystem access without
456 * changing the "normal" UIDs which are used for other things.
458 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
460 int inode_permission(struct inode
*inode
, int mask
)
464 retval
= sb_permission(inode
->i_sb
, inode
, mask
);
467 return __inode_permission(inode
, mask
);
469 EXPORT_SYMBOL(inode_permission
);
472 * path_get - get a reference to a path
473 * @path: path to get the reference to
475 * Given a path increment the reference count to the dentry and the vfsmount.
477 void path_get(const struct path
*path
)
482 EXPORT_SYMBOL(path_get
);
485 * path_put - put a reference to a path
486 * @path: path to put the reference to
488 * Given a path decrement the reference count to the dentry and the vfsmount.
490 void path_put(const struct path
*path
)
495 EXPORT_SYMBOL(path_put
);
501 struct inode
*inode
; /* path.dentry.d_inode */
507 char *saved_names
[MAX_NESTED_LINKS
+ 1];
511 * Path walking has 2 modes, rcu-walk and ref-walk (see
512 * Documentation/filesystems/path-lookup.txt). In situations when we can't
513 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
514 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
515 * mode. Refcounts are grabbed at the last known good point before rcu-walk
516 * got stuck, so ref-walk may continue from there. If this is not successful
517 * (eg. a seqcount has changed), then failure is returned and it's up to caller
518 * to restart the path walk from the beginning in ref-walk mode.
522 * unlazy_walk - try to switch to ref-walk mode.
523 * @nd: nameidata pathwalk data
524 * @dentry: child of nd->path.dentry or NULL
525 * Returns: 0 on success, -ECHILD on failure
527 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
528 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
529 * @nd or NULL. Must be called from rcu-walk context.
531 static int unlazy_walk(struct nameidata
*nd
, struct dentry
*dentry
)
533 struct fs_struct
*fs
= current
->fs
;
534 struct dentry
*parent
= nd
->path
.dentry
;
536 BUG_ON(!(nd
->flags
& LOOKUP_RCU
));
539 * After legitimizing the bastards, terminate_walk()
540 * will do the right thing for non-RCU mode, and all our
541 * subsequent exit cases should rcu_read_unlock()
542 * before returning. Do vfsmount first; if dentry
543 * can't be legitimized, just set nd->path.dentry to NULL
544 * and rely on dput(NULL) being a no-op.
546 if (!legitimize_mnt(nd
->path
.mnt
, nd
->m_seq
))
548 nd
->flags
&= ~LOOKUP_RCU
;
550 if (!lockref_get_not_dead(&parent
->d_lockref
)) {
551 nd
->path
.dentry
= NULL
;
556 * For a negative lookup, the lookup sequence point is the parents
557 * sequence point, and it only needs to revalidate the parent dentry.
559 * For a positive lookup, we need to move both the parent and the
560 * dentry from the RCU domain to be properly refcounted. And the
561 * sequence number in the dentry validates *both* dentry counters,
562 * since we checked the sequence number of the parent after we got
563 * the child sequence number. So we know the parent must still
564 * be valid if the child sequence number is still valid.
567 if (read_seqcount_retry(&parent
->d_seq
, nd
->seq
))
569 BUG_ON(nd
->inode
!= parent
->d_inode
);
571 if (!lockref_get_not_dead(&dentry
->d_lockref
))
573 if (read_seqcount_retry(&dentry
->d_seq
, nd
->seq
))
578 * Sequence counts matched. Now make sure that the root is
579 * still valid and get it if required.
581 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
582 spin_lock(&fs
->lock
);
583 if (nd
->root
.mnt
!= fs
->root
.mnt
|| nd
->root
.dentry
!= fs
->root
.dentry
)
584 goto unlock_and_drop_dentry
;
586 spin_unlock(&fs
->lock
);
592 unlock_and_drop_dentry
:
593 spin_unlock(&fs
->lock
);
601 if (!(nd
->flags
& LOOKUP_ROOT
))
606 static inline int d_revalidate(struct dentry
*dentry
, unsigned int flags
)
608 return dentry
->d_op
->d_revalidate(dentry
, flags
);
612 * complete_walk - successful completion of path walk
613 * @nd: pointer nameidata
615 * If we had been in RCU mode, drop out of it and legitimize nd->path.
616 * Revalidate the final result, unless we'd already done that during
617 * the path walk or the filesystem doesn't ask for it. Return 0 on
618 * success, -error on failure. In case of failure caller does not
619 * need to drop nd->path.
621 static int complete_walk(struct nameidata
*nd
)
623 struct dentry
*dentry
= nd
->path
.dentry
;
626 if (nd
->flags
& LOOKUP_RCU
) {
627 nd
->flags
&= ~LOOKUP_RCU
;
628 if (!(nd
->flags
& LOOKUP_ROOT
))
631 if (!legitimize_mnt(nd
->path
.mnt
, nd
->m_seq
)) {
635 if (unlikely(!lockref_get_not_dead(&dentry
->d_lockref
))) {
637 mntput(nd
->path
.mnt
);
640 if (read_seqcount_retry(&dentry
->d_seq
, nd
->seq
)) {
643 mntput(nd
->path
.mnt
);
649 if (likely(!(nd
->flags
& LOOKUP_JUMPED
)))
652 if (likely(!(dentry
->d_flags
& DCACHE_OP_WEAK_REVALIDATE
)))
655 status
= dentry
->d_op
->d_weak_revalidate(dentry
, nd
->flags
);
666 static __always_inline
void set_root(struct nameidata
*nd
)
668 get_fs_root(current
->fs
, &nd
->root
);
671 static int link_path_walk(const char *, struct nameidata
*);
673 static __always_inline
unsigned set_root_rcu(struct nameidata
*nd
)
675 struct fs_struct
*fs
= current
->fs
;
679 seq
= read_seqcount_begin(&fs
->seq
);
681 res
= __read_seqcount_begin(&nd
->root
.dentry
->d_seq
);
682 } while (read_seqcount_retry(&fs
->seq
, seq
));
686 static void path_put_conditional(struct path
*path
, struct nameidata
*nd
)
689 if (path
->mnt
!= nd
->path
.mnt
)
693 static inline void path_to_nameidata(const struct path
*path
,
694 struct nameidata
*nd
)
696 if (!(nd
->flags
& LOOKUP_RCU
)) {
697 dput(nd
->path
.dentry
);
698 if (nd
->path
.mnt
!= path
->mnt
)
699 mntput(nd
->path
.mnt
);
701 nd
->path
.mnt
= path
->mnt
;
702 nd
->path
.dentry
= path
->dentry
;
706 * Helper to directly jump to a known parsed path from ->follow_link,
707 * caller must have taken a reference to path beforehand.
709 void nd_jump_link(struct nameidata
*nd
, struct path
*path
)
714 nd
->inode
= nd
->path
.dentry
->d_inode
;
715 nd
->flags
|= LOOKUP_JUMPED
;
718 void nd_set_link(struct nameidata
*nd
, char *path
)
720 nd
->saved_names
[nd
->depth
] = path
;
722 EXPORT_SYMBOL(nd_set_link
);
724 char *nd_get_link(struct nameidata
*nd
)
726 return nd
->saved_names
[nd
->depth
];
728 EXPORT_SYMBOL(nd_get_link
);
730 static inline void put_link(struct nameidata
*nd
, struct path
*link
, void *cookie
)
732 struct inode
*inode
= link
->dentry
->d_inode
;
733 if (inode
->i_op
->put_link
)
734 inode
->i_op
->put_link(link
->dentry
, nd
, cookie
);
738 int sysctl_protected_symlinks __read_mostly
= 0;
739 int sysctl_protected_hardlinks __read_mostly
= 0;
742 * may_follow_link - Check symlink following for unsafe situations
743 * @link: The path of the symlink
744 * @nd: nameidata pathwalk data
746 * In the case of the sysctl_protected_symlinks sysctl being enabled,
747 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
748 * in a sticky world-writable directory. This is to protect privileged
749 * processes from failing races against path names that may change out
750 * from under them by way of other users creating malicious symlinks.
751 * It will permit symlinks to be followed only when outside a sticky
752 * world-writable directory, or when the uid of the symlink and follower
753 * match, or when the directory owner matches the symlink's owner.
755 * Returns 0 if following the symlink is allowed, -ve on error.
757 static inline int may_follow_link(struct path
*link
, struct nameidata
*nd
)
759 const struct inode
*inode
;
760 const struct inode
*parent
;
762 if (!sysctl_protected_symlinks
)
765 /* Allowed if owner and follower match. */
766 inode
= link
->dentry
->d_inode
;
767 if (uid_eq(current_cred()->fsuid
, inode
->i_uid
))
770 /* Allowed if parent directory not sticky and world-writable. */
771 parent
= nd
->path
.dentry
->d_inode
;
772 if ((parent
->i_mode
& (S_ISVTX
|S_IWOTH
)) != (S_ISVTX
|S_IWOTH
))
775 /* Allowed if parent directory and link owner match. */
776 if (uid_eq(parent
->i_uid
, inode
->i_uid
))
779 audit_log_link_denied("follow_link", link
);
780 path_put_conditional(link
, nd
);
786 * safe_hardlink_source - Check for safe hardlink conditions
787 * @inode: the source inode to hardlink from
789 * Return false if at least one of the following conditions:
790 * - inode is not a regular file
792 * - inode is setgid and group-exec
793 * - access failure for read and write
795 * Otherwise returns true.
797 static bool safe_hardlink_source(struct inode
*inode
)
799 umode_t mode
= inode
->i_mode
;
801 /* Special files should not get pinned to the filesystem. */
805 /* Setuid files should not get pinned to the filesystem. */
809 /* Executable setgid files should not get pinned to the filesystem. */
810 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
))
813 /* Hardlinking to unreadable or unwritable sources is dangerous. */
814 if (inode_permission(inode
, MAY_READ
| MAY_WRITE
))
821 * may_linkat - Check permissions for creating a hardlink
822 * @link: the source to hardlink from
824 * Block hardlink when all of:
825 * - sysctl_protected_hardlinks enabled
826 * - fsuid does not match inode
827 * - hardlink source is unsafe (see safe_hardlink_source() above)
830 * Returns 0 if successful, -ve on error.
832 static int may_linkat(struct path
*link
)
834 const struct cred
*cred
;
837 if (!sysctl_protected_hardlinks
)
840 cred
= current_cred();
841 inode
= link
->dentry
->d_inode
;
843 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
844 * otherwise, it must be a safe source.
846 if (uid_eq(cred
->fsuid
, inode
->i_uid
) || safe_hardlink_source(inode
) ||
850 audit_log_link_denied("linkat", link
);
854 static __always_inline
int
855 follow_link(struct path
*link
, struct nameidata
*nd
, void **p
)
857 struct dentry
*dentry
= link
->dentry
;
861 BUG_ON(nd
->flags
& LOOKUP_RCU
);
863 if (link
->mnt
== nd
->path
.mnt
)
867 if (unlikely(current
->total_link_count
>= 40))
868 goto out_put_nd_path
;
871 current
->total_link_count
++;
874 nd_set_link(nd
, NULL
);
876 error
= security_inode_follow_link(link
->dentry
, nd
);
878 goto out_put_nd_path
;
880 nd
->last_type
= LAST_BIND
;
881 *p
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
884 goto out_put_nd_path
;
889 if (unlikely(IS_ERR(s
))) {
891 put_link(nd
, link
, *p
);
900 nd
->flags
|= LOOKUP_JUMPED
;
902 nd
->inode
= nd
->path
.dentry
->d_inode
;
903 error
= link_path_walk(s
, nd
);
905 put_link(nd
, link
, *p
);
917 static int follow_up_rcu(struct path
*path
)
919 struct mount
*mnt
= real_mount(path
->mnt
);
920 struct mount
*parent
;
921 struct dentry
*mountpoint
;
923 parent
= mnt
->mnt_parent
;
924 if (&parent
->mnt
== path
->mnt
)
926 mountpoint
= mnt
->mnt_mountpoint
;
927 path
->dentry
= mountpoint
;
928 path
->mnt
= &parent
->mnt
;
933 * follow_up - Find the mountpoint of path's vfsmount
935 * Given a path, find the mountpoint of its source file system.
936 * Replace @path with the path of the mountpoint in the parent mount.
939 * Return 1 if we went up a level and 0 if we were already at the
942 int follow_up(struct path
*path
)
944 struct mount
*mnt
= real_mount(path
->mnt
);
945 struct mount
*parent
;
946 struct dentry
*mountpoint
;
948 read_seqlock_excl(&mount_lock
);
949 parent
= mnt
->mnt_parent
;
951 read_sequnlock_excl(&mount_lock
);
954 mntget(&parent
->mnt
);
955 mountpoint
= dget(mnt
->mnt_mountpoint
);
956 read_sequnlock_excl(&mount_lock
);
958 path
->dentry
= mountpoint
;
960 path
->mnt
= &parent
->mnt
;
963 EXPORT_SYMBOL(follow_up
);
966 * Perform an automount
967 * - return -EISDIR to tell follow_managed() to stop and return the path we
970 static int follow_automount(struct path
*path
, unsigned flags
,
973 struct vfsmount
*mnt
;
976 if (!path
->dentry
->d_op
|| !path
->dentry
->d_op
->d_automount
)
979 /* We don't want to mount if someone's just doing a stat -
980 * unless they're stat'ing a directory and appended a '/' to
983 * We do, however, want to mount if someone wants to open or
984 * create a file of any type under the mountpoint, wants to
985 * traverse through the mountpoint or wants to open the
986 * mounted directory. Also, autofs may mark negative dentries
987 * as being automount points. These will need the attentions
988 * of the daemon to instantiate them before they can be used.
990 if (!(flags
& (LOOKUP_PARENT
| LOOKUP_DIRECTORY
|
991 LOOKUP_OPEN
| LOOKUP_CREATE
| LOOKUP_AUTOMOUNT
)) &&
992 path
->dentry
->d_inode
)
995 current
->total_link_count
++;
996 if (current
->total_link_count
>= 40)
999 mnt
= path
->dentry
->d_op
->d_automount(path
);
1002 * The filesystem is allowed to return -EISDIR here to indicate
1003 * it doesn't want to automount. For instance, autofs would do
1004 * this so that its userspace daemon can mount on this dentry.
1006 * However, we can only permit this if it's a terminal point in
1007 * the path being looked up; if it wasn't then the remainder of
1008 * the path is inaccessible and we should say so.
1010 if (PTR_ERR(mnt
) == -EISDIR
&& (flags
& LOOKUP_PARENT
))
1012 return PTR_ERR(mnt
);
1015 if (!mnt
) /* mount collision */
1018 if (!*need_mntput
) {
1019 /* lock_mount() may release path->mnt on error */
1021 *need_mntput
= true;
1023 err
= finish_automount(mnt
, path
);
1027 /* Someone else made a mount here whilst we were busy */
1032 path
->dentry
= dget(mnt
->mnt_root
);
1041 * Handle a dentry that is managed in some way.
1042 * - Flagged for transit management (autofs)
1043 * - Flagged as mountpoint
1044 * - Flagged as automount point
1046 * This may only be called in refwalk mode.
1048 * Serialization is taken care of in namespace.c
1050 static int follow_managed(struct path
*path
, unsigned flags
)
1052 struct vfsmount
*mnt
= path
->mnt
; /* held by caller, must be left alone */
1054 bool need_mntput
= false;
1057 /* Given that we're not holding a lock here, we retain the value in a
1058 * local variable for each dentry as we look at it so that we don't see
1059 * the components of that value change under us */
1060 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
1061 managed
&= DCACHE_MANAGED_DENTRY
,
1062 unlikely(managed
!= 0)) {
1063 /* Allow the filesystem to manage the transit without i_mutex
1065 if (managed
& DCACHE_MANAGE_TRANSIT
) {
1066 BUG_ON(!path
->dentry
->d_op
);
1067 BUG_ON(!path
->dentry
->d_op
->d_manage
);
1068 ret
= path
->dentry
->d_op
->d_manage(path
->dentry
, false);
1073 /* Transit to a mounted filesystem. */
1074 if (managed
& DCACHE_MOUNTED
) {
1075 struct vfsmount
*mounted
= lookup_mnt(path
);
1080 path
->mnt
= mounted
;
1081 path
->dentry
= dget(mounted
->mnt_root
);
1086 /* Something is mounted on this dentry in another
1087 * namespace and/or whatever was mounted there in this
1088 * namespace got unmounted before lookup_mnt() could
1092 /* Handle an automount point */
1093 if (managed
& DCACHE_NEED_AUTOMOUNT
) {
1094 ret
= follow_automount(path
, flags
, &need_mntput
);
1100 /* We didn't change the current path point */
1104 if (need_mntput
&& path
->mnt
== mnt
)
1108 return ret
< 0 ? ret
: need_mntput
;
1111 int follow_down_one(struct path
*path
)
1113 struct vfsmount
*mounted
;
1115 mounted
= lookup_mnt(path
);
1119 path
->mnt
= mounted
;
1120 path
->dentry
= dget(mounted
->mnt_root
);
1125 EXPORT_SYMBOL(follow_down_one
);
1127 static inline int managed_dentry_rcu(struct dentry
*dentry
)
1129 return (dentry
->d_flags
& DCACHE_MANAGE_TRANSIT
) ?
1130 dentry
->d_op
->d_manage(dentry
, true) : 0;
1134 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1135 * we meet a managed dentry that would need blocking.
1137 static bool __follow_mount_rcu(struct nameidata
*nd
, struct path
*path
,
1138 struct inode
**inode
)
1141 struct mount
*mounted
;
1143 * Don't forget we might have a non-mountpoint managed dentry
1144 * that wants to block transit.
1146 switch (managed_dentry_rcu(path
->dentry
)) {
1156 if (!d_mountpoint(path
->dentry
))
1157 return !(path
->dentry
->d_flags
& DCACHE_NEED_AUTOMOUNT
);
1159 mounted
= __lookup_mnt(path
->mnt
, path
->dentry
);
1162 path
->mnt
= &mounted
->mnt
;
1163 path
->dentry
= mounted
->mnt
.mnt_root
;
1164 nd
->flags
|= LOOKUP_JUMPED
;
1165 nd
->seq
= read_seqcount_begin(&path
->dentry
->d_seq
);
1167 * Update the inode too. We don't need to re-check the
1168 * dentry sequence number here after this d_inode read,
1169 * because a mount-point is always pinned.
1171 *inode
= path
->dentry
->d_inode
;
1173 return !read_seqretry(&mount_lock
, nd
->m_seq
) &&
1174 !(path
->dentry
->d_flags
& DCACHE_NEED_AUTOMOUNT
);
1177 static int follow_dotdot_rcu(struct nameidata
*nd
)
1179 struct inode
*inode
= nd
->inode
;
1184 if (nd
->path
.dentry
== nd
->root
.dentry
&&
1185 nd
->path
.mnt
== nd
->root
.mnt
) {
1188 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
1189 struct dentry
*old
= nd
->path
.dentry
;
1190 struct dentry
*parent
= old
->d_parent
;
1193 inode
= parent
->d_inode
;
1194 seq
= read_seqcount_begin(&parent
->d_seq
);
1195 if (read_seqcount_retry(&old
->d_seq
, nd
->seq
))
1197 nd
->path
.dentry
= parent
;
1201 if (!follow_up_rcu(&nd
->path
))
1203 inode
= nd
->path
.dentry
->d_inode
;
1204 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1206 while (d_mountpoint(nd
->path
.dentry
)) {
1207 struct mount
*mounted
;
1208 mounted
= __lookup_mnt(nd
->path
.mnt
, nd
->path
.dentry
);
1211 nd
->path
.mnt
= &mounted
->mnt
;
1212 nd
->path
.dentry
= mounted
->mnt
.mnt_root
;
1213 inode
= nd
->path
.dentry
->d_inode
;
1214 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1215 if (read_seqretry(&mount_lock
, nd
->m_seq
))
1222 nd
->flags
&= ~LOOKUP_RCU
;
1223 if (!(nd
->flags
& LOOKUP_ROOT
))
1224 nd
->root
.mnt
= NULL
;
1230 * Follow down to the covering mount currently visible to userspace. At each
1231 * point, the filesystem owning that dentry may be queried as to whether the
1232 * caller is permitted to proceed or not.
1234 int follow_down(struct path
*path
)
1239 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
1240 unlikely(managed
& DCACHE_MANAGED_DENTRY
)) {
1241 /* Allow the filesystem to manage the transit without i_mutex
1244 * We indicate to the filesystem if someone is trying to mount
1245 * something here. This gives autofs the chance to deny anyone
1246 * other than its daemon the right to mount on its
1249 * The filesystem may sleep at this point.
1251 if (managed
& DCACHE_MANAGE_TRANSIT
) {
1252 BUG_ON(!path
->dentry
->d_op
);
1253 BUG_ON(!path
->dentry
->d_op
->d_manage
);
1254 ret
= path
->dentry
->d_op
->d_manage(
1255 path
->dentry
, false);
1257 return ret
== -EISDIR
? 0 : ret
;
1260 /* Transit to a mounted filesystem. */
1261 if (managed
& DCACHE_MOUNTED
) {
1262 struct vfsmount
*mounted
= lookup_mnt(path
);
1267 path
->mnt
= mounted
;
1268 path
->dentry
= dget(mounted
->mnt_root
);
1272 /* Don't handle automount points here */
1277 EXPORT_SYMBOL(follow_down
);
1280 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1282 static void follow_mount(struct path
*path
)
1284 while (d_mountpoint(path
->dentry
)) {
1285 struct vfsmount
*mounted
= lookup_mnt(path
);
1290 path
->mnt
= mounted
;
1291 path
->dentry
= dget(mounted
->mnt_root
);
1295 static void follow_dotdot(struct nameidata
*nd
)
1301 struct dentry
*old
= nd
->path
.dentry
;
1303 if (nd
->path
.dentry
== nd
->root
.dentry
&&
1304 nd
->path
.mnt
== nd
->root
.mnt
) {
1307 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
1308 /* rare case of legitimate dget_parent()... */
1309 nd
->path
.dentry
= dget_parent(nd
->path
.dentry
);
1313 if (!follow_up(&nd
->path
))
1316 follow_mount(&nd
->path
);
1317 nd
->inode
= nd
->path
.dentry
->d_inode
;
1321 * This looks up the name in dcache, possibly revalidates the old dentry and
1322 * allocates a new one if not found or not valid. In the need_lookup argument
1323 * returns whether i_op->lookup is necessary.
1325 * dir->d_inode->i_mutex must be held
1327 static struct dentry
*lookup_dcache(struct qstr
*name
, struct dentry
*dir
,
1328 unsigned int flags
, bool *need_lookup
)
1330 struct dentry
*dentry
;
1333 *need_lookup
= false;
1334 dentry
= d_lookup(dir
, name
);
1336 if (dentry
->d_flags
& DCACHE_OP_REVALIDATE
) {
1337 error
= d_revalidate(dentry
, flags
);
1338 if (unlikely(error
<= 0)) {
1341 return ERR_PTR(error
);
1343 d_invalidate(dentry
);
1352 dentry
= d_alloc(dir
, name
);
1353 if (unlikely(!dentry
))
1354 return ERR_PTR(-ENOMEM
);
1356 *need_lookup
= true;
1362 * Call i_op->lookup on the dentry. The dentry must be negative and
1365 * dir->d_inode->i_mutex must be held
1367 static struct dentry
*lookup_real(struct inode
*dir
, struct dentry
*dentry
,
1372 /* Don't create child dentry for a dead directory. */
1373 if (unlikely(IS_DEADDIR(dir
))) {
1375 return ERR_PTR(-ENOENT
);
1378 old
= dir
->i_op
->lookup(dir
, dentry
, flags
);
1379 if (unlikely(old
)) {
1386 static struct dentry
*__lookup_hash(struct qstr
*name
,
1387 struct dentry
*base
, unsigned int flags
)
1390 struct dentry
*dentry
;
1392 dentry
= lookup_dcache(name
, base
, flags
, &need_lookup
);
1396 return lookup_real(base
->d_inode
, dentry
, flags
);
1400 * It's more convoluted than I'd like it to be, but... it's still fairly
1401 * small and for now I'd prefer to have fast path as straight as possible.
1402 * It _is_ time-critical.
1404 static int lookup_fast(struct nameidata
*nd
,
1405 struct path
*path
, struct inode
**inode
)
1407 struct vfsmount
*mnt
= nd
->path
.mnt
;
1408 struct dentry
*dentry
, *parent
= nd
->path
.dentry
;
1414 * Rename seqlock is not required here because in the off chance
1415 * of a false negative due to a concurrent rename, we're going to
1416 * do the non-racy lookup, below.
1418 if (nd
->flags
& LOOKUP_RCU
) {
1420 dentry
= __d_lookup_rcu(parent
, &nd
->last
, &seq
);
1425 * This sequence count validates that the inode matches
1426 * the dentry name information from lookup.
1428 *inode
= dentry
->d_inode
;
1429 if (read_seqcount_retry(&dentry
->d_seq
, seq
))
1433 * This sequence count validates that the parent had no
1434 * changes while we did the lookup of the dentry above.
1436 * The memory barrier in read_seqcount_begin of child is
1437 * enough, we can use __read_seqcount_retry here.
1439 if (__read_seqcount_retry(&parent
->d_seq
, nd
->seq
))
1443 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
)) {
1444 status
= d_revalidate(dentry
, nd
->flags
);
1445 if (unlikely(status
<= 0)) {
1446 if (status
!= -ECHILD
)
1452 path
->dentry
= dentry
;
1453 if (likely(__follow_mount_rcu(nd
, path
, inode
)))
1456 if (unlazy_walk(nd
, dentry
))
1459 dentry
= __d_lookup(parent
, &nd
->last
);
1462 if (unlikely(!dentry
))
1465 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
) && need_reval
)
1466 status
= d_revalidate(dentry
, nd
->flags
);
1467 if (unlikely(status
<= 0)) {
1472 d_invalidate(dentry
);
1478 path
->dentry
= dentry
;
1479 err
= follow_managed(path
, nd
->flags
);
1480 if (unlikely(err
< 0)) {
1481 path_put_conditional(path
, nd
);
1485 nd
->flags
|= LOOKUP_JUMPED
;
1486 *inode
= path
->dentry
->d_inode
;
1493 /* Fast lookup failed, do it the slow way */
1494 static int lookup_slow(struct nameidata
*nd
, struct path
*path
)
1496 struct dentry
*dentry
, *parent
;
1499 parent
= nd
->path
.dentry
;
1500 BUG_ON(nd
->inode
!= parent
->d_inode
);
1502 mutex_lock(&parent
->d_inode
->i_mutex
);
1503 dentry
= __lookup_hash(&nd
->last
, parent
, nd
->flags
);
1504 mutex_unlock(&parent
->d_inode
->i_mutex
);
1506 return PTR_ERR(dentry
);
1507 path
->mnt
= nd
->path
.mnt
;
1508 path
->dentry
= dentry
;
1509 err
= follow_managed(path
, nd
->flags
);
1510 if (unlikely(err
< 0)) {
1511 path_put_conditional(path
, nd
);
1515 nd
->flags
|= LOOKUP_JUMPED
;
1519 static inline int may_lookup(struct nameidata
*nd
)
1521 if (nd
->flags
& LOOKUP_RCU
) {
1522 int err
= inode_permission(nd
->inode
, MAY_EXEC
|MAY_NOT_BLOCK
);
1525 if (unlazy_walk(nd
, NULL
))
1528 return inode_permission(nd
->inode
, MAY_EXEC
);
1531 static inline int handle_dots(struct nameidata
*nd
, int type
)
1533 if (type
== LAST_DOTDOT
) {
1534 if (nd
->flags
& LOOKUP_RCU
) {
1535 if (follow_dotdot_rcu(nd
))
1543 static void terminate_walk(struct nameidata
*nd
)
1545 if (!(nd
->flags
& LOOKUP_RCU
)) {
1546 path_put(&nd
->path
);
1548 nd
->flags
&= ~LOOKUP_RCU
;
1549 if (!(nd
->flags
& LOOKUP_ROOT
))
1550 nd
->root
.mnt
= NULL
;
1556 * Do we need to follow links? We _really_ want to be able
1557 * to do this check without having to look at inode->i_op,
1558 * so we keep a cache of "no, this doesn't need follow_link"
1559 * for the common case.
1561 static inline int should_follow_link(struct dentry
*dentry
, int follow
)
1563 return unlikely(d_is_symlink(dentry
)) ? follow
: 0;
1566 static inline int walk_component(struct nameidata
*nd
, struct path
*path
,
1569 struct inode
*inode
;
1572 * "." and ".." are special - ".." especially so because it has
1573 * to be able to know about the current root directory and
1574 * parent relationships.
1576 if (unlikely(nd
->last_type
!= LAST_NORM
))
1577 return handle_dots(nd
, nd
->last_type
);
1578 err
= lookup_fast(nd
, path
, &inode
);
1579 if (unlikely(err
)) {
1583 err
= lookup_slow(nd
, path
);
1587 inode
= path
->dentry
->d_inode
;
1590 if (!inode
|| d_is_negative(path
->dentry
))
1593 if (should_follow_link(path
->dentry
, follow
)) {
1594 if (nd
->flags
& LOOKUP_RCU
) {
1595 if (unlikely(unlazy_walk(nd
, path
->dentry
))) {
1600 BUG_ON(inode
!= path
->dentry
->d_inode
);
1603 path_to_nameidata(path
, nd
);
1608 path_to_nameidata(path
, nd
);
1615 * This limits recursive symlink follows to 8, while
1616 * limiting consecutive symlinks to 40.
1618 * Without that kind of total limit, nasty chains of consecutive
1619 * symlinks can cause almost arbitrarily long lookups.
1621 static inline int nested_symlink(struct path
*path
, struct nameidata
*nd
)
1625 if (unlikely(current
->link_count
>= MAX_NESTED_LINKS
)) {
1626 path_put_conditional(path
, nd
);
1627 path_put(&nd
->path
);
1630 BUG_ON(nd
->depth
>= MAX_NESTED_LINKS
);
1633 current
->link_count
++;
1636 struct path link
= *path
;
1639 res
= follow_link(&link
, nd
, &cookie
);
1642 res
= walk_component(nd
, path
, LOOKUP_FOLLOW
);
1643 put_link(nd
, &link
, cookie
);
1646 current
->link_count
--;
1652 * We can do the critical dentry name comparison and hashing
1653 * operations one word at a time, but we are limited to:
1655 * - Architectures with fast unaligned word accesses. We could
1656 * do a "get_unaligned()" if this helps and is sufficiently
1659 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1660 * do not trap on the (extremely unlikely) case of a page
1661 * crossing operation.
1663 * - Furthermore, we need an efficient 64-bit compile for the
1664 * 64-bit case in order to generate the "number of bytes in
1665 * the final mask". Again, that could be replaced with a
1666 * efficient population count instruction or similar.
1668 #ifdef CONFIG_DCACHE_WORD_ACCESS
1670 #include <asm/word-at-a-time.h>
1674 static inline unsigned int fold_hash(unsigned long hash
)
1676 return hash_64(hash
, 32);
1679 #else /* 32-bit case */
1681 #define fold_hash(x) (x)
1685 unsigned int full_name_hash(const unsigned char *name
, unsigned int len
)
1687 unsigned long a
, mask
;
1688 unsigned long hash
= 0;
1691 a
= load_unaligned_zeropad(name
);
1692 if (len
< sizeof(unsigned long))
1696 name
+= sizeof(unsigned long);
1697 len
-= sizeof(unsigned long);
1701 mask
= bytemask_from_count(len
);
1704 return fold_hash(hash
);
1706 EXPORT_SYMBOL(full_name_hash
);
1709 * Calculate the length and hash of the path component, and
1710 * return the "hash_len" as the result.
1712 static inline u64
hash_name(const char *name
)
1714 unsigned long a
, b
, adata
, bdata
, mask
, hash
, len
;
1715 const struct word_at_a_time constants
= WORD_AT_A_TIME_CONSTANTS
;
1718 len
= -sizeof(unsigned long);
1720 hash
= (hash
+ a
) * 9;
1721 len
+= sizeof(unsigned long);
1722 a
= load_unaligned_zeropad(name
+len
);
1723 b
= a
^ REPEAT_BYTE('/');
1724 } while (!(has_zero(a
, &adata
, &constants
) | has_zero(b
, &bdata
, &constants
)));
1726 adata
= prep_zero_mask(a
, adata
, &constants
);
1727 bdata
= prep_zero_mask(b
, bdata
, &constants
);
1729 mask
= create_zero_mask(adata
| bdata
);
1731 hash
+= a
& zero_bytemask(mask
);
1732 len
+= find_zero(mask
);
1733 return hashlen_create(fold_hash(hash
), len
);
1738 unsigned int full_name_hash(const unsigned char *name
, unsigned int len
)
1740 unsigned long hash
= init_name_hash();
1742 hash
= partial_name_hash(*name
++, hash
);
1743 return end_name_hash(hash
);
1745 EXPORT_SYMBOL(full_name_hash
);
1748 * We know there's a real path component here of at least
1751 static inline u64
hash_name(const char *name
)
1753 unsigned long hash
= init_name_hash();
1754 unsigned long len
= 0, c
;
1756 c
= (unsigned char)*name
;
1759 hash
= partial_name_hash(c
, hash
);
1760 c
= (unsigned char)name
[len
];
1761 } while (c
&& c
!= '/');
1762 return hashlen_create(end_name_hash(hash
), len
);
1769 * This is the basic name resolution function, turning a pathname into
1770 * the final dentry. We expect 'base' to be positive and a directory.
1772 * Returns 0 and nd will have valid dentry and mnt on success.
1773 * Returns error and drops reference to input namei data on failure.
1775 static int link_path_walk(const char *name
, struct nameidata
*nd
)
1785 /* At this point we know we have a real path component. */
1790 err
= may_lookup(nd
);
1794 hash_len
= hash_name(name
);
1797 if (name
[0] == '.') switch (hashlen_len(hash_len
)) {
1799 if (name
[1] == '.') {
1801 nd
->flags
|= LOOKUP_JUMPED
;
1807 if (likely(type
== LAST_NORM
)) {
1808 struct dentry
*parent
= nd
->path
.dentry
;
1809 nd
->flags
&= ~LOOKUP_JUMPED
;
1810 if (unlikely(parent
->d_flags
& DCACHE_OP_HASH
)) {
1811 struct qstr
this = { { .hash_len
= hash_len
}, .name
= name
};
1812 err
= parent
->d_op
->d_hash(parent
, &this);
1815 hash_len
= this.hash_len
;
1820 nd
->last
.hash_len
= hash_len
;
1821 nd
->last
.name
= name
;
1822 nd
->last_type
= type
;
1824 name
+= hashlen_len(hash_len
);
1828 * If it wasn't NUL, we know it was '/'. Skip that
1829 * slash, and continue until no more slashes.
1833 } while (unlikely(*name
== '/'));
1837 err
= walk_component(nd
, &next
, LOOKUP_FOLLOW
);
1842 err
= nested_symlink(&next
, nd
);
1846 if (!d_can_lookup(nd
->path
.dentry
)) {
1855 static int path_init(int dfd
, const char *name
, unsigned int flags
,
1856 struct nameidata
*nd
)
1860 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
1861 nd
->flags
= flags
| LOOKUP_JUMPED
| LOOKUP_PARENT
;
1864 if (flags
& LOOKUP_ROOT
) {
1865 struct dentry
*root
= nd
->root
.dentry
;
1866 struct inode
*inode
= root
->d_inode
;
1868 if (!d_can_lookup(root
))
1870 retval
= inode_permission(inode
, MAY_EXEC
);
1874 nd
->path
= nd
->root
;
1876 if (flags
& LOOKUP_RCU
) {
1878 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1879 nd
->m_seq
= read_seqbegin(&mount_lock
);
1881 path_get(&nd
->path
);
1886 nd
->root
.mnt
= NULL
;
1888 nd
->m_seq
= read_seqbegin(&mount_lock
);
1890 if (flags
& LOOKUP_RCU
) {
1892 nd
->seq
= set_root_rcu(nd
);
1895 path_get(&nd
->root
);
1897 nd
->path
= nd
->root
;
1898 } else if (dfd
== AT_FDCWD
) {
1899 if (flags
& LOOKUP_RCU
) {
1900 struct fs_struct
*fs
= current
->fs
;
1906 seq
= read_seqcount_begin(&fs
->seq
);
1908 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1909 } while (read_seqcount_retry(&fs
->seq
, seq
));
1911 get_fs_pwd(current
->fs
, &nd
->path
);
1914 /* Caller must check execute permissions on the starting path component */
1915 struct fd f
= fdget_raw(dfd
);
1916 struct dentry
*dentry
;
1921 dentry
= f
.file
->f_path
.dentry
;
1924 if (!d_can_lookup(dentry
)) {
1930 nd
->path
= f
.file
->f_path
;
1931 if (flags
& LOOKUP_RCU
) {
1932 if (f
.flags
& FDPUT_FPUT
)
1934 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1937 path_get(&nd
->path
);
1942 nd
->inode
= nd
->path
.dentry
->d_inode
;
1943 if (!(flags
& LOOKUP_RCU
))
1945 if (likely(!read_seqcount_retry(&nd
->path
.dentry
->d_seq
, nd
->seq
)))
1947 if (!(nd
->flags
& LOOKUP_ROOT
))
1948 nd
->root
.mnt
= NULL
;
1952 current
->total_link_count
= 0;
1953 return link_path_walk(name
, nd
);
1956 static void path_cleanup(struct nameidata
*nd
)
1958 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
1959 path_put(&nd
->root
);
1960 nd
->root
.mnt
= NULL
;
1962 if (unlikely(nd
->base
))
1966 static inline int lookup_last(struct nameidata
*nd
, struct path
*path
)
1968 if (nd
->last_type
== LAST_NORM
&& nd
->last
.name
[nd
->last
.len
])
1969 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
1971 nd
->flags
&= ~LOOKUP_PARENT
;
1972 return walk_component(nd
, path
, nd
->flags
& LOOKUP_FOLLOW
);
1975 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1976 static int path_lookupat(int dfd
, const char *name
,
1977 unsigned int flags
, struct nameidata
*nd
)
1983 * Path walking is largely split up into 2 different synchronisation
1984 * schemes, rcu-walk and ref-walk (explained in
1985 * Documentation/filesystems/path-lookup.txt). These share much of the
1986 * path walk code, but some things particularly setup, cleanup, and
1987 * following mounts are sufficiently divergent that functions are
1988 * duplicated. Typically there is a function foo(), and its RCU
1989 * analogue, foo_rcu().
1991 * -ECHILD is the error number of choice (just to avoid clashes) that
1992 * is returned if some aspect of an rcu-walk fails. Such an error must
1993 * be handled by restarting a traditional ref-walk (which will always
1994 * be able to complete).
1996 err
= path_init(dfd
, name
, flags
, nd
);
1997 if (!err
&& !(flags
& LOOKUP_PARENT
)) {
1998 err
= lookup_last(nd
, &path
);
2001 struct path link
= path
;
2002 err
= may_follow_link(&link
, nd
);
2005 nd
->flags
|= LOOKUP_PARENT
;
2006 err
= follow_link(&link
, nd
, &cookie
);
2009 err
= lookup_last(nd
, &path
);
2010 put_link(nd
, &link
, cookie
);
2015 err
= complete_walk(nd
);
2017 if (!err
&& nd
->flags
& LOOKUP_DIRECTORY
) {
2018 if (!d_can_lookup(nd
->path
.dentry
)) {
2019 path_put(&nd
->path
);
2028 static int filename_lookup(int dfd
, struct filename
*name
,
2029 unsigned int flags
, struct nameidata
*nd
)
2031 int retval
= path_lookupat(dfd
, name
->name
, flags
| LOOKUP_RCU
, nd
);
2032 if (unlikely(retval
== -ECHILD
))
2033 retval
= path_lookupat(dfd
, name
->name
, flags
, nd
);
2034 if (unlikely(retval
== -ESTALE
))
2035 retval
= path_lookupat(dfd
, name
->name
,
2036 flags
| LOOKUP_REVAL
, nd
);
2038 if (likely(!retval
))
2039 audit_inode(name
, nd
->path
.dentry
, flags
& LOOKUP_PARENT
);
2043 static int do_path_lookup(int dfd
, const char *name
,
2044 unsigned int flags
, struct nameidata
*nd
)
2046 struct filename
*filename
= getname_kernel(name
);
2047 int retval
= PTR_ERR(filename
);
2049 if (!IS_ERR(filename
)) {
2050 retval
= filename_lookup(dfd
, filename
, flags
, nd
);
2056 /* does lookup, returns the object with parent locked */
2057 struct dentry
*kern_path_locked(const char *name
, struct path
*path
)
2059 struct filename
*filename
= getname_kernel(name
);
2060 struct nameidata nd
;
2064 if (IS_ERR(filename
))
2065 return ERR_CAST(filename
);
2067 err
= filename_lookup(AT_FDCWD
, filename
, LOOKUP_PARENT
, &nd
);
2072 if (nd
.last_type
!= LAST_NORM
) {
2074 d
= ERR_PTR(-EINVAL
);
2077 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2078 d
= __lookup_hash(&nd
.last
, nd
.path
.dentry
, 0);
2080 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2090 int kern_path(const char *name
, unsigned int flags
, struct path
*path
)
2092 struct nameidata nd
;
2093 int res
= do_path_lookup(AT_FDCWD
, name
, flags
, &nd
);
2098 EXPORT_SYMBOL(kern_path
);
2101 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2102 * @dentry: pointer to dentry of the base directory
2103 * @mnt: pointer to vfs mount of the base directory
2104 * @name: pointer to file name
2105 * @flags: lookup flags
2106 * @path: pointer to struct path to fill
2108 int vfs_path_lookup(struct dentry
*dentry
, struct vfsmount
*mnt
,
2109 const char *name
, unsigned int flags
,
2112 struct nameidata nd
;
2114 nd
.root
.dentry
= dentry
;
2116 BUG_ON(flags
& LOOKUP_PARENT
);
2117 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2118 err
= do_path_lookup(AT_FDCWD
, name
, flags
| LOOKUP_ROOT
, &nd
);
2123 EXPORT_SYMBOL(vfs_path_lookup
);
2126 * Restricted form of lookup. Doesn't follow links, single-component only,
2127 * needs parent already locked. Doesn't follow mounts.
2130 static struct dentry
*lookup_hash(struct nameidata
*nd
)
2132 return __lookup_hash(&nd
->last
, nd
->path
.dentry
, nd
->flags
);
2136 * lookup_one_len - filesystem helper to lookup single pathname component
2137 * @name: pathname component to lookup
2138 * @base: base directory to lookup from
2139 * @len: maximum length @len should be interpreted to
2141 * Note that this routine is purely a helper for filesystem usage and should
2142 * not be called by generic code. Also note that by using this function the
2143 * nameidata argument is passed to the filesystem methods and a filesystem
2144 * using this helper needs to be prepared for that.
2146 struct dentry
*lookup_one_len(const char *name
, struct dentry
*base
, int len
)
2152 WARN_ON_ONCE(!mutex_is_locked(&base
->d_inode
->i_mutex
));
2156 this.hash
= full_name_hash(name
, len
);
2158 return ERR_PTR(-EACCES
);
2160 if (unlikely(name
[0] == '.')) {
2161 if (len
< 2 || (len
== 2 && name
[1] == '.'))
2162 return ERR_PTR(-EACCES
);
2166 c
= *(const unsigned char *)name
++;
2167 if (c
== '/' || c
== '\0')
2168 return ERR_PTR(-EACCES
);
2171 * See if the low-level filesystem might want
2172 * to use its own hash..
2174 if (base
->d_flags
& DCACHE_OP_HASH
) {
2175 int err
= base
->d_op
->d_hash(base
, &this);
2177 return ERR_PTR(err
);
2180 err
= inode_permission(base
->d_inode
, MAY_EXEC
);
2182 return ERR_PTR(err
);
2184 return __lookup_hash(&this, base
, 0);
2186 EXPORT_SYMBOL(lookup_one_len
);
2188 int user_path_at_empty(int dfd
, const char __user
*name
, unsigned flags
,
2189 struct path
*path
, int *empty
)
2191 struct nameidata nd
;
2192 struct filename
*tmp
= getname_flags(name
, flags
, empty
);
2193 int err
= PTR_ERR(tmp
);
2196 BUG_ON(flags
& LOOKUP_PARENT
);
2198 err
= filename_lookup(dfd
, tmp
, flags
, &nd
);
2206 int user_path_at(int dfd
, const char __user
*name
, unsigned flags
,
2209 return user_path_at_empty(dfd
, name
, flags
, path
, NULL
);
2211 EXPORT_SYMBOL(user_path_at
);
2214 * NB: most callers don't do anything directly with the reference to the
2215 * to struct filename, but the nd->last pointer points into the name string
2216 * allocated by getname. So we must hold the reference to it until all
2217 * path-walking is complete.
2219 static struct filename
*
2220 user_path_parent(int dfd
, const char __user
*path
, struct nameidata
*nd
,
2223 struct filename
*s
= getname(path
);
2226 /* only LOOKUP_REVAL is allowed in extra flags */
2227 flags
&= LOOKUP_REVAL
;
2232 error
= filename_lookup(dfd
, s
, flags
| LOOKUP_PARENT
, nd
);
2235 return ERR_PTR(error
);
2242 * mountpoint_last - look up last component for umount
2243 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2244 * @path: pointer to container for result
2246 * This is a special lookup_last function just for umount. In this case, we
2247 * need to resolve the path without doing any revalidation.
2249 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2250 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2251 * in almost all cases, this lookup will be served out of the dcache. The only
2252 * cases where it won't are if nd->last refers to a symlink or the path is
2253 * bogus and it doesn't exist.
2256 * -error: if there was an error during lookup. This includes -ENOENT if the
2257 * lookup found a negative dentry. The nd->path reference will also be
2260 * 0: if we successfully resolved nd->path and found it to not to be a
2261 * symlink that needs to be followed. "path" will also be populated.
2262 * The nd->path reference will also be put.
2264 * 1: if we successfully resolved nd->last and found it to be a symlink
2265 * that needs to be followed. "path" will be populated with the path
2266 * to the link, and nd->path will *not* be put.
2269 mountpoint_last(struct nameidata
*nd
, struct path
*path
)
2272 struct dentry
*dentry
;
2273 struct dentry
*dir
= nd
->path
.dentry
;
2275 /* If we're in rcuwalk, drop out of it to handle last component */
2276 if (nd
->flags
& LOOKUP_RCU
) {
2277 if (unlazy_walk(nd
, NULL
)) {
2283 nd
->flags
&= ~LOOKUP_PARENT
;
2285 if (unlikely(nd
->last_type
!= LAST_NORM
)) {
2286 error
= handle_dots(nd
, nd
->last_type
);
2289 dentry
= dget(nd
->path
.dentry
);
2293 mutex_lock(&dir
->d_inode
->i_mutex
);
2294 dentry
= d_lookup(dir
, &nd
->last
);
2297 * No cached dentry. Mounted dentries are pinned in the cache,
2298 * so that means that this dentry is probably a symlink or the
2299 * path doesn't actually point to a mounted dentry.
2301 dentry
= d_alloc(dir
, &nd
->last
);
2304 mutex_unlock(&dir
->d_inode
->i_mutex
);
2307 dentry
= lookup_real(dir
->d_inode
, dentry
, nd
->flags
);
2308 error
= PTR_ERR(dentry
);
2309 if (IS_ERR(dentry
)) {
2310 mutex_unlock(&dir
->d_inode
->i_mutex
);
2314 mutex_unlock(&dir
->d_inode
->i_mutex
);
2317 if (!dentry
->d_inode
|| d_is_negative(dentry
)) {
2322 path
->dentry
= dentry
;
2323 path
->mnt
= nd
->path
.mnt
;
2324 if (should_follow_link(dentry
, nd
->flags
& LOOKUP_FOLLOW
))
2335 * path_mountpoint - look up a path to be umounted
2336 * @dfd: directory file descriptor to start walk from
2337 * @name: full pathname to walk
2338 * @path: pointer to container for result
2339 * @flags: lookup flags
2341 * Look up the given name, but don't attempt to revalidate the last component.
2342 * Returns 0 and "path" will be valid on success; Returns error otherwise.
2345 path_mountpoint(int dfd
, const char *name
, struct path
*path
, unsigned int flags
)
2347 struct nameidata nd
;
2350 err
= path_init(dfd
, name
, flags
, &nd
);
2354 err
= mountpoint_last(&nd
, path
);
2357 struct path link
= *path
;
2358 err
= may_follow_link(&link
, &nd
);
2361 nd
.flags
|= LOOKUP_PARENT
;
2362 err
= follow_link(&link
, &nd
, &cookie
);
2365 err
= mountpoint_last(&nd
, path
);
2366 put_link(&nd
, &link
, cookie
);
2374 filename_mountpoint(int dfd
, struct filename
*s
, struct path
*path
,
2380 error
= path_mountpoint(dfd
, s
->name
, path
, flags
| LOOKUP_RCU
);
2381 if (unlikely(error
== -ECHILD
))
2382 error
= path_mountpoint(dfd
, s
->name
, path
, flags
);
2383 if (unlikely(error
== -ESTALE
))
2384 error
= path_mountpoint(dfd
, s
->name
, path
, flags
| LOOKUP_REVAL
);
2386 audit_inode(s
, path
->dentry
, 0);
2392 * user_path_mountpoint_at - lookup a path from userland in order to umount it
2393 * @dfd: directory file descriptor
2394 * @name: pathname from userland
2395 * @flags: lookup flags
2396 * @path: pointer to container to hold result
2398 * A umount is a special case for path walking. We're not actually interested
2399 * in the inode in this situation, and ESTALE errors can be a problem. We
2400 * simply want track down the dentry and vfsmount attached at the mountpoint
2401 * and avoid revalidating the last component.
2403 * Returns 0 and populates "path" on success.
2406 user_path_mountpoint_at(int dfd
, const char __user
*name
, unsigned int flags
,
2409 return filename_mountpoint(dfd
, getname(name
), path
, flags
);
2413 kern_path_mountpoint(int dfd
, const char *name
, struct path
*path
,
2416 return filename_mountpoint(dfd
, getname_kernel(name
), path
, flags
);
2418 EXPORT_SYMBOL(kern_path_mountpoint
);
2420 int __check_sticky(struct inode
*dir
, struct inode
*inode
)
2422 kuid_t fsuid
= current_fsuid();
2424 if (uid_eq(inode
->i_uid
, fsuid
))
2426 if (uid_eq(dir
->i_uid
, fsuid
))
2428 return !capable_wrt_inode_uidgid(inode
, CAP_FOWNER
);
2430 EXPORT_SYMBOL(__check_sticky
);
2433 * Check whether we can remove a link victim from directory dir, check
2434 * whether the type of victim is right.
2435 * 1. We can't do it if dir is read-only (done in permission())
2436 * 2. We should have write and exec permissions on dir
2437 * 3. We can't remove anything from append-only dir
2438 * 4. We can't do anything with immutable dir (done in permission())
2439 * 5. If the sticky bit on dir is set we should either
2440 * a. be owner of dir, or
2441 * b. be owner of victim, or
2442 * c. have CAP_FOWNER capability
2443 * 6. If the victim is append-only or immutable we can't do antyhing with
2444 * links pointing to it.
2445 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2446 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2447 * 9. We can't remove a root or mountpoint.
2448 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2449 * nfs_async_unlink().
2451 static int may_delete(struct inode
*dir
, struct dentry
*victim
, bool isdir
)
2453 struct inode
*inode
= victim
->d_inode
;
2456 if (d_is_negative(victim
))
2460 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
2461 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
2463 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
2469 if (check_sticky(dir
, inode
) || IS_APPEND(inode
) ||
2470 IS_IMMUTABLE(inode
) || IS_SWAPFILE(inode
))
2473 if (!d_is_dir(victim
))
2475 if (IS_ROOT(victim
))
2477 } else if (d_is_dir(victim
))
2479 if (IS_DEADDIR(dir
))
2481 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
2486 /* Check whether we can create an object with dentry child in directory
2488 * 1. We can't do it if child already exists (open has special treatment for
2489 * this case, but since we are inlined it's OK)
2490 * 2. We can't do it if dir is read-only (done in permission())
2491 * 3. We should have write and exec permissions on dir
2492 * 4. We can't do it if dir is immutable (done in permission())
2494 static inline int may_create(struct inode
*dir
, struct dentry
*child
)
2496 audit_inode_child(dir
, child
, AUDIT_TYPE_CHILD_CREATE
);
2499 if (IS_DEADDIR(dir
))
2501 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
2505 * p1 and p2 should be directories on the same fs.
2507 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
2512 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2516 mutex_lock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
2518 p
= d_ancestor(p2
, p1
);
2520 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2521 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
2525 p
= d_ancestor(p1
, p2
);
2527 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2528 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
2532 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2533 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_PARENT2
);
2536 EXPORT_SYMBOL(lock_rename
);
2538 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
2540 mutex_unlock(&p1
->d_inode
->i_mutex
);
2542 mutex_unlock(&p2
->d_inode
->i_mutex
);
2543 mutex_unlock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
2546 EXPORT_SYMBOL(unlock_rename
);
2548 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2551 int error
= may_create(dir
, dentry
);
2555 if (!dir
->i_op
->create
)
2556 return -EACCES
; /* shouldn't it be ENOSYS? */
2559 error
= security_inode_create(dir
, dentry
, mode
);
2562 error
= dir
->i_op
->create(dir
, dentry
, mode
, want_excl
);
2564 fsnotify_create(dir
, dentry
);
2567 EXPORT_SYMBOL(vfs_create
);
2569 static int may_open(struct path
*path
, int acc_mode
, int flag
)
2571 struct dentry
*dentry
= path
->dentry
;
2572 struct inode
*inode
= dentry
->d_inode
;
2582 switch (inode
->i_mode
& S_IFMT
) {
2586 if (acc_mode
& MAY_WRITE
)
2591 if (path
->mnt
->mnt_flags
& MNT_NODEV
)
2600 error
= inode_permission(inode
, acc_mode
);
2605 * An append-only file must be opened in append mode for writing.
2607 if (IS_APPEND(inode
)) {
2608 if ((flag
& O_ACCMODE
) != O_RDONLY
&& !(flag
& O_APPEND
))
2614 /* O_NOATIME can only be set by the owner or superuser */
2615 if (flag
& O_NOATIME
&& !inode_owner_or_capable(inode
))
2621 static int handle_truncate(struct file
*filp
)
2623 struct path
*path
= &filp
->f_path
;
2624 struct inode
*inode
= path
->dentry
->d_inode
;
2625 int error
= get_write_access(inode
);
2629 * Refuse to truncate files with mandatory locks held on them.
2631 error
= locks_verify_locked(filp
);
2633 error
= security_path_truncate(path
);
2635 error
= do_truncate(path
->dentry
, 0,
2636 ATTR_MTIME
|ATTR_CTIME
|ATTR_OPEN
,
2639 put_write_access(inode
);
2643 static inline int open_to_namei_flags(int flag
)
2645 if ((flag
& O_ACCMODE
) == 3)
2650 static int may_o_create(struct path
*dir
, struct dentry
*dentry
, umode_t mode
)
2652 int error
= security_path_mknod(dir
, dentry
, mode
, 0);
2656 error
= inode_permission(dir
->dentry
->d_inode
, MAY_WRITE
| MAY_EXEC
);
2660 return security_inode_create(dir
->dentry
->d_inode
, dentry
, mode
);
2664 * Attempt to atomically look up, create and open a file from a negative
2667 * Returns 0 if successful. The file will have been created and attached to
2668 * @file by the filesystem calling finish_open().
2670 * Returns 1 if the file was looked up only or didn't need creating. The
2671 * caller will need to perform the open themselves. @path will have been
2672 * updated to point to the new dentry. This may be negative.
2674 * Returns an error code otherwise.
2676 static int atomic_open(struct nameidata
*nd
, struct dentry
*dentry
,
2677 struct path
*path
, struct file
*file
,
2678 const struct open_flags
*op
,
2679 bool got_write
, bool need_lookup
,
2682 struct inode
*dir
= nd
->path
.dentry
->d_inode
;
2683 unsigned open_flag
= open_to_namei_flags(op
->open_flag
);
2687 int create_error
= 0;
2688 struct dentry
*const DENTRY_NOT_SET
= (void *) -1UL;
2691 BUG_ON(dentry
->d_inode
);
2693 /* Don't create child dentry for a dead directory. */
2694 if (unlikely(IS_DEADDIR(dir
))) {
2700 if ((open_flag
& O_CREAT
) && !IS_POSIXACL(dir
))
2701 mode
&= ~current_umask();
2703 excl
= (open_flag
& (O_EXCL
| O_CREAT
)) == (O_EXCL
| O_CREAT
);
2705 open_flag
&= ~O_TRUNC
;
2708 * Checking write permission is tricky, bacuse we don't know if we are
2709 * going to actually need it: O_CREAT opens should work as long as the
2710 * file exists. But checking existence breaks atomicity. The trick is
2711 * to check access and if not granted clear O_CREAT from the flags.
2713 * Another problem is returing the "right" error value (e.g. for an
2714 * O_EXCL open we want to return EEXIST not EROFS).
2716 if (((open_flag
& (O_CREAT
| O_TRUNC
)) ||
2717 (open_flag
& O_ACCMODE
) != O_RDONLY
) && unlikely(!got_write
)) {
2718 if (!(open_flag
& O_CREAT
)) {
2720 * No O_CREATE -> atomicity not a requirement -> fall
2721 * back to lookup + open
2724 } else if (open_flag
& (O_EXCL
| O_TRUNC
)) {
2725 /* Fall back and fail with the right error */
2726 create_error
= -EROFS
;
2729 /* No side effects, safe to clear O_CREAT */
2730 create_error
= -EROFS
;
2731 open_flag
&= ~O_CREAT
;
2735 if (open_flag
& O_CREAT
) {
2736 error
= may_o_create(&nd
->path
, dentry
, mode
);
2738 create_error
= error
;
2739 if (open_flag
& O_EXCL
)
2741 open_flag
&= ~O_CREAT
;
2745 if (nd
->flags
& LOOKUP_DIRECTORY
)
2746 open_flag
|= O_DIRECTORY
;
2748 file
->f_path
.dentry
= DENTRY_NOT_SET
;
2749 file
->f_path
.mnt
= nd
->path
.mnt
;
2750 error
= dir
->i_op
->atomic_open(dir
, dentry
, file
, open_flag
, mode
,
2753 if (create_error
&& error
== -ENOENT
)
2754 error
= create_error
;
2758 if (error
) { /* returned 1, that is */
2759 if (WARN_ON(file
->f_path
.dentry
== DENTRY_NOT_SET
)) {
2763 if (file
->f_path
.dentry
) {
2765 dentry
= file
->f_path
.dentry
;
2767 if (*opened
& FILE_CREATED
)
2768 fsnotify_create(dir
, dentry
);
2769 if (!dentry
->d_inode
) {
2770 WARN_ON(*opened
& FILE_CREATED
);
2772 error
= create_error
;
2776 if (excl
&& !(*opened
& FILE_CREATED
)) {
2785 * We didn't have the inode before the open, so check open permission
2788 acc_mode
= op
->acc_mode
;
2789 if (*opened
& FILE_CREATED
) {
2790 WARN_ON(!(open_flag
& O_CREAT
));
2791 fsnotify_create(dir
, dentry
);
2792 acc_mode
= MAY_OPEN
;
2794 error
= may_open(&file
->f_path
, acc_mode
, open_flag
);
2804 dentry
= lookup_real(dir
, dentry
, nd
->flags
);
2806 return PTR_ERR(dentry
);
2809 int open_flag
= op
->open_flag
;
2811 error
= create_error
;
2812 if ((open_flag
& O_EXCL
)) {
2813 if (!dentry
->d_inode
)
2815 } else if (!dentry
->d_inode
) {
2817 } else if ((open_flag
& O_TRUNC
) &&
2818 S_ISREG(dentry
->d_inode
->i_mode
)) {
2821 /* will fail later, go on to get the right error */
2825 path
->dentry
= dentry
;
2826 path
->mnt
= nd
->path
.mnt
;
2831 * Look up and maybe create and open the last component.
2833 * Must be called with i_mutex held on parent.
2835 * Returns 0 if the file was successfully atomically created (if necessary) and
2836 * opened. In this case the file will be returned attached to @file.
2838 * Returns 1 if the file was not completely opened at this time, though lookups
2839 * and creations will have been performed and the dentry returned in @path will
2840 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2841 * specified then a negative dentry may be returned.
2843 * An error code is returned otherwise.
2845 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2846 * cleared otherwise prior to returning.
2848 static int lookup_open(struct nameidata
*nd
, struct path
*path
,
2850 const struct open_flags
*op
,
2851 bool got_write
, int *opened
)
2853 struct dentry
*dir
= nd
->path
.dentry
;
2854 struct inode
*dir_inode
= dir
->d_inode
;
2855 struct dentry
*dentry
;
2859 *opened
&= ~FILE_CREATED
;
2860 dentry
= lookup_dcache(&nd
->last
, dir
, nd
->flags
, &need_lookup
);
2862 return PTR_ERR(dentry
);
2864 /* Cached positive dentry: will open in f_op->open */
2865 if (!need_lookup
&& dentry
->d_inode
)
2868 if ((nd
->flags
& LOOKUP_OPEN
) && dir_inode
->i_op
->atomic_open
) {
2869 return atomic_open(nd
, dentry
, path
, file
, op
, got_write
,
2870 need_lookup
, opened
);
2874 BUG_ON(dentry
->d_inode
);
2876 dentry
= lookup_real(dir_inode
, dentry
, nd
->flags
);
2878 return PTR_ERR(dentry
);
2881 /* Negative dentry, just create the file */
2882 if (!dentry
->d_inode
&& (op
->open_flag
& O_CREAT
)) {
2883 umode_t mode
= op
->mode
;
2884 if (!IS_POSIXACL(dir
->d_inode
))
2885 mode
&= ~current_umask();
2887 * This write is needed to ensure that a
2888 * rw->ro transition does not occur between
2889 * the time when the file is created and when
2890 * a permanent write count is taken through
2891 * the 'struct file' in finish_open().
2897 *opened
|= FILE_CREATED
;
2898 error
= security_path_mknod(&nd
->path
, dentry
, mode
, 0);
2901 error
= vfs_create(dir
->d_inode
, dentry
, mode
,
2902 nd
->flags
& LOOKUP_EXCL
);
2907 path
->dentry
= dentry
;
2908 path
->mnt
= nd
->path
.mnt
;
2917 * Handle the last step of open()
2919 static int do_last(struct nameidata
*nd
, struct path
*path
,
2920 struct file
*file
, const struct open_flags
*op
,
2921 int *opened
, struct filename
*name
)
2923 struct dentry
*dir
= nd
->path
.dentry
;
2924 int open_flag
= op
->open_flag
;
2925 bool will_truncate
= (open_flag
& O_TRUNC
) != 0;
2926 bool got_write
= false;
2927 int acc_mode
= op
->acc_mode
;
2928 struct inode
*inode
;
2929 bool symlink_ok
= false;
2930 struct path save_parent
= { .dentry
= NULL
, .mnt
= NULL
};
2931 bool retried
= false;
2934 nd
->flags
&= ~LOOKUP_PARENT
;
2935 nd
->flags
|= op
->intent
;
2937 if (nd
->last_type
!= LAST_NORM
) {
2938 error
= handle_dots(nd
, nd
->last_type
);
2944 if (!(open_flag
& O_CREAT
)) {
2945 if (nd
->last
.name
[nd
->last
.len
])
2946 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
2947 if (open_flag
& O_PATH
&& !(nd
->flags
& LOOKUP_FOLLOW
))
2949 /* we _can_ be in RCU mode here */
2950 error
= lookup_fast(nd
, path
, &inode
);
2957 BUG_ON(nd
->inode
!= dir
->d_inode
);
2959 /* create side of things */
2961 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2962 * has been cleared when we got to the last component we are
2965 error
= complete_walk(nd
);
2969 audit_inode(name
, dir
, LOOKUP_PARENT
);
2971 /* trailing slashes? */
2972 if (nd
->last
.name
[nd
->last
.len
])
2977 if (op
->open_flag
& (O_CREAT
| O_TRUNC
| O_WRONLY
| O_RDWR
)) {
2978 error
= mnt_want_write(nd
->path
.mnt
);
2982 * do _not_ fail yet - we might not need that or fail with
2983 * a different error; let lookup_open() decide; we'll be
2984 * dropping this one anyway.
2987 mutex_lock(&dir
->d_inode
->i_mutex
);
2988 error
= lookup_open(nd
, path
, file
, op
, got_write
, opened
);
2989 mutex_unlock(&dir
->d_inode
->i_mutex
);
2995 if ((*opened
& FILE_CREATED
) ||
2996 !S_ISREG(file_inode(file
)->i_mode
))
2997 will_truncate
= false;
2999 audit_inode(name
, file
->f_path
.dentry
, 0);
3003 if (*opened
& FILE_CREATED
) {
3004 /* Don't check for write permission, don't truncate */
3005 open_flag
&= ~O_TRUNC
;
3006 will_truncate
= false;
3007 acc_mode
= MAY_OPEN
;
3008 path_to_nameidata(path
, nd
);
3009 goto finish_open_created
;
3013 * create/update audit record if it already exists.
3015 if (d_is_positive(path
->dentry
))
3016 audit_inode(name
, path
->dentry
, 0);
3019 * If atomic_open() acquired write access it is dropped now due to
3020 * possible mount and symlink following (this might be optimized away if
3024 mnt_drop_write(nd
->path
.mnt
);
3029 if ((open_flag
& (O_EXCL
| O_CREAT
)) == (O_EXCL
| O_CREAT
))
3032 error
= follow_managed(path
, nd
->flags
);
3037 nd
->flags
|= LOOKUP_JUMPED
;
3039 BUG_ON(nd
->flags
& LOOKUP_RCU
);
3040 inode
= path
->dentry
->d_inode
;
3042 /* we _can_ be in RCU mode here */
3044 if (!inode
|| d_is_negative(path
->dentry
)) {
3045 path_to_nameidata(path
, nd
);
3049 if (should_follow_link(path
->dentry
, !symlink_ok
)) {
3050 if (nd
->flags
& LOOKUP_RCU
) {
3051 if (unlikely(unlazy_walk(nd
, path
->dentry
))) {
3056 BUG_ON(inode
!= path
->dentry
->d_inode
);
3060 if ((nd
->flags
& LOOKUP_RCU
) || nd
->path
.mnt
!= path
->mnt
) {
3061 path_to_nameidata(path
, nd
);
3063 save_parent
.dentry
= nd
->path
.dentry
;
3064 save_parent
.mnt
= mntget(path
->mnt
);
3065 nd
->path
.dentry
= path
->dentry
;
3069 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
3071 error
= complete_walk(nd
);
3073 path_put(&save_parent
);
3076 audit_inode(name
, nd
->path
.dentry
, 0);
3078 if ((open_flag
& O_CREAT
) && d_is_dir(nd
->path
.dentry
))
3081 if ((nd
->flags
& LOOKUP_DIRECTORY
) && !d_can_lookup(nd
->path
.dentry
))
3083 if (!S_ISREG(nd
->inode
->i_mode
))
3084 will_truncate
= false;
3086 if (will_truncate
) {
3087 error
= mnt_want_write(nd
->path
.mnt
);
3092 finish_open_created
:
3093 error
= may_open(&nd
->path
, acc_mode
, open_flag
);
3097 BUG_ON(*opened
& FILE_OPENED
); /* once it's opened, it's opened */
3098 error
= vfs_open(&nd
->path
, file
, current_cred());
3100 *opened
|= FILE_OPENED
;
3102 if (error
== -EOPENSTALE
)
3107 error
= open_check_o_direct(file
);
3110 error
= ima_file_check(file
, op
->acc_mode
, *opened
);
3114 if (will_truncate
) {
3115 error
= handle_truncate(file
);
3121 mnt_drop_write(nd
->path
.mnt
);
3122 path_put(&save_parent
);
3127 path_put_conditional(path
, nd
);
3134 /* If no saved parent or already retried then can't retry */
3135 if (!save_parent
.dentry
|| retried
)
3138 BUG_ON(save_parent
.dentry
!= dir
);
3139 path_put(&nd
->path
);
3140 nd
->path
= save_parent
;
3141 nd
->inode
= dir
->d_inode
;
3142 save_parent
.mnt
= NULL
;
3143 save_parent
.dentry
= NULL
;
3145 mnt_drop_write(nd
->path
.mnt
);
3152 static int do_tmpfile(int dfd
, struct filename
*pathname
,
3153 struct nameidata
*nd
, int flags
,
3154 const struct open_flags
*op
,
3155 struct file
*file
, int *opened
)
3157 static const struct qstr name
= QSTR_INIT("/", 1);
3158 struct dentry
*dentry
, *child
;
3160 int error
= path_lookupat(dfd
, pathname
->name
,
3161 flags
| LOOKUP_DIRECTORY
, nd
);
3162 if (unlikely(error
))
3164 error
= mnt_want_write(nd
->path
.mnt
);
3165 if (unlikely(error
))
3167 /* we want directory to be writable */
3168 error
= inode_permission(nd
->inode
, MAY_WRITE
| MAY_EXEC
);
3171 dentry
= nd
->path
.dentry
;
3172 dir
= dentry
->d_inode
;
3173 if (!dir
->i_op
->tmpfile
) {
3174 error
= -EOPNOTSUPP
;
3177 child
= d_alloc(dentry
, &name
);
3178 if (unlikely(!child
)) {
3182 nd
->flags
&= ~LOOKUP_DIRECTORY
;
3183 nd
->flags
|= op
->intent
;
3184 dput(nd
->path
.dentry
);
3185 nd
->path
.dentry
= child
;
3186 error
= dir
->i_op
->tmpfile(dir
, nd
->path
.dentry
, op
->mode
);
3189 audit_inode(pathname
, nd
->path
.dentry
, 0);
3190 /* Don't check for other permissions, the inode was just created */
3191 error
= may_open(&nd
->path
, MAY_OPEN
, op
->open_flag
);
3194 file
->f_path
.mnt
= nd
->path
.mnt
;
3195 error
= finish_open(file
, nd
->path
.dentry
, NULL
, opened
);
3198 error
= open_check_o_direct(file
);
3201 } else if (!(op
->open_flag
& O_EXCL
)) {
3202 struct inode
*inode
= file_inode(file
);
3203 spin_lock(&inode
->i_lock
);
3204 inode
->i_state
|= I_LINKABLE
;
3205 spin_unlock(&inode
->i_lock
);
3208 mnt_drop_write(nd
->path
.mnt
);
3210 path_put(&nd
->path
);
3214 static struct file
*path_openat(int dfd
, struct filename
*pathname
,
3215 struct nameidata
*nd
, const struct open_flags
*op
, int flags
)
3222 file
= get_empty_filp();
3226 file
->f_flags
= op
->open_flag
;
3228 if (unlikely(file
->f_flags
& __O_TMPFILE
)) {
3229 error
= do_tmpfile(dfd
, pathname
, nd
, flags
, op
, file
, &opened
);
3233 error
= path_init(dfd
, pathname
->name
, flags
, nd
);
3234 if (unlikely(error
))
3237 error
= do_last(nd
, &path
, file
, op
, &opened
, pathname
);
3238 while (unlikely(error
> 0)) { /* trailing symlink */
3239 struct path link
= path
;
3241 if (!(nd
->flags
& LOOKUP_FOLLOW
)) {
3242 path_put_conditional(&path
, nd
);
3243 path_put(&nd
->path
);
3247 error
= may_follow_link(&link
, nd
);
3248 if (unlikely(error
))
3250 nd
->flags
|= LOOKUP_PARENT
;
3251 nd
->flags
&= ~(LOOKUP_OPEN
|LOOKUP_CREATE
|LOOKUP_EXCL
);
3252 error
= follow_link(&link
, nd
, &cookie
);
3253 if (unlikely(error
))
3255 error
= do_last(nd
, &path
, file
, op
, &opened
, pathname
);
3256 put_link(nd
, &link
, cookie
);
3260 if (!(opened
& FILE_OPENED
)) {
3264 if (unlikely(error
)) {
3265 if (error
== -EOPENSTALE
) {
3266 if (flags
& LOOKUP_RCU
)
3271 file
= ERR_PTR(error
);
3276 struct file
*do_filp_open(int dfd
, struct filename
*pathname
,
3277 const struct open_flags
*op
)
3279 struct nameidata nd
;
3280 int flags
= op
->lookup_flags
;
3283 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
| LOOKUP_RCU
);
3284 if (unlikely(filp
== ERR_PTR(-ECHILD
)))
3285 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
);
3286 if (unlikely(filp
== ERR_PTR(-ESTALE
)))
3287 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
| LOOKUP_REVAL
);
3291 struct file
*do_file_open_root(struct dentry
*dentry
, struct vfsmount
*mnt
,
3292 const char *name
, const struct open_flags
*op
)
3294 struct nameidata nd
;
3296 struct filename
*filename
;
3297 int flags
= op
->lookup_flags
| LOOKUP_ROOT
;
3300 nd
.root
.dentry
= dentry
;
3302 if (d_is_symlink(dentry
) && op
->intent
& LOOKUP_OPEN
)
3303 return ERR_PTR(-ELOOP
);
3305 filename
= getname_kernel(name
);
3306 if (unlikely(IS_ERR(filename
)))
3307 return ERR_CAST(filename
);
3309 file
= path_openat(-1, filename
, &nd
, op
, flags
| LOOKUP_RCU
);
3310 if (unlikely(file
== ERR_PTR(-ECHILD
)))
3311 file
= path_openat(-1, filename
, &nd
, op
, flags
);
3312 if (unlikely(file
== ERR_PTR(-ESTALE
)))
3313 file
= path_openat(-1, filename
, &nd
, op
, flags
| LOOKUP_REVAL
);
3318 static struct dentry
*filename_create(int dfd
, struct filename
*name
,
3319 struct path
*path
, unsigned int lookup_flags
)
3321 struct dentry
*dentry
= ERR_PTR(-EEXIST
);
3322 struct nameidata nd
;
3325 bool is_dir
= (lookup_flags
& LOOKUP_DIRECTORY
);
3328 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3329 * other flags passed in are ignored!
3331 lookup_flags
&= LOOKUP_REVAL
;
3333 error
= filename_lookup(dfd
, name
, LOOKUP_PARENT
|lookup_flags
, &nd
);
3335 return ERR_PTR(error
);
3338 * Yucky last component or no last component at all?
3339 * (foo/., foo/.., /////)
3341 if (nd
.last_type
!= LAST_NORM
)
3343 nd
.flags
&= ~LOOKUP_PARENT
;
3344 nd
.flags
|= LOOKUP_CREATE
| LOOKUP_EXCL
;
3346 /* don't fail immediately if it's r/o, at least try to report other errors */
3347 err2
= mnt_want_write(nd
.path
.mnt
);
3349 * Do the final lookup.
3351 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
3352 dentry
= lookup_hash(&nd
);
3357 if (d_is_positive(dentry
))
3361 * Special case - lookup gave negative, but... we had foo/bar/
3362 * From the vfs_mknod() POV we just have a negative dentry -
3363 * all is fine. Let's be bastards - you had / on the end, you've
3364 * been asking for (non-existent) directory. -ENOENT for you.
3366 if (unlikely(!is_dir
&& nd
.last
.name
[nd
.last
.len
])) {
3370 if (unlikely(err2
)) {
3378 dentry
= ERR_PTR(error
);
3380 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
3382 mnt_drop_write(nd
.path
.mnt
);
3388 struct dentry
*kern_path_create(int dfd
, const char *pathname
,
3389 struct path
*path
, unsigned int lookup_flags
)
3391 struct filename
*filename
= getname_kernel(pathname
);
3394 if (IS_ERR(filename
))
3395 return ERR_CAST(filename
);
3396 res
= filename_create(dfd
, filename
, path
, lookup_flags
);
3400 EXPORT_SYMBOL(kern_path_create
);
3402 void done_path_create(struct path
*path
, struct dentry
*dentry
)
3405 mutex_unlock(&path
->dentry
->d_inode
->i_mutex
);
3406 mnt_drop_write(path
->mnt
);
3409 EXPORT_SYMBOL(done_path_create
);
3411 struct dentry
*user_path_create(int dfd
, const char __user
*pathname
,
3412 struct path
*path
, unsigned int lookup_flags
)
3414 struct filename
*tmp
= getname(pathname
);
3417 return ERR_CAST(tmp
);
3418 res
= filename_create(dfd
, tmp
, path
, lookup_flags
);
3422 EXPORT_SYMBOL(user_path_create
);
3424 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t dev
)
3426 int error
= may_create(dir
, dentry
);
3431 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
3434 if (!dir
->i_op
->mknod
)
3437 error
= devcgroup_inode_mknod(mode
, dev
);
3441 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
3445 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
3447 fsnotify_create(dir
, dentry
);
3450 EXPORT_SYMBOL(vfs_mknod
);
3452 static int may_mknod(umode_t mode
)
3454 switch (mode
& S_IFMT
) {
3460 case 0: /* zero mode translates to S_IFREG */
3469 SYSCALL_DEFINE4(mknodat
, int, dfd
, const char __user
*, filename
, umode_t
, mode
,
3472 struct dentry
*dentry
;
3475 unsigned int lookup_flags
= 0;
3477 error
= may_mknod(mode
);
3481 dentry
= user_path_create(dfd
, filename
, &path
, lookup_flags
);
3483 return PTR_ERR(dentry
);
3485 if (!IS_POSIXACL(path
.dentry
->d_inode
))
3486 mode
&= ~current_umask();
3487 error
= security_path_mknod(&path
, dentry
, mode
, dev
);
3490 switch (mode
& S_IFMT
) {
3491 case 0: case S_IFREG
:
3492 error
= vfs_create(path
.dentry
->d_inode
,dentry
,mode
,true);
3494 case S_IFCHR
: case S_IFBLK
:
3495 error
= vfs_mknod(path
.dentry
->d_inode
,dentry
,mode
,
3496 new_decode_dev(dev
));
3498 case S_IFIFO
: case S_IFSOCK
:
3499 error
= vfs_mknod(path
.dentry
->d_inode
,dentry
,mode
,0);
3503 done_path_create(&path
, dentry
);
3504 if (retry_estale(error
, lookup_flags
)) {
3505 lookup_flags
|= LOOKUP_REVAL
;
3511 SYSCALL_DEFINE3(mknod
, const char __user
*, filename
, umode_t
, mode
, unsigned, dev
)
3513 return sys_mknodat(AT_FDCWD
, filename
, mode
, dev
);
3516 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
3518 int error
= may_create(dir
, dentry
);
3519 unsigned max_links
= dir
->i_sb
->s_max_links
;
3524 if (!dir
->i_op
->mkdir
)
3527 mode
&= (S_IRWXUGO
|S_ISVTX
);
3528 error
= security_inode_mkdir(dir
, dentry
, mode
);
3532 if (max_links
&& dir
->i_nlink
>= max_links
)
3535 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
3537 fsnotify_mkdir(dir
, dentry
);
3540 EXPORT_SYMBOL(vfs_mkdir
);
3542 SYSCALL_DEFINE3(mkdirat
, int, dfd
, const char __user
*, pathname
, umode_t
, mode
)
3544 struct dentry
*dentry
;
3547 unsigned int lookup_flags
= LOOKUP_DIRECTORY
;
3550 dentry
= user_path_create(dfd
, pathname
, &path
, lookup_flags
);
3552 return PTR_ERR(dentry
);
3554 if (!IS_POSIXACL(path
.dentry
->d_inode
))
3555 mode
&= ~current_umask();
3556 error
= security_path_mkdir(&path
, dentry
, mode
);
3558 error
= vfs_mkdir(path
.dentry
->d_inode
, dentry
, mode
);
3559 done_path_create(&path
, dentry
);
3560 if (retry_estale(error
, lookup_flags
)) {
3561 lookup_flags
|= LOOKUP_REVAL
;
3567 SYSCALL_DEFINE2(mkdir
, const char __user
*, pathname
, umode_t
, mode
)
3569 return sys_mkdirat(AT_FDCWD
, pathname
, mode
);
3573 * The dentry_unhash() helper will try to drop the dentry early: we
3574 * should have a usage count of 1 if we're the only user of this
3575 * dentry, and if that is true (possibly after pruning the dcache),
3576 * then we drop the dentry now.
3578 * A low-level filesystem can, if it choses, legally
3581 * if (!d_unhashed(dentry))
3584 * if it cannot handle the case of removing a directory
3585 * that is still in use by something else..
3587 void dentry_unhash(struct dentry
*dentry
)
3589 shrink_dcache_parent(dentry
);
3590 spin_lock(&dentry
->d_lock
);
3591 if (dentry
->d_lockref
.count
== 1)
3593 spin_unlock(&dentry
->d_lock
);
3595 EXPORT_SYMBOL(dentry_unhash
);
3597 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3599 int error
= may_delete(dir
, dentry
, 1);
3604 if (!dir
->i_op
->rmdir
)
3608 mutex_lock(&dentry
->d_inode
->i_mutex
);
3611 if (is_local_mountpoint(dentry
))
3614 error
= security_inode_rmdir(dir
, dentry
);
3618 shrink_dcache_parent(dentry
);
3619 error
= dir
->i_op
->rmdir(dir
, dentry
);
3623 dentry
->d_inode
->i_flags
|= S_DEAD
;
3625 detach_mounts(dentry
);
3628 mutex_unlock(&dentry
->d_inode
->i_mutex
);
3634 EXPORT_SYMBOL(vfs_rmdir
);
3636 static long do_rmdir(int dfd
, const char __user
*pathname
)
3639 struct filename
*name
;
3640 struct dentry
*dentry
;
3641 struct nameidata nd
;
3642 unsigned int lookup_flags
= 0;
3644 name
= user_path_parent(dfd
, pathname
, &nd
, lookup_flags
);
3646 return PTR_ERR(name
);
3648 switch(nd
.last_type
) {
3660 nd
.flags
&= ~LOOKUP_PARENT
;
3661 error
= mnt_want_write(nd
.path
.mnt
);
3665 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
3666 dentry
= lookup_hash(&nd
);
3667 error
= PTR_ERR(dentry
);
3670 if (!dentry
->d_inode
) {
3674 error
= security_path_rmdir(&nd
.path
, dentry
);
3677 error
= vfs_rmdir(nd
.path
.dentry
->d_inode
, dentry
);
3681 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
3682 mnt_drop_write(nd
.path
.mnt
);
3686 if (retry_estale(error
, lookup_flags
)) {
3687 lookup_flags
|= LOOKUP_REVAL
;
3693 SYSCALL_DEFINE1(rmdir
, const char __user
*, pathname
)
3695 return do_rmdir(AT_FDCWD
, pathname
);
3699 * vfs_unlink - unlink a filesystem object
3700 * @dir: parent directory
3702 * @delegated_inode: returns victim inode, if the inode is delegated.
3704 * The caller must hold dir->i_mutex.
3706 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3707 * return a reference to the inode in delegated_inode. The caller
3708 * should then break the delegation on that inode and retry. Because
3709 * breaking a delegation may take a long time, the caller should drop
3710 * dir->i_mutex before doing so.
3712 * Alternatively, a caller may pass NULL for delegated_inode. This may
3713 * be appropriate for callers that expect the underlying filesystem not
3714 * to be NFS exported.
3716 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
, struct inode
**delegated_inode
)
3718 struct inode
*target
= dentry
->d_inode
;
3719 int error
= may_delete(dir
, dentry
, 0);
3724 if (!dir
->i_op
->unlink
)
3727 mutex_lock(&target
->i_mutex
);
3728 if (is_local_mountpoint(dentry
))
3731 error
= security_inode_unlink(dir
, dentry
);
3733 error
= try_break_deleg(target
, delegated_inode
);
3736 error
= dir
->i_op
->unlink(dir
, dentry
);
3739 detach_mounts(dentry
);
3744 mutex_unlock(&target
->i_mutex
);
3746 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3747 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
3748 fsnotify_link_count(target
);
3754 EXPORT_SYMBOL(vfs_unlink
);
3757 * Make sure that the actual truncation of the file will occur outside its
3758 * directory's i_mutex. Truncate can take a long time if there is a lot of
3759 * writeout happening, and we don't want to prevent access to the directory
3760 * while waiting on the I/O.
3762 static long do_unlinkat(int dfd
, const char __user
*pathname
)
3765 struct filename
*name
;
3766 struct dentry
*dentry
;
3767 struct nameidata nd
;
3768 struct inode
*inode
= NULL
;
3769 struct inode
*delegated_inode
= NULL
;
3770 unsigned int lookup_flags
= 0;
3772 name
= user_path_parent(dfd
, pathname
, &nd
, lookup_flags
);
3774 return PTR_ERR(name
);
3777 if (nd
.last_type
!= LAST_NORM
)
3780 nd
.flags
&= ~LOOKUP_PARENT
;
3781 error
= mnt_want_write(nd
.path
.mnt
);
3785 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
3786 dentry
= lookup_hash(&nd
);
3787 error
= PTR_ERR(dentry
);
3788 if (!IS_ERR(dentry
)) {
3789 /* Why not before? Because we want correct error value */
3790 if (nd
.last
.name
[nd
.last
.len
])
3792 inode
= dentry
->d_inode
;
3793 if (d_is_negative(dentry
))
3796 error
= security_path_unlink(&nd
.path
, dentry
);
3799 error
= vfs_unlink(nd
.path
.dentry
->d_inode
, dentry
, &delegated_inode
);
3803 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
3805 iput(inode
); /* truncate the inode here */
3807 if (delegated_inode
) {
3808 error
= break_deleg_wait(&delegated_inode
);
3812 mnt_drop_write(nd
.path
.mnt
);
3816 if (retry_estale(error
, lookup_flags
)) {
3817 lookup_flags
|= LOOKUP_REVAL
;
3824 if (d_is_negative(dentry
))
3826 else if (d_is_dir(dentry
))
3833 SYSCALL_DEFINE3(unlinkat
, int, dfd
, const char __user
*, pathname
, int, flag
)
3835 if ((flag
& ~AT_REMOVEDIR
) != 0)
3838 if (flag
& AT_REMOVEDIR
)
3839 return do_rmdir(dfd
, pathname
);
3841 return do_unlinkat(dfd
, pathname
);
3844 SYSCALL_DEFINE1(unlink
, const char __user
*, pathname
)
3846 return do_unlinkat(AT_FDCWD
, pathname
);
3849 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
3851 int error
= may_create(dir
, dentry
);
3856 if (!dir
->i_op
->symlink
)
3859 error
= security_inode_symlink(dir
, dentry
, oldname
);
3863 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
3865 fsnotify_create(dir
, dentry
);
3868 EXPORT_SYMBOL(vfs_symlink
);
3870 SYSCALL_DEFINE3(symlinkat
, const char __user
*, oldname
,
3871 int, newdfd
, const char __user
*, newname
)
3874 struct filename
*from
;
3875 struct dentry
*dentry
;
3877 unsigned int lookup_flags
= 0;
3879 from
= getname(oldname
);
3881 return PTR_ERR(from
);
3883 dentry
= user_path_create(newdfd
, newname
, &path
, lookup_flags
);
3884 error
= PTR_ERR(dentry
);
3888 error
= security_path_symlink(&path
, dentry
, from
->name
);
3890 error
= vfs_symlink(path
.dentry
->d_inode
, dentry
, from
->name
);
3891 done_path_create(&path
, dentry
);
3892 if (retry_estale(error
, lookup_flags
)) {
3893 lookup_flags
|= LOOKUP_REVAL
;
3901 SYSCALL_DEFINE2(symlink
, const char __user
*, oldname
, const char __user
*, newname
)
3903 return sys_symlinkat(oldname
, AT_FDCWD
, newname
);
3907 * vfs_link - create a new link
3908 * @old_dentry: object to be linked
3910 * @new_dentry: where to create the new link
3911 * @delegated_inode: returns inode needing a delegation break
3913 * The caller must hold dir->i_mutex
3915 * If vfs_link discovers a delegation on the to-be-linked file in need
3916 * of breaking, it will return -EWOULDBLOCK and return a reference to the
3917 * inode in delegated_inode. The caller should then break the delegation
3918 * and retry. Because breaking a delegation may take a long time, the
3919 * caller should drop the i_mutex before doing so.
3921 * Alternatively, a caller may pass NULL for delegated_inode. This may
3922 * be appropriate for callers that expect the underlying filesystem not
3923 * to be NFS exported.
3925 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
, struct inode
**delegated_inode
)
3927 struct inode
*inode
= old_dentry
->d_inode
;
3928 unsigned max_links
= dir
->i_sb
->s_max_links
;
3934 error
= may_create(dir
, new_dentry
);
3938 if (dir
->i_sb
!= inode
->i_sb
)
3942 * A link to an append-only or immutable file cannot be created.
3944 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
3946 if (!dir
->i_op
->link
)
3948 if (S_ISDIR(inode
->i_mode
))
3951 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
3955 mutex_lock(&inode
->i_mutex
);
3956 /* Make sure we don't allow creating hardlink to an unlinked file */
3957 if (inode
->i_nlink
== 0 && !(inode
->i_state
& I_LINKABLE
))
3959 else if (max_links
&& inode
->i_nlink
>= max_links
)
3962 error
= try_break_deleg(inode
, delegated_inode
);
3964 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
3967 if (!error
&& (inode
->i_state
& I_LINKABLE
)) {
3968 spin_lock(&inode
->i_lock
);
3969 inode
->i_state
&= ~I_LINKABLE
;
3970 spin_unlock(&inode
->i_lock
);
3972 mutex_unlock(&inode
->i_mutex
);
3974 fsnotify_link(dir
, inode
, new_dentry
);
3977 EXPORT_SYMBOL(vfs_link
);
3980 * Hardlinks are often used in delicate situations. We avoid
3981 * security-related surprises by not following symlinks on the
3984 * We don't follow them on the oldname either to be compatible
3985 * with linux 2.0, and to avoid hard-linking to directories
3986 * and other special files. --ADM
3988 SYSCALL_DEFINE5(linkat
, int, olddfd
, const char __user
*, oldname
,
3989 int, newdfd
, const char __user
*, newname
, int, flags
)
3991 struct dentry
*new_dentry
;
3992 struct path old_path
, new_path
;
3993 struct inode
*delegated_inode
= NULL
;
3997 if ((flags
& ~(AT_SYMLINK_FOLLOW
| AT_EMPTY_PATH
)) != 0)
4000 * To use null names we require CAP_DAC_READ_SEARCH
4001 * This ensures that not everyone will be able to create
4002 * handlink using the passed filedescriptor.
4004 if (flags
& AT_EMPTY_PATH
) {
4005 if (!capable(CAP_DAC_READ_SEARCH
))
4010 if (flags
& AT_SYMLINK_FOLLOW
)
4011 how
|= LOOKUP_FOLLOW
;
4013 error
= user_path_at(olddfd
, oldname
, how
, &old_path
);
4017 new_dentry
= user_path_create(newdfd
, newname
, &new_path
,
4018 (how
& LOOKUP_REVAL
));
4019 error
= PTR_ERR(new_dentry
);
4020 if (IS_ERR(new_dentry
))
4024 if (old_path
.mnt
!= new_path
.mnt
)
4026 error
= may_linkat(&old_path
);
4027 if (unlikely(error
))
4029 error
= security_path_link(old_path
.dentry
, &new_path
, new_dentry
);
4032 error
= vfs_link(old_path
.dentry
, new_path
.dentry
->d_inode
, new_dentry
, &delegated_inode
);
4034 done_path_create(&new_path
, new_dentry
);
4035 if (delegated_inode
) {
4036 error
= break_deleg_wait(&delegated_inode
);
4038 path_put(&old_path
);
4042 if (retry_estale(error
, how
)) {
4043 path_put(&old_path
);
4044 how
|= LOOKUP_REVAL
;
4048 path_put(&old_path
);
4053 SYSCALL_DEFINE2(link
, const char __user
*, oldname
, const char __user
*, newname
)
4055 return sys_linkat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
, 0);
4059 * vfs_rename - rename a filesystem object
4060 * @old_dir: parent of source
4061 * @old_dentry: source
4062 * @new_dir: parent of destination
4063 * @new_dentry: destination
4064 * @delegated_inode: returns an inode needing a delegation break
4065 * @flags: rename flags
4067 * The caller must hold multiple mutexes--see lock_rename()).
4069 * If vfs_rename discovers a delegation in need of breaking at either
4070 * the source or destination, it will return -EWOULDBLOCK and return a
4071 * reference to the inode in delegated_inode. The caller should then
4072 * break the delegation and retry. Because breaking a delegation may
4073 * take a long time, the caller should drop all locks before doing
4076 * Alternatively, a caller may pass NULL for delegated_inode. This may
4077 * be appropriate for callers that expect the underlying filesystem not
4078 * to be NFS exported.
4080 * The worst of all namespace operations - renaming directory. "Perverted"
4081 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4083 * a) we can get into loop creation.
4084 * b) race potential - two innocent renames can create a loop together.
4085 * That's where 4.4 screws up. Current fix: serialization on
4086 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
4088 * c) we have to lock _four_ objects - parents and victim (if it exists),
4089 * and source (if it is not a directory).
4090 * And that - after we got ->i_mutex on parents (until then we don't know
4091 * whether the target exists). Solution: try to be smart with locking
4092 * order for inodes. We rely on the fact that tree topology may change
4093 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
4094 * move will be locked. Thus we can rank directories by the tree
4095 * (ancestors first) and rank all non-directories after them.
4096 * That works since everybody except rename does "lock parent, lookup,
4097 * lock child" and rename is under ->s_vfs_rename_mutex.
4098 * HOWEVER, it relies on the assumption that any object with ->lookup()
4099 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4100 * we'd better make sure that there's no link(2) for them.
4101 * d) conversion from fhandle to dentry may come in the wrong moment - when
4102 * we are removing the target. Solution: we will have to grab ->i_mutex
4103 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4104 * ->i_mutex on parents, which works but leads to some truly excessive
4107 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
4108 struct inode
*new_dir
, struct dentry
*new_dentry
,
4109 struct inode
**delegated_inode
, unsigned int flags
)
4112 bool is_dir
= d_is_dir(old_dentry
);
4113 const unsigned char *old_name
;
4114 struct inode
*source
= old_dentry
->d_inode
;
4115 struct inode
*target
= new_dentry
->d_inode
;
4116 bool new_is_dir
= false;
4117 unsigned max_links
= new_dir
->i_sb
->s_max_links
;
4119 if (source
== target
)
4122 error
= may_delete(old_dir
, old_dentry
, is_dir
);
4127 error
= may_create(new_dir
, new_dentry
);
4129 new_is_dir
= d_is_dir(new_dentry
);
4131 if (!(flags
& RENAME_EXCHANGE
))
4132 error
= may_delete(new_dir
, new_dentry
, is_dir
);
4134 error
= may_delete(new_dir
, new_dentry
, new_is_dir
);
4139 if (!old_dir
->i_op
->rename
&& !old_dir
->i_op
->rename2
)
4142 if (flags
&& !old_dir
->i_op
->rename2
)
4146 * If we are going to change the parent - check write permissions,
4147 * we'll need to flip '..'.
4149 if (new_dir
!= old_dir
) {
4151 error
= inode_permission(source
, MAY_WRITE
);
4155 if ((flags
& RENAME_EXCHANGE
) && new_is_dir
) {
4156 error
= inode_permission(target
, MAY_WRITE
);
4162 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
,
4167 old_name
= fsnotify_oldname_init(old_dentry
->d_name
.name
);
4169 if (!is_dir
|| (flags
& RENAME_EXCHANGE
))
4170 lock_two_nondirectories(source
, target
);
4172 mutex_lock(&target
->i_mutex
);
4175 if (is_local_mountpoint(old_dentry
) || is_local_mountpoint(new_dentry
))
4178 if (max_links
&& new_dir
!= old_dir
) {
4180 if (is_dir
&& !new_is_dir
&& new_dir
->i_nlink
>= max_links
)
4182 if ((flags
& RENAME_EXCHANGE
) && !is_dir
&& new_is_dir
&&
4183 old_dir
->i_nlink
>= max_links
)
4186 if (is_dir
&& !(flags
& RENAME_EXCHANGE
) && target
)
4187 shrink_dcache_parent(new_dentry
);
4189 error
= try_break_deleg(source
, delegated_inode
);
4193 if (target
&& !new_is_dir
) {
4194 error
= try_break_deleg(target
, delegated_inode
);
4198 if (!old_dir
->i_op
->rename2
) {
4199 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
,
4200 new_dir
, new_dentry
);
4202 WARN_ON(old_dir
->i_op
->rename
!= NULL
);
4203 error
= old_dir
->i_op
->rename2(old_dir
, old_dentry
,
4204 new_dir
, new_dentry
, flags
);
4209 if (!(flags
& RENAME_EXCHANGE
) && target
) {
4211 target
->i_flags
|= S_DEAD
;
4212 dont_mount(new_dentry
);
4213 detach_mounts(new_dentry
);
4215 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
)) {
4216 if (!(flags
& RENAME_EXCHANGE
))
4217 d_move(old_dentry
, new_dentry
);
4219 d_exchange(old_dentry
, new_dentry
);
4222 if (!is_dir
|| (flags
& RENAME_EXCHANGE
))
4223 unlock_two_nondirectories(source
, target
);
4225 mutex_unlock(&target
->i_mutex
);
4228 fsnotify_move(old_dir
, new_dir
, old_name
, is_dir
,
4229 !(flags
& RENAME_EXCHANGE
) ? target
: NULL
, old_dentry
);
4230 if (flags
& RENAME_EXCHANGE
) {
4231 fsnotify_move(new_dir
, old_dir
, old_dentry
->d_name
.name
,
4232 new_is_dir
, NULL
, new_dentry
);
4235 fsnotify_oldname_free(old_name
);
4239 EXPORT_SYMBOL(vfs_rename
);
4241 SYSCALL_DEFINE5(renameat2
, int, olddfd
, const char __user
*, oldname
,
4242 int, newdfd
, const char __user
*, newname
, unsigned int, flags
)
4244 struct dentry
*old_dir
, *new_dir
;
4245 struct dentry
*old_dentry
, *new_dentry
;
4246 struct dentry
*trap
;
4247 struct nameidata oldnd
, newnd
;
4248 struct inode
*delegated_inode
= NULL
;
4249 struct filename
*from
;
4250 struct filename
*to
;
4251 unsigned int lookup_flags
= 0;
4252 bool should_retry
= false;
4255 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
4258 if ((flags
& (RENAME_NOREPLACE
| RENAME_WHITEOUT
)) &&
4259 (flags
& RENAME_EXCHANGE
))
4262 if ((flags
& RENAME_WHITEOUT
) && !capable(CAP_MKNOD
))
4266 from
= user_path_parent(olddfd
, oldname
, &oldnd
, lookup_flags
);
4268 error
= PTR_ERR(from
);
4272 to
= user_path_parent(newdfd
, newname
, &newnd
, lookup_flags
);
4274 error
= PTR_ERR(to
);
4279 if (oldnd
.path
.mnt
!= newnd
.path
.mnt
)
4282 old_dir
= oldnd
.path
.dentry
;
4284 if (oldnd
.last_type
!= LAST_NORM
)
4287 new_dir
= newnd
.path
.dentry
;
4288 if (flags
& RENAME_NOREPLACE
)
4290 if (newnd
.last_type
!= LAST_NORM
)
4293 error
= mnt_want_write(oldnd
.path
.mnt
);
4297 oldnd
.flags
&= ~LOOKUP_PARENT
;
4298 newnd
.flags
&= ~LOOKUP_PARENT
;
4299 if (!(flags
& RENAME_EXCHANGE
))
4300 newnd
.flags
|= LOOKUP_RENAME_TARGET
;
4303 trap
= lock_rename(new_dir
, old_dir
);
4305 old_dentry
= lookup_hash(&oldnd
);
4306 error
= PTR_ERR(old_dentry
);
4307 if (IS_ERR(old_dentry
))
4309 /* source must exist */
4311 if (d_is_negative(old_dentry
))
4313 new_dentry
= lookup_hash(&newnd
);
4314 error
= PTR_ERR(new_dentry
);
4315 if (IS_ERR(new_dentry
))
4318 if ((flags
& RENAME_NOREPLACE
) && d_is_positive(new_dentry
))
4320 if (flags
& RENAME_EXCHANGE
) {
4322 if (d_is_negative(new_dentry
))
4325 if (!d_is_dir(new_dentry
)) {
4327 if (newnd
.last
.name
[newnd
.last
.len
])
4331 /* unless the source is a directory trailing slashes give -ENOTDIR */
4332 if (!d_is_dir(old_dentry
)) {
4334 if (oldnd
.last
.name
[oldnd
.last
.len
])
4336 if (!(flags
& RENAME_EXCHANGE
) && newnd
.last
.name
[newnd
.last
.len
])
4339 /* source should not be ancestor of target */
4341 if (old_dentry
== trap
)
4343 /* target should not be an ancestor of source */
4344 if (!(flags
& RENAME_EXCHANGE
))
4346 if (new_dentry
== trap
)
4349 error
= security_path_rename(&oldnd
.path
, old_dentry
,
4350 &newnd
.path
, new_dentry
, flags
);
4353 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
4354 new_dir
->d_inode
, new_dentry
,
4355 &delegated_inode
, flags
);
4361 unlock_rename(new_dir
, old_dir
);
4362 if (delegated_inode
) {
4363 error
= break_deleg_wait(&delegated_inode
);
4367 mnt_drop_write(oldnd
.path
.mnt
);
4369 if (retry_estale(error
, lookup_flags
))
4370 should_retry
= true;
4371 path_put(&newnd
.path
);
4374 path_put(&oldnd
.path
);
4377 should_retry
= false;
4378 lookup_flags
|= LOOKUP_REVAL
;
4385 SYSCALL_DEFINE4(renameat
, int, olddfd
, const char __user
*, oldname
,
4386 int, newdfd
, const char __user
*, newname
)
4388 return sys_renameat2(olddfd
, oldname
, newdfd
, newname
, 0);
4391 SYSCALL_DEFINE2(rename
, const char __user
*, oldname
, const char __user
*, newname
)
4393 return sys_renameat2(AT_FDCWD
, oldname
, AT_FDCWD
, newname
, 0);
4396 int vfs_whiteout(struct inode
*dir
, struct dentry
*dentry
)
4398 int error
= may_create(dir
, dentry
);
4402 if (!dir
->i_op
->mknod
)
4405 return dir
->i_op
->mknod(dir
, dentry
,
4406 S_IFCHR
| WHITEOUT_MODE
, WHITEOUT_DEV
);
4408 EXPORT_SYMBOL(vfs_whiteout
);
4410 int readlink_copy(char __user
*buffer
, int buflen
, const char *link
)
4412 int len
= PTR_ERR(link
);
4417 if (len
> (unsigned) buflen
)
4419 if (copy_to_user(buffer
, link
, len
))
4424 EXPORT_SYMBOL(readlink_copy
);
4427 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4428 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4429 * using) it for any given inode is up to filesystem.
4431 int generic_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
4433 struct nameidata nd
;
4438 cookie
= dentry
->d_inode
->i_op
->follow_link(dentry
, &nd
);
4440 return PTR_ERR(cookie
);
4442 res
= readlink_copy(buffer
, buflen
, nd_get_link(&nd
));
4443 if (dentry
->d_inode
->i_op
->put_link
)
4444 dentry
->d_inode
->i_op
->put_link(dentry
, &nd
, cookie
);
4447 EXPORT_SYMBOL(generic_readlink
);
4449 /* get the link contents into pagecache */
4450 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
4454 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
4455 page
= read_mapping_page(mapping
, 0, NULL
);
4460 nd_terminate_link(kaddr
, dentry
->d_inode
->i_size
, PAGE_SIZE
- 1);
4464 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
4466 struct page
*page
= NULL
;
4467 int res
= readlink_copy(buffer
, buflen
, page_getlink(dentry
, &page
));
4470 page_cache_release(page
);
4474 EXPORT_SYMBOL(page_readlink
);
4476 void *page_follow_link_light(struct dentry
*dentry
, struct nameidata
*nd
)
4478 struct page
*page
= NULL
;
4479 nd_set_link(nd
, page_getlink(dentry
, &page
));
4482 EXPORT_SYMBOL(page_follow_link_light
);
4484 void page_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *cookie
)
4486 struct page
*page
= cookie
;
4490 page_cache_release(page
);
4493 EXPORT_SYMBOL(page_put_link
);
4496 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4498 int __page_symlink(struct inode
*inode
, const char *symname
, int len
, int nofs
)
4500 struct address_space
*mapping
= inode
->i_mapping
;
4505 unsigned int flags
= AOP_FLAG_UNINTERRUPTIBLE
;
4507 flags
|= AOP_FLAG_NOFS
;
4510 err
= pagecache_write_begin(NULL
, mapping
, 0, len
-1,
4511 flags
, &page
, &fsdata
);
4515 kaddr
= kmap_atomic(page
);
4516 memcpy(kaddr
, symname
, len
-1);
4517 kunmap_atomic(kaddr
);
4519 err
= pagecache_write_end(NULL
, mapping
, 0, len
-1, len
-1,
4526 mark_inode_dirty(inode
);
4531 EXPORT_SYMBOL(__page_symlink
);
4533 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
4535 return __page_symlink(inode
, symname
, len
,
4536 !(mapping_gfp_mask(inode
->i_mapping
) & __GFP_FS
));
4538 EXPORT_SYMBOL(page_symlink
);
4540 const struct inode_operations page_symlink_inode_operations
= {
4541 .readlink
= generic_readlink
,
4542 .follow_link
= page_follow_link_light
,
4543 .put_link
= page_put_link
,
4545 EXPORT_SYMBOL(page_symlink_inode_operations
);