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/module.h>
19 #include <linux/slab.h>
21 #include <linux/namei.h>
22 #include <linux/pagemap.h>
23 #include <linux/fsnotify.h>
24 #include <linux/personality.h>
25 #include <linux/security.h>
26 #include <linux/ima.h>
27 #include <linux/syscalls.h>
28 #include <linux/mount.h>
29 #include <linux/audit.h>
30 #include <linux/capability.h>
31 #include <linux/file.h>
32 #include <linux/fcntl.h>
33 #include <linux/device_cgroup.h>
34 #include <linux/fs_struct.h>
35 #include <asm/uaccess.h>
39 /* [Feb-1997 T. Schoebel-Theuer]
40 * Fundamental changes in the pathname lookup mechanisms (namei)
41 * were necessary because of omirr. The reason is that omirr needs
42 * to know the _real_ pathname, not the user-supplied one, in case
43 * of symlinks (and also when transname replacements occur).
45 * The new code replaces the old recursive symlink resolution with
46 * an iterative one (in case of non-nested symlink chains). It does
47 * this with calls to <fs>_follow_link().
48 * As a side effect, dir_namei(), _namei() and follow_link() are now
49 * replaced with a single function lookup_dentry() that can handle all
50 * the special cases of the former code.
52 * With the new dcache, the pathname is stored at each inode, at least as
53 * long as the refcount of the inode is positive. As a side effect, the
54 * size of the dcache depends on the inode cache and thus is dynamic.
56 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57 * resolution to correspond with current state of the code.
59 * Note that the symlink resolution is not *completely* iterative.
60 * There is still a significant amount of tail- and mid- recursion in
61 * the algorithm. Also, note that <fs>_readlink() is not used in
62 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
63 * may return different results than <fs>_follow_link(). Many virtual
64 * filesystems (including /proc) exhibit this behavior.
67 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
68 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
69 * and the name already exists in form of a symlink, try to create the new
70 * name indicated by the symlink. The old code always complained that the
71 * name already exists, due to not following the symlink even if its target
72 * is nonexistent. The new semantics affects also mknod() and link() when
73 * the name is a symlink pointing to a non-existant name.
75 * I don't know which semantics is the right one, since I have no access
76 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78 * "old" one. Personally, I think the new semantics is much more logical.
79 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80 * file does succeed in both HP-UX and SunOs, but not in Solaris
81 * and in the old Linux semantics.
84 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85 * semantics. See the comments in "open_namei" and "do_link" below.
87 * [10-Sep-98 Alan Modra] Another symlink change.
90 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91 * inside the path - always follow.
92 * in the last component in creation/removal/renaming - never follow.
93 * if LOOKUP_FOLLOW passed - follow.
94 * if the pathname has trailing slashes - follow.
95 * otherwise - don't follow.
96 * (applied in that order).
98 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100 * During the 2.4 we need to fix the userland stuff depending on it -
101 * hopefully we will be able to get rid of that wart in 2.5. So far only
102 * XEmacs seems to be relying on it...
105 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
106 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
107 * any extra contention...
110 /* In order to reduce some races, while at the same time doing additional
111 * checking and hopefully speeding things up, we copy filenames to the
112 * kernel data space before using them..
114 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
115 * PATH_MAX includes the nul terminator --RR.
117 static int do_getname(const char __user *filename, char *page)
120 unsigned long len = PATH_MAX;
122 if (!segment_eq(get_fs(), KERNEL_DS)) {
123 if ((unsigned long) filename >= TASK_SIZE)
125 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
126 len = TASK_SIZE - (unsigned long) filename;
129 retval = strncpy_from_user(page, filename, len);
133 return -ENAMETOOLONG;
139 static char *getname_flags(const char __user * filename, int flags)
143 result = ERR_PTR(-ENOMEM);
146 int retval = do_getname(filename, tmp);
150 if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
152 result = ERR_PTR(retval);
156 audit_getname(result);
160 char *getname(const char __user * filename)
162 return getname_flags(filename, 0);
165 #ifdef CONFIG_AUDITSYSCALL
166 void putname(const char *name)
168 if (unlikely(!audit_dummy_context()))
173 EXPORT_SYMBOL(putname);
177 * This does basic POSIX ACL permission checking
179 static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
180 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
182 umode_t mode = inode->i_mode;
184 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
186 if (current_fsuid() == inode->i_uid)
189 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
190 int error = check_acl(inode, mask, flags);
191 if (error != -EAGAIN)
195 if (in_group_p(inode->i_gid))
200 * If the DACs are ok we don't need any capability check.
202 if ((mask & ~mode) == 0)
208 * generic_permission - check for access rights on a Posix-like filesystem
209 * @inode: inode to check access rights for
210 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
211 * @check_acl: optional callback to check for Posix ACLs
212 * @flags: IPERM_FLAG_ flags.
214 * Used to check for read/write/execute permissions on a file.
215 * We use "fsuid" for this, letting us set arbitrary permissions
216 * for filesystem access without changing the "normal" uids which
217 * are used for other things.
219 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
220 * request cannot be satisfied (eg. requires blocking or too much complexity).
221 * It would then be called again in ref-walk mode.
223 int generic_permission(struct inode *inode, int mask, unsigned int flags,
224 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
229 * Do the basic POSIX ACL permission checks.
231 ret = acl_permission_check(inode, mask, flags, check_acl);
236 * Read/write DACs are always overridable.
237 * Executable DACs are overridable if at least one exec bit is set.
239 if (!(mask & MAY_EXEC) || execute_ok(inode))
240 if (capable(CAP_DAC_OVERRIDE))
244 * Searching includes executable on directories, else just read.
246 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
247 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
248 if (capable(CAP_DAC_READ_SEARCH))
255 * inode_permission - check for access rights to a given inode
256 * @inode: inode to check permission on
257 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
259 * Used to check for read/write/execute permissions on an inode.
260 * We use "fsuid" for this, letting us set arbitrary permissions
261 * for filesystem access without changing the "normal" uids which
262 * are used for other things.
264 int inode_permission(struct inode *inode, int mask)
268 if (mask & MAY_WRITE) {
269 umode_t mode = inode->i_mode;
272 * Nobody gets write access to a read-only fs.
274 if (IS_RDONLY(inode) &&
275 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
279 * Nobody gets write access to an immutable file.
281 if (IS_IMMUTABLE(inode))
285 if (inode->i_op->permission)
286 retval = inode->i_op->permission(inode, mask, 0);
288 retval = generic_permission(inode, mask, 0,
289 inode->i_op->check_acl);
294 retval = devcgroup_inode_permission(inode, mask);
298 return security_inode_permission(inode, mask);
302 * file_permission - check for additional access rights to a given file
303 * @file: file to check access rights for
304 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
306 * Used to check for read/write/execute permissions on an already opened
310 * Do not use this function in new code. All access checks should
311 * be done using inode_permission().
313 int file_permission(struct file *file, int mask)
315 return inode_permission(file->f_path.dentry->d_inode, mask);
319 * get_write_access() gets write permission for a file.
320 * put_write_access() releases this write permission.
321 * This is used for regular files.
322 * We cannot support write (and maybe mmap read-write shared) accesses and
323 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
324 * can have the following values:
325 * 0: no writers, no VM_DENYWRITE mappings
326 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
327 * > 0: (i_writecount) users are writing to the file.
329 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
330 * except for the cases where we don't hold i_writecount yet. Then we need to
331 * use {get,deny}_write_access() - these functions check the sign and refuse
332 * to do the change if sign is wrong. Exclusion between them is provided by
333 * the inode->i_lock spinlock.
336 int get_write_access(struct inode * inode)
338 spin_lock(&inode->i_lock);
339 if (atomic_read(&inode->i_writecount) < 0) {
340 spin_unlock(&inode->i_lock);
343 atomic_inc(&inode->i_writecount);
344 spin_unlock(&inode->i_lock);
349 int deny_write_access(struct file * file)
351 struct inode *inode = file->f_path.dentry->d_inode;
353 spin_lock(&inode->i_lock);
354 if (atomic_read(&inode->i_writecount) > 0) {
355 spin_unlock(&inode->i_lock);
358 atomic_dec(&inode->i_writecount);
359 spin_unlock(&inode->i_lock);
365 * path_get - get a reference to a path
366 * @path: path to get the reference to
368 * Given a path increment the reference count to the dentry and the vfsmount.
370 void path_get(struct path *path)
375 EXPORT_SYMBOL(path_get);
378 * path_put - put a reference to a path
379 * @path: path to put the reference to
381 * Given a path decrement the reference count to the dentry and the vfsmount.
383 void path_put(struct path *path)
388 EXPORT_SYMBOL(path_put);
391 * nameidata_drop_rcu - drop this nameidata out of rcu-walk
392 * @nd: nameidata pathwalk data to drop
393 * Returns: 0 on success, -ECHILD on failure
395 * Path walking has 2 modes, rcu-walk and ref-walk (see
396 * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt
397 * to drop out of rcu-walk mode and take normal reference counts on dentries
398 * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take
399 * refcounts at the last known good point before rcu-walk got stuck, so
400 * ref-walk may continue from there. If this is not successful (eg. a seqcount
401 * has changed), then failure is returned and path walk restarts from the
402 * beginning in ref-walk mode.
404 * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into
405 * ref-walk. Must be called from rcu-walk context.
407 static int nameidata_drop_rcu(struct nameidata *nd)
409 struct fs_struct *fs = current->fs;
410 struct dentry *dentry = nd->path.dentry;
413 BUG_ON(!(nd->flags & LOOKUP_RCU));
414 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
416 spin_lock(&fs->lock);
417 if (nd->root.mnt != fs->root.mnt ||
418 nd->root.dentry != fs->root.dentry)
421 spin_lock(&dentry->d_lock);
422 if (!__d_rcu_to_refcount(dentry, nd->seq))
424 BUG_ON(nd->inode != dentry->d_inode);
425 spin_unlock(&dentry->d_lock);
428 spin_unlock(&fs->lock);
430 mntget(nd->path.mnt);
433 br_read_unlock(vfsmount_lock);
434 nd->flags &= ~LOOKUP_RCU;
437 spin_unlock(&dentry->d_lock);
440 spin_unlock(&fs->lock);
444 /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
445 static inline int nameidata_drop_rcu_maybe(struct nameidata *nd)
447 if (nd->flags & LOOKUP_RCU)
448 return nameidata_drop_rcu(nd);
453 * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
454 * @nd: nameidata pathwalk data to drop
455 * @dentry: dentry to drop
456 * Returns: 0 on success, -ECHILD on failure
458 * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
459 * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
460 * @nd. Must be called from rcu-walk context.
462 static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry)
464 struct fs_struct *fs = current->fs;
465 struct dentry *parent = nd->path.dentry;
468 BUG_ON(!(nd->flags & LOOKUP_RCU));
469 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
471 spin_lock(&fs->lock);
472 if (nd->root.mnt != fs->root.mnt ||
473 nd->root.dentry != fs->root.dentry)
476 spin_lock(&parent->d_lock);
477 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
478 if (!__d_rcu_to_refcount(dentry, nd->seq))
481 * If the sequence check on the child dentry passed, then the child has
482 * not been removed from its parent. This means the parent dentry must
483 * be valid and able to take a reference at this point.
485 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
486 BUG_ON(!parent->d_count);
488 spin_unlock(&dentry->d_lock);
489 spin_unlock(&parent->d_lock);
492 spin_unlock(&fs->lock);
494 mntget(nd->path.mnt);
497 br_read_unlock(vfsmount_lock);
498 nd->flags &= ~LOOKUP_RCU;
501 spin_unlock(&dentry->d_lock);
502 spin_unlock(&parent->d_lock);
505 spin_unlock(&fs->lock);
509 /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
510 static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
512 if (nd->flags & LOOKUP_RCU) {
513 if (unlikely(nameidata_dentry_drop_rcu(nd, dentry))) {
514 nd->flags &= ~LOOKUP_RCU;
515 if (!(nd->flags & LOOKUP_ROOT))
518 br_read_unlock(vfsmount_lock);
526 * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
527 * @nd: nameidata pathwalk data to drop
528 * Returns: 0 on success, -ECHILD on failure
530 * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
531 * nd->path should be the final element of the lookup, so nd->root is discarded.
532 * Must be called from rcu-walk context.
534 static int nameidata_drop_rcu_last(struct nameidata *nd)
536 struct dentry *dentry = nd->path.dentry;
538 BUG_ON(!(nd->flags & LOOKUP_RCU));
539 nd->flags &= ~LOOKUP_RCU;
540 if (!(nd->flags & LOOKUP_ROOT))
542 spin_lock(&dentry->d_lock);
543 if (!__d_rcu_to_refcount(dentry, nd->seq))
545 BUG_ON(nd->inode != dentry->d_inode);
546 spin_unlock(&dentry->d_lock);
548 mntget(nd->path.mnt);
551 br_read_unlock(vfsmount_lock);
556 spin_unlock(&dentry->d_lock);
558 br_read_unlock(vfsmount_lock);
563 * release_open_intent - free up open intent resources
564 * @nd: pointer to nameidata
566 void release_open_intent(struct nameidata *nd)
568 struct file *file = nd->intent.open.file;
570 if (file && !IS_ERR(file)) {
571 if (file->f_path.dentry == NULL)
578 static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
580 return dentry->d_op->d_revalidate(dentry, nd);
583 static struct dentry *
584 do_revalidate(struct dentry *dentry, struct nameidata *nd)
586 int status = d_revalidate(dentry, nd);
587 if (unlikely(status <= 0)) {
589 * The dentry failed validation.
590 * If d_revalidate returned 0 attempt to invalidate
591 * the dentry otherwise d_revalidate is asking us
592 * to return a fail status.
596 dentry = ERR_PTR(status);
597 } else if (!d_invalidate(dentry)) {
606 * handle_reval_path - force revalidation of a dentry
608 * In some situations the path walking code will trust dentries without
609 * revalidating them. This causes problems for filesystems that depend on
610 * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
611 * (which indicates that it's possible for the dentry to go stale), force
612 * a d_revalidate call before proceeding.
614 * Returns 0 if the revalidation was successful. If the revalidation fails,
615 * either return the error returned by d_revalidate or -ESTALE if the
616 * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
617 * invalidate the dentry. It's up to the caller to handle putting references
618 * to the path if necessary.
620 static inline int handle_reval_path(struct nameidata *nd)
622 struct dentry *dentry = nd->path.dentry;
625 if (likely(!(nd->flags & LOOKUP_JUMPED)))
628 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
631 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
634 /* Note: we do not d_invalidate() */
635 status = d_revalidate(dentry, nd);
646 * Short-cut version of permission(), for calling on directories
647 * during pathname resolution. Combines parts of permission()
648 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
650 * If appropriate, check DAC only. If not appropriate, or
651 * short-cut DAC fails, then call ->permission() to do more
652 * complete permission check.
654 static inline int exec_permission(struct inode *inode, unsigned int flags)
658 if (inode->i_op->permission) {
659 ret = inode->i_op->permission(inode, MAY_EXEC, flags);
661 ret = acl_permission_check(inode, MAY_EXEC, flags,
662 inode->i_op->check_acl);
669 if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
674 return security_inode_exec_permission(inode, flags);
677 static __always_inline void set_root(struct nameidata *nd)
680 get_fs_root(current->fs, &nd->root);
683 static int link_path_walk(const char *, struct nameidata *);
685 static __always_inline void set_root_rcu(struct nameidata *nd)
688 struct fs_struct *fs = current->fs;
692 seq = read_seqcount_begin(&fs->seq);
694 } while (read_seqcount_retry(&fs->seq, seq));
698 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
710 nd->flags |= LOOKUP_JUMPED;
712 nd->inode = nd->path.dentry->d_inode;
714 ret = link_path_walk(link, nd);
718 return PTR_ERR(link);
721 static void path_put_conditional(struct path *path, struct nameidata *nd)
724 if (path->mnt != nd->path.mnt)
728 static inline void path_to_nameidata(const struct path *path,
729 struct nameidata *nd)
731 if (!(nd->flags & LOOKUP_RCU)) {
732 dput(nd->path.dentry);
733 if (nd->path.mnt != path->mnt)
734 mntput(nd->path.mnt);
736 nd->path.mnt = path->mnt;
737 nd->path.dentry = path->dentry;
740 static __always_inline int
741 __do_follow_link(const struct path *link, struct nameidata *nd, void **p)
744 struct dentry *dentry = link->dentry;
746 BUG_ON(nd->flags & LOOKUP_RCU);
748 touch_atime(link->mnt, dentry);
749 nd_set_link(nd, NULL);
751 if (link->mnt == nd->path.mnt)
754 error = security_inode_follow_link(link->dentry, nd);
756 *p = ERR_PTR(error); /* no ->put_link(), please */
761 nd->last_type = LAST_BIND;
762 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
765 char *s = nd_get_link(nd);
768 error = __vfs_follow_link(nd, s);
769 else if (nd->last_type == LAST_BIND) {
770 nd->flags |= LOOKUP_JUMPED;
771 nd->inode = nd->path.dentry->d_inode;
772 if (nd->inode->i_op->follow_link) {
773 /* stepped on a _really_ weird one */
783 * This limits recursive symlink follows to 8, while
784 * limiting consecutive symlinks to 40.
786 * Without that kind of total limit, nasty chains of consecutive
787 * symlinks can cause almost arbitrarily long lookups.
789 static inline int do_follow_link(struct path *path, struct nameidata *nd)
794 if (current->link_count >= MAX_NESTED_LINKS)
796 if (current->total_link_count >= 40)
798 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
800 current->link_count++;
801 current->total_link_count++;
803 err = __do_follow_link(path, nd, &cookie);
804 if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
805 path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
807 current->link_count--;
811 path_put_conditional(path, nd);
816 static int follow_up_rcu(struct path *path)
818 struct vfsmount *parent;
819 struct dentry *mountpoint;
821 parent = path->mnt->mnt_parent;
822 if (parent == path->mnt)
824 mountpoint = path->mnt->mnt_mountpoint;
825 path->dentry = mountpoint;
830 int follow_up(struct path *path)
832 struct vfsmount *parent;
833 struct dentry *mountpoint;
835 br_read_lock(vfsmount_lock);
836 parent = path->mnt->mnt_parent;
837 if (parent == path->mnt) {
838 br_read_unlock(vfsmount_lock);
842 mountpoint = dget(path->mnt->mnt_mountpoint);
843 br_read_unlock(vfsmount_lock);
845 path->dentry = mountpoint;
852 * Perform an automount
853 * - return -EISDIR to tell follow_managed() to stop and return the path we
856 static int follow_automount(struct path *path, unsigned flags,
859 struct vfsmount *mnt;
862 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
865 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
866 * and this is the terminal part of the path.
868 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
869 return -EISDIR; /* we actually want to stop here */
871 /* We want to mount if someone is trying to open/create a file of any
872 * type under the mountpoint, wants to traverse through the mountpoint
873 * or wants to open the mounted directory.
875 * We don't want to mount if someone's just doing a stat and they've
876 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
877 * appended a '/' to the name.
879 if (!(flags & LOOKUP_FOLLOW) &&
880 !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
881 LOOKUP_OPEN | LOOKUP_CREATE)))
884 current->total_link_count++;
885 if (current->total_link_count >= 40)
888 mnt = path->dentry->d_op->d_automount(path);
891 * The filesystem is allowed to return -EISDIR here to indicate
892 * it doesn't want to automount. For instance, autofs would do
893 * this so that its userspace daemon can mount on this dentry.
895 * However, we can only permit this if it's a terminal point in
896 * the path being looked up; if it wasn't then the remainder of
897 * the path is inaccessible and we should say so.
899 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
904 if (!mnt) /* mount collision */
907 err = finish_automount(mnt, path);
911 /* Someone else made a mount here whilst we were busy */
918 path->dentry = dget(mnt->mnt_root);
928 * Handle a dentry that is managed in some way.
929 * - Flagged for transit management (autofs)
930 * - Flagged as mountpoint
931 * - Flagged as automount point
933 * This may only be called in refwalk mode.
935 * Serialization is taken care of in namespace.c
937 static int follow_managed(struct path *path, unsigned flags)
940 bool need_mntput = false;
943 /* Given that we're not holding a lock here, we retain the value in a
944 * local variable for each dentry as we look at it so that we don't see
945 * the components of that value change under us */
946 while (managed = ACCESS_ONCE(path->dentry->d_flags),
947 managed &= DCACHE_MANAGED_DENTRY,
948 unlikely(managed != 0)) {
949 /* Allow the filesystem to manage the transit without i_mutex
951 if (managed & DCACHE_MANAGE_TRANSIT) {
952 BUG_ON(!path->dentry->d_op);
953 BUG_ON(!path->dentry->d_op->d_manage);
954 ret = path->dentry->d_op->d_manage(path->dentry,
957 return ret == -EISDIR ? 0 : ret;
960 /* Transit to a mounted filesystem. */
961 if (managed & DCACHE_MOUNTED) {
962 struct vfsmount *mounted = lookup_mnt(path);
968 path->dentry = dget(mounted->mnt_root);
973 /* Something is mounted on this dentry in another
974 * namespace and/or whatever was mounted there in this
975 * namespace got unmounted before we managed to get the
979 /* Handle an automount point */
980 if (managed & DCACHE_NEED_AUTOMOUNT) {
981 ret = follow_automount(path, flags, &need_mntput);
983 return ret == -EISDIR ? 0 : ret;
987 /* We didn't change the current path point */
993 int follow_down_one(struct path *path)
995 struct vfsmount *mounted;
997 mounted = lookup_mnt(path);
1001 path->mnt = mounted;
1002 path->dentry = dget(mounted->mnt_root);
1009 * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we
1010 * meet a managed dentry and we're not walking to "..". True is returned to
1011 * continue, false to abort.
1013 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1014 struct inode **inode, bool reverse_transit)
1016 while (d_mountpoint(path->dentry)) {
1017 struct vfsmount *mounted;
1018 if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
1020 path->dentry->d_op->d_manage(path->dentry, false, true) < 0)
1022 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1025 path->mnt = mounted;
1026 path->dentry = mounted->mnt_root;
1027 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
1028 *inode = path->dentry->d_inode;
1031 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1032 return reverse_transit;
1036 static int follow_dotdot_rcu(struct nameidata *nd)
1038 struct inode *inode = nd->inode;
1043 if (nd->path.dentry == nd->root.dentry &&
1044 nd->path.mnt == nd->root.mnt) {
1047 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1048 struct dentry *old = nd->path.dentry;
1049 struct dentry *parent = old->d_parent;
1052 seq = read_seqcount_begin(&parent->d_seq);
1053 if (read_seqcount_retry(&old->d_seq, nd->seq))
1055 inode = parent->d_inode;
1056 nd->path.dentry = parent;
1060 if (!follow_up_rcu(&nd->path))
1062 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1063 inode = nd->path.dentry->d_inode;
1065 __follow_mount_rcu(nd, &nd->path, &inode, true);
1070 nd->flags &= ~LOOKUP_RCU;
1071 if (!(nd->flags & LOOKUP_ROOT))
1072 nd->root.mnt = NULL;
1074 br_read_unlock(vfsmount_lock);
1079 * Follow down to the covering mount currently visible to userspace. At each
1080 * point, the filesystem owning that dentry may be queried as to whether the
1081 * caller is permitted to proceed or not.
1083 * Care must be taken as namespace_sem may be held (indicated by mounting_here
1086 int follow_down(struct path *path, bool mounting_here)
1091 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1092 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1093 /* Allow the filesystem to manage the transit without i_mutex
1096 * We indicate to the filesystem if someone is trying to mount
1097 * something here. This gives autofs the chance to deny anyone
1098 * other than its daemon the right to mount on its
1101 * The filesystem may sleep at this point.
1103 if (managed & DCACHE_MANAGE_TRANSIT) {
1104 BUG_ON(!path->dentry->d_op);
1105 BUG_ON(!path->dentry->d_op->d_manage);
1106 ret = path->dentry->d_op->d_manage(
1107 path->dentry, mounting_here, false);
1109 return ret == -EISDIR ? 0 : ret;
1112 /* Transit to a mounted filesystem. */
1113 if (managed & DCACHE_MOUNTED) {
1114 struct vfsmount *mounted = lookup_mnt(path);
1119 path->mnt = mounted;
1120 path->dentry = dget(mounted->mnt_root);
1124 /* Don't handle automount points here */
1131 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1133 static void follow_mount(struct path *path)
1135 while (d_mountpoint(path->dentry)) {
1136 struct vfsmount *mounted = lookup_mnt(path);
1141 path->mnt = mounted;
1142 path->dentry = dget(mounted->mnt_root);
1146 static void follow_dotdot(struct nameidata *nd)
1151 struct dentry *old = nd->path.dentry;
1153 if (nd->path.dentry == nd->root.dentry &&
1154 nd->path.mnt == nd->root.mnt) {
1157 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1158 /* rare case of legitimate dget_parent()... */
1159 nd->path.dentry = dget_parent(nd->path.dentry);
1163 if (!follow_up(&nd->path))
1166 follow_mount(&nd->path);
1167 nd->inode = nd->path.dentry->d_inode;
1171 * Allocate a dentry with name and parent, and perform a parent
1172 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1173 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1174 * have verified that no child exists while under i_mutex.
1176 static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1177 struct qstr *name, struct nameidata *nd)
1179 struct inode *inode = parent->d_inode;
1180 struct dentry *dentry;
1183 /* Don't create child dentry for a dead directory. */
1184 if (unlikely(IS_DEADDIR(inode)))
1185 return ERR_PTR(-ENOENT);
1187 dentry = d_alloc(parent, name);
1188 if (unlikely(!dentry))
1189 return ERR_PTR(-ENOMEM);
1191 old = inode->i_op->lookup(inode, dentry, nd);
1192 if (unlikely(old)) {
1200 * It's more convoluted than I'd like it to be, but... it's still fairly
1201 * small and for now I'd prefer to have fast path as straight as possible.
1202 * It _is_ time-critical.
1204 static int do_lookup(struct nameidata *nd, struct qstr *name,
1205 struct path *path, struct inode **inode)
1207 struct vfsmount *mnt = nd->path.mnt;
1208 struct dentry *dentry, *parent = nd->path.dentry;
1214 * Rename seqlock is not required here because in the off chance
1215 * of a false negative due to a concurrent rename, we're going to
1216 * do the non-racy lookup, below.
1218 if (nd->flags & LOOKUP_RCU) {
1221 dentry = __d_lookup_rcu(parent, name, &seq, inode);
1225 /* Memory barrier in read_seqcount_begin of child is enough */
1226 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1230 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1231 status = d_revalidate(dentry, nd);
1232 if (unlikely(status <= 0)) {
1233 if (status != -ECHILD)
1239 path->dentry = dentry;
1240 if (likely(__follow_mount_rcu(nd, path, inode, false)))
1244 if (nameidata_dentry_drop_rcu(nd, dentry))
1247 if (nameidata_drop_rcu(nd))
1251 dentry = __d_lookup(parent, name);
1255 if (unlikely(!dentry)) {
1256 struct inode *dir = parent->d_inode;
1257 BUG_ON(nd->inode != dir);
1259 mutex_lock(&dir->i_mutex);
1260 dentry = d_lookup(parent, name);
1261 if (likely(!dentry)) {
1262 dentry = d_alloc_and_lookup(parent, name, nd);
1263 if (IS_ERR(dentry)) {
1264 mutex_unlock(&dir->i_mutex);
1265 return PTR_ERR(dentry);
1271 mutex_unlock(&dir->i_mutex);
1273 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1274 status = d_revalidate(dentry, nd);
1275 if (unlikely(status <= 0)) {
1280 if (!d_invalidate(dentry)) {
1289 path->dentry = dentry;
1290 err = follow_managed(path, nd->flags);
1291 if (unlikely(err < 0)) {
1292 path_put_conditional(path, nd);
1295 *inode = path->dentry->d_inode;
1299 static inline int may_lookup(struct nameidata *nd)
1301 if (nd->flags & LOOKUP_RCU) {
1302 int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
1305 if (nameidata_drop_rcu(nd))
1308 return exec_permission(nd->inode, 0);
1311 static inline int handle_dots(struct nameidata *nd, int type)
1313 if (type == LAST_DOTDOT) {
1314 if (nd->flags & LOOKUP_RCU) {
1315 if (follow_dotdot_rcu(nd))
1323 static void terminate_walk(struct nameidata *nd)
1325 if (!(nd->flags & LOOKUP_RCU)) {
1326 path_put(&nd->path);
1328 nd->flags &= ~LOOKUP_RCU;
1329 if (!(nd->flags & LOOKUP_ROOT))
1330 nd->root.mnt = NULL;
1332 br_read_unlock(vfsmount_lock);
1336 static inline int walk_component(struct nameidata *nd, struct path *path,
1337 struct qstr *name, int type, int follow)
1339 struct inode *inode;
1342 * "." and ".." are special - ".." especially so because it has
1343 * to be able to know about the current root directory and
1344 * parent relationships.
1346 if (unlikely(type != LAST_NORM))
1347 return handle_dots(nd, type);
1348 err = do_lookup(nd, name, path, &inode);
1349 if (unlikely(err)) {
1354 path_to_nameidata(path, nd);
1358 if (unlikely(inode->i_op->follow_link) && follow) {
1359 if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
1361 BUG_ON(inode != path->dentry->d_inode);
1364 path_to_nameidata(path, nd);
1371 * This is the basic name resolution function, turning a pathname into
1372 * the final dentry. We expect 'base' to be positive and a directory.
1374 * Returns 0 and nd will have valid dentry and mnt on success.
1375 * Returns error and drops reference to input namei data on failure.
1377 static int link_path_walk(const char *name, struct nameidata *nd)
1381 unsigned int lookup_flags = nd->flags;
1389 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
1391 /* At this point we know we have a real path component. */
1398 nd->flags |= LOOKUP_CONTINUE;
1400 err = may_lookup(nd);
1405 c = *(const unsigned char *)name;
1407 hash = init_name_hash();
1410 hash = partial_name_hash(c, hash);
1411 c = *(const unsigned char *)name;
1412 } while (c && (c != '/'));
1413 this.len = name - (const char *) this.name;
1414 this.hash = end_name_hash(hash);
1417 if (this.name[0] == '.') switch (this.len) {
1419 if (this.name[1] == '.') {
1421 nd->flags |= LOOKUP_JUMPED;
1427 if (likely(type == LAST_NORM)) {
1428 struct dentry *parent = nd->path.dentry;
1429 nd->flags &= ~LOOKUP_JUMPED;
1430 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1431 err = parent->d_op->d_hash(parent, nd->inode,
1438 /* remove trailing slashes? */
1440 goto last_component;
1441 while (*++name == '/');
1443 goto last_with_slashes;
1445 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1450 err = do_follow_link(&next, nd);
1455 if (!nd->inode->i_op->lookup)
1458 /* here ends the main loop */
1461 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1463 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1464 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
1465 if (lookup_flags & LOOKUP_PARENT) {
1467 nd->last_type = type;
1470 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1474 err = do_follow_link(&next, nd);
1484 static int path_init(int dfd, const char *name, unsigned int flags,
1485 struct nameidata *nd, struct file **fp)
1491 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1492 nd->flags = flags | LOOKUP_JUMPED;
1494 if (flags & LOOKUP_ROOT) {
1495 struct inode *inode = nd->root.dentry->d_inode;
1497 if (!inode->i_op->lookup)
1499 retval = inode_permission(inode, MAY_EXEC);
1503 nd->path = nd->root;
1505 if (flags & LOOKUP_RCU) {
1506 br_read_lock(vfsmount_lock);
1508 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1510 path_get(&nd->path);
1515 nd->root.mnt = NULL;
1518 if (flags & LOOKUP_RCU) {
1519 br_read_lock(vfsmount_lock);
1524 path_get(&nd->root);
1526 nd->path = nd->root;
1527 } else if (dfd == AT_FDCWD) {
1528 if (flags & LOOKUP_RCU) {
1529 struct fs_struct *fs = current->fs;
1532 br_read_lock(vfsmount_lock);
1536 seq = read_seqcount_begin(&fs->seq);
1538 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1539 } while (read_seqcount_retry(&fs->seq, seq));
1541 get_fs_pwd(current->fs, &nd->path);
1544 struct dentry *dentry;
1546 file = fget_raw_light(dfd, &fput_needed);
1551 dentry = file->f_path.dentry;
1555 if (!S_ISDIR(dentry->d_inode->i_mode))
1558 retval = file_permission(file, MAY_EXEC);
1563 nd->path = file->f_path;
1564 if (flags & LOOKUP_RCU) {
1567 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1568 br_read_lock(vfsmount_lock);
1571 path_get(&file->f_path);
1572 fput_light(file, fput_needed);
1576 nd->inode = nd->path.dentry->d_inode;
1580 fput_light(file, fput_needed);
1585 static inline int lookup_last(struct nameidata *nd, struct path *path)
1587 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1588 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1590 nd->flags &= ~LOOKUP_PARENT;
1591 return walk_component(nd, path, &nd->last, nd->last_type,
1592 nd->flags & LOOKUP_FOLLOW);
1595 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1596 static int path_lookupat(int dfd, const char *name,
1597 unsigned int flags, struct nameidata *nd)
1599 struct file *base = NULL;
1604 * Path walking is largely split up into 2 different synchronisation
1605 * schemes, rcu-walk and ref-walk (explained in
1606 * Documentation/filesystems/path-lookup.txt). These share much of the
1607 * path walk code, but some things particularly setup, cleanup, and
1608 * following mounts are sufficiently divergent that functions are
1609 * duplicated. Typically there is a function foo(), and its RCU
1610 * analogue, foo_rcu().
1612 * -ECHILD is the error number of choice (just to avoid clashes) that
1613 * is returned if some aspect of an rcu-walk fails. Such an error must
1614 * be handled by restarting a traditional ref-walk (which will always
1615 * be able to complete).
1617 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1622 current->total_link_count = 0;
1623 err = link_path_walk(name, nd);
1625 if (!err && !(flags & LOOKUP_PARENT)) {
1627 err = lookup_last(nd, &path);
1630 struct path link = path;
1631 struct inode *inode = link.dentry->d_inode;
1634 path_put_conditional(&path, nd);
1635 path_put(&nd->path);
1640 nd->flags |= LOOKUP_PARENT;
1641 err = __do_follow_link(&link, nd, &cookie);
1643 err = lookup_last(nd, &path);
1644 if (!IS_ERR(cookie) && inode->i_op->put_link)
1645 inode->i_op->put_link(link.dentry, nd, cookie);
1650 if (nd->flags & LOOKUP_RCU) {
1651 /* went all way through without dropping RCU */
1653 if (nameidata_drop_rcu_last(nd))
1658 err = handle_reval_path(nd);
1660 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1661 if (!nd->inode->i_op->lookup) {
1662 path_put(&nd->path);
1670 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1671 path_put(&nd->root);
1672 nd->root.mnt = NULL;
1677 static int do_path_lookup(int dfd, const char *name,
1678 unsigned int flags, struct nameidata *nd)
1680 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1681 if (unlikely(retval == -ECHILD))
1682 retval = path_lookupat(dfd, name, flags, nd);
1683 if (unlikely(retval == -ESTALE))
1684 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1686 if (likely(!retval)) {
1687 if (unlikely(!audit_dummy_context())) {
1688 if (nd->path.dentry && nd->inode)
1689 audit_inode(name, nd->path.dentry);
1695 int kern_path_parent(const char *name, struct nameidata *nd)
1697 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
1700 int kern_path(const char *name, unsigned int flags, struct path *path)
1702 struct nameidata nd;
1703 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1710 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1711 * @dentry: pointer to dentry of the base directory
1712 * @mnt: pointer to vfs mount of the base directory
1713 * @name: pointer to file name
1714 * @flags: lookup flags
1715 * @nd: pointer to nameidata
1717 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1718 const char *name, unsigned int flags,
1719 struct nameidata *nd)
1721 nd->root.dentry = dentry;
1723 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1724 return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
1727 static struct dentry *__lookup_hash(struct qstr *name,
1728 struct dentry *base, struct nameidata *nd)
1730 struct inode *inode = base->d_inode;
1731 struct dentry *dentry;
1734 err = exec_permission(inode, 0);
1736 return ERR_PTR(err);
1739 * Don't bother with __d_lookup: callers are for creat as
1740 * well as unlink, so a lot of the time it would cost
1743 dentry = d_lookup(base, name);
1745 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
1746 dentry = do_revalidate(dentry, nd);
1749 dentry = d_alloc_and_lookup(base, name, nd);
1755 * Restricted form of lookup. Doesn't follow links, single-component only,
1756 * needs parent already locked. Doesn't follow mounts.
1759 static struct dentry *lookup_hash(struct nameidata *nd)
1761 return __lookup_hash(&nd->last, nd->path.dentry, nd);
1765 * lookup_one_len - filesystem helper to lookup single pathname component
1766 * @name: pathname component to lookup
1767 * @base: base directory to lookup from
1768 * @len: maximum length @len should be interpreted to
1770 * Note that this routine is purely a helper for filesystem usage and should
1771 * not be called by generic code. Also note that by using this function the
1772 * nameidata argument is passed to the filesystem methods and a filesystem
1773 * using this helper needs to be prepared for that.
1775 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1781 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1786 return ERR_PTR(-EACCES);
1788 hash = init_name_hash();
1790 c = *(const unsigned char *)name++;
1791 if (c == '/' || c == '\0')
1792 return ERR_PTR(-EACCES);
1793 hash = partial_name_hash(c, hash);
1795 this.hash = end_name_hash(hash);
1797 * See if the low-level filesystem might want
1798 * to use its own hash..
1800 if (base->d_flags & DCACHE_OP_HASH) {
1801 int err = base->d_op->d_hash(base, base->d_inode, &this);
1803 return ERR_PTR(err);
1806 return __lookup_hash(&this, base, NULL);
1809 int user_path_at(int dfd, const char __user *name, unsigned flags,
1812 struct nameidata nd;
1813 char *tmp = getname_flags(name, flags);
1814 int err = PTR_ERR(tmp);
1817 BUG_ON(flags & LOOKUP_PARENT);
1819 err = do_path_lookup(dfd, tmp, flags, &nd);
1827 static int user_path_parent(int dfd, const char __user *path,
1828 struct nameidata *nd, char **name)
1830 char *s = getname(path);
1836 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1846 * It's inline, so penalty for filesystems that don't use sticky bit is
1849 static inline int check_sticky(struct inode *dir, struct inode *inode)
1851 uid_t fsuid = current_fsuid();
1853 if (!(dir->i_mode & S_ISVTX))
1855 if (inode->i_uid == fsuid)
1857 if (dir->i_uid == fsuid)
1859 return !capable(CAP_FOWNER);
1863 * Check whether we can remove a link victim from directory dir, check
1864 * whether the type of victim is right.
1865 * 1. We can't do it if dir is read-only (done in permission())
1866 * 2. We should have write and exec permissions on dir
1867 * 3. We can't remove anything from append-only dir
1868 * 4. We can't do anything with immutable dir (done in permission())
1869 * 5. If the sticky bit on dir is set we should either
1870 * a. be owner of dir, or
1871 * b. be owner of victim, or
1872 * c. have CAP_FOWNER capability
1873 * 6. If the victim is append-only or immutable we can't do antyhing with
1874 * links pointing to it.
1875 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1876 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1877 * 9. We can't remove a root or mountpoint.
1878 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1879 * nfs_async_unlink().
1881 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1885 if (!victim->d_inode)
1888 BUG_ON(victim->d_parent->d_inode != dir);
1889 audit_inode_child(victim, dir);
1891 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1896 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1897 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1900 if (!S_ISDIR(victim->d_inode->i_mode))
1902 if (IS_ROOT(victim))
1904 } else if (S_ISDIR(victim->d_inode->i_mode))
1906 if (IS_DEADDIR(dir))
1908 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1913 /* Check whether we can create an object with dentry child in directory
1915 * 1. We can't do it if child already exists (open has special treatment for
1916 * this case, but since we are inlined it's OK)
1917 * 2. We can't do it if dir is read-only (done in permission())
1918 * 3. We should have write and exec permissions on dir
1919 * 4. We can't do it if dir is immutable (done in permission())
1921 static inline int may_create(struct inode *dir, struct dentry *child)
1925 if (IS_DEADDIR(dir))
1927 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1931 * p1 and p2 should be directories on the same fs.
1933 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1938 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1942 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1944 p = d_ancestor(p2, p1);
1946 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1947 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1951 p = d_ancestor(p1, p2);
1953 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1954 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1958 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1959 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1963 void unlock_rename(struct dentry *p1, struct dentry *p2)
1965 mutex_unlock(&p1->d_inode->i_mutex);
1967 mutex_unlock(&p2->d_inode->i_mutex);
1968 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1972 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1973 struct nameidata *nd)
1975 int error = may_create(dir, dentry);
1980 if (!dir->i_op->create)
1981 return -EACCES; /* shouldn't it be ENOSYS? */
1984 error = security_inode_create(dir, dentry, mode);
1987 error = dir->i_op->create(dir, dentry, mode, nd);
1989 fsnotify_create(dir, dentry);
1993 static int may_open(struct path *path, int acc_mode, int flag)
1995 struct dentry *dentry = path->dentry;
1996 struct inode *inode = dentry->d_inode;
2006 switch (inode->i_mode & S_IFMT) {
2010 if (acc_mode & MAY_WRITE)
2015 if (path->mnt->mnt_flags & MNT_NODEV)
2024 error = inode_permission(inode, acc_mode);
2029 * An append-only file must be opened in append mode for writing.
2031 if (IS_APPEND(inode)) {
2032 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2038 /* O_NOATIME can only be set by the owner or superuser */
2039 if (flag & O_NOATIME && !is_owner_or_cap(inode))
2043 * Ensure there are no outstanding leases on the file.
2045 return break_lease(inode, flag);
2048 static int handle_truncate(struct file *filp)
2050 struct path *path = &filp->f_path;
2051 struct inode *inode = path->dentry->d_inode;
2052 int error = get_write_access(inode);
2056 * Refuse to truncate files with mandatory locks held on them.
2058 error = locks_verify_locked(inode);
2060 error = security_path_truncate(path);
2062 error = do_truncate(path->dentry, 0,
2063 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2066 put_write_access(inode);
2071 * Note that while the flag value (low two bits) for sys_open means:
2076 * it is changed into
2077 * 00 - no permissions needed
2078 * 01 - read-permission
2079 * 10 - write-permission
2081 * for the internal routines (ie open_namei()/follow_link() etc)
2082 * This is more logical, and also allows the 00 "no perm needed"
2083 * to be used for symlinks (where the permissions are checked
2087 static inline int open_to_namei_flags(int flag)
2089 if ((flag+1) & O_ACCMODE)
2095 * Handle the last step of open()
2097 static struct file *do_last(struct nameidata *nd, struct path *path,
2098 const struct open_flags *op, const char *pathname)
2100 struct dentry *dir = nd->path.dentry;
2101 struct dentry *dentry;
2102 int open_flag = op->open_flag;
2103 int will_truncate = open_flag & O_TRUNC;
2105 int acc_mode = op->acc_mode;
2109 nd->flags &= ~LOOKUP_PARENT;
2110 nd->flags |= op->intent;
2112 switch (nd->last_type) {
2115 error = handle_dots(nd, nd->last_type);
2117 return ERR_PTR(error);
2120 if (nd->flags & LOOKUP_RCU) {
2121 if (nameidata_drop_rcu_last(nd))
2122 return ERR_PTR(-ECHILD);
2124 error = handle_reval_path(nd);
2127 audit_inode(pathname, nd->path.dentry);
2128 if (open_flag & O_CREAT) {
2134 /* can't be RCU mode here */
2135 error = handle_reval_path(nd);
2138 audit_inode(pathname, dir);
2142 if (!(open_flag & O_CREAT)) {
2144 if (nd->last.name[nd->last.len])
2145 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2146 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2148 /* we _can_ be in RCU mode here */
2149 error = walk_component(nd, path, &nd->last, LAST_NORM,
2152 return ERR_PTR(error);
2153 if (error) /* symlink */
2156 if (nd->flags & LOOKUP_RCU) {
2157 if (nameidata_drop_rcu_last(nd))
2158 return ERR_PTR(-ECHILD);
2162 if (nd->flags & LOOKUP_DIRECTORY) {
2163 if (!nd->inode->i_op->lookup)
2166 audit_inode(pathname, nd->path.dentry);
2170 /* create side of things */
2172 if (nd->flags & LOOKUP_RCU) {
2173 if (nameidata_drop_rcu_last(nd))
2174 return ERR_PTR(-ECHILD);
2177 audit_inode(pathname, dir);
2179 /* trailing slashes? */
2180 if (nd->last.name[nd->last.len])
2183 mutex_lock(&dir->d_inode->i_mutex);
2185 dentry = lookup_hash(nd);
2186 error = PTR_ERR(dentry);
2187 if (IS_ERR(dentry)) {
2188 mutex_unlock(&dir->d_inode->i_mutex);
2192 path->dentry = dentry;
2193 path->mnt = nd->path.mnt;
2195 /* Negative dentry, just create the file */
2196 if (!dentry->d_inode) {
2197 int mode = op->mode;
2198 if (!IS_POSIXACL(dir->d_inode))
2199 mode &= ~current_umask();
2201 * This write is needed to ensure that a
2202 * rw->ro transition does not occur between
2203 * the time when the file is created and when
2204 * a permanent write count is taken through
2205 * the 'struct file' in nameidata_to_filp().
2207 error = mnt_want_write(nd->path.mnt);
2209 goto exit_mutex_unlock;
2211 /* Don't check for write permission, don't truncate */
2212 open_flag &= ~O_TRUNC;
2214 acc_mode = MAY_OPEN;
2215 error = security_path_mknod(&nd->path, dentry, mode, 0);
2217 goto exit_mutex_unlock;
2218 error = vfs_create(dir->d_inode, dentry, mode, nd);
2220 goto exit_mutex_unlock;
2221 mutex_unlock(&dir->d_inode->i_mutex);
2222 dput(nd->path.dentry);
2223 nd->path.dentry = dentry;
2228 * It already exists.
2230 mutex_unlock(&dir->d_inode->i_mutex);
2231 audit_inode(pathname, path->dentry);
2234 if (open_flag & O_EXCL)
2237 error = follow_managed(path, nd->flags);
2242 if (!path->dentry->d_inode)
2245 if (path->dentry->d_inode->i_op->follow_link)
2248 path_to_nameidata(path, nd);
2249 nd->inode = path->dentry->d_inode;
2251 if (S_ISDIR(nd->inode->i_mode))
2254 if (!S_ISREG(nd->inode->i_mode))
2257 if (will_truncate) {
2258 error = mnt_want_write(nd->path.mnt);
2264 error = may_open(&nd->path, acc_mode, open_flag);
2267 filp = nameidata_to_filp(nd);
2268 if (!IS_ERR(filp)) {
2269 error = ima_file_check(filp, op->acc_mode);
2272 filp = ERR_PTR(error);
2275 if (!IS_ERR(filp)) {
2276 if (will_truncate) {
2277 error = handle_truncate(filp);
2280 filp = ERR_PTR(error);
2286 mnt_drop_write(nd->path.mnt);
2287 path_put(&nd->path);
2291 mutex_unlock(&dir->d_inode->i_mutex);
2293 path_put_conditional(path, nd);
2295 filp = ERR_PTR(error);
2299 static struct file *path_openat(int dfd, const char *pathname,
2300 struct nameidata *nd, const struct open_flags *op, int flags)
2302 struct file *base = NULL;
2308 filp = get_empty_filp();
2310 return ERR_PTR(-ENFILE);
2312 filp->f_flags = op->open_flag;
2313 nd->intent.open.file = filp;
2314 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2315 nd->intent.open.create_mode = op->mode;
2317 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
2318 if (unlikely(error))
2321 current->total_link_count = 0;
2322 error = link_path_walk(pathname, nd);
2323 if (unlikely(error))
2326 filp = do_last(nd, &path, op, pathname);
2327 while (unlikely(!filp)) { /* trailing symlink */
2328 struct path link = path;
2329 struct inode *linki = link.dentry->d_inode;
2331 if (!(nd->flags & LOOKUP_FOLLOW) || count++ == 32) {
2332 path_put_conditional(&path, nd);
2333 path_put(&nd->path);
2334 filp = ERR_PTR(-ELOOP);
2338 * This is subtle. Instead of calling do_follow_link() we do
2339 * the thing by hands. The reason is that this way we have zero
2340 * link_count and path_walk() (called from ->follow_link)
2341 * honoring LOOKUP_PARENT. After that we have the parent and
2342 * last component, i.e. we are in the same situation as after
2343 * the first path_walk(). Well, almost - if the last component
2344 * is normal we get its copy stored in nd->last.name and we will
2345 * have to putname() it when we are done. Procfs-like symlinks
2346 * just set LAST_BIND.
2348 nd->flags |= LOOKUP_PARENT;
2349 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2350 error = __do_follow_link(&link, nd, &cookie);
2351 if (unlikely(error))
2352 filp = ERR_PTR(error);
2354 filp = do_last(nd, &path, op, pathname);
2355 if (!IS_ERR(cookie) && linki->i_op->put_link)
2356 linki->i_op->put_link(link.dentry, nd, cookie);
2360 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2361 path_put(&nd->root);
2364 release_open_intent(nd);
2368 filp = ERR_PTR(error);
2372 struct file *do_filp_open(int dfd, const char *pathname,
2373 const struct open_flags *op, int flags)
2375 struct nameidata nd;
2378 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
2379 if (unlikely(filp == ERR_PTR(-ECHILD)))
2380 filp = path_openat(dfd, pathname, &nd, op, flags);
2381 if (unlikely(filp == ERR_PTR(-ESTALE)))
2382 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
2386 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2387 const char *name, const struct open_flags *op, int flags)
2389 struct nameidata nd;
2393 nd.root.dentry = dentry;
2395 flags |= LOOKUP_ROOT;
2397 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
2398 return ERR_PTR(-ELOOP);
2400 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2401 if (unlikely(file == ERR_PTR(-ECHILD)))
2402 file = path_openat(-1, name, &nd, op, flags);
2403 if (unlikely(file == ERR_PTR(-ESTALE)))
2404 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2409 * lookup_create - lookup a dentry, creating it if it doesn't exist
2410 * @nd: nameidata info
2411 * @is_dir: directory flag
2413 * Simple function to lookup and return a dentry and create it
2414 * if it doesn't exist. Is SMP-safe.
2416 * Returns with nd->path.dentry->d_inode->i_mutex locked.
2418 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2420 struct dentry *dentry = ERR_PTR(-EEXIST);
2422 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2424 * Yucky last component or no last component at all?
2425 * (foo/., foo/.., /////)
2427 if (nd->last_type != LAST_NORM)
2429 nd->flags &= ~LOOKUP_PARENT;
2430 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2431 nd->intent.open.flags = O_EXCL;
2434 * Do the final lookup.
2436 dentry = lookup_hash(nd);
2440 if (dentry->d_inode)
2443 * Special case - lookup gave negative, but... we had foo/bar/
2444 * From the vfs_mknod() POV we just have a negative dentry -
2445 * all is fine. Let's be bastards - you had / on the end, you've
2446 * been asking for (non-existent) directory. -ENOENT for you.
2448 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2450 dentry = ERR_PTR(-ENOENT);
2455 dentry = ERR_PTR(-EEXIST);
2459 EXPORT_SYMBOL_GPL(lookup_create);
2461 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2463 int error = may_create(dir, dentry);
2468 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
2471 if (!dir->i_op->mknod)
2474 error = devcgroup_inode_mknod(mode, dev);
2478 error = security_inode_mknod(dir, dentry, mode, dev);
2482 error = dir->i_op->mknod(dir, dentry, mode, dev);
2484 fsnotify_create(dir, dentry);
2488 static int may_mknod(mode_t mode)
2490 switch (mode & S_IFMT) {
2496 case 0: /* zero mode translates to S_IFREG */
2505 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2510 struct dentry *dentry;
2511 struct nameidata nd;
2516 error = user_path_parent(dfd, filename, &nd, &tmp);
2520 dentry = lookup_create(&nd, 0);
2521 if (IS_ERR(dentry)) {
2522 error = PTR_ERR(dentry);
2525 if (!IS_POSIXACL(nd.path.dentry->d_inode))
2526 mode &= ~current_umask();
2527 error = may_mknod(mode);
2530 error = mnt_want_write(nd.path.mnt);
2533 error = security_path_mknod(&nd.path, dentry, mode, dev);
2535 goto out_drop_write;
2536 switch (mode & S_IFMT) {
2537 case 0: case S_IFREG:
2538 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
2540 case S_IFCHR: case S_IFBLK:
2541 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
2542 new_decode_dev(dev));
2544 case S_IFIFO: case S_IFSOCK:
2545 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
2549 mnt_drop_write(nd.path.mnt);
2553 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2560 SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
2562 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2565 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2567 int error = may_create(dir, dentry);
2572 if (!dir->i_op->mkdir)
2575 mode &= (S_IRWXUGO|S_ISVTX);
2576 error = security_inode_mkdir(dir, dentry, mode);
2580 error = dir->i_op->mkdir(dir, dentry, mode);
2582 fsnotify_mkdir(dir, dentry);
2586 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
2590 struct dentry *dentry;
2591 struct nameidata nd;
2593 error = user_path_parent(dfd, pathname, &nd, &tmp);
2597 dentry = lookup_create(&nd, 1);
2598 error = PTR_ERR(dentry);
2602 if (!IS_POSIXACL(nd.path.dentry->d_inode))
2603 mode &= ~current_umask();
2604 error = mnt_want_write(nd.path.mnt);
2607 error = security_path_mkdir(&nd.path, dentry, mode);
2609 goto out_drop_write;
2610 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2612 mnt_drop_write(nd.path.mnt);
2616 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2623 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
2625 return sys_mkdirat(AT_FDCWD, pathname, mode);
2629 * We try to drop the dentry early: we should have
2630 * a usage count of 2 if we're the only user of this
2631 * dentry, and if that is true (possibly after pruning
2632 * the dcache), then we drop the dentry now.
2634 * A low-level filesystem can, if it choses, legally
2637 * if (!d_unhashed(dentry))
2640 * if it cannot handle the case of removing a directory
2641 * that is still in use by something else..
2643 void dentry_unhash(struct dentry *dentry)
2646 shrink_dcache_parent(dentry);
2647 spin_lock(&dentry->d_lock);
2648 if (dentry->d_count == 2)
2650 spin_unlock(&dentry->d_lock);
2653 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2655 int error = may_delete(dir, dentry, 1);
2660 if (!dir->i_op->rmdir)
2663 mutex_lock(&dentry->d_inode->i_mutex);
2664 dentry_unhash(dentry);
2665 if (d_mountpoint(dentry))
2668 error = security_inode_rmdir(dir, dentry);
2670 error = dir->i_op->rmdir(dir, dentry);
2672 dentry->d_inode->i_flags |= S_DEAD;
2677 mutex_unlock(&dentry->d_inode->i_mutex);
2686 static long do_rmdir(int dfd, const char __user *pathname)
2690 struct dentry *dentry;
2691 struct nameidata nd;
2693 error = user_path_parent(dfd, pathname, &nd, &name);
2697 switch(nd.last_type) {
2709 nd.flags &= ~LOOKUP_PARENT;
2711 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2712 dentry = lookup_hash(&nd);
2713 error = PTR_ERR(dentry);
2716 error = mnt_want_write(nd.path.mnt);
2719 error = security_path_rmdir(&nd.path, dentry);
2722 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2724 mnt_drop_write(nd.path.mnt);
2728 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2735 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
2737 return do_rmdir(AT_FDCWD, pathname);
2740 int vfs_unlink(struct inode *dir, struct dentry *dentry)
2742 int error = may_delete(dir, dentry, 0);
2747 if (!dir->i_op->unlink)
2750 mutex_lock(&dentry->d_inode->i_mutex);
2751 if (d_mountpoint(dentry))
2754 error = security_inode_unlink(dir, dentry);
2756 error = dir->i_op->unlink(dir, dentry);
2761 mutex_unlock(&dentry->d_inode->i_mutex);
2763 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2764 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2765 fsnotify_link_count(dentry->d_inode);
2773 * Make sure that the actual truncation of the file will occur outside its
2774 * directory's i_mutex. Truncate can take a long time if there is a lot of
2775 * writeout happening, and we don't want to prevent access to the directory
2776 * while waiting on the I/O.
2778 static long do_unlinkat(int dfd, const char __user *pathname)
2782 struct dentry *dentry;
2783 struct nameidata nd;
2784 struct inode *inode = NULL;
2786 error = user_path_parent(dfd, pathname, &nd, &name);
2791 if (nd.last_type != LAST_NORM)
2794 nd.flags &= ~LOOKUP_PARENT;
2796 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2797 dentry = lookup_hash(&nd);
2798 error = PTR_ERR(dentry);
2799 if (!IS_ERR(dentry)) {
2800 /* Why not before? Because we want correct error value */
2801 if (nd.last.name[nd.last.len])
2803 inode = dentry->d_inode;
2806 error = mnt_want_write(nd.path.mnt);
2809 error = security_path_unlink(&nd.path, dentry);
2812 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2814 mnt_drop_write(nd.path.mnt);
2818 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2820 iput(inode); /* truncate the inode here */
2827 error = !dentry->d_inode ? -ENOENT :
2828 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2832 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
2834 if ((flag & ~AT_REMOVEDIR) != 0)
2837 if (flag & AT_REMOVEDIR)
2838 return do_rmdir(dfd, pathname);
2840 return do_unlinkat(dfd, pathname);
2843 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
2845 return do_unlinkat(AT_FDCWD, pathname);
2848 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
2850 int error = may_create(dir, dentry);
2855 if (!dir->i_op->symlink)
2858 error = security_inode_symlink(dir, dentry, oldname);
2862 error = dir->i_op->symlink(dir, dentry, oldname);
2864 fsnotify_create(dir, dentry);
2868 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2869 int, newdfd, const char __user *, newname)
2874 struct dentry *dentry;
2875 struct nameidata nd;
2877 from = getname(oldname);
2879 return PTR_ERR(from);
2881 error = user_path_parent(newdfd, newname, &nd, &to);
2885 dentry = lookup_create(&nd, 0);
2886 error = PTR_ERR(dentry);
2890 error = mnt_want_write(nd.path.mnt);
2893 error = security_path_symlink(&nd.path, dentry, from);
2895 goto out_drop_write;
2896 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2898 mnt_drop_write(nd.path.mnt);
2902 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2910 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
2912 return sys_symlinkat(oldname, AT_FDCWD, newname);
2915 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2917 struct inode *inode = old_dentry->d_inode;
2923 error = may_create(dir, new_dentry);
2927 if (dir->i_sb != inode->i_sb)
2931 * A link to an append-only or immutable file cannot be created.
2933 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2935 if (!dir->i_op->link)
2937 if (S_ISDIR(inode->i_mode))
2940 error = security_inode_link(old_dentry, dir, new_dentry);
2944 mutex_lock(&inode->i_mutex);
2945 /* Make sure we don't allow creating hardlink to an unlinked file */
2946 if (inode->i_nlink == 0)
2949 error = dir->i_op->link(old_dentry, dir, new_dentry);
2950 mutex_unlock(&inode->i_mutex);
2952 fsnotify_link(dir, inode, new_dentry);
2957 * Hardlinks are often used in delicate situations. We avoid
2958 * security-related surprises by not following symlinks on the
2961 * We don't follow them on the oldname either to be compatible
2962 * with linux 2.0, and to avoid hard-linking to directories
2963 * and other special files. --ADM
2965 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2966 int, newdfd, const char __user *, newname, int, flags)
2968 struct dentry *new_dentry;
2969 struct nameidata nd;
2970 struct path old_path;
2975 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2978 * To use null names we require CAP_DAC_READ_SEARCH
2979 * This ensures that not everyone will be able to create
2980 * handlink using the passed filedescriptor.
2982 if (flags & AT_EMPTY_PATH) {
2983 if (!capable(CAP_DAC_READ_SEARCH))
2988 if (flags & AT_SYMLINK_FOLLOW)
2989 how |= LOOKUP_FOLLOW;
2991 error = user_path_at(olddfd, oldname, how, &old_path);
2995 error = user_path_parent(newdfd, newname, &nd, &to);
2999 if (old_path.mnt != nd.path.mnt)
3001 new_dentry = lookup_create(&nd, 0);
3002 error = PTR_ERR(new_dentry);
3003 if (IS_ERR(new_dentry))
3005 error = mnt_want_write(nd.path.mnt);
3008 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
3010 goto out_drop_write;
3011 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
3013 mnt_drop_write(nd.path.mnt);
3017 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3022 path_put(&old_path);
3027 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
3029 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
3033 * The worst of all namespace operations - renaming directory. "Perverted"
3034 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3036 * a) we can get into loop creation. Check is done in is_subdir().
3037 * b) race potential - two innocent renames can create a loop together.
3038 * That's where 4.4 screws up. Current fix: serialization on
3039 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3041 * c) we have to lock _three_ objects - parents and victim (if it exists).
3042 * And that - after we got ->i_mutex on parents (until then we don't know
3043 * whether the target exists). Solution: try to be smart with locking
3044 * order for inodes. We rely on the fact that tree topology may change
3045 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
3046 * move will be locked. Thus we can rank directories by the tree
3047 * (ancestors first) and rank all non-directories after them.
3048 * That works since everybody except rename does "lock parent, lookup,
3049 * lock child" and rename is under ->s_vfs_rename_mutex.
3050 * HOWEVER, it relies on the assumption that any object with ->lookup()
3051 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3052 * we'd better make sure that there's no link(2) for them.
3053 * d) some filesystems don't support opened-but-unlinked directories,
3054 * either because of layout or because they are not ready to deal with
3055 * all cases correctly. The latter will be fixed (taking this sort of
3056 * stuff into VFS), but the former is not going away. Solution: the same
3057 * trick as in rmdir().
3058 * e) conversion from fhandle to dentry may come in the wrong moment - when
3059 * we are removing the target. Solution: we will have to grab ->i_mutex
3060 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3061 * ->i_mutex on parents, which works but leads to some truly excessive
3064 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3065 struct inode *new_dir, struct dentry *new_dentry)
3068 struct inode *target;
3071 * If we are going to change the parent - check write permissions,
3072 * we'll need to flip '..'.
3074 if (new_dir != old_dir) {
3075 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
3080 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3084 target = new_dentry->d_inode;
3086 mutex_lock(&target->i_mutex);
3087 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3091 dentry_unhash(new_dentry);
3092 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3096 target->i_flags |= S_DEAD;
3097 dont_mount(new_dentry);
3099 mutex_unlock(&target->i_mutex);
3100 if (d_unhashed(new_dentry))
3101 d_rehash(new_dentry);
3105 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3106 d_move(old_dentry,new_dentry);
3110 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3111 struct inode *new_dir, struct dentry *new_dentry)
3113 struct inode *target;
3116 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3121 target = new_dentry->d_inode;
3123 mutex_lock(&target->i_mutex);
3124 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3127 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3130 dont_mount(new_dentry);
3131 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3132 d_move(old_dentry, new_dentry);
3135 mutex_unlock(&target->i_mutex);
3140 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3141 struct inode *new_dir, struct dentry *new_dentry)
3144 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
3145 const unsigned char *old_name;
3147 if (old_dentry->d_inode == new_dentry->d_inode)
3150 error = may_delete(old_dir, old_dentry, is_dir);
3154 if (!new_dentry->d_inode)
3155 error = may_create(new_dir, new_dentry);
3157 error = may_delete(new_dir, new_dentry, is_dir);
3161 if (!old_dir->i_op->rename)
3164 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3167 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3169 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3171 fsnotify_move(old_dir, new_dir, old_name, is_dir,
3172 new_dentry->d_inode, old_dentry);
3173 fsnotify_oldname_free(old_name);
3178 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3179 int, newdfd, const char __user *, newname)
3181 struct dentry *old_dir, *new_dir;
3182 struct dentry *old_dentry, *new_dentry;
3183 struct dentry *trap;
3184 struct nameidata oldnd, newnd;
3189 error = user_path_parent(olddfd, oldname, &oldnd, &from);
3193 error = user_path_parent(newdfd, newname, &newnd, &to);
3198 if (oldnd.path.mnt != newnd.path.mnt)
3201 old_dir = oldnd.path.dentry;
3203 if (oldnd.last_type != LAST_NORM)
3206 new_dir = newnd.path.dentry;
3207 if (newnd.last_type != LAST_NORM)
3210 oldnd.flags &= ~LOOKUP_PARENT;
3211 newnd.flags &= ~LOOKUP_PARENT;
3212 newnd.flags |= LOOKUP_RENAME_TARGET;
3214 trap = lock_rename(new_dir, old_dir);
3216 old_dentry = lookup_hash(&oldnd);
3217 error = PTR_ERR(old_dentry);
3218 if (IS_ERR(old_dentry))
3220 /* source must exist */
3222 if (!old_dentry->d_inode)
3224 /* unless the source is a directory trailing slashes give -ENOTDIR */
3225 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3227 if (oldnd.last.name[oldnd.last.len])
3229 if (newnd.last.name[newnd.last.len])
3232 /* source should not be ancestor of target */
3234 if (old_dentry == trap)
3236 new_dentry = lookup_hash(&newnd);
3237 error = PTR_ERR(new_dentry);
3238 if (IS_ERR(new_dentry))
3240 /* target should not be an ancestor of source */
3242 if (new_dentry == trap)
3245 error = mnt_want_write(oldnd.path.mnt);
3248 error = security_path_rename(&oldnd.path, old_dentry,
3249 &newnd.path, new_dentry);
3252 error = vfs_rename(old_dir->d_inode, old_dentry,
3253 new_dir->d_inode, new_dentry);
3255 mnt_drop_write(oldnd.path.mnt);
3261 unlock_rename(new_dir, old_dir);
3263 path_put(&newnd.path);
3266 path_put(&oldnd.path);
3272 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
3274 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3277 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3281 len = PTR_ERR(link);
3286 if (len > (unsigned) buflen)
3288 if (copy_to_user(buffer, link, len))
3295 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3296 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3297 * using) it for any given inode is up to filesystem.
3299 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3301 struct nameidata nd;
3306 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3308 return PTR_ERR(cookie);
3310 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3311 if (dentry->d_inode->i_op->put_link)
3312 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3316 int vfs_follow_link(struct nameidata *nd, const char *link)
3318 return __vfs_follow_link(nd, link);
3321 /* get the link contents into pagecache */
3322 static char *page_getlink(struct dentry * dentry, struct page **ppage)
3326 struct address_space *mapping = dentry->d_inode->i_mapping;
3327 page = read_mapping_page(mapping, 0, NULL);
3332 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3336 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3338 struct page *page = NULL;
3339 char *s = page_getlink(dentry, &page);
3340 int res = vfs_readlink(dentry,buffer,buflen,s);
3343 page_cache_release(page);
3348 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
3350 struct page *page = NULL;
3351 nd_set_link(nd, page_getlink(dentry, &page));
3355 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
3357 struct page *page = cookie;
3361 page_cache_release(page);
3366 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3368 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
3370 struct address_space *mapping = inode->i_mapping;
3375 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3377 flags |= AOP_FLAG_NOFS;
3380 err = pagecache_write_begin(NULL, mapping, 0, len-1,
3381 flags, &page, &fsdata);
3385 kaddr = kmap_atomic(page, KM_USER0);
3386 memcpy(kaddr, symname, len-1);
3387 kunmap_atomic(kaddr, KM_USER0);
3389 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3396 mark_inode_dirty(inode);
3402 int page_symlink(struct inode *inode, const char *symname, int len)
3404 return __page_symlink(inode, symname, len,
3405 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
3408 const struct inode_operations page_symlink_inode_operations = {
3409 .readlink = generic_readlink,
3410 .follow_link = page_follow_link_light,
3411 .put_link = page_put_link,
3414 EXPORT_SYMBOL(user_path_at);
3415 EXPORT_SYMBOL(follow_down_one);
3416 EXPORT_SYMBOL(follow_down);
3417 EXPORT_SYMBOL(follow_up);
3418 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3419 EXPORT_SYMBOL(getname);
3420 EXPORT_SYMBOL(lock_rename);
3421 EXPORT_SYMBOL(lookup_one_len);
3422 EXPORT_SYMBOL(page_follow_link_light);
3423 EXPORT_SYMBOL(page_put_link);
3424 EXPORT_SYMBOL(page_readlink);
3425 EXPORT_SYMBOL(__page_symlink);
3426 EXPORT_SYMBOL(page_symlink);
3427 EXPORT_SYMBOL(page_symlink_inode_operations);
3428 EXPORT_SYMBOL(kern_path_parent);
3429 EXPORT_SYMBOL(kern_path);
3430 EXPORT_SYMBOL(vfs_path_lookup);
3431 EXPORT_SYMBOL(inode_permission);
3432 EXPORT_SYMBOL(file_permission);
3433 EXPORT_SYMBOL(unlock_rename);
3434 EXPORT_SYMBOL(vfs_create);
3435 EXPORT_SYMBOL(vfs_follow_link);
3436 EXPORT_SYMBOL(vfs_link);
3437 EXPORT_SYMBOL(vfs_mkdir);
3438 EXPORT_SYMBOL(vfs_mknod);
3439 EXPORT_SYMBOL(generic_permission);
3440 EXPORT_SYMBOL(vfs_readlink);
3441 EXPORT_SYMBOL(vfs_rename);
3442 EXPORT_SYMBOL(vfs_rmdir);
3443 EXPORT_SYMBOL(vfs_symlink);
3444 EXPORT_SYMBOL(vfs_unlink);
3445 EXPORT_SYMBOL(dentry_unhash);
3446 EXPORT_SYMBOL(generic_readlink);