bonding: fix the err path for dev hwaddr sync in bond_enslave
[linux/fpc-iii.git] / fs / namei.c
blob891670e0956bc453ccae127979f613518ce69c03
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 <linux/hash.h>
38 #include <linux/bitops.h>
39 #include <linux/init_task.h>
40 #include <asm/uaccess.h>
42 #include "internal.h"
43 #include "mount.h"
45 /* [Feb-1997 T. Schoebel-Theuer]
46 * Fundamental changes in the pathname lookup mechanisms (namei)
47 * were necessary because of omirr. The reason is that omirr needs
48 * to know the _real_ pathname, not the user-supplied one, in case
49 * of symlinks (and also when transname replacements occur).
51 * The new code replaces the old recursive symlink resolution with
52 * an iterative one (in case of non-nested symlink chains). It does
53 * this with calls to <fs>_follow_link().
54 * As a side effect, dir_namei(), _namei() and follow_link() are now
55 * replaced with a single function lookup_dentry() that can handle all
56 * the special cases of the former code.
58 * With the new dcache, the pathname is stored at each inode, at least as
59 * long as the refcount of the inode is positive. As a side effect, the
60 * size of the dcache depends on the inode cache and thus is dynamic.
62 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
63 * resolution to correspond with current state of the code.
65 * Note that the symlink resolution is not *completely* iterative.
66 * There is still a significant amount of tail- and mid- recursion in
67 * the algorithm. Also, note that <fs>_readlink() is not used in
68 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
69 * may return different results than <fs>_follow_link(). Many virtual
70 * filesystems (including /proc) exhibit this behavior.
73 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
74 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
75 * and the name already exists in form of a symlink, try to create the new
76 * name indicated by the symlink. The old code always complained that the
77 * name already exists, due to not following the symlink even if its target
78 * is nonexistent. The new semantics affects also mknod() and link() when
79 * the name is a symlink pointing to a non-existent name.
81 * I don't know which semantics is the right one, since I have no access
82 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
83 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
84 * "old" one. Personally, I think the new semantics is much more logical.
85 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
86 * file does succeed in both HP-UX and SunOs, but not in Solaris
87 * and in the old Linux semantics.
90 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
91 * semantics. See the comments in "open_namei" and "do_link" below.
93 * [10-Sep-98 Alan Modra] Another symlink change.
96 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
97 * inside the path - always follow.
98 * in the last component in creation/removal/renaming - never follow.
99 * if LOOKUP_FOLLOW passed - follow.
100 * if the pathname has trailing slashes - follow.
101 * otherwise - don't follow.
102 * (applied in that order).
104 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
105 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
106 * During the 2.4 we need to fix the userland stuff depending on it -
107 * hopefully we will be able to get rid of that wart in 2.5. So far only
108 * XEmacs seems to be relying on it...
111 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
112 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
113 * any extra contention...
116 /* In order to reduce some races, while at the same time doing additional
117 * checking and hopefully speeding things up, we copy filenames to the
118 * kernel data space before using them..
120 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
121 * PATH_MAX includes the nul terminator --RR.
124 #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
126 struct filename *
127 getname_flags(const char __user *filename, int flags, int *empty)
129 struct filename *result;
130 char *kname;
131 int len;
133 result = audit_reusename(filename);
134 if (result)
135 return result;
137 result = __getname();
138 if (unlikely(!result))
139 return ERR_PTR(-ENOMEM);
142 * First, try to embed the struct filename inside the names_cache
143 * allocation
145 kname = (char *)result->iname;
146 result->name = kname;
148 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
149 if (unlikely(len < 0)) {
150 __putname(result);
151 return ERR_PTR(len);
155 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
156 * separate struct filename so we can dedicate the entire
157 * names_cache allocation for the pathname, and re-do the copy from
158 * userland.
160 if (unlikely(len == EMBEDDED_NAME_MAX)) {
161 const size_t size = offsetof(struct filename, iname[1]);
162 kname = (char *)result;
165 * size is chosen that way we to guarantee that
166 * result->iname[0] is within the same object and that
167 * kname can't be equal to result->iname, no matter what.
169 result = kzalloc(size, GFP_KERNEL);
170 if (unlikely(!result)) {
171 __putname(kname);
172 return ERR_PTR(-ENOMEM);
174 result->name = kname;
175 len = strncpy_from_user(kname, filename, PATH_MAX);
176 if (unlikely(len < 0)) {
177 __putname(kname);
178 kfree(result);
179 return ERR_PTR(len);
181 if (unlikely(len == PATH_MAX)) {
182 __putname(kname);
183 kfree(result);
184 return ERR_PTR(-ENAMETOOLONG);
188 result->refcnt = 1;
189 /* The empty path is special. */
190 if (unlikely(!len)) {
191 if (empty)
192 *empty = 1;
193 if (!(flags & LOOKUP_EMPTY)) {
194 putname(result);
195 return ERR_PTR(-ENOENT);
199 result->uptr = filename;
200 result->aname = NULL;
201 audit_getname(result);
202 return result;
205 struct filename *
206 getname(const char __user * filename)
208 return getname_flags(filename, 0, NULL);
211 struct filename *
212 getname_kernel(const char * filename)
214 struct filename *result;
215 int len = strlen(filename) + 1;
217 result = __getname();
218 if (unlikely(!result))
219 return ERR_PTR(-ENOMEM);
221 if (len <= EMBEDDED_NAME_MAX) {
222 result->name = (char *)result->iname;
223 } else if (len <= PATH_MAX) {
224 struct filename *tmp;
226 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
227 if (unlikely(!tmp)) {
228 __putname(result);
229 return ERR_PTR(-ENOMEM);
231 tmp->name = (char *)result;
232 result = tmp;
233 } else {
234 __putname(result);
235 return ERR_PTR(-ENAMETOOLONG);
237 memcpy((char *)result->name, filename, len);
238 result->uptr = NULL;
239 result->aname = NULL;
240 result->refcnt = 1;
241 audit_getname(result);
243 return result;
246 void putname(struct filename *name)
248 BUG_ON(name->refcnt <= 0);
250 if (--name->refcnt > 0)
251 return;
253 if (name->name != name->iname) {
254 __putname(name->name);
255 kfree(name);
256 } else
257 __putname(name);
260 static int check_acl(struct inode *inode, int mask)
262 #ifdef CONFIG_FS_POSIX_ACL
263 struct posix_acl *acl;
265 if (mask & MAY_NOT_BLOCK) {
266 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
267 if (!acl)
268 return -EAGAIN;
269 /* no ->get_acl() calls in RCU mode... */
270 if (is_uncached_acl(acl))
271 return -ECHILD;
272 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
275 acl = get_acl(inode, ACL_TYPE_ACCESS);
276 if (IS_ERR(acl))
277 return PTR_ERR(acl);
278 if (acl) {
279 int error = posix_acl_permission(inode, acl, mask);
280 posix_acl_release(acl);
281 return error;
283 #endif
285 return -EAGAIN;
289 * This does the basic permission checking
291 static int acl_permission_check(struct inode *inode, int mask)
293 unsigned int mode = inode->i_mode;
295 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
296 mode >>= 6;
297 else {
298 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
299 int error = check_acl(inode, mask);
300 if (error != -EAGAIN)
301 return error;
304 if (in_group_p(inode->i_gid))
305 mode >>= 3;
309 * If the DACs are ok we don't need any capability check.
311 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
312 return 0;
313 return -EACCES;
317 * generic_permission - check for access rights on a Posix-like filesystem
318 * @inode: inode to check access rights for
319 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
321 * Used to check for read/write/execute permissions on a file.
322 * We use "fsuid" for this, letting us set arbitrary permissions
323 * for filesystem access without changing the "normal" uids which
324 * are used for other things.
326 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
327 * request cannot be satisfied (eg. requires blocking or too much complexity).
328 * It would then be called again in ref-walk mode.
330 int generic_permission(struct inode *inode, int mask)
332 int ret;
335 * Do the basic permission checks.
337 ret = acl_permission_check(inode, mask);
338 if (ret != -EACCES)
339 return ret;
341 if (S_ISDIR(inode->i_mode)) {
342 /* DACs are overridable for directories */
343 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
344 return 0;
345 if (!(mask & MAY_WRITE))
346 if (capable_wrt_inode_uidgid(inode,
347 CAP_DAC_READ_SEARCH))
348 return 0;
349 return -EACCES;
352 * Read/write DACs are always overridable.
353 * Executable DACs are overridable when there is
354 * at least one exec bit set.
356 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
357 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
358 return 0;
361 * Searching includes executable on directories, else just read.
363 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
364 if (mask == MAY_READ)
365 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
366 return 0;
368 return -EACCES;
370 EXPORT_SYMBOL(generic_permission);
373 * We _really_ want to just do "generic_permission()" without
374 * even looking at the inode->i_op values. So we keep a cache
375 * flag in inode->i_opflags, that says "this has not special
376 * permission function, use the fast case".
378 static inline int do_inode_permission(struct inode *inode, int mask)
380 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
381 if (likely(inode->i_op->permission))
382 return inode->i_op->permission(inode, mask);
384 /* This gets set once for the inode lifetime */
385 spin_lock(&inode->i_lock);
386 inode->i_opflags |= IOP_FASTPERM;
387 spin_unlock(&inode->i_lock);
389 return generic_permission(inode, mask);
393 * __inode_permission - Check for access rights to a given inode
394 * @inode: Inode to check permission on
395 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
397 * Check for read/write/execute permissions on an inode.
399 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
401 * This does not check for a read-only file system. You probably want
402 * inode_permission().
404 int __inode_permission(struct inode *inode, int mask)
406 int retval;
408 if (unlikely(mask & MAY_WRITE)) {
410 * Nobody gets write access to an immutable file.
412 if (IS_IMMUTABLE(inode))
413 return -EPERM;
416 * Updating mtime will likely cause i_uid and i_gid to be
417 * written back improperly if their true value is unknown
418 * to the vfs.
420 if (HAS_UNMAPPED_ID(inode))
421 return -EACCES;
424 retval = do_inode_permission(inode, mask);
425 if (retval)
426 return retval;
428 retval = devcgroup_inode_permission(inode, mask);
429 if (retval)
430 return retval;
432 return security_inode_permission(inode, mask);
434 EXPORT_SYMBOL(__inode_permission);
437 * sb_permission - Check superblock-level permissions
438 * @sb: Superblock of inode to check permission on
439 * @inode: Inode to check permission on
440 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
442 * Separate out file-system wide checks from inode-specific permission checks.
444 static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
446 if (unlikely(mask & MAY_WRITE)) {
447 umode_t mode = inode->i_mode;
449 /* Nobody gets write access to a read-only fs. */
450 if ((sb->s_flags & MS_RDONLY) &&
451 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
452 return -EROFS;
454 return 0;
458 * inode_permission - Check for access rights to a given inode
459 * @inode: Inode to check permission on
460 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
462 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
463 * this, letting us set arbitrary permissions for filesystem access without
464 * changing the "normal" UIDs which are used for other things.
466 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
468 int inode_permission(struct inode *inode, int mask)
470 int retval;
472 retval = sb_permission(inode->i_sb, inode, mask);
473 if (retval)
474 return retval;
475 return __inode_permission(inode, mask);
477 EXPORT_SYMBOL(inode_permission);
480 * path_get - get a reference to a path
481 * @path: path to get the reference to
483 * Given a path increment the reference count to the dentry and the vfsmount.
485 void path_get(const struct path *path)
487 mntget(path->mnt);
488 dget(path->dentry);
490 EXPORT_SYMBOL(path_get);
493 * path_put - put a reference to a path
494 * @path: path to put the reference to
496 * Given a path decrement the reference count to the dentry and the vfsmount.
498 void path_put(const struct path *path)
500 dput(path->dentry);
501 mntput(path->mnt);
503 EXPORT_SYMBOL(path_put);
505 #define EMBEDDED_LEVELS 2
506 struct nameidata {
507 struct path path;
508 struct qstr last;
509 struct path root;
510 struct inode *inode; /* path.dentry.d_inode */
511 unsigned int flags;
512 unsigned seq, m_seq;
513 int last_type;
514 unsigned depth;
515 int total_link_count;
516 struct saved {
517 struct path link;
518 struct delayed_call done;
519 const char *name;
520 unsigned seq;
521 } *stack, internal[EMBEDDED_LEVELS];
522 struct filename *name;
523 struct nameidata *saved;
524 struct inode *link_inode;
525 unsigned root_seq;
526 int dfd;
529 static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
531 struct nameidata *old = current->nameidata;
532 p->stack = p->internal;
533 p->dfd = dfd;
534 p->name = name;
535 p->total_link_count = old ? old->total_link_count : 0;
536 p->saved = old;
537 current->nameidata = p;
540 static void restore_nameidata(void)
542 struct nameidata *now = current->nameidata, *old = now->saved;
544 current->nameidata = old;
545 if (old)
546 old->total_link_count = now->total_link_count;
547 if (now->stack != now->internal)
548 kfree(now->stack);
551 static int __nd_alloc_stack(struct nameidata *nd)
553 struct saved *p;
555 if (nd->flags & LOOKUP_RCU) {
556 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
557 GFP_ATOMIC);
558 if (unlikely(!p))
559 return -ECHILD;
560 } else {
561 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
562 GFP_KERNEL);
563 if (unlikely(!p))
564 return -ENOMEM;
566 memcpy(p, nd->internal, sizeof(nd->internal));
567 nd->stack = p;
568 return 0;
572 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
573 * @path: nameidate to verify
575 * Rename can sometimes move a file or directory outside of a bind
576 * mount, path_connected allows those cases to be detected.
578 static bool path_connected(const struct path *path)
580 struct vfsmount *mnt = path->mnt;
581 struct super_block *sb = mnt->mnt_sb;
583 /* Bind mounts and multi-root filesystems can have disconnected paths */
584 if (!(sb->s_iflags & SB_I_MULTIROOT) && (mnt->mnt_root == sb->s_root))
585 return true;
587 return is_subdir(path->dentry, mnt->mnt_root);
590 static inline int nd_alloc_stack(struct nameidata *nd)
592 if (likely(nd->depth != EMBEDDED_LEVELS))
593 return 0;
594 if (likely(nd->stack != nd->internal))
595 return 0;
596 return __nd_alloc_stack(nd);
599 static void drop_links(struct nameidata *nd)
601 int i = nd->depth;
602 while (i--) {
603 struct saved *last = nd->stack + i;
604 do_delayed_call(&last->done);
605 clear_delayed_call(&last->done);
609 static void terminate_walk(struct nameidata *nd)
611 drop_links(nd);
612 if (!(nd->flags & LOOKUP_RCU)) {
613 int i;
614 path_put(&nd->path);
615 for (i = 0; i < nd->depth; i++)
616 path_put(&nd->stack[i].link);
617 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
618 path_put(&nd->root);
619 nd->root.mnt = NULL;
621 } else {
622 nd->flags &= ~LOOKUP_RCU;
623 if (!(nd->flags & LOOKUP_ROOT))
624 nd->root.mnt = NULL;
625 rcu_read_unlock();
627 nd->depth = 0;
630 /* path_put is needed afterwards regardless of success or failure */
631 static bool legitimize_path(struct nameidata *nd,
632 struct path *path, unsigned seq)
634 int res = __legitimize_mnt(path->mnt, nd->m_seq);
635 if (unlikely(res)) {
636 if (res > 0)
637 path->mnt = NULL;
638 path->dentry = NULL;
639 return false;
641 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
642 path->dentry = NULL;
643 return false;
645 return !read_seqcount_retry(&path->dentry->d_seq, seq);
648 static bool legitimize_links(struct nameidata *nd)
650 int i;
651 for (i = 0; i < nd->depth; i++) {
652 struct saved *last = nd->stack + i;
653 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
654 drop_links(nd);
655 nd->depth = i + 1;
656 return false;
659 return true;
663 * Path walking has 2 modes, rcu-walk and ref-walk (see
664 * Documentation/filesystems/path-lookup.txt). In situations when we can't
665 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
666 * normal reference counts on dentries and vfsmounts to transition to ref-walk
667 * mode. Refcounts are grabbed at the last known good point before rcu-walk
668 * got stuck, so ref-walk may continue from there. If this is not successful
669 * (eg. a seqcount has changed), then failure is returned and it's up to caller
670 * to restart the path walk from the beginning in ref-walk mode.
674 * unlazy_walk - try to switch to ref-walk mode.
675 * @nd: nameidata pathwalk data
676 * @dentry: child of nd->path.dentry or NULL
677 * @seq: seq number to check dentry against
678 * Returns: 0 on success, -ECHILD on failure
680 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
681 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
682 * @nd or NULL. Must be called from rcu-walk context.
683 * Nothing should touch nameidata between unlazy_walk() failure and
684 * terminate_walk().
686 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry, unsigned seq)
688 struct dentry *parent = nd->path.dentry;
690 BUG_ON(!(nd->flags & LOOKUP_RCU));
692 nd->flags &= ~LOOKUP_RCU;
693 if (unlikely(!legitimize_links(nd)))
694 goto out2;
695 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
696 goto out2;
697 if (unlikely(!lockref_get_not_dead(&parent->d_lockref)))
698 goto out1;
701 * For a negative lookup, the lookup sequence point is the parents
702 * sequence point, and it only needs to revalidate the parent dentry.
704 * For a positive lookup, we need to move both the parent and the
705 * dentry from the RCU domain to be properly refcounted. And the
706 * sequence number in the dentry validates *both* dentry counters,
707 * since we checked the sequence number of the parent after we got
708 * the child sequence number. So we know the parent must still
709 * be valid if the child sequence number is still valid.
711 if (!dentry) {
712 if (read_seqcount_retry(&parent->d_seq, nd->seq))
713 goto out;
714 BUG_ON(nd->inode != parent->d_inode);
715 } else {
716 if (!lockref_get_not_dead(&dentry->d_lockref))
717 goto out;
718 if (read_seqcount_retry(&dentry->d_seq, seq))
719 goto drop_dentry;
723 * Sequence counts matched. Now make sure that the root is
724 * still valid and get it if required.
726 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
727 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
728 rcu_read_unlock();
729 dput(dentry);
730 return -ECHILD;
734 rcu_read_unlock();
735 return 0;
737 drop_dentry:
738 rcu_read_unlock();
739 dput(dentry);
740 goto drop_root_mnt;
741 out2:
742 nd->path.mnt = NULL;
743 out1:
744 nd->path.dentry = NULL;
745 out:
746 rcu_read_unlock();
747 drop_root_mnt:
748 if (!(nd->flags & LOOKUP_ROOT))
749 nd->root.mnt = NULL;
750 return -ECHILD;
753 static int unlazy_link(struct nameidata *nd, struct path *link, unsigned seq)
755 if (unlikely(!legitimize_path(nd, link, seq))) {
756 drop_links(nd);
757 nd->depth = 0;
758 nd->flags &= ~LOOKUP_RCU;
759 nd->path.mnt = NULL;
760 nd->path.dentry = NULL;
761 if (!(nd->flags & LOOKUP_ROOT))
762 nd->root.mnt = NULL;
763 rcu_read_unlock();
764 } else if (likely(unlazy_walk(nd, NULL, 0)) == 0) {
765 return 0;
767 path_put(link);
768 return -ECHILD;
771 static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
773 return dentry->d_op->d_revalidate(dentry, flags);
777 * complete_walk - successful completion of path walk
778 * @nd: pointer nameidata
780 * If we had been in RCU mode, drop out of it and legitimize nd->path.
781 * Revalidate the final result, unless we'd already done that during
782 * the path walk or the filesystem doesn't ask for it. Return 0 on
783 * success, -error on failure. In case of failure caller does not
784 * need to drop nd->path.
786 static int complete_walk(struct nameidata *nd)
788 struct dentry *dentry = nd->path.dentry;
789 int status;
791 if (nd->flags & LOOKUP_RCU) {
792 if (!(nd->flags & LOOKUP_ROOT))
793 nd->root.mnt = NULL;
794 if (unlikely(unlazy_walk(nd, NULL, 0)))
795 return -ECHILD;
798 if (likely(!(nd->flags & LOOKUP_JUMPED)))
799 return 0;
801 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
802 return 0;
804 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
805 if (status > 0)
806 return 0;
808 if (!status)
809 status = -ESTALE;
811 return status;
814 static void set_root(struct nameidata *nd)
816 struct fs_struct *fs = current->fs;
818 if (nd->flags & LOOKUP_RCU) {
819 unsigned seq;
821 do {
822 seq = read_seqcount_begin(&fs->seq);
823 nd->root = fs->root;
824 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
825 } while (read_seqcount_retry(&fs->seq, seq));
826 } else {
827 get_fs_root(fs, &nd->root);
831 static void path_put_conditional(struct path *path, struct nameidata *nd)
833 dput(path->dentry);
834 if (path->mnt != nd->path.mnt)
835 mntput(path->mnt);
838 static inline void path_to_nameidata(const struct path *path,
839 struct nameidata *nd)
841 if (!(nd->flags & LOOKUP_RCU)) {
842 dput(nd->path.dentry);
843 if (nd->path.mnt != path->mnt)
844 mntput(nd->path.mnt);
846 nd->path.mnt = path->mnt;
847 nd->path.dentry = path->dentry;
850 static int nd_jump_root(struct nameidata *nd)
852 if (nd->flags & LOOKUP_RCU) {
853 struct dentry *d;
854 nd->path = nd->root;
855 d = nd->path.dentry;
856 nd->inode = d->d_inode;
857 nd->seq = nd->root_seq;
858 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
859 return -ECHILD;
860 } else {
861 path_put(&nd->path);
862 nd->path = nd->root;
863 path_get(&nd->path);
864 nd->inode = nd->path.dentry->d_inode;
866 nd->flags |= LOOKUP_JUMPED;
867 return 0;
871 * Helper to directly jump to a known parsed path from ->get_link,
872 * caller must have taken a reference to path beforehand.
874 void nd_jump_link(struct path *path)
876 struct nameidata *nd = current->nameidata;
877 path_put(&nd->path);
879 nd->path = *path;
880 nd->inode = nd->path.dentry->d_inode;
881 nd->flags |= LOOKUP_JUMPED;
884 static inline void put_link(struct nameidata *nd)
886 struct saved *last = nd->stack + --nd->depth;
887 do_delayed_call(&last->done);
888 if (!(nd->flags & LOOKUP_RCU))
889 path_put(&last->link);
892 int sysctl_protected_symlinks __read_mostly = 0;
893 int sysctl_protected_hardlinks __read_mostly = 0;
896 * may_follow_link - Check symlink following for unsafe situations
897 * @nd: nameidata pathwalk data
899 * In the case of the sysctl_protected_symlinks sysctl being enabled,
900 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
901 * in a sticky world-writable directory. This is to protect privileged
902 * processes from failing races against path names that may change out
903 * from under them by way of other users creating malicious symlinks.
904 * It will permit symlinks to be followed only when outside a sticky
905 * world-writable directory, or when the uid of the symlink and follower
906 * match, or when the directory owner matches the symlink's owner.
908 * Returns 0 if following the symlink is allowed, -ve on error.
910 static inline int may_follow_link(struct nameidata *nd)
912 const struct inode *inode;
913 const struct inode *parent;
914 kuid_t puid;
916 if (!sysctl_protected_symlinks)
917 return 0;
919 /* Allowed if owner and follower match. */
920 inode = nd->link_inode;
921 if (uid_eq(current_cred()->fsuid, inode->i_uid))
922 return 0;
924 /* Allowed if parent directory not sticky and world-writable. */
925 parent = nd->inode;
926 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
927 return 0;
929 /* Allowed if parent directory and link owner match. */
930 puid = parent->i_uid;
931 if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
932 return 0;
934 if (nd->flags & LOOKUP_RCU)
935 return -ECHILD;
937 audit_log_link_denied("follow_link", &nd->stack[0].link);
938 return -EACCES;
942 * safe_hardlink_source - Check for safe hardlink conditions
943 * @inode: the source inode to hardlink from
945 * Return false if at least one of the following conditions:
946 * - inode is not a regular file
947 * - inode is setuid
948 * - inode is setgid and group-exec
949 * - access failure for read and write
951 * Otherwise returns true.
953 static bool safe_hardlink_source(struct inode *inode)
955 umode_t mode = inode->i_mode;
957 /* Special files should not get pinned to the filesystem. */
958 if (!S_ISREG(mode))
959 return false;
961 /* Setuid files should not get pinned to the filesystem. */
962 if (mode & S_ISUID)
963 return false;
965 /* Executable setgid files should not get pinned to the filesystem. */
966 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
967 return false;
969 /* Hardlinking to unreadable or unwritable sources is dangerous. */
970 if (inode_permission(inode, MAY_READ | MAY_WRITE))
971 return false;
973 return true;
977 * may_linkat - Check permissions for creating a hardlink
978 * @link: the source to hardlink from
980 * Block hardlink when all of:
981 * - sysctl_protected_hardlinks enabled
982 * - fsuid does not match inode
983 * - hardlink source is unsafe (see safe_hardlink_source() above)
984 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
986 * Returns 0 if successful, -ve on error.
988 static int may_linkat(struct path *link)
990 struct inode *inode;
992 if (!sysctl_protected_hardlinks)
993 return 0;
995 inode = link->dentry->d_inode;
997 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
998 * otherwise, it must be a safe source.
1000 if (inode_owner_or_capable(inode) || safe_hardlink_source(inode))
1001 return 0;
1003 audit_log_link_denied("linkat", link);
1004 return -EPERM;
1007 static __always_inline
1008 const char *get_link(struct nameidata *nd)
1010 struct saved *last = nd->stack + nd->depth - 1;
1011 struct dentry *dentry = last->link.dentry;
1012 struct inode *inode = nd->link_inode;
1013 int error;
1014 const char *res;
1016 if (!(nd->flags & LOOKUP_RCU)) {
1017 touch_atime(&last->link);
1018 cond_resched();
1019 } else if (atime_needs_update_rcu(&last->link, inode)) {
1020 if (unlikely(unlazy_walk(nd, NULL, 0)))
1021 return ERR_PTR(-ECHILD);
1022 touch_atime(&last->link);
1025 error = security_inode_follow_link(dentry, inode,
1026 nd->flags & LOOKUP_RCU);
1027 if (unlikely(error))
1028 return ERR_PTR(error);
1030 nd->last_type = LAST_BIND;
1031 res = inode->i_link;
1032 if (!res) {
1033 const char * (*get)(struct dentry *, struct inode *,
1034 struct delayed_call *);
1035 get = inode->i_op->get_link;
1036 if (nd->flags & LOOKUP_RCU) {
1037 res = get(NULL, inode, &last->done);
1038 if (res == ERR_PTR(-ECHILD)) {
1039 if (unlikely(unlazy_walk(nd, NULL, 0)))
1040 return ERR_PTR(-ECHILD);
1041 res = get(dentry, inode, &last->done);
1043 } else {
1044 res = get(dentry, inode, &last->done);
1046 if (IS_ERR_OR_NULL(res))
1047 return res;
1049 if (*res == '/') {
1050 if (!nd->root.mnt)
1051 set_root(nd);
1052 if (unlikely(nd_jump_root(nd)))
1053 return ERR_PTR(-ECHILD);
1054 while (unlikely(*++res == '/'))
1057 if (!*res)
1058 res = NULL;
1059 return res;
1063 * follow_up - Find the mountpoint of path's vfsmount
1065 * Given a path, find the mountpoint of its source file system.
1066 * Replace @path with the path of the mountpoint in the parent mount.
1067 * Up is towards /.
1069 * Return 1 if we went up a level and 0 if we were already at the
1070 * root.
1072 int follow_up(struct path *path)
1074 struct mount *mnt = real_mount(path->mnt);
1075 struct mount *parent;
1076 struct dentry *mountpoint;
1078 read_seqlock_excl(&mount_lock);
1079 parent = mnt->mnt_parent;
1080 if (parent == mnt) {
1081 read_sequnlock_excl(&mount_lock);
1082 return 0;
1084 mntget(&parent->mnt);
1085 mountpoint = dget(mnt->mnt_mountpoint);
1086 read_sequnlock_excl(&mount_lock);
1087 dput(path->dentry);
1088 path->dentry = mountpoint;
1089 mntput(path->mnt);
1090 path->mnt = &parent->mnt;
1091 return 1;
1093 EXPORT_SYMBOL(follow_up);
1096 * Perform an automount
1097 * - return -EISDIR to tell follow_managed() to stop and return the path we
1098 * were called with.
1100 static int follow_automount(struct path *path, struct nameidata *nd,
1101 bool *need_mntput)
1103 struct vfsmount *mnt;
1104 int err;
1106 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1107 return -EREMOTE;
1109 /* We don't want to mount if someone's just doing a stat -
1110 * unless they're stat'ing a directory and appended a '/' to
1111 * the name.
1113 * We do, however, want to mount if someone wants to open or
1114 * create a file of any type under the mountpoint, wants to
1115 * traverse through the mountpoint or wants to open the
1116 * mounted directory. Also, autofs may mark negative dentries
1117 * as being automount points. These will need the attentions
1118 * of the daemon to instantiate them before they can be used.
1120 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
1121 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
1122 path->dentry->d_inode)
1123 return -EISDIR;
1125 nd->total_link_count++;
1126 if (nd->total_link_count >= 40)
1127 return -ELOOP;
1129 mnt = path->dentry->d_op->d_automount(path);
1130 if (IS_ERR(mnt)) {
1132 * The filesystem is allowed to return -EISDIR here to indicate
1133 * it doesn't want to automount. For instance, autofs would do
1134 * this so that its userspace daemon can mount on this dentry.
1136 * However, we can only permit this if it's a terminal point in
1137 * the path being looked up; if it wasn't then the remainder of
1138 * the path is inaccessible and we should say so.
1140 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
1141 return -EREMOTE;
1142 return PTR_ERR(mnt);
1145 if (!mnt) /* mount collision */
1146 return 0;
1148 if (!*need_mntput) {
1149 /* lock_mount() may release path->mnt on error */
1150 mntget(path->mnt);
1151 *need_mntput = true;
1153 err = finish_automount(mnt, path);
1155 switch (err) {
1156 case -EBUSY:
1157 /* Someone else made a mount here whilst we were busy */
1158 return 0;
1159 case 0:
1160 path_put(path);
1161 path->mnt = mnt;
1162 path->dentry = dget(mnt->mnt_root);
1163 return 0;
1164 default:
1165 return err;
1171 * Handle a dentry that is managed in some way.
1172 * - Flagged for transit management (autofs)
1173 * - Flagged as mountpoint
1174 * - Flagged as automount point
1176 * This may only be called in refwalk mode.
1178 * Serialization is taken care of in namespace.c
1180 static int follow_managed(struct path *path, struct nameidata *nd)
1182 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
1183 unsigned managed;
1184 bool need_mntput = false;
1185 int ret = 0;
1187 /* Given that we're not holding a lock here, we retain the value in a
1188 * local variable for each dentry as we look at it so that we don't see
1189 * the components of that value change under us */
1190 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1191 managed &= DCACHE_MANAGED_DENTRY,
1192 unlikely(managed != 0)) {
1193 /* Allow the filesystem to manage the transit without i_mutex
1194 * being held. */
1195 if (managed & DCACHE_MANAGE_TRANSIT) {
1196 BUG_ON(!path->dentry->d_op);
1197 BUG_ON(!path->dentry->d_op->d_manage);
1198 ret = path->dentry->d_op->d_manage(path->dentry, false);
1199 if (ret < 0)
1200 break;
1203 /* Transit to a mounted filesystem. */
1204 if (managed & DCACHE_MOUNTED) {
1205 struct vfsmount *mounted = lookup_mnt(path);
1206 if (mounted) {
1207 dput(path->dentry);
1208 if (need_mntput)
1209 mntput(path->mnt);
1210 path->mnt = mounted;
1211 path->dentry = dget(mounted->mnt_root);
1212 need_mntput = true;
1213 continue;
1216 /* Something is mounted on this dentry in another
1217 * namespace and/or whatever was mounted there in this
1218 * namespace got unmounted before lookup_mnt() could
1219 * get it */
1222 /* Handle an automount point */
1223 if (managed & DCACHE_NEED_AUTOMOUNT) {
1224 ret = follow_automount(path, nd, &need_mntput);
1225 if (ret < 0)
1226 break;
1227 continue;
1230 /* We didn't change the current path point */
1231 break;
1234 if (need_mntput && path->mnt == mnt)
1235 mntput(path->mnt);
1236 if (ret == -EISDIR || !ret)
1237 ret = 1;
1238 if (need_mntput)
1239 nd->flags |= LOOKUP_JUMPED;
1240 if (unlikely(ret < 0))
1241 path_put_conditional(path, nd);
1242 return ret;
1245 int follow_down_one(struct path *path)
1247 struct vfsmount *mounted;
1249 mounted = lookup_mnt(path);
1250 if (mounted) {
1251 dput(path->dentry);
1252 mntput(path->mnt);
1253 path->mnt = mounted;
1254 path->dentry = dget(mounted->mnt_root);
1255 return 1;
1257 return 0;
1259 EXPORT_SYMBOL(follow_down_one);
1261 static inline int managed_dentry_rcu(struct dentry *dentry)
1263 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1264 dentry->d_op->d_manage(dentry, true) : 0;
1268 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1269 * we meet a managed dentry that would need blocking.
1271 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1272 struct inode **inode, unsigned *seqp)
1274 for (;;) {
1275 struct mount *mounted;
1277 * Don't forget we might have a non-mountpoint managed dentry
1278 * that wants to block transit.
1280 switch (managed_dentry_rcu(path->dentry)) {
1281 case -ECHILD:
1282 default:
1283 return false;
1284 case -EISDIR:
1285 return true;
1286 case 0:
1287 break;
1290 if (!d_mountpoint(path->dentry))
1291 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1293 mounted = __lookup_mnt(path->mnt, path->dentry);
1294 if (!mounted)
1295 break;
1296 path->mnt = &mounted->mnt;
1297 path->dentry = mounted->mnt.mnt_root;
1298 nd->flags |= LOOKUP_JUMPED;
1299 *seqp = read_seqcount_begin(&path->dentry->d_seq);
1301 * Update the inode too. We don't need to re-check the
1302 * dentry sequence number here after this d_inode read,
1303 * because a mount-point is always pinned.
1305 *inode = path->dentry->d_inode;
1307 return !read_seqretry(&mount_lock, nd->m_seq) &&
1308 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1311 static int follow_dotdot_rcu(struct nameidata *nd)
1313 struct inode *inode = nd->inode;
1315 while (1) {
1316 if (path_equal(&nd->path, &nd->root))
1317 break;
1318 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1319 struct dentry *old = nd->path.dentry;
1320 struct dentry *parent = old->d_parent;
1321 unsigned seq;
1323 inode = parent->d_inode;
1324 seq = read_seqcount_begin(&parent->d_seq);
1325 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1326 return -ECHILD;
1327 nd->path.dentry = parent;
1328 nd->seq = seq;
1329 if (unlikely(!path_connected(&nd->path)))
1330 return -ENOENT;
1331 break;
1332 } else {
1333 struct mount *mnt = real_mount(nd->path.mnt);
1334 struct mount *mparent = mnt->mnt_parent;
1335 struct dentry *mountpoint = mnt->mnt_mountpoint;
1336 struct inode *inode2 = mountpoint->d_inode;
1337 unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1338 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1339 return -ECHILD;
1340 if (&mparent->mnt == nd->path.mnt)
1341 break;
1342 /* we know that mountpoint was pinned */
1343 nd->path.dentry = mountpoint;
1344 nd->path.mnt = &mparent->mnt;
1345 inode = inode2;
1346 nd->seq = seq;
1349 while (unlikely(d_mountpoint(nd->path.dentry))) {
1350 struct mount *mounted;
1351 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1352 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1353 return -ECHILD;
1354 if (!mounted)
1355 break;
1356 nd->path.mnt = &mounted->mnt;
1357 nd->path.dentry = mounted->mnt.mnt_root;
1358 inode = nd->path.dentry->d_inode;
1359 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1361 nd->inode = inode;
1362 return 0;
1366 * Follow down to the covering mount currently visible to userspace. At each
1367 * point, the filesystem owning that dentry may be queried as to whether the
1368 * caller is permitted to proceed or not.
1370 int follow_down(struct path *path)
1372 unsigned managed;
1373 int ret;
1375 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1376 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1377 /* Allow the filesystem to manage the transit without i_mutex
1378 * being held.
1380 * We indicate to the filesystem if someone is trying to mount
1381 * something here. This gives autofs the chance to deny anyone
1382 * other than its daemon the right to mount on its
1383 * superstructure.
1385 * The filesystem may sleep at this point.
1387 if (managed & DCACHE_MANAGE_TRANSIT) {
1388 BUG_ON(!path->dentry->d_op);
1389 BUG_ON(!path->dentry->d_op->d_manage);
1390 ret = path->dentry->d_op->d_manage(
1391 path->dentry, false);
1392 if (ret < 0)
1393 return ret == -EISDIR ? 0 : ret;
1396 /* Transit to a mounted filesystem. */
1397 if (managed & DCACHE_MOUNTED) {
1398 struct vfsmount *mounted = lookup_mnt(path);
1399 if (!mounted)
1400 break;
1401 dput(path->dentry);
1402 mntput(path->mnt);
1403 path->mnt = mounted;
1404 path->dentry = dget(mounted->mnt_root);
1405 continue;
1408 /* Don't handle automount points here */
1409 break;
1411 return 0;
1413 EXPORT_SYMBOL(follow_down);
1416 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1418 static void follow_mount(struct path *path)
1420 while (d_mountpoint(path->dentry)) {
1421 struct vfsmount *mounted = lookup_mnt(path);
1422 if (!mounted)
1423 break;
1424 dput(path->dentry);
1425 mntput(path->mnt);
1426 path->mnt = mounted;
1427 path->dentry = dget(mounted->mnt_root);
1431 static int path_parent_directory(struct path *path)
1433 struct dentry *old = path->dentry;
1434 /* rare case of legitimate dget_parent()... */
1435 path->dentry = dget_parent(path->dentry);
1436 dput(old);
1437 if (unlikely(!path_connected(path)))
1438 return -ENOENT;
1439 return 0;
1442 static int follow_dotdot(struct nameidata *nd)
1444 while(1) {
1445 if (nd->path.dentry == nd->root.dentry &&
1446 nd->path.mnt == nd->root.mnt) {
1447 break;
1449 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1450 int ret = path_parent_directory(&nd->path);
1451 if (ret)
1452 return ret;
1453 break;
1455 if (!follow_up(&nd->path))
1456 break;
1458 follow_mount(&nd->path);
1459 nd->inode = nd->path.dentry->d_inode;
1460 return 0;
1464 * This looks up the name in dcache and possibly revalidates the found dentry.
1465 * NULL is returned if the dentry does not exist in the cache.
1467 static struct dentry *lookup_dcache(const struct qstr *name,
1468 struct dentry *dir,
1469 unsigned int flags)
1471 struct dentry *dentry;
1472 int error;
1474 dentry = d_lookup(dir, name);
1475 if (dentry) {
1476 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1477 error = d_revalidate(dentry, flags);
1478 if (unlikely(error <= 0)) {
1479 if (!error)
1480 d_invalidate(dentry);
1481 dput(dentry);
1482 return ERR_PTR(error);
1486 return dentry;
1490 * Call i_op->lookup on the dentry. The dentry must be negative and
1491 * unhashed.
1493 * dir->d_inode->i_mutex must be held
1495 static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1496 unsigned int flags)
1498 struct dentry *old;
1500 /* Don't create child dentry for a dead directory. */
1501 if (unlikely(IS_DEADDIR(dir))) {
1502 dput(dentry);
1503 return ERR_PTR(-ENOENT);
1506 old = dir->i_op->lookup(dir, dentry, flags);
1507 if (unlikely(old)) {
1508 dput(dentry);
1509 dentry = old;
1511 return dentry;
1514 static struct dentry *__lookup_hash(const struct qstr *name,
1515 struct dentry *base, unsigned int flags)
1517 struct dentry *dentry = lookup_dcache(name, base, flags);
1519 if (dentry)
1520 return dentry;
1522 dentry = d_alloc(base, name);
1523 if (unlikely(!dentry))
1524 return ERR_PTR(-ENOMEM);
1526 return lookup_real(base->d_inode, dentry, flags);
1529 static int lookup_fast(struct nameidata *nd,
1530 struct path *path, struct inode **inode,
1531 unsigned *seqp)
1533 struct vfsmount *mnt = nd->path.mnt;
1534 struct dentry *dentry, *parent = nd->path.dentry;
1535 int status = 1;
1536 int err;
1539 * Rename seqlock is not required here because in the off chance
1540 * of a false negative due to a concurrent rename, the caller is
1541 * going to fall back to non-racy lookup.
1543 if (nd->flags & LOOKUP_RCU) {
1544 unsigned seq;
1545 bool negative;
1546 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
1547 if (unlikely(!dentry)) {
1548 if (unlazy_walk(nd, NULL, 0))
1549 return -ECHILD;
1550 return 0;
1554 * This sequence count validates that the inode matches
1555 * the dentry name information from lookup.
1557 *inode = d_backing_inode(dentry);
1558 negative = d_is_negative(dentry);
1559 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
1560 return -ECHILD;
1563 * This sequence count validates that the parent had no
1564 * changes while we did the lookup of the dentry above.
1566 * The memory barrier in read_seqcount_begin of child is
1567 * enough, we can use __read_seqcount_retry here.
1569 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
1570 return -ECHILD;
1572 *seqp = seq;
1573 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
1574 status = d_revalidate(dentry, nd->flags);
1575 if (unlikely(status <= 0)) {
1576 if (unlazy_walk(nd, dentry, seq))
1577 return -ECHILD;
1578 if (status == -ECHILD)
1579 status = d_revalidate(dentry, nd->flags);
1580 } else {
1582 * Note: do negative dentry check after revalidation in
1583 * case that drops it.
1585 if (unlikely(negative))
1586 return -ENOENT;
1587 path->mnt = mnt;
1588 path->dentry = dentry;
1589 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
1590 return 1;
1591 if (unlazy_walk(nd, dentry, seq))
1592 return -ECHILD;
1594 } else {
1595 dentry = __d_lookup(parent, &nd->last);
1596 if (unlikely(!dentry))
1597 return 0;
1598 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
1599 status = d_revalidate(dentry, nd->flags);
1601 if (unlikely(status <= 0)) {
1602 if (!status)
1603 d_invalidate(dentry);
1604 dput(dentry);
1605 return status;
1607 if (unlikely(d_is_negative(dentry))) {
1608 dput(dentry);
1609 return -ENOENT;
1612 path->mnt = mnt;
1613 path->dentry = dentry;
1614 err = follow_managed(path, nd);
1615 if (likely(err > 0))
1616 *inode = d_backing_inode(path->dentry);
1617 return err;
1620 /* Fast lookup failed, do it the slow way */
1621 static struct dentry *lookup_slow(const struct qstr *name,
1622 struct dentry *dir,
1623 unsigned int flags)
1625 struct dentry *dentry = ERR_PTR(-ENOENT), *old;
1626 struct inode *inode = dir->d_inode;
1627 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1629 inode_lock_shared(inode);
1630 /* Don't go there if it's already dead */
1631 if (unlikely(IS_DEADDIR(inode)))
1632 goto out;
1633 again:
1634 dentry = d_alloc_parallel(dir, name, &wq);
1635 if (IS_ERR(dentry))
1636 goto out;
1637 if (unlikely(!d_in_lookup(dentry))) {
1638 if ((dentry->d_flags & DCACHE_OP_REVALIDATE) &&
1639 !(flags & LOOKUP_NO_REVAL)) {
1640 int error = d_revalidate(dentry, flags);
1641 if (unlikely(error <= 0)) {
1642 if (!error) {
1643 d_invalidate(dentry);
1644 dput(dentry);
1645 goto again;
1647 dput(dentry);
1648 dentry = ERR_PTR(error);
1651 } else {
1652 old = inode->i_op->lookup(inode, dentry, flags);
1653 d_lookup_done(dentry);
1654 if (unlikely(old)) {
1655 dput(dentry);
1656 dentry = old;
1659 out:
1660 inode_unlock_shared(inode);
1661 return dentry;
1664 static inline int may_lookup(struct nameidata *nd)
1666 if (nd->flags & LOOKUP_RCU) {
1667 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1668 if (err != -ECHILD)
1669 return err;
1670 if (unlazy_walk(nd, NULL, 0))
1671 return -ECHILD;
1673 return inode_permission(nd->inode, MAY_EXEC);
1676 static inline int handle_dots(struct nameidata *nd, int type)
1678 if (type == LAST_DOTDOT) {
1679 if (!nd->root.mnt)
1680 set_root(nd);
1681 if (nd->flags & LOOKUP_RCU) {
1682 return follow_dotdot_rcu(nd);
1683 } else
1684 return follow_dotdot(nd);
1686 return 0;
1689 static int pick_link(struct nameidata *nd, struct path *link,
1690 struct inode *inode, unsigned seq)
1692 int error;
1693 struct saved *last;
1694 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
1695 path_to_nameidata(link, nd);
1696 return -ELOOP;
1698 if (!(nd->flags & LOOKUP_RCU)) {
1699 if (link->mnt == nd->path.mnt)
1700 mntget(link->mnt);
1702 error = nd_alloc_stack(nd);
1703 if (unlikely(error)) {
1704 if (error == -ECHILD) {
1705 if (unlikely(unlazy_link(nd, link, seq)))
1706 return -ECHILD;
1707 error = nd_alloc_stack(nd);
1709 if (error) {
1710 path_put(link);
1711 return error;
1715 last = nd->stack + nd->depth++;
1716 last->link = *link;
1717 clear_delayed_call(&last->done);
1718 nd->link_inode = inode;
1719 last->seq = seq;
1720 return 1;
1724 * Do we need to follow links? We _really_ want to be able
1725 * to do this check without having to look at inode->i_op,
1726 * so we keep a cache of "no, this doesn't need follow_link"
1727 * for the common case.
1729 static inline int should_follow_link(struct nameidata *nd, struct path *link,
1730 int follow,
1731 struct inode *inode, unsigned seq)
1733 if (likely(!d_is_symlink(link->dentry)))
1734 return 0;
1735 if (!follow)
1736 return 0;
1737 /* make sure that d_is_symlink above matches inode */
1738 if (nd->flags & LOOKUP_RCU) {
1739 if (read_seqcount_retry(&link->dentry->d_seq, seq))
1740 return -ECHILD;
1742 return pick_link(nd, link, inode, seq);
1745 enum {WALK_GET = 1, WALK_PUT = 2};
1747 static int walk_component(struct nameidata *nd, int flags)
1749 struct path path;
1750 struct inode *inode;
1751 unsigned seq;
1752 int err;
1754 * "." and ".." are special - ".." especially so because it has
1755 * to be able to know about the current root directory and
1756 * parent relationships.
1758 if (unlikely(nd->last_type != LAST_NORM)) {
1759 err = handle_dots(nd, nd->last_type);
1760 if (flags & WALK_PUT)
1761 put_link(nd);
1762 return err;
1764 err = lookup_fast(nd, &path, &inode, &seq);
1765 if (unlikely(err <= 0)) {
1766 if (err < 0)
1767 return err;
1768 path.dentry = lookup_slow(&nd->last, nd->path.dentry,
1769 nd->flags);
1770 if (IS_ERR(path.dentry))
1771 return PTR_ERR(path.dentry);
1773 path.mnt = nd->path.mnt;
1774 err = follow_managed(&path, nd);
1775 if (unlikely(err < 0))
1776 return err;
1778 if (unlikely(d_is_negative(path.dentry))) {
1779 path_to_nameidata(&path, nd);
1780 return -ENOENT;
1783 seq = 0; /* we are already out of RCU mode */
1784 inode = d_backing_inode(path.dentry);
1787 if (flags & WALK_PUT)
1788 put_link(nd);
1789 err = should_follow_link(nd, &path, flags & WALK_GET, inode, seq);
1790 if (unlikely(err))
1791 return err;
1792 path_to_nameidata(&path, nd);
1793 nd->inode = inode;
1794 nd->seq = seq;
1795 return 0;
1799 * We can do the critical dentry name comparison and hashing
1800 * operations one word at a time, but we are limited to:
1802 * - Architectures with fast unaligned word accesses. We could
1803 * do a "get_unaligned()" if this helps and is sufficiently
1804 * fast.
1806 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1807 * do not trap on the (extremely unlikely) case of a page
1808 * crossing operation.
1810 * - Furthermore, we need an efficient 64-bit compile for the
1811 * 64-bit case in order to generate the "number of bytes in
1812 * the final mask". Again, that could be replaced with a
1813 * efficient population count instruction or similar.
1815 #ifdef CONFIG_DCACHE_WORD_ACCESS
1817 #include <asm/word-at-a-time.h>
1819 #ifdef HASH_MIX
1821 /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1823 #elif defined(CONFIG_64BIT)
1825 * Register pressure in the mixing function is an issue, particularly
1826 * on 32-bit x86, but almost any function requires one state value and
1827 * one temporary. Instead, use a function designed for two state values
1828 * and no temporaries.
1830 * This function cannot create a collision in only two iterations, so
1831 * we have two iterations to achieve avalanche. In those two iterations,
1832 * we have six layers of mixing, which is enough to spread one bit's
1833 * influence out to 2^6 = 64 state bits.
1835 * Rotate constants are scored by considering either 64 one-bit input
1836 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
1837 * probability of that delta causing a change to each of the 128 output
1838 * bits, using a sample of random initial states.
1840 * The Shannon entropy of the computed probabilities is then summed
1841 * to produce a score. Ideally, any input change has a 50% chance of
1842 * toggling any given output bit.
1844 * Mixing scores (in bits) for (12,45):
1845 * Input delta: 1-bit 2-bit
1846 * 1 round: 713.3 42542.6
1847 * 2 rounds: 2753.7 140389.8
1848 * 3 rounds: 5954.1 233458.2
1849 * 4 rounds: 7862.6 256672.2
1850 * Perfect: 8192 258048
1851 * (64*128) (64*63/2 * 128)
1853 #define HASH_MIX(x, y, a) \
1854 ( x ^= (a), \
1855 y ^= x, x = rol64(x,12),\
1856 x += y, y = rol64(y,45),\
1857 y *= 9 )
1860 * Fold two longs into one 32-bit hash value. This must be fast, but
1861 * latency isn't quite as critical, as there is a fair bit of additional
1862 * work done before the hash value is used.
1864 static inline unsigned int fold_hash(unsigned long x, unsigned long y)
1866 y ^= x * GOLDEN_RATIO_64;
1867 y *= GOLDEN_RATIO_64;
1868 return y >> 32;
1871 #else /* 32-bit case */
1874 * Mixing scores (in bits) for (7,20):
1875 * Input delta: 1-bit 2-bit
1876 * 1 round: 330.3 9201.6
1877 * 2 rounds: 1246.4 25475.4
1878 * 3 rounds: 1907.1 31295.1
1879 * 4 rounds: 2042.3 31718.6
1880 * Perfect: 2048 31744
1881 * (32*64) (32*31/2 * 64)
1883 #define HASH_MIX(x, y, a) \
1884 ( x ^= (a), \
1885 y ^= x, x = rol32(x, 7),\
1886 x += y, y = rol32(y,20),\
1887 y *= 9 )
1889 static inline unsigned int fold_hash(unsigned long x, unsigned long y)
1891 /* Use arch-optimized multiply if one exists */
1892 return __hash_32(y ^ __hash_32(x));
1895 #endif
1898 * Return the hash of a string of known length. This is carfully
1899 * designed to match hash_name(), which is the more critical function.
1900 * In particular, we must end by hashing a final word containing 0..7
1901 * payload bytes, to match the way that hash_name() iterates until it
1902 * finds the delimiter after the name.
1904 unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
1906 unsigned long a, x = 0, y = (unsigned long)salt;
1908 for (;;) {
1909 if (!len)
1910 goto done;
1911 a = load_unaligned_zeropad(name);
1912 if (len < sizeof(unsigned long))
1913 break;
1914 HASH_MIX(x, y, a);
1915 name += sizeof(unsigned long);
1916 len -= sizeof(unsigned long);
1918 x ^= a & bytemask_from_count(len);
1919 done:
1920 return fold_hash(x, y);
1922 EXPORT_SYMBOL(full_name_hash);
1924 /* Return the "hash_len" (hash and length) of a null-terminated string */
1925 u64 hashlen_string(const void *salt, const char *name)
1927 unsigned long a = 0, x = 0, y = (unsigned long)salt;
1928 unsigned long adata, mask, len;
1929 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1931 len = 0;
1932 goto inside;
1934 do {
1935 HASH_MIX(x, y, a);
1936 len += sizeof(unsigned long);
1937 inside:
1938 a = load_unaligned_zeropad(name+len);
1939 } while (!has_zero(a, &adata, &constants));
1941 adata = prep_zero_mask(a, adata, &constants);
1942 mask = create_zero_mask(adata);
1943 x ^= a & zero_bytemask(mask);
1945 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
1947 EXPORT_SYMBOL(hashlen_string);
1950 * Calculate the length and hash of the path component, and
1951 * return the "hash_len" as the result.
1953 static inline u64 hash_name(const void *salt, const char *name)
1955 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
1956 unsigned long adata, bdata, mask, len;
1957 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1959 len = 0;
1960 goto inside;
1962 do {
1963 HASH_MIX(x, y, a);
1964 len += sizeof(unsigned long);
1965 inside:
1966 a = load_unaligned_zeropad(name+len);
1967 b = a ^ REPEAT_BYTE('/');
1968 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1970 adata = prep_zero_mask(a, adata, &constants);
1971 bdata = prep_zero_mask(b, bdata, &constants);
1972 mask = create_zero_mask(adata | bdata);
1973 x ^= a & zero_bytemask(mask);
1975 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
1978 #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
1980 /* Return the hash of a string of known length */
1981 unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
1983 unsigned long hash = init_name_hash(salt);
1984 while (len--)
1985 hash = partial_name_hash((unsigned char)*name++, hash);
1986 return end_name_hash(hash);
1988 EXPORT_SYMBOL(full_name_hash);
1990 /* Return the "hash_len" (hash and length) of a null-terminated string */
1991 u64 hashlen_string(const void *salt, const char *name)
1993 unsigned long hash = init_name_hash(salt);
1994 unsigned long len = 0, c;
1996 c = (unsigned char)*name;
1997 while (c) {
1998 len++;
1999 hash = partial_name_hash(c, hash);
2000 c = (unsigned char)name[len];
2002 return hashlen_create(end_name_hash(hash), len);
2004 EXPORT_SYMBOL(hashlen_string);
2007 * We know there's a real path component here of at least
2008 * one character.
2010 static inline u64 hash_name(const void *salt, const char *name)
2012 unsigned long hash = init_name_hash(salt);
2013 unsigned long len = 0, c;
2015 c = (unsigned char)*name;
2016 do {
2017 len++;
2018 hash = partial_name_hash(c, hash);
2019 c = (unsigned char)name[len];
2020 } while (c && c != '/');
2021 return hashlen_create(end_name_hash(hash), len);
2024 #endif
2027 * Name resolution.
2028 * This is the basic name resolution function, turning a pathname into
2029 * the final dentry. We expect 'base' to be positive and a directory.
2031 * Returns 0 and nd will have valid dentry and mnt on success.
2032 * Returns error and drops reference to input namei data on failure.
2034 static int link_path_walk(const char *name, struct nameidata *nd)
2036 int err;
2038 while (*name=='/')
2039 name++;
2040 if (!*name)
2041 return 0;
2043 /* At this point we know we have a real path component. */
2044 for(;;) {
2045 u64 hash_len;
2046 int type;
2048 err = may_lookup(nd);
2049 if (err)
2050 return err;
2052 hash_len = hash_name(nd->path.dentry, name);
2054 type = LAST_NORM;
2055 if (name[0] == '.') switch (hashlen_len(hash_len)) {
2056 case 2:
2057 if (name[1] == '.') {
2058 type = LAST_DOTDOT;
2059 nd->flags |= LOOKUP_JUMPED;
2061 break;
2062 case 1:
2063 type = LAST_DOT;
2065 if (likely(type == LAST_NORM)) {
2066 struct dentry *parent = nd->path.dentry;
2067 nd->flags &= ~LOOKUP_JUMPED;
2068 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
2069 struct qstr this = { { .hash_len = hash_len }, .name = name };
2070 err = parent->d_op->d_hash(parent, &this);
2071 if (err < 0)
2072 return err;
2073 hash_len = this.hash_len;
2074 name = this.name;
2078 nd->last.hash_len = hash_len;
2079 nd->last.name = name;
2080 nd->last_type = type;
2082 name += hashlen_len(hash_len);
2083 if (!*name)
2084 goto OK;
2086 * If it wasn't NUL, we know it was '/'. Skip that
2087 * slash, and continue until no more slashes.
2089 do {
2090 name++;
2091 } while (unlikely(*name == '/'));
2092 if (unlikely(!*name)) {
2094 /* pathname body, done */
2095 if (!nd->depth)
2096 return 0;
2097 name = nd->stack[nd->depth - 1].name;
2098 /* trailing symlink, done */
2099 if (!name)
2100 return 0;
2101 /* last component of nested symlink */
2102 err = walk_component(nd, WALK_GET | WALK_PUT);
2103 } else {
2104 err = walk_component(nd, WALK_GET);
2106 if (err < 0)
2107 return err;
2109 if (err) {
2110 const char *s = get_link(nd);
2112 if (IS_ERR(s))
2113 return PTR_ERR(s);
2114 err = 0;
2115 if (unlikely(!s)) {
2116 /* jumped */
2117 put_link(nd);
2118 } else {
2119 nd->stack[nd->depth - 1].name = name;
2120 name = s;
2121 continue;
2124 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2125 if (nd->flags & LOOKUP_RCU) {
2126 if (unlazy_walk(nd, NULL, 0))
2127 return -ECHILD;
2129 return -ENOTDIR;
2134 static const char *path_init(struct nameidata *nd, unsigned flags)
2136 int retval = 0;
2137 const char *s = nd->name->name;
2139 if (!*s)
2140 flags &= ~LOOKUP_RCU;
2142 nd->last_type = LAST_ROOT; /* if there are only slashes... */
2143 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
2144 nd->depth = 0;
2145 if (flags & LOOKUP_ROOT) {
2146 struct dentry *root = nd->root.dentry;
2147 struct inode *inode = root->d_inode;
2148 if (*s) {
2149 if (!d_can_lookup(root))
2150 return ERR_PTR(-ENOTDIR);
2151 retval = inode_permission(inode, MAY_EXEC);
2152 if (retval)
2153 return ERR_PTR(retval);
2155 nd->path = nd->root;
2156 nd->inode = inode;
2157 if (flags & LOOKUP_RCU) {
2158 rcu_read_lock();
2159 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2160 nd->root_seq = nd->seq;
2161 nd->m_seq = read_seqbegin(&mount_lock);
2162 } else {
2163 path_get(&nd->path);
2165 return s;
2168 nd->root.mnt = NULL;
2169 nd->path.mnt = NULL;
2170 nd->path.dentry = NULL;
2172 nd->m_seq = read_seqbegin(&mount_lock);
2173 if (*s == '/') {
2174 if (flags & LOOKUP_RCU)
2175 rcu_read_lock();
2176 set_root(nd);
2177 if (likely(!nd_jump_root(nd)))
2178 return s;
2179 nd->root.mnt = NULL;
2180 rcu_read_unlock();
2181 return ERR_PTR(-ECHILD);
2182 } else if (nd->dfd == AT_FDCWD) {
2183 if (flags & LOOKUP_RCU) {
2184 struct fs_struct *fs = current->fs;
2185 unsigned seq;
2187 rcu_read_lock();
2189 do {
2190 seq = read_seqcount_begin(&fs->seq);
2191 nd->path = fs->pwd;
2192 nd->inode = nd->path.dentry->d_inode;
2193 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2194 } while (read_seqcount_retry(&fs->seq, seq));
2195 } else {
2196 get_fs_pwd(current->fs, &nd->path);
2197 nd->inode = nd->path.dentry->d_inode;
2199 return s;
2200 } else {
2201 /* Caller must check execute permissions on the starting path component */
2202 struct fd f = fdget_raw(nd->dfd);
2203 struct dentry *dentry;
2205 if (!f.file)
2206 return ERR_PTR(-EBADF);
2208 dentry = f.file->f_path.dentry;
2210 if (*s) {
2211 if (!d_can_lookup(dentry)) {
2212 fdput(f);
2213 return ERR_PTR(-ENOTDIR);
2217 nd->path = f.file->f_path;
2218 if (flags & LOOKUP_RCU) {
2219 rcu_read_lock();
2220 nd->inode = nd->path.dentry->d_inode;
2221 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
2222 } else {
2223 path_get(&nd->path);
2224 nd->inode = nd->path.dentry->d_inode;
2226 fdput(f);
2227 return s;
2231 static const char *trailing_symlink(struct nameidata *nd)
2233 const char *s;
2234 int error = may_follow_link(nd);
2235 if (unlikely(error))
2236 return ERR_PTR(error);
2237 nd->flags |= LOOKUP_PARENT;
2238 nd->stack[0].name = NULL;
2239 s = get_link(nd);
2240 return s ? s : "";
2243 static inline int lookup_last(struct nameidata *nd)
2245 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2246 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2248 nd->flags &= ~LOOKUP_PARENT;
2249 return walk_component(nd,
2250 nd->flags & LOOKUP_FOLLOW
2251 ? nd->depth
2252 ? WALK_PUT | WALK_GET
2253 : WALK_GET
2254 : 0);
2257 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2258 static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
2260 const char *s = path_init(nd, flags);
2261 int err;
2263 if (IS_ERR(s))
2264 return PTR_ERR(s);
2265 while (!(err = link_path_walk(s, nd))
2266 && ((err = lookup_last(nd)) > 0)) {
2267 s = trailing_symlink(nd);
2268 if (IS_ERR(s)) {
2269 err = PTR_ERR(s);
2270 break;
2273 if (!err)
2274 err = complete_walk(nd);
2276 if (!err && nd->flags & LOOKUP_DIRECTORY)
2277 if (!d_can_lookup(nd->path.dentry))
2278 err = -ENOTDIR;
2279 if (!err) {
2280 *path = nd->path;
2281 nd->path.mnt = NULL;
2282 nd->path.dentry = NULL;
2284 terminate_walk(nd);
2285 return err;
2288 static int filename_lookup(int dfd, struct filename *name, unsigned flags,
2289 struct path *path, struct path *root)
2291 int retval;
2292 struct nameidata nd;
2293 if (IS_ERR(name))
2294 return PTR_ERR(name);
2295 if (unlikely(root)) {
2296 nd.root = *root;
2297 flags |= LOOKUP_ROOT;
2299 set_nameidata(&nd, dfd, name);
2300 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
2301 if (unlikely(retval == -ECHILD))
2302 retval = path_lookupat(&nd, flags, path);
2303 if (unlikely(retval == -ESTALE))
2304 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
2306 if (likely(!retval))
2307 audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
2308 restore_nameidata();
2309 putname(name);
2310 return retval;
2313 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2314 static int path_parentat(struct nameidata *nd, unsigned flags,
2315 struct path *parent)
2317 const char *s = path_init(nd, flags);
2318 int err;
2319 if (IS_ERR(s))
2320 return PTR_ERR(s);
2321 err = link_path_walk(s, nd);
2322 if (!err)
2323 err = complete_walk(nd);
2324 if (!err) {
2325 *parent = nd->path;
2326 nd->path.mnt = NULL;
2327 nd->path.dentry = NULL;
2329 terminate_walk(nd);
2330 return err;
2333 static struct filename *filename_parentat(int dfd, struct filename *name,
2334 unsigned int flags, struct path *parent,
2335 struct qstr *last, int *type)
2337 int retval;
2338 struct nameidata nd;
2340 if (IS_ERR(name))
2341 return name;
2342 set_nameidata(&nd, dfd, name);
2343 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
2344 if (unlikely(retval == -ECHILD))
2345 retval = path_parentat(&nd, flags, parent);
2346 if (unlikely(retval == -ESTALE))
2347 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
2348 if (likely(!retval)) {
2349 *last = nd.last;
2350 *type = nd.last_type;
2351 audit_inode(name, parent->dentry, LOOKUP_PARENT);
2352 } else {
2353 putname(name);
2354 name = ERR_PTR(retval);
2356 restore_nameidata();
2357 return name;
2360 /* does lookup, returns the object with parent locked */
2361 struct dentry *kern_path_locked(const char *name, struct path *path)
2363 struct filename *filename;
2364 struct dentry *d;
2365 struct qstr last;
2366 int type;
2368 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2369 &last, &type);
2370 if (IS_ERR(filename))
2371 return ERR_CAST(filename);
2372 if (unlikely(type != LAST_NORM)) {
2373 path_put(path);
2374 putname(filename);
2375 return ERR_PTR(-EINVAL);
2377 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
2378 d = __lookup_hash(&last, path->dentry, 0);
2379 if (IS_ERR(d)) {
2380 inode_unlock(path->dentry->d_inode);
2381 path_put(path);
2383 putname(filename);
2384 return d;
2387 int kern_path(const char *name, unsigned int flags, struct path *path)
2389 return filename_lookup(AT_FDCWD, getname_kernel(name),
2390 flags, path, NULL);
2392 EXPORT_SYMBOL(kern_path);
2395 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2396 * @dentry: pointer to dentry of the base directory
2397 * @mnt: pointer to vfs mount of the base directory
2398 * @name: pointer to file name
2399 * @flags: lookup flags
2400 * @path: pointer to struct path to fill
2402 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2403 const char *name, unsigned int flags,
2404 struct path *path)
2406 struct path root = {.mnt = mnt, .dentry = dentry};
2407 /* the first argument of filename_lookup() is ignored with root */
2408 return filename_lookup(AT_FDCWD, getname_kernel(name),
2409 flags , path, &root);
2411 EXPORT_SYMBOL(vfs_path_lookup);
2414 * lookup_one_len - filesystem helper to lookup single pathname component
2415 * @name: pathname component to lookup
2416 * @base: base directory to lookup from
2417 * @len: maximum length @len should be interpreted to
2419 * Note that this routine is purely a helper for filesystem usage and should
2420 * not be called by generic code.
2422 * The caller must hold base->i_mutex.
2424 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2426 struct qstr this;
2427 unsigned int c;
2428 int err;
2430 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2432 this.name = name;
2433 this.len = len;
2434 this.hash = full_name_hash(base, name, len);
2435 if (!len)
2436 return ERR_PTR(-EACCES);
2438 if (unlikely(name[0] == '.')) {
2439 if (len < 2 || (len == 2 && name[1] == '.'))
2440 return ERR_PTR(-EACCES);
2443 while (len--) {
2444 c = *(const unsigned char *)name++;
2445 if (c == '/' || c == '\0')
2446 return ERR_PTR(-EACCES);
2449 * See if the low-level filesystem might want
2450 * to use its own hash..
2452 if (base->d_flags & DCACHE_OP_HASH) {
2453 int err = base->d_op->d_hash(base, &this);
2454 if (err < 0)
2455 return ERR_PTR(err);
2458 err = inode_permission(base->d_inode, MAY_EXEC);
2459 if (err)
2460 return ERR_PTR(err);
2462 return __lookup_hash(&this, base, 0);
2464 EXPORT_SYMBOL(lookup_one_len);
2467 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2468 * @name: pathname component to lookup
2469 * @base: base directory to lookup from
2470 * @len: maximum length @len should be interpreted to
2472 * Note that this routine is purely a helper for filesystem usage and should
2473 * not be called by generic code.
2475 * Unlike lookup_one_len, it should be called without the parent
2476 * i_mutex held, and will take the i_mutex itself if necessary.
2478 struct dentry *lookup_one_len_unlocked(const char *name,
2479 struct dentry *base, int len)
2481 struct qstr this;
2482 unsigned int c;
2483 int err;
2484 struct dentry *ret;
2486 this.name = name;
2487 this.len = len;
2488 this.hash = full_name_hash(base, name, len);
2489 if (!len)
2490 return ERR_PTR(-EACCES);
2492 if (unlikely(name[0] == '.')) {
2493 if (len < 2 || (len == 2 && name[1] == '.'))
2494 return ERR_PTR(-EACCES);
2497 while (len--) {
2498 c = *(const unsigned char *)name++;
2499 if (c == '/' || c == '\0')
2500 return ERR_PTR(-EACCES);
2503 * See if the low-level filesystem might want
2504 * to use its own hash..
2506 if (base->d_flags & DCACHE_OP_HASH) {
2507 int err = base->d_op->d_hash(base, &this);
2508 if (err < 0)
2509 return ERR_PTR(err);
2512 err = inode_permission(base->d_inode, MAY_EXEC);
2513 if (err)
2514 return ERR_PTR(err);
2516 ret = lookup_dcache(&this, base, 0);
2517 if (!ret)
2518 ret = lookup_slow(&this, base, 0);
2519 return ret;
2521 EXPORT_SYMBOL(lookup_one_len_unlocked);
2523 #ifdef CONFIG_UNIX98_PTYS
2524 int path_pts(struct path *path)
2526 /* Find something mounted on "pts" in the same directory as
2527 * the input path.
2529 struct dentry *child, *parent;
2530 struct qstr this;
2531 int ret;
2533 ret = path_parent_directory(path);
2534 if (ret)
2535 return ret;
2537 parent = path->dentry;
2538 this.name = "pts";
2539 this.len = 3;
2540 child = d_hash_and_lookup(parent, &this);
2541 if (!child)
2542 return -ENOENT;
2544 path->dentry = child;
2545 dput(parent);
2546 follow_mount(path);
2547 return 0;
2549 #endif
2551 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2552 struct path *path, int *empty)
2554 return filename_lookup(dfd, getname_flags(name, flags, empty),
2555 flags, path, NULL);
2557 EXPORT_SYMBOL(user_path_at_empty);
2560 * NB: most callers don't do anything directly with the reference to the
2561 * to struct filename, but the nd->last pointer points into the name string
2562 * allocated by getname. So we must hold the reference to it until all
2563 * path-walking is complete.
2565 static inline struct filename *
2566 user_path_parent(int dfd, const char __user *path,
2567 struct path *parent,
2568 struct qstr *last,
2569 int *type,
2570 unsigned int flags)
2572 /* only LOOKUP_REVAL is allowed in extra flags */
2573 return filename_parentat(dfd, getname(path), flags & LOOKUP_REVAL,
2574 parent, last, type);
2578 * mountpoint_last - look up last component for umount
2579 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2580 * @path: pointer to container for result
2582 * This is a special lookup_last function just for umount. In this case, we
2583 * need to resolve the path without doing any revalidation.
2585 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2586 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2587 * in almost all cases, this lookup will be served out of the dcache. The only
2588 * cases where it won't are if nd->last refers to a symlink or the path is
2589 * bogus and it doesn't exist.
2591 * Returns:
2592 * -error: if there was an error during lookup. This includes -ENOENT if the
2593 * lookup found a negative dentry. The nd->path reference will also be
2594 * put in this case.
2596 * 0: if we successfully resolved nd->path and found it to not to be a
2597 * symlink that needs to be followed. "path" will also be populated.
2598 * The nd->path reference will also be put.
2600 * 1: if we successfully resolved nd->last and found it to be a symlink
2601 * that needs to be followed. "path" will be populated with the path
2602 * to the link, and nd->path will *not* be put.
2604 static int
2605 mountpoint_last(struct nameidata *nd, struct path *path)
2607 int error = 0;
2608 struct dentry *dentry;
2609 struct dentry *dir = nd->path.dentry;
2611 /* If we're in rcuwalk, drop out of it to handle last component */
2612 if (nd->flags & LOOKUP_RCU) {
2613 if (unlazy_walk(nd, NULL, 0))
2614 return -ECHILD;
2617 nd->flags &= ~LOOKUP_PARENT;
2619 if (unlikely(nd->last_type != LAST_NORM)) {
2620 error = handle_dots(nd, nd->last_type);
2621 if (error)
2622 return error;
2623 dentry = dget(nd->path.dentry);
2624 } else {
2625 dentry = d_lookup(dir, &nd->last);
2626 if (!dentry) {
2628 * No cached dentry. Mounted dentries are pinned in the
2629 * cache, so that means that this dentry is probably
2630 * a symlink or the path doesn't actually point
2631 * to a mounted dentry.
2633 dentry = lookup_slow(&nd->last, dir,
2634 nd->flags | LOOKUP_NO_REVAL);
2635 if (IS_ERR(dentry))
2636 return PTR_ERR(dentry);
2639 if (d_is_negative(dentry)) {
2640 dput(dentry);
2641 return -ENOENT;
2643 if (nd->depth)
2644 put_link(nd);
2645 path->dentry = dentry;
2646 path->mnt = nd->path.mnt;
2647 error = should_follow_link(nd, path, nd->flags & LOOKUP_FOLLOW,
2648 d_backing_inode(dentry), 0);
2649 if (unlikely(error))
2650 return error;
2651 mntget(path->mnt);
2652 follow_mount(path);
2653 return 0;
2657 * path_mountpoint - look up a path to be umounted
2658 * @nd: lookup context
2659 * @flags: lookup flags
2660 * @path: pointer to container for result
2662 * Look up the given name, but don't attempt to revalidate the last component.
2663 * Returns 0 and "path" will be valid on success; Returns error otherwise.
2665 static int
2666 path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
2668 const char *s = path_init(nd, flags);
2669 int err;
2670 if (IS_ERR(s))
2671 return PTR_ERR(s);
2672 while (!(err = link_path_walk(s, nd)) &&
2673 (err = mountpoint_last(nd, path)) > 0) {
2674 s = trailing_symlink(nd);
2675 if (IS_ERR(s)) {
2676 err = PTR_ERR(s);
2677 break;
2680 terminate_walk(nd);
2681 return err;
2684 static int
2685 filename_mountpoint(int dfd, struct filename *name, struct path *path,
2686 unsigned int flags)
2688 struct nameidata nd;
2689 int error;
2690 if (IS_ERR(name))
2691 return PTR_ERR(name);
2692 set_nameidata(&nd, dfd, name);
2693 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
2694 if (unlikely(error == -ECHILD))
2695 error = path_mountpoint(&nd, flags, path);
2696 if (unlikely(error == -ESTALE))
2697 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
2698 if (likely(!error))
2699 audit_inode(name, path->dentry, 0);
2700 restore_nameidata();
2701 putname(name);
2702 return error;
2706 * user_path_mountpoint_at - lookup a path from userland in order to umount it
2707 * @dfd: directory file descriptor
2708 * @name: pathname from userland
2709 * @flags: lookup flags
2710 * @path: pointer to container to hold result
2712 * A umount is a special case for path walking. We're not actually interested
2713 * in the inode in this situation, and ESTALE errors can be a problem. We
2714 * simply want track down the dentry and vfsmount attached at the mountpoint
2715 * and avoid revalidating the last component.
2717 * Returns 0 and populates "path" on success.
2720 user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
2721 struct path *path)
2723 return filename_mountpoint(dfd, getname(name), path, flags);
2727 kern_path_mountpoint(int dfd, const char *name, struct path *path,
2728 unsigned int flags)
2730 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
2732 EXPORT_SYMBOL(kern_path_mountpoint);
2734 int __check_sticky(struct inode *dir, struct inode *inode)
2736 kuid_t fsuid = current_fsuid();
2738 if (uid_eq(inode->i_uid, fsuid))
2739 return 0;
2740 if (uid_eq(dir->i_uid, fsuid))
2741 return 0;
2742 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
2744 EXPORT_SYMBOL(__check_sticky);
2747 * Check whether we can remove a link victim from directory dir, check
2748 * whether the type of victim is right.
2749 * 1. We can't do it if dir is read-only (done in permission())
2750 * 2. We should have write and exec permissions on dir
2751 * 3. We can't remove anything from append-only dir
2752 * 4. We can't do anything with immutable dir (done in permission())
2753 * 5. If the sticky bit on dir is set we should either
2754 * a. be owner of dir, or
2755 * b. be owner of victim, or
2756 * c. have CAP_FOWNER capability
2757 * 6. If the victim is append-only or immutable we can't do antyhing with
2758 * links pointing to it.
2759 * 7. If the victim has an unknown uid or gid we can't change the inode.
2760 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2761 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2762 * 10. We can't remove a root or mountpoint.
2763 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
2764 * nfs_async_unlink().
2766 static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
2768 struct inode *inode = d_backing_inode(victim);
2769 int error;
2771 if (d_is_negative(victim))
2772 return -ENOENT;
2773 BUG_ON(!inode);
2775 BUG_ON(victim->d_parent->d_inode != dir);
2776 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
2778 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
2779 if (error)
2780 return error;
2781 if (IS_APPEND(dir))
2782 return -EPERM;
2784 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2785 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
2786 return -EPERM;
2787 if (isdir) {
2788 if (!d_is_dir(victim))
2789 return -ENOTDIR;
2790 if (IS_ROOT(victim))
2791 return -EBUSY;
2792 } else if (d_is_dir(victim))
2793 return -EISDIR;
2794 if (IS_DEADDIR(dir))
2795 return -ENOENT;
2796 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2797 return -EBUSY;
2798 return 0;
2801 /* Check whether we can create an object with dentry child in directory
2802 * dir.
2803 * 1. We can't do it if child already exists (open has special treatment for
2804 * this case, but since we are inlined it's OK)
2805 * 2. We can't do it if dir is read-only (done in permission())
2806 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2807 * 4. We should have write and exec permissions on dir
2808 * 5. We can't do it if dir is immutable (done in permission())
2810 static inline int may_create(struct inode *dir, struct dentry *child)
2812 struct user_namespace *s_user_ns;
2813 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
2814 if (child->d_inode)
2815 return -EEXIST;
2816 if (IS_DEADDIR(dir))
2817 return -ENOENT;
2818 s_user_ns = dir->i_sb->s_user_ns;
2819 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2820 !kgid_has_mapping(s_user_ns, current_fsgid()))
2821 return -EOVERFLOW;
2822 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2826 * p1 and p2 should be directories on the same fs.
2828 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2830 struct dentry *p;
2832 if (p1 == p2) {
2833 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2834 return NULL;
2837 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
2839 p = d_ancestor(p2, p1);
2840 if (p) {
2841 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2842 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
2843 return p;
2846 p = d_ancestor(p1, p2);
2847 if (p) {
2848 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2849 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
2850 return p;
2853 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2854 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
2855 return NULL;
2857 EXPORT_SYMBOL(lock_rename);
2859 void unlock_rename(struct dentry *p1, struct dentry *p2)
2861 inode_unlock(p1->d_inode);
2862 if (p1 != p2) {
2863 inode_unlock(p2->d_inode);
2864 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
2867 EXPORT_SYMBOL(unlock_rename);
2869 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2870 bool want_excl)
2872 int error = may_create(dir, dentry);
2873 if (error)
2874 return error;
2876 if (!dir->i_op->create)
2877 return -EACCES; /* shouldn't it be ENOSYS? */
2878 mode &= S_IALLUGO;
2879 mode |= S_IFREG;
2880 error = security_inode_create(dir, dentry, mode);
2881 if (error)
2882 return error;
2883 error = dir->i_op->create(dir, dentry, mode, want_excl);
2884 if (!error)
2885 fsnotify_create(dir, dentry);
2886 return error;
2888 EXPORT_SYMBOL(vfs_create);
2890 bool may_open_dev(const struct path *path)
2892 return !(path->mnt->mnt_flags & MNT_NODEV) &&
2893 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2896 static int may_open(struct path *path, int acc_mode, int flag)
2898 struct dentry *dentry = path->dentry;
2899 struct inode *inode = dentry->d_inode;
2900 int error;
2902 if (!inode)
2903 return -ENOENT;
2905 switch (inode->i_mode & S_IFMT) {
2906 case S_IFLNK:
2907 return -ELOOP;
2908 case S_IFDIR:
2909 if (acc_mode & MAY_WRITE)
2910 return -EISDIR;
2911 break;
2912 case S_IFBLK:
2913 case S_IFCHR:
2914 if (!may_open_dev(path))
2915 return -EACCES;
2916 /*FALLTHRU*/
2917 case S_IFIFO:
2918 case S_IFSOCK:
2919 flag &= ~O_TRUNC;
2920 break;
2923 error = inode_permission(inode, MAY_OPEN | acc_mode);
2924 if (error)
2925 return error;
2928 * An append-only file must be opened in append mode for writing.
2930 if (IS_APPEND(inode)) {
2931 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2932 return -EPERM;
2933 if (flag & O_TRUNC)
2934 return -EPERM;
2937 /* O_NOATIME can only be set by the owner or superuser */
2938 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2939 return -EPERM;
2941 return 0;
2944 static int handle_truncate(struct file *filp)
2946 struct path *path = &filp->f_path;
2947 struct inode *inode = path->dentry->d_inode;
2948 int error = get_write_access(inode);
2949 if (error)
2950 return error;
2952 * Refuse to truncate files with mandatory locks held on them.
2954 error = locks_verify_locked(filp);
2955 if (!error)
2956 error = security_path_truncate(path);
2957 if (!error) {
2958 error = do_truncate(path->dentry, 0,
2959 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2960 filp);
2962 put_write_access(inode);
2963 return error;
2966 static inline int open_to_namei_flags(int flag)
2968 if ((flag & O_ACCMODE) == 3)
2969 flag--;
2970 return flag;
2973 static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
2975 struct user_namespace *s_user_ns;
2976 int error = security_path_mknod(dir, dentry, mode, 0);
2977 if (error)
2978 return error;
2980 s_user_ns = dir->dentry->d_sb->s_user_ns;
2981 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2982 !kgid_has_mapping(s_user_ns, current_fsgid()))
2983 return -EOVERFLOW;
2985 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2986 if (error)
2987 return error;
2989 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2993 * Attempt to atomically look up, create and open a file from a negative
2994 * dentry.
2996 * Returns 0 if successful. The file will have been created and attached to
2997 * @file by the filesystem calling finish_open().
2999 * Returns 1 if the file was looked up only or didn't need creating. The
3000 * caller will need to perform the open themselves. @path will have been
3001 * updated to point to the new dentry. This may be negative.
3003 * Returns an error code otherwise.
3005 static int atomic_open(struct nameidata *nd, struct dentry *dentry,
3006 struct path *path, struct file *file,
3007 const struct open_flags *op,
3008 int open_flag, umode_t mode,
3009 int *opened)
3011 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
3012 struct inode *dir = nd->path.dentry->d_inode;
3013 int error;
3015 if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
3016 open_flag &= ~O_TRUNC;
3018 if (nd->flags & LOOKUP_DIRECTORY)
3019 open_flag |= O_DIRECTORY;
3021 file->f_path.dentry = DENTRY_NOT_SET;
3022 file->f_path.mnt = nd->path.mnt;
3023 error = dir->i_op->atomic_open(dir, dentry, file,
3024 open_to_namei_flags(open_flag),
3025 mode, opened);
3026 d_lookup_done(dentry);
3027 if (!error) {
3029 * We didn't have the inode before the open, so check open
3030 * permission here.
3032 int acc_mode = op->acc_mode;
3033 if (*opened & FILE_CREATED) {
3034 WARN_ON(!(open_flag & O_CREAT));
3035 fsnotify_create(dir, dentry);
3036 acc_mode = 0;
3038 error = may_open(&file->f_path, acc_mode, open_flag);
3039 if (WARN_ON(error > 0))
3040 error = -EINVAL;
3041 } else if (error > 0) {
3042 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
3043 error = -EIO;
3044 } else {
3045 if (file->f_path.dentry) {
3046 dput(dentry);
3047 dentry = file->f_path.dentry;
3049 if (*opened & FILE_CREATED)
3050 fsnotify_create(dir, dentry);
3051 if (unlikely(d_is_negative(dentry))) {
3052 error = -ENOENT;
3053 } else {
3054 path->dentry = dentry;
3055 path->mnt = nd->path.mnt;
3056 return 1;
3060 dput(dentry);
3061 return error;
3065 * Look up and maybe create and open the last component.
3067 * Must be called with i_mutex held on parent.
3069 * Returns 0 if the file was successfully atomically created (if necessary) and
3070 * opened. In this case the file will be returned attached to @file.
3072 * Returns 1 if the file was not completely opened at this time, though lookups
3073 * and creations will have been performed and the dentry returned in @path will
3074 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
3075 * specified then a negative dentry may be returned.
3077 * An error code is returned otherwise.
3079 * FILE_CREATE will be set in @*opened if the dentry was created and will be
3080 * cleared otherwise prior to returning.
3082 static int lookup_open(struct nameidata *nd, struct path *path,
3083 struct file *file,
3084 const struct open_flags *op,
3085 bool got_write, int *opened)
3087 struct dentry *dir = nd->path.dentry;
3088 struct inode *dir_inode = dir->d_inode;
3089 int open_flag = op->open_flag;
3090 struct dentry *dentry;
3091 int error, create_error = 0;
3092 umode_t mode = op->mode;
3093 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
3095 if (unlikely(IS_DEADDIR(dir_inode)))
3096 return -ENOENT;
3098 *opened &= ~FILE_CREATED;
3099 dentry = d_lookup(dir, &nd->last);
3100 for (;;) {
3101 if (!dentry) {
3102 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3103 if (IS_ERR(dentry))
3104 return PTR_ERR(dentry);
3106 if (d_in_lookup(dentry))
3107 break;
3109 if (!(dentry->d_flags & DCACHE_OP_REVALIDATE))
3110 break;
3112 error = d_revalidate(dentry, nd->flags);
3113 if (likely(error > 0))
3114 break;
3115 if (error)
3116 goto out_dput;
3117 d_invalidate(dentry);
3118 dput(dentry);
3119 dentry = NULL;
3121 if (dentry->d_inode) {
3122 /* Cached positive dentry: will open in f_op->open */
3123 goto out_no_open;
3127 * Checking write permission is tricky, bacuse we don't know if we are
3128 * going to actually need it: O_CREAT opens should work as long as the
3129 * file exists. But checking existence breaks atomicity. The trick is
3130 * to check access and if not granted clear O_CREAT from the flags.
3132 * Another problem is returing the "right" error value (e.g. for an
3133 * O_EXCL open we want to return EEXIST not EROFS).
3135 if (open_flag & O_CREAT) {
3136 if (!IS_POSIXACL(dir->d_inode))
3137 mode &= ~current_umask();
3138 if (unlikely(!got_write)) {
3139 create_error = -EROFS;
3140 open_flag &= ~O_CREAT;
3141 if (open_flag & (O_EXCL | O_TRUNC))
3142 goto no_open;
3143 /* No side effects, safe to clear O_CREAT */
3144 } else {
3145 create_error = may_o_create(&nd->path, dentry, mode);
3146 if (create_error) {
3147 open_flag &= ~O_CREAT;
3148 if (open_flag & O_EXCL)
3149 goto no_open;
3152 } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
3153 unlikely(!got_write)) {
3155 * No O_CREATE -> atomicity not a requirement -> fall
3156 * back to lookup + open
3158 goto no_open;
3161 if (dir_inode->i_op->atomic_open) {
3162 error = atomic_open(nd, dentry, path, file, op, open_flag,
3163 mode, opened);
3164 if (unlikely(error == -ENOENT) && create_error)
3165 error = create_error;
3166 return error;
3169 no_open:
3170 if (d_in_lookup(dentry)) {
3171 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3172 nd->flags);
3173 d_lookup_done(dentry);
3174 if (unlikely(res)) {
3175 if (IS_ERR(res)) {
3176 error = PTR_ERR(res);
3177 goto out_dput;
3179 dput(dentry);
3180 dentry = res;
3184 /* Negative dentry, just create the file */
3185 if (!dentry->d_inode && (open_flag & O_CREAT)) {
3186 *opened |= FILE_CREATED;
3187 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
3188 if (!dir_inode->i_op->create) {
3189 error = -EACCES;
3190 goto out_dput;
3192 error = dir_inode->i_op->create(dir_inode, dentry, mode,
3193 open_flag & O_EXCL);
3194 if (error)
3195 goto out_dput;
3196 fsnotify_create(dir_inode, dentry);
3198 if (unlikely(create_error) && !dentry->d_inode) {
3199 error = create_error;
3200 goto out_dput;
3202 out_no_open:
3203 path->dentry = dentry;
3204 path->mnt = nd->path.mnt;
3205 return 1;
3207 out_dput:
3208 dput(dentry);
3209 return error;
3213 * Handle the last step of open()
3215 static int do_last(struct nameidata *nd,
3216 struct file *file, const struct open_flags *op,
3217 int *opened)
3219 struct dentry *dir = nd->path.dentry;
3220 int open_flag = op->open_flag;
3221 bool will_truncate = (open_flag & O_TRUNC) != 0;
3222 bool got_write = false;
3223 int acc_mode = op->acc_mode;
3224 unsigned seq;
3225 struct inode *inode;
3226 struct path path;
3227 int error;
3229 nd->flags &= ~LOOKUP_PARENT;
3230 nd->flags |= op->intent;
3232 if (nd->last_type != LAST_NORM) {
3233 error = handle_dots(nd, nd->last_type);
3234 if (unlikely(error))
3235 return error;
3236 goto finish_open;
3239 if (!(open_flag & O_CREAT)) {
3240 if (nd->last.name[nd->last.len])
3241 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3242 /* we _can_ be in RCU mode here */
3243 error = lookup_fast(nd, &path, &inode, &seq);
3244 if (likely(error > 0))
3245 goto finish_lookup;
3247 if (error < 0)
3248 return error;
3250 BUG_ON(nd->inode != dir->d_inode);
3251 BUG_ON(nd->flags & LOOKUP_RCU);
3252 } else {
3253 /* create side of things */
3255 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3256 * has been cleared when we got to the last component we are
3257 * about to look up
3259 error = complete_walk(nd);
3260 if (error)
3261 return error;
3263 audit_inode(nd->name, dir, LOOKUP_PARENT);
3264 /* trailing slashes? */
3265 if (unlikely(nd->last.name[nd->last.len]))
3266 return -EISDIR;
3269 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
3270 error = mnt_want_write(nd->path.mnt);
3271 if (!error)
3272 got_write = true;
3274 * do _not_ fail yet - we might not need that or fail with
3275 * a different error; let lookup_open() decide; we'll be
3276 * dropping this one anyway.
3279 if (open_flag & O_CREAT)
3280 inode_lock(dir->d_inode);
3281 else
3282 inode_lock_shared(dir->d_inode);
3283 error = lookup_open(nd, &path, file, op, got_write, opened);
3284 if (open_flag & O_CREAT)
3285 inode_unlock(dir->d_inode);
3286 else
3287 inode_unlock_shared(dir->d_inode);
3289 if (error <= 0) {
3290 if (error)
3291 goto out;
3293 if ((*opened & FILE_CREATED) ||
3294 !S_ISREG(file_inode(file)->i_mode))
3295 will_truncate = false;
3297 audit_inode(nd->name, file->f_path.dentry, 0);
3298 goto opened;
3301 if (*opened & FILE_CREATED) {
3302 /* Don't check for write permission, don't truncate */
3303 open_flag &= ~O_TRUNC;
3304 will_truncate = false;
3305 acc_mode = 0;
3306 path_to_nameidata(&path, nd);
3307 goto finish_open_created;
3311 * If atomic_open() acquired write access it is dropped now due to
3312 * possible mount and symlink following (this might be optimized away if
3313 * necessary...)
3315 if (got_write) {
3316 mnt_drop_write(nd->path.mnt);
3317 got_write = false;
3320 error = follow_managed(&path, nd);
3321 if (unlikely(error < 0))
3322 return error;
3324 if (unlikely(d_is_negative(path.dentry))) {
3325 path_to_nameidata(&path, nd);
3326 return -ENOENT;
3330 * create/update audit record if it already exists.
3332 audit_inode(nd->name, path.dentry, 0);
3334 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3335 path_to_nameidata(&path, nd);
3336 return -EEXIST;
3339 seq = 0; /* out of RCU mode, so the value doesn't matter */
3340 inode = d_backing_inode(path.dentry);
3341 finish_lookup:
3342 if (nd->depth)
3343 put_link(nd);
3344 error = should_follow_link(nd, &path, nd->flags & LOOKUP_FOLLOW,
3345 inode, seq);
3346 if (unlikely(error))
3347 return error;
3349 path_to_nameidata(&path, nd);
3350 nd->inode = inode;
3351 nd->seq = seq;
3352 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
3353 finish_open:
3354 error = complete_walk(nd);
3355 if (error)
3356 return error;
3357 audit_inode(nd->name, nd->path.dentry, 0);
3358 error = -EISDIR;
3359 if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
3360 goto out;
3361 error = -ENOTDIR;
3362 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
3363 goto out;
3364 if (!d_is_reg(nd->path.dentry))
3365 will_truncate = false;
3367 if (will_truncate) {
3368 error = mnt_want_write(nd->path.mnt);
3369 if (error)
3370 goto out;
3371 got_write = true;
3373 finish_open_created:
3374 error = may_open(&nd->path, acc_mode, open_flag);
3375 if (error)
3376 goto out;
3377 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3378 error = vfs_open(&nd->path, file, current_cred());
3379 if (error)
3380 goto out;
3381 *opened |= FILE_OPENED;
3382 opened:
3383 error = open_check_o_direct(file);
3384 if (!error)
3385 error = ima_file_check(file, op->acc_mode, *opened);
3386 if (!error && will_truncate)
3387 error = handle_truncate(file);
3388 out:
3389 if (unlikely(error) && (*opened & FILE_OPENED))
3390 fput(file);
3391 if (unlikely(error > 0)) {
3392 WARN_ON(1);
3393 error = -EINVAL;
3395 if (got_write)
3396 mnt_drop_write(nd->path.mnt);
3397 return error;
3400 static int do_tmpfile(struct nameidata *nd, unsigned flags,
3401 const struct open_flags *op,
3402 struct file *file, int *opened)
3404 static const struct qstr name = QSTR_INIT("/", 1);
3405 struct dentry *child;
3406 struct inode *dir;
3407 struct path path;
3408 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
3409 if (unlikely(error))
3410 return error;
3411 error = mnt_want_write(path.mnt);
3412 if (unlikely(error))
3413 goto out;
3414 dir = path.dentry->d_inode;
3415 /* we want directory to be writable */
3416 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
3417 if (error)
3418 goto out2;
3419 if (!dir->i_op->tmpfile) {
3420 error = -EOPNOTSUPP;
3421 goto out2;
3423 child = d_alloc(path.dentry, &name);
3424 if (unlikely(!child)) {
3425 error = -ENOMEM;
3426 goto out2;
3428 dput(path.dentry);
3429 path.dentry = child;
3430 error = dir->i_op->tmpfile(dir, child, op->mode);
3431 if (error)
3432 goto out2;
3433 audit_inode(nd->name, child, 0);
3434 /* Don't check for other permissions, the inode was just created */
3435 error = may_open(&path, 0, op->open_flag);
3436 if (error)
3437 goto out2;
3438 file->f_path.mnt = path.mnt;
3439 error = finish_open(file, child, NULL, opened);
3440 if (error)
3441 goto out2;
3442 error = open_check_o_direct(file);
3443 if (error) {
3444 fput(file);
3445 } else if (!(op->open_flag & O_EXCL)) {
3446 struct inode *inode = file_inode(file);
3447 spin_lock(&inode->i_lock);
3448 inode->i_state |= I_LINKABLE;
3449 spin_unlock(&inode->i_lock);
3451 out2:
3452 mnt_drop_write(path.mnt);
3453 out:
3454 path_put(&path);
3455 return error;
3458 static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3460 struct path path;
3461 int error = path_lookupat(nd, flags, &path);
3462 if (!error) {
3463 audit_inode(nd->name, path.dentry, 0);
3464 error = vfs_open(&path, file, current_cred());
3465 path_put(&path);
3467 return error;
3470 static struct file *path_openat(struct nameidata *nd,
3471 const struct open_flags *op, unsigned flags)
3473 const char *s;
3474 struct file *file;
3475 int opened = 0;
3476 int error;
3478 file = get_empty_filp();
3479 if (IS_ERR(file))
3480 return file;
3482 file->f_flags = op->open_flag;
3484 if (unlikely(file->f_flags & __O_TMPFILE)) {
3485 error = do_tmpfile(nd, flags, op, file, &opened);
3486 goto out2;
3489 if (unlikely(file->f_flags & O_PATH)) {
3490 error = do_o_path(nd, flags, file);
3491 if (!error)
3492 opened |= FILE_OPENED;
3493 goto out2;
3496 s = path_init(nd, flags);
3497 if (IS_ERR(s)) {
3498 put_filp(file);
3499 return ERR_CAST(s);
3501 while (!(error = link_path_walk(s, nd)) &&
3502 (error = do_last(nd, file, op, &opened)) > 0) {
3503 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3504 s = trailing_symlink(nd);
3505 if (IS_ERR(s)) {
3506 error = PTR_ERR(s);
3507 break;
3510 terminate_walk(nd);
3511 out2:
3512 if (!(opened & FILE_OPENED)) {
3513 BUG_ON(!error);
3514 put_filp(file);
3516 if (unlikely(error)) {
3517 if (error == -EOPENSTALE) {
3518 if (flags & LOOKUP_RCU)
3519 error = -ECHILD;
3520 else
3521 error = -ESTALE;
3523 file = ERR_PTR(error);
3525 return file;
3528 struct file *do_filp_open(int dfd, struct filename *pathname,
3529 const struct open_flags *op)
3531 struct nameidata nd;
3532 int flags = op->lookup_flags;
3533 struct file *filp;
3535 set_nameidata(&nd, dfd, pathname);
3536 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
3537 if (unlikely(filp == ERR_PTR(-ECHILD)))
3538 filp = path_openat(&nd, op, flags);
3539 if (unlikely(filp == ERR_PTR(-ESTALE)))
3540 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
3541 restore_nameidata();
3542 return filp;
3545 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3546 const char *name, const struct open_flags *op)
3548 struct nameidata nd;
3549 struct file *file;
3550 struct filename *filename;
3551 int flags = op->lookup_flags | LOOKUP_ROOT;
3553 nd.root.mnt = mnt;
3554 nd.root.dentry = dentry;
3556 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
3557 return ERR_PTR(-ELOOP);
3559 filename = getname_kernel(name);
3560 if (IS_ERR(filename))
3561 return ERR_CAST(filename);
3563 set_nameidata(&nd, -1, filename);
3564 file = path_openat(&nd, op, flags | LOOKUP_RCU);
3565 if (unlikely(file == ERR_PTR(-ECHILD)))
3566 file = path_openat(&nd, op, flags);
3567 if (unlikely(file == ERR_PTR(-ESTALE)))
3568 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
3569 restore_nameidata();
3570 putname(filename);
3571 return file;
3574 static struct dentry *filename_create(int dfd, struct filename *name,
3575 struct path *path, unsigned int lookup_flags)
3577 struct dentry *dentry = ERR_PTR(-EEXIST);
3578 struct qstr last;
3579 int type;
3580 int err2;
3581 int error;
3582 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3585 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3586 * other flags passed in are ignored!
3588 lookup_flags &= LOOKUP_REVAL;
3590 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3591 if (IS_ERR(name))
3592 return ERR_CAST(name);
3595 * Yucky last component or no last component at all?
3596 * (foo/., foo/.., /////)
3598 if (unlikely(type != LAST_NORM))
3599 goto out;
3601 /* don't fail immediately if it's r/o, at least try to report other errors */
3602 err2 = mnt_want_write(path->mnt);
3604 * Do the final lookup.
3606 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3607 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
3608 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
3609 if (IS_ERR(dentry))
3610 goto unlock;
3612 error = -EEXIST;
3613 if (d_is_positive(dentry))
3614 goto fail;
3617 * Special case - lookup gave negative, but... we had foo/bar/
3618 * From the vfs_mknod() POV we just have a negative dentry -
3619 * all is fine. Let's be bastards - you had / on the end, you've
3620 * been asking for (non-existent) directory. -ENOENT for you.
3622 if (unlikely(!is_dir && last.name[last.len])) {
3623 error = -ENOENT;
3624 goto fail;
3626 if (unlikely(err2)) {
3627 error = err2;
3628 goto fail;
3630 putname(name);
3631 return dentry;
3632 fail:
3633 dput(dentry);
3634 dentry = ERR_PTR(error);
3635 unlock:
3636 inode_unlock(path->dentry->d_inode);
3637 if (!err2)
3638 mnt_drop_write(path->mnt);
3639 out:
3640 path_put(path);
3641 putname(name);
3642 return dentry;
3645 struct dentry *kern_path_create(int dfd, const char *pathname,
3646 struct path *path, unsigned int lookup_flags)
3648 return filename_create(dfd, getname_kernel(pathname),
3649 path, lookup_flags);
3651 EXPORT_SYMBOL(kern_path_create);
3653 void done_path_create(struct path *path, struct dentry *dentry)
3655 dput(dentry);
3656 inode_unlock(path->dentry->d_inode);
3657 mnt_drop_write(path->mnt);
3658 path_put(path);
3660 EXPORT_SYMBOL(done_path_create);
3662 inline struct dentry *user_path_create(int dfd, const char __user *pathname,
3663 struct path *path, unsigned int lookup_flags)
3665 return filename_create(dfd, getname(pathname), path, lookup_flags);
3667 EXPORT_SYMBOL(user_path_create);
3669 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3671 int error = may_create(dir, dentry);
3673 if (error)
3674 return error;
3676 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
3677 return -EPERM;
3679 if (!dir->i_op->mknod)
3680 return -EPERM;
3682 error = devcgroup_inode_mknod(mode, dev);
3683 if (error)
3684 return error;
3686 error = security_inode_mknod(dir, dentry, mode, dev);
3687 if (error)
3688 return error;
3690 error = dir->i_op->mknod(dir, dentry, mode, dev);
3691 if (!error)
3692 fsnotify_create(dir, dentry);
3693 return error;
3695 EXPORT_SYMBOL(vfs_mknod);
3697 static int may_mknod(umode_t mode)
3699 switch (mode & S_IFMT) {
3700 case S_IFREG:
3701 case S_IFCHR:
3702 case S_IFBLK:
3703 case S_IFIFO:
3704 case S_IFSOCK:
3705 case 0: /* zero mode translates to S_IFREG */
3706 return 0;
3707 case S_IFDIR:
3708 return -EPERM;
3709 default:
3710 return -EINVAL;
3714 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3715 unsigned, dev)
3717 struct dentry *dentry;
3718 struct path path;
3719 int error;
3720 unsigned int lookup_flags = 0;
3722 error = may_mknod(mode);
3723 if (error)
3724 return error;
3725 retry:
3726 dentry = user_path_create(dfd, filename, &path, lookup_flags);
3727 if (IS_ERR(dentry))
3728 return PTR_ERR(dentry);
3730 if (!IS_POSIXACL(path.dentry->d_inode))
3731 mode &= ~current_umask();
3732 error = security_path_mknod(&path, dentry, mode, dev);
3733 if (error)
3734 goto out;
3735 switch (mode & S_IFMT) {
3736 case 0: case S_IFREG:
3737 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
3738 if (!error)
3739 ima_post_path_mknod(dentry);
3740 break;
3741 case S_IFCHR: case S_IFBLK:
3742 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
3743 new_decode_dev(dev));
3744 break;
3745 case S_IFIFO: case S_IFSOCK:
3746 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
3747 break;
3749 out:
3750 done_path_create(&path, dentry);
3751 if (retry_estale(error, lookup_flags)) {
3752 lookup_flags |= LOOKUP_REVAL;
3753 goto retry;
3755 return error;
3758 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
3760 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3763 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3765 int error = may_create(dir, dentry);
3766 unsigned max_links = dir->i_sb->s_max_links;
3768 if (error)
3769 return error;
3771 if (!dir->i_op->mkdir)
3772 return -EPERM;
3774 mode &= (S_IRWXUGO|S_ISVTX);
3775 error = security_inode_mkdir(dir, dentry, mode);
3776 if (error)
3777 return error;
3779 if (max_links && dir->i_nlink >= max_links)
3780 return -EMLINK;
3782 error = dir->i_op->mkdir(dir, dentry, mode);
3783 if (!error)
3784 fsnotify_mkdir(dir, dentry);
3785 return error;
3787 EXPORT_SYMBOL(vfs_mkdir);
3789 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3791 struct dentry *dentry;
3792 struct path path;
3793 int error;
3794 unsigned int lookup_flags = LOOKUP_DIRECTORY;
3796 retry:
3797 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
3798 if (IS_ERR(dentry))
3799 return PTR_ERR(dentry);
3801 if (!IS_POSIXACL(path.dentry->d_inode))
3802 mode &= ~current_umask();
3803 error = security_path_mkdir(&path, dentry, mode);
3804 if (!error)
3805 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3806 done_path_create(&path, dentry);
3807 if (retry_estale(error, lookup_flags)) {
3808 lookup_flags |= LOOKUP_REVAL;
3809 goto retry;
3811 return error;
3814 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
3816 return sys_mkdirat(AT_FDCWD, pathname, mode);
3819 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3821 int error = may_delete(dir, dentry, 1);
3823 if (error)
3824 return error;
3826 if (!dir->i_op->rmdir)
3827 return -EPERM;
3829 dget(dentry);
3830 inode_lock(dentry->d_inode);
3832 error = -EBUSY;
3833 if (is_local_mountpoint(dentry))
3834 goto out;
3836 error = security_inode_rmdir(dir, dentry);
3837 if (error)
3838 goto out;
3840 shrink_dcache_parent(dentry);
3841 error = dir->i_op->rmdir(dir, dentry);
3842 if (error)
3843 goto out;
3845 dentry->d_inode->i_flags |= S_DEAD;
3846 dont_mount(dentry);
3847 detach_mounts(dentry);
3849 out:
3850 inode_unlock(dentry->d_inode);
3851 dput(dentry);
3852 if (!error)
3853 d_delete(dentry);
3854 return error;
3856 EXPORT_SYMBOL(vfs_rmdir);
3858 static long do_rmdir(int dfd, const char __user *pathname)
3860 int error = 0;
3861 struct filename *name;
3862 struct dentry *dentry;
3863 struct path path;
3864 struct qstr last;
3865 int type;
3866 unsigned int lookup_flags = 0;
3867 retry:
3868 name = user_path_parent(dfd, pathname,
3869 &path, &last, &type, lookup_flags);
3870 if (IS_ERR(name))
3871 return PTR_ERR(name);
3873 switch (type) {
3874 case LAST_DOTDOT:
3875 error = -ENOTEMPTY;
3876 goto exit1;
3877 case LAST_DOT:
3878 error = -EINVAL;
3879 goto exit1;
3880 case LAST_ROOT:
3881 error = -EBUSY;
3882 goto exit1;
3885 error = mnt_want_write(path.mnt);
3886 if (error)
3887 goto exit1;
3889 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
3890 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
3891 error = PTR_ERR(dentry);
3892 if (IS_ERR(dentry))
3893 goto exit2;
3894 if (!dentry->d_inode) {
3895 error = -ENOENT;
3896 goto exit3;
3898 error = security_path_rmdir(&path, dentry);
3899 if (error)
3900 goto exit3;
3901 error = vfs_rmdir(path.dentry->d_inode, dentry);
3902 exit3:
3903 dput(dentry);
3904 exit2:
3905 inode_unlock(path.dentry->d_inode);
3906 mnt_drop_write(path.mnt);
3907 exit1:
3908 path_put(&path);
3909 putname(name);
3910 if (retry_estale(error, lookup_flags)) {
3911 lookup_flags |= LOOKUP_REVAL;
3912 goto retry;
3914 return error;
3917 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
3919 return do_rmdir(AT_FDCWD, pathname);
3923 * vfs_unlink - unlink a filesystem object
3924 * @dir: parent directory
3925 * @dentry: victim
3926 * @delegated_inode: returns victim inode, if the inode is delegated.
3928 * The caller must hold dir->i_mutex.
3930 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3931 * return a reference to the inode in delegated_inode. The caller
3932 * should then break the delegation on that inode and retry. Because
3933 * breaking a delegation may take a long time, the caller should drop
3934 * dir->i_mutex before doing so.
3936 * Alternatively, a caller may pass NULL for delegated_inode. This may
3937 * be appropriate for callers that expect the underlying filesystem not
3938 * to be NFS exported.
3940 int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
3942 struct inode *target = dentry->d_inode;
3943 int error = may_delete(dir, dentry, 0);
3945 if (error)
3946 return error;
3948 if (!dir->i_op->unlink)
3949 return -EPERM;
3951 inode_lock(target);
3952 if (is_local_mountpoint(dentry))
3953 error = -EBUSY;
3954 else {
3955 error = security_inode_unlink(dir, dentry);
3956 if (!error) {
3957 error = try_break_deleg(target, delegated_inode);
3958 if (error)
3959 goto out;
3960 error = dir->i_op->unlink(dir, dentry);
3961 if (!error) {
3962 dont_mount(dentry);
3963 detach_mounts(dentry);
3967 out:
3968 inode_unlock(target);
3970 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3971 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3972 fsnotify_link_count(target);
3973 d_delete(dentry);
3976 return error;
3978 EXPORT_SYMBOL(vfs_unlink);
3981 * Make sure that the actual truncation of the file will occur outside its
3982 * directory's i_mutex. Truncate can take a long time if there is a lot of
3983 * writeout happening, and we don't want to prevent access to the directory
3984 * while waiting on the I/O.
3986 static long do_unlinkat(int dfd, const char __user *pathname)
3988 int error;
3989 struct filename *name;
3990 struct dentry *dentry;
3991 struct path path;
3992 struct qstr last;
3993 int type;
3994 struct inode *inode = NULL;
3995 struct inode *delegated_inode = NULL;
3996 unsigned int lookup_flags = 0;
3997 retry:
3998 name = user_path_parent(dfd, pathname,
3999 &path, &last, &type, lookup_flags);
4000 if (IS_ERR(name))
4001 return PTR_ERR(name);
4003 error = -EISDIR;
4004 if (type != LAST_NORM)
4005 goto exit1;
4007 error = mnt_want_write(path.mnt);
4008 if (error)
4009 goto exit1;
4010 retry_deleg:
4011 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
4012 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
4013 error = PTR_ERR(dentry);
4014 if (!IS_ERR(dentry)) {
4015 /* Why not before? Because we want correct error value */
4016 if (last.name[last.len])
4017 goto slashes;
4018 inode = dentry->d_inode;
4019 if (d_is_negative(dentry))
4020 goto slashes;
4021 ihold(inode);
4022 error = security_path_unlink(&path, dentry);
4023 if (error)
4024 goto exit2;
4025 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
4026 exit2:
4027 dput(dentry);
4029 inode_unlock(path.dentry->d_inode);
4030 if (inode)
4031 iput(inode); /* truncate the inode here */
4032 inode = NULL;
4033 if (delegated_inode) {
4034 error = break_deleg_wait(&delegated_inode);
4035 if (!error)
4036 goto retry_deleg;
4038 mnt_drop_write(path.mnt);
4039 exit1:
4040 path_put(&path);
4041 putname(name);
4042 if (retry_estale(error, lookup_flags)) {
4043 lookup_flags |= LOOKUP_REVAL;
4044 inode = NULL;
4045 goto retry;
4047 return error;
4049 slashes:
4050 if (d_is_negative(dentry))
4051 error = -ENOENT;
4052 else if (d_is_dir(dentry))
4053 error = -EISDIR;
4054 else
4055 error = -ENOTDIR;
4056 goto exit2;
4059 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
4061 if ((flag & ~AT_REMOVEDIR) != 0)
4062 return -EINVAL;
4064 if (flag & AT_REMOVEDIR)
4065 return do_rmdir(dfd, pathname);
4067 return do_unlinkat(dfd, pathname);
4070 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
4072 return do_unlinkat(AT_FDCWD, pathname);
4075 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
4077 int error = may_create(dir, dentry);
4079 if (error)
4080 return error;
4082 if (!dir->i_op->symlink)
4083 return -EPERM;
4085 error = security_inode_symlink(dir, dentry, oldname);
4086 if (error)
4087 return error;
4089 error = dir->i_op->symlink(dir, dentry, oldname);
4090 if (!error)
4091 fsnotify_create(dir, dentry);
4092 return error;
4094 EXPORT_SYMBOL(vfs_symlink);
4096 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4097 int, newdfd, const char __user *, newname)
4099 int error;
4100 struct filename *from;
4101 struct dentry *dentry;
4102 struct path path;
4103 unsigned int lookup_flags = 0;
4105 from = getname(oldname);
4106 if (IS_ERR(from))
4107 return PTR_ERR(from);
4108 retry:
4109 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
4110 error = PTR_ERR(dentry);
4111 if (IS_ERR(dentry))
4112 goto out_putname;
4114 error = security_path_symlink(&path, dentry, from->name);
4115 if (!error)
4116 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
4117 done_path_create(&path, dentry);
4118 if (retry_estale(error, lookup_flags)) {
4119 lookup_flags |= LOOKUP_REVAL;
4120 goto retry;
4122 out_putname:
4123 putname(from);
4124 return error;
4127 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
4129 return sys_symlinkat(oldname, AT_FDCWD, newname);
4133 * vfs_link - create a new link
4134 * @old_dentry: object to be linked
4135 * @dir: new parent
4136 * @new_dentry: where to create the new link
4137 * @delegated_inode: returns inode needing a delegation break
4139 * The caller must hold dir->i_mutex
4141 * If vfs_link discovers a delegation on the to-be-linked file in need
4142 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4143 * inode in delegated_inode. The caller should then break the delegation
4144 * and retry. Because breaking a delegation may take a long time, the
4145 * caller should drop the i_mutex before doing so.
4147 * Alternatively, a caller may pass NULL for delegated_inode. This may
4148 * be appropriate for callers that expect the underlying filesystem not
4149 * to be NFS exported.
4151 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
4153 struct inode *inode = old_dentry->d_inode;
4154 unsigned max_links = dir->i_sb->s_max_links;
4155 int error;
4157 if (!inode)
4158 return -ENOENT;
4160 error = may_create(dir, new_dentry);
4161 if (error)
4162 return error;
4164 if (dir->i_sb != inode->i_sb)
4165 return -EXDEV;
4168 * A link to an append-only or immutable file cannot be created.
4170 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4171 return -EPERM;
4173 * Updating the link count will likely cause i_uid and i_gid to
4174 * be writen back improperly if their true value is unknown to
4175 * the vfs.
4177 if (HAS_UNMAPPED_ID(inode))
4178 return -EPERM;
4179 if (!dir->i_op->link)
4180 return -EPERM;
4181 if (S_ISDIR(inode->i_mode))
4182 return -EPERM;
4184 error = security_inode_link(old_dentry, dir, new_dentry);
4185 if (error)
4186 return error;
4188 inode_lock(inode);
4189 /* Make sure we don't allow creating hardlink to an unlinked file */
4190 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
4191 error = -ENOENT;
4192 else if (max_links && inode->i_nlink >= max_links)
4193 error = -EMLINK;
4194 else {
4195 error = try_break_deleg(inode, delegated_inode);
4196 if (!error)
4197 error = dir->i_op->link(old_dentry, dir, new_dentry);
4200 if (!error && (inode->i_state & I_LINKABLE)) {
4201 spin_lock(&inode->i_lock);
4202 inode->i_state &= ~I_LINKABLE;
4203 spin_unlock(&inode->i_lock);
4205 inode_unlock(inode);
4206 if (!error)
4207 fsnotify_link(dir, inode, new_dentry);
4208 return error;
4210 EXPORT_SYMBOL(vfs_link);
4213 * Hardlinks are often used in delicate situations. We avoid
4214 * security-related surprises by not following symlinks on the
4215 * newname. --KAB
4217 * We don't follow them on the oldname either to be compatible
4218 * with linux 2.0, and to avoid hard-linking to directories
4219 * and other special files. --ADM
4221 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4222 int, newdfd, const char __user *, newname, int, flags)
4224 struct dentry *new_dentry;
4225 struct path old_path, new_path;
4226 struct inode *delegated_inode = NULL;
4227 int how = 0;
4228 int error;
4230 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
4231 return -EINVAL;
4233 * To use null names we require CAP_DAC_READ_SEARCH
4234 * This ensures that not everyone will be able to create
4235 * handlink using the passed filedescriptor.
4237 if (flags & AT_EMPTY_PATH) {
4238 if (!capable(CAP_DAC_READ_SEARCH))
4239 return -ENOENT;
4240 how = LOOKUP_EMPTY;
4243 if (flags & AT_SYMLINK_FOLLOW)
4244 how |= LOOKUP_FOLLOW;
4245 retry:
4246 error = user_path_at(olddfd, oldname, how, &old_path);
4247 if (error)
4248 return error;
4250 new_dentry = user_path_create(newdfd, newname, &new_path,
4251 (how & LOOKUP_REVAL));
4252 error = PTR_ERR(new_dentry);
4253 if (IS_ERR(new_dentry))
4254 goto out;
4256 error = -EXDEV;
4257 if (old_path.mnt != new_path.mnt)
4258 goto out_dput;
4259 error = may_linkat(&old_path);
4260 if (unlikely(error))
4261 goto out_dput;
4262 error = security_path_link(old_path.dentry, &new_path, new_dentry);
4263 if (error)
4264 goto out_dput;
4265 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
4266 out_dput:
4267 done_path_create(&new_path, new_dentry);
4268 if (delegated_inode) {
4269 error = break_deleg_wait(&delegated_inode);
4270 if (!error) {
4271 path_put(&old_path);
4272 goto retry;
4275 if (retry_estale(error, how)) {
4276 path_put(&old_path);
4277 how |= LOOKUP_REVAL;
4278 goto retry;
4280 out:
4281 path_put(&old_path);
4283 return error;
4286 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
4288 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
4292 * vfs_rename - rename a filesystem object
4293 * @old_dir: parent of source
4294 * @old_dentry: source
4295 * @new_dir: parent of destination
4296 * @new_dentry: destination
4297 * @delegated_inode: returns an inode needing a delegation break
4298 * @flags: rename flags
4300 * The caller must hold multiple mutexes--see lock_rename()).
4302 * If vfs_rename discovers a delegation in need of breaking at either
4303 * the source or destination, it will return -EWOULDBLOCK and return a
4304 * reference to the inode in delegated_inode. The caller should then
4305 * break the delegation and retry. Because breaking a delegation may
4306 * take a long time, the caller should drop all locks before doing
4307 * so.
4309 * Alternatively, a caller may pass NULL for delegated_inode. This may
4310 * be appropriate for callers that expect the underlying filesystem not
4311 * to be NFS exported.
4313 * The worst of all namespace operations - renaming directory. "Perverted"
4314 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4315 * Problems:
4316 * a) we can get into loop creation.
4317 * b) race potential - two innocent renames can create a loop together.
4318 * That's where 4.4 screws up. Current fix: serialization on
4319 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
4320 * story.
4321 * c) we have to lock _four_ objects - parents and victim (if it exists),
4322 * and source (if it is not a directory).
4323 * And that - after we got ->i_mutex on parents (until then we don't know
4324 * whether the target exists). Solution: try to be smart with locking
4325 * order for inodes. We rely on the fact that tree topology may change
4326 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
4327 * move will be locked. Thus we can rank directories by the tree
4328 * (ancestors first) and rank all non-directories after them.
4329 * That works since everybody except rename does "lock parent, lookup,
4330 * lock child" and rename is under ->s_vfs_rename_mutex.
4331 * HOWEVER, it relies on the assumption that any object with ->lookup()
4332 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4333 * we'd better make sure that there's no link(2) for them.
4334 * d) conversion from fhandle to dentry may come in the wrong moment - when
4335 * we are removing the target. Solution: we will have to grab ->i_mutex
4336 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4337 * ->i_mutex on parents, which works but leads to some truly excessive
4338 * locking].
4340 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4341 struct inode *new_dir, struct dentry *new_dentry,
4342 struct inode **delegated_inode, unsigned int flags)
4344 int error;
4345 bool is_dir = d_is_dir(old_dentry);
4346 struct inode *source = old_dentry->d_inode;
4347 struct inode *target = new_dentry->d_inode;
4348 bool new_is_dir = false;
4349 unsigned max_links = new_dir->i_sb->s_max_links;
4350 struct name_snapshot old_name;
4353 * Check source == target.
4354 * On overlayfs need to look at underlying inodes.
4356 if (d_real_inode(old_dentry) == d_real_inode(new_dentry))
4357 return 0;
4359 error = may_delete(old_dir, old_dentry, is_dir);
4360 if (error)
4361 return error;
4363 if (!target) {
4364 error = may_create(new_dir, new_dentry);
4365 } else {
4366 new_is_dir = d_is_dir(new_dentry);
4368 if (!(flags & RENAME_EXCHANGE))
4369 error = may_delete(new_dir, new_dentry, is_dir);
4370 else
4371 error = may_delete(new_dir, new_dentry, new_is_dir);
4373 if (error)
4374 return error;
4376 if (!old_dir->i_op->rename)
4377 return -EPERM;
4380 * If we are going to change the parent - check write permissions,
4381 * we'll need to flip '..'.
4383 if (new_dir != old_dir) {
4384 if (is_dir) {
4385 error = inode_permission(source, MAY_WRITE);
4386 if (error)
4387 return error;
4389 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4390 error = inode_permission(target, MAY_WRITE);
4391 if (error)
4392 return error;
4396 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4397 flags);
4398 if (error)
4399 return error;
4401 take_dentry_name_snapshot(&old_name, old_dentry);
4402 dget(new_dentry);
4403 if (!is_dir || (flags & RENAME_EXCHANGE))
4404 lock_two_nondirectories(source, target);
4405 else if (target)
4406 inode_lock(target);
4408 error = -EBUSY;
4409 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
4410 goto out;
4412 if (max_links && new_dir != old_dir) {
4413 error = -EMLINK;
4414 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
4415 goto out;
4416 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4417 old_dir->i_nlink >= max_links)
4418 goto out;
4420 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4421 shrink_dcache_parent(new_dentry);
4422 if (!is_dir) {
4423 error = try_break_deleg(source, delegated_inode);
4424 if (error)
4425 goto out;
4427 if (target && !new_is_dir) {
4428 error = try_break_deleg(target, delegated_inode);
4429 if (error)
4430 goto out;
4432 error = old_dir->i_op->rename(old_dir, old_dentry,
4433 new_dir, new_dentry, flags);
4434 if (error)
4435 goto out;
4437 if (!(flags & RENAME_EXCHANGE) && target) {
4438 if (is_dir)
4439 target->i_flags |= S_DEAD;
4440 dont_mount(new_dentry);
4441 detach_mounts(new_dentry);
4443 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4444 if (!(flags & RENAME_EXCHANGE))
4445 d_move(old_dentry, new_dentry);
4446 else
4447 d_exchange(old_dentry, new_dentry);
4449 out:
4450 if (!is_dir || (flags & RENAME_EXCHANGE))
4451 unlock_two_nondirectories(source, target);
4452 else if (target)
4453 inode_unlock(target);
4454 dput(new_dentry);
4455 if (!error) {
4456 fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
4457 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4458 if (flags & RENAME_EXCHANGE) {
4459 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4460 new_is_dir, NULL, new_dentry);
4463 release_dentry_name_snapshot(&old_name);
4465 return error;
4467 EXPORT_SYMBOL(vfs_rename);
4469 SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4470 int, newdfd, const char __user *, newname, unsigned int, flags)
4472 struct dentry *old_dentry, *new_dentry;
4473 struct dentry *trap;
4474 struct path old_path, new_path;
4475 struct qstr old_last, new_last;
4476 int old_type, new_type;
4477 struct inode *delegated_inode = NULL;
4478 struct filename *from;
4479 struct filename *to;
4480 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
4481 bool should_retry = false;
4482 int error;
4484 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
4485 return -EINVAL;
4487 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4488 (flags & RENAME_EXCHANGE))
4489 return -EINVAL;
4491 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4492 return -EPERM;
4494 if (flags & RENAME_EXCHANGE)
4495 target_flags = 0;
4497 retry:
4498 from = user_path_parent(olddfd, oldname,
4499 &old_path, &old_last, &old_type, lookup_flags);
4500 if (IS_ERR(from)) {
4501 error = PTR_ERR(from);
4502 goto exit;
4505 to = user_path_parent(newdfd, newname,
4506 &new_path, &new_last, &new_type, lookup_flags);
4507 if (IS_ERR(to)) {
4508 error = PTR_ERR(to);
4509 goto exit1;
4512 error = -EXDEV;
4513 if (old_path.mnt != new_path.mnt)
4514 goto exit2;
4516 error = -EBUSY;
4517 if (old_type != LAST_NORM)
4518 goto exit2;
4520 if (flags & RENAME_NOREPLACE)
4521 error = -EEXIST;
4522 if (new_type != LAST_NORM)
4523 goto exit2;
4525 error = mnt_want_write(old_path.mnt);
4526 if (error)
4527 goto exit2;
4529 retry_deleg:
4530 trap = lock_rename(new_path.dentry, old_path.dentry);
4532 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
4533 error = PTR_ERR(old_dentry);
4534 if (IS_ERR(old_dentry))
4535 goto exit3;
4536 /* source must exist */
4537 error = -ENOENT;
4538 if (d_is_negative(old_dentry))
4539 goto exit4;
4540 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
4541 error = PTR_ERR(new_dentry);
4542 if (IS_ERR(new_dentry))
4543 goto exit4;
4544 error = -EEXIST;
4545 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4546 goto exit5;
4547 if (flags & RENAME_EXCHANGE) {
4548 error = -ENOENT;
4549 if (d_is_negative(new_dentry))
4550 goto exit5;
4552 if (!d_is_dir(new_dentry)) {
4553 error = -ENOTDIR;
4554 if (new_last.name[new_last.len])
4555 goto exit5;
4558 /* unless the source is a directory trailing slashes give -ENOTDIR */
4559 if (!d_is_dir(old_dentry)) {
4560 error = -ENOTDIR;
4561 if (old_last.name[old_last.len])
4562 goto exit5;
4563 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
4564 goto exit5;
4566 /* source should not be ancestor of target */
4567 error = -EINVAL;
4568 if (old_dentry == trap)
4569 goto exit5;
4570 /* target should not be an ancestor of source */
4571 if (!(flags & RENAME_EXCHANGE))
4572 error = -ENOTEMPTY;
4573 if (new_dentry == trap)
4574 goto exit5;
4576 error = security_path_rename(&old_path, old_dentry,
4577 &new_path, new_dentry, flags);
4578 if (error)
4579 goto exit5;
4580 error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4581 new_path.dentry->d_inode, new_dentry,
4582 &delegated_inode, flags);
4583 exit5:
4584 dput(new_dentry);
4585 exit4:
4586 dput(old_dentry);
4587 exit3:
4588 unlock_rename(new_path.dentry, old_path.dentry);
4589 if (delegated_inode) {
4590 error = break_deleg_wait(&delegated_inode);
4591 if (!error)
4592 goto retry_deleg;
4594 mnt_drop_write(old_path.mnt);
4595 exit2:
4596 if (retry_estale(error, lookup_flags))
4597 should_retry = true;
4598 path_put(&new_path);
4599 putname(to);
4600 exit1:
4601 path_put(&old_path);
4602 putname(from);
4603 if (should_retry) {
4604 should_retry = false;
4605 lookup_flags |= LOOKUP_REVAL;
4606 goto retry;
4608 exit:
4609 return error;
4612 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4613 int, newdfd, const char __user *, newname)
4615 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4618 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
4620 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
4623 int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4625 int error = may_create(dir, dentry);
4626 if (error)
4627 return error;
4629 if (!dir->i_op->mknod)
4630 return -EPERM;
4632 return dir->i_op->mknod(dir, dentry,
4633 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4635 EXPORT_SYMBOL(vfs_whiteout);
4637 int readlink_copy(char __user *buffer, int buflen, const char *link)
4639 int len = PTR_ERR(link);
4640 if (IS_ERR(link))
4641 goto out;
4643 len = strlen(link);
4644 if (len > (unsigned) buflen)
4645 len = buflen;
4646 if (copy_to_user(buffer, link, len))
4647 len = -EFAULT;
4648 out:
4649 return len;
4653 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4654 * have ->get_link() not calling nd_jump_link(). Using (or not using) it
4655 * for any given inode is up to filesystem.
4657 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4659 DEFINE_DELAYED_CALL(done);
4660 struct inode *inode = d_inode(dentry);
4661 const char *link = inode->i_link;
4662 int res;
4664 if (!link) {
4665 link = inode->i_op->get_link(dentry, inode, &done);
4666 if (IS_ERR(link))
4667 return PTR_ERR(link);
4669 res = readlink_copy(buffer, buflen, link);
4670 do_delayed_call(&done);
4671 return res;
4673 EXPORT_SYMBOL(generic_readlink);
4676 * vfs_get_link - get symlink body
4677 * @dentry: dentry on which to get symbolic link
4678 * @done: caller needs to free returned data with this
4680 * Calls security hook and i_op->get_link() on the supplied inode.
4682 * It does not touch atime. That's up to the caller if necessary.
4684 * Does not work on "special" symlinks like /proc/$$/fd/N
4686 const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4688 const char *res = ERR_PTR(-EINVAL);
4689 struct inode *inode = d_inode(dentry);
4691 if (d_is_symlink(dentry)) {
4692 res = ERR_PTR(security_inode_readlink(dentry));
4693 if (!res)
4694 res = inode->i_op->get_link(dentry, inode, done);
4696 return res;
4698 EXPORT_SYMBOL(vfs_get_link);
4700 /* get the link contents into pagecache */
4701 const char *page_get_link(struct dentry *dentry, struct inode *inode,
4702 struct delayed_call *callback)
4704 char *kaddr;
4705 struct page *page;
4706 struct address_space *mapping = inode->i_mapping;
4708 if (!dentry) {
4709 page = find_get_page(mapping, 0);
4710 if (!page)
4711 return ERR_PTR(-ECHILD);
4712 if (!PageUptodate(page)) {
4713 put_page(page);
4714 return ERR_PTR(-ECHILD);
4716 } else {
4717 page = read_mapping_page(mapping, 0, NULL);
4718 if (IS_ERR(page))
4719 return (char*)page;
4721 set_delayed_call(callback, page_put_link, page);
4722 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4723 kaddr = page_address(page);
4724 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
4725 return kaddr;
4728 EXPORT_SYMBOL(page_get_link);
4730 void page_put_link(void *arg)
4732 put_page(arg);
4734 EXPORT_SYMBOL(page_put_link);
4736 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4738 DEFINE_DELAYED_CALL(done);
4739 int res = readlink_copy(buffer, buflen,
4740 page_get_link(dentry, d_inode(dentry),
4741 &done));
4742 do_delayed_call(&done);
4743 return res;
4745 EXPORT_SYMBOL(page_readlink);
4748 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4750 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
4752 struct address_space *mapping = inode->i_mapping;
4753 struct page *page;
4754 void *fsdata;
4755 int err;
4756 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4757 if (nofs)
4758 flags |= AOP_FLAG_NOFS;
4760 retry:
4761 err = pagecache_write_begin(NULL, mapping, 0, len-1,
4762 flags, &page, &fsdata);
4763 if (err)
4764 goto fail;
4766 memcpy(page_address(page), symname, len-1);
4768 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4769 page, fsdata);
4770 if (err < 0)
4771 goto fail;
4772 if (err < len-1)
4773 goto retry;
4775 mark_inode_dirty(inode);
4776 return 0;
4777 fail:
4778 return err;
4780 EXPORT_SYMBOL(__page_symlink);
4782 int page_symlink(struct inode *inode, const char *symname, int len)
4784 return __page_symlink(inode, symname, len,
4785 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
4787 EXPORT_SYMBOL(page_symlink);
4789 const struct inode_operations page_symlink_inode_operations = {
4790 .readlink = generic_readlink,
4791 .get_link = page_get_link,
4793 EXPORT_SYMBOL(page_symlink_inode_operations);