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 <asm/uaccess.h>
42 /* [Feb-1997 T. Schoebel-Theuer]
43 * Fundamental changes in the pathname lookup mechanisms (namei)
44 * were necessary because of omirr. The reason is that omirr needs
45 * to know the _real_ pathname, not the user-supplied one, in case
46 * of symlinks (and also when transname replacements occur).
48 * The new code replaces the old recursive symlink resolution with
49 * an iterative one (in case of non-nested symlink chains). It does
50 * this with calls to <fs>_follow_link().
51 * As a side effect, dir_namei(), _namei() and follow_link() are now
52 * replaced with a single function lookup_dentry() that can handle all
53 * the special cases of the former code.
55 * With the new dcache, the pathname is stored at each inode, at least as
56 * long as the refcount of the inode is positive. As a side effect, the
57 * size of the dcache depends on the inode cache and thus is dynamic.
59 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
60 * resolution to correspond with current state of the code.
62 * Note that the symlink resolution is not *completely* iterative.
63 * There is still a significant amount of tail- and mid- recursion in
64 * the algorithm. Also, note that <fs>_readlink() is not used in
65 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
66 * may return different results than <fs>_follow_link(). Many virtual
67 * filesystems (including /proc) exhibit this behavior.
70 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
71 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
72 * and the name already exists in form of a symlink, try to create the new
73 * name indicated by the symlink. The old code always complained that the
74 * name already exists, due to not following the symlink even if its target
75 * is nonexistent. The new semantics affects also mknod() and link() when
76 * the name is a symlink pointing to a non-existent name.
78 * I don't know which semantics is the right one, since I have no access
79 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
80 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
81 * "old" one. Personally, I think the new semantics is much more logical.
82 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
83 * file does succeed in both HP-UX and SunOs, but not in Solaris
84 * and in the old Linux semantics.
87 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
88 * semantics. See the comments in "open_namei" and "do_link" below.
90 * [10-Sep-98 Alan Modra] Another symlink change.
93 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
94 * inside the path - always follow.
95 * in the last component in creation/removal/renaming - never follow.
96 * if LOOKUP_FOLLOW passed - follow.
97 * if the pathname has trailing slashes - follow.
98 * otherwise - don't follow.
99 * (applied in that order).
101 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
102 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
103 * During the 2.4 we need to fix the userland stuff depending on it -
104 * hopefully we will be able to get rid of that wart in 2.5. So far only
105 * XEmacs seems to be relying on it...
108 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
109 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
110 * any extra contention...
113 /* In order to reduce some races, while at the same time doing additional
114 * checking and hopefully speeding things up, we copy filenames to the
115 * kernel data space before using them..
117 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
118 * PATH_MAX includes the nul terminator --RR.
120 void final_putname(struct filename *name)
122 __putname(name->name);
126 static struct filename *
127 getname_flags(const char __user *filename, int flags, int *empty)
129 struct filename *result, *err;
133 result = audit_reusename(filename);
137 /* FIXME: create dedicated slabcache? */
138 result = kzalloc(sizeof(*result), GFP_KERNEL);
139 if (unlikely(!result))
140 return ERR_PTR(-ENOMEM);
143 if (unlikely(!kname)) {
144 err = ERR_PTR(-ENOMEM);
145 goto error_free_name;
148 result->name = kname;
149 result->uptr = filename;
150 len = strncpy_from_user(kname, filename, PATH_MAX);
151 if (unlikely(len < 0)) {
156 /* The empty path is special. */
157 if (unlikely(!len)) {
160 err = ERR_PTR(-ENOENT);
161 if (!(flags & LOOKUP_EMPTY))
165 err = ERR_PTR(-ENAMETOOLONG);
166 if (likely(len < PATH_MAX)) {
167 audit_getname(result);
179 getname(const char __user * filename)
181 return getname_flags(filename, 0, NULL);
183 EXPORT_SYMBOL(getname);
185 #ifdef CONFIG_AUDITSYSCALL
186 void putname(struct filename *name)
188 if (unlikely(!audit_dummy_context()))
189 return audit_putname(name);
194 static int check_acl(struct inode *inode, int mask)
196 #ifdef CONFIG_FS_POSIX_ACL
197 struct posix_acl *acl;
199 if (mask & MAY_NOT_BLOCK) {
200 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
203 /* no ->get_acl() calls in RCU mode... */
204 if (acl == ACL_NOT_CACHED)
206 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
209 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
212 * A filesystem can force a ACL callback by just never filling the
213 * ACL cache. But normally you'd fill the cache either at inode
214 * instantiation time, or on the first ->get_acl call.
216 * If the filesystem doesn't have a get_acl() function at all, we'll
217 * just create the negative cache entry.
219 if (acl == ACL_NOT_CACHED) {
220 if (inode->i_op->get_acl) {
221 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
225 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
231 int error = posix_acl_permission(inode, acl, mask);
232 posix_acl_release(acl);
241 * This does the basic permission checking
243 static int acl_permission_check(struct inode *inode, int mask)
245 unsigned int mode = inode->i_mode;
247 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
250 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
251 int error = check_acl(inode, mask);
252 if (error != -EAGAIN)
256 if (in_group_p(inode->i_gid))
261 * If the DACs are ok we don't need any capability check.
263 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
269 * generic_permission - check for access rights on a Posix-like filesystem
270 * @inode: inode to check access rights for
271 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
273 * Used to check for read/write/execute permissions on a file.
274 * We use "fsuid" for this, letting us set arbitrary permissions
275 * for filesystem access without changing the "normal" uids which
276 * are used for other things.
278 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
279 * request cannot be satisfied (eg. requires blocking or too much complexity).
280 * It would then be called again in ref-walk mode.
282 int generic_permission(struct inode *inode, int mask)
287 * Do the basic permission checks.
289 ret = acl_permission_check(inode, mask);
293 if (S_ISDIR(inode->i_mode)) {
294 /* DACs are overridable for directories */
295 if (inode_capable(inode, CAP_DAC_OVERRIDE))
297 if (!(mask & MAY_WRITE))
298 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
303 * Read/write DACs are always overridable.
304 * Executable DACs are overridable when there is
305 * at least one exec bit set.
307 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
308 if (inode_capable(inode, CAP_DAC_OVERRIDE))
312 * Searching includes executable on directories, else just read.
314 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
315 if (mask == MAY_READ)
316 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
323 * We _really_ want to just do "generic_permission()" without
324 * even looking at the inode->i_op values. So we keep a cache
325 * flag in inode->i_opflags, that says "this has not special
326 * permission function, use the fast case".
328 static inline int do_inode_permission(struct inode *inode, int mask)
330 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
331 if (likely(inode->i_op->permission))
332 return inode->i_op->permission(inode, mask);
334 /* This gets set once for the inode lifetime */
335 spin_lock(&inode->i_lock);
336 inode->i_opflags |= IOP_FASTPERM;
337 spin_unlock(&inode->i_lock);
339 return generic_permission(inode, mask);
343 * __inode_permission - Check for access rights to a given inode
344 * @inode: Inode to check permission on
345 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
347 * Check for read/write/execute permissions on an inode.
349 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
351 * This does not check for a read-only file system. You probably want
352 * inode_permission().
354 int __inode_permission(struct inode *inode, int mask)
358 if (unlikely(mask & MAY_WRITE)) {
360 * Nobody gets write access to an immutable file.
362 if (IS_IMMUTABLE(inode))
366 retval = do_inode_permission(inode, mask);
370 retval = devcgroup_inode_permission(inode, mask);
374 return security_inode_permission(inode, mask);
378 * sb_permission - Check superblock-level permissions
379 * @sb: Superblock of inode to check permission on
380 * @inode: Inode to check permission on
381 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
383 * Separate out file-system wide checks from inode-specific permission checks.
385 static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
387 if (unlikely(mask & MAY_WRITE)) {
388 umode_t mode = inode->i_mode;
390 /* Nobody gets write access to a read-only fs. */
391 if ((sb->s_flags & MS_RDONLY) &&
392 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
399 * inode_permission - Check for access rights to a given inode
400 * @inode: Inode to check permission on
401 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
403 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
404 * this, letting us set arbitrary permissions for filesystem access without
405 * changing the "normal" UIDs which are used for other things.
407 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
409 int inode_permission(struct inode *inode, int mask)
413 retval = sb_permission(inode->i_sb, inode, mask);
416 return __inode_permission(inode, mask);
420 * path_get - get a reference to a path
421 * @path: path to get the reference to
423 * Given a path increment the reference count to the dentry and the vfsmount.
425 void path_get(struct path *path)
430 EXPORT_SYMBOL(path_get);
433 * path_put - put a reference to a path
434 * @path: path to put the reference to
436 * Given a path decrement the reference count to the dentry and the vfsmount.
438 void path_put(struct path *path)
443 EXPORT_SYMBOL(path_put);
446 * Path walking has 2 modes, rcu-walk and ref-walk (see
447 * Documentation/filesystems/path-lookup.txt). In situations when we can't
448 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
449 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
450 * mode. Refcounts are grabbed at the last known good point before rcu-walk
451 * got stuck, so ref-walk may continue from there. If this is not successful
452 * (eg. a seqcount has changed), then failure is returned and it's up to caller
453 * to restart the path walk from the beginning in ref-walk mode.
456 static inline void lock_rcu_walk(void)
458 br_read_lock(&vfsmount_lock);
462 static inline void unlock_rcu_walk(void)
465 br_read_unlock(&vfsmount_lock);
469 * unlazy_walk - try to switch to ref-walk mode.
470 * @nd: nameidata pathwalk data
471 * @dentry: child of nd->path.dentry or NULL
472 * Returns: 0 on success, -ECHILD on failure
474 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
475 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
476 * @nd or NULL. Must be called from rcu-walk context.
478 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
480 struct fs_struct *fs = current->fs;
481 struct dentry *parent = nd->path.dentry;
484 BUG_ON(!(nd->flags & LOOKUP_RCU));
485 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
487 spin_lock(&fs->lock);
488 if (nd->root.mnt != fs->root.mnt ||
489 nd->root.dentry != fs->root.dentry)
492 spin_lock(&parent->d_lock);
494 if (!__d_rcu_to_refcount(parent, nd->seq))
496 BUG_ON(nd->inode != parent->d_inode);
498 if (dentry->d_parent != parent)
500 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
501 if (!__d_rcu_to_refcount(dentry, nd->seq))
504 * If the sequence check on the child dentry passed, then
505 * the child has not been removed from its parent. This
506 * means the parent dentry must be valid and able to take
507 * a reference at this point.
509 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
510 BUG_ON(!parent->d_count);
512 spin_unlock(&dentry->d_lock);
514 spin_unlock(&parent->d_lock);
517 spin_unlock(&fs->lock);
519 mntget(nd->path.mnt);
522 nd->flags &= ~LOOKUP_RCU;
526 spin_unlock(&dentry->d_lock);
528 spin_unlock(&parent->d_lock);
531 spin_unlock(&fs->lock);
535 static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
537 return dentry->d_op->d_revalidate(dentry, flags);
541 * complete_walk - successful completion of path walk
542 * @nd: pointer nameidata
544 * If we had been in RCU mode, drop out of it and legitimize nd->path.
545 * Revalidate the final result, unless we'd already done that during
546 * the path walk or the filesystem doesn't ask for it. Return 0 on
547 * success, -error on failure. In case of failure caller does not
548 * need to drop nd->path.
550 static int complete_walk(struct nameidata *nd)
552 struct dentry *dentry = nd->path.dentry;
555 if (nd->flags & LOOKUP_RCU) {
556 nd->flags &= ~LOOKUP_RCU;
557 if (!(nd->flags & LOOKUP_ROOT))
559 spin_lock(&dentry->d_lock);
560 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
561 spin_unlock(&dentry->d_lock);
565 BUG_ON(nd->inode != dentry->d_inode);
566 spin_unlock(&dentry->d_lock);
567 mntget(nd->path.mnt);
571 if (likely(!(nd->flags & LOOKUP_JUMPED)))
574 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
577 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
580 /* Note: we do not d_invalidate() */
581 status = d_revalidate(dentry, nd->flags);
592 static __always_inline void set_root(struct nameidata *nd)
595 get_fs_root(current->fs, &nd->root);
598 static int link_path_walk(const char *, struct nameidata *);
600 static __always_inline void set_root_rcu(struct nameidata *nd)
603 struct fs_struct *fs = current->fs;
607 seq = read_seqcount_begin(&fs->seq);
609 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
610 } while (read_seqcount_retry(&fs->seq, seq));
614 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
626 nd->flags |= LOOKUP_JUMPED;
628 nd->inode = nd->path.dentry->d_inode;
630 ret = link_path_walk(link, nd);
634 return PTR_ERR(link);
637 static void path_put_conditional(struct path *path, struct nameidata *nd)
640 if (path->mnt != nd->path.mnt)
644 static inline void path_to_nameidata(const struct path *path,
645 struct nameidata *nd)
647 if (!(nd->flags & LOOKUP_RCU)) {
648 dput(nd->path.dentry);
649 if (nd->path.mnt != path->mnt)
650 mntput(nd->path.mnt);
652 nd->path.mnt = path->mnt;
653 nd->path.dentry = path->dentry;
657 * Helper to directly jump to a known parsed path from ->follow_link,
658 * caller must have taken a reference to path beforehand.
660 void nd_jump_link(struct nameidata *nd, struct path *path)
665 nd->inode = nd->path.dentry->d_inode;
666 nd->flags |= LOOKUP_JUMPED;
668 BUG_ON(nd->inode->i_op->follow_link);
671 static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
673 struct inode *inode = link->dentry->d_inode;
674 if (inode->i_op->put_link)
675 inode->i_op->put_link(link->dentry, nd, cookie);
679 int sysctl_protected_symlinks __read_mostly = 1;
680 int sysctl_protected_hardlinks __read_mostly = 1;
683 * may_follow_link - Check symlink following for unsafe situations
684 * @link: The path of the symlink
685 * @nd: nameidata pathwalk data
687 * In the case of the sysctl_protected_symlinks sysctl being enabled,
688 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
689 * in a sticky world-writable directory. This is to protect privileged
690 * processes from failing races against path names that may change out
691 * from under them by way of other users creating malicious symlinks.
692 * It will permit symlinks to be followed only when outside a sticky
693 * world-writable directory, or when the uid of the symlink and follower
694 * match, or when the directory owner matches the symlink's owner.
696 * Returns 0 if following the symlink is allowed, -ve on error.
698 static inline int may_follow_link(struct path *link, struct nameidata *nd)
700 const struct inode *inode;
701 const struct inode *parent;
703 if (!sysctl_protected_symlinks)
706 /* Allowed if owner and follower match. */
707 inode = link->dentry->d_inode;
708 if (uid_eq(current_cred()->fsuid, inode->i_uid))
711 /* Allowed if parent directory not sticky and world-writable. */
712 parent = nd->path.dentry->d_inode;
713 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
716 /* Allowed if parent directory and link owner match. */
717 if (uid_eq(parent->i_uid, inode->i_uid))
720 audit_log_link_denied("follow_link", link);
721 path_put_conditional(link, nd);
727 * safe_hardlink_source - Check for safe hardlink conditions
728 * @inode: the source inode to hardlink from
730 * Return false if at least one of the following conditions:
731 * - inode is not a regular file
733 * - inode is setgid and group-exec
734 * - access failure for read and write
736 * Otherwise returns true.
738 static bool safe_hardlink_source(struct inode *inode)
740 umode_t mode = inode->i_mode;
742 /* Special files should not get pinned to the filesystem. */
746 /* Setuid files should not get pinned to the filesystem. */
750 /* Executable setgid files should not get pinned to the filesystem. */
751 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
754 /* Hardlinking to unreadable or unwritable sources is dangerous. */
755 if (inode_permission(inode, MAY_READ | MAY_WRITE))
762 * may_linkat - Check permissions for creating a hardlink
763 * @link: the source to hardlink from
765 * Block hardlink when all of:
766 * - sysctl_protected_hardlinks enabled
767 * - fsuid does not match inode
768 * - hardlink source is unsafe (see safe_hardlink_source() above)
771 * Returns 0 if successful, -ve on error.
773 static int may_linkat(struct path *link)
775 const struct cred *cred;
778 if (!sysctl_protected_hardlinks)
781 cred = current_cred();
782 inode = link->dentry->d_inode;
784 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
785 * otherwise, it must be a safe source.
787 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
791 audit_log_link_denied("linkat", link);
795 static __always_inline int
796 follow_link(struct path *link, struct nameidata *nd, void **p)
798 struct dentry *dentry = link->dentry;
802 BUG_ON(nd->flags & LOOKUP_RCU);
804 if (link->mnt == nd->path.mnt)
808 if (unlikely(current->total_link_count >= 40))
809 goto out_put_nd_path;
812 current->total_link_count++;
815 nd_set_link(nd, NULL);
817 error = security_inode_follow_link(link->dentry, nd);
819 goto out_put_nd_path;
821 nd->last_type = LAST_BIND;
822 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
825 goto out_put_nd_path;
830 error = __vfs_follow_link(nd, s);
832 put_link(nd, link, *p);
844 static int follow_up_rcu(struct path *path)
846 struct mount *mnt = real_mount(path->mnt);
847 struct mount *parent;
848 struct dentry *mountpoint;
850 parent = mnt->mnt_parent;
851 if (&parent->mnt == path->mnt)
853 mountpoint = mnt->mnt_mountpoint;
854 path->dentry = mountpoint;
855 path->mnt = &parent->mnt;
860 * follow_up - Find the mountpoint of path's vfsmount
862 * Given a path, find the mountpoint of its source file system.
863 * Replace @path with the path of the mountpoint in the parent mount.
866 * Return 1 if we went up a level and 0 if we were already at the
869 int follow_up(struct path *path)
871 struct mount *mnt = real_mount(path->mnt);
872 struct mount *parent;
873 struct dentry *mountpoint;
875 br_read_lock(&vfsmount_lock);
876 parent = mnt->mnt_parent;
878 br_read_unlock(&vfsmount_lock);
881 mntget(&parent->mnt);
882 mountpoint = dget(mnt->mnt_mountpoint);
883 br_read_unlock(&vfsmount_lock);
885 path->dentry = mountpoint;
887 path->mnt = &parent->mnt;
892 * Perform an automount
893 * - return -EISDIR to tell follow_managed() to stop and return the path we
896 static int follow_automount(struct path *path, unsigned flags,
899 struct vfsmount *mnt;
902 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
905 /* We don't want to mount if someone's just doing a stat -
906 * unless they're stat'ing a directory and appended a '/' to
909 * We do, however, want to mount if someone wants to open or
910 * create a file of any type under the mountpoint, wants to
911 * traverse through the mountpoint or wants to open the
912 * mounted directory. Also, autofs may mark negative dentries
913 * as being automount points. These will need the attentions
914 * of the daemon to instantiate them before they can be used.
916 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
917 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
918 path->dentry->d_inode)
921 current->total_link_count++;
922 if (current->total_link_count >= 40)
925 mnt = path->dentry->d_op->d_automount(path);
928 * The filesystem is allowed to return -EISDIR here to indicate
929 * it doesn't want to automount. For instance, autofs would do
930 * this so that its userspace daemon can mount on this dentry.
932 * However, we can only permit this if it's a terminal point in
933 * the path being looked up; if it wasn't then the remainder of
934 * the path is inaccessible and we should say so.
936 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
941 if (!mnt) /* mount collision */
945 /* lock_mount() may release path->mnt on error */
949 err = finish_automount(mnt, path);
953 /* Someone else made a mount here whilst we were busy */
958 path->dentry = dget(mnt->mnt_root);
967 * Handle a dentry that is managed in some way.
968 * - Flagged for transit management (autofs)
969 * - Flagged as mountpoint
970 * - Flagged as automount point
972 * This may only be called in refwalk mode.
974 * Serialization is taken care of in namespace.c
976 static int follow_managed(struct path *path, unsigned flags)
978 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
980 bool need_mntput = false;
983 /* Given that we're not holding a lock here, we retain the value in a
984 * local variable for each dentry as we look at it so that we don't see
985 * the components of that value change under us */
986 while (managed = ACCESS_ONCE(path->dentry->d_flags),
987 managed &= DCACHE_MANAGED_DENTRY,
988 unlikely(managed != 0)) {
989 /* Allow the filesystem to manage the transit without i_mutex
991 if (managed & DCACHE_MANAGE_TRANSIT) {
992 BUG_ON(!path->dentry->d_op);
993 BUG_ON(!path->dentry->d_op->d_manage);
994 ret = path->dentry->d_op->d_manage(path->dentry, false);
999 /* Transit to a mounted filesystem. */
1000 if (managed & DCACHE_MOUNTED) {
1001 struct vfsmount *mounted = lookup_mnt(path);
1006 path->mnt = mounted;
1007 path->dentry = dget(mounted->mnt_root);
1012 /* Something is mounted on this dentry in another
1013 * namespace and/or whatever was mounted there in this
1014 * namespace got unmounted before we managed to get the
1018 /* Handle an automount point */
1019 if (managed & DCACHE_NEED_AUTOMOUNT) {
1020 ret = follow_automount(path, flags, &need_mntput);
1026 /* We didn't change the current path point */
1030 if (need_mntput && path->mnt == mnt)
1034 return ret < 0 ? ret : need_mntput;
1037 int follow_down_one(struct path *path)
1039 struct vfsmount *mounted;
1041 mounted = lookup_mnt(path);
1045 path->mnt = mounted;
1046 path->dentry = dget(mounted->mnt_root);
1052 static inline bool managed_dentry_might_block(struct dentry *dentry)
1054 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1055 dentry->d_op->d_manage(dentry, true) < 0);
1059 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1060 * we meet a managed dentry that would need blocking.
1062 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1063 struct inode **inode)
1066 struct mount *mounted;
1068 * Don't forget we might have a non-mountpoint managed dentry
1069 * that wants to block transit.
1071 if (unlikely(managed_dentry_might_block(path->dentry)))
1074 if (!d_mountpoint(path->dentry))
1077 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1080 path->mnt = &mounted->mnt;
1081 path->dentry = mounted->mnt.mnt_root;
1082 nd->flags |= LOOKUP_JUMPED;
1083 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
1085 * Update the inode too. We don't need to re-check the
1086 * dentry sequence number here after this d_inode read,
1087 * because a mount-point is always pinned.
1089 *inode = path->dentry->d_inode;
1094 static void follow_mount_rcu(struct nameidata *nd)
1096 while (d_mountpoint(nd->path.dentry)) {
1097 struct mount *mounted;
1098 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
1101 nd->path.mnt = &mounted->mnt;
1102 nd->path.dentry = mounted->mnt.mnt_root;
1103 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1107 static int follow_dotdot_rcu(struct nameidata *nd)
1112 if (nd->path.dentry == nd->root.dentry &&
1113 nd->path.mnt == nd->root.mnt) {
1116 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1117 struct dentry *old = nd->path.dentry;
1118 struct dentry *parent = old->d_parent;
1121 seq = read_seqcount_begin(&parent->d_seq);
1122 if (read_seqcount_retry(&old->d_seq, nd->seq))
1124 nd->path.dentry = parent;
1128 if (!follow_up_rcu(&nd->path))
1130 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1132 follow_mount_rcu(nd);
1133 nd->inode = nd->path.dentry->d_inode;
1137 nd->flags &= ~LOOKUP_RCU;
1138 if (!(nd->flags & LOOKUP_ROOT))
1139 nd->root.mnt = NULL;
1145 * Follow down to the covering mount currently visible to userspace. At each
1146 * point, the filesystem owning that dentry may be queried as to whether the
1147 * caller is permitted to proceed or not.
1149 int follow_down(struct path *path)
1154 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1155 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1156 /* Allow the filesystem to manage the transit without i_mutex
1159 * We indicate to the filesystem if someone is trying to mount
1160 * something here. This gives autofs the chance to deny anyone
1161 * other than its daemon the right to mount on its
1164 * The filesystem may sleep at this point.
1166 if (managed & DCACHE_MANAGE_TRANSIT) {
1167 BUG_ON(!path->dentry->d_op);
1168 BUG_ON(!path->dentry->d_op->d_manage);
1169 ret = path->dentry->d_op->d_manage(
1170 path->dentry, false);
1172 return ret == -EISDIR ? 0 : ret;
1175 /* Transit to a mounted filesystem. */
1176 if (managed & DCACHE_MOUNTED) {
1177 struct vfsmount *mounted = lookup_mnt(path);
1182 path->mnt = mounted;
1183 path->dentry = dget(mounted->mnt_root);
1187 /* Don't handle automount points here */
1194 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1196 static void follow_mount(struct path *path)
1198 while (d_mountpoint(path->dentry)) {
1199 struct vfsmount *mounted = lookup_mnt(path);
1204 path->mnt = mounted;
1205 path->dentry = dget(mounted->mnt_root);
1209 static void follow_dotdot(struct nameidata *nd)
1214 struct dentry *old = nd->path.dentry;
1216 if (nd->path.dentry == nd->root.dentry &&
1217 nd->path.mnt == nd->root.mnt) {
1220 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1221 /* rare case of legitimate dget_parent()... */
1222 nd->path.dentry = dget_parent(nd->path.dentry);
1226 if (!follow_up(&nd->path))
1229 follow_mount(&nd->path);
1230 nd->inode = nd->path.dentry->d_inode;
1234 * This looks up the name in dcache, possibly revalidates the old dentry and
1235 * allocates a new one if not found or not valid. In the need_lookup argument
1236 * returns whether i_op->lookup is necessary.
1238 * dir->d_inode->i_mutex must be held
1240 static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1241 unsigned int flags, bool *need_lookup)
1243 struct dentry *dentry;
1246 *need_lookup = false;
1247 dentry = d_lookup(dir, name);
1249 if (d_need_lookup(dentry)) {
1250 *need_lookup = true;
1251 } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1252 error = d_revalidate(dentry, flags);
1253 if (unlikely(error <= 0)) {
1256 return ERR_PTR(error);
1257 } else if (!d_invalidate(dentry)) {
1266 dentry = d_alloc(dir, name);
1267 if (unlikely(!dentry))
1268 return ERR_PTR(-ENOMEM);
1270 *need_lookup = true;
1276 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1277 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1279 * dir->d_inode->i_mutex must be held
1281 static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1286 /* Don't create child dentry for a dead directory. */
1287 if (unlikely(IS_DEADDIR(dir))) {
1289 return ERR_PTR(-ENOENT);
1292 old = dir->i_op->lookup(dir, dentry, flags);
1293 if (unlikely(old)) {
1300 static struct dentry *__lookup_hash(struct qstr *name,
1301 struct dentry *base, unsigned int flags)
1304 struct dentry *dentry;
1306 dentry = lookup_dcache(name, base, flags, &need_lookup);
1310 return lookup_real(base->d_inode, dentry, flags);
1314 * It's more convoluted than I'd like it to be, but... it's still fairly
1315 * small and for now I'd prefer to have fast path as straight as possible.
1316 * It _is_ time-critical.
1318 static int lookup_fast(struct nameidata *nd, struct qstr *name,
1319 struct path *path, struct inode **inode)
1321 struct vfsmount *mnt = nd->path.mnt;
1322 struct dentry *dentry, *parent = nd->path.dentry;
1328 * Rename seqlock is not required here because in the off chance
1329 * of a false negative due to a concurrent rename, we're going to
1330 * do the non-racy lookup, below.
1332 if (nd->flags & LOOKUP_RCU) {
1334 dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
1339 * This sequence count validates that the inode matches
1340 * the dentry name information from lookup.
1342 *inode = dentry->d_inode;
1343 if (read_seqcount_retry(&dentry->d_seq, seq))
1347 * This sequence count validates that the parent had no
1348 * changes while we did the lookup of the dentry above.
1350 * The memory barrier in read_seqcount_begin of child is
1351 * enough, we can use __read_seqcount_retry here.
1353 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1357 if (unlikely(d_need_lookup(dentry)))
1359 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1360 status = d_revalidate(dentry, nd->flags);
1361 if (unlikely(status <= 0)) {
1362 if (status != -ECHILD)
1368 path->dentry = dentry;
1369 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1371 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1375 if (unlazy_walk(nd, dentry))
1378 dentry = __d_lookup(parent, name);
1381 if (unlikely(!dentry))
1384 if (unlikely(d_need_lookup(dentry))) {
1389 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1390 status = d_revalidate(dentry, nd->flags);
1391 if (unlikely(status <= 0)) {
1396 if (!d_invalidate(dentry)) {
1403 path->dentry = dentry;
1404 err = follow_managed(path, nd->flags);
1405 if (unlikely(err < 0)) {
1406 path_put_conditional(path, nd);
1410 nd->flags |= LOOKUP_JUMPED;
1411 *inode = path->dentry->d_inode;
1418 /* Fast lookup failed, do it the slow way */
1419 static int lookup_slow(struct nameidata *nd, struct qstr *name,
1422 struct dentry *dentry, *parent;
1425 parent = nd->path.dentry;
1426 BUG_ON(nd->inode != parent->d_inode);
1428 mutex_lock(&parent->d_inode->i_mutex);
1429 dentry = __lookup_hash(name, parent, nd->flags);
1430 mutex_unlock(&parent->d_inode->i_mutex);
1432 return PTR_ERR(dentry);
1433 path->mnt = nd->path.mnt;
1434 path->dentry = dentry;
1435 err = follow_managed(path, nd->flags);
1436 if (unlikely(err < 0)) {
1437 path_put_conditional(path, nd);
1441 nd->flags |= LOOKUP_JUMPED;
1445 static inline int may_lookup(struct nameidata *nd)
1447 if (nd->flags & LOOKUP_RCU) {
1448 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1451 if (unlazy_walk(nd, NULL))
1454 return inode_permission(nd->inode, MAY_EXEC);
1457 static inline int handle_dots(struct nameidata *nd, int type)
1459 if (type == LAST_DOTDOT) {
1460 if (nd->flags & LOOKUP_RCU) {
1461 if (follow_dotdot_rcu(nd))
1469 static void terminate_walk(struct nameidata *nd)
1471 if (!(nd->flags & LOOKUP_RCU)) {
1472 path_put(&nd->path);
1474 nd->flags &= ~LOOKUP_RCU;
1475 if (!(nd->flags & LOOKUP_ROOT))
1476 nd->root.mnt = NULL;
1482 * Do we need to follow links? We _really_ want to be able
1483 * to do this check without having to look at inode->i_op,
1484 * so we keep a cache of "no, this doesn't need follow_link"
1485 * for the common case.
1487 static inline int should_follow_link(struct inode *inode, int follow)
1489 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1490 if (likely(inode->i_op->follow_link))
1493 /* This gets set once for the inode lifetime */
1494 spin_lock(&inode->i_lock);
1495 inode->i_opflags |= IOP_NOFOLLOW;
1496 spin_unlock(&inode->i_lock);
1501 static inline int walk_component(struct nameidata *nd, struct path *path,
1502 struct qstr *name, int type, int follow)
1504 struct inode *inode;
1507 * "." and ".." are special - ".." especially so because it has
1508 * to be able to know about the current root directory and
1509 * parent relationships.
1511 if (unlikely(type != LAST_NORM))
1512 return handle_dots(nd, type);
1513 err = lookup_fast(nd, name, path, &inode);
1514 if (unlikely(err)) {
1518 err = lookup_slow(nd, name, path);
1522 inode = path->dentry->d_inode;
1528 if (should_follow_link(inode, follow)) {
1529 if (nd->flags & LOOKUP_RCU) {
1530 if (unlikely(unlazy_walk(nd, path->dentry))) {
1535 BUG_ON(inode != path->dentry->d_inode);
1538 path_to_nameidata(path, nd);
1543 path_to_nameidata(path, nd);
1550 * This limits recursive symlink follows to 8, while
1551 * limiting consecutive symlinks to 40.
1553 * Without that kind of total limit, nasty chains of consecutive
1554 * symlinks can cause almost arbitrarily long lookups.
1556 static inline int nested_symlink(struct path *path, struct nameidata *nd)
1560 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1561 path_put_conditional(path, nd);
1562 path_put(&nd->path);
1565 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1568 current->link_count++;
1571 struct path link = *path;
1574 res = follow_link(&link, nd, &cookie);
1577 res = walk_component(nd, path, &nd->last,
1578 nd->last_type, LOOKUP_FOLLOW);
1579 put_link(nd, &link, cookie);
1582 current->link_count--;
1588 * We really don't want to look at inode->i_op->lookup
1589 * when we don't have to. So we keep a cache bit in
1590 * the inode ->i_opflags field that says "yes, we can
1591 * do lookup on this inode".
1593 static inline int can_lookup(struct inode *inode)
1595 if (likely(inode->i_opflags & IOP_LOOKUP))
1597 if (likely(!inode->i_op->lookup))
1600 /* We do this once for the lifetime of the inode */
1601 spin_lock(&inode->i_lock);
1602 inode->i_opflags |= IOP_LOOKUP;
1603 spin_unlock(&inode->i_lock);
1608 * We can do the critical dentry name comparison and hashing
1609 * operations one word at a time, but we are limited to:
1611 * - Architectures with fast unaligned word accesses. We could
1612 * do a "get_unaligned()" if this helps and is sufficiently
1615 * - Little-endian machines (so that we can generate the mask
1616 * of low bytes efficiently). Again, we *could* do a byte
1617 * swapping load on big-endian architectures if that is not
1618 * expensive enough to make the optimization worthless.
1620 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1621 * do not trap on the (extremely unlikely) case of a page
1622 * crossing operation.
1624 * - Furthermore, we need an efficient 64-bit compile for the
1625 * 64-bit case in order to generate the "number of bytes in
1626 * the final mask". Again, that could be replaced with a
1627 * efficient population count instruction or similar.
1629 #ifdef CONFIG_DCACHE_WORD_ACCESS
1631 #include <asm/word-at-a-time.h>
1635 static inline unsigned int fold_hash(unsigned long hash)
1637 hash += hash >> (8*sizeof(int));
1641 #else /* 32-bit case */
1643 #define fold_hash(x) (x)
1647 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1649 unsigned long a, mask;
1650 unsigned long hash = 0;
1653 a = load_unaligned_zeropad(name);
1654 if (len < sizeof(unsigned long))
1658 name += sizeof(unsigned long);
1659 len -= sizeof(unsigned long);
1663 mask = ~(~0ul << len*8);
1666 return fold_hash(hash);
1668 EXPORT_SYMBOL(full_name_hash);
1671 * Calculate the length and hash of the path component, and
1672 * return the length of the component;
1674 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1676 unsigned long a, b, adata, bdata, mask, hash, len;
1677 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1680 len = -sizeof(unsigned long);
1682 hash = (hash + a) * 9;
1683 len += sizeof(unsigned long);
1684 a = load_unaligned_zeropad(name+len);
1685 b = a ^ REPEAT_BYTE('/');
1686 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1688 adata = prep_zero_mask(a, adata, &constants);
1689 bdata = prep_zero_mask(b, bdata, &constants);
1691 mask = create_zero_mask(adata | bdata);
1693 hash += a & zero_bytemask(mask);
1694 *hashp = fold_hash(hash);
1696 return len + find_zero(mask);
1701 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1703 unsigned long hash = init_name_hash();
1705 hash = partial_name_hash(*name++, hash);
1706 return end_name_hash(hash);
1708 EXPORT_SYMBOL(full_name_hash);
1711 * We know there's a real path component here of at least
1714 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1716 unsigned long hash = init_name_hash();
1717 unsigned long len = 0, c;
1719 c = (unsigned char)*name;
1722 hash = partial_name_hash(c, hash);
1723 c = (unsigned char)name[len];
1724 } while (c && c != '/');
1725 *hashp = end_name_hash(hash);
1733 * This is the basic name resolution function, turning a pathname into
1734 * the final dentry. We expect 'base' to be positive and a directory.
1736 * Returns 0 and nd will have valid dentry and mnt on success.
1737 * Returns error and drops reference to input namei data on failure.
1739 static int link_path_walk(const char *name, struct nameidata *nd)
1749 /* At this point we know we have a real path component. */
1755 err = may_lookup(nd);
1759 len = hash_name(name, &this.hash);
1764 if (name[0] == '.') switch (len) {
1766 if (name[1] == '.') {
1768 nd->flags |= LOOKUP_JUMPED;
1774 if (likely(type == LAST_NORM)) {
1775 struct dentry *parent = nd->path.dentry;
1776 nd->flags &= ~LOOKUP_JUMPED;
1777 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1778 err = parent->d_op->d_hash(parent, nd->inode,
1786 goto last_component;
1788 * If it wasn't NUL, we know it was '/'. Skip that
1789 * slash, and continue until no more slashes.
1793 } while (unlikely(name[len] == '/'));
1795 goto last_component;
1798 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1803 err = nested_symlink(&next, nd);
1807 if (can_lookup(nd->inode))
1811 /* here ends the main loop */
1815 nd->last_type = type;
1822 static int path_init(int dfd, const char *name, unsigned int flags,
1823 struct nameidata *nd, struct file **fp)
1827 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1828 nd->flags = flags | LOOKUP_JUMPED;
1830 if (flags & LOOKUP_ROOT) {
1831 struct inode *inode = nd->root.dentry->d_inode;
1833 if (!inode->i_op->lookup)
1835 retval = inode_permission(inode, MAY_EXEC);
1839 nd->path = nd->root;
1841 if (flags & LOOKUP_RCU) {
1843 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1845 path_get(&nd->path);
1850 nd->root.mnt = NULL;
1853 if (flags & LOOKUP_RCU) {
1858 path_get(&nd->root);
1860 nd->path = nd->root;
1861 } else if (dfd == AT_FDCWD) {
1862 if (flags & LOOKUP_RCU) {
1863 struct fs_struct *fs = current->fs;
1869 seq = read_seqcount_begin(&fs->seq);
1871 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1872 } while (read_seqcount_retry(&fs->seq, seq));
1874 get_fs_pwd(current->fs, &nd->path);
1877 struct fd f = fdget_raw(dfd);
1878 struct dentry *dentry;
1883 dentry = f.file->f_path.dentry;
1886 if (!S_ISDIR(dentry->d_inode->i_mode)) {
1891 retval = inode_permission(dentry->d_inode, MAY_EXEC);
1898 nd->path = f.file->f_path;
1899 if (flags & LOOKUP_RCU) {
1902 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1905 path_get(&nd->path);
1910 nd->inode = nd->path.dentry->d_inode;
1914 static inline int lookup_last(struct nameidata *nd, struct path *path)
1916 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1917 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1919 nd->flags &= ~LOOKUP_PARENT;
1920 return walk_component(nd, path, &nd->last, nd->last_type,
1921 nd->flags & LOOKUP_FOLLOW);
1924 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1925 static int path_lookupat(int dfd, const char *name,
1926 unsigned int flags, struct nameidata *nd)
1928 struct file *base = NULL;
1933 * Path walking is largely split up into 2 different synchronisation
1934 * schemes, rcu-walk and ref-walk (explained in
1935 * Documentation/filesystems/path-lookup.txt). These share much of the
1936 * path walk code, but some things particularly setup, cleanup, and
1937 * following mounts are sufficiently divergent that functions are
1938 * duplicated. Typically there is a function foo(), and its RCU
1939 * analogue, foo_rcu().
1941 * -ECHILD is the error number of choice (just to avoid clashes) that
1942 * is returned if some aspect of an rcu-walk fails. Such an error must
1943 * be handled by restarting a traditional ref-walk (which will always
1944 * be able to complete).
1946 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1951 current->total_link_count = 0;
1952 err = link_path_walk(name, nd);
1954 if (!err && !(flags & LOOKUP_PARENT)) {
1955 err = lookup_last(nd, &path);
1958 struct path link = path;
1959 err = may_follow_link(&link, nd);
1962 nd->flags |= LOOKUP_PARENT;
1963 err = follow_link(&link, nd, &cookie);
1966 err = lookup_last(nd, &path);
1967 put_link(nd, &link, cookie);
1972 err = complete_walk(nd);
1974 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1975 if (!nd->inode->i_op->lookup) {
1976 path_put(&nd->path);
1984 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1985 path_put(&nd->root);
1986 nd->root.mnt = NULL;
1991 static int filename_lookup(int dfd, struct filename *name,
1992 unsigned int flags, struct nameidata *nd)
1994 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
1995 if (unlikely(retval == -ECHILD))
1996 retval = path_lookupat(dfd, name->name, flags, nd);
1997 if (unlikely(retval == -ESTALE))
1998 retval = path_lookupat(dfd, name->name,
1999 flags | LOOKUP_REVAL, nd);
2001 if (likely(!retval))
2002 audit_inode(name->name, nd->path.dentry,
2003 flags & LOOKUP_PARENT);
2007 static int do_path_lookup(int dfd, const char *name,
2008 unsigned int flags, struct nameidata *nd)
2010 struct filename filename = { .name = name };
2012 return filename_lookup(dfd, &filename, flags, nd);
2015 /* does lookup, returns the object with parent locked */
2016 struct dentry *kern_path_locked(const char *name, struct path *path)
2018 struct nameidata nd;
2020 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2022 return ERR_PTR(err);
2023 if (nd.last_type != LAST_NORM) {
2025 return ERR_PTR(-EINVAL);
2027 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2028 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
2030 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2038 int kern_path(const char *name, unsigned int flags, struct path *path)
2040 struct nameidata nd;
2041 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2048 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2049 * @dentry: pointer to dentry of the base directory
2050 * @mnt: pointer to vfs mount of the base directory
2051 * @name: pointer to file name
2052 * @flags: lookup flags
2053 * @path: pointer to struct path to fill
2055 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2056 const char *name, unsigned int flags,
2059 struct nameidata nd;
2061 nd.root.dentry = dentry;
2063 BUG_ON(flags & LOOKUP_PARENT);
2064 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2065 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2072 * Restricted form of lookup. Doesn't follow links, single-component only,
2073 * needs parent already locked. Doesn't follow mounts.
2076 static struct dentry *lookup_hash(struct nameidata *nd)
2078 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
2082 * lookup_one_len - filesystem helper to lookup single pathname component
2083 * @name: pathname component to lookup
2084 * @base: base directory to lookup from
2085 * @len: maximum length @len should be interpreted to
2087 * Note that this routine is purely a helper for filesystem usage and should
2088 * not be called by generic code. Also note that by using this function the
2089 * nameidata argument is passed to the filesystem methods and a filesystem
2090 * using this helper needs to be prepared for that.
2092 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2098 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2102 this.hash = full_name_hash(name, len);
2104 return ERR_PTR(-EACCES);
2107 c = *(const unsigned char *)name++;
2108 if (c == '/' || c == '\0')
2109 return ERR_PTR(-EACCES);
2112 * See if the low-level filesystem might want
2113 * to use its own hash..
2115 if (base->d_flags & DCACHE_OP_HASH) {
2116 int err = base->d_op->d_hash(base, base->d_inode, &this);
2118 return ERR_PTR(err);
2121 err = inode_permission(base->d_inode, MAY_EXEC);
2123 return ERR_PTR(err);
2125 return __lookup_hash(&this, base, 0);
2128 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2129 struct path *path, int *empty)
2131 struct nameidata nd;
2132 struct filename *tmp = getname_flags(name, flags, empty);
2133 int err = PTR_ERR(tmp);
2136 BUG_ON(flags & LOOKUP_PARENT);
2138 err = filename_lookup(dfd, tmp, flags, &nd);
2146 int user_path_at(int dfd, const char __user *name, unsigned flags,
2149 return user_path_at_empty(dfd, name, flags, path, NULL);
2153 * NB: most callers don't do anything directly with the reference to the
2154 * to struct filename, but the nd->last pointer points into the name string
2155 * allocated by getname. So we must hold the reference to it until all
2156 * path-walking is complete.
2158 static struct filename *
2159 user_path_parent(int dfd, const char __user *path, struct nameidata *nd)
2161 struct filename *s = getname(path);
2167 error = filename_lookup(dfd, s, LOOKUP_PARENT, nd);
2170 return ERR_PTR(error);
2177 * It's inline, so penalty for filesystems that don't use sticky bit is
2180 static inline int check_sticky(struct inode *dir, struct inode *inode)
2182 kuid_t fsuid = current_fsuid();
2184 if (!(dir->i_mode & S_ISVTX))
2186 if (uid_eq(inode->i_uid, fsuid))
2188 if (uid_eq(dir->i_uid, fsuid))
2190 return !inode_capable(inode, CAP_FOWNER);
2194 * Check whether we can remove a link victim from directory dir, check
2195 * whether the type of victim is right.
2196 * 1. We can't do it if dir is read-only (done in permission())
2197 * 2. We should have write and exec permissions on dir
2198 * 3. We can't remove anything from append-only dir
2199 * 4. We can't do anything with immutable dir (done in permission())
2200 * 5. If the sticky bit on dir is set we should either
2201 * a. be owner of dir, or
2202 * b. be owner of victim, or
2203 * c. have CAP_FOWNER capability
2204 * 6. If the victim is append-only or immutable we can't do antyhing with
2205 * links pointing to it.
2206 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2207 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2208 * 9. We can't remove a root or mountpoint.
2209 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2210 * nfs_async_unlink().
2212 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
2216 if (!victim->d_inode)
2219 BUG_ON(victim->d_parent->d_inode != dir);
2220 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
2222 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
2227 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2228 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
2231 if (!S_ISDIR(victim->d_inode->i_mode))
2233 if (IS_ROOT(victim))
2235 } else if (S_ISDIR(victim->d_inode->i_mode))
2237 if (IS_DEADDIR(dir))
2239 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2244 /* Check whether we can create an object with dentry child in directory
2246 * 1. We can't do it if child already exists (open has special treatment for
2247 * this case, but since we are inlined it's OK)
2248 * 2. We can't do it if dir is read-only (done in permission())
2249 * 3. We should have write and exec permissions on dir
2250 * 4. We can't do it if dir is immutable (done in permission())
2252 static inline int may_create(struct inode *dir, struct dentry *child)
2256 if (IS_DEADDIR(dir))
2258 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2262 * p1 and p2 should be directories on the same fs.
2264 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2269 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2273 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2275 p = d_ancestor(p2, p1);
2277 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2278 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2282 p = d_ancestor(p1, p2);
2284 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2285 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2289 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2290 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2294 void unlock_rename(struct dentry *p1, struct dentry *p2)
2296 mutex_unlock(&p1->d_inode->i_mutex);
2298 mutex_unlock(&p2->d_inode->i_mutex);
2299 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2303 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2306 int error = may_create(dir, dentry);
2310 if (!dir->i_op->create)
2311 return -EACCES; /* shouldn't it be ENOSYS? */
2314 error = security_inode_create(dir, dentry, mode);
2317 error = dir->i_op->create(dir, dentry, mode, want_excl);
2319 fsnotify_create(dir, dentry);
2323 static int may_open(struct path *path, int acc_mode, int flag)
2325 struct dentry *dentry = path->dentry;
2326 struct inode *inode = dentry->d_inode;
2336 switch (inode->i_mode & S_IFMT) {
2340 if (acc_mode & MAY_WRITE)
2345 if (path->mnt->mnt_flags & MNT_NODEV)
2354 error = inode_permission(inode, acc_mode);
2359 * An append-only file must be opened in append mode for writing.
2361 if (IS_APPEND(inode)) {
2362 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2368 /* O_NOATIME can only be set by the owner or superuser */
2369 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2375 static int handle_truncate(struct file *filp)
2377 struct path *path = &filp->f_path;
2378 struct inode *inode = path->dentry->d_inode;
2379 int error = get_write_access(inode);
2383 * Refuse to truncate files with mandatory locks held on them.
2385 error = locks_verify_locked(inode);
2387 error = security_path_truncate(path);
2389 error = do_truncate(path->dentry, 0,
2390 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2393 put_write_access(inode);
2397 static inline int open_to_namei_flags(int flag)
2399 if ((flag & O_ACCMODE) == 3)
2404 static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2406 int error = security_path_mknod(dir, dentry, mode, 0);
2410 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2414 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2418 * Attempt to atomically look up, create and open a file from a negative
2421 * Returns 0 if successful. The file will have been created and attached to
2422 * @file by the filesystem calling finish_open().
2424 * Returns 1 if the file was looked up only or didn't need creating. The
2425 * caller will need to perform the open themselves. @path will have been
2426 * updated to point to the new dentry. This may be negative.
2428 * Returns an error code otherwise.
2430 static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2431 struct path *path, struct file *file,
2432 const struct open_flags *op,
2433 bool got_write, bool need_lookup,
2436 struct inode *dir = nd->path.dentry->d_inode;
2437 unsigned open_flag = open_to_namei_flags(op->open_flag);
2441 int create_error = 0;
2442 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2444 BUG_ON(dentry->d_inode);
2446 /* Don't create child dentry for a dead directory. */
2447 if (unlikely(IS_DEADDIR(dir))) {
2453 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2454 mode &= ~current_umask();
2456 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
2457 open_flag &= ~O_TRUNC;
2458 *opened |= FILE_CREATED;
2462 * Checking write permission is tricky, bacuse we don't know if we are
2463 * going to actually need it: O_CREAT opens should work as long as the
2464 * file exists. But checking existence breaks atomicity. The trick is
2465 * to check access and if not granted clear O_CREAT from the flags.
2467 * Another problem is returing the "right" error value (e.g. for an
2468 * O_EXCL open we want to return EEXIST not EROFS).
2470 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2471 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2472 if (!(open_flag & O_CREAT)) {
2474 * No O_CREATE -> atomicity not a requirement -> fall
2475 * back to lookup + open
2478 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2479 /* Fall back and fail with the right error */
2480 create_error = -EROFS;
2483 /* No side effects, safe to clear O_CREAT */
2484 create_error = -EROFS;
2485 open_flag &= ~O_CREAT;
2489 if (open_flag & O_CREAT) {
2490 error = may_o_create(&nd->path, dentry, mode);
2492 create_error = error;
2493 if (open_flag & O_EXCL)
2495 open_flag &= ~O_CREAT;
2499 if (nd->flags & LOOKUP_DIRECTORY)
2500 open_flag |= O_DIRECTORY;
2502 file->f_path.dentry = DENTRY_NOT_SET;
2503 file->f_path.mnt = nd->path.mnt;
2504 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
2507 if (create_error && error == -ENOENT)
2508 error = create_error;
2512 acc_mode = op->acc_mode;
2513 if (*opened & FILE_CREATED) {
2514 fsnotify_create(dir, dentry);
2515 acc_mode = MAY_OPEN;
2518 if (error) { /* returned 1, that is */
2519 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2523 if (file->f_path.dentry) {
2525 dentry = file->f_path.dentry;
2527 if (create_error && dentry->d_inode == NULL) {
2528 error = create_error;
2535 * We didn't have the inode before the open, so check open permission
2538 error = may_open(&file->f_path, acc_mode, open_flag);
2548 dentry = lookup_real(dir, dentry, nd->flags);
2550 return PTR_ERR(dentry);
2553 int open_flag = op->open_flag;
2555 error = create_error;
2556 if ((open_flag & O_EXCL)) {
2557 if (!dentry->d_inode)
2559 } else if (!dentry->d_inode) {
2561 } else if ((open_flag & O_TRUNC) &&
2562 S_ISREG(dentry->d_inode->i_mode)) {
2565 /* will fail later, go on to get the right error */
2569 path->dentry = dentry;
2570 path->mnt = nd->path.mnt;
2575 * Look up and maybe create and open the last component.
2577 * Must be called with i_mutex held on parent.
2579 * Returns 0 if the file was successfully atomically created (if necessary) and
2580 * opened. In this case the file will be returned attached to @file.
2582 * Returns 1 if the file was not completely opened at this time, though lookups
2583 * and creations will have been performed and the dentry returned in @path will
2584 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2585 * specified then a negative dentry may be returned.
2587 * An error code is returned otherwise.
2589 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2590 * cleared otherwise prior to returning.
2592 static int lookup_open(struct nameidata *nd, struct path *path,
2594 const struct open_flags *op,
2595 bool got_write, int *opened)
2597 struct dentry *dir = nd->path.dentry;
2598 struct inode *dir_inode = dir->d_inode;
2599 struct dentry *dentry;
2603 *opened &= ~FILE_CREATED;
2604 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2606 return PTR_ERR(dentry);
2608 /* Cached positive dentry: will open in f_op->open */
2609 if (!need_lookup && dentry->d_inode)
2612 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
2613 return atomic_open(nd, dentry, path, file, op, got_write,
2614 need_lookup, opened);
2618 BUG_ON(dentry->d_inode);
2620 dentry = lookup_real(dir_inode, dentry, nd->flags);
2622 return PTR_ERR(dentry);
2625 /* Negative dentry, just create the file */
2626 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2627 umode_t mode = op->mode;
2628 if (!IS_POSIXACL(dir->d_inode))
2629 mode &= ~current_umask();
2631 * This write is needed to ensure that a
2632 * rw->ro transition does not occur between
2633 * the time when the file is created and when
2634 * a permanent write count is taken through
2635 * the 'struct file' in finish_open().
2641 *opened |= FILE_CREATED;
2642 error = security_path_mknod(&nd->path, dentry, mode, 0);
2645 error = vfs_create(dir->d_inode, dentry, mode,
2646 nd->flags & LOOKUP_EXCL);
2651 path->dentry = dentry;
2652 path->mnt = nd->path.mnt;
2661 * Handle the last step of open()
2663 static int do_last(struct nameidata *nd, struct path *path,
2664 struct file *file, const struct open_flags *op,
2665 int *opened, struct filename *name)
2667 struct dentry *dir = nd->path.dentry;
2668 int open_flag = op->open_flag;
2669 bool will_truncate = (open_flag & O_TRUNC) != 0;
2670 bool got_write = false;
2671 int acc_mode = op->acc_mode;
2672 struct inode *inode;
2673 bool symlink_ok = false;
2674 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2675 bool retried = false;
2677 const char *pathname = name->name;
2679 nd->flags &= ~LOOKUP_PARENT;
2680 nd->flags |= op->intent;
2682 switch (nd->last_type) {
2685 error = handle_dots(nd, nd->last_type);
2690 error = complete_walk(nd);
2693 audit_inode(pathname, nd->path.dentry, 0);
2694 if (open_flag & O_CREAT) {
2700 error = complete_walk(nd);
2703 audit_inode(pathname, dir, 0);
2707 if (!(open_flag & O_CREAT)) {
2708 if (nd->last.name[nd->last.len])
2709 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2710 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2712 /* we _can_ be in RCU mode here */
2713 error = lookup_fast(nd, &nd->last, path, &inode);
2720 BUG_ON(nd->inode != dir->d_inode);
2722 /* create side of things */
2724 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2725 * has been cleared when we got to the last component we are
2728 error = complete_walk(nd);
2732 audit_inode(pathname, dir, 0);
2734 /* trailing slashes? */
2735 if (nd->last.name[nd->last.len])
2740 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2741 error = mnt_want_write(nd->path.mnt);
2745 * do _not_ fail yet - we might not need that or fail with
2746 * a different error; let lookup_open() decide; we'll be
2747 * dropping this one anyway.
2750 mutex_lock(&dir->d_inode->i_mutex);
2751 error = lookup_open(nd, path, file, op, got_write, opened);
2752 mutex_unlock(&dir->d_inode->i_mutex);
2758 if ((*opened & FILE_CREATED) ||
2759 !S_ISREG(file->f_path.dentry->d_inode->i_mode))
2760 will_truncate = false;
2762 audit_inode(pathname, file->f_path.dentry, 0);
2766 if (*opened & FILE_CREATED) {
2767 /* Don't check for write permission, don't truncate */
2768 open_flag &= ~O_TRUNC;
2769 will_truncate = false;
2770 acc_mode = MAY_OPEN;
2771 path_to_nameidata(path, nd);
2772 goto finish_open_created;
2776 * create/update audit record if it already exists.
2778 if (path->dentry->d_inode)
2779 audit_inode(pathname, path->dentry, 0);
2782 * If atomic_open() acquired write access it is dropped now due to
2783 * possible mount and symlink following (this might be optimized away if
2787 mnt_drop_write(nd->path.mnt);
2792 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
2795 error = follow_managed(path, nd->flags);
2800 nd->flags |= LOOKUP_JUMPED;
2802 BUG_ON(nd->flags & LOOKUP_RCU);
2803 inode = path->dentry->d_inode;
2805 /* we _can_ be in RCU mode here */
2808 path_to_nameidata(path, nd);
2812 if (should_follow_link(inode, !symlink_ok)) {
2813 if (nd->flags & LOOKUP_RCU) {
2814 if (unlikely(unlazy_walk(nd, path->dentry))) {
2819 BUG_ON(inode != path->dentry->d_inode);
2823 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2824 path_to_nameidata(path, nd);
2826 save_parent.dentry = nd->path.dentry;
2827 save_parent.mnt = mntget(path->mnt);
2828 nd->path.dentry = path->dentry;
2832 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2833 error = complete_walk(nd);
2835 path_put(&save_parent);
2839 if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
2842 if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup)
2844 audit_inode(pathname, nd->path.dentry, 0);
2846 if (!S_ISREG(nd->inode->i_mode))
2847 will_truncate = false;
2849 if (will_truncate) {
2850 error = mnt_want_write(nd->path.mnt);
2855 finish_open_created:
2856 error = may_open(&nd->path, acc_mode, open_flag);
2859 file->f_path.mnt = nd->path.mnt;
2860 error = finish_open(file, nd->path.dentry, NULL, opened);
2862 if (error == -EOPENSTALE)
2867 error = open_check_o_direct(file);
2870 error = ima_file_check(file, op->acc_mode);
2874 if (will_truncate) {
2875 error = handle_truncate(file);
2881 mnt_drop_write(nd->path.mnt);
2882 path_put(&save_parent);
2887 path_put_conditional(path, nd);
2894 /* If no saved parent or already retried then can't retry */
2895 if (!save_parent.dentry || retried)
2898 BUG_ON(save_parent.dentry != dir);
2899 path_put(&nd->path);
2900 nd->path = save_parent;
2901 nd->inode = dir->d_inode;
2902 save_parent.mnt = NULL;
2903 save_parent.dentry = NULL;
2905 mnt_drop_write(nd->path.mnt);
2912 static struct file *path_openat(int dfd, struct filename *pathname,
2913 struct nameidata *nd, const struct open_flags *op, int flags)
2915 struct file *base = NULL;
2921 file = get_empty_filp();
2923 return ERR_PTR(-ENFILE);
2925 file->f_flags = op->open_flag;
2927 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
2928 if (unlikely(error))
2931 current->total_link_count = 0;
2932 error = link_path_walk(pathname->name, nd);
2933 if (unlikely(error))
2936 error = do_last(nd, &path, file, op, &opened, pathname);
2937 while (unlikely(error > 0)) { /* trailing symlink */
2938 struct path link = path;
2940 if (!(nd->flags & LOOKUP_FOLLOW)) {
2941 path_put_conditional(&path, nd);
2942 path_put(&nd->path);
2946 error = may_follow_link(&link, nd);
2947 if (unlikely(error))
2949 nd->flags |= LOOKUP_PARENT;
2950 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2951 error = follow_link(&link, nd, &cookie);
2952 if (unlikely(error))
2954 error = do_last(nd, &path, file, op, &opened, pathname);
2955 put_link(nd, &link, cookie);
2958 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2959 path_put(&nd->root);
2962 if (!(opened & FILE_OPENED)) {
2966 if (unlikely(error)) {
2967 if (error == -EOPENSTALE) {
2968 if (flags & LOOKUP_RCU)
2973 file = ERR_PTR(error);
2978 struct file *do_filp_open(int dfd, struct filename *pathname,
2979 const struct open_flags *op, int flags)
2981 struct nameidata nd;
2984 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
2985 if (unlikely(filp == ERR_PTR(-ECHILD)))
2986 filp = path_openat(dfd, pathname, &nd, op, flags);
2987 if (unlikely(filp == ERR_PTR(-ESTALE)))
2988 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
2992 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2993 const char *name, const struct open_flags *op, int flags)
2995 struct nameidata nd;
2997 struct filename filename = { .name = name };
3000 nd.root.dentry = dentry;
3002 flags |= LOOKUP_ROOT;
3004 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
3005 return ERR_PTR(-ELOOP);
3007 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
3008 if (unlikely(file == ERR_PTR(-ECHILD)))
3009 file = path_openat(-1, &filename, &nd, op, flags);
3010 if (unlikely(file == ERR_PTR(-ESTALE)))
3011 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
3015 struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
3017 struct dentry *dentry = ERR_PTR(-EEXIST);
3018 struct nameidata nd;
3020 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
3022 return ERR_PTR(error);
3025 * Yucky last component or no last component at all?
3026 * (foo/., foo/.., /////)
3028 if (nd.last_type != LAST_NORM)
3030 nd.flags &= ~LOOKUP_PARENT;
3031 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3033 /* don't fail immediately if it's r/o, at least try to report other errors */
3034 err2 = mnt_want_write(nd.path.mnt);
3036 * Do the final lookup.
3038 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3039 dentry = lookup_hash(&nd);
3044 if (dentry->d_inode)
3047 * Special case - lookup gave negative, but... we had foo/bar/
3048 * From the vfs_mknod() POV we just have a negative dentry -
3049 * all is fine. Let's be bastards - you had / on the end, you've
3050 * been asking for (non-existent) directory. -ENOENT for you.
3052 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
3056 if (unlikely(err2)) {
3064 dentry = ERR_PTR(error);
3066 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3068 mnt_drop_write(nd.path.mnt);
3073 EXPORT_SYMBOL(kern_path_create);
3075 void done_path_create(struct path *path, struct dentry *dentry)
3078 mutex_unlock(&path->dentry->d_inode->i_mutex);
3079 mnt_drop_write(path->mnt);
3082 EXPORT_SYMBOL(done_path_create);
3084 struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
3086 struct filename *tmp = getname(pathname);
3089 return ERR_CAST(tmp);
3090 res = kern_path_create(dfd, tmp->name, path, is_dir);
3094 EXPORT_SYMBOL(user_path_create);
3096 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3098 int error = may_create(dir, dentry);
3103 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
3106 if (!dir->i_op->mknod)
3109 error = devcgroup_inode_mknod(mode, dev);
3113 error = security_inode_mknod(dir, dentry, mode, dev);
3117 error = dir->i_op->mknod(dir, dentry, mode, dev);
3119 fsnotify_create(dir, dentry);
3123 static int may_mknod(umode_t mode)
3125 switch (mode & S_IFMT) {
3131 case 0: /* zero mode translates to S_IFREG */
3140 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3143 struct dentry *dentry;
3147 error = may_mknod(mode);
3151 dentry = user_path_create(dfd, filename, &path, 0);
3153 return PTR_ERR(dentry);
3155 if (!IS_POSIXACL(path.dentry->d_inode))
3156 mode &= ~current_umask();
3157 error = security_path_mknod(&path, dentry, mode, dev);
3160 switch (mode & S_IFMT) {
3161 case 0: case S_IFREG:
3162 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
3164 case S_IFCHR: case S_IFBLK:
3165 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
3166 new_decode_dev(dev));
3168 case S_IFIFO: case S_IFSOCK:
3169 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
3173 done_path_create(&path, dentry);
3177 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
3179 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3182 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3184 int error = may_create(dir, dentry);
3185 unsigned max_links = dir->i_sb->s_max_links;
3190 if (!dir->i_op->mkdir)
3193 mode &= (S_IRWXUGO|S_ISVTX);
3194 error = security_inode_mkdir(dir, dentry, mode);
3198 if (max_links && dir->i_nlink >= max_links)
3201 error = dir->i_op->mkdir(dir, dentry, mode);
3203 fsnotify_mkdir(dir, dentry);
3207 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3209 struct dentry *dentry;
3213 dentry = user_path_create(dfd, pathname, &path, 1);
3215 return PTR_ERR(dentry);
3217 if (!IS_POSIXACL(path.dentry->d_inode))
3218 mode &= ~current_umask();
3219 error = security_path_mkdir(&path, dentry, mode);
3221 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3222 done_path_create(&path, dentry);
3226 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
3228 return sys_mkdirat(AT_FDCWD, pathname, mode);
3232 * The dentry_unhash() helper will try to drop the dentry early: we
3233 * should have a usage count of 1 if we're the only user of this
3234 * dentry, and if that is true (possibly after pruning the dcache),
3235 * then we drop the dentry now.
3237 * A low-level filesystem can, if it choses, legally
3240 * if (!d_unhashed(dentry))
3243 * if it cannot handle the case of removing a directory
3244 * that is still in use by something else..
3246 void dentry_unhash(struct dentry *dentry)
3248 shrink_dcache_parent(dentry);
3249 spin_lock(&dentry->d_lock);
3250 if (dentry->d_count == 1)
3252 spin_unlock(&dentry->d_lock);
3255 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3257 int error = may_delete(dir, dentry, 1);
3262 if (!dir->i_op->rmdir)
3266 mutex_lock(&dentry->d_inode->i_mutex);
3269 if (d_mountpoint(dentry))
3272 error = security_inode_rmdir(dir, dentry);
3276 shrink_dcache_parent(dentry);
3277 error = dir->i_op->rmdir(dir, dentry);
3281 dentry->d_inode->i_flags |= S_DEAD;
3285 mutex_unlock(&dentry->d_inode->i_mutex);
3292 static long do_rmdir(int dfd, const char __user *pathname)
3295 struct filename *name;
3296 struct dentry *dentry;
3297 struct nameidata nd;
3299 name = user_path_parent(dfd, pathname, &nd);
3301 return PTR_ERR(name);
3303 switch(nd.last_type) {
3315 nd.flags &= ~LOOKUP_PARENT;
3316 error = mnt_want_write(nd.path.mnt);
3320 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3321 dentry = lookup_hash(&nd);
3322 error = PTR_ERR(dentry);
3325 if (!dentry->d_inode) {
3329 error = security_path_rmdir(&nd.path, dentry);
3332 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
3336 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3337 mnt_drop_write(nd.path.mnt);
3344 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
3346 return do_rmdir(AT_FDCWD, pathname);
3349 int vfs_unlink(struct inode *dir, struct dentry *dentry)
3351 int error = may_delete(dir, dentry, 0);
3356 if (!dir->i_op->unlink)
3359 mutex_lock(&dentry->d_inode->i_mutex);
3360 if (d_mountpoint(dentry))
3363 error = security_inode_unlink(dir, dentry);
3365 error = dir->i_op->unlink(dir, dentry);
3370 mutex_unlock(&dentry->d_inode->i_mutex);
3372 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3373 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3374 fsnotify_link_count(dentry->d_inode);
3382 * Make sure that the actual truncation of the file will occur outside its
3383 * directory's i_mutex. Truncate can take a long time if there is a lot of
3384 * writeout happening, and we don't want to prevent access to the directory
3385 * while waiting on the I/O.
3387 static long do_unlinkat(int dfd, const char __user *pathname)
3390 struct filename *name;
3391 struct dentry *dentry;
3392 struct nameidata nd;
3393 struct inode *inode = NULL;
3395 name = user_path_parent(dfd, pathname, &nd);
3397 return PTR_ERR(name);
3400 if (nd.last_type != LAST_NORM)
3403 nd.flags &= ~LOOKUP_PARENT;
3404 error = mnt_want_write(nd.path.mnt);
3408 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3409 dentry = lookup_hash(&nd);
3410 error = PTR_ERR(dentry);
3411 if (!IS_ERR(dentry)) {
3412 /* Why not before? Because we want correct error value */
3413 if (nd.last.name[nd.last.len])
3415 inode = dentry->d_inode;
3419 error = security_path_unlink(&nd.path, dentry);
3422 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
3426 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3428 iput(inode); /* truncate the inode here */
3429 mnt_drop_write(nd.path.mnt);
3436 error = !dentry->d_inode ? -ENOENT :
3437 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
3441 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
3443 if ((flag & ~AT_REMOVEDIR) != 0)
3446 if (flag & AT_REMOVEDIR)
3447 return do_rmdir(dfd, pathname);
3449 return do_unlinkat(dfd, pathname);
3452 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
3454 return do_unlinkat(AT_FDCWD, pathname);
3457 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
3459 int error = may_create(dir, dentry);
3464 if (!dir->i_op->symlink)
3467 error = security_inode_symlink(dir, dentry, oldname);
3471 error = dir->i_op->symlink(dir, dentry, oldname);
3473 fsnotify_create(dir, dentry);
3477 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3478 int, newdfd, const char __user *, newname)
3481 struct filename *from;
3482 struct dentry *dentry;
3485 from = getname(oldname);
3487 return PTR_ERR(from);
3489 dentry = user_path_create(newdfd, newname, &path, 0);
3490 error = PTR_ERR(dentry);
3494 error = security_path_symlink(&path, dentry, from->name);
3496 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
3497 done_path_create(&path, dentry);
3503 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
3505 return sys_symlinkat(oldname, AT_FDCWD, newname);
3508 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3510 struct inode *inode = old_dentry->d_inode;
3511 unsigned max_links = dir->i_sb->s_max_links;
3517 error = may_create(dir, new_dentry);
3521 if (dir->i_sb != inode->i_sb)
3525 * A link to an append-only or immutable file cannot be created.
3527 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3529 if (!dir->i_op->link)
3531 if (S_ISDIR(inode->i_mode))
3534 error = security_inode_link(old_dentry, dir, new_dentry);
3538 mutex_lock(&inode->i_mutex);
3539 /* Make sure we don't allow creating hardlink to an unlinked file */
3540 if (inode->i_nlink == 0)
3542 else if (max_links && inode->i_nlink >= max_links)
3545 error = dir->i_op->link(old_dentry, dir, new_dentry);
3546 mutex_unlock(&inode->i_mutex);
3548 fsnotify_link(dir, inode, new_dentry);
3553 * Hardlinks are often used in delicate situations. We avoid
3554 * security-related surprises by not following symlinks on the
3557 * We don't follow them on the oldname either to be compatible
3558 * with linux 2.0, and to avoid hard-linking to directories
3559 * and other special files. --ADM
3561 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3562 int, newdfd, const char __user *, newname, int, flags)
3564 struct dentry *new_dentry;
3565 struct path old_path, new_path;
3569 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3572 * To use null names we require CAP_DAC_READ_SEARCH
3573 * This ensures that not everyone will be able to create
3574 * handlink using the passed filedescriptor.
3576 if (flags & AT_EMPTY_PATH) {
3577 if (!capable(CAP_DAC_READ_SEARCH))
3582 if (flags & AT_SYMLINK_FOLLOW)
3583 how |= LOOKUP_FOLLOW;
3585 error = user_path_at(olddfd, oldname, how, &old_path);
3589 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
3590 error = PTR_ERR(new_dentry);
3591 if (IS_ERR(new_dentry))
3595 if (old_path.mnt != new_path.mnt)
3597 error = may_linkat(&old_path);
3598 if (unlikely(error))
3600 error = security_path_link(old_path.dentry, &new_path, new_dentry);
3603 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3605 done_path_create(&new_path, new_dentry);
3607 path_put(&old_path);
3612 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
3614 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
3618 * The worst of all namespace operations - renaming directory. "Perverted"
3619 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3621 * a) we can get into loop creation. Check is done in is_subdir().
3622 * b) race potential - two innocent renames can create a loop together.
3623 * That's where 4.4 screws up. Current fix: serialization on
3624 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3626 * c) we have to lock _three_ objects - parents and victim (if it exists).
3627 * And that - after we got ->i_mutex on parents (until then we don't know
3628 * whether the target exists). Solution: try to be smart with locking
3629 * order for inodes. We rely on the fact that tree topology may change
3630 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
3631 * move will be locked. Thus we can rank directories by the tree
3632 * (ancestors first) and rank all non-directories after them.
3633 * That works since everybody except rename does "lock parent, lookup,
3634 * lock child" and rename is under ->s_vfs_rename_mutex.
3635 * HOWEVER, it relies on the assumption that any object with ->lookup()
3636 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3637 * we'd better make sure that there's no link(2) for them.
3638 * d) conversion from fhandle to dentry may come in the wrong moment - when
3639 * we are removing the target. Solution: we will have to grab ->i_mutex
3640 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3641 * ->i_mutex on parents, which works but leads to some truly excessive
3644 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3645 struct inode *new_dir, struct dentry *new_dentry)
3648 struct inode *target = new_dentry->d_inode;
3649 unsigned max_links = new_dir->i_sb->s_max_links;
3652 * If we are going to change the parent - check write permissions,
3653 * we'll need to flip '..'.
3655 if (new_dir != old_dir) {
3656 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
3661 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3667 mutex_lock(&target->i_mutex);
3670 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3674 if (max_links && !target && new_dir != old_dir &&
3675 new_dir->i_nlink >= max_links)
3679 shrink_dcache_parent(new_dentry);
3680 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3685 target->i_flags |= S_DEAD;
3686 dont_mount(new_dentry);
3690 mutex_unlock(&target->i_mutex);
3693 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3694 d_move(old_dentry,new_dentry);
3698 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3699 struct inode *new_dir, struct dentry *new_dentry)
3701 struct inode *target = new_dentry->d_inode;
3704 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3710 mutex_lock(&target->i_mutex);
3713 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3716 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3721 dont_mount(new_dentry);
3722 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3723 d_move(old_dentry, new_dentry);
3726 mutex_unlock(&target->i_mutex);
3731 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3732 struct inode *new_dir, struct dentry *new_dentry)
3735 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
3736 const unsigned char *old_name;
3738 if (old_dentry->d_inode == new_dentry->d_inode)
3741 error = may_delete(old_dir, old_dentry, is_dir);
3745 if (!new_dentry->d_inode)
3746 error = may_create(new_dir, new_dentry);
3748 error = may_delete(new_dir, new_dentry, is_dir);
3752 if (!old_dir->i_op->rename)
3755 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3758 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3760 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3762 fsnotify_move(old_dir, new_dir, old_name, is_dir,
3763 new_dentry->d_inode, old_dentry);
3764 fsnotify_oldname_free(old_name);
3769 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3770 int, newdfd, const char __user *, newname)
3772 struct dentry *old_dir, *new_dir;
3773 struct dentry *old_dentry, *new_dentry;
3774 struct dentry *trap;
3775 struct nameidata oldnd, newnd;
3776 struct filename *from;
3777 struct filename *to;
3780 from = user_path_parent(olddfd, oldname, &oldnd);
3782 error = PTR_ERR(from);
3786 to = user_path_parent(newdfd, newname, &newnd);
3788 error = PTR_ERR(to);
3793 if (oldnd.path.mnt != newnd.path.mnt)
3796 old_dir = oldnd.path.dentry;
3798 if (oldnd.last_type != LAST_NORM)
3801 new_dir = newnd.path.dentry;
3802 if (newnd.last_type != LAST_NORM)
3805 error = mnt_want_write(oldnd.path.mnt);
3809 oldnd.flags &= ~LOOKUP_PARENT;
3810 newnd.flags &= ~LOOKUP_PARENT;
3811 newnd.flags |= LOOKUP_RENAME_TARGET;
3813 trap = lock_rename(new_dir, old_dir);
3815 old_dentry = lookup_hash(&oldnd);
3816 error = PTR_ERR(old_dentry);
3817 if (IS_ERR(old_dentry))
3819 /* source must exist */
3821 if (!old_dentry->d_inode)
3823 /* unless the source is a directory trailing slashes give -ENOTDIR */
3824 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3826 if (oldnd.last.name[oldnd.last.len])
3828 if (newnd.last.name[newnd.last.len])
3831 /* source should not be ancestor of target */
3833 if (old_dentry == trap)
3835 new_dentry = lookup_hash(&newnd);
3836 error = PTR_ERR(new_dentry);
3837 if (IS_ERR(new_dentry))
3839 /* target should not be an ancestor of source */
3841 if (new_dentry == trap)
3844 error = security_path_rename(&oldnd.path, old_dentry,
3845 &newnd.path, new_dentry);
3848 error = vfs_rename(old_dir->d_inode, old_dentry,
3849 new_dir->d_inode, new_dentry);
3855 unlock_rename(new_dir, old_dir);
3856 mnt_drop_write(oldnd.path.mnt);
3858 path_put(&newnd.path);
3861 path_put(&oldnd.path);
3867 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
3869 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3872 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3876 len = PTR_ERR(link);
3881 if (len > (unsigned) buflen)
3883 if (copy_to_user(buffer, link, len))
3890 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3891 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3892 * using) it for any given inode is up to filesystem.
3894 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3896 struct nameidata nd;
3901 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3903 return PTR_ERR(cookie);
3905 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3906 if (dentry->d_inode->i_op->put_link)
3907 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3911 int vfs_follow_link(struct nameidata *nd, const char *link)
3913 return __vfs_follow_link(nd, link);
3916 /* get the link contents into pagecache */
3917 static char *page_getlink(struct dentry * dentry, struct page **ppage)
3921 struct address_space *mapping = dentry->d_inode->i_mapping;
3922 page = read_mapping_page(mapping, 0, NULL);
3927 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3931 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3933 struct page *page = NULL;
3934 char *s = page_getlink(dentry, &page);
3935 int res = vfs_readlink(dentry,buffer,buflen,s);
3938 page_cache_release(page);
3943 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
3945 struct page *page = NULL;
3946 nd_set_link(nd, page_getlink(dentry, &page));
3950 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
3952 struct page *page = cookie;
3956 page_cache_release(page);
3961 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3963 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
3965 struct address_space *mapping = inode->i_mapping;
3970 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3972 flags |= AOP_FLAG_NOFS;
3975 err = pagecache_write_begin(NULL, mapping, 0, len-1,
3976 flags, &page, &fsdata);
3980 kaddr = kmap_atomic(page);
3981 memcpy(kaddr, symname, len-1);
3982 kunmap_atomic(kaddr);
3984 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3991 mark_inode_dirty(inode);
3997 int page_symlink(struct inode *inode, const char *symname, int len)
3999 return __page_symlink(inode, symname, len,
4000 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
4003 const struct inode_operations page_symlink_inode_operations = {
4004 .readlink = generic_readlink,
4005 .follow_link = page_follow_link_light,
4006 .put_link = page_put_link,
4009 EXPORT_SYMBOL(user_path_at);
4010 EXPORT_SYMBOL(follow_down_one);
4011 EXPORT_SYMBOL(follow_down);
4012 EXPORT_SYMBOL(follow_up);
4013 EXPORT_SYMBOL(get_write_access); /* nfsd */
4014 EXPORT_SYMBOL(lock_rename);
4015 EXPORT_SYMBOL(lookup_one_len);
4016 EXPORT_SYMBOL(page_follow_link_light);
4017 EXPORT_SYMBOL(page_put_link);
4018 EXPORT_SYMBOL(page_readlink);
4019 EXPORT_SYMBOL(__page_symlink);
4020 EXPORT_SYMBOL(page_symlink);
4021 EXPORT_SYMBOL(page_symlink_inode_operations);
4022 EXPORT_SYMBOL(kern_path);
4023 EXPORT_SYMBOL(vfs_path_lookup);
4024 EXPORT_SYMBOL(inode_permission);
4025 EXPORT_SYMBOL(unlock_rename);
4026 EXPORT_SYMBOL(vfs_create);
4027 EXPORT_SYMBOL(vfs_follow_link);
4028 EXPORT_SYMBOL(vfs_link);
4029 EXPORT_SYMBOL(vfs_mkdir);
4030 EXPORT_SYMBOL(vfs_mknod);
4031 EXPORT_SYMBOL(generic_permission);
4032 EXPORT_SYMBOL(vfs_readlink);
4033 EXPORT_SYMBOL(vfs_rename);
4034 EXPORT_SYMBOL(vfs_rmdir);
4035 EXPORT_SYMBOL(vfs_symlink);
4036 EXPORT_SYMBOL(vfs_unlink);
4037 EXPORT_SYMBOL(dentry_unhash);
4038 EXPORT_SYMBOL(generic_readlink);