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 if (name->separate) {
123 __putname(name->name);
130 #define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename))
132 static struct filename *
133 getname_flags(const char __user *filename, int flags, int *empty)
135 struct filename *result, *err;
140 result = audit_reusename(filename);
144 result = __getname();
145 if (unlikely(!result))
146 return ERR_PTR(-ENOMEM);
149 * First, try to embed the struct filename inside the names_cache
152 kname = (char *)result + sizeof(*result);
153 result->name = kname;
154 result->separate = false;
155 max = EMBEDDED_NAME_MAX;
158 len = strncpy_from_user(kname, filename, max);
159 if (unlikely(len < 0)) {
165 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
166 * separate struct filename so we can dedicate the entire
167 * names_cache allocation for the pathname, and re-do the copy from
170 if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
171 kname = (char *)result;
173 result = kzalloc(sizeof(*result), GFP_KERNEL);
175 err = ERR_PTR(-ENOMEM);
176 result = (struct filename *)kname;
179 result->name = kname;
180 result->separate = true;
185 /* The empty path is special. */
186 if (unlikely(!len)) {
189 err = ERR_PTR(-ENOENT);
190 if (!(flags & LOOKUP_EMPTY))
194 err = ERR_PTR(-ENAMETOOLONG);
195 if (unlikely(len >= PATH_MAX))
198 result->uptr = filename;
199 result->aname = NULL;
200 audit_getname(result);
204 final_putname(result);
209 getname(const char __user * filename)
211 return getname_flags(filename, 0, NULL);
215 * The "getname_kernel()" interface doesn't do pathnames longer
216 * than EMBEDDED_NAME_MAX. Deal with it - you're a kernel user.
219 getname_kernel(const char * filename)
221 struct filename *result;
225 len = strlen(filename);
226 if (len >= EMBEDDED_NAME_MAX)
227 return ERR_PTR(-ENAMETOOLONG);
229 result = __getname();
230 if (unlikely(!result))
231 return ERR_PTR(-ENOMEM);
233 kname = (char *)result + sizeof(*result);
234 result->name = kname;
236 result->aname = NULL;
237 result->separate = false;
239 strlcpy(kname, filename, EMBEDDED_NAME_MAX);
243 #ifdef CONFIG_AUDITSYSCALL
244 void putname(struct filename *name)
246 if (unlikely(!audit_dummy_context()))
247 return audit_putname(name);
252 static int check_acl(struct inode *inode, int mask)
254 #ifdef CONFIG_FS_POSIX_ACL
255 struct posix_acl *acl;
257 if (mask & MAY_NOT_BLOCK) {
258 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
261 /* no ->get_acl() calls in RCU mode... */
262 if (acl == ACL_NOT_CACHED)
264 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
267 acl = get_acl(inode, ACL_TYPE_ACCESS);
271 int error = posix_acl_permission(inode, acl, mask);
272 posix_acl_release(acl);
281 * This does the basic permission checking
283 static int acl_permission_check(struct inode *inode, int mask)
285 unsigned int mode = inode->i_mode;
287 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
290 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
291 int error = check_acl(inode, mask);
292 if (error != -EAGAIN)
296 if (in_group_p(inode->i_gid))
301 * If the DACs are ok we don't need any capability check.
303 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
309 * generic_permission - check for access rights on a Posix-like filesystem
310 * @inode: inode to check access rights for
311 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
313 * Used to check for read/write/execute permissions on a file.
314 * We use "fsuid" for this, letting us set arbitrary permissions
315 * for filesystem access without changing the "normal" uids which
316 * are used for other things.
318 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
319 * request cannot be satisfied (eg. requires blocking or too much complexity).
320 * It would then be called again in ref-walk mode.
322 int generic_permission(struct inode *inode, int mask)
327 * Do the basic permission checks.
329 ret = acl_permission_check(inode, mask);
333 if (S_ISDIR(inode->i_mode)) {
334 /* DACs are overridable for directories */
335 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
337 if (!(mask & MAY_WRITE))
338 if (capable_wrt_inode_uidgid(inode,
339 CAP_DAC_READ_SEARCH))
344 * Read/write DACs are always overridable.
345 * Executable DACs are overridable when there is
346 * at least one exec bit set.
348 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
349 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
353 * Searching includes executable on directories, else just read.
355 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
356 if (mask == MAY_READ)
357 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
364 * We _really_ want to just do "generic_permission()" without
365 * even looking at the inode->i_op values. So we keep a cache
366 * flag in inode->i_opflags, that says "this has not special
367 * permission function, use the fast case".
369 static inline int do_inode_permission(struct inode *inode, int mask)
371 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
372 if (likely(inode->i_op->permission))
373 return inode->i_op->permission(inode, mask);
375 /* This gets set once for the inode lifetime */
376 spin_lock(&inode->i_lock);
377 inode->i_opflags |= IOP_FASTPERM;
378 spin_unlock(&inode->i_lock);
380 return generic_permission(inode, mask);
384 * __inode_permission - Check for access rights to a given inode
385 * @inode: Inode to check permission on
386 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
388 * Check for read/write/execute permissions on an inode.
390 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
392 * This does not check for a read-only file system. You probably want
393 * inode_permission().
395 int __inode_permission(struct inode *inode, int mask)
399 if (unlikely(mask & MAY_WRITE)) {
401 * Nobody gets write access to an immutable file.
403 if (IS_IMMUTABLE(inode))
407 retval = do_inode_permission(inode, mask);
411 retval = devcgroup_inode_permission(inode, mask);
415 return security_inode_permission(inode, mask);
419 * sb_permission - Check superblock-level permissions
420 * @sb: Superblock of inode to check permission on
421 * @inode: Inode to check permission on
422 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
424 * Separate out file-system wide checks from inode-specific permission checks.
426 static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
428 if (unlikely(mask & MAY_WRITE)) {
429 umode_t mode = inode->i_mode;
431 /* Nobody gets write access to a read-only fs. */
432 if ((sb->s_flags & MS_RDONLY) &&
433 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
440 * inode_permission - Check for access rights to a given inode
441 * @inode: Inode to check permission on
442 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
444 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
445 * this, letting us set arbitrary permissions for filesystem access without
446 * changing the "normal" UIDs which are used for other things.
448 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
450 int inode_permission(struct inode *inode, int mask)
454 retval = sb_permission(inode->i_sb, inode, mask);
457 return __inode_permission(inode, mask);
461 * path_get - get a reference to a path
462 * @path: path to get the reference to
464 * Given a path increment the reference count to the dentry and the vfsmount.
466 void path_get(const struct path *path)
471 EXPORT_SYMBOL(path_get);
474 * path_put - put a reference to a path
475 * @path: path to put the reference to
477 * Given a path decrement the reference count to the dentry and the vfsmount.
479 void path_put(const struct path *path)
484 EXPORT_SYMBOL(path_put);
487 * Path walking has 2 modes, rcu-walk and ref-walk (see
488 * Documentation/filesystems/path-lookup.txt). In situations when we can't
489 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
490 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
491 * mode. Refcounts are grabbed at the last known good point before rcu-walk
492 * got stuck, so ref-walk may continue from there. If this is not successful
493 * (eg. a seqcount has changed), then failure is returned and it's up to caller
494 * to restart the path walk from the beginning in ref-walk mode.
498 * unlazy_walk - try to switch to ref-walk mode.
499 * @nd: nameidata pathwalk data
500 * @dentry: child of nd->path.dentry or NULL
501 * Returns: 0 on success, -ECHILD on failure
503 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
504 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
505 * @nd or NULL. Must be called from rcu-walk context.
507 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
509 struct fs_struct *fs = current->fs;
510 struct dentry *parent = nd->path.dentry;
512 BUG_ON(!(nd->flags & LOOKUP_RCU));
515 * After legitimizing the bastards, terminate_walk()
516 * will do the right thing for non-RCU mode, and all our
517 * subsequent exit cases should rcu_read_unlock()
518 * before returning. Do vfsmount first; if dentry
519 * can't be legitimized, just set nd->path.dentry to NULL
520 * and rely on dput(NULL) being a no-op.
522 if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
524 nd->flags &= ~LOOKUP_RCU;
526 if (!lockref_get_not_dead(&parent->d_lockref)) {
527 nd->path.dentry = NULL;
532 * For a negative lookup, the lookup sequence point is the parents
533 * sequence point, and it only needs to revalidate the parent dentry.
535 * For a positive lookup, we need to move both the parent and the
536 * dentry from the RCU domain to be properly refcounted. And the
537 * sequence number in the dentry validates *both* dentry counters,
538 * since we checked the sequence number of the parent after we got
539 * the child sequence number. So we know the parent must still
540 * be valid if the child sequence number is still valid.
543 if (read_seqcount_retry(&parent->d_seq, nd->seq))
545 BUG_ON(nd->inode != parent->d_inode);
547 if (!lockref_get_not_dead(&dentry->d_lockref))
549 if (read_seqcount_retry(&dentry->d_seq, nd->seq))
554 * Sequence counts matched. Now make sure that the root is
555 * still valid and get it if required.
557 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
558 spin_lock(&fs->lock);
559 if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
560 goto unlock_and_drop_dentry;
562 spin_unlock(&fs->lock);
568 unlock_and_drop_dentry:
569 spin_unlock(&fs->lock);
577 if (!(nd->flags & LOOKUP_ROOT))
582 static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
584 return dentry->d_op->d_revalidate(dentry, flags);
588 * complete_walk - successful completion of path walk
589 * @nd: pointer nameidata
591 * If we had been in RCU mode, drop out of it and legitimize nd->path.
592 * Revalidate the final result, unless we'd already done that during
593 * the path walk or the filesystem doesn't ask for it. Return 0 on
594 * success, -error on failure. In case of failure caller does not
595 * need to drop nd->path.
597 static int complete_walk(struct nameidata *nd)
599 struct dentry *dentry = nd->path.dentry;
602 if (nd->flags & LOOKUP_RCU) {
603 nd->flags &= ~LOOKUP_RCU;
604 if (!(nd->flags & LOOKUP_ROOT))
607 if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) {
611 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
613 mntput(nd->path.mnt);
616 if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
619 mntput(nd->path.mnt);
625 if (likely(!(nd->flags & LOOKUP_JUMPED)))
628 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
631 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
642 static __always_inline void set_root(struct nameidata *nd)
645 get_fs_root(current->fs, &nd->root);
648 static int link_path_walk(const char *, struct nameidata *);
650 static __always_inline void set_root_rcu(struct nameidata *nd)
653 struct fs_struct *fs = current->fs;
657 seq = read_seqcount_begin(&fs->seq);
659 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
660 } while (read_seqcount_retry(&fs->seq, seq));
664 static void path_put_conditional(struct path *path, struct nameidata *nd)
667 if (path->mnt != nd->path.mnt)
671 static inline void path_to_nameidata(const struct path *path,
672 struct nameidata *nd)
674 if (!(nd->flags & LOOKUP_RCU)) {
675 dput(nd->path.dentry);
676 if (nd->path.mnt != path->mnt)
677 mntput(nd->path.mnt);
679 nd->path.mnt = path->mnt;
680 nd->path.dentry = path->dentry;
684 * Helper to directly jump to a known parsed path from ->follow_link,
685 * caller must have taken a reference to path beforehand.
687 void nd_jump_link(struct nameidata *nd, struct path *path)
692 nd->inode = nd->path.dentry->d_inode;
693 nd->flags |= LOOKUP_JUMPED;
696 static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
698 struct inode *inode = link->dentry->d_inode;
699 if (inode->i_op->put_link)
700 inode->i_op->put_link(link->dentry, nd, cookie);
704 int sysctl_protected_symlinks __read_mostly = 0;
705 int sysctl_protected_hardlinks __read_mostly = 0;
708 * may_follow_link - Check symlink following for unsafe situations
709 * @link: The path of the symlink
710 * @nd: nameidata pathwalk data
712 * In the case of the sysctl_protected_symlinks sysctl being enabled,
713 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
714 * in a sticky world-writable directory. This is to protect privileged
715 * processes from failing races against path names that may change out
716 * from under them by way of other users creating malicious symlinks.
717 * It will permit symlinks to be followed only when outside a sticky
718 * world-writable directory, or when the uid of the symlink and follower
719 * match, or when the directory owner matches the symlink's owner.
721 * Returns 0 if following the symlink is allowed, -ve on error.
723 static inline int may_follow_link(struct path *link, struct nameidata *nd)
725 const struct inode *inode;
726 const struct inode *parent;
728 if (!sysctl_protected_symlinks)
731 /* Allowed if owner and follower match. */
732 inode = link->dentry->d_inode;
733 if (uid_eq(current_cred()->fsuid, inode->i_uid))
736 /* Allowed if parent directory not sticky and world-writable. */
737 parent = nd->path.dentry->d_inode;
738 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
741 /* Allowed if parent directory and link owner match. */
742 if (uid_eq(parent->i_uid, inode->i_uid))
745 audit_log_link_denied("follow_link", link);
746 path_put_conditional(link, nd);
752 * safe_hardlink_source - Check for safe hardlink conditions
753 * @inode: the source inode to hardlink from
755 * Return false if at least one of the following conditions:
756 * - inode is not a regular file
758 * - inode is setgid and group-exec
759 * - access failure for read and write
761 * Otherwise returns true.
763 static bool safe_hardlink_source(struct inode *inode)
765 umode_t mode = inode->i_mode;
767 /* Special files should not get pinned to the filesystem. */
771 /* Setuid files should not get pinned to the filesystem. */
775 /* Executable setgid files should not get pinned to the filesystem. */
776 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
779 /* Hardlinking to unreadable or unwritable sources is dangerous. */
780 if (inode_permission(inode, MAY_READ | MAY_WRITE))
787 * may_linkat - Check permissions for creating a hardlink
788 * @link: the source to hardlink from
790 * Block hardlink when all of:
791 * - sysctl_protected_hardlinks enabled
792 * - fsuid does not match inode
793 * - hardlink source is unsafe (see safe_hardlink_source() above)
796 * Returns 0 if successful, -ve on error.
798 static int may_linkat(struct path *link)
800 const struct cred *cred;
803 if (!sysctl_protected_hardlinks)
806 cred = current_cred();
807 inode = link->dentry->d_inode;
809 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
810 * otherwise, it must be a safe source.
812 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
816 audit_log_link_denied("linkat", link);
820 static __always_inline int
821 follow_link(struct path *link, struct nameidata *nd, void **p)
823 struct dentry *dentry = link->dentry;
827 BUG_ON(nd->flags & LOOKUP_RCU);
829 if (link->mnt == nd->path.mnt)
833 if (unlikely(current->total_link_count >= 40))
834 goto out_put_nd_path;
837 current->total_link_count++;
840 nd_set_link(nd, NULL);
842 error = security_inode_follow_link(link->dentry, nd);
844 goto out_put_nd_path;
846 nd->last_type = LAST_BIND;
847 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
850 goto out_put_nd_path;
855 if (unlikely(IS_ERR(s))) {
857 put_link(nd, link, *p);
865 nd->flags |= LOOKUP_JUMPED;
867 nd->inode = nd->path.dentry->d_inode;
868 error = link_path_walk(s, nd);
870 put_link(nd, link, *p);
882 static int follow_up_rcu(struct path *path)
884 struct mount *mnt = real_mount(path->mnt);
885 struct mount *parent;
886 struct dentry *mountpoint;
888 parent = mnt->mnt_parent;
889 if (&parent->mnt == path->mnt)
891 mountpoint = mnt->mnt_mountpoint;
892 path->dentry = mountpoint;
893 path->mnt = &parent->mnt;
898 * follow_up - Find the mountpoint of path's vfsmount
900 * Given a path, find the mountpoint of its source file system.
901 * Replace @path with the path of the mountpoint in the parent mount.
904 * Return 1 if we went up a level and 0 if we were already at the
907 int follow_up(struct path *path)
909 struct mount *mnt = real_mount(path->mnt);
910 struct mount *parent;
911 struct dentry *mountpoint;
913 read_seqlock_excl(&mount_lock);
914 parent = mnt->mnt_parent;
916 read_sequnlock_excl(&mount_lock);
919 mntget(&parent->mnt);
920 mountpoint = dget(mnt->mnt_mountpoint);
921 read_sequnlock_excl(&mount_lock);
923 path->dentry = mountpoint;
925 path->mnt = &parent->mnt;
930 * Perform an automount
931 * - return -EISDIR to tell follow_managed() to stop and return the path we
934 static int follow_automount(struct path *path, unsigned flags,
937 struct vfsmount *mnt;
940 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
943 /* We don't want to mount if someone's just doing a stat -
944 * unless they're stat'ing a directory and appended a '/' to
947 * We do, however, want to mount if someone wants to open or
948 * create a file of any type under the mountpoint, wants to
949 * traverse through the mountpoint or wants to open the
950 * mounted directory. Also, autofs may mark negative dentries
951 * as being automount points. These will need the attentions
952 * of the daemon to instantiate them before they can be used.
954 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
955 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
956 path->dentry->d_inode)
959 current->total_link_count++;
960 if (current->total_link_count >= 40)
963 mnt = path->dentry->d_op->d_automount(path);
966 * The filesystem is allowed to return -EISDIR here to indicate
967 * it doesn't want to automount. For instance, autofs would do
968 * this so that its userspace daemon can mount on this dentry.
970 * However, we can only permit this if it's a terminal point in
971 * the path being looked up; if it wasn't then the remainder of
972 * the path is inaccessible and we should say so.
974 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
979 if (!mnt) /* mount collision */
983 /* lock_mount() may release path->mnt on error */
987 err = finish_automount(mnt, path);
991 /* Someone else made a mount here whilst we were busy */
996 path->dentry = dget(mnt->mnt_root);
1005 * Handle a dentry that is managed in some way.
1006 * - Flagged for transit management (autofs)
1007 * - Flagged as mountpoint
1008 * - Flagged as automount point
1010 * This may only be called in refwalk mode.
1012 * Serialization is taken care of in namespace.c
1014 static int follow_managed(struct path *path, unsigned flags)
1016 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
1018 bool need_mntput = false;
1021 /* Given that we're not holding a lock here, we retain the value in a
1022 * local variable for each dentry as we look at it so that we don't see
1023 * the components of that value change under us */
1024 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1025 managed &= DCACHE_MANAGED_DENTRY,
1026 unlikely(managed != 0)) {
1027 /* Allow the filesystem to manage the transit without i_mutex
1029 if (managed & DCACHE_MANAGE_TRANSIT) {
1030 BUG_ON(!path->dentry->d_op);
1031 BUG_ON(!path->dentry->d_op->d_manage);
1032 ret = path->dentry->d_op->d_manage(path->dentry, false);
1037 /* Transit to a mounted filesystem. */
1038 if (managed & DCACHE_MOUNTED) {
1039 struct vfsmount *mounted = lookup_mnt(path);
1044 path->mnt = mounted;
1045 path->dentry = dget(mounted->mnt_root);
1050 /* Something is mounted on this dentry in another
1051 * namespace and/or whatever was mounted there in this
1052 * namespace got unmounted before lookup_mnt() could
1056 /* Handle an automount point */
1057 if (managed & DCACHE_NEED_AUTOMOUNT) {
1058 ret = follow_automount(path, flags, &need_mntput);
1064 /* We didn't change the current path point */
1068 if (need_mntput && path->mnt == mnt)
1072 return ret < 0 ? ret : need_mntput;
1075 int follow_down_one(struct path *path)
1077 struct vfsmount *mounted;
1079 mounted = lookup_mnt(path);
1083 path->mnt = mounted;
1084 path->dentry = dget(mounted->mnt_root);
1090 static inline bool managed_dentry_might_block(struct dentry *dentry)
1092 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1093 dentry->d_op->d_manage(dentry, true) < 0);
1097 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1098 * we meet a managed dentry that would need blocking.
1100 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1101 struct inode **inode)
1104 struct mount *mounted;
1106 * Don't forget we might have a non-mountpoint managed dentry
1107 * that wants to block transit.
1109 if (unlikely(managed_dentry_might_block(path->dentry)))
1112 if (!d_mountpoint(path->dentry))
1115 mounted = __lookup_mnt(path->mnt, path->dentry);
1118 path->mnt = &mounted->mnt;
1119 path->dentry = mounted->mnt.mnt_root;
1120 nd->flags |= LOOKUP_JUMPED;
1121 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
1123 * Update the inode too. We don't need to re-check the
1124 * dentry sequence number here after this d_inode read,
1125 * because a mount-point is always pinned.
1127 *inode = path->dentry->d_inode;
1129 return read_seqretry(&mount_lock, nd->m_seq);
1132 static int follow_dotdot_rcu(struct nameidata *nd)
1137 if (nd->path.dentry == nd->root.dentry &&
1138 nd->path.mnt == nd->root.mnt) {
1141 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1142 struct dentry *old = nd->path.dentry;
1143 struct dentry *parent = old->d_parent;
1146 seq = read_seqcount_begin(&parent->d_seq);
1147 if (read_seqcount_retry(&old->d_seq, nd->seq))
1149 nd->path.dentry = parent;
1153 if (!follow_up_rcu(&nd->path))
1155 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1157 while (d_mountpoint(nd->path.dentry)) {
1158 struct mount *mounted;
1159 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1162 nd->path.mnt = &mounted->mnt;
1163 nd->path.dentry = mounted->mnt.mnt_root;
1164 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1165 if (!read_seqretry(&mount_lock, nd->m_seq))
1168 nd->inode = nd->path.dentry->d_inode;
1172 nd->flags &= ~LOOKUP_RCU;
1173 if (!(nd->flags & LOOKUP_ROOT))
1174 nd->root.mnt = NULL;
1180 * Follow down to the covering mount currently visible to userspace. At each
1181 * point, the filesystem owning that dentry may be queried as to whether the
1182 * caller is permitted to proceed or not.
1184 int follow_down(struct path *path)
1189 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1190 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1191 /* Allow the filesystem to manage the transit without i_mutex
1194 * We indicate to the filesystem if someone is trying to mount
1195 * something here. This gives autofs the chance to deny anyone
1196 * other than its daemon the right to mount on its
1199 * The filesystem may sleep at this point.
1201 if (managed & DCACHE_MANAGE_TRANSIT) {
1202 BUG_ON(!path->dentry->d_op);
1203 BUG_ON(!path->dentry->d_op->d_manage);
1204 ret = path->dentry->d_op->d_manage(
1205 path->dentry, false);
1207 return ret == -EISDIR ? 0 : ret;
1210 /* Transit to a mounted filesystem. */
1211 if (managed & DCACHE_MOUNTED) {
1212 struct vfsmount *mounted = lookup_mnt(path);
1217 path->mnt = mounted;
1218 path->dentry = dget(mounted->mnt_root);
1222 /* Don't handle automount points here */
1229 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1231 static void follow_mount(struct path *path)
1233 while (d_mountpoint(path->dentry)) {
1234 struct vfsmount *mounted = lookup_mnt(path);
1239 path->mnt = mounted;
1240 path->dentry = dget(mounted->mnt_root);
1244 static void follow_dotdot(struct nameidata *nd)
1249 struct dentry *old = nd->path.dentry;
1251 if (nd->path.dentry == nd->root.dentry &&
1252 nd->path.mnt == nd->root.mnt) {
1255 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1256 /* rare case of legitimate dget_parent()... */
1257 nd->path.dentry = dget_parent(nd->path.dentry);
1261 if (!follow_up(&nd->path))
1264 follow_mount(&nd->path);
1265 nd->inode = nd->path.dentry->d_inode;
1269 * This looks up the name in dcache, possibly revalidates the old dentry and
1270 * allocates a new one if not found or not valid. In the need_lookup argument
1271 * returns whether i_op->lookup is necessary.
1273 * dir->d_inode->i_mutex must be held
1275 static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1276 unsigned int flags, bool *need_lookup)
1278 struct dentry *dentry;
1281 *need_lookup = false;
1282 dentry = d_lookup(dir, name);
1284 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1285 error = d_revalidate(dentry, flags);
1286 if (unlikely(error <= 0)) {
1289 return ERR_PTR(error);
1290 } else if (!d_invalidate(dentry)) {
1299 dentry = d_alloc(dir, name);
1300 if (unlikely(!dentry))
1301 return ERR_PTR(-ENOMEM);
1303 *need_lookup = true;
1309 * Call i_op->lookup on the dentry. The dentry must be negative and
1312 * dir->d_inode->i_mutex must be held
1314 static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1319 /* Don't create child dentry for a dead directory. */
1320 if (unlikely(IS_DEADDIR(dir))) {
1322 return ERR_PTR(-ENOENT);
1325 old = dir->i_op->lookup(dir, dentry, flags);
1326 if (unlikely(old)) {
1333 static struct dentry *__lookup_hash(struct qstr *name,
1334 struct dentry *base, unsigned int flags)
1337 struct dentry *dentry;
1339 dentry = lookup_dcache(name, base, flags, &need_lookup);
1343 return lookup_real(base->d_inode, dentry, flags);
1347 * It's more convoluted than I'd like it to be, but... it's still fairly
1348 * small and for now I'd prefer to have fast path as straight as possible.
1349 * It _is_ time-critical.
1351 static int lookup_fast(struct nameidata *nd,
1352 struct path *path, struct inode **inode)
1354 struct vfsmount *mnt = nd->path.mnt;
1355 struct dentry *dentry, *parent = nd->path.dentry;
1361 * Rename seqlock is not required here because in the off chance
1362 * of a false negative due to a concurrent rename, we're going to
1363 * do the non-racy lookup, below.
1365 if (nd->flags & LOOKUP_RCU) {
1367 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
1372 * This sequence count validates that the inode matches
1373 * the dentry name information from lookup.
1375 *inode = dentry->d_inode;
1376 if (read_seqcount_retry(&dentry->d_seq, seq))
1380 * This sequence count validates that the parent had no
1381 * changes while we did the lookup of the dentry above.
1383 * The memory barrier in read_seqcount_begin of child is
1384 * enough, we can use __read_seqcount_retry here.
1386 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1390 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1391 status = d_revalidate(dentry, nd->flags);
1392 if (unlikely(status <= 0)) {
1393 if (status != -ECHILD)
1399 path->dentry = dentry;
1400 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1402 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1406 if (unlazy_walk(nd, dentry))
1409 dentry = __d_lookup(parent, &nd->last);
1412 if (unlikely(!dentry))
1415 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1416 status = d_revalidate(dentry, nd->flags);
1417 if (unlikely(status <= 0)) {
1422 if (!d_invalidate(dentry)) {
1429 path->dentry = dentry;
1430 err = follow_managed(path, nd->flags);
1431 if (unlikely(err < 0)) {
1432 path_put_conditional(path, nd);
1436 nd->flags |= LOOKUP_JUMPED;
1437 *inode = path->dentry->d_inode;
1444 /* Fast lookup failed, do it the slow way */
1445 static int lookup_slow(struct nameidata *nd, struct path *path)
1447 struct dentry *dentry, *parent;
1450 parent = nd->path.dentry;
1451 BUG_ON(nd->inode != parent->d_inode);
1453 mutex_lock(&parent->d_inode->i_mutex);
1454 dentry = __lookup_hash(&nd->last, parent, nd->flags);
1455 mutex_unlock(&parent->d_inode->i_mutex);
1457 return PTR_ERR(dentry);
1458 path->mnt = nd->path.mnt;
1459 path->dentry = dentry;
1460 err = follow_managed(path, nd->flags);
1461 if (unlikely(err < 0)) {
1462 path_put_conditional(path, nd);
1466 nd->flags |= LOOKUP_JUMPED;
1470 static inline int may_lookup(struct nameidata *nd)
1472 if (nd->flags & LOOKUP_RCU) {
1473 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1476 if (unlazy_walk(nd, NULL))
1479 return inode_permission(nd->inode, MAY_EXEC);
1482 static inline int handle_dots(struct nameidata *nd, int type)
1484 if (type == LAST_DOTDOT) {
1485 if (nd->flags & LOOKUP_RCU) {
1486 if (follow_dotdot_rcu(nd))
1494 static void terminate_walk(struct nameidata *nd)
1496 if (!(nd->flags & LOOKUP_RCU)) {
1497 path_put(&nd->path);
1499 nd->flags &= ~LOOKUP_RCU;
1500 if (!(nd->flags & LOOKUP_ROOT))
1501 nd->root.mnt = NULL;
1507 * Do we need to follow links? We _really_ want to be able
1508 * to do this check without having to look at inode->i_op,
1509 * so we keep a cache of "no, this doesn't need follow_link"
1510 * for the common case.
1512 static inline int should_follow_link(struct dentry *dentry, int follow)
1514 return unlikely(d_is_symlink(dentry)) ? follow : 0;
1517 static inline int walk_component(struct nameidata *nd, struct path *path,
1520 struct inode *inode;
1523 * "." and ".." are special - ".." especially so because it has
1524 * to be able to know about the current root directory and
1525 * parent relationships.
1527 if (unlikely(nd->last_type != LAST_NORM))
1528 return handle_dots(nd, nd->last_type);
1529 err = lookup_fast(nd, path, &inode);
1530 if (unlikely(err)) {
1534 err = lookup_slow(nd, path);
1538 inode = path->dentry->d_inode;
1541 if (!inode || d_is_negative(path->dentry))
1544 if (should_follow_link(path->dentry, follow)) {
1545 if (nd->flags & LOOKUP_RCU) {
1546 if (unlikely(unlazy_walk(nd, path->dentry))) {
1551 BUG_ON(inode != path->dentry->d_inode);
1554 path_to_nameidata(path, nd);
1559 path_to_nameidata(path, nd);
1566 * This limits recursive symlink follows to 8, while
1567 * limiting consecutive symlinks to 40.
1569 * Without that kind of total limit, nasty chains of consecutive
1570 * symlinks can cause almost arbitrarily long lookups.
1572 static inline int nested_symlink(struct path *path, struct nameidata *nd)
1576 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1577 path_put_conditional(path, nd);
1578 path_put(&nd->path);
1581 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1584 current->link_count++;
1587 struct path link = *path;
1590 res = follow_link(&link, nd, &cookie);
1593 res = walk_component(nd, path, LOOKUP_FOLLOW);
1594 put_link(nd, &link, cookie);
1597 current->link_count--;
1603 * We can do the critical dentry name comparison and hashing
1604 * operations one word at a time, but we are limited to:
1606 * - Architectures with fast unaligned word accesses. We could
1607 * do a "get_unaligned()" if this helps and is sufficiently
1610 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1611 * do not trap on the (extremely unlikely) case of a page
1612 * crossing operation.
1614 * - Furthermore, we need an efficient 64-bit compile for the
1615 * 64-bit case in order to generate the "number of bytes in
1616 * the final mask". Again, that could be replaced with a
1617 * efficient population count instruction or similar.
1619 #ifdef CONFIG_DCACHE_WORD_ACCESS
1621 #include <asm/word-at-a-time.h>
1625 static inline unsigned int fold_hash(unsigned long hash)
1627 hash += hash >> (8*sizeof(int));
1631 #else /* 32-bit case */
1633 #define fold_hash(x) (x)
1637 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1639 unsigned long a, mask;
1640 unsigned long hash = 0;
1643 a = load_unaligned_zeropad(name);
1644 if (len < sizeof(unsigned long))
1648 name += sizeof(unsigned long);
1649 len -= sizeof(unsigned long);
1653 mask = bytemask_from_count(len);
1656 return fold_hash(hash);
1658 EXPORT_SYMBOL(full_name_hash);
1661 * Calculate the length and hash of the path component, and
1662 * return the length of the component;
1664 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1666 unsigned long a, b, adata, bdata, mask, hash, len;
1667 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1670 len = -sizeof(unsigned long);
1672 hash = (hash + a) * 9;
1673 len += sizeof(unsigned long);
1674 a = load_unaligned_zeropad(name+len);
1675 b = a ^ REPEAT_BYTE('/');
1676 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1678 adata = prep_zero_mask(a, adata, &constants);
1679 bdata = prep_zero_mask(b, bdata, &constants);
1681 mask = create_zero_mask(adata | bdata);
1683 hash += a & zero_bytemask(mask);
1684 *hashp = fold_hash(hash);
1686 return len + find_zero(mask);
1691 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1693 unsigned long hash = init_name_hash();
1695 hash = partial_name_hash(*name++, hash);
1696 return end_name_hash(hash);
1698 EXPORT_SYMBOL(full_name_hash);
1701 * We know there's a real path component here of at least
1704 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1706 unsigned long hash = init_name_hash();
1707 unsigned long len = 0, c;
1709 c = (unsigned char)*name;
1712 hash = partial_name_hash(c, hash);
1713 c = (unsigned char)name[len];
1714 } while (c && c != '/');
1715 *hashp = end_name_hash(hash);
1723 * This is the basic name resolution function, turning a pathname into
1724 * the final dentry. We expect 'base' to be positive and a directory.
1726 * Returns 0 and nd will have valid dentry and mnt on success.
1727 * Returns error and drops reference to input namei data on failure.
1729 static int link_path_walk(const char *name, struct nameidata *nd)
1739 /* At this point we know we have a real path component. */
1745 err = may_lookup(nd);
1749 len = hash_name(name, &this.hash);
1754 if (name[0] == '.') switch (len) {
1756 if (name[1] == '.') {
1758 nd->flags |= LOOKUP_JUMPED;
1764 if (likely(type == LAST_NORM)) {
1765 struct dentry *parent = nd->path.dentry;
1766 nd->flags &= ~LOOKUP_JUMPED;
1767 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1768 err = parent->d_op->d_hash(parent, &this);
1775 nd->last_type = type;
1780 * If it wasn't NUL, we know it was '/'. Skip that
1781 * slash, and continue until no more slashes.
1785 } while (unlikely(name[len] == '/'));
1791 err = walk_component(nd, &next, LOOKUP_FOLLOW);
1796 err = nested_symlink(&next, nd);
1800 if (!d_is_directory(nd->path.dentry)) {
1809 static int path_init(int dfd, const char *name, unsigned int flags,
1810 struct nameidata *nd, struct file **fp)
1814 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1815 nd->flags = flags | LOOKUP_JUMPED;
1817 if (flags & LOOKUP_ROOT) {
1818 struct dentry *root = nd->root.dentry;
1819 struct inode *inode = root->d_inode;
1821 if (!d_is_directory(root))
1823 retval = inode_permission(inode, MAY_EXEC);
1827 nd->path = nd->root;
1829 if (flags & LOOKUP_RCU) {
1831 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1832 nd->m_seq = read_seqbegin(&mount_lock);
1834 path_get(&nd->path);
1839 nd->root.mnt = NULL;
1841 nd->m_seq = read_seqbegin(&mount_lock);
1843 if (flags & LOOKUP_RCU) {
1848 path_get(&nd->root);
1850 nd->path = nd->root;
1851 } else if (dfd == AT_FDCWD) {
1852 if (flags & LOOKUP_RCU) {
1853 struct fs_struct *fs = current->fs;
1859 seq = read_seqcount_begin(&fs->seq);
1861 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1862 } while (read_seqcount_retry(&fs->seq, seq));
1864 get_fs_pwd(current->fs, &nd->path);
1867 /* Caller must check execute permissions on the starting path component */
1868 struct fd f = fdget_raw(dfd);
1869 struct dentry *dentry;
1874 dentry = f.file->f_path.dentry;
1877 if (!d_is_directory(dentry)) {
1883 nd->path = f.file->f_path;
1884 if (flags & LOOKUP_RCU) {
1885 if (f.flags & FDPUT_FPUT)
1887 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1890 path_get(&nd->path);
1895 nd->inode = nd->path.dentry->d_inode;
1899 static inline int lookup_last(struct nameidata *nd, struct path *path)
1901 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1902 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1904 nd->flags &= ~LOOKUP_PARENT;
1905 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
1908 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1909 static int path_lookupat(int dfd, const char *name,
1910 unsigned int flags, struct nameidata *nd)
1912 struct file *base = NULL;
1917 * Path walking is largely split up into 2 different synchronisation
1918 * schemes, rcu-walk and ref-walk (explained in
1919 * Documentation/filesystems/path-lookup.txt). These share much of the
1920 * path walk code, but some things particularly setup, cleanup, and
1921 * following mounts are sufficiently divergent that functions are
1922 * duplicated. Typically there is a function foo(), and its RCU
1923 * analogue, foo_rcu().
1925 * -ECHILD is the error number of choice (just to avoid clashes) that
1926 * is returned if some aspect of an rcu-walk fails. Such an error must
1927 * be handled by restarting a traditional ref-walk (which will always
1928 * be able to complete).
1930 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1935 current->total_link_count = 0;
1936 err = link_path_walk(name, nd);
1938 if (!err && !(flags & LOOKUP_PARENT)) {
1939 err = lookup_last(nd, &path);
1942 struct path link = path;
1943 err = may_follow_link(&link, nd);
1946 nd->flags |= LOOKUP_PARENT;
1947 err = follow_link(&link, nd, &cookie);
1950 err = lookup_last(nd, &path);
1951 put_link(nd, &link, cookie);
1956 err = complete_walk(nd);
1958 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1959 if (!d_is_directory(nd->path.dentry)) {
1960 path_put(&nd->path);
1968 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1969 path_put(&nd->root);
1970 nd->root.mnt = NULL;
1975 static int filename_lookup(int dfd, struct filename *name,
1976 unsigned int flags, struct nameidata *nd)
1978 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
1979 if (unlikely(retval == -ECHILD))
1980 retval = path_lookupat(dfd, name->name, flags, nd);
1981 if (unlikely(retval == -ESTALE))
1982 retval = path_lookupat(dfd, name->name,
1983 flags | LOOKUP_REVAL, nd);
1985 if (likely(!retval))
1986 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
1990 static int do_path_lookup(int dfd, const char *name,
1991 unsigned int flags, struct nameidata *nd)
1993 struct filename filename = { .name = name };
1995 return filename_lookup(dfd, &filename, flags, nd);
1998 /* does lookup, returns the object with parent locked */
1999 struct dentry *kern_path_locked(const char *name, struct path *path)
2001 struct nameidata nd;
2003 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2005 return ERR_PTR(err);
2006 if (nd.last_type != LAST_NORM) {
2008 return ERR_PTR(-EINVAL);
2010 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2011 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
2013 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2021 int kern_path(const char *name, unsigned int flags, struct path *path)
2023 struct nameidata nd;
2024 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2031 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2032 * @dentry: pointer to dentry of the base directory
2033 * @mnt: pointer to vfs mount of the base directory
2034 * @name: pointer to file name
2035 * @flags: lookup flags
2036 * @path: pointer to struct path to fill
2038 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2039 const char *name, unsigned int flags,
2042 struct nameidata nd;
2044 nd.root.dentry = dentry;
2046 BUG_ON(flags & LOOKUP_PARENT);
2047 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2048 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2055 * Restricted form of lookup. Doesn't follow links, single-component only,
2056 * needs parent already locked. Doesn't follow mounts.
2059 static struct dentry *lookup_hash(struct nameidata *nd)
2061 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
2065 * lookup_one_len - filesystem helper to lookup single pathname component
2066 * @name: pathname component to lookup
2067 * @base: base directory to lookup from
2068 * @len: maximum length @len should be interpreted to
2070 * Note that this routine is purely a helper for filesystem usage and should
2071 * not be called by generic code. Also note that by using this function the
2072 * nameidata argument is passed to the filesystem methods and a filesystem
2073 * using this helper needs to be prepared for that.
2075 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2081 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2085 this.hash = full_name_hash(name, len);
2087 return ERR_PTR(-EACCES);
2089 if (unlikely(name[0] == '.')) {
2090 if (len < 2 || (len == 2 && name[1] == '.'))
2091 return ERR_PTR(-EACCES);
2095 c = *(const unsigned char *)name++;
2096 if (c == '/' || c == '\0')
2097 return ERR_PTR(-EACCES);
2100 * See if the low-level filesystem might want
2101 * to use its own hash..
2103 if (base->d_flags & DCACHE_OP_HASH) {
2104 int err = base->d_op->d_hash(base, &this);
2106 return ERR_PTR(err);
2109 err = inode_permission(base->d_inode, MAY_EXEC);
2111 return ERR_PTR(err);
2113 return __lookup_hash(&this, base, 0);
2116 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2117 struct path *path, int *empty)
2119 struct nameidata nd;
2120 struct filename *tmp = getname_flags(name, flags, empty);
2121 int err = PTR_ERR(tmp);
2124 BUG_ON(flags & LOOKUP_PARENT);
2126 err = filename_lookup(dfd, tmp, flags, &nd);
2134 int user_path_at(int dfd, const char __user *name, unsigned flags,
2137 return user_path_at_empty(dfd, name, flags, path, NULL);
2141 * NB: most callers don't do anything directly with the reference to the
2142 * to struct filename, but the nd->last pointer points into the name string
2143 * allocated by getname. So we must hold the reference to it until all
2144 * path-walking is complete.
2146 static struct filename *
2147 user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2150 struct filename *s = getname(path);
2153 /* only LOOKUP_REVAL is allowed in extra flags */
2154 flags &= LOOKUP_REVAL;
2159 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
2162 return ERR_PTR(error);
2169 * mountpoint_last - look up last component for umount
2170 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2171 * @path: pointer to container for result
2173 * This is a special lookup_last function just for umount. In this case, we
2174 * need to resolve the path without doing any revalidation.
2176 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2177 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2178 * in almost all cases, this lookup will be served out of the dcache. The only
2179 * cases where it won't are if nd->last refers to a symlink or the path is
2180 * bogus and it doesn't exist.
2183 * -error: if there was an error during lookup. This includes -ENOENT if the
2184 * lookup found a negative dentry. The nd->path reference will also be
2187 * 0: if we successfully resolved nd->path and found it to not to be a
2188 * symlink that needs to be followed. "path" will also be populated.
2189 * The nd->path reference will also be put.
2191 * 1: if we successfully resolved nd->last and found it to be a symlink
2192 * that needs to be followed. "path" will be populated with the path
2193 * to the link, and nd->path will *not* be put.
2196 mountpoint_last(struct nameidata *nd, struct path *path)
2199 struct dentry *dentry;
2200 struct dentry *dir = nd->path.dentry;
2202 /* If we're in rcuwalk, drop out of it to handle last component */
2203 if (nd->flags & LOOKUP_RCU) {
2204 if (unlazy_walk(nd, NULL)) {
2210 nd->flags &= ~LOOKUP_PARENT;
2212 if (unlikely(nd->last_type != LAST_NORM)) {
2213 error = handle_dots(nd, nd->last_type);
2216 dentry = dget(nd->path.dentry);
2220 mutex_lock(&dir->d_inode->i_mutex);
2221 dentry = d_lookup(dir, &nd->last);
2224 * No cached dentry. Mounted dentries are pinned in the cache,
2225 * so that means that this dentry is probably a symlink or the
2226 * path doesn't actually point to a mounted dentry.
2228 dentry = d_alloc(dir, &nd->last);
2231 mutex_unlock(&dir->d_inode->i_mutex);
2234 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
2235 error = PTR_ERR(dentry);
2236 if (IS_ERR(dentry)) {
2237 mutex_unlock(&dir->d_inode->i_mutex);
2241 mutex_unlock(&dir->d_inode->i_mutex);
2244 if (!dentry->d_inode || d_is_negative(dentry)) {
2249 path->dentry = dentry;
2250 path->mnt = mntget(nd->path.mnt);
2251 if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
2261 * path_mountpoint - look up a path to be umounted
2262 * @dfd: directory file descriptor to start walk from
2263 * @name: full pathname to walk
2264 * @path: pointer to container for result
2265 * @flags: lookup flags
2267 * Look up the given name, but don't attempt to revalidate the last component.
2268 * Returns 0 and "path" will be valid on success; Returns error otherwise.
2271 path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
2273 struct file *base = NULL;
2274 struct nameidata nd;
2277 err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
2281 current->total_link_count = 0;
2282 err = link_path_walk(name, &nd);
2286 err = mountpoint_last(&nd, path);
2289 struct path link = *path;
2290 err = may_follow_link(&link, &nd);
2293 nd.flags |= LOOKUP_PARENT;
2294 err = follow_link(&link, &nd, &cookie);
2297 err = mountpoint_last(&nd, path);
2298 put_link(&nd, &link, cookie);
2304 if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
2311 filename_mountpoint(int dfd, struct filename *s, struct path *path,
2314 int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
2315 if (unlikely(error == -ECHILD))
2316 error = path_mountpoint(dfd, s->name, path, flags);
2317 if (unlikely(error == -ESTALE))
2318 error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
2320 audit_inode(s, path->dentry, 0);
2325 * user_path_mountpoint_at - lookup a path from userland in order to umount it
2326 * @dfd: directory file descriptor
2327 * @name: pathname from userland
2328 * @flags: lookup flags
2329 * @path: pointer to container to hold result
2331 * A umount is a special case for path walking. We're not actually interested
2332 * in the inode in this situation, and ESTALE errors can be a problem. We
2333 * simply want track down the dentry and vfsmount attached at the mountpoint
2334 * and avoid revalidating the last component.
2336 * Returns 0 and populates "path" on success.
2339 user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
2342 struct filename *s = getname(name);
2346 error = filename_mountpoint(dfd, s, path, flags);
2352 kern_path_mountpoint(int dfd, const char *name, struct path *path,
2355 struct filename s = {.name = name};
2356 return filename_mountpoint(dfd, &s, path, flags);
2358 EXPORT_SYMBOL(kern_path_mountpoint);
2361 * It's inline, so penalty for filesystems that don't use sticky bit is
2364 static inline int check_sticky(struct inode *dir, struct inode *inode)
2366 kuid_t fsuid = current_fsuid();
2368 if (!(dir->i_mode & S_ISVTX))
2370 if (uid_eq(inode->i_uid, fsuid))
2372 if (uid_eq(dir->i_uid, fsuid))
2374 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
2378 * Check whether we can remove a link victim from directory dir, check
2379 * whether the type of victim is right.
2380 * 1. We can't do it if dir is read-only (done in permission())
2381 * 2. We should have write and exec permissions on dir
2382 * 3. We can't remove anything from append-only dir
2383 * 4. We can't do anything with immutable dir (done in permission())
2384 * 5. If the sticky bit on dir is set we should either
2385 * a. be owner of dir, or
2386 * b. be owner of victim, or
2387 * c. have CAP_FOWNER capability
2388 * 6. If the victim is append-only or immutable we can't do antyhing with
2389 * links pointing to it.
2390 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2391 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2392 * 9. We can't remove a root or mountpoint.
2393 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2394 * nfs_async_unlink().
2396 static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
2398 struct inode *inode = victim->d_inode;
2401 if (d_is_negative(victim))
2405 BUG_ON(victim->d_parent->d_inode != dir);
2406 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
2408 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
2414 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2415 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
2418 if (!d_is_directory(victim) && !d_is_autodir(victim))
2420 if (IS_ROOT(victim))
2422 } else if (d_is_directory(victim) || d_is_autodir(victim))
2424 if (IS_DEADDIR(dir))
2426 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2431 /* Check whether we can create an object with dentry child in directory
2433 * 1. We can't do it if child already exists (open has special treatment for
2434 * this case, but since we are inlined it's OK)
2435 * 2. We can't do it if dir is read-only (done in permission())
2436 * 3. We should have write and exec permissions on dir
2437 * 4. We can't do it if dir is immutable (done in permission())
2439 static inline int may_create(struct inode *dir, struct dentry *child)
2441 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
2444 if (IS_DEADDIR(dir))
2446 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2450 * p1 and p2 should be directories on the same fs.
2452 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2457 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2461 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2463 p = d_ancestor(p2, p1);
2465 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2466 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2470 p = d_ancestor(p1, p2);
2472 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2473 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2477 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2478 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2482 void unlock_rename(struct dentry *p1, struct dentry *p2)
2484 mutex_unlock(&p1->d_inode->i_mutex);
2486 mutex_unlock(&p2->d_inode->i_mutex);
2487 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2491 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2494 int error = may_create(dir, dentry);
2498 if (!dir->i_op->create)
2499 return -EACCES; /* shouldn't it be ENOSYS? */
2502 error = security_inode_create(dir, dentry, mode);
2505 error = dir->i_op->create(dir, dentry, mode, want_excl);
2507 fsnotify_create(dir, dentry);
2511 static int may_open(struct path *path, int acc_mode, int flag)
2513 struct dentry *dentry = path->dentry;
2514 struct inode *inode = dentry->d_inode;
2524 switch (inode->i_mode & S_IFMT) {
2528 if (acc_mode & MAY_WRITE)
2533 if (path->mnt->mnt_flags & MNT_NODEV)
2542 error = inode_permission(inode, acc_mode);
2547 * An append-only file must be opened in append mode for writing.
2549 if (IS_APPEND(inode)) {
2550 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2556 /* O_NOATIME can only be set by the owner or superuser */
2557 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2563 static int handle_truncate(struct file *filp)
2565 struct path *path = &filp->f_path;
2566 struct inode *inode = path->dentry->d_inode;
2567 int error = get_write_access(inode);
2571 * Refuse to truncate files with mandatory locks held on them.
2573 error = locks_verify_locked(inode);
2575 error = security_path_truncate(path);
2577 error = do_truncate(path->dentry, 0,
2578 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2581 put_write_access(inode);
2585 static inline int open_to_namei_flags(int flag)
2587 if ((flag & O_ACCMODE) == 3)
2592 static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2594 int error = security_path_mknod(dir, dentry, mode, 0);
2598 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2602 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2606 * Attempt to atomically look up, create and open a file from a negative
2609 * Returns 0 if successful. The file will have been created and attached to
2610 * @file by the filesystem calling finish_open().
2612 * Returns 1 if the file was looked up only or didn't need creating. The
2613 * caller will need to perform the open themselves. @path will have been
2614 * updated to point to the new dentry. This may be negative.
2616 * Returns an error code otherwise.
2618 static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2619 struct path *path, struct file *file,
2620 const struct open_flags *op,
2621 bool got_write, bool need_lookup,
2624 struct inode *dir = nd->path.dentry->d_inode;
2625 unsigned open_flag = open_to_namei_flags(op->open_flag);
2629 int create_error = 0;
2630 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2633 BUG_ON(dentry->d_inode);
2635 /* Don't create child dentry for a dead directory. */
2636 if (unlikely(IS_DEADDIR(dir))) {
2642 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2643 mode &= ~current_umask();
2645 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2647 open_flag &= ~O_TRUNC;
2650 * Checking write permission is tricky, bacuse we don't know if we are
2651 * going to actually need it: O_CREAT opens should work as long as the
2652 * file exists. But checking existence breaks atomicity. The trick is
2653 * to check access and if not granted clear O_CREAT from the flags.
2655 * Another problem is returing the "right" error value (e.g. for an
2656 * O_EXCL open we want to return EEXIST not EROFS).
2658 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2659 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2660 if (!(open_flag & O_CREAT)) {
2662 * No O_CREATE -> atomicity not a requirement -> fall
2663 * back to lookup + open
2666 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2667 /* Fall back and fail with the right error */
2668 create_error = -EROFS;
2671 /* No side effects, safe to clear O_CREAT */
2672 create_error = -EROFS;
2673 open_flag &= ~O_CREAT;
2677 if (open_flag & O_CREAT) {
2678 error = may_o_create(&nd->path, dentry, mode);
2680 create_error = error;
2681 if (open_flag & O_EXCL)
2683 open_flag &= ~O_CREAT;
2687 if (nd->flags & LOOKUP_DIRECTORY)
2688 open_flag |= O_DIRECTORY;
2690 file->f_path.dentry = DENTRY_NOT_SET;
2691 file->f_path.mnt = nd->path.mnt;
2692 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
2695 if (create_error && error == -ENOENT)
2696 error = create_error;
2700 if (error) { /* returned 1, that is */
2701 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2705 if (file->f_path.dentry) {
2707 dentry = file->f_path.dentry;
2709 if (*opened & FILE_CREATED)
2710 fsnotify_create(dir, dentry);
2711 if (!dentry->d_inode) {
2712 WARN_ON(*opened & FILE_CREATED);
2714 error = create_error;
2718 if (excl && !(*opened & FILE_CREATED)) {
2727 * We didn't have the inode before the open, so check open permission
2730 acc_mode = op->acc_mode;
2731 if (*opened & FILE_CREATED) {
2732 WARN_ON(!(open_flag & O_CREAT));
2733 fsnotify_create(dir, dentry);
2734 acc_mode = MAY_OPEN;
2736 error = may_open(&file->f_path, acc_mode, open_flag);
2746 dentry = lookup_real(dir, dentry, nd->flags);
2748 return PTR_ERR(dentry);
2751 int open_flag = op->open_flag;
2753 error = create_error;
2754 if ((open_flag & O_EXCL)) {
2755 if (!dentry->d_inode)
2757 } else if (!dentry->d_inode) {
2759 } else if ((open_flag & O_TRUNC) &&
2760 S_ISREG(dentry->d_inode->i_mode)) {
2763 /* will fail later, go on to get the right error */
2767 path->dentry = dentry;
2768 path->mnt = nd->path.mnt;
2773 * Look up and maybe create and open the last component.
2775 * Must be called with i_mutex held on parent.
2777 * Returns 0 if the file was successfully atomically created (if necessary) and
2778 * opened. In this case the file will be returned attached to @file.
2780 * Returns 1 if the file was not completely opened at this time, though lookups
2781 * and creations will have been performed and the dentry returned in @path will
2782 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2783 * specified then a negative dentry may be returned.
2785 * An error code is returned otherwise.
2787 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2788 * cleared otherwise prior to returning.
2790 static int lookup_open(struct nameidata *nd, struct path *path,
2792 const struct open_flags *op,
2793 bool got_write, int *opened)
2795 struct dentry *dir = nd->path.dentry;
2796 struct inode *dir_inode = dir->d_inode;
2797 struct dentry *dentry;
2801 *opened &= ~FILE_CREATED;
2802 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2804 return PTR_ERR(dentry);
2806 /* Cached positive dentry: will open in f_op->open */
2807 if (!need_lookup && dentry->d_inode)
2810 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
2811 return atomic_open(nd, dentry, path, file, op, got_write,
2812 need_lookup, opened);
2816 BUG_ON(dentry->d_inode);
2818 dentry = lookup_real(dir_inode, dentry, nd->flags);
2820 return PTR_ERR(dentry);
2823 /* Negative dentry, just create the file */
2824 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2825 umode_t mode = op->mode;
2826 if (!IS_POSIXACL(dir->d_inode))
2827 mode &= ~current_umask();
2829 * This write is needed to ensure that a
2830 * rw->ro transition does not occur between
2831 * the time when the file is created and when
2832 * a permanent write count is taken through
2833 * the 'struct file' in finish_open().
2839 *opened |= FILE_CREATED;
2840 error = security_path_mknod(&nd->path, dentry, mode, 0);
2843 error = vfs_create(dir->d_inode, dentry, mode,
2844 nd->flags & LOOKUP_EXCL);
2849 path->dentry = dentry;
2850 path->mnt = nd->path.mnt;
2859 * Handle the last step of open()
2861 static int do_last(struct nameidata *nd, struct path *path,
2862 struct file *file, const struct open_flags *op,
2863 int *opened, struct filename *name)
2865 struct dentry *dir = nd->path.dentry;
2866 int open_flag = op->open_flag;
2867 bool will_truncate = (open_flag & O_TRUNC) != 0;
2868 bool got_write = false;
2869 int acc_mode = op->acc_mode;
2870 struct inode *inode;
2871 bool symlink_ok = false;
2872 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2873 bool retried = false;
2876 nd->flags &= ~LOOKUP_PARENT;
2877 nd->flags |= op->intent;
2879 if (nd->last_type != LAST_NORM) {
2880 error = handle_dots(nd, nd->last_type);
2886 if (!(open_flag & O_CREAT)) {
2887 if (nd->last.name[nd->last.len])
2888 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2889 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2891 /* we _can_ be in RCU mode here */
2892 error = lookup_fast(nd, path, &inode);
2899 BUG_ON(nd->inode != dir->d_inode);
2901 /* create side of things */
2903 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2904 * has been cleared when we got to the last component we are
2907 error = complete_walk(nd);
2911 audit_inode(name, dir, LOOKUP_PARENT);
2913 /* trailing slashes? */
2914 if (nd->last.name[nd->last.len])
2919 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2920 error = mnt_want_write(nd->path.mnt);
2924 * do _not_ fail yet - we might not need that or fail with
2925 * a different error; let lookup_open() decide; we'll be
2926 * dropping this one anyway.
2929 mutex_lock(&dir->d_inode->i_mutex);
2930 error = lookup_open(nd, path, file, op, got_write, opened);
2931 mutex_unlock(&dir->d_inode->i_mutex);
2937 if ((*opened & FILE_CREATED) ||
2938 !S_ISREG(file_inode(file)->i_mode))
2939 will_truncate = false;
2941 audit_inode(name, file->f_path.dentry, 0);
2945 if (*opened & FILE_CREATED) {
2946 /* Don't check for write permission, don't truncate */
2947 open_flag &= ~O_TRUNC;
2948 will_truncate = false;
2949 acc_mode = MAY_OPEN;
2950 path_to_nameidata(path, nd);
2951 goto finish_open_created;
2955 * create/update audit record if it already exists.
2957 if (d_is_positive(path->dentry))
2958 audit_inode(name, path->dentry, 0);
2961 * If atomic_open() acquired write access it is dropped now due to
2962 * possible mount and symlink following (this might be optimized away if
2966 mnt_drop_write(nd->path.mnt);
2971 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
2974 error = follow_managed(path, nd->flags);
2979 nd->flags |= LOOKUP_JUMPED;
2981 BUG_ON(nd->flags & LOOKUP_RCU);
2982 inode = path->dentry->d_inode;
2984 /* we _can_ be in RCU mode here */
2986 if (!inode || d_is_negative(path->dentry)) {
2987 path_to_nameidata(path, nd);
2991 if (should_follow_link(path->dentry, !symlink_ok)) {
2992 if (nd->flags & LOOKUP_RCU) {
2993 if (unlikely(unlazy_walk(nd, path->dentry))) {
2998 BUG_ON(inode != path->dentry->d_inode);
3002 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3003 path_to_nameidata(path, nd);
3005 save_parent.dentry = nd->path.dentry;
3006 save_parent.mnt = mntget(path->mnt);
3007 nd->path.dentry = path->dentry;
3011 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
3013 error = complete_walk(nd);
3015 path_put(&save_parent);
3018 audit_inode(name, nd->path.dentry, 0);
3020 if ((open_flag & O_CREAT) &&
3021 (d_is_directory(nd->path.dentry) || d_is_autodir(nd->path.dentry)))
3024 if ((nd->flags & LOOKUP_DIRECTORY) && !d_is_directory(nd->path.dentry))
3026 if (!S_ISREG(nd->inode->i_mode))
3027 will_truncate = false;
3029 if (will_truncate) {
3030 error = mnt_want_write(nd->path.mnt);
3035 finish_open_created:
3036 error = may_open(&nd->path, acc_mode, open_flag);
3039 file->f_path.mnt = nd->path.mnt;
3040 error = finish_open(file, nd->path.dentry, NULL, opened);
3042 if (error == -EOPENSTALE)
3047 error = open_check_o_direct(file);
3050 error = ima_file_check(file, op->acc_mode);
3054 if (will_truncate) {
3055 error = handle_truncate(file);
3061 mnt_drop_write(nd->path.mnt);
3062 path_put(&save_parent);
3067 path_put_conditional(path, nd);
3074 /* If no saved parent or already retried then can't retry */
3075 if (!save_parent.dentry || retried)
3078 BUG_ON(save_parent.dentry != dir);
3079 path_put(&nd->path);
3080 nd->path = save_parent;
3081 nd->inode = dir->d_inode;
3082 save_parent.mnt = NULL;
3083 save_parent.dentry = NULL;
3085 mnt_drop_write(nd->path.mnt);
3092 static int do_tmpfile(int dfd, struct filename *pathname,
3093 struct nameidata *nd, int flags,
3094 const struct open_flags *op,
3095 struct file *file, int *opened)
3097 static const struct qstr name = QSTR_INIT("/", 1);
3098 struct dentry *dentry, *child;
3100 int error = path_lookupat(dfd, pathname->name,
3101 flags | LOOKUP_DIRECTORY, nd);
3102 if (unlikely(error))
3104 error = mnt_want_write(nd->path.mnt);
3105 if (unlikely(error))
3107 /* we want directory to be writable */
3108 error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
3111 dentry = nd->path.dentry;
3112 dir = dentry->d_inode;
3113 if (!dir->i_op->tmpfile) {
3114 error = -EOPNOTSUPP;
3117 child = d_alloc(dentry, &name);
3118 if (unlikely(!child)) {
3122 nd->flags &= ~LOOKUP_DIRECTORY;
3123 nd->flags |= op->intent;
3124 dput(nd->path.dentry);
3125 nd->path.dentry = child;
3126 error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
3129 audit_inode(pathname, nd->path.dentry, 0);
3130 error = may_open(&nd->path, op->acc_mode, op->open_flag);
3133 file->f_path.mnt = nd->path.mnt;
3134 error = finish_open(file, nd->path.dentry, NULL, opened);
3137 error = open_check_o_direct(file);
3140 } else if (!(op->open_flag & O_EXCL)) {
3141 struct inode *inode = file_inode(file);
3142 spin_lock(&inode->i_lock);
3143 inode->i_state |= I_LINKABLE;
3144 spin_unlock(&inode->i_lock);
3147 mnt_drop_write(nd->path.mnt);
3149 path_put(&nd->path);
3153 static struct file *path_openat(int dfd, struct filename *pathname,
3154 struct nameidata *nd, const struct open_flags *op, int flags)
3156 struct file *base = NULL;
3162 file = get_empty_filp();
3166 file->f_flags = op->open_flag;
3168 if (unlikely(file->f_flags & __O_TMPFILE)) {
3169 error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
3173 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
3174 if (unlikely(error))
3177 current->total_link_count = 0;
3178 error = link_path_walk(pathname->name, nd);
3179 if (unlikely(error))
3182 error = do_last(nd, &path, file, op, &opened, pathname);
3183 while (unlikely(error > 0)) { /* trailing symlink */
3184 struct path link = path;
3186 if (!(nd->flags & LOOKUP_FOLLOW)) {
3187 path_put_conditional(&path, nd);
3188 path_put(&nd->path);
3192 error = may_follow_link(&link, nd);
3193 if (unlikely(error))
3195 nd->flags |= LOOKUP_PARENT;
3196 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3197 error = follow_link(&link, nd, &cookie);
3198 if (unlikely(error))
3200 error = do_last(nd, &path, file, op, &opened, pathname);
3201 put_link(nd, &link, cookie);
3204 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
3205 path_put(&nd->root);
3208 if (!(opened & FILE_OPENED)) {
3212 if (unlikely(error)) {
3213 if (error == -EOPENSTALE) {
3214 if (flags & LOOKUP_RCU)
3219 file = ERR_PTR(error);
3224 struct file *do_filp_open(int dfd, struct filename *pathname,
3225 const struct open_flags *op)
3227 struct nameidata nd;
3228 int flags = op->lookup_flags;
3231 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
3232 if (unlikely(filp == ERR_PTR(-ECHILD)))
3233 filp = path_openat(dfd, pathname, &nd, op, flags);
3234 if (unlikely(filp == ERR_PTR(-ESTALE)))
3235 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
3239 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3240 const char *name, const struct open_flags *op)
3242 struct nameidata nd;
3244 struct filename filename = { .name = name };
3245 int flags = op->lookup_flags | LOOKUP_ROOT;
3248 nd.root.dentry = dentry;
3250 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
3251 return ERR_PTR(-ELOOP);
3253 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
3254 if (unlikely(file == ERR_PTR(-ECHILD)))
3255 file = path_openat(-1, &filename, &nd, op, flags);
3256 if (unlikely(file == ERR_PTR(-ESTALE)))
3257 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
3261 struct dentry *kern_path_create(int dfd, const char *pathname,
3262 struct path *path, unsigned int lookup_flags)
3264 struct dentry *dentry = ERR_PTR(-EEXIST);
3265 struct nameidata nd;
3268 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3271 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3272 * other flags passed in are ignored!
3274 lookup_flags &= LOOKUP_REVAL;
3276 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
3278 return ERR_PTR(error);
3281 * Yucky last component or no last component at all?
3282 * (foo/., foo/.., /////)
3284 if (nd.last_type != LAST_NORM)
3286 nd.flags &= ~LOOKUP_PARENT;
3287 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3289 /* don't fail immediately if it's r/o, at least try to report other errors */
3290 err2 = mnt_want_write(nd.path.mnt);
3292 * Do the final lookup.
3294 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3295 dentry = lookup_hash(&nd);
3300 if (d_is_positive(dentry))
3304 * Special case - lookup gave negative, but... we had foo/bar/
3305 * From the vfs_mknod() POV we just have a negative dentry -
3306 * all is fine. Let's be bastards - you had / on the end, you've
3307 * been asking for (non-existent) directory. -ENOENT for you.
3309 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
3313 if (unlikely(err2)) {
3321 dentry = ERR_PTR(error);
3323 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3325 mnt_drop_write(nd.path.mnt);
3330 EXPORT_SYMBOL(kern_path_create);
3332 void done_path_create(struct path *path, struct dentry *dentry)
3335 mutex_unlock(&path->dentry->d_inode->i_mutex);
3336 mnt_drop_write(path->mnt);
3339 EXPORT_SYMBOL(done_path_create);
3341 struct dentry *user_path_create(int dfd, const char __user *pathname,
3342 struct path *path, unsigned int lookup_flags)
3344 struct filename *tmp = getname(pathname);
3347 return ERR_CAST(tmp);
3348 res = kern_path_create(dfd, tmp->name, path, lookup_flags);
3352 EXPORT_SYMBOL(user_path_create);
3354 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3356 int error = may_create(dir, dentry);
3361 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
3364 if (!dir->i_op->mknod)
3367 error = devcgroup_inode_mknod(mode, dev);
3371 error = security_inode_mknod(dir, dentry, mode, dev);
3375 error = dir->i_op->mknod(dir, dentry, mode, dev);
3377 fsnotify_create(dir, dentry);
3381 static int may_mknod(umode_t mode)
3383 switch (mode & S_IFMT) {
3389 case 0: /* zero mode translates to S_IFREG */
3398 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3401 struct dentry *dentry;
3404 unsigned int lookup_flags = 0;
3406 error = may_mknod(mode);
3410 dentry = user_path_create(dfd, filename, &path, lookup_flags);
3412 return PTR_ERR(dentry);
3414 if (!IS_POSIXACL(path.dentry->d_inode))
3415 mode &= ~current_umask();
3416 error = security_path_mknod(&path, dentry, mode, dev);
3419 switch (mode & S_IFMT) {
3420 case 0: case S_IFREG:
3421 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
3423 case S_IFCHR: case S_IFBLK:
3424 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
3425 new_decode_dev(dev));
3427 case S_IFIFO: case S_IFSOCK:
3428 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
3432 done_path_create(&path, dentry);
3433 if (retry_estale(error, lookup_flags)) {
3434 lookup_flags |= LOOKUP_REVAL;
3440 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
3442 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3445 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3447 int error = may_create(dir, dentry);
3448 unsigned max_links = dir->i_sb->s_max_links;
3453 if (!dir->i_op->mkdir)
3456 mode &= (S_IRWXUGO|S_ISVTX);
3457 error = security_inode_mkdir(dir, dentry, mode);
3461 if (max_links && dir->i_nlink >= max_links)
3464 error = dir->i_op->mkdir(dir, dentry, mode);
3466 fsnotify_mkdir(dir, dentry);
3470 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3472 struct dentry *dentry;
3475 unsigned int lookup_flags = LOOKUP_DIRECTORY;
3478 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
3480 return PTR_ERR(dentry);
3482 if (!IS_POSIXACL(path.dentry->d_inode))
3483 mode &= ~current_umask();
3484 error = security_path_mkdir(&path, dentry, mode);
3486 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3487 done_path_create(&path, dentry);
3488 if (retry_estale(error, lookup_flags)) {
3489 lookup_flags |= LOOKUP_REVAL;
3495 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
3497 return sys_mkdirat(AT_FDCWD, pathname, mode);
3501 * The dentry_unhash() helper will try to drop the dentry early: we
3502 * should have a usage count of 1 if we're the only user of this
3503 * dentry, and if that is true (possibly after pruning the dcache),
3504 * then we drop the dentry now.
3506 * A low-level filesystem can, if it choses, legally
3509 * if (!d_unhashed(dentry))
3512 * if it cannot handle the case of removing a directory
3513 * that is still in use by something else..
3515 void dentry_unhash(struct dentry *dentry)
3517 shrink_dcache_parent(dentry);
3518 spin_lock(&dentry->d_lock);
3519 if (dentry->d_lockref.count == 1)
3521 spin_unlock(&dentry->d_lock);
3524 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3526 int error = may_delete(dir, dentry, 1);
3531 if (!dir->i_op->rmdir)
3535 mutex_lock(&dentry->d_inode->i_mutex);
3538 if (d_mountpoint(dentry))
3541 error = security_inode_rmdir(dir, dentry);
3545 shrink_dcache_parent(dentry);
3546 error = dir->i_op->rmdir(dir, dentry);
3550 dentry->d_inode->i_flags |= S_DEAD;
3554 mutex_unlock(&dentry->d_inode->i_mutex);
3561 static long do_rmdir(int dfd, const char __user *pathname)
3564 struct filename *name;
3565 struct dentry *dentry;
3566 struct nameidata nd;
3567 unsigned int lookup_flags = 0;
3569 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
3571 return PTR_ERR(name);
3573 switch(nd.last_type) {
3585 nd.flags &= ~LOOKUP_PARENT;
3586 error = mnt_want_write(nd.path.mnt);
3590 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3591 dentry = lookup_hash(&nd);
3592 error = PTR_ERR(dentry);
3595 if (!dentry->d_inode) {
3599 error = security_path_rmdir(&nd.path, dentry);
3602 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
3606 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3607 mnt_drop_write(nd.path.mnt);
3611 if (retry_estale(error, lookup_flags)) {
3612 lookup_flags |= LOOKUP_REVAL;
3618 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
3620 return do_rmdir(AT_FDCWD, pathname);
3624 * vfs_unlink - unlink a filesystem object
3625 * @dir: parent directory
3627 * @delegated_inode: returns victim inode, if the inode is delegated.
3629 * The caller must hold dir->i_mutex.
3631 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3632 * return a reference to the inode in delegated_inode. The caller
3633 * should then break the delegation on that inode and retry. Because
3634 * breaking a delegation may take a long time, the caller should drop
3635 * dir->i_mutex before doing so.
3637 * Alternatively, a caller may pass NULL for delegated_inode. This may
3638 * be appropriate for callers that expect the underlying filesystem not
3639 * to be NFS exported.
3641 int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
3643 struct inode *target = dentry->d_inode;
3644 int error = may_delete(dir, dentry, 0);
3649 if (!dir->i_op->unlink)
3652 mutex_lock(&target->i_mutex);
3653 if (d_mountpoint(dentry))
3656 error = security_inode_unlink(dir, dentry);
3658 error = try_break_deleg(target, delegated_inode);
3661 error = dir->i_op->unlink(dir, dentry);
3667 mutex_unlock(&target->i_mutex);
3669 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3670 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3671 fsnotify_link_count(target);
3679 * Make sure that the actual truncation of the file will occur outside its
3680 * directory's i_mutex. Truncate can take a long time if there is a lot of
3681 * writeout happening, and we don't want to prevent access to the directory
3682 * while waiting on the I/O.
3684 static long do_unlinkat(int dfd, const char __user *pathname)
3687 struct filename *name;
3688 struct dentry *dentry;
3689 struct nameidata nd;
3690 struct inode *inode = NULL;
3691 struct inode *delegated_inode = NULL;
3692 unsigned int lookup_flags = 0;
3694 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
3696 return PTR_ERR(name);
3699 if (nd.last_type != LAST_NORM)
3702 nd.flags &= ~LOOKUP_PARENT;
3703 error = mnt_want_write(nd.path.mnt);
3707 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3708 dentry = lookup_hash(&nd);
3709 error = PTR_ERR(dentry);
3710 if (!IS_ERR(dentry)) {
3711 /* Why not before? Because we want correct error value */
3712 if (nd.last.name[nd.last.len])
3714 inode = dentry->d_inode;
3715 if (d_is_negative(dentry))
3718 error = security_path_unlink(&nd.path, dentry);
3721 error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode);
3725 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3727 iput(inode); /* truncate the inode here */
3729 if (delegated_inode) {
3730 error = break_deleg_wait(&delegated_inode);
3734 mnt_drop_write(nd.path.mnt);
3738 if (retry_estale(error, lookup_flags)) {
3739 lookup_flags |= LOOKUP_REVAL;
3746 if (d_is_negative(dentry))
3748 else if (d_is_directory(dentry) || d_is_autodir(dentry))
3755 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
3757 if ((flag & ~AT_REMOVEDIR) != 0)
3760 if (flag & AT_REMOVEDIR)
3761 return do_rmdir(dfd, pathname);
3763 return do_unlinkat(dfd, pathname);
3766 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
3768 return do_unlinkat(AT_FDCWD, pathname);
3771 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
3773 int error = may_create(dir, dentry);
3778 if (!dir->i_op->symlink)
3781 error = security_inode_symlink(dir, dentry, oldname);
3785 error = dir->i_op->symlink(dir, dentry, oldname);
3787 fsnotify_create(dir, dentry);
3791 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3792 int, newdfd, const char __user *, newname)
3795 struct filename *from;
3796 struct dentry *dentry;
3798 unsigned int lookup_flags = 0;
3800 from = getname(oldname);
3802 return PTR_ERR(from);
3804 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
3805 error = PTR_ERR(dentry);
3809 error = security_path_symlink(&path, dentry, from->name);
3811 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
3812 done_path_create(&path, dentry);
3813 if (retry_estale(error, lookup_flags)) {
3814 lookup_flags |= LOOKUP_REVAL;
3822 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
3824 return sys_symlinkat(oldname, AT_FDCWD, newname);
3828 * vfs_link - create a new link
3829 * @old_dentry: object to be linked
3831 * @new_dentry: where to create the new link
3832 * @delegated_inode: returns inode needing a delegation break
3834 * The caller must hold dir->i_mutex
3836 * If vfs_link discovers a delegation on the to-be-linked file in need
3837 * of breaking, it will return -EWOULDBLOCK and return a reference to the
3838 * inode in delegated_inode. The caller should then break the delegation
3839 * and retry. Because breaking a delegation may take a long time, the
3840 * caller should drop the i_mutex before doing so.
3842 * Alternatively, a caller may pass NULL for delegated_inode. This may
3843 * be appropriate for callers that expect the underlying filesystem not
3844 * to be NFS exported.
3846 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
3848 struct inode *inode = old_dentry->d_inode;
3849 unsigned max_links = dir->i_sb->s_max_links;
3855 error = may_create(dir, new_dentry);
3859 if (dir->i_sb != inode->i_sb)
3863 * A link to an append-only or immutable file cannot be created.
3865 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3867 if (!dir->i_op->link)
3869 if (S_ISDIR(inode->i_mode))
3872 error = security_inode_link(old_dentry, dir, new_dentry);
3876 mutex_lock(&inode->i_mutex);
3877 /* Make sure we don't allow creating hardlink to an unlinked file */
3878 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
3880 else if (max_links && inode->i_nlink >= max_links)
3883 error = try_break_deleg(inode, delegated_inode);
3885 error = dir->i_op->link(old_dentry, dir, new_dentry);
3888 if (!error && (inode->i_state & I_LINKABLE)) {
3889 spin_lock(&inode->i_lock);
3890 inode->i_state &= ~I_LINKABLE;
3891 spin_unlock(&inode->i_lock);
3893 mutex_unlock(&inode->i_mutex);
3895 fsnotify_link(dir, inode, new_dentry);
3900 * Hardlinks are often used in delicate situations. We avoid
3901 * security-related surprises by not following symlinks on the
3904 * We don't follow them on the oldname either to be compatible
3905 * with linux 2.0, and to avoid hard-linking to directories
3906 * and other special files. --ADM
3908 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3909 int, newdfd, const char __user *, newname, int, flags)
3911 struct dentry *new_dentry;
3912 struct path old_path, new_path;
3913 struct inode *delegated_inode = NULL;
3917 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3920 * To use null names we require CAP_DAC_READ_SEARCH
3921 * This ensures that not everyone will be able to create
3922 * handlink using the passed filedescriptor.
3924 if (flags & AT_EMPTY_PATH) {
3925 if (!capable(CAP_DAC_READ_SEARCH))
3930 if (flags & AT_SYMLINK_FOLLOW)
3931 how |= LOOKUP_FOLLOW;
3933 error = user_path_at(olddfd, oldname, how, &old_path);
3937 new_dentry = user_path_create(newdfd, newname, &new_path,
3938 (how & LOOKUP_REVAL));
3939 error = PTR_ERR(new_dentry);
3940 if (IS_ERR(new_dentry))
3944 if (old_path.mnt != new_path.mnt)
3946 error = may_linkat(&old_path);
3947 if (unlikely(error))
3949 error = security_path_link(old_path.dentry, &new_path, new_dentry);
3952 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
3954 done_path_create(&new_path, new_dentry);
3955 if (delegated_inode) {
3956 error = break_deleg_wait(&delegated_inode);
3958 path_put(&old_path);
3962 if (retry_estale(error, how)) {
3963 path_put(&old_path);
3964 how |= LOOKUP_REVAL;
3968 path_put(&old_path);
3973 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
3975 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
3979 * The worst of all namespace operations - renaming directory. "Perverted"
3980 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3982 * a) we can get into loop creation. Check is done in is_subdir().
3983 * b) race potential - two innocent renames can create a loop together.
3984 * That's where 4.4 screws up. Current fix: serialization on
3985 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3987 * c) we have to lock _four_ objects - parents and victim (if it exists),
3988 * and source (if it is not a directory).
3989 * And that - after we got ->i_mutex on parents (until then we don't know
3990 * whether the target exists). Solution: try to be smart with locking
3991 * order for inodes. We rely on the fact that tree topology may change
3992 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
3993 * move will be locked. Thus we can rank directories by the tree
3994 * (ancestors first) and rank all non-directories after them.
3995 * That works since everybody except rename does "lock parent, lookup,
3996 * lock child" and rename is under ->s_vfs_rename_mutex.
3997 * HOWEVER, it relies on the assumption that any object with ->lookup()
3998 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3999 * we'd better make sure that there's no link(2) for them.
4000 * d) conversion from fhandle to dentry may come in the wrong moment - when
4001 * we are removing the target. Solution: we will have to grab ->i_mutex
4002 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4003 * ->i_mutex on parents, which works but leads to some truly excessive
4006 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
4007 struct inode *new_dir, struct dentry *new_dentry)
4010 struct inode *target = new_dentry->d_inode;
4011 unsigned max_links = new_dir->i_sb->s_max_links;
4014 * If we are going to change the parent - check write permissions,
4015 * we'll need to flip '..'.
4017 if (new_dir != old_dir) {
4018 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
4023 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
4029 mutex_lock(&target->i_mutex);
4032 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
4036 if (max_links && !target && new_dir != old_dir &&
4037 new_dir->i_nlink >= max_links)
4041 shrink_dcache_parent(new_dentry);
4042 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4047 target->i_flags |= S_DEAD;
4048 dont_mount(new_dentry);
4052 mutex_unlock(&target->i_mutex);
4055 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
4056 d_move(old_dentry,new_dentry);
4060 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
4061 struct inode *new_dir, struct dentry *new_dentry,
4062 struct inode **delegated_inode)
4064 struct inode *target = new_dentry->d_inode;
4065 struct inode *source = old_dentry->d_inode;
4068 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
4073 lock_two_nondirectories(source, target);
4076 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
4079 error = try_break_deleg(source, delegated_inode);
4083 error = try_break_deleg(target, delegated_inode);
4087 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4092 dont_mount(new_dentry);
4093 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
4094 d_move(old_dentry, new_dentry);
4096 unlock_two_nondirectories(source, target);
4102 * vfs_rename - rename a filesystem object
4103 * @old_dir: parent of source
4104 * @old_dentry: source
4105 * @new_dir: parent of destination
4106 * @new_dentry: destination
4107 * @delegated_inode: returns an inode needing a delegation break
4109 * The caller must hold multiple mutexes--see lock_rename()).
4111 * If vfs_rename discovers a delegation in need of breaking at either
4112 * the source or destination, it will return -EWOULDBLOCK and return a
4113 * reference to the inode in delegated_inode. The caller should then
4114 * break the delegation and retry. Because breaking a delegation may
4115 * take a long time, the caller should drop all locks before doing
4118 * Alternatively, a caller may pass NULL for delegated_inode. This may
4119 * be appropriate for callers that expect the underlying filesystem not
4120 * to be NFS exported.
4122 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4123 struct inode *new_dir, struct dentry *new_dentry,
4124 struct inode **delegated_inode)
4127 int is_dir = d_is_directory(old_dentry) || d_is_autodir(old_dentry);
4128 const unsigned char *old_name;
4130 if (old_dentry->d_inode == new_dentry->d_inode)
4133 error = may_delete(old_dir, old_dentry, is_dir);
4137 if (!new_dentry->d_inode)
4138 error = may_create(new_dir, new_dentry);
4140 error = may_delete(new_dir, new_dentry, is_dir);
4144 if (!old_dir->i_op->rename)
4147 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4150 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
4152 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry,delegated_inode);
4154 fsnotify_move(old_dir, new_dir, old_name, is_dir,
4155 new_dentry->d_inode, old_dentry);
4156 fsnotify_oldname_free(old_name);
4161 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4162 int, newdfd, const char __user *, newname)
4164 struct dentry *old_dir, *new_dir;
4165 struct dentry *old_dentry, *new_dentry;
4166 struct dentry *trap;
4167 struct nameidata oldnd, newnd;
4168 struct inode *delegated_inode = NULL;
4169 struct filename *from;
4170 struct filename *to;
4171 unsigned int lookup_flags = 0;
4172 bool should_retry = false;
4175 from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
4177 error = PTR_ERR(from);
4181 to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
4183 error = PTR_ERR(to);
4188 if (oldnd.path.mnt != newnd.path.mnt)
4191 old_dir = oldnd.path.dentry;
4193 if (oldnd.last_type != LAST_NORM)
4196 new_dir = newnd.path.dentry;
4197 if (newnd.last_type != LAST_NORM)
4200 error = mnt_want_write(oldnd.path.mnt);
4204 oldnd.flags &= ~LOOKUP_PARENT;
4205 newnd.flags &= ~LOOKUP_PARENT;
4206 newnd.flags |= LOOKUP_RENAME_TARGET;
4209 trap = lock_rename(new_dir, old_dir);
4211 old_dentry = lookup_hash(&oldnd);
4212 error = PTR_ERR(old_dentry);
4213 if (IS_ERR(old_dentry))
4215 /* source must exist */
4217 if (d_is_negative(old_dentry))
4219 /* unless the source is a directory trailing slashes give -ENOTDIR */
4220 if (!d_is_directory(old_dentry) && !d_is_autodir(old_dentry)) {
4222 if (oldnd.last.name[oldnd.last.len])
4224 if (newnd.last.name[newnd.last.len])
4227 /* source should not be ancestor of target */
4229 if (old_dentry == trap)
4231 new_dentry = lookup_hash(&newnd);
4232 error = PTR_ERR(new_dentry);
4233 if (IS_ERR(new_dentry))
4235 /* target should not be an ancestor of source */
4237 if (new_dentry == trap)
4240 error = security_path_rename(&oldnd.path, old_dentry,
4241 &newnd.path, new_dentry);
4244 error = vfs_rename(old_dir->d_inode, old_dentry,
4245 new_dir->d_inode, new_dentry,
4252 unlock_rename(new_dir, old_dir);
4253 if (delegated_inode) {
4254 error = break_deleg_wait(&delegated_inode);
4258 mnt_drop_write(oldnd.path.mnt);
4260 if (retry_estale(error, lookup_flags))
4261 should_retry = true;
4262 path_put(&newnd.path);
4265 path_put(&oldnd.path);
4268 should_retry = false;
4269 lookup_flags |= LOOKUP_REVAL;
4276 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
4278 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
4281 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
4285 len = PTR_ERR(link);
4290 if (len > (unsigned) buflen)
4292 if (copy_to_user(buffer, link, len))
4299 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4300 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4301 * using) it for any given inode is up to filesystem.
4303 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4305 struct nameidata nd;
4310 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
4312 return PTR_ERR(cookie);
4314 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
4315 if (dentry->d_inode->i_op->put_link)
4316 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4320 /* get the link contents into pagecache */
4321 static char *page_getlink(struct dentry * dentry, struct page **ppage)
4325 struct address_space *mapping = dentry->d_inode->i_mapping;
4326 page = read_mapping_page(mapping, 0, NULL);
4331 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4335 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4337 struct page *page = NULL;
4338 char *s = page_getlink(dentry, &page);
4339 int res = vfs_readlink(dentry,buffer,buflen,s);
4342 page_cache_release(page);
4347 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
4349 struct page *page = NULL;
4350 nd_set_link(nd, page_getlink(dentry, &page));
4354 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
4356 struct page *page = cookie;
4360 page_cache_release(page);
4365 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4367 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
4369 struct address_space *mapping = inode->i_mapping;
4374 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4376 flags |= AOP_FLAG_NOFS;
4379 err = pagecache_write_begin(NULL, mapping, 0, len-1,
4380 flags, &page, &fsdata);
4384 kaddr = kmap_atomic(page);
4385 memcpy(kaddr, symname, len-1);
4386 kunmap_atomic(kaddr);
4388 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4395 mark_inode_dirty(inode);
4401 int page_symlink(struct inode *inode, const char *symname, int len)
4403 return __page_symlink(inode, symname, len,
4404 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
4407 const struct inode_operations page_symlink_inode_operations = {
4408 .readlink = generic_readlink,
4409 .follow_link = page_follow_link_light,
4410 .put_link = page_put_link,
4413 EXPORT_SYMBOL(user_path_at);
4414 EXPORT_SYMBOL(follow_down_one);
4415 EXPORT_SYMBOL(follow_down);
4416 EXPORT_SYMBOL(follow_up);
4417 EXPORT_SYMBOL(get_write_access); /* nfsd */
4418 EXPORT_SYMBOL(lock_rename);
4419 EXPORT_SYMBOL(lookup_one_len);
4420 EXPORT_SYMBOL(page_follow_link_light);
4421 EXPORT_SYMBOL(page_put_link);
4422 EXPORT_SYMBOL(page_readlink);
4423 EXPORT_SYMBOL(__page_symlink);
4424 EXPORT_SYMBOL(page_symlink);
4425 EXPORT_SYMBOL(page_symlink_inode_operations);
4426 EXPORT_SYMBOL(kern_path);
4427 EXPORT_SYMBOL(vfs_path_lookup);
4428 EXPORT_SYMBOL(inode_permission);
4429 EXPORT_SYMBOL(unlock_rename);
4430 EXPORT_SYMBOL(vfs_create);
4431 EXPORT_SYMBOL(vfs_link);
4432 EXPORT_SYMBOL(vfs_mkdir);
4433 EXPORT_SYMBOL(vfs_mknod);
4434 EXPORT_SYMBOL(generic_permission);
4435 EXPORT_SYMBOL(vfs_readlink);
4436 EXPORT_SYMBOL(vfs_rename);
4437 EXPORT_SYMBOL(vfs_rmdir);
4438 EXPORT_SYMBOL(vfs_symlink);
4439 EXPORT_SYMBOL(vfs_unlink);
4440 EXPORT_SYMBOL(dentry_unhash);
4441 EXPORT_SYMBOL(generic_readlink);