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>
39 #include <linux/statfs.h>
44 /* [Feb-1997 T. Schoebel-Theuer]
45 * Fundamental changes in the pathname lookup mechanisms (namei)
46 * were necessary because of omirr. The reason is that omirr needs
47 * to know the _real_ pathname, not the user-supplied one, in case
48 * of symlinks (and also when transname replacements occur).
50 * The new code replaces the old recursive symlink resolution with
51 * an iterative one (in case of non-nested symlink chains). It does
52 * this with calls to <fs>_follow_link().
53 * As a side effect, dir_namei(), _namei() and follow_link() are now
54 * replaced with a single function lookup_dentry() that can handle all
55 * the special cases of the former code.
57 * With the new dcache, the pathname is stored at each inode, at least as
58 * long as the refcount of the inode is positive. As a side effect, the
59 * size of the dcache depends on the inode cache and thus is dynamic.
61 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
62 * resolution to correspond with current state of the code.
64 * Note that the symlink resolution is not *completely* iterative.
65 * There is still a significant amount of tail- and mid- recursion in
66 * the algorithm. Also, note that <fs>_readlink() is not used in
67 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
68 * may return different results than <fs>_follow_link(). Many virtual
69 * filesystems (including /proc) exhibit this behavior.
72 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
73 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
74 * and the name already exists in form of a symlink, try to create the new
75 * name indicated by the symlink. The old code always complained that the
76 * name already exists, due to not following the symlink even if its target
77 * is nonexistent. The new semantics affects also mknod() and link() when
78 * the name is a symlink pointing to a non-existent name.
80 * I don't know which semantics is the right one, since I have no access
81 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
82 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
83 * "old" one. Personally, I think the new semantics is much more logical.
84 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
85 * file does succeed in both HP-UX and SunOs, but not in Solaris
86 * and in the old Linux semantics.
89 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
90 * semantics. See the comments in "open_namei" and "do_link" below.
92 * [10-Sep-98 Alan Modra] Another symlink change.
95 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
96 * inside the path - always follow.
97 * in the last component in creation/removal/renaming - never follow.
98 * if LOOKUP_FOLLOW passed - follow.
99 * if the pathname has trailing slashes - follow.
100 * otherwise - don't follow.
101 * (applied in that order).
103 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
104 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
105 * During the 2.4 we need to fix the userland stuff depending on it -
106 * hopefully we will be able to get rid of that wart in 2.5. So far only
107 * XEmacs seems to be relying on it...
110 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
111 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
112 * any extra contention...
115 /* In order to reduce some races, while at the same time doing additional
116 * checking and hopefully speeding things up, we copy filenames to the
117 * kernel data space before using them..
119 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
120 * PATH_MAX includes the nul terminator --RR.
123 #define DATA_MNT_POINT "/data"
124 #define DATA_PATITION_FS_TYPE "ext4"
125 #define PATH_NAME_MAX 4096
127 //unsigned long g_rsrvd_size = EMMC_DATA_RESERVED_SIZE;
128 //EXPORT_SYMBOL_GPL(g_rsrvd_size);
130 extern unsigned long g_rsrvd_size;
131 extern char reseved_space_nand_where[1024];
132 extern char reseved_space_emmc_where[1024];
134 int check_can_ops(struct dentry *cur_dir, struct path* path);
135 int check_have_space(struct dentry* dest, struct path* path);
136 int statfs_data_partition(struct path *dst_path, struct kstatfs* stat);
138 void final_putname(struct filename *name)
140 if (name->separate) {
141 __putname(name->name);
148 #define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename))
150 static struct filename *
151 getname_flags(const char __user *filename, int flags, int *empty)
153 struct filename *result, *err;
158 result = audit_reusename(filename);
162 result = __getname();
163 if (unlikely(!result))
164 return ERR_PTR(-ENOMEM);
167 * First, try to embed the struct filename inside the names_cache
170 kname = (char *)result + sizeof(*result);
171 result->name = kname;
172 result->separate = false;
173 max = EMBEDDED_NAME_MAX;
176 len = strncpy_from_user(kname, filename, max);
177 if (unlikely(len < 0)) {
183 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
184 * separate struct filename so we can dedicate the entire
185 * names_cache allocation for the pathname, and re-do the copy from
188 if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
189 kname = (char *)result;
191 result = kzalloc(sizeof(*result), GFP_KERNEL);
193 err = ERR_PTR(-ENOMEM);
194 result = (struct filename *)kname;
197 result->name = kname;
198 result->separate = true;
203 /* The empty path is special. */
204 if (unlikely(!len)) {
207 err = ERR_PTR(-ENOENT);
208 if (!(flags & LOOKUP_EMPTY))
212 err = ERR_PTR(-ENAMETOOLONG);
213 if (unlikely(len >= PATH_MAX))
216 result->uptr = filename;
217 audit_getname(result);
221 final_putname(result);
226 getname(const char __user * filename)
228 return getname_flags(filename, 0, NULL);
230 EXPORT_SYMBOL(getname);
232 #ifdef CONFIG_AUDITSYSCALL
233 void putname(struct filename *name)
235 if (unlikely(!audit_dummy_context()))
236 return audit_putname(name);
241 static int check_acl(struct inode *inode, int mask)
243 #ifdef CONFIG_FS_POSIX_ACL
244 struct posix_acl *acl;
246 if (mask & MAY_NOT_BLOCK) {
247 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
250 /* no ->get_acl() calls in RCU mode... */
251 if (acl == ACL_NOT_CACHED)
253 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
256 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
259 * A filesystem can force a ACL callback by just never filling the
260 * ACL cache. But normally you'd fill the cache either at inode
261 * instantiation time, or on the first ->get_acl call.
263 * If the filesystem doesn't have a get_acl() function at all, we'll
264 * just create the negative cache entry.
266 if (acl == ACL_NOT_CACHED) {
267 if (inode->i_op->get_acl) {
268 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
272 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
278 int error = posix_acl_permission(inode, acl, mask);
279 posix_acl_release(acl);
288 * This does the basic permission checking
290 static int acl_permission_check(struct inode *inode, int mask)
292 unsigned int mode = inode->i_mode;
294 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
297 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
298 int error = check_acl(inode, mask);
299 if (error != -EAGAIN)
303 if (in_group_p(inode->i_gid))
308 * If the DACs are ok we don't need any capability check.
310 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
316 * generic_permission - check for access rights on a Posix-like filesystem
317 * @inode: inode to check access rights for
318 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
320 * Used to check for read/write/execute permissions on a file.
321 * We use "fsuid" for this, letting us set arbitrary permissions
322 * for filesystem access without changing the "normal" uids which
323 * are used for other things.
325 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
326 * request cannot be satisfied (eg. requires blocking or too much complexity).
327 * It would then be called again in ref-walk mode.
329 int generic_permission(struct inode *inode, int mask)
334 * Do the basic permission checks.
336 ret = acl_permission_check(inode, mask);
340 if (S_ISDIR(inode->i_mode)) {
341 /* DACs are overridable for directories */
342 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
344 if (!(mask & MAY_WRITE))
345 if (capable_wrt_inode_uidgid(inode,
346 CAP_DAC_READ_SEARCH))
351 * Read/write DACs are always overridable.
352 * Executable DACs are overridable when there is
353 * at least one exec bit set.
355 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
356 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
360 * Searching includes executable on directories, else just read.
362 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
363 if (mask == MAY_READ)
364 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
371 * We _really_ want to just do "generic_permission()" without
372 * even looking at the inode->i_op values. So we keep a cache
373 * flag in inode->i_opflags, that says "this has not special
374 * permission function, use the fast case".
376 static inline int do_inode_permission(struct inode *inode, int mask)
378 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
379 if (likely(inode->i_op->permission))
380 return inode->i_op->permission(inode, mask);
382 /* This gets set once for the inode lifetime */
383 spin_lock(&inode->i_lock);
384 inode->i_opflags |= IOP_FASTPERM;
385 spin_unlock(&inode->i_lock);
387 return generic_permission(inode, mask);
391 * __inode_permission - Check for access rights to a given inode
392 * @inode: Inode to check permission on
393 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
395 * Check for read/write/execute permissions on an inode.
397 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
399 * This does not check for a read-only file system. You probably want
400 * inode_permission().
402 int __inode_permission(struct inode *inode, int mask)
406 if (unlikely(mask & MAY_WRITE)) {
408 * Nobody gets write access to an immutable file.
410 if (IS_IMMUTABLE(inode))
414 retval = do_inode_permission(inode, mask);
418 retval = devcgroup_inode_permission(inode, mask);
422 return security_inode_permission(inode, mask);
426 * sb_permission - Check superblock-level permissions
427 * @sb: Superblock of inode to check permission on
428 * @inode: Inode to check permission on
429 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
431 * Separate out file-system wide checks from inode-specific permission checks.
433 static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
435 if (unlikely(mask & MAY_WRITE)) {
436 umode_t mode = inode->i_mode;
438 /* Nobody gets write access to a read-only fs. */
439 if ((sb->s_flags & MS_RDONLY) &&
440 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
447 * inode_permission - Check for access rights to a given inode
448 * @inode: Inode to check permission on
449 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
451 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
452 * this, letting us set arbitrary permissions for filesystem access without
453 * changing the "normal" UIDs which are used for other things.
455 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
457 int inode_permission(struct inode *inode, int mask)
461 retval = sb_permission(inode->i_sb, inode, mask);
464 return __inode_permission(inode, mask);
468 * path_get - get a reference to a path
469 * @path: path to get the reference to
471 * Given a path increment the reference count to the dentry and the vfsmount.
473 void path_get(const struct path *path)
478 EXPORT_SYMBOL(path_get);
481 * path_put - put a reference to a path
482 * @path: path to put the reference to
484 * Given a path decrement the reference count to the dentry and the vfsmount.
486 void path_put(const struct path *path)
491 EXPORT_SYMBOL(path_put);
494 * Path walking has 2 modes, rcu-walk and ref-walk (see
495 * Documentation/filesystems/path-lookup.txt). In situations when we can't
496 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
497 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
498 * mode. Refcounts are grabbed at the last known good point before rcu-walk
499 * got stuck, so ref-walk may continue from there. If this is not successful
500 * (eg. a seqcount has changed), then failure is returned and it's up to caller
501 * to restart the path walk from the beginning in ref-walk mode.
504 static inline void lock_rcu_walk(void)
506 br_read_lock(&vfsmount_lock);
510 static inline void unlock_rcu_walk(void)
513 br_read_unlock(&vfsmount_lock);
517 * unlazy_walk - try to switch to ref-walk mode.
518 * @nd: nameidata pathwalk data
519 * @dentry: child of nd->path.dentry or NULL
520 * Returns: 0 on success, -ECHILD on failure
522 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
523 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
524 * @nd or NULL. Must be called from rcu-walk context.
526 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
528 struct fs_struct *fs = current->fs;
529 struct dentry *parent = nd->path.dentry;
532 BUG_ON(!(nd->flags & LOOKUP_RCU));
533 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
535 spin_lock(&fs->lock);
536 if (nd->root.mnt != fs->root.mnt ||
537 nd->root.dentry != fs->root.dentry)
540 spin_lock(&parent->d_lock);
542 if (!__d_rcu_to_refcount(parent, nd->seq))
544 BUG_ON(nd->inode != parent->d_inode);
546 if (dentry->d_parent != parent)
548 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
549 if (!__d_rcu_to_refcount(dentry, nd->seq))
552 * If the sequence check on the child dentry passed, then
553 * the child has not been removed from its parent. This
554 * means the parent dentry must be valid and able to take
555 * a reference at this point.
557 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
558 BUG_ON(!parent->d_count);
560 spin_unlock(&dentry->d_lock);
562 spin_unlock(&parent->d_lock);
565 spin_unlock(&fs->lock);
567 mntget(nd->path.mnt);
570 nd->flags &= ~LOOKUP_RCU;
574 spin_unlock(&dentry->d_lock);
576 spin_unlock(&parent->d_lock);
579 spin_unlock(&fs->lock);
583 static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
585 return dentry->d_op->d_revalidate(dentry, flags);
589 * complete_walk - successful completion of path walk
590 * @nd: pointer nameidata
592 * If we had been in RCU mode, drop out of it and legitimize nd->path.
593 * Revalidate the final result, unless we'd already done that during
594 * the path walk or the filesystem doesn't ask for it. Return 0 on
595 * success, -error on failure. In case of failure caller does not
596 * need to drop nd->path.
598 static int complete_walk(struct nameidata *nd)
600 struct dentry *dentry = nd->path.dentry;
603 if (nd->flags & LOOKUP_RCU) {
604 nd->flags &= ~LOOKUP_RCU;
605 if (!(nd->flags & LOOKUP_ROOT))
607 spin_lock(&dentry->d_lock);
608 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
609 spin_unlock(&dentry->d_lock);
613 BUG_ON(nd->inode != dentry->d_inode);
614 spin_unlock(&dentry->d_lock);
615 mntget(nd->path.mnt);
619 if (likely(!(nd->flags & LOOKUP_JUMPED)))
622 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
625 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
636 static __always_inline void set_root(struct nameidata *nd)
639 get_fs_root(current->fs, &nd->root);
642 static int link_path_walk(const char *, struct nameidata *);
644 static __always_inline void set_root_rcu(struct nameidata *nd)
647 struct fs_struct *fs = current->fs;
651 seq = read_seqcount_begin(&fs->seq);
653 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
654 } while (read_seqcount_retry(&fs->seq, seq));
658 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
670 nd->flags |= LOOKUP_JUMPED;
672 nd->inode = nd->path.dentry->d_inode;
674 ret = link_path_walk(link, nd);
678 return PTR_ERR(link);
681 static void path_put_conditional(struct path *path, struct nameidata *nd)
684 if (path->mnt != nd->path.mnt)
688 static inline void path_to_nameidata(const struct path *path,
689 struct nameidata *nd)
691 if (!(nd->flags & LOOKUP_RCU)) {
692 dput(nd->path.dentry);
693 if (nd->path.mnt != path->mnt)
694 mntput(nd->path.mnt);
696 nd->path.mnt = path->mnt;
697 nd->path.dentry = path->dentry;
701 * Helper to directly jump to a known parsed path from ->follow_link,
702 * caller must have taken a reference to path beforehand.
704 void nd_jump_link(struct nameidata *nd, struct path *path)
709 nd->inode = nd->path.dentry->d_inode;
710 nd->flags |= LOOKUP_JUMPED;
713 static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
715 struct inode *inode = link->dentry->d_inode;
716 if (inode->i_op->put_link)
717 inode->i_op->put_link(link->dentry, nd, cookie);
721 int sysctl_protected_symlinks __read_mostly = 0;
722 int sysctl_protected_hardlinks __read_mostly = 0;
725 * may_follow_link - Check symlink following for unsafe situations
726 * @link: The path of the symlink
727 * @nd: nameidata pathwalk data
729 * In the case of the sysctl_protected_symlinks sysctl being enabled,
730 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
731 * in a sticky world-writable directory. This is to protect privileged
732 * processes from failing races against path names that may change out
733 * from under them by way of other users creating malicious symlinks.
734 * It will permit symlinks to be followed only when outside a sticky
735 * world-writable directory, or when the uid of the symlink and follower
736 * match, or when the directory owner matches the symlink's owner.
738 * Returns 0 if following the symlink is allowed, -ve on error.
740 static inline int may_follow_link(struct path *link, struct nameidata *nd)
742 const struct inode *inode;
743 const struct inode *parent;
745 if (!sysctl_protected_symlinks)
748 /* Allowed if owner and follower match. */
749 inode = link->dentry->d_inode;
750 if (uid_eq(current_cred()->fsuid, inode->i_uid))
753 /* Allowed if parent directory not sticky and world-writable. */
754 parent = nd->path.dentry->d_inode;
755 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
758 /* Allowed if parent directory and link owner match. */
759 if (uid_eq(parent->i_uid, inode->i_uid))
762 audit_log_link_denied("follow_link", link);
763 path_put_conditional(link, nd);
769 * safe_hardlink_source - Check for safe hardlink conditions
770 * @inode: the source inode to hardlink from
772 * Return false if at least one of the following conditions:
773 * - inode is not a regular file
775 * - inode is setgid and group-exec
776 * - access failure for read and write
778 * Otherwise returns true.
780 static bool safe_hardlink_source(struct inode *inode)
782 umode_t mode = inode->i_mode;
784 /* Special files should not get pinned to the filesystem. */
788 /* Setuid files should not get pinned to the filesystem. */
792 /* Executable setgid files should not get pinned to the filesystem. */
793 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
796 /* Hardlinking to unreadable or unwritable sources is dangerous. */
797 if (inode_permission(inode, MAY_READ | MAY_WRITE))
804 * may_linkat - Check permissions for creating a hardlink
805 * @link: the source to hardlink from
807 * Block hardlink when all of:
808 * - sysctl_protected_hardlinks enabled
809 * - fsuid does not match inode
810 * - hardlink source is unsafe (see safe_hardlink_source() above)
813 * Returns 0 if successful, -ve on error.
815 static int may_linkat(struct path *link)
817 const struct cred *cred;
820 if (!sysctl_protected_hardlinks)
823 cred = current_cred();
824 inode = link->dentry->d_inode;
826 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
827 * otherwise, it must be a safe source.
829 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
833 audit_log_link_denied("linkat", link);
837 static __always_inline int
838 follow_link(struct path *link, struct nameidata *nd, void **p)
840 struct dentry *dentry = link->dentry;
844 BUG_ON(nd->flags & LOOKUP_RCU);
846 if (link->mnt == nd->path.mnt)
850 if (unlikely(current->total_link_count >= 40))
851 goto out_put_nd_path;
854 current->total_link_count++;
857 nd_set_link(nd, NULL);
859 error = security_inode_follow_link(link->dentry, nd);
861 goto out_put_nd_path;
863 nd->last_type = LAST_BIND;
864 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
867 goto out_put_nd_path;
872 error = __vfs_follow_link(nd, s);
874 put_link(nd, link, *p);
886 static int follow_up_rcu(struct path *path)
888 struct mount *mnt = real_mount(path->mnt);
889 struct mount *parent;
890 struct dentry *mountpoint;
892 parent = mnt->mnt_parent;
893 if (&parent->mnt == path->mnt)
895 mountpoint = mnt->mnt_mountpoint;
896 path->dentry = mountpoint;
897 path->mnt = &parent->mnt;
902 * follow_up - Find the mountpoint of path's vfsmount
904 * Given a path, find the mountpoint of its source file system.
905 * Replace @path with the path of the mountpoint in the parent mount.
908 * Return 1 if we went up a level and 0 if we were already at the
911 int follow_up(struct path *path)
913 struct mount *mnt = real_mount(path->mnt);
914 struct mount *parent;
915 struct dentry *mountpoint;
917 br_read_lock(&vfsmount_lock);
918 parent = mnt->mnt_parent;
920 br_read_unlock(&vfsmount_lock);
923 mntget(&parent->mnt);
924 mountpoint = dget(mnt->mnt_mountpoint);
925 br_read_unlock(&vfsmount_lock);
927 path->dentry = mountpoint;
929 path->mnt = &parent->mnt;
934 * Perform an automount
935 * - return -EISDIR to tell follow_managed() to stop and return the path we
938 static int follow_automount(struct path *path, unsigned flags,
941 struct vfsmount *mnt;
944 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
947 /* We don't want to mount if someone's just doing a stat -
948 * unless they're stat'ing a directory and appended a '/' to
951 * We do, however, want to mount if someone wants to open or
952 * create a file of any type under the mountpoint, wants to
953 * traverse through the mountpoint or wants to open the
954 * mounted directory. Also, autofs may mark negative dentries
955 * as being automount points. These will need the attentions
956 * of the daemon to instantiate them before they can be used.
958 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
959 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
960 path->dentry->d_inode)
963 current->total_link_count++;
964 if (current->total_link_count >= 40)
967 mnt = path->dentry->d_op->d_automount(path);
970 * The filesystem is allowed to return -EISDIR here to indicate
971 * it doesn't want to automount. For instance, autofs would do
972 * this so that its userspace daemon can mount on this dentry.
974 * However, we can only permit this if it's a terminal point in
975 * the path being looked up; if it wasn't then the remainder of
976 * the path is inaccessible and we should say so.
978 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
983 if (!mnt) /* mount collision */
987 /* lock_mount() may release path->mnt on error */
991 err = finish_automount(mnt, path);
995 /* Someone else made a mount here whilst we were busy */
1000 path->dentry = dget(mnt->mnt_root);
1009 * Handle a dentry that is managed in some way.
1010 * - Flagged for transit management (autofs)
1011 * - Flagged as mountpoint
1012 * - Flagged as automount point
1014 * This may only be called in refwalk mode.
1016 * Serialization is taken care of in namespace.c
1018 static int follow_managed(struct path *path, unsigned flags)
1020 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
1022 bool need_mntput = false;
1025 /* Given that we're not holding a lock here, we retain the value in a
1026 * local variable for each dentry as we look at it so that we don't see
1027 * the components of that value change under us */
1028 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1029 managed &= DCACHE_MANAGED_DENTRY,
1030 unlikely(managed != 0)) {
1031 /* Allow the filesystem to manage the transit without i_mutex
1033 if (managed & DCACHE_MANAGE_TRANSIT) {
1034 BUG_ON(!path->dentry->d_op);
1035 BUG_ON(!path->dentry->d_op->d_manage);
1036 ret = path->dentry->d_op->d_manage(path->dentry, false);
1041 /* Transit to a mounted filesystem. */
1042 if (managed & DCACHE_MOUNTED) {
1043 struct vfsmount *mounted = lookup_mnt(path);
1048 path->mnt = mounted;
1049 path->dentry = dget(mounted->mnt_root);
1054 /* Something is mounted on this dentry in another
1055 * namespace and/or whatever was mounted there in this
1056 * namespace got unmounted before we managed to get the
1060 /* Handle an automount point */
1061 if (managed & DCACHE_NEED_AUTOMOUNT) {
1062 ret = follow_automount(path, flags, &need_mntput);
1068 /* We didn't change the current path point */
1072 if (need_mntput && path->mnt == mnt)
1076 return ret < 0 ? ret : need_mntput;
1079 int follow_down_one(struct path *path)
1081 struct vfsmount *mounted;
1083 mounted = lookup_mnt(path);
1087 path->mnt = mounted;
1088 path->dentry = dget(mounted->mnt_root);
1094 static inline bool managed_dentry_might_block(struct dentry *dentry)
1096 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1097 dentry->d_op->d_manage(dentry, true) < 0);
1101 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1102 * we meet a managed dentry that would need blocking.
1104 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1105 struct inode **inode)
1108 struct mount *mounted;
1110 * Don't forget we might have a non-mountpoint managed dentry
1111 * that wants to block transit.
1113 if (unlikely(managed_dentry_might_block(path->dentry)))
1116 if (!d_mountpoint(path->dentry))
1119 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1122 path->mnt = &mounted->mnt;
1123 path->dentry = mounted->mnt.mnt_root;
1124 nd->flags |= LOOKUP_JUMPED;
1125 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
1127 * Update the inode too. We don't need to re-check the
1128 * dentry sequence number here after this d_inode read,
1129 * because a mount-point is always pinned.
1131 *inode = path->dentry->d_inode;
1136 static void follow_mount_rcu(struct nameidata *nd)
1138 while (d_mountpoint(nd->path.dentry)) {
1139 struct mount *mounted;
1140 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
1143 nd->path.mnt = &mounted->mnt;
1144 nd->path.dentry = mounted->mnt.mnt_root;
1145 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1149 static int follow_dotdot_rcu(struct nameidata *nd)
1154 if (nd->path.dentry == nd->root.dentry &&
1155 nd->path.mnt == nd->root.mnt) {
1158 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1159 struct dentry *old = nd->path.dentry;
1160 struct dentry *parent = old->d_parent;
1163 seq = read_seqcount_begin(&parent->d_seq);
1164 if (read_seqcount_retry(&old->d_seq, nd->seq))
1166 nd->path.dentry = parent;
1170 if (!follow_up_rcu(&nd->path))
1172 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1174 follow_mount_rcu(nd);
1175 nd->inode = nd->path.dentry->d_inode;
1179 nd->flags &= ~LOOKUP_RCU;
1180 if (!(nd->flags & LOOKUP_ROOT))
1181 nd->root.mnt = NULL;
1187 * Follow down to the covering mount currently visible to userspace. At each
1188 * point, the filesystem owning that dentry may be queried as to whether the
1189 * caller is permitted to proceed or not.
1191 int follow_down(struct path *path)
1196 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1197 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1198 /* Allow the filesystem to manage the transit without i_mutex
1201 * We indicate to the filesystem if someone is trying to mount
1202 * something here. This gives autofs the chance to deny anyone
1203 * other than its daemon the right to mount on its
1206 * The filesystem may sleep at this point.
1208 if (managed & DCACHE_MANAGE_TRANSIT) {
1209 BUG_ON(!path->dentry->d_op);
1210 BUG_ON(!path->dentry->d_op->d_manage);
1211 ret = path->dentry->d_op->d_manage(
1212 path->dentry, false);
1214 return ret == -EISDIR ? 0 : ret;
1217 /* Transit to a mounted filesystem. */
1218 if (managed & DCACHE_MOUNTED) {
1219 struct vfsmount *mounted = lookup_mnt(path);
1224 path->mnt = mounted;
1225 path->dentry = dget(mounted->mnt_root);
1229 /* Don't handle automount points here */
1236 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1238 static void follow_mount(struct path *path)
1240 while (d_mountpoint(path->dentry)) {
1241 struct vfsmount *mounted = lookup_mnt(path);
1246 path->mnt = mounted;
1247 path->dentry = dget(mounted->mnt_root);
1251 static void follow_dotdot(struct nameidata *nd)
1256 struct dentry *old = nd->path.dentry;
1258 if (nd->path.dentry == nd->root.dentry &&
1259 nd->path.mnt == nd->root.mnt) {
1262 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1263 /* rare case of legitimate dget_parent()... */
1264 nd->path.dentry = dget_parent(nd->path.dentry);
1268 if (!follow_up(&nd->path))
1271 follow_mount(&nd->path);
1272 nd->inode = nd->path.dentry->d_inode;
1276 * This looks up the name in dcache, possibly revalidates the old dentry and
1277 * allocates a new one if not found or not valid. In the need_lookup argument
1278 * returns whether i_op->lookup is necessary.
1280 * dir->d_inode->i_mutex must be held
1282 static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1283 unsigned int flags, bool *need_lookup)
1285 struct dentry *dentry;
1288 *need_lookup = false;
1289 dentry = d_lookup(dir, name);
1291 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1292 error = d_revalidate(dentry, flags);
1293 if (unlikely(error <= 0)) {
1296 return ERR_PTR(error);
1297 } else if (!d_invalidate(dentry)) {
1306 dentry = d_alloc(dir, name);
1307 if (unlikely(!dentry))
1308 return ERR_PTR(-ENOMEM);
1310 *need_lookup = true;
1316 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1317 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1319 * dir->d_inode->i_mutex must be held
1321 static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1326 /* Don't create child dentry for a dead directory. */
1327 if (unlikely(IS_DEADDIR(dir))) {
1329 return ERR_PTR(-ENOENT);
1332 old = dir->i_op->lookup(dir, dentry, flags);
1333 if (unlikely(old)) {
1340 static struct dentry *__lookup_hash(struct qstr *name,
1341 struct dentry *base, unsigned int flags)
1344 struct dentry *dentry;
1346 dentry = lookup_dcache(name, base, flags, &need_lookup);
1350 return lookup_real(base->d_inode, dentry, flags);
1354 * It's more convoluted than I'd like it to be, but... it's still fairly
1355 * small and for now I'd prefer to have fast path as straight as possible.
1356 * It _is_ time-critical.
1358 static int lookup_fast(struct nameidata *nd,
1359 struct path *path, struct inode **inode)
1361 struct vfsmount *mnt = nd->path.mnt;
1362 struct dentry *dentry, *parent = nd->path.dentry;
1368 * Rename seqlock is not required here because in the off chance
1369 * of a false negative due to a concurrent rename, we're going to
1370 * do the non-racy lookup, below.
1372 if (nd->flags & LOOKUP_RCU) {
1374 dentry = __d_lookup_rcu(parent, &nd->last, &seq, nd->inode);
1379 * This sequence count validates that the inode matches
1380 * the dentry name information from lookup.
1382 *inode = dentry->d_inode;
1383 if (read_seqcount_retry(&dentry->d_seq, seq))
1387 * This sequence count validates that the parent had no
1388 * changes while we did the lookup of the dentry above.
1390 * The memory barrier in read_seqcount_begin of child is
1391 * enough, we can use __read_seqcount_retry here.
1393 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1397 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1398 status = d_revalidate(dentry, nd->flags);
1399 if (unlikely(status <= 0)) {
1400 if (status != -ECHILD)
1406 path->dentry = dentry;
1407 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1409 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1413 if (unlazy_walk(nd, dentry))
1416 dentry = __d_lookup(parent, &nd->last);
1419 if (unlikely(!dentry))
1422 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1423 status = d_revalidate(dentry, nd->flags);
1424 if (unlikely(status <= 0)) {
1429 if (!d_invalidate(dentry)) {
1436 path->dentry = dentry;
1437 err = follow_managed(path, nd->flags);
1438 if (unlikely(err < 0)) {
1439 path_put_conditional(path, nd);
1443 nd->flags |= LOOKUP_JUMPED;
1444 *inode = path->dentry->d_inode;
1451 /* Fast lookup failed, do it the slow way */
1452 static int lookup_slow(struct nameidata *nd, struct path *path)
1454 struct dentry *dentry, *parent;
1457 parent = nd->path.dentry;
1458 BUG_ON(nd->inode != parent->d_inode);
1460 mutex_lock(&parent->d_inode->i_mutex);
1461 dentry = __lookup_hash(&nd->last, parent, nd->flags);
1462 mutex_unlock(&parent->d_inode->i_mutex);
1464 return PTR_ERR(dentry);
1465 path->mnt = nd->path.mnt;
1466 path->dentry = dentry;
1467 err = follow_managed(path, nd->flags);
1468 if (unlikely(err < 0)) {
1469 path_put_conditional(path, nd);
1473 nd->flags |= LOOKUP_JUMPED;
1477 static inline int may_lookup(struct nameidata *nd)
1479 if (nd->flags & LOOKUP_RCU) {
1480 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1483 if (unlazy_walk(nd, NULL))
1486 return inode_permission(nd->inode, MAY_EXEC);
1489 static inline int handle_dots(struct nameidata *nd, int type)
1491 if (type == LAST_DOTDOT) {
1492 if (nd->flags & LOOKUP_RCU) {
1493 if (follow_dotdot_rcu(nd))
1501 static void terminate_walk(struct nameidata *nd)
1503 if (!(nd->flags & LOOKUP_RCU)) {
1504 path_put(&nd->path);
1506 nd->flags &= ~LOOKUP_RCU;
1507 if (!(nd->flags & LOOKUP_ROOT))
1508 nd->root.mnt = NULL;
1514 * Do we need to follow links? We _really_ want to be able
1515 * to do this check without having to look at inode->i_op,
1516 * so we keep a cache of "no, this doesn't need follow_link"
1517 * for the common case.
1519 static inline int should_follow_link(struct inode *inode, int follow)
1521 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1522 if (likely(inode->i_op->follow_link))
1525 /* This gets set once for the inode lifetime */
1526 spin_lock(&inode->i_lock);
1527 inode->i_opflags |= IOP_NOFOLLOW;
1528 spin_unlock(&inode->i_lock);
1533 static inline int walk_component(struct nameidata *nd, struct path *path,
1536 struct inode *inode;
1539 * "." and ".." are special - ".." especially so because it has
1540 * to be able to know about the current root directory and
1541 * parent relationships.
1543 if (unlikely(nd->last_type != LAST_NORM))
1544 return handle_dots(nd, nd->last_type);
1545 err = lookup_fast(nd, path, &inode);
1546 if (unlikely(err)) {
1550 err = lookup_slow(nd, path);
1554 inode = path->dentry->d_inode;
1560 if (should_follow_link(inode, follow)) {
1561 if (nd->flags & LOOKUP_RCU) {
1562 if (unlikely(unlazy_walk(nd, path->dentry))) {
1567 BUG_ON(inode != path->dentry->d_inode);
1570 path_to_nameidata(path, nd);
1575 path_to_nameidata(path, nd);
1582 * This limits recursive symlink follows to 8, while
1583 * limiting consecutive symlinks to 40.
1585 * Without that kind of total limit, nasty chains of consecutive
1586 * symlinks can cause almost arbitrarily long lookups.
1588 static inline int nested_symlink(struct path *path, struct nameidata *nd)
1592 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1593 path_put_conditional(path, nd);
1594 path_put(&nd->path);
1597 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1600 current->link_count++;
1603 struct path link = *path;
1606 res = follow_link(&link, nd, &cookie);
1609 res = walk_component(nd, path, LOOKUP_FOLLOW);
1610 put_link(nd, &link, cookie);
1613 current->link_count--;
1619 * We really don't want to look at inode->i_op->lookup
1620 * when we don't have to. So we keep a cache bit in
1621 * the inode ->i_opflags field that says "yes, we can
1622 * do lookup on this inode".
1624 static inline int can_lookup(struct inode *inode)
1626 if (likely(inode->i_opflags & IOP_LOOKUP))
1628 if (likely(!inode->i_op->lookup))
1631 /* We do this once for the lifetime of the inode */
1632 spin_lock(&inode->i_lock);
1633 inode->i_opflags |= IOP_LOOKUP;
1634 spin_unlock(&inode->i_lock);
1639 * We can do the critical dentry name comparison and hashing
1640 * operations one word at a time, but we are limited to:
1642 * - Architectures with fast unaligned word accesses. We could
1643 * do a "get_unaligned()" if this helps and is sufficiently
1646 * - Little-endian machines (so that we can generate the mask
1647 * of low bytes efficiently). Again, we *could* do a byte
1648 * swapping load on big-endian architectures if that is not
1649 * expensive enough to make the optimization worthless.
1651 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1652 * do not trap on the (extremely unlikely) case of a page
1653 * crossing operation.
1655 * - Furthermore, we need an efficient 64-bit compile for the
1656 * 64-bit case in order to generate the "number of bytes in
1657 * the final mask". Again, that could be replaced with a
1658 * efficient population count instruction or similar.
1660 #ifdef CONFIG_DCACHE_WORD_ACCESS
1662 #include <asm/word-at-a-time.h>
1666 static inline unsigned int fold_hash(unsigned long hash)
1668 return hash_64(hash, 32);
1671 #else /* 32-bit case */
1673 #define fold_hash(x) (x)
1677 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1679 unsigned long a, mask;
1680 unsigned long hash = 0;
1683 a = load_unaligned_zeropad(name);
1684 if (len < sizeof(unsigned long))
1688 name += sizeof(unsigned long);
1689 len -= sizeof(unsigned long);
1693 mask = ~(~0ul << len*8);
1696 return fold_hash(hash);
1698 EXPORT_SYMBOL(full_name_hash);
1701 * Calculate the length and hash of the path component, and
1702 * return the length of the component;
1704 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1706 unsigned long a, b, adata, bdata, mask, hash, len;
1707 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1710 len = -sizeof(unsigned long);
1712 hash = (hash + a) * 9;
1713 len += sizeof(unsigned long);
1714 a = load_unaligned_zeropad(name+len);
1715 b = a ^ REPEAT_BYTE('/');
1716 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1718 adata = prep_zero_mask(a, adata, &constants);
1719 bdata = prep_zero_mask(b, bdata, &constants);
1721 mask = create_zero_mask(adata | bdata);
1723 hash += a & zero_bytemask(mask);
1724 *hashp = fold_hash(hash);
1726 return len + find_zero(mask);
1731 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1733 unsigned long hash = init_name_hash();
1735 hash = partial_name_hash(*name++, hash);
1736 return end_name_hash(hash);
1738 EXPORT_SYMBOL(full_name_hash);
1741 * We know there's a real path component here of at least
1744 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1746 unsigned long hash = init_name_hash();
1747 unsigned long len = 0, c;
1749 c = (unsigned char)*name;
1752 hash = partial_name_hash(c, hash);
1753 c = (unsigned char)name[len];
1754 } while (c && c != '/');
1755 *hashp = end_name_hash(hash);
1763 * This is the basic name resolution function, turning a pathname into
1764 * the final dentry. We expect 'base' to be positive and a directory.
1766 * Returns 0 and nd will have valid dentry and mnt on success.
1767 * Returns error and drops reference to input namei data on failure.
1769 static int link_path_walk(const char *name, struct nameidata *nd)
1779 /* At this point we know we have a real path component. */
1785 err = may_lookup(nd);
1789 len = hash_name(name, &this.hash);
1794 if (name[0] == '.') switch (len) {
1796 if (name[1] == '.') {
1798 nd->flags |= LOOKUP_JUMPED;
1804 if (likely(type == LAST_NORM)) {
1805 struct dentry *parent = nd->path.dentry;
1806 nd->flags &= ~LOOKUP_JUMPED;
1807 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1808 err = parent->d_op->d_hash(parent, nd->inode,
1816 nd->last_type = type;
1821 * If it wasn't NUL, we know it was '/'. Skip that
1822 * slash, and continue until no more slashes.
1826 } while (unlikely(name[len] == '/'));
1832 err = walk_component(nd, &next, LOOKUP_FOLLOW);
1837 err = nested_symlink(&next, nd);
1841 if (!can_lookup(nd->inode)) {
1850 static int path_init(int dfd, const char *name, unsigned int flags,
1851 struct nameidata *nd, struct file **fp)
1855 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1856 nd->flags = flags | LOOKUP_JUMPED;
1858 if (flags & LOOKUP_ROOT) {
1859 struct inode *inode = nd->root.dentry->d_inode;
1861 if (!can_lookup(inode))
1863 retval = inode_permission(inode, MAY_EXEC);
1867 nd->path = nd->root;
1869 if (flags & LOOKUP_RCU) {
1871 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1873 path_get(&nd->path);
1878 nd->root.mnt = NULL;
1881 if (flags & LOOKUP_RCU) {
1886 path_get(&nd->root);
1888 nd->path = nd->root;
1889 } else if (dfd == AT_FDCWD) {
1890 if (flags & LOOKUP_RCU) {
1891 struct fs_struct *fs = current->fs;
1897 seq = read_seqcount_begin(&fs->seq);
1899 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1900 } while (read_seqcount_retry(&fs->seq, seq));
1902 get_fs_pwd(current->fs, &nd->path);
1905 /* Caller must check execute permissions on the starting path component */
1906 struct fd f = fdget_raw(dfd);
1907 struct dentry *dentry;
1912 dentry = f.file->f_path.dentry;
1915 if (!can_lookup(dentry->d_inode)) {
1921 nd->path = f.file->f_path;
1922 if (flags & LOOKUP_RCU) {
1925 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1928 path_get(&nd->path);
1933 nd->inode = nd->path.dentry->d_inode;
1937 static inline int lookup_last(struct nameidata *nd, struct path *path)
1939 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1940 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1942 nd->flags &= ~LOOKUP_PARENT;
1943 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
1946 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1947 static int path_lookupat(int dfd, const char *name,
1948 unsigned int flags, struct nameidata *nd)
1950 struct file *base = NULL;
1955 * Path walking is largely split up into 2 different synchronisation
1956 * schemes, rcu-walk and ref-walk (explained in
1957 * Documentation/filesystems/path-lookup.txt). These share much of the
1958 * path walk code, but some things particularly setup, cleanup, and
1959 * following mounts are sufficiently divergent that functions are
1960 * duplicated. Typically there is a function foo(), and its RCU
1961 * analogue, foo_rcu().
1963 * -ECHILD is the error number of choice (just to avoid clashes) that
1964 * is returned if some aspect of an rcu-walk fails. Such an error must
1965 * be handled by restarting a traditional ref-walk (which will always
1966 * be able to complete).
1968 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1973 current->total_link_count = 0;
1974 err = link_path_walk(name, nd);
1976 if (!err && !(flags & LOOKUP_PARENT)) {
1977 err = lookup_last(nd, &path);
1980 struct path link = path;
1981 err = may_follow_link(&link, nd);
1984 nd->flags |= LOOKUP_PARENT;
1985 err = follow_link(&link, nd, &cookie);
1988 err = lookup_last(nd, &path);
1989 put_link(nd, &link, cookie);
1994 err = complete_walk(nd);
1996 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1997 if (!can_lookup(nd->inode)) {
1998 path_put(&nd->path);
2006 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
2007 path_put(&nd->root);
2008 nd->root.mnt = NULL;
2013 static int filename_lookup(int dfd, struct filename *name,
2014 unsigned int flags, struct nameidata *nd)
2016 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
2017 if (unlikely(retval == -ECHILD))
2018 retval = path_lookupat(dfd, name->name, flags, nd);
2019 if (unlikely(retval == -ESTALE))
2020 retval = path_lookupat(dfd, name->name,
2021 flags | LOOKUP_REVAL, nd);
2023 if (likely(!retval))
2024 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
2028 static int do_path_lookup(int dfd, const char *name,
2029 unsigned int flags, struct nameidata *nd)
2031 struct filename filename = { .name = name };
2033 return filename_lookup(dfd, &filename, flags, nd);
2036 /* does lookup, returns the object with parent locked */
2037 struct dentry *kern_path_locked(const char *name, struct path *path)
2039 struct nameidata nd;
2041 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2043 return ERR_PTR(err);
2044 if (nd.last_type != LAST_NORM) {
2046 return ERR_PTR(-EINVAL);
2048 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2049 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
2051 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2059 int kern_path(const char *name, unsigned int flags, struct path *path)
2061 struct nameidata nd;
2062 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2069 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2070 * @dentry: pointer to dentry of the base directory
2071 * @mnt: pointer to vfs mount of the base directory
2072 * @name: pointer to file name
2073 * @flags: lookup flags
2074 * @path: pointer to struct path to fill
2076 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2077 const char *name, unsigned int flags,
2080 struct nameidata nd;
2082 nd.root.dentry = dentry;
2084 BUG_ON(flags & LOOKUP_PARENT);
2085 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2086 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2093 * Restricted form of lookup. Doesn't follow links, single-component only,
2094 * needs parent already locked. Doesn't follow mounts.
2097 static struct dentry *lookup_hash(struct nameidata *nd)
2099 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
2103 * lookup_one_len - filesystem helper to lookup single pathname component
2104 * @name: pathname component to lookup
2105 * @base: base directory to lookup from
2106 * @len: maximum length @len should be interpreted to
2108 * Note that this routine is purely a helper for filesystem usage and should
2109 * not be called by generic code. Also note that by using this function the
2110 * nameidata argument is passed to the filesystem methods and a filesystem
2111 * using this helper needs to be prepared for that.
2113 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2119 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2123 this.hash = full_name_hash(name, len);
2125 return ERR_PTR(-EACCES);
2127 if (unlikely(name[0] == '.')) {
2128 if (len < 2 || (len == 2 && name[1] == '.'))
2129 return ERR_PTR(-EACCES);
2133 c = *(const unsigned char *)name++;
2134 if (c == '/' || c == '\0')
2135 return ERR_PTR(-EACCES);
2138 * See if the low-level filesystem might want
2139 * to use its own hash..
2141 if (base->d_flags & DCACHE_OP_HASH) {
2142 int err = base->d_op->d_hash(base, base->d_inode, &this);
2144 return ERR_PTR(err);
2147 err = inode_permission(base->d_inode, MAY_EXEC);
2149 return ERR_PTR(err);
2151 return __lookup_hash(&this, base, 0);
2154 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2155 struct path *path, int *empty)
2157 struct nameidata nd;
2158 struct filename *tmp = getname_flags(name, flags, empty);
2159 int err = PTR_ERR(tmp);
2162 BUG_ON(flags & LOOKUP_PARENT);
2164 err = filename_lookup(dfd, tmp, flags, &nd);
2172 int user_path_at(int dfd, const char __user *name, unsigned flags,
2175 return user_path_at_empty(dfd, name, flags, path, NULL);
2179 * NB: most callers don't do anything directly with the reference to the
2180 * to struct filename, but the nd->last pointer points into the name string
2181 * allocated by getname. So we must hold the reference to it until all
2182 * path-walking is complete.
2184 static struct filename *
2185 user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2188 struct filename *s = getname(path);
2191 /* only LOOKUP_REVAL is allowed in extra flags */
2192 flags &= LOOKUP_REVAL;
2197 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
2200 return ERR_PTR(error);
2207 * It's inline, so penalty for filesystems that don't use sticky bit is
2210 static inline int check_sticky(struct inode *dir, struct inode *inode)
2212 kuid_t fsuid = current_fsuid();
2214 if (!(dir->i_mode & S_ISVTX))
2216 if (uid_eq(inode->i_uid, fsuid))
2218 if (uid_eq(dir->i_uid, fsuid))
2220 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
2224 * Check whether we can remove a link victim from directory dir, check
2225 * whether the type of victim is right.
2226 * 1. We can't do it if dir is read-only (done in permission())
2227 * 2. We should have write and exec permissions on dir
2228 * 3. We can't remove anything from append-only dir
2229 * 4. We can't do anything with immutable dir (done in permission())
2230 * 5. If the sticky bit on dir is set we should either
2231 * a. be owner of dir, or
2232 * b. be owner of victim, or
2233 * c. have CAP_FOWNER capability
2234 * 6. If the victim is append-only or immutable we can't do antyhing with
2235 * links pointing to it.
2236 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2237 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2238 * 9. We can't remove a root or mountpoint.
2239 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2240 * nfs_async_unlink().
2242 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
2246 if (!victim->d_inode)
2249 BUG_ON(victim->d_parent->d_inode != dir);
2250 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
2252 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
2257 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2258 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
2261 if (!S_ISDIR(victim->d_inode->i_mode))
2263 if (IS_ROOT(victim))
2265 } else if (S_ISDIR(victim->d_inode->i_mode))
2267 if (IS_DEADDIR(dir))
2269 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2274 /* Check whether we can create an object with dentry child in directory
2276 * 1. We can't do it if child already exists (open has special treatment for
2277 * this case, but since we are inlined it's OK)
2278 * 2. We can't do it if dir is read-only (done in permission())
2279 * 3. We should have write and exec permissions on dir
2280 * 4. We can't do it if dir is immutable (done in permission())
2282 static inline int may_create(struct inode *dir, struct dentry *child)
2284 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
2287 if (IS_DEADDIR(dir))
2289 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2293 * p1 and p2 should be directories on the same fs.
2295 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2300 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2304 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2306 p = d_ancestor(p2, p1);
2308 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2309 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2313 p = d_ancestor(p1, p2);
2315 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2316 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2320 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2321 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2325 void unlock_rename(struct dentry *p1, struct dentry *p2)
2327 mutex_unlock(&p1->d_inode->i_mutex);
2329 mutex_unlock(&p2->d_inode->i_mutex);
2330 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2334 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2337 int error = may_create(dir, dentry);
2341 if (!dir->i_op->create)
2342 return -EACCES; /* shouldn't it be ENOSYS? */
2345 error = security_inode_create(dir, dentry, mode);
2348 error = dir->i_op->create(dir, dentry, mode, want_excl);
2350 fsnotify_create(dir, dentry);
2354 static int may_open(struct path *path, int acc_mode, int flag)
2356 struct dentry *dentry = path->dentry;
2357 struct inode *inode = dentry->d_inode;
2367 switch (inode->i_mode & S_IFMT) {
2371 if (acc_mode & MAY_WRITE)
2376 if (path->mnt->mnt_flags & MNT_NODEV)
2385 error = inode_permission(inode, acc_mode);
2390 * An append-only file must be opened in append mode for writing.
2392 if (IS_APPEND(inode)) {
2393 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2399 /* O_NOATIME can only be set by the owner or superuser */
2400 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2406 static int handle_truncate(struct file *filp)
2408 struct path *path = &filp->f_path;
2409 struct inode *inode = path->dentry->d_inode;
2410 int error = get_write_access(inode);
2414 * Refuse to truncate files with mandatory locks held on them.
2416 error = locks_verify_locked(inode);
2418 error = security_path_truncate(path);
2420 error = do_truncate(path->dentry, 0,
2421 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2424 put_write_access(inode);
2428 static inline int open_to_namei_flags(int flag)
2430 if ((flag & O_ACCMODE) == 3)
2435 static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2437 int error = security_path_mknod(dir, dentry, mode, 0);
2441 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2445 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2449 * Attempt to atomically look up, create and open a file from a negative
2452 * Returns 0 if successful. The file will have been created and attached to
2453 * @file by the filesystem calling finish_open().
2455 * Returns 1 if the file was looked up only or didn't need creating. The
2456 * caller will need to perform the open themselves. @path will have been
2457 * updated to point to the new dentry. This may be negative.
2459 * Returns an error code otherwise.
2461 static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2462 struct path *path, struct file *file,
2463 const struct open_flags *op,
2464 bool got_write, bool need_lookup,
2467 struct inode *dir = nd->path.dentry->d_inode;
2468 unsigned open_flag = open_to_namei_flags(op->open_flag);
2472 int create_error = 0;
2473 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2475 BUG_ON(dentry->d_inode);
2477 /* Don't create child dentry for a dead directory. */
2478 if (unlikely(IS_DEADDIR(dir))) {
2484 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2485 mode &= ~current_umask();
2487 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
2488 open_flag &= ~O_TRUNC;
2489 *opened |= FILE_CREATED;
2493 * Checking write permission is tricky, bacuse we don't know if we are
2494 * going to actually need it: O_CREAT opens should work as long as the
2495 * file exists. But checking existence breaks atomicity. The trick is
2496 * to check access and if not granted clear O_CREAT from the flags.
2498 * Another problem is returing the "right" error value (e.g. for an
2499 * O_EXCL open we want to return EEXIST not EROFS).
2501 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2502 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2503 if (!(open_flag & O_CREAT)) {
2505 * No O_CREATE -> atomicity not a requirement -> fall
2506 * back to lookup + open
2509 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2510 /* Fall back and fail with the right error */
2511 create_error = -EROFS;
2514 /* No side effects, safe to clear O_CREAT */
2515 create_error = -EROFS;
2516 open_flag &= ~O_CREAT;
2520 if (open_flag & O_CREAT) {
2521 error = may_o_create(&nd->path, dentry, mode);
2523 create_error = error;
2524 if (open_flag & O_EXCL)
2526 open_flag &= ~O_CREAT;
2530 if (nd->flags & LOOKUP_DIRECTORY)
2531 open_flag |= O_DIRECTORY;
2533 file->f_path.dentry = DENTRY_NOT_SET;
2534 file->f_path.mnt = nd->path.mnt;
2535 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
2538 if (create_error && error == -ENOENT)
2539 error = create_error;
2543 acc_mode = op->acc_mode;
2544 if (*opened & FILE_CREATED) {
2545 fsnotify_create(dir, dentry);
2546 acc_mode = MAY_OPEN;
2549 if (error) { /* returned 1, that is */
2550 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2554 if (file->f_path.dentry) {
2556 dentry = file->f_path.dentry;
2558 if (create_error && dentry->d_inode == NULL) {
2559 error = create_error;
2566 * We didn't have the inode before the open, so check open permission
2569 error = may_open(&file->f_path, acc_mode, open_flag);
2579 dentry = lookup_real(dir, dentry, nd->flags);
2581 return PTR_ERR(dentry);
2584 int open_flag = op->open_flag;
2586 error = create_error;
2587 if ((open_flag & O_EXCL)) {
2588 if (!dentry->d_inode)
2590 } else if (!dentry->d_inode) {
2592 } else if ((open_flag & O_TRUNC) &&
2593 S_ISREG(dentry->d_inode->i_mode)) {
2596 /* will fail later, go on to get the right error */
2600 path->dentry = dentry;
2601 path->mnt = nd->path.mnt;
2606 * Look up and maybe create and open the last component.
2608 * Must be called with i_mutex held on parent.
2610 * Returns 0 if the file was successfully atomically created (if necessary) and
2611 * opened. In this case the file will be returned attached to @file.
2613 * Returns 1 if the file was not completely opened at this time, though lookups
2614 * and creations will have been performed and the dentry returned in @path will
2615 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2616 * specified then a negative dentry may be returned.
2618 * An error code is returned otherwise.
2620 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2621 * cleared otherwise prior to returning.
2623 static int lookup_open(struct nameidata *nd, struct path *path,
2625 const struct open_flags *op,
2626 bool got_write, int *opened)
2628 struct dentry *dir = nd->path.dentry;
2629 struct inode *dir_inode = dir->d_inode;
2630 struct dentry *dentry;
2634 *opened &= ~FILE_CREATED;
2635 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2637 return PTR_ERR(dentry);
2639 /* Cached positive dentry: will open in f_op->open */
2640 if (!need_lookup && dentry->d_inode)
2643 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
2644 return atomic_open(nd, dentry, path, file, op, got_write,
2645 need_lookup, opened);
2649 BUG_ON(dentry->d_inode);
2651 dentry = lookup_real(dir_inode, dentry, nd->flags);
2653 return PTR_ERR(dentry);
2656 /* Negative dentry, just create the file */
2657 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2658 umode_t mode = op->mode;
2659 if (!IS_POSIXACL(dir->d_inode))
2660 mode &= ~current_umask();
2662 * This write is needed to ensure that a
2663 * rw->ro transition does not occur between
2664 * the time when the file is created and when
2665 * a permanent write count is taken through
2666 * the 'struct file' in finish_open().
2672 *opened |= FILE_CREATED;
2673 error = security_path_mknod(&nd->path, dentry, mode, 0);
2676 error = vfs_create(dir->d_inode, dentry, mode,
2677 nd->flags & LOOKUP_EXCL);
2682 path->dentry = dentry;
2683 path->mnt = nd->path.mnt;
2692 * Handle the last step of open()
2694 static int do_last(struct nameidata *nd, struct path *path,
2695 struct file *file, const struct open_flags *op,
2696 int *opened, struct filename *name)
2698 struct dentry *dir = nd->path.dentry;
2699 int open_flag = op->open_flag;
2700 bool will_truncate = (open_flag & O_TRUNC) != 0;
2701 bool got_write = false;
2702 int acc_mode = op->acc_mode;
2703 struct inode *inode;
2704 bool symlink_ok = false;
2705 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2706 bool retried = false;
2709 nd->flags &= ~LOOKUP_PARENT;
2710 nd->flags |= op->intent;
2712 switch (nd->last_type) {
2715 error = handle_dots(nd, nd->last_type);
2720 error = complete_walk(nd);
2723 audit_inode(name, nd->path.dentry, 0);
2724 if (open_flag & O_CREAT) {
2730 error = complete_walk(nd);
2733 audit_inode(name, dir, 0);
2737 if (!(open_flag & O_CREAT)) {
2738 if (nd->last.name[nd->last.len])
2739 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2740 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2742 /* we _can_ be in RCU mode here */
2743 error = lookup_fast(nd, path, &inode);
2750 BUG_ON(nd->inode != dir->d_inode);
2752 /* create side of things */
2754 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2755 * has been cleared when we got to the last component we are
2758 error = complete_walk(nd);
2762 audit_inode(name, dir, LOOKUP_PARENT);
2764 /* trailing slashes? */
2765 if (nd->last.name[nd->last.len])
2770 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2771 error = mnt_want_write(nd->path.mnt);
2775 * do _not_ fail yet - we might not need that or fail with
2776 * a different error; let lookup_open() decide; we'll be
2777 * dropping this one anyway.
2780 mutex_lock(&dir->d_inode->i_mutex);
2781 error = lookup_open(nd, path, file, op, got_write, opened);
2782 mutex_unlock(&dir->d_inode->i_mutex);
2788 if ((*opened & FILE_CREATED) ||
2789 !S_ISREG(file_inode(file)->i_mode))
2790 will_truncate = false;
2792 audit_inode(name, file->f_path.dentry, 0);
2796 if (*opened & FILE_CREATED) {
2797 /* Don't check for write permission, don't truncate */
2798 open_flag &= ~O_TRUNC;
2799 will_truncate = false;
2800 acc_mode = MAY_OPEN;
2801 path_to_nameidata(path, nd);
2802 goto finish_open_created;
2806 * create/update audit record if it already exists.
2808 if (path->dentry->d_inode)
2809 audit_inode(name, path->dentry, 0);
2812 * If atomic_open() acquired write access it is dropped now due to
2813 * possible mount and symlink following (this might be optimized away if
2817 mnt_drop_write(nd->path.mnt);
2822 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
2825 error = follow_managed(path, nd->flags);
2830 nd->flags |= LOOKUP_JUMPED;
2832 BUG_ON(nd->flags & LOOKUP_RCU);
2833 inode = path->dentry->d_inode;
2835 /* we _can_ be in RCU mode here */
2838 path_to_nameidata(path, nd);
2842 if (should_follow_link(inode, !symlink_ok)) {
2843 if (nd->flags & LOOKUP_RCU) {
2844 if (unlikely(unlazy_walk(nd, path->dentry))) {
2849 BUG_ON(inode != path->dentry->d_inode);
2853 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2854 path_to_nameidata(path, nd);
2856 save_parent.dentry = nd->path.dentry;
2857 save_parent.mnt = mntget(path->mnt);
2858 nd->path.dentry = path->dentry;
2862 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2863 error = complete_walk(nd);
2865 path_put(&save_parent);
2869 if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
2872 if ((nd->flags & LOOKUP_DIRECTORY) && !can_lookup(nd->inode))
2874 audit_inode(name, nd->path.dentry, 0);
2876 if (!S_ISREG(nd->inode->i_mode))
2877 will_truncate = false;
2879 if (will_truncate) {
2880 error = mnt_want_write(nd->path.mnt);
2885 finish_open_created:
2886 error = may_open(&nd->path, acc_mode, open_flag);
2889 file->f_path.mnt = nd->path.mnt;
2890 error = finish_open(file, nd->path.dentry, NULL, opened);
2892 if (error == -EOPENSTALE)
2897 error = open_check_o_direct(file);
2900 error = ima_file_check(file, op->acc_mode);
2904 if (will_truncate) {
2905 error = handle_truncate(file);
2911 mnt_drop_write(nd->path.mnt);
2912 path_put(&save_parent);
2917 path_put_conditional(path, nd);
2924 /* If no saved parent or already retried then can't retry */
2925 if (!save_parent.dentry || retried)
2928 BUG_ON(save_parent.dentry != dir);
2929 path_put(&nd->path);
2930 nd->path = save_parent;
2931 nd->inode = dir->d_inode;
2932 save_parent.mnt = NULL;
2933 save_parent.dentry = NULL;
2935 mnt_drop_write(nd->path.mnt);
2942 static struct file *path_openat(int dfd, struct filename *pathname,
2943 struct nameidata *nd, const struct open_flags *op, int flags)
2945 struct file *base = NULL;
2951 file = get_empty_filp();
2955 file->f_flags = op->open_flag;
2957 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
2958 if (unlikely(error))
2961 current->total_link_count = 0;
2962 error = link_path_walk(pathname->name, nd);
2963 if (unlikely(error))
2966 error = do_last(nd, &path, file, op, &opened, pathname);
2967 while (unlikely(error > 0)) { /* trailing symlink */
2968 struct path link = path;
2970 if (!(nd->flags & LOOKUP_FOLLOW)) {
2971 path_put_conditional(&path, nd);
2972 path_put(&nd->path);
2976 error = may_follow_link(&link, nd);
2977 if (unlikely(error))
2979 nd->flags |= LOOKUP_PARENT;
2980 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2981 error = follow_link(&link, nd, &cookie);
2982 if (unlikely(error))
2984 error = do_last(nd, &path, file, op, &opened, pathname);
2985 put_link(nd, &link, cookie);
2988 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2989 path_put(&nd->root);
2992 if (!(opened & FILE_OPENED)) {
2996 if (unlikely(error)) {
2997 if (error == -EOPENSTALE) {
2998 if (flags & LOOKUP_RCU)
3003 file = ERR_PTR(error);
3008 struct file *do_filp_open(int dfd, struct filename *pathname,
3009 const struct open_flags *op, int flags)
3011 struct nameidata nd;
3014 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
3015 if (unlikely(filp == ERR_PTR(-ECHILD)))
3016 filp = path_openat(dfd, pathname, &nd, op, flags);
3017 if (unlikely(filp == ERR_PTR(-ESTALE)))
3018 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
3022 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3023 const char *name, const struct open_flags *op, int flags)
3025 struct nameidata nd;
3027 struct filename filename = { .name = name };
3030 nd.root.dentry = dentry;
3032 flags |= LOOKUP_ROOT;
3034 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
3035 return ERR_PTR(-ELOOP);
3037 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
3038 if (unlikely(file == ERR_PTR(-ECHILD)))
3039 file = path_openat(-1, &filename, &nd, op, flags);
3040 if (unlikely(file == ERR_PTR(-ESTALE)))
3041 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
3045 struct dentry *kern_path_create(int dfd, const char *pathname,
3046 struct path *path, unsigned int lookup_flags)
3048 struct dentry *dentry = ERR_PTR(-EEXIST);
3049 struct nameidata nd;
3052 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3055 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3056 * other flags passed in are ignored!
3058 lookup_flags &= LOOKUP_REVAL;
3060 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
3062 return ERR_PTR(error);
3065 * Yucky last component or no last component at all?
3066 * (foo/., foo/.., /////)
3068 if (nd.last_type != LAST_NORM)
3070 nd.flags &= ~LOOKUP_PARENT;
3071 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3073 /* don't fail immediately if it's r/o, at least try to report other errors */
3074 err2 = mnt_want_write(nd.path.mnt);
3076 * Do the final lookup.
3078 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3079 dentry = lookup_hash(&nd);
3084 if (dentry->d_inode)
3087 * Special case - lookup gave negative, but... we had foo/bar/
3088 * From the vfs_mknod() POV we just have a negative dentry -
3089 * all is fine. Let's be bastards - you had / on the end, you've
3090 * been asking for (non-existent) directory. -ENOENT for you.
3092 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
3096 if (unlikely(err2)) {
3104 dentry = ERR_PTR(error);
3106 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3108 mnt_drop_write(nd.path.mnt);
3113 EXPORT_SYMBOL(kern_path_create);
3115 void done_path_create(struct path *path, struct dentry *dentry)
3118 mutex_unlock(&path->dentry->d_inode->i_mutex);
3119 mnt_drop_write(path->mnt);
3122 EXPORT_SYMBOL(done_path_create);
3124 struct dentry *user_path_create(int dfd, const char __user *pathname,
3125 struct path *path, unsigned int lookup_flags)
3127 struct filename *tmp = getname(pathname);
3130 return ERR_CAST(tmp);
3131 res = kern_path_create(dfd, tmp->name, path, lookup_flags);
3135 EXPORT_SYMBOL(user_path_create);
3137 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3139 int error = may_create(dir, dentry);
3144 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
3147 if (!dir->i_op->mknod)
3150 error = devcgroup_inode_mknod(mode, dev);
3154 error = security_inode_mknod(dir, dentry, mode, dev);
3158 error = dir->i_op->mknod(dir, dentry, mode, dev);
3160 fsnotify_create(dir, dentry);
3164 static int may_mknod(umode_t mode)
3166 switch (mode & S_IFMT) {
3172 case 0: /* zero mode translates to S_IFREG */
3181 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3184 struct dentry *dentry;
3187 unsigned int lookup_flags = 0;
3189 error = may_mknod(mode);
3193 dentry = user_path_create(dfd, filename, &path, lookup_flags);
3195 return PTR_ERR(dentry);
3197 /*sprd reserve 10M space*/
3198 error = check_can_ops(dentry, &path);
3200 pr_err("[mknodat]error is %d\n", error);
3201 done_path_create(&path, dentry);
3205 if (!IS_POSIXACL(path.dentry->d_inode))
3206 mode &= ~current_umask();
3207 error = security_path_mknod(&path, dentry, mode, dev);
3210 switch (mode & S_IFMT) {
3211 case 0: case S_IFREG:
3212 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
3214 case S_IFCHR: case S_IFBLK:
3215 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
3216 new_decode_dev(dev));
3218 case S_IFIFO: case S_IFSOCK:
3219 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
3223 done_path_create(&path, dentry);
3224 if (retry_estale(error, lookup_flags)) {
3225 lookup_flags |= LOOKUP_REVAL;
3231 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
3233 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3236 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3238 int error = may_create(dir, dentry);
3239 unsigned max_links = dir->i_sb->s_max_links;
3244 if (!dir->i_op->mkdir)
3247 mode &= (S_IRWXUGO|S_ISVTX);
3248 error = security_inode_mkdir(dir, dentry, mode);
3252 if (max_links && dir->i_nlink >= max_links)
3255 error = dir->i_op->mkdir(dir, dentry, mode);
3257 fsnotify_mkdir(dir, dentry);
3261 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3263 struct dentry *dentry;
3266 unsigned int lookup_flags = LOOKUP_DIRECTORY;
3269 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
3271 return PTR_ERR(dentry);
3273 /*sprd reserve 10M space*/
3274 error = check_can_ops(dentry, &path);
3276 pr_err("[mkdirat]error is %d\n", error);
3277 done_path_create(&path, dentry);
3281 if (!IS_POSIXACL(path.dentry->d_inode))
3282 mode &= ~current_umask();
3283 error = security_path_mkdir(&path, dentry, mode);
3285 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3286 done_path_create(&path, dentry);
3287 if (retry_estale(error, lookup_flags)) {
3288 lookup_flags |= LOOKUP_REVAL;
3294 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
3296 return sys_mkdirat(AT_FDCWD, pathname, mode);
3300 * The dentry_unhash() helper will try to drop the dentry early: we
3301 * should have a usage count of 1 if we're the only user of this
3302 * dentry, and if that is true (possibly after pruning the dcache),
3303 * then we drop the dentry now.
3305 * A low-level filesystem can, if it choses, legally
3308 * if (!d_unhashed(dentry))
3311 * if it cannot handle the case of removing a directory
3312 * that is still in use by something else..
3314 void dentry_unhash(struct dentry *dentry)
3316 shrink_dcache_parent(dentry);
3317 spin_lock(&dentry->d_lock);
3318 if (dentry->d_count == 1)
3320 spin_unlock(&dentry->d_lock);
3323 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3325 int error = may_delete(dir, dentry, 1);
3330 if (!dir->i_op->rmdir)
3334 mutex_lock(&dentry->d_inode->i_mutex);
3337 if (d_mountpoint(dentry))
3340 error = security_inode_rmdir(dir, dentry);
3344 shrink_dcache_parent(dentry);
3345 error = dir->i_op->rmdir(dir, dentry);
3349 dentry->d_inode->i_flags |= S_DEAD;
3353 mutex_unlock(&dentry->d_inode->i_mutex);
3360 static long do_rmdir(int dfd, const char __user *pathname)
3363 struct filename *name;
3364 struct dentry *dentry;
3365 struct nameidata nd;
3366 unsigned int lookup_flags = 0;
3368 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
3370 return PTR_ERR(name);
3372 switch(nd.last_type) {
3384 nd.flags &= ~LOOKUP_PARENT;
3385 error = mnt_want_write(nd.path.mnt);
3389 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3390 dentry = lookup_hash(&nd);
3391 error = PTR_ERR(dentry);
3394 if (!dentry->d_inode) {
3398 error = security_path_rmdir(&nd.path, dentry);
3401 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
3405 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3406 mnt_drop_write(nd.path.mnt);
3410 if (retry_estale(error, lookup_flags)) {
3411 lookup_flags |= LOOKUP_REVAL;
3417 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
3419 return do_rmdir(AT_FDCWD, pathname);
3422 int vfs_unlink(struct inode *dir, struct dentry *dentry)
3424 int error = may_delete(dir, dentry, 0);
3429 if (!dir->i_op->unlink)
3432 mutex_lock(&dentry->d_inode->i_mutex);
3433 if (d_mountpoint(dentry))
3436 error = security_inode_unlink(dir, dentry);
3438 error = dir->i_op->unlink(dir, dentry);
3443 mutex_unlock(&dentry->d_inode->i_mutex);
3445 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3446 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3447 fsnotify_link_count(dentry->d_inode);
3455 * Make sure that the actual truncation of the file will occur outside its
3456 * directory's i_mutex. Truncate can take a long time if there is a lot of
3457 * writeout happening, and we don't want to prevent access to the directory
3458 * while waiting on the I/O.
3460 static long do_unlinkat(int dfd, const char __user *pathname)
3463 struct filename *name;
3464 struct dentry *dentry;
3465 struct nameidata nd;
3466 struct inode *inode = NULL;
3467 unsigned int lookup_flags = 0;
3469 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
3471 return PTR_ERR(name);
3474 if (nd.last_type != LAST_NORM)
3477 nd.flags &= ~LOOKUP_PARENT;
3478 error = mnt_want_write(nd.path.mnt);
3482 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3483 dentry = lookup_hash(&nd);
3484 error = PTR_ERR(dentry);
3485 if (!IS_ERR(dentry)) {
3486 /* Why not before? Because we want correct error value */
3487 if (nd.last.name[nd.last.len])
3489 inode = dentry->d_inode;
3493 error = security_path_unlink(&nd.path, dentry);
3496 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
3500 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3502 iput(inode); /* truncate the inode here */
3503 mnt_drop_write(nd.path.mnt);
3507 if (retry_estale(error, lookup_flags)) {
3508 lookup_flags |= LOOKUP_REVAL;
3515 error = !dentry->d_inode ? -ENOENT :
3516 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
3520 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
3522 if ((flag & ~AT_REMOVEDIR) != 0)
3525 if (flag & AT_REMOVEDIR)
3526 return do_rmdir(dfd, pathname);
3528 return do_unlinkat(dfd, pathname);
3531 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
3533 return do_unlinkat(AT_FDCWD, pathname);
3536 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
3538 int error = may_create(dir, dentry);
3543 if (!dir->i_op->symlink)
3546 error = security_inode_symlink(dir, dentry, oldname);
3550 error = dir->i_op->symlink(dir, dentry, oldname);
3552 fsnotify_create(dir, dentry);
3556 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3557 int, newdfd, const char __user *, newname)
3560 struct filename *from;
3561 struct dentry *dentry;
3563 unsigned int lookup_flags = 0;
3565 from = getname(oldname);
3567 return PTR_ERR(from);
3569 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
3570 error = PTR_ERR(dentry);
3574 error = security_path_symlink(&path, dentry, from->name);
3576 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
3577 done_path_create(&path, dentry);
3578 if (retry_estale(error, lookup_flags)) {
3579 lookup_flags |= LOOKUP_REVAL;
3587 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
3589 return sys_symlinkat(oldname, AT_FDCWD, newname);
3592 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3594 struct inode *inode = old_dentry->d_inode;
3595 unsigned max_links = dir->i_sb->s_max_links;
3601 error = may_create(dir, new_dentry);
3605 if (dir->i_sb != inode->i_sb)
3609 * A link to an append-only or immutable file cannot be created.
3611 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3613 if (!dir->i_op->link)
3615 if (S_ISDIR(inode->i_mode))
3618 error = security_inode_link(old_dentry, dir, new_dentry);
3622 mutex_lock(&inode->i_mutex);
3623 /* Make sure we don't allow creating hardlink to an unlinked file */
3624 if (inode->i_nlink == 0)
3626 else if (max_links && inode->i_nlink >= max_links)
3629 error = dir->i_op->link(old_dentry, dir, new_dentry);
3630 mutex_unlock(&inode->i_mutex);
3632 fsnotify_link(dir, inode, new_dentry);
3637 * Hardlinks are often used in delicate situations. We avoid
3638 * security-related surprises by not following symlinks on the
3641 * We don't follow them on the oldname either to be compatible
3642 * with linux 2.0, and to avoid hard-linking to directories
3643 * and other special files. --ADM
3645 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3646 int, newdfd, const char __user *, newname, int, flags)
3648 struct dentry *new_dentry;
3649 struct path old_path, new_path;
3653 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3656 * To use null names we require CAP_DAC_READ_SEARCH
3657 * This ensures that not everyone will be able to create
3658 * handlink using the passed filedescriptor.
3660 if (flags & AT_EMPTY_PATH) {
3661 if (!capable(CAP_DAC_READ_SEARCH))
3666 if (flags & AT_SYMLINK_FOLLOW)
3667 how |= LOOKUP_FOLLOW;
3669 error = user_path_at(olddfd, oldname, how, &old_path);
3673 new_dentry = user_path_create(newdfd, newname, &new_path,
3674 (how & LOOKUP_REVAL));
3675 error = PTR_ERR(new_dentry);
3676 if (IS_ERR(new_dentry))
3680 if (old_path.mnt != new_path.mnt)
3682 error = may_linkat(&old_path);
3683 if (unlikely(error))
3685 error = security_path_link(old_path.dentry, &new_path, new_dentry);
3688 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3690 done_path_create(&new_path, new_dentry);
3691 if (retry_estale(error, how)) {
3692 path_put(&old_path);
3693 how |= LOOKUP_REVAL;
3697 path_put(&old_path);
3702 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
3704 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
3708 * The worst of all namespace operations - renaming directory. "Perverted"
3709 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3711 * a) we can get into loop creation. Check is done in is_subdir().
3712 * b) race potential - two innocent renames can create a loop together.
3713 * That's where 4.4 screws up. Current fix: serialization on
3714 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3716 * c) we have to lock _three_ objects - parents and victim (if it exists).
3717 * And that - after we got ->i_mutex on parents (until then we don't know
3718 * whether the target exists). Solution: try to be smart with locking
3719 * order for inodes. We rely on the fact that tree topology may change
3720 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
3721 * move will be locked. Thus we can rank directories by the tree
3722 * (ancestors first) and rank all non-directories after them.
3723 * That works since everybody except rename does "lock parent, lookup,
3724 * lock child" and rename is under ->s_vfs_rename_mutex.
3725 * HOWEVER, it relies on the assumption that any object with ->lookup()
3726 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3727 * we'd better make sure that there's no link(2) for them.
3728 * d) conversion from fhandle to dentry may come in the wrong moment - when
3729 * we are removing the target. Solution: we will have to grab ->i_mutex
3730 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3731 * ->i_mutex on parents, which works but leads to some truly excessive
3734 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3735 struct inode *new_dir, struct dentry *new_dentry)
3738 struct inode *target = new_dentry->d_inode;
3739 unsigned max_links = new_dir->i_sb->s_max_links;
3742 * If we are going to change the parent - check write permissions,
3743 * we'll need to flip '..'.
3745 if (new_dir != old_dir) {
3746 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
3751 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3757 mutex_lock(&target->i_mutex);
3760 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3764 if (max_links && !target && new_dir != old_dir &&
3765 new_dir->i_nlink >= max_links)
3769 shrink_dcache_parent(new_dentry);
3770 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3775 target->i_flags |= S_DEAD;
3776 dont_mount(new_dentry);
3780 mutex_unlock(&target->i_mutex);
3783 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3784 d_move(old_dentry,new_dentry);
3788 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3789 struct inode *new_dir, struct dentry *new_dentry)
3791 struct inode *target = new_dentry->d_inode;
3794 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3800 mutex_lock(&target->i_mutex);
3803 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3806 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3811 dont_mount(new_dentry);
3812 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3813 d_move(old_dentry, new_dentry);
3816 mutex_unlock(&target->i_mutex);
3821 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3822 struct inode *new_dir, struct dentry *new_dentry)
3825 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
3826 const unsigned char *old_name;
3828 if (old_dentry->d_inode == new_dentry->d_inode)
3831 error = may_delete(old_dir, old_dentry, is_dir);
3835 if (!new_dentry->d_inode)
3836 error = may_create(new_dir, new_dentry);
3838 error = may_delete(new_dir, new_dentry, is_dir);
3842 if (!old_dir->i_op->rename)
3845 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3848 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3850 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3852 fsnotify_move(old_dir, new_dir, old_name, is_dir,
3853 new_dentry->d_inode, old_dentry);
3854 fsnotify_oldname_free(old_name);
3859 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3860 int, newdfd, const char __user *, newname)
3862 struct dentry *old_dir, *new_dir;
3863 struct dentry *old_dentry, *new_dentry;
3864 struct dentry *trap;
3865 struct nameidata oldnd, newnd;
3866 struct filename *from;
3867 struct filename *to;
3868 unsigned int lookup_flags = 0;
3869 bool should_retry = false;
3872 from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
3874 error = PTR_ERR(from);
3878 to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
3880 error = PTR_ERR(to);
3885 if (oldnd.path.mnt != newnd.path.mnt)
3888 old_dir = oldnd.path.dentry;
3890 if (oldnd.last_type != LAST_NORM)
3893 new_dir = newnd.path.dentry;
3894 if (newnd.last_type != LAST_NORM)
3897 error = mnt_want_write(oldnd.path.mnt);
3901 oldnd.flags &= ~LOOKUP_PARENT;
3902 newnd.flags &= ~LOOKUP_PARENT;
3903 newnd.flags |= LOOKUP_RENAME_TARGET;
3905 trap = lock_rename(new_dir, old_dir);
3907 old_dentry = lookup_hash(&oldnd);
3908 error = PTR_ERR(old_dentry);
3909 if (IS_ERR(old_dentry))
3911 /* source must exist */
3913 if (!old_dentry->d_inode)
3915 /* unless the source is a directory trailing slashes give -ENOTDIR */
3916 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3918 if (oldnd.last.name[oldnd.last.len])
3920 if (newnd.last.name[newnd.last.len])
3923 /* source should not be ancestor of target */
3925 if (old_dentry == trap)
3927 new_dentry = lookup_hash(&newnd);
3928 error = PTR_ERR(new_dentry);
3929 if (IS_ERR(new_dentry))
3931 /* target should not be an ancestor of source */
3933 if (new_dentry == trap)
3936 error = security_path_rename(&oldnd.path, old_dentry,
3937 &newnd.path, new_dentry);
3940 error = vfs_rename(old_dir->d_inode, old_dentry,
3941 new_dir->d_inode, new_dentry);
3947 unlock_rename(new_dir, old_dir);
3948 mnt_drop_write(oldnd.path.mnt);
3950 if (retry_estale(error, lookup_flags))
3951 should_retry = true;
3952 path_put(&newnd.path);
3955 path_put(&oldnd.path);
3958 should_retry = false;
3959 lookup_flags |= LOOKUP_REVAL;
3966 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
3968 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3971 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3975 len = PTR_ERR(link);
3980 if (len > (unsigned) buflen)
3982 if (copy_to_user(buffer, link, len))
3989 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3990 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3991 * using) it for any given inode is up to filesystem.
3993 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3995 struct nameidata nd;
4000 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
4002 return PTR_ERR(cookie);
4004 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
4005 if (dentry->d_inode->i_op->put_link)
4006 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4010 int vfs_follow_link(struct nameidata *nd, const char *link)
4012 return __vfs_follow_link(nd, link);
4015 /* get the link contents into pagecache */
4016 static char *page_getlink(struct dentry * dentry, struct page **ppage)
4020 struct address_space *mapping = dentry->d_inode->i_mapping;
4021 page = read_mapping_page(mapping, 0, NULL);
4026 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4030 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4032 struct page *page = NULL;
4033 char *s = page_getlink(dentry, &page);
4034 int res = vfs_readlink(dentry,buffer,buflen,s);
4037 page_cache_release(page);
4042 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
4044 struct page *page = NULL;
4045 nd_set_link(nd, page_getlink(dentry, &page));
4049 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
4051 struct page *page = cookie;
4055 page_cache_release(page);
4060 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4062 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
4064 struct address_space *mapping = inode->i_mapping;
4069 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4071 flags |= AOP_FLAG_NOFS;
4074 err = pagecache_write_begin(NULL, mapping, 0, len-1,
4075 flags, &page, &fsdata);
4079 kaddr = kmap_atomic(page);
4080 memcpy(kaddr, symname, len-1);
4081 kunmap_atomic(kaddr);
4083 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4090 mark_inode_dirty(inode);
4096 int page_symlink(struct inode *inode, const char *symname, int len)
4098 return __page_symlink(inode, symname, len,
4099 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
4102 /*for data_partition*/
4103 int statfs_data_partition(struct path *dst_path, struct kstatfs* stat)
4107 if ((!dst_path) || (!stat)) {
4112 error = vfs_statfs(dst_path, stat);
4118 bool is_in_black_list(uid_t euid, gid_t egid)
4122 uid_t id[2] = {euid, egid};
4124 if (!strncmp(current->comm, "slog", 4)) {
4125 return (ret = true);
4130 for (i=0; i< 2; i++) {
4132 case 1001://AID_RADIO
4133 case 1005://AID_AUDIO
4134 case 1006://AID_CAMERA
4135 case 1012://AID_INSTALL
4136 case 1013://AID_MEDIA
4137 case 1022://AID_UNUSED1
4138 case 1023://AID_MEDIA_RW
4140 case 1025://AID_UNUSED2
4141 case 9999://AID_NOBODY
4159 bool check_have_permission(void)
4162 uid_t cur_uid, cur_euid;
4163 gid_t cur_gid, cur_egid;
4165 current_uid_gid(&cur_uid, &cur_gid);
4166 current_euid_egid(&cur_euid, &cur_egid);
4168 ret = (cur_euid < 10000 || cur_egid < 10000)?(!is_in_black_list(cur_euid, cur_egid)):false;
4174 int check_have_space(struct dentry* dest, struct path* path)
4177 long long total_size = 0;
4178 long long avail_size = 0;
4179 struct kstatfs stat;
4180 bool is_perm = false;
4188 err = statfs_data_partition(path, &stat);
4194 total_size = (long long)stat.f_blocks * (long long)stat.f_bsize;
4195 avail_size = (long long)stat.f_bavail * (long long)stat.f_bsize;
4197 if (avail_size < g_rsrvd_size) {
4198 is_perm = check_have_permission();
4200 current_euid_egid(&euid, &egid);
4201 pr_err("[check_have_space]avail_size = %lld, total_size = %lld, \
4202 euid = %u, egid = %u, current->pid %u, current->comm = %s\n",
4203 avail_size, total_size, euid, egid, current->pid, current->comm);
4212 int check_can_ops(struct dentry *cur_dir, struct path* path)
4214 char* path_buf = NULL;
4215 char kbuf[PATH_NAME_MAX];
4217 unsigned long dirname_len = 0;
4218 struct kstatfs stat;
4220 if (!path || !cur_dir) {
4225 br_read_lock(&vfsmount_lock);
4226 list_for_each_entry(mnt, &cur_dir->d_sb->s_mounts, mnt_instance) {
4227 if(strncmp(mnt->mnt_devname, reseved_space_emmc_where, strlen(mnt->mnt_devname))
4228 && strncmp(mnt->mnt_devname, reseved_space_nand_where, strlen(mnt->mnt_devname))){
4229 br_read_unlock(&vfsmount_lock);
4233 br_read_unlock(&vfsmount_lock);
4236 dirname_len = strlen(cur_dir->d_name.name);
4238 memset(kbuf, 0, PATH_NAME_MAX);
4239 path_buf = d_path(path, kbuf, (PATH_NAME_MAX - 64 - dirname_len));
4240 if (IS_ERR(path_buf)) {
4241 err = PTR_ERR(path_buf);
4242 pr_err("[check_can_ops]err is %d\n", err);
4247 strcat(path_buf,"/");
4248 strcat(path_buf,cur_dir->d_name.name);
4250 if (!strncmp(path_buf, DATA_MNT_POINT, 5)) {
4251 if (check_have_space(cur_dir, path) < 0) {
4252 pr_err("[check_can_ops]path_buf is %s\n", path_buf);
4265 int hide_reserved_space_for_user(struct kstatfs *stat)
4268 long long avail_size;
4277 current_euid_egid(&cur_euid, &cur_egid);
4278 /*reserved space is visible for root and system*/
4279 if(cur_euid <= 1000 || cur_egid <= 1000)
4282 avail_size = (long long)stat->f_bavail * (long long)stat->f_bsize;
4283 if (avail_size <= g_rsrvd_size) {
4286 stat->f_bavail -= (unsigned long)g_rsrvd_size /(unsigned long)stat->f_bsize;
4293 const struct inode_operations page_symlink_inode_operations = {
4294 .readlink = generic_readlink,
4295 .follow_link = page_follow_link_light,
4296 .put_link = page_put_link,
4299 EXPORT_SYMBOL(user_path_at);
4300 EXPORT_SYMBOL(follow_down_one);
4301 EXPORT_SYMBOL(follow_down);
4302 EXPORT_SYMBOL(follow_up);
4303 EXPORT_SYMBOL(get_write_access); /* nfsd */
4304 EXPORT_SYMBOL(lock_rename);
4305 EXPORT_SYMBOL(lookup_one_len);
4306 EXPORT_SYMBOL(page_follow_link_light);
4307 EXPORT_SYMBOL(page_put_link);
4308 EXPORT_SYMBOL(page_readlink);
4309 EXPORT_SYMBOL(__page_symlink);
4310 EXPORT_SYMBOL(page_symlink);
4311 EXPORT_SYMBOL(page_symlink_inode_operations);
4312 EXPORT_SYMBOL(kern_path);
4313 EXPORT_SYMBOL(vfs_path_lookup);
4314 EXPORT_SYMBOL(inode_permission);
4315 EXPORT_SYMBOL(unlock_rename);
4316 EXPORT_SYMBOL(vfs_create);
4317 EXPORT_SYMBOL(vfs_follow_link);
4318 EXPORT_SYMBOL(vfs_link);
4319 EXPORT_SYMBOL(vfs_mkdir);
4320 EXPORT_SYMBOL(vfs_mknod);
4321 EXPORT_SYMBOL(generic_permission);
4322 EXPORT_SYMBOL(vfs_readlink);
4323 EXPORT_SYMBOL(vfs_rename);
4324 EXPORT_SYMBOL(vfs_rmdir);
4325 EXPORT_SYMBOL(vfs_symlink);
4326 EXPORT_SYMBOL(vfs_unlink);
4327 EXPORT_SYMBOL(dentry_unhash);
4328 EXPORT_SYMBOL(generic_readlink);
4329 EXPORT_SYMBOL(check_can_ops);
4330 EXPORT_SYMBOL(hide_reserved_space_for_user);