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))
133 static 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 audit_getname(result);
204 final_putname(result);
209 getname(const char __user * filename)
211 return getname_flags(filename, 0, NULL);
213 EXPORT_SYMBOL(getname);
215 #ifdef CONFIG_AUDITSYSCALL
216 void putname(struct filename *name)
218 if (unlikely(!audit_dummy_context()))
219 return audit_putname(name);
224 static int check_acl(struct inode *inode, int mask)
226 #ifdef CONFIG_FS_POSIX_ACL
227 struct posix_acl *acl;
229 if (mask & MAY_NOT_BLOCK) {
230 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
233 /* no ->get_acl() calls in RCU mode... */
234 if (acl == ACL_NOT_CACHED)
236 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
239 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
242 * A filesystem can force a ACL callback by just never filling the
243 * ACL cache. But normally you'd fill the cache either at inode
244 * instantiation time, or on the first ->get_acl call.
246 * If the filesystem doesn't have a get_acl() function at all, we'll
247 * just create the negative cache entry.
249 if (acl == ACL_NOT_CACHED) {
250 if (inode->i_op->get_acl) {
251 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
255 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
261 int error = posix_acl_permission(inode, acl, mask);
262 posix_acl_release(acl);
271 * This does the basic permission checking
273 static int acl_permission_check(struct inode *inode, int mask)
275 unsigned int mode = inode->i_mode;
277 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
280 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
281 int error = check_acl(inode, mask);
282 if (error != -EAGAIN)
286 if (in_group_p(inode->i_gid))
291 * If the DACs are ok we don't need any capability check.
293 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
299 * generic_permission - check for access rights on a Posix-like filesystem
300 * @inode: inode to check access rights for
301 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
303 * Used to check for read/write/execute permissions on a file.
304 * We use "fsuid" for this, letting us set arbitrary permissions
305 * for filesystem access without changing the "normal" uids which
306 * are used for other things.
308 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
309 * request cannot be satisfied (eg. requires blocking or too much complexity).
310 * It would then be called again in ref-walk mode.
312 int generic_permission(struct inode *inode, int mask)
317 * Do the basic permission checks.
319 ret = acl_permission_check(inode, mask);
323 if (S_ISDIR(inode->i_mode)) {
324 /* DACs are overridable for directories */
325 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
327 if (!(mask & MAY_WRITE))
328 if (capable_wrt_inode_uidgid(inode,
329 CAP_DAC_READ_SEARCH))
334 * Read/write DACs are always overridable.
335 * Executable DACs are overridable when there is
336 * at least one exec bit set.
338 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
339 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
343 * Searching includes executable on directories, else just read.
345 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
346 if (mask == MAY_READ)
347 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
354 * We _really_ want to just do "generic_permission()" without
355 * even looking at the inode->i_op values. So we keep a cache
356 * flag in inode->i_opflags, that says "this has not special
357 * permission function, use the fast case".
359 static inline int do_inode_permission(struct inode *inode, int mask)
361 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
362 if (likely(inode->i_op->permission))
363 return inode->i_op->permission(inode, mask);
365 /* This gets set once for the inode lifetime */
366 spin_lock(&inode->i_lock);
367 inode->i_opflags |= IOP_FASTPERM;
368 spin_unlock(&inode->i_lock);
370 return generic_permission(inode, mask);
374 * __inode_permission - Check for access rights to a given inode
375 * @inode: Inode to check permission on
376 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
378 * Check for read/write/execute permissions on an inode.
380 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
382 * This does not check for a read-only file system. You probably want
383 * inode_permission().
385 int __inode_permission(struct inode *inode, int mask)
389 if (unlikely(mask & MAY_WRITE)) {
391 * Nobody gets write access to an immutable file.
393 if (IS_IMMUTABLE(inode))
397 retval = do_inode_permission(inode, mask);
401 retval = devcgroup_inode_permission(inode, mask);
405 return security_inode_permission(inode, mask);
409 * sb_permission - Check superblock-level permissions
410 * @sb: Superblock of inode to check permission on
411 * @inode: Inode to check permission on
412 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
414 * Separate out file-system wide checks from inode-specific permission checks.
416 static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
418 if (unlikely(mask & MAY_WRITE)) {
419 umode_t mode = inode->i_mode;
421 /* Nobody gets write access to a read-only fs. */
422 if ((sb->s_flags & MS_RDONLY) &&
423 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
430 * inode_permission - Check for access rights to a given inode
431 * @inode: Inode to check permission on
432 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
434 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
435 * this, letting us set arbitrary permissions for filesystem access without
436 * changing the "normal" UIDs which are used for other things.
438 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
440 int inode_permission(struct inode *inode, int mask)
444 retval = sb_permission(inode->i_sb, inode, mask);
447 return __inode_permission(inode, mask);
451 * path_get - get a reference to a path
452 * @path: path to get the reference to
454 * Given a path increment the reference count to the dentry and the vfsmount.
456 void path_get(const struct path *path)
461 EXPORT_SYMBOL(path_get);
464 * path_put - put a reference to a path
465 * @path: path to put the reference to
467 * Given a path decrement the reference count to the dentry and the vfsmount.
469 void path_put(const struct path *path)
474 EXPORT_SYMBOL(path_put);
477 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
478 * @path: nameidate to verify
480 * Rename can sometimes move a file or directory outside of a bind
481 * mount, path_connected allows those cases to be detected.
483 static bool path_connected(const struct path *path)
485 struct vfsmount *mnt = path->mnt;
487 /* Only bind mounts can have disconnected paths */
488 if (mnt->mnt_root == mnt->mnt_sb->s_root)
491 return is_subdir(path->dentry, mnt->mnt_root);
495 * Path walking has 2 modes, rcu-walk and ref-walk (see
496 * Documentation/filesystems/path-lookup.txt). In situations when we can't
497 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
498 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
499 * mode. Refcounts are grabbed at the last known good point before rcu-walk
500 * got stuck, so ref-walk may continue from there. If this is not successful
501 * (eg. a seqcount has changed), then failure is returned and it's up to caller
502 * to restart the path walk from the beginning in ref-walk mode.
505 static inline void lock_rcu_walk(void)
507 br_read_lock(&vfsmount_lock);
511 static inline void unlock_rcu_walk(void)
514 br_read_unlock(&vfsmount_lock);
518 * unlazy_walk - try to switch to ref-walk mode.
519 * @nd: nameidata pathwalk data
520 * @dentry: child of nd->path.dentry or NULL
521 * Returns: 0 on success, -ECHILD on failure
523 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
524 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
525 * @nd or NULL. Must be called from rcu-walk context.
527 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
529 struct fs_struct *fs = current->fs;
530 struct dentry *parent = nd->path.dentry;
533 BUG_ON(!(nd->flags & LOOKUP_RCU));
534 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
536 spin_lock(&fs->lock);
537 if (nd->root.mnt != fs->root.mnt ||
538 nd->root.dentry != fs->root.dentry)
541 spin_lock(&parent->d_lock);
543 if (!__d_rcu_to_refcount(parent, nd->seq))
545 BUG_ON(nd->inode != parent->d_inode);
547 if (dentry->d_parent != parent)
549 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
550 if (!__d_rcu_to_refcount(dentry, nd->seq))
553 * If the sequence check on the child dentry passed, then
554 * the child has not been removed from its parent. This
555 * means the parent dentry must be valid and able to take
556 * a reference at this point.
558 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
559 BUG_ON(!parent->d_count);
561 spin_unlock(&dentry->d_lock);
563 spin_unlock(&parent->d_lock);
566 spin_unlock(&fs->lock);
568 mntget(nd->path.mnt);
571 nd->flags &= ~LOOKUP_RCU;
575 spin_unlock(&dentry->d_lock);
577 spin_unlock(&parent->d_lock);
580 spin_unlock(&fs->lock);
584 static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
586 return dentry->d_op->d_revalidate(dentry, flags);
590 * complete_walk - successful completion of path walk
591 * @nd: pointer nameidata
593 * If we had been in RCU mode, drop out of it and legitimize nd->path.
594 * Revalidate the final result, unless we'd already done that during
595 * the path walk or the filesystem doesn't ask for it. Return 0 on
596 * success, -error on failure. In case of failure caller does not
597 * need to drop nd->path.
599 static int complete_walk(struct nameidata *nd)
601 struct dentry *dentry = nd->path.dentry;
604 if (nd->flags & LOOKUP_RCU) {
605 nd->flags &= ~LOOKUP_RCU;
606 if (!(nd->flags & LOOKUP_ROOT))
608 spin_lock(&dentry->d_lock);
609 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
610 spin_unlock(&dentry->d_lock);
614 BUG_ON(nd->inode != dentry->d_inode);
615 spin_unlock(&dentry->d_lock);
616 mntget(nd->path.mnt);
620 if (likely(!(nd->flags & LOOKUP_JUMPED)))
623 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
626 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
637 static __always_inline void set_root(struct nameidata *nd)
640 get_fs_root(current->fs, &nd->root);
643 static int link_path_walk(const char *, struct nameidata *);
645 static __always_inline void set_root_rcu(struct nameidata *nd)
648 struct fs_struct *fs = current->fs;
652 seq = read_seqcount_begin(&fs->seq);
654 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
655 } while (read_seqcount_retry(&fs->seq, seq));
659 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
671 nd->flags |= LOOKUP_JUMPED;
673 nd->inode = nd->path.dentry->d_inode;
675 ret = link_path_walk(link, nd);
679 return PTR_ERR(link);
682 static void path_put_conditional(struct path *path, struct nameidata *nd)
685 if (path->mnt != nd->path.mnt)
689 static inline void path_to_nameidata(const struct path *path,
690 struct nameidata *nd)
692 if (!(nd->flags & LOOKUP_RCU)) {
693 dput(nd->path.dentry);
694 if (nd->path.mnt != path->mnt)
695 mntput(nd->path.mnt);
697 nd->path.mnt = path->mnt;
698 nd->path.dentry = path->dentry;
702 * Helper to directly jump to a known parsed path from ->follow_link,
703 * caller must have taken a reference to path beforehand.
705 void nd_jump_link(struct nameidata *nd, struct path *path)
710 nd->inode = nd->path.dentry->d_inode;
711 nd->flags |= LOOKUP_JUMPED;
714 static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
716 struct inode *inode = link->dentry->d_inode;
717 if (inode->i_op->put_link)
718 inode->i_op->put_link(link->dentry, nd, cookie);
722 int sysctl_protected_symlinks __read_mostly = 0;
723 int sysctl_protected_hardlinks __read_mostly = 0;
726 * may_follow_link - Check symlink following for unsafe situations
727 * @link: The path of the symlink
728 * @nd: nameidata pathwalk data
730 * In the case of the sysctl_protected_symlinks sysctl being enabled,
731 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
732 * in a sticky world-writable directory. This is to protect privileged
733 * processes from failing races against path names that may change out
734 * from under them by way of other users creating malicious symlinks.
735 * It will permit symlinks to be followed only when outside a sticky
736 * world-writable directory, or when the uid of the symlink and follower
737 * match, or when the directory owner matches the symlink's owner.
739 * Returns 0 if following the symlink is allowed, -ve on error.
741 static inline int may_follow_link(struct path *link, struct nameidata *nd)
743 const struct inode *inode;
744 const struct inode *parent;
746 if (!sysctl_protected_symlinks)
749 /* Allowed if owner and follower match. */
750 inode = link->dentry->d_inode;
751 if (uid_eq(current_cred()->fsuid, inode->i_uid))
754 /* Allowed if parent directory not sticky and world-writable. */
755 parent = nd->path.dentry->d_inode;
756 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
759 /* Allowed if parent directory and link owner match. */
760 if (uid_eq(parent->i_uid, inode->i_uid))
763 audit_log_link_denied("follow_link", link);
764 path_put_conditional(link, nd);
770 * safe_hardlink_source - Check for safe hardlink conditions
771 * @inode: the source inode to hardlink from
773 * Return false if at least one of the following conditions:
774 * - inode is not a regular file
776 * - inode is setgid and group-exec
777 * - access failure for read and write
779 * Otherwise returns true.
781 static bool safe_hardlink_source(struct inode *inode)
783 umode_t mode = inode->i_mode;
785 /* Special files should not get pinned to the filesystem. */
789 /* Setuid files should not get pinned to the filesystem. */
793 /* Executable setgid files should not get pinned to the filesystem. */
794 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
797 /* Hardlinking to unreadable or unwritable sources is dangerous. */
798 if (inode_permission(inode, MAY_READ | MAY_WRITE))
805 * may_linkat - Check permissions for creating a hardlink
806 * @link: the source to hardlink from
808 * Block hardlink when all of:
809 * - sysctl_protected_hardlinks enabled
810 * - fsuid does not match inode
811 * - hardlink source is unsafe (see safe_hardlink_source() above)
814 * Returns 0 if successful, -ve on error.
816 static int may_linkat(struct path *link)
818 const struct cred *cred;
821 if (!sysctl_protected_hardlinks)
824 cred = current_cred();
825 inode = link->dentry->d_inode;
827 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
828 * otherwise, it must be a safe source.
830 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
834 audit_log_link_denied("linkat", link);
838 static __always_inline int
839 follow_link(struct path *link, struct nameidata *nd, void **p)
841 struct dentry *dentry = link->dentry;
845 BUG_ON(nd->flags & LOOKUP_RCU);
847 if (link->mnt == nd->path.mnt)
851 if (unlikely(current->total_link_count >= 40))
852 goto out_put_nd_path;
855 current->total_link_count++;
858 nd_set_link(nd, NULL);
860 error = security_inode_follow_link(link->dentry, nd);
862 goto out_put_nd_path;
864 nd->last_type = LAST_BIND;
865 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
868 goto out_put_nd_path;
873 error = __vfs_follow_link(nd, s);
875 put_link(nd, link, *p);
887 static int follow_up_rcu(struct path *path)
889 struct mount *mnt = real_mount(path->mnt);
890 struct mount *parent;
891 struct dentry *mountpoint;
893 parent = mnt->mnt_parent;
894 if (&parent->mnt == path->mnt)
896 mountpoint = mnt->mnt_mountpoint;
897 path->dentry = mountpoint;
898 path->mnt = &parent->mnt;
903 * follow_up - Find the mountpoint of path's vfsmount
905 * Given a path, find the mountpoint of its source file system.
906 * Replace @path with the path of the mountpoint in the parent mount.
909 * Return 1 if we went up a level and 0 if we were already at the
912 int follow_up(struct path *path)
914 struct mount *mnt = real_mount(path->mnt);
915 struct mount *parent;
916 struct dentry *mountpoint;
918 br_read_lock(&vfsmount_lock);
919 parent = mnt->mnt_parent;
921 br_read_unlock(&vfsmount_lock);
924 mntget(&parent->mnt);
925 mountpoint = dget(mnt->mnt_mountpoint);
926 br_read_unlock(&vfsmount_lock);
928 path->dentry = mountpoint;
930 path->mnt = &parent->mnt;
935 * Perform an automount
936 * - return -EISDIR to tell follow_managed() to stop and return the path we
939 static int follow_automount(struct path *path, unsigned flags,
942 struct vfsmount *mnt;
945 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
948 /* We don't want to mount if someone's just doing a stat -
949 * unless they're stat'ing a directory and appended a '/' to
952 * We do, however, want to mount if someone wants to open or
953 * create a file of any type under the mountpoint, wants to
954 * traverse through the mountpoint or wants to open the
955 * mounted directory. Also, autofs may mark negative dentries
956 * as being automount points. These will need the attentions
957 * of the daemon to instantiate them before they can be used.
959 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
960 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
961 path->dentry->d_inode)
964 current->total_link_count++;
965 if (current->total_link_count >= 40)
968 mnt = path->dentry->d_op->d_automount(path);
971 * The filesystem is allowed to return -EISDIR here to indicate
972 * it doesn't want to automount. For instance, autofs would do
973 * this so that its userspace daemon can mount on this dentry.
975 * However, we can only permit this if it's a terminal point in
976 * the path being looked up; if it wasn't then the remainder of
977 * the path is inaccessible and we should say so.
979 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
984 if (!mnt) /* mount collision */
988 /* lock_mount() may release path->mnt on error */
992 err = finish_automount(mnt, path);
996 /* Someone else made a mount here whilst we were busy */
1001 path->dentry = dget(mnt->mnt_root);
1010 * Handle a dentry that is managed in some way.
1011 * - Flagged for transit management (autofs)
1012 * - Flagged as mountpoint
1013 * - Flagged as automount point
1015 * This may only be called in refwalk mode.
1017 * Serialization is taken care of in namespace.c
1019 static int follow_managed(struct path *path, unsigned flags)
1021 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
1023 bool need_mntput = false;
1026 /* Given that we're not holding a lock here, we retain the value in a
1027 * local variable for each dentry as we look at it so that we don't see
1028 * the components of that value change under us */
1029 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1030 managed &= DCACHE_MANAGED_DENTRY,
1031 unlikely(managed != 0)) {
1032 /* Allow the filesystem to manage the transit without i_mutex
1034 if (managed & DCACHE_MANAGE_TRANSIT) {
1035 BUG_ON(!path->dentry->d_op);
1036 BUG_ON(!path->dentry->d_op->d_manage);
1037 ret = path->dentry->d_op->d_manage(path->dentry, false);
1042 /* Transit to a mounted filesystem. */
1043 if (managed & DCACHE_MOUNTED) {
1044 struct vfsmount *mounted = lookup_mnt(path);
1049 path->mnt = mounted;
1050 path->dentry = dget(mounted->mnt_root);
1055 /* Something is mounted on this dentry in another
1056 * namespace and/or whatever was mounted there in this
1057 * namespace got unmounted before we managed to get the
1061 /* Handle an automount point */
1062 if (managed & DCACHE_NEED_AUTOMOUNT) {
1063 ret = follow_automount(path, flags, &need_mntput);
1069 /* We didn't change the current path point */
1073 if (need_mntput && path->mnt == mnt)
1077 return ret < 0 ? ret : need_mntput;
1080 int follow_down_one(struct path *path)
1082 struct vfsmount *mounted;
1084 mounted = lookup_mnt(path);
1088 path->mnt = mounted;
1089 path->dentry = dget(mounted->mnt_root);
1095 static inline bool managed_dentry_might_block(struct dentry *dentry)
1097 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1098 dentry->d_op->d_manage(dentry, true) < 0);
1102 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1103 * we meet a managed dentry that would need blocking.
1105 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1106 struct inode **inode)
1109 struct mount *mounted;
1111 * Don't forget we might have a non-mountpoint managed dentry
1112 * that wants to block transit.
1114 if (unlikely(managed_dentry_might_block(path->dentry)))
1117 if (!d_mountpoint(path->dentry))
1120 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1123 path->mnt = &mounted->mnt;
1124 path->dentry = mounted->mnt.mnt_root;
1125 nd->flags |= LOOKUP_JUMPED;
1126 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
1128 * Update the inode too. We don't need to re-check the
1129 * dentry sequence number here after this d_inode read,
1130 * because a mount-point is always pinned.
1132 *inode = path->dentry->d_inode;
1137 static void follow_mount_rcu(struct nameidata *nd)
1139 while (d_mountpoint(nd->path.dentry)) {
1140 struct mount *mounted;
1141 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
1144 nd->path.mnt = &mounted->mnt;
1145 nd->path.dentry = mounted->mnt.mnt_root;
1146 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1150 static int follow_dotdot_rcu(struct nameidata *nd)
1155 if (nd->path.dentry == nd->root.dentry &&
1156 nd->path.mnt == nd->root.mnt) {
1159 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1160 struct dentry *old = nd->path.dentry;
1161 struct dentry *parent = old->d_parent;
1164 seq = read_seqcount_begin(&parent->d_seq);
1165 if (read_seqcount_retry(&old->d_seq, nd->seq))
1167 nd->path.dentry = parent;
1169 if (unlikely(!path_connected(&nd->path)))
1173 if (!follow_up_rcu(&nd->path))
1175 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1177 follow_mount_rcu(nd);
1178 nd->inode = nd->path.dentry->d_inode;
1182 nd->flags &= ~LOOKUP_RCU;
1183 if (!(nd->flags & LOOKUP_ROOT))
1184 nd->root.mnt = NULL;
1190 * Follow down to the covering mount currently visible to userspace. At each
1191 * point, the filesystem owning that dentry may be queried as to whether the
1192 * caller is permitted to proceed or not.
1194 int follow_down(struct path *path)
1199 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1200 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1201 /* Allow the filesystem to manage the transit without i_mutex
1204 * We indicate to the filesystem if someone is trying to mount
1205 * something here. This gives autofs the chance to deny anyone
1206 * other than its daemon the right to mount on its
1209 * The filesystem may sleep at this point.
1211 if (managed & DCACHE_MANAGE_TRANSIT) {
1212 BUG_ON(!path->dentry->d_op);
1213 BUG_ON(!path->dentry->d_op->d_manage);
1214 ret = path->dentry->d_op->d_manage(
1215 path->dentry, false);
1217 return ret == -EISDIR ? 0 : ret;
1220 /* Transit to a mounted filesystem. */
1221 if (managed & DCACHE_MOUNTED) {
1222 struct vfsmount *mounted = lookup_mnt(path);
1227 path->mnt = mounted;
1228 path->dentry = dget(mounted->mnt_root);
1232 /* Don't handle automount points here */
1239 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1241 static void follow_mount(struct path *path)
1243 while (d_mountpoint(path->dentry)) {
1244 struct vfsmount *mounted = lookup_mnt(path);
1249 path->mnt = mounted;
1250 path->dentry = dget(mounted->mnt_root);
1254 static int follow_dotdot(struct nameidata *nd)
1259 struct dentry *old = nd->path.dentry;
1261 if (nd->path.dentry == nd->root.dentry &&
1262 nd->path.mnt == nd->root.mnt) {
1265 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1266 /* rare case of legitimate dget_parent()... */
1267 nd->path.dentry = dget_parent(nd->path.dentry);
1269 if (unlikely(!path_connected(&nd->path))) {
1270 path_put(&nd->path);
1275 if (!follow_up(&nd->path))
1278 follow_mount(&nd->path);
1279 nd->inode = nd->path.dentry->d_inode;
1284 * This looks up the name in dcache, possibly revalidates the old dentry and
1285 * allocates a new one if not found or not valid. In the need_lookup argument
1286 * returns whether i_op->lookup is necessary.
1288 * dir->d_inode->i_mutex must be held
1290 static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1291 unsigned int flags, bool *need_lookup)
1293 struct dentry *dentry;
1296 *need_lookup = false;
1297 dentry = d_lookup(dir, name);
1299 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1300 error = d_revalidate(dentry, flags);
1301 if (unlikely(error <= 0)) {
1304 return ERR_PTR(error);
1305 } else if (!d_invalidate(dentry)) {
1314 dentry = d_alloc(dir, name);
1315 if (unlikely(!dentry))
1316 return ERR_PTR(-ENOMEM);
1318 *need_lookup = true;
1324 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1325 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1327 * dir->d_inode->i_mutex must be held
1329 static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1334 /* Don't create child dentry for a dead directory. */
1335 if (unlikely(IS_DEADDIR(dir))) {
1337 return ERR_PTR(-ENOENT);
1340 old = dir->i_op->lookup(dir, dentry, flags);
1341 if (unlikely(old)) {
1348 static struct dentry *__lookup_hash(struct qstr *name,
1349 struct dentry *base, unsigned int flags)
1352 struct dentry *dentry;
1354 dentry = lookup_dcache(name, base, flags, &need_lookup);
1358 return lookup_real(base->d_inode, dentry, flags);
1362 * It's more convoluted than I'd like it to be, but... it's still fairly
1363 * small and for now I'd prefer to have fast path as straight as possible.
1364 * It _is_ time-critical.
1366 static int lookup_fast(struct nameidata *nd,
1367 struct path *path, struct inode **inode)
1369 struct vfsmount *mnt = nd->path.mnt;
1370 struct dentry *dentry, *parent = nd->path.dentry;
1376 * Rename seqlock is not required here because in the off chance
1377 * of a false negative due to a concurrent rename, we're going to
1378 * do the non-racy lookup, below.
1380 if (nd->flags & LOOKUP_RCU) {
1382 dentry = __d_lookup_rcu(parent, &nd->last, &seq, nd->inode);
1387 * This sequence count validates that the inode matches
1388 * the dentry name information from lookup.
1390 *inode = dentry->d_inode;
1391 if (read_seqcount_retry(&dentry->d_seq, seq))
1395 * This sequence count validates that the parent had no
1396 * changes while we did the lookup of the dentry above.
1398 * The memory barrier in read_seqcount_begin of child is
1399 * enough, we can use __read_seqcount_retry here.
1401 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1405 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1406 status = d_revalidate(dentry, nd->flags);
1407 if (unlikely(status <= 0)) {
1408 if (status != -ECHILD)
1414 path->dentry = dentry;
1415 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1417 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1421 if (unlazy_walk(nd, dentry))
1424 dentry = __d_lookup(parent, &nd->last);
1427 if (unlikely(!dentry))
1430 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1431 status = d_revalidate(dentry, nd->flags);
1432 if (unlikely(status <= 0)) {
1437 if (!d_invalidate(dentry)) {
1444 path->dentry = dentry;
1445 err = follow_managed(path, nd->flags);
1446 if (unlikely(err < 0)) {
1447 path_put_conditional(path, nd);
1451 nd->flags |= LOOKUP_JUMPED;
1452 *inode = path->dentry->d_inode;
1459 /* Fast lookup failed, do it the slow way */
1460 static int lookup_slow(struct nameidata *nd, struct path *path)
1462 struct dentry *dentry, *parent;
1465 parent = nd->path.dentry;
1466 BUG_ON(nd->inode != parent->d_inode);
1468 mutex_lock(&parent->d_inode->i_mutex);
1469 dentry = __lookup_hash(&nd->last, parent, nd->flags);
1470 mutex_unlock(&parent->d_inode->i_mutex);
1472 return PTR_ERR(dentry);
1473 path->mnt = nd->path.mnt;
1474 path->dentry = dentry;
1475 err = follow_managed(path, nd->flags);
1476 if (unlikely(err < 0)) {
1477 path_put_conditional(path, nd);
1481 nd->flags |= LOOKUP_JUMPED;
1485 static inline int may_lookup(struct nameidata *nd)
1487 if (nd->flags & LOOKUP_RCU) {
1488 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1491 if (unlazy_walk(nd, NULL))
1494 return inode_permission(nd->inode, MAY_EXEC);
1497 static inline int handle_dots(struct nameidata *nd, int type)
1499 if (type == LAST_DOTDOT) {
1500 if (nd->flags & LOOKUP_RCU) {
1501 if (follow_dotdot_rcu(nd))
1504 return follow_dotdot(nd);
1509 static void terminate_walk(struct nameidata *nd)
1511 if (!(nd->flags & LOOKUP_RCU)) {
1512 path_put(&nd->path);
1514 nd->flags &= ~LOOKUP_RCU;
1515 if (!(nd->flags & LOOKUP_ROOT))
1516 nd->root.mnt = NULL;
1522 * Do we need to follow links? We _really_ want to be able
1523 * to do this check without having to look at inode->i_op,
1524 * so we keep a cache of "no, this doesn't need follow_link"
1525 * for the common case.
1527 static inline int should_follow_link(struct inode *inode, int follow)
1529 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1530 if (likely(inode->i_op->follow_link))
1533 /* This gets set once for the inode lifetime */
1534 spin_lock(&inode->i_lock);
1535 inode->i_opflags |= IOP_NOFOLLOW;
1536 spin_unlock(&inode->i_lock);
1541 static inline int walk_component(struct nameidata *nd, struct path *path,
1544 struct inode *inode;
1547 * "." and ".." are special - ".." especially so because it has
1548 * to be able to know about the current root directory and
1549 * parent relationships.
1551 if (unlikely(nd->last_type != LAST_NORM))
1552 return handle_dots(nd, nd->last_type);
1553 err = lookup_fast(nd, path, &inode);
1554 if (unlikely(err)) {
1558 err = lookup_slow(nd, path);
1562 inode = path->dentry->d_inode;
1568 if (should_follow_link(inode, follow)) {
1569 if (nd->flags & LOOKUP_RCU) {
1570 if (unlikely(nd->path.mnt != path->mnt ||
1571 unlazy_walk(nd, path->dentry))) {
1576 BUG_ON(inode != path->dentry->d_inode);
1579 path_to_nameidata(path, nd);
1584 path_to_nameidata(path, nd);
1591 * This limits recursive symlink follows to 8, while
1592 * limiting consecutive symlinks to 40.
1594 * Without that kind of total limit, nasty chains of consecutive
1595 * symlinks can cause almost arbitrarily long lookups.
1597 static inline int nested_symlink(struct path *path, struct nameidata *nd)
1601 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1602 path_put_conditional(path, nd);
1603 path_put(&nd->path);
1606 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1609 current->link_count++;
1612 struct path link = *path;
1615 res = follow_link(&link, nd, &cookie);
1618 res = walk_component(nd, path, LOOKUP_FOLLOW);
1619 put_link(nd, &link, cookie);
1622 current->link_count--;
1628 * We really don't want to look at inode->i_op->lookup
1629 * when we don't have to. So we keep a cache bit in
1630 * the inode ->i_opflags field that says "yes, we can
1631 * do lookup on this inode".
1633 static inline int can_lookup(struct inode *inode)
1635 if (likely(inode->i_opflags & IOP_LOOKUP))
1637 if (likely(!inode->i_op->lookup))
1640 /* We do this once for the lifetime of the inode */
1641 spin_lock(&inode->i_lock);
1642 inode->i_opflags |= IOP_LOOKUP;
1643 spin_unlock(&inode->i_lock);
1648 * We can do the critical dentry name comparison and hashing
1649 * operations one word at a time, but we are limited to:
1651 * - Architectures with fast unaligned word accesses. We could
1652 * do a "get_unaligned()" if this helps and is sufficiently
1655 * - Little-endian machines (so that we can generate the mask
1656 * of low bytes efficiently). Again, we *could* do a byte
1657 * swapping load on big-endian architectures if that is not
1658 * expensive enough to make the optimization worthless.
1660 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1661 * do not trap on the (extremely unlikely) case of a page
1662 * crossing operation.
1664 * - Furthermore, we need an efficient 64-bit compile for the
1665 * 64-bit case in order to generate the "number of bytes in
1666 * the final mask". Again, that could be replaced with a
1667 * efficient population count instruction or similar.
1669 #ifdef CONFIG_DCACHE_WORD_ACCESS
1671 #include <asm/word-at-a-time.h>
1675 static inline unsigned int fold_hash(unsigned long hash)
1677 return hash_64(hash, 32);
1680 #else /* 32-bit case */
1682 #define fold_hash(x) (x)
1686 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1688 unsigned long a, mask;
1689 unsigned long hash = 0;
1692 a = load_unaligned_zeropad(name);
1693 if (len < sizeof(unsigned long))
1697 name += sizeof(unsigned long);
1698 len -= sizeof(unsigned long);
1702 mask = ~(~0ul << len*8);
1705 return fold_hash(hash);
1707 EXPORT_SYMBOL(full_name_hash);
1710 * Calculate the length and hash of the path component, and
1711 * return the length of the component;
1713 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1715 unsigned long a, b, adata, bdata, mask, hash, len;
1716 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1719 len = -sizeof(unsigned long);
1721 hash = (hash + a) * 9;
1722 len += sizeof(unsigned long);
1723 a = load_unaligned_zeropad(name+len);
1724 b = a ^ REPEAT_BYTE('/');
1725 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1727 adata = prep_zero_mask(a, adata, &constants);
1728 bdata = prep_zero_mask(b, bdata, &constants);
1730 mask = create_zero_mask(adata | bdata);
1732 hash += a & zero_bytemask(mask);
1733 *hashp = fold_hash(hash);
1735 return len + find_zero(mask);
1740 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1742 unsigned long hash = init_name_hash();
1744 hash = partial_name_hash(*name++, hash);
1745 return end_name_hash(hash);
1747 EXPORT_SYMBOL(full_name_hash);
1750 * We know there's a real path component here of at least
1753 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1755 unsigned long hash = init_name_hash();
1756 unsigned long len = 0, c;
1758 c = (unsigned char)*name;
1761 hash = partial_name_hash(c, hash);
1762 c = (unsigned char)name[len];
1763 } while (c && c != '/');
1764 *hashp = end_name_hash(hash);
1772 * This is the basic name resolution function, turning a pathname into
1773 * the final dentry. We expect 'base' to be positive and a directory.
1775 * Returns 0 and nd will have valid dentry and mnt on success.
1776 * Returns error and drops reference to input namei data on failure.
1778 static int link_path_walk(const char *name, struct nameidata *nd)
1788 /* At this point we know we have a real path component. */
1794 err = may_lookup(nd);
1798 len = hash_name(name, &this.hash);
1803 if (name[0] == '.') switch (len) {
1805 if (name[1] == '.') {
1807 nd->flags |= LOOKUP_JUMPED;
1813 if (likely(type == LAST_NORM)) {
1814 struct dentry *parent = nd->path.dentry;
1815 nd->flags &= ~LOOKUP_JUMPED;
1816 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1817 err = parent->d_op->d_hash(parent, nd->inode,
1825 nd->last_type = type;
1830 * If it wasn't NUL, we know it was '/'. Skip that
1831 * slash, and continue until no more slashes.
1835 } while (unlikely(name[len] == '/'));
1841 err = walk_component(nd, &next, LOOKUP_FOLLOW);
1846 err = nested_symlink(&next, nd);
1850 if (!can_lookup(nd->inode)) {
1859 static int path_init(int dfd, const char *name, unsigned int flags,
1860 struct nameidata *nd, struct file **fp)
1864 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1865 nd->flags = flags | LOOKUP_JUMPED;
1867 if (flags & LOOKUP_ROOT) {
1868 struct inode *inode = nd->root.dentry->d_inode;
1870 if (!can_lookup(inode))
1872 retval = inode_permission(inode, MAY_EXEC);
1876 nd->path = nd->root;
1878 if (flags & LOOKUP_RCU) {
1880 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1882 path_get(&nd->path);
1887 nd->root.mnt = NULL;
1890 if (flags & LOOKUP_RCU) {
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 (!can_lookup(dentry->d_inode)) {
1930 nd->path = f.file->f_path;
1931 if (flags & LOOKUP_RCU) {
1934 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1937 path_get(&nd->path);
1942 nd->inode = nd->path.dentry->d_inode;
1946 static inline int lookup_last(struct nameidata *nd, struct path *path)
1948 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1949 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1951 nd->flags &= ~LOOKUP_PARENT;
1952 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
1955 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1956 static int path_lookupat(int dfd, const char *name,
1957 unsigned int flags, struct nameidata *nd)
1959 struct file *base = NULL;
1964 * Path walking is largely split up into 2 different synchronisation
1965 * schemes, rcu-walk and ref-walk (explained in
1966 * Documentation/filesystems/path-lookup.txt). These share much of the
1967 * path walk code, but some things particularly setup, cleanup, and
1968 * following mounts are sufficiently divergent that functions are
1969 * duplicated. Typically there is a function foo(), and its RCU
1970 * analogue, foo_rcu().
1972 * -ECHILD is the error number of choice (just to avoid clashes) that
1973 * is returned if some aspect of an rcu-walk fails. Such an error must
1974 * be handled by restarting a traditional ref-walk (which will always
1975 * be able to complete).
1977 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1982 current->total_link_count = 0;
1983 err = link_path_walk(name, nd);
1985 if (!err && !(flags & LOOKUP_PARENT)) {
1986 err = lookup_last(nd, &path);
1989 struct path link = path;
1990 err = may_follow_link(&link, nd);
1993 nd->flags |= LOOKUP_PARENT;
1994 err = follow_link(&link, nd, &cookie);
1997 err = lookup_last(nd, &path);
1998 put_link(nd, &link, cookie);
2003 err = complete_walk(nd);
2005 if (!err && nd->flags & LOOKUP_DIRECTORY) {
2006 if (!can_lookup(nd->inode)) {
2007 path_put(&nd->path);
2015 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
2016 path_put(&nd->root);
2017 nd->root.mnt = NULL;
2022 static int filename_lookup(int dfd, struct filename *name,
2023 unsigned int flags, struct nameidata *nd)
2025 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
2026 if (unlikely(retval == -ECHILD))
2027 retval = path_lookupat(dfd, name->name, flags, nd);
2028 if (unlikely(retval == -ESTALE))
2029 retval = path_lookupat(dfd, name->name,
2030 flags | LOOKUP_REVAL, nd);
2032 if (likely(!retval))
2033 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
2037 static int do_path_lookup(int dfd, const char *name,
2038 unsigned int flags, struct nameidata *nd)
2040 struct filename filename = { .name = name };
2042 return filename_lookup(dfd, &filename, flags, nd);
2045 /* does lookup, returns the object with parent locked */
2046 struct dentry *kern_path_locked(const char *name, struct path *path)
2048 struct nameidata nd;
2050 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2052 return ERR_PTR(err);
2053 if (nd.last_type != LAST_NORM) {
2055 return ERR_PTR(-EINVAL);
2057 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2058 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
2060 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2068 int kern_path(const char *name, unsigned int flags, struct path *path)
2070 struct nameidata nd;
2071 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2078 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2079 * @dentry: pointer to dentry of the base directory
2080 * @mnt: pointer to vfs mount of the base directory
2081 * @name: pointer to file name
2082 * @flags: lookup flags
2083 * @path: pointer to struct path to fill
2085 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2086 const char *name, unsigned int flags,
2089 struct nameidata nd;
2091 nd.root.dentry = dentry;
2093 BUG_ON(flags & LOOKUP_PARENT);
2094 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2095 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2102 * Restricted form of lookup. Doesn't follow links, single-component only,
2103 * needs parent already locked. Doesn't follow mounts.
2106 static struct dentry *lookup_hash(struct nameidata *nd)
2108 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
2112 * lookup_one_len - filesystem helper to lookup single pathname component
2113 * @name: pathname component to lookup
2114 * @base: base directory to lookup from
2115 * @len: maximum length @len should be interpreted to
2117 * Note that this routine is purely a helper for filesystem usage and should
2118 * not be called by generic code. Also note that by using this function the
2119 * nameidata argument is passed to the filesystem methods and a filesystem
2120 * using this helper needs to be prepared for that.
2122 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2128 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2132 this.hash = full_name_hash(name, len);
2134 return ERR_PTR(-EACCES);
2136 if (unlikely(name[0] == '.')) {
2137 if (len < 2 || (len == 2 && name[1] == '.'))
2138 return ERR_PTR(-EACCES);
2142 c = *(const unsigned char *)name++;
2143 if (c == '/' || c == '\0')
2144 return ERR_PTR(-EACCES);
2147 * See if the low-level filesystem might want
2148 * to use its own hash..
2150 if (base->d_flags & DCACHE_OP_HASH) {
2151 int err = base->d_op->d_hash(base, base->d_inode, &this);
2153 return ERR_PTR(err);
2156 err = inode_permission(base->d_inode, MAY_EXEC);
2158 return ERR_PTR(err);
2160 return __lookup_hash(&this, base, 0);
2163 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2164 struct path *path, int *empty)
2166 struct nameidata nd;
2167 struct filename *tmp = getname_flags(name, flags, empty);
2168 int err = PTR_ERR(tmp);
2171 BUG_ON(flags & LOOKUP_PARENT);
2173 err = filename_lookup(dfd, tmp, flags, &nd);
2181 int user_path_at(int dfd, const char __user *name, unsigned flags,
2184 return user_path_at_empty(dfd, name, flags, path, NULL);
2188 * NB: most callers don't do anything directly with the reference to the
2189 * to struct filename, but the nd->last pointer points into the name string
2190 * allocated by getname. So we must hold the reference to it until all
2191 * path-walking is complete.
2193 static struct filename *
2194 user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2197 struct filename *s = getname(path);
2200 /* only LOOKUP_REVAL is allowed in extra flags */
2201 flags &= LOOKUP_REVAL;
2206 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
2209 return ERR_PTR(error);
2216 * It's inline, so penalty for filesystems that don't use sticky bit is
2219 static inline int check_sticky(struct inode *dir, struct inode *inode)
2221 kuid_t fsuid = current_fsuid();
2223 if (!(dir->i_mode & S_ISVTX))
2225 if (uid_eq(inode->i_uid, fsuid))
2227 if (uid_eq(dir->i_uid, fsuid))
2229 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
2233 * Check whether we can remove a link victim from directory dir, check
2234 * whether the type of victim is right.
2235 * 1. We can't do it if dir is read-only (done in permission())
2236 * 2. We should have write and exec permissions on dir
2237 * 3. We can't remove anything from append-only dir
2238 * 4. We can't do anything with immutable dir (done in permission())
2239 * 5. If the sticky bit on dir is set we should either
2240 * a. be owner of dir, or
2241 * b. be owner of victim, or
2242 * c. have CAP_FOWNER capability
2243 * 6. If the victim is append-only or immutable we can't do antyhing with
2244 * links pointing to it.
2245 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2246 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2247 * 9. We can't remove a root or mountpoint.
2248 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2249 * nfs_async_unlink().
2251 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
2255 if (!victim->d_inode)
2258 BUG_ON(victim->d_parent->d_inode != dir);
2259 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
2261 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
2266 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2267 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
2270 if (!S_ISDIR(victim->d_inode->i_mode))
2272 if (IS_ROOT(victim))
2274 } else if (S_ISDIR(victim->d_inode->i_mode))
2276 if (IS_DEADDIR(dir))
2278 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2283 /* Check whether we can create an object with dentry child in directory
2285 * 1. We can't do it if child already exists (open has special treatment for
2286 * this case, but since we are inlined it's OK)
2287 * 2. We can't do it if dir is read-only (done in permission())
2288 * 3. We should have write and exec permissions on dir
2289 * 4. We can't do it if dir is immutable (done in permission())
2291 static inline int may_create(struct inode *dir, struct dentry *child)
2293 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
2296 if (IS_DEADDIR(dir))
2298 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2302 * p1 and p2 should be directories on the same fs.
2304 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2309 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2313 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2315 p = d_ancestor(p2, p1);
2317 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2318 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2322 p = d_ancestor(p1, p2);
2324 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2325 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2329 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2330 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2334 void unlock_rename(struct dentry *p1, struct dentry *p2)
2336 mutex_unlock(&p1->d_inode->i_mutex);
2338 mutex_unlock(&p2->d_inode->i_mutex);
2339 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2343 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2346 int error = may_create(dir, dentry);
2350 if (!dir->i_op->create)
2351 return -EACCES; /* shouldn't it be ENOSYS? */
2354 error = security_inode_create(dir, dentry, mode);
2357 error = dir->i_op->create(dir, dentry, mode, want_excl);
2359 fsnotify_create(dir, dentry);
2363 static int may_open(struct path *path, int acc_mode, int flag)
2365 struct dentry *dentry = path->dentry;
2366 struct inode *inode = dentry->d_inode;
2376 switch (inode->i_mode & S_IFMT) {
2380 if (acc_mode & MAY_WRITE)
2385 if (path->mnt->mnt_flags & MNT_NODEV)
2394 error = inode_permission(inode, acc_mode);
2399 * An append-only file must be opened in append mode for writing.
2401 if (IS_APPEND(inode)) {
2402 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2408 /* O_NOATIME can only be set by the owner or superuser */
2409 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2415 static int handle_truncate(struct file *filp)
2417 struct path *path = &filp->f_path;
2418 struct inode *inode = path->dentry->d_inode;
2419 int error = get_write_access(inode);
2423 * Refuse to truncate files with mandatory locks held on them.
2425 error = locks_verify_locked(inode);
2427 error = security_path_truncate(path);
2429 error = do_truncate(path->dentry, 0,
2430 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2433 put_write_access(inode);
2437 static inline int open_to_namei_flags(int flag)
2439 if ((flag & O_ACCMODE) == 3)
2444 static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2446 int error = security_path_mknod(dir, dentry, mode, 0);
2450 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2454 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2458 * Attempt to atomically look up, create and open a file from a negative
2461 * Returns 0 if successful. The file will have been created and attached to
2462 * @file by the filesystem calling finish_open().
2464 * Returns 1 if the file was looked up only or didn't need creating. The
2465 * caller will need to perform the open themselves. @path will have been
2466 * updated to point to the new dentry. This may be negative.
2468 * Returns an error code otherwise.
2470 static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2471 struct path *path, struct file *file,
2472 const struct open_flags *op,
2473 bool got_write, bool need_lookup,
2476 struct inode *dir = nd->path.dentry->d_inode;
2477 unsigned open_flag = open_to_namei_flags(op->open_flag);
2481 int create_error = 0;
2482 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2484 BUG_ON(dentry->d_inode);
2486 /* Don't create child dentry for a dead directory. */
2487 if (unlikely(IS_DEADDIR(dir))) {
2493 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2494 mode &= ~current_umask();
2496 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
2497 open_flag &= ~O_TRUNC;
2498 *opened |= FILE_CREATED;
2502 * Checking write permission is tricky, bacuse we don't know if we are
2503 * going to actually need it: O_CREAT opens should work as long as the
2504 * file exists. But checking existence breaks atomicity. The trick is
2505 * to check access and if not granted clear O_CREAT from the flags.
2507 * Another problem is returing the "right" error value (e.g. for an
2508 * O_EXCL open we want to return EEXIST not EROFS).
2510 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2511 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2512 if (!(open_flag & O_CREAT)) {
2514 * No O_CREATE -> atomicity not a requirement -> fall
2515 * back to lookup + open
2518 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2519 /* Fall back and fail with the right error */
2520 create_error = -EROFS;
2523 /* No side effects, safe to clear O_CREAT */
2524 create_error = -EROFS;
2525 open_flag &= ~O_CREAT;
2529 if (open_flag & O_CREAT) {
2530 error = may_o_create(&nd->path, dentry, mode);
2532 create_error = error;
2533 if (open_flag & O_EXCL)
2535 open_flag &= ~O_CREAT;
2539 if (nd->flags & LOOKUP_DIRECTORY)
2540 open_flag |= O_DIRECTORY;
2542 file->f_path.dentry = DENTRY_NOT_SET;
2543 file->f_path.mnt = nd->path.mnt;
2544 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
2547 if (create_error && error == -ENOENT)
2548 error = create_error;
2552 acc_mode = op->acc_mode;
2553 if (*opened & FILE_CREATED) {
2554 fsnotify_create(dir, dentry);
2555 acc_mode = MAY_OPEN;
2558 if (error) { /* returned 1, that is */
2559 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2563 if (file->f_path.dentry) {
2565 dentry = file->f_path.dentry;
2567 if (create_error && dentry->d_inode == NULL) {
2568 error = create_error;
2575 * We didn't have the inode before the open, so check open permission
2578 error = may_open(&file->f_path, acc_mode, open_flag);
2588 dentry = lookup_real(dir, dentry, nd->flags);
2590 return PTR_ERR(dentry);
2593 int open_flag = op->open_flag;
2595 error = create_error;
2596 if ((open_flag & O_EXCL)) {
2597 if (!dentry->d_inode)
2599 } else if (!dentry->d_inode) {
2601 } else if ((open_flag & O_TRUNC) &&
2602 S_ISREG(dentry->d_inode->i_mode)) {
2605 /* will fail later, go on to get the right error */
2609 path->dentry = dentry;
2610 path->mnt = nd->path.mnt;
2615 * Look up and maybe create and open the last component.
2617 * Must be called with i_mutex held on parent.
2619 * Returns 0 if the file was successfully atomically created (if necessary) and
2620 * opened. In this case the file will be returned attached to @file.
2622 * Returns 1 if the file was not completely opened at this time, though lookups
2623 * and creations will have been performed and the dentry returned in @path will
2624 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2625 * specified then a negative dentry may be returned.
2627 * An error code is returned otherwise.
2629 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2630 * cleared otherwise prior to returning.
2632 static int lookup_open(struct nameidata *nd, struct path *path,
2634 const struct open_flags *op,
2635 bool got_write, int *opened)
2637 struct dentry *dir = nd->path.dentry;
2638 struct inode *dir_inode = dir->d_inode;
2639 struct dentry *dentry;
2643 *opened &= ~FILE_CREATED;
2644 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2646 return PTR_ERR(dentry);
2648 /* Cached positive dentry: will open in f_op->open */
2649 if (!need_lookup && dentry->d_inode)
2652 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
2653 return atomic_open(nd, dentry, path, file, op, got_write,
2654 need_lookup, opened);
2658 BUG_ON(dentry->d_inode);
2660 dentry = lookup_real(dir_inode, dentry, nd->flags);
2662 return PTR_ERR(dentry);
2665 /* Negative dentry, just create the file */
2666 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2667 umode_t mode = op->mode;
2668 if (!IS_POSIXACL(dir->d_inode))
2669 mode &= ~current_umask();
2671 * This write is needed to ensure that a
2672 * rw->ro transition does not occur between
2673 * the time when the file is created and when
2674 * a permanent write count is taken through
2675 * the 'struct file' in finish_open().
2681 *opened |= FILE_CREATED;
2682 error = security_path_mknod(&nd->path, dentry, mode, 0);
2685 error = vfs_create(dir->d_inode, dentry, mode,
2686 nd->flags & LOOKUP_EXCL);
2691 path->dentry = dentry;
2692 path->mnt = nd->path.mnt;
2701 * Handle the last step of open()
2703 static int do_last(struct nameidata *nd, struct path *path,
2704 struct file *file, const struct open_flags *op,
2705 int *opened, struct filename *name)
2707 struct dentry *dir = nd->path.dentry;
2708 int open_flag = op->open_flag;
2709 bool will_truncate = (open_flag & O_TRUNC) != 0;
2710 bool got_write = false;
2711 int acc_mode = op->acc_mode;
2712 struct inode *inode;
2713 bool symlink_ok = false;
2714 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2715 bool retried = false;
2718 nd->flags &= ~LOOKUP_PARENT;
2719 nd->flags |= op->intent;
2721 switch (nd->last_type) {
2724 error = handle_dots(nd, nd->last_type);
2729 error = complete_walk(nd);
2732 audit_inode(name, nd->path.dentry, 0);
2733 if (open_flag & O_CREAT) {
2739 error = complete_walk(nd);
2742 audit_inode(name, dir, 0);
2746 if (!(open_flag & O_CREAT)) {
2747 if (nd->last.name[nd->last.len])
2748 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2749 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2751 /* we _can_ be in RCU mode here */
2752 error = lookup_fast(nd, path, &inode);
2759 BUG_ON(nd->inode != dir->d_inode);
2761 /* create side of things */
2763 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2764 * has been cleared when we got to the last component we are
2767 error = complete_walk(nd);
2771 audit_inode(name, dir, LOOKUP_PARENT);
2773 /* trailing slashes? */
2774 if (nd->last.name[nd->last.len])
2779 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2780 error = mnt_want_write(nd->path.mnt);
2784 * do _not_ fail yet - we might not need that or fail with
2785 * a different error; let lookup_open() decide; we'll be
2786 * dropping this one anyway.
2789 mutex_lock(&dir->d_inode->i_mutex);
2790 error = lookup_open(nd, path, file, op, got_write, opened);
2791 mutex_unlock(&dir->d_inode->i_mutex);
2797 if ((*opened & FILE_CREATED) ||
2798 !S_ISREG(file_inode(file)->i_mode))
2799 will_truncate = false;
2801 audit_inode(name, file->f_path.dentry, 0);
2805 if (*opened & FILE_CREATED) {
2806 /* Don't check for write permission, don't truncate */
2807 open_flag &= ~O_TRUNC;
2808 will_truncate = false;
2809 acc_mode = MAY_OPEN;
2810 path_to_nameidata(path, nd);
2811 goto finish_open_created;
2815 * create/update audit record if it already exists.
2817 if (path->dentry->d_inode)
2818 audit_inode(name, path->dentry, 0);
2821 * If atomic_open() acquired write access it is dropped now due to
2822 * possible mount and symlink following (this might be optimized away if
2826 mnt_drop_write(nd->path.mnt);
2831 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
2834 error = follow_managed(path, nd->flags);
2839 nd->flags |= LOOKUP_JUMPED;
2841 BUG_ON(nd->flags & LOOKUP_RCU);
2842 inode = path->dentry->d_inode;
2844 /* we _can_ be in RCU mode here */
2847 path_to_nameidata(path, nd);
2851 if (should_follow_link(inode, !symlink_ok)) {
2852 if (nd->flags & LOOKUP_RCU) {
2853 if (unlikely(nd->path.mnt != path->mnt ||
2854 unlazy_walk(nd, path->dentry))) {
2859 BUG_ON(inode != path->dentry->d_inode);
2863 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2864 path_to_nameidata(path, nd);
2866 save_parent.dentry = nd->path.dentry;
2867 save_parent.mnt = mntget(path->mnt);
2868 nd->path.dentry = path->dentry;
2872 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2873 error = complete_walk(nd);
2875 path_put(&save_parent);
2879 if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
2882 if ((nd->flags & LOOKUP_DIRECTORY) && !can_lookup(nd->inode))
2884 audit_inode(name, nd->path.dentry, 0);
2886 if (!S_ISREG(nd->inode->i_mode))
2887 will_truncate = false;
2889 if (will_truncate) {
2890 error = mnt_want_write(nd->path.mnt);
2895 finish_open_created:
2896 error = may_open(&nd->path, acc_mode, open_flag);
2899 file->f_path.mnt = nd->path.mnt;
2900 error = finish_open(file, nd->path.dentry, NULL, opened);
2902 if (error == -EOPENSTALE)
2907 error = open_check_o_direct(file);
2910 error = ima_file_check(file, op->acc_mode);
2914 if (will_truncate) {
2915 error = handle_truncate(file);
2921 mnt_drop_write(nd->path.mnt);
2922 path_put(&save_parent);
2927 path_put_conditional(path, nd);
2934 /* If no saved parent or already retried then can't retry */
2935 if (!save_parent.dentry || retried)
2938 BUG_ON(save_parent.dentry != dir);
2939 path_put(&nd->path);
2940 nd->path = save_parent;
2941 nd->inode = dir->d_inode;
2942 save_parent.mnt = NULL;
2943 save_parent.dentry = NULL;
2945 mnt_drop_write(nd->path.mnt);
2952 static struct file *path_openat(int dfd, struct filename *pathname,
2953 struct nameidata *nd, const struct open_flags *op, int flags)
2955 struct file *base = NULL;
2961 file = get_empty_filp();
2965 file->f_flags = op->open_flag;
2967 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
2968 if (unlikely(error))
2971 current->total_link_count = 0;
2972 error = link_path_walk(pathname->name, nd);
2973 if (unlikely(error))
2976 error = do_last(nd, &path, file, op, &opened, pathname);
2977 while (unlikely(error > 0)) { /* trailing symlink */
2978 struct path link = path;
2980 if (!(nd->flags & LOOKUP_FOLLOW)) {
2981 path_put_conditional(&path, nd);
2982 path_put(&nd->path);
2986 error = may_follow_link(&link, nd);
2987 if (unlikely(error))
2989 nd->flags |= LOOKUP_PARENT;
2990 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2991 error = follow_link(&link, nd, &cookie);
2992 if (unlikely(error))
2994 error = do_last(nd, &path, file, op, &opened, pathname);
2995 put_link(nd, &link, cookie);
2998 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2999 path_put(&nd->root);
3002 if (!(opened & FILE_OPENED)) {
3006 if (unlikely(error)) {
3007 if (error == -EOPENSTALE) {
3008 if (flags & LOOKUP_RCU)
3013 file = ERR_PTR(error);
3018 struct file *do_filp_open(int dfd, struct filename *pathname,
3019 const struct open_flags *op, int flags)
3021 struct nameidata nd;
3024 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
3025 if (unlikely(filp == ERR_PTR(-ECHILD)))
3026 filp = path_openat(dfd, pathname, &nd, op, flags);
3027 if (unlikely(filp == ERR_PTR(-ESTALE)))
3028 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
3032 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3033 const char *name, const struct open_flags *op, int flags)
3035 struct nameidata nd;
3037 struct filename filename = { .name = name };
3040 nd.root.dentry = dentry;
3042 flags |= LOOKUP_ROOT;
3044 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
3045 return ERR_PTR(-ELOOP);
3047 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
3048 if (unlikely(file == ERR_PTR(-ECHILD)))
3049 file = path_openat(-1, &filename, &nd, op, flags);
3050 if (unlikely(file == ERR_PTR(-ESTALE)))
3051 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
3055 struct dentry *kern_path_create(int dfd, const char *pathname,
3056 struct path *path, unsigned int lookup_flags)
3058 struct dentry *dentry = ERR_PTR(-EEXIST);
3059 struct nameidata nd;
3062 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3065 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3066 * other flags passed in are ignored!
3068 lookup_flags &= LOOKUP_REVAL;
3070 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
3072 return ERR_PTR(error);
3075 * Yucky last component or no last component at all?
3076 * (foo/., foo/.., /////)
3078 if (nd.last_type != LAST_NORM)
3080 nd.flags &= ~LOOKUP_PARENT;
3081 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3083 /* don't fail immediately if it's r/o, at least try to report other errors */
3084 err2 = mnt_want_write(nd.path.mnt);
3086 * Do the final lookup.
3088 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3089 dentry = lookup_hash(&nd);
3094 if (dentry->d_inode)
3097 * Special case - lookup gave negative, but... we had foo/bar/
3098 * From the vfs_mknod() POV we just have a negative dentry -
3099 * all is fine. Let's be bastards - you had / on the end, you've
3100 * been asking for (non-existent) directory. -ENOENT for you.
3102 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
3106 if (unlikely(err2)) {
3114 dentry = ERR_PTR(error);
3116 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3118 mnt_drop_write(nd.path.mnt);
3123 EXPORT_SYMBOL(kern_path_create);
3125 void done_path_create(struct path *path, struct dentry *dentry)
3128 mutex_unlock(&path->dentry->d_inode->i_mutex);
3129 mnt_drop_write(path->mnt);
3132 EXPORT_SYMBOL(done_path_create);
3134 struct dentry *user_path_create(int dfd, const char __user *pathname,
3135 struct path *path, unsigned int lookup_flags)
3137 struct filename *tmp = getname(pathname);
3140 return ERR_CAST(tmp);
3141 res = kern_path_create(dfd, tmp->name, path, lookup_flags);
3145 EXPORT_SYMBOL(user_path_create);
3147 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3149 int error = may_create(dir, dentry);
3154 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
3157 if (!dir->i_op->mknod)
3160 error = devcgroup_inode_mknod(mode, dev);
3164 error = security_inode_mknod(dir, dentry, mode, dev);
3168 error = dir->i_op->mknod(dir, dentry, mode, dev);
3170 fsnotify_create(dir, dentry);
3174 static int may_mknod(umode_t mode)
3176 switch (mode & S_IFMT) {
3182 case 0: /* zero mode translates to S_IFREG */
3191 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3194 struct dentry *dentry;
3197 unsigned int lookup_flags = 0;
3199 error = may_mknod(mode);
3203 dentry = user_path_create(dfd, filename, &path, lookup_flags);
3205 return PTR_ERR(dentry);
3207 if (!IS_POSIXACL(path.dentry->d_inode))
3208 mode &= ~current_umask();
3209 error = security_path_mknod(&path, dentry, mode, dev);
3212 switch (mode & S_IFMT) {
3213 case 0: case S_IFREG:
3214 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
3216 case S_IFCHR: case S_IFBLK:
3217 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
3218 new_decode_dev(dev));
3220 case S_IFIFO: case S_IFSOCK:
3221 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
3225 done_path_create(&path, dentry);
3226 if (retry_estale(error, lookup_flags)) {
3227 lookup_flags |= LOOKUP_REVAL;
3233 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
3235 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3238 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3240 int error = may_create(dir, dentry);
3241 unsigned max_links = dir->i_sb->s_max_links;
3246 if (!dir->i_op->mkdir)
3249 mode &= (S_IRWXUGO|S_ISVTX);
3250 error = security_inode_mkdir(dir, dentry, mode);
3254 if (max_links && dir->i_nlink >= max_links)
3257 error = dir->i_op->mkdir(dir, dentry, mode);
3259 fsnotify_mkdir(dir, dentry);
3263 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3265 struct dentry *dentry;
3268 unsigned int lookup_flags = LOOKUP_DIRECTORY;
3271 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
3273 return PTR_ERR(dentry);
3275 if (!IS_POSIXACL(path.dentry->d_inode))
3276 mode &= ~current_umask();
3277 error = security_path_mkdir(&path, dentry, mode);
3279 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3280 done_path_create(&path, dentry);
3281 if (retry_estale(error, lookup_flags)) {
3282 lookup_flags |= LOOKUP_REVAL;
3288 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
3290 return sys_mkdirat(AT_FDCWD, pathname, mode);
3294 * The dentry_unhash() helper will try to drop the dentry early: we
3295 * should have a usage count of 1 if we're the only user of this
3296 * dentry, and if that is true (possibly after pruning the dcache),
3297 * then we drop the dentry now.
3299 * A low-level filesystem can, if it choses, legally
3302 * if (!d_unhashed(dentry))
3305 * if it cannot handle the case of removing a directory
3306 * that is still in use by something else..
3308 void dentry_unhash(struct dentry *dentry)
3310 shrink_dcache_parent(dentry);
3311 spin_lock(&dentry->d_lock);
3312 if (dentry->d_count == 1)
3314 spin_unlock(&dentry->d_lock);
3317 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3319 int error = may_delete(dir, dentry, 1);
3324 if (!dir->i_op->rmdir)
3328 mutex_lock(&dentry->d_inode->i_mutex);
3331 if (d_mountpoint(dentry))
3334 error = security_inode_rmdir(dir, dentry);
3338 shrink_dcache_parent(dentry);
3339 error = dir->i_op->rmdir(dir, dentry);
3343 dentry->d_inode->i_flags |= S_DEAD;
3347 mutex_unlock(&dentry->d_inode->i_mutex);
3354 static long do_rmdir(int dfd, const char __user *pathname)
3357 struct filename *name;
3358 struct dentry *dentry;
3359 struct nameidata nd;
3360 unsigned int lookup_flags = 0;
3362 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
3364 return PTR_ERR(name);
3366 switch(nd.last_type) {
3378 nd.flags &= ~LOOKUP_PARENT;
3379 error = mnt_want_write(nd.path.mnt);
3383 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3384 dentry = lookup_hash(&nd);
3385 error = PTR_ERR(dentry);
3388 if (!dentry->d_inode) {
3392 error = security_path_rmdir(&nd.path, dentry);
3395 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
3399 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3400 mnt_drop_write(nd.path.mnt);
3404 if (retry_estale(error, lookup_flags)) {
3405 lookup_flags |= LOOKUP_REVAL;
3411 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
3413 return do_rmdir(AT_FDCWD, pathname);
3416 int vfs_unlink(struct inode *dir, struct dentry *dentry)
3418 int error = may_delete(dir, dentry, 0);
3423 if (!dir->i_op->unlink)
3426 mutex_lock(&dentry->d_inode->i_mutex);
3427 if (d_mountpoint(dentry))
3430 error = security_inode_unlink(dir, dentry);
3432 error = dir->i_op->unlink(dir, dentry);
3437 mutex_unlock(&dentry->d_inode->i_mutex);
3439 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3440 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3441 fsnotify_link_count(dentry->d_inode);
3449 * Make sure that the actual truncation of the file will occur outside its
3450 * directory's i_mutex. Truncate can take a long time if there is a lot of
3451 * writeout happening, and we don't want to prevent access to the directory
3452 * while waiting on the I/O.
3454 static long do_unlinkat(int dfd, const char __user *pathname)
3457 struct filename *name;
3458 struct dentry *dentry;
3459 struct nameidata nd;
3460 struct inode *inode = NULL;
3461 unsigned int lookup_flags = 0;
3463 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
3465 return PTR_ERR(name);
3468 if (nd.last_type != LAST_NORM)
3471 nd.flags &= ~LOOKUP_PARENT;
3472 error = mnt_want_write(nd.path.mnt);
3476 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3477 dentry = lookup_hash(&nd);
3478 error = PTR_ERR(dentry);
3479 if (!IS_ERR(dentry)) {
3480 /* Why not before? Because we want correct error value */
3481 if (nd.last.name[nd.last.len])
3483 inode = dentry->d_inode;
3487 error = security_path_unlink(&nd.path, dentry);
3490 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
3494 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3496 iput(inode); /* truncate the inode here */
3497 mnt_drop_write(nd.path.mnt);
3501 if (retry_estale(error, lookup_flags)) {
3502 lookup_flags |= LOOKUP_REVAL;
3509 error = !dentry->d_inode ? -ENOENT :
3510 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
3514 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
3516 if ((flag & ~AT_REMOVEDIR) != 0)
3519 if (flag & AT_REMOVEDIR)
3520 return do_rmdir(dfd, pathname);
3522 return do_unlinkat(dfd, pathname);
3525 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
3527 return do_unlinkat(AT_FDCWD, pathname);
3530 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
3532 int error = may_create(dir, dentry);
3537 if (!dir->i_op->symlink)
3540 error = security_inode_symlink(dir, dentry, oldname);
3544 error = dir->i_op->symlink(dir, dentry, oldname);
3546 fsnotify_create(dir, dentry);
3550 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3551 int, newdfd, const char __user *, newname)
3554 struct filename *from;
3555 struct dentry *dentry;
3557 unsigned int lookup_flags = 0;
3559 from = getname(oldname);
3561 return PTR_ERR(from);
3563 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
3564 error = PTR_ERR(dentry);
3568 error = security_path_symlink(&path, dentry, from->name);
3570 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
3571 done_path_create(&path, dentry);
3572 if (retry_estale(error, lookup_flags)) {
3573 lookup_flags |= LOOKUP_REVAL;
3581 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
3583 return sys_symlinkat(oldname, AT_FDCWD, newname);
3586 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3588 struct inode *inode = old_dentry->d_inode;
3589 unsigned max_links = dir->i_sb->s_max_links;
3595 error = may_create(dir, new_dentry);
3599 if (dir->i_sb != inode->i_sb)
3603 * A link to an append-only or immutable file cannot be created.
3605 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3607 if (!dir->i_op->link)
3609 if (S_ISDIR(inode->i_mode))
3612 error = security_inode_link(old_dentry, dir, new_dentry);
3616 mutex_lock(&inode->i_mutex);
3617 /* Make sure we don't allow creating hardlink to an unlinked file */
3618 if (inode->i_nlink == 0)
3620 else if (max_links && inode->i_nlink >= max_links)
3623 error = dir->i_op->link(old_dentry, dir, new_dentry);
3624 mutex_unlock(&inode->i_mutex);
3626 fsnotify_link(dir, inode, new_dentry);
3631 * Hardlinks are often used in delicate situations. We avoid
3632 * security-related surprises by not following symlinks on the
3635 * We don't follow them on the oldname either to be compatible
3636 * with linux 2.0, and to avoid hard-linking to directories
3637 * and other special files. --ADM
3639 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3640 int, newdfd, const char __user *, newname, int, flags)
3642 struct dentry *new_dentry;
3643 struct path old_path, new_path;
3647 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3650 * To use null names we require CAP_DAC_READ_SEARCH
3651 * This ensures that not everyone will be able to create
3652 * handlink using the passed filedescriptor.
3654 if (flags & AT_EMPTY_PATH) {
3655 if (!capable(CAP_DAC_READ_SEARCH))
3660 if (flags & AT_SYMLINK_FOLLOW)
3661 how |= LOOKUP_FOLLOW;
3663 error = user_path_at(olddfd, oldname, how, &old_path);
3667 new_dentry = user_path_create(newdfd, newname, &new_path,
3668 (how & LOOKUP_REVAL));
3669 error = PTR_ERR(new_dentry);
3670 if (IS_ERR(new_dentry))
3674 if (old_path.mnt != new_path.mnt)
3676 error = may_linkat(&old_path);
3677 if (unlikely(error))
3679 error = security_path_link(old_path.dentry, &new_path, new_dentry);
3682 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3684 done_path_create(&new_path, new_dentry);
3685 if (retry_estale(error, how)) {
3686 path_put(&old_path);
3687 how |= LOOKUP_REVAL;
3691 path_put(&old_path);
3696 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
3698 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
3702 * The worst of all namespace operations - renaming directory. "Perverted"
3703 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3705 * a) we can get into loop creation. Check is done in is_subdir().
3706 * b) race potential - two innocent renames can create a loop together.
3707 * That's where 4.4 screws up. Current fix: serialization on
3708 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3710 * c) we have to lock _three_ objects - parents and victim (if it exists).
3711 * And that - after we got ->i_mutex on parents (until then we don't know
3712 * whether the target exists). Solution: try to be smart with locking
3713 * order for inodes. We rely on the fact that tree topology may change
3714 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
3715 * move will be locked. Thus we can rank directories by the tree
3716 * (ancestors first) and rank all non-directories after them.
3717 * That works since everybody except rename does "lock parent, lookup,
3718 * lock child" and rename is under ->s_vfs_rename_mutex.
3719 * HOWEVER, it relies on the assumption that any object with ->lookup()
3720 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3721 * we'd better make sure that there's no link(2) for them.
3722 * d) conversion from fhandle to dentry may come in the wrong moment - when
3723 * we are removing the target. Solution: we will have to grab ->i_mutex
3724 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3725 * ->i_mutex on parents, which works but leads to some truly excessive
3728 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3729 struct inode *new_dir, struct dentry *new_dentry)
3732 struct inode *target = new_dentry->d_inode;
3733 unsigned max_links = new_dir->i_sb->s_max_links;
3736 * If we are going to change the parent - check write permissions,
3737 * we'll need to flip '..'.
3739 if (new_dir != old_dir) {
3740 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
3745 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3751 mutex_lock(&target->i_mutex);
3754 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3758 if (max_links && !target && new_dir != old_dir &&
3759 new_dir->i_nlink >= max_links)
3763 shrink_dcache_parent(new_dentry);
3764 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3769 target->i_flags |= S_DEAD;
3770 dont_mount(new_dentry);
3774 mutex_unlock(&target->i_mutex);
3777 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3778 d_move(old_dentry,new_dentry);
3782 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3783 struct inode *new_dir, struct dentry *new_dentry)
3785 struct inode *target = new_dentry->d_inode;
3788 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3794 mutex_lock(&target->i_mutex);
3797 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3800 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3805 dont_mount(new_dentry);
3806 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3807 d_move(old_dentry, new_dentry);
3810 mutex_unlock(&target->i_mutex);
3815 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3816 struct inode *new_dir, struct dentry *new_dentry)
3819 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
3820 const unsigned char *old_name;
3822 if (old_dentry->d_inode == new_dentry->d_inode)
3825 error = may_delete(old_dir, old_dentry, is_dir);
3829 if (!new_dentry->d_inode)
3830 error = may_create(new_dir, new_dentry);
3832 error = may_delete(new_dir, new_dentry, is_dir);
3836 if (!old_dir->i_op->rename)
3839 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3842 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3844 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3846 fsnotify_move(old_dir, new_dir, old_name, is_dir,
3847 new_dentry->d_inode, old_dentry);
3848 fsnotify_oldname_free(old_name);
3853 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3854 int, newdfd, const char __user *, newname)
3856 struct dentry *old_dir, *new_dir;
3857 struct dentry *old_dentry, *new_dentry;
3858 struct dentry *trap;
3859 struct nameidata oldnd, newnd;
3860 struct filename *from;
3861 struct filename *to;
3862 unsigned int lookup_flags = 0;
3863 bool should_retry = false;
3866 from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
3868 error = PTR_ERR(from);
3872 to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
3874 error = PTR_ERR(to);
3879 if (oldnd.path.mnt != newnd.path.mnt)
3882 old_dir = oldnd.path.dentry;
3884 if (oldnd.last_type != LAST_NORM)
3887 new_dir = newnd.path.dentry;
3888 if (newnd.last_type != LAST_NORM)
3891 error = mnt_want_write(oldnd.path.mnt);
3895 oldnd.flags &= ~LOOKUP_PARENT;
3896 newnd.flags &= ~LOOKUP_PARENT;
3897 newnd.flags |= LOOKUP_RENAME_TARGET;
3899 trap = lock_rename(new_dir, old_dir);
3901 old_dentry = lookup_hash(&oldnd);
3902 error = PTR_ERR(old_dentry);
3903 if (IS_ERR(old_dentry))
3905 /* source must exist */
3907 if (!old_dentry->d_inode)
3909 /* unless the source is a directory trailing slashes give -ENOTDIR */
3910 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3912 if (oldnd.last.name[oldnd.last.len])
3914 if (newnd.last.name[newnd.last.len])
3917 /* source should not be ancestor of target */
3919 if (old_dentry == trap)
3921 new_dentry = lookup_hash(&newnd);
3922 error = PTR_ERR(new_dentry);
3923 if (IS_ERR(new_dentry))
3925 /* target should not be an ancestor of source */
3927 if (new_dentry == trap)
3930 error = security_path_rename(&oldnd.path, old_dentry,
3931 &newnd.path, new_dentry);
3934 error = vfs_rename(old_dir->d_inode, old_dentry,
3935 new_dir->d_inode, new_dentry);
3941 unlock_rename(new_dir, old_dir);
3942 mnt_drop_write(oldnd.path.mnt);
3944 if (retry_estale(error, lookup_flags))
3945 should_retry = true;
3946 path_put(&newnd.path);
3949 path_put(&oldnd.path);
3952 should_retry = false;
3953 lookup_flags |= LOOKUP_REVAL;
3960 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
3962 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3965 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3969 len = PTR_ERR(link);
3974 if (len > (unsigned) buflen)
3976 if (copy_to_user(buffer, link, len))
3983 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3984 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3985 * using) it for any given inode is up to filesystem.
3987 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3989 struct nameidata nd;
3994 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3996 return PTR_ERR(cookie);
3998 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3999 if (dentry->d_inode->i_op->put_link)
4000 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4004 int vfs_follow_link(struct nameidata *nd, const char *link)
4006 return __vfs_follow_link(nd, link);
4009 /* get the link contents into pagecache */
4010 static char *page_getlink(struct dentry * dentry, struct page **ppage)
4014 struct address_space *mapping = dentry->d_inode->i_mapping;
4015 page = read_mapping_page(mapping, 0, NULL);
4020 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4024 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4026 struct page *page = NULL;
4027 char *s = page_getlink(dentry, &page);
4028 int res = vfs_readlink(dentry,buffer,buflen,s);
4031 page_cache_release(page);
4036 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
4038 struct page *page = NULL;
4039 nd_set_link(nd, page_getlink(dentry, &page));
4043 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
4045 struct page *page = cookie;
4049 page_cache_release(page);
4054 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4056 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
4058 struct address_space *mapping = inode->i_mapping;
4063 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4065 flags |= AOP_FLAG_NOFS;
4068 err = pagecache_write_begin(NULL, mapping, 0, len-1,
4069 flags, &page, &fsdata);
4073 kaddr = kmap_atomic(page);
4074 memcpy(kaddr, symname, len-1);
4075 kunmap_atomic(kaddr);
4077 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4084 mark_inode_dirty(inode);
4090 int page_symlink(struct inode *inode, const char *symname, int len)
4092 return __page_symlink(inode, symname, len,
4093 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
4096 const struct inode_operations page_symlink_inode_operations = {
4097 .readlink = generic_readlink,
4098 .follow_link = page_follow_link_light,
4099 .put_link = page_put_link,
4102 EXPORT_SYMBOL(user_path_at);
4103 EXPORT_SYMBOL(follow_down_one);
4104 EXPORT_SYMBOL(follow_down);
4105 EXPORT_SYMBOL(follow_up);
4106 EXPORT_SYMBOL(get_write_access); /* nfsd */
4107 EXPORT_SYMBOL(lock_rename);
4108 EXPORT_SYMBOL(lookup_one_len);
4109 EXPORT_SYMBOL(page_follow_link_light);
4110 EXPORT_SYMBOL(page_put_link);
4111 EXPORT_SYMBOL(page_readlink);
4112 EXPORT_SYMBOL(__page_symlink);
4113 EXPORT_SYMBOL(page_symlink);
4114 EXPORT_SYMBOL(page_symlink_inode_operations);
4115 EXPORT_SYMBOL(kern_path);
4116 EXPORT_SYMBOL(vfs_path_lookup);
4117 EXPORT_SYMBOL(inode_permission);
4118 EXPORT_SYMBOL(unlock_rename);
4119 EXPORT_SYMBOL(vfs_create);
4120 EXPORT_SYMBOL(vfs_follow_link);
4121 EXPORT_SYMBOL(vfs_link);
4122 EXPORT_SYMBOL(vfs_mkdir);
4123 EXPORT_SYMBOL(vfs_mknod);
4124 EXPORT_SYMBOL(generic_permission);
4125 EXPORT_SYMBOL(vfs_readlink);
4126 EXPORT_SYMBOL(vfs_rename);
4127 EXPORT_SYMBOL(vfs_rmdir);
4128 EXPORT_SYMBOL(vfs_symlink);
4129 EXPORT_SYMBOL(vfs_unlink);
4130 EXPORT_SYMBOL(dentry_unhash);
4131 EXPORT_SYMBOL(generic_readlink);