dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / fs / namei.c
blobde57dd59d95f947f4ccd91a8b8144d172a74d786
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 <asm/uaccess.h>
40 #include "internal.h"
41 #include "mount.h"
43 /* [Feb-1997 T. Schoebel-Theuer]
44 * Fundamental changes in the pathname lookup mechanisms (namei)
45 * were necessary because of omirr. The reason is that omirr needs
46 * to know the _real_ pathname, not the user-supplied one, in case
47 * of symlinks (and also when transname replacements occur).
49 * The new code replaces the old recursive symlink resolution with
50 * an iterative one (in case of non-nested symlink chains). It does
51 * this with calls to <fs>_follow_link().
52 * As a side effect, dir_namei(), _namei() and follow_link() are now
53 * replaced with a single function lookup_dentry() that can handle all
54 * the special cases of the former code.
56 * With the new dcache, the pathname is stored at each inode, at least as
57 * long as the refcount of the inode is positive. As a side effect, the
58 * size of the dcache depends on the inode cache and thus is dynamic.
60 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
61 * resolution to correspond with current state of the code.
63 * Note that the symlink resolution is not *completely* iterative.
64 * There is still a significant amount of tail- and mid- recursion in
65 * the algorithm. Also, note that <fs>_readlink() is not used in
66 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
67 * may return different results than <fs>_follow_link(). Many virtual
68 * filesystems (including /proc) exhibit this behavior.
71 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
72 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
73 * and the name already exists in form of a symlink, try to create the new
74 * name indicated by the symlink. The old code always complained that the
75 * name already exists, due to not following the symlink even if its target
76 * is nonexistent. The new semantics affects also mknod() and link() when
77 * the name is a symlink pointing to a non-existent name.
79 * I don't know which semantics is the right one, since I have no access
80 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
81 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
82 * "old" one. Personally, I think the new semantics is much more logical.
83 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
84 * file does succeed in both HP-UX and SunOs, but not in Solaris
85 * and in the old Linux semantics.
88 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
89 * semantics. See the comments in "open_namei" and "do_link" below.
91 * [10-Sep-98 Alan Modra] Another symlink change.
94 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
95 * inside the path - always follow.
96 * in the last component in creation/removal/renaming - never follow.
97 * if LOOKUP_FOLLOW passed - follow.
98 * if the pathname has trailing slashes - follow.
99 * otherwise - don't follow.
100 * (applied in that order).
102 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
103 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
104 * During the 2.4 we need to fix the userland stuff depending on it -
105 * hopefully we will be able to get rid of that wart in 2.5. So far only
106 * XEmacs seems to be relying on it...
109 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
110 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
111 * any extra contention...
114 /* In order to reduce some races, while at the same time doing additional
115 * checking and hopefully speeding things up, we copy filenames to the
116 * kernel data space before using them..
118 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
119 * PATH_MAX includes the nul terminator --RR.
122 #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
124 struct filename *
125 getname_flags(const char __user *filename, int flags, int *empty)
127 struct filename *result;
128 char *kname;
129 int len;
131 result = audit_reusename(filename);
132 if (result)
133 return result;
135 result = __getname();
136 if (unlikely(!result))
137 return ERR_PTR(-ENOMEM);
140 * First, try to embed the struct filename inside the names_cache
141 * allocation
143 kname = (char *)result->iname;
144 result->name = kname;
146 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
147 if (unlikely(len < 0)) {
148 __putname(result);
149 return ERR_PTR(len);
153 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
154 * separate struct filename so we can dedicate the entire
155 * names_cache allocation for the pathname, and re-do the copy from
156 * userland.
158 if (unlikely(len == EMBEDDED_NAME_MAX)) {
159 const size_t size = offsetof(struct filename, iname[1]);
160 kname = (char *)result;
163 * size is chosen that way we to guarantee that
164 * result->iname[0] is within the same object and that
165 * kname can't be equal to result->iname, no matter what.
167 result = kzalloc(size, GFP_KERNEL);
168 if (unlikely(!result)) {
169 __putname(kname);
170 return ERR_PTR(-ENOMEM);
172 result->name = kname;
173 len = strncpy_from_user(kname, filename, PATH_MAX);
174 if (unlikely(len < 0)) {
175 __putname(kname);
176 kfree(result);
177 return ERR_PTR(len);
179 if (unlikely(len == PATH_MAX)) {
180 __putname(kname);
181 kfree(result);
182 return ERR_PTR(-ENAMETOOLONG);
186 result->refcnt = 1;
187 /* The empty path is special. */
188 if (unlikely(!len)) {
189 if (empty)
190 *empty = 1;
191 if (!(flags & LOOKUP_EMPTY)) {
192 putname(result);
193 return ERR_PTR(-ENOENT);
197 result->uptr = filename;
198 result->aname = NULL;
199 audit_getname(result);
200 return result;
203 struct filename *
204 getname(const char __user * filename)
206 return getname_flags(filename, 0, NULL);
209 struct filename *
210 getname_kernel(const char * filename)
212 struct filename *result;
213 int len = strlen(filename) + 1;
215 result = __getname();
216 if (unlikely(!result))
217 return ERR_PTR(-ENOMEM);
219 if (len <= EMBEDDED_NAME_MAX) {
220 result->name = (char *)result->iname;
221 } else if (len <= PATH_MAX) {
222 const size_t size = offsetof(struct filename, iname[1]);
223 struct filename *tmp;
225 tmp = kmalloc(size, GFP_KERNEL);
226 if (unlikely(!tmp)) {
227 __putname(result);
228 return ERR_PTR(-ENOMEM);
230 tmp->name = (char *)result;
231 result = tmp;
232 } else {
233 __putname(result);
234 return ERR_PTR(-ENAMETOOLONG);
236 memcpy((char *)result->name, filename, len);
237 result->uptr = NULL;
238 result->aname = NULL;
239 result->refcnt = 1;
240 audit_getname(result);
242 return result;
245 void putname(struct filename *name)
247 BUG_ON(name->refcnt <= 0);
249 if (--name->refcnt > 0)
250 return;
252 if (name->name != name->iname) {
253 __putname(name->name);
254 kfree(name);
255 } else
256 __putname(name);
259 static int check_acl(struct inode *inode, int mask)
261 #ifdef CONFIG_FS_POSIX_ACL
262 struct posix_acl *acl;
264 if (mask & MAY_NOT_BLOCK) {
265 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
266 if (!acl)
267 return -EAGAIN;
268 /* no ->get_acl() calls in RCU mode... */
269 if (acl == ACL_NOT_CACHED)
270 return -ECHILD;
271 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
274 acl = get_acl(inode, ACL_TYPE_ACCESS);
275 if (IS_ERR(acl))
276 return PTR_ERR(acl);
277 if (acl) {
278 int error = posix_acl_permission(inode, acl, mask);
279 posix_acl_release(acl);
280 return error;
282 #endif
284 return -EAGAIN;
288 * This does the basic permission checking
290 static int acl_permission_check(struct inode *inode, int mask)
292 unsigned int mode = inode->i_mode;
294 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
295 mode >>= 6;
296 else {
297 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
298 int error = check_acl(inode, mask);
299 if (error != -EAGAIN)
300 return error;
303 if (in_group_p(inode->i_gid))
304 mode >>= 3;
308 * If the DACs are ok we don't need any capability check.
310 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
311 return 0;
312 return -EACCES;
316 * generic_permission - check for access rights on a Posix-like filesystem
317 * @inode: inode to check access rights for
318 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
320 * Used to check for read/write/execute permissions on a file.
321 * We use "fsuid" for this, letting us set arbitrary permissions
322 * for filesystem access without changing the "normal" uids which
323 * are used for other things.
325 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
326 * request cannot be satisfied (eg. requires blocking or too much complexity).
327 * It would then be called again in ref-walk mode.
329 int generic_permission(struct inode *inode, int mask)
331 int ret;
334 * Do the basic permission checks.
336 ret = acl_permission_check(inode, mask);
337 if (ret != -EACCES)
338 return ret;
340 if (S_ISDIR(inode->i_mode)) {
341 /* DACs are overridable for directories */
342 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
343 return 0;
344 if (!(mask & MAY_WRITE))
345 if (capable_wrt_inode_uidgid(inode,
346 CAP_DAC_READ_SEARCH))
347 return 0;
348 return -EACCES;
351 * Read/write DACs are always overridable.
352 * Executable DACs are overridable when there is
353 * at least one exec bit set.
355 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
356 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
357 return 0;
360 * Searching includes executable on directories, else just read.
362 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
363 if (mask == MAY_READ)
364 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
365 return 0;
367 return -EACCES;
369 EXPORT_SYMBOL(generic_permission);
372 * We _really_ want to just do "generic_permission()" without
373 * even looking at the inode->i_op values. So we keep a cache
374 * flag in inode->i_opflags, that says "this has not special
375 * permission function, use the fast case".
377 static inline int do_inode_permission(struct inode *inode, int mask)
379 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
380 if (likely(inode->i_op->permission))
381 return inode->i_op->permission(inode, mask);
383 /* This gets set once for the inode lifetime */
384 spin_lock(&inode->i_lock);
385 inode->i_opflags |= IOP_FASTPERM;
386 spin_unlock(&inode->i_lock);
388 return generic_permission(inode, mask);
392 * __inode_permission - Check for access rights to a given inode
393 * @inode: Inode to check permission on
394 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
396 * Check for read/write/execute permissions on an inode.
398 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
400 * This does not check for a read-only file system. You probably want
401 * inode_permission().
403 int __inode_permission(struct inode *inode, int mask)
405 int retval;
407 if (unlikely(mask & MAY_WRITE)) {
409 * Nobody gets write access to an immutable file.
411 if (IS_IMMUTABLE(inode))
412 return -EACCES;
415 retval = do_inode_permission(inode, mask);
416 if (retval)
417 return retval;
419 retval = devcgroup_inode_permission(inode, mask);
420 if (retval)
421 return retval;
423 return security_inode_permission(inode, mask);
425 EXPORT_SYMBOL(__inode_permission);
428 * sb_permission - Check superblock-level permissions
429 * @sb: Superblock of inode to check permission on
430 * @inode: Inode to check permission on
431 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
433 * Separate out file-system wide checks from inode-specific permission checks.
435 static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
437 if (unlikely(mask & MAY_WRITE)) {
438 umode_t mode = inode->i_mode;
440 /* Nobody gets write access to a read-only fs. */
441 if ((sb->s_flags & MS_RDONLY) &&
442 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
443 return -EROFS;
445 return 0;
449 * inode_permission - Check for access rights to a given inode
450 * @inode: Inode to check permission on
451 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
453 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
454 * this, letting us set arbitrary permissions for filesystem access without
455 * changing the "normal" UIDs which are used for other things.
457 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
459 int inode_permission(struct inode *inode, int mask)
461 int retval;
463 retval = sb_permission(inode->i_sb, inode, mask);
464 if (retval)
465 return retval;
466 return __inode_permission(inode, mask);
468 EXPORT_SYMBOL(inode_permission);
471 * path_get - get a reference to a path
472 * @path: path to get the reference to
474 * Given a path increment the reference count to the dentry and the vfsmount.
476 void path_get(const struct path *path)
478 mntget(path->mnt);
479 dget(path->dentry);
481 EXPORT_SYMBOL(path_get);
484 * path_put - put a reference to a path
485 * @path: path to put the reference to
487 * Given a path decrement the reference count to the dentry and the vfsmount.
489 void path_put(const struct path *path)
491 dput(path->dentry);
492 mntput(path->mnt);
494 EXPORT_SYMBOL(path_put);
496 #define EMBEDDED_LEVELS 2
497 struct nameidata {
498 struct path path;
499 struct qstr last;
500 struct path root;
501 struct inode *inode; /* path.dentry.d_inode */
502 unsigned int flags;
503 unsigned seq, m_seq;
504 int last_type;
505 unsigned depth;
506 int total_link_count;
507 struct saved {
508 struct path link;
509 void *cookie;
510 const char *name;
511 struct inode *inode;
512 unsigned seq;
513 } *stack, internal[EMBEDDED_LEVELS];
514 struct filename *name;
515 struct nameidata *saved;
516 unsigned root_seq;
517 int dfd;
520 static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
522 struct nameidata *old = current->nameidata;
523 p->stack = p->internal;
524 p->dfd = dfd;
525 p->name = name;
526 p->total_link_count = old ? old->total_link_count : 0;
527 p->saved = old;
528 current->nameidata = p;
531 static void restore_nameidata(void)
533 struct nameidata *now = current->nameidata, *old = now->saved;
535 current->nameidata = old;
536 if (old)
537 old->total_link_count = now->total_link_count;
538 if (now->stack != now->internal) {
539 kfree(now->stack);
540 now->stack = now->internal;
544 static int __nd_alloc_stack(struct nameidata *nd)
546 struct saved *p;
548 if (nd->flags & LOOKUP_RCU) {
549 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
550 GFP_ATOMIC);
551 if (unlikely(!p))
552 return -ECHILD;
553 } else {
554 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
555 GFP_KERNEL);
556 if (unlikely(!p))
557 return -ENOMEM;
559 memcpy(p, nd->internal, sizeof(nd->internal));
560 nd->stack = p;
561 return 0;
565 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
566 * @path: nameidate to verify
568 * Rename can sometimes move a file or directory outside of a bind
569 * mount, path_connected allows those cases to be detected.
571 static bool path_connected(const struct path *path)
573 struct vfsmount *mnt = path->mnt;
574 struct super_block *sb = mnt->mnt_sb;
576 /* Bind mounts and multi-root filesystems can have disconnected paths */
577 if (!(sb->s_iflags & SB_I_MULTIROOT) && (mnt->mnt_root == sb->s_root))
578 return true;
580 return is_subdir(path->dentry, mnt->mnt_root);
583 static inline int nd_alloc_stack(struct nameidata *nd)
585 if (likely(nd->depth != EMBEDDED_LEVELS))
586 return 0;
587 if (likely(nd->stack != nd->internal))
588 return 0;
589 return __nd_alloc_stack(nd);
592 static void drop_links(struct nameidata *nd)
594 int i = nd->depth;
595 while (i--) {
596 struct saved *last = nd->stack + i;
597 struct inode *inode = last->inode;
598 if (last->cookie && inode->i_op->put_link) {
599 inode->i_op->put_link(inode, last->cookie);
600 last->cookie = NULL;
605 static void terminate_walk(struct nameidata *nd)
607 drop_links(nd);
608 if (!(nd->flags & LOOKUP_RCU)) {
609 int i;
610 path_put(&nd->path);
611 for (i = 0; i < nd->depth; i++)
612 path_put(&nd->stack[i].link);
613 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
614 path_put(&nd->root);
615 nd->root.mnt = NULL;
617 } else {
618 nd->flags &= ~LOOKUP_RCU;
619 if (!(nd->flags & LOOKUP_ROOT))
620 nd->root.mnt = NULL;
621 rcu_read_unlock();
623 nd->depth = 0;
626 /* path_put is needed afterwards regardless of success or failure */
627 static bool legitimize_path(struct nameidata *nd,
628 struct path *path, unsigned seq)
630 int res = __legitimize_mnt(path->mnt, nd->m_seq);
631 if (unlikely(res)) {
632 if (res > 0)
633 path->mnt = NULL;
634 path->dentry = NULL;
635 return false;
637 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
638 path->dentry = NULL;
639 return false;
641 return !read_seqcount_retry(&path->dentry->d_seq, seq);
644 static bool legitimize_links(struct nameidata *nd)
646 int i;
647 for (i = 0; i < nd->depth; i++) {
648 struct saved *last = nd->stack + i;
649 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
650 drop_links(nd);
651 nd->depth = i + 1;
652 return false;
655 return true;
659 * Path walking has 2 modes, rcu-walk and ref-walk (see
660 * Documentation/filesystems/path-lookup.txt). In situations when we can't
661 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
662 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
663 * mode. Refcounts are grabbed at the last known good point before rcu-walk
664 * got stuck, so ref-walk may continue from there. If this is not successful
665 * (eg. a seqcount has changed), then failure is returned and it's up to caller
666 * to restart the path walk from the beginning in ref-walk mode.
670 * unlazy_walk - try to switch to ref-walk mode.
671 * @nd: nameidata pathwalk data
672 * @dentry: child of nd->path.dentry or NULL
673 * @seq: seq number to check dentry against
674 * Returns: 0 on success, -ECHILD on failure
676 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
677 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
678 * @nd or NULL. Must be called from rcu-walk context.
679 * Nothing should touch nameidata between unlazy_walk() failure and
680 * terminate_walk().
682 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry, unsigned seq)
684 struct dentry *parent = nd->path.dentry;
686 BUG_ON(!(nd->flags & LOOKUP_RCU));
688 nd->flags &= ~LOOKUP_RCU;
689 if (unlikely(!legitimize_links(nd)))
690 goto out2;
691 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
692 goto out2;
693 if (unlikely(!lockref_get_not_dead(&parent->d_lockref)))
694 goto out1;
697 * For a negative lookup, the lookup sequence point is the parents
698 * sequence point, and it only needs to revalidate the parent dentry.
700 * For a positive lookup, we need to move both the parent and the
701 * dentry from the RCU domain to be properly refcounted. And the
702 * sequence number in the dentry validates *both* dentry counters,
703 * since we checked the sequence number of the parent after we got
704 * the child sequence number. So we know the parent must still
705 * be valid if the child sequence number is still valid.
707 if (!dentry) {
708 if (read_seqcount_retry(&parent->d_seq, nd->seq))
709 goto out;
710 BUG_ON(nd->inode != parent->d_inode);
711 } else {
712 if (!lockref_get_not_dead(&dentry->d_lockref))
713 goto out;
714 if (read_seqcount_retry(&dentry->d_seq, seq))
715 goto drop_dentry;
719 * Sequence counts matched. Now make sure that the root is
720 * still valid and get it if required.
722 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
723 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
724 rcu_read_unlock();
725 dput(dentry);
726 return -ECHILD;
730 rcu_read_unlock();
731 return 0;
733 drop_dentry:
734 rcu_read_unlock();
735 dput(dentry);
736 goto drop_root_mnt;
737 out2:
738 nd->path.mnt = NULL;
739 out1:
740 nd->path.dentry = NULL;
741 out:
742 rcu_read_unlock();
743 drop_root_mnt:
744 if (!(nd->flags & LOOKUP_ROOT))
745 nd->root.mnt = NULL;
746 return -ECHILD;
749 static int unlazy_link(struct nameidata *nd, struct path *link, unsigned seq)
751 if (unlikely(!legitimize_path(nd, link, seq))) {
752 drop_links(nd);
753 nd->depth = 0;
754 nd->flags &= ~LOOKUP_RCU;
755 nd->path.mnt = NULL;
756 nd->path.dentry = NULL;
757 if (!(nd->flags & LOOKUP_ROOT))
758 nd->root.mnt = NULL;
759 rcu_read_unlock();
760 } else if (likely(unlazy_walk(nd, NULL, 0)) == 0) {
761 return 0;
763 path_put(link);
764 return -ECHILD;
767 static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
769 return dentry->d_op->d_revalidate(dentry, flags);
773 * complete_walk - successful completion of path walk
774 * @nd: pointer nameidata
776 * If we had been in RCU mode, drop out of it and legitimize nd->path.
777 * Revalidate the final result, unless we'd already done that during
778 * the path walk or the filesystem doesn't ask for it. Return 0 on
779 * success, -error on failure. In case of failure caller does not
780 * need to drop nd->path.
782 static int complete_walk(struct nameidata *nd)
784 struct dentry *dentry = nd->path.dentry;
785 int status;
787 if (nd->flags & LOOKUP_RCU) {
788 if (!(nd->flags & LOOKUP_ROOT))
789 nd->root.mnt = NULL;
790 if (unlikely(unlazy_walk(nd, NULL, 0)))
791 return -ECHILD;
794 if (likely(!(nd->flags & LOOKUP_JUMPED)))
795 return 0;
797 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
798 return 0;
800 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
801 if (status > 0)
802 return 0;
804 if (!status)
805 status = -ESTALE;
807 return status;
810 static void set_root(struct nameidata *nd)
812 get_fs_root(current->fs, &nd->root);
815 static void set_root_rcu(struct nameidata *nd)
817 struct fs_struct *fs = current->fs;
818 unsigned seq;
820 do {
821 seq = read_seqcount_begin(&fs->seq);
822 nd->root = fs->root;
823 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
824 } while (read_seqcount_retry(&fs->seq, seq));
827 static void path_put_conditional(struct path *path, struct nameidata *nd)
829 dput(path->dentry);
830 if (path->mnt != nd->path.mnt)
831 mntput(path->mnt);
834 static inline void path_to_nameidata(const struct path *path,
835 struct nameidata *nd)
837 if (!(nd->flags & LOOKUP_RCU)) {
838 dput(nd->path.dentry);
839 if (nd->path.mnt != path->mnt)
840 mntput(nd->path.mnt);
842 nd->path.mnt = path->mnt;
843 nd->path.dentry = path->dentry;
847 * Helper to directly jump to a known parsed path from ->follow_link,
848 * caller must have taken a reference to path beforehand.
850 void nd_jump_link(struct path *path)
852 struct nameidata *nd = current->nameidata;
853 path_put(&nd->path);
855 nd->path = *path;
856 nd->inode = nd->path.dentry->d_inode;
857 nd->flags |= LOOKUP_JUMPED;
860 static inline void put_link(struct nameidata *nd)
862 struct saved *last = nd->stack + --nd->depth;
863 struct inode *inode = last->inode;
864 if (last->cookie && inode->i_op->put_link)
865 inode->i_op->put_link(inode, last->cookie);
866 if (!(nd->flags & LOOKUP_RCU))
867 path_put(&last->link);
870 int sysctl_protected_symlinks __read_mostly = 0;
871 int sysctl_protected_hardlinks __read_mostly = 0;
874 * may_follow_link - Check symlink following for unsafe situations
875 * @nd: nameidata pathwalk data
877 * In the case of the sysctl_protected_symlinks sysctl being enabled,
878 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
879 * in a sticky world-writable directory. This is to protect privileged
880 * processes from failing races against path names that may change out
881 * from under them by way of other users creating malicious symlinks.
882 * It will permit symlinks to be followed only when outside a sticky
883 * world-writable directory, or when the uid of the symlink and follower
884 * match, or when the directory owner matches the symlink's owner.
886 * Returns 0 if following the symlink is allowed, -ve on error.
888 static inline int may_follow_link(struct nameidata *nd)
890 const struct inode *inode;
891 const struct inode *parent;
892 kuid_t puid;
894 if (!sysctl_protected_symlinks)
895 return 0;
897 /* Allowed if owner and follower match. */
898 inode = nd->stack[0].inode;
899 if (uid_eq(current_cred()->fsuid, inode->i_uid))
900 return 0;
902 /* Allowed if parent directory not sticky and world-writable. */
903 parent = nd->inode;
904 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
905 return 0;
907 /* Allowed if parent directory and link owner match. */
908 puid = parent->i_uid;
909 if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
910 return 0;
912 if (nd->flags & LOOKUP_RCU)
913 return -ECHILD;
915 audit_log_link_denied("follow_link", &nd->stack[0].link);
916 return -EACCES;
920 * safe_hardlink_source - Check for safe hardlink conditions
921 * @inode: the source inode to hardlink from
923 * Return false if at least one of the following conditions:
924 * - inode is not a regular file
925 * - inode is setuid
926 * - inode is setgid and group-exec
927 * - access failure for read and write
929 * Otherwise returns true.
931 static bool safe_hardlink_source(struct inode *inode)
933 umode_t mode = inode->i_mode;
935 /* Special files should not get pinned to the filesystem. */
936 if (!S_ISREG(mode))
937 return false;
939 /* Setuid files should not get pinned to the filesystem. */
940 if (mode & S_ISUID)
941 return false;
943 /* Executable setgid files should not get pinned to the filesystem. */
944 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
945 return false;
947 /* Hardlinking to unreadable or unwritable sources is dangerous. */
948 if (inode_permission(inode, MAY_READ | MAY_WRITE))
949 return false;
951 return true;
955 * may_linkat - Check permissions for creating a hardlink
956 * @link: the source to hardlink from
958 * Block hardlink when all of:
959 * - sysctl_protected_hardlinks enabled
960 * - fsuid does not match inode
961 * - hardlink source is unsafe (see safe_hardlink_source() above)
962 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
964 * Returns 0 if successful, -ve on error.
966 static int may_linkat(struct path *link)
968 struct inode *inode;
970 if (!sysctl_protected_hardlinks)
971 return 0;
973 inode = link->dentry->d_inode;
975 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
976 * otherwise, it must be a safe source.
978 if (inode_owner_or_capable(inode) || safe_hardlink_source(inode))
979 return 0;
981 audit_log_link_denied("linkat", link);
982 return -EPERM;
985 static __always_inline
986 const char *get_link(struct nameidata *nd)
988 struct saved *last = nd->stack + nd->depth - 1;
989 struct dentry *dentry = last->link.dentry;
990 struct inode *inode = last->inode;
991 int error;
992 const char *res;
994 if (!(nd->flags & LOOKUP_RCU)) {
995 touch_atime(&last->link);
996 cond_resched();
997 } else if (atime_needs_update(&last->link, inode)) {
998 if (unlikely(unlazy_walk(nd, NULL, 0)))
999 return ERR_PTR(-ECHILD);
1000 touch_atime(&last->link);
1003 error = security_inode_follow_link(dentry, inode,
1004 nd->flags & LOOKUP_RCU);
1005 if (unlikely(error))
1006 return ERR_PTR(error);
1008 nd->last_type = LAST_BIND;
1009 res = inode->i_link;
1010 if (!res) {
1011 if (nd->flags & LOOKUP_RCU) {
1012 if (unlikely(unlazy_walk(nd, NULL, 0)))
1013 return ERR_PTR(-ECHILD);
1015 res = inode->i_op->follow_link(dentry, &last->cookie);
1016 if (IS_ERR_OR_NULL(res)) {
1017 last->cookie = NULL;
1018 return res;
1021 if (*res == '/') {
1022 if (nd->flags & LOOKUP_RCU) {
1023 struct dentry *d;
1024 if (!nd->root.mnt)
1025 set_root_rcu(nd);
1026 nd->path = nd->root;
1027 d = nd->path.dentry;
1028 nd->inode = d->d_inode;
1029 nd->seq = nd->root_seq;
1030 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
1031 return ERR_PTR(-ECHILD);
1032 } else {
1033 if (!nd->root.mnt)
1034 set_root(nd);
1035 path_put(&nd->path);
1036 nd->path = nd->root;
1037 path_get(&nd->root);
1038 nd->inode = nd->path.dentry->d_inode;
1040 nd->flags |= LOOKUP_JUMPED;
1041 while (unlikely(*++res == '/'))
1044 if (!*res)
1045 res = NULL;
1046 return res;
1050 * follow_up - Find the mountpoint of path's vfsmount
1052 * Given a path, find the mountpoint of its source file system.
1053 * Replace @path with the path of the mountpoint in the parent mount.
1054 * Up is towards /.
1056 * Return 1 if we went up a level and 0 if we were already at the
1057 * root.
1059 int follow_up(struct path *path)
1061 struct mount *mnt = real_mount(path->mnt);
1062 struct mount *parent;
1063 struct dentry *mountpoint;
1065 read_seqlock_excl(&mount_lock);
1066 parent = mnt->mnt_parent;
1067 if (parent == mnt) {
1068 read_sequnlock_excl(&mount_lock);
1069 return 0;
1071 mntget(&parent->mnt);
1072 mountpoint = dget(mnt->mnt_mountpoint);
1073 read_sequnlock_excl(&mount_lock);
1074 dput(path->dentry);
1075 path->dentry = mountpoint;
1076 mntput(path->mnt);
1077 path->mnt = &parent->mnt;
1078 return 1;
1080 EXPORT_SYMBOL(follow_up);
1083 * Perform an automount
1084 * - return -EISDIR to tell follow_managed() to stop and return the path we
1085 * were called with.
1087 static int follow_automount(struct path *path, struct nameidata *nd,
1088 bool *need_mntput)
1090 struct vfsmount *mnt;
1091 int err;
1093 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1094 return -EREMOTE;
1096 /* We don't want to mount if someone's just doing a stat -
1097 * unless they're stat'ing a directory and appended a '/' to
1098 * the name.
1100 * We do, however, want to mount if someone wants to open or
1101 * create a file of any type under the mountpoint, wants to
1102 * traverse through the mountpoint or wants to open the
1103 * mounted directory. Also, autofs may mark negative dentries
1104 * as being automount points. These will need the attentions
1105 * of the daemon to instantiate them before they can be used.
1107 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
1108 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
1109 path->dentry->d_inode)
1110 return -EISDIR;
1112 nd->total_link_count++;
1113 if (nd->total_link_count >= 40)
1114 return -ELOOP;
1116 mnt = path->dentry->d_op->d_automount(path);
1117 if (IS_ERR(mnt)) {
1119 * The filesystem is allowed to return -EISDIR here to indicate
1120 * it doesn't want to automount. For instance, autofs would do
1121 * this so that its userspace daemon can mount on this dentry.
1123 * However, we can only permit this if it's a terminal point in
1124 * the path being looked up; if it wasn't then the remainder of
1125 * the path is inaccessible and we should say so.
1127 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
1128 return -EREMOTE;
1129 return PTR_ERR(mnt);
1132 if (!mnt) /* mount collision */
1133 return 0;
1135 if (!*need_mntput) {
1136 /* lock_mount() may release path->mnt on error */
1137 mntget(path->mnt);
1138 *need_mntput = true;
1140 err = finish_automount(mnt, path);
1142 switch (err) {
1143 case -EBUSY:
1144 /* Someone else made a mount here whilst we were busy */
1145 return 0;
1146 case 0:
1147 path_put(path);
1148 path->mnt = mnt;
1149 path->dentry = dget(mnt->mnt_root);
1150 return 0;
1151 default:
1152 return err;
1158 * Handle a dentry that is managed in some way.
1159 * - Flagged for transit management (autofs)
1160 * - Flagged as mountpoint
1161 * - Flagged as automount point
1163 * This may only be called in refwalk mode.
1165 * Serialization is taken care of in namespace.c
1167 static int follow_managed(struct path *path, struct nameidata *nd)
1169 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
1170 unsigned managed;
1171 bool need_mntput = false;
1172 int ret = 0;
1174 /* Given that we're not holding a lock here, we retain the value in a
1175 * local variable for each dentry as we look at it so that we don't see
1176 * the components of that value change under us */
1177 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1178 managed &= DCACHE_MANAGED_DENTRY,
1179 unlikely(managed != 0)) {
1180 /* Allow the filesystem to manage the transit without i_mutex
1181 * being held. */
1182 if (managed & DCACHE_MANAGE_TRANSIT) {
1183 BUG_ON(!path->dentry->d_op);
1184 BUG_ON(!path->dentry->d_op->d_manage);
1185 ret = path->dentry->d_op->d_manage(path->dentry, false);
1186 if (ret < 0)
1187 break;
1190 /* Transit to a mounted filesystem. */
1191 if (managed & DCACHE_MOUNTED) {
1192 struct vfsmount *mounted = lookup_mnt(path);
1193 if (mounted) {
1194 dput(path->dentry);
1195 if (need_mntput)
1196 mntput(path->mnt);
1197 path->mnt = mounted;
1198 path->dentry = dget(mounted->mnt_root);
1199 need_mntput = true;
1200 continue;
1203 /* Something is mounted on this dentry in another
1204 * namespace and/or whatever was mounted there in this
1205 * namespace got unmounted before lookup_mnt() could
1206 * get it */
1209 /* Handle an automount point */
1210 if (managed & DCACHE_NEED_AUTOMOUNT) {
1211 ret = follow_automount(path, nd, &need_mntput);
1212 if (ret < 0)
1213 break;
1214 continue;
1217 /* We didn't change the current path point */
1218 break;
1221 if (need_mntput && path->mnt == mnt)
1222 mntput(path->mnt);
1223 if (ret == -EISDIR)
1224 ret = 0;
1225 if (need_mntput)
1226 nd->flags |= LOOKUP_JUMPED;
1227 if (unlikely(ret < 0))
1228 path_put_conditional(path, nd);
1229 return ret;
1232 int follow_down_one(struct path *path)
1234 struct vfsmount *mounted;
1236 mounted = lookup_mnt(path);
1237 if (mounted) {
1238 dput(path->dentry);
1239 mntput(path->mnt);
1240 path->mnt = mounted;
1241 path->dentry = dget(mounted->mnt_root);
1242 return 1;
1244 return 0;
1246 EXPORT_SYMBOL(follow_down_one);
1248 static inline int managed_dentry_rcu(struct dentry *dentry)
1250 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1251 dentry->d_op->d_manage(dentry, true) : 0;
1255 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1256 * we meet a managed dentry that would need blocking.
1258 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1259 struct inode **inode, unsigned *seqp)
1261 for (;;) {
1262 struct mount *mounted;
1264 * Don't forget we might have a non-mountpoint managed dentry
1265 * that wants to block transit.
1267 switch (managed_dentry_rcu(path->dentry)) {
1268 case -ECHILD:
1269 default:
1270 return false;
1271 case -EISDIR:
1272 return true;
1273 case 0:
1274 break;
1277 if (!d_mountpoint(path->dentry))
1278 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1280 mounted = __lookup_mnt(path->mnt, path->dentry);
1281 if (!mounted)
1282 break;
1283 path->mnt = &mounted->mnt;
1284 path->dentry = mounted->mnt.mnt_root;
1285 nd->flags |= LOOKUP_JUMPED;
1286 *seqp = read_seqcount_begin(&path->dentry->d_seq);
1288 * Update the inode too. We don't need to re-check the
1289 * dentry sequence number here after this d_inode read,
1290 * because a mount-point is always pinned.
1292 *inode = path->dentry->d_inode;
1294 return !read_seqretry(&mount_lock, nd->m_seq) &&
1295 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1298 static int follow_dotdot_rcu(struct nameidata *nd)
1300 struct inode *inode = nd->inode;
1301 if (!nd->root.mnt)
1302 set_root_rcu(nd);
1304 while (1) {
1305 if (path_equal(&nd->path, &nd->root))
1306 break;
1307 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1308 struct dentry *old = nd->path.dentry;
1309 struct dentry *parent = old->d_parent;
1310 unsigned seq;
1312 inode = parent->d_inode;
1313 seq = read_seqcount_begin(&parent->d_seq);
1314 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1315 return -ECHILD;
1316 nd->path.dentry = parent;
1317 nd->seq = seq;
1318 if (unlikely(!path_connected(&nd->path)))
1319 return -ENOENT;
1320 break;
1321 } else {
1322 struct mount *mnt = real_mount(nd->path.mnt);
1323 struct mount *mparent = mnt->mnt_parent;
1324 struct dentry *mountpoint = mnt->mnt_mountpoint;
1325 struct inode *inode2 = mountpoint->d_inode;
1326 unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1327 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1328 return -ECHILD;
1329 if (&mparent->mnt == nd->path.mnt)
1330 break;
1331 /* we know that mountpoint was pinned */
1332 nd->path.dentry = mountpoint;
1333 nd->path.mnt = &mparent->mnt;
1334 inode = inode2;
1335 nd->seq = seq;
1338 while (unlikely(d_mountpoint(nd->path.dentry))) {
1339 struct mount *mounted;
1340 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1341 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1342 return -ECHILD;
1343 if (!mounted)
1344 break;
1345 nd->path.mnt = &mounted->mnt;
1346 nd->path.dentry = mounted->mnt.mnt_root;
1347 inode = nd->path.dentry->d_inode;
1348 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1350 nd->inode = inode;
1351 return 0;
1355 * Follow down to the covering mount currently visible to userspace. At each
1356 * point, the filesystem owning that dentry may be queried as to whether the
1357 * caller is permitted to proceed or not.
1359 int follow_down(struct path *path)
1361 unsigned managed;
1362 int ret;
1364 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1365 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1366 /* Allow the filesystem to manage the transit without i_mutex
1367 * being held.
1369 * We indicate to the filesystem if someone is trying to mount
1370 * something here. This gives autofs the chance to deny anyone
1371 * other than its daemon the right to mount on its
1372 * superstructure.
1374 * The filesystem may sleep at this point.
1376 if (managed & DCACHE_MANAGE_TRANSIT) {
1377 BUG_ON(!path->dentry->d_op);
1378 BUG_ON(!path->dentry->d_op->d_manage);
1379 ret = path->dentry->d_op->d_manage(
1380 path->dentry, false);
1381 if (ret < 0)
1382 return ret == -EISDIR ? 0 : ret;
1385 /* Transit to a mounted filesystem. */
1386 if (managed & DCACHE_MOUNTED) {
1387 struct vfsmount *mounted = lookup_mnt(path);
1388 if (!mounted)
1389 break;
1390 dput(path->dentry);
1391 mntput(path->mnt);
1392 path->mnt = mounted;
1393 path->dentry = dget(mounted->mnt_root);
1394 continue;
1397 /* Don't handle automount points here */
1398 break;
1400 return 0;
1402 EXPORT_SYMBOL(follow_down);
1405 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1407 static void follow_mount(struct path *path)
1409 while (d_mountpoint(path->dentry)) {
1410 struct vfsmount *mounted = lookup_mnt(path);
1411 if (!mounted)
1412 break;
1413 dput(path->dentry);
1414 mntput(path->mnt);
1415 path->mnt = mounted;
1416 path->dentry = dget(mounted->mnt_root);
1420 static int follow_dotdot(struct nameidata *nd)
1422 if (!nd->root.mnt)
1423 set_root(nd);
1425 while(1) {
1426 struct dentry *old = nd->path.dentry;
1428 if (nd->path.dentry == nd->root.dentry &&
1429 nd->path.mnt == nd->root.mnt) {
1430 break;
1432 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1433 /* rare case of legitimate dget_parent()... */
1434 nd->path.dentry = dget_parent(nd->path.dentry);
1435 dput(old);
1436 if (unlikely(!path_connected(&nd->path)))
1437 return -ENOENT;
1438 break;
1440 if (!follow_up(&nd->path))
1441 break;
1443 follow_mount(&nd->path);
1444 nd->inode = nd->path.dentry->d_inode;
1445 return 0;
1449 * This looks up the name in dcache, possibly revalidates the old dentry and
1450 * allocates a new one if not found or not valid. In the need_lookup argument
1451 * returns whether i_op->lookup is necessary.
1453 * dir->d_inode->i_mutex must be held
1455 static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1456 unsigned int flags, bool *need_lookup)
1458 struct dentry *dentry;
1459 int error;
1461 *need_lookup = false;
1462 dentry = d_lookup(dir, name);
1463 if (dentry) {
1464 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1465 error = d_revalidate(dentry, flags);
1466 if (unlikely(error <= 0)) {
1467 if (error < 0) {
1468 dput(dentry);
1469 return ERR_PTR(error);
1470 } else {
1471 d_invalidate(dentry);
1472 dput(dentry);
1473 dentry = NULL;
1479 if (!dentry) {
1480 dentry = d_alloc(dir, name);
1481 if (unlikely(!dentry))
1482 return ERR_PTR(-ENOMEM);
1484 *need_lookup = true;
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(struct qstr *name,
1515 struct dentry *base, unsigned int flags)
1517 bool need_lookup;
1518 struct dentry *dentry;
1520 dentry = lookup_dcache(name, base, flags, &need_lookup);
1521 if (!need_lookup)
1522 return dentry;
1524 return lookup_real(base->d_inode, dentry, flags);
1528 * It's more convoluted than I'd like it to be, but... it's still fairly
1529 * small and for now I'd prefer to have fast path as straight as possible.
1530 * It _is_ time-critical.
1532 static int lookup_fast(struct nameidata *nd,
1533 struct path *path, struct inode **inode,
1534 unsigned *seqp)
1536 struct vfsmount *mnt = nd->path.mnt;
1537 struct dentry *dentry, *parent = nd->path.dentry;
1538 int need_reval = 1;
1539 int status = 1;
1540 int err;
1543 * Rename seqlock is not required here because in the off chance
1544 * of a false negative due to a concurrent rename, we're going to
1545 * do the non-racy lookup, below.
1547 if (nd->flags & LOOKUP_RCU) {
1548 unsigned seq;
1549 bool negative;
1550 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
1551 if (!dentry)
1552 goto unlazy;
1555 * This sequence count validates that the inode matches
1556 * the dentry name information from lookup.
1558 *inode = d_backing_inode(dentry);
1559 negative = d_is_negative(dentry);
1560 if (read_seqcount_retry(&dentry->d_seq, seq))
1561 return -ECHILD;
1564 * This sequence count validates that the parent had no
1565 * changes while we did the lookup of the dentry above.
1567 * The memory barrier in read_seqcount_begin of child is
1568 * enough, we can use __read_seqcount_retry here.
1570 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1571 return -ECHILD;
1573 *seqp = seq;
1574 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1575 status = d_revalidate(dentry, nd->flags);
1576 if (unlikely(status <= 0)) {
1577 if (status != -ECHILD)
1578 need_reval = 0;
1579 goto unlazy;
1583 * Note: do negative dentry check after revalidation in
1584 * case that drops it.
1586 if (negative)
1587 return -ENOENT;
1588 path->mnt = mnt;
1589 path->dentry = dentry;
1590 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
1591 return 0;
1592 unlazy:
1593 if (unlazy_walk(nd, dentry, seq))
1594 return -ECHILD;
1595 } else {
1596 dentry = __d_lookup(parent, &nd->last);
1599 if (unlikely(!dentry))
1600 goto need_lookup;
1602 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1603 status = d_revalidate(dentry, nd->flags);
1604 if (unlikely(status <= 0)) {
1605 if (status < 0) {
1606 dput(dentry);
1607 return status;
1609 d_invalidate(dentry);
1610 dput(dentry);
1611 goto need_lookup;
1614 if (unlikely(d_is_negative(dentry))) {
1615 dput(dentry);
1616 return -ENOENT;
1618 path->mnt = mnt;
1619 path->dentry = dentry;
1620 err = follow_managed(path, nd);
1621 if (likely(!err))
1622 *inode = d_backing_inode(path->dentry);
1623 return err;
1625 need_lookup:
1626 return 1;
1629 /* Fast lookup failed, do it the slow way */
1630 static int lookup_slow(struct nameidata *nd, struct path *path)
1632 struct dentry *dentry, *parent;
1634 parent = nd->path.dentry;
1635 BUG_ON(nd->inode != parent->d_inode);
1637 mutex_lock(&parent->d_inode->i_mutex);
1638 dentry = __lookup_hash(&nd->last, parent, nd->flags);
1639 mutex_unlock(&parent->d_inode->i_mutex);
1640 if (IS_ERR(dentry))
1641 return PTR_ERR(dentry);
1642 path->mnt = nd->path.mnt;
1643 path->dentry = dentry;
1644 return follow_managed(path, nd);
1647 static inline int may_lookup(struct nameidata *nd)
1649 if (nd->flags & LOOKUP_RCU) {
1650 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1651 if (err != -ECHILD)
1652 return err;
1653 if (unlazy_walk(nd, NULL, 0))
1654 return -ECHILD;
1656 return inode_permission(nd->inode, MAY_EXEC);
1659 static inline int handle_dots(struct nameidata *nd, int type)
1661 if (type == LAST_DOTDOT) {
1662 if (nd->flags & LOOKUP_RCU) {
1663 return follow_dotdot_rcu(nd);
1664 } else
1665 return follow_dotdot(nd);
1667 return 0;
1670 static int pick_link(struct nameidata *nd, struct path *link,
1671 struct inode *inode, unsigned seq)
1673 int error;
1674 struct saved *last;
1675 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
1676 path_to_nameidata(link, nd);
1677 return -ELOOP;
1679 if (!(nd->flags & LOOKUP_RCU)) {
1680 if (link->mnt == nd->path.mnt)
1681 mntget(link->mnt);
1683 error = nd_alloc_stack(nd);
1684 if (unlikely(error)) {
1685 if (error == -ECHILD) {
1686 if (unlikely(unlazy_link(nd, link, seq)))
1687 return -ECHILD;
1688 error = nd_alloc_stack(nd);
1690 if (error) {
1691 path_put(link);
1692 return error;
1696 last = nd->stack + nd->depth++;
1697 last->link = *link;
1698 last->cookie = NULL;
1699 last->inode = inode;
1700 last->seq = seq;
1701 return 1;
1705 * Do we need to follow links? We _really_ want to be able
1706 * to do this check without having to look at inode->i_op,
1707 * so we keep a cache of "no, this doesn't need follow_link"
1708 * for the common case.
1710 static inline int should_follow_link(struct nameidata *nd, struct path *link,
1711 int follow,
1712 struct inode *inode, unsigned seq)
1714 if (likely(!d_is_symlink(link->dentry)))
1715 return 0;
1716 if (!follow)
1717 return 0;
1718 /* make sure that d_is_symlink above matches inode */
1719 if (nd->flags & LOOKUP_RCU) {
1720 if (read_seqcount_retry(&link->dentry->d_seq, seq))
1721 return -ECHILD;
1723 return pick_link(nd, link, inode, seq);
1726 enum {WALK_GET = 1, WALK_PUT = 2};
1728 static int walk_component(struct nameidata *nd, int flags)
1730 struct path path;
1731 struct inode *inode;
1732 unsigned seq;
1733 int err;
1735 * "." and ".." are special - ".." especially so because it has
1736 * to be able to know about the current root directory and
1737 * parent relationships.
1739 if (unlikely(nd->last_type != LAST_NORM)) {
1740 err = handle_dots(nd, nd->last_type);
1741 if (flags & WALK_PUT)
1742 put_link(nd);
1743 return err;
1745 err = lookup_fast(nd, &path, &inode, &seq);
1746 if (unlikely(err)) {
1747 if (err < 0)
1748 return err;
1750 err = lookup_slow(nd, &path);
1751 if (err < 0)
1752 return err;
1754 seq = 0; /* we are already out of RCU mode */
1755 err = -ENOENT;
1756 if (d_is_negative(path.dentry))
1757 goto out_path_put;
1758 inode = d_backing_inode(path.dentry);
1761 if (flags & WALK_PUT)
1762 put_link(nd);
1763 err = should_follow_link(nd, &path, flags & WALK_GET, inode, seq);
1764 if (unlikely(err))
1765 return err;
1766 path_to_nameidata(&path, nd);
1767 nd->inode = inode;
1768 nd->seq = seq;
1769 return 0;
1771 out_path_put:
1772 path_to_nameidata(&path, nd);
1773 return err;
1777 * We can do the critical dentry name comparison and hashing
1778 * operations one word at a time, but we are limited to:
1780 * - Architectures with fast unaligned word accesses. We could
1781 * do a "get_unaligned()" if this helps and is sufficiently
1782 * fast.
1784 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1785 * do not trap on the (extremely unlikely) case of a page
1786 * crossing operation.
1788 * - Furthermore, we need an efficient 64-bit compile for the
1789 * 64-bit case in order to generate the "number of bytes in
1790 * the final mask". Again, that could be replaced with a
1791 * efficient population count instruction or similar.
1793 #ifdef CONFIG_DCACHE_WORD_ACCESS
1795 #include <asm/word-at-a-time.h>
1797 #ifdef CONFIG_64BIT
1799 static inline unsigned int fold_hash(unsigned long hash)
1801 return hash_64(hash, 32);
1804 #else /* 32-bit case */
1806 #define fold_hash(x) (x)
1808 #endif
1810 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1812 unsigned long a, mask;
1813 unsigned long hash = 0;
1815 for (;;) {
1816 a = load_unaligned_zeropad(name);
1817 if (len < sizeof(unsigned long))
1818 break;
1819 hash += a;
1820 hash *= 9;
1821 name += sizeof(unsigned long);
1822 len -= sizeof(unsigned long);
1823 if (!len)
1824 goto done;
1826 mask = bytemask_from_count(len);
1827 hash += mask & a;
1828 done:
1829 return fold_hash(hash);
1831 EXPORT_SYMBOL(full_name_hash);
1834 * Calculate the length and hash of the path component, and
1835 * return the "hash_len" as the result.
1837 static inline u64 hash_name(const char *name)
1839 unsigned long a, b, adata, bdata, mask, hash, len;
1840 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1842 hash = a = 0;
1843 len = -sizeof(unsigned long);
1844 do {
1845 hash = (hash + a) * 9;
1846 len += sizeof(unsigned long);
1847 a = load_unaligned_zeropad(name+len);
1848 b = a ^ REPEAT_BYTE('/');
1849 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1851 adata = prep_zero_mask(a, adata, &constants);
1852 bdata = prep_zero_mask(b, bdata, &constants);
1854 mask = create_zero_mask(adata | bdata);
1856 hash += a & zero_bytemask(mask);
1857 len += find_zero(mask);
1858 return hashlen_create(fold_hash(hash), len);
1861 #else
1863 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1865 unsigned long hash = init_name_hash();
1866 while (len--)
1867 hash = partial_name_hash(*name++, hash);
1868 return end_name_hash(hash);
1870 EXPORT_SYMBOL(full_name_hash);
1873 * We know there's a real path component here of at least
1874 * one character.
1876 static inline u64 hash_name(const char *name)
1878 unsigned long hash = init_name_hash();
1879 unsigned long len = 0, c;
1881 c = (unsigned char)*name;
1882 do {
1883 len++;
1884 hash = partial_name_hash(c, hash);
1885 c = (unsigned char)name[len];
1886 } while (c && c != '/');
1887 return hashlen_create(end_name_hash(hash), len);
1890 #endif
1893 * Name resolution.
1894 * This is the basic name resolution function, turning a pathname into
1895 * the final dentry. We expect 'base' to be positive and a directory.
1897 * Returns 0 and nd will have valid dentry and mnt on success.
1898 * Returns error and drops reference to input namei data on failure.
1900 static int link_path_walk(const char *name, struct nameidata *nd)
1902 int err;
1904 while (*name=='/')
1905 name++;
1906 if (!*name)
1907 return 0;
1909 /* At this point we know we have a real path component. */
1910 for(;;) {
1911 u64 hash_len;
1912 int type;
1914 err = may_lookup(nd);
1915 if (err)
1916 return err;
1918 hash_len = hash_name(name);
1920 type = LAST_NORM;
1921 if (name[0] == '.') switch (hashlen_len(hash_len)) {
1922 case 2:
1923 if (name[1] == '.') {
1924 type = LAST_DOTDOT;
1925 nd->flags |= LOOKUP_JUMPED;
1927 break;
1928 case 1:
1929 type = LAST_DOT;
1931 if (likely(type == LAST_NORM)) {
1932 struct dentry *parent = nd->path.dentry;
1933 nd->flags &= ~LOOKUP_JUMPED;
1934 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1935 struct qstr this = { { .hash_len = hash_len }, .name = name };
1936 err = parent->d_op->d_hash(parent, &this);
1937 if (err < 0)
1938 return err;
1939 hash_len = this.hash_len;
1940 name = this.name;
1944 nd->last.hash_len = hash_len;
1945 nd->last.name = name;
1946 nd->last_type = type;
1948 name += hashlen_len(hash_len);
1949 if (!*name)
1950 goto OK;
1952 * If it wasn't NUL, we know it was '/'. Skip that
1953 * slash, and continue until no more slashes.
1955 do {
1956 name++;
1957 } while (unlikely(*name == '/'));
1958 if (unlikely(!*name)) {
1960 /* pathname body, done */
1961 if (!nd->depth)
1962 return 0;
1963 name = nd->stack[nd->depth - 1].name;
1964 /* trailing symlink, done */
1965 if (!name)
1966 return 0;
1967 /* last component of nested symlink */
1968 err = walk_component(nd, WALK_GET | WALK_PUT);
1969 } else {
1970 err = walk_component(nd, WALK_GET);
1972 if (err < 0)
1973 return err;
1975 if (err) {
1976 const char *s = get_link(nd);
1978 if (IS_ERR(s))
1979 return PTR_ERR(s);
1980 err = 0;
1981 if (unlikely(!s)) {
1982 /* jumped */
1983 put_link(nd);
1984 } else {
1985 nd->stack[nd->depth - 1].name = name;
1986 name = s;
1987 continue;
1990 if (unlikely(!d_can_lookup(nd->path.dentry))) {
1991 if (nd->flags & LOOKUP_RCU) {
1992 if (unlazy_walk(nd, NULL, 0))
1993 return -ECHILD;
1995 return -ENOTDIR;
2000 static const char *path_init(struct nameidata *nd, unsigned flags)
2002 int retval = 0;
2003 const char *s = nd->name->name;
2005 if (!*s)
2006 flags &= ~LOOKUP_RCU;
2008 nd->last_type = LAST_ROOT; /* if there are only slashes... */
2009 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
2010 nd->depth = 0;
2011 if (flags & LOOKUP_ROOT) {
2012 struct dentry *root = nd->root.dentry;
2013 struct inode *inode = root->d_inode;
2014 if (*s) {
2015 if (!d_can_lookup(root))
2016 return ERR_PTR(-ENOTDIR);
2017 retval = inode_permission(inode, MAY_EXEC);
2018 if (retval)
2019 return ERR_PTR(retval);
2021 nd->path = nd->root;
2022 nd->inode = inode;
2023 if (flags & LOOKUP_RCU) {
2024 rcu_read_lock();
2025 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2026 nd->root_seq = nd->seq;
2027 nd->m_seq = read_seqbegin(&mount_lock);
2028 } else {
2029 path_get(&nd->path);
2031 return s;
2034 nd->root.mnt = NULL;
2036 nd->m_seq = read_seqbegin(&mount_lock);
2037 if (*s == '/') {
2038 if (flags & LOOKUP_RCU) {
2039 rcu_read_lock();
2040 set_root_rcu(nd);
2041 nd->seq = nd->root_seq;
2042 } else {
2043 set_root(nd);
2044 path_get(&nd->root);
2046 nd->path = nd->root;
2047 } else if (nd->dfd == AT_FDCWD) {
2048 if (flags & LOOKUP_RCU) {
2049 struct fs_struct *fs = current->fs;
2050 unsigned seq;
2052 rcu_read_lock();
2054 do {
2055 seq = read_seqcount_begin(&fs->seq);
2056 nd->path = fs->pwd;
2057 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2058 } while (read_seqcount_retry(&fs->seq, seq));
2059 } else {
2060 get_fs_pwd(current->fs, &nd->path);
2062 } else {
2063 /* Caller must check execute permissions on the starting path component */
2064 struct fd f = fdget_raw(nd->dfd);
2065 struct dentry *dentry;
2067 if (!f.file)
2068 return ERR_PTR(-EBADF);
2070 dentry = f.file->f_path.dentry;
2072 if (*s) {
2073 if (!d_can_lookup(dentry)) {
2074 fdput(f);
2075 return ERR_PTR(-ENOTDIR);
2079 nd->path = f.file->f_path;
2080 if (flags & LOOKUP_RCU) {
2081 rcu_read_lock();
2082 nd->inode = nd->path.dentry->d_inode;
2083 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
2084 } else {
2085 path_get(&nd->path);
2086 nd->inode = nd->path.dentry->d_inode;
2088 fdput(f);
2089 return s;
2092 nd->inode = nd->path.dentry->d_inode;
2093 if (!(flags & LOOKUP_RCU))
2094 return s;
2095 if (likely(!read_seqcount_retry(&nd->path.dentry->d_seq, nd->seq)))
2096 return s;
2097 if (!(nd->flags & LOOKUP_ROOT))
2098 nd->root.mnt = NULL;
2099 rcu_read_unlock();
2100 return ERR_PTR(-ECHILD);
2103 static const char *trailing_symlink(struct nameidata *nd)
2105 const char *s;
2106 int error = may_follow_link(nd);
2107 if (unlikely(error))
2108 return ERR_PTR(error);
2109 nd->flags |= LOOKUP_PARENT;
2110 nd->stack[0].name = NULL;
2111 s = get_link(nd);
2112 return s ? s : "";
2115 static inline int lookup_last(struct nameidata *nd)
2117 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2118 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2120 nd->flags &= ~LOOKUP_PARENT;
2121 return walk_component(nd,
2122 nd->flags & LOOKUP_FOLLOW
2123 ? nd->depth
2124 ? WALK_PUT | WALK_GET
2125 : WALK_GET
2126 : 0);
2129 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2130 static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
2132 const char *s = path_init(nd, flags);
2133 int err;
2135 if (IS_ERR(s))
2136 return PTR_ERR(s);
2137 while (!(err = link_path_walk(s, nd))
2138 && ((err = lookup_last(nd)) > 0)) {
2139 s = trailing_symlink(nd);
2140 if (IS_ERR(s)) {
2141 err = PTR_ERR(s);
2142 break;
2145 if (!err)
2146 err = complete_walk(nd);
2148 if (!err && nd->flags & LOOKUP_DIRECTORY)
2149 if (!d_can_lookup(nd->path.dentry))
2150 err = -ENOTDIR;
2151 if (!err) {
2152 *path = nd->path;
2153 nd->path.mnt = NULL;
2154 nd->path.dentry = NULL;
2156 terminate_walk(nd);
2157 return err;
2160 static int filename_lookup(int dfd, struct filename *name, unsigned flags,
2161 struct path *path, struct path *root)
2163 int retval;
2164 struct nameidata nd;
2165 if (IS_ERR(name))
2166 return PTR_ERR(name);
2167 if (unlikely(root)) {
2168 nd.root = *root;
2169 flags |= LOOKUP_ROOT;
2171 set_nameidata(&nd, dfd, name);
2172 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
2173 if (unlikely(retval == -ECHILD))
2174 retval = path_lookupat(&nd, flags, path);
2175 if (unlikely(retval == -ESTALE))
2176 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
2178 if (likely(!retval))
2179 audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
2180 restore_nameidata();
2181 putname(name);
2182 return retval;
2185 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2186 static int path_parentat(struct nameidata *nd, unsigned flags,
2187 struct path *parent)
2189 const char *s = path_init(nd, flags);
2190 int err;
2191 if (IS_ERR(s))
2192 return PTR_ERR(s);
2193 err = link_path_walk(s, nd);
2194 if (!err)
2195 err = complete_walk(nd);
2196 if (!err) {
2197 *parent = nd->path;
2198 nd->path.mnt = NULL;
2199 nd->path.dentry = NULL;
2201 terminate_walk(nd);
2202 return err;
2205 static struct filename *filename_parentat(int dfd, struct filename *name,
2206 unsigned int flags, struct path *parent,
2207 struct qstr *last, int *type)
2209 int retval;
2210 struct nameidata nd;
2212 if (IS_ERR(name))
2213 return name;
2214 set_nameidata(&nd, dfd, name);
2215 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
2216 if (unlikely(retval == -ECHILD))
2217 retval = path_parentat(&nd, flags, parent);
2218 if (unlikely(retval == -ESTALE))
2219 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
2220 if (likely(!retval)) {
2221 *last = nd.last;
2222 *type = nd.last_type;
2223 audit_inode(name, parent->dentry, LOOKUP_PARENT);
2224 } else {
2225 putname(name);
2226 name = ERR_PTR(retval);
2228 restore_nameidata();
2229 return name;
2232 /* does lookup, returns the object with parent locked */
2233 struct dentry *kern_path_locked(const char *name, struct path *path)
2235 struct filename *filename;
2236 struct dentry *d;
2237 struct qstr last;
2238 int type;
2240 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2241 &last, &type);
2242 if (IS_ERR(filename))
2243 return ERR_CAST(filename);
2244 if (unlikely(type != LAST_NORM)) {
2245 path_put(path);
2246 putname(filename);
2247 return ERR_PTR(-EINVAL);
2249 mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2250 d = __lookup_hash(&last, path->dentry, 0);
2251 if (IS_ERR(d)) {
2252 mutex_unlock(&path->dentry->d_inode->i_mutex);
2253 path_put(path);
2255 putname(filename);
2256 return d;
2259 int kern_path(const char *name, unsigned int flags, struct path *path)
2261 return filename_lookup(AT_FDCWD, getname_kernel(name),
2262 flags, path, NULL);
2264 EXPORT_SYMBOL(kern_path);
2267 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2268 * @dentry: pointer to dentry of the base directory
2269 * @mnt: pointer to vfs mount of the base directory
2270 * @name: pointer to file name
2271 * @flags: lookup flags
2272 * @path: pointer to struct path to fill
2274 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2275 const char *name, unsigned int flags,
2276 struct path *path)
2278 struct path root = {.mnt = mnt, .dentry = dentry};
2279 /* the first argument of filename_lookup() is ignored with root */
2280 return filename_lookup(AT_FDCWD, getname_kernel(name),
2281 flags , path, &root);
2283 EXPORT_SYMBOL(vfs_path_lookup);
2286 * lookup_one_len - filesystem helper to lookup single pathname component
2287 * @name: pathname component to lookup
2288 * @base: base directory to lookup from
2289 * @len: maximum length @len should be interpreted to
2291 * Note that this routine is purely a helper for filesystem usage and should
2292 * not be called by generic code.
2294 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2296 struct qstr this;
2297 unsigned int c;
2298 int err;
2300 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2302 this.name = name;
2303 this.len = len;
2304 this.hash = full_name_hash(name, len);
2305 if (!len)
2306 return ERR_PTR(-EACCES);
2308 if (unlikely(name[0] == '.')) {
2309 if (len < 2 || (len == 2 && name[1] == '.'))
2310 return ERR_PTR(-EACCES);
2313 while (len--) {
2314 c = *(const unsigned char *)name++;
2315 if (c == '/' || c == '\0')
2316 return ERR_PTR(-EACCES);
2319 * See if the low-level filesystem might want
2320 * to use its own hash..
2322 if (base->d_flags & DCACHE_OP_HASH) {
2323 int err = base->d_op->d_hash(base, &this);
2324 if (err < 0)
2325 return ERR_PTR(err);
2328 err = inode_permission(base->d_inode, MAY_EXEC);
2329 if (err)
2330 return ERR_PTR(err);
2332 return __lookup_hash(&this, base, 0);
2334 EXPORT_SYMBOL(lookup_one_len);
2336 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2337 struct path *path, int *empty)
2339 return filename_lookup(dfd, getname_flags(name, flags, empty),
2340 flags, path, NULL);
2342 EXPORT_SYMBOL(user_path_at_empty);
2345 * NB: most callers don't do anything directly with the reference to the
2346 * to struct filename, but the nd->last pointer points into the name string
2347 * allocated by getname. So we must hold the reference to it until all
2348 * path-walking is complete.
2350 static inline struct filename *
2351 user_path_parent(int dfd, const char __user *path,
2352 struct path *parent,
2353 struct qstr *last,
2354 int *type,
2355 unsigned int flags)
2357 /* only LOOKUP_REVAL is allowed in extra flags */
2358 return filename_parentat(dfd, getname(path), flags & LOOKUP_REVAL,
2359 parent, last, type);
2363 * mountpoint_last - look up last component for umount
2364 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2365 * @path: pointer to container for result
2367 * This is a special lookup_last function just for umount. In this case, we
2368 * need to resolve the path without doing any revalidation.
2370 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2371 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2372 * in almost all cases, this lookup will be served out of the dcache. The only
2373 * cases where it won't are if nd->last refers to a symlink or the path is
2374 * bogus and it doesn't exist.
2376 * Returns:
2377 * -error: if there was an error during lookup. This includes -ENOENT if the
2378 * lookup found a negative dentry. The nd->path reference will also be
2379 * put in this case.
2381 * 0: if we successfully resolved nd->path and found it to not to be a
2382 * symlink that needs to be followed. "path" will also be populated.
2383 * The nd->path reference will also be put.
2385 * 1: if we successfully resolved nd->last and found it to be a symlink
2386 * that needs to be followed. "path" will be populated with the path
2387 * to the link, and nd->path will *not* be put.
2389 static int
2390 mountpoint_last(struct nameidata *nd, struct path *path)
2392 int error = 0;
2393 struct dentry *dentry;
2394 struct dentry *dir = nd->path.dentry;
2396 /* If we're in rcuwalk, drop out of it to handle last component */
2397 if (nd->flags & LOOKUP_RCU) {
2398 if (unlazy_walk(nd, NULL, 0))
2399 return -ECHILD;
2402 nd->flags &= ~LOOKUP_PARENT;
2404 if (unlikely(nd->last_type != LAST_NORM)) {
2405 error = handle_dots(nd, nd->last_type);
2406 if (error)
2407 return error;
2408 dentry = dget(nd->path.dentry);
2409 goto done;
2412 mutex_lock(&dir->d_inode->i_mutex);
2413 dentry = d_lookup(dir, &nd->last);
2414 if (!dentry) {
2416 * No cached dentry. Mounted dentries are pinned in the cache,
2417 * so that means that this dentry is probably a symlink or the
2418 * path doesn't actually point to a mounted dentry.
2420 dentry = d_alloc(dir, &nd->last);
2421 if (!dentry) {
2422 mutex_unlock(&dir->d_inode->i_mutex);
2423 return -ENOMEM;
2425 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
2426 if (IS_ERR(dentry)) {
2427 mutex_unlock(&dir->d_inode->i_mutex);
2428 return PTR_ERR(dentry);
2431 mutex_unlock(&dir->d_inode->i_mutex);
2433 done:
2434 if (d_is_negative(dentry)) {
2435 dput(dentry);
2436 return -ENOENT;
2438 if (nd->depth)
2439 put_link(nd);
2440 path->dentry = dentry;
2441 path->mnt = nd->path.mnt;
2442 error = should_follow_link(nd, path, nd->flags & LOOKUP_FOLLOW,
2443 d_backing_inode(dentry), 0);
2444 if (unlikely(error))
2445 return error;
2446 mntget(path->mnt);
2447 follow_mount(path);
2448 return 0;
2452 * path_mountpoint - look up a path to be umounted
2453 * @nd: lookup context
2454 * @flags: lookup flags
2455 * @path: pointer to container for result
2457 * Look up the given name, but don't attempt to revalidate the last component.
2458 * Returns 0 and "path" will be valid on success; Returns error otherwise.
2460 static int
2461 path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
2463 const char *s = path_init(nd, flags);
2464 int err;
2465 if (IS_ERR(s))
2466 return PTR_ERR(s);
2467 while (!(err = link_path_walk(s, nd)) &&
2468 (err = mountpoint_last(nd, path)) > 0) {
2469 s = trailing_symlink(nd);
2470 if (IS_ERR(s)) {
2471 err = PTR_ERR(s);
2472 break;
2475 terminate_walk(nd);
2476 return err;
2479 static int
2480 filename_mountpoint(int dfd, struct filename *name, struct path *path,
2481 unsigned int flags)
2483 struct nameidata nd;
2484 int error;
2485 if (IS_ERR(name))
2486 return PTR_ERR(name);
2487 set_nameidata(&nd, dfd, name);
2488 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
2489 if (unlikely(error == -ECHILD))
2490 error = path_mountpoint(&nd, flags, path);
2491 if (unlikely(error == -ESTALE))
2492 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
2493 if (likely(!error))
2494 audit_inode(name, path->dentry, 0);
2495 restore_nameidata();
2496 putname(name);
2497 return error;
2501 * user_path_mountpoint_at - lookup a path from userland in order to umount it
2502 * @dfd: directory file descriptor
2503 * @name: pathname from userland
2504 * @flags: lookup flags
2505 * @path: pointer to container to hold result
2507 * A umount is a special case for path walking. We're not actually interested
2508 * in the inode in this situation, and ESTALE errors can be a problem. We
2509 * simply want track down the dentry and vfsmount attached at the mountpoint
2510 * and avoid revalidating the last component.
2512 * Returns 0 and populates "path" on success.
2515 user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
2516 struct path *path)
2518 return filename_mountpoint(dfd, getname(name), path, flags);
2522 kern_path_mountpoint(int dfd, const char *name, struct path *path,
2523 unsigned int flags)
2525 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
2527 EXPORT_SYMBOL(kern_path_mountpoint);
2529 int __check_sticky(struct inode *dir, struct inode *inode)
2531 kuid_t fsuid = current_fsuid();
2533 if (uid_eq(inode->i_uid, fsuid))
2534 return 0;
2535 if (uid_eq(dir->i_uid, fsuid))
2536 return 0;
2537 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
2539 EXPORT_SYMBOL(__check_sticky);
2542 * Check whether we can remove a link victim from directory dir, check
2543 * whether the type of victim is right.
2544 * 1. We can't do it if dir is read-only (done in permission())
2545 * 2. We should have write and exec permissions on dir
2546 * 3. We can't remove anything from append-only dir
2547 * 4. We can't do anything with immutable dir (done in permission())
2548 * 5. If the sticky bit on dir is set we should either
2549 * a. be owner of dir, or
2550 * b. be owner of victim, or
2551 * c. have CAP_FOWNER capability
2552 * 6. If the victim is append-only or immutable we can't do antyhing with
2553 * links pointing to it.
2554 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2555 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2556 * 9. We can't remove a root or mountpoint.
2557 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2558 * nfs_async_unlink().
2560 static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
2562 struct inode *inode = d_backing_inode(victim);
2563 int error;
2565 if (d_is_negative(victim))
2566 return -ENOENT;
2567 BUG_ON(!inode);
2569 BUG_ON(victim->d_parent->d_inode != dir);
2570 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
2572 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
2573 if (error)
2574 return error;
2575 if (IS_APPEND(dir))
2576 return -EPERM;
2578 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2579 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
2580 return -EPERM;
2581 if (isdir) {
2582 if (!d_is_dir(victim))
2583 return -ENOTDIR;
2584 if (IS_ROOT(victim))
2585 return -EBUSY;
2586 } else if (d_is_dir(victim))
2587 return -EISDIR;
2588 if (IS_DEADDIR(dir))
2589 return -ENOENT;
2590 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2591 return -EBUSY;
2592 return 0;
2595 /* Check whether we can create an object with dentry child in directory
2596 * dir.
2597 * 1. We can't do it if child already exists (open has special treatment for
2598 * this case, but since we are inlined it's OK)
2599 * 2. We can't do it if dir is read-only (done in permission())
2600 * 3. We should have write and exec permissions on dir
2601 * 4. We can't do it if dir is immutable (done in permission())
2603 static inline int may_create(struct inode *dir, struct dentry *child)
2605 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
2606 if (child->d_inode)
2607 return -EEXIST;
2608 if (IS_DEADDIR(dir))
2609 return -ENOENT;
2610 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2614 * p1 and p2 should be directories on the same fs.
2616 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2618 struct dentry *p;
2620 if (p1 == p2) {
2621 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2622 return NULL;
2625 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2627 p = d_ancestor(p2, p1);
2628 if (p) {
2629 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2630 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2631 return p;
2634 p = d_ancestor(p1, p2);
2635 if (p) {
2636 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2637 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2638 return p;
2641 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2642 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT2);
2643 return NULL;
2645 EXPORT_SYMBOL(lock_rename);
2647 void unlock_rename(struct dentry *p1, struct dentry *p2)
2649 mutex_unlock(&p1->d_inode->i_mutex);
2650 if (p1 != p2) {
2651 mutex_unlock(&p2->d_inode->i_mutex);
2652 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2655 EXPORT_SYMBOL(unlock_rename);
2657 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2658 bool want_excl)
2660 int error = may_create(dir, dentry);
2661 if (error)
2662 return error;
2664 if (!dir->i_op->create)
2665 return -EACCES; /* shouldn't it be ENOSYS? */
2666 mode &= S_IALLUGO;
2667 mode |= S_IFREG;
2668 error = security_inode_create(dir, dentry, mode);
2669 if (error)
2670 return error;
2671 error = dir->i_op->create(dir, dentry, mode, want_excl);
2672 if (!error)
2673 fsnotify_create(dir, dentry);
2674 return error;
2676 EXPORT_SYMBOL(vfs_create);
2678 static int may_open(struct path *path, int acc_mode, int flag)
2680 struct dentry *dentry = path->dentry;
2681 struct inode *inode = dentry->d_inode;
2682 int error;
2684 /* O_PATH? */
2685 if (!acc_mode)
2686 return 0;
2688 if (!inode)
2689 return -ENOENT;
2691 switch (inode->i_mode & S_IFMT) {
2692 case S_IFLNK:
2693 return -ELOOP;
2694 case S_IFDIR:
2695 if (acc_mode & MAY_WRITE)
2696 return -EISDIR;
2697 break;
2698 case S_IFBLK:
2699 case S_IFCHR:
2700 if (path->mnt->mnt_flags & MNT_NODEV)
2701 return -EACCES;
2702 /*FALLTHRU*/
2703 case S_IFIFO:
2704 case S_IFSOCK:
2705 flag &= ~O_TRUNC;
2706 break;
2709 error = inode_permission(inode, acc_mode);
2710 if (error)
2711 return error;
2714 * An append-only file must be opened in append mode for writing.
2716 if (IS_APPEND(inode)) {
2717 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2718 return -EPERM;
2719 if (flag & O_TRUNC)
2720 return -EPERM;
2723 /* O_NOATIME can only be set by the owner or superuser */
2724 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2725 return -EPERM;
2727 return 0;
2730 static int handle_truncate(struct file *filp)
2732 struct path *path = &filp->f_path;
2733 struct inode *inode = path->dentry->d_inode;
2734 int error = get_write_access(inode);
2735 if (error)
2736 return error;
2738 * Refuse to truncate files with mandatory locks held on them.
2740 error = locks_verify_locked(filp);
2741 if (!error)
2742 error = security_path_truncate(path);
2743 if (!error) {
2744 error = do_truncate(path->dentry, 0,
2745 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2746 filp);
2748 put_write_access(inode);
2749 return error;
2752 static inline int open_to_namei_flags(int flag)
2754 if ((flag & O_ACCMODE) == 3)
2755 flag--;
2756 return flag;
2759 static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2761 int error = security_path_mknod(dir, dentry, mode, 0);
2762 if (error)
2763 return error;
2765 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2766 if (error)
2767 return error;
2769 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2773 * Attempt to atomically look up, create and open a file from a negative
2774 * dentry.
2776 * Returns 0 if successful. The file will have been created and attached to
2777 * @file by the filesystem calling finish_open().
2779 * Returns 1 if the file was looked up only or didn't need creating. The
2780 * caller will need to perform the open themselves. @path will have been
2781 * updated to point to the new dentry. This may be negative.
2783 * Returns an error code otherwise.
2785 static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2786 struct path *path, struct file *file,
2787 const struct open_flags *op,
2788 bool got_write, bool need_lookup,
2789 int *opened)
2791 struct inode *dir = nd->path.dentry->d_inode;
2792 unsigned open_flag = open_to_namei_flags(op->open_flag);
2793 umode_t mode;
2794 int error;
2795 int acc_mode;
2796 int create_error = 0;
2797 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2798 bool excl;
2800 BUG_ON(dentry->d_inode);
2802 /* Don't create child dentry for a dead directory. */
2803 if (unlikely(IS_DEADDIR(dir))) {
2804 error = -ENOENT;
2805 goto out;
2808 mode = op->mode;
2809 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2810 mode &= ~current_umask();
2812 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2813 if (excl)
2814 open_flag &= ~O_TRUNC;
2817 * Checking write permission is tricky, bacuse we don't know if we are
2818 * going to actually need it: O_CREAT opens should work as long as the
2819 * file exists. But checking existence breaks atomicity. The trick is
2820 * to check access and if not granted clear O_CREAT from the flags.
2822 * Another problem is returing the "right" error value (e.g. for an
2823 * O_EXCL open we want to return EEXIST not EROFS).
2825 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2826 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2827 if (!(open_flag & O_CREAT)) {
2829 * No O_CREATE -> atomicity not a requirement -> fall
2830 * back to lookup + open
2832 goto no_open;
2833 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2834 /* Fall back and fail with the right error */
2835 create_error = -EROFS;
2836 goto no_open;
2837 } else {
2838 /* No side effects, safe to clear O_CREAT */
2839 create_error = -EROFS;
2840 open_flag &= ~O_CREAT;
2844 if (open_flag & O_CREAT) {
2845 error = may_o_create(&nd->path, dentry, mode);
2846 if (error) {
2847 create_error = error;
2848 if (open_flag & O_EXCL)
2849 goto no_open;
2850 open_flag &= ~O_CREAT;
2854 if (nd->flags & LOOKUP_DIRECTORY)
2855 open_flag |= O_DIRECTORY;
2857 file->f_path.dentry = DENTRY_NOT_SET;
2858 file->f_path.mnt = nd->path.mnt;
2859 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
2860 opened);
2861 if (error < 0) {
2862 if (create_error && error == -ENOENT)
2863 error = create_error;
2864 goto out;
2867 if (error) { /* returned 1, that is */
2868 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2869 error = -EIO;
2870 goto out;
2872 if (file->f_path.dentry) {
2873 dput(dentry);
2874 dentry = file->f_path.dentry;
2876 if (*opened & FILE_CREATED)
2877 fsnotify_create(dir, dentry);
2878 if (!dentry->d_inode) {
2879 WARN_ON(*opened & FILE_CREATED);
2880 if (create_error) {
2881 error = create_error;
2882 goto out;
2884 } else {
2885 if (excl && !(*opened & FILE_CREATED)) {
2886 error = -EEXIST;
2887 goto out;
2890 goto looked_up;
2894 * We didn't have the inode before the open, so check open permission
2895 * here.
2897 acc_mode = op->acc_mode;
2898 if (*opened & FILE_CREATED) {
2899 WARN_ON(!(open_flag & O_CREAT));
2900 fsnotify_create(dir, dentry);
2901 acc_mode = MAY_OPEN;
2903 error = may_open(&file->f_path, acc_mode, open_flag);
2904 if (error)
2905 fput(file);
2907 out:
2908 dput(dentry);
2909 return error;
2911 no_open:
2912 if (need_lookup) {
2913 dentry = lookup_real(dir, dentry, nd->flags);
2914 if (IS_ERR(dentry))
2915 return PTR_ERR(dentry);
2917 if (create_error && !dentry->d_inode) {
2918 error = create_error;
2919 goto out;
2921 looked_up:
2922 path->dentry = dentry;
2923 path->mnt = nd->path.mnt;
2924 return 1;
2928 * Look up and maybe create and open the last component.
2930 * Must be called with i_mutex held on parent.
2932 * Returns 0 if the file was successfully atomically created (if necessary) and
2933 * opened. In this case the file will be returned attached to @file.
2935 * Returns 1 if the file was not completely opened at this time, though lookups
2936 * and creations will have been performed and the dentry returned in @path will
2937 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2938 * specified then a negative dentry may be returned.
2940 * An error code is returned otherwise.
2942 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2943 * cleared otherwise prior to returning.
2945 static int lookup_open(struct nameidata *nd, struct path *path,
2946 struct file *file,
2947 const struct open_flags *op,
2948 bool got_write, int *opened)
2950 struct dentry *dir = nd->path.dentry;
2951 struct inode *dir_inode = dir->d_inode;
2952 struct dentry *dentry;
2953 int error;
2954 bool need_lookup;
2956 *opened &= ~FILE_CREATED;
2957 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2958 if (IS_ERR(dentry))
2959 return PTR_ERR(dentry);
2961 /* Cached positive dentry: will open in f_op->open */
2962 if (!need_lookup && dentry->d_inode)
2963 goto out_no_open;
2965 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
2966 return atomic_open(nd, dentry, path, file, op, got_write,
2967 need_lookup, opened);
2970 if (need_lookup) {
2971 BUG_ON(dentry->d_inode);
2973 dentry = lookup_real(dir_inode, dentry, nd->flags);
2974 if (IS_ERR(dentry))
2975 return PTR_ERR(dentry);
2978 /* Negative dentry, just create the file */
2979 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2980 umode_t mode = op->mode;
2981 if (!IS_POSIXACL(dir->d_inode))
2982 mode &= ~current_umask();
2984 * This write is needed to ensure that a
2985 * rw->ro transition does not occur between
2986 * the time when the file is created and when
2987 * a permanent write count is taken through
2988 * the 'struct file' in finish_open().
2990 if (!got_write) {
2991 error = -EROFS;
2992 goto out_dput;
2994 *opened |= FILE_CREATED;
2995 error = security_path_mknod(&nd->path, dentry, mode, 0);
2996 if (error)
2997 goto out_dput;
2998 error = vfs_create(dir->d_inode, dentry, mode,
2999 nd->flags & LOOKUP_EXCL);
3000 if (error)
3001 goto out_dput;
3003 out_no_open:
3004 path->dentry = dentry;
3005 path->mnt = nd->path.mnt;
3006 return 1;
3008 out_dput:
3009 dput(dentry);
3010 return error;
3014 * Handle the last step of open()
3016 static int do_last(struct nameidata *nd,
3017 struct file *file, const struct open_flags *op,
3018 int *opened)
3020 struct dentry *dir = nd->path.dentry;
3021 int open_flag = op->open_flag;
3022 bool will_truncate = (open_flag & O_TRUNC) != 0;
3023 bool got_write = false;
3024 int acc_mode = op->acc_mode;
3025 unsigned seq;
3026 struct inode *inode;
3027 struct path save_parent = { .dentry = NULL, .mnt = NULL };
3028 struct path path;
3029 bool retried = false;
3030 int error;
3032 nd->flags &= ~LOOKUP_PARENT;
3033 nd->flags |= op->intent;
3035 if (nd->last_type != LAST_NORM) {
3036 error = handle_dots(nd, nd->last_type);
3037 if (unlikely(error))
3038 return error;
3039 goto finish_open;
3042 if (!(open_flag & O_CREAT)) {
3043 if (nd->last.name[nd->last.len])
3044 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3045 /* we _can_ be in RCU mode here */
3046 error = lookup_fast(nd, &path, &inode, &seq);
3047 if (likely(!error))
3048 goto finish_lookup;
3050 if (error < 0)
3051 return error;
3053 BUG_ON(nd->inode != dir->d_inode);
3054 } else {
3055 /* create side of things */
3057 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3058 * has been cleared when we got to the last component we are
3059 * about to look up
3061 error = complete_walk(nd);
3062 if (error)
3063 return error;
3065 audit_inode(nd->name, dir, LOOKUP_PARENT);
3066 /* trailing slashes? */
3067 if (unlikely(nd->last.name[nd->last.len]))
3068 return -EISDIR;
3071 retry_lookup:
3072 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
3073 error = mnt_want_write(nd->path.mnt);
3074 if (!error)
3075 got_write = true;
3077 * do _not_ fail yet - we might not need that or fail with
3078 * a different error; let lookup_open() decide; we'll be
3079 * dropping this one anyway.
3082 mutex_lock(&dir->d_inode->i_mutex);
3083 error = lookup_open(nd, &path, file, op, got_write, opened);
3084 mutex_unlock(&dir->d_inode->i_mutex);
3086 if (error <= 0) {
3087 if (error)
3088 goto out;
3090 if ((*opened & FILE_CREATED) ||
3091 !S_ISREG(file_inode(file)->i_mode))
3092 will_truncate = false;
3094 audit_inode(nd->name, file->f_path.dentry, 0);
3095 goto opened;
3098 if (*opened & FILE_CREATED) {
3099 /* Don't check for write permission, don't truncate */
3100 open_flag &= ~O_TRUNC;
3101 will_truncate = false;
3102 acc_mode = MAY_OPEN;
3103 path_to_nameidata(&path, nd);
3104 goto finish_open_created;
3108 * create/update audit record if it already exists.
3110 if (d_is_positive(path.dentry))
3111 audit_inode(nd->name, path.dentry, 0);
3114 * If atomic_open() acquired write access it is dropped now due to
3115 * possible mount and symlink following (this might be optimized away if
3116 * necessary...)
3118 if (got_write) {
3119 mnt_drop_write(nd->path.mnt);
3120 got_write = false;
3123 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3124 path_to_nameidata(&path, nd);
3125 return -EEXIST;
3128 error = follow_managed(&path, nd);
3129 if (unlikely(error < 0))
3130 return error;
3132 BUG_ON(nd->flags & LOOKUP_RCU);
3133 seq = 0; /* out of RCU mode, so the value doesn't matter */
3134 if (unlikely(d_is_negative(path.dentry))) {
3135 path_to_nameidata(&path, nd);
3136 return -ENOENT;
3138 inode = d_backing_inode(path.dentry);
3139 finish_lookup:
3140 if (nd->depth)
3141 put_link(nd);
3142 error = should_follow_link(nd, &path, nd->flags & LOOKUP_FOLLOW,
3143 inode, seq);
3144 if (unlikely(error))
3145 return error;
3147 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path.mnt) {
3148 path_to_nameidata(&path, nd);
3149 } else {
3150 save_parent.dentry = nd->path.dentry;
3151 save_parent.mnt = mntget(path.mnt);
3152 nd->path.dentry = path.dentry;
3155 nd->inode = inode;
3156 nd->seq = seq;
3157 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
3158 finish_open:
3159 error = complete_walk(nd);
3160 if (error) {
3161 path_put(&save_parent);
3162 return error;
3164 audit_inode(nd->name, nd->path.dentry, 0);
3165 if (unlikely(d_is_symlink(nd->path.dentry)) && !(open_flag & O_PATH)) {
3166 error = -ELOOP;
3167 goto out;
3169 error = -EISDIR;
3170 if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
3171 goto out;
3172 error = -ENOTDIR;
3173 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
3174 goto out;
3175 if (!d_is_reg(nd->path.dentry))
3176 will_truncate = false;
3178 if (will_truncate) {
3179 error = mnt_want_write(nd->path.mnt);
3180 if (error)
3181 goto out;
3182 got_write = true;
3184 finish_open_created:
3185 error = may_open(&nd->path, acc_mode, open_flag);
3186 if (error)
3187 goto out;
3189 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3190 error = vfs_open(&nd->path, file, current_cred());
3191 if (!error) {
3192 *opened |= FILE_OPENED;
3193 } else {
3194 if (error == -EOPENSTALE)
3195 goto stale_open;
3196 goto out;
3198 opened:
3199 error = open_check_o_direct(file);
3200 if (error)
3201 goto exit_fput;
3202 error = ima_file_check(file, op->acc_mode, *opened);
3203 if (error)
3204 goto exit_fput;
3206 if (will_truncate) {
3207 error = handle_truncate(file);
3208 if (error)
3209 goto exit_fput;
3211 out:
3212 if (unlikely(error > 0)) {
3213 WARN_ON(1);
3214 error = -EINVAL;
3216 if (got_write)
3217 mnt_drop_write(nd->path.mnt);
3218 path_put(&save_parent);
3219 return error;
3221 exit_fput:
3222 fput(file);
3223 goto out;
3225 stale_open:
3226 /* If no saved parent or already retried then can't retry */
3227 if (!save_parent.dentry || retried)
3228 goto out;
3230 BUG_ON(save_parent.dentry != dir);
3231 path_put(&nd->path);
3232 nd->path = save_parent;
3233 nd->inode = dir->d_inode;
3234 save_parent.mnt = NULL;
3235 save_parent.dentry = NULL;
3236 if (got_write) {
3237 mnt_drop_write(nd->path.mnt);
3238 got_write = false;
3240 retried = true;
3241 goto retry_lookup;
3244 static int do_tmpfile(struct nameidata *nd, unsigned flags,
3245 const struct open_flags *op,
3246 struct file *file, int *opened)
3248 static const struct qstr name = QSTR_INIT("/", 1);
3249 struct dentry *child;
3250 struct inode *dir;
3251 struct path path;
3252 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
3253 if (unlikely(error))
3254 return error;
3255 error = mnt_want_write(path.mnt);
3256 if (unlikely(error))
3257 goto out;
3258 dir = path.dentry->d_inode;
3259 /* we want directory to be writable */
3260 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
3261 if (error)
3262 goto out2;
3263 if (!dir->i_op->tmpfile) {
3264 error = -EOPNOTSUPP;
3265 goto out2;
3267 child = d_alloc(path.dentry, &name);
3268 if (unlikely(!child)) {
3269 error = -ENOMEM;
3270 goto out2;
3272 dput(path.dentry);
3273 path.dentry = child;
3274 error = dir->i_op->tmpfile(dir, child, op->mode);
3275 if (error)
3276 goto out2;
3277 audit_inode(nd->name, child, 0);
3278 /* Don't check for other permissions, the inode was just created */
3279 error = may_open(&path, MAY_OPEN, op->open_flag);
3280 if (error)
3281 goto out2;
3282 file->f_path.mnt = path.mnt;
3283 error = finish_open(file, child, NULL, opened);
3284 if (error)
3285 goto out2;
3286 error = open_check_o_direct(file);
3287 if (error) {
3288 fput(file);
3289 } else if (!(op->open_flag & O_EXCL)) {
3290 struct inode *inode = file_inode(file);
3291 spin_lock(&inode->i_lock);
3292 inode->i_state |= I_LINKABLE;
3293 spin_unlock(&inode->i_lock);
3295 out2:
3296 mnt_drop_write(path.mnt);
3297 out:
3298 path_put(&path);
3299 return error;
3302 static struct file *path_openat(struct nameidata *nd,
3303 const struct open_flags *op, unsigned flags)
3305 const char *s;
3306 struct file *file;
3307 int opened = 0;
3308 int error;
3310 file = get_empty_filp();
3311 if (IS_ERR(file))
3312 return file;
3314 file->f_flags = op->open_flag;
3316 if (unlikely(file->f_flags & __O_TMPFILE)) {
3317 error = do_tmpfile(nd, flags, op, file, &opened);
3318 goto out2;
3321 s = path_init(nd, flags);
3322 if (IS_ERR(s)) {
3323 put_filp(file);
3324 return ERR_CAST(s);
3326 while (!(error = link_path_walk(s, nd)) &&
3327 (error = do_last(nd, file, op, &opened)) > 0) {
3328 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3329 s = trailing_symlink(nd);
3330 if (IS_ERR(s)) {
3331 error = PTR_ERR(s);
3332 break;
3335 terminate_walk(nd);
3336 out2:
3337 if (!(opened & FILE_OPENED)) {
3338 BUG_ON(!error);
3339 put_filp(file);
3341 if (unlikely(error)) {
3342 if (error == -EOPENSTALE) {
3343 if (flags & LOOKUP_RCU)
3344 error = -ECHILD;
3345 else
3346 error = -ESTALE;
3348 file = ERR_PTR(error);
3350 return file;
3353 struct file *do_filp_open(int dfd, struct filename *pathname,
3354 const struct open_flags *op)
3356 struct nameidata nd;
3357 int flags = op->lookup_flags;
3358 struct file *filp;
3360 set_nameidata(&nd, dfd, pathname);
3361 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
3362 if (unlikely(filp == ERR_PTR(-ECHILD)))
3363 filp = path_openat(&nd, op, flags);
3364 if (unlikely(filp == ERR_PTR(-ESTALE)))
3365 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
3366 restore_nameidata();
3367 return filp;
3370 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3371 const char *name, const struct open_flags *op)
3373 struct nameidata nd;
3374 struct file *file;
3375 struct filename *filename;
3376 int flags = op->lookup_flags | LOOKUP_ROOT;
3378 nd.root.mnt = mnt;
3379 nd.root.dentry = dentry;
3381 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
3382 return ERR_PTR(-ELOOP);
3384 filename = getname_kernel(name);
3385 if (IS_ERR(filename))
3386 return ERR_CAST(filename);
3388 set_nameidata(&nd, -1, filename);
3389 file = path_openat(&nd, op, flags | LOOKUP_RCU);
3390 if (unlikely(file == ERR_PTR(-ECHILD)))
3391 file = path_openat(&nd, op, flags);
3392 if (unlikely(file == ERR_PTR(-ESTALE)))
3393 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
3394 restore_nameidata();
3395 putname(filename);
3396 return file;
3399 static struct dentry *filename_create(int dfd, struct filename *name,
3400 struct path *path, unsigned int lookup_flags)
3402 struct dentry *dentry = ERR_PTR(-EEXIST);
3403 struct qstr last;
3404 int type;
3405 int err2;
3406 int error;
3407 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3410 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3411 * other flags passed in are ignored!
3413 lookup_flags &= LOOKUP_REVAL;
3415 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3416 if (IS_ERR(name))
3417 return ERR_CAST(name);
3420 * Yucky last component or no last component at all?
3421 * (foo/., foo/.., /////)
3423 if (unlikely(type != LAST_NORM))
3424 goto out;
3426 /* don't fail immediately if it's r/o, at least try to report other errors */
3427 err2 = mnt_want_write(path->mnt);
3429 * Do the final lookup.
3431 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3432 mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3433 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
3434 if (IS_ERR(dentry))
3435 goto unlock;
3437 error = -EEXIST;
3438 if (d_is_positive(dentry))
3439 goto fail;
3442 * Special case - lookup gave negative, but... we had foo/bar/
3443 * From the vfs_mknod() POV we just have a negative dentry -
3444 * all is fine. Let's be bastards - you had / on the end, you've
3445 * been asking for (non-existent) directory. -ENOENT for you.
3447 if (unlikely(!is_dir && last.name[last.len])) {
3448 error = -ENOENT;
3449 goto fail;
3451 if (unlikely(err2)) {
3452 error = err2;
3453 goto fail;
3455 putname(name);
3456 return dentry;
3457 fail:
3458 dput(dentry);
3459 dentry = ERR_PTR(error);
3460 unlock:
3461 mutex_unlock(&path->dentry->d_inode->i_mutex);
3462 if (!err2)
3463 mnt_drop_write(path->mnt);
3464 out:
3465 path_put(path);
3466 putname(name);
3467 return dentry;
3470 struct dentry *kern_path_create(int dfd, const char *pathname,
3471 struct path *path, unsigned int lookup_flags)
3473 return filename_create(dfd, getname_kernel(pathname),
3474 path, lookup_flags);
3476 EXPORT_SYMBOL(kern_path_create);
3478 void done_path_create(struct path *path, struct dentry *dentry)
3480 dput(dentry);
3481 mutex_unlock(&path->dentry->d_inode->i_mutex);
3482 mnt_drop_write(path->mnt);
3483 path_put(path);
3485 EXPORT_SYMBOL(done_path_create);
3487 inline struct dentry *user_path_create(int dfd, const char __user *pathname,
3488 struct path *path, unsigned int lookup_flags)
3490 return filename_create(dfd, getname(pathname), path, lookup_flags);
3492 EXPORT_SYMBOL(user_path_create);
3494 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3496 int error = may_create(dir, dentry);
3498 if (error)
3499 return error;
3501 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
3502 return -EPERM;
3504 if (!dir->i_op->mknod)
3505 return -EPERM;
3507 error = devcgroup_inode_mknod(mode, dev);
3508 if (error)
3509 return error;
3511 error = security_inode_mknod(dir, dentry, mode, dev);
3512 if (error)
3513 return error;
3515 error = dir->i_op->mknod(dir, dentry, mode, dev);
3516 if (!error)
3517 fsnotify_create(dir, dentry);
3518 return error;
3520 EXPORT_SYMBOL(vfs_mknod);
3522 static int may_mknod(umode_t mode)
3524 switch (mode & S_IFMT) {
3525 case S_IFREG:
3526 case S_IFCHR:
3527 case S_IFBLK:
3528 case S_IFIFO:
3529 case S_IFSOCK:
3530 case 0: /* zero mode translates to S_IFREG */
3531 return 0;
3532 case S_IFDIR:
3533 return -EPERM;
3534 default:
3535 return -EINVAL;
3539 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3540 unsigned, dev)
3542 struct dentry *dentry;
3543 struct path path;
3544 int error;
3545 unsigned int lookup_flags = 0;
3547 error = may_mknod(mode);
3548 if (error)
3549 return error;
3550 retry:
3551 dentry = user_path_create(dfd, filename, &path, lookup_flags);
3552 if (IS_ERR(dentry))
3553 return PTR_ERR(dentry);
3555 if (!IS_POSIXACL(path.dentry->d_inode))
3556 mode &= ~current_umask();
3557 error = security_path_mknod(&path, dentry, mode, dev);
3558 if (error)
3559 goto out;
3560 switch (mode & S_IFMT) {
3561 case 0: case S_IFREG:
3562 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
3563 break;
3564 case S_IFCHR: case S_IFBLK:
3565 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
3566 new_decode_dev(dev));
3567 break;
3568 case S_IFIFO: case S_IFSOCK:
3569 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
3570 break;
3572 out:
3573 done_path_create(&path, dentry);
3574 if (retry_estale(error, lookup_flags)) {
3575 lookup_flags |= LOOKUP_REVAL;
3576 goto retry;
3578 return error;
3581 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
3583 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3586 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3588 int error = may_create(dir, dentry);
3589 unsigned max_links = dir->i_sb->s_max_links;
3591 if (error)
3592 return error;
3594 if (!dir->i_op->mkdir)
3595 return -EPERM;
3597 mode &= (S_IRWXUGO|S_ISVTX);
3598 error = security_inode_mkdir(dir, dentry, mode);
3599 if (error)
3600 return error;
3602 if (max_links && dir->i_nlink >= max_links)
3603 return -EMLINK;
3605 error = dir->i_op->mkdir(dir, dentry, mode);
3606 if (!error)
3607 fsnotify_mkdir(dir, dentry);
3608 return error;
3610 EXPORT_SYMBOL(vfs_mkdir);
3612 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3614 struct dentry *dentry;
3615 struct path path;
3616 int error;
3617 unsigned int lookup_flags = LOOKUP_DIRECTORY;
3619 retry:
3620 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
3621 if (IS_ERR(dentry))
3622 return PTR_ERR(dentry);
3624 if (!IS_POSIXACL(path.dentry->d_inode))
3625 mode &= ~current_umask();
3626 error = security_path_mkdir(&path, dentry, mode);
3627 if (!error)
3628 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3629 done_path_create(&path, dentry);
3630 if (retry_estale(error, lookup_flags)) {
3631 lookup_flags |= LOOKUP_REVAL;
3632 goto retry;
3634 return error;
3637 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
3639 return sys_mkdirat(AT_FDCWD, pathname, mode);
3643 * The dentry_unhash() helper will try to drop the dentry early: we
3644 * should have a usage count of 1 if we're the only user of this
3645 * dentry, and if that is true (possibly after pruning the dcache),
3646 * then we drop the dentry now.
3648 * A low-level filesystem can, if it choses, legally
3649 * do a
3651 * if (!d_unhashed(dentry))
3652 * return -EBUSY;
3654 * if it cannot handle the case of removing a directory
3655 * that is still in use by something else..
3657 void dentry_unhash(struct dentry *dentry)
3659 shrink_dcache_parent(dentry);
3660 spin_lock(&dentry->d_lock);
3661 if (dentry->d_lockref.count == 1)
3662 __d_drop(dentry);
3663 spin_unlock(&dentry->d_lock);
3665 EXPORT_SYMBOL(dentry_unhash);
3667 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3669 int error = may_delete(dir, dentry, 1);
3671 if (error)
3672 return error;
3674 if (!dir->i_op->rmdir)
3675 return -EPERM;
3677 dget(dentry);
3678 mutex_lock(&dentry->d_inode->i_mutex);
3680 error = -EBUSY;
3681 if (is_local_mountpoint(dentry))
3682 goto out;
3684 error = security_inode_rmdir(dir, dentry);
3685 if (error)
3686 goto out;
3688 shrink_dcache_parent(dentry);
3689 error = dir->i_op->rmdir(dir, dentry);
3690 if (error)
3691 goto out;
3693 dentry->d_inode->i_flags |= S_DEAD;
3694 dont_mount(dentry);
3695 detach_mounts(dentry);
3697 out:
3698 mutex_unlock(&dentry->d_inode->i_mutex);
3699 dput(dentry);
3700 if (!error)
3701 d_delete(dentry);
3702 return error;
3704 EXPORT_SYMBOL(vfs_rmdir);
3706 static long do_rmdir(int dfd, const char __user *pathname)
3708 int error = 0;
3709 struct filename *name;
3710 struct dentry *dentry;
3711 struct path path;
3712 struct qstr last;
3713 int type;
3714 unsigned int lookup_flags = 0;
3715 retry:
3716 name = user_path_parent(dfd, pathname,
3717 &path, &last, &type, lookup_flags);
3718 if (IS_ERR(name))
3719 return PTR_ERR(name);
3721 switch (type) {
3722 case LAST_DOTDOT:
3723 error = -ENOTEMPTY;
3724 goto exit1;
3725 case LAST_DOT:
3726 error = -EINVAL;
3727 goto exit1;
3728 case LAST_ROOT:
3729 error = -EBUSY;
3730 goto exit1;
3733 error = mnt_want_write(path.mnt);
3734 if (error)
3735 goto exit1;
3737 mutex_lock_nested(&path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3738 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
3739 error = PTR_ERR(dentry);
3740 if (IS_ERR(dentry))
3741 goto exit2;
3742 if (!dentry->d_inode) {
3743 error = -ENOENT;
3744 goto exit3;
3746 error = security_path_rmdir(&path, dentry);
3747 if (error)
3748 goto exit3;
3749 error = vfs_rmdir(path.dentry->d_inode, dentry);
3750 exit3:
3751 dput(dentry);
3752 exit2:
3753 mutex_unlock(&path.dentry->d_inode->i_mutex);
3754 mnt_drop_write(path.mnt);
3755 exit1:
3756 path_put(&path);
3757 putname(name);
3758 if (retry_estale(error, lookup_flags)) {
3759 lookup_flags |= LOOKUP_REVAL;
3760 goto retry;
3762 return error;
3765 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
3767 return do_rmdir(AT_FDCWD, pathname);
3771 * vfs_unlink - unlink a filesystem object
3772 * @dir: parent directory
3773 * @dentry: victim
3774 * @delegated_inode: returns victim inode, if the inode is delegated.
3776 * The caller must hold dir->i_mutex.
3778 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3779 * return a reference to the inode in delegated_inode. The caller
3780 * should then break the delegation on that inode and retry. Because
3781 * breaking a delegation may take a long time, the caller should drop
3782 * dir->i_mutex before doing so.
3784 * Alternatively, a caller may pass NULL for delegated_inode. This may
3785 * be appropriate for callers that expect the underlying filesystem not
3786 * to be NFS exported.
3788 int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
3790 struct inode *target = dentry->d_inode;
3791 int error = may_delete(dir, dentry, 0);
3793 if (error)
3794 return error;
3796 if (!dir->i_op->unlink)
3797 return -EPERM;
3799 mutex_lock(&target->i_mutex);
3800 if (is_local_mountpoint(dentry))
3801 error = -EBUSY;
3802 else {
3803 error = security_inode_unlink(dir, dentry);
3804 if (!error) {
3805 error = try_break_deleg(target, delegated_inode);
3806 if (error)
3807 goto out;
3808 error = dir->i_op->unlink(dir, dentry);
3809 if (!error) {
3810 dont_mount(dentry);
3811 detach_mounts(dentry);
3815 out:
3816 mutex_unlock(&target->i_mutex);
3818 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3819 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3820 fsnotify_link_count(target);
3821 d_delete(dentry);
3824 return error;
3826 EXPORT_SYMBOL(vfs_unlink);
3829 * Make sure that the actual truncation of the file will occur outside its
3830 * directory's i_mutex. Truncate can take a long time if there is a lot of
3831 * writeout happening, and we don't want to prevent access to the directory
3832 * while waiting on the I/O.
3834 static long do_unlinkat(int dfd, const char __user *pathname)
3836 int error;
3837 struct filename *name;
3838 struct dentry *dentry;
3839 struct path path;
3840 struct qstr last;
3841 int type;
3842 struct inode *inode = NULL;
3843 struct inode *delegated_inode = NULL;
3844 unsigned int lookup_flags = 0;
3845 retry:
3846 name = user_path_parent(dfd, pathname,
3847 &path, &last, &type, lookup_flags);
3848 if (IS_ERR(name))
3849 return PTR_ERR(name);
3851 error = -EISDIR;
3852 if (type != LAST_NORM)
3853 goto exit1;
3855 error = mnt_want_write(path.mnt);
3856 if (error)
3857 goto exit1;
3858 retry_deleg:
3859 mutex_lock_nested(&path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3860 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
3861 error = PTR_ERR(dentry);
3862 if (!IS_ERR(dentry)) {
3863 /* Why not before? Because we want correct error value */
3864 if (last.name[last.len])
3865 goto slashes;
3866 inode = dentry->d_inode;
3867 if (d_is_negative(dentry))
3868 goto slashes;
3869 ihold(inode);
3870 error = security_path_unlink(&path, dentry);
3871 if (error)
3872 goto exit2;
3873 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
3874 exit2:
3875 dput(dentry);
3877 mutex_unlock(&path.dentry->d_inode->i_mutex);
3878 if (inode)
3879 iput(inode); /* truncate the inode here */
3880 inode = NULL;
3881 if (delegated_inode) {
3882 error = break_deleg_wait(&delegated_inode);
3883 if (!error)
3884 goto retry_deleg;
3886 mnt_drop_write(path.mnt);
3887 exit1:
3888 path_put(&path);
3889 putname(name);
3890 if (retry_estale(error, lookup_flags)) {
3891 lookup_flags |= LOOKUP_REVAL;
3892 inode = NULL;
3893 goto retry;
3895 return error;
3897 slashes:
3898 if (d_is_negative(dentry))
3899 error = -ENOENT;
3900 else if (d_is_dir(dentry))
3901 error = -EISDIR;
3902 else
3903 error = -ENOTDIR;
3904 goto exit2;
3907 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
3909 if ((flag & ~AT_REMOVEDIR) != 0)
3910 return -EINVAL;
3912 if (flag & AT_REMOVEDIR)
3913 return do_rmdir(dfd, pathname);
3915 return do_unlinkat(dfd, pathname);
3918 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
3920 return do_unlinkat(AT_FDCWD, pathname);
3923 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
3925 int error = may_create(dir, dentry);
3927 if (error)
3928 return error;
3930 if (!dir->i_op->symlink)
3931 return -EPERM;
3933 error = security_inode_symlink(dir, dentry, oldname);
3934 if (error)
3935 return error;
3937 error = dir->i_op->symlink(dir, dentry, oldname);
3938 if (!error)
3939 fsnotify_create(dir, dentry);
3940 return error;
3942 EXPORT_SYMBOL(vfs_symlink);
3944 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3945 int, newdfd, const char __user *, newname)
3947 int error;
3948 struct filename *from;
3949 struct dentry *dentry;
3950 struct path path;
3951 unsigned int lookup_flags = 0;
3953 from = getname(oldname);
3954 if (IS_ERR(from))
3955 return PTR_ERR(from);
3956 retry:
3957 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
3958 error = PTR_ERR(dentry);
3959 if (IS_ERR(dentry))
3960 goto out_putname;
3962 error = security_path_symlink(&path, dentry, from->name);
3963 if (!error)
3964 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
3965 done_path_create(&path, dentry);
3966 if (retry_estale(error, lookup_flags)) {
3967 lookup_flags |= LOOKUP_REVAL;
3968 goto retry;
3970 out_putname:
3971 putname(from);
3972 return error;
3975 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
3977 return sys_symlinkat(oldname, AT_FDCWD, newname);
3981 * vfs_link - create a new link
3982 * @old_dentry: object to be linked
3983 * @dir: new parent
3984 * @new_dentry: where to create the new link
3985 * @delegated_inode: returns inode needing a delegation break
3987 * The caller must hold dir->i_mutex
3989 * If vfs_link discovers a delegation on the to-be-linked file in need
3990 * of breaking, it will return -EWOULDBLOCK and return a reference to the
3991 * inode in delegated_inode. The caller should then break the delegation
3992 * and retry. Because breaking a delegation may take a long time, the
3993 * caller should drop the i_mutex before doing so.
3995 * Alternatively, a caller may pass NULL for delegated_inode. This may
3996 * be appropriate for callers that expect the underlying filesystem not
3997 * to be NFS exported.
3999 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
4001 struct inode *inode = old_dentry->d_inode;
4002 unsigned max_links = dir->i_sb->s_max_links;
4003 int error;
4005 if (!inode)
4006 return -ENOENT;
4008 error = may_create(dir, new_dentry);
4009 if (error)
4010 return error;
4012 if (dir->i_sb != inode->i_sb)
4013 return -EXDEV;
4016 * A link to an append-only or immutable file cannot be created.
4018 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4019 return -EPERM;
4020 if (!dir->i_op->link)
4021 return -EPERM;
4022 if (S_ISDIR(inode->i_mode))
4023 return -EPERM;
4025 error = security_inode_link(old_dentry, dir, new_dentry);
4026 if (error)
4027 return error;
4029 mutex_lock(&inode->i_mutex);
4030 /* Make sure we don't allow creating hardlink to an unlinked file */
4031 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
4032 error = -ENOENT;
4033 else if (max_links && inode->i_nlink >= max_links)
4034 error = -EMLINK;
4035 else {
4036 error = try_break_deleg(inode, delegated_inode);
4037 if (!error)
4038 error = dir->i_op->link(old_dentry, dir, new_dentry);
4041 if (!error && (inode->i_state & I_LINKABLE)) {
4042 spin_lock(&inode->i_lock);
4043 inode->i_state &= ~I_LINKABLE;
4044 spin_unlock(&inode->i_lock);
4046 mutex_unlock(&inode->i_mutex);
4047 if (!error)
4048 fsnotify_link(dir, inode, new_dentry);
4049 return error;
4051 EXPORT_SYMBOL(vfs_link);
4054 * Hardlinks are often used in delicate situations. We avoid
4055 * security-related surprises by not following symlinks on the
4056 * newname. --KAB
4058 * We don't follow them on the oldname either to be compatible
4059 * with linux 2.0, and to avoid hard-linking to directories
4060 * and other special files. --ADM
4062 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4063 int, newdfd, const char __user *, newname, int, flags)
4065 struct dentry *new_dentry;
4066 struct path old_path, new_path;
4067 struct inode *delegated_inode = NULL;
4068 int how = 0;
4069 int error;
4071 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
4072 return -EINVAL;
4074 * To use null names we require CAP_DAC_READ_SEARCH
4075 * This ensures that not everyone will be able to create
4076 * handlink using the passed filedescriptor.
4078 if (flags & AT_EMPTY_PATH) {
4079 if (!capable(CAP_DAC_READ_SEARCH))
4080 return -ENOENT;
4081 how = LOOKUP_EMPTY;
4084 if (flags & AT_SYMLINK_FOLLOW)
4085 how |= LOOKUP_FOLLOW;
4086 retry:
4087 error = user_path_at(olddfd, oldname, how, &old_path);
4088 if (error)
4089 return error;
4091 new_dentry = user_path_create(newdfd, newname, &new_path,
4092 (how & LOOKUP_REVAL));
4093 error = PTR_ERR(new_dentry);
4094 if (IS_ERR(new_dentry))
4095 goto out;
4097 error = -EXDEV;
4098 if (old_path.mnt != new_path.mnt)
4099 goto out_dput;
4100 error = may_linkat(&old_path);
4101 if (unlikely(error))
4102 goto out_dput;
4103 error = security_path_link(old_path.dentry, &new_path, new_dentry);
4104 if (error)
4105 goto out_dput;
4106 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
4107 out_dput:
4108 done_path_create(&new_path, new_dentry);
4109 if (delegated_inode) {
4110 error = break_deleg_wait(&delegated_inode);
4111 if (!error) {
4112 path_put(&old_path);
4113 goto retry;
4116 if (retry_estale(error, how)) {
4117 path_put(&old_path);
4118 how |= LOOKUP_REVAL;
4119 goto retry;
4121 out:
4122 path_put(&old_path);
4124 return error;
4127 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
4129 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
4133 * vfs_rename - rename a filesystem object
4134 * @old_dir: parent of source
4135 * @old_dentry: source
4136 * @new_dir: parent of destination
4137 * @new_dentry: destination
4138 * @delegated_inode: returns an inode needing a delegation break
4139 * @flags: rename flags
4141 * The caller must hold multiple mutexes--see lock_rename()).
4143 * If vfs_rename discovers a delegation in need of breaking at either
4144 * the source or destination, it will return -EWOULDBLOCK and return a
4145 * reference to the inode in delegated_inode. The caller should then
4146 * break the delegation and retry. Because breaking a delegation may
4147 * take a long time, the caller should drop all locks before doing
4148 * so.
4150 * Alternatively, a caller may pass NULL for delegated_inode. This may
4151 * be appropriate for callers that expect the underlying filesystem not
4152 * to be NFS exported.
4154 * The worst of all namespace operations - renaming directory. "Perverted"
4155 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4156 * Problems:
4157 * a) we can get into loop creation.
4158 * b) race potential - two innocent renames can create a loop together.
4159 * That's where 4.4 screws up. Current fix: serialization on
4160 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
4161 * story.
4162 * c) we have to lock _four_ objects - parents and victim (if it exists),
4163 * and source (if it is not a directory).
4164 * And that - after we got ->i_mutex on parents (until then we don't know
4165 * whether the target exists). Solution: try to be smart with locking
4166 * order for inodes. We rely on the fact that tree topology may change
4167 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
4168 * move will be locked. Thus we can rank directories by the tree
4169 * (ancestors first) and rank all non-directories after them.
4170 * That works since everybody except rename does "lock parent, lookup,
4171 * lock child" and rename is under ->s_vfs_rename_mutex.
4172 * HOWEVER, it relies on the assumption that any object with ->lookup()
4173 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4174 * we'd better make sure that there's no link(2) for them.
4175 * d) conversion from fhandle to dentry may come in the wrong moment - when
4176 * we are removing the target. Solution: we will have to grab ->i_mutex
4177 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4178 * ->i_mutex on parents, which works but leads to some truly excessive
4179 * locking].
4181 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4182 struct inode *new_dir, struct dentry *new_dentry,
4183 struct inode **delegated_inode, unsigned int flags)
4185 int error;
4186 bool is_dir = d_is_dir(old_dentry);
4187 struct inode *source = old_dentry->d_inode;
4188 struct inode *target = new_dentry->d_inode;
4189 bool new_is_dir = false;
4190 unsigned max_links = new_dir->i_sb->s_max_links;
4191 struct name_snapshot old_name;
4194 * Check source == target.
4195 * On overlayfs need to look at underlying inodes.
4197 if (vfs_select_inode(old_dentry, 0) == vfs_select_inode(new_dentry, 0))
4198 return 0;
4200 error = may_delete(old_dir, old_dentry, is_dir);
4201 if (error)
4202 return error;
4204 if (!target) {
4205 error = may_create(new_dir, new_dentry);
4206 } else {
4207 new_is_dir = d_is_dir(new_dentry);
4209 if (!(flags & RENAME_EXCHANGE))
4210 error = may_delete(new_dir, new_dentry, is_dir);
4211 else
4212 error = may_delete(new_dir, new_dentry, new_is_dir);
4214 if (error)
4215 return error;
4217 if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
4218 return -EPERM;
4220 if (flags && !old_dir->i_op->rename2)
4221 return -EINVAL;
4224 * If we are going to change the parent - check write permissions,
4225 * we'll need to flip '..'.
4227 if (new_dir != old_dir) {
4228 if (is_dir) {
4229 error = inode_permission(source, MAY_WRITE);
4230 if (error)
4231 return error;
4233 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4234 error = inode_permission(target, MAY_WRITE);
4235 if (error)
4236 return error;
4240 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4241 flags);
4242 if (error)
4243 return error;
4245 take_dentry_name_snapshot(&old_name, old_dentry);
4246 dget(new_dentry);
4247 if (!is_dir || (flags & RENAME_EXCHANGE))
4248 lock_two_nondirectories(source, target);
4249 else if (target)
4250 mutex_lock(&target->i_mutex);
4252 error = -EBUSY;
4253 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
4254 goto out;
4256 if (max_links && new_dir != old_dir) {
4257 error = -EMLINK;
4258 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
4259 goto out;
4260 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4261 old_dir->i_nlink >= max_links)
4262 goto out;
4264 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4265 shrink_dcache_parent(new_dentry);
4266 if (!is_dir) {
4267 error = try_break_deleg(source, delegated_inode);
4268 if (error)
4269 goto out;
4271 if (target && !new_is_dir) {
4272 error = try_break_deleg(target, delegated_inode);
4273 if (error)
4274 goto out;
4276 if (!old_dir->i_op->rename2) {
4277 error = old_dir->i_op->rename(old_dir, old_dentry,
4278 new_dir, new_dentry);
4279 } else {
4280 WARN_ON(old_dir->i_op->rename != NULL);
4281 error = old_dir->i_op->rename2(old_dir, old_dentry,
4282 new_dir, new_dentry, flags);
4284 if (error)
4285 goto out;
4287 if (!(flags & RENAME_EXCHANGE) && target) {
4288 if (is_dir)
4289 target->i_flags |= S_DEAD;
4290 dont_mount(new_dentry);
4291 detach_mounts(new_dentry);
4293 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4294 if (!(flags & RENAME_EXCHANGE))
4295 d_move(old_dentry, new_dentry);
4296 else
4297 d_exchange(old_dentry, new_dentry);
4299 out:
4300 if (!is_dir || (flags & RENAME_EXCHANGE))
4301 unlock_two_nondirectories(source, target);
4302 else if (target)
4303 mutex_unlock(&target->i_mutex);
4304 dput(new_dentry);
4305 if (!error) {
4306 fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
4307 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4308 if (flags & RENAME_EXCHANGE) {
4309 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4310 new_is_dir, NULL, new_dentry);
4313 release_dentry_name_snapshot(&old_name);
4315 return error;
4317 EXPORT_SYMBOL(vfs_rename);
4319 SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4320 int, newdfd, const char __user *, newname, unsigned int, flags)
4322 struct dentry *old_dentry, *new_dentry;
4323 struct dentry *trap;
4324 struct path old_path, new_path;
4325 struct qstr old_last, new_last;
4326 int old_type, new_type;
4327 struct inode *delegated_inode = NULL;
4328 struct filename *from;
4329 struct filename *to;
4330 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
4331 bool should_retry = false;
4332 int error;
4334 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
4335 return -EINVAL;
4337 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4338 (flags & RENAME_EXCHANGE))
4339 return -EINVAL;
4341 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4342 return -EPERM;
4344 if (flags & RENAME_EXCHANGE)
4345 target_flags = 0;
4347 retry:
4348 from = user_path_parent(olddfd, oldname,
4349 &old_path, &old_last, &old_type, lookup_flags);
4350 if (IS_ERR(from)) {
4351 error = PTR_ERR(from);
4352 goto exit;
4355 to = user_path_parent(newdfd, newname,
4356 &new_path, &new_last, &new_type, lookup_flags);
4357 if (IS_ERR(to)) {
4358 error = PTR_ERR(to);
4359 goto exit1;
4362 error = -EXDEV;
4363 if (old_path.mnt != new_path.mnt)
4364 goto exit2;
4366 error = -EBUSY;
4367 if (old_type != LAST_NORM)
4368 goto exit2;
4370 if (flags & RENAME_NOREPLACE)
4371 error = -EEXIST;
4372 if (new_type != LAST_NORM)
4373 goto exit2;
4375 error = mnt_want_write(old_path.mnt);
4376 if (error)
4377 goto exit2;
4379 retry_deleg:
4380 trap = lock_rename(new_path.dentry, old_path.dentry);
4382 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
4383 error = PTR_ERR(old_dentry);
4384 if (IS_ERR(old_dentry))
4385 goto exit3;
4386 /* source must exist */
4387 error = -ENOENT;
4388 if (d_is_negative(old_dentry))
4389 goto exit4;
4390 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
4391 error = PTR_ERR(new_dentry);
4392 if (IS_ERR(new_dentry))
4393 goto exit4;
4394 error = -EEXIST;
4395 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4396 goto exit5;
4397 if (flags & RENAME_EXCHANGE) {
4398 error = -ENOENT;
4399 if (d_is_negative(new_dentry))
4400 goto exit5;
4402 if (!d_is_dir(new_dentry)) {
4403 error = -ENOTDIR;
4404 if (new_last.name[new_last.len])
4405 goto exit5;
4408 /* unless the source is a directory trailing slashes give -ENOTDIR */
4409 if (!d_is_dir(old_dentry)) {
4410 error = -ENOTDIR;
4411 if (old_last.name[old_last.len])
4412 goto exit5;
4413 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
4414 goto exit5;
4416 /* source should not be ancestor of target */
4417 error = -EINVAL;
4418 if (old_dentry == trap)
4419 goto exit5;
4420 /* target should not be an ancestor of source */
4421 if (!(flags & RENAME_EXCHANGE))
4422 error = -ENOTEMPTY;
4423 if (new_dentry == trap)
4424 goto exit5;
4426 error = security_path_rename(&old_path, old_dentry,
4427 &new_path, new_dentry, flags);
4428 if (error)
4429 goto exit5;
4430 error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4431 new_path.dentry->d_inode, new_dentry,
4432 &delegated_inode, flags);
4433 exit5:
4434 dput(new_dentry);
4435 exit4:
4436 dput(old_dentry);
4437 exit3:
4438 unlock_rename(new_path.dentry, old_path.dentry);
4439 if (delegated_inode) {
4440 error = break_deleg_wait(&delegated_inode);
4441 if (!error)
4442 goto retry_deleg;
4444 mnt_drop_write(old_path.mnt);
4445 exit2:
4446 if (retry_estale(error, lookup_flags))
4447 should_retry = true;
4448 path_put(&new_path);
4449 putname(to);
4450 exit1:
4451 path_put(&old_path);
4452 putname(from);
4453 if (should_retry) {
4454 should_retry = false;
4455 lookup_flags |= LOOKUP_REVAL;
4456 goto retry;
4458 exit:
4459 return error;
4462 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4463 int, newdfd, const char __user *, newname)
4465 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4468 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
4470 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
4473 int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4475 int error = may_create(dir, dentry);
4476 if (error)
4477 return error;
4479 if (!dir->i_op->mknod)
4480 return -EPERM;
4482 return dir->i_op->mknod(dir, dentry,
4483 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4485 EXPORT_SYMBOL(vfs_whiteout);
4487 int readlink_copy(char __user *buffer, int buflen, const char *link)
4489 int len = PTR_ERR(link);
4490 if (IS_ERR(link))
4491 goto out;
4493 len = strlen(link);
4494 if (len > (unsigned) buflen)
4495 len = buflen;
4496 if (copy_to_user(buffer, link, len))
4497 len = -EFAULT;
4498 out:
4499 return len;
4501 EXPORT_SYMBOL(readlink_copy);
4504 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4505 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4506 * using) it for any given inode is up to filesystem.
4508 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4510 void *cookie;
4511 struct inode *inode = d_inode(dentry);
4512 const char *link = inode->i_link;
4513 int res;
4515 if (!link) {
4516 link = inode->i_op->follow_link(dentry, &cookie);
4517 if (IS_ERR(link))
4518 return PTR_ERR(link);
4520 res = readlink_copy(buffer, buflen, link);
4521 if (inode->i_op->put_link)
4522 inode->i_op->put_link(inode, cookie);
4523 return res;
4525 EXPORT_SYMBOL(generic_readlink);
4527 /* get the link contents into pagecache */
4528 static char *page_getlink(struct dentry * dentry, struct page **ppage)
4530 char *kaddr;
4531 struct page *page;
4532 struct address_space *mapping = dentry->d_inode->i_mapping;
4533 page = read_mapping_page(mapping, 0, NULL);
4534 if (IS_ERR(page))
4535 return (char*)page;
4536 *ppage = page;
4537 kaddr = kmap(page);
4538 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4539 return kaddr;
4542 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4544 struct page *page = NULL;
4545 int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
4546 if (page) {
4547 kunmap(page);
4548 page_cache_release(page);
4550 return res;
4552 EXPORT_SYMBOL(page_readlink);
4554 const char *page_follow_link_light(struct dentry *dentry, void **cookie)
4556 struct page *page = NULL;
4557 char *res = page_getlink(dentry, &page);
4558 if (!IS_ERR(res))
4559 *cookie = page;
4560 return res;
4562 EXPORT_SYMBOL(page_follow_link_light);
4564 void page_put_link(struct inode *unused, void *cookie)
4566 struct page *page = cookie;
4567 kunmap(page);
4568 page_cache_release(page);
4570 EXPORT_SYMBOL(page_put_link);
4573 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4575 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
4577 struct address_space *mapping = inode->i_mapping;
4578 struct page *page;
4579 void *fsdata;
4580 int err;
4581 char *kaddr;
4582 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4583 if (nofs)
4584 flags |= AOP_FLAG_NOFS;
4586 retry:
4587 err = pagecache_write_begin(NULL, mapping, 0, len-1,
4588 flags, &page, &fsdata);
4589 if (err)
4590 goto fail;
4592 kaddr = kmap_atomic(page);
4593 memcpy(kaddr, symname, len-1);
4594 kunmap_atomic(kaddr);
4596 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4597 page, fsdata);
4598 if (err < 0)
4599 goto fail;
4600 if (err < len-1)
4601 goto retry;
4603 mark_inode_dirty(inode);
4604 return 0;
4605 fail:
4606 return err;
4608 EXPORT_SYMBOL(__page_symlink);
4610 int page_symlink(struct inode *inode, const char *symname, int len)
4612 return __page_symlink(inode, symname, len,
4613 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
4615 EXPORT_SYMBOL(page_symlink);
4617 const struct inode_operations page_symlink_inode_operations = {
4618 .readlink = generic_readlink,
4619 .follow_link = page_follow_link_light,
4620 .put_link = page_put_link,
4622 EXPORT_SYMBOL(page_symlink_inode_operations);