1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
9 * Some corrections by tytso.
12 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
15 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
18 #include <linux/init.h>
19 #include <linux/export.h>
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
23 #include <linux/namei.h>
24 #include <linux/pagemap.h>
25 #include <linux/fsnotify.h>
26 #include <linux/personality.h>
27 #include <linux/security.h>
28 #include <linux/ima.h>
29 #include <linux/syscalls.h>
30 #include <linux/mount.h>
31 #include <linux/audit.h>
32 #include <linux/capability.h>
33 #include <linux/file.h>
34 #include <linux/fcntl.h>
35 #include <linux/device_cgroup.h>
36 #include <linux/fs_struct.h>
37 #include <linux/posix_acl.h>
38 #include <linux/hash.h>
39 #include <linux/bitops.h>
40 #include <linux/init_task.h>
41 #include <linux/uaccess.h>
42 #include <linux/build_bug.h>
47 /* [Feb-1997 T. Schoebel-Theuer]
48 * Fundamental changes in the pathname lookup mechanisms (namei)
49 * were necessary because of omirr. The reason is that omirr needs
50 * to know the _real_ pathname, not the user-supplied one, in case
51 * of symlinks (and also when transname replacements occur).
53 * The new code replaces the old recursive symlink resolution with
54 * an iterative one (in case of non-nested symlink chains). It does
55 * this with calls to <fs>_follow_link().
56 * As a side effect, dir_namei(), _namei() and follow_link() are now
57 * replaced with a single function lookup_dentry() that can handle all
58 * the special cases of the former code.
60 * With the new dcache, the pathname is stored at each inode, at least as
61 * long as the refcount of the inode is positive. As a side effect, the
62 * size of the dcache depends on the inode cache and thus is dynamic.
64 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
65 * resolution to correspond with current state of the code.
67 * Note that the symlink resolution is not *completely* iterative.
68 * There is still a significant amount of tail- and mid- recursion in
69 * the algorithm. Also, note that <fs>_readlink() is not used in
70 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
71 * may return different results than <fs>_follow_link(). Many virtual
72 * filesystems (including /proc) exhibit this behavior.
75 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
76 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
77 * and the name already exists in form of a symlink, try to create the new
78 * name indicated by the symlink. The old code always complained that the
79 * name already exists, due to not following the symlink even if its target
80 * is nonexistent. The new semantics affects also mknod() and link() when
81 * the name is a symlink pointing to a non-existent name.
83 * I don't know which semantics is the right one, since I have no access
84 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
85 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
86 * "old" one. Personally, I think the new semantics is much more logical.
87 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
88 * file does succeed in both HP-UX and SunOs, but not in Solaris
89 * and in the old Linux semantics.
92 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
93 * semantics. See the comments in "open_namei" and "do_link" below.
95 * [10-Sep-98 Alan Modra] Another symlink change.
98 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
99 * inside the path - always follow.
100 * in the last component in creation/removal/renaming - never follow.
101 * if LOOKUP_FOLLOW passed - follow.
102 * if the pathname has trailing slashes - follow.
103 * otherwise - don't follow.
104 * (applied in that order).
106 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
107 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
108 * During the 2.4 we need to fix the userland stuff depending on it -
109 * hopefully we will be able to get rid of that wart in 2.5. So far only
110 * XEmacs seems to be relying on it...
113 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
114 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
115 * any extra contention...
118 /* In order to reduce some races, while at the same time doing additional
119 * checking and hopefully speeding things up, we copy filenames to the
120 * kernel data space before using them..
122 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
123 * PATH_MAX includes the nul terminator --RR.
126 #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
129 getname_flags(const char __user *filename, int flags, int *empty)
131 struct filename *result;
134 BUILD_BUG_ON(offsetof(struct filename, iname) % sizeof(long) != 0);
136 result = audit_reusename(filename);
140 result = __getname();
141 if (unlikely(!result))
142 return ERR_PTR(-ENOMEM);
145 * First, try to embed the struct filename inside the names_cache
148 kname = (char *)result->iname;
149 result->name = kname;
151 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
152 if (unlikely(len < 0)) {
158 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
159 * separate struct filename so we can dedicate the entire
160 * names_cache allocation for the pathname, and re-do the copy from
163 if (unlikely(len == EMBEDDED_NAME_MAX)) {
164 const size_t size = offsetof(struct filename, iname[1]);
165 kname = (char *)result;
168 * size is chosen that way we to guarantee that
169 * result->iname[0] is within the same object and that
170 * kname can't be equal to result->iname, no matter what.
172 result = kzalloc(size, GFP_KERNEL);
173 if (unlikely(!result)) {
175 return ERR_PTR(-ENOMEM);
177 result->name = kname;
178 len = strncpy_from_user(kname, filename, PATH_MAX);
179 if (unlikely(len < 0)) {
184 if (unlikely(len == PATH_MAX)) {
187 return ERR_PTR(-ENAMETOOLONG);
192 /* The empty path is special. */
193 if (unlikely(!len)) {
196 if (!(flags & LOOKUP_EMPTY)) {
198 return ERR_PTR(-ENOENT);
202 result->uptr = filename;
203 result->aname = NULL;
204 audit_getname(result);
209 getname(const char __user * filename)
211 return getname_flags(filename, 0, NULL);
215 getname_kernel(const char * filename)
217 struct filename *result;
218 int len = strlen(filename) + 1;
220 result = __getname();
221 if (unlikely(!result))
222 return ERR_PTR(-ENOMEM);
224 if (len <= EMBEDDED_NAME_MAX) {
225 result->name = (char *)result->iname;
226 } else if (len <= PATH_MAX) {
227 const size_t size = offsetof(struct filename, iname[1]);
228 struct filename *tmp;
230 tmp = kmalloc(size, GFP_KERNEL);
231 if (unlikely(!tmp)) {
233 return ERR_PTR(-ENOMEM);
235 tmp->name = (char *)result;
239 return ERR_PTR(-ENAMETOOLONG);
241 memcpy((char *)result->name, filename, len);
243 result->aname = NULL;
245 audit_getname(result);
250 void putname(struct filename *name)
252 BUG_ON(name->refcnt <= 0);
254 if (--name->refcnt > 0)
257 if (name->name != name->iname) {
258 __putname(name->name);
264 static int check_acl(struct inode *inode, int mask)
266 #ifdef CONFIG_FS_POSIX_ACL
267 struct posix_acl *acl;
269 if (mask & MAY_NOT_BLOCK) {
270 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
273 /* no ->get_acl() calls in RCU mode... */
274 if (is_uncached_acl(acl))
276 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
279 acl = get_acl(inode, ACL_TYPE_ACCESS);
283 int error = posix_acl_permission(inode, acl, mask);
284 posix_acl_release(acl);
293 * This does the basic permission checking
295 static int acl_permission_check(struct inode *inode, int mask)
297 unsigned int mode = inode->i_mode;
299 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
302 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
303 int error = check_acl(inode, mask);
304 if (error != -EAGAIN)
308 if (in_group_p(inode->i_gid))
313 * If the DACs are ok we don't need any capability check.
315 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
321 * generic_permission - check for access rights on a Posix-like filesystem
322 * @inode: inode to check access rights for
323 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
325 * Used to check for read/write/execute permissions on a file.
326 * We use "fsuid" for this, letting us set arbitrary permissions
327 * for filesystem access without changing the "normal" uids which
328 * are used for other things.
330 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
331 * request cannot be satisfied (eg. requires blocking or too much complexity).
332 * It would then be called again in ref-walk mode.
334 int generic_permission(struct inode *inode, int mask)
339 * Do the basic permission checks.
341 ret = acl_permission_check(inode, mask);
345 if (S_ISDIR(inode->i_mode)) {
346 /* DACs are overridable for directories */
347 if (!(mask & MAY_WRITE))
348 if (capable_wrt_inode_uidgid(inode,
349 CAP_DAC_READ_SEARCH))
351 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
357 * Searching includes executable on directories, else just read.
359 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
360 if (mask == MAY_READ)
361 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
364 * Read/write DACs are always overridable.
365 * Executable DACs are overridable when there is
366 * at least one exec bit set.
368 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
369 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
374 EXPORT_SYMBOL(generic_permission);
377 * We _really_ want to just do "generic_permission()" without
378 * even looking at the inode->i_op values. So we keep a cache
379 * flag in inode->i_opflags, that says "this has not special
380 * permission function, use the fast case".
382 static inline int do_inode_permission(struct inode *inode, int mask)
384 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
385 if (likely(inode->i_op->permission))
386 return inode->i_op->permission(inode, mask);
388 /* This gets set once for the inode lifetime */
389 spin_lock(&inode->i_lock);
390 inode->i_opflags |= IOP_FASTPERM;
391 spin_unlock(&inode->i_lock);
393 return generic_permission(inode, mask);
397 * sb_permission - Check superblock-level permissions
398 * @sb: Superblock of inode to check permission on
399 * @inode: Inode to check permission on
400 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
402 * Separate out file-system wide checks from inode-specific permission checks.
404 static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
406 if (unlikely(mask & MAY_WRITE)) {
407 umode_t mode = inode->i_mode;
409 /* Nobody gets write access to a read-only fs. */
410 if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
417 * inode_permission - Check for access rights to a given inode
418 * @inode: Inode to check permission on
419 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
421 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
422 * this, letting us set arbitrary permissions for filesystem access without
423 * changing the "normal" UIDs which are used for other things.
425 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
427 int inode_permission(struct inode *inode, int mask)
431 retval = sb_permission(inode->i_sb, inode, mask);
435 if (unlikely(mask & MAY_WRITE)) {
437 * Nobody gets write access to an immutable file.
439 if (IS_IMMUTABLE(inode))
443 * Updating mtime will likely cause i_uid and i_gid to be
444 * written back improperly if their true value is unknown
447 if (HAS_UNMAPPED_ID(inode))
451 retval = do_inode_permission(inode, mask);
455 retval = devcgroup_inode_permission(inode, mask);
459 return security_inode_permission(inode, mask);
461 EXPORT_SYMBOL(inode_permission);
464 * path_get - get a reference to a path
465 * @path: path to get the reference to
467 * Given a path increment the reference count to the dentry and the vfsmount.
469 void path_get(const struct path *path)
474 EXPORT_SYMBOL(path_get);
477 * path_put - put a reference to a path
478 * @path: path to put the reference to
480 * Given a path decrement the reference count to the dentry and the vfsmount.
482 void path_put(const struct path *path)
487 EXPORT_SYMBOL(path_put);
489 #define EMBEDDED_LEVELS 2
494 struct inode *inode; /* path.dentry.d_inode */
499 int total_link_count;
502 struct delayed_call done;
505 } *stack, internal[EMBEDDED_LEVELS];
506 struct filename *name;
507 struct nameidata *saved;
508 struct inode *link_inode;
511 } __randomize_layout;
513 static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
515 struct nameidata *old = current->nameidata;
516 p->stack = p->internal;
519 p->total_link_count = old ? old->total_link_count : 0;
521 current->nameidata = p;
524 static void restore_nameidata(void)
526 struct nameidata *now = current->nameidata, *old = now->saved;
528 current->nameidata = old;
530 old->total_link_count = now->total_link_count;
531 if (now->stack != now->internal)
535 static int __nd_alloc_stack(struct nameidata *nd)
539 if (nd->flags & LOOKUP_RCU) {
540 p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
545 p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
550 memcpy(p, nd->internal, sizeof(nd->internal));
556 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
557 * @path: nameidate to verify
559 * Rename can sometimes move a file or directory outside of a bind
560 * mount, path_connected allows those cases to be detected.
562 static bool path_connected(const struct path *path)
564 struct vfsmount *mnt = path->mnt;
565 struct super_block *sb = mnt->mnt_sb;
567 /* Bind mounts and multi-root filesystems can have disconnected paths */
568 if (!(sb->s_iflags & SB_I_MULTIROOT) && (mnt->mnt_root == sb->s_root))
571 return is_subdir(path->dentry, mnt->mnt_root);
574 static inline int nd_alloc_stack(struct nameidata *nd)
576 if (likely(nd->depth != EMBEDDED_LEVELS))
578 if (likely(nd->stack != nd->internal))
580 return __nd_alloc_stack(nd);
583 static void drop_links(struct nameidata *nd)
587 struct saved *last = nd->stack + i;
588 do_delayed_call(&last->done);
589 clear_delayed_call(&last->done);
593 static void terminate_walk(struct nameidata *nd)
596 if (!(nd->flags & LOOKUP_RCU)) {
599 for (i = 0; i < nd->depth; i++)
600 path_put(&nd->stack[i].link);
601 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
606 nd->flags &= ~LOOKUP_RCU;
607 if (!(nd->flags & LOOKUP_ROOT))
614 /* path_put is needed afterwards regardless of success or failure */
615 static bool legitimize_path(struct nameidata *nd,
616 struct path *path, unsigned seq)
618 int res = __legitimize_mnt(path->mnt, nd->m_seq);
625 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
629 return !read_seqcount_retry(&path->dentry->d_seq, seq);
632 static bool legitimize_links(struct nameidata *nd)
635 for (i = 0; i < nd->depth; i++) {
636 struct saved *last = nd->stack + i;
637 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
647 * Path walking has 2 modes, rcu-walk and ref-walk (see
648 * Documentation/filesystems/path-lookup.txt). In situations when we can't
649 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
650 * normal reference counts on dentries and vfsmounts to transition to ref-walk
651 * mode. Refcounts are grabbed at the last known good point before rcu-walk
652 * got stuck, so ref-walk may continue from there. If this is not successful
653 * (eg. a seqcount has changed), then failure is returned and it's up to caller
654 * to restart the path walk from the beginning in ref-walk mode.
658 * unlazy_walk - try to switch to ref-walk mode.
659 * @nd: nameidata pathwalk data
660 * Returns: 0 on success, -ECHILD on failure
662 * unlazy_walk attempts to legitimize the current nd->path and nd->root
664 * Must be called from rcu-walk context.
665 * Nothing should touch nameidata between unlazy_walk() failure and
668 static int unlazy_walk(struct nameidata *nd)
670 struct dentry *parent = nd->path.dentry;
672 BUG_ON(!(nd->flags & LOOKUP_RCU));
674 nd->flags &= ~LOOKUP_RCU;
675 if (unlikely(!legitimize_links(nd)))
677 if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
679 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
680 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq)))
684 BUG_ON(nd->inode != parent->d_inode);
689 nd->path.dentry = NULL;
691 if (!(nd->flags & LOOKUP_ROOT))
699 * unlazy_child - try to switch to ref-walk mode.
700 * @nd: nameidata pathwalk data
701 * @dentry: child of nd->path.dentry
702 * @seq: seq number to check dentry against
703 * Returns: 0 on success, -ECHILD on failure
705 * unlazy_child attempts to legitimize the current nd->path, nd->root and dentry
706 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
707 * @nd. Must be called from rcu-walk context.
708 * Nothing should touch nameidata between unlazy_child() failure and
711 static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned seq)
713 BUG_ON(!(nd->flags & LOOKUP_RCU));
715 nd->flags &= ~LOOKUP_RCU;
716 if (unlikely(!legitimize_links(nd)))
718 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
720 if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
724 * We need to move both the parent and the dentry from the RCU domain
725 * to be properly refcounted. And the sequence number in the dentry
726 * validates *both* dentry counters, since we checked the sequence
727 * number of the parent after we got the child sequence number. So we
728 * know the parent must still be valid if the child sequence number is
730 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
732 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) {
738 * Sequence counts matched. Now make sure that the root is
739 * still valid and get it if required.
741 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
742 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
755 nd->path.dentry = NULL;
759 if (!(nd->flags & LOOKUP_ROOT))
764 static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
766 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
767 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;
787 if (nd->flags & LOOKUP_RCU) {
788 if (!(nd->flags & LOOKUP_ROOT))
790 if (unlikely(unlazy_walk(nd)))
794 if (likely(!(nd->flags & LOOKUP_JUMPED)))
797 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
800 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
810 static void set_root(struct nameidata *nd)
812 struct fs_struct *fs = current->fs;
814 if (nd->flags & LOOKUP_RCU) {
818 seq = read_seqcount_begin(&fs->seq);
820 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
821 } while (read_seqcount_retry(&fs->seq, seq));
823 get_fs_root(fs, &nd->root);
827 static void path_put_conditional(struct path *path, struct nameidata *nd)
830 if (path->mnt != nd->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;
846 static int nd_jump_root(struct nameidata *nd)
848 if (nd->flags & LOOKUP_RCU) {
852 nd->inode = d->d_inode;
853 nd->seq = nd->root_seq;
854 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
860 nd->inode = nd->path.dentry->d_inode;
862 nd->flags |= LOOKUP_JUMPED;
867 * Helper to directly jump to a known parsed path from ->get_link,
868 * caller must have taken a reference to path beforehand.
870 void nd_jump_link(struct path *path)
872 struct nameidata *nd = current->nameidata;
876 nd->inode = nd->path.dentry->d_inode;
877 nd->flags |= LOOKUP_JUMPED;
880 static inline void put_link(struct nameidata *nd)
882 struct saved *last = nd->stack + --nd->depth;
883 do_delayed_call(&last->done);
884 if (!(nd->flags & LOOKUP_RCU))
885 path_put(&last->link);
888 int sysctl_protected_symlinks __read_mostly = 0;
889 int sysctl_protected_hardlinks __read_mostly = 0;
890 int sysctl_protected_fifos __read_mostly;
891 int sysctl_protected_regular __read_mostly;
894 * may_follow_link - Check symlink following for unsafe situations
895 * @nd: nameidata pathwalk data
897 * In the case of the sysctl_protected_symlinks sysctl being enabled,
898 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
899 * in a sticky world-writable directory. This is to protect privileged
900 * processes from failing races against path names that may change out
901 * from under them by way of other users creating malicious symlinks.
902 * It will permit symlinks to be followed only when outside a sticky
903 * world-writable directory, or when the uid of the symlink and follower
904 * match, or when the directory owner matches the symlink's owner.
906 * Returns 0 if following the symlink is allowed, -ve on error.
908 static inline int may_follow_link(struct nameidata *nd)
910 const struct inode *inode;
911 const struct inode *parent;
914 if (!sysctl_protected_symlinks)
917 /* Allowed if owner and follower match. */
918 inode = nd->link_inode;
919 if (uid_eq(current_cred()->fsuid, inode->i_uid))
922 /* Allowed if parent directory not sticky and world-writable. */
924 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
927 /* Allowed if parent directory and link owner match. */
928 puid = parent->i_uid;
929 if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
932 if (nd->flags & LOOKUP_RCU)
935 audit_inode(nd->name, nd->stack[0].link.dentry, 0);
936 audit_log_link_denied("follow_link");
941 * safe_hardlink_source - Check for safe hardlink conditions
942 * @inode: the source inode to hardlink from
944 * Return false if at least one of the following conditions:
945 * - inode is not a regular file
947 * - inode is setgid and group-exec
948 * - access failure for read and write
950 * Otherwise returns true.
952 static bool safe_hardlink_source(struct inode *inode)
954 umode_t mode = inode->i_mode;
956 /* Special files should not get pinned to the filesystem. */
960 /* Setuid files should not get pinned to the filesystem. */
964 /* Executable setgid files should not get pinned to the filesystem. */
965 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
968 /* Hardlinking to unreadable or unwritable sources is dangerous. */
969 if (inode_permission(inode, MAY_READ | MAY_WRITE))
976 * may_linkat - Check permissions for creating a hardlink
977 * @link: the source to hardlink from
979 * Block hardlink when all of:
980 * - sysctl_protected_hardlinks enabled
981 * - fsuid does not match inode
982 * - hardlink source is unsafe (see safe_hardlink_source() above)
983 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
985 * Returns 0 if successful, -ve on error.
987 static int may_linkat(struct path *link)
989 struct inode *inode = link->dentry->d_inode;
991 /* Inode writeback is not safe when the uid or gid are invalid. */
992 if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
995 if (!sysctl_protected_hardlinks)
998 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
999 * otherwise, it must be a safe source.
1001 if (safe_hardlink_source(inode) || inode_owner_or_capable(inode))
1004 audit_log_link_denied("linkat");
1009 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1010 * should be allowed, or not, on files that already
1012 * @dir: the sticky parent directory
1013 * @inode: the inode of the file to open
1015 * Block an O_CREAT open of a FIFO (or a regular file) when:
1016 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1017 * - the file already exists
1018 * - we are in a sticky directory
1019 * - we don't own the file
1020 * - the owner of the directory doesn't own the file
1021 * - the directory is world writable
1022 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1023 * the directory doesn't have to be world writable: being group writable will
1026 * Returns 0 if the open is allowed, -ve on error.
1028 static int may_create_in_sticky(struct dentry * const dir,
1029 struct inode * const inode)
1031 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
1032 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
1033 likely(!(dir->d_inode->i_mode & S_ISVTX)) ||
1034 uid_eq(inode->i_uid, dir->d_inode->i_uid) ||
1035 uid_eq(current_fsuid(), inode->i_uid))
1038 if (likely(dir->d_inode->i_mode & 0002) ||
1039 (dir->d_inode->i_mode & 0020 &&
1040 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
1041 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
1047 static __always_inline
1048 const char *get_link(struct nameidata *nd)
1050 struct saved *last = nd->stack + nd->depth - 1;
1051 struct dentry *dentry = last->link.dentry;
1052 struct inode *inode = nd->link_inode;
1056 if (!(nd->flags & LOOKUP_RCU)) {
1057 touch_atime(&last->link);
1059 } else if (atime_needs_update(&last->link, inode)) {
1060 if (unlikely(unlazy_walk(nd)))
1061 return ERR_PTR(-ECHILD);
1062 touch_atime(&last->link);
1065 error = security_inode_follow_link(dentry, inode,
1066 nd->flags & LOOKUP_RCU);
1067 if (unlikely(error))
1068 return ERR_PTR(error);
1070 nd->last_type = LAST_BIND;
1071 res = inode->i_link;
1073 const char * (*get)(struct dentry *, struct inode *,
1074 struct delayed_call *);
1075 get = inode->i_op->get_link;
1076 if (nd->flags & LOOKUP_RCU) {
1077 res = get(NULL, inode, &last->done);
1078 if (res == ERR_PTR(-ECHILD)) {
1079 if (unlikely(unlazy_walk(nd)))
1080 return ERR_PTR(-ECHILD);
1081 res = get(dentry, inode, &last->done);
1084 res = get(dentry, inode, &last->done);
1086 if (IS_ERR_OR_NULL(res))
1092 if (unlikely(nd_jump_root(nd)))
1093 return ERR_PTR(-ECHILD);
1094 while (unlikely(*++res == '/'))
1103 * follow_up - Find the mountpoint of path's vfsmount
1105 * Given a path, find the mountpoint of its source file system.
1106 * Replace @path with the path of the mountpoint in the parent mount.
1109 * Return 1 if we went up a level and 0 if we were already at the
1112 int follow_up(struct path *path)
1114 struct mount *mnt = real_mount(path->mnt);
1115 struct mount *parent;
1116 struct dentry *mountpoint;
1118 read_seqlock_excl(&mount_lock);
1119 parent = mnt->mnt_parent;
1120 if (parent == mnt) {
1121 read_sequnlock_excl(&mount_lock);
1124 mntget(&parent->mnt);
1125 mountpoint = dget(mnt->mnt_mountpoint);
1126 read_sequnlock_excl(&mount_lock);
1128 path->dentry = mountpoint;
1130 path->mnt = &parent->mnt;
1133 EXPORT_SYMBOL(follow_up);
1136 * Perform an automount
1137 * - return -EISDIR to tell follow_managed() to stop and return the path we
1140 static int follow_automount(struct path *path, struct nameidata *nd,
1143 struct vfsmount *mnt;
1146 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1149 /* We don't want to mount if someone's just doing a stat -
1150 * unless they're stat'ing a directory and appended a '/' to
1153 * We do, however, want to mount if someone wants to open or
1154 * create a file of any type under the mountpoint, wants to
1155 * traverse through the mountpoint or wants to open the
1156 * mounted directory. Also, autofs may mark negative dentries
1157 * as being automount points. These will need the attentions
1158 * of the daemon to instantiate them before they can be used.
1160 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
1161 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
1162 path->dentry->d_inode)
1165 nd->total_link_count++;
1166 if (nd->total_link_count >= 40)
1169 mnt = path->dentry->d_op->d_automount(path);
1172 * The filesystem is allowed to return -EISDIR here to indicate
1173 * it doesn't want to automount. For instance, autofs would do
1174 * this so that its userspace daemon can mount on this dentry.
1176 * However, we can only permit this if it's a terminal point in
1177 * the path being looked up; if it wasn't then the remainder of
1178 * the path is inaccessible and we should say so.
1180 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
1182 return PTR_ERR(mnt);
1185 if (!mnt) /* mount collision */
1188 if (!*need_mntput) {
1189 /* lock_mount() may release path->mnt on error */
1191 *need_mntput = true;
1193 err = finish_automount(mnt, path);
1197 /* Someone else made a mount here whilst we were busy */
1202 path->dentry = dget(mnt->mnt_root);
1211 * Handle a dentry that is managed in some way.
1212 * - Flagged for transit management (autofs)
1213 * - Flagged as mountpoint
1214 * - Flagged as automount point
1216 * This may only be called in refwalk mode.
1218 * Serialization is taken care of in namespace.c
1220 static int follow_managed(struct path *path, struct nameidata *nd)
1222 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
1224 bool need_mntput = false;
1227 /* Given that we're not holding a lock here, we retain the value in a
1228 * local variable for each dentry as we look at it so that we don't see
1229 * the components of that value change under us */
1230 while (managed = READ_ONCE(path->dentry->d_flags),
1231 managed &= DCACHE_MANAGED_DENTRY,
1232 unlikely(managed != 0)) {
1233 /* Allow the filesystem to manage the transit without i_mutex
1235 if (managed & DCACHE_MANAGE_TRANSIT) {
1236 BUG_ON(!path->dentry->d_op);
1237 BUG_ON(!path->dentry->d_op->d_manage);
1238 ret = path->dentry->d_op->d_manage(path, false);
1243 /* Transit to a mounted filesystem. */
1244 if (managed & DCACHE_MOUNTED) {
1245 struct vfsmount *mounted = lookup_mnt(path);
1250 path->mnt = mounted;
1251 path->dentry = dget(mounted->mnt_root);
1256 /* Something is mounted on this dentry in another
1257 * namespace and/or whatever was mounted there in this
1258 * namespace got unmounted before lookup_mnt() could
1262 /* Handle an automount point */
1263 if (managed & DCACHE_NEED_AUTOMOUNT) {
1264 ret = follow_automount(path, nd, &need_mntput);
1270 /* We didn't change the current path point */
1274 if (need_mntput && path->mnt == mnt)
1276 if (ret == -EISDIR || !ret)
1279 nd->flags |= LOOKUP_JUMPED;
1280 if (unlikely(ret < 0))
1281 path_put_conditional(path, nd);
1285 int follow_down_one(struct path *path)
1287 struct vfsmount *mounted;
1289 mounted = lookup_mnt(path);
1293 path->mnt = mounted;
1294 path->dentry = dget(mounted->mnt_root);
1299 EXPORT_SYMBOL(follow_down_one);
1301 static inline int managed_dentry_rcu(const struct path *path)
1303 return (path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1304 path->dentry->d_op->d_manage(path, true) : 0;
1308 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1309 * we meet a managed dentry that would need blocking.
1311 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1312 struct inode **inode, unsigned *seqp)
1315 struct mount *mounted;
1317 * Don't forget we might have a non-mountpoint managed dentry
1318 * that wants to block transit.
1320 switch (managed_dentry_rcu(path)) {
1330 if (!d_mountpoint(path->dentry))
1331 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1333 mounted = __lookup_mnt(path->mnt, path->dentry);
1336 path->mnt = &mounted->mnt;
1337 path->dentry = mounted->mnt.mnt_root;
1338 nd->flags |= LOOKUP_JUMPED;
1339 *seqp = read_seqcount_begin(&path->dentry->d_seq);
1341 * Update the inode too. We don't need to re-check the
1342 * dentry sequence number here after this d_inode read,
1343 * because a mount-point is always pinned.
1345 *inode = path->dentry->d_inode;
1347 return !read_seqretry(&mount_lock, nd->m_seq) &&
1348 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1351 static int follow_dotdot_rcu(struct nameidata *nd)
1353 struct inode *inode = nd->inode;
1356 if (path_equal(&nd->path, &nd->root))
1358 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1359 struct dentry *old = nd->path.dentry;
1360 struct dentry *parent = old->d_parent;
1363 inode = parent->d_inode;
1364 seq = read_seqcount_begin(&parent->d_seq);
1365 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1367 nd->path.dentry = parent;
1369 if (unlikely(!path_connected(&nd->path)))
1373 struct mount *mnt = real_mount(nd->path.mnt);
1374 struct mount *mparent = mnt->mnt_parent;
1375 struct dentry *mountpoint = mnt->mnt_mountpoint;
1376 struct inode *inode2 = mountpoint->d_inode;
1377 unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1378 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1380 if (&mparent->mnt == nd->path.mnt)
1382 /* we know that mountpoint was pinned */
1383 nd->path.dentry = mountpoint;
1384 nd->path.mnt = &mparent->mnt;
1389 while (unlikely(d_mountpoint(nd->path.dentry))) {
1390 struct mount *mounted;
1391 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1392 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1396 nd->path.mnt = &mounted->mnt;
1397 nd->path.dentry = mounted->mnt.mnt_root;
1398 inode = nd->path.dentry->d_inode;
1399 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1406 * Follow down to the covering mount currently visible to userspace. At each
1407 * point, the filesystem owning that dentry may be queried as to whether the
1408 * caller is permitted to proceed or not.
1410 int follow_down(struct path *path)
1415 while (managed = READ_ONCE(path->dentry->d_flags),
1416 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1417 /* Allow the filesystem to manage the transit without i_mutex
1420 * We indicate to the filesystem if someone is trying to mount
1421 * something here. This gives autofs the chance to deny anyone
1422 * other than its daemon the right to mount on its
1425 * The filesystem may sleep at this point.
1427 if (managed & DCACHE_MANAGE_TRANSIT) {
1428 BUG_ON(!path->dentry->d_op);
1429 BUG_ON(!path->dentry->d_op->d_manage);
1430 ret = path->dentry->d_op->d_manage(path, false);
1432 return ret == -EISDIR ? 0 : ret;
1435 /* Transit to a mounted filesystem. */
1436 if (managed & DCACHE_MOUNTED) {
1437 struct vfsmount *mounted = lookup_mnt(path);
1442 path->mnt = mounted;
1443 path->dentry = dget(mounted->mnt_root);
1447 /* Don't handle automount points here */
1452 EXPORT_SYMBOL(follow_down);
1455 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1457 static void follow_mount(struct path *path)
1459 while (d_mountpoint(path->dentry)) {
1460 struct vfsmount *mounted = lookup_mnt(path);
1465 path->mnt = mounted;
1466 path->dentry = dget(mounted->mnt_root);
1470 static int path_parent_directory(struct path *path)
1472 struct dentry *old = path->dentry;
1473 /* rare case of legitimate dget_parent()... */
1474 path->dentry = dget_parent(path->dentry);
1476 if (unlikely(!path_connected(path)))
1481 static int follow_dotdot(struct nameidata *nd)
1484 if (path_equal(&nd->path, &nd->root))
1486 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1487 int ret = path_parent_directory(&nd->path);
1492 if (!follow_up(&nd->path))
1495 follow_mount(&nd->path);
1496 nd->inode = nd->path.dentry->d_inode;
1501 * This looks up the name in dcache and possibly revalidates the found dentry.
1502 * NULL is returned if the dentry does not exist in the cache.
1504 static struct dentry *lookup_dcache(const struct qstr *name,
1508 struct dentry *dentry = d_lookup(dir, name);
1510 int error = d_revalidate(dentry, flags);
1511 if (unlikely(error <= 0)) {
1513 d_invalidate(dentry);
1515 return ERR_PTR(error);
1522 * Parent directory has inode locked exclusive. This is one
1523 * and only case when ->lookup() gets called on non in-lookup
1524 * dentries - as the matter of fact, this only gets called
1525 * when directory is guaranteed to have no in-lookup children
1528 static struct dentry *__lookup_hash(const struct qstr *name,
1529 struct dentry *base, unsigned int flags)
1531 struct dentry *dentry = lookup_dcache(name, base, flags);
1533 struct inode *dir = base->d_inode;
1538 /* Don't create child dentry for a dead directory. */
1539 if (unlikely(IS_DEADDIR(dir)))
1540 return ERR_PTR(-ENOENT);
1542 dentry = d_alloc(base, name);
1543 if (unlikely(!dentry))
1544 return ERR_PTR(-ENOMEM);
1546 old = dir->i_op->lookup(dir, dentry, flags);
1547 if (unlikely(old)) {
1554 static int lookup_fast(struct nameidata *nd,
1555 struct path *path, struct inode **inode,
1558 struct vfsmount *mnt = nd->path.mnt;
1559 struct dentry *dentry, *parent = nd->path.dentry;
1564 * Rename seqlock is not required here because in the off chance
1565 * of a false negative due to a concurrent rename, the caller is
1566 * going to fall back to non-racy lookup.
1568 if (nd->flags & LOOKUP_RCU) {
1571 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
1572 if (unlikely(!dentry)) {
1573 if (unlazy_walk(nd))
1579 * This sequence count validates that the inode matches
1580 * the dentry name information from lookup.
1582 *inode = d_backing_inode(dentry);
1583 negative = d_is_negative(dentry);
1584 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
1588 * This sequence count validates that the parent had no
1589 * changes while we did the lookup of the dentry above.
1591 * The memory barrier in read_seqcount_begin of child is
1592 * enough, we can use __read_seqcount_retry here.
1594 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
1598 status = d_revalidate(dentry, nd->flags);
1599 if (likely(status > 0)) {
1601 * Note: do negative dentry check after revalidation in
1602 * case that drops it.
1604 if (unlikely(negative))
1607 path->dentry = dentry;
1608 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
1611 if (unlazy_child(nd, dentry, seq))
1613 if (unlikely(status == -ECHILD))
1614 /* we'd been told to redo it in non-rcu mode */
1615 status = d_revalidate(dentry, nd->flags);
1617 dentry = __d_lookup(parent, &nd->last);
1618 if (unlikely(!dentry))
1620 status = d_revalidate(dentry, nd->flags);
1622 if (unlikely(status <= 0)) {
1624 d_invalidate(dentry);
1628 if (unlikely(d_is_negative(dentry))) {
1634 path->dentry = dentry;
1635 err = follow_managed(path, nd);
1636 if (likely(err > 0))
1637 *inode = d_backing_inode(path->dentry);
1641 /* Fast lookup failed, do it the slow way */
1642 static struct dentry *__lookup_slow(const struct qstr *name,
1646 struct dentry *dentry, *old;
1647 struct inode *inode = dir->d_inode;
1648 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1650 /* Don't go there if it's already dead */
1651 if (unlikely(IS_DEADDIR(inode)))
1652 return ERR_PTR(-ENOENT);
1654 dentry = d_alloc_parallel(dir, name, &wq);
1657 if (unlikely(!d_in_lookup(dentry))) {
1658 if (!(flags & LOOKUP_NO_REVAL)) {
1659 int error = d_revalidate(dentry, flags);
1660 if (unlikely(error <= 0)) {
1662 d_invalidate(dentry);
1667 dentry = ERR_PTR(error);
1671 old = inode->i_op->lookup(inode, dentry, flags);
1672 d_lookup_done(dentry);
1673 if (unlikely(old)) {
1681 static struct dentry *lookup_slow(const struct qstr *name,
1685 struct inode *inode = dir->d_inode;
1687 inode_lock_shared(inode);
1688 res = __lookup_slow(name, dir, flags);
1689 inode_unlock_shared(inode);
1693 static inline int may_lookup(struct nameidata *nd)
1695 if (nd->flags & LOOKUP_RCU) {
1696 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1699 if (unlazy_walk(nd))
1702 return inode_permission(nd->inode, MAY_EXEC);
1705 static inline int handle_dots(struct nameidata *nd, int type)
1707 if (type == LAST_DOTDOT) {
1710 if (nd->flags & LOOKUP_RCU) {
1711 return follow_dotdot_rcu(nd);
1713 return follow_dotdot(nd);
1718 static int pick_link(struct nameidata *nd, struct path *link,
1719 struct inode *inode, unsigned seq)
1723 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
1724 path_to_nameidata(link, nd);
1727 if (!(nd->flags & LOOKUP_RCU)) {
1728 if (link->mnt == nd->path.mnt)
1731 error = nd_alloc_stack(nd);
1732 if (unlikely(error)) {
1733 if (error == -ECHILD) {
1734 if (unlikely(!legitimize_path(nd, link, seq))) {
1737 nd->flags &= ~LOOKUP_RCU;
1738 nd->path.mnt = NULL;
1739 nd->path.dentry = NULL;
1740 if (!(nd->flags & LOOKUP_ROOT))
1741 nd->root.mnt = NULL;
1743 } else if (likely(unlazy_walk(nd)) == 0)
1744 error = nd_alloc_stack(nd);
1752 last = nd->stack + nd->depth++;
1754 clear_delayed_call(&last->done);
1755 nd->link_inode = inode;
1760 enum {WALK_FOLLOW = 1, WALK_MORE = 2};
1763 * Do we need to follow links? We _really_ want to be able
1764 * to do this check without having to look at inode->i_op,
1765 * so we keep a cache of "no, this doesn't need follow_link"
1766 * for the common case.
1768 static inline int step_into(struct nameidata *nd, struct path *path,
1769 int flags, struct inode *inode, unsigned seq)
1771 if (!(flags & WALK_MORE) && nd->depth)
1773 if (likely(!d_is_symlink(path->dentry)) ||
1774 !(flags & WALK_FOLLOW || nd->flags & LOOKUP_FOLLOW)) {
1775 /* not a symlink or should not follow */
1776 path_to_nameidata(path, nd);
1781 /* make sure that d_is_symlink above matches inode */
1782 if (nd->flags & LOOKUP_RCU) {
1783 if (read_seqcount_retry(&path->dentry->d_seq, seq))
1786 return pick_link(nd, path, inode, seq);
1789 static int walk_component(struct nameidata *nd, int flags)
1792 struct inode *inode;
1796 * "." and ".." are special - ".." especially so because it has
1797 * to be able to know about the current root directory and
1798 * parent relationships.
1800 if (unlikely(nd->last_type != LAST_NORM)) {
1801 err = handle_dots(nd, nd->last_type);
1802 if (!(flags & WALK_MORE) && nd->depth)
1806 err = lookup_fast(nd, &path, &inode, &seq);
1807 if (unlikely(err <= 0)) {
1810 path.dentry = lookup_slow(&nd->last, nd->path.dentry,
1812 if (IS_ERR(path.dentry))
1813 return PTR_ERR(path.dentry);
1815 path.mnt = nd->path.mnt;
1816 err = follow_managed(&path, nd);
1817 if (unlikely(err < 0))
1820 if (unlikely(d_is_negative(path.dentry))) {
1821 path_to_nameidata(&path, nd);
1825 seq = 0; /* we are already out of RCU mode */
1826 inode = d_backing_inode(path.dentry);
1829 return step_into(nd, &path, flags, inode, seq);
1833 * We can do the critical dentry name comparison and hashing
1834 * operations one word at a time, but we are limited to:
1836 * - Architectures with fast unaligned word accesses. We could
1837 * do a "get_unaligned()" if this helps and is sufficiently
1840 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1841 * do not trap on the (extremely unlikely) case of a page
1842 * crossing operation.
1844 * - Furthermore, we need an efficient 64-bit compile for the
1845 * 64-bit case in order to generate the "number of bytes in
1846 * the final mask". Again, that could be replaced with a
1847 * efficient population count instruction or similar.
1849 #ifdef CONFIG_DCACHE_WORD_ACCESS
1851 #include <asm/word-at-a-time.h>
1855 /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1857 #elif defined(CONFIG_64BIT)
1859 * Register pressure in the mixing function is an issue, particularly
1860 * on 32-bit x86, but almost any function requires one state value and
1861 * one temporary. Instead, use a function designed for two state values
1862 * and no temporaries.
1864 * This function cannot create a collision in only two iterations, so
1865 * we have two iterations to achieve avalanche. In those two iterations,
1866 * we have six layers of mixing, which is enough to spread one bit's
1867 * influence out to 2^6 = 64 state bits.
1869 * Rotate constants are scored by considering either 64 one-bit input
1870 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
1871 * probability of that delta causing a change to each of the 128 output
1872 * bits, using a sample of random initial states.
1874 * The Shannon entropy of the computed probabilities is then summed
1875 * to produce a score. Ideally, any input change has a 50% chance of
1876 * toggling any given output bit.
1878 * Mixing scores (in bits) for (12,45):
1879 * Input delta: 1-bit 2-bit
1880 * 1 round: 713.3 42542.6
1881 * 2 rounds: 2753.7 140389.8
1882 * 3 rounds: 5954.1 233458.2
1883 * 4 rounds: 7862.6 256672.2
1884 * Perfect: 8192 258048
1885 * (64*128) (64*63/2 * 128)
1887 #define HASH_MIX(x, y, a) \
1889 y ^= x, x = rol64(x,12),\
1890 x += y, y = rol64(y,45),\
1894 * Fold two longs into one 32-bit hash value. This must be fast, but
1895 * latency isn't quite as critical, as there is a fair bit of additional
1896 * work done before the hash value is used.
1898 static inline unsigned int fold_hash(unsigned long x, unsigned long y)
1900 y ^= x * GOLDEN_RATIO_64;
1901 y *= GOLDEN_RATIO_64;
1905 #else /* 32-bit case */
1908 * Mixing scores (in bits) for (7,20):
1909 * Input delta: 1-bit 2-bit
1910 * 1 round: 330.3 9201.6
1911 * 2 rounds: 1246.4 25475.4
1912 * 3 rounds: 1907.1 31295.1
1913 * 4 rounds: 2042.3 31718.6
1914 * Perfect: 2048 31744
1915 * (32*64) (32*31/2 * 64)
1917 #define HASH_MIX(x, y, a) \
1919 y ^= x, x = rol32(x, 7),\
1920 x += y, y = rol32(y,20),\
1923 static inline unsigned int fold_hash(unsigned long x, unsigned long y)
1925 /* Use arch-optimized multiply if one exists */
1926 return __hash_32(y ^ __hash_32(x));
1932 * Return the hash of a string of known length. This is carfully
1933 * designed to match hash_name(), which is the more critical function.
1934 * In particular, we must end by hashing a final word containing 0..7
1935 * payload bytes, to match the way that hash_name() iterates until it
1936 * finds the delimiter after the name.
1938 unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
1940 unsigned long a, x = 0, y = (unsigned long)salt;
1945 a = load_unaligned_zeropad(name);
1946 if (len < sizeof(unsigned long))
1949 name += sizeof(unsigned long);
1950 len -= sizeof(unsigned long);
1952 x ^= a & bytemask_from_count(len);
1954 return fold_hash(x, y);
1956 EXPORT_SYMBOL(full_name_hash);
1958 /* Return the "hash_len" (hash and length) of a null-terminated string */
1959 u64 hashlen_string(const void *salt, const char *name)
1961 unsigned long a = 0, x = 0, y = (unsigned long)salt;
1962 unsigned long adata, mask, len;
1963 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1970 len += sizeof(unsigned long);
1972 a = load_unaligned_zeropad(name+len);
1973 } while (!has_zero(a, &adata, &constants));
1975 adata = prep_zero_mask(a, adata, &constants);
1976 mask = create_zero_mask(adata);
1977 x ^= a & zero_bytemask(mask);
1979 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
1981 EXPORT_SYMBOL(hashlen_string);
1984 * Calculate the length and hash of the path component, and
1985 * return the "hash_len" as the result.
1987 static inline u64 hash_name(const void *salt, const char *name)
1989 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
1990 unsigned long adata, bdata, mask, len;
1991 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1998 len += sizeof(unsigned long);
2000 a = load_unaligned_zeropad(name+len);
2001 b = a ^ REPEAT_BYTE('/');
2002 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
2004 adata = prep_zero_mask(a, adata, &constants);
2005 bdata = prep_zero_mask(b, bdata, &constants);
2006 mask = create_zero_mask(adata | bdata);
2007 x ^= a & zero_bytemask(mask);
2009 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
2012 #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
2014 /* Return the hash of a string of known length */
2015 unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
2017 unsigned long hash = init_name_hash(salt);
2019 hash = partial_name_hash((unsigned char)*name++, hash);
2020 return end_name_hash(hash);
2022 EXPORT_SYMBOL(full_name_hash);
2024 /* Return the "hash_len" (hash and length) of a null-terminated string */
2025 u64 hashlen_string(const void *salt, const char *name)
2027 unsigned long hash = init_name_hash(salt);
2028 unsigned long len = 0, c;
2030 c = (unsigned char)*name;
2033 hash = partial_name_hash(c, hash);
2034 c = (unsigned char)name[len];
2036 return hashlen_create(end_name_hash(hash), len);
2038 EXPORT_SYMBOL(hashlen_string);
2041 * We know there's a real path component here of at least
2044 static inline u64 hash_name(const void *salt, const char *name)
2046 unsigned long hash = init_name_hash(salt);
2047 unsigned long len = 0, c;
2049 c = (unsigned char)*name;
2052 hash = partial_name_hash(c, hash);
2053 c = (unsigned char)name[len];
2054 } while (c && c != '/');
2055 return hashlen_create(end_name_hash(hash), len);
2062 * This is the basic name resolution function, turning a pathname into
2063 * the final dentry. We expect 'base' to be positive and a directory.
2065 * Returns 0 and nd will have valid dentry and mnt on success.
2066 * Returns error and drops reference to input namei data on failure.
2068 static int link_path_walk(const char *name, struct nameidata *nd)
2073 return PTR_ERR(name);
2079 /* At this point we know we have a real path component. */
2084 err = may_lookup(nd);
2088 hash_len = hash_name(nd->path.dentry, name);
2091 if (name[0] == '.') switch (hashlen_len(hash_len)) {
2093 if (name[1] == '.') {
2095 nd->flags |= LOOKUP_JUMPED;
2101 if (likely(type == LAST_NORM)) {
2102 struct dentry *parent = nd->path.dentry;
2103 nd->flags &= ~LOOKUP_JUMPED;
2104 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
2105 struct qstr this = { { .hash_len = hash_len }, .name = name };
2106 err = parent->d_op->d_hash(parent, &this);
2109 hash_len = this.hash_len;
2114 nd->last.hash_len = hash_len;
2115 nd->last.name = name;
2116 nd->last_type = type;
2118 name += hashlen_len(hash_len);
2122 * If it wasn't NUL, we know it was '/'. Skip that
2123 * slash, and continue until no more slashes.
2127 } while (unlikely(*name == '/'));
2128 if (unlikely(!*name)) {
2130 /* pathname body, done */
2133 name = nd->stack[nd->depth - 1].name;
2134 /* trailing symlink, done */
2137 /* last component of nested symlink */
2138 err = walk_component(nd, WALK_FOLLOW);
2140 /* not the last component */
2141 err = walk_component(nd, WALK_FOLLOW | WALK_MORE);
2147 const char *s = get_link(nd);
2156 nd->stack[nd->depth - 1].name = name;
2161 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2162 if (nd->flags & LOOKUP_RCU) {
2163 if (unlazy_walk(nd))
2171 /* must be paired with terminate_walk() */
2172 static const char *path_init(struct nameidata *nd, unsigned flags)
2174 const char *s = nd->name->name;
2177 flags &= ~LOOKUP_RCU;
2178 if (flags & LOOKUP_RCU)
2181 nd->last_type = LAST_ROOT; /* if there are only slashes... */
2182 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
2184 if (flags & LOOKUP_ROOT) {
2185 struct dentry *root = nd->root.dentry;
2186 struct inode *inode = root->d_inode;
2187 if (*s && unlikely(!d_can_lookup(root)))
2188 return ERR_PTR(-ENOTDIR);
2189 nd->path = nd->root;
2191 if (flags & LOOKUP_RCU) {
2192 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2193 nd->root_seq = nd->seq;
2194 nd->m_seq = read_seqbegin(&mount_lock);
2196 path_get(&nd->path);
2201 nd->root.mnt = NULL;
2202 nd->path.mnt = NULL;
2203 nd->path.dentry = NULL;
2205 nd->m_seq = read_seqbegin(&mount_lock);
2208 if (likely(!nd_jump_root(nd)))
2210 return ERR_PTR(-ECHILD);
2211 } else if (nd->dfd == AT_FDCWD) {
2212 if (flags & LOOKUP_RCU) {
2213 struct fs_struct *fs = current->fs;
2217 seq = read_seqcount_begin(&fs->seq);
2219 nd->inode = nd->path.dentry->d_inode;
2220 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2221 } while (read_seqcount_retry(&fs->seq, seq));
2223 get_fs_pwd(current->fs, &nd->path);
2224 nd->inode = nd->path.dentry->d_inode;
2228 /* Caller must check execute permissions on the starting path component */
2229 struct fd f = fdget_raw(nd->dfd);
2230 struct dentry *dentry;
2233 return ERR_PTR(-EBADF);
2235 dentry = f.file->f_path.dentry;
2237 if (*s && unlikely(!d_can_lookup(dentry))) {
2239 return ERR_PTR(-ENOTDIR);
2242 nd->path = f.file->f_path;
2243 if (flags & LOOKUP_RCU) {
2244 nd->inode = nd->path.dentry->d_inode;
2245 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
2247 path_get(&nd->path);
2248 nd->inode = nd->path.dentry->d_inode;
2255 static const char *trailing_symlink(struct nameidata *nd)
2258 int error = may_follow_link(nd);
2259 if (unlikely(error))
2260 return ERR_PTR(error);
2261 nd->flags |= LOOKUP_PARENT;
2262 nd->stack[0].name = NULL;
2267 static inline int lookup_last(struct nameidata *nd)
2269 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2270 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2272 nd->flags &= ~LOOKUP_PARENT;
2273 return walk_component(nd, 0);
2276 static int handle_lookup_down(struct nameidata *nd)
2278 struct path path = nd->path;
2279 struct inode *inode = nd->inode;
2280 unsigned seq = nd->seq;
2283 if (nd->flags & LOOKUP_RCU) {
2285 * don't bother with unlazy_walk on failure - we are
2286 * at the very beginning of walk, so we lose nothing
2287 * if we simply redo everything in non-RCU mode
2289 if (unlikely(!__follow_mount_rcu(nd, &path, &inode, &seq)))
2293 err = follow_managed(&path, nd);
2294 if (unlikely(err < 0))
2296 inode = d_backing_inode(path.dentry);
2299 path_to_nameidata(&path, nd);
2305 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2306 static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
2308 const char *s = path_init(nd, flags);
2311 if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
2312 err = handle_lookup_down(nd);
2313 if (unlikely(err < 0))
2317 while (!(err = link_path_walk(s, nd))
2318 && ((err = lookup_last(nd)) > 0)) {
2319 s = trailing_symlink(nd);
2322 err = complete_walk(nd);
2324 if (!err && nd->flags & LOOKUP_DIRECTORY)
2325 if (!d_can_lookup(nd->path.dentry))
2329 nd->path.mnt = NULL;
2330 nd->path.dentry = NULL;
2336 static int filename_lookup(int dfd, struct filename *name, unsigned flags,
2337 struct path *path, struct path *root)
2340 struct nameidata nd;
2342 return PTR_ERR(name);
2343 if (unlikely(root)) {
2345 flags |= LOOKUP_ROOT;
2347 set_nameidata(&nd, dfd, name);
2348 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
2349 if (unlikely(retval == -ECHILD))
2350 retval = path_lookupat(&nd, flags, path);
2351 if (unlikely(retval == -ESTALE))
2352 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
2354 if (likely(!retval))
2355 audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
2356 restore_nameidata();
2361 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2362 static int path_parentat(struct nameidata *nd, unsigned flags,
2363 struct path *parent)
2365 const char *s = path_init(nd, flags);
2366 int err = link_path_walk(s, nd);
2368 err = complete_walk(nd);
2371 nd->path.mnt = NULL;
2372 nd->path.dentry = NULL;
2378 static struct filename *filename_parentat(int dfd, struct filename *name,
2379 unsigned int flags, struct path *parent,
2380 struct qstr *last, int *type)
2383 struct nameidata nd;
2387 set_nameidata(&nd, dfd, name);
2388 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
2389 if (unlikely(retval == -ECHILD))
2390 retval = path_parentat(&nd, flags, parent);
2391 if (unlikely(retval == -ESTALE))
2392 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
2393 if (likely(!retval)) {
2395 *type = nd.last_type;
2396 audit_inode(name, parent->dentry, LOOKUP_PARENT);
2399 name = ERR_PTR(retval);
2401 restore_nameidata();
2405 /* does lookup, returns the object with parent locked */
2406 struct dentry *kern_path_locked(const char *name, struct path *path)
2408 struct filename *filename;
2413 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2415 if (IS_ERR(filename))
2416 return ERR_CAST(filename);
2417 if (unlikely(type != LAST_NORM)) {
2420 return ERR_PTR(-EINVAL);
2422 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
2423 d = __lookup_hash(&last, path->dentry, 0);
2425 inode_unlock(path->dentry->d_inode);
2432 int kern_path(const char *name, unsigned int flags, struct path *path)
2434 return filename_lookup(AT_FDCWD, getname_kernel(name),
2437 EXPORT_SYMBOL(kern_path);
2440 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2441 * @dentry: pointer to dentry of the base directory
2442 * @mnt: pointer to vfs mount of the base directory
2443 * @name: pointer to file name
2444 * @flags: lookup flags
2445 * @path: pointer to struct path to fill
2447 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2448 const char *name, unsigned int flags,
2451 struct path root = {.mnt = mnt, .dentry = dentry};
2452 /* the first argument of filename_lookup() is ignored with root */
2453 return filename_lookup(AT_FDCWD, getname_kernel(name),
2454 flags , path, &root);
2456 EXPORT_SYMBOL(vfs_path_lookup);
2458 static int lookup_one_len_common(const char *name, struct dentry *base,
2459 int len, struct qstr *this)
2463 this->hash = full_name_hash(base, name, len);
2467 if (unlikely(name[0] == '.')) {
2468 if (len < 2 || (len == 2 && name[1] == '.'))
2473 unsigned int c = *(const unsigned char *)name++;
2474 if (c == '/' || c == '\0')
2478 * See if the low-level filesystem might want
2479 * to use its own hash..
2481 if (base->d_flags & DCACHE_OP_HASH) {
2482 int err = base->d_op->d_hash(base, this);
2487 return inode_permission(base->d_inode, MAY_EXEC);
2491 * try_lookup_one_len - filesystem helper to lookup single pathname component
2492 * @name: pathname component to lookup
2493 * @base: base directory to lookup from
2494 * @len: maximum length @len should be interpreted to
2496 * Look up a dentry by name in the dcache, returning NULL if it does not
2497 * currently exist. The function does not try to create a dentry.
2499 * Note that this routine is purely a helper for filesystem usage and should
2500 * not be called by generic code.
2502 * The caller must hold base->i_mutex.
2504 struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
2509 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2511 err = lookup_one_len_common(name, base, len, &this);
2513 return ERR_PTR(err);
2515 return lookup_dcache(&this, base, 0);
2517 EXPORT_SYMBOL(try_lookup_one_len);
2520 * lookup_one_len - filesystem helper to lookup single pathname component
2521 * @name: pathname component to lookup
2522 * @base: base directory to lookup from
2523 * @len: maximum length @len should be interpreted to
2525 * Note that this routine is purely a helper for filesystem usage and should
2526 * not be called by generic code.
2528 * The caller must hold base->i_mutex.
2530 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2532 struct dentry *dentry;
2536 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2538 err = lookup_one_len_common(name, base, len, &this);
2540 return ERR_PTR(err);
2542 dentry = lookup_dcache(&this, base, 0);
2543 return dentry ? dentry : __lookup_slow(&this, base, 0);
2545 EXPORT_SYMBOL(lookup_one_len);
2548 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2549 * @name: pathname component to lookup
2550 * @base: base directory to lookup from
2551 * @len: maximum length @len should be interpreted to
2553 * Note that this routine is purely a helper for filesystem usage and should
2554 * not be called by generic code.
2556 * Unlike lookup_one_len, it should be called without the parent
2557 * i_mutex held, and will take the i_mutex itself if necessary.
2559 struct dentry *lookup_one_len_unlocked(const char *name,
2560 struct dentry *base, int len)
2566 err = lookup_one_len_common(name, base, len, &this);
2568 return ERR_PTR(err);
2570 ret = lookup_dcache(&this, base, 0);
2572 ret = lookup_slow(&this, base, 0);
2575 EXPORT_SYMBOL(lookup_one_len_unlocked);
2577 #ifdef CONFIG_UNIX98_PTYS
2578 int path_pts(struct path *path)
2580 /* Find something mounted on "pts" in the same directory as
2583 struct dentry *child, *parent;
2587 ret = path_parent_directory(path);
2591 parent = path->dentry;
2594 child = d_hash_and_lookup(parent, &this);
2598 path->dentry = child;
2605 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2606 struct path *path, int *empty)
2608 return filename_lookup(dfd, getname_flags(name, flags, empty),
2611 EXPORT_SYMBOL(user_path_at_empty);
2614 * mountpoint_last - look up last component for umount
2615 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2617 * This is a special lookup_last function just for umount. In this case, we
2618 * need to resolve the path without doing any revalidation.
2620 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2621 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2622 * in almost all cases, this lookup will be served out of the dcache. The only
2623 * cases where it won't are if nd->last refers to a symlink or the path is
2624 * bogus and it doesn't exist.
2627 * -error: if there was an error during lookup. This includes -ENOENT if the
2628 * lookup found a negative dentry.
2630 * 0: if we successfully resolved nd->last and found it to not to be a
2631 * symlink that needs to be followed.
2633 * 1: if we successfully resolved nd->last and found it to be a symlink
2634 * that needs to be followed.
2637 mountpoint_last(struct nameidata *nd)
2640 struct dentry *dir = nd->path.dentry;
2643 /* If we're in rcuwalk, drop out of it to handle last component */
2644 if (nd->flags & LOOKUP_RCU) {
2645 if (unlazy_walk(nd))
2649 nd->flags &= ~LOOKUP_PARENT;
2651 if (unlikely(nd->last_type != LAST_NORM)) {
2652 error = handle_dots(nd, nd->last_type);
2655 path.dentry = dget(nd->path.dentry);
2657 path.dentry = d_lookup(dir, &nd->last);
2660 * No cached dentry. Mounted dentries are pinned in the
2661 * cache, so that means that this dentry is probably
2662 * a symlink or the path doesn't actually point
2663 * to a mounted dentry.
2665 path.dentry = lookup_slow(&nd->last, dir,
2666 nd->flags | LOOKUP_NO_REVAL);
2667 if (IS_ERR(path.dentry))
2668 return PTR_ERR(path.dentry);
2671 if (d_is_negative(path.dentry)) {
2675 path.mnt = nd->path.mnt;
2676 return step_into(nd, &path, 0, d_backing_inode(path.dentry), 0);
2680 * path_mountpoint - look up a path to be umounted
2681 * @nd: lookup context
2682 * @flags: lookup flags
2683 * @path: pointer to container for result
2685 * Look up the given name, but don't attempt to revalidate the last component.
2686 * Returns 0 and "path" will be valid on success; Returns error otherwise.
2689 path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
2691 const char *s = path_init(nd, flags);
2694 while (!(err = link_path_walk(s, nd)) &&
2695 (err = mountpoint_last(nd)) > 0) {
2696 s = trailing_symlink(nd);
2700 nd->path.mnt = NULL;
2701 nd->path.dentry = NULL;
2709 filename_mountpoint(int dfd, struct filename *name, struct path *path,
2712 struct nameidata nd;
2715 return PTR_ERR(name);
2716 set_nameidata(&nd, dfd, name);
2717 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
2718 if (unlikely(error == -ECHILD))
2719 error = path_mountpoint(&nd, flags, path);
2720 if (unlikely(error == -ESTALE))
2721 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
2723 audit_inode(name, path->dentry, 0);
2724 restore_nameidata();
2730 * user_path_mountpoint_at - lookup a path from userland in order to umount it
2731 * @dfd: directory file descriptor
2732 * @name: pathname from userland
2733 * @flags: lookup flags
2734 * @path: pointer to container to hold result
2736 * A umount is a special case for path walking. We're not actually interested
2737 * in the inode in this situation, and ESTALE errors can be a problem. We
2738 * simply want track down the dentry and vfsmount attached at the mountpoint
2739 * and avoid revalidating the last component.
2741 * Returns 0 and populates "path" on success.
2744 user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
2747 return filename_mountpoint(dfd, getname(name), path, flags);
2751 kern_path_mountpoint(int dfd, const char *name, struct path *path,
2754 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
2756 EXPORT_SYMBOL(kern_path_mountpoint);
2758 int __check_sticky(struct inode *dir, struct inode *inode)
2760 kuid_t fsuid = current_fsuid();
2762 if (uid_eq(inode->i_uid, fsuid))
2764 if (uid_eq(dir->i_uid, fsuid))
2766 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
2768 EXPORT_SYMBOL(__check_sticky);
2771 * Check whether we can remove a link victim from directory dir, check
2772 * whether the type of victim is right.
2773 * 1. We can't do it if dir is read-only (done in permission())
2774 * 2. We should have write and exec permissions on dir
2775 * 3. We can't remove anything from append-only dir
2776 * 4. We can't do anything with immutable dir (done in permission())
2777 * 5. If the sticky bit on dir is set we should either
2778 * a. be owner of dir, or
2779 * b. be owner of victim, or
2780 * c. have CAP_FOWNER capability
2781 * 6. If the victim is append-only or immutable we can't do antyhing with
2782 * links pointing to it.
2783 * 7. If the victim has an unknown uid or gid we can't change the inode.
2784 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2785 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2786 * 10. We can't remove a root or mountpoint.
2787 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
2788 * nfs_async_unlink().
2790 static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
2792 struct inode *inode = d_backing_inode(victim);
2795 if (d_is_negative(victim))
2799 BUG_ON(victim->d_parent->d_inode != dir);
2801 /* Inode writeback is not safe when the uid or gid are invalid. */
2802 if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
2805 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
2807 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
2813 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2814 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
2817 if (!d_is_dir(victim))
2819 if (IS_ROOT(victim))
2821 } else if (d_is_dir(victim))
2823 if (IS_DEADDIR(dir))
2825 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2830 /* Check whether we can create an object with dentry child in directory
2832 * 1. We can't do it if child already exists (open has special treatment for
2833 * this case, but since we are inlined it's OK)
2834 * 2. We can't do it if dir is read-only (done in permission())
2835 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2836 * 4. We should have write and exec permissions on dir
2837 * 5. We can't do it if dir is immutable (done in permission())
2839 static inline int may_create(struct inode *dir, struct dentry *child)
2841 struct user_namespace *s_user_ns;
2842 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
2845 if (IS_DEADDIR(dir))
2847 s_user_ns = dir->i_sb->s_user_ns;
2848 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2849 !kgid_has_mapping(s_user_ns, current_fsgid()))
2851 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2855 * p1 and p2 should be directories on the same fs.
2857 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2862 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2866 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
2868 p = d_ancestor(p2, p1);
2870 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2871 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
2875 p = d_ancestor(p1, p2);
2877 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2878 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
2882 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2883 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
2886 EXPORT_SYMBOL(lock_rename);
2888 void unlock_rename(struct dentry *p1, struct dentry *p2)
2890 inode_unlock(p1->d_inode);
2892 inode_unlock(p2->d_inode);
2893 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
2896 EXPORT_SYMBOL(unlock_rename);
2898 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2901 int error = may_create(dir, dentry);
2905 if (!dir->i_op->create)
2906 return -EACCES; /* shouldn't it be ENOSYS? */
2909 error = security_inode_create(dir, dentry, mode);
2912 error = dir->i_op->create(dir, dentry, mode, want_excl);
2914 fsnotify_create(dir, dentry);
2917 EXPORT_SYMBOL(vfs_create);
2919 int vfs_mkobj(struct dentry *dentry, umode_t mode,
2920 int (*f)(struct dentry *, umode_t, void *),
2923 struct inode *dir = dentry->d_parent->d_inode;
2924 int error = may_create(dir, dentry);
2930 error = security_inode_create(dir, dentry, mode);
2933 error = f(dentry, mode, arg);
2935 fsnotify_create(dir, dentry);
2938 EXPORT_SYMBOL(vfs_mkobj);
2940 bool may_open_dev(const struct path *path)
2942 return !(path->mnt->mnt_flags & MNT_NODEV) &&
2943 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2946 static int may_open(const struct path *path, int acc_mode, int flag)
2948 struct dentry *dentry = path->dentry;
2949 struct inode *inode = dentry->d_inode;
2955 switch (inode->i_mode & S_IFMT) {
2959 if (acc_mode & MAY_WRITE)
2964 if (!may_open_dev(path))
2973 error = inode_permission(inode, MAY_OPEN | acc_mode);
2978 * An append-only file must be opened in append mode for writing.
2980 if (IS_APPEND(inode)) {
2981 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2987 /* O_NOATIME can only be set by the owner or superuser */
2988 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2994 static int handle_truncate(struct file *filp)
2996 const struct path *path = &filp->f_path;
2997 struct inode *inode = path->dentry->d_inode;
2998 int error = get_write_access(inode);
3002 * Refuse to truncate files with mandatory locks held on them.
3004 error = locks_verify_locked(filp);
3006 error = security_path_truncate(path);
3008 error = do_truncate(path->dentry, 0,
3009 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
3012 put_write_access(inode);
3016 static inline int open_to_namei_flags(int flag)
3018 if ((flag & O_ACCMODE) == 3)
3023 static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
3025 struct user_namespace *s_user_ns;
3026 int error = security_path_mknod(dir, dentry, mode, 0);
3030 s_user_ns = dir->dentry->d_sb->s_user_ns;
3031 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
3032 !kgid_has_mapping(s_user_ns, current_fsgid()))
3035 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
3039 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3043 * Attempt to atomically look up, create and open a file from a negative
3046 * Returns 0 if successful. The file will have been created and attached to
3047 * @file by the filesystem calling finish_open().
3049 * If the file was looked up only or didn't need creating, FMODE_OPENED won't
3050 * be set. The caller will need to perform the open themselves. @path will
3051 * have been updated to point to the new dentry. This may be negative.
3053 * Returns an error code otherwise.
3055 static int atomic_open(struct nameidata *nd, struct dentry *dentry,
3056 struct path *path, struct file *file,
3057 const struct open_flags *op,
3058 int open_flag, umode_t mode)
3060 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
3061 struct inode *dir = nd->path.dentry->d_inode;
3064 if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
3065 open_flag &= ~O_TRUNC;
3067 if (nd->flags & LOOKUP_DIRECTORY)
3068 open_flag |= O_DIRECTORY;
3070 file->f_path.dentry = DENTRY_NOT_SET;
3071 file->f_path.mnt = nd->path.mnt;
3072 error = dir->i_op->atomic_open(dir, dentry, file,
3073 open_to_namei_flags(open_flag), mode);
3074 d_lookup_done(dentry);
3076 if (file->f_mode & FMODE_OPENED) {
3078 * We didn't have the inode before the open, so check open
3081 int acc_mode = op->acc_mode;
3082 if (file->f_mode & FMODE_CREATED) {
3083 WARN_ON(!(open_flag & O_CREAT));
3084 fsnotify_create(dir, dentry);
3087 error = may_open(&file->f_path, acc_mode, open_flag);
3088 if (WARN_ON(error > 0))
3090 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
3093 if (file->f_path.dentry) {
3095 dentry = file->f_path.dentry;
3097 if (file->f_mode & FMODE_CREATED)
3098 fsnotify_create(dir, dentry);
3099 if (unlikely(d_is_negative(dentry))) {
3102 path->dentry = dentry;
3103 path->mnt = nd->path.mnt;
3113 * Look up and maybe create and open the last component.
3115 * Must be called with parent locked (exclusive in O_CREAT case).
3117 * Returns 0 on success, that is, if
3118 * the file was successfully atomically created (if necessary) and opened, or
3119 * the file was not completely opened at this time, though lookups and
3120 * creations were performed.
3121 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
3122 * In the latter case dentry returned in @path might be negative if O_CREAT
3123 * hadn't been specified.
3125 * An error code is returned on failure.
3127 static int lookup_open(struct nameidata *nd, struct path *path,
3129 const struct open_flags *op,
3132 struct dentry *dir = nd->path.dentry;
3133 struct inode *dir_inode = dir->d_inode;
3134 int open_flag = op->open_flag;
3135 struct dentry *dentry;
3136 int error, create_error = 0;
3137 umode_t mode = op->mode;
3138 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
3140 if (unlikely(IS_DEADDIR(dir_inode)))
3143 file->f_mode &= ~FMODE_CREATED;
3144 dentry = d_lookup(dir, &nd->last);
3147 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3149 return PTR_ERR(dentry);
3151 if (d_in_lookup(dentry))
3154 error = d_revalidate(dentry, nd->flags);
3155 if (likely(error > 0))
3159 d_invalidate(dentry);
3163 if (dentry->d_inode) {
3164 /* Cached positive dentry: will open in f_op->open */
3169 * Checking write permission is tricky, bacuse we don't know if we are
3170 * going to actually need it: O_CREAT opens should work as long as the
3171 * file exists. But checking existence breaks atomicity. The trick is
3172 * to check access and if not granted clear O_CREAT from the flags.
3174 * Another problem is returing the "right" error value (e.g. for an
3175 * O_EXCL open we want to return EEXIST not EROFS).
3177 if (open_flag & O_CREAT) {
3178 if (!IS_POSIXACL(dir->d_inode))
3179 mode &= ~current_umask();
3180 if (unlikely(!got_write)) {
3181 create_error = -EROFS;
3182 open_flag &= ~O_CREAT;
3183 if (open_flag & (O_EXCL | O_TRUNC))
3185 /* No side effects, safe to clear O_CREAT */
3187 create_error = may_o_create(&nd->path, dentry, mode);
3189 open_flag &= ~O_CREAT;
3190 if (open_flag & O_EXCL)
3194 } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
3195 unlikely(!got_write)) {
3197 * No O_CREATE -> atomicity not a requirement -> fall
3198 * back to lookup + open
3203 if (dir_inode->i_op->atomic_open) {
3204 error = atomic_open(nd, dentry, path, file, op, open_flag,
3206 if (unlikely(error == -ENOENT) && create_error)
3207 error = create_error;
3212 if (d_in_lookup(dentry)) {
3213 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3215 d_lookup_done(dentry);
3216 if (unlikely(res)) {
3218 error = PTR_ERR(res);
3226 /* Negative dentry, just create the file */
3227 if (!dentry->d_inode && (open_flag & O_CREAT)) {
3228 file->f_mode |= FMODE_CREATED;
3229 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
3230 if (!dir_inode->i_op->create) {
3234 error = dir_inode->i_op->create(dir_inode, dentry, mode,
3235 open_flag & O_EXCL);
3238 fsnotify_create(dir_inode, dentry);
3240 if (unlikely(create_error) && !dentry->d_inode) {
3241 error = create_error;
3245 path->dentry = dentry;
3246 path->mnt = nd->path.mnt;
3255 * Handle the last step of open()
3257 static int do_last(struct nameidata *nd,
3258 struct file *file, const struct open_flags *op)
3260 struct dentry *dir = nd->path.dentry;
3261 int open_flag = op->open_flag;
3262 bool will_truncate = (open_flag & O_TRUNC) != 0;
3263 bool got_write = false;
3264 int acc_mode = op->acc_mode;
3266 struct inode *inode;
3270 nd->flags &= ~LOOKUP_PARENT;
3271 nd->flags |= op->intent;
3273 if (nd->last_type != LAST_NORM) {
3274 error = handle_dots(nd, nd->last_type);
3275 if (unlikely(error))
3280 if (!(open_flag & O_CREAT)) {
3281 if (nd->last.name[nd->last.len])
3282 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3283 /* we _can_ be in RCU mode here */
3284 error = lookup_fast(nd, &path, &inode, &seq);
3285 if (likely(error > 0))
3291 BUG_ON(nd->inode != dir->d_inode);
3292 BUG_ON(nd->flags & LOOKUP_RCU);
3294 /* create side of things */
3296 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3297 * has been cleared when we got to the last component we are
3300 error = complete_walk(nd);
3304 audit_inode(nd->name, dir, LOOKUP_PARENT);
3305 /* trailing slashes? */
3306 if (unlikely(nd->last.name[nd->last.len]))
3310 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
3311 error = mnt_want_write(nd->path.mnt);
3315 * do _not_ fail yet - we might not need that or fail with
3316 * a different error; let lookup_open() decide; we'll be
3317 * dropping this one anyway.
3320 if (open_flag & O_CREAT)
3321 inode_lock(dir->d_inode);
3323 inode_lock_shared(dir->d_inode);
3324 error = lookup_open(nd, &path, file, op, got_write);
3325 if (open_flag & O_CREAT)
3326 inode_unlock(dir->d_inode);
3328 inode_unlock_shared(dir->d_inode);
3333 if (file->f_mode & FMODE_OPENED) {
3334 if ((file->f_mode & FMODE_CREATED) ||
3335 !S_ISREG(file_inode(file)->i_mode))
3336 will_truncate = false;
3338 audit_inode(nd->name, file->f_path.dentry, 0);
3342 if (file->f_mode & FMODE_CREATED) {
3343 /* Don't check for write permission, don't truncate */
3344 open_flag &= ~O_TRUNC;
3345 will_truncate = false;
3347 path_to_nameidata(&path, nd);
3348 goto finish_open_created;
3352 * If atomic_open() acquired write access it is dropped now due to
3353 * possible mount and symlink following (this might be optimized away if
3357 mnt_drop_write(nd->path.mnt);
3361 error = follow_managed(&path, nd);
3362 if (unlikely(error < 0))
3365 if (unlikely(d_is_negative(path.dentry))) {
3366 path_to_nameidata(&path, nd);
3371 * create/update audit record if it already exists.
3373 audit_inode(nd->name, path.dentry, 0);
3375 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3376 path_to_nameidata(&path, nd);
3380 seq = 0; /* out of RCU mode, so the value doesn't matter */
3381 inode = d_backing_inode(path.dentry);
3383 error = step_into(nd, &path, 0, inode, seq);
3384 if (unlikely(error))
3387 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
3388 error = complete_walk(nd);
3391 audit_inode(nd->name, nd->path.dentry, 0);
3392 if (open_flag & O_CREAT) {
3394 if (d_is_dir(nd->path.dentry))
3396 error = may_create_in_sticky(dir,
3397 d_backing_inode(nd->path.dentry));
3398 if (unlikely(error))
3402 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
3404 if (!d_is_reg(nd->path.dentry))
3405 will_truncate = false;
3407 if (will_truncate) {
3408 error = mnt_want_write(nd->path.mnt);
3413 finish_open_created:
3414 error = may_open(&nd->path, acc_mode, open_flag);
3417 BUG_ON(file->f_mode & FMODE_OPENED); /* once it's opened, it's opened */
3418 error = vfs_open(&nd->path, file);
3422 error = ima_file_check(file, op->acc_mode);
3423 if (!error && will_truncate)
3424 error = handle_truncate(file);
3426 if (unlikely(error > 0)) {
3431 mnt_drop_write(nd->path.mnt);
3435 struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag)
3437 struct dentry *child = NULL;
3438 struct inode *dir = dentry->d_inode;
3439 struct inode *inode;
3442 /* we want directory to be writable */
3443 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
3446 error = -EOPNOTSUPP;
3447 if (!dir->i_op->tmpfile)
3450 child = d_alloc(dentry, &slash_name);
3451 if (unlikely(!child))
3453 error = dir->i_op->tmpfile(dir, child, mode);
3457 inode = child->d_inode;
3458 if (unlikely(!inode))
3460 if (!(open_flag & O_EXCL)) {
3461 spin_lock(&inode->i_lock);
3462 inode->i_state |= I_LINKABLE;
3463 spin_unlock(&inode->i_lock);
3469 return ERR_PTR(error);
3471 EXPORT_SYMBOL(vfs_tmpfile);
3473 static int do_tmpfile(struct nameidata *nd, unsigned flags,
3474 const struct open_flags *op,
3477 struct dentry *child;
3479 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
3480 if (unlikely(error))
3482 error = mnt_want_write(path.mnt);
3483 if (unlikely(error))
3485 child = vfs_tmpfile(path.dentry, op->mode, op->open_flag);
3486 error = PTR_ERR(child);
3490 path.dentry = child;
3491 audit_inode(nd->name, child, 0);
3492 /* Don't check for other permissions, the inode was just created */
3493 error = may_open(&path, 0, op->open_flag);
3496 file->f_path.mnt = path.mnt;
3497 error = finish_open(file, child, NULL);
3499 mnt_drop_write(path.mnt);
3505 static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3508 int error = path_lookupat(nd, flags, &path);
3510 audit_inode(nd->name, path.dentry, 0);
3511 error = vfs_open(&path, file);
3517 static struct file *path_openat(struct nameidata *nd,
3518 const struct open_flags *op, unsigned flags)
3523 file = alloc_empty_file(op->open_flag, current_cred());
3527 if (unlikely(file->f_flags & __O_TMPFILE)) {
3528 error = do_tmpfile(nd, flags, op, file);
3529 } else if (unlikely(file->f_flags & O_PATH)) {
3530 error = do_o_path(nd, flags, file);
3532 const char *s = path_init(nd, flags);
3533 while (!(error = link_path_walk(s, nd)) &&
3534 (error = do_last(nd, file, op)) > 0) {
3535 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3536 s = trailing_symlink(nd);
3540 if (likely(!error)) {
3541 if (likely(file->f_mode & FMODE_OPENED))
3547 if (error == -EOPENSTALE) {
3548 if (flags & LOOKUP_RCU)
3553 return ERR_PTR(error);
3556 struct file *do_filp_open(int dfd, struct filename *pathname,
3557 const struct open_flags *op)
3559 struct nameidata nd;
3560 int flags = op->lookup_flags;
3563 set_nameidata(&nd, dfd, pathname);
3564 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
3565 if (unlikely(filp == ERR_PTR(-ECHILD)))
3566 filp = path_openat(&nd, op, flags);
3567 if (unlikely(filp == ERR_PTR(-ESTALE)))
3568 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
3569 restore_nameidata();
3573 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3574 const char *name, const struct open_flags *op)
3576 struct nameidata nd;
3578 struct filename *filename;
3579 int flags = op->lookup_flags | LOOKUP_ROOT;
3582 nd.root.dentry = dentry;
3584 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
3585 return ERR_PTR(-ELOOP);
3587 filename = getname_kernel(name);
3588 if (IS_ERR(filename))
3589 return ERR_CAST(filename);
3591 set_nameidata(&nd, -1, filename);
3592 file = path_openat(&nd, op, flags | LOOKUP_RCU);
3593 if (unlikely(file == ERR_PTR(-ECHILD)))
3594 file = path_openat(&nd, op, flags);
3595 if (unlikely(file == ERR_PTR(-ESTALE)))
3596 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
3597 restore_nameidata();
3602 static struct dentry *filename_create(int dfd, struct filename *name,
3603 struct path *path, unsigned int lookup_flags)
3605 struct dentry *dentry = ERR_PTR(-EEXIST);
3610 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3613 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3614 * other flags passed in are ignored!
3616 lookup_flags &= LOOKUP_REVAL;
3618 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3620 return ERR_CAST(name);
3623 * Yucky last component or no last component at all?
3624 * (foo/., foo/.., /////)
3626 if (unlikely(type != LAST_NORM))
3629 /* don't fail immediately if it's r/o, at least try to report other errors */
3630 err2 = mnt_want_write(path->mnt);
3632 * Do the final lookup.
3634 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3635 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
3636 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
3641 if (d_is_positive(dentry))
3645 * Special case - lookup gave negative, but... we had foo/bar/
3646 * From the vfs_mknod() POV we just have a negative dentry -
3647 * all is fine. Let's be bastards - you had / on the end, you've
3648 * been asking for (non-existent) directory. -ENOENT for you.
3650 if (unlikely(!is_dir && last.name[last.len])) {
3654 if (unlikely(err2)) {
3662 dentry = ERR_PTR(error);
3664 inode_unlock(path->dentry->d_inode);
3666 mnt_drop_write(path->mnt);
3673 struct dentry *kern_path_create(int dfd, const char *pathname,
3674 struct path *path, unsigned int lookup_flags)
3676 return filename_create(dfd, getname_kernel(pathname),
3677 path, lookup_flags);
3679 EXPORT_SYMBOL(kern_path_create);
3681 void done_path_create(struct path *path, struct dentry *dentry)
3684 inode_unlock(path->dentry->d_inode);
3685 mnt_drop_write(path->mnt);
3688 EXPORT_SYMBOL(done_path_create);
3690 inline struct dentry *user_path_create(int dfd, const char __user *pathname,
3691 struct path *path, unsigned int lookup_flags)
3693 return filename_create(dfd, getname(pathname), path, lookup_flags);
3695 EXPORT_SYMBOL(user_path_create);
3697 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3699 int error = may_create(dir, dentry);
3704 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
3707 if (!dir->i_op->mknod)
3710 error = devcgroup_inode_mknod(mode, dev);
3714 error = security_inode_mknod(dir, dentry, mode, dev);
3718 error = dir->i_op->mknod(dir, dentry, mode, dev);
3720 fsnotify_create(dir, dentry);
3723 EXPORT_SYMBOL(vfs_mknod);
3725 static int may_mknod(umode_t mode)
3727 switch (mode & S_IFMT) {
3733 case 0: /* zero mode translates to S_IFREG */
3742 long do_mknodat(int dfd, const char __user *filename, umode_t mode,
3745 struct dentry *dentry;
3748 unsigned int lookup_flags = 0;
3750 error = may_mknod(mode);
3754 dentry = user_path_create(dfd, filename, &path, lookup_flags);
3756 return PTR_ERR(dentry);
3758 if (!IS_POSIXACL(path.dentry->d_inode))
3759 mode &= ~current_umask();
3760 error = security_path_mknod(&path, dentry, mode, dev);
3763 switch (mode & S_IFMT) {
3764 case 0: case S_IFREG:
3765 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
3767 ima_post_path_mknod(dentry);
3769 case S_IFCHR: case S_IFBLK:
3770 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
3771 new_decode_dev(dev));
3773 case S_IFIFO: case S_IFSOCK:
3774 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
3778 done_path_create(&path, dentry);
3779 if (retry_estale(error, lookup_flags)) {
3780 lookup_flags |= LOOKUP_REVAL;
3786 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3789 return do_mknodat(dfd, filename, mode, dev);
3792 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
3794 return do_mknodat(AT_FDCWD, filename, mode, dev);
3797 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3799 int error = may_create(dir, dentry);
3800 unsigned max_links = dir->i_sb->s_max_links;
3805 if (!dir->i_op->mkdir)
3808 mode &= (S_IRWXUGO|S_ISVTX);
3809 error = security_inode_mkdir(dir, dentry, mode);
3813 if (max_links && dir->i_nlink >= max_links)
3816 error = dir->i_op->mkdir(dir, dentry, mode);
3818 fsnotify_mkdir(dir, dentry);
3821 EXPORT_SYMBOL(vfs_mkdir);
3823 long do_mkdirat(int dfd, const char __user *pathname, umode_t mode)
3825 struct dentry *dentry;
3828 unsigned int lookup_flags = LOOKUP_DIRECTORY;
3831 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
3833 return PTR_ERR(dentry);
3835 if (!IS_POSIXACL(path.dentry->d_inode))
3836 mode &= ~current_umask();
3837 error = security_path_mkdir(&path, dentry, mode);
3839 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3840 done_path_create(&path, dentry);
3841 if (retry_estale(error, lookup_flags)) {
3842 lookup_flags |= LOOKUP_REVAL;
3848 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3850 return do_mkdirat(dfd, pathname, mode);
3853 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
3855 return do_mkdirat(AT_FDCWD, pathname, mode);
3858 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3860 int error = may_delete(dir, dentry, 1);
3865 if (!dir->i_op->rmdir)
3869 inode_lock(dentry->d_inode);
3872 if (is_local_mountpoint(dentry))
3875 error = security_inode_rmdir(dir, dentry);
3879 error = dir->i_op->rmdir(dir, dentry);
3883 shrink_dcache_parent(dentry);
3884 dentry->d_inode->i_flags |= S_DEAD;
3886 detach_mounts(dentry);
3889 inode_unlock(dentry->d_inode);
3895 EXPORT_SYMBOL(vfs_rmdir);
3897 long do_rmdir(int dfd, const char __user *pathname)
3900 struct filename *name;
3901 struct dentry *dentry;
3905 unsigned int lookup_flags = 0;
3907 name = filename_parentat(dfd, getname(pathname), lookup_flags,
3908 &path, &last, &type);
3910 return PTR_ERR(name);
3924 error = mnt_want_write(path.mnt);
3928 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
3929 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
3930 error = PTR_ERR(dentry);
3933 if (!dentry->d_inode) {
3937 error = security_path_rmdir(&path, dentry);
3940 error = vfs_rmdir(path.dentry->d_inode, dentry);
3944 inode_unlock(path.dentry->d_inode);
3945 mnt_drop_write(path.mnt);
3949 if (retry_estale(error, lookup_flags)) {
3950 lookup_flags |= LOOKUP_REVAL;
3956 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
3958 return do_rmdir(AT_FDCWD, pathname);
3962 * vfs_unlink - unlink a filesystem object
3963 * @dir: parent directory
3965 * @delegated_inode: returns victim inode, if the inode is delegated.
3967 * The caller must hold dir->i_mutex.
3969 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3970 * return a reference to the inode in delegated_inode. The caller
3971 * should then break the delegation on that inode and retry. Because
3972 * breaking a delegation may take a long time, the caller should drop
3973 * dir->i_mutex before doing so.
3975 * Alternatively, a caller may pass NULL for delegated_inode. This may
3976 * be appropriate for callers that expect the underlying filesystem not
3977 * to be NFS exported.
3979 int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
3981 struct inode *target = dentry->d_inode;
3982 int error = may_delete(dir, dentry, 0);
3987 if (!dir->i_op->unlink)
3991 if (is_local_mountpoint(dentry))
3994 error = security_inode_unlink(dir, dentry);
3996 error = try_break_deleg(target, delegated_inode);
3999 error = dir->i_op->unlink(dir, dentry);
4002 detach_mounts(dentry);
4007 inode_unlock(target);
4009 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
4010 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
4011 fsnotify_link_count(target);
4017 EXPORT_SYMBOL(vfs_unlink);
4020 * Make sure that the actual truncation of the file will occur outside its
4021 * directory's i_mutex. Truncate can take a long time if there is a lot of
4022 * writeout happening, and we don't want to prevent access to the directory
4023 * while waiting on the I/O.
4025 long do_unlinkat(int dfd, struct filename *name)
4028 struct dentry *dentry;
4032 struct inode *inode = NULL;
4033 struct inode *delegated_inode = NULL;
4034 unsigned int lookup_flags = 0;
4036 name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
4038 return PTR_ERR(name);
4041 if (type != LAST_NORM)
4044 error = mnt_want_write(path.mnt);
4048 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
4049 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
4050 error = PTR_ERR(dentry);
4051 if (!IS_ERR(dentry)) {
4052 /* Why not before? Because we want correct error value */
4053 if (last.name[last.len])
4055 inode = dentry->d_inode;
4056 if (d_is_negative(dentry))
4059 error = security_path_unlink(&path, dentry);
4062 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
4066 inode_unlock(path.dentry->d_inode);
4068 iput(inode); /* truncate the inode here */
4070 if (delegated_inode) {
4071 error = break_deleg_wait(&delegated_inode);
4075 mnt_drop_write(path.mnt);
4078 if (retry_estale(error, lookup_flags)) {
4079 lookup_flags |= LOOKUP_REVAL;
4087 if (d_is_negative(dentry))
4089 else if (d_is_dir(dentry))
4096 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
4098 if ((flag & ~AT_REMOVEDIR) != 0)
4101 if (flag & AT_REMOVEDIR)
4102 return do_rmdir(dfd, pathname);
4104 return do_unlinkat(dfd, getname(pathname));
4107 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
4109 return do_unlinkat(AT_FDCWD, getname(pathname));
4112 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
4114 int error = may_create(dir, dentry);
4119 if (!dir->i_op->symlink)
4122 error = security_inode_symlink(dir, dentry, oldname);
4126 error = dir->i_op->symlink(dir, dentry, oldname);
4128 fsnotify_create(dir, dentry);
4131 EXPORT_SYMBOL(vfs_symlink);
4133 long do_symlinkat(const char __user *oldname, int newdfd,
4134 const char __user *newname)
4137 struct filename *from;
4138 struct dentry *dentry;
4140 unsigned int lookup_flags = 0;
4142 from = getname(oldname);
4144 return PTR_ERR(from);
4146 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
4147 error = PTR_ERR(dentry);
4151 error = security_path_symlink(&path, dentry, from->name);
4153 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
4154 done_path_create(&path, dentry);
4155 if (retry_estale(error, lookup_flags)) {
4156 lookup_flags |= LOOKUP_REVAL;
4164 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4165 int, newdfd, const char __user *, newname)
4167 return do_symlinkat(oldname, newdfd, newname);
4170 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
4172 return do_symlinkat(oldname, AT_FDCWD, newname);
4176 * vfs_link - create a new link
4177 * @old_dentry: object to be linked
4179 * @new_dentry: where to create the new link
4180 * @delegated_inode: returns inode needing a delegation break
4182 * The caller must hold dir->i_mutex
4184 * If vfs_link discovers a delegation on the to-be-linked file in need
4185 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4186 * inode in delegated_inode. The caller should then break the delegation
4187 * and retry. Because breaking a delegation may take a long time, the
4188 * caller should drop the i_mutex before doing so.
4190 * Alternatively, a caller may pass NULL for delegated_inode. This may
4191 * be appropriate for callers that expect the underlying filesystem not
4192 * to be NFS exported.
4194 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
4196 struct inode *inode = old_dentry->d_inode;
4197 unsigned max_links = dir->i_sb->s_max_links;
4203 error = may_create(dir, new_dentry);
4207 if (dir->i_sb != inode->i_sb)
4211 * A link to an append-only or immutable file cannot be created.
4213 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4216 * Updating the link count will likely cause i_uid and i_gid to
4217 * be writen back improperly if their true value is unknown to
4220 if (HAS_UNMAPPED_ID(inode))
4222 if (!dir->i_op->link)
4224 if (S_ISDIR(inode->i_mode))
4227 error = security_inode_link(old_dentry, dir, new_dentry);
4232 /* Make sure we don't allow creating hardlink to an unlinked file */
4233 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
4235 else if (max_links && inode->i_nlink >= max_links)
4238 error = try_break_deleg(inode, delegated_inode);
4240 error = dir->i_op->link(old_dentry, dir, new_dentry);
4243 if (!error && (inode->i_state & I_LINKABLE)) {
4244 spin_lock(&inode->i_lock);
4245 inode->i_state &= ~I_LINKABLE;
4246 spin_unlock(&inode->i_lock);
4248 inode_unlock(inode);
4250 fsnotify_link(dir, inode, new_dentry);
4253 EXPORT_SYMBOL(vfs_link);
4256 * Hardlinks are often used in delicate situations. We avoid
4257 * security-related surprises by not following symlinks on the
4260 * We don't follow them on the oldname either to be compatible
4261 * with linux 2.0, and to avoid hard-linking to directories
4262 * and other special files. --ADM
4264 int do_linkat(int olddfd, const char __user *oldname, int newdfd,
4265 const char __user *newname, int flags)
4267 struct dentry *new_dentry;
4268 struct path old_path, new_path;
4269 struct inode *delegated_inode = NULL;
4273 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
4276 * To use null names we require CAP_DAC_READ_SEARCH
4277 * This ensures that not everyone will be able to create
4278 * handlink using the passed filedescriptor.
4280 if (flags & AT_EMPTY_PATH) {
4281 if (!capable(CAP_DAC_READ_SEARCH))
4286 if (flags & AT_SYMLINK_FOLLOW)
4287 how |= LOOKUP_FOLLOW;
4289 error = user_path_at(olddfd, oldname, how, &old_path);
4293 new_dentry = user_path_create(newdfd, newname, &new_path,
4294 (how & LOOKUP_REVAL));
4295 error = PTR_ERR(new_dentry);
4296 if (IS_ERR(new_dentry))
4300 if (old_path.mnt != new_path.mnt)
4302 error = may_linkat(&old_path);
4303 if (unlikely(error))
4305 error = security_path_link(old_path.dentry, &new_path, new_dentry);
4308 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
4310 done_path_create(&new_path, new_dentry);
4311 if (delegated_inode) {
4312 error = break_deleg_wait(&delegated_inode);
4314 path_put(&old_path);
4318 if (retry_estale(error, how)) {
4319 path_put(&old_path);
4320 how |= LOOKUP_REVAL;
4324 path_put(&old_path);
4329 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4330 int, newdfd, const char __user *, newname, int, flags)
4332 return do_linkat(olddfd, oldname, newdfd, newname, flags);
4335 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
4337 return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
4341 * vfs_rename - rename a filesystem object
4342 * @old_dir: parent of source
4343 * @old_dentry: source
4344 * @new_dir: parent of destination
4345 * @new_dentry: destination
4346 * @delegated_inode: returns an inode needing a delegation break
4347 * @flags: rename flags
4349 * The caller must hold multiple mutexes--see lock_rename()).
4351 * If vfs_rename discovers a delegation in need of breaking at either
4352 * the source or destination, it will return -EWOULDBLOCK and return a
4353 * reference to the inode in delegated_inode. The caller should then
4354 * break the delegation and retry. Because breaking a delegation may
4355 * take a long time, the caller should drop all locks before doing
4358 * Alternatively, a caller may pass NULL for delegated_inode. This may
4359 * be appropriate for callers that expect the underlying filesystem not
4360 * to be NFS exported.
4362 * The worst of all namespace operations - renaming directory. "Perverted"
4363 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4366 * a) we can get into loop creation.
4367 * b) race potential - two innocent renames can create a loop together.
4368 * That's where 4.4 screws up. Current fix: serialization on
4369 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
4371 * c) we have to lock _four_ objects - parents and victim (if it exists),
4372 * and source (if it is not a directory).
4373 * And that - after we got ->i_mutex on parents (until then we don't know
4374 * whether the target exists). Solution: try to be smart with locking
4375 * order for inodes. We rely on the fact that tree topology may change
4376 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
4377 * move will be locked. Thus we can rank directories by the tree
4378 * (ancestors first) and rank all non-directories after them.
4379 * That works since everybody except rename does "lock parent, lookup,
4380 * lock child" and rename is under ->s_vfs_rename_mutex.
4381 * HOWEVER, it relies on the assumption that any object with ->lookup()
4382 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4383 * we'd better make sure that there's no link(2) for them.
4384 * d) conversion from fhandle to dentry may come in the wrong moment - when
4385 * we are removing the target. Solution: we will have to grab ->i_mutex
4386 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4387 * ->i_mutex on parents, which works but leads to some truly excessive
4390 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4391 struct inode *new_dir, struct dentry *new_dentry,
4392 struct inode **delegated_inode, unsigned int flags)
4395 bool is_dir = d_is_dir(old_dentry);
4396 struct inode *source = old_dentry->d_inode;
4397 struct inode *target = new_dentry->d_inode;
4398 bool new_is_dir = false;
4399 unsigned max_links = new_dir->i_sb->s_max_links;
4400 struct name_snapshot old_name;
4402 if (source == target)
4405 error = may_delete(old_dir, old_dentry, is_dir);
4410 error = may_create(new_dir, new_dentry);
4412 new_is_dir = d_is_dir(new_dentry);
4414 if (!(flags & RENAME_EXCHANGE))
4415 error = may_delete(new_dir, new_dentry, is_dir);
4417 error = may_delete(new_dir, new_dentry, new_is_dir);
4422 if (!old_dir->i_op->rename)
4426 * If we are going to change the parent - check write permissions,
4427 * we'll need to flip '..'.
4429 if (new_dir != old_dir) {
4431 error = inode_permission(source, MAY_WRITE);
4435 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4436 error = inode_permission(target, MAY_WRITE);
4442 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4447 take_dentry_name_snapshot(&old_name, old_dentry);
4449 if (!is_dir || (flags & RENAME_EXCHANGE))
4450 lock_two_nondirectories(source, target);
4455 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
4458 if (max_links && new_dir != old_dir) {
4460 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
4462 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4463 old_dir->i_nlink >= max_links)
4467 error = try_break_deleg(source, delegated_inode);
4471 if (target && !new_is_dir) {
4472 error = try_break_deleg(target, delegated_inode);
4476 error = old_dir->i_op->rename(old_dir, old_dentry,
4477 new_dir, new_dentry, flags);
4481 if (!(flags & RENAME_EXCHANGE) && target) {
4483 shrink_dcache_parent(new_dentry);
4484 target->i_flags |= S_DEAD;
4486 dont_mount(new_dentry);
4487 detach_mounts(new_dentry);
4489 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4490 if (!(flags & RENAME_EXCHANGE))
4491 d_move(old_dentry, new_dentry);
4493 d_exchange(old_dentry, new_dentry);
4496 if (!is_dir || (flags & RENAME_EXCHANGE))
4497 unlock_two_nondirectories(source, target);
4499 inode_unlock(target);
4502 fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
4503 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4504 if (flags & RENAME_EXCHANGE) {
4505 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4506 new_is_dir, NULL, new_dentry);
4509 release_dentry_name_snapshot(&old_name);
4513 EXPORT_SYMBOL(vfs_rename);
4515 static int do_renameat2(int olddfd, const char __user *oldname, int newdfd,
4516 const char __user *newname, unsigned int flags)
4518 struct dentry *old_dentry, *new_dentry;
4519 struct dentry *trap;
4520 struct path old_path, new_path;
4521 struct qstr old_last, new_last;
4522 int old_type, new_type;
4523 struct inode *delegated_inode = NULL;
4524 struct filename *from;
4525 struct filename *to;
4526 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
4527 bool should_retry = false;
4530 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
4533 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4534 (flags & RENAME_EXCHANGE))
4537 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4540 if (flags & RENAME_EXCHANGE)
4544 from = filename_parentat(olddfd, getname(oldname), lookup_flags,
4545 &old_path, &old_last, &old_type);
4547 error = PTR_ERR(from);
4551 to = filename_parentat(newdfd, getname(newname), lookup_flags,
4552 &new_path, &new_last, &new_type);
4554 error = PTR_ERR(to);
4559 if (old_path.mnt != new_path.mnt)
4563 if (old_type != LAST_NORM)
4566 if (flags & RENAME_NOREPLACE)
4568 if (new_type != LAST_NORM)
4571 error = mnt_want_write(old_path.mnt);
4576 trap = lock_rename(new_path.dentry, old_path.dentry);
4578 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
4579 error = PTR_ERR(old_dentry);
4580 if (IS_ERR(old_dentry))
4582 /* source must exist */
4584 if (d_is_negative(old_dentry))
4586 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
4587 error = PTR_ERR(new_dentry);
4588 if (IS_ERR(new_dentry))
4591 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4593 if (flags & RENAME_EXCHANGE) {
4595 if (d_is_negative(new_dentry))
4598 if (!d_is_dir(new_dentry)) {
4600 if (new_last.name[new_last.len])
4604 /* unless the source is a directory trailing slashes give -ENOTDIR */
4605 if (!d_is_dir(old_dentry)) {
4607 if (old_last.name[old_last.len])
4609 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
4612 /* source should not be ancestor of target */
4614 if (old_dentry == trap)
4616 /* target should not be an ancestor of source */
4617 if (!(flags & RENAME_EXCHANGE))
4619 if (new_dentry == trap)
4622 error = security_path_rename(&old_path, old_dentry,
4623 &new_path, new_dentry, flags);
4626 error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4627 new_path.dentry->d_inode, new_dentry,
4628 &delegated_inode, flags);
4634 unlock_rename(new_path.dentry, old_path.dentry);
4635 if (delegated_inode) {
4636 error = break_deleg_wait(&delegated_inode);
4640 mnt_drop_write(old_path.mnt);
4642 if (retry_estale(error, lookup_flags))
4643 should_retry = true;
4644 path_put(&new_path);
4647 path_put(&old_path);
4650 should_retry = false;
4651 lookup_flags |= LOOKUP_REVAL;
4658 SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4659 int, newdfd, const char __user *, newname, unsigned int, flags)
4661 return do_renameat2(olddfd, oldname, newdfd, newname, flags);
4664 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4665 int, newdfd, const char __user *, newname)
4667 return do_renameat2(olddfd, oldname, newdfd, newname, 0);
4670 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
4672 return do_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
4675 int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4677 int error = may_create(dir, dentry);
4681 if (!dir->i_op->mknod)
4684 return dir->i_op->mknod(dir, dentry,
4685 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4687 EXPORT_SYMBOL(vfs_whiteout);
4689 int readlink_copy(char __user *buffer, int buflen, const char *link)
4691 int len = PTR_ERR(link);
4696 if (len > (unsigned) buflen)
4698 if (copy_to_user(buffer, link, len))
4705 * vfs_readlink - copy symlink body into userspace buffer
4706 * @dentry: dentry on which to get symbolic link
4707 * @buffer: user memory pointer
4708 * @buflen: size of buffer
4710 * Does not touch atime. That's up to the caller if necessary
4712 * Does not call security hook.
4714 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4716 struct inode *inode = d_inode(dentry);
4717 DEFINE_DELAYED_CALL(done);
4721 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
4722 if (unlikely(inode->i_op->readlink))
4723 return inode->i_op->readlink(dentry, buffer, buflen);
4725 if (!d_is_symlink(dentry))
4728 spin_lock(&inode->i_lock);
4729 inode->i_opflags |= IOP_DEFAULT_READLINK;
4730 spin_unlock(&inode->i_lock);
4733 link = inode->i_link;
4735 link = inode->i_op->get_link(dentry, inode, &done);
4737 return PTR_ERR(link);
4739 res = readlink_copy(buffer, buflen, link);
4740 do_delayed_call(&done);
4743 EXPORT_SYMBOL(vfs_readlink);
4746 * vfs_get_link - get symlink body
4747 * @dentry: dentry on which to get symbolic link
4748 * @done: caller needs to free returned data with this
4750 * Calls security hook and i_op->get_link() on the supplied inode.
4752 * It does not touch atime. That's up to the caller if necessary.
4754 * Does not work on "special" symlinks like /proc/$$/fd/N
4756 const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4758 const char *res = ERR_PTR(-EINVAL);
4759 struct inode *inode = d_inode(dentry);
4761 if (d_is_symlink(dentry)) {
4762 res = ERR_PTR(security_inode_readlink(dentry));
4764 res = inode->i_op->get_link(dentry, inode, done);
4768 EXPORT_SYMBOL(vfs_get_link);
4770 /* get the link contents into pagecache */
4771 const char *page_get_link(struct dentry *dentry, struct inode *inode,
4772 struct delayed_call *callback)
4776 struct address_space *mapping = inode->i_mapping;
4779 page = find_get_page(mapping, 0);
4781 return ERR_PTR(-ECHILD);
4782 if (!PageUptodate(page)) {
4784 return ERR_PTR(-ECHILD);
4787 page = read_mapping_page(mapping, 0, NULL);
4791 set_delayed_call(callback, page_put_link, page);
4792 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4793 kaddr = page_address(page);
4794 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
4798 EXPORT_SYMBOL(page_get_link);
4800 void page_put_link(void *arg)
4804 EXPORT_SYMBOL(page_put_link);
4806 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4808 DEFINE_DELAYED_CALL(done);
4809 int res = readlink_copy(buffer, buflen,
4810 page_get_link(dentry, d_inode(dentry),
4812 do_delayed_call(&done);
4815 EXPORT_SYMBOL(page_readlink);
4818 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4820 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
4822 struct address_space *mapping = inode->i_mapping;
4826 unsigned int flags = 0;
4828 flags |= AOP_FLAG_NOFS;
4831 err = pagecache_write_begin(NULL, mapping, 0, len-1,
4832 flags, &page, &fsdata);
4836 memcpy(page_address(page), symname, len-1);
4838 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4845 mark_inode_dirty(inode);
4850 EXPORT_SYMBOL(__page_symlink);
4852 int page_symlink(struct inode *inode, const char *symname, int len)
4854 return __page_symlink(inode, symname, len,
4855 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
4857 EXPORT_SYMBOL(page_symlink);
4859 const struct inode_operations page_symlink_inode_operations = {
4860 .get_link = page_get_link,
4862 EXPORT_SYMBOL(page_symlink_inode_operations);