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 <asm/uaccess.h>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/namei.h>
46 /* [Feb-1997 T. Schoebel-Theuer]
47 * Fundamental changes in the pathname lookup mechanisms (namei)
48 * were necessary because of omirr. The reason is that omirr needs
49 * to know the _real_ pathname, not the user-supplied one, in case
50 * of symlinks (and also when transname replacements occur).
52 * The new code replaces the old recursive symlink resolution with
53 * an iterative one (in case of non-nested symlink chains). It does
54 * this with calls to <fs>_follow_link().
55 * As a side effect, dir_namei(), _namei() and follow_link() are now
56 * replaced with a single function lookup_dentry() that can handle all
57 * the special cases of the former code.
59 * With the new dcache, the pathname is stored at each inode, at least as
60 * long as the refcount of the inode is positive. As a side effect, the
61 * size of the dcache depends on the inode cache and thus is dynamic.
63 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
64 * resolution to correspond with current state of the code.
66 * Note that the symlink resolution is not *completely* iterative.
67 * There is still a significant amount of tail- and mid- recursion in
68 * the algorithm. Also, note that <fs>_readlink() is not used in
69 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
70 * may return different results than <fs>_follow_link(). Many virtual
71 * filesystems (including /proc) exhibit this behavior.
74 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
75 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
76 * and the name already exists in form of a symlink, try to create the new
77 * name indicated by the symlink. The old code always complained that the
78 * name already exists, due to not following the symlink even if its target
79 * is nonexistent. The new semantics affects also mknod() and link() when
80 * the name is a symlink pointing to a non-existent name.
82 * I don't know which semantics is the right one, since I have no access
83 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
84 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
85 * "old" one. Personally, I think the new semantics is much more logical.
86 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
87 * file does succeed in both HP-UX and SunOs, but not in Solaris
88 * and in the old Linux semantics.
91 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
92 * semantics. See the comments in "open_namei" and "do_link" below.
94 * [10-Sep-98 Alan Modra] Another symlink change.
97 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
98 * inside the path - always follow.
99 * in the last component in creation/removal/renaming - never follow.
100 * if LOOKUP_FOLLOW passed - follow.
101 * if the pathname has trailing slashes - follow.
102 * otherwise - don't follow.
103 * (applied in that order).
105 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
106 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
107 * During the 2.4 we need to fix the userland stuff depending on it -
108 * hopefully we will be able to get rid of that wart in 2.5. So far only
109 * XEmacs seems to be relying on it...
112 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
113 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
114 * any extra contention...
117 /* In order to reduce some races, while at the same time doing additional
118 * checking and hopefully speeding things up, we copy filenames to the
119 * kernel data space before using them..
121 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
122 * PATH_MAX includes the nul terminator --RR.
125 #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
128 getname_flags(const char __user *filename, int flags, int *empty)
130 struct filename *result;
134 result = audit_reusename(filename);
138 result = __getname();
139 if (unlikely(!result))
140 return ERR_PTR(-ENOMEM);
143 * First, try to embed the struct filename inside the names_cache
146 kname = (char *)result->iname;
147 result->name = kname;
149 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
150 if (unlikely(len < 0)) {
156 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
157 * separate struct filename so we can dedicate the entire
158 * names_cache allocation for the pathname, and re-do the copy from
161 if (unlikely(len == EMBEDDED_NAME_MAX)) {
162 const size_t size = offsetof(struct filename, iname[1]);
163 kname = (char *)result;
166 * size is chosen that way we to guarantee that
167 * result->iname[0] is within the same object and that
168 * kname can't be equal to result->iname, no matter what.
170 result = kzalloc(size, GFP_KERNEL);
171 if (unlikely(!result)) {
173 return ERR_PTR(-ENOMEM);
175 result->name = kname;
176 len = strncpy_from_user(kname, filename, PATH_MAX);
177 if (unlikely(len < 0)) {
182 if (unlikely(len == PATH_MAX)) {
185 return ERR_PTR(-ENAMETOOLONG);
190 /* The empty path is special. */
191 if (unlikely(!len)) {
194 if (!(flags & LOOKUP_EMPTY)) {
196 return ERR_PTR(-ENOENT);
200 result->uptr = filename;
201 result->aname = NULL;
202 audit_getname(result);
207 getname(const char __user * filename)
209 return getname_flags(filename, 0, NULL);
213 getname_kernel(const char * filename)
215 struct filename *result;
216 int len = strlen(filename) + 1;
218 result = __getname();
219 if (unlikely(!result))
220 return ERR_PTR(-ENOMEM);
222 if (len <= EMBEDDED_NAME_MAX) {
223 result->name = (char *)result->iname;
224 } else if (len <= PATH_MAX) {
225 const size_t size = offsetof(struct filename, iname[1]);
226 struct filename *tmp;
228 tmp = kmalloc(size, GFP_KERNEL);
229 if (unlikely(!tmp)) {
231 return ERR_PTR(-ENOMEM);
233 tmp->name = (char *)result;
237 return ERR_PTR(-ENAMETOOLONG);
239 memcpy((char *)result->name, filename, len);
241 result->aname = NULL;
243 audit_getname(result);
248 void putname(struct filename *name)
250 BUG_ON(name->refcnt <= 0);
252 if (--name->refcnt > 0)
255 if (name->name != name->iname) {
256 __putname(name->name);
262 static int check_acl(struct inode *inode, int mask)
264 #ifdef CONFIG_FS_POSIX_ACL
265 struct posix_acl *acl;
267 if (mask & MAY_NOT_BLOCK) {
268 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
271 /* no ->get_acl() calls in RCU mode... */
272 if (acl == ACL_NOT_CACHED)
274 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
277 acl = get_acl(inode, ACL_TYPE_ACCESS);
281 int error = posix_acl_permission(inode, acl, mask);
282 posix_acl_release(acl);
291 * This does the basic permission checking
293 static int acl_permission_check(struct inode *inode, int mask)
295 unsigned int mode = inode->i_mode;
297 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
300 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
301 int error = check_acl(inode, mask);
302 if (error != -EAGAIN)
306 if (in_group_p(inode->i_gid))
311 * If the DACs are ok we don't need any capability check.
313 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
319 * generic_permission - check for access rights on a Posix-like filesystem
320 * @inode: inode to check access rights for
321 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
323 * Used to check for read/write/execute permissions on a file.
324 * We use "fsuid" for this, letting us set arbitrary permissions
325 * for filesystem access without changing the "normal" uids which
326 * are used for other things.
328 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
329 * request cannot be satisfied (eg. requires blocking or too much complexity).
330 * It would then be called again in ref-walk mode.
332 int generic_permission(struct inode *inode, int mask)
337 * Do the basic permission checks.
339 ret = acl_permission_check(inode, mask);
343 if (S_ISDIR(inode->i_mode)) {
344 /* DACs are overridable for directories */
345 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
347 if (!(mask & MAY_WRITE))
348 if (capable_wrt_inode_uidgid(inode,
349 CAP_DAC_READ_SEARCH))
354 * Read/write DACs are always overridable.
355 * Executable DACs are overridable when there is
356 * at least one exec bit set.
358 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
359 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
363 * Searching includes executable on directories, else just read.
365 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
366 if (mask == MAY_READ)
367 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
372 EXPORT_SYMBOL(generic_permission);
375 * We _really_ want to just do "generic_permission()" without
376 * even looking at the inode->i_op values. So we keep a cache
377 * flag in inode->i_opflags, that says "this has not special
378 * permission function, use the fast case".
380 static inline int do_inode_permission(struct vfsmount *mnt, struct inode *inode, int mask)
382 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
383 if (likely(mnt && inode->i_op->permission2))
384 return inode->i_op->permission2(mnt, inode, mask);
385 if (likely(inode->i_op->permission))
386 return inode->i_op->permission(inode, mask);
388 /* This gets set once for the inode lifetime */
389 spin_lock(&inode->i_lock);
390 inode->i_opflags |= IOP_FASTPERM;
391 spin_unlock(&inode->i_lock);
393 return generic_permission(inode, mask);
397 * __inode_permission - Check for access rights to a given inode
398 * @inode: Inode to check permission on
399 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
401 * Check for read/write/execute permissions on an inode.
403 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
405 * This does not check for a read-only file system. You probably want
406 * inode_permission().
408 int __inode_permission2(struct vfsmount *mnt, struct inode *inode, int mask)
412 if (unlikely(mask & MAY_WRITE)) {
414 * Nobody gets write access to an immutable file.
416 if (IS_IMMUTABLE(inode))
420 retval = do_inode_permission(mnt, inode, mask);
424 retval = devcgroup_inode_permission(inode, mask);
428 retval = security_inode_permission(inode, mask);
431 EXPORT_SYMBOL(__inode_permission2);
433 int __inode_permission(struct inode *inode, int mask)
435 return __inode_permission2(NULL, inode, mask);
437 EXPORT_SYMBOL(__inode_permission);
440 * sb_permission - Check superblock-level permissions
441 * @sb: Superblock of inode to check permission on
442 * @inode: Inode to check permission on
443 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
445 * Separate out file-system wide checks from inode-specific permission checks.
447 static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
449 if (unlikely(mask & MAY_WRITE)) {
450 umode_t mode = inode->i_mode;
452 /* Nobody gets write access to a read-only fs. */
453 if ((sb->s_flags & MS_RDONLY) &&
454 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
461 * inode_permission - Check for access rights to a given inode
462 * @inode: Inode to check permission on
463 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
465 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
466 * this, letting us set arbitrary permissions for filesystem access without
467 * changing the "normal" UIDs which are used for other things.
469 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
471 int inode_permission2(struct vfsmount *mnt, struct inode *inode, int mask)
475 retval = sb_permission(inode->i_sb, inode, mask);
478 return __inode_permission2(mnt, inode, mask);
480 EXPORT_SYMBOL(inode_permission2);
482 int inode_permission(struct inode *inode, int mask)
484 return inode_permission2(NULL, inode, mask);
486 EXPORT_SYMBOL(inode_permission);
489 * path_get - get a reference to a path
490 * @path: path to get the reference to
492 * Given a path increment the reference count to the dentry and the vfsmount.
494 void path_get(const struct path *path)
499 EXPORT_SYMBOL(path_get);
502 * path_put - put a reference to a path
503 * @path: path to put the reference to
505 * Given a path decrement the reference count to the dentry and the vfsmount.
507 void path_put(const struct path *path)
512 EXPORT_SYMBOL(path_put);
514 #define EMBEDDED_LEVELS 2
519 struct inode *inode; /* path.dentry.d_inode */
524 int total_link_count;
531 } *stack, internal[EMBEDDED_LEVELS];
532 struct filename *name;
533 struct nameidata *saved;
538 static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
540 struct nameidata *old = current->nameidata;
541 p->stack = p->internal;
544 p->total_link_count = old ? old->total_link_count : 0;
546 current->nameidata = p;
549 static void restore_nameidata(void)
551 struct nameidata *now = current->nameidata, *old = now->saved;
553 current->nameidata = old;
555 old->total_link_count = now->total_link_count;
556 if (now->stack != now->internal) {
558 now->stack = now->internal;
562 static int __nd_alloc_stack(struct nameidata *nd)
566 if (nd->flags & LOOKUP_RCU) {
567 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
572 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
577 memcpy(p, nd->internal, sizeof(nd->internal));
583 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
584 * @path: nameidate to verify
586 * Rename can sometimes move a file or directory outside of a bind
587 * mount, path_connected allows those cases to be detected.
589 static bool path_connected(const struct path *path)
591 struct vfsmount *mnt = path->mnt;
592 struct super_block *sb = mnt->mnt_sb;
594 /* Bind mounts and multi-root filesystems can have disconnected paths */
595 if (!(sb->s_iflags & SB_I_MULTIROOT) && (mnt->mnt_root == sb->s_root))
598 return is_subdir(path->dentry, mnt->mnt_root);
601 static inline int nd_alloc_stack(struct nameidata *nd)
603 if (likely(nd->depth != EMBEDDED_LEVELS))
605 if (likely(nd->stack != nd->internal))
607 return __nd_alloc_stack(nd);
610 static void drop_links(struct nameidata *nd)
614 struct saved *last = nd->stack + i;
615 struct inode *inode = last->inode;
616 if (last->cookie && inode->i_op->put_link) {
617 inode->i_op->put_link(inode, last->cookie);
623 static void terminate_walk(struct nameidata *nd)
626 if (!(nd->flags & LOOKUP_RCU)) {
629 for (i = 0; i < nd->depth; i++)
630 path_put(&nd->stack[i].link);
631 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
636 nd->flags &= ~LOOKUP_RCU;
637 if (!(nd->flags & LOOKUP_ROOT))
644 /* path_put is needed afterwards regardless of success or failure */
645 static bool legitimize_path(struct nameidata *nd,
646 struct path *path, unsigned seq)
648 int res = __legitimize_mnt(path->mnt, nd->m_seq);
655 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
659 return !read_seqcount_retry(&path->dentry->d_seq, seq);
662 static bool legitimize_links(struct nameidata *nd)
665 for (i = 0; i < nd->depth; i++) {
666 struct saved *last = nd->stack + i;
667 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
677 * Path walking has 2 modes, rcu-walk and ref-walk (see
678 * Documentation/filesystems/path-lookup.txt). In situations when we can't
679 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
680 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
681 * mode. Refcounts are grabbed at the last known good point before rcu-walk
682 * got stuck, so ref-walk may continue from there. If this is not successful
683 * (eg. a seqcount has changed), then failure is returned and it's up to caller
684 * to restart the path walk from the beginning in ref-walk mode.
688 * unlazy_walk - try to switch to ref-walk mode.
689 * @nd: nameidata pathwalk data
690 * @dentry: child of nd->path.dentry or NULL
691 * @seq: seq number to check dentry against
692 * Returns: 0 on success, -ECHILD on failure
694 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
695 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
696 * @nd or NULL. Must be called from rcu-walk context.
697 * Nothing should touch nameidata between unlazy_walk() failure and
700 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry, unsigned seq)
702 struct dentry *parent = nd->path.dentry;
704 BUG_ON(!(nd->flags & LOOKUP_RCU));
706 nd->flags &= ~LOOKUP_RCU;
707 if (unlikely(!legitimize_links(nd)))
709 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
711 if (unlikely(!lockref_get_not_dead(&parent->d_lockref)))
715 * For a negative lookup, the lookup sequence point is the parents
716 * sequence point, and it only needs to revalidate the parent dentry.
718 * For a positive lookup, we need to move both the parent and the
719 * dentry from the RCU domain to be properly refcounted. And the
720 * sequence number in the dentry validates *both* dentry counters,
721 * since we checked the sequence number of the parent after we got
722 * the child sequence number. So we know the parent must still
723 * be valid if the child sequence number is still valid.
726 if (read_seqcount_retry(&parent->d_seq, nd->seq))
728 BUG_ON(nd->inode != parent->d_inode);
730 if (!lockref_get_not_dead(&dentry->d_lockref))
732 if (read_seqcount_retry(&dentry->d_seq, seq))
737 * Sequence counts matched. Now make sure that the root is
738 * still valid and get it if required.
740 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
741 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
758 nd->path.dentry = NULL;
762 if (!(nd->flags & LOOKUP_ROOT))
767 static int unlazy_link(struct nameidata *nd, struct path *link, unsigned seq)
769 if (unlikely(!legitimize_path(nd, link, seq))) {
772 nd->flags &= ~LOOKUP_RCU;
774 nd->path.dentry = NULL;
775 if (!(nd->flags & LOOKUP_ROOT))
778 } else if (likely(unlazy_walk(nd, NULL, 0)) == 0) {
785 static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
787 return dentry->d_op->d_revalidate(dentry, flags);
790 #define INIT_PATH_SIZE 64
792 static void success_walk_trace(struct nameidata *nd)
794 struct path *pt = &nd->path;
795 struct inode *i = nd->inode;
796 char buf[INIT_PATH_SIZE], *try_buf;
800 /* When eBPF/ tracepoint is disabled, keep overhead low. */
801 if (!trace_inodepath_enabled())
804 /* First try stack allocated buffer. */
806 cur_path_size = INIT_PATH_SIZE;
808 while (cur_path_size <= PATH_MAX) {
809 /* Free previous heap allocation if we are now trying
810 * a second or later heap allocation.
815 /* All but the first alloc are on the heap. */
816 if (cur_path_size != INIT_PATH_SIZE) {
817 try_buf = kmalloc(cur_path_size, GFP_KERNEL);
820 sprintf(try_buf, "error:buf_alloc_failed");
825 p = d_path(pt, try_buf, cur_path_size);
828 char *end = mangle_path(try_buf, p, "\n");
831 try_buf[end - try_buf] = 0;
834 /* On mangle errors, double path size
837 cur_path_size = cur_path_size << 1;
842 if (PTR_ERR(p) == -ENAMETOOLONG) {
843 /* If d_path complains that name is too long,
844 * then double path size till PATH_MAX.
846 cur_path_size = cur_path_size << 1;
850 sprintf(try_buf, "error:d_path_failed_%lu",
855 if (cur_path_size > PATH_MAX)
856 sprintf(try_buf, "error:d_path_name_too_long");
858 trace_inodepath(i, try_buf);
866 * complete_walk - successful completion of path walk
867 * @nd: pointer nameidata
869 * If we had been in RCU mode, drop out of it and legitimize nd->path.
870 * Revalidate the final result, unless we'd already done that during
871 * the path walk or the filesystem doesn't ask for it. Return 0 on
872 * success, -error on failure. In case of failure caller does not
873 * need to drop nd->path.
875 static int complete_walk(struct nameidata *nd)
877 struct dentry *dentry = nd->path.dentry;
880 if (nd->flags & LOOKUP_RCU) {
881 if (!(nd->flags & LOOKUP_ROOT))
883 if (unlikely(unlazy_walk(nd, NULL, 0)))
887 if (likely(!(nd->flags & LOOKUP_JUMPED))) {
888 success_walk_trace(nd);
892 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) {
893 success_walk_trace(nd);
897 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
899 success_walk_trace(nd);
909 static void set_root(struct nameidata *nd)
911 get_fs_root(current->fs, &nd->root);
914 static void set_root_rcu(struct nameidata *nd)
916 struct fs_struct *fs = current->fs;
920 seq = read_seqcount_begin(&fs->seq);
922 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
923 } while (read_seqcount_retry(&fs->seq, seq));
926 static void path_put_conditional(struct path *path, struct nameidata *nd)
929 if (path->mnt != nd->path.mnt)
933 static inline void path_to_nameidata(const struct path *path,
934 struct nameidata *nd)
936 if (!(nd->flags & LOOKUP_RCU)) {
937 dput(nd->path.dentry);
938 if (nd->path.mnt != path->mnt)
939 mntput(nd->path.mnt);
941 nd->path.mnt = path->mnt;
942 nd->path.dentry = path->dentry;
946 * Helper to directly jump to a known parsed path from ->follow_link,
947 * caller must have taken a reference to path beforehand.
949 void nd_jump_link(struct path *path)
951 struct nameidata *nd = current->nameidata;
955 nd->inode = nd->path.dentry->d_inode;
956 nd->flags |= LOOKUP_JUMPED;
959 static inline void put_link(struct nameidata *nd)
961 struct saved *last = nd->stack + --nd->depth;
962 struct inode *inode = last->inode;
963 if (last->cookie && inode->i_op->put_link)
964 inode->i_op->put_link(inode, last->cookie);
965 if (!(nd->flags & LOOKUP_RCU))
966 path_put(&last->link);
969 int sysctl_protected_symlinks __read_mostly = 0;
970 int sysctl_protected_hardlinks __read_mostly = 0;
971 int sysctl_protected_fifos __read_mostly;
972 int sysctl_protected_regular __read_mostly;
975 * may_follow_link - Check symlink following for unsafe situations
976 * @nd: nameidata pathwalk data
978 * In the case of the sysctl_protected_symlinks sysctl being enabled,
979 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
980 * in a sticky world-writable directory. This is to protect privileged
981 * processes from failing races against path names that may change out
982 * from under them by way of other users creating malicious symlinks.
983 * It will permit symlinks to be followed only when outside a sticky
984 * world-writable directory, or when the uid of the symlink and follower
985 * match, or when the directory owner matches the symlink's owner.
987 * Returns 0 if following the symlink is allowed, -ve on error.
989 static inline int may_follow_link(struct nameidata *nd)
991 const struct inode *inode;
992 const struct inode *parent;
995 if (!sysctl_protected_symlinks)
998 /* Allowed if owner and follower match. */
999 inode = nd->stack[0].inode;
1000 if (uid_eq(current_cred()->fsuid, inode->i_uid))
1003 /* Allowed if parent directory not sticky and world-writable. */
1005 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
1008 /* Allowed if parent directory and link owner match. */
1009 puid = parent->i_uid;
1010 if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
1013 if (nd->flags & LOOKUP_RCU)
1016 audit_log_link_denied("follow_link", &nd->stack[0].link);
1021 * safe_hardlink_source - Check for safe hardlink conditions
1022 * @inode: the source inode to hardlink from
1024 * Return false if at least one of the following conditions:
1025 * - inode is not a regular file
1027 * - inode is setgid and group-exec
1028 * - access failure for read and write
1030 * Otherwise returns true.
1032 static bool safe_hardlink_source(struct inode *inode)
1034 umode_t mode = inode->i_mode;
1036 /* Special files should not get pinned to the filesystem. */
1040 /* Setuid files should not get pinned to the filesystem. */
1044 /* Executable setgid files should not get pinned to the filesystem. */
1045 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
1048 /* Hardlinking to unreadable or unwritable sources is dangerous. */
1049 if (inode_permission(inode, MAY_READ | MAY_WRITE))
1056 * may_linkat - Check permissions for creating a hardlink
1057 * @link: the source to hardlink from
1059 * Block hardlink when all of:
1060 * - sysctl_protected_hardlinks enabled
1061 * - fsuid does not match inode
1062 * - hardlink source is unsafe (see safe_hardlink_source() above)
1063 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
1065 * Returns 0 if successful, -ve on error.
1067 static int may_linkat(struct path *link)
1069 struct inode *inode;
1071 if (!sysctl_protected_hardlinks)
1074 inode = link->dentry->d_inode;
1076 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1077 * otherwise, it must be a safe source.
1079 if (inode_owner_or_capable(inode) || safe_hardlink_source(inode))
1082 audit_log_link_denied("linkat", link);
1087 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1088 * should be allowed, or not, on files that already
1090 * @dir: the sticky parent directory
1091 * @inode: the inode of the file to open
1093 * Block an O_CREAT open of a FIFO (or a regular file) when:
1094 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1095 * - the file already exists
1096 * - we are in a sticky directory
1097 * - we don't own the file
1098 * - the owner of the directory doesn't own the file
1099 * - the directory is world writable
1100 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1101 * the directory doesn't have to be world writable: being group writable will
1104 * Returns 0 if the open is allowed, -ve on error.
1106 static int may_create_in_sticky(struct dentry * const dir,
1107 struct inode * const inode)
1109 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
1110 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
1111 likely(!(dir->d_inode->i_mode & S_ISVTX)) ||
1112 uid_eq(inode->i_uid, dir->d_inode->i_uid) ||
1113 uid_eq(current_fsuid(), inode->i_uid))
1116 if (likely(dir->d_inode->i_mode & 0002) ||
1117 (dir->d_inode->i_mode & 0020 &&
1118 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
1119 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
1125 static __always_inline
1126 const char *get_link(struct nameidata *nd)
1128 struct saved *last = nd->stack + nd->depth - 1;
1129 struct dentry *dentry = last->link.dentry;
1130 struct inode *inode = last->inode;
1134 if (!(nd->flags & LOOKUP_RCU)) {
1135 touch_atime(&last->link);
1137 } else if (atime_needs_update(&last->link, inode)) {
1138 if (unlikely(unlazy_walk(nd, NULL, 0)))
1139 return ERR_PTR(-ECHILD);
1140 touch_atime(&last->link);
1143 error = security_inode_follow_link(dentry, inode,
1144 nd->flags & LOOKUP_RCU);
1145 if (unlikely(error))
1146 return ERR_PTR(error);
1148 nd->last_type = LAST_BIND;
1149 res = inode->i_link;
1151 if (nd->flags & LOOKUP_RCU) {
1152 if (unlikely(unlazy_walk(nd, NULL, 0)))
1153 return ERR_PTR(-ECHILD);
1155 res = inode->i_op->follow_link(dentry, &last->cookie);
1156 if (IS_ERR_OR_NULL(res)) {
1157 last->cookie = NULL;
1162 if (nd->flags & LOOKUP_RCU) {
1166 nd->path = nd->root;
1167 d = nd->path.dentry;
1168 nd->inode = d->d_inode;
1169 nd->seq = nd->root_seq;
1170 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
1171 return ERR_PTR(-ECHILD);
1175 path_put(&nd->path);
1176 nd->path = nd->root;
1177 path_get(&nd->root);
1178 nd->inode = nd->path.dentry->d_inode;
1180 nd->flags |= LOOKUP_JUMPED;
1181 while (unlikely(*++res == '/'))
1190 * follow_up - Find the mountpoint of path's vfsmount
1192 * Given a path, find the mountpoint of its source file system.
1193 * Replace @path with the path of the mountpoint in the parent mount.
1196 * Return 1 if we went up a level and 0 if we were already at the
1199 int follow_up(struct path *path)
1201 struct mount *mnt = real_mount(path->mnt);
1202 struct mount *parent;
1203 struct dentry *mountpoint;
1205 read_seqlock_excl(&mount_lock);
1206 parent = mnt->mnt_parent;
1207 if (parent == mnt) {
1208 read_sequnlock_excl(&mount_lock);
1211 mntget(&parent->mnt);
1212 mountpoint = dget(mnt->mnt_mountpoint);
1213 read_sequnlock_excl(&mount_lock);
1215 path->dentry = mountpoint;
1217 path->mnt = &parent->mnt;
1220 EXPORT_SYMBOL(follow_up);
1223 * Perform an automount
1224 * - return -EISDIR to tell follow_managed() to stop and return the path we
1227 static int follow_automount(struct path *path, struct nameidata *nd,
1230 struct vfsmount *mnt;
1233 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1236 /* We don't want to mount if someone's just doing a stat -
1237 * unless they're stat'ing a directory and appended a '/' to
1240 * We do, however, want to mount if someone wants to open or
1241 * create a file of any type under the mountpoint, wants to
1242 * traverse through the mountpoint or wants to open the
1243 * mounted directory. Also, autofs may mark negative dentries
1244 * as being automount points. These will need the attentions
1245 * of the daemon to instantiate them before they can be used.
1247 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
1248 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
1249 path->dentry->d_inode)
1252 nd->total_link_count++;
1253 if (nd->total_link_count >= 40)
1256 mnt = path->dentry->d_op->d_automount(path);
1259 * The filesystem is allowed to return -EISDIR here to indicate
1260 * it doesn't want to automount. For instance, autofs would do
1261 * this so that its userspace daemon can mount on this dentry.
1263 * However, we can only permit this if it's a terminal point in
1264 * the path being looked up; if it wasn't then the remainder of
1265 * the path is inaccessible and we should say so.
1267 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
1269 return PTR_ERR(mnt);
1272 if (!mnt) /* mount collision */
1275 if (!*need_mntput) {
1276 /* lock_mount() may release path->mnt on error */
1278 *need_mntput = true;
1280 err = finish_automount(mnt, path);
1284 /* Someone else made a mount here whilst we were busy */
1289 path->dentry = dget(mnt->mnt_root);
1298 * Handle a dentry that is managed in some way.
1299 * - Flagged for transit management (autofs)
1300 * - Flagged as mountpoint
1301 * - Flagged as automount point
1303 * This may only be called in refwalk mode.
1305 * Serialization is taken care of in namespace.c
1307 static int follow_managed(struct path *path, struct nameidata *nd)
1309 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
1311 bool need_mntput = false;
1314 /* Given that we're not holding a lock here, we retain the value in a
1315 * local variable for each dentry as we look at it so that we don't see
1316 * the components of that value change under us */
1317 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1318 managed &= DCACHE_MANAGED_DENTRY,
1319 unlikely(managed != 0)) {
1320 /* Allow the filesystem to manage the transit without i_mutex
1322 if (managed & DCACHE_MANAGE_TRANSIT) {
1323 BUG_ON(!path->dentry->d_op);
1324 BUG_ON(!path->dentry->d_op->d_manage);
1325 ret = path->dentry->d_op->d_manage(path->dentry, false);
1330 /* Transit to a mounted filesystem. */
1331 if (managed & DCACHE_MOUNTED) {
1332 struct vfsmount *mounted = lookup_mnt(path);
1337 path->mnt = mounted;
1338 path->dentry = dget(mounted->mnt_root);
1343 /* Something is mounted on this dentry in another
1344 * namespace and/or whatever was mounted there in this
1345 * namespace got unmounted before lookup_mnt() could
1349 /* Handle an automount point */
1350 if (managed & DCACHE_NEED_AUTOMOUNT) {
1351 ret = follow_automount(path, nd, &need_mntput);
1357 /* We didn't change the current path point */
1361 if (need_mntput && path->mnt == mnt)
1366 nd->flags |= LOOKUP_JUMPED;
1367 if (unlikely(ret < 0))
1368 path_put_conditional(path, nd);
1372 int follow_down_one(struct path *path)
1374 struct vfsmount *mounted;
1376 mounted = lookup_mnt(path);
1380 path->mnt = mounted;
1381 path->dentry = dget(mounted->mnt_root);
1386 EXPORT_SYMBOL(follow_down_one);
1388 static inline int managed_dentry_rcu(struct dentry *dentry)
1390 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1391 dentry->d_op->d_manage(dentry, true) : 0;
1395 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1396 * we meet a managed dentry that would need blocking.
1398 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1399 struct inode **inode, unsigned *seqp)
1402 struct mount *mounted;
1404 * Don't forget we might have a non-mountpoint managed dentry
1405 * that wants to block transit.
1407 switch (managed_dentry_rcu(path->dentry)) {
1417 if (!d_mountpoint(path->dentry))
1418 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1420 mounted = __lookup_mnt(path->mnt, path->dentry);
1423 path->mnt = &mounted->mnt;
1424 path->dentry = mounted->mnt.mnt_root;
1425 nd->flags |= LOOKUP_JUMPED;
1426 *seqp = read_seqcount_begin(&path->dentry->d_seq);
1428 * Update the inode too. We don't need to re-check the
1429 * dentry sequence number here after this d_inode read,
1430 * because a mount-point is always pinned.
1432 *inode = path->dentry->d_inode;
1434 return !read_seqretry(&mount_lock, nd->m_seq) &&
1435 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1438 static int follow_dotdot_rcu(struct nameidata *nd)
1440 struct inode *inode = nd->inode;
1445 if (path_equal(&nd->path, &nd->root))
1447 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1448 struct dentry *old = nd->path.dentry;
1449 struct dentry *parent = old->d_parent;
1452 inode = parent->d_inode;
1453 seq = read_seqcount_begin(&parent->d_seq);
1454 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1456 nd->path.dentry = parent;
1458 if (unlikely(!path_connected(&nd->path)))
1462 struct mount *mnt = real_mount(nd->path.mnt);
1463 struct mount *mparent = mnt->mnt_parent;
1464 struct dentry *mountpoint = mnt->mnt_mountpoint;
1465 struct inode *inode2 = mountpoint->d_inode;
1466 unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1467 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1469 if (&mparent->mnt == nd->path.mnt)
1471 /* we know that mountpoint was pinned */
1472 nd->path.dentry = mountpoint;
1473 nd->path.mnt = &mparent->mnt;
1478 while (unlikely(d_mountpoint(nd->path.dentry))) {
1479 struct mount *mounted;
1480 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1481 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1485 nd->path.mnt = &mounted->mnt;
1486 nd->path.dentry = mounted->mnt.mnt_root;
1487 inode = nd->path.dentry->d_inode;
1488 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1495 * Follow down to the covering mount currently visible to userspace. At each
1496 * point, the filesystem owning that dentry may be queried as to whether the
1497 * caller is permitted to proceed or not.
1499 int follow_down(struct path *path)
1504 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1505 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1506 /* Allow the filesystem to manage the transit without i_mutex
1509 * We indicate to the filesystem if someone is trying to mount
1510 * something here. This gives autofs the chance to deny anyone
1511 * other than its daemon the right to mount on its
1514 * The filesystem may sleep at this point.
1516 if (managed & DCACHE_MANAGE_TRANSIT) {
1517 BUG_ON(!path->dentry->d_op);
1518 BUG_ON(!path->dentry->d_op->d_manage);
1519 ret = path->dentry->d_op->d_manage(
1520 path->dentry, false);
1522 return ret == -EISDIR ? 0 : ret;
1525 /* Transit to a mounted filesystem. */
1526 if (managed & DCACHE_MOUNTED) {
1527 struct vfsmount *mounted = lookup_mnt(path);
1532 path->mnt = mounted;
1533 path->dentry = dget(mounted->mnt_root);
1537 /* Don't handle automount points here */
1542 EXPORT_SYMBOL(follow_down);
1545 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1547 static void follow_mount(struct path *path)
1549 while (d_mountpoint(path->dentry)) {
1550 struct vfsmount *mounted = lookup_mnt(path);
1555 path->mnt = mounted;
1556 path->dentry = dget(mounted->mnt_root);
1560 static int follow_dotdot(struct nameidata *nd)
1566 struct dentry *old = nd->path.dentry;
1568 if (nd->path.dentry == nd->root.dentry &&
1569 nd->path.mnt == nd->root.mnt) {
1572 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1573 /* rare case of legitimate dget_parent()... */
1574 nd->path.dentry = dget_parent(nd->path.dentry);
1576 if (unlikely(!path_connected(&nd->path)))
1580 if (!follow_up(&nd->path))
1583 follow_mount(&nd->path);
1584 nd->inode = nd->path.dentry->d_inode;
1589 * This looks up the name in dcache, possibly revalidates the old dentry and
1590 * allocates a new one if not found or not valid. In the need_lookup argument
1591 * returns whether i_op->lookup is necessary.
1593 * dir->d_inode->i_mutex must be held
1595 static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1596 unsigned int flags, bool *need_lookup)
1598 struct dentry *dentry;
1601 *need_lookup = false;
1602 dentry = d_lookup(dir, name);
1604 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1605 error = d_revalidate(dentry, flags);
1606 if (unlikely(error <= 0)) {
1609 return ERR_PTR(error);
1611 d_invalidate(dentry);
1620 dentry = d_alloc(dir, name);
1621 if (unlikely(!dentry))
1622 return ERR_PTR(-ENOMEM);
1624 *need_lookup = true;
1630 * Call i_op->lookup on the dentry. The dentry must be negative and
1633 * dir->d_inode->i_mutex must be held
1635 static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1640 /* Don't create child dentry for a dead directory. */
1641 if (unlikely(IS_DEADDIR(dir))) {
1643 return ERR_PTR(-ENOENT);
1646 old = dir->i_op->lookup(dir, dentry, flags);
1647 if (unlikely(old)) {
1654 static struct dentry *__lookup_hash(struct qstr *name,
1655 struct dentry *base, unsigned int flags)
1658 struct dentry *dentry;
1660 dentry = lookup_dcache(name, base, flags, &need_lookup);
1664 return lookup_real(base->d_inode, dentry, flags);
1668 * It's more convoluted than I'd like it to be, but... it's still fairly
1669 * small and for now I'd prefer to have fast path as straight as possible.
1670 * It _is_ time-critical.
1672 static int lookup_fast(struct nameidata *nd,
1673 struct path *path, struct inode **inode,
1676 struct vfsmount *mnt = nd->path.mnt;
1677 struct dentry *dentry, *parent = nd->path.dentry;
1683 * Rename seqlock is not required here because in the off chance
1684 * of a false negative due to a concurrent rename, we're going to
1685 * do the non-racy lookup, below.
1687 if (nd->flags & LOOKUP_RCU) {
1690 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
1695 * This sequence count validates that the inode matches
1696 * the dentry name information from lookup.
1698 *inode = d_backing_inode(dentry);
1699 negative = d_is_negative(dentry);
1700 if (read_seqcount_retry(&dentry->d_seq, seq))
1704 * This sequence count validates that the parent had no
1705 * changes while we did the lookup of the dentry above.
1707 * The memory barrier in read_seqcount_begin of child is
1708 * enough, we can use __read_seqcount_retry here.
1710 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1714 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1715 status = d_revalidate(dentry, nd->flags);
1716 if (unlikely(status <= 0)) {
1717 if (status != -ECHILD)
1723 * Note: do negative dentry check after revalidation in
1724 * case that drops it.
1729 path->dentry = dentry;
1730 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
1733 if (unlazy_walk(nd, dentry, seq))
1736 dentry = __d_lookup(parent, &nd->last);
1739 if (unlikely(!dentry))
1742 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1743 status = d_revalidate(dentry, nd->flags);
1744 if (unlikely(status <= 0)) {
1749 d_invalidate(dentry);
1754 if (unlikely(d_is_negative(dentry))) {
1759 path->dentry = dentry;
1760 err = follow_managed(path, nd);
1762 *inode = d_backing_inode(path->dentry);
1769 /* Fast lookup failed, do it the slow way */
1770 static int lookup_slow(struct nameidata *nd, struct path *path)
1772 struct dentry *dentry, *parent;
1774 parent = nd->path.dentry;
1775 BUG_ON(nd->inode != parent->d_inode);
1777 mutex_lock(&parent->d_inode->i_mutex);
1778 dentry = __lookup_hash(&nd->last, parent, nd->flags);
1779 mutex_unlock(&parent->d_inode->i_mutex);
1781 return PTR_ERR(dentry);
1782 path->mnt = nd->path.mnt;
1783 path->dentry = dentry;
1784 return follow_managed(path, nd);
1787 static inline int may_lookup(struct nameidata *nd)
1789 if (nd->flags & LOOKUP_RCU) {
1790 int err = inode_permission2(nd->path.mnt, nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1793 if (unlazy_walk(nd, NULL, 0))
1796 return inode_permission2(nd->path.mnt, nd->inode, MAY_EXEC);
1799 static inline int handle_dots(struct nameidata *nd, int type)
1801 if (type == LAST_DOTDOT) {
1802 if (nd->flags & LOOKUP_RCU) {
1803 return follow_dotdot_rcu(nd);
1805 return follow_dotdot(nd);
1810 static int pick_link(struct nameidata *nd, struct path *link,
1811 struct inode *inode, unsigned seq)
1815 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
1816 path_to_nameidata(link, nd);
1819 if (!(nd->flags & LOOKUP_RCU)) {
1820 if (link->mnt == nd->path.mnt)
1823 error = nd_alloc_stack(nd);
1824 if (unlikely(error)) {
1825 if (error == -ECHILD) {
1826 if (unlikely(unlazy_link(nd, link, seq)))
1828 error = nd_alloc_stack(nd);
1836 last = nd->stack + nd->depth++;
1838 last->cookie = NULL;
1839 last->inode = inode;
1845 * Do we need to follow links? We _really_ want to be able
1846 * to do this check without having to look at inode->i_op,
1847 * so we keep a cache of "no, this doesn't need follow_link"
1848 * for the common case.
1850 static inline int should_follow_link(struct nameidata *nd, struct path *link,
1852 struct inode *inode, unsigned seq)
1854 if (likely(!d_is_symlink(link->dentry)))
1858 /* make sure that d_is_symlink above matches inode */
1859 if (nd->flags & LOOKUP_RCU) {
1860 if (read_seqcount_retry(&link->dentry->d_seq, seq))
1863 return pick_link(nd, link, inode, seq);
1866 enum {WALK_GET = 1, WALK_PUT = 2};
1868 static int walk_component(struct nameidata *nd, int flags)
1871 struct inode *inode;
1875 * "." and ".." are special - ".." especially so because it has
1876 * to be able to know about the current root directory and
1877 * parent relationships.
1879 if (unlikely(nd->last_type != LAST_NORM)) {
1880 err = handle_dots(nd, nd->last_type);
1881 if (flags & WALK_PUT)
1885 err = lookup_fast(nd, &path, &inode, &seq);
1886 if (unlikely(err)) {
1890 err = lookup_slow(nd, &path);
1894 seq = 0; /* we are already out of RCU mode */
1896 if (d_is_negative(path.dentry))
1898 inode = d_backing_inode(path.dentry);
1901 if (flags & WALK_PUT)
1903 err = should_follow_link(nd, &path, flags & WALK_GET, inode, seq);
1906 path_to_nameidata(&path, nd);
1912 path_to_nameidata(&path, nd);
1917 * We can do the critical dentry name comparison and hashing
1918 * operations one word at a time, but we are limited to:
1920 * - Architectures with fast unaligned word accesses. We could
1921 * do a "get_unaligned()" if this helps and is sufficiently
1924 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1925 * do not trap on the (extremely unlikely) case of a page
1926 * crossing operation.
1928 * - Furthermore, we need an efficient 64-bit compile for the
1929 * 64-bit case in order to generate the "number of bytes in
1930 * the final mask". Again, that could be replaced with a
1931 * efficient population count instruction or similar.
1933 #ifdef CONFIG_DCACHE_WORD_ACCESS
1935 #include <asm/word-at-a-time.h>
1939 static inline unsigned int fold_hash(unsigned long hash)
1941 return hash_64(hash, 32);
1944 #else /* 32-bit case */
1946 #define fold_hash(x) (x)
1950 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1952 unsigned long a, mask;
1953 unsigned long hash = 0;
1956 a = load_unaligned_zeropad(name);
1957 if (len < sizeof(unsigned long))
1961 name += sizeof(unsigned long);
1962 len -= sizeof(unsigned long);
1966 mask = bytemask_from_count(len);
1969 return fold_hash(hash);
1971 EXPORT_SYMBOL(full_name_hash);
1974 * Calculate the length and hash of the path component, and
1975 * return the "hash_len" as the result.
1977 static inline u64 hash_name(const char *name)
1979 unsigned long a, b, adata, bdata, mask, hash, len;
1980 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1983 len = -sizeof(unsigned long);
1985 hash = (hash + a) * 9;
1986 len += sizeof(unsigned long);
1987 a = load_unaligned_zeropad(name+len);
1988 b = a ^ REPEAT_BYTE('/');
1989 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1991 adata = prep_zero_mask(a, adata, &constants);
1992 bdata = prep_zero_mask(b, bdata, &constants);
1994 mask = create_zero_mask(adata | bdata);
1996 hash += a & zero_bytemask(mask);
1997 len += find_zero(mask);
1998 return hashlen_create(fold_hash(hash), len);
2003 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
2005 unsigned long hash = init_name_hash();
2007 hash = partial_name_hash(*name++, hash);
2008 return end_name_hash(hash);
2010 EXPORT_SYMBOL(full_name_hash);
2013 * We know there's a real path component here of at least
2016 static inline u64 hash_name(const char *name)
2018 unsigned long hash = init_name_hash();
2019 unsigned long len = 0, c;
2021 c = (unsigned char)*name;
2024 hash = partial_name_hash(c, hash);
2025 c = (unsigned char)name[len];
2026 } while (c && c != '/');
2027 return hashlen_create(end_name_hash(hash), len);
2034 * This is the basic name resolution function, turning a pathname into
2035 * the final dentry. We expect 'base' to be positive and a directory.
2037 * Returns 0 and nd will have valid dentry and mnt on success.
2038 * Returns error and drops reference to input namei data on failure.
2040 static int link_path_walk(const char *name, struct nameidata *nd)
2049 /* At this point we know we have a real path component. */
2054 err = may_lookup(nd);
2058 hash_len = hash_name(name);
2061 if (name[0] == '.') switch (hashlen_len(hash_len)) {
2063 if (name[1] == '.') {
2065 nd->flags |= LOOKUP_JUMPED;
2071 if (likely(type == LAST_NORM)) {
2072 struct dentry *parent = nd->path.dentry;
2073 nd->flags &= ~LOOKUP_JUMPED;
2074 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
2075 struct qstr this = { { .hash_len = hash_len }, .name = name };
2076 err = parent->d_op->d_hash(parent, &this);
2079 hash_len = this.hash_len;
2084 nd->last.hash_len = hash_len;
2085 nd->last.name = name;
2086 nd->last_type = type;
2088 name += hashlen_len(hash_len);
2092 * If it wasn't NUL, we know it was '/'. Skip that
2093 * slash, and continue until no more slashes.
2097 } while (unlikely(*name == '/'));
2098 if (unlikely(!*name)) {
2100 /* pathname body, done */
2103 name = nd->stack[nd->depth - 1].name;
2104 /* trailing symlink, done */
2107 /* last component of nested symlink */
2108 err = walk_component(nd, WALK_GET | WALK_PUT);
2110 err = walk_component(nd, WALK_GET);
2116 const char *s = get_link(nd);
2125 nd->stack[nd->depth - 1].name = name;
2130 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2131 if (nd->flags & LOOKUP_RCU) {
2132 if (unlazy_walk(nd, NULL, 0))
2140 static const char *path_init(struct nameidata *nd, unsigned flags)
2143 const char *s = nd->name->name;
2146 flags &= ~LOOKUP_RCU;
2148 nd->last_type = LAST_ROOT; /* if there are only slashes... */
2149 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
2151 if (flags & LOOKUP_ROOT) {
2152 struct dentry *root = nd->root.dentry;
2153 struct vfsmount *mnt = nd->root.mnt;
2154 struct inode *inode = root->d_inode;
2156 if (!d_can_lookup(root))
2157 return ERR_PTR(-ENOTDIR);
2158 retval = inode_permission2(mnt, inode, MAY_EXEC);
2160 return ERR_PTR(retval);
2162 nd->path = nd->root;
2164 if (flags & LOOKUP_RCU) {
2166 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2167 nd->root_seq = nd->seq;
2168 nd->m_seq = read_seqbegin(&mount_lock);
2170 path_get(&nd->path);
2175 nd->root.mnt = NULL;
2177 nd->m_seq = read_seqbegin(&mount_lock);
2179 if (flags & LOOKUP_RCU) {
2182 nd->seq = nd->root_seq;
2185 path_get(&nd->root);
2187 nd->path = nd->root;
2188 } else if (nd->dfd == AT_FDCWD) {
2189 if (flags & LOOKUP_RCU) {
2190 struct fs_struct *fs = current->fs;
2196 seq = read_seqcount_begin(&fs->seq);
2198 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2199 } while (read_seqcount_retry(&fs->seq, seq));
2201 get_fs_pwd(current->fs, &nd->path);
2204 /* Caller must check execute permissions on the starting path component */
2205 struct fd f = fdget_raw(nd->dfd);
2206 struct dentry *dentry;
2209 return ERR_PTR(-EBADF);
2211 dentry = f.file->f_path.dentry;
2214 if (!d_can_lookup(dentry)) {
2216 return ERR_PTR(-ENOTDIR);
2220 nd->path = f.file->f_path;
2221 if (flags & LOOKUP_RCU) {
2223 nd->inode = nd->path.dentry->d_inode;
2224 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
2226 path_get(&nd->path);
2227 nd->inode = nd->path.dentry->d_inode;
2233 nd->inode = nd->path.dentry->d_inode;
2234 if (!(flags & LOOKUP_RCU))
2236 if (likely(!read_seqcount_retry(&nd->path.dentry->d_seq, nd->seq)))
2238 if (!(nd->flags & LOOKUP_ROOT))
2239 nd->root.mnt = NULL;
2241 return ERR_PTR(-ECHILD);
2244 static const char *trailing_symlink(struct nameidata *nd)
2247 int error = may_follow_link(nd);
2248 if (unlikely(error))
2249 return ERR_PTR(error);
2250 nd->flags |= LOOKUP_PARENT;
2251 nd->stack[0].name = NULL;
2256 static inline int lookup_last(struct nameidata *nd)
2258 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2259 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2261 nd->flags &= ~LOOKUP_PARENT;
2262 return walk_component(nd,
2263 nd->flags & LOOKUP_FOLLOW
2265 ? WALK_PUT | WALK_GET
2270 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2271 static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
2273 const char *s = path_init(nd, flags);
2278 while (!(err = link_path_walk(s, nd))
2279 && ((err = lookup_last(nd)) > 0)) {
2280 s = trailing_symlink(nd);
2287 err = complete_walk(nd);
2289 if (!err && nd->flags & LOOKUP_DIRECTORY)
2290 if (!d_can_lookup(nd->path.dentry))
2294 nd->path.mnt = NULL;
2295 nd->path.dentry = NULL;
2301 static int filename_lookup(int dfd, struct filename *name, unsigned flags,
2302 struct path *path, struct path *root)
2305 struct nameidata nd;
2307 return PTR_ERR(name);
2308 if (unlikely(root)) {
2310 flags |= LOOKUP_ROOT;
2312 set_nameidata(&nd, dfd, name);
2313 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
2314 if (unlikely(retval == -ECHILD))
2315 retval = path_lookupat(&nd, flags, path);
2316 if (unlikely(retval == -ESTALE))
2317 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
2319 if (likely(!retval))
2320 audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
2321 restore_nameidata();
2326 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2327 static int path_parentat(struct nameidata *nd, unsigned flags,
2328 struct path *parent)
2330 const char *s = path_init(nd, flags);
2334 err = link_path_walk(s, nd);
2336 err = complete_walk(nd);
2339 nd->path.mnt = NULL;
2340 nd->path.dentry = NULL;
2346 static struct filename *filename_parentat(int dfd, struct filename *name,
2347 unsigned int flags, struct path *parent,
2348 struct qstr *last, int *type)
2351 struct nameidata nd;
2355 set_nameidata(&nd, dfd, name);
2356 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
2357 if (unlikely(retval == -ECHILD))
2358 retval = path_parentat(&nd, flags, parent);
2359 if (unlikely(retval == -ESTALE))
2360 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
2361 if (likely(!retval)) {
2363 *type = nd.last_type;
2364 audit_inode(name, parent->dentry, LOOKUP_PARENT);
2367 name = ERR_PTR(retval);
2369 restore_nameidata();
2373 /* does lookup, returns the object with parent locked */
2374 struct dentry *kern_path_locked(const char *name, struct path *path)
2376 struct filename *filename;
2381 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2383 if (IS_ERR(filename))
2384 return ERR_CAST(filename);
2385 if (unlikely(type != LAST_NORM)) {
2388 return ERR_PTR(-EINVAL);
2390 mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2391 d = __lookup_hash(&last, path->dentry, 0);
2393 mutex_unlock(&path->dentry->d_inode->i_mutex);
2400 int kern_path(const char *name, unsigned int flags, struct path *path)
2402 return filename_lookup(AT_FDCWD, getname_kernel(name),
2405 EXPORT_SYMBOL(kern_path);
2408 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2409 * @dentry: pointer to dentry of the base directory
2410 * @mnt: pointer to vfs mount of the base directory
2411 * @name: pointer to file name
2412 * @flags: lookup flags
2413 * @path: pointer to struct path to fill
2415 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2416 const char *name, unsigned int flags,
2419 struct path root = {.mnt = mnt, .dentry = dentry};
2420 /* the first argument of filename_lookup() is ignored with root */
2421 return filename_lookup(AT_FDCWD, getname_kernel(name),
2422 flags , path, &root);
2424 EXPORT_SYMBOL(vfs_path_lookup);
2427 * lookup_one_len - filesystem helper to lookup single pathname component
2428 * @name: pathname component to lookup
2429 * @mnt: mount we are looking up on
2430 * @base: base directory to lookup from
2431 * @len: maximum length @len should be interpreted to
2433 * Note that this routine is purely a helper for filesystem usage and should
2434 * not be called by generic code.
2436 struct dentry *lookup_one_len2(const char *name, struct vfsmount *mnt, struct dentry *base, int len)
2442 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2446 this.hash = full_name_hash(name, len);
2448 return ERR_PTR(-EACCES);
2450 if (unlikely(name[0] == '.')) {
2451 if (len < 2 || (len == 2 && name[1] == '.'))
2452 return ERR_PTR(-EACCES);
2456 c = *(const unsigned char *)name++;
2457 if (c == '/' || c == '\0')
2458 return ERR_PTR(-EACCES);
2461 * See if the low-level filesystem might want
2462 * to use its own hash..
2464 if (base->d_flags & DCACHE_OP_HASH) {
2465 int err = base->d_op->d_hash(base, &this);
2467 return ERR_PTR(err);
2470 err = inode_permission2(mnt, base->d_inode, MAY_EXEC);
2472 return ERR_PTR(err);
2474 return __lookup_hash(&this, base, 0);
2476 EXPORT_SYMBOL(lookup_one_len2);
2478 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2480 return lookup_one_len2(name, NULL, base, len);
2482 EXPORT_SYMBOL(lookup_one_len);
2484 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2485 struct path *path, int *empty)
2487 return filename_lookup(dfd, getname_flags(name, flags, empty),
2490 EXPORT_SYMBOL(user_path_at_empty);
2493 * NB: most callers don't do anything directly with the reference to the
2494 * to struct filename, but the nd->last pointer points into the name string
2495 * allocated by getname. So we must hold the reference to it until all
2496 * path-walking is complete.
2498 static inline struct filename *
2499 user_path_parent(int dfd, const char __user *path,
2500 struct path *parent,
2505 /* only LOOKUP_REVAL is allowed in extra flags */
2506 return filename_parentat(dfd, getname(path), flags & LOOKUP_REVAL,
2507 parent, last, type);
2511 * mountpoint_last - look up last component for umount
2512 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2513 * @path: pointer to container for result
2515 * This is a special lookup_last function just for umount. In this case, we
2516 * need to resolve the path without doing any revalidation.
2518 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2519 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2520 * in almost all cases, this lookup will be served out of the dcache. The only
2521 * cases where it won't are if nd->last refers to a symlink or the path is
2522 * bogus and it doesn't exist.
2525 * -error: if there was an error during lookup. This includes -ENOENT if the
2526 * lookup found a negative dentry. The nd->path reference will also be
2529 * 0: if we successfully resolved nd->path and found it to not to be a
2530 * symlink that needs to be followed. "path" will also be populated.
2531 * The nd->path reference will also be put.
2533 * 1: if we successfully resolved nd->last and found it to be a symlink
2534 * that needs to be followed. "path" will be populated with the path
2535 * to the link, and nd->path will *not* be put.
2538 mountpoint_last(struct nameidata *nd, struct path *path)
2541 struct dentry *dentry;
2542 struct dentry *dir = nd->path.dentry;
2544 /* If we're in rcuwalk, drop out of it to handle last component */
2545 if (nd->flags & LOOKUP_RCU) {
2546 if (unlazy_walk(nd, NULL, 0))
2550 nd->flags &= ~LOOKUP_PARENT;
2552 if (unlikely(nd->last_type != LAST_NORM)) {
2553 error = handle_dots(nd, nd->last_type);
2556 dentry = dget(nd->path.dentry);
2560 mutex_lock(&dir->d_inode->i_mutex);
2561 dentry = d_lookup(dir, &nd->last);
2564 * No cached dentry. Mounted dentries are pinned in the cache,
2565 * so that means that this dentry is probably a symlink or the
2566 * path doesn't actually point to a mounted dentry.
2568 dentry = d_alloc(dir, &nd->last);
2570 mutex_unlock(&dir->d_inode->i_mutex);
2573 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
2574 if (IS_ERR(dentry)) {
2575 mutex_unlock(&dir->d_inode->i_mutex);
2576 return PTR_ERR(dentry);
2579 mutex_unlock(&dir->d_inode->i_mutex);
2582 if (d_is_negative(dentry)) {
2588 path->dentry = dentry;
2589 path->mnt = nd->path.mnt;
2590 error = should_follow_link(nd, path, nd->flags & LOOKUP_FOLLOW,
2591 d_backing_inode(dentry), 0);
2592 if (unlikely(error))
2600 * path_mountpoint - look up a path to be umounted
2601 * @nd: lookup context
2602 * @flags: lookup flags
2603 * @path: pointer to container for result
2605 * Look up the given name, but don't attempt to revalidate the last component.
2606 * Returns 0 and "path" will be valid on success; Returns error otherwise.
2609 path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
2611 const char *s = path_init(nd, flags);
2615 while (!(err = link_path_walk(s, nd)) &&
2616 (err = mountpoint_last(nd, path)) > 0) {
2617 s = trailing_symlink(nd);
2628 filename_mountpoint(int dfd, struct filename *name, struct path *path,
2631 struct nameidata nd;
2634 return PTR_ERR(name);
2635 set_nameidata(&nd, dfd, name);
2636 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
2637 if (unlikely(error == -ECHILD))
2638 error = path_mountpoint(&nd, flags, path);
2639 if (unlikely(error == -ESTALE))
2640 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
2642 audit_inode(name, path->dentry, 0);
2643 restore_nameidata();
2649 * user_path_mountpoint_at - lookup a path from userland in order to umount it
2650 * @dfd: directory file descriptor
2651 * @name: pathname from userland
2652 * @flags: lookup flags
2653 * @path: pointer to container to hold result
2655 * A umount is a special case for path walking. We're not actually interested
2656 * in the inode in this situation, and ESTALE errors can be a problem. We
2657 * simply want track down the dentry and vfsmount attached at the mountpoint
2658 * and avoid revalidating the last component.
2660 * Returns 0 and populates "path" on success.
2663 user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
2666 return filename_mountpoint(dfd, getname(name), path, flags);
2670 kern_path_mountpoint(int dfd, const char *name, struct path *path,
2673 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
2675 EXPORT_SYMBOL(kern_path_mountpoint);
2677 int __check_sticky(struct inode *dir, struct inode *inode)
2679 kuid_t fsuid = current_fsuid();
2681 if (uid_eq(inode->i_uid, fsuid))
2683 if (uid_eq(dir->i_uid, fsuid))
2685 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
2687 EXPORT_SYMBOL(__check_sticky);
2690 * Check whether we can remove a link victim from directory dir, check
2691 * whether the type of victim is right.
2692 * 1. We can't do it if dir is read-only (done in permission())
2693 * 2. We should have write and exec permissions on dir
2694 * 3. We can't remove anything from append-only dir
2695 * 4. We can't do anything with immutable dir (done in permission())
2696 * 5. If the sticky bit on dir is set we should either
2697 * a. be owner of dir, or
2698 * b. be owner of victim, or
2699 * c. have CAP_FOWNER capability
2700 * 6. If the victim is append-only or immutable we can't do antyhing with
2701 * links pointing to it.
2702 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2703 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2704 * 9. We can't remove a root or mountpoint.
2705 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2706 * nfs_async_unlink().
2708 static int may_delete(struct vfsmount *mnt, struct inode *dir, struct dentry *victim, bool isdir)
2710 struct inode *inode = d_backing_inode(victim);
2713 if (d_is_negative(victim))
2717 BUG_ON(victim->d_parent->d_inode != dir);
2718 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
2720 error = inode_permission2(mnt, dir, MAY_WRITE | MAY_EXEC);
2726 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2727 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
2730 if (!d_is_dir(victim))
2732 if (IS_ROOT(victim))
2734 } else if (d_is_dir(victim))
2736 if (IS_DEADDIR(dir))
2738 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2743 /* Check whether we can create an object with dentry child in directory
2745 * 1. We can't do it if child already exists (open has special treatment for
2746 * this case, but since we are inlined it's OK)
2747 * 2. We can't do it if dir is read-only (done in permission())
2748 * 3. We should have write and exec permissions on dir
2749 * 4. We can't do it if dir is immutable (done in permission())
2751 static inline int may_create(struct vfsmount *mnt, struct inode *dir, struct dentry *child)
2753 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
2756 if (IS_DEADDIR(dir))
2758 return inode_permission2(mnt, dir, MAY_WRITE | MAY_EXEC);
2762 * p1 and p2 should be directories on the same fs.
2764 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2769 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2773 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2775 p = d_ancestor(p2, p1);
2777 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2778 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2782 p = d_ancestor(p1, p2);
2784 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2785 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2789 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2790 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT2);
2793 EXPORT_SYMBOL(lock_rename);
2795 void unlock_rename(struct dentry *p1, struct dentry *p2)
2797 mutex_unlock(&p1->d_inode->i_mutex);
2799 mutex_unlock(&p2->d_inode->i_mutex);
2800 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2803 EXPORT_SYMBOL(unlock_rename);
2805 int vfs_create2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry,
2806 umode_t mode, bool want_excl)
2808 int error = may_create(mnt, dir, dentry);
2812 if (!dir->i_op->create)
2813 return -EACCES; /* shouldn't it be ENOSYS? */
2816 error = security_inode_create(dir, dentry, mode);
2819 error = dir->i_op->create(dir, dentry, mode, want_excl);
2822 error = security_inode_post_create(dir, dentry, mode);
2826 fsnotify_create(dir, dentry);
2830 EXPORT_SYMBOL(vfs_create2);
2832 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2835 return vfs_create2(NULL, dir, dentry, mode, want_excl);
2837 EXPORT_SYMBOL(vfs_create);
2839 static int may_open(struct path *path, int acc_mode, int flag)
2841 struct dentry *dentry = path->dentry;
2842 struct vfsmount *mnt = path->mnt;
2843 struct inode *inode = dentry->d_inode;
2853 switch (inode->i_mode & S_IFMT) {
2857 if (acc_mode & MAY_WRITE)
2862 if (path->mnt->mnt_flags & MNT_NODEV)
2871 error = inode_permission2(mnt, inode, acc_mode);
2876 * An append-only file must be opened in append mode for writing.
2878 if (IS_APPEND(inode)) {
2879 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2885 /* O_NOATIME can only be set by the owner or superuser */
2886 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2892 static int handle_truncate(struct file *filp)
2894 struct path *path = &filp->f_path;
2895 struct inode *inode = path->dentry->d_inode;
2896 int error = get_write_access(inode);
2900 * Refuse to truncate files with mandatory locks held on them.
2902 error = locks_verify_locked(filp);
2904 error = security_path_truncate(path);
2906 error = do_truncate2(path->mnt, path->dentry, 0,
2907 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2910 put_write_access(inode);
2914 static inline int open_to_namei_flags(int flag)
2916 if ((flag & O_ACCMODE) == 3)
2921 static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2923 int error = security_path_mknod(dir, dentry, mode, 0);
2927 error = inode_permission2(dir->mnt, dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2931 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2935 * Attempt to atomically look up, create and open a file from a negative
2938 * Returns 0 if successful. The file will have been created and attached to
2939 * @file by the filesystem calling finish_open().
2941 * Returns 1 if the file was looked up only or didn't need creating. The
2942 * caller will need to perform the open themselves. @path will have been
2943 * updated to point to the new dentry. This may be negative.
2945 * Returns an error code otherwise.
2947 static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2948 struct path *path, struct file *file,
2949 const struct open_flags *op,
2950 bool got_write, bool need_lookup,
2953 struct inode *dir = nd->path.dentry->d_inode;
2954 unsigned open_flag = open_to_namei_flags(op->open_flag);
2958 int create_error = 0;
2959 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2962 BUG_ON(dentry->d_inode);
2964 /* Don't create child dentry for a dead directory. */
2965 if (unlikely(IS_DEADDIR(dir))) {
2971 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2972 mode &= ~current_umask();
2974 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2976 open_flag &= ~O_TRUNC;
2979 * Checking write permission is tricky, bacuse we don't know if we are
2980 * going to actually need it: O_CREAT opens should work as long as the
2981 * file exists. But checking existence breaks atomicity. The trick is
2982 * to check access and if not granted clear O_CREAT from the flags.
2984 * Another problem is returing the "right" error value (e.g. for an
2985 * O_EXCL open we want to return EEXIST not EROFS).
2987 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2988 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2989 if (!(open_flag & O_CREAT)) {
2991 * No O_CREATE -> atomicity not a requirement -> fall
2992 * back to lookup + open
2995 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2996 /* Fall back and fail with the right error */
2997 create_error = -EROFS;
3000 /* No side effects, safe to clear O_CREAT */
3001 create_error = -EROFS;
3002 open_flag &= ~O_CREAT;
3006 if (open_flag & O_CREAT) {
3007 error = may_o_create(&nd->path, dentry, mode);
3009 create_error = error;
3010 if (open_flag & O_EXCL)
3012 open_flag &= ~O_CREAT;
3016 if (nd->flags & LOOKUP_DIRECTORY)
3017 open_flag |= O_DIRECTORY;
3019 file->f_path.dentry = DENTRY_NOT_SET;
3020 file->f_path.mnt = nd->path.mnt;
3021 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
3024 if (create_error && error == -ENOENT)
3025 error = create_error;
3029 if (error) { /* returned 1, that is */
3030 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
3034 if (file->f_path.dentry) {
3036 dentry = file->f_path.dentry;
3038 if (*opened & FILE_CREATED)
3039 fsnotify_create(dir, dentry);
3040 if (!dentry->d_inode) {
3041 WARN_ON(*opened & FILE_CREATED);
3043 error = create_error;
3047 if (excl && !(*opened & FILE_CREATED)) {
3056 * We didn't have the inode before the open, so check open permission
3059 acc_mode = op->acc_mode;
3060 if (*opened & FILE_CREATED) {
3061 WARN_ON(!(open_flag & O_CREAT));
3062 fsnotify_create(dir, dentry);
3063 acc_mode = MAY_OPEN;
3065 error = may_open(&file->f_path, acc_mode, open_flag);
3075 dentry = lookup_real(dir, dentry, nd->flags);
3077 return PTR_ERR(dentry);
3079 if (create_error && !dentry->d_inode) {
3080 error = create_error;
3084 path->dentry = dentry;
3085 path->mnt = nd->path.mnt;
3090 * Look up and maybe create and open the last component.
3092 * Must be called with i_mutex held on parent.
3094 * Returns 0 if the file was successfully atomically created (if necessary) and
3095 * opened. In this case the file will be returned attached to @file.
3097 * Returns 1 if the file was not completely opened at this time, though lookups
3098 * and creations will have been performed and the dentry returned in @path will
3099 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
3100 * specified then a negative dentry may be returned.
3102 * An error code is returned otherwise.
3104 * FILE_CREATE will be set in @*opened if the dentry was created and will be
3105 * cleared otherwise prior to returning.
3107 static int lookup_open(struct nameidata *nd, struct path *path,
3109 const struct open_flags *op,
3110 bool got_write, int *opened)
3112 struct dentry *dir = nd->path.dentry;
3113 struct vfsmount *mnt = nd->path.mnt;
3114 struct inode *dir_inode = dir->d_inode;
3115 struct dentry *dentry;
3119 *opened &= ~FILE_CREATED;
3120 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
3122 return PTR_ERR(dentry);
3124 /* Cached positive dentry: will open in f_op->open */
3125 if (!need_lookup && dentry->d_inode)
3128 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
3129 return atomic_open(nd, dentry, path, file, op, got_write,
3130 need_lookup, opened);
3134 BUG_ON(dentry->d_inode);
3136 dentry = lookup_real(dir_inode, dentry, nd->flags);
3138 return PTR_ERR(dentry);
3141 /* Negative dentry, just create the file */
3142 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
3143 umode_t mode = op->mode;
3144 if (!IS_POSIXACL(dir->d_inode))
3145 mode &= ~current_umask();
3147 * This write is needed to ensure that a
3148 * rw->ro transition does not occur between
3149 * the time when the file is created and when
3150 * a permanent write count is taken through
3151 * the 'struct file' in finish_open().
3157 *opened |= FILE_CREATED;
3158 error = security_path_mknod(&nd->path, dentry, mode, 0);
3161 error = vfs_create2(mnt, dir->d_inode, dentry, mode,
3162 nd->flags & LOOKUP_EXCL);
3167 path->dentry = dentry;
3168 path->mnt = nd->path.mnt;
3177 * Handle the last step of open()
3179 static int do_last(struct nameidata *nd,
3180 struct file *file, const struct open_flags *op,
3183 struct dentry *dir = nd->path.dentry;
3184 int open_flag = op->open_flag;
3185 bool will_truncate = (open_flag & O_TRUNC) != 0;
3186 bool got_write = false;
3187 int acc_mode = op->acc_mode;
3189 struct inode *inode;
3190 struct path save_parent = { .dentry = NULL, .mnt = NULL };
3192 bool retried = false;
3195 nd->flags &= ~LOOKUP_PARENT;
3196 nd->flags |= op->intent;
3198 if (nd->last_type != LAST_NORM) {
3199 error = handle_dots(nd, nd->last_type);
3200 if (unlikely(error))
3205 if (!(open_flag & O_CREAT)) {
3206 if (nd->last.name[nd->last.len])
3207 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3208 /* we _can_ be in RCU mode here */
3209 error = lookup_fast(nd, &path, &inode, &seq);
3216 BUG_ON(nd->inode != dir->d_inode);
3218 /* create side of things */
3220 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3221 * has been cleared when we got to the last component we are
3224 error = complete_walk(nd);
3228 audit_inode(nd->name, dir, LOOKUP_PARENT);
3229 /* trailing slashes? */
3230 if (unlikely(nd->last.name[nd->last.len]))
3235 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
3236 error = mnt_want_write(nd->path.mnt);
3240 * do _not_ fail yet - we might not need that or fail with
3241 * a different error; let lookup_open() decide; we'll be
3242 * dropping this one anyway.
3245 mutex_lock(&dir->d_inode->i_mutex);
3246 error = lookup_open(nd, &path, file, op, got_write, opened);
3247 mutex_unlock(&dir->d_inode->i_mutex);
3253 if ((*opened & FILE_CREATED) ||
3254 !S_ISREG(file_inode(file)->i_mode))
3255 will_truncate = false;
3257 audit_inode(nd->name, file->f_path.dentry, 0);
3261 if (*opened & FILE_CREATED) {
3262 /* Don't check for write permission, don't truncate */
3263 open_flag &= ~O_TRUNC;
3264 will_truncate = false;
3265 acc_mode = MAY_OPEN;
3266 path_to_nameidata(&path, nd);
3267 goto finish_open_created;
3271 * create/update audit record if it already exists.
3273 if (d_is_positive(path.dentry))
3274 audit_inode(nd->name, path.dentry, 0);
3277 * If atomic_open() acquired write access it is dropped now due to
3278 * possible mount and symlink following (this might be optimized away if
3282 mnt_drop_write(nd->path.mnt);
3286 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3287 path_to_nameidata(&path, nd);
3291 error = follow_managed(&path, nd);
3292 if (unlikely(error < 0))
3295 BUG_ON(nd->flags & LOOKUP_RCU);
3296 seq = 0; /* out of RCU mode, so the value doesn't matter */
3297 if (unlikely(d_is_negative(path.dentry))) {
3298 path_to_nameidata(&path, nd);
3301 inode = d_backing_inode(path.dentry);
3305 error = should_follow_link(nd, &path, nd->flags & LOOKUP_FOLLOW,
3307 if (unlikely(error))
3310 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path.mnt) {
3311 path_to_nameidata(&path, nd);
3313 save_parent.dentry = nd->path.dentry;
3314 save_parent.mnt = mntget(path.mnt);
3315 nd->path.dentry = path.dentry;
3320 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
3322 error = complete_walk(nd);
3324 path_put(&save_parent);
3327 audit_inode(nd->name, nd->path.dentry, 0);
3328 if (unlikely(d_is_symlink(nd->path.dentry)) && !(open_flag & O_PATH)) {
3332 if (open_flag & O_CREAT) {
3334 if (d_is_dir(nd->path.dentry))
3336 error = may_create_in_sticky(dir,
3337 d_backing_inode(nd->path.dentry));
3338 if (unlikely(error))
3342 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
3344 if (!d_is_reg(nd->path.dentry))
3345 will_truncate = false;
3347 if (will_truncate) {
3348 error = mnt_want_write(nd->path.mnt);
3353 finish_open_created:
3354 error = may_open(&nd->path, acc_mode, open_flag);
3358 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3359 error = vfs_open(&nd->path, file, current_cred());
3361 *opened |= FILE_OPENED;
3363 if (error == -EOPENSTALE)
3368 error = open_check_o_direct(file);
3371 error = ima_file_check(file, op->acc_mode, *opened);
3375 if (will_truncate) {
3376 error = handle_truncate(file);
3381 if (unlikely(error > 0)) {
3386 mnt_drop_write(nd->path.mnt);
3387 path_put(&save_parent);
3395 /* If no saved parent or already retried then can't retry */
3396 if (!save_parent.dentry || retried)
3399 BUG_ON(save_parent.dentry != dir);
3400 path_put(&nd->path);
3401 nd->path = save_parent;
3402 nd->inode = dir->d_inode;
3403 save_parent.mnt = NULL;
3404 save_parent.dentry = NULL;
3406 mnt_drop_write(nd->path.mnt);
3413 static int do_tmpfile(struct nameidata *nd, unsigned flags,
3414 const struct open_flags *op,
3415 struct file *file, int *opened)
3417 static const struct qstr name = QSTR_INIT("/", 1);
3418 struct dentry *child;
3421 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
3422 if (unlikely(error))
3424 error = mnt_want_write(path.mnt);
3425 if (unlikely(error))
3427 dir = path.dentry->d_inode;
3428 /* we want directory to be writable */
3429 error = inode_permission2(path.mnt, dir, MAY_WRITE | MAY_EXEC);
3432 if (!dir->i_op->tmpfile) {
3433 error = -EOPNOTSUPP;
3436 child = d_alloc(path.dentry, &name);
3437 if (unlikely(!child)) {
3442 path.dentry = child;
3443 error = dir->i_op->tmpfile(dir, child, op->mode);
3446 audit_inode(nd->name, child, 0);
3447 /* Don't check for other permissions, the inode was just created */
3448 error = may_open(&path, MAY_OPEN, op->open_flag);
3451 file->f_path.mnt = path.mnt;
3452 error = finish_open(file, child, NULL, opened);
3455 error = open_check_o_direct(file);
3458 } else if (!(op->open_flag & O_EXCL)) {
3459 struct inode *inode = file_inode(file);
3460 spin_lock(&inode->i_lock);
3461 inode->i_state |= I_LINKABLE;
3462 spin_unlock(&inode->i_lock);
3465 mnt_drop_write(path.mnt);
3471 static struct file *path_openat(struct nameidata *nd,
3472 const struct open_flags *op, unsigned flags)
3479 file = get_empty_filp();
3483 file->f_flags = op->open_flag;
3485 if (unlikely(file->f_flags & __O_TMPFILE)) {
3486 error = do_tmpfile(nd, flags, op, file, &opened);
3490 s = path_init(nd, flags);
3495 while (!(error = link_path_walk(s, nd)) &&
3496 (error = do_last(nd, file, op, &opened)) > 0) {
3497 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3498 s = trailing_symlink(nd);
3506 if (!(opened & FILE_OPENED)) {
3510 if (unlikely(error)) {
3511 if (error == -EOPENSTALE) {
3512 if (flags & LOOKUP_RCU)
3517 file = ERR_PTR(error);
3519 global_filetable_add(file);
3524 struct file *do_filp_open(int dfd, struct filename *pathname,
3525 const struct open_flags *op)
3527 struct nameidata nd;
3528 int flags = op->lookup_flags;
3531 set_nameidata(&nd, dfd, pathname);
3532 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
3533 if (unlikely(filp == ERR_PTR(-ECHILD)))
3534 filp = path_openat(&nd, op, flags);
3535 if (unlikely(filp == ERR_PTR(-ESTALE)))
3536 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
3537 restore_nameidata();
3541 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3542 const char *name, const struct open_flags *op)
3544 struct nameidata nd;
3546 struct filename *filename;
3547 int flags = op->lookup_flags | LOOKUP_ROOT;
3550 nd.root.dentry = dentry;
3552 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
3553 return ERR_PTR(-ELOOP);
3555 filename = getname_kernel(name);
3556 if (IS_ERR(filename))
3557 return ERR_CAST(filename);
3559 set_nameidata(&nd, -1, filename);
3560 file = path_openat(&nd, op, flags | LOOKUP_RCU);
3561 if (unlikely(file == ERR_PTR(-ECHILD)))
3562 file = path_openat(&nd, op, flags);
3563 if (unlikely(file == ERR_PTR(-ESTALE)))
3564 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
3565 restore_nameidata();
3570 static struct dentry *filename_create(int dfd, struct filename *name,
3571 struct path *path, unsigned int lookup_flags)
3573 struct dentry *dentry = ERR_PTR(-EEXIST);
3578 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3581 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3582 * other flags passed in are ignored!
3584 lookup_flags &= LOOKUP_REVAL;
3586 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3588 return ERR_CAST(name);
3591 * Yucky last component or no last component at all?
3592 * (foo/., foo/.., /////)
3594 if (unlikely(type != LAST_NORM))
3597 /* don't fail immediately if it's r/o, at least try to report other errors */
3598 err2 = mnt_want_write(path->mnt);
3600 * Do the final lookup.
3602 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3603 mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3604 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
3609 if (d_is_positive(dentry))
3613 * Special case - lookup gave negative, but... we had foo/bar/
3614 * From the vfs_mknod() POV we just have a negative dentry -
3615 * all is fine. Let's be bastards - you had / on the end, you've
3616 * been asking for (non-existent) directory. -ENOENT for you.
3618 if (unlikely(!is_dir && last.name[last.len])) {
3622 if (unlikely(err2)) {
3630 dentry = ERR_PTR(error);
3632 mutex_unlock(&path->dentry->d_inode->i_mutex);
3634 mnt_drop_write(path->mnt);
3641 struct dentry *kern_path_create(int dfd, const char *pathname,
3642 struct path *path, unsigned int lookup_flags)
3644 return filename_create(dfd, getname_kernel(pathname),
3645 path, lookup_flags);
3647 EXPORT_SYMBOL(kern_path_create);
3649 void done_path_create(struct path *path, struct dentry *dentry)
3652 mutex_unlock(&path->dentry->d_inode->i_mutex);
3653 mnt_drop_write(path->mnt);
3656 EXPORT_SYMBOL(done_path_create);
3658 inline struct dentry *user_path_create(int dfd, const char __user *pathname,
3659 struct path *path, unsigned int lookup_flags)
3661 return filename_create(dfd, getname(pathname), path, lookup_flags);
3663 EXPORT_SYMBOL(user_path_create);
3665 int vfs_mknod2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3667 int error = may_create(mnt, dir, dentry);
3672 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
3675 if (!dir->i_op->mknod)
3678 error = devcgroup_inode_mknod(mode, dev);
3682 error = security_inode_mknod(dir, dentry, mode, dev);
3686 error = dir->i_op->mknod(dir, dentry, mode, dev);
3690 error = security_inode_post_create(dir, dentry, mode);
3695 fsnotify_create(dir, dentry);
3699 EXPORT_SYMBOL(vfs_mknod2);
3701 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3703 return vfs_mknod2(NULL, dir, dentry, mode, dev);
3705 EXPORT_SYMBOL(vfs_mknod);
3707 static int may_mknod(umode_t mode)
3709 switch (mode & S_IFMT) {
3715 case 0: /* zero mode translates to S_IFREG */
3724 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3727 struct dentry *dentry;
3730 unsigned int lookup_flags = 0;
3732 error = may_mknod(mode);
3736 dentry = user_path_create(dfd, filename, &path, lookup_flags);
3738 return PTR_ERR(dentry);
3740 if (!IS_POSIXACL(path.dentry->d_inode))
3741 mode &= ~current_umask();
3742 error = security_path_mknod(&path, dentry, mode, dev);
3745 switch (mode & S_IFMT) {
3746 case 0: case S_IFREG:
3747 error = vfs_create2(path.mnt, path.dentry->d_inode,dentry,mode,true);
3749 case S_IFCHR: case S_IFBLK:
3750 error = vfs_mknod2(path.mnt, path.dentry->d_inode,dentry,mode,
3751 new_decode_dev(dev));
3753 case S_IFIFO: case S_IFSOCK:
3754 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
3758 done_path_create(&path, dentry);
3759 if (retry_estale(error, lookup_flags)) {
3760 lookup_flags |= LOOKUP_REVAL;
3766 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
3768 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3771 int vfs_mkdir2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, umode_t mode)
3773 int error = may_create(mnt, dir, dentry);
3774 unsigned max_links = dir->i_sb->s_max_links;
3779 if (!dir->i_op->mkdir)
3782 mode &= (S_IRWXUGO|S_ISVTX);
3783 error = security_inode_mkdir(dir, dentry, mode);
3787 if (max_links && dir->i_nlink >= max_links)
3790 error = dir->i_op->mkdir(dir, dentry, mode);
3792 fsnotify_mkdir(dir, dentry);
3795 EXPORT_SYMBOL(vfs_mkdir2);
3797 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3799 return vfs_mkdir2(NULL, dir, dentry, mode);
3801 EXPORT_SYMBOL(vfs_mkdir);
3803 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3805 struct dentry *dentry;
3808 unsigned int lookup_flags = LOOKUP_DIRECTORY;
3811 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
3813 return PTR_ERR(dentry);
3815 if (!IS_POSIXACL(path.dentry->d_inode))
3816 mode &= ~current_umask();
3817 error = security_path_mkdir(&path, dentry, mode);
3819 error = vfs_mkdir2(path.mnt, path.dentry->d_inode, dentry, mode);
3820 done_path_create(&path, dentry);
3821 if (retry_estale(error, lookup_flags)) {
3822 lookup_flags |= LOOKUP_REVAL;
3828 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
3830 return sys_mkdirat(AT_FDCWD, pathname, mode);
3834 * The dentry_unhash() helper will try to drop the dentry early: we
3835 * should have a usage count of 1 if we're the only user of this
3836 * dentry, and if that is true (possibly after pruning the dcache),
3837 * then we drop the dentry now.
3839 * A low-level filesystem can, if it choses, legally
3842 * if (!d_unhashed(dentry))
3845 * if it cannot handle the case of removing a directory
3846 * that is still in use by something else..
3848 void dentry_unhash(struct dentry *dentry)
3850 shrink_dcache_parent(dentry);
3851 spin_lock(&dentry->d_lock);
3852 if (dentry->d_lockref.count == 1)
3854 spin_unlock(&dentry->d_lock);
3856 EXPORT_SYMBOL(dentry_unhash);
3858 int vfs_rmdir2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry)
3860 int error = may_delete(mnt, dir, dentry, 1);
3865 if (!dir->i_op->rmdir)
3869 mutex_lock(&dentry->d_inode->i_mutex);
3872 if (is_local_mountpoint(dentry))
3875 error = security_inode_rmdir(dir, dentry);
3879 shrink_dcache_parent(dentry);
3880 error = dir->i_op->rmdir(dir, dentry);
3884 dentry->d_inode->i_flags |= S_DEAD;
3886 detach_mounts(dentry);
3889 mutex_unlock(&dentry->d_inode->i_mutex);
3895 EXPORT_SYMBOL(vfs_rmdir2);
3897 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3899 return vfs_rmdir2(NULL, dir, dentry);
3901 EXPORT_SYMBOL(vfs_rmdir);
3903 static long do_rmdir(int dfd, const char __user *pathname)
3906 struct filename *name;
3907 struct dentry *dentry;
3911 unsigned int lookup_flags = 0;
3913 name = user_path_parent(dfd, pathname,
3914 &path, &last, &type, lookup_flags);
3916 return PTR_ERR(name);
3930 error = mnt_want_write(path.mnt);
3934 mutex_lock_nested(&path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3935 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
3936 error = PTR_ERR(dentry);
3939 if (!dentry->d_inode) {
3943 error = security_path_rmdir(&path, dentry);
3946 error = vfs_rmdir2(path.mnt, path.dentry->d_inode, dentry);
3950 mutex_unlock(&path.dentry->d_inode->i_mutex);
3951 mnt_drop_write(path.mnt);
3955 if (retry_estale(error, lookup_flags)) {
3956 lookup_flags |= LOOKUP_REVAL;
3962 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
3964 return do_rmdir(AT_FDCWD, pathname);
3968 * vfs_unlink - unlink a filesystem object
3969 * @dir: parent directory
3971 * @delegated_inode: returns victim inode, if the inode is delegated.
3973 * The caller must hold dir->i_mutex.
3975 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3976 * return a reference to the inode in delegated_inode. The caller
3977 * should then break the delegation on that inode and retry. Because
3978 * breaking a delegation may take a long time, the caller should drop
3979 * dir->i_mutex before doing so.
3981 * Alternatively, a caller may pass NULL for delegated_inode. This may
3982 * be appropriate for callers that expect the underlying filesystem not
3983 * to be NFS exported.
3985 int vfs_unlink2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
3987 struct inode *target = dentry->d_inode;
3988 int error = may_delete(mnt, dir, dentry, 0);
3993 if (!dir->i_op->unlink)
3996 mutex_lock(&target->i_mutex);
3997 if (is_local_mountpoint(dentry))
4000 error = security_inode_unlink(dir, dentry);
4002 error = try_break_deleg(target, delegated_inode);
4005 error = dir->i_op->unlink(dir, dentry);
4008 detach_mounts(dentry);
4013 mutex_unlock(&target->i_mutex);
4015 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
4016 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
4017 fsnotify_link_count(target);
4023 EXPORT_SYMBOL(vfs_unlink2);
4025 int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
4027 return vfs_unlink2(NULL, dir, dentry, delegated_inode);
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 mutex_lock_nested(&path.dentry->d_inode->i_mutex, 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_unlink2(path.mnt, path.dentry->d_inode, dentry, &delegated_inode);
4080 mutex_unlock(&path.dentry->d_inode->i_mutex);
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_symlink2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, const char *oldname)
4128 int error = may_create(mnt, 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_symlink2);
4147 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
4149 return vfs_symlink2(NULL, dir, dentry, oldname);
4151 EXPORT_SYMBOL(vfs_symlink);
4153 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4154 int, newdfd, const char __user *, newname)
4157 struct filename *from;
4158 struct dentry *dentry;
4160 unsigned int lookup_flags = 0;
4162 from = getname(oldname);
4164 return PTR_ERR(from);
4166 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
4167 error = PTR_ERR(dentry);
4171 error = security_path_symlink(&path, dentry, from->name);
4173 error = vfs_symlink2(path.mnt, path.dentry->d_inode, dentry, from->name);
4174 done_path_create(&path, dentry);
4175 if (retry_estale(error, lookup_flags)) {
4176 lookup_flags |= LOOKUP_REVAL;
4184 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
4186 return sys_symlinkat(oldname, AT_FDCWD, newname);
4190 * vfs_link - create a new link
4191 * @old_dentry: object to be linked
4193 * @new_dentry: where to create the new link
4194 * @delegated_inode: returns inode needing a delegation break
4196 * The caller must hold dir->i_mutex
4198 * If vfs_link discovers a delegation on the to-be-linked file in need
4199 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4200 * inode in delegated_inode. The caller should then break the delegation
4201 * and retry. Because breaking a delegation may take a long time, the
4202 * caller should drop the i_mutex before doing so.
4204 * Alternatively, a caller may pass NULL for delegated_inode. This may
4205 * be appropriate for callers that expect the underlying filesystem not
4206 * to be NFS exported.
4208 int vfs_link2(struct vfsmount *mnt, struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
4210 struct inode *inode = old_dentry->d_inode;
4211 unsigned max_links = dir->i_sb->s_max_links;
4217 error = may_create(mnt, dir, new_dentry);
4221 if (dir->i_sb != inode->i_sb)
4225 * A link to an append-only or immutable file cannot be created.
4227 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4229 if (!dir->i_op->link)
4231 if (S_ISDIR(inode->i_mode))
4234 error = security_inode_link(old_dentry, dir, new_dentry);
4238 mutex_lock(&inode->i_mutex);
4239 /* Make sure we don't allow creating hardlink to an unlinked file */
4240 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
4242 else if (max_links && inode->i_nlink >= max_links)
4245 error = try_break_deleg(inode, delegated_inode);
4247 error = dir->i_op->link(old_dentry, dir, new_dentry);
4250 if (!error && (inode->i_state & I_LINKABLE)) {
4251 spin_lock(&inode->i_lock);
4252 inode->i_state &= ~I_LINKABLE;
4253 spin_unlock(&inode->i_lock);
4255 mutex_unlock(&inode->i_mutex);
4257 fsnotify_link(dir, inode, new_dentry);
4260 EXPORT_SYMBOL(vfs_link2);
4262 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
4264 return vfs_link2(NULL, old_dentry, dir, new_dentry, delegated_inode);
4266 EXPORT_SYMBOL(vfs_link);
4269 * Hardlinks are often used in delicate situations. We avoid
4270 * security-related surprises by not following symlinks on the
4273 * We don't follow them on the oldname either to be compatible
4274 * with linux 2.0, and to avoid hard-linking to directories
4275 * and other special files. --ADM
4277 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4278 int, newdfd, const char __user *, newname, int, flags)
4280 struct dentry *new_dentry;
4281 struct path old_path, new_path;
4282 struct inode *delegated_inode = NULL;
4286 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
4289 * To use null names we require CAP_DAC_READ_SEARCH
4290 * This ensures that not everyone will be able to create
4291 * handlink using the passed filedescriptor.
4293 if (flags & AT_EMPTY_PATH) {
4294 if (!capable(CAP_DAC_READ_SEARCH))
4299 if (flags & AT_SYMLINK_FOLLOW)
4300 how |= LOOKUP_FOLLOW;
4302 error = user_path_at(olddfd, oldname, how, &old_path);
4306 new_dentry = user_path_create(newdfd, newname, &new_path,
4307 (how & LOOKUP_REVAL));
4308 error = PTR_ERR(new_dentry);
4309 if (IS_ERR(new_dentry))
4313 if (old_path.mnt != new_path.mnt)
4315 error = may_linkat(&old_path);
4316 if (unlikely(error))
4318 error = security_path_link(old_path.dentry, &new_path, new_dentry);
4321 error = vfs_link2(old_path.mnt, old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
4323 done_path_create(&new_path, new_dentry);
4324 if (delegated_inode) {
4325 error = break_deleg_wait(&delegated_inode);
4327 path_put(&old_path);
4331 if (retry_estale(error, how)) {
4332 path_put(&old_path);
4333 how |= LOOKUP_REVAL;
4337 path_put(&old_path);
4342 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
4344 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
4348 * vfs_rename - rename a filesystem object
4349 * @old_dir: parent of source
4350 * @old_dentry: source
4351 * @new_dir: parent of destination
4352 * @new_dentry: destination
4353 * @delegated_inode: returns an inode needing a delegation break
4354 * @flags: rename flags
4356 * The caller must hold multiple mutexes--see lock_rename()).
4358 * If vfs_rename discovers a delegation in need of breaking at either
4359 * the source or destination, it will return -EWOULDBLOCK and return a
4360 * reference to the inode in delegated_inode. The caller should then
4361 * break the delegation and retry. Because breaking a delegation may
4362 * take a long time, the caller should drop all locks before doing
4365 * Alternatively, a caller may pass NULL for delegated_inode. This may
4366 * be appropriate for callers that expect the underlying filesystem not
4367 * to be NFS exported.
4369 * The worst of all namespace operations - renaming directory. "Perverted"
4370 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4372 * a) we can get into loop creation.
4373 * b) race potential - two innocent renames can create a loop together.
4374 * That's where 4.4 screws up. Current fix: serialization on
4375 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
4377 * c) we have to lock _four_ objects - parents and victim (if it exists),
4378 * and source (if it is not a directory).
4379 * And that - after we got ->i_mutex on parents (until then we don't know
4380 * whether the target exists). Solution: try to be smart with locking
4381 * order for inodes. We rely on the fact that tree topology may change
4382 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
4383 * move will be locked. Thus we can rank directories by the tree
4384 * (ancestors first) and rank all non-directories after them.
4385 * That works since everybody except rename does "lock parent, lookup,
4386 * lock child" and rename is under ->s_vfs_rename_mutex.
4387 * HOWEVER, it relies on the assumption that any object with ->lookup()
4388 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4389 * we'd better make sure that there's no link(2) for them.
4390 * d) conversion from fhandle to dentry may come in the wrong moment - when
4391 * we are removing the target. Solution: we will have to grab ->i_mutex
4392 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4393 * ->i_mutex on parents, which works but leads to some truly excessive
4396 int vfs_rename2(struct vfsmount *mnt,
4397 struct inode *old_dir, struct dentry *old_dentry,
4398 struct inode *new_dir, struct dentry *new_dentry,
4399 struct inode **delegated_inode, unsigned int flags)
4402 bool is_dir = d_is_dir(old_dentry);
4403 struct inode *source = old_dentry->d_inode;
4404 struct inode *target = new_dentry->d_inode;
4405 bool new_is_dir = false;
4406 unsigned max_links = new_dir->i_sb->s_max_links;
4407 struct name_snapshot old_name;
4410 * Check source == target.
4411 * On overlayfs need to look at underlying inodes.
4413 if (vfs_select_inode(old_dentry, 0) == vfs_select_inode(new_dentry, 0))
4416 error = may_delete(mnt, old_dir, old_dentry, is_dir);
4421 error = may_create(mnt, new_dir, new_dentry);
4423 new_is_dir = d_is_dir(new_dentry);
4425 if (!(flags & RENAME_EXCHANGE))
4426 error = may_delete(mnt, new_dir, new_dentry, is_dir);
4428 error = may_delete(mnt, new_dir, new_dentry, new_is_dir);
4433 if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
4436 if (flags && !old_dir->i_op->rename2)
4440 * If we are going to change the parent - check write permissions,
4441 * we'll need to flip '..'.
4443 if (new_dir != old_dir) {
4445 error = inode_permission2(mnt, source, MAY_WRITE);
4449 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4450 error = inode_permission2(mnt, target, MAY_WRITE);
4456 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4461 take_dentry_name_snapshot(&old_name, old_dentry);
4463 if (!is_dir || (flags & RENAME_EXCHANGE))
4464 lock_two_nondirectories(source, target);
4466 mutex_lock(&target->i_mutex);
4469 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
4472 if (max_links && new_dir != old_dir) {
4474 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
4476 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4477 old_dir->i_nlink >= max_links)
4480 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4481 shrink_dcache_parent(new_dentry);
4483 error = try_break_deleg(source, delegated_inode);
4487 if (target && !new_is_dir) {
4488 error = try_break_deleg(target, delegated_inode);
4492 if (!old_dir->i_op->rename2) {
4493 error = old_dir->i_op->rename(old_dir, old_dentry,
4494 new_dir, new_dentry);
4496 WARN_ON(old_dir->i_op->rename != NULL);
4497 error = old_dir->i_op->rename2(old_dir, old_dentry,
4498 new_dir, new_dentry, flags);
4503 if (!(flags & RENAME_EXCHANGE) && target) {
4505 target->i_flags |= S_DEAD;
4506 dont_mount(new_dentry);
4507 detach_mounts(new_dentry);
4509 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4510 if (!(flags & RENAME_EXCHANGE))
4511 d_move(old_dentry, new_dentry);
4513 d_exchange(old_dentry, new_dentry);
4516 if (!is_dir || (flags & RENAME_EXCHANGE))
4517 unlock_two_nondirectories(source, target);
4519 mutex_unlock(&target->i_mutex);
4522 fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
4523 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4524 if (flags & RENAME_EXCHANGE) {
4525 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4526 new_is_dir, NULL, new_dentry);
4529 release_dentry_name_snapshot(&old_name);
4533 EXPORT_SYMBOL(vfs_rename2);
4535 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4536 struct inode *new_dir, struct dentry *new_dentry,
4537 struct inode **delegated_inode, unsigned int flags)
4539 return vfs_rename2(NULL, old_dir, old_dentry, new_dir, new_dentry, delegated_inode, flags);
4541 EXPORT_SYMBOL(vfs_rename);
4543 SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4544 int, newdfd, const char __user *, newname, unsigned int, flags)
4546 struct dentry *old_dentry, *new_dentry;
4547 struct dentry *trap;
4548 struct path old_path, new_path;
4549 struct qstr old_last, new_last;
4550 int old_type, new_type;
4551 struct inode *delegated_inode = NULL;
4552 struct filename *from;
4553 struct filename *to;
4554 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
4555 bool should_retry = false;
4558 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
4561 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4562 (flags & RENAME_EXCHANGE))
4565 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4568 if (flags & RENAME_EXCHANGE)
4572 from = user_path_parent(olddfd, oldname,
4573 &old_path, &old_last, &old_type, lookup_flags);
4575 error = PTR_ERR(from);
4579 to = user_path_parent(newdfd, newname,
4580 &new_path, &new_last, &new_type, lookup_flags);
4582 error = PTR_ERR(to);
4587 if (old_path.mnt != new_path.mnt)
4591 if (old_type != LAST_NORM)
4594 if (flags & RENAME_NOREPLACE)
4596 if (new_type != LAST_NORM)
4599 error = mnt_want_write(old_path.mnt);
4604 trap = lock_rename(new_path.dentry, old_path.dentry);
4606 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
4607 error = PTR_ERR(old_dentry);
4608 if (IS_ERR(old_dentry))
4610 /* source must exist */
4612 if (d_is_negative(old_dentry))
4614 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
4615 error = PTR_ERR(new_dentry);
4616 if (IS_ERR(new_dentry))
4619 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4621 if (flags & RENAME_EXCHANGE) {
4623 if (d_is_negative(new_dentry))
4626 if (!d_is_dir(new_dentry)) {
4628 if (new_last.name[new_last.len])
4632 /* unless the source is a directory trailing slashes give -ENOTDIR */
4633 if (!d_is_dir(old_dentry)) {
4635 if (old_last.name[old_last.len])
4637 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
4640 /* source should not be ancestor of target */
4642 if (old_dentry == trap)
4644 /* target should not be an ancestor of source */
4645 if (!(flags & RENAME_EXCHANGE))
4647 if (new_dentry == trap)
4650 error = security_path_rename(&old_path, old_dentry,
4651 &new_path, new_dentry, flags);
4654 error = vfs_rename2(old_path.mnt, old_path.dentry->d_inode, old_dentry,
4655 new_path.dentry->d_inode, new_dentry,
4656 &delegated_inode, flags);
4662 unlock_rename(new_path.dentry, old_path.dentry);
4663 if (delegated_inode) {
4664 error = break_deleg_wait(&delegated_inode);
4668 mnt_drop_write(old_path.mnt);
4670 if (retry_estale(error, lookup_flags))
4671 should_retry = true;
4672 path_put(&new_path);
4675 path_put(&old_path);
4678 should_retry = false;
4679 lookup_flags |= LOOKUP_REVAL;
4686 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4687 int, newdfd, const char __user *, newname)
4689 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4692 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
4694 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
4697 int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4699 int error = may_create(NULL, dir, dentry);
4703 if (!dir->i_op->mknod)
4706 return dir->i_op->mknod(dir, dentry,
4707 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4709 EXPORT_SYMBOL(vfs_whiteout);
4711 int readlink_copy(char __user *buffer, int buflen, const char *link)
4713 int len = PTR_ERR(link);
4718 if (len > (unsigned) buflen)
4720 if (copy_to_user(buffer, link, len))
4725 EXPORT_SYMBOL(readlink_copy);
4728 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4729 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4730 * using) it for any given inode is up to filesystem.
4732 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4735 struct inode *inode = d_inode(dentry);
4736 const char *link = inode->i_link;
4740 link = inode->i_op->follow_link(dentry, &cookie);
4742 return PTR_ERR(link);
4744 res = readlink_copy(buffer, buflen, link);
4745 if (inode->i_op->put_link)
4746 inode->i_op->put_link(inode, cookie);
4749 EXPORT_SYMBOL(generic_readlink);
4751 /* get the link contents into pagecache */
4752 static char *page_getlink(struct dentry * dentry, struct page **ppage)
4756 struct address_space *mapping = dentry->d_inode->i_mapping;
4757 page = read_mapping_page(mapping, 0, NULL);
4762 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4766 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4768 struct page *page = NULL;
4769 int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
4772 page_cache_release(page);
4776 EXPORT_SYMBOL(page_readlink);
4778 const char *page_follow_link_light(struct dentry *dentry, void **cookie)
4780 struct page *page = NULL;
4781 char *res = page_getlink(dentry, &page);
4786 EXPORT_SYMBOL(page_follow_link_light);
4788 void page_put_link(struct inode *unused, void *cookie)
4790 struct page *page = cookie;
4792 page_cache_release(page);
4794 EXPORT_SYMBOL(page_put_link);
4797 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4799 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
4801 struct address_space *mapping = inode->i_mapping;
4806 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4808 flags |= AOP_FLAG_NOFS;
4811 err = pagecache_write_begin(NULL, mapping, 0, len-1,
4812 flags, &page, &fsdata);
4816 kaddr = kmap_atomic(page);
4817 memcpy(kaddr, symname, len-1);
4818 kunmap_atomic(kaddr);
4820 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4827 mark_inode_dirty(inode);
4832 EXPORT_SYMBOL(__page_symlink);
4834 int page_symlink(struct inode *inode, const char *symname, int len)
4836 return __page_symlink(inode, symname, len,
4837 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
4839 EXPORT_SYMBOL(page_symlink);
4841 const struct inode_operations page_symlink_inode_operations = {
4842 .readlink = generic_readlink,
4843 .follow_link = page_follow_link_light,
4844 .put_link = page_put_link,
4846 EXPORT_SYMBOL(page_symlink_inode_operations);