4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Some corrections by tytso.
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
17 #include <linux/init.h>
18 #include <linux/export.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
22 #include <linux/namei.h>
23 #include <linux/pagemap.h>
24 #include <linux/fsnotify.h>
25 #include <linux/personality.h>
26 #include <linux/security.h>
27 #include <linux/ima.h>
28 #include <linux/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <linux/capability.h>
32 #include <linux/file.h>
33 #include <linux/fcntl.h>
34 #include <linux/device_cgroup.h>
35 #include <linux/fs_struct.h>
36 #include <linux/posix_acl.h>
37 #include <linux/hash.h>
38 #include <linux/bitops.h>
39 #include <linux/init_task.h>
40 #include <asm/uaccess.h>
45 /* [Feb-1997 T. Schoebel-Theuer]
46 * Fundamental changes in the pathname lookup mechanisms (namei)
47 * were necessary because of omirr. The reason is that omirr needs
48 * to know the _real_ pathname, not the user-supplied one, in case
49 * of symlinks (and also when transname replacements occur).
51 * The new code replaces the old recursive symlink resolution with
52 * an iterative one (in case of non-nested symlink chains). It does
53 * this with calls to <fs>_follow_link().
54 * As a side effect, dir_namei(), _namei() and follow_link() are now
55 * replaced with a single function lookup_dentry() that can handle all
56 * the special cases of the former code.
58 * With the new dcache, the pathname is stored at each inode, at least as
59 * long as the refcount of the inode is positive. As a side effect, the
60 * size of the dcache depends on the inode cache and thus is dynamic.
62 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
63 * resolution to correspond with current state of the code.
65 * Note that the symlink resolution is not *completely* iterative.
66 * There is still a significant amount of tail- and mid- recursion in
67 * the algorithm. Also, note that <fs>_readlink() is not used in
68 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
69 * may return different results than <fs>_follow_link(). Many virtual
70 * filesystems (including /proc) exhibit this behavior.
73 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
74 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
75 * and the name already exists in form of a symlink, try to create the new
76 * name indicated by the symlink. The old code always complained that the
77 * name already exists, due to not following the symlink even if its target
78 * is nonexistent. The new semantics affects also mknod() and link() when
79 * the name is a symlink pointing to a non-existent name.
81 * I don't know which semantics is the right one, since I have no access
82 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
83 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
84 * "old" one. Personally, I think the new semantics is much more logical.
85 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
86 * file does succeed in both HP-UX and SunOs, but not in Solaris
87 * and in the old Linux semantics.
90 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
91 * semantics. See the comments in "open_namei" and "do_link" below.
93 * [10-Sep-98 Alan Modra] Another symlink change.
96 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
97 * inside the path - always follow.
98 * in the last component in creation/removal/renaming - never follow.
99 * if LOOKUP_FOLLOW passed - follow.
100 * if the pathname has trailing slashes - follow.
101 * otherwise - don't follow.
102 * (applied in that order).
104 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
105 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
106 * During the 2.4 we need to fix the userland stuff depending on it -
107 * hopefully we will be able to get rid of that wart in 2.5. So far only
108 * XEmacs seems to be relying on it...
111 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
112 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
113 * any extra contention...
116 /* In order to reduce some races, while at the same time doing additional
117 * checking and hopefully speeding things up, we copy filenames to the
118 * kernel data space before using them..
120 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
121 * PATH_MAX includes the nul terminator --RR.
124 #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
127 getname_flags(const char __user *filename, int flags, int *empty)
129 struct filename *result;
133 result = audit_reusename(filename);
137 result = __getname();
138 if (unlikely(!result))
139 return ERR_PTR(-ENOMEM);
142 * First, try to embed the struct filename inside the names_cache
145 kname = (char *)result->iname;
146 result->name = kname;
148 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
149 if (unlikely(len < 0)) {
155 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
156 * separate struct filename so we can dedicate the entire
157 * names_cache allocation for the pathname, and re-do the copy from
160 if (unlikely(len == EMBEDDED_NAME_MAX)) {
161 const size_t size = offsetof(struct filename, iname[1]);
162 kname = (char *)result;
165 * size is chosen that way we to guarantee that
166 * result->iname[0] is within the same object and that
167 * kname can't be equal to result->iname, no matter what.
169 result = kzalloc(size, GFP_KERNEL);
170 if (unlikely(!result)) {
172 return ERR_PTR(-ENOMEM);
174 result->name = kname;
175 len = strncpy_from_user(kname, filename, PATH_MAX);
176 if (unlikely(len < 0)) {
181 if (unlikely(len == PATH_MAX)) {
184 return ERR_PTR(-ENAMETOOLONG);
189 /* The empty path is special. */
190 if (unlikely(!len)) {
193 if (!(flags & LOOKUP_EMPTY)) {
195 return ERR_PTR(-ENOENT);
199 result->uptr = filename;
200 result->aname = NULL;
201 audit_getname(result);
206 getname(const char __user * filename)
208 return getname_flags(filename, 0, NULL);
212 getname_kernel(const char * filename)
214 struct filename *result;
215 int len = strlen(filename) + 1;
217 result = __getname();
218 if (unlikely(!result))
219 return ERR_PTR(-ENOMEM);
221 if (len <= EMBEDDED_NAME_MAX) {
222 result->name = (char *)result->iname;
223 } else if (len <= PATH_MAX) {
224 const size_t size = offsetof(struct filename, iname[1]);
225 struct filename *tmp;
227 tmp = kmalloc(size, GFP_KERNEL);
228 if (unlikely(!tmp)) {
230 return ERR_PTR(-ENOMEM);
232 tmp->name = (char *)result;
236 return ERR_PTR(-ENAMETOOLONG);
238 memcpy((char *)result->name, filename, len);
240 result->aname = NULL;
242 audit_getname(result);
247 void putname(struct filename *name)
249 BUG_ON(name->refcnt <= 0);
251 if (--name->refcnt > 0)
254 if (name->name != name->iname) {
255 __putname(name->name);
261 static int check_acl(struct inode *inode, int mask)
263 #ifdef CONFIG_FS_POSIX_ACL
264 struct posix_acl *acl;
266 if (mask & MAY_NOT_BLOCK) {
267 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
270 /* no ->get_acl() calls in RCU mode... */
271 if (is_uncached_acl(acl))
273 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
276 acl = get_acl(inode, ACL_TYPE_ACCESS);
280 int error = posix_acl_permission(inode, acl, mask);
281 posix_acl_release(acl);
290 * This does the basic permission checking
292 static int acl_permission_check(struct inode *inode, int mask)
294 unsigned int mode = inode->i_mode;
296 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
299 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
300 int error = check_acl(inode, mask);
301 if (error != -EAGAIN)
305 if (in_group_p(inode->i_gid))
310 * If the DACs are ok we don't need any capability check.
312 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
318 * generic_permission - check for access rights on a Posix-like filesystem
319 * @inode: inode to check access rights for
320 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
322 * Used to check for read/write/execute permissions on a file.
323 * We use "fsuid" for this, letting us set arbitrary permissions
324 * for filesystem access without changing the "normal" uids which
325 * are used for other things.
327 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
328 * request cannot be satisfied (eg. requires blocking or too much complexity).
329 * It would then be called again in ref-walk mode.
331 int generic_permission(struct inode *inode, int mask)
336 * Do the basic permission checks.
338 ret = acl_permission_check(inode, mask);
342 if (S_ISDIR(inode->i_mode)) {
343 /* DACs are overridable for directories */
344 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
346 if (!(mask & MAY_WRITE))
347 if (capable_wrt_inode_uidgid(inode,
348 CAP_DAC_READ_SEARCH))
353 * Read/write DACs are always overridable.
354 * Executable DACs are overridable when there is
355 * at least one exec bit set.
357 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
358 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
362 * Searching includes executable on directories, else just read.
364 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
365 if (mask == MAY_READ)
366 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
371 EXPORT_SYMBOL(generic_permission);
374 * We _really_ want to just do "generic_permission()" without
375 * even looking at the inode->i_op values. So we keep a cache
376 * flag in inode->i_opflags, that says "this has not special
377 * permission function, use the fast case".
379 static inline int do_inode_permission(struct inode *inode, int mask)
381 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
382 if (likely(inode->i_op->permission))
383 return inode->i_op->permission(inode, mask);
385 /* This gets set once for the inode lifetime */
386 spin_lock(&inode->i_lock);
387 inode->i_opflags |= IOP_FASTPERM;
388 spin_unlock(&inode->i_lock);
390 return generic_permission(inode, mask);
394 * __inode_permission - Check for access rights to a given inode
395 * @inode: Inode to check permission on
396 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
398 * Check for read/write/execute permissions on an inode.
400 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
402 * This does not check for a read-only file system. You probably want
403 * inode_permission().
405 int __inode_permission(struct inode *inode, int mask)
409 if (unlikely(mask & MAY_WRITE)) {
411 * Nobody gets write access to an immutable file.
413 if (IS_IMMUTABLE(inode))
417 * Updating mtime will likely cause i_uid and i_gid to be
418 * written back improperly if their true value is unknown
421 if (HAS_UNMAPPED_ID(inode))
425 retval = do_inode_permission(inode, mask);
429 retval = devcgroup_inode_permission(inode, mask);
433 return security_inode_permission(inode, mask);
435 EXPORT_SYMBOL(__inode_permission);
438 * sb_permission - Check superblock-level permissions
439 * @sb: Superblock of inode to check permission on
440 * @inode: Inode to check permission on
441 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
443 * Separate out file-system wide checks from inode-specific permission checks.
445 static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
447 if (unlikely(mask & MAY_WRITE)) {
448 umode_t mode = inode->i_mode;
450 /* Nobody gets write access to a read-only fs. */
451 if ((sb->s_flags & MS_RDONLY) &&
452 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
459 * inode_permission - Check for access rights to a given inode
460 * @inode: Inode to check permission on
461 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
463 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
464 * this, letting us set arbitrary permissions for filesystem access without
465 * changing the "normal" UIDs which are used for other things.
467 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
469 int inode_permission(struct inode *inode, int mask)
473 retval = sb_permission(inode->i_sb, inode, mask);
476 return __inode_permission(inode, mask);
478 EXPORT_SYMBOL(inode_permission);
481 * path_get - get a reference to a path
482 * @path: path to get the reference to
484 * Given a path increment the reference count to the dentry and the vfsmount.
486 void path_get(const struct path *path)
491 EXPORT_SYMBOL(path_get);
494 * path_put - put a reference to a path
495 * @path: path to put the reference to
497 * Given a path decrement the reference count to the dentry and the vfsmount.
499 void path_put(const struct path *path)
504 EXPORT_SYMBOL(path_put);
506 #define EMBEDDED_LEVELS 2
511 struct inode *inode; /* path.dentry.d_inode */
516 int total_link_count;
519 struct delayed_call done;
522 } *stack, internal[EMBEDDED_LEVELS];
523 struct filename *name;
524 struct nameidata *saved;
525 struct inode *link_inode;
530 static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
532 struct nameidata *old = current->nameidata;
533 p->stack = p->internal;
536 p->total_link_count = old ? old->total_link_count : 0;
538 current->nameidata = p;
541 static void restore_nameidata(void)
543 struct nameidata *now = current->nameidata, *old = now->saved;
545 current->nameidata = old;
547 old->total_link_count = now->total_link_count;
548 if (now->stack != now->internal)
552 static int __nd_alloc_stack(struct nameidata *nd)
556 if (nd->flags & LOOKUP_RCU) {
557 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
562 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
567 memcpy(p, nd->internal, sizeof(nd->internal));
573 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
574 * @path: nameidate to verify
576 * Rename can sometimes move a file or directory outside of a bind
577 * mount, path_connected allows those cases to be detected.
579 static bool path_connected(const struct path *path)
581 struct vfsmount *mnt = path->mnt;
582 struct super_block *sb = mnt->mnt_sb;
584 /* Bind mounts and multi-root filesystems can have disconnected paths */
585 if (!(sb->s_iflags & SB_I_MULTIROOT) && (mnt->mnt_root == sb->s_root))
588 return is_subdir(path->dentry, mnt->mnt_root);
591 static inline int nd_alloc_stack(struct nameidata *nd)
593 if (likely(nd->depth != EMBEDDED_LEVELS))
595 if (likely(nd->stack != nd->internal))
597 return __nd_alloc_stack(nd);
600 static void drop_links(struct nameidata *nd)
604 struct saved *last = nd->stack + i;
605 do_delayed_call(&last->done);
606 clear_delayed_call(&last->done);
610 static void terminate_walk(struct nameidata *nd)
613 if (!(nd->flags & LOOKUP_RCU)) {
616 for (i = 0; i < nd->depth; i++)
617 path_put(&nd->stack[i].link);
618 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
623 nd->flags &= ~LOOKUP_RCU;
624 if (!(nd->flags & LOOKUP_ROOT))
631 /* path_put is needed afterwards regardless of success or failure */
632 static bool legitimize_path(struct nameidata *nd,
633 struct path *path, unsigned seq)
635 int res = __legitimize_mnt(path->mnt, nd->m_seq);
642 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
646 return !read_seqcount_retry(&path->dentry->d_seq, seq);
649 static bool legitimize_links(struct nameidata *nd)
652 for (i = 0; i < nd->depth; i++) {
653 struct saved *last = nd->stack + i;
654 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
664 * Path walking has 2 modes, rcu-walk and ref-walk (see
665 * Documentation/filesystems/path-lookup.txt). In situations when we can't
666 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
667 * normal reference counts on dentries and vfsmounts to transition to ref-walk
668 * mode. Refcounts are grabbed at the last known good point before rcu-walk
669 * got stuck, so ref-walk may continue from there. If this is not successful
670 * (eg. a seqcount has changed), then failure is returned and it's up to caller
671 * to restart the path walk from the beginning in ref-walk mode.
675 * unlazy_walk - try to switch to ref-walk mode.
676 * @nd: nameidata pathwalk data
677 * @dentry: child of nd->path.dentry or NULL
678 * @seq: seq number to check dentry against
679 * Returns: 0 on success, -ECHILD on failure
681 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
682 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
683 * @nd or NULL. Must be called from rcu-walk context.
684 * Nothing should touch nameidata between unlazy_walk() failure and
687 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry, unsigned seq)
689 struct dentry *parent = nd->path.dentry;
691 BUG_ON(!(nd->flags & LOOKUP_RCU));
693 nd->flags &= ~LOOKUP_RCU;
694 if (unlikely(!legitimize_links(nd)))
696 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
698 if (unlikely(!lockref_get_not_dead(&parent->d_lockref)))
702 * For a negative lookup, the lookup sequence point is the parents
703 * sequence point, and it only needs to revalidate the parent dentry.
705 * For a positive lookup, we need to move both the parent and the
706 * dentry from the RCU domain to be properly refcounted. And the
707 * sequence number in the dentry validates *both* dentry counters,
708 * since we checked the sequence number of the parent after we got
709 * the child sequence number. So we know the parent must still
710 * be valid if the child sequence number is still valid.
713 if (read_seqcount_retry(&parent->d_seq, nd->seq))
715 BUG_ON(nd->inode != parent->d_inode);
717 if (!lockref_get_not_dead(&dentry->d_lockref))
719 if (read_seqcount_retry(&dentry->d_seq, seq))
724 * Sequence counts matched. Now make sure that the root is
725 * still valid and get it if required.
727 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
728 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
745 nd->path.dentry = NULL;
749 if (!(nd->flags & LOOKUP_ROOT))
754 static int unlazy_link(struct nameidata *nd, struct path *link, unsigned seq)
756 if (unlikely(!legitimize_path(nd, link, seq))) {
759 nd->flags &= ~LOOKUP_RCU;
761 nd->path.dentry = NULL;
762 if (!(nd->flags & LOOKUP_ROOT))
765 } else if (likely(unlazy_walk(nd, NULL, 0)) == 0) {
772 static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
774 return dentry->d_op->d_revalidate(dentry, flags);
778 * complete_walk - successful completion of path walk
779 * @nd: pointer nameidata
781 * If we had been in RCU mode, drop out of it and legitimize nd->path.
782 * Revalidate the final result, unless we'd already done that during
783 * the path walk or the filesystem doesn't ask for it. Return 0 on
784 * success, -error on failure. In case of failure caller does not
785 * need to drop nd->path.
787 static int complete_walk(struct nameidata *nd)
789 struct dentry *dentry = nd->path.dentry;
792 if (nd->flags & LOOKUP_RCU) {
793 if (!(nd->flags & LOOKUP_ROOT))
795 if (unlikely(unlazy_walk(nd, NULL, 0)))
799 if (likely(!(nd->flags & LOOKUP_JUMPED)))
802 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
805 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
815 static void set_root(struct nameidata *nd)
817 struct fs_struct *fs = current->fs;
819 if (nd->flags & LOOKUP_RCU) {
823 seq = read_seqcount_begin(&fs->seq);
825 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
826 } while (read_seqcount_retry(&fs->seq, seq));
828 get_fs_root(fs, &nd->root);
832 static void path_put_conditional(struct path *path, struct nameidata *nd)
835 if (path->mnt != nd->path.mnt)
839 static inline void path_to_nameidata(const struct path *path,
840 struct nameidata *nd)
842 if (!(nd->flags & LOOKUP_RCU)) {
843 dput(nd->path.dentry);
844 if (nd->path.mnt != path->mnt)
845 mntput(nd->path.mnt);
847 nd->path.mnt = path->mnt;
848 nd->path.dentry = path->dentry;
851 static int nd_jump_root(struct nameidata *nd)
853 if (nd->flags & LOOKUP_RCU) {
857 nd->inode = d->d_inode;
858 nd->seq = nd->root_seq;
859 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
865 nd->inode = nd->path.dentry->d_inode;
867 nd->flags |= LOOKUP_JUMPED;
872 * Helper to directly jump to a known parsed path from ->get_link,
873 * caller must have taken a reference to path beforehand.
875 void nd_jump_link(struct path *path)
877 struct nameidata *nd = current->nameidata;
881 nd->inode = nd->path.dentry->d_inode;
882 nd->flags |= LOOKUP_JUMPED;
885 static inline void put_link(struct nameidata *nd)
887 struct saved *last = nd->stack + --nd->depth;
888 do_delayed_call(&last->done);
889 if (!(nd->flags & LOOKUP_RCU))
890 path_put(&last->link);
893 int sysctl_protected_symlinks __read_mostly = 0;
894 int sysctl_protected_hardlinks __read_mostly = 0;
895 int sysctl_protected_fifos __read_mostly;
896 int sysctl_protected_regular __read_mostly;
899 * may_follow_link - Check symlink following for unsafe situations
900 * @nd: nameidata pathwalk data
902 * In the case of the sysctl_protected_symlinks sysctl being enabled,
903 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
904 * in a sticky world-writable directory. This is to protect privileged
905 * processes from failing races against path names that may change out
906 * from under them by way of other users creating malicious symlinks.
907 * It will permit symlinks to be followed only when outside a sticky
908 * world-writable directory, or when the uid of the symlink and follower
909 * match, or when the directory owner matches the symlink's owner.
911 * Returns 0 if following the symlink is allowed, -ve on error.
913 static inline int may_follow_link(struct nameidata *nd)
915 const struct inode *inode;
916 const struct inode *parent;
919 if (!sysctl_protected_symlinks)
922 /* Allowed if owner and follower match. */
923 inode = nd->link_inode;
924 if (uid_eq(current_cred()->fsuid, inode->i_uid))
927 /* Allowed if parent directory not sticky and world-writable. */
929 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
932 /* Allowed if parent directory and link owner match. */
933 puid = parent->i_uid;
934 if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
937 if (nd->flags & LOOKUP_RCU)
940 audit_log_link_denied("follow_link", &nd->stack[0].link);
945 * safe_hardlink_source - Check for safe hardlink conditions
946 * @inode: the source inode to hardlink from
948 * Return false if at least one of the following conditions:
949 * - inode is not a regular file
951 * - inode is setgid and group-exec
952 * - access failure for read and write
954 * Otherwise returns true.
956 static bool safe_hardlink_source(struct inode *inode)
958 umode_t mode = inode->i_mode;
960 /* Special files should not get pinned to the filesystem. */
964 /* Setuid files should not get pinned to the filesystem. */
968 /* Executable setgid files should not get pinned to the filesystem. */
969 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
972 /* Hardlinking to unreadable or unwritable sources is dangerous. */
973 if (inode_permission(inode, MAY_READ | MAY_WRITE))
980 * may_linkat - Check permissions for creating a hardlink
981 * @link: the source to hardlink from
983 * Block hardlink when all of:
984 * - sysctl_protected_hardlinks enabled
985 * - fsuid does not match inode
986 * - hardlink source is unsafe (see safe_hardlink_source() above)
987 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
989 * Returns 0 if successful, -ve on error.
991 static int may_linkat(struct path *link)
995 if (!sysctl_protected_hardlinks)
998 inode = link->dentry->d_inode;
1000 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1001 * otherwise, it must be a safe source.
1003 if (inode_owner_or_capable(inode) || safe_hardlink_source(inode))
1006 audit_log_link_denied("linkat", link);
1011 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1012 * should be allowed, or not, on files that already
1014 * @dir_mode: mode bits of directory
1015 * @dir_uid: owner of directory
1016 * @inode: the inode of the file to open
1018 * Block an O_CREAT open of a FIFO (or a regular file) when:
1019 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1020 * - the file already exists
1021 * - we are in a sticky directory
1022 * - we don't own the file
1023 * - the owner of the directory doesn't own the file
1024 * - the directory is world writable
1025 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1026 * the directory doesn't have to be world writable: being group writable will
1029 * Returns 0 if the open is allowed, -ve on error.
1031 static int may_create_in_sticky(umode_t dir_mode, kuid_t dir_uid,
1032 struct inode * const inode)
1034 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
1035 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
1036 likely(!(dir_mode & S_ISVTX)) ||
1037 uid_eq(inode->i_uid, dir_uid) ||
1038 uid_eq(current_fsuid(), inode->i_uid))
1041 if (likely(dir_mode & 0002) ||
1043 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
1044 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
1050 static __always_inline
1051 const char *get_link(struct nameidata *nd)
1053 struct saved *last = nd->stack + nd->depth - 1;
1054 struct dentry *dentry = last->link.dentry;
1055 struct inode *inode = nd->link_inode;
1059 if (!(nd->flags & LOOKUP_RCU)) {
1060 touch_atime(&last->link);
1062 } else if (atime_needs_update_rcu(&last->link, inode)) {
1063 if (unlikely(unlazy_walk(nd, NULL, 0)))
1064 return ERR_PTR(-ECHILD);
1065 touch_atime(&last->link);
1068 error = security_inode_follow_link(dentry, inode,
1069 nd->flags & LOOKUP_RCU);
1070 if (unlikely(error))
1071 return ERR_PTR(error);
1073 nd->last_type = LAST_BIND;
1074 res = inode->i_link;
1076 const char * (*get)(struct dentry *, struct inode *,
1077 struct delayed_call *);
1078 get = inode->i_op->get_link;
1079 if (nd->flags & LOOKUP_RCU) {
1080 res = get(NULL, inode, &last->done);
1081 if (res == ERR_PTR(-ECHILD)) {
1082 if (unlikely(unlazy_walk(nd, NULL, 0)))
1083 return ERR_PTR(-ECHILD);
1084 res = get(dentry, inode, &last->done);
1087 res = get(dentry, inode, &last->done);
1089 if (IS_ERR_OR_NULL(res))
1095 if (unlikely(nd_jump_root(nd)))
1096 return ERR_PTR(-ECHILD);
1097 while (unlikely(*++res == '/'))
1106 * follow_up - Find the mountpoint of path's vfsmount
1108 * Given a path, find the mountpoint of its source file system.
1109 * Replace @path with the path of the mountpoint in the parent mount.
1112 * Return 1 if we went up a level and 0 if we were already at the
1115 int follow_up(struct path *path)
1117 struct mount *mnt = real_mount(path->mnt);
1118 struct mount *parent;
1119 struct dentry *mountpoint;
1121 read_seqlock_excl(&mount_lock);
1122 parent = mnt->mnt_parent;
1123 if (parent == mnt) {
1124 read_sequnlock_excl(&mount_lock);
1127 mntget(&parent->mnt);
1128 mountpoint = dget(mnt->mnt_mountpoint);
1129 read_sequnlock_excl(&mount_lock);
1131 path->dentry = mountpoint;
1133 path->mnt = &parent->mnt;
1136 EXPORT_SYMBOL(follow_up);
1139 * Perform an automount
1140 * - return -EISDIR to tell follow_managed() to stop and return the path we
1143 static int follow_automount(struct path *path, struct nameidata *nd,
1146 struct vfsmount *mnt;
1149 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1152 /* We don't want to mount if someone's just doing a stat -
1153 * unless they're stat'ing a directory and appended a '/' to
1156 * We do, however, want to mount if someone wants to open or
1157 * create a file of any type under the mountpoint, wants to
1158 * traverse through the mountpoint or wants to open the
1159 * mounted directory. Also, autofs may mark negative dentries
1160 * as being automount points. These will need the attentions
1161 * of the daemon to instantiate them before they can be used.
1163 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
1164 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
1165 path->dentry->d_inode)
1168 nd->total_link_count++;
1169 if (nd->total_link_count >= 40)
1172 mnt = path->dentry->d_op->d_automount(path);
1175 * The filesystem is allowed to return -EISDIR here to indicate
1176 * it doesn't want to automount. For instance, autofs would do
1177 * this so that its userspace daemon can mount on this dentry.
1179 * However, we can only permit this if it's a terminal point in
1180 * the path being looked up; if it wasn't then the remainder of
1181 * the path is inaccessible and we should say so.
1183 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
1185 return PTR_ERR(mnt);
1188 if (!mnt) /* mount collision */
1191 if (!*need_mntput) {
1192 /* lock_mount() may release path->mnt on error */
1194 *need_mntput = true;
1196 err = finish_automount(mnt, path);
1200 /* Someone else made a mount here whilst we were busy */
1205 path->dentry = dget(mnt->mnt_root);
1214 * Handle a dentry that is managed in some way.
1215 * - Flagged for transit management (autofs)
1216 * - Flagged as mountpoint
1217 * - Flagged as automount point
1219 * This may only be called in refwalk mode.
1221 * Serialization is taken care of in namespace.c
1223 static int follow_managed(struct path *path, struct nameidata *nd)
1225 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
1227 bool need_mntput = false;
1230 /* Given that we're not holding a lock here, we retain the value in a
1231 * local variable for each dentry as we look at it so that we don't see
1232 * the components of that value change under us */
1233 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1234 managed &= DCACHE_MANAGED_DENTRY,
1235 unlikely(managed != 0)) {
1236 /* Allow the filesystem to manage the transit without i_mutex
1238 if (managed & DCACHE_MANAGE_TRANSIT) {
1239 BUG_ON(!path->dentry->d_op);
1240 BUG_ON(!path->dentry->d_op->d_manage);
1241 ret = path->dentry->d_op->d_manage(path->dentry, false);
1246 /* Transit to a mounted filesystem. */
1247 if (managed & DCACHE_MOUNTED) {
1248 struct vfsmount *mounted = lookup_mnt(path);
1253 path->mnt = mounted;
1254 path->dentry = dget(mounted->mnt_root);
1259 /* Something is mounted on this dentry in another
1260 * namespace and/or whatever was mounted there in this
1261 * namespace got unmounted before lookup_mnt() could
1265 /* Handle an automount point */
1266 if (managed & DCACHE_NEED_AUTOMOUNT) {
1267 ret = follow_automount(path, nd, &need_mntput);
1273 /* We didn't change the current path point */
1277 if (need_mntput && path->mnt == mnt)
1279 if (ret == -EISDIR || !ret)
1282 nd->flags |= LOOKUP_JUMPED;
1283 if (unlikely(ret < 0))
1284 path_put_conditional(path, nd);
1288 int follow_down_one(struct path *path)
1290 struct vfsmount *mounted;
1292 mounted = lookup_mnt(path);
1296 path->mnt = mounted;
1297 path->dentry = dget(mounted->mnt_root);
1302 EXPORT_SYMBOL(follow_down_one);
1304 static inline int managed_dentry_rcu(struct dentry *dentry)
1306 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1307 dentry->d_op->d_manage(dentry, true) : 0;
1311 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1312 * we meet a managed dentry that would need blocking.
1314 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1315 struct inode **inode, unsigned *seqp)
1318 struct mount *mounted;
1320 * Don't forget we might have a non-mountpoint managed dentry
1321 * that wants to block transit.
1323 switch (managed_dentry_rcu(path->dentry)) {
1333 if (!d_mountpoint(path->dentry))
1334 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1336 mounted = __lookup_mnt(path->mnt, path->dentry);
1339 path->mnt = &mounted->mnt;
1340 path->dentry = mounted->mnt.mnt_root;
1341 nd->flags |= LOOKUP_JUMPED;
1342 *seqp = read_seqcount_begin(&path->dentry->d_seq);
1344 * Update the inode too. We don't need to re-check the
1345 * dentry sequence number here after this d_inode read,
1346 * because a mount-point is always pinned.
1348 *inode = path->dentry->d_inode;
1350 return !read_seqretry(&mount_lock, nd->m_seq) &&
1351 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1354 static int follow_dotdot_rcu(struct nameidata *nd)
1356 struct inode *inode = nd->inode;
1359 if (path_equal(&nd->path, &nd->root))
1361 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1362 struct dentry *old = nd->path.dentry;
1363 struct dentry *parent = old->d_parent;
1366 inode = parent->d_inode;
1367 seq = read_seqcount_begin(&parent->d_seq);
1368 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1370 nd->path.dentry = parent;
1372 if (unlikely(!path_connected(&nd->path)))
1376 struct mount *mnt = real_mount(nd->path.mnt);
1377 struct mount *mparent = mnt->mnt_parent;
1378 struct dentry *mountpoint = mnt->mnt_mountpoint;
1379 struct inode *inode2 = mountpoint->d_inode;
1380 unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1381 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1383 if (&mparent->mnt == nd->path.mnt)
1385 /* we know that mountpoint was pinned */
1386 nd->path.dentry = mountpoint;
1387 nd->path.mnt = &mparent->mnt;
1392 while (unlikely(d_mountpoint(nd->path.dentry))) {
1393 struct mount *mounted;
1394 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1395 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1399 nd->path.mnt = &mounted->mnt;
1400 nd->path.dentry = mounted->mnt.mnt_root;
1401 inode = nd->path.dentry->d_inode;
1402 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1409 * Follow down to the covering mount currently visible to userspace. At each
1410 * point, the filesystem owning that dentry may be queried as to whether the
1411 * caller is permitted to proceed or not.
1413 int follow_down(struct path *path)
1418 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1419 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1420 /* Allow the filesystem to manage the transit without i_mutex
1423 * We indicate to the filesystem if someone is trying to mount
1424 * something here. This gives autofs the chance to deny anyone
1425 * other than its daemon the right to mount on its
1428 * The filesystem may sleep at this point.
1430 if (managed & DCACHE_MANAGE_TRANSIT) {
1431 BUG_ON(!path->dentry->d_op);
1432 BUG_ON(!path->dentry->d_op->d_manage);
1433 ret = path->dentry->d_op->d_manage(
1434 path->dentry, false);
1436 return ret == -EISDIR ? 0 : ret;
1439 /* Transit to a mounted filesystem. */
1440 if (managed & DCACHE_MOUNTED) {
1441 struct vfsmount *mounted = lookup_mnt(path);
1446 path->mnt = mounted;
1447 path->dentry = dget(mounted->mnt_root);
1451 /* Don't handle automount points here */
1456 EXPORT_SYMBOL(follow_down);
1459 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1461 static void follow_mount(struct path *path)
1463 while (d_mountpoint(path->dentry)) {
1464 struct vfsmount *mounted = lookup_mnt(path);
1469 path->mnt = mounted;
1470 path->dentry = dget(mounted->mnt_root);
1474 static int path_parent_directory(struct path *path)
1476 struct dentry *old = path->dentry;
1477 /* rare case of legitimate dget_parent()... */
1478 path->dentry = dget_parent(path->dentry);
1480 if (unlikely(!path_connected(path)))
1485 static int follow_dotdot(struct nameidata *nd)
1488 if (nd->path.dentry == nd->root.dentry &&
1489 nd->path.mnt == nd->root.mnt) {
1492 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1493 int ret = path_parent_directory(&nd->path);
1498 if (!follow_up(&nd->path))
1501 follow_mount(&nd->path);
1502 nd->inode = nd->path.dentry->d_inode;
1507 * This looks up the name in dcache and possibly revalidates the found dentry.
1508 * NULL is returned if the dentry does not exist in the cache.
1510 static struct dentry *lookup_dcache(const struct qstr *name,
1514 struct dentry *dentry;
1517 dentry = d_lookup(dir, name);
1519 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1520 error = d_revalidate(dentry, flags);
1521 if (unlikely(error <= 0)) {
1523 d_invalidate(dentry);
1525 return ERR_PTR(error);
1533 * Call i_op->lookup on the dentry. The dentry must be negative and
1536 * dir->d_inode->i_mutex must be held
1538 static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1543 /* Don't create child dentry for a dead directory. */
1544 if (unlikely(IS_DEADDIR(dir))) {
1546 return ERR_PTR(-ENOENT);
1549 old = dir->i_op->lookup(dir, dentry, flags);
1550 if (unlikely(old)) {
1557 static struct dentry *__lookup_hash(const struct qstr *name,
1558 struct dentry *base, unsigned int flags)
1560 struct dentry *dentry = lookup_dcache(name, base, flags);
1565 dentry = d_alloc(base, name);
1566 if (unlikely(!dentry))
1567 return ERR_PTR(-ENOMEM);
1569 return lookup_real(base->d_inode, dentry, flags);
1572 static int lookup_fast(struct nameidata *nd,
1573 struct path *path, struct inode **inode,
1576 struct vfsmount *mnt = nd->path.mnt;
1577 struct dentry *dentry, *parent = nd->path.dentry;
1582 * Rename seqlock is not required here because in the off chance
1583 * of a false negative due to a concurrent rename, the caller is
1584 * going to fall back to non-racy lookup.
1586 if (nd->flags & LOOKUP_RCU) {
1589 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
1590 if (unlikely(!dentry)) {
1591 if (unlazy_walk(nd, NULL, 0))
1597 * This sequence count validates that the inode matches
1598 * the dentry name information from lookup.
1600 *inode = d_backing_inode(dentry);
1601 negative = d_is_negative(dentry);
1602 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
1606 * This sequence count validates that the parent had no
1607 * changes while we did the lookup of the dentry above.
1609 * The memory barrier in read_seqcount_begin of child is
1610 * enough, we can use __read_seqcount_retry here.
1612 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
1616 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
1617 status = d_revalidate(dentry, nd->flags);
1618 if (unlikely(status <= 0)) {
1619 if (unlazy_walk(nd, dentry, seq))
1621 if (status == -ECHILD)
1622 status = d_revalidate(dentry, nd->flags);
1625 * Note: do negative dentry check after revalidation in
1626 * case that drops it.
1628 if (unlikely(negative))
1631 path->dentry = dentry;
1632 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
1634 if (unlazy_walk(nd, dentry, seq))
1638 dentry = __d_lookup(parent, &nd->last);
1639 if (unlikely(!dentry))
1641 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
1642 status = d_revalidate(dentry, nd->flags);
1644 if (unlikely(status <= 0)) {
1646 d_invalidate(dentry);
1650 if (unlikely(d_is_negative(dentry))) {
1656 path->dentry = dentry;
1657 err = follow_managed(path, nd);
1658 if (likely(err > 0))
1659 *inode = d_backing_inode(path->dentry);
1663 /* Fast lookup failed, do it the slow way */
1664 static struct dentry *lookup_slow(const struct qstr *name,
1668 struct dentry *dentry = ERR_PTR(-ENOENT), *old;
1669 struct inode *inode = dir->d_inode;
1670 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1672 inode_lock_shared(inode);
1673 /* Don't go there if it's already dead */
1674 if (unlikely(IS_DEADDIR(inode)))
1677 dentry = d_alloc_parallel(dir, name, &wq);
1680 if (unlikely(!d_in_lookup(dentry))) {
1681 if ((dentry->d_flags & DCACHE_OP_REVALIDATE) &&
1682 !(flags & LOOKUP_NO_REVAL)) {
1683 int error = d_revalidate(dentry, flags);
1684 if (unlikely(error <= 0)) {
1686 d_invalidate(dentry);
1691 dentry = ERR_PTR(error);
1695 old = inode->i_op->lookup(inode, dentry, flags);
1696 d_lookup_done(dentry);
1697 if (unlikely(old)) {
1703 inode_unlock_shared(inode);
1707 static inline int may_lookup(struct nameidata *nd)
1709 if (nd->flags & LOOKUP_RCU) {
1710 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1713 if (unlazy_walk(nd, NULL, 0))
1716 return inode_permission(nd->inode, MAY_EXEC);
1719 static inline int handle_dots(struct nameidata *nd, int type)
1721 if (type == LAST_DOTDOT) {
1724 if (nd->flags & LOOKUP_RCU) {
1725 return follow_dotdot_rcu(nd);
1727 return follow_dotdot(nd);
1732 static int pick_link(struct nameidata *nd, struct path *link,
1733 struct inode *inode, unsigned seq)
1737 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
1738 path_to_nameidata(link, nd);
1741 if (!(nd->flags & LOOKUP_RCU)) {
1742 if (link->mnt == nd->path.mnt)
1745 error = nd_alloc_stack(nd);
1746 if (unlikely(error)) {
1747 if (error == -ECHILD) {
1748 if (unlikely(unlazy_link(nd, link, seq)))
1750 error = nd_alloc_stack(nd);
1758 last = nd->stack + nd->depth++;
1760 clear_delayed_call(&last->done);
1761 nd->link_inode = inode;
1767 * Do we need to follow links? We _really_ want to be able
1768 * to do this check without having to look at inode->i_op,
1769 * so we keep a cache of "no, this doesn't need follow_link"
1770 * for the common case.
1772 static inline int should_follow_link(struct nameidata *nd, struct path *link,
1774 struct inode *inode, unsigned seq)
1776 if (likely(!d_is_symlink(link->dentry)))
1780 /* make sure that d_is_symlink above matches inode */
1781 if (nd->flags & LOOKUP_RCU) {
1782 if (read_seqcount_retry(&link->dentry->d_seq, seq))
1785 return pick_link(nd, link, inode, seq);
1788 enum {WALK_GET = 1, WALK_PUT = 2};
1790 static int walk_component(struct nameidata *nd, int flags)
1793 struct inode *inode;
1797 * "." and ".." are special - ".." especially so because it has
1798 * to be able to know about the current root directory and
1799 * parent relationships.
1801 if (unlikely(nd->last_type != LAST_NORM)) {
1802 err = handle_dots(nd, nd->last_type);
1803 if (flags & WALK_PUT)
1807 err = lookup_fast(nd, &path, &inode, &seq);
1808 if (unlikely(err <= 0)) {
1811 path.dentry = lookup_slow(&nd->last, nd->path.dentry,
1813 if (IS_ERR(path.dentry))
1814 return PTR_ERR(path.dentry);
1816 path.mnt = nd->path.mnt;
1817 err = follow_managed(&path, nd);
1818 if (unlikely(err < 0))
1821 if (unlikely(d_is_negative(path.dentry))) {
1822 path_to_nameidata(&path, nd);
1826 seq = 0; /* we are already out of RCU mode */
1827 inode = d_backing_inode(path.dentry);
1830 if (flags & WALK_PUT)
1832 err = should_follow_link(nd, &path, flags & WALK_GET, inode, seq);
1835 path_to_nameidata(&path, nd);
1842 * We can do the critical dentry name comparison and hashing
1843 * operations one word at a time, but we are limited to:
1845 * - Architectures with fast unaligned word accesses. We could
1846 * do a "get_unaligned()" if this helps and is sufficiently
1849 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1850 * do not trap on the (extremely unlikely) case of a page
1851 * crossing operation.
1853 * - Furthermore, we need an efficient 64-bit compile for the
1854 * 64-bit case in order to generate the "number of bytes in
1855 * the final mask". Again, that could be replaced with a
1856 * efficient population count instruction or similar.
1858 #ifdef CONFIG_DCACHE_WORD_ACCESS
1860 #include <asm/word-at-a-time.h>
1864 /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1866 #elif defined(CONFIG_64BIT)
1868 * Register pressure in the mixing function is an issue, particularly
1869 * on 32-bit x86, but almost any function requires one state value and
1870 * one temporary. Instead, use a function designed for two state values
1871 * and no temporaries.
1873 * This function cannot create a collision in only two iterations, so
1874 * we have two iterations to achieve avalanche. In those two iterations,
1875 * we have six layers of mixing, which is enough to spread one bit's
1876 * influence out to 2^6 = 64 state bits.
1878 * Rotate constants are scored by considering either 64 one-bit input
1879 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
1880 * probability of that delta causing a change to each of the 128 output
1881 * bits, using a sample of random initial states.
1883 * The Shannon entropy of the computed probabilities is then summed
1884 * to produce a score. Ideally, any input change has a 50% chance of
1885 * toggling any given output bit.
1887 * Mixing scores (in bits) for (12,45):
1888 * Input delta: 1-bit 2-bit
1889 * 1 round: 713.3 42542.6
1890 * 2 rounds: 2753.7 140389.8
1891 * 3 rounds: 5954.1 233458.2
1892 * 4 rounds: 7862.6 256672.2
1893 * Perfect: 8192 258048
1894 * (64*128) (64*63/2 * 128)
1896 #define HASH_MIX(x, y, a) \
1898 y ^= x, x = rol64(x,12),\
1899 x += y, y = rol64(y,45),\
1903 * Fold two longs into one 32-bit hash value. This must be fast, but
1904 * latency isn't quite as critical, as there is a fair bit of additional
1905 * work done before the hash value is used.
1907 static inline unsigned int fold_hash(unsigned long x, unsigned long y)
1909 y ^= x * GOLDEN_RATIO_64;
1910 y *= GOLDEN_RATIO_64;
1914 #else /* 32-bit case */
1917 * Mixing scores (in bits) for (7,20):
1918 * Input delta: 1-bit 2-bit
1919 * 1 round: 330.3 9201.6
1920 * 2 rounds: 1246.4 25475.4
1921 * 3 rounds: 1907.1 31295.1
1922 * 4 rounds: 2042.3 31718.6
1923 * Perfect: 2048 31744
1924 * (32*64) (32*31/2 * 64)
1926 #define HASH_MIX(x, y, a) \
1928 y ^= x, x = rol32(x, 7),\
1929 x += y, y = rol32(y,20),\
1932 static inline unsigned int fold_hash(unsigned long x, unsigned long y)
1934 /* Use arch-optimized multiply if one exists */
1935 return __hash_32(y ^ __hash_32(x));
1941 * Return the hash of a string of known length. This is carfully
1942 * designed to match hash_name(), which is the more critical function.
1943 * In particular, we must end by hashing a final word containing 0..7
1944 * payload bytes, to match the way that hash_name() iterates until it
1945 * finds the delimiter after the name.
1947 unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
1949 unsigned long a, x = 0, y = (unsigned long)salt;
1954 a = load_unaligned_zeropad(name);
1955 if (len < sizeof(unsigned long))
1958 name += sizeof(unsigned long);
1959 len -= sizeof(unsigned long);
1961 x ^= a & bytemask_from_count(len);
1963 return fold_hash(x, y);
1965 EXPORT_SYMBOL(full_name_hash);
1967 /* Return the "hash_len" (hash and length) of a null-terminated string */
1968 u64 hashlen_string(const void *salt, const char *name)
1970 unsigned long a = 0, x = 0, y = (unsigned long)salt;
1971 unsigned long adata, mask, len;
1972 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1979 len += sizeof(unsigned long);
1981 a = load_unaligned_zeropad(name+len);
1982 } while (!has_zero(a, &adata, &constants));
1984 adata = prep_zero_mask(a, adata, &constants);
1985 mask = create_zero_mask(adata);
1986 x ^= a & zero_bytemask(mask);
1988 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
1990 EXPORT_SYMBOL(hashlen_string);
1993 * Calculate the length and hash of the path component, and
1994 * return the "hash_len" as the result.
1996 static inline u64 hash_name(const void *salt, const char *name)
1998 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
1999 unsigned long adata, bdata, mask, len;
2000 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
2007 len += sizeof(unsigned long);
2009 a = load_unaligned_zeropad(name+len);
2010 b = a ^ REPEAT_BYTE('/');
2011 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
2013 adata = prep_zero_mask(a, adata, &constants);
2014 bdata = prep_zero_mask(b, bdata, &constants);
2015 mask = create_zero_mask(adata | bdata);
2016 x ^= a & zero_bytemask(mask);
2018 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
2021 #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
2023 /* Return the hash of a string of known length */
2024 unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
2026 unsigned long hash = init_name_hash(salt);
2028 hash = partial_name_hash((unsigned char)*name++, hash);
2029 return end_name_hash(hash);
2031 EXPORT_SYMBOL(full_name_hash);
2033 /* Return the "hash_len" (hash and length) of a null-terminated string */
2034 u64 hashlen_string(const void *salt, const char *name)
2036 unsigned long hash = init_name_hash(salt);
2037 unsigned long len = 0, c;
2039 c = (unsigned char)*name;
2042 hash = partial_name_hash(c, hash);
2043 c = (unsigned char)name[len];
2045 return hashlen_create(end_name_hash(hash), len);
2047 EXPORT_SYMBOL(hashlen_string);
2050 * We know there's a real path component here of at least
2053 static inline u64 hash_name(const void *salt, const char *name)
2055 unsigned long hash = init_name_hash(salt);
2056 unsigned long len = 0, c;
2058 c = (unsigned char)*name;
2061 hash = partial_name_hash(c, hash);
2062 c = (unsigned char)name[len];
2063 } while (c && c != '/');
2064 return hashlen_create(end_name_hash(hash), len);
2071 * This is the basic name resolution function, turning a pathname into
2072 * the final dentry. We expect 'base' to be positive and a directory.
2074 * Returns 0 and nd will have valid dentry and mnt on success.
2075 * Returns error and drops reference to input namei data on failure.
2077 static int link_path_walk(const char *name, struct nameidata *nd)
2086 /* At this point we know we have a real path component. */
2091 err = may_lookup(nd);
2095 hash_len = hash_name(nd->path.dentry, name);
2098 if (name[0] == '.') switch (hashlen_len(hash_len)) {
2100 if (name[1] == '.') {
2102 nd->flags |= LOOKUP_JUMPED;
2108 if (likely(type == LAST_NORM)) {
2109 struct dentry *parent = nd->path.dentry;
2110 nd->flags &= ~LOOKUP_JUMPED;
2111 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
2112 struct qstr this = { { .hash_len = hash_len }, .name = name };
2113 err = parent->d_op->d_hash(parent, &this);
2116 hash_len = this.hash_len;
2121 nd->last.hash_len = hash_len;
2122 nd->last.name = name;
2123 nd->last_type = type;
2125 name += hashlen_len(hash_len);
2129 * If it wasn't NUL, we know it was '/'. Skip that
2130 * slash, and continue until no more slashes.
2134 } while (unlikely(*name == '/'));
2135 if (unlikely(!*name)) {
2137 /* pathname body, done */
2140 name = nd->stack[nd->depth - 1].name;
2141 /* trailing symlink, done */
2144 /* last component of nested symlink */
2145 err = walk_component(nd, WALK_GET | WALK_PUT);
2147 err = walk_component(nd, WALK_GET);
2153 const char *s = get_link(nd);
2162 nd->stack[nd->depth - 1].name = name;
2167 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2168 if (nd->flags & LOOKUP_RCU) {
2169 if (unlazy_walk(nd, NULL, 0))
2177 static const char *path_init(struct nameidata *nd, unsigned flags)
2180 const char *s = nd->name->name;
2183 flags &= ~LOOKUP_RCU;
2185 nd->last_type = LAST_ROOT; /* if there are only slashes... */
2186 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
2188 if (flags & LOOKUP_ROOT) {
2189 struct dentry *root = nd->root.dentry;
2190 struct inode *inode = root->d_inode;
2192 if (!d_can_lookup(root))
2193 return ERR_PTR(-ENOTDIR);
2194 retval = inode_permission(inode, MAY_EXEC);
2196 return ERR_PTR(retval);
2198 nd->path = nd->root;
2200 if (flags & LOOKUP_RCU) {
2202 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2203 nd->root_seq = nd->seq;
2204 nd->m_seq = read_seqbegin(&mount_lock);
2206 path_get(&nd->path);
2211 nd->root.mnt = NULL;
2212 nd->path.mnt = NULL;
2213 nd->path.dentry = NULL;
2215 nd->m_seq = read_seqbegin(&mount_lock);
2217 if (flags & LOOKUP_RCU)
2220 if (likely(!nd_jump_root(nd)))
2222 nd->root.mnt = NULL;
2224 return ERR_PTR(-ECHILD);
2225 } else if (nd->dfd == AT_FDCWD) {
2226 if (flags & LOOKUP_RCU) {
2227 struct fs_struct *fs = current->fs;
2233 seq = read_seqcount_begin(&fs->seq);
2235 nd->inode = nd->path.dentry->d_inode;
2236 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2237 } while (read_seqcount_retry(&fs->seq, seq));
2239 get_fs_pwd(current->fs, &nd->path);
2240 nd->inode = nd->path.dentry->d_inode;
2244 /* Caller must check execute permissions on the starting path component */
2245 struct fd f = fdget_raw(nd->dfd);
2246 struct dentry *dentry;
2249 return ERR_PTR(-EBADF);
2251 dentry = f.file->f_path.dentry;
2254 if (!d_can_lookup(dentry)) {
2256 return ERR_PTR(-ENOTDIR);
2260 nd->path = f.file->f_path;
2261 if (flags & LOOKUP_RCU) {
2263 nd->inode = nd->path.dentry->d_inode;
2264 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
2266 path_get(&nd->path);
2267 nd->inode = nd->path.dentry->d_inode;
2274 static const char *trailing_symlink(struct nameidata *nd)
2277 int error = may_follow_link(nd);
2278 if (unlikely(error))
2279 return ERR_PTR(error);
2280 nd->flags |= LOOKUP_PARENT;
2281 nd->stack[0].name = NULL;
2286 static inline int lookup_last(struct nameidata *nd)
2288 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2289 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2291 nd->flags &= ~LOOKUP_PARENT;
2292 return walk_component(nd,
2293 nd->flags & LOOKUP_FOLLOW
2295 ? WALK_PUT | WALK_GET
2300 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2301 static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
2303 const char *s = path_init(nd, flags);
2308 while (!(err = link_path_walk(s, nd))
2309 && ((err = lookup_last(nd)) > 0)) {
2310 s = trailing_symlink(nd);
2317 err = complete_walk(nd);
2319 if (!err && nd->flags & LOOKUP_DIRECTORY)
2320 if (!d_can_lookup(nd->path.dentry))
2324 nd->path.mnt = NULL;
2325 nd->path.dentry = NULL;
2331 static int filename_lookup(int dfd, struct filename *name, unsigned flags,
2332 struct path *path, struct path *root)
2335 struct nameidata nd;
2337 return PTR_ERR(name);
2338 if (unlikely(root)) {
2340 flags |= LOOKUP_ROOT;
2342 set_nameidata(&nd, dfd, name);
2343 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
2344 if (unlikely(retval == -ECHILD))
2345 retval = path_lookupat(&nd, flags, path);
2346 if (unlikely(retval == -ESTALE))
2347 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
2349 if (likely(!retval))
2350 audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
2351 restore_nameidata();
2356 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2357 static int path_parentat(struct nameidata *nd, unsigned flags,
2358 struct path *parent)
2360 const char *s = path_init(nd, flags);
2364 err = link_path_walk(s, nd);
2366 err = complete_walk(nd);
2369 nd->path.mnt = NULL;
2370 nd->path.dentry = NULL;
2376 static struct filename *filename_parentat(int dfd, struct filename *name,
2377 unsigned int flags, struct path *parent,
2378 struct qstr *last, int *type)
2381 struct nameidata nd;
2385 set_nameidata(&nd, dfd, name);
2386 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
2387 if (unlikely(retval == -ECHILD))
2388 retval = path_parentat(&nd, flags, parent);
2389 if (unlikely(retval == -ESTALE))
2390 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
2391 if (likely(!retval)) {
2393 *type = nd.last_type;
2394 audit_inode(name, parent->dentry, LOOKUP_PARENT);
2397 name = ERR_PTR(retval);
2399 restore_nameidata();
2403 /* does lookup, returns the object with parent locked */
2404 struct dentry *kern_path_locked(const char *name, struct path *path)
2406 struct filename *filename;
2411 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2413 if (IS_ERR(filename))
2414 return ERR_CAST(filename);
2415 if (unlikely(type != LAST_NORM)) {
2418 return ERR_PTR(-EINVAL);
2420 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
2421 d = __lookup_hash(&last, path->dentry, 0);
2423 inode_unlock(path->dentry->d_inode);
2430 int kern_path(const char *name, unsigned int flags, struct path *path)
2432 return filename_lookup(AT_FDCWD, getname_kernel(name),
2435 EXPORT_SYMBOL(kern_path);
2438 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2439 * @dentry: pointer to dentry of the base directory
2440 * @mnt: pointer to vfs mount of the base directory
2441 * @name: pointer to file name
2442 * @flags: lookup flags
2443 * @path: pointer to struct path to fill
2445 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2446 const char *name, unsigned int flags,
2449 struct path root = {.mnt = mnt, .dentry = dentry};
2450 /* the first argument of filename_lookup() is ignored with root */
2451 return filename_lookup(AT_FDCWD, getname_kernel(name),
2452 flags , path, &root);
2454 EXPORT_SYMBOL(vfs_path_lookup);
2457 * lookup_one_len - filesystem helper to lookup single pathname component
2458 * @name: pathname component to lookup
2459 * @base: base directory to lookup from
2460 * @len: maximum length @len should be interpreted to
2462 * Note that this routine is purely a helper for filesystem usage and should
2463 * not be called by generic code.
2465 * The caller must hold base->i_mutex.
2467 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2473 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2477 this.hash = full_name_hash(base, name, len);
2479 return ERR_PTR(-EACCES);
2481 if (unlikely(name[0] == '.')) {
2482 if (len < 2 || (len == 2 && name[1] == '.'))
2483 return ERR_PTR(-EACCES);
2487 c = *(const unsigned char *)name++;
2488 if (c == '/' || c == '\0')
2489 return ERR_PTR(-EACCES);
2492 * See if the low-level filesystem might want
2493 * to use its own hash..
2495 if (base->d_flags & DCACHE_OP_HASH) {
2496 int err = base->d_op->d_hash(base, &this);
2498 return ERR_PTR(err);
2501 err = inode_permission(base->d_inode, MAY_EXEC);
2503 return ERR_PTR(err);
2505 return __lookup_hash(&this, base, 0);
2507 EXPORT_SYMBOL(lookup_one_len);
2510 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2511 * @name: pathname component to lookup
2512 * @base: base directory to lookup from
2513 * @len: maximum length @len should be interpreted to
2515 * Note that this routine is purely a helper for filesystem usage and should
2516 * not be called by generic code.
2518 * Unlike lookup_one_len, it should be called without the parent
2519 * i_mutex held, and will take the i_mutex itself if necessary.
2521 struct dentry *lookup_one_len_unlocked(const char *name,
2522 struct dentry *base, int len)
2531 this.hash = full_name_hash(base, name, len);
2533 return ERR_PTR(-EACCES);
2535 if (unlikely(name[0] == '.')) {
2536 if (len < 2 || (len == 2 && name[1] == '.'))
2537 return ERR_PTR(-EACCES);
2541 c = *(const unsigned char *)name++;
2542 if (c == '/' || c == '\0')
2543 return ERR_PTR(-EACCES);
2546 * See if the low-level filesystem might want
2547 * to use its own hash..
2549 if (base->d_flags & DCACHE_OP_HASH) {
2550 int err = base->d_op->d_hash(base, &this);
2552 return ERR_PTR(err);
2555 err = inode_permission(base->d_inode, MAY_EXEC);
2557 return ERR_PTR(err);
2559 ret = lookup_dcache(&this, base, 0);
2561 ret = lookup_slow(&this, base, 0);
2564 EXPORT_SYMBOL(lookup_one_len_unlocked);
2566 #ifdef CONFIG_UNIX98_PTYS
2567 int path_pts(struct path *path)
2569 /* Find something mounted on "pts" in the same directory as
2572 struct dentry *child, *parent;
2576 ret = path_parent_directory(path);
2580 parent = path->dentry;
2583 child = d_hash_and_lookup(parent, &this);
2587 path->dentry = child;
2594 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2595 struct path *path, int *empty)
2597 return filename_lookup(dfd, getname_flags(name, flags, empty),
2600 EXPORT_SYMBOL(user_path_at_empty);
2603 * NB: most callers don't do anything directly with the reference to the
2604 * to struct filename, but the nd->last pointer points into the name string
2605 * allocated by getname. So we must hold the reference to it until all
2606 * path-walking is complete.
2608 static inline struct filename *
2609 user_path_parent(int dfd, const char __user *path,
2610 struct path *parent,
2615 /* only LOOKUP_REVAL is allowed in extra flags */
2616 return filename_parentat(dfd, getname(path), flags & LOOKUP_REVAL,
2617 parent, last, type);
2621 * mountpoint_last - look up last component for umount
2622 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2623 * @path: pointer to container for result
2625 * This is a special lookup_last function just for umount. In this case, we
2626 * need to resolve the path without doing any revalidation.
2628 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2629 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2630 * in almost all cases, this lookup will be served out of the dcache. The only
2631 * cases where it won't are if nd->last refers to a symlink or the path is
2632 * bogus and it doesn't exist.
2635 * -error: if there was an error during lookup. This includes -ENOENT if the
2636 * lookup found a negative dentry. The nd->path reference will also be
2639 * 0: if we successfully resolved nd->path and found it to not to be a
2640 * symlink that needs to be followed. "path" will also be populated.
2641 * The nd->path reference will also be put.
2643 * 1: if we successfully resolved nd->last and found it to be a symlink
2644 * that needs to be followed. "path" will be populated with the path
2645 * to the link, and nd->path will *not* be put.
2648 mountpoint_last(struct nameidata *nd, struct path *path)
2651 struct dentry *dentry;
2652 struct dentry *dir = nd->path.dentry;
2654 /* If we're in rcuwalk, drop out of it to handle last component */
2655 if (nd->flags & LOOKUP_RCU) {
2656 if (unlazy_walk(nd, NULL, 0))
2660 nd->flags &= ~LOOKUP_PARENT;
2662 if (unlikely(nd->last_type != LAST_NORM)) {
2663 error = handle_dots(nd, nd->last_type);
2666 dentry = dget(nd->path.dentry);
2668 dentry = d_lookup(dir, &nd->last);
2671 * No cached dentry. Mounted dentries are pinned in the
2672 * cache, so that means that this dentry is probably
2673 * a symlink or the path doesn't actually point
2674 * to a mounted dentry.
2676 dentry = lookup_slow(&nd->last, dir,
2677 nd->flags | LOOKUP_NO_REVAL);
2679 return PTR_ERR(dentry);
2682 if (d_is_negative(dentry)) {
2688 path->dentry = dentry;
2689 path->mnt = nd->path.mnt;
2690 error = should_follow_link(nd, path, nd->flags & LOOKUP_FOLLOW,
2691 d_backing_inode(dentry), 0);
2692 if (unlikely(error))
2700 * path_mountpoint - look up a path to be umounted
2701 * @nd: lookup context
2702 * @flags: lookup flags
2703 * @path: pointer to container for result
2705 * Look up the given name, but don't attempt to revalidate the last component.
2706 * Returns 0 and "path" will be valid on success; Returns error otherwise.
2709 path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
2711 const char *s = path_init(nd, flags);
2715 while (!(err = link_path_walk(s, nd)) &&
2716 (err = mountpoint_last(nd, path)) > 0) {
2717 s = trailing_symlink(nd);
2728 filename_mountpoint(int dfd, struct filename *name, struct path *path,
2731 struct nameidata nd;
2734 return PTR_ERR(name);
2735 set_nameidata(&nd, dfd, name);
2736 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
2737 if (unlikely(error == -ECHILD))
2738 error = path_mountpoint(&nd, flags, path);
2739 if (unlikely(error == -ESTALE))
2740 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
2742 audit_inode(name, path->dentry, 0);
2743 restore_nameidata();
2749 * user_path_mountpoint_at - lookup a path from userland in order to umount it
2750 * @dfd: directory file descriptor
2751 * @name: pathname from userland
2752 * @flags: lookup flags
2753 * @path: pointer to container to hold result
2755 * A umount is a special case for path walking. We're not actually interested
2756 * in the inode in this situation, and ESTALE errors can be a problem. We
2757 * simply want track down the dentry and vfsmount attached at the mountpoint
2758 * and avoid revalidating the last component.
2760 * Returns 0 and populates "path" on success.
2763 user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
2766 return filename_mountpoint(dfd, getname(name), path, flags);
2770 kern_path_mountpoint(int dfd, const char *name, struct path *path,
2773 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
2775 EXPORT_SYMBOL(kern_path_mountpoint);
2777 int __check_sticky(struct inode *dir, struct inode *inode)
2779 kuid_t fsuid = current_fsuid();
2781 if (uid_eq(inode->i_uid, fsuid))
2783 if (uid_eq(dir->i_uid, fsuid))
2785 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
2787 EXPORT_SYMBOL(__check_sticky);
2790 * Check whether we can remove a link victim from directory dir, check
2791 * whether the type of victim is right.
2792 * 1. We can't do it if dir is read-only (done in permission())
2793 * 2. We should have write and exec permissions on dir
2794 * 3. We can't remove anything from append-only dir
2795 * 4. We can't do anything with immutable dir (done in permission())
2796 * 5. If the sticky bit on dir is set we should either
2797 * a. be owner of dir, or
2798 * b. be owner of victim, or
2799 * c. have CAP_FOWNER capability
2800 * 6. If the victim is append-only or immutable we can't do antyhing with
2801 * links pointing to it.
2802 * 7. If the victim has an unknown uid or gid we can't change the inode.
2803 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2804 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2805 * 10. We can't remove a root or mountpoint.
2806 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
2807 * nfs_async_unlink().
2809 static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
2811 struct inode *inode = d_backing_inode(victim);
2814 if (d_is_negative(victim))
2818 BUG_ON(victim->d_parent->d_inode != dir);
2819 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
2821 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
2827 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2828 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
2831 if (!d_is_dir(victim))
2833 if (IS_ROOT(victim))
2835 } else if (d_is_dir(victim))
2837 if (IS_DEADDIR(dir))
2839 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2844 /* Check whether we can create an object with dentry child in directory
2846 * 1. We can't do it if child already exists (open has special treatment for
2847 * this case, but since we are inlined it's OK)
2848 * 2. We can't do it if dir is read-only (done in permission())
2849 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2850 * 4. We should have write and exec permissions on dir
2851 * 5. We can't do it if dir is immutable (done in permission())
2853 static inline int may_create(struct inode *dir, struct dentry *child)
2855 struct user_namespace *s_user_ns;
2856 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
2859 if (IS_DEADDIR(dir))
2861 s_user_ns = dir->i_sb->s_user_ns;
2862 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2863 !kgid_has_mapping(s_user_ns, current_fsgid()))
2865 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2869 * p1 and p2 should be directories on the same fs.
2871 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2876 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2880 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
2882 p = d_ancestor(p2, p1);
2884 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2885 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
2889 p = d_ancestor(p1, p2);
2891 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2892 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
2896 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2897 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
2900 EXPORT_SYMBOL(lock_rename);
2902 void unlock_rename(struct dentry *p1, struct dentry *p2)
2904 inode_unlock(p1->d_inode);
2906 inode_unlock(p2->d_inode);
2907 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
2910 EXPORT_SYMBOL(unlock_rename);
2912 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2915 int error = may_create(dir, dentry);
2919 if (!dir->i_op->create)
2920 return -EACCES; /* shouldn't it be ENOSYS? */
2923 error = security_inode_create(dir, dentry, mode);
2926 error = dir->i_op->create(dir, dentry, mode, want_excl);
2928 fsnotify_create(dir, dentry);
2931 EXPORT_SYMBOL(vfs_create);
2933 bool may_open_dev(const struct path *path)
2935 return !(path->mnt->mnt_flags & MNT_NODEV) &&
2936 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2939 static int may_open(struct path *path, int acc_mode, int flag)
2941 struct dentry *dentry = path->dentry;
2942 struct inode *inode = dentry->d_inode;
2948 switch (inode->i_mode & S_IFMT) {
2952 if (acc_mode & MAY_WRITE)
2957 if (!may_open_dev(path))
2966 error = inode_permission(inode, MAY_OPEN | acc_mode);
2971 * An append-only file must be opened in append mode for writing.
2973 if (IS_APPEND(inode)) {
2974 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2980 /* O_NOATIME can only be set by the owner or superuser */
2981 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2987 static int handle_truncate(struct file *filp)
2989 struct path *path = &filp->f_path;
2990 struct inode *inode = path->dentry->d_inode;
2991 int error = get_write_access(inode);
2995 * Refuse to truncate files with mandatory locks held on them.
2997 error = locks_verify_locked(filp);
2999 error = security_path_truncate(path);
3001 error = do_truncate(path->dentry, 0,
3002 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
3005 put_write_access(inode);
3009 static inline int open_to_namei_flags(int flag)
3011 if ((flag & O_ACCMODE) == 3)
3016 static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
3018 struct user_namespace *s_user_ns;
3019 int error = security_path_mknod(dir, dentry, mode, 0);
3023 s_user_ns = dir->dentry->d_sb->s_user_ns;
3024 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
3025 !kgid_has_mapping(s_user_ns, current_fsgid()))
3028 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
3032 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3036 * Attempt to atomically look up, create and open a file from a negative
3039 * Returns 0 if successful. The file will have been created and attached to
3040 * @file by the filesystem calling finish_open().
3042 * Returns 1 if the file was looked up only or didn't need creating. The
3043 * caller will need to perform the open themselves. @path will have been
3044 * updated to point to the new dentry. This may be negative.
3046 * Returns an error code otherwise.
3048 static int atomic_open(struct nameidata *nd, struct dentry *dentry,
3049 struct path *path, struct file *file,
3050 const struct open_flags *op,
3051 int open_flag, umode_t mode,
3054 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
3055 struct inode *dir = nd->path.dentry->d_inode;
3058 if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
3059 open_flag &= ~O_TRUNC;
3061 if (nd->flags & LOOKUP_DIRECTORY)
3062 open_flag |= O_DIRECTORY;
3064 file->f_path.dentry = DENTRY_NOT_SET;
3065 file->f_path.mnt = nd->path.mnt;
3066 error = dir->i_op->atomic_open(dir, dentry, file,
3067 open_to_namei_flags(open_flag),
3069 d_lookup_done(dentry);
3072 * We didn't have the inode before the open, so check open
3075 int acc_mode = op->acc_mode;
3076 if (*opened & FILE_CREATED) {
3077 WARN_ON(!(open_flag & O_CREAT));
3078 fsnotify_create(dir, dentry);
3081 error = may_open(&file->f_path, acc_mode, open_flag);
3082 if (WARN_ON(error > 0))
3084 } else if (error > 0) {
3085 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
3088 if (file->f_path.dentry) {
3090 dentry = file->f_path.dentry;
3092 if (*opened & FILE_CREATED)
3093 fsnotify_create(dir, dentry);
3094 if (unlikely(d_is_negative(dentry))) {
3097 path->dentry = dentry;
3098 path->mnt = nd->path.mnt;
3108 * Look up and maybe create and open the last component.
3110 * Must be called with i_mutex held on parent.
3112 * Returns 0 if the file was successfully atomically created (if necessary) and
3113 * opened. In this case the file will be returned attached to @file.
3115 * Returns 1 if the file was not completely opened at this time, though lookups
3116 * and creations will have been performed and the dentry returned in @path will
3117 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
3118 * specified then a negative dentry may be returned.
3120 * An error code is returned otherwise.
3122 * FILE_CREATE will be set in @*opened if the dentry was created and will be
3123 * cleared otherwise prior to returning.
3125 static int lookup_open(struct nameidata *nd, struct path *path,
3127 const struct open_flags *op,
3128 bool got_write, int *opened)
3130 struct dentry *dir = nd->path.dentry;
3131 struct inode *dir_inode = dir->d_inode;
3132 int open_flag = op->open_flag;
3133 struct dentry *dentry;
3134 int error, create_error = 0;
3135 umode_t mode = op->mode;
3136 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
3138 if (unlikely(IS_DEADDIR(dir_inode)))
3141 *opened &= ~FILE_CREATED;
3142 dentry = d_lookup(dir, &nd->last);
3145 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3147 return PTR_ERR(dentry);
3149 if (d_in_lookup(dentry))
3152 if (!(dentry->d_flags & DCACHE_OP_REVALIDATE))
3155 error = d_revalidate(dentry, nd->flags);
3156 if (likely(error > 0))
3160 d_invalidate(dentry);
3164 if (dentry->d_inode) {
3165 /* Cached positive dentry: will open in f_op->open */
3170 * Checking write permission is tricky, bacuse we don't know if we are
3171 * going to actually need it: O_CREAT opens should work as long as the
3172 * file exists. But checking existence breaks atomicity. The trick is
3173 * to check access and if not granted clear O_CREAT from the flags.
3175 * Another problem is returing the "right" error value (e.g. for an
3176 * O_EXCL open we want to return EEXIST not EROFS).
3178 if (open_flag & O_CREAT) {
3179 if (!IS_POSIXACL(dir->d_inode))
3180 mode &= ~current_umask();
3181 if (unlikely(!got_write)) {
3182 create_error = -EROFS;
3183 open_flag &= ~O_CREAT;
3184 if (open_flag & (O_EXCL | O_TRUNC))
3186 /* No side effects, safe to clear O_CREAT */
3188 create_error = may_o_create(&nd->path, dentry, mode);
3190 open_flag &= ~O_CREAT;
3191 if (open_flag & O_EXCL)
3195 } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
3196 unlikely(!got_write)) {
3198 * No O_CREATE -> atomicity not a requirement -> fall
3199 * back to lookup + open
3204 if (dir_inode->i_op->atomic_open) {
3205 error = atomic_open(nd, dentry, path, file, op, open_flag,
3207 if (unlikely(error == -ENOENT) && create_error)
3208 error = create_error;
3213 if (d_in_lookup(dentry)) {
3214 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3216 d_lookup_done(dentry);
3217 if (unlikely(res)) {
3219 error = PTR_ERR(res);
3227 /* Negative dentry, just create the file */
3228 if (!dentry->d_inode && (open_flag & O_CREAT)) {
3229 *opened |= FILE_CREATED;
3230 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
3231 if (!dir_inode->i_op->create) {
3235 error = dir_inode->i_op->create(dir_inode, dentry, mode,
3236 open_flag & O_EXCL);
3239 fsnotify_create(dir_inode, dentry);
3241 if (unlikely(create_error) && !dentry->d_inode) {
3242 error = create_error;
3246 path->dentry = dentry;
3247 path->mnt = nd->path.mnt;
3256 * Handle the last step of open()
3258 static int do_last(struct nameidata *nd,
3259 struct file *file, const struct open_flags *op,
3262 struct dentry *dir = nd->path.dentry;
3263 kuid_t dir_uid = nd->inode->i_uid;
3264 umode_t dir_mode = nd->inode->i_mode;
3265 int open_flag = op->open_flag;
3266 bool will_truncate = (open_flag & O_TRUNC) != 0;
3267 bool got_write = false;
3268 int acc_mode = op->acc_mode;
3270 struct inode *inode;
3274 nd->flags &= ~LOOKUP_PARENT;
3275 nd->flags |= op->intent;
3277 if (nd->last_type != LAST_NORM) {
3278 error = handle_dots(nd, nd->last_type);
3279 if (unlikely(error))
3284 if (!(open_flag & O_CREAT)) {
3285 if (nd->last.name[nd->last.len])
3286 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3287 /* we _can_ be in RCU mode here */
3288 error = lookup_fast(nd, &path, &inode, &seq);
3289 if (likely(error > 0))
3295 BUG_ON(nd->inode != dir->d_inode);
3296 BUG_ON(nd->flags & LOOKUP_RCU);
3298 /* create side of things */
3300 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3301 * has been cleared when we got to the last component we are
3304 error = complete_walk(nd);
3308 audit_inode(nd->name, dir, LOOKUP_PARENT);
3309 /* trailing slashes? */
3310 if (unlikely(nd->last.name[nd->last.len]))
3314 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
3315 error = mnt_want_write(nd->path.mnt);
3319 * do _not_ fail yet - we might not need that or fail with
3320 * a different error; let lookup_open() decide; we'll be
3321 * dropping this one anyway.
3324 if (open_flag & O_CREAT)
3325 inode_lock(dir->d_inode);
3327 inode_lock_shared(dir->d_inode);
3328 error = lookup_open(nd, &path, file, op, got_write, opened);
3329 if (open_flag & O_CREAT)
3330 inode_unlock(dir->d_inode);
3332 inode_unlock_shared(dir->d_inode);
3338 if ((*opened & FILE_CREATED) ||
3339 !S_ISREG(file_inode(file)->i_mode))
3340 will_truncate = false;
3342 audit_inode(nd->name, file->f_path.dentry, 0);
3346 if (*opened & FILE_CREATED) {
3347 /* Don't check for write permission, don't truncate */
3348 open_flag &= ~O_TRUNC;
3349 will_truncate = false;
3351 path_to_nameidata(&path, nd);
3352 goto finish_open_created;
3356 * If atomic_open() acquired write access it is dropped now due to
3357 * possible mount and symlink following (this might be optimized away if
3361 mnt_drop_write(nd->path.mnt);
3365 error = follow_managed(&path, nd);
3366 if (unlikely(error < 0))
3369 if (unlikely(d_is_negative(path.dentry))) {
3370 path_to_nameidata(&path, nd);
3375 * create/update audit record if it already exists.
3377 audit_inode(nd->name, path.dentry, 0);
3379 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3380 path_to_nameidata(&path, nd);
3384 seq = 0; /* out of RCU mode, so the value doesn't matter */
3385 inode = d_backing_inode(path.dentry);
3389 error = should_follow_link(nd, &path, nd->flags & LOOKUP_FOLLOW,
3391 if (unlikely(error))
3394 path_to_nameidata(&path, nd);
3397 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
3399 error = complete_walk(nd);
3402 audit_inode(nd->name, nd->path.dentry, 0);
3403 if (open_flag & O_CREAT) {
3405 if (d_is_dir(nd->path.dentry))
3407 error = may_create_in_sticky(dir_mode, dir_uid,
3408 d_backing_inode(nd->path.dentry));
3409 if (unlikely(error))
3413 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
3415 if (!d_is_reg(nd->path.dentry))
3416 will_truncate = false;
3418 if (will_truncate) {
3419 error = mnt_want_write(nd->path.mnt);
3424 finish_open_created:
3425 error = may_open(&nd->path, acc_mode, open_flag);
3428 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3429 error = vfs_open(&nd->path, file, current_cred());
3432 *opened |= FILE_OPENED;
3434 error = open_check_o_direct(file);
3436 error = ima_file_check(file, op->acc_mode, *opened);
3437 if (!error && will_truncate)
3438 error = handle_truncate(file);
3440 if (unlikely(error) && (*opened & FILE_OPENED))
3442 if (unlikely(error > 0)) {
3447 mnt_drop_write(nd->path.mnt);
3451 static int do_tmpfile(struct nameidata *nd, unsigned flags,
3452 const struct open_flags *op,
3453 struct file *file, int *opened)
3455 static const struct qstr name = QSTR_INIT("/", 1);
3456 struct dentry *child;
3459 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
3460 if (unlikely(error))
3462 error = mnt_want_write(path.mnt);
3463 if (unlikely(error))
3465 dir = path.dentry->d_inode;
3466 /* we want directory to be writable */
3467 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
3470 if (!dir->i_op->tmpfile) {
3471 error = -EOPNOTSUPP;
3474 child = d_alloc(path.dentry, &name);
3475 if (unlikely(!child)) {
3480 path.dentry = child;
3481 error = dir->i_op->tmpfile(dir, child, op->mode);
3484 audit_inode(nd->name, child, 0);
3485 /* Don't check for other permissions, the inode was just created */
3486 error = may_open(&path, 0, op->open_flag);
3489 file->f_path.mnt = path.mnt;
3490 error = finish_open(file, child, NULL, opened);
3493 error = open_check_o_direct(file);
3496 } else if (!(op->open_flag & O_EXCL)) {
3497 struct inode *inode = file_inode(file);
3498 spin_lock(&inode->i_lock);
3499 inode->i_state |= I_LINKABLE;
3500 spin_unlock(&inode->i_lock);
3503 mnt_drop_write(path.mnt);
3509 static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3512 int error = path_lookupat(nd, flags, &path);
3514 audit_inode(nd->name, path.dentry, 0);
3515 error = vfs_open(&path, file, current_cred());
3521 static struct file *path_openat(struct nameidata *nd,
3522 const struct open_flags *op, unsigned flags)
3529 file = get_empty_filp();
3533 file->f_flags = op->open_flag;
3535 if (unlikely(file->f_flags & __O_TMPFILE)) {
3536 error = do_tmpfile(nd, flags, op, file, &opened);
3540 if (unlikely(file->f_flags & O_PATH)) {
3541 error = do_o_path(nd, flags, file);
3543 opened |= FILE_OPENED;
3547 s = path_init(nd, flags);
3552 while (!(error = link_path_walk(s, nd)) &&
3553 (error = do_last(nd, file, op, &opened)) > 0) {
3554 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3555 s = trailing_symlink(nd);
3563 if (!(opened & FILE_OPENED)) {
3567 if (unlikely(error)) {
3568 if (error == -EOPENSTALE) {
3569 if (flags & LOOKUP_RCU)
3574 file = ERR_PTR(error);
3579 struct file *do_filp_open(int dfd, struct filename *pathname,
3580 const struct open_flags *op)
3582 struct nameidata nd;
3583 int flags = op->lookup_flags;
3586 set_nameidata(&nd, dfd, pathname);
3587 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
3588 if (unlikely(filp == ERR_PTR(-ECHILD)))
3589 filp = path_openat(&nd, op, flags);
3590 if (unlikely(filp == ERR_PTR(-ESTALE)))
3591 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
3592 restore_nameidata();
3596 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3597 const char *name, const struct open_flags *op)
3599 struct nameidata nd;
3601 struct filename *filename;
3602 int flags = op->lookup_flags | LOOKUP_ROOT;
3605 nd.root.dentry = dentry;
3607 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
3608 return ERR_PTR(-ELOOP);
3610 filename = getname_kernel(name);
3611 if (IS_ERR(filename))
3612 return ERR_CAST(filename);
3614 set_nameidata(&nd, -1, filename);
3615 file = path_openat(&nd, op, flags | LOOKUP_RCU);
3616 if (unlikely(file == ERR_PTR(-ECHILD)))
3617 file = path_openat(&nd, op, flags);
3618 if (unlikely(file == ERR_PTR(-ESTALE)))
3619 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
3620 restore_nameidata();
3625 static struct dentry *filename_create(int dfd, struct filename *name,
3626 struct path *path, unsigned int lookup_flags)
3628 struct dentry *dentry = ERR_PTR(-EEXIST);
3633 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3636 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3637 * other flags passed in are ignored!
3639 lookup_flags &= LOOKUP_REVAL;
3641 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3643 return ERR_CAST(name);
3646 * Yucky last component or no last component at all?
3647 * (foo/., foo/.., /////)
3649 if (unlikely(type != LAST_NORM))
3652 /* don't fail immediately if it's r/o, at least try to report other errors */
3653 err2 = mnt_want_write(path->mnt);
3655 * Do the final lookup.
3657 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3658 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
3659 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
3664 if (d_is_positive(dentry))
3668 * Special case - lookup gave negative, but... we had foo/bar/
3669 * From the vfs_mknod() POV we just have a negative dentry -
3670 * all is fine. Let's be bastards - you had / on the end, you've
3671 * been asking for (non-existent) directory. -ENOENT for you.
3673 if (unlikely(!is_dir && last.name[last.len])) {
3677 if (unlikely(err2)) {
3685 dentry = ERR_PTR(error);
3687 inode_unlock(path->dentry->d_inode);
3689 mnt_drop_write(path->mnt);
3696 struct dentry *kern_path_create(int dfd, const char *pathname,
3697 struct path *path, unsigned int lookup_flags)
3699 return filename_create(dfd, getname_kernel(pathname),
3700 path, lookup_flags);
3702 EXPORT_SYMBOL(kern_path_create);
3704 void done_path_create(struct path *path, struct dentry *dentry)
3707 inode_unlock(path->dentry->d_inode);
3708 mnt_drop_write(path->mnt);
3711 EXPORT_SYMBOL(done_path_create);
3713 inline struct dentry *user_path_create(int dfd, const char __user *pathname,
3714 struct path *path, unsigned int lookup_flags)
3716 return filename_create(dfd, getname(pathname), path, lookup_flags);
3718 EXPORT_SYMBOL(user_path_create);
3720 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3722 int error = may_create(dir, dentry);
3727 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
3730 if (!dir->i_op->mknod)
3733 error = devcgroup_inode_mknod(mode, dev);
3737 error = security_inode_mknod(dir, dentry, mode, dev);
3741 error = dir->i_op->mknod(dir, dentry, mode, dev);
3743 fsnotify_create(dir, dentry);
3746 EXPORT_SYMBOL(vfs_mknod);
3748 static int may_mknod(umode_t mode)
3750 switch (mode & S_IFMT) {
3756 case 0: /* zero mode translates to S_IFREG */
3765 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3768 struct dentry *dentry;
3771 unsigned int lookup_flags = 0;
3773 error = may_mknod(mode);
3777 dentry = user_path_create(dfd, filename, &path, lookup_flags);
3779 return PTR_ERR(dentry);
3781 if (!IS_POSIXACL(path.dentry->d_inode))
3782 mode &= ~current_umask();
3783 error = security_path_mknod(&path, dentry, mode, dev);
3786 switch (mode & S_IFMT) {
3787 case 0: case S_IFREG:
3788 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
3790 ima_post_path_mknod(dentry);
3792 case S_IFCHR: case S_IFBLK:
3793 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
3794 new_decode_dev(dev));
3796 case S_IFIFO: case S_IFSOCK:
3797 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
3801 done_path_create(&path, dentry);
3802 if (retry_estale(error, lookup_flags)) {
3803 lookup_flags |= LOOKUP_REVAL;
3809 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
3811 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3814 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3816 int error = may_create(dir, dentry);
3817 unsigned max_links = dir->i_sb->s_max_links;
3822 if (!dir->i_op->mkdir)
3825 mode &= (S_IRWXUGO|S_ISVTX);
3826 error = security_inode_mkdir(dir, dentry, mode);
3830 if (max_links && dir->i_nlink >= max_links)
3833 error = dir->i_op->mkdir(dir, dentry, mode);
3835 fsnotify_mkdir(dir, dentry);
3838 EXPORT_SYMBOL(vfs_mkdir);
3840 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3842 struct dentry *dentry;
3845 unsigned int lookup_flags = LOOKUP_DIRECTORY;
3848 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
3850 return PTR_ERR(dentry);
3852 if (!IS_POSIXACL(path.dentry->d_inode))
3853 mode &= ~current_umask();
3854 error = security_path_mkdir(&path, dentry, mode);
3856 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3857 done_path_create(&path, dentry);
3858 if (retry_estale(error, lookup_flags)) {
3859 lookup_flags |= LOOKUP_REVAL;
3865 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
3867 return sys_mkdirat(AT_FDCWD, pathname, mode);
3870 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3872 int error = may_delete(dir, dentry, 1);
3877 if (!dir->i_op->rmdir)
3881 inode_lock(dentry->d_inode);
3884 if (is_local_mountpoint(dentry))
3887 error = security_inode_rmdir(dir, dentry);
3891 shrink_dcache_parent(dentry);
3892 error = dir->i_op->rmdir(dir, dentry);
3896 dentry->d_inode->i_flags |= S_DEAD;
3898 detach_mounts(dentry);
3901 inode_unlock(dentry->d_inode);
3907 EXPORT_SYMBOL(vfs_rmdir);
3909 static long do_rmdir(int dfd, const char __user *pathname)
3912 struct filename *name;
3913 struct dentry *dentry;
3917 unsigned int lookup_flags = 0;
3919 name = user_path_parent(dfd, pathname,
3920 &path, &last, &type, lookup_flags);
3922 return PTR_ERR(name);
3936 error = mnt_want_write(path.mnt);
3940 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
3941 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
3942 error = PTR_ERR(dentry);
3945 if (!dentry->d_inode) {
3949 error = security_path_rmdir(&path, dentry);
3952 error = vfs_rmdir(path.dentry->d_inode, dentry);
3956 inode_unlock(path.dentry->d_inode);
3957 mnt_drop_write(path.mnt);
3961 if (retry_estale(error, lookup_flags)) {
3962 lookup_flags |= LOOKUP_REVAL;
3968 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
3970 return do_rmdir(AT_FDCWD, pathname);
3974 * vfs_unlink - unlink a filesystem object
3975 * @dir: parent directory
3977 * @delegated_inode: returns victim inode, if the inode is delegated.
3979 * The caller must hold dir->i_mutex.
3981 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3982 * return a reference to the inode in delegated_inode. The caller
3983 * should then break the delegation on that inode and retry. Because
3984 * breaking a delegation may take a long time, the caller should drop
3985 * dir->i_mutex before doing so.
3987 * Alternatively, a caller may pass NULL for delegated_inode. This may
3988 * be appropriate for callers that expect the underlying filesystem not
3989 * to be NFS exported.
3991 int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
3993 struct inode *target = dentry->d_inode;
3994 int error = may_delete(dir, dentry, 0);
3999 if (!dir->i_op->unlink)
4003 if (is_local_mountpoint(dentry))
4006 error = security_inode_unlink(dir, dentry);
4008 error = try_break_deleg(target, delegated_inode);
4011 error = dir->i_op->unlink(dir, dentry);
4014 detach_mounts(dentry);
4019 inode_unlock(target);
4021 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
4022 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
4023 fsnotify_link_count(target);
4029 EXPORT_SYMBOL(vfs_unlink);
4032 * Make sure that the actual truncation of the file will occur outside its
4033 * directory's i_mutex. Truncate can take a long time if there is a lot of
4034 * writeout happening, and we don't want to prevent access to the directory
4035 * while waiting on the I/O.
4037 static long do_unlinkat(int dfd, const char __user *pathname)
4040 struct filename *name;
4041 struct dentry *dentry;
4045 struct inode *inode = NULL;
4046 struct inode *delegated_inode = NULL;
4047 unsigned int lookup_flags = 0;
4049 name = user_path_parent(dfd, pathname,
4050 &path, &last, &type, lookup_flags);
4052 return PTR_ERR(name);
4055 if (type != LAST_NORM)
4058 error = mnt_want_write(path.mnt);
4062 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
4063 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
4064 error = PTR_ERR(dentry);
4065 if (!IS_ERR(dentry)) {
4066 /* Why not before? Because we want correct error value */
4067 if (last.name[last.len])
4069 inode = dentry->d_inode;
4070 if (d_is_negative(dentry))
4073 error = security_path_unlink(&path, dentry);
4076 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
4080 inode_unlock(path.dentry->d_inode);
4082 iput(inode); /* truncate the inode here */
4084 if (delegated_inode) {
4085 error = break_deleg_wait(&delegated_inode);
4089 mnt_drop_write(path.mnt);
4093 if (retry_estale(error, lookup_flags)) {
4094 lookup_flags |= LOOKUP_REVAL;
4101 if (d_is_negative(dentry))
4103 else if (d_is_dir(dentry))
4110 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
4112 if ((flag & ~AT_REMOVEDIR) != 0)
4115 if (flag & AT_REMOVEDIR)
4116 return do_rmdir(dfd, pathname);
4118 return do_unlinkat(dfd, pathname);
4121 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
4123 return do_unlinkat(AT_FDCWD, pathname);
4126 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
4128 int error = may_create(dir, dentry);
4133 if (!dir->i_op->symlink)
4136 error = security_inode_symlink(dir, dentry, oldname);
4140 error = dir->i_op->symlink(dir, dentry, oldname);
4142 fsnotify_create(dir, dentry);
4145 EXPORT_SYMBOL(vfs_symlink);
4147 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4148 int, newdfd, const char __user *, newname)
4151 struct filename *from;
4152 struct dentry *dentry;
4154 unsigned int lookup_flags = 0;
4156 from = getname(oldname);
4158 return PTR_ERR(from);
4160 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
4161 error = PTR_ERR(dentry);
4165 error = security_path_symlink(&path, dentry, from->name);
4167 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
4168 done_path_create(&path, dentry);
4169 if (retry_estale(error, lookup_flags)) {
4170 lookup_flags |= LOOKUP_REVAL;
4178 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
4180 return sys_symlinkat(oldname, AT_FDCWD, newname);
4184 * vfs_link - create a new link
4185 * @old_dentry: object to be linked
4187 * @new_dentry: where to create the new link
4188 * @delegated_inode: returns inode needing a delegation break
4190 * The caller must hold dir->i_mutex
4192 * If vfs_link discovers a delegation on the to-be-linked file in need
4193 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4194 * inode in delegated_inode. The caller should then break the delegation
4195 * and retry. Because breaking a delegation may take a long time, the
4196 * caller should drop the i_mutex before doing so.
4198 * Alternatively, a caller may pass NULL for delegated_inode. This may
4199 * be appropriate for callers that expect the underlying filesystem not
4200 * to be NFS exported.
4202 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
4204 struct inode *inode = old_dentry->d_inode;
4205 unsigned max_links = dir->i_sb->s_max_links;
4211 error = may_create(dir, new_dentry);
4215 if (dir->i_sb != inode->i_sb)
4219 * A link to an append-only or immutable file cannot be created.
4221 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4224 * Updating the link count will likely cause i_uid and i_gid to
4225 * be writen back improperly if their true value is unknown to
4228 if (HAS_UNMAPPED_ID(inode))
4230 if (!dir->i_op->link)
4232 if (S_ISDIR(inode->i_mode))
4235 error = security_inode_link(old_dentry, dir, new_dentry);
4240 /* Make sure we don't allow creating hardlink to an unlinked file */
4241 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
4243 else if (max_links && inode->i_nlink >= max_links)
4246 error = try_break_deleg(inode, delegated_inode);
4248 error = dir->i_op->link(old_dentry, dir, new_dentry);
4251 if (!error && (inode->i_state & I_LINKABLE)) {
4252 spin_lock(&inode->i_lock);
4253 inode->i_state &= ~I_LINKABLE;
4254 spin_unlock(&inode->i_lock);
4256 inode_unlock(inode);
4258 fsnotify_link(dir, inode, new_dentry);
4261 EXPORT_SYMBOL(vfs_link);
4264 * Hardlinks are often used in delicate situations. We avoid
4265 * security-related surprises by not following symlinks on the
4268 * We don't follow them on the oldname either to be compatible
4269 * with linux 2.0, and to avoid hard-linking to directories
4270 * and other special files. --ADM
4272 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4273 int, newdfd, const char __user *, newname, int, flags)
4275 struct dentry *new_dentry;
4276 struct path old_path, new_path;
4277 struct inode *delegated_inode = NULL;
4281 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
4284 * To use null names we require CAP_DAC_READ_SEARCH
4285 * This ensures that not everyone will be able to create
4286 * handlink using the passed filedescriptor.
4288 if (flags & AT_EMPTY_PATH) {
4289 if (!capable(CAP_DAC_READ_SEARCH))
4294 if (flags & AT_SYMLINK_FOLLOW)
4295 how |= LOOKUP_FOLLOW;
4297 error = user_path_at(olddfd, oldname, how, &old_path);
4301 new_dentry = user_path_create(newdfd, newname, &new_path,
4302 (how & LOOKUP_REVAL));
4303 error = PTR_ERR(new_dentry);
4304 if (IS_ERR(new_dentry))
4308 if (old_path.mnt != new_path.mnt)
4310 error = may_linkat(&old_path);
4311 if (unlikely(error))
4313 error = security_path_link(old_path.dentry, &new_path, new_dentry);
4316 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
4318 done_path_create(&new_path, new_dentry);
4319 if (delegated_inode) {
4320 error = break_deleg_wait(&delegated_inode);
4322 path_put(&old_path);
4326 if (retry_estale(error, how)) {
4327 path_put(&old_path);
4328 how |= LOOKUP_REVAL;
4332 path_put(&old_path);
4337 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
4339 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
4343 * vfs_rename - rename a filesystem object
4344 * @old_dir: parent of source
4345 * @old_dentry: source
4346 * @new_dir: parent of destination
4347 * @new_dentry: destination
4348 * @delegated_inode: returns an inode needing a delegation break
4349 * @flags: rename flags
4351 * The caller must hold multiple mutexes--see lock_rename()).
4353 * If vfs_rename discovers a delegation in need of breaking at either
4354 * the source or destination, it will return -EWOULDBLOCK and return a
4355 * reference to the inode in delegated_inode. The caller should then
4356 * break the delegation and retry. Because breaking a delegation may
4357 * take a long time, the caller should drop all locks before doing
4360 * Alternatively, a caller may pass NULL for delegated_inode. This may
4361 * be appropriate for callers that expect the underlying filesystem not
4362 * to be NFS exported.
4364 * The worst of all namespace operations - renaming directory. "Perverted"
4365 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4367 * a) we can get into loop creation.
4368 * b) race potential - two innocent renames can create a loop together.
4369 * That's where 4.4 screws up. Current fix: serialization on
4370 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
4372 * c) we have to lock _four_ objects - parents and victim (if it exists),
4373 * and source (if it is not a directory).
4374 * And that - after we got ->i_mutex on parents (until then we don't know
4375 * whether the target exists). Solution: try to be smart with locking
4376 * order for inodes. We rely on the fact that tree topology may change
4377 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
4378 * move will be locked. Thus we can rank directories by the tree
4379 * (ancestors first) and rank all non-directories after them.
4380 * That works since everybody except rename does "lock parent, lookup,
4381 * lock child" and rename is under ->s_vfs_rename_mutex.
4382 * HOWEVER, it relies on the assumption that any object with ->lookup()
4383 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4384 * we'd better make sure that there's no link(2) for them.
4385 * d) conversion from fhandle to dentry may come in the wrong moment - when
4386 * we are removing the target. Solution: we will have to grab ->i_mutex
4387 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4388 * ->i_mutex on parents, which works but leads to some truly excessive
4391 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4392 struct inode *new_dir, struct dentry *new_dentry,
4393 struct inode **delegated_inode, unsigned int flags)
4396 bool is_dir = d_is_dir(old_dentry);
4397 struct inode *source = old_dentry->d_inode;
4398 struct inode *target = new_dentry->d_inode;
4399 bool new_is_dir = false;
4400 unsigned max_links = new_dir->i_sb->s_max_links;
4401 struct name_snapshot old_name;
4404 * Check source == target.
4405 * On overlayfs need to look at underlying inodes.
4407 if (d_real_inode(old_dentry) == d_real_inode(new_dentry))
4410 error = may_delete(old_dir, old_dentry, is_dir);
4415 error = may_create(new_dir, new_dentry);
4417 new_is_dir = d_is_dir(new_dentry);
4419 if (!(flags & RENAME_EXCHANGE))
4420 error = may_delete(new_dir, new_dentry, is_dir);
4422 error = may_delete(new_dir, new_dentry, new_is_dir);
4427 if (!old_dir->i_op->rename)
4431 * If we are going to change the parent - check write permissions,
4432 * we'll need to flip '..'.
4434 if (new_dir != old_dir) {
4436 error = inode_permission(source, MAY_WRITE);
4440 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4441 error = inode_permission(target, MAY_WRITE);
4447 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4452 take_dentry_name_snapshot(&old_name, old_dentry);
4454 if (!is_dir || (flags & RENAME_EXCHANGE))
4455 lock_two_nondirectories(source, target);
4460 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
4463 if (max_links && new_dir != old_dir) {
4465 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
4467 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4468 old_dir->i_nlink >= max_links)
4471 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4472 shrink_dcache_parent(new_dentry);
4474 error = try_break_deleg(source, delegated_inode);
4478 if (target && !new_is_dir) {
4479 error = try_break_deleg(target, delegated_inode);
4483 error = old_dir->i_op->rename(old_dir, old_dentry,
4484 new_dir, new_dentry, flags);
4488 if (!(flags & RENAME_EXCHANGE) && target) {
4490 target->i_flags |= S_DEAD;
4491 dont_mount(new_dentry);
4492 detach_mounts(new_dentry);
4494 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4495 if (!(flags & RENAME_EXCHANGE))
4496 d_move(old_dentry, new_dentry);
4498 d_exchange(old_dentry, new_dentry);
4501 if (!is_dir || (flags & RENAME_EXCHANGE))
4502 unlock_two_nondirectories(source, target);
4504 inode_unlock(target);
4507 fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
4508 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4509 if (flags & RENAME_EXCHANGE) {
4510 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4511 new_is_dir, NULL, new_dentry);
4514 release_dentry_name_snapshot(&old_name);
4518 EXPORT_SYMBOL(vfs_rename);
4520 SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4521 int, newdfd, const char __user *, newname, unsigned int, flags)
4523 struct dentry *old_dentry, *new_dentry;
4524 struct dentry *trap;
4525 struct path old_path, new_path;
4526 struct qstr old_last, new_last;
4527 int old_type, new_type;
4528 struct inode *delegated_inode = NULL;
4529 struct filename *from;
4530 struct filename *to;
4531 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
4532 bool should_retry = false;
4535 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
4538 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4539 (flags & RENAME_EXCHANGE))
4542 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4545 if (flags & RENAME_EXCHANGE)
4549 from = user_path_parent(olddfd, oldname,
4550 &old_path, &old_last, &old_type, lookup_flags);
4552 error = PTR_ERR(from);
4556 to = user_path_parent(newdfd, newname,
4557 &new_path, &new_last, &new_type, lookup_flags);
4559 error = PTR_ERR(to);
4564 if (old_path.mnt != new_path.mnt)
4568 if (old_type != LAST_NORM)
4571 if (flags & RENAME_NOREPLACE)
4573 if (new_type != LAST_NORM)
4576 error = mnt_want_write(old_path.mnt);
4581 trap = lock_rename(new_path.dentry, old_path.dentry);
4583 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
4584 error = PTR_ERR(old_dentry);
4585 if (IS_ERR(old_dentry))
4587 /* source must exist */
4589 if (d_is_negative(old_dentry))
4591 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
4592 error = PTR_ERR(new_dentry);
4593 if (IS_ERR(new_dentry))
4596 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4598 if (flags & RENAME_EXCHANGE) {
4600 if (d_is_negative(new_dentry))
4603 if (!d_is_dir(new_dentry)) {
4605 if (new_last.name[new_last.len])
4609 /* unless the source is a directory trailing slashes give -ENOTDIR */
4610 if (!d_is_dir(old_dentry)) {
4612 if (old_last.name[old_last.len])
4614 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
4617 /* source should not be ancestor of target */
4619 if (old_dentry == trap)
4621 /* target should not be an ancestor of source */
4622 if (!(flags & RENAME_EXCHANGE))
4624 if (new_dentry == trap)
4627 error = security_path_rename(&old_path, old_dentry,
4628 &new_path, new_dentry, flags);
4631 error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4632 new_path.dentry->d_inode, new_dentry,
4633 &delegated_inode, flags);
4639 unlock_rename(new_path.dentry, old_path.dentry);
4640 if (delegated_inode) {
4641 error = break_deleg_wait(&delegated_inode);
4645 mnt_drop_write(old_path.mnt);
4647 if (retry_estale(error, lookup_flags))
4648 should_retry = true;
4649 path_put(&new_path);
4652 path_put(&old_path);
4655 should_retry = false;
4656 lookup_flags |= LOOKUP_REVAL;
4663 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4664 int, newdfd, const char __user *, newname)
4666 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4669 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
4671 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
4674 int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4676 int error = may_create(dir, dentry);
4680 if (!dir->i_op->mknod)
4683 return dir->i_op->mknod(dir, dentry,
4684 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4686 EXPORT_SYMBOL(vfs_whiteout);
4688 int readlink_copy(char __user *buffer, int buflen, const char *link)
4690 int len = PTR_ERR(link);
4695 if (len > (unsigned) buflen)
4697 if (copy_to_user(buffer, link, len))
4704 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4705 * have ->get_link() not calling nd_jump_link(). Using (or not using) it
4706 * for any given inode is up to filesystem.
4708 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4710 DEFINE_DELAYED_CALL(done);
4711 struct inode *inode = d_inode(dentry);
4712 const char *link = inode->i_link;
4716 link = inode->i_op->get_link(dentry, inode, &done);
4718 return PTR_ERR(link);
4720 res = readlink_copy(buffer, buflen, link);
4721 do_delayed_call(&done);
4724 EXPORT_SYMBOL(generic_readlink);
4727 * vfs_get_link - get symlink body
4728 * @dentry: dentry on which to get symbolic link
4729 * @done: caller needs to free returned data with this
4731 * Calls security hook and i_op->get_link() on the supplied inode.
4733 * It does not touch atime. That's up to the caller if necessary.
4735 * Does not work on "special" symlinks like /proc/$$/fd/N
4737 const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4739 const char *res = ERR_PTR(-EINVAL);
4740 struct inode *inode = d_inode(dentry);
4742 if (d_is_symlink(dentry)) {
4743 res = ERR_PTR(security_inode_readlink(dentry));
4745 res = inode->i_op->get_link(dentry, inode, done);
4749 EXPORT_SYMBOL(vfs_get_link);
4751 /* get the link contents into pagecache */
4752 const char *page_get_link(struct dentry *dentry, struct inode *inode,
4753 struct delayed_call *callback)
4757 struct address_space *mapping = inode->i_mapping;
4760 page = find_get_page(mapping, 0);
4762 return ERR_PTR(-ECHILD);
4763 if (!PageUptodate(page)) {
4765 return ERR_PTR(-ECHILD);
4768 page = read_mapping_page(mapping, 0, NULL);
4772 set_delayed_call(callback, page_put_link, page);
4773 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4774 kaddr = page_address(page);
4775 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
4779 EXPORT_SYMBOL(page_get_link);
4781 void page_put_link(void *arg)
4785 EXPORT_SYMBOL(page_put_link);
4787 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4789 DEFINE_DELAYED_CALL(done);
4790 int res = readlink_copy(buffer, buflen,
4791 page_get_link(dentry, d_inode(dentry),
4793 do_delayed_call(&done);
4796 EXPORT_SYMBOL(page_readlink);
4799 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4801 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
4803 struct address_space *mapping = inode->i_mapping;
4807 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4809 flags |= AOP_FLAG_NOFS;
4812 err = pagecache_write_begin(NULL, mapping, 0, len-1,
4813 flags, &page, &fsdata);
4817 memcpy(page_address(page), symname, len-1);
4819 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4826 mark_inode_dirty(inode);
4831 EXPORT_SYMBOL(__page_symlink);
4833 int page_symlink(struct inode *inode, const char *symname, int len)
4835 return __page_symlink(inode, symname, len,
4836 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
4838 EXPORT_SYMBOL(page_symlink);
4840 const struct inode_operations page_symlink_inode_operations = {
4841 .readlink = generic_readlink,
4842 .get_link = page_get_link,
4844 EXPORT_SYMBOL(page_symlink_inode_operations);