Linux 3.12.28
[linux/fpc-iii.git] / fs / namei.c
blobe3249d565c95a9b2d8d61a2199de28cb8d733c1c
1 /*
2 * linux/fs/namei.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * Some corrections by tytso.
9 */
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
17 #include <linux/init.h>
18 #include <linux/export.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/fs.h>
22 #include <linux/namei.h>
23 #include <linux/pagemap.h>
24 #include <linux/fsnotify.h>
25 #include <linux/personality.h>
26 #include <linux/security.h>
27 #include <linux/ima.h>
28 #include <linux/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <linux/capability.h>
32 #include <linux/file.h>
33 #include <linux/fcntl.h>
34 #include <linux/device_cgroup.h>
35 #include <linux/fs_struct.h>
36 #include <linux/posix_acl.h>
37 #include <asm/uaccess.h>
39 #include "internal.h"
40 #include "mount.h"
42 /* [Feb-1997 T. Schoebel-Theuer]
43 * Fundamental changes in the pathname lookup mechanisms (namei)
44 * were necessary because of omirr. The reason is that omirr needs
45 * to know the _real_ pathname, not the user-supplied one, in case
46 * of symlinks (and also when transname replacements occur).
48 * The new code replaces the old recursive symlink resolution with
49 * an iterative one (in case of non-nested symlink chains). It does
50 * this with calls to <fs>_follow_link().
51 * As a side effect, dir_namei(), _namei() and follow_link() are now
52 * replaced with a single function lookup_dentry() that can handle all
53 * the special cases of the former code.
55 * With the new dcache, the pathname is stored at each inode, at least as
56 * long as the refcount of the inode is positive. As a side effect, the
57 * size of the dcache depends on the inode cache and thus is dynamic.
59 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
60 * resolution to correspond with current state of the code.
62 * Note that the symlink resolution is not *completely* iterative.
63 * There is still a significant amount of tail- and mid- recursion in
64 * the algorithm. Also, note that <fs>_readlink() is not used in
65 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
66 * may return different results than <fs>_follow_link(). Many virtual
67 * filesystems (including /proc) exhibit this behavior.
70 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
71 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
72 * and the name already exists in form of a symlink, try to create the new
73 * name indicated by the symlink. The old code always complained that the
74 * name already exists, due to not following the symlink even if its target
75 * is nonexistent. The new semantics affects also mknod() and link() when
76 * the name is a symlink pointing to a non-existent name.
78 * I don't know which semantics is the right one, since I have no access
79 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
80 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
81 * "old" one. Personally, I think the new semantics is much more logical.
82 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
83 * file does succeed in both HP-UX and SunOs, but not in Solaris
84 * and in the old Linux semantics.
87 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
88 * semantics. See the comments in "open_namei" and "do_link" below.
90 * [10-Sep-98 Alan Modra] Another symlink change.
93 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
94 * inside the path - always follow.
95 * in the last component in creation/removal/renaming - never follow.
96 * if LOOKUP_FOLLOW passed - follow.
97 * if the pathname has trailing slashes - follow.
98 * otherwise - don't follow.
99 * (applied in that order).
101 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
102 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
103 * During the 2.4 we need to fix the userland stuff depending on it -
104 * hopefully we will be able to get rid of that wart in 2.5. So far only
105 * XEmacs seems to be relying on it...
108 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
109 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
110 * any extra contention...
113 /* In order to reduce some races, while at the same time doing additional
114 * checking and hopefully speeding things up, we copy filenames to the
115 * kernel data space before using them..
117 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
118 * PATH_MAX includes the nul terminator --RR.
120 void final_putname(struct filename *name)
122 if (name->separate) {
123 __putname(name->name);
124 kfree(name);
125 } else {
126 __putname(name);
130 #define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename))
132 static struct filename *
133 getname_flags(const char __user *filename, int flags, int *empty)
135 struct filename *result, *err;
136 int len;
137 long max;
138 char *kname;
140 result = audit_reusename(filename);
141 if (result)
142 return result;
144 result = __getname();
145 if (unlikely(!result))
146 return ERR_PTR(-ENOMEM);
149 * First, try to embed the struct filename inside the names_cache
150 * allocation
152 kname = (char *)result + sizeof(*result);
153 result->name = kname;
154 result->separate = false;
155 max = EMBEDDED_NAME_MAX;
157 recopy:
158 len = strncpy_from_user(kname, filename, max);
159 if (unlikely(len < 0)) {
160 err = ERR_PTR(len);
161 goto error;
165 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
166 * separate struct filename so we can dedicate the entire
167 * names_cache allocation for the pathname, and re-do the copy from
168 * userland.
170 if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
171 kname = (char *)result;
173 result = kzalloc(sizeof(*result), GFP_KERNEL);
174 if (!result) {
175 err = ERR_PTR(-ENOMEM);
176 result = (struct filename *)kname;
177 goto error;
179 result->name = kname;
180 result->separate = true;
181 max = PATH_MAX;
182 goto recopy;
185 /* The empty path is special. */
186 if (unlikely(!len)) {
187 if (empty)
188 *empty = 1;
189 err = ERR_PTR(-ENOENT);
190 if (!(flags & LOOKUP_EMPTY))
191 goto error;
194 err = ERR_PTR(-ENAMETOOLONG);
195 if (unlikely(len >= PATH_MAX))
196 goto error;
198 result->uptr = filename;
199 audit_getname(result);
200 return result;
202 error:
203 final_putname(result);
204 return err;
207 struct filename *
208 getname(const char __user * filename)
210 return getname_flags(filename, 0, NULL);
212 EXPORT_SYMBOL(getname);
214 #ifdef CONFIG_AUDITSYSCALL
215 void putname(struct filename *name)
217 if (unlikely(!audit_dummy_context()))
218 return audit_putname(name);
219 final_putname(name);
221 #endif
223 static int check_acl(struct inode *inode, int mask)
225 #ifdef CONFIG_FS_POSIX_ACL
226 struct posix_acl *acl;
228 if (mask & MAY_NOT_BLOCK) {
229 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
230 if (!acl)
231 return -EAGAIN;
232 /* no ->get_acl() calls in RCU mode... */
233 if (acl == ACL_NOT_CACHED)
234 return -ECHILD;
235 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
238 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
241 * A filesystem can force a ACL callback by just never filling the
242 * ACL cache. But normally you'd fill the cache either at inode
243 * instantiation time, or on the first ->get_acl call.
245 * If the filesystem doesn't have a get_acl() function at all, we'll
246 * just create the negative cache entry.
248 if (acl == ACL_NOT_CACHED) {
249 if (inode->i_op->get_acl) {
250 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
251 if (IS_ERR(acl))
252 return PTR_ERR(acl);
253 } else {
254 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
255 return -EAGAIN;
259 if (acl) {
260 int error = posix_acl_permission(inode, acl, mask);
261 posix_acl_release(acl);
262 return error;
264 #endif
266 return -EAGAIN;
270 * This does the basic permission checking
272 static int acl_permission_check(struct inode *inode, int mask)
274 unsigned int mode = inode->i_mode;
276 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
277 mode >>= 6;
278 else {
279 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
280 int error = check_acl(inode, mask);
281 if (error != -EAGAIN)
282 return error;
285 if (in_group_p(inode->i_gid))
286 mode >>= 3;
290 * If the DACs are ok we don't need any capability check.
292 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
293 return 0;
294 return -EACCES;
298 * generic_permission - check for access rights on a Posix-like filesystem
299 * @inode: inode to check access rights for
300 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
302 * Used to check for read/write/execute permissions on a file.
303 * We use "fsuid" for this, letting us set arbitrary permissions
304 * for filesystem access without changing the "normal" uids which
305 * are used for other things.
307 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
308 * request cannot be satisfied (eg. requires blocking or too much complexity).
309 * It would then be called again in ref-walk mode.
311 int generic_permission(struct inode *inode, int mask)
313 int ret;
316 * Do the basic permission checks.
318 ret = acl_permission_check(inode, mask);
319 if (ret != -EACCES)
320 return ret;
322 if (S_ISDIR(inode->i_mode)) {
323 /* DACs are overridable for directories */
324 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
325 return 0;
326 if (!(mask & MAY_WRITE))
327 if (capable_wrt_inode_uidgid(inode,
328 CAP_DAC_READ_SEARCH))
329 return 0;
330 return -EACCES;
333 * Read/write DACs are always overridable.
334 * Executable DACs are overridable when there is
335 * at least one exec bit set.
337 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
338 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
339 return 0;
342 * Searching includes executable on directories, else just read.
344 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
345 if (mask == MAY_READ)
346 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
347 return 0;
349 return -EACCES;
353 * We _really_ want to just do "generic_permission()" without
354 * even looking at the inode->i_op values. So we keep a cache
355 * flag in inode->i_opflags, that says "this has not special
356 * permission function, use the fast case".
358 static inline int do_inode_permission(struct inode *inode, int mask)
360 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
361 if (likely(inode->i_op->permission))
362 return inode->i_op->permission(inode, mask);
364 /* This gets set once for the inode lifetime */
365 spin_lock(&inode->i_lock);
366 inode->i_opflags |= IOP_FASTPERM;
367 spin_unlock(&inode->i_lock);
369 return generic_permission(inode, mask);
373 * __inode_permission - Check for access rights to a given inode
374 * @inode: Inode to check permission on
375 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
377 * Check for read/write/execute permissions on an inode.
379 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
381 * This does not check for a read-only file system. You probably want
382 * inode_permission().
384 int __inode_permission(struct inode *inode, int mask)
386 int retval;
388 if (unlikely(mask & MAY_WRITE)) {
390 * Nobody gets write access to an immutable file.
392 if (IS_IMMUTABLE(inode))
393 return -EACCES;
396 retval = do_inode_permission(inode, mask);
397 if (retval)
398 return retval;
400 retval = devcgroup_inode_permission(inode, mask);
401 if (retval)
402 return retval;
404 return security_inode_permission(inode, mask);
408 * sb_permission - Check superblock-level permissions
409 * @sb: Superblock of inode to check permission on
410 * @inode: Inode to check permission on
411 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
413 * Separate out file-system wide checks from inode-specific permission checks.
415 static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
417 if (unlikely(mask & MAY_WRITE)) {
418 umode_t mode = inode->i_mode;
420 /* Nobody gets write access to a read-only fs. */
421 if ((sb->s_flags & MS_RDONLY) &&
422 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
423 return -EROFS;
425 return 0;
429 * inode_permission - Check for access rights to a given inode
430 * @inode: Inode to check permission on
431 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
433 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
434 * this, letting us set arbitrary permissions for filesystem access without
435 * changing the "normal" UIDs which are used for other things.
437 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
439 int inode_permission(struct inode *inode, int mask)
441 int retval;
443 retval = sb_permission(inode->i_sb, inode, mask);
444 if (retval)
445 return retval;
446 return __inode_permission(inode, mask);
450 * path_get - get a reference to a path
451 * @path: path to get the reference to
453 * Given a path increment the reference count to the dentry and the vfsmount.
455 void path_get(const struct path *path)
457 mntget(path->mnt);
458 dget(path->dentry);
460 EXPORT_SYMBOL(path_get);
463 * path_put - put a reference to a path
464 * @path: path to put the reference to
466 * Given a path decrement the reference count to the dentry and the vfsmount.
468 void path_put(const struct path *path)
470 dput(path->dentry);
471 mntput(path->mnt);
473 EXPORT_SYMBOL(path_put);
476 * Path walking has 2 modes, rcu-walk and ref-walk (see
477 * Documentation/filesystems/path-lookup.txt). In situations when we can't
478 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
479 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
480 * mode. Refcounts are grabbed at the last known good point before rcu-walk
481 * got stuck, so ref-walk may continue from there. If this is not successful
482 * (eg. a seqcount has changed), then failure is returned and it's up to caller
483 * to restart the path walk from the beginning in ref-walk mode.
486 static inline void lock_rcu_walk(void)
488 br_read_lock(&vfsmount_lock);
489 rcu_read_lock();
492 static inline void unlock_rcu_walk(void)
494 rcu_read_unlock();
495 br_read_unlock(&vfsmount_lock);
499 * unlazy_walk - try to switch to ref-walk mode.
500 * @nd: nameidata pathwalk data
501 * @dentry: child of nd->path.dentry or NULL
502 * Returns: 0 on success, -ECHILD on failure
504 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
505 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
506 * @nd or NULL. Must be called from rcu-walk context.
508 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
510 struct fs_struct *fs = current->fs;
511 struct dentry *parent = nd->path.dentry;
513 BUG_ON(!(nd->flags & LOOKUP_RCU));
516 * Get a reference to the parent first: we're
517 * going to make "path_put(nd->path)" valid in
518 * non-RCU context for "terminate_walk()".
520 * If this doesn't work, return immediately with
521 * RCU walking still active (and then we will do
522 * the RCU walk cleanup in terminate_walk()).
524 if (!lockref_get_not_dead(&parent->d_lockref))
525 return -ECHILD;
528 * After the mntget(), we terminate_walk() will do
529 * the right thing for non-RCU mode, and all our
530 * subsequent exit cases should unlock_rcu_walk()
531 * before returning.
533 mntget(nd->path.mnt);
534 nd->flags &= ~LOOKUP_RCU;
537 * For a negative lookup, the lookup sequence point is the parents
538 * sequence point, and it only needs to revalidate the parent dentry.
540 * For a positive lookup, we need to move both the parent and the
541 * dentry from the RCU domain to be properly refcounted. And the
542 * sequence number in the dentry validates *both* dentry counters,
543 * since we checked the sequence number of the parent after we got
544 * the child sequence number. So we know the parent must still
545 * be valid if the child sequence number is still valid.
547 if (!dentry) {
548 if (read_seqcount_retry(&parent->d_seq, nd->seq))
549 goto out;
550 BUG_ON(nd->inode != parent->d_inode);
551 } else {
552 if (!lockref_get_not_dead(&dentry->d_lockref))
553 goto out;
554 if (read_seqcount_retry(&dentry->d_seq, nd->seq))
555 goto drop_dentry;
559 * Sequence counts matched. Now make sure that the root is
560 * still valid and get it if required.
562 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
563 spin_lock(&fs->lock);
564 if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
565 goto unlock_and_drop_dentry;
566 path_get(&nd->root);
567 spin_unlock(&fs->lock);
570 unlock_rcu_walk();
571 return 0;
573 unlock_and_drop_dentry:
574 spin_unlock(&fs->lock);
575 drop_dentry:
576 unlock_rcu_walk();
577 dput(dentry);
578 goto drop_root_mnt;
579 out:
580 unlock_rcu_walk();
581 drop_root_mnt:
582 if (!(nd->flags & LOOKUP_ROOT))
583 nd->root.mnt = NULL;
584 return -ECHILD;
587 static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
589 return dentry->d_op->d_revalidate(dentry, flags);
593 * complete_walk - successful completion of path walk
594 * @nd: pointer nameidata
596 * If we had been in RCU mode, drop out of it and legitimize nd->path.
597 * Revalidate the final result, unless we'd already done that during
598 * the path walk or the filesystem doesn't ask for it. Return 0 on
599 * success, -error on failure. In case of failure caller does not
600 * need to drop nd->path.
602 static int complete_walk(struct nameidata *nd)
604 struct dentry *dentry = nd->path.dentry;
605 int status;
607 if (nd->flags & LOOKUP_RCU) {
608 nd->flags &= ~LOOKUP_RCU;
609 if (!(nd->flags & LOOKUP_ROOT))
610 nd->root.mnt = NULL;
612 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
613 unlock_rcu_walk();
614 return -ECHILD;
616 if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
617 unlock_rcu_walk();
618 dput(dentry);
619 return -ECHILD;
621 mntget(nd->path.mnt);
622 unlock_rcu_walk();
625 if (likely(!(nd->flags & LOOKUP_JUMPED)))
626 return 0;
628 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
629 return 0;
631 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
632 if (status > 0)
633 return 0;
635 if (!status)
636 status = -ESTALE;
638 path_put(&nd->path);
639 return status;
642 static __always_inline void set_root(struct nameidata *nd)
644 if (!nd->root.mnt)
645 get_fs_root(current->fs, &nd->root);
648 static int link_path_walk(const char *, struct nameidata *);
650 static __always_inline void set_root_rcu(struct nameidata *nd)
652 if (!nd->root.mnt) {
653 struct fs_struct *fs = current->fs;
654 unsigned seq;
656 do {
657 seq = read_seqcount_begin(&fs->seq);
658 nd->root = fs->root;
659 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
660 } while (read_seqcount_retry(&fs->seq, seq));
664 static void path_put_conditional(struct path *path, struct nameidata *nd)
666 dput(path->dentry);
667 if (path->mnt != nd->path.mnt)
668 mntput(path->mnt);
671 static inline void path_to_nameidata(const struct path *path,
672 struct nameidata *nd)
674 if (!(nd->flags & LOOKUP_RCU)) {
675 dput(nd->path.dentry);
676 if (nd->path.mnt != path->mnt)
677 mntput(nd->path.mnt);
679 nd->path.mnt = path->mnt;
680 nd->path.dentry = path->dentry;
684 * Helper to directly jump to a known parsed path from ->follow_link,
685 * caller must have taken a reference to path beforehand.
687 void nd_jump_link(struct nameidata *nd, struct path *path)
689 path_put(&nd->path);
691 nd->path = *path;
692 nd->inode = nd->path.dentry->d_inode;
693 nd->flags |= LOOKUP_JUMPED;
696 static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
698 struct inode *inode = link->dentry->d_inode;
699 if (inode->i_op->put_link)
700 inode->i_op->put_link(link->dentry, nd, cookie);
701 path_put(link);
704 int sysctl_protected_symlinks __read_mostly = 0;
705 int sysctl_protected_hardlinks __read_mostly = 0;
708 * may_follow_link - Check symlink following for unsafe situations
709 * @link: The path of the symlink
710 * @nd: nameidata pathwalk data
712 * In the case of the sysctl_protected_symlinks sysctl being enabled,
713 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
714 * in a sticky world-writable directory. This is to protect privileged
715 * processes from failing races against path names that may change out
716 * from under them by way of other users creating malicious symlinks.
717 * It will permit symlinks to be followed only when outside a sticky
718 * world-writable directory, or when the uid of the symlink and follower
719 * match, or when the directory owner matches the symlink's owner.
721 * Returns 0 if following the symlink is allowed, -ve on error.
723 static inline int may_follow_link(struct path *link, struct nameidata *nd)
725 const struct inode *inode;
726 const struct inode *parent;
728 if (!sysctl_protected_symlinks)
729 return 0;
731 /* Allowed if owner and follower match. */
732 inode = link->dentry->d_inode;
733 if (uid_eq(current_cred()->fsuid, inode->i_uid))
734 return 0;
736 /* Allowed if parent directory not sticky and world-writable. */
737 parent = nd->path.dentry->d_inode;
738 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
739 return 0;
741 /* Allowed if parent directory and link owner match. */
742 if (uid_eq(parent->i_uid, inode->i_uid))
743 return 0;
745 audit_log_link_denied("follow_link", link);
746 path_put_conditional(link, nd);
747 path_put(&nd->path);
748 return -EACCES;
752 * safe_hardlink_source - Check for safe hardlink conditions
753 * @inode: the source inode to hardlink from
755 * Return false if at least one of the following conditions:
756 * - inode is not a regular file
757 * - inode is setuid
758 * - inode is setgid and group-exec
759 * - access failure for read and write
761 * Otherwise returns true.
763 static bool safe_hardlink_source(struct inode *inode)
765 umode_t mode = inode->i_mode;
767 /* Special files should not get pinned to the filesystem. */
768 if (!S_ISREG(mode))
769 return false;
771 /* Setuid files should not get pinned to the filesystem. */
772 if (mode & S_ISUID)
773 return false;
775 /* Executable setgid files should not get pinned to the filesystem. */
776 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
777 return false;
779 /* Hardlinking to unreadable or unwritable sources is dangerous. */
780 if (inode_permission(inode, MAY_READ | MAY_WRITE))
781 return false;
783 return true;
787 * may_linkat - Check permissions for creating a hardlink
788 * @link: the source to hardlink from
790 * Block hardlink when all of:
791 * - sysctl_protected_hardlinks enabled
792 * - fsuid does not match inode
793 * - hardlink source is unsafe (see safe_hardlink_source() above)
794 * - not CAP_FOWNER
796 * Returns 0 if successful, -ve on error.
798 static int may_linkat(struct path *link)
800 const struct cred *cred;
801 struct inode *inode;
803 if (!sysctl_protected_hardlinks)
804 return 0;
806 cred = current_cred();
807 inode = link->dentry->d_inode;
809 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
810 * otherwise, it must be a safe source.
812 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
813 capable(CAP_FOWNER))
814 return 0;
816 audit_log_link_denied("linkat", link);
817 return -EPERM;
820 static __always_inline int
821 follow_link(struct path *link, struct nameidata *nd, void **p)
823 struct dentry *dentry = link->dentry;
824 int error;
825 char *s;
827 BUG_ON(nd->flags & LOOKUP_RCU);
829 if (link->mnt == nd->path.mnt)
830 mntget(link->mnt);
832 error = -ELOOP;
833 if (unlikely(current->total_link_count >= 40))
834 goto out_put_nd_path;
836 cond_resched();
837 current->total_link_count++;
839 touch_atime(link);
840 nd_set_link(nd, NULL);
842 error = security_inode_follow_link(link->dentry, nd);
843 if (error)
844 goto out_put_nd_path;
846 nd->last_type = LAST_BIND;
847 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
848 error = PTR_ERR(*p);
849 if (IS_ERR(*p))
850 goto out_put_nd_path;
852 error = 0;
853 s = nd_get_link(nd);
854 if (s) {
855 if (unlikely(IS_ERR(s))) {
856 path_put(&nd->path);
857 put_link(nd, link, *p);
858 return PTR_ERR(s);
860 if (*s == '/') {
861 set_root(nd);
862 path_put(&nd->path);
863 nd->path = nd->root;
864 path_get(&nd->root);
865 nd->flags |= LOOKUP_JUMPED;
867 nd->inode = nd->path.dentry->d_inode;
868 error = link_path_walk(s, nd);
869 if (unlikely(error))
870 put_link(nd, link, *p);
873 return error;
875 out_put_nd_path:
876 *p = NULL;
877 path_put(&nd->path);
878 path_put(link);
879 return error;
882 static int follow_up_rcu(struct path *path)
884 struct mount *mnt = real_mount(path->mnt);
885 struct mount *parent;
886 struct dentry *mountpoint;
888 parent = mnt->mnt_parent;
889 if (&parent->mnt == path->mnt)
890 return 0;
891 mountpoint = mnt->mnt_mountpoint;
892 path->dentry = mountpoint;
893 path->mnt = &parent->mnt;
894 return 1;
898 * follow_up - Find the mountpoint of path's vfsmount
900 * Given a path, find the mountpoint of its source file system.
901 * Replace @path with the path of the mountpoint in the parent mount.
902 * Up is towards /.
904 * Return 1 if we went up a level and 0 if we were already at the
905 * root.
907 int follow_up(struct path *path)
909 struct mount *mnt = real_mount(path->mnt);
910 struct mount *parent;
911 struct dentry *mountpoint;
913 br_read_lock(&vfsmount_lock);
914 parent = mnt->mnt_parent;
915 if (parent == mnt) {
916 br_read_unlock(&vfsmount_lock);
917 return 0;
919 mntget(&parent->mnt);
920 mountpoint = dget(mnt->mnt_mountpoint);
921 br_read_unlock(&vfsmount_lock);
922 dput(path->dentry);
923 path->dentry = mountpoint;
924 mntput(path->mnt);
925 path->mnt = &parent->mnt;
926 return 1;
930 * Perform an automount
931 * - return -EISDIR to tell follow_managed() to stop and return the path we
932 * were called with.
934 static int follow_automount(struct path *path, unsigned flags,
935 bool *need_mntput)
937 struct vfsmount *mnt;
938 int err;
940 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
941 return -EREMOTE;
943 /* We don't want to mount if someone's just doing a stat -
944 * unless they're stat'ing a directory and appended a '/' to
945 * the name.
947 * We do, however, want to mount if someone wants to open or
948 * create a file of any type under the mountpoint, wants to
949 * traverse through the mountpoint or wants to open the
950 * mounted directory. Also, autofs may mark negative dentries
951 * as being automount points. These will need the attentions
952 * of the daemon to instantiate them before they can be used.
954 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
955 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
956 path->dentry->d_inode)
957 return -EISDIR;
959 current->total_link_count++;
960 if (current->total_link_count >= 40)
961 return -ELOOP;
963 mnt = path->dentry->d_op->d_automount(path);
964 if (IS_ERR(mnt)) {
966 * The filesystem is allowed to return -EISDIR here to indicate
967 * it doesn't want to automount. For instance, autofs would do
968 * this so that its userspace daemon can mount on this dentry.
970 * However, we can only permit this if it's a terminal point in
971 * the path being looked up; if it wasn't then the remainder of
972 * the path is inaccessible and we should say so.
974 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
975 return -EREMOTE;
976 return PTR_ERR(mnt);
979 if (!mnt) /* mount collision */
980 return 0;
982 if (!*need_mntput) {
983 /* lock_mount() may release path->mnt on error */
984 mntget(path->mnt);
985 *need_mntput = true;
987 err = finish_automount(mnt, path);
989 switch (err) {
990 case -EBUSY:
991 /* Someone else made a mount here whilst we were busy */
992 return 0;
993 case 0:
994 path_put(path);
995 path->mnt = mnt;
996 path->dentry = dget(mnt->mnt_root);
997 return 0;
998 default:
999 return err;
1005 * Handle a dentry that is managed in some way.
1006 * - Flagged for transit management (autofs)
1007 * - Flagged as mountpoint
1008 * - Flagged as automount point
1010 * This may only be called in refwalk mode.
1012 * Serialization is taken care of in namespace.c
1014 static int follow_managed(struct path *path, unsigned flags)
1016 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
1017 unsigned managed;
1018 bool need_mntput = false;
1019 int ret = 0;
1021 /* Given that we're not holding a lock here, we retain the value in a
1022 * local variable for each dentry as we look at it so that we don't see
1023 * the components of that value change under us */
1024 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1025 managed &= DCACHE_MANAGED_DENTRY,
1026 unlikely(managed != 0)) {
1027 /* Allow the filesystem to manage the transit without i_mutex
1028 * being held. */
1029 if (managed & DCACHE_MANAGE_TRANSIT) {
1030 BUG_ON(!path->dentry->d_op);
1031 BUG_ON(!path->dentry->d_op->d_manage);
1032 ret = path->dentry->d_op->d_manage(path->dentry, false);
1033 if (ret < 0)
1034 break;
1037 /* Transit to a mounted filesystem. */
1038 if (managed & DCACHE_MOUNTED) {
1039 struct vfsmount *mounted = lookup_mnt(path);
1040 if (mounted) {
1041 dput(path->dentry);
1042 if (need_mntput)
1043 mntput(path->mnt);
1044 path->mnt = mounted;
1045 path->dentry = dget(mounted->mnt_root);
1046 need_mntput = true;
1047 continue;
1050 /* Something is mounted on this dentry in another
1051 * namespace and/or whatever was mounted there in this
1052 * namespace got unmounted before we managed to get the
1053 * vfsmount_lock */
1056 /* Handle an automount point */
1057 if (managed & DCACHE_NEED_AUTOMOUNT) {
1058 ret = follow_automount(path, flags, &need_mntput);
1059 if (ret < 0)
1060 break;
1061 continue;
1064 /* We didn't change the current path point */
1065 break;
1068 if (need_mntput && path->mnt == mnt)
1069 mntput(path->mnt);
1070 if (ret == -EISDIR)
1071 ret = 0;
1072 return ret < 0 ? ret : need_mntput;
1075 int follow_down_one(struct path *path)
1077 struct vfsmount *mounted;
1079 mounted = lookup_mnt(path);
1080 if (mounted) {
1081 dput(path->dentry);
1082 mntput(path->mnt);
1083 path->mnt = mounted;
1084 path->dentry = dget(mounted->mnt_root);
1085 return 1;
1087 return 0;
1090 static inline bool managed_dentry_might_block(struct dentry *dentry)
1092 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1093 dentry->d_op->d_manage(dentry, true) < 0);
1097 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1098 * we meet a managed dentry that would need blocking.
1100 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1101 struct inode **inode)
1103 for (;;) {
1104 struct mount *mounted;
1106 * Don't forget we might have a non-mountpoint managed dentry
1107 * that wants to block transit.
1109 if (unlikely(managed_dentry_might_block(path->dentry)))
1110 return false;
1112 if (!d_mountpoint(path->dentry))
1113 break;
1115 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1116 if (!mounted)
1117 break;
1118 path->mnt = &mounted->mnt;
1119 path->dentry = mounted->mnt.mnt_root;
1120 nd->flags |= LOOKUP_JUMPED;
1121 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
1123 * Update the inode too. We don't need to re-check the
1124 * dentry sequence number here after this d_inode read,
1125 * because a mount-point is always pinned.
1127 *inode = path->dentry->d_inode;
1129 return true;
1132 static void follow_mount_rcu(struct nameidata *nd)
1134 while (d_mountpoint(nd->path.dentry)) {
1135 struct mount *mounted;
1136 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
1137 if (!mounted)
1138 break;
1139 nd->path.mnt = &mounted->mnt;
1140 nd->path.dentry = mounted->mnt.mnt_root;
1141 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1145 static int follow_dotdot_rcu(struct nameidata *nd)
1147 set_root_rcu(nd);
1149 while (1) {
1150 if (nd->path.dentry == nd->root.dentry &&
1151 nd->path.mnt == nd->root.mnt) {
1152 break;
1154 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1155 struct dentry *old = nd->path.dentry;
1156 struct dentry *parent = old->d_parent;
1157 unsigned seq;
1159 seq = read_seqcount_begin(&parent->d_seq);
1160 if (read_seqcount_retry(&old->d_seq, nd->seq))
1161 goto failed;
1162 nd->path.dentry = parent;
1163 nd->seq = seq;
1164 break;
1166 if (!follow_up_rcu(&nd->path))
1167 break;
1168 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1170 follow_mount_rcu(nd);
1171 nd->inode = nd->path.dentry->d_inode;
1172 return 0;
1174 failed:
1175 nd->flags &= ~LOOKUP_RCU;
1176 if (!(nd->flags & LOOKUP_ROOT))
1177 nd->root.mnt = NULL;
1178 unlock_rcu_walk();
1179 return -ECHILD;
1183 * Follow down to the covering mount currently visible to userspace. At each
1184 * point, the filesystem owning that dentry may be queried as to whether the
1185 * caller is permitted to proceed or not.
1187 int follow_down(struct path *path)
1189 unsigned managed;
1190 int ret;
1192 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1193 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1194 /* Allow the filesystem to manage the transit without i_mutex
1195 * being held.
1197 * We indicate to the filesystem if someone is trying to mount
1198 * something here. This gives autofs the chance to deny anyone
1199 * other than its daemon the right to mount on its
1200 * superstructure.
1202 * The filesystem may sleep at this point.
1204 if (managed & DCACHE_MANAGE_TRANSIT) {
1205 BUG_ON(!path->dentry->d_op);
1206 BUG_ON(!path->dentry->d_op->d_manage);
1207 ret = path->dentry->d_op->d_manage(
1208 path->dentry, false);
1209 if (ret < 0)
1210 return ret == -EISDIR ? 0 : ret;
1213 /* Transit to a mounted filesystem. */
1214 if (managed & DCACHE_MOUNTED) {
1215 struct vfsmount *mounted = lookup_mnt(path);
1216 if (!mounted)
1217 break;
1218 dput(path->dentry);
1219 mntput(path->mnt);
1220 path->mnt = mounted;
1221 path->dentry = dget(mounted->mnt_root);
1222 continue;
1225 /* Don't handle automount points here */
1226 break;
1228 return 0;
1232 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1234 static void follow_mount(struct path *path)
1236 while (d_mountpoint(path->dentry)) {
1237 struct vfsmount *mounted = lookup_mnt(path);
1238 if (!mounted)
1239 break;
1240 dput(path->dentry);
1241 mntput(path->mnt);
1242 path->mnt = mounted;
1243 path->dentry = dget(mounted->mnt_root);
1247 static void follow_dotdot(struct nameidata *nd)
1249 set_root(nd);
1251 while(1) {
1252 struct dentry *old = nd->path.dentry;
1254 if (nd->path.dentry == nd->root.dentry &&
1255 nd->path.mnt == nd->root.mnt) {
1256 break;
1258 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1259 /* rare case of legitimate dget_parent()... */
1260 nd->path.dentry = dget_parent(nd->path.dentry);
1261 dput(old);
1262 break;
1264 if (!follow_up(&nd->path))
1265 break;
1267 follow_mount(&nd->path);
1268 nd->inode = nd->path.dentry->d_inode;
1272 * This looks up the name in dcache, possibly revalidates the old dentry and
1273 * allocates a new one if not found or not valid. In the need_lookup argument
1274 * returns whether i_op->lookup is necessary.
1276 * dir->d_inode->i_mutex must be held
1278 static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1279 unsigned int flags, bool *need_lookup)
1281 struct dentry *dentry;
1282 int error;
1284 *need_lookup = false;
1285 dentry = d_lookup(dir, name);
1286 if (dentry) {
1287 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1288 error = d_revalidate(dentry, flags);
1289 if (unlikely(error <= 0)) {
1290 if (error < 0) {
1291 dput(dentry);
1292 return ERR_PTR(error);
1293 } else if (!d_invalidate(dentry)) {
1294 dput(dentry);
1295 dentry = NULL;
1301 if (!dentry) {
1302 dentry = d_alloc(dir, name);
1303 if (unlikely(!dentry))
1304 return ERR_PTR(-ENOMEM);
1306 *need_lookup = true;
1308 return dentry;
1312 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1313 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1315 * dir->d_inode->i_mutex must be held
1317 static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1318 unsigned int flags)
1320 struct dentry *old;
1322 /* Don't create child dentry for a dead directory. */
1323 if (unlikely(IS_DEADDIR(dir))) {
1324 dput(dentry);
1325 return ERR_PTR(-ENOENT);
1328 old = dir->i_op->lookup(dir, dentry, flags);
1329 if (unlikely(old)) {
1330 dput(dentry);
1331 dentry = old;
1333 return dentry;
1336 static struct dentry *__lookup_hash(struct qstr *name,
1337 struct dentry *base, unsigned int flags)
1339 bool need_lookup;
1340 struct dentry *dentry;
1342 dentry = lookup_dcache(name, base, flags, &need_lookup);
1343 if (!need_lookup)
1344 return dentry;
1346 return lookup_real(base->d_inode, dentry, flags);
1350 * It's more convoluted than I'd like it to be, but... it's still fairly
1351 * small and for now I'd prefer to have fast path as straight as possible.
1352 * It _is_ time-critical.
1354 static int lookup_fast(struct nameidata *nd,
1355 struct path *path, struct inode **inode)
1357 struct vfsmount *mnt = nd->path.mnt;
1358 struct dentry *dentry, *parent = nd->path.dentry;
1359 int need_reval = 1;
1360 int status = 1;
1361 int err;
1364 * Rename seqlock is not required here because in the off chance
1365 * of a false negative due to a concurrent rename, we're going to
1366 * do the non-racy lookup, below.
1368 if (nd->flags & LOOKUP_RCU) {
1369 unsigned seq;
1370 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
1371 if (!dentry)
1372 goto unlazy;
1375 * This sequence count validates that the inode matches
1376 * the dentry name information from lookup.
1378 *inode = dentry->d_inode;
1379 if (read_seqcount_retry(&dentry->d_seq, seq))
1380 return -ECHILD;
1383 * This sequence count validates that the parent had no
1384 * changes while we did the lookup of the dentry above.
1386 * The memory barrier in read_seqcount_begin of child is
1387 * enough, we can use __read_seqcount_retry here.
1389 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1390 return -ECHILD;
1391 nd->seq = seq;
1393 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1394 status = d_revalidate(dentry, nd->flags);
1395 if (unlikely(status <= 0)) {
1396 if (status != -ECHILD)
1397 need_reval = 0;
1398 goto unlazy;
1401 path->mnt = mnt;
1402 path->dentry = dentry;
1403 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1404 goto unlazy;
1405 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1406 goto unlazy;
1407 return 0;
1408 unlazy:
1409 if (unlazy_walk(nd, dentry))
1410 return -ECHILD;
1411 } else {
1412 dentry = __d_lookup(parent, &nd->last);
1415 if (unlikely(!dentry))
1416 goto need_lookup;
1418 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1419 status = d_revalidate(dentry, nd->flags);
1420 if (unlikely(status <= 0)) {
1421 if (status < 0) {
1422 dput(dentry);
1423 return status;
1425 if (!d_invalidate(dentry)) {
1426 dput(dentry);
1427 goto need_lookup;
1431 path->mnt = mnt;
1432 path->dentry = dentry;
1433 err = follow_managed(path, nd->flags);
1434 if (unlikely(err < 0)) {
1435 path_put_conditional(path, nd);
1436 return err;
1438 if (err)
1439 nd->flags |= LOOKUP_JUMPED;
1440 *inode = path->dentry->d_inode;
1441 return 0;
1443 need_lookup:
1444 return 1;
1447 /* Fast lookup failed, do it the slow way */
1448 static int lookup_slow(struct nameidata *nd, struct path *path)
1450 struct dentry *dentry, *parent;
1451 int err;
1453 parent = nd->path.dentry;
1454 BUG_ON(nd->inode != parent->d_inode);
1456 mutex_lock(&parent->d_inode->i_mutex);
1457 dentry = __lookup_hash(&nd->last, parent, nd->flags);
1458 mutex_unlock(&parent->d_inode->i_mutex);
1459 if (IS_ERR(dentry))
1460 return PTR_ERR(dentry);
1461 path->mnt = nd->path.mnt;
1462 path->dentry = dentry;
1463 err = follow_managed(path, nd->flags);
1464 if (unlikely(err < 0)) {
1465 path_put_conditional(path, nd);
1466 return err;
1468 if (err)
1469 nd->flags |= LOOKUP_JUMPED;
1470 return 0;
1473 static inline int may_lookup(struct nameidata *nd)
1475 if (nd->flags & LOOKUP_RCU) {
1476 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1477 if (err != -ECHILD)
1478 return err;
1479 if (unlazy_walk(nd, NULL))
1480 return -ECHILD;
1482 return inode_permission(nd->inode, MAY_EXEC);
1485 static inline int handle_dots(struct nameidata *nd, int type)
1487 if (type == LAST_DOTDOT) {
1488 if (nd->flags & LOOKUP_RCU) {
1489 if (follow_dotdot_rcu(nd))
1490 return -ECHILD;
1491 } else
1492 follow_dotdot(nd);
1494 return 0;
1497 static void terminate_walk(struct nameidata *nd)
1499 if (!(nd->flags & LOOKUP_RCU)) {
1500 path_put(&nd->path);
1501 } else {
1502 nd->flags &= ~LOOKUP_RCU;
1503 if (!(nd->flags & LOOKUP_ROOT))
1504 nd->root.mnt = NULL;
1505 unlock_rcu_walk();
1510 * Do we need to follow links? We _really_ want to be able
1511 * to do this check without having to look at inode->i_op,
1512 * so we keep a cache of "no, this doesn't need follow_link"
1513 * for the common case.
1515 static inline int should_follow_link(struct inode *inode, int follow)
1517 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1518 if (likely(inode->i_op->follow_link))
1519 return follow;
1521 /* This gets set once for the inode lifetime */
1522 spin_lock(&inode->i_lock);
1523 inode->i_opflags |= IOP_NOFOLLOW;
1524 spin_unlock(&inode->i_lock);
1526 return 0;
1529 static inline int walk_component(struct nameidata *nd, struct path *path,
1530 int follow)
1532 struct inode *inode;
1533 int err;
1535 * "." and ".." are special - ".." especially so because it has
1536 * to be able to know about the current root directory and
1537 * parent relationships.
1539 if (unlikely(nd->last_type != LAST_NORM))
1540 return handle_dots(nd, nd->last_type);
1541 err = lookup_fast(nd, path, &inode);
1542 if (unlikely(err)) {
1543 if (err < 0)
1544 goto out_err;
1546 err = lookup_slow(nd, path);
1547 if (err < 0)
1548 goto out_err;
1550 inode = path->dentry->d_inode;
1552 err = -ENOENT;
1553 if (!inode)
1554 goto out_path_put;
1556 if (should_follow_link(inode, follow)) {
1557 if (nd->flags & LOOKUP_RCU) {
1558 if (unlikely(unlazy_walk(nd, path->dentry))) {
1559 err = -ECHILD;
1560 goto out_err;
1563 BUG_ON(inode != path->dentry->d_inode);
1564 return 1;
1566 path_to_nameidata(path, nd);
1567 nd->inode = inode;
1568 return 0;
1570 out_path_put:
1571 path_to_nameidata(path, nd);
1572 out_err:
1573 terminate_walk(nd);
1574 return err;
1578 * This limits recursive symlink follows to 8, while
1579 * limiting consecutive symlinks to 40.
1581 * Without that kind of total limit, nasty chains of consecutive
1582 * symlinks can cause almost arbitrarily long lookups.
1584 static inline int nested_symlink(struct path *path, struct nameidata *nd)
1586 int res;
1588 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1589 path_put_conditional(path, nd);
1590 path_put(&nd->path);
1591 return -ELOOP;
1593 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1595 nd->depth++;
1596 current->link_count++;
1598 do {
1599 struct path link = *path;
1600 void *cookie;
1602 res = follow_link(&link, nd, &cookie);
1603 if (res)
1604 break;
1605 res = walk_component(nd, path, LOOKUP_FOLLOW);
1606 put_link(nd, &link, cookie);
1607 } while (res > 0);
1609 current->link_count--;
1610 nd->depth--;
1611 return res;
1615 * We really don't want to look at inode->i_op->lookup
1616 * when we don't have to. So we keep a cache bit in
1617 * the inode ->i_opflags field that says "yes, we can
1618 * do lookup on this inode".
1620 static inline int can_lookup(struct inode *inode)
1622 if (likely(inode->i_opflags & IOP_LOOKUP))
1623 return 1;
1624 if (likely(!inode->i_op->lookup))
1625 return 0;
1627 /* We do this once for the lifetime of the inode */
1628 spin_lock(&inode->i_lock);
1629 inode->i_opflags |= IOP_LOOKUP;
1630 spin_unlock(&inode->i_lock);
1631 return 1;
1635 * We can do the critical dentry name comparison and hashing
1636 * operations one word at a time, but we are limited to:
1638 * - Architectures with fast unaligned word accesses. We could
1639 * do a "get_unaligned()" if this helps and is sufficiently
1640 * fast.
1642 * - Little-endian machines (so that we can generate the mask
1643 * of low bytes efficiently). Again, we *could* do a byte
1644 * swapping load on big-endian architectures if that is not
1645 * expensive enough to make the optimization worthless.
1647 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1648 * do not trap on the (extremely unlikely) case of a page
1649 * crossing operation.
1651 * - Furthermore, we need an efficient 64-bit compile for the
1652 * 64-bit case in order to generate the "number of bytes in
1653 * the final mask". Again, that could be replaced with a
1654 * efficient population count instruction or similar.
1656 #ifdef CONFIG_DCACHE_WORD_ACCESS
1658 #include <asm/word-at-a-time.h>
1660 #ifdef CONFIG_64BIT
1662 static inline unsigned int fold_hash(unsigned long hash)
1664 hash += hash >> (8*sizeof(int));
1665 return hash;
1668 #else /* 32-bit case */
1670 #define fold_hash(x) (x)
1672 #endif
1674 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1676 unsigned long a, mask;
1677 unsigned long hash = 0;
1679 for (;;) {
1680 a = load_unaligned_zeropad(name);
1681 if (len < sizeof(unsigned long))
1682 break;
1683 hash += a;
1684 hash *= 9;
1685 name += sizeof(unsigned long);
1686 len -= sizeof(unsigned long);
1687 if (!len)
1688 goto done;
1690 mask = ~(~0ul << len*8);
1691 hash += mask & a;
1692 done:
1693 return fold_hash(hash);
1695 EXPORT_SYMBOL(full_name_hash);
1698 * Calculate the length and hash of the path component, and
1699 * return the length of the component;
1701 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1703 unsigned long a, b, adata, bdata, mask, hash, len;
1704 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1706 hash = a = 0;
1707 len = -sizeof(unsigned long);
1708 do {
1709 hash = (hash + a) * 9;
1710 len += sizeof(unsigned long);
1711 a = load_unaligned_zeropad(name+len);
1712 b = a ^ REPEAT_BYTE('/');
1713 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1715 adata = prep_zero_mask(a, adata, &constants);
1716 bdata = prep_zero_mask(b, bdata, &constants);
1718 mask = create_zero_mask(adata | bdata);
1720 hash += a & zero_bytemask(mask);
1721 *hashp = fold_hash(hash);
1723 return len + find_zero(mask);
1726 #else
1728 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1730 unsigned long hash = init_name_hash();
1731 while (len--)
1732 hash = partial_name_hash(*name++, hash);
1733 return end_name_hash(hash);
1735 EXPORT_SYMBOL(full_name_hash);
1738 * We know there's a real path component here of at least
1739 * one character.
1741 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1743 unsigned long hash = init_name_hash();
1744 unsigned long len = 0, c;
1746 c = (unsigned char)*name;
1747 do {
1748 len++;
1749 hash = partial_name_hash(c, hash);
1750 c = (unsigned char)name[len];
1751 } while (c && c != '/');
1752 *hashp = end_name_hash(hash);
1753 return len;
1756 #endif
1759 * Name resolution.
1760 * This is the basic name resolution function, turning a pathname into
1761 * the final dentry. We expect 'base' to be positive and a directory.
1763 * Returns 0 and nd will have valid dentry and mnt on success.
1764 * Returns error and drops reference to input namei data on failure.
1766 static int link_path_walk(const char *name, struct nameidata *nd)
1768 struct path next;
1769 int err;
1771 while (*name=='/')
1772 name++;
1773 if (!*name)
1774 return 0;
1776 /* At this point we know we have a real path component. */
1777 for(;;) {
1778 struct qstr this;
1779 long len;
1780 int type;
1782 err = may_lookup(nd);
1783 if (err)
1784 break;
1786 len = hash_name(name, &this.hash);
1787 this.name = name;
1788 this.len = len;
1790 type = LAST_NORM;
1791 if (name[0] == '.') switch (len) {
1792 case 2:
1793 if (name[1] == '.') {
1794 type = LAST_DOTDOT;
1795 nd->flags |= LOOKUP_JUMPED;
1797 break;
1798 case 1:
1799 type = LAST_DOT;
1801 if (likely(type == LAST_NORM)) {
1802 struct dentry *parent = nd->path.dentry;
1803 nd->flags &= ~LOOKUP_JUMPED;
1804 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1805 err = parent->d_op->d_hash(parent, &this);
1806 if (err < 0)
1807 break;
1811 nd->last = this;
1812 nd->last_type = type;
1814 if (!name[len])
1815 return 0;
1817 * If it wasn't NUL, we know it was '/'. Skip that
1818 * slash, and continue until no more slashes.
1820 do {
1821 len++;
1822 } while (unlikely(name[len] == '/'));
1823 if (!name[len])
1824 return 0;
1826 name += len;
1828 err = walk_component(nd, &next, LOOKUP_FOLLOW);
1829 if (err < 0)
1830 return err;
1832 if (err) {
1833 err = nested_symlink(&next, nd);
1834 if (err)
1835 return err;
1837 if (!can_lookup(nd->inode)) {
1838 err = -ENOTDIR;
1839 break;
1842 terminate_walk(nd);
1843 return err;
1846 static int path_init(int dfd, const char *name, unsigned int flags,
1847 struct nameidata *nd, struct file **fp)
1849 int retval = 0;
1851 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1852 nd->flags = flags | LOOKUP_JUMPED;
1853 nd->depth = 0;
1854 if (flags & LOOKUP_ROOT) {
1855 struct inode *inode = nd->root.dentry->d_inode;
1856 if (*name) {
1857 if (!can_lookup(inode))
1858 return -ENOTDIR;
1859 retval = inode_permission(inode, MAY_EXEC);
1860 if (retval)
1861 return retval;
1863 nd->path = nd->root;
1864 nd->inode = inode;
1865 if (flags & LOOKUP_RCU) {
1866 lock_rcu_walk();
1867 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1868 } else {
1869 path_get(&nd->path);
1871 return 0;
1874 nd->root.mnt = NULL;
1876 if (*name=='/') {
1877 if (flags & LOOKUP_RCU) {
1878 lock_rcu_walk();
1879 set_root_rcu(nd);
1880 } else {
1881 set_root(nd);
1882 path_get(&nd->root);
1884 nd->path = nd->root;
1885 } else if (dfd == AT_FDCWD) {
1886 if (flags & LOOKUP_RCU) {
1887 struct fs_struct *fs = current->fs;
1888 unsigned seq;
1890 lock_rcu_walk();
1892 do {
1893 seq = read_seqcount_begin(&fs->seq);
1894 nd->path = fs->pwd;
1895 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1896 } while (read_seqcount_retry(&fs->seq, seq));
1897 } else {
1898 get_fs_pwd(current->fs, &nd->path);
1900 } else {
1901 /* Caller must check execute permissions on the starting path component */
1902 struct fd f = fdget_raw(dfd);
1903 struct dentry *dentry;
1905 if (!f.file)
1906 return -EBADF;
1908 dentry = f.file->f_path.dentry;
1910 if (*name) {
1911 if (!can_lookup(dentry->d_inode)) {
1912 fdput(f);
1913 return -ENOTDIR;
1917 nd->path = f.file->f_path;
1918 if (flags & LOOKUP_RCU) {
1919 if (f.need_put)
1920 *fp = f.file;
1921 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1922 lock_rcu_walk();
1923 } else {
1924 path_get(&nd->path);
1925 fdput(f);
1929 nd->inode = nd->path.dentry->d_inode;
1930 return 0;
1933 static inline int lookup_last(struct nameidata *nd, struct path *path)
1935 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1936 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1938 nd->flags &= ~LOOKUP_PARENT;
1939 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
1942 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1943 static int path_lookupat(int dfd, const char *name,
1944 unsigned int flags, struct nameidata *nd)
1946 struct file *base = NULL;
1947 struct path path;
1948 int err;
1951 * Path walking is largely split up into 2 different synchronisation
1952 * schemes, rcu-walk and ref-walk (explained in
1953 * Documentation/filesystems/path-lookup.txt). These share much of the
1954 * path walk code, but some things particularly setup, cleanup, and
1955 * following mounts are sufficiently divergent that functions are
1956 * duplicated. Typically there is a function foo(), and its RCU
1957 * analogue, foo_rcu().
1959 * -ECHILD is the error number of choice (just to avoid clashes) that
1960 * is returned if some aspect of an rcu-walk fails. Such an error must
1961 * be handled by restarting a traditional ref-walk (which will always
1962 * be able to complete).
1964 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1966 if (unlikely(err))
1967 return err;
1969 current->total_link_count = 0;
1970 err = link_path_walk(name, nd);
1972 if (!err && !(flags & LOOKUP_PARENT)) {
1973 err = lookup_last(nd, &path);
1974 while (err > 0) {
1975 void *cookie;
1976 struct path link = path;
1977 err = may_follow_link(&link, nd);
1978 if (unlikely(err))
1979 break;
1980 nd->flags |= LOOKUP_PARENT;
1981 err = follow_link(&link, nd, &cookie);
1982 if (err)
1983 break;
1984 err = lookup_last(nd, &path);
1985 put_link(nd, &link, cookie);
1989 if (!err)
1990 err = complete_walk(nd);
1992 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1993 if (!can_lookup(nd->inode)) {
1994 path_put(&nd->path);
1995 err = -ENOTDIR;
1999 if (base)
2000 fput(base);
2002 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
2003 path_put(&nd->root);
2004 nd->root.mnt = NULL;
2006 return err;
2009 static int filename_lookup(int dfd, struct filename *name,
2010 unsigned int flags, struct nameidata *nd)
2012 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
2013 if (unlikely(retval == -ECHILD))
2014 retval = path_lookupat(dfd, name->name, flags, nd);
2015 if (unlikely(retval == -ESTALE))
2016 retval = path_lookupat(dfd, name->name,
2017 flags | LOOKUP_REVAL, nd);
2019 if (likely(!retval))
2020 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
2021 return retval;
2024 static int do_path_lookup(int dfd, const char *name,
2025 unsigned int flags, struct nameidata *nd)
2027 struct filename filename = { .name = name };
2029 return filename_lookup(dfd, &filename, flags, nd);
2032 /* does lookup, returns the object with parent locked */
2033 struct dentry *kern_path_locked(const char *name, struct path *path)
2035 struct nameidata nd;
2036 struct dentry *d;
2037 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2038 if (err)
2039 return ERR_PTR(err);
2040 if (nd.last_type != LAST_NORM) {
2041 path_put(&nd.path);
2042 return ERR_PTR(-EINVAL);
2044 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2045 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
2046 if (IS_ERR(d)) {
2047 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2048 path_put(&nd.path);
2049 return d;
2051 *path = nd.path;
2052 return d;
2055 int kern_path(const char *name, unsigned int flags, struct path *path)
2057 struct nameidata nd;
2058 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2059 if (!res)
2060 *path = nd.path;
2061 return res;
2065 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2066 * @dentry: pointer to dentry of the base directory
2067 * @mnt: pointer to vfs mount of the base directory
2068 * @name: pointer to file name
2069 * @flags: lookup flags
2070 * @path: pointer to struct path to fill
2072 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2073 const char *name, unsigned int flags,
2074 struct path *path)
2076 struct nameidata nd;
2077 int err;
2078 nd.root.dentry = dentry;
2079 nd.root.mnt = mnt;
2080 BUG_ON(flags & LOOKUP_PARENT);
2081 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2082 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2083 if (!err)
2084 *path = nd.path;
2085 return err;
2089 * Restricted form of lookup. Doesn't follow links, single-component only,
2090 * needs parent already locked. Doesn't follow mounts.
2091 * SMP-safe.
2093 static struct dentry *lookup_hash(struct nameidata *nd)
2095 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
2099 * lookup_one_len - filesystem helper to lookup single pathname component
2100 * @name: pathname component to lookup
2101 * @base: base directory to lookup from
2102 * @len: maximum length @len should be interpreted to
2104 * Note that this routine is purely a helper for filesystem usage and should
2105 * not be called by generic code. Also note that by using this function the
2106 * nameidata argument is passed to the filesystem methods and a filesystem
2107 * using this helper needs to be prepared for that.
2109 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2111 struct qstr this;
2112 unsigned int c;
2113 int err;
2115 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2117 this.name = name;
2118 this.len = len;
2119 this.hash = full_name_hash(name, len);
2120 if (!len)
2121 return ERR_PTR(-EACCES);
2123 if (unlikely(name[0] == '.')) {
2124 if (len < 2 || (len == 2 && name[1] == '.'))
2125 return ERR_PTR(-EACCES);
2128 while (len--) {
2129 c = *(const unsigned char *)name++;
2130 if (c == '/' || c == '\0')
2131 return ERR_PTR(-EACCES);
2134 * See if the low-level filesystem might want
2135 * to use its own hash..
2137 if (base->d_flags & DCACHE_OP_HASH) {
2138 int err = base->d_op->d_hash(base, &this);
2139 if (err < 0)
2140 return ERR_PTR(err);
2143 err = inode_permission(base->d_inode, MAY_EXEC);
2144 if (err)
2145 return ERR_PTR(err);
2147 return __lookup_hash(&this, base, 0);
2150 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2151 struct path *path, int *empty)
2153 struct nameidata nd;
2154 struct filename *tmp = getname_flags(name, flags, empty);
2155 int err = PTR_ERR(tmp);
2156 if (!IS_ERR(tmp)) {
2158 BUG_ON(flags & LOOKUP_PARENT);
2160 err = filename_lookup(dfd, tmp, flags, &nd);
2161 putname(tmp);
2162 if (!err)
2163 *path = nd.path;
2165 return err;
2168 int user_path_at(int dfd, const char __user *name, unsigned flags,
2169 struct path *path)
2171 return user_path_at_empty(dfd, name, flags, path, NULL);
2175 * NB: most callers don't do anything directly with the reference to the
2176 * to struct filename, but the nd->last pointer points into the name string
2177 * allocated by getname. So we must hold the reference to it until all
2178 * path-walking is complete.
2180 static struct filename *
2181 user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2182 unsigned int flags)
2184 struct filename *s = getname(path);
2185 int error;
2187 /* only LOOKUP_REVAL is allowed in extra flags */
2188 flags &= LOOKUP_REVAL;
2190 if (IS_ERR(s))
2191 return s;
2193 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
2194 if (error) {
2195 putname(s);
2196 return ERR_PTR(error);
2199 return s;
2203 * mountpoint_last - look up last component for umount
2204 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2205 * @path: pointer to container for result
2207 * This is a special lookup_last function just for umount. In this case, we
2208 * need to resolve the path without doing any revalidation.
2210 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2211 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2212 * in almost all cases, this lookup will be served out of the dcache. The only
2213 * cases where it won't are if nd->last refers to a symlink or the path is
2214 * bogus and it doesn't exist.
2216 * Returns:
2217 * -error: if there was an error during lookup. This includes -ENOENT if the
2218 * lookup found a negative dentry. The nd->path reference will also be
2219 * put in this case.
2221 * 0: if we successfully resolved nd->path and found it to not to be a
2222 * symlink that needs to be followed. "path" will also be populated.
2223 * The nd->path reference will also be put.
2225 * 1: if we successfully resolved nd->last and found it to be a symlink
2226 * that needs to be followed. "path" will be populated with the path
2227 * to the link, and nd->path will *not* be put.
2229 static int
2230 mountpoint_last(struct nameidata *nd, struct path *path)
2232 int error = 0;
2233 struct dentry *dentry;
2234 struct dentry *dir = nd->path.dentry;
2236 /* If we're in rcuwalk, drop out of it to handle last component */
2237 if (nd->flags & LOOKUP_RCU) {
2238 if (unlazy_walk(nd, NULL)) {
2239 error = -ECHILD;
2240 goto out;
2244 nd->flags &= ~LOOKUP_PARENT;
2246 if (unlikely(nd->last_type != LAST_NORM)) {
2247 error = handle_dots(nd, nd->last_type);
2248 if (error)
2249 goto out;
2250 dentry = dget(nd->path.dentry);
2251 goto done;
2254 mutex_lock(&dir->d_inode->i_mutex);
2255 dentry = d_lookup(dir, &nd->last);
2256 if (!dentry) {
2258 * No cached dentry. Mounted dentries are pinned in the cache,
2259 * so that means that this dentry is probably a symlink or the
2260 * path doesn't actually point to a mounted dentry.
2262 dentry = d_alloc(dir, &nd->last);
2263 if (!dentry) {
2264 error = -ENOMEM;
2265 mutex_unlock(&dir->d_inode->i_mutex);
2266 goto out;
2268 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
2269 error = PTR_ERR(dentry);
2270 if (IS_ERR(dentry)) {
2271 mutex_unlock(&dir->d_inode->i_mutex);
2272 goto out;
2275 mutex_unlock(&dir->d_inode->i_mutex);
2277 done:
2278 if (!dentry->d_inode) {
2279 error = -ENOENT;
2280 dput(dentry);
2281 goto out;
2283 path->dentry = dentry;
2284 path->mnt = nd->path.mnt;
2285 if (should_follow_link(dentry->d_inode, nd->flags & LOOKUP_FOLLOW))
2286 return 1;
2287 mntget(path->mnt);
2288 follow_mount(path);
2289 error = 0;
2290 out:
2291 terminate_walk(nd);
2292 return error;
2296 * path_mountpoint - look up a path to be umounted
2297 * @dfd: directory file descriptor to start walk from
2298 * @name: full pathname to walk
2299 * @path: pointer to container for result
2300 * @flags: lookup flags
2302 * Look up the given name, but don't attempt to revalidate the last component.
2303 * Returns 0 and "path" will be valid on success; Returns error otherwise.
2305 static int
2306 path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
2308 struct file *base = NULL;
2309 struct nameidata nd;
2310 int err;
2312 err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
2313 if (unlikely(err))
2314 return err;
2316 current->total_link_count = 0;
2317 err = link_path_walk(name, &nd);
2318 if (err)
2319 goto out;
2321 err = mountpoint_last(&nd, path);
2322 while (err > 0) {
2323 void *cookie;
2324 struct path link = *path;
2325 err = may_follow_link(&link, &nd);
2326 if (unlikely(err))
2327 break;
2328 nd.flags |= LOOKUP_PARENT;
2329 err = follow_link(&link, &nd, &cookie);
2330 if (err)
2331 break;
2332 err = mountpoint_last(&nd, path);
2333 put_link(&nd, &link, cookie);
2335 out:
2336 if (base)
2337 fput(base);
2339 if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
2340 path_put(&nd.root);
2342 return err;
2345 static int
2346 filename_mountpoint(int dfd, struct filename *s, struct path *path,
2347 unsigned int flags)
2349 int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
2350 if (unlikely(error == -ECHILD))
2351 error = path_mountpoint(dfd, s->name, path, flags);
2352 if (unlikely(error == -ESTALE))
2353 error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
2354 if (likely(!error))
2355 audit_inode(s, path->dentry, 0);
2356 return error;
2360 * user_path_mountpoint_at - lookup a path from userland in order to umount it
2361 * @dfd: directory file descriptor
2362 * @name: pathname from userland
2363 * @flags: lookup flags
2364 * @path: pointer to container to hold result
2366 * A umount is a special case for path walking. We're not actually interested
2367 * in the inode in this situation, and ESTALE errors can be a problem. We
2368 * simply want track down the dentry and vfsmount attached at the mountpoint
2369 * and avoid revalidating the last component.
2371 * Returns 0 and populates "path" on success.
2374 user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
2375 struct path *path)
2377 struct filename *s = getname(name);
2378 int error;
2379 if (IS_ERR(s))
2380 return PTR_ERR(s);
2381 error = filename_mountpoint(dfd, s, path, flags);
2382 putname(s);
2383 return error;
2387 kern_path_mountpoint(int dfd, const char *name, struct path *path,
2388 unsigned int flags)
2390 struct filename s = {.name = name};
2391 return filename_mountpoint(dfd, &s, path, flags);
2393 EXPORT_SYMBOL(kern_path_mountpoint);
2396 * It's inline, so penalty for filesystems that don't use sticky bit is
2397 * minimal.
2399 static inline int check_sticky(struct inode *dir, struct inode *inode)
2401 kuid_t fsuid = current_fsuid();
2403 if (!(dir->i_mode & S_ISVTX))
2404 return 0;
2405 if (uid_eq(inode->i_uid, fsuid))
2406 return 0;
2407 if (uid_eq(dir->i_uid, fsuid))
2408 return 0;
2409 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
2413 * Check whether we can remove a link victim from directory dir, check
2414 * whether the type of victim is right.
2415 * 1. We can't do it if dir is read-only (done in permission())
2416 * 2. We should have write and exec permissions on dir
2417 * 3. We can't remove anything from append-only dir
2418 * 4. We can't do anything with immutable dir (done in permission())
2419 * 5. If the sticky bit on dir is set we should either
2420 * a. be owner of dir, or
2421 * b. be owner of victim, or
2422 * c. have CAP_FOWNER capability
2423 * 6. If the victim is append-only or immutable we can't do antyhing with
2424 * links pointing to it.
2425 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2426 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2427 * 9. We can't remove a root or mountpoint.
2428 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2429 * nfs_async_unlink().
2431 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
2433 int error;
2435 if (!victim->d_inode)
2436 return -ENOENT;
2438 BUG_ON(victim->d_parent->d_inode != dir);
2439 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
2441 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
2442 if (error)
2443 return error;
2444 if (IS_APPEND(dir))
2445 return -EPERM;
2446 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2447 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
2448 return -EPERM;
2449 if (isdir) {
2450 if (!S_ISDIR(victim->d_inode->i_mode))
2451 return -ENOTDIR;
2452 if (IS_ROOT(victim))
2453 return -EBUSY;
2454 } else if (S_ISDIR(victim->d_inode->i_mode))
2455 return -EISDIR;
2456 if (IS_DEADDIR(dir))
2457 return -ENOENT;
2458 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2459 return -EBUSY;
2460 return 0;
2463 /* Check whether we can create an object with dentry child in directory
2464 * dir.
2465 * 1. We can't do it if child already exists (open has special treatment for
2466 * this case, but since we are inlined it's OK)
2467 * 2. We can't do it if dir is read-only (done in permission())
2468 * 3. We should have write and exec permissions on dir
2469 * 4. We can't do it if dir is immutable (done in permission())
2471 static inline int may_create(struct inode *dir, struct dentry *child)
2473 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
2474 if (child->d_inode)
2475 return -EEXIST;
2476 if (IS_DEADDIR(dir))
2477 return -ENOENT;
2478 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2482 * p1 and p2 should be directories on the same fs.
2484 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2486 struct dentry *p;
2488 if (p1 == p2) {
2489 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2490 return NULL;
2493 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2495 p = d_ancestor(p2, p1);
2496 if (p) {
2497 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2498 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2499 return p;
2502 p = d_ancestor(p1, p2);
2503 if (p) {
2504 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2505 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2506 return p;
2509 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2510 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2511 return NULL;
2514 void unlock_rename(struct dentry *p1, struct dentry *p2)
2516 mutex_unlock(&p1->d_inode->i_mutex);
2517 if (p1 != p2) {
2518 mutex_unlock(&p2->d_inode->i_mutex);
2519 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2523 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2524 bool want_excl)
2526 int error = may_create(dir, dentry);
2527 if (error)
2528 return error;
2530 if (!dir->i_op->create)
2531 return -EACCES; /* shouldn't it be ENOSYS? */
2532 mode &= S_IALLUGO;
2533 mode |= S_IFREG;
2534 error = security_inode_create(dir, dentry, mode);
2535 if (error)
2536 return error;
2537 error = dir->i_op->create(dir, dentry, mode, want_excl);
2538 if (!error)
2539 fsnotify_create(dir, dentry);
2540 return error;
2543 static int may_open(struct path *path, int acc_mode, int flag)
2545 struct dentry *dentry = path->dentry;
2546 struct inode *inode = dentry->d_inode;
2547 int error;
2549 /* O_PATH? */
2550 if (!acc_mode)
2551 return 0;
2553 if (!inode)
2554 return -ENOENT;
2556 switch (inode->i_mode & S_IFMT) {
2557 case S_IFLNK:
2558 return -ELOOP;
2559 case S_IFDIR:
2560 if (acc_mode & MAY_WRITE)
2561 return -EISDIR;
2562 break;
2563 case S_IFBLK:
2564 case S_IFCHR:
2565 if (path->mnt->mnt_flags & MNT_NODEV)
2566 return -EACCES;
2567 /*FALLTHRU*/
2568 case S_IFIFO:
2569 case S_IFSOCK:
2570 flag &= ~O_TRUNC;
2571 break;
2574 error = inode_permission(inode, acc_mode);
2575 if (error)
2576 return error;
2579 * An append-only file must be opened in append mode for writing.
2581 if (IS_APPEND(inode)) {
2582 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2583 return -EPERM;
2584 if (flag & O_TRUNC)
2585 return -EPERM;
2588 /* O_NOATIME can only be set by the owner or superuser */
2589 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2590 return -EPERM;
2592 return 0;
2595 static int handle_truncate(struct file *filp)
2597 struct path *path = &filp->f_path;
2598 struct inode *inode = path->dentry->d_inode;
2599 int error = get_write_access(inode);
2600 if (error)
2601 return error;
2603 * Refuse to truncate files with mandatory locks held on them.
2605 error = locks_verify_locked(inode);
2606 if (!error)
2607 error = security_path_truncate(path);
2608 if (!error) {
2609 error = do_truncate(path->dentry, 0,
2610 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2611 filp);
2613 put_write_access(inode);
2614 return error;
2617 static inline int open_to_namei_flags(int flag)
2619 if ((flag & O_ACCMODE) == 3)
2620 flag--;
2621 return flag;
2624 static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2626 int error = security_path_mknod(dir, dentry, mode, 0);
2627 if (error)
2628 return error;
2630 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2631 if (error)
2632 return error;
2634 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2638 * Attempt to atomically look up, create and open a file from a negative
2639 * dentry.
2641 * Returns 0 if successful. The file will have been created and attached to
2642 * @file by the filesystem calling finish_open().
2644 * Returns 1 if the file was looked up only or didn't need creating. The
2645 * caller will need to perform the open themselves. @path will have been
2646 * updated to point to the new dentry. This may be negative.
2648 * Returns an error code otherwise.
2650 static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2651 struct path *path, struct file *file,
2652 const struct open_flags *op,
2653 bool got_write, bool need_lookup,
2654 int *opened)
2656 struct inode *dir = nd->path.dentry->d_inode;
2657 unsigned open_flag = open_to_namei_flags(op->open_flag);
2658 umode_t mode;
2659 int error;
2660 int acc_mode;
2661 int create_error = 0;
2662 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2663 bool excl;
2665 BUG_ON(dentry->d_inode);
2667 /* Don't create child dentry for a dead directory. */
2668 if (unlikely(IS_DEADDIR(dir))) {
2669 error = -ENOENT;
2670 goto out;
2673 mode = op->mode;
2674 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2675 mode &= ~current_umask();
2677 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2678 if (excl)
2679 open_flag &= ~O_TRUNC;
2682 * Checking write permission is tricky, bacuse we don't know if we are
2683 * going to actually need it: O_CREAT opens should work as long as the
2684 * file exists. But checking existence breaks atomicity. The trick is
2685 * to check access and if not granted clear O_CREAT from the flags.
2687 * Another problem is returing the "right" error value (e.g. for an
2688 * O_EXCL open we want to return EEXIST not EROFS).
2690 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2691 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2692 if (!(open_flag & O_CREAT)) {
2694 * No O_CREATE -> atomicity not a requirement -> fall
2695 * back to lookup + open
2697 goto no_open;
2698 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2699 /* Fall back and fail with the right error */
2700 create_error = -EROFS;
2701 goto no_open;
2702 } else {
2703 /* No side effects, safe to clear O_CREAT */
2704 create_error = -EROFS;
2705 open_flag &= ~O_CREAT;
2709 if (open_flag & O_CREAT) {
2710 error = may_o_create(&nd->path, dentry, mode);
2711 if (error) {
2712 create_error = error;
2713 if (open_flag & O_EXCL)
2714 goto no_open;
2715 open_flag &= ~O_CREAT;
2719 if (nd->flags & LOOKUP_DIRECTORY)
2720 open_flag |= O_DIRECTORY;
2722 file->f_path.dentry = DENTRY_NOT_SET;
2723 file->f_path.mnt = nd->path.mnt;
2724 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
2725 opened);
2726 if (error < 0) {
2727 if (create_error && error == -ENOENT)
2728 error = create_error;
2729 goto out;
2732 if (error) { /* returned 1, that is */
2733 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2734 error = -EIO;
2735 goto out;
2737 if (file->f_path.dentry) {
2738 dput(dentry);
2739 dentry = file->f_path.dentry;
2741 if (*opened & FILE_CREATED)
2742 fsnotify_create(dir, dentry);
2743 if (!dentry->d_inode) {
2744 WARN_ON(*opened & FILE_CREATED);
2745 if (create_error) {
2746 error = create_error;
2747 goto out;
2749 } else {
2750 if (excl && !(*opened & FILE_CREATED)) {
2751 error = -EEXIST;
2752 goto out;
2755 goto looked_up;
2759 * We didn't have the inode before the open, so check open permission
2760 * here.
2762 acc_mode = op->acc_mode;
2763 if (*opened & FILE_CREATED) {
2764 WARN_ON(!(open_flag & O_CREAT));
2765 fsnotify_create(dir, dentry);
2766 acc_mode = MAY_OPEN;
2768 error = may_open(&file->f_path, acc_mode, open_flag);
2769 if (error)
2770 fput(file);
2772 out:
2773 dput(dentry);
2774 return error;
2776 no_open:
2777 if (need_lookup) {
2778 dentry = lookup_real(dir, dentry, nd->flags);
2779 if (IS_ERR(dentry))
2780 return PTR_ERR(dentry);
2782 if (create_error) {
2783 int open_flag = op->open_flag;
2785 error = create_error;
2786 if ((open_flag & O_EXCL)) {
2787 if (!dentry->d_inode)
2788 goto out;
2789 } else if (!dentry->d_inode) {
2790 goto out;
2791 } else if ((open_flag & O_TRUNC) &&
2792 S_ISREG(dentry->d_inode->i_mode)) {
2793 goto out;
2795 /* will fail later, go on to get the right error */
2798 looked_up:
2799 path->dentry = dentry;
2800 path->mnt = nd->path.mnt;
2801 return 1;
2805 * Look up and maybe create and open the last component.
2807 * Must be called with i_mutex held on parent.
2809 * Returns 0 if the file was successfully atomically created (if necessary) and
2810 * opened. In this case the file will be returned attached to @file.
2812 * Returns 1 if the file was not completely opened at this time, though lookups
2813 * and creations will have been performed and the dentry returned in @path will
2814 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2815 * specified then a negative dentry may be returned.
2817 * An error code is returned otherwise.
2819 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2820 * cleared otherwise prior to returning.
2822 static int lookup_open(struct nameidata *nd, struct path *path,
2823 struct file *file,
2824 const struct open_flags *op,
2825 bool got_write, int *opened)
2827 struct dentry *dir = nd->path.dentry;
2828 struct inode *dir_inode = dir->d_inode;
2829 struct dentry *dentry;
2830 int error;
2831 bool need_lookup;
2833 *opened &= ~FILE_CREATED;
2834 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2835 if (IS_ERR(dentry))
2836 return PTR_ERR(dentry);
2838 /* Cached positive dentry: will open in f_op->open */
2839 if (!need_lookup && dentry->d_inode)
2840 goto out_no_open;
2842 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
2843 return atomic_open(nd, dentry, path, file, op, got_write,
2844 need_lookup, opened);
2847 if (need_lookup) {
2848 BUG_ON(dentry->d_inode);
2850 dentry = lookup_real(dir_inode, dentry, nd->flags);
2851 if (IS_ERR(dentry))
2852 return PTR_ERR(dentry);
2855 /* Negative dentry, just create the file */
2856 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2857 umode_t mode = op->mode;
2858 if (!IS_POSIXACL(dir->d_inode))
2859 mode &= ~current_umask();
2861 * This write is needed to ensure that a
2862 * rw->ro transition does not occur between
2863 * the time when the file is created and when
2864 * a permanent write count is taken through
2865 * the 'struct file' in finish_open().
2867 if (!got_write) {
2868 error = -EROFS;
2869 goto out_dput;
2871 *opened |= FILE_CREATED;
2872 error = security_path_mknod(&nd->path, dentry, mode, 0);
2873 if (error)
2874 goto out_dput;
2875 error = vfs_create(dir->d_inode, dentry, mode,
2876 nd->flags & LOOKUP_EXCL);
2877 if (error)
2878 goto out_dput;
2880 out_no_open:
2881 path->dentry = dentry;
2882 path->mnt = nd->path.mnt;
2883 return 1;
2885 out_dput:
2886 dput(dentry);
2887 return error;
2891 * Handle the last step of open()
2893 static int do_last(struct nameidata *nd, struct path *path,
2894 struct file *file, const struct open_flags *op,
2895 int *opened, struct filename *name)
2897 struct dentry *dir = nd->path.dentry;
2898 int open_flag = op->open_flag;
2899 bool will_truncate = (open_flag & O_TRUNC) != 0;
2900 bool got_write = false;
2901 int acc_mode = op->acc_mode;
2902 struct inode *inode;
2903 bool symlink_ok = false;
2904 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2905 bool retried = false;
2906 int error;
2908 nd->flags &= ~LOOKUP_PARENT;
2909 nd->flags |= op->intent;
2911 if (nd->last_type != LAST_NORM) {
2912 error = handle_dots(nd, nd->last_type);
2913 if (error)
2914 return error;
2915 goto finish_open;
2918 if (!(open_flag & O_CREAT)) {
2919 if (nd->last.name[nd->last.len])
2920 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2921 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2922 symlink_ok = true;
2923 /* we _can_ be in RCU mode here */
2924 error = lookup_fast(nd, path, &inode);
2925 if (likely(!error))
2926 goto finish_lookup;
2928 if (error < 0)
2929 goto out;
2931 BUG_ON(nd->inode != dir->d_inode);
2932 } else {
2933 /* create side of things */
2935 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2936 * has been cleared when we got to the last component we are
2937 * about to look up
2939 error = complete_walk(nd);
2940 if (error)
2941 return error;
2943 audit_inode(name, dir, LOOKUP_PARENT);
2944 error = -EISDIR;
2945 /* trailing slashes? */
2946 if (nd->last.name[nd->last.len])
2947 goto out;
2950 retry_lookup:
2951 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2952 error = mnt_want_write(nd->path.mnt);
2953 if (!error)
2954 got_write = true;
2956 * do _not_ fail yet - we might not need that or fail with
2957 * a different error; let lookup_open() decide; we'll be
2958 * dropping this one anyway.
2961 mutex_lock(&dir->d_inode->i_mutex);
2962 error = lookup_open(nd, path, file, op, got_write, opened);
2963 mutex_unlock(&dir->d_inode->i_mutex);
2965 if (error <= 0) {
2966 if (error)
2967 goto out;
2969 if ((*opened & FILE_CREATED) ||
2970 !S_ISREG(file_inode(file)->i_mode))
2971 will_truncate = false;
2973 audit_inode(name, file->f_path.dentry, 0);
2974 goto opened;
2977 if (*opened & FILE_CREATED) {
2978 /* Don't check for write permission, don't truncate */
2979 open_flag &= ~O_TRUNC;
2980 will_truncate = false;
2981 acc_mode = MAY_OPEN;
2982 path_to_nameidata(path, nd);
2983 goto finish_open_created;
2987 * create/update audit record if it already exists.
2989 if (path->dentry->d_inode)
2990 audit_inode(name, path->dentry, 0);
2993 * If atomic_open() acquired write access it is dropped now due to
2994 * possible mount and symlink following (this might be optimized away if
2995 * necessary...)
2997 if (got_write) {
2998 mnt_drop_write(nd->path.mnt);
2999 got_write = false;
3002 error = -EEXIST;
3003 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
3004 goto exit_dput;
3006 error = follow_managed(path, nd->flags);
3007 if (error < 0)
3008 goto exit_dput;
3010 if (error)
3011 nd->flags |= LOOKUP_JUMPED;
3013 BUG_ON(nd->flags & LOOKUP_RCU);
3014 inode = path->dentry->d_inode;
3015 finish_lookup:
3016 /* we _can_ be in RCU mode here */
3017 error = -ENOENT;
3018 if (!inode) {
3019 path_to_nameidata(path, nd);
3020 goto out;
3023 if (should_follow_link(inode, !symlink_ok)) {
3024 if (nd->flags & LOOKUP_RCU) {
3025 if (unlikely(unlazy_walk(nd, path->dentry))) {
3026 error = -ECHILD;
3027 goto out;
3030 BUG_ON(inode != path->dentry->d_inode);
3031 return 1;
3034 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3035 path_to_nameidata(path, nd);
3036 } else {
3037 save_parent.dentry = nd->path.dentry;
3038 save_parent.mnt = mntget(path->mnt);
3039 nd->path.dentry = path->dentry;
3042 nd->inode = inode;
3043 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
3044 finish_open:
3045 error = complete_walk(nd);
3046 if (error) {
3047 path_put(&save_parent);
3048 return error;
3050 audit_inode(name, nd->path.dentry, 0);
3051 error = -EISDIR;
3052 if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
3053 goto out;
3054 error = -ENOTDIR;
3055 if ((nd->flags & LOOKUP_DIRECTORY) && !can_lookup(nd->inode))
3056 goto out;
3057 if (!S_ISREG(nd->inode->i_mode))
3058 will_truncate = false;
3060 if (will_truncate) {
3061 error = mnt_want_write(nd->path.mnt);
3062 if (error)
3063 goto out;
3064 got_write = true;
3066 finish_open_created:
3067 error = may_open(&nd->path, acc_mode, open_flag);
3068 if (error)
3069 goto out;
3070 file->f_path.mnt = nd->path.mnt;
3071 error = finish_open(file, nd->path.dentry, NULL, opened);
3072 if (error) {
3073 if (error == -EOPENSTALE)
3074 goto stale_open;
3075 goto out;
3077 opened:
3078 error = open_check_o_direct(file);
3079 if (error)
3080 goto exit_fput;
3081 error = ima_file_check(file, op->acc_mode);
3082 if (error)
3083 goto exit_fput;
3085 if (will_truncate) {
3086 error = handle_truncate(file);
3087 if (error)
3088 goto exit_fput;
3090 out:
3091 if (got_write)
3092 mnt_drop_write(nd->path.mnt);
3093 path_put(&save_parent);
3094 terminate_walk(nd);
3095 return error;
3097 exit_dput:
3098 path_put_conditional(path, nd);
3099 goto out;
3100 exit_fput:
3101 fput(file);
3102 goto out;
3104 stale_open:
3105 /* If no saved parent or already retried then can't retry */
3106 if (!save_parent.dentry || retried)
3107 goto out;
3109 BUG_ON(save_parent.dentry != dir);
3110 path_put(&nd->path);
3111 nd->path = save_parent;
3112 nd->inode = dir->d_inode;
3113 save_parent.mnt = NULL;
3114 save_parent.dentry = NULL;
3115 if (got_write) {
3116 mnt_drop_write(nd->path.mnt);
3117 got_write = false;
3119 retried = true;
3120 goto retry_lookup;
3123 static int do_tmpfile(int dfd, struct filename *pathname,
3124 struct nameidata *nd, int flags,
3125 const struct open_flags *op,
3126 struct file *file, int *opened)
3128 static const struct qstr name = QSTR_INIT("/", 1);
3129 struct dentry *dentry, *child;
3130 struct inode *dir;
3131 int error = path_lookupat(dfd, pathname->name,
3132 flags | LOOKUP_DIRECTORY, nd);
3133 if (unlikely(error))
3134 return error;
3135 error = mnt_want_write(nd->path.mnt);
3136 if (unlikely(error))
3137 goto out;
3138 /* we want directory to be writable */
3139 error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
3140 if (error)
3141 goto out2;
3142 dentry = nd->path.dentry;
3143 dir = dentry->d_inode;
3144 if (!dir->i_op->tmpfile) {
3145 error = -EOPNOTSUPP;
3146 goto out2;
3148 child = d_alloc(dentry, &name);
3149 if (unlikely(!child)) {
3150 error = -ENOMEM;
3151 goto out2;
3153 nd->flags &= ~LOOKUP_DIRECTORY;
3154 nd->flags |= op->intent;
3155 dput(nd->path.dentry);
3156 nd->path.dentry = child;
3157 error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
3158 if (error)
3159 goto out2;
3160 audit_inode(pathname, nd->path.dentry, 0);
3161 error = may_open(&nd->path, op->acc_mode, op->open_flag);
3162 if (error)
3163 goto out2;
3164 file->f_path.mnt = nd->path.mnt;
3165 error = finish_open(file, nd->path.dentry, NULL, opened);
3166 if (error)
3167 goto out2;
3168 error = open_check_o_direct(file);
3169 if (error) {
3170 fput(file);
3171 } else if (!(op->open_flag & O_EXCL)) {
3172 struct inode *inode = file_inode(file);
3173 spin_lock(&inode->i_lock);
3174 inode->i_state |= I_LINKABLE;
3175 spin_unlock(&inode->i_lock);
3177 out2:
3178 mnt_drop_write(nd->path.mnt);
3179 out:
3180 path_put(&nd->path);
3181 return error;
3184 static struct file *path_openat(int dfd, struct filename *pathname,
3185 struct nameidata *nd, const struct open_flags *op, int flags)
3187 struct file *base = NULL;
3188 struct file *file;
3189 struct path path;
3190 int opened = 0;
3191 int error;
3193 file = get_empty_filp();
3194 if (IS_ERR(file))
3195 return file;
3197 file->f_flags = op->open_flag;
3199 if (unlikely(file->f_flags & __O_TMPFILE)) {
3200 error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
3201 goto out;
3204 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
3205 if (unlikely(error))
3206 goto out;
3208 current->total_link_count = 0;
3209 error = link_path_walk(pathname->name, nd);
3210 if (unlikely(error))
3211 goto out;
3213 error = do_last(nd, &path, file, op, &opened, pathname);
3214 while (unlikely(error > 0)) { /* trailing symlink */
3215 struct path link = path;
3216 void *cookie;
3217 if (!(nd->flags & LOOKUP_FOLLOW)) {
3218 path_put_conditional(&path, nd);
3219 path_put(&nd->path);
3220 error = -ELOOP;
3221 break;
3223 error = may_follow_link(&link, nd);
3224 if (unlikely(error))
3225 break;
3226 nd->flags |= LOOKUP_PARENT;
3227 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3228 error = follow_link(&link, nd, &cookie);
3229 if (unlikely(error))
3230 break;
3231 error = do_last(nd, &path, file, op, &opened, pathname);
3232 put_link(nd, &link, cookie);
3234 out:
3235 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
3236 path_put(&nd->root);
3237 if (base)
3238 fput(base);
3239 if (!(opened & FILE_OPENED)) {
3240 BUG_ON(!error);
3241 put_filp(file);
3243 if (unlikely(error)) {
3244 if (error == -EOPENSTALE) {
3245 if (flags & LOOKUP_RCU)
3246 error = -ECHILD;
3247 else
3248 error = -ESTALE;
3250 file = ERR_PTR(error);
3252 return file;
3255 struct file *do_filp_open(int dfd, struct filename *pathname,
3256 const struct open_flags *op)
3258 struct nameidata nd;
3259 int flags = op->lookup_flags;
3260 struct file *filp;
3262 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
3263 if (unlikely(filp == ERR_PTR(-ECHILD)))
3264 filp = path_openat(dfd, pathname, &nd, op, flags);
3265 if (unlikely(filp == ERR_PTR(-ESTALE)))
3266 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
3267 return filp;
3270 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3271 const char *name, const struct open_flags *op)
3273 struct nameidata nd;
3274 struct file *file;
3275 struct filename filename = { .name = name };
3276 int flags = op->lookup_flags | LOOKUP_ROOT;
3278 nd.root.mnt = mnt;
3279 nd.root.dentry = dentry;
3281 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
3282 return ERR_PTR(-ELOOP);
3284 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
3285 if (unlikely(file == ERR_PTR(-ECHILD)))
3286 file = path_openat(-1, &filename, &nd, op, flags);
3287 if (unlikely(file == ERR_PTR(-ESTALE)))
3288 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
3289 return file;
3292 struct dentry *kern_path_create(int dfd, const char *pathname,
3293 struct path *path, unsigned int lookup_flags)
3295 struct dentry *dentry = ERR_PTR(-EEXIST);
3296 struct nameidata nd;
3297 int err2;
3298 int error;
3299 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3302 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3303 * other flags passed in are ignored!
3305 lookup_flags &= LOOKUP_REVAL;
3307 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
3308 if (error)
3309 return ERR_PTR(error);
3312 * Yucky last component or no last component at all?
3313 * (foo/., foo/.., /////)
3315 if (nd.last_type != LAST_NORM)
3316 goto out;
3317 nd.flags &= ~LOOKUP_PARENT;
3318 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3320 /* don't fail immediately if it's r/o, at least try to report other errors */
3321 err2 = mnt_want_write(nd.path.mnt);
3323 * Do the final lookup.
3325 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3326 dentry = lookup_hash(&nd);
3327 if (IS_ERR(dentry))
3328 goto unlock;
3330 error = -EEXIST;
3331 if (dentry->d_inode)
3332 goto fail;
3334 * Special case - lookup gave negative, but... we had foo/bar/
3335 * From the vfs_mknod() POV we just have a negative dentry -
3336 * all is fine. Let's be bastards - you had / on the end, you've
3337 * been asking for (non-existent) directory. -ENOENT for you.
3339 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
3340 error = -ENOENT;
3341 goto fail;
3343 if (unlikely(err2)) {
3344 error = err2;
3345 goto fail;
3347 *path = nd.path;
3348 return dentry;
3349 fail:
3350 dput(dentry);
3351 dentry = ERR_PTR(error);
3352 unlock:
3353 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3354 if (!err2)
3355 mnt_drop_write(nd.path.mnt);
3356 out:
3357 path_put(&nd.path);
3358 return dentry;
3360 EXPORT_SYMBOL(kern_path_create);
3362 void done_path_create(struct path *path, struct dentry *dentry)
3364 dput(dentry);
3365 mutex_unlock(&path->dentry->d_inode->i_mutex);
3366 mnt_drop_write(path->mnt);
3367 path_put(path);
3369 EXPORT_SYMBOL(done_path_create);
3371 struct dentry *user_path_create(int dfd, const char __user *pathname,
3372 struct path *path, unsigned int lookup_flags)
3374 struct filename *tmp = getname(pathname);
3375 struct dentry *res;
3376 if (IS_ERR(tmp))
3377 return ERR_CAST(tmp);
3378 res = kern_path_create(dfd, tmp->name, path, lookup_flags);
3379 putname(tmp);
3380 return res;
3382 EXPORT_SYMBOL(user_path_create);
3384 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3386 int error = may_create(dir, dentry);
3388 if (error)
3389 return error;
3391 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
3392 return -EPERM;
3394 if (!dir->i_op->mknod)
3395 return -EPERM;
3397 error = devcgroup_inode_mknod(mode, dev);
3398 if (error)
3399 return error;
3401 error = security_inode_mknod(dir, dentry, mode, dev);
3402 if (error)
3403 return error;
3405 error = dir->i_op->mknod(dir, dentry, mode, dev);
3406 if (!error)
3407 fsnotify_create(dir, dentry);
3408 return error;
3411 static int may_mknod(umode_t mode)
3413 switch (mode & S_IFMT) {
3414 case S_IFREG:
3415 case S_IFCHR:
3416 case S_IFBLK:
3417 case S_IFIFO:
3418 case S_IFSOCK:
3419 case 0: /* zero mode translates to S_IFREG */
3420 return 0;
3421 case S_IFDIR:
3422 return -EPERM;
3423 default:
3424 return -EINVAL;
3428 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3429 unsigned, dev)
3431 struct dentry *dentry;
3432 struct path path;
3433 int error;
3434 unsigned int lookup_flags = 0;
3436 error = may_mknod(mode);
3437 if (error)
3438 return error;
3439 retry:
3440 dentry = user_path_create(dfd, filename, &path, lookup_flags);
3441 if (IS_ERR(dentry))
3442 return PTR_ERR(dentry);
3444 if (!IS_POSIXACL(path.dentry->d_inode))
3445 mode &= ~current_umask();
3446 error = security_path_mknod(&path, dentry, mode, dev);
3447 if (error)
3448 goto out;
3449 switch (mode & S_IFMT) {
3450 case 0: case S_IFREG:
3451 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
3452 break;
3453 case S_IFCHR: case S_IFBLK:
3454 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
3455 new_decode_dev(dev));
3456 break;
3457 case S_IFIFO: case S_IFSOCK:
3458 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
3459 break;
3461 out:
3462 done_path_create(&path, dentry);
3463 if (retry_estale(error, lookup_flags)) {
3464 lookup_flags |= LOOKUP_REVAL;
3465 goto retry;
3467 return error;
3470 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
3472 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3475 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3477 int error = may_create(dir, dentry);
3478 unsigned max_links = dir->i_sb->s_max_links;
3480 if (error)
3481 return error;
3483 if (!dir->i_op->mkdir)
3484 return -EPERM;
3486 mode &= (S_IRWXUGO|S_ISVTX);
3487 error = security_inode_mkdir(dir, dentry, mode);
3488 if (error)
3489 return error;
3491 if (max_links && dir->i_nlink >= max_links)
3492 return -EMLINK;
3494 error = dir->i_op->mkdir(dir, dentry, mode);
3495 if (!error)
3496 fsnotify_mkdir(dir, dentry);
3497 return error;
3500 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3502 struct dentry *dentry;
3503 struct path path;
3504 int error;
3505 unsigned int lookup_flags = LOOKUP_DIRECTORY;
3507 retry:
3508 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
3509 if (IS_ERR(dentry))
3510 return PTR_ERR(dentry);
3512 if (!IS_POSIXACL(path.dentry->d_inode))
3513 mode &= ~current_umask();
3514 error = security_path_mkdir(&path, dentry, mode);
3515 if (!error)
3516 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3517 done_path_create(&path, dentry);
3518 if (retry_estale(error, lookup_flags)) {
3519 lookup_flags |= LOOKUP_REVAL;
3520 goto retry;
3522 return error;
3525 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
3527 return sys_mkdirat(AT_FDCWD, pathname, mode);
3531 * The dentry_unhash() helper will try to drop the dentry early: we
3532 * should have a usage count of 1 if we're the only user of this
3533 * dentry, and if that is true (possibly after pruning the dcache),
3534 * then we drop the dentry now.
3536 * A low-level filesystem can, if it choses, legally
3537 * do a
3539 * if (!d_unhashed(dentry))
3540 * return -EBUSY;
3542 * if it cannot handle the case of removing a directory
3543 * that is still in use by something else..
3545 void dentry_unhash(struct dentry *dentry)
3547 shrink_dcache_parent(dentry);
3548 spin_lock(&dentry->d_lock);
3549 if (dentry->d_lockref.count == 1)
3550 __d_drop(dentry);
3551 spin_unlock(&dentry->d_lock);
3554 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3556 int error = may_delete(dir, dentry, 1);
3558 if (error)
3559 return error;
3561 if (!dir->i_op->rmdir)
3562 return -EPERM;
3564 dget(dentry);
3565 mutex_lock(&dentry->d_inode->i_mutex);
3567 error = -EBUSY;
3568 if (d_mountpoint(dentry))
3569 goto out;
3571 error = security_inode_rmdir(dir, dentry);
3572 if (error)
3573 goto out;
3575 shrink_dcache_parent(dentry);
3576 error = dir->i_op->rmdir(dir, dentry);
3577 if (error)
3578 goto out;
3580 dentry->d_inode->i_flags |= S_DEAD;
3581 dont_mount(dentry);
3583 out:
3584 mutex_unlock(&dentry->d_inode->i_mutex);
3585 dput(dentry);
3586 if (!error)
3587 d_delete(dentry);
3588 return error;
3591 static long do_rmdir(int dfd, const char __user *pathname)
3593 int error = 0;
3594 struct filename *name;
3595 struct dentry *dentry;
3596 struct nameidata nd;
3597 unsigned int lookup_flags = 0;
3598 retry:
3599 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
3600 if (IS_ERR(name))
3601 return PTR_ERR(name);
3603 switch(nd.last_type) {
3604 case LAST_DOTDOT:
3605 error = -ENOTEMPTY;
3606 goto exit1;
3607 case LAST_DOT:
3608 error = -EINVAL;
3609 goto exit1;
3610 case LAST_ROOT:
3611 error = -EBUSY;
3612 goto exit1;
3615 nd.flags &= ~LOOKUP_PARENT;
3616 error = mnt_want_write(nd.path.mnt);
3617 if (error)
3618 goto exit1;
3620 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3621 dentry = lookup_hash(&nd);
3622 error = PTR_ERR(dentry);
3623 if (IS_ERR(dentry))
3624 goto exit2;
3625 if (!dentry->d_inode) {
3626 error = -ENOENT;
3627 goto exit3;
3629 error = security_path_rmdir(&nd.path, dentry);
3630 if (error)
3631 goto exit3;
3632 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
3633 exit3:
3634 dput(dentry);
3635 exit2:
3636 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3637 mnt_drop_write(nd.path.mnt);
3638 exit1:
3639 path_put(&nd.path);
3640 putname(name);
3641 if (retry_estale(error, lookup_flags)) {
3642 lookup_flags |= LOOKUP_REVAL;
3643 goto retry;
3645 return error;
3648 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
3650 return do_rmdir(AT_FDCWD, pathname);
3653 int vfs_unlink(struct inode *dir, struct dentry *dentry)
3655 int error = may_delete(dir, dentry, 0);
3657 if (error)
3658 return error;
3660 if (!dir->i_op->unlink)
3661 return -EPERM;
3663 mutex_lock(&dentry->d_inode->i_mutex);
3664 if (d_mountpoint(dentry))
3665 error = -EBUSY;
3666 else {
3667 error = security_inode_unlink(dir, dentry);
3668 if (!error) {
3669 error = dir->i_op->unlink(dir, dentry);
3670 if (!error)
3671 dont_mount(dentry);
3674 mutex_unlock(&dentry->d_inode->i_mutex);
3676 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3677 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3678 fsnotify_link_count(dentry->d_inode);
3679 d_delete(dentry);
3682 return error;
3686 * Make sure that the actual truncation of the file will occur outside its
3687 * directory's i_mutex. Truncate can take a long time if there is a lot of
3688 * writeout happening, and we don't want to prevent access to the directory
3689 * while waiting on the I/O.
3691 static long do_unlinkat(int dfd, const char __user *pathname)
3693 int error;
3694 struct filename *name;
3695 struct dentry *dentry;
3696 struct nameidata nd;
3697 struct inode *inode = NULL;
3698 unsigned int lookup_flags = 0;
3699 retry:
3700 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
3701 if (IS_ERR(name))
3702 return PTR_ERR(name);
3704 error = -EISDIR;
3705 if (nd.last_type != LAST_NORM)
3706 goto exit1;
3708 nd.flags &= ~LOOKUP_PARENT;
3709 error = mnt_want_write(nd.path.mnt);
3710 if (error)
3711 goto exit1;
3713 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3714 dentry = lookup_hash(&nd);
3715 error = PTR_ERR(dentry);
3716 if (!IS_ERR(dentry)) {
3717 /* Why not before? Because we want correct error value */
3718 if (nd.last.name[nd.last.len])
3719 goto slashes;
3720 inode = dentry->d_inode;
3721 if (!inode)
3722 goto slashes;
3723 ihold(inode);
3724 error = security_path_unlink(&nd.path, dentry);
3725 if (error)
3726 goto exit2;
3727 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
3728 exit2:
3729 dput(dentry);
3731 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3732 if (inode)
3733 iput(inode); /* truncate the inode here */
3734 mnt_drop_write(nd.path.mnt);
3735 exit1:
3736 path_put(&nd.path);
3737 putname(name);
3738 if (retry_estale(error, lookup_flags)) {
3739 lookup_flags |= LOOKUP_REVAL;
3740 inode = NULL;
3741 goto retry;
3743 return error;
3745 slashes:
3746 error = !dentry->d_inode ? -ENOENT :
3747 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
3748 goto exit2;
3751 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
3753 if ((flag & ~AT_REMOVEDIR) != 0)
3754 return -EINVAL;
3756 if (flag & AT_REMOVEDIR)
3757 return do_rmdir(dfd, pathname);
3759 return do_unlinkat(dfd, pathname);
3762 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
3764 return do_unlinkat(AT_FDCWD, pathname);
3767 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
3769 int error = may_create(dir, dentry);
3771 if (error)
3772 return error;
3774 if (!dir->i_op->symlink)
3775 return -EPERM;
3777 error = security_inode_symlink(dir, dentry, oldname);
3778 if (error)
3779 return error;
3781 error = dir->i_op->symlink(dir, dentry, oldname);
3782 if (!error)
3783 fsnotify_create(dir, dentry);
3784 return error;
3787 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3788 int, newdfd, const char __user *, newname)
3790 int error;
3791 struct filename *from;
3792 struct dentry *dentry;
3793 struct path path;
3794 unsigned int lookup_flags = 0;
3796 from = getname(oldname);
3797 if (IS_ERR(from))
3798 return PTR_ERR(from);
3799 retry:
3800 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
3801 error = PTR_ERR(dentry);
3802 if (IS_ERR(dentry))
3803 goto out_putname;
3805 error = security_path_symlink(&path, dentry, from->name);
3806 if (!error)
3807 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
3808 done_path_create(&path, dentry);
3809 if (retry_estale(error, lookup_flags)) {
3810 lookup_flags |= LOOKUP_REVAL;
3811 goto retry;
3813 out_putname:
3814 putname(from);
3815 return error;
3818 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
3820 return sys_symlinkat(oldname, AT_FDCWD, newname);
3823 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3825 struct inode *inode = old_dentry->d_inode;
3826 unsigned max_links = dir->i_sb->s_max_links;
3827 int error;
3829 if (!inode)
3830 return -ENOENT;
3832 error = may_create(dir, new_dentry);
3833 if (error)
3834 return error;
3836 if (dir->i_sb != inode->i_sb)
3837 return -EXDEV;
3840 * A link to an append-only or immutable file cannot be created.
3842 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3843 return -EPERM;
3844 if (!dir->i_op->link)
3845 return -EPERM;
3846 if (S_ISDIR(inode->i_mode))
3847 return -EPERM;
3849 error = security_inode_link(old_dentry, dir, new_dentry);
3850 if (error)
3851 return error;
3853 mutex_lock(&inode->i_mutex);
3854 /* Make sure we don't allow creating hardlink to an unlinked file */
3855 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
3856 error = -ENOENT;
3857 else if (max_links && inode->i_nlink >= max_links)
3858 error = -EMLINK;
3859 else
3860 error = dir->i_op->link(old_dentry, dir, new_dentry);
3862 if (!error && (inode->i_state & I_LINKABLE)) {
3863 spin_lock(&inode->i_lock);
3864 inode->i_state &= ~I_LINKABLE;
3865 spin_unlock(&inode->i_lock);
3867 mutex_unlock(&inode->i_mutex);
3868 if (!error)
3869 fsnotify_link(dir, inode, new_dentry);
3870 return error;
3874 * Hardlinks are often used in delicate situations. We avoid
3875 * security-related surprises by not following symlinks on the
3876 * newname. --KAB
3878 * We don't follow them on the oldname either to be compatible
3879 * with linux 2.0, and to avoid hard-linking to directories
3880 * and other special files. --ADM
3882 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3883 int, newdfd, const char __user *, newname, int, flags)
3885 struct dentry *new_dentry;
3886 struct path old_path, new_path;
3887 int how = 0;
3888 int error;
3890 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3891 return -EINVAL;
3893 * To use null names we require CAP_DAC_READ_SEARCH
3894 * This ensures that not everyone will be able to create
3895 * handlink using the passed filedescriptor.
3897 if (flags & AT_EMPTY_PATH) {
3898 if (!capable(CAP_DAC_READ_SEARCH))
3899 return -ENOENT;
3900 how = LOOKUP_EMPTY;
3903 if (flags & AT_SYMLINK_FOLLOW)
3904 how |= LOOKUP_FOLLOW;
3905 retry:
3906 error = user_path_at(olddfd, oldname, how, &old_path);
3907 if (error)
3908 return error;
3910 new_dentry = user_path_create(newdfd, newname, &new_path,
3911 (how & LOOKUP_REVAL));
3912 error = PTR_ERR(new_dentry);
3913 if (IS_ERR(new_dentry))
3914 goto out;
3916 error = -EXDEV;
3917 if (old_path.mnt != new_path.mnt)
3918 goto out_dput;
3919 error = may_linkat(&old_path);
3920 if (unlikely(error))
3921 goto out_dput;
3922 error = security_path_link(old_path.dentry, &new_path, new_dentry);
3923 if (error)
3924 goto out_dput;
3925 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3926 out_dput:
3927 done_path_create(&new_path, new_dentry);
3928 if (retry_estale(error, how)) {
3929 path_put(&old_path);
3930 how |= LOOKUP_REVAL;
3931 goto retry;
3933 out:
3934 path_put(&old_path);
3936 return error;
3939 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
3941 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
3945 * The worst of all namespace operations - renaming directory. "Perverted"
3946 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3947 * Problems:
3948 * a) we can get into loop creation. Check is done in is_subdir().
3949 * b) race potential - two innocent renames can create a loop together.
3950 * That's where 4.4 screws up. Current fix: serialization on
3951 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3952 * story.
3953 * c) we have to lock _three_ objects - parents and victim (if it exists).
3954 * And that - after we got ->i_mutex on parents (until then we don't know
3955 * whether the target exists). Solution: try to be smart with locking
3956 * order for inodes. We rely on the fact that tree topology may change
3957 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
3958 * move will be locked. Thus we can rank directories by the tree
3959 * (ancestors first) and rank all non-directories after them.
3960 * That works since everybody except rename does "lock parent, lookup,
3961 * lock child" and rename is under ->s_vfs_rename_mutex.
3962 * HOWEVER, it relies on the assumption that any object with ->lookup()
3963 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3964 * we'd better make sure that there's no link(2) for them.
3965 * d) conversion from fhandle to dentry may come in the wrong moment - when
3966 * we are removing the target. Solution: we will have to grab ->i_mutex
3967 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3968 * ->i_mutex on parents, which works but leads to some truly excessive
3969 * locking].
3971 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3972 struct inode *new_dir, struct dentry *new_dentry)
3974 int error = 0;
3975 struct inode *target = new_dentry->d_inode;
3976 unsigned max_links = new_dir->i_sb->s_max_links;
3979 * If we are going to change the parent - check write permissions,
3980 * we'll need to flip '..'.
3982 if (new_dir != old_dir) {
3983 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
3984 if (error)
3985 return error;
3988 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3989 if (error)
3990 return error;
3992 dget(new_dentry);
3993 if (target)
3994 mutex_lock(&target->i_mutex);
3996 error = -EBUSY;
3997 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3998 goto out;
4000 error = -EMLINK;
4001 if (max_links && !target && new_dir != old_dir &&
4002 new_dir->i_nlink >= max_links)
4003 goto out;
4005 if (target)
4006 shrink_dcache_parent(new_dentry);
4007 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4008 if (error)
4009 goto out;
4011 if (target) {
4012 target->i_flags |= S_DEAD;
4013 dont_mount(new_dentry);
4015 out:
4016 if (target)
4017 mutex_unlock(&target->i_mutex);
4018 dput(new_dentry);
4019 if (!error)
4020 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
4021 d_move(old_dentry,new_dentry);
4022 return error;
4025 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
4026 struct inode *new_dir, struct dentry *new_dentry)
4028 struct inode *target = new_dentry->d_inode;
4029 int error;
4031 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
4032 if (error)
4033 return error;
4035 dget(new_dentry);
4036 if (target)
4037 mutex_lock(&target->i_mutex);
4039 error = -EBUSY;
4040 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
4041 goto out;
4043 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4044 if (error)
4045 goto out;
4047 if (target)
4048 dont_mount(new_dentry);
4049 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
4050 d_move(old_dentry, new_dentry);
4051 out:
4052 if (target)
4053 mutex_unlock(&target->i_mutex);
4054 dput(new_dentry);
4055 return error;
4058 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4059 struct inode *new_dir, struct dentry *new_dentry)
4061 int error;
4062 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
4063 const unsigned char *old_name;
4065 if (old_dentry->d_inode == new_dentry->d_inode)
4066 return 0;
4068 error = may_delete(old_dir, old_dentry, is_dir);
4069 if (error)
4070 return error;
4072 if (!new_dentry->d_inode)
4073 error = may_create(new_dir, new_dentry);
4074 else
4075 error = may_delete(new_dir, new_dentry, is_dir);
4076 if (error)
4077 return error;
4079 if (!old_dir->i_op->rename)
4080 return -EPERM;
4082 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4084 if (is_dir)
4085 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
4086 else
4087 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
4088 if (!error)
4089 fsnotify_move(old_dir, new_dir, old_name, is_dir,
4090 new_dentry->d_inode, old_dentry);
4091 fsnotify_oldname_free(old_name);
4093 return error;
4096 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4097 int, newdfd, const char __user *, newname)
4099 struct dentry *old_dir, *new_dir;
4100 struct dentry *old_dentry, *new_dentry;
4101 struct dentry *trap;
4102 struct nameidata oldnd, newnd;
4103 struct filename *from;
4104 struct filename *to;
4105 unsigned int lookup_flags = 0;
4106 bool should_retry = false;
4107 int error;
4108 retry:
4109 from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
4110 if (IS_ERR(from)) {
4111 error = PTR_ERR(from);
4112 goto exit;
4115 to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
4116 if (IS_ERR(to)) {
4117 error = PTR_ERR(to);
4118 goto exit1;
4121 error = -EXDEV;
4122 if (oldnd.path.mnt != newnd.path.mnt)
4123 goto exit2;
4125 old_dir = oldnd.path.dentry;
4126 error = -EBUSY;
4127 if (oldnd.last_type != LAST_NORM)
4128 goto exit2;
4130 new_dir = newnd.path.dentry;
4131 if (newnd.last_type != LAST_NORM)
4132 goto exit2;
4134 error = mnt_want_write(oldnd.path.mnt);
4135 if (error)
4136 goto exit2;
4138 oldnd.flags &= ~LOOKUP_PARENT;
4139 newnd.flags &= ~LOOKUP_PARENT;
4140 newnd.flags |= LOOKUP_RENAME_TARGET;
4142 trap = lock_rename(new_dir, old_dir);
4144 old_dentry = lookup_hash(&oldnd);
4145 error = PTR_ERR(old_dentry);
4146 if (IS_ERR(old_dentry))
4147 goto exit3;
4148 /* source must exist */
4149 error = -ENOENT;
4150 if (!old_dentry->d_inode)
4151 goto exit4;
4152 /* unless the source is a directory trailing slashes give -ENOTDIR */
4153 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
4154 error = -ENOTDIR;
4155 if (oldnd.last.name[oldnd.last.len])
4156 goto exit4;
4157 if (newnd.last.name[newnd.last.len])
4158 goto exit4;
4160 /* source should not be ancestor of target */
4161 error = -EINVAL;
4162 if (old_dentry == trap)
4163 goto exit4;
4164 new_dentry = lookup_hash(&newnd);
4165 error = PTR_ERR(new_dentry);
4166 if (IS_ERR(new_dentry))
4167 goto exit4;
4168 /* target should not be an ancestor of source */
4169 error = -ENOTEMPTY;
4170 if (new_dentry == trap)
4171 goto exit5;
4173 error = security_path_rename(&oldnd.path, old_dentry,
4174 &newnd.path, new_dentry);
4175 if (error)
4176 goto exit5;
4177 error = vfs_rename(old_dir->d_inode, old_dentry,
4178 new_dir->d_inode, new_dentry);
4179 exit5:
4180 dput(new_dentry);
4181 exit4:
4182 dput(old_dentry);
4183 exit3:
4184 unlock_rename(new_dir, old_dir);
4185 mnt_drop_write(oldnd.path.mnt);
4186 exit2:
4187 if (retry_estale(error, lookup_flags))
4188 should_retry = true;
4189 path_put(&newnd.path);
4190 putname(to);
4191 exit1:
4192 path_put(&oldnd.path);
4193 putname(from);
4194 if (should_retry) {
4195 should_retry = false;
4196 lookup_flags |= LOOKUP_REVAL;
4197 goto retry;
4199 exit:
4200 return error;
4203 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
4205 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
4208 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
4210 int len;
4212 len = PTR_ERR(link);
4213 if (IS_ERR(link))
4214 goto out;
4216 len = strlen(link);
4217 if (len > (unsigned) buflen)
4218 len = buflen;
4219 if (copy_to_user(buffer, link, len))
4220 len = -EFAULT;
4221 out:
4222 return len;
4226 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4227 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4228 * using) it for any given inode is up to filesystem.
4230 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4232 struct nameidata nd;
4233 void *cookie;
4234 int res;
4236 nd.depth = 0;
4237 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
4238 if (IS_ERR(cookie))
4239 return PTR_ERR(cookie);
4241 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
4242 if (dentry->d_inode->i_op->put_link)
4243 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4244 return res;
4247 /* get the link contents into pagecache */
4248 static char *page_getlink(struct dentry * dentry, struct page **ppage)
4250 char *kaddr;
4251 struct page *page;
4252 struct address_space *mapping = dentry->d_inode->i_mapping;
4253 page = read_mapping_page(mapping, 0, NULL);
4254 if (IS_ERR(page))
4255 return (char*)page;
4256 *ppage = page;
4257 kaddr = kmap(page);
4258 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4259 return kaddr;
4262 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4264 struct page *page = NULL;
4265 char *s = page_getlink(dentry, &page);
4266 int res = vfs_readlink(dentry,buffer,buflen,s);
4267 if (page) {
4268 kunmap(page);
4269 page_cache_release(page);
4271 return res;
4274 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
4276 struct page *page = NULL;
4277 nd_set_link(nd, page_getlink(dentry, &page));
4278 return page;
4281 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
4283 struct page *page = cookie;
4285 if (page) {
4286 kunmap(page);
4287 page_cache_release(page);
4292 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4294 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
4296 struct address_space *mapping = inode->i_mapping;
4297 struct page *page;
4298 void *fsdata;
4299 int err;
4300 char *kaddr;
4301 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4302 if (nofs)
4303 flags |= AOP_FLAG_NOFS;
4305 retry:
4306 err = pagecache_write_begin(NULL, mapping, 0, len-1,
4307 flags, &page, &fsdata);
4308 if (err)
4309 goto fail;
4311 kaddr = kmap_atomic(page);
4312 memcpy(kaddr, symname, len-1);
4313 kunmap_atomic(kaddr);
4315 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4316 page, fsdata);
4317 if (err < 0)
4318 goto fail;
4319 if (err < len-1)
4320 goto retry;
4322 mark_inode_dirty(inode);
4323 return 0;
4324 fail:
4325 return err;
4328 int page_symlink(struct inode *inode, const char *symname, int len)
4330 return __page_symlink(inode, symname, len,
4331 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
4334 const struct inode_operations page_symlink_inode_operations = {
4335 .readlink = generic_readlink,
4336 .follow_link = page_follow_link_light,
4337 .put_link = page_put_link,
4340 EXPORT_SYMBOL(user_path_at);
4341 EXPORT_SYMBOL(follow_down_one);
4342 EXPORT_SYMBOL(follow_down);
4343 EXPORT_SYMBOL(follow_up);
4344 EXPORT_SYMBOL(get_write_access); /* nfsd */
4345 EXPORT_SYMBOL(lock_rename);
4346 EXPORT_SYMBOL(lookup_one_len);
4347 EXPORT_SYMBOL(page_follow_link_light);
4348 EXPORT_SYMBOL(page_put_link);
4349 EXPORT_SYMBOL(page_readlink);
4350 EXPORT_SYMBOL(__page_symlink);
4351 EXPORT_SYMBOL(page_symlink);
4352 EXPORT_SYMBOL(page_symlink_inode_operations);
4353 EXPORT_SYMBOL(kern_path);
4354 EXPORT_SYMBOL(vfs_path_lookup);
4355 EXPORT_SYMBOL(inode_permission);
4356 EXPORT_SYMBOL(unlock_rename);
4357 EXPORT_SYMBOL(vfs_create);
4358 EXPORT_SYMBOL(vfs_link);
4359 EXPORT_SYMBOL(vfs_mkdir);
4360 EXPORT_SYMBOL(vfs_mknod);
4361 EXPORT_SYMBOL(generic_permission);
4362 EXPORT_SYMBOL(vfs_readlink);
4363 EXPORT_SYMBOL(vfs_rename);
4364 EXPORT_SYMBOL(vfs_rmdir);
4365 EXPORT_SYMBOL(vfs_symlink);
4366 EXPORT_SYMBOL(vfs_unlink);
4367 EXPORT_SYMBOL(dentry_unhash);
4368 EXPORT_SYMBOL(generic_readlink);