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-existent 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 unsigned int mode = inode->i_mode;
184 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
186 if (current_user_ns() != inode_userns(inode))
189 if (current_fsuid() == inode->i_uid)
192 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
193 int error = check_acl(inode, mask, flags);
194 if (error != -EAGAIN)
198 if (in_group_p(inode->i_gid))
204 * If the DACs are ok we don't need any capability check.
206 if ((mask & ~mode) == 0)
212 * generic_permission - check for access rights on a Posix-like filesystem
213 * @inode: inode to check access rights for
214 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
215 * @check_acl: optional callback to check for Posix ACLs
216 * @flags: IPERM_FLAG_ flags.
218 * Used to check for read/write/execute permissions on a file.
219 * We use "fsuid" for this, letting us set arbitrary permissions
220 * for filesystem access without changing the "normal" uids which
221 * are used for other things.
223 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
224 * request cannot be satisfied (eg. requires blocking or too much complexity).
225 * It would then be called again in ref-walk mode.
227 int generic_permission(struct inode *inode, int mask, unsigned int flags,
228 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
233 * Do the basic POSIX ACL permission checks.
235 ret = acl_permission_check(inode, mask, flags, check_acl);
240 * Read/write DACs are always overridable.
241 * Executable DACs are overridable for all directories and
242 * for non-directories that have least one exec bit set.
244 if (!(mask & MAY_EXEC) || execute_ok(inode))
245 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
249 * Searching includes executable on directories, else just read.
251 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
252 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
253 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
260 * inode_permission - check for access rights to a given inode
261 * @inode: inode to check permission on
262 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
264 * Used to check for read/write/execute permissions on an inode.
265 * We use "fsuid" for this, letting us set arbitrary permissions
266 * for filesystem access without changing the "normal" uids which
267 * are used for other things.
269 int inode_permission(struct inode *inode, int mask)
273 if (mask & MAY_WRITE) {
274 umode_t mode = inode->i_mode;
277 * Nobody gets write access to a read-only fs.
279 if (IS_RDONLY(inode) &&
280 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
284 * Nobody gets write access to an immutable file.
286 if (IS_IMMUTABLE(inode))
290 if (inode->i_op->permission)
291 retval = inode->i_op->permission(inode, mask, 0);
293 retval = generic_permission(inode, mask, 0,
294 inode->i_op->check_acl);
299 retval = devcgroup_inode_permission(inode, mask);
303 return security_inode_permission(inode, mask);
307 * file_permission - check for additional access rights to a given file
308 * @file: file to check access rights for
309 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
311 * Used to check for read/write/execute permissions on an already opened
315 * Do not use this function in new code. All access checks should
316 * be done using inode_permission().
318 int file_permission(struct file *file, int mask)
320 return inode_permission(file->f_path.dentry->d_inode, mask);
324 * get_write_access() gets write permission for a file.
325 * put_write_access() releases this write permission.
326 * This is used for regular files.
327 * We cannot support write (and maybe mmap read-write shared) accesses and
328 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
329 * can have the following values:
330 * 0: no writers, no VM_DENYWRITE mappings
331 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
332 * > 0: (i_writecount) users are writing to the file.
334 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
335 * except for the cases where we don't hold i_writecount yet. Then we need to
336 * use {get,deny}_write_access() - these functions check the sign and refuse
337 * to do the change if sign is wrong. Exclusion between them is provided by
338 * the inode->i_lock spinlock.
341 int get_write_access(struct inode * inode)
343 spin_lock(&inode->i_lock);
344 if (atomic_read(&inode->i_writecount) < 0) {
345 spin_unlock(&inode->i_lock);
348 atomic_inc(&inode->i_writecount);
349 spin_unlock(&inode->i_lock);
354 int deny_write_access(struct file * file)
356 struct inode *inode = file->f_path.dentry->d_inode;
358 spin_lock(&inode->i_lock);
359 if (atomic_read(&inode->i_writecount) > 0) {
360 spin_unlock(&inode->i_lock);
363 atomic_dec(&inode->i_writecount);
364 spin_unlock(&inode->i_lock);
370 * path_get - get a reference to a path
371 * @path: path to get the reference to
373 * Given a path increment the reference count to the dentry and the vfsmount.
375 void path_get(struct path *path)
380 EXPORT_SYMBOL(path_get);
383 * path_put - put a reference to a path
384 * @path: path to put the reference to
386 * Given a path decrement the reference count to the dentry and the vfsmount.
388 void path_put(struct path *path)
393 EXPORT_SYMBOL(path_put);
396 * Path walking has 2 modes, rcu-walk and ref-walk (see
397 * Documentation/filesystems/path-lookup.txt). In situations when we can't
398 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
399 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
400 * mode. Refcounts are grabbed at the last known good point before rcu-walk
401 * got stuck, so ref-walk may continue from there. If this is not successful
402 * (eg. a seqcount has changed), then failure is returned and it's up to caller
403 * to restart the path walk from the beginning in ref-walk mode.
407 * unlazy_walk - try to switch to ref-walk mode.
408 * @nd: nameidata pathwalk data
409 * @dentry: child of nd->path.dentry or NULL
410 * Returns: 0 on success, -ECHILD on failure
412 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
413 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
414 * @nd or NULL. Must be called from rcu-walk context.
416 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
418 struct fs_struct *fs = current->fs;
419 struct dentry *parent = nd->path.dentry;
422 BUG_ON(!(nd->flags & LOOKUP_RCU));
423 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
425 spin_lock(&fs->lock);
426 if (nd->root.mnt != fs->root.mnt ||
427 nd->root.dentry != fs->root.dentry)
430 spin_lock(&parent->d_lock);
432 if (!__d_rcu_to_refcount(parent, nd->seq))
434 BUG_ON(nd->inode != parent->d_inode);
436 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
437 if (!__d_rcu_to_refcount(dentry, nd->seq))
440 * If the sequence check on the child dentry passed, then
441 * the child has not been removed from its parent. This
442 * means the parent dentry must be valid and able to take
443 * a reference at this point.
445 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
446 BUG_ON(!parent->d_count);
448 spin_unlock(&dentry->d_lock);
450 spin_unlock(&parent->d_lock);
453 spin_unlock(&fs->lock);
455 mntget(nd->path.mnt);
458 br_read_unlock(vfsmount_lock);
459 nd->flags &= ~LOOKUP_RCU;
463 spin_unlock(&dentry->d_lock);
465 spin_unlock(&parent->d_lock);
468 spin_unlock(&fs->lock);
473 * release_open_intent - free up open intent resources
474 * @nd: pointer to nameidata
476 void release_open_intent(struct nameidata *nd)
478 struct file *file = nd->intent.open.file;
480 if (file && !IS_ERR(file)) {
481 if (file->f_path.dentry == NULL)
488 static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
490 return dentry->d_op->d_revalidate(dentry, nd);
493 static struct dentry *
494 do_revalidate(struct dentry *dentry, struct nameidata *nd)
496 int status = d_revalidate(dentry, nd);
497 if (unlikely(status <= 0)) {
499 * The dentry failed validation.
500 * If d_revalidate returned 0 attempt to invalidate
501 * the dentry otherwise d_revalidate is asking us
502 * to return a fail status.
506 dentry = ERR_PTR(status);
507 } else if (!d_invalidate(dentry)) {
516 * complete_walk - successful completion of path walk
517 * @nd: pointer nameidata
519 * If we had been in RCU mode, drop out of it and legitimize nd->path.
520 * Revalidate the final result, unless we'd already done that during
521 * the path walk or the filesystem doesn't ask for it. Return 0 on
522 * success, -error on failure. In case of failure caller does not
523 * need to drop nd->path.
525 static int complete_walk(struct nameidata *nd)
527 struct dentry *dentry = nd->path.dentry;
530 if (nd->flags & LOOKUP_RCU) {
531 nd->flags &= ~LOOKUP_RCU;
532 if (!(nd->flags & LOOKUP_ROOT))
534 spin_lock(&dentry->d_lock);
535 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
536 spin_unlock(&dentry->d_lock);
538 br_read_unlock(vfsmount_lock);
541 BUG_ON(nd->inode != dentry->d_inode);
542 spin_unlock(&dentry->d_lock);
543 mntget(nd->path.mnt);
545 br_read_unlock(vfsmount_lock);
548 if (likely(!(nd->flags & LOOKUP_JUMPED)))
551 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
554 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
557 /* Note: we do not d_invalidate() */
558 status = d_revalidate(dentry, nd);
570 * Short-cut version of permission(), for calling on directories
571 * during pathname resolution. Combines parts of permission()
572 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
574 * If appropriate, check DAC only. If not appropriate, or
575 * short-cut DAC fails, then call ->permission() to do more
576 * complete permission check.
578 static inline int exec_permission(struct inode *inode, unsigned int flags)
581 struct user_namespace *ns = inode_userns(inode);
583 if (inode->i_op->permission) {
584 ret = inode->i_op->permission(inode, MAY_EXEC, flags);
586 ret = acl_permission_check(inode, MAY_EXEC, flags,
587 inode->i_op->check_acl);
594 if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
595 ns_capable(ns, CAP_DAC_READ_SEARCH))
600 return security_inode_exec_permission(inode, flags);
603 static __always_inline void set_root(struct nameidata *nd)
606 get_fs_root(current->fs, &nd->root);
609 static int link_path_walk(const char *, struct nameidata *);
611 static __always_inline void set_root_rcu(struct nameidata *nd)
614 struct fs_struct *fs = current->fs;
618 seq = read_seqcount_begin(&fs->seq);
620 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
621 } while (read_seqcount_retry(&fs->seq, seq));
625 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
637 nd->flags |= LOOKUP_JUMPED;
639 nd->inode = nd->path.dentry->d_inode;
641 ret = link_path_walk(link, nd);
645 return PTR_ERR(link);
648 static void path_put_conditional(struct path *path, struct nameidata *nd)
651 if (path->mnt != nd->path.mnt)
655 static inline void path_to_nameidata(const struct path *path,
656 struct nameidata *nd)
658 if (!(nd->flags & LOOKUP_RCU)) {
659 dput(nd->path.dentry);
660 if (nd->path.mnt != path->mnt)
661 mntput(nd->path.mnt);
663 nd->path.mnt = path->mnt;
664 nd->path.dentry = path->dentry;
667 static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
669 struct inode *inode = link->dentry->d_inode;
670 if (!IS_ERR(cookie) && inode->i_op->put_link)
671 inode->i_op->put_link(link->dentry, nd, cookie);
675 static __always_inline int
676 follow_link(struct path *link, struct nameidata *nd, void **p)
679 struct dentry *dentry = link->dentry;
681 BUG_ON(nd->flags & LOOKUP_RCU);
683 if (link->mnt == nd->path.mnt)
686 if (unlikely(current->total_link_count >= 40)) {
687 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
692 current->total_link_count++;
694 touch_atime(link->mnt, dentry);
695 nd_set_link(nd, NULL);
697 error = security_inode_follow_link(link->dentry, nd);
699 *p = ERR_PTR(error); /* no ->put_link(), please */
704 nd->last_type = LAST_BIND;
705 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
708 char *s = nd_get_link(nd);
711 error = __vfs_follow_link(nd, s);
712 else if (nd->last_type == LAST_BIND) {
713 nd->flags |= LOOKUP_JUMPED;
714 nd->inode = nd->path.dentry->d_inode;
715 if (nd->inode->i_op->follow_link) {
716 /* stepped on a _really_ weird one */
725 static int follow_up_rcu(struct path *path)
727 struct vfsmount *parent;
728 struct dentry *mountpoint;
730 parent = path->mnt->mnt_parent;
731 if (parent == path->mnt)
733 mountpoint = path->mnt->mnt_mountpoint;
734 path->dentry = mountpoint;
739 int follow_up(struct path *path)
741 struct vfsmount *parent;
742 struct dentry *mountpoint;
744 br_read_lock(vfsmount_lock);
745 parent = path->mnt->mnt_parent;
746 if (parent == path->mnt) {
747 br_read_unlock(vfsmount_lock);
751 mountpoint = dget(path->mnt->mnt_mountpoint);
752 br_read_unlock(vfsmount_lock);
754 path->dentry = mountpoint;
761 * Perform an automount
762 * - return -EISDIR to tell follow_managed() to stop and return the path we
765 static int follow_automount(struct path *path, unsigned flags,
768 struct vfsmount *mnt;
771 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
774 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
775 * and this is the terminal part of the path.
777 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
778 return -EISDIR; /* we actually want to stop here */
780 /* We want to mount if someone is trying to open/create a file of any
781 * type under the mountpoint, wants to traverse through the mountpoint
782 * or wants to open the mounted directory.
784 * We don't want to mount if someone's just doing a stat and they've
785 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
786 * appended a '/' to the name.
788 if (!(flags & LOOKUP_FOLLOW) &&
789 !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
790 LOOKUP_OPEN | LOOKUP_CREATE)))
793 current->total_link_count++;
794 if (current->total_link_count >= 40)
797 mnt = path->dentry->d_op->d_automount(path);
800 * The filesystem is allowed to return -EISDIR here to indicate
801 * it doesn't want to automount. For instance, autofs would do
802 * this so that its userspace daemon can mount on this dentry.
804 * However, we can only permit this if it's a terminal point in
805 * the path being looked up; if it wasn't then the remainder of
806 * the path is inaccessible and we should say so.
808 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
813 if (!mnt) /* mount collision */
817 /* lock_mount() may release path->mnt on error */
821 err = finish_automount(mnt, path);
825 /* Someone else made a mount here whilst we were busy */
830 path->dentry = dget(mnt->mnt_root);
839 * Handle a dentry that is managed in some way.
840 * - Flagged for transit management (autofs)
841 * - Flagged as mountpoint
842 * - Flagged as automount point
844 * This may only be called in refwalk mode.
846 * Serialization is taken care of in namespace.c
848 static int follow_managed(struct path *path, unsigned flags)
850 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
852 bool need_mntput = false;
855 /* Given that we're not holding a lock here, we retain the value in a
856 * local variable for each dentry as we look at it so that we don't see
857 * the components of that value change under us */
858 while (managed = ACCESS_ONCE(path->dentry->d_flags),
859 managed &= DCACHE_MANAGED_DENTRY,
860 unlikely(managed != 0)) {
861 /* Allow the filesystem to manage the transit without i_mutex
863 if (managed & DCACHE_MANAGE_TRANSIT) {
864 BUG_ON(!path->dentry->d_op);
865 BUG_ON(!path->dentry->d_op->d_manage);
866 ret = path->dentry->d_op->d_manage(path->dentry, false);
871 /* Transit to a mounted filesystem. */
872 if (managed & DCACHE_MOUNTED) {
873 struct vfsmount *mounted = lookup_mnt(path);
879 path->dentry = dget(mounted->mnt_root);
884 /* Something is mounted on this dentry in another
885 * namespace and/or whatever was mounted there in this
886 * namespace got unmounted before we managed to get the
890 /* Handle an automount point */
891 if (managed & DCACHE_NEED_AUTOMOUNT) {
892 ret = follow_automount(path, flags, &need_mntput);
898 /* We didn't change the current path point */
902 if (need_mntput && path->mnt == mnt)
909 int follow_down_one(struct path *path)
911 struct vfsmount *mounted;
913 mounted = lookup_mnt(path);
918 path->dentry = dget(mounted->mnt_root);
924 static inline bool managed_dentry_might_block(struct dentry *dentry)
926 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
927 dentry->d_op->d_manage(dentry, true) < 0);
931 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
932 * we meet a managed dentry that would need blocking.
934 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
935 struct inode **inode)
938 struct vfsmount *mounted;
940 * Don't forget we might have a non-mountpoint managed dentry
941 * that wants to block transit.
943 *inode = path->dentry->d_inode;
944 if (unlikely(managed_dentry_might_block(path->dentry)))
947 if (!d_mountpoint(path->dentry))
950 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
954 path->dentry = mounted->mnt_root;
955 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
960 static void follow_mount_rcu(struct nameidata *nd)
962 while (d_mountpoint(nd->path.dentry)) {
963 struct vfsmount *mounted;
964 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
967 nd->path.mnt = mounted;
968 nd->path.dentry = mounted->mnt_root;
969 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
973 static int follow_dotdot_rcu(struct nameidata *nd)
978 if (nd->path.dentry == nd->root.dentry &&
979 nd->path.mnt == nd->root.mnt) {
982 if (nd->path.dentry != nd->path.mnt->mnt_root) {
983 struct dentry *old = nd->path.dentry;
984 struct dentry *parent = old->d_parent;
987 seq = read_seqcount_begin(&parent->d_seq);
988 if (read_seqcount_retry(&old->d_seq, nd->seq))
990 nd->path.dentry = parent;
994 if (!follow_up_rcu(&nd->path))
996 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
998 follow_mount_rcu(nd);
999 nd->inode = nd->path.dentry->d_inode;
1003 nd->flags &= ~LOOKUP_RCU;
1004 if (!(nd->flags & LOOKUP_ROOT))
1005 nd->root.mnt = NULL;
1007 br_read_unlock(vfsmount_lock);
1012 * Follow down to the covering mount currently visible to userspace. At each
1013 * point, the filesystem owning that dentry may be queried as to whether the
1014 * caller is permitted to proceed or not.
1016 int follow_down(struct path *path)
1021 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1022 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1023 /* Allow the filesystem to manage the transit without i_mutex
1026 * We indicate to the filesystem if someone is trying to mount
1027 * something here. This gives autofs the chance to deny anyone
1028 * other than its daemon the right to mount on its
1031 * The filesystem may sleep at this point.
1033 if (managed & DCACHE_MANAGE_TRANSIT) {
1034 BUG_ON(!path->dentry->d_op);
1035 BUG_ON(!path->dentry->d_op->d_manage);
1036 ret = path->dentry->d_op->d_manage(
1037 path->dentry, false);
1039 return ret == -EISDIR ? 0 : ret;
1042 /* Transit to a mounted filesystem. */
1043 if (managed & DCACHE_MOUNTED) {
1044 struct vfsmount *mounted = lookup_mnt(path);
1049 path->mnt = mounted;
1050 path->dentry = dget(mounted->mnt_root);
1054 /* Don't handle automount points here */
1061 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1063 static void follow_mount(struct path *path)
1065 while (d_mountpoint(path->dentry)) {
1066 struct vfsmount *mounted = lookup_mnt(path);
1071 path->mnt = mounted;
1072 path->dentry = dget(mounted->mnt_root);
1076 static void follow_dotdot(struct nameidata *nd)
1081 struct dentry *old = nd->path.dentry;
1083 if (nd->path.dentry == nd->root.dentry &&
1084 nd->path.mnt == nd->root.mnt) {
1087 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1088 /* rare case of legitimate dget_parent()... */
1089 nd->path.dentry = dget_parent(nd->path.dentry);
1093 if (!follow_up(&nd->path))
1096 follow_mount(&nd->path);
1097 nd->inode = nd->path.dentry->d_inode;
1101 * Allocate a dentry with name and parent, and perform a parent
1102 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1103 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1104 * have verified that no child exists while under i_mutex.
1106 static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1107 struct qstr *name, struct nameidata *nd)
1109 struct inode *inode = parent->d_inode;
1110 struct dentry *dentry;
1113 /* Don't create child dentry for a dead directory. */
1114 if (unlikely(IS_DEADDIR(inode)))
1115 return ERR_PTR(-ENOENT);
1117 dentry = d_alloc(parent, name);
1118 if (unlikely(!dentry))
1119 return ERR_PTR(-ENOMEM);
1121 old = inode->i_op->lookup(inode, dentry, nd);
1122 if (unlikely(old)) {
1130 * It's more convoluted than I'd like it to be, but... it's still fairly
1131 * small and for now I'd prefer to have fast path as straight as possible.
1132 * It _is_ time-critical.
1134 static int do_lookup(struct nameidata *nd, struct qstr *name,
1135 struct path *path, struct inode **inode)
1137 struct vfsmount *mnt = nd->path.mnt;
1138 struct dentry *dentry, *parent = nd->path.dentry;
1144 * Rename seqlock is not required here because in the off chance
1145 * of a false negative due to a concurrent rename, we're going to
1146 * do the non-racy lookup, below.
1148 if (nd->flags & LOOKUP_RCU) {
1151 dentry = __d_lookup_rcu(parent, name, &seq, inode);
1155 /* Memory barrier in read_seqcount_begin of child is enough */
1156 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1160 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1161 status = d_revalidate(dentry, nd);
1162 if (unlikely(status <= 0)) {
1163 if (status != -ECHILD)
1169 path->dentry = dentry;
1170 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1172 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1176 if (unlazy_walk(nd, dentry))
1179 dentry = __d_lookup(parent, name);
1183 if (unlikely(!dentry)) {
1184 struct inode *dir = parent->d_inode;
1185 BUG_ON(nd->inode != dir);
1187 mutex_lock(&dir->i_mutex);
1188 dentry = d_lookup(parent, name);
1189 if (likely(!dentry)) {
1190 dentry = d_alloc_and_lookup(parent, name, nd);
1191 if (IS_ERR(dentry)) {
1192 mutex_unlock(&dir->i_mutex);
1193 return PTR_ERR(dentry);
1199 mutex_unlock(&dir->i_mutex);
1201 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1202 status = d_revalidate(dentry, nd);
1203 if (unlikely(status <= 0)) {
1208 if (!d_invalidate(dentry)) {
1217 path->dentry = dentry;
1218 err = follow_managed(path, nd->flags);
1219 if (unlikely(err < 0)) {
1220 path_put_conditional(path, nd);
1223 *inode = path->dentry->d_inode;
1227 static inline int may_lookup(struct nameidata *nd)
1229 if (nd->flags & LOOKUP_RCU) {
1230 int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
1233 if (unlazy_walk(nd, NULL))
1236 return exec_permission(nd->inode, 0);
1239 static inline int handle_dots(struct nameidata *nd, int type)
1241 if (type == LAST_DOTDOT) {
1242 if (nd->flags & LOOKUP_RCU) {
1243 if (follow_dotdot_rcu(nd))
1251 static void terminate_walk(struct nameidata *nd)
1253 if (!(nd->flags & LOOKUP_RCU)) {
1254 path_put(&nd->path);
1256 nd->flags &= ~LOOKUP_RCU;
1257 if (!(nd->flags & LOOKUP_ROOT))
1258 nd->root.mnt = NULL;
1260 br_read_unlock(vfsmount_lock);
1264 static inline int walk_component(struct nameidata *nd, struct path *path,
1265 struct qstr *name, int type, int follow)
1267 struct inode *inode;
1270 * "." and ".." are special - ".." especially so because it has
1271 * to be able to know about the current root directory and
1272 * parent relationships.
1274 if (unlikely(type != LAST_NORM))
1275 return handle_dots(nd, type);
1276 err = do_lookup(nd, name, path, &inode);
1277 if (unlikely(err)) {
1282 path_to_nameidata(path, nd);
1286 if (unlikely(inode->i_op->follow_link) && follow) {
1287 if (nd->flags & LOOKUP_RCU) {
1288 if (unlikely(unlazy_walk(nd, path->dentry))) {
1293 BUG_ON(inode != path->dentry->d_inode);
1296 path_to_nameidata(path, nd);
1302 * This limits recursive symlink follows to 8, while
1303 * limiting consecutive symlinks to 40.
1305 * Without that kind of total limit, nasty chains of consecutive
1306 * symlinks can cause almost arbitrarily long lookups.
1308 static inline int nested_symlink(struct path *path, struct nameidata *nd)
1312 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1313 path_put_conditional(path, nd);
1314 path_put(&nd->path);
1317 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1320 current->link_count++;
1323 struct path link = *path;
1326 res = follow_link(&link, nd, &cookie);
1328 res = walk_component(nd, path, &nd->last,
1329 nd->last_type, LOOKUP_FOLLOW);
1330 put_link(nd, &link, cookie);
1333 current->link_count--;
1340 * This is the basic name resolution function, turning a pathname into
1341 * the final dentry. We expect 'base' to be positive and a directory.
1343 * Returns 0 and nd will have valid dentry and mnt on success.
1344 * Returns error and drops reference to input namei data on failure.
1346 static int link_path_walk(const char *name, struct nameidata *nd)
1350 unsigned int lookup_flags = nd->flags;
1357 /* At this point we know we have a real path component. */
1364 nd->flags |= LOOKUP_CONTINUE;
1366 err = may_lookup(nd);
1371 c = *(const unsigned char *)name;
1373 hash = init_name_hash();
1376 hash = partial_name_hash(c, hash);
1377 c = *(const unsigned char *)name;
1378 } while (c && (c != '/'));
1379 this.len = name - (const char *) this.name;
1380 this.hash = end_name_hash(hash);
1383 if (this.name[0] == '.') switch (this.len) {
1385 if (this.name[1] == '.') {
1387 nd->flags |= LOOKUP_JUMPED;
1393 if (likely(type == LAST_NORM)) {
1394 struct dentry *parent = nd->path.dentry;
1395 nd->flags &= ~LOOKUP_JUMPED;
1396 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1397 err = parent->d_op->d_hash(parent, nd->inode,
1404 /* remove trailing slashes? */
1406 goto last_component;
1407 while (*++name == '/');
1409 goto last_component;
1411 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1416 err = nested_symlink(&next, nd);
1421 if (!nd->inode->i_op->lookup)
1424 /* here ends the main loop */
1427 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1428 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
1430 nd->last_type = type;
1437 static int path_init(int dfd, const char *name, unsigned int flags,
1438 struct nameidata *nd, struct file **fp)
1444 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1445 nd->flags = flags | LOOKUP_JUMPED;
1447 if (flags & LOOKUP_ROOT) {
1448 struct inode *inode = nd->root.dentry->d_inode;
1450 if (!inode->i_op->lookup)
1452 retval = inode_permission(inode, MAY_EXEC);
1456 nd->path = nd->root;
1458 if (flags & LOOKUP_RCU) {
1459 br_read_lock(vfsmount_lock);
1461 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1463 path_get(&nd->path);
1468 nd->root.mnt = NULL;
1471 if (flags & LOOKUP_RCU) {
1472 br_read_lock(vfsmount_lock);
1477 path_get(&nd->root);
1479 nd->path = nd->root;
1480 } else if (dfd == AT_FDCWD) {
1481 if (flags & LOOKUP_RCU) {
1482 struct fs_struct *fs = current->fs;
1485 br_read_lock(vfsmount_lock);
1489 seq = read_seqcount_begin(&fs->seq);
1491 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1492 } while (read_seqcount_retry(&fs->seq, seq));
1494 get_fs_pwd(current->fs, &nd->path);
1497 struct dentry *dentry;
1499 file = fget_raw_light(dfd, &fput_needed);
1504 dentry = file->f_path.dentry;
1508 if (!S_ISDIR(dentry->d_inode->i_mode))
1511 retval = file_permission(file, MAY_EXEC);
1516 nd->path = file->f_path;
1517 if (flags & LOOKUP_RCU) {
1520 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1521 br_read_lock(vfsmount_lock);
1524 path_get(&file->f_path);
1525 fput_light(file, fput_needed);
1529 nd->inode = nd->path.dentry->d_inode;
1533 fput_light(file, fput_needed);
1538 static inline int lookup_last(struct nameidata *nd, struct path *path)
1540 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1541 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1543 nd->flags &= ~LOOKUP_PARENT;
1544 return walk_component(nd, path, &nd->last, nd->last_type,
1545 nd->flags & LOOKUP_FOLLOW);
1548 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1549 static int path_lookupat(int dfd, const char *name,
1550 unsigned int flags, struct nameidata *nd)
1552 struct file *base = NULL;
1557 * Path walking is largely split up into 2 different synchronisation
1558 * schemes, rcu-walk and ref-walk (explained in
1559 * Documentation/filesystems/path-lookup.txt). These share much of the
1560 * path walk code, but some things particularly setup, cleanup, and
1561 * following mounts are sufficiently divergent that functions are
1562 * duplicated. Typically there is a function foo(), and its RCU
1563 * analogue, foo_rcu().
1565 * -ECHILD is the error number of choice (just to avoid clashes) that
1566 * is returned if some aspect of an rcu-walk fails. Such an error must
1567 * be handled by restarting a traditional ref-walk (which will always
1568 * be able to complete).
1570 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1575 current->total_link_count = 0;
1576 err = link_path_walk(name, nd);
1578 if (!err && !(flags & LOOKUP_PARENT)) {
1579 err = lookup_last(nd, &path);
1582 struct path link = path;
1583 nd->flags |= LOOKUP_PARENT;
1584 err = follow_link(&link, nd, &cookie);
1586 err = lookup_last(nd, &path);
1587 put_link(nd, &link, cookie);
1592 err = complete_walk(nd);
1594 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1595 if (!nd->inode->i_op->lookup) {
1596 path_put(&nd->path);
1604 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1605 path_put(&nd->root);
1606 nd->root.mnt = NULL;
1611 static int do_path_lookup(int dfd, const char *name,
1612 unsigned int flags, struct nameidata *nd)
1614 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1615 if (unlikely(retval == -ECHILD))
1616 retval = path_lookupat(dfd, name, flags, nd);
1617 if (unlikely(retval == -ESTALE))
1618 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1620 if (likely(!retval)) {
1621 if (unlikely(!audit_dummy_context())) {
1622 if (nd->path.dentry && nd->inode)
1623 audit_inode(name, nd->path.dentry);
1629 int kern_path_parent(const char *name, struct nameidata *nd)
1631 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
1634 int kern_path(const char *name, unsigned int flags, struct path *path)
1636 struct nameidata nd;
1637 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1644 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1645 * @dentry: pointer to dentry of the base directory
1646 * @mnt: pointer to vfs mount of the base directory
1647 * @name: pointer to file name
1648 * @flags: lookup flags
1649 * @nd: pointer to nameidata
1651 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1652 const char *name, unsigned int flags,
1653 struct nameidata *nd)
1655 nd->root.dentry = dentry;
1657 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1658 return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
1661 static struct dentry *__lookup_hash(struct qstr *name,
1662 struct dentry *base, struct nameidata *nd)
1664 struct inode *inode = base->d_inode;
1665 struct dentry *dentry;
1668 err = exec_permission(inode, 0);
1670 return ERR_PTR(err);
1673 * Don't bother with __d_lookup: callers are for creat as
1674 * well as unlink, so a lot of the time it would cost
1677 dentry = d_lookup(base, name);
1679 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
1680 dentry = do_revalidate(dentry, nd);
1683 dentry = d_alloc_and_lookup(base, name, nd);
1689 * Restricted form of lookup. Doesn't follow links, single-component only,
1690 * needs parent already locked. Doesn't follow mounts.
1693 static struct dentry *lookup_hash(struct nameidata *nd)
1695 return __lookup_hash(&nd->last, nd->path.dentry, nd);
1699 * lookup_one_len - filesystem helper to lookup single pathname component
1700 * @name: pathname component to lookup
1701 * @base: base directory to lookup from
1702 * @len: maximum length @len should be interpreted to
1704 * Note that this routine is purely a helper for filesystem usage and should
1705 * not be called by generic code. Also note that by using this function the
1706 * nameidata argument is passed to the filesystem methods and a filesystem
1707 * using this helper needs to be prepared for that.
1709 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1715 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1720 return ERR_PTR(-EACCES);
1722 hash = init_name_hash();
1724 c = *(const unsigned char *)name++;
1725 if (c == '/' || c == '\0')
1726 return ERR_PTR(-EACCES);
1727 hash = partial_name_hash(c, hash);
1729 this.hash = end_name_hash(hash);
1731 * See if the low-level filesystem might want
1732 * to use its own hash..
1734 if (base->d_flags & DCACHE_OP_HASH) {
1735 int err = base->d_op->d_hash(base, base->d_inode, &this);
1737 return ERR_PTR(err);
1740 return __lookup_hash(&this, base, NULL);
1743 int user_path_at(int dfd, const char __user *name, unsigned flags,
1746 struct nameidata nd;
1747 char *tmp = getname_flags(name, flags);
1748 int err = PTR_ERR(tmp);
1751 BUG_ON(flags & LOOKUP_PARENT);
1753 err = do_path_lookup(dfd, tmp, flags, &nd);
1761 static int user_path_parent(int dfd, const char __user *path,
1762 struct nameidata *nd, char **name)
1764 char *s = getname(path);
1770 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1780 * It's inline, so penalty for filesystems that don't use sticky bit is
1783 static inline int check_sticky(struct inode *dir, struct inode *inode)
1785 uid_t fsuid = current_fsuid();
1787 if (!(dir->i_mode & S_ISVTX))
1789 if (current_user_ns() != inode_userns(inode))
1791 if (inode->i_uid == fsuid)
1793 if (dir->i_uid == fsuid)
1797 return !ns_capable(inode_userns(inode), CAP_FOWNER);
1801 * Check whether we can remove a link victim from directory dir, check
1802 * whether the type of victim is right.
1803 * 1. We can't do it if dir is read-only (done in permission())
1804 * 2. We should have write and exec permissions on dir
1805 * 3. We can't remove anything from append-only dir
1806 * 4. We can't do anything with immutable dir (done in permission())
1807 * 5. If the sticky bit on dir is set we should either
1808 * a. be owner of dir, or
1809 * b. be owner of victim, or
1810 * c. have CAP_FOWNER capability
1811 * 6. If the victim is append-only or immutable we can't do antyhing with
1812 * links pointing to it.
1813 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1814 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1815 * 9. We can't remove a root or mountpoint.
1816 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1817 * nfs_async_unlink().
1819 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1823 if (!victim->d_inode)
1826 BUG_ON(victim->d_parent->d_inode != dir);
1827 audit_inode_child(victim, dir);
1829 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1834 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1835 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1838 if (!S_ISDIR(victim->d_inode->i_mode))
1840 if (IS_ROOT(victim))
1842 } else if (S_ISDIR(victim->d_inode->i_mode))
1844 if (IS_DEADDIR(dir))
1846 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1851 /* Check whether we can create an object with dentry child in directory
1853 * 1. We can't do it if child already exists (open has special treatment for
1854 * this case, but since we are inlined it's OK)
1855 * 2. We can't do it if dir is read-only (done in permission())
1856 * 3. We should have write and exec permissions on dir
1857 * 4. We can't do it if dir is immutable (done in permission())
1859 static inline int may_create(struct inode *dir, struct dentry *child)
1863 if (IS_DEADDIR(dir))
1865 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1869 * p1 and p2 should be directories on the same fs.
1871 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1876 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1880 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1882 p = d_ancestor(p2, p1);
1884 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1885 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1889 p = d_ancestor(p1, p2);
1891 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1892 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1896 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1897 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1901 void unlock_rename(struct dentry *p1, struct dentry *p2)
1903 mutex_unlock(&p1->d_inode->i_mutex);
1905 mutex_unlock(&p2->d_inode->i_mutex);
1906 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1910 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1911 struct nameidata *nd)
1913 int error = may_create(dir, dentry);
1918 if (!dir->i_op->create)
1919 return -EACCES; /* shouldn't it be ENOSYS? */
1922 error = security_inode_create(dir, dentry, mode);
1925 error = dir->i_op->create(dir, dentry, mode, nd);
1927 fsnotify_create(dir, dentry);
1931 static int may_open(struct path *path, int acc_mode, int flag)
1933 struct dentry *dentry = path->dentry;
1934 struct inode *inode = dentry->d_inode;
1944 switch (inode->i_mode & S_IFMT) {
1948 if (acc_mode & MAY_WRITE)
1953 if (path->mnt->mnt_flags & MNT_NODEV)
1962 error = inode_permission(inode, acc_mode);
1967 * An append-only file must be opened in append mode for writing.
1969 if (IS_APPEND(inode)) {
1970 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
1976 /* O_NOATIME can only be set by the owner or superuser */
1977 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
1981 * Ensure there are no outstanding leases on the file.
1983 return break_lease(inode, flag);
1986 static int handle_truncate(struct file *filp)
1988 struct path *path = &filp->f_path;
1989 struct inode *inode = path->dentry->d_inode;
1990 int error = get_write_access(inode);
1994 * Refuse to truncate files with mandatory locks held on them.
1996 error = locks_verify_locked(inode);
1998 error = security_path_truncate(path);
2000 error = do_truncate(path->dentry, 0,
2001 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2004 put_write_access(inode);
2009 * Note that while the flag value (low two bits) for sys_open means:
2014 * it is changed into
2015 * 00 - no permissions needed
2016 * 01 - read-permission
2017 * 10 - write-permission
2019 * for the internal routines (ie open_namei()/follow_link() etc)
2020 * This is more logical, and also allows the 00 "no perm needed"
2021 * to be used for symlinks (where the permissions are checked
2025 static inline int open_to_namei_flags(int flag)
2027 if ((flag+1) & O_ACCMODE)
2033 * Handle the last step of open()
2035 static struct file *do_last(struct nameidata *nd, struct path *path,
2036 const struct open_flags *op, const char *pathname)
2038 struct dentry *dir = nd->path.dentry;
2039 struct dentry *dentry;
2040 int open_flag = op->open_flag;
2041 int will_truncate = open_flag & O_TRUNC;
2043 int acc_mode = op->acc_mode;
2047 nd->flags &= ~LOOKUP_PARENT;
2048 nd->flags |= op->intent;
2050 switch (nd->last_type) {
2053 error = handle_dots(nd, nd->last_type);
2055 return ERR_PTR(error);
2058 error = complete_walk(nd);
2060 return ERR_PTR(error);
2061 audit_inode(pathname, nd->path.dentry);
2062 if (open_flag & O_CREAT) {
2068 error = complete_walk(nd);
2070 return ERR_PTR(error);
2071 audit_inode(pathname, dir);
2075 if (!(open_flag & O_CREAT)) {
2077 if (nd->last.name[nd->last.len])
2078 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2079 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2081 /* we _can_ be in RCU mode here */
2082 error = walk_component(nd, path, &nd->last, LAST_NORM,
2085 return ERR_PTR(error);
2086 if (error) /* symlink */
2089 error = complete_walk(nd);
2091 return ERR_PTR(-ECHILD);
2094 if (nd->flags & LOOKUP_DIRECTORY) {
2095 if (!nd->inode->i_op->lookup)
2098 audit_inode(pathname, nd->path.dentry);
2102 /* create side of things */
2103 error = complete_walk(nd);
2105 return ERR_PTR(error);
2107 audit_inode(pathname, dir);
2109 /* trailing slashes? */
2110 if (nd->last.name[nd->last.len])
2113 mutex_lock(&dir->d_inode->i_mutex);
2115 dentry = lookup_hash(nd);
2116 error = PTR_ERR(dentry);
2117 if (IS_ERR(dentry)) {
2118 mutex_unlock(&dir->d_inode->i_mutex);
2122 path->dentry = dentry;
2123 path->mnt = nd->path.mnt;
2125 /* Negative dentry, just create the file */
2126 if (!dentry->d_inode) {
2127 int mode = op->mode;
2128 if (!IS_POSIXACL(dir->d_inode))
2129 mode &= ~current_umask();
2131 * This write is needed to ensure that a
2132 * rw->ro transition does not occur between
2133 * the time when the file is created and when
2134 * a permanent write count is taken through
2135 * the 'struct file' in nameidata_to_filp().
2137 error = mnt_want_write(nd->path.mnt);
2139 goto exit_mutex_unlock;
2141 /* Don't check for write permission, don't truncate */
2142 open_flag &= ~O_TRUNC;
2144 acc_mode = MAY_OPEN;
2145 error = security_path_mknod(&nd->path, dentry, mode, 0);
2147 goto exit_mutex_unlock;
2148 error = vfs_create(dir->d_inode, dentry, mode, nd);
2150 goto exit_mutex_unlock;
2151 mutex_unlock(&dir->d_inode->i_mutex);
2152 dput(nd->path.dentry);
2153 nd->path.dentry = dentry;
2158 * It already exists.
2160 mutex_unlock(&dir->d_inode->i_mutex);
2161 audit_inode(pathname, path->dentry);
2164 if (open_flag & O_EXCL)
2167 error = follow_managed(path, nd->flags);
2172 if (!path->dentry->d_inode)
2175 if (path->dentry->d_inode->i_op->follow_link)
2178 path_to_nameidata(path, nd);
2179 nd->inode = path->dentry->d_inode;
2181 if (S_ISDIR(nd->inode->i_mode))
2184 if (!S_ISREG(nd->inode->i_mode))
2187 if (will_truncate) {
2188 error = mnt_want_write(nd->path.mnt);
2194 error = may_open(&nd->path, acc_mode, open_flag);
2197 filp = nameidata_to_filp(nd);
2198 if (!IS_ERR(filp)) {
2199 error = ima_file_check(filp, op->acc_mode);
2202 filp = ERR_PTR(error);
2205 if (!IS_ERR(filp)) {
2206 if (will_truncate) {
2207 error = handle_truncate(filp);
2210 filp = ERR_PTR(error);
2216 mnt_drop_write(nd->path.mnt);
2217 path_put(&nd->path);
2221 mutex_unlock(&dir->d_inode->i_mutex);
2223 path_put_conditional(path, nd);
2225 filp = ERR_PTR(error);
2229 static struct file *path_openat(int dfd, const char *pathname,
2230 struct nameidata *nd, const struct open_flags *op, int flags)
2232 struct file *base = NULL;
2237 filp = get_empty_filp();
2239 return ERR_PTR(-ENFILE);
2241 filp->f_flags = op->open_flag;
2242 nd->intent.open.file = filp;
2243 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2244 nd->intent.open.create_mode = op->mode;
2246 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
2247 if (unlikely(error))
2250 current->total_link_count = 0;
2251 error = link_path_walk(pathname, nd);
2252 if (unlikely(error))
2255 filp = do_last(nd, &path, op, pathname);
2256 while (unlikely(!filp)) { /* trailing symlink */
2257 struct path link = path;
2259 if (!(nd->flags & LOOKUP_FOLLOW)) {
2260 path_put_conditional(&path, nd);
2261 path_put(&nd->path);
2262 filp = ERR_PTR(-ELOOP);
2265 nd->flags |= LOOKUP_PARENT;
2266 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2267 error = follow_link(&link, nd, &cookie);
2268 if (unlikely(error))
2269 filp = ERR_PTR(error);
2271 filp = do_last(nd, &path, op, pathname);
2272 put_link(nd, &link, cookie);
2275 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2276 path_put(&nd->root);
2279 release_open_intent(nd);
2283 filp = ERR_PTR(error);
2287 struct file *do_filp_open(int dfd, const char *pathname,
2288 const struct open_flags *op, int flags)
2290 struct nameidata nd;
2293 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
2294 if (unlikely(filp == ERR_PTR(-ECHILD)))
2295 filp = path_openat(dfd, pathname, &nd, op, flags);
2296 if (unlikely(filp == ERR_PTR(-ESTALE)))
2297 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
2301 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2302 const char *name, const struct open_flags *op, int flags)
2304 struct nameidata nd;
2308 nd.root.dentry = dentry;
2310 flags |= LOOKUP_ROOT;
2312 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
2313 return ERR_PTR(-ELOOP);
2315 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2316 if (unlikely(file == ERR_PTR(-ECHILD)))
2317 file = path_openat(-1, name, &nd, op, flags);
2318 if (unlikely(file == ERR_PTR(-ESTALE)))
2319 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2324 * lookup_create - lookup a dentry, creating it if it doesn't exist
2325 * @nd: nameidata info
2326 * @is_dir: directory flag
2328 * Simple function to lookup and return a dentry and create it
2329 * if it doesn't exist. Is SMP-safe.
2331 * Returns with nd->path.dentry->d_inode->i_mutex locked.
2333 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2335 struct dentry *dentry = ERR_PTR(-EEXIST);
2337 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2339 * Yucky last component or no last component at all?
2340 * (foo/., foo/.., /////)
2342 if (nd->last_type != LAST_NORM)
2344 nd->flags &= ~LOOKUP_PARENT;
2345 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2346 nd->intent.open.flags = O_EXCL;
2349 * Do the final lookup.
2351 dentry = lookup_hash(nd);
2355 if (dentry->d_inode)
2358 * Special case - lookup gave negative, but... we had foo/bar/
2359 * From the vfs_mknod() POV we just have a negative dentry -
2360 * all is fine. Let's be bastards - you had / on the end, you've
2361 * been asking for (non-existent) directory. -ENOENT for you.
2363 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2365 dentry = ERR_PTR(-ENOENT);
2370 dentry = ERR_PTR(-EEXIST);
2374 EXPORT_SYMBOL_GPL(lookup_create);
2376 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2378 int error = may_create(dir, dentry);
2383 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2384 !ns_capable(inode_userns(dir), CAP_MKNOD))
2387 if (!dir->i_op->mknod)
2390 error = devcgroup_inode_mknod(mode, dev);
2394 error = security_inode_mknod(dir, dentry, mode, dev);
2398 error = dir->i_op->mknod(dir, dentry, mode, dev);
2400 fsnotify_create(dir, dentry);
2404 static int may_mknod(mode_t mode)
2406 switch (mode & S_IFMT) {
2412 case 0: /* zero mode translates to S_IFREG */
2421 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2426 struct dentry *dentry;
2427 struct nameidata nd;
2432 error = user_path_parent(dfd, filename, &nd, &tmp);
2436 dentry = lookup_create(&nd, 0);
2437 if (IS_ERR(dentry)) {
2438 error = PTR_ERR(dentry);
2441 if (!IS_POSIXACL(nd.path.dentry->d_inode))
2442 mode &= ~current_umask();
2443 error = may_mknod(mode);
2446 error = mnt_want_write(nd.path.mnt);
2449 error = security_path_mknod(&nd.path, dentry, mode, dev);
2451 goto out_drop_write;
2452 switch (mode & S_IFMT) {
2453 case 0: case S_IFREG:
2454 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
2456 case S_IFCHR: case S_IFBLK:
2457 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
2458 new_decode_dev(dev));
2460 case S_IFIFO: case S_IFSOCK:
2461 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
2465 mnt_drop_write(nd.path.mnt);
2469 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2476 SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
2478 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2481 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2483 int error = may_create(dir, dentry);
2488 if (!dir->i_op->mkdir)
2491 mode &= (S_IRWXUGO|S_ISVTX);
2492 error = security_inode_mkdir(dir, dentry, mode);
2496 error = dir->i_op->mkdir(dir, dentry, mode);
2498 fsnotify_mkdir(dir, dentry);
2502 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
2506 struct dentry *dentry;
2507 struct nameidata nd;
2509 error = user_path_parent(dfd, pathname, &nd, &tmp);
2513 dentry = lookup_create(&nd, 1);
2514 error = PTR_ERR(dentry);
2518 if (!IS_POSIXACL(nd.path.dentry->d_inode))
2519 mode &= ~current_umask();
2520 error = mnt_want_write(nd.path.mnt);
2523 error = security_path_mkdir(&nd.path, dentry, mode);
2525 goto out_drop_write;
2526 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2528 mnt_drop_write(nd.path.mnt);
2532 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2539 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
2541 return sys_mkdirat(AT_FDCWD, pathname, mode);
2545 * The dentry_unhash() helper will try to drop the dentry early: we
2546 * should have a usage count of 2 if we're the only user of this
2547 * dentry, and if that is true (possibly after pruning the dcache),
2548 * then we drop the dentry now.
2550 * A low-level filesystem can, if it choses, legally
2553 * if (!d_unhashed(dentry))
2556 * if it cannot handle the case of removing a directory
2557 * that is still in use by something else..
2559 void dentry_unhash(struct dentry *dentry)
2561 shrink_dcache_parent(dentry);
2562 spin_lock(&dentry->d_lock);
2563 if (dentry->d_count == 1)
2565 spin_unlock(&dentry->d_lock);
2568 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2570 int error = may_delete(dir, dentry, 1);
2575 if (!dir->i_op->rmdir)
2578 mutex_lock(&dentry->d_inode->i_mutex);
2581 if (d_mountpoint(dentry))
2584 error = security_inode_rmdir(dir, dentry);
2588 shrink_dcache_parent(dentry);
2589 error = dir->i_op->rmdir(dir, dentry);
2593 dentry->d_inode->i_flags |= S_DEAD;
2597 mutex_unlock(&dentry->d_inode->i_mutex);
2603 static long do_rmdir(int dfd, const char __user *pathname)
2607 struct dentry *dentry;
2608 struct nameidata nd;
2610 error = user_path_parent(dfd, pathname, &nd, &name);
2614 switch(nd.last_type) {
2626 nd.flags &= ~LOOKUP_PARENT;
2628 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2629 dentry = lookup_hash(&nd);
2630 error = PTR_ERR(dentry);
2633 if (!dentry->d_inode) {
2637 error = mnt_want_write(nd.path.mnt);
2640 error = security_path_rmdir(&nd.path, dentry);
2643 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2645 mnt_drop_write(nd.path.mnt);
2649 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2656 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
2658 return do_rmdir(AT_FDCWD, pathname);
2661 int vfs_unlink(struct inode *dir, struct dentry *dentry)
2663 int error = may_delete(dir, dentry, 0);
2668 if (!dir->i_op->unlink)
2671 mutex_lock(&dentry->d_inode->i_mutex);
2672 if (d_mountpoint(dentry))
2675 error = security_inode_unlink(dir, dentry);
2677 error = dir->i_op->unlink(dir, dentry);
2682 mutex_unlock(&dentry->d_inode->i_mutex);
2684 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2685 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2686 fsnotify_link_count(dentry->d_inode);
2694 * Make sure that the actual truncation of the file will occur outside its
2695 * directory's i_mutex. Truncate can take a long time if there is a lot of
2696 * writeout happening, and we don't want to prevent access to the directory
2697 * while waiting on the I/O.
2699 static long do_unlinkat(int dfd, const char __user *pathname)
2703 struct dentry *dentry;
2704 struct nameidata nd;
2705 struct inode *inode = NULL;
2707 error = user_path_parent(dfd, pathname, &nd, &name);
2712 if (nd.last_type != LAST_NORM)
2715 nd.flags &= ~LOOKUP_PARENT;
2717 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2718 dentry = lookup_hash(&nd);
2719 error = PTR_ERR(dentry);
2720 if (!IS_ERR(dentry)) {
2721 /* Why not before? Because we want correct error value */
2722 if (nd.last.name[nd.last.len])
2724 inode = dentry->d_inode;
2728 error = mnt_want_write(nd.path.mnt);
2731 error = security_path_unlink(&nd.path, dentry);
2734 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2736 mnt_drop_write(nd.path.mnt);
2740 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2742 iput(inode); /* truncate the inode here */
2749 error = !dentry->d_inode ? -ENOENT :
2750 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2754 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
2756 if ((flag & ~AT_REMOVEDIR) != 0)
2759 if (flag & AT_REMOVEDIR)
2760 return do_rmdir(dfd, pathname);
2762 return do_unlinkat(dfd, pathname);
2765 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
2767 return do_unlinkat(AT_FDCWD, pathname);
2770 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
2772 int error = may_create(dir, dentry);
2777 if (!dir->i_op->symlink)
2780 error = security_inode_symlink(dir, dentry, oldname);
2784 error = dir->i_op->symlink(dir, dentry, oldname);
2786 fsnotify_create(dir, dentry);
2790 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2791 int, newdfd, const char __user *, newname)
2796 struct dentry *dentry;
2797 struct nameidata nd;
2799 from = getname(oldname);
2801 return PTR_ERR(from);
2803 error = user_path_parent(newdfd, newname, &nd, &to);
2807 dentry = lookup_create(&nd, 0);
2808 error = PTR_ERR(dentry);
2812 error = mnt_want_write(nd.path.mnt);
2815 error = security_path_symlink(&nd.path, dentry, from);
2817 goto out_drop_write;
2818 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2820 mnt_drop_write(nd.path.mnt);
2824 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2832 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
2834 return sys_symlinkat(oldname, AT_FDCWD, newname);
2837 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2839 struct inode *inode = old_dentry->d_inode;
2845 error = may_create(dir, new_dentry);
2849 if (dir->i_sb != inode->i_sb)
2853 * A link to an append-only or immutable file cannot be created.
2855 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2857 if (!dir->i_op->link)
2859 if (S_ISDIR(inode->i_mode))
2862 error = security_inode_link(old_dentry, dir, new_dentry);
2866 mutex_lock(&inode->i_mutex);
2867 /* Make sure we don't allow creating hardlink to an unlinked file */
2868 if (inode->i_nlink == 0)
2871 error = dir->i_op->link(old_dentry, dir, new_dentry);
2872 mutex_unlock(&inode->i_mutex);
2874 fsnotify_link(dir, inode, new_dentry);
2879 * Hardlinks are often used in delicate situations. We avoid
2880 * security-related surprises by not following symlinks on the
2883 * We don't follow them on the oldname either to be compatible
2884 * with linux 2.0, and to avoid hard-linking to directories
2885 * and other special files. --ADM
2887 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2888 int, newdfd, const char __user *, newname, int, flags)
2890 struct dentry *new_dentry;
2891 struct nameidata nd;
2892 struct path old_path;
2897 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2900 * To use null names we require CAP_DAC_READ_SEARCH
2901 * This ensures that not everyone will be able to create
2902 * handlink using the passed filedescriptor.
2904 if (flags & AT_EMPTY_PATH) {
2905 if (!capable(CAP_DAC_READ_SEARCH))
2910 if (flags & AT_SYMLINK_FOLLOW)
2911 how |= LOOKUP_FOLLOW;
2913 error = user_path_at(olddfd, oldname, how, &old_path);
2917 error = user_path_parent(newdfd, newname, &nd, &to);
2921 if (old_path.mnt != nd.path.mnt)
2923 new_dentry = lookup_create(&nd, 0);
2924 error = PTR_ERR(new_dentry);
2925 if (IS_ERR(new_dentry))
2927 error = mnt_want_write(nd.path.mnt);
2930 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2932 goto out_drop_write;
2933 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2935 mnt_drop_write(nd.path.mnt);
2939 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2944 path_put(&old_path);
2949 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
2951 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
2955 * The worst of all namespace operations - renaming directory. "Perverted"
2956 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2958 * a) we can get into loop creation. Check is done in is_subdir().
2959 * b) race potential - two innocent renames can create a loop together.
2960 * That's where 4.4 screws up. Current fix: serialization on
2961 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
2963 * c) we have to lock _three_ objects - parents and victim (if it exists).
2964 * And that - after we got ->i_mutex on parents (until then we don't know
2965 * whether the target exists). Solution: try to be smart with locking
2966 * order for inodes. We rely on the fact that tree topology may change
2967 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
2968 * move will be locked. Thus we can rank directories by the tree
2969 * (ancestors first) and rank all non-directories after them.
2970 * That works since everybody except rename does "lock parent, lookup,
2971 * lock child" and rename is under ->s_vfs_rename_mutex.
2972 * HOWEVER, it relies on the assumption that any object with ->lookup()
2973 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2974 * we'd better make sure that there's no link(2) for them.
2975 * d) conversion from fhandle to dentry may come in the wrong moment - when
2976 * we are removing the target. Solution: we will have to grab ->i_mutex
2977 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2978 * ->i_mutex on parents, which works but leads to some truly excessive
2981 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2982 struct inode *new_dir, struct dentry *new_dentry)
2985 struct inode *target = new_dentry->d_inode;
2988 * If we are going to change the parent - check write permissions,
2989 * we'll need to flip '..'.
2991 if (new_dir != old_dir) {
2992 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
2997 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3002 mutex_lock(&target->i_mutex);
3005 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3009 shrink_dcache_parent(new_dentry);
3010 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3015 target->i_flags |= S_DEAD;
3016 dont_mount(new_dentry);
3020 mutex_unlock(&target->i_mutex);
3022 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3023 d_move(old_dentry,new_dentry);
3027 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3028 struct inode *new_dir, struct dentry *new_dentry)
3030 struct inode *target = new_dentry->d_inode;
3033 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3039 mutex_lock(&target->i_mutex);
3042 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3045 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3050 dont_mount(new_dentry);
3051 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3052 d_move(old_dentry, new_dentry);
3055 mutex_unlock(&target->i_mutex);
3060 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3061 struct inode *new_dir, struct dentry *new_dentry)
3064 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
3065 const unsigned char *old_name;
3067 if (old_dentry->d_inode == new_dentry->d_inode)
3070 error = may_delete(old_dir, old_dentry, is_dir);
3074 if (!new_dentry->d_inode)
3075 error = may_create(new_dir, new_dentry);
3077 error = may_delete(new_dir, new_dentry, is_dir);
3081 if (!old_dir->i_op->rename)
3084 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3087 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3089 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3091 fsnotify_move(old_dir, new_dir, old_name, is_dir,
3092 new_dentry->d_inode, old_dentry);
3093 fsnotify_oldname_free(old_name);
3098 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3099 int, newdfd, const char __user *, newname)
3101 struct dentry *old_dir, *new_dir;
3102 struct dentry *old_dentry, *new_dentry;
3103 struct dentry *trap;
3104 struct nameidata oldnd, newnd;
3109 error = user_path_parent(olddfd, oldname, &oldnd, &from);
3113 error = user_path_parent(newdfd, newname, &newnd, &to);
3118 if (oldnd.path.mnt != newnd.path.mnt)
3121 old_dir = oldnd.path.dentry;
3123 if (oldnd.last_type != LAST_NORM)
3126 new_dir = newnd.path.dentry;
3127 if (newnd.last_type != LAST_NORM)
3130 oldnd.flags &= ~LOOKUP_PARENT;
3131 newnd.flags &= ~LOOKUP_PARENT;
3132 newnd.flags |= LOOKUP_RENAME_TARGET;
3134 trap = lock_rename(new_dir, old_dir);
3136 old_dentry = lookup_hash(&oldnd);
3137 error = PTR_ERR(old_dentry);
3138 if (IS_ERR(old_dentry))
3140 /* source must exist */
3142 if (!old_dentry->d_inode)
3144 /* unless the source is a directory trailing slashes give -ENOTDIR */
3145 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3147 if (oldnd.last.name[oldnd.last.len])
3149 if (newnd.last.name[newnd.last.len])
3152 /* source should not be ancestor of target */
3154 if (old_dentry == trap)
3156 new_dentry = lookup_hash(&newnd);
3157 error = PTR_ERR(new_dentry);
3158 if (IS_ERR(new_dentry))
3160 /* target should not be an ancestor of source */
3162 if (new_dentry == trap)
3165 error = mnt_want_write(oldnd.path.mnt);
3168 error = security_path_rename(&oldnd.path, old_dentry,
3169 &newnd.path, new_dentry);
3172 error = vfs_rename(old_dir->d_inode, old_dentry,
3173 new_dir->d_inode, new_dentry);
3175 mnt_drop_write(oldnd.path.mnt);
3181 unlock_rename(new_dir, old_dir);
3183 path_put(&newnd.path);
3186 path_put(&oldnd.path);
3192 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
3194 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3197 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3201 len = PTR_ERR(link);
3206 if (len > (unsigned) buflen)
3208 if (copy_to_user(buffer, link, len))
3215 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3216 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3217 * using) it for any given inode is up to filesystem.
3219 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3221 struct nameidata nd;
3226 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3228 return PTR_ERR(cookie);
3230 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3231 if (dentry->d_inode->i_op->put_link)
3232 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3236 int vfs_follow_link(struct nameidata *nd, const char *link)
3238 return __vfs_follow_link(nd, link);
3241 /* get the link contents into pagecache */
3242 static char *page_getlink(struct dentry * dentry, struct page **ppage)
3246 struct address_space *mapping = dentry->d_inode->i_mapping;
3247 page = read_mapping_page(mapping, 0, NULL);
3252 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3256 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3258 struct page *page = NULL;
3259 char *s = page_getlink(dentry, &page);
3260 int res = vfs_readlink(dentry,buffer,buflen,s);
3263 page_cache_release(page);
3268 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
3270 struct page *page = NULL;
3271 nd_set_link(nd, page_getlink(dentry, &page));
3275 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
3277 struct page *page = cookie;
3281 page_cache_release(page);
3286 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3288 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
3290 struct address_space *mapping = inode->i_mapping;
3295 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3297 flags |= AOP_FLAG_NOFS;
3300 err = pagecache_write_begin(NULL, mapping, 0, len-1,
3301 flags, &page, &fsdata);
3305 kaddr = kmap_atomic(page, KM_USER0);
3306 memcpy(kaddr, symname, len-1);
3307 kunmap_atomic(kaddr, KM_USER0);
3309 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3316 mark_inode_dirty(inode);
3322 int page_symlink(struct inode *inode, const char *symname, int len)
3324 return __page_symlink(inode, symname, len,
3325 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
3328 const struct inode_operations page_symlink_inode_operations = {
3329 .readlink = generic_readlink,
3330 .follow_link = page_follow_link_light,
3331 .put_link = page_put_link,
3334 EXPORT_SYMBOL(user_path_at);
3335 EXPORT_SYMBOL(follow_down_one);
3336 EXPORT_SYMBOL(follow_down);
3337 EXPORT_SYMBOL(follow_up);
3338 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3339 EXPORT_SYMBOL(getname);
3340 EXPORT_SYMBOL(lock_rename);
3341 EXPORT_SYMBOL(lookup_one_len);
3342 EXPORT_SYMBOL(page_follow_link_light);
3343 EXPORT_SYMBOL(page_put_link);
3344 EXPORT_SYMBOL(page_readlink);
3345 EXPORT_SYMBOL(__page_symlink);
3346 EXPORT_SYMBOL(page_symlink);
3347 EXPORT_SYMBOL(page_symlink_inode_operations);
3348 EXPORT_SYMBOL(kern_path_parent);
3349 EXPORT_SYMBOL(kern_path);
3350 EXPORT_SYMBOL(vfs_path_lookup);
3351 EXPORT_SYMBOL(inode_permission);
3352 EXPORT_SYMBOL(file_permission);
3353 EXPORT_SYMBOL(unlock_rename);
3354 EXPORT_SYMBOL(vfs_create);
3355 EXPORT_SYMBOL(vfs_follow_link);
3356 EXPORT_SYMBOL(vfs_link);
3357 EXPORT_SYMBOL(vfs_mkdir);
3358 EXPORT_SYMBOL(vfs_mknod);
3359 EXPORT_SYMBOL(generic_permission);
3360 EXPORT_SYMBOL(vfs_readlink);
3361 EXPORT_SYMBOL(vfs_rename);
3362 EXPORT_SYMBOL(vfs_rmdir);
3363 EXPORT_SYMBOL(vfs_symlink);
3364 EXPORT_SYMBOL(vfs_unlink);
3365 EXPORT_SYMBOL(dentry_unhash);
3366 EXPORT_SYMBOL(generic_readlink);