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/module.h>
19 #include <linux/slab.h>
21 #include <linux/namei.h>
22 #include <linux/pagemap.h>
23 #include <linux/fsnotify.h>
24 #include <linux/personality.h>
25 #include <linux/security.h>
26 #include <linux/ima.h>
27 #include <linux/syscalls.h>
28 #include <linux/mount.h>
29 #include <linux/audit.h>
30 #include <linux/capability.h>
31 #include <linux/file.h>
32 #include <linux/fcntl.h>
33 #include <linux/device_cgroup.h>
34 #include <linux/fs_struct.h>
35 #include <asm/uaccess.h>
39 /* [Feb-1997 T. Schoebel-Theuer]
40 * Fundamental changes in the pathname lookup mechanisms (namei)
41 * were necessary because of omirr. The reason is that omirr needs
42 * to know the _real_ pathname, not the user-supplied one, in case
43 * of symlinks (and also when transname replacements occur).
45 * The new code replaces the old recursive symlink resolution with
46 * an iterative one (in case of non-nested symlink chains). It does
47 * this with calls to <fs>_follow_link().
48 * As a side effect, dir_namei(), _namei() and follow_link() are now
49 * replaced with a single function lookup_dentry() that can handle all
50 * the special cases of the former code.
52 * With the new dcache, the pathname is stored at each inode, at least as
53 * long as the refcount of the inode is positive. As a side effect, the
54 * size of the dcache depends on the inode cache and thus is dynamic.
56 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57 * resolution to correspond with current state of the code.
59 * Note that the symlink resolution is not *completely* iterative.
60 * There is still a significant amount of tail- and mid- recursion in
61 * the algorithm. Also, note that <fs>_readlink() is not used in
62 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
63 * may return different results than <fs>_follow_link(). Many virtual
64 * filesystems (including /proc) exhibit this behavior.
67 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
68 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
69 * and the name already exists in form of a symlink, try to create the new
70 * name indicated by the symlink. The old code always complained that the
71 * name already exists, due to not following the symlink even if its target
72 * is nonexistent. The new semantics affects also mknod() and link() when
73 * the name is a symlink pointing to a non-existant name.
75 * I don't know which semantics is the right one, since I have no access
76 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78 * "old" one. Personally, I think the new semantics is much more logical.
79 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80 * file does succeed in both HP-UX and SunOs, but not in Solaris
81 * and in the old Linux semantics.
84 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85 * semantics. See the comments in "open_namei" and "do_link" below.
87 * [10-Sep-98 Alan Modra] Another symlink change.
90 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91 * inside the path - always follow.
92 * in the last component in creation/removal/renaming - never follow.
93 * if LOOKUP_FOLLOW passed - follow.
94 * if the pathname has trailing slashes - follow.
95 * otherwise - don't follow.
96 * (applied in that order).
98 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100 * During the 2.4 we need to fix the userland stuff depending on it -
101 * hopefully we will be able to get rid of that wart in 2.5. So far only
102 * XEmacs seems to be relying on it...
105 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
106 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
107 * any extra contention...
110 /* In order to reduce some races, while at the same time doing additional
111 * checking and hopefully speeding things up, we copy filenames to the
112 * kernel data space before using them..
114 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
115 * PATH_MAX includes the nul terminator --RR.
117 static int do_getname(const char __user *filename, char *page)
120 unsigned long len = PATH_MAX;
122 if (!segment_eq(get_fs(), KERNEL_DS)) {
123 if ((unsigned long) filename >= TASK_SIZE)
125 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
126 len = TASK_SIZE - (unsigned long) filename;
129 retval = strncpy_from_user(page, filename, len);
133 return -ENAMETOOLONG;
139 char * getname(const char __user * filename)
143 result = ERR_PTR(-ENOMEM);
146 int retval = do_getname(filename, tmp);
151 result = ERR_PTR(retval);
154 audit_getname(result);
158 #ifdef CONFIG_AUDITSYSCALL
159 void putname(const char *name)
161 if (unlikely(!audit_dummy_context()))
166 EXPORT_SYMBOL(putname);
170 * This does basic POSIX ACL permission checking
172 static int acl_permission_check(struct inode *inode, int mask,
173 int (*check_acl)(struct inode *inode, int mask))
175 umode_t mode = inode->i_mode;
177 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
179 if (current_fsuid() == inode->i_uid)
182 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
183 int error = check_acl(inode, mask);
184 if (error != -EAGAIN)
188 if (in_group_p(inode->i_gid))
193 * If the DACs are ok we don't need any capability check.
195 if ((mask & ~mode) == 0)
201 * generic_permission - check for access rights on a Posix-like filesystem
202 * @inode: inode to check access rights for
203 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
204 * @check_acl: optional callback to check for Posix ACLs
206 * Used to check for read/write/execute permissions on a file.
207 * We use "fsuid" for this, letting us set arbitrary permissions
208 * for filesystem access without changing the "normal" uids which
209 * are used for other things..
211 int generic_permission(struct inode *inode, int mask,
212 int (*check_acl)(struct inode *inode, int mask))
217 * Do the basic POSIX ACL permission checks.
219 ret = acl_permission_check(inode, mask, check_acl);
224 * Read/write DACs are always overridable.
225 * Executable DACs are overridable if at least one exec bit is set.
227 if (!(mask & MAY_EXEC) || execute_ok(inode))
228 if (capable(CAP_DAC_OVERRIDE))
232 * Searching includes executable on directories, else just read.
234 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
235 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
236 if (capable(CAP_DAC_READ_SEARCH))
243 * inode_permission - check for access rights to a given inode
244 * @inode: inode to check permission on
245 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
247 * Used to check for read/write/execute permissions on an inode.
248 * We use "fsuid" for this, letting us set arbitrary permissions
249 * for filesystem access without changing the "normal" uids which
250 * are used for other things.
252 int inode_permission(struct inode *inode, int mask)
256 if (mask & MAY_WRITE) {
257 umode_t mode = inode->i_mode;
260 * Nobody gets write access to a read-only fs.
262 if (IS_RDONLY(inode) &&
263 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
267 * Nobody gets write access to an immutable file.
269 if (IS_IMMUTABLE(inode))
273 if (inode->i_op->permission)
274 retval = inode->i_op->permission(inode, mask);
276 retval = generic_permission(inode, mask, inode->i_op->check_acl);
281 retval = devcgroup_inode_permission(inode, mask);
285 return security_inode_permission(inode,
286 mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
290 * file_permission - check for additional access rights to a given file
291 * @file: file to check access rights for
292 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
294 * Used to check for read/write/execute permissions on an already opened
298 * Do not use this function in new code. All access checks should
299 * be done using inode_permission().
301 int file_permission(struct file *file, int mask)
303 return inode_permission(file->f_path.dentry->d_inode, mask);
307 * get_write_access() gets write permission for a file.
308 * put_write_access() releases this write permission.
309 * This is used for regular files.
310 * We cannot support write (and maybe mmap read-write shared) accesses and
311 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
312 * can have the following values:
313 * 0: no writers, no VM_DENYWRITE mappings
314 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
315 * > 0: (i_writecount) users are writing to the file.
317 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
318 * except for the cases where we don't hold i_writecount yet. Then we need to
319 * use {get,deny}_write_access() - these functions check the sign and refuse
320 * to do the change if sign is wrong. Exclusion between them is provided by
321 * the inode->i_lock spinlock.
324 int get_write_access(struct inode * inode)
326 spin_lock(&inode->i_lock);
327 if (atomic_read(&inode->i_writecount) < 0) {
328 spin_unlock(&inode->i_lock);
331 atomic_inc(&inode->i_writecount);
332 spin_unlock(&inode->i_lock);
337 int deny_write_access(struct file * file)
339 struct inode *inode = file->f_path.dentry->d_inode;
341 spin_lock(&inode->i_lock);
342 if (atomic_read(&inode->i_writecount) > 0) {
343 spin_unlock(&inode->i_lock);
346 atomic_dec(&inode->i_writecount);
347 spin_unlock(&inode->i_lock);
353 * path_get - get a reference to a path
354 * @path: path to get the reference to
356 * Given a path increment the reference count to the dentry and the vfsmount.
358 void path_get(struct path *path)
363 EXPORT_SYMBOL(path_get);
366 * path_put - put a reference to a path
367 * @path: path to put the reference to
369 * Given a path decrement the reference count to the dentry and the vfsmount.
371 void path_put(struct path *path)
376 EXPORT_SYMBOL(path_put);
379 * release_open_intent - free up open intent resources
380 * @nd: pointer to nameidata
382 void release_open_intent(struct nameidata *nd)
384 if (nd->intent.open.file->f_path.dentry == NULL)
385 put_filp(nd->intent.open.file);
387 fput(nd->intent.open.file);
390 static inline struct dentry *
391 do_revalidate(struct dentry *dentry, struct nameidata *nd)
393 int status = dentry->d_op->d_revalidate(dentry, nd);
394 if (unlikely(status <= 0)) {
396 * The dentry failed validation.
397 * If d_revalidate returned 0 attempt to invalidate
398 * the dentry otherwise d_revalidate is asking us
399 * to return a fail status.
402 if (!d_invalidate(dentry)) {
408 dentry = ERR_PTR(status);
415 * force_reval_path - force revalidation of a dentry
417 * In some situations the path walking code will trust dentries without
418 * revalidating them. This causes problems for filesystems that depend on
419 * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
420 * (which indicates that it's possible for the dentry to go stale), force
421 * a d_revalidate call before proceeding.
423 * Returns 0 if the revalidation was successful. If the revalidation fails,
424 * either return the error returned by d_revalidate or -ESTALE if the
425 * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
426 * invalidate the dentry. It's up to the caller to handle putting references
427 * to the path if necessary.
430 force_reval_path(struct path *path, struct nameidata *nd)
433 struct dentry *dentry = path->dentry;
436 * only check on filesystems where it's possible for the dentry to
437 * become stale. It's assumed that if this flag is set then the
438 * d_revalidate op will also be defined.
440 if (!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))
443 status = dentry->d_op->d_revalidate(dentry, nd);
448 d_invalidate(dentry);
455 * Short-cut version of permission(), for calling on directories
456 * during pathname resolution. Combines parts of permission()
457 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
459 * If appropriate, check DAC only. If not appropriate, or
460 * short-cut DAC fails, then call ->permission() to do more
461 * complete permission check.
463 static int exec_permission(struct inode *inode)
467 if (inode->i_op->permission) {
468 ret = inode->i_op->permission(inode, MAY_EXEC);
473 ret = acl_permission_check(inode, MAY_EXEC, inode->i_op->check_acl);
477 if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
482 return security_inode_permission(inode, MAY_EXEC);
485 static __always_inline void set_root(struct nameidata *nd)
488 struct fs_struct *fs = current->fs;
489 read_lock(&fs->lock);
492 read_unlock(&fs->lock);
496 static int link_path_walk(const char *, struct nameidata *);
498 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
512 res = link_path_walk(link, nd);
513 if (nd->depth || res || nd->last_type!=LAST_NORM)
516 * If it is an iterative symlinks resolution in open_namei() we
517 * have to copy the last component. And all that crap because of
518 * bloody create() on broken symlinks. Furrfu...
521 if (unlikely(!name)) {
525 strcpy(name, nd->last.name);
526 nd->last.name = name;
530 return PTR_ERR(link);
533 static void path_put_conditional(struct path *path, struct nameidata *nd)
536 if (path->mnt != nd->path.mnt)
540 static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
542 dput(nd->path.dentry);
543 if (nd->path.mnt != path->mnt)
544 mntput(nd->path.mnt);
545 nd->path.mnt = path->mnt;
546 nd->path.dentry = path->dentry;
549 static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
553 struct dentry *dentry = path->dentry;
555 touch_atime(path->mnt, dentry);
556 nd_set_link(nd, NULL);
558 if (path->mnt != nd->path.mnt) {
559 path_to_nameidata(path, nd);
563 nd->last_type = LAST_BIND;
564 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
565 error = PTR_ERR(cookie);
566 if (!IS_ERR(cookie)) {
567 char *s = nd_get_link(nd);
570 error = __vfs_follow_link(nd, s);
571 else if (nd->last_type == LAST_BIND) {
572 error = force_reval_path(&nd->path, nd);
576 if (dentry->d_inode->i_op->put_link)
577 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
583 * This limits recursive symlink follows to 8, while
584 * limiting consecutive symlinks to 40.
586 * Without that kind of total limit, nasty chains of consecutive
587 * symlinks can cause almost arbitrarily long lookups.
589 static inline int do_follow_link(struct path *path, struct nameidata *nd)
592 if (current->link_count >= MAX_NESTED_LINKS)
594 if (current->total_link_count >= 40)
596 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
598 err = security_inode_follow_link(path->dentry, nd);
601 current->link_count++;
602 current->total_link_count++;
604 err = __do_follow_link(path, nd);
606 current->link_count--;
610 path_put_conditional(path, nd);
615 int follow_up(struct path *path)
617 struct vfsmount *parent;
618 struct dentry *mountpoint;
619 spin_lock(&vfsmount_lock);
620 parent = path->mnt->mnt_parent;
621 if (parent == path->mnt) {
622 spin_unlock(&vfsmount_lock);
626 mountpoint = dget(path->mnt->mnt_mountpoint);
627 spin_unlock(&vfsmount_lock);
629 path->dentry = mountpoint;
635 /* no need for dcache_lock, as serialization is taken care in
638 static int __follow_mount(struct path *path)
641 while (d_mountpoint(path->dentry)) {
642 struct vfsmount *mounted = lookup_mnt(path);
649 path->dentry = dget(mounted->mnt_root);
655 static void follow_mount(struct path *path)
657 while (d_mountpoint(path->dentry)) {
658 struct vfsmount *mounted = lookup_mnt(path);
664 path->dentry = dget(mounted->mnt_root);
668 /* no need for dcache_lock, as serialization is taken care in
671 int follow_down(struct path *path)
673 struct vfsmount *mounted;
675 mounted = lookup_mnt(path);
680 path->dentry = dget(mounted->mnt_root);
686 static __always_inline void follow_dotdot(struct nameidata *nd)
691 struct vfsmount *parent;
692 struct dentry *old = nd->path.dentry;
694 if (nd->path.dentry == nd->root.dentry &&
695 nd->path.mnt == nd->root.mnt) {
698 spin_lock(&dcache_lock);
699 if (nd->path.dentry != nd->path.mnt->mnt_root) {
700 nd->path.dentry = dget(nd->path.dentry->d_parent);
701 spin_unlock(&dcache_lock);
705 spin_unlock(&dcache_lock);
706 spin_lock(&vfsmount_lock);
707 parent = nd->path.mnt->mnt_parent;
708 if (parent == nd->path.mnt) {
709 spin_unlock(&vfsmount_lock);
713 nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
714 spin_unlock(&vfsmount_lock);
716 mntput(nd->path.mnt);
717 nd->path.mnt = parent;
719 follow_mount(&nd->path);
723 * It's more convoluted than I'd like it to be, but... it's still fairly
724 * small and for now I'd prefer to have fast path as straight as possible.
725 * It _is_ time-critical.
727 static int do_lookup(struct nameidata *nd, struct qstr *name,
730 struct vfsmount *mnt = nd->path.mnt;
731 struct dentry *dentry, *parent;
734 * See if the low-level filesystem might want
735 * to use its own hash..
737 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
738 int err = nd->path.dentry->d_op->d_hash(nd->path.dentry, name);
743 dentry = __d_lookup(nd->path.dentry, name);
746 if (dentry->d_op && dentry->d_op->d_revalidate)
747 goto need_revalidate;
750 path->dentry = dentry;
751 __follow_mount(path);
755 parent = nd->path.dentry;
756 dir = parent->d_inode;
758 mutex_lock(&dir->i_mutex);
760 * First re-do the cached lookup just in case it was created
761 * while we waited for the directory semaphore..
763 * FIXME! This could use version numbering or similar to
764 * avoid unnecessary cache lookups.
766 * The "dcache_lock" is purely to protect the RCU list walker
767 * from concurrent renames at this point (we mustn't get false
768 * negatives from the RCU list walk here, unlike the optimistic
771 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
773 dentry = d_lookup(parent, name);
777 /* Don't create child dentry for a dead directory. */
778 dentry = ERR_PTR(-ENOENT);
782 new = d_alloc(parent, name);
783 dentry = ERR_PTR(-ENOMEM);
785 dentry = dir->i_op->lookup(dir, new, nd);
792 mutex_unlock(&dir->i_mutex);
799 * Uhhuh! Nasty case: the cache was re-populated while
800 * we waited on the semaphore. Need to revalidate.
802 mutex_unlock(&dir->i_mutex);
803 if (dentry->d_op && dentry->d_op->d_revalidate) {
804 dentry = do_revalidate(dentry, nd);
806 dentry = ERR_PTR(-ENOENT);
813 dentry = do_revalidate(dentry, nd);
821 return PTR_ERR(dentry);
825 * This is a temporary kludge to deal with "automount" symlinks; proper
826 * solution is to trigger them on follow_mount(), so that do_lookup()
827 * would DTRT. To be killed before 2.6.34-final.
829 static inline int follow_on_final(struct inode *inode, unsigned lookup_flags)
831 return inode && unlikely(inode->i_op->follow_link) &&
832 ((lookup_flags & LOOKUP_FOLLOW) || S_ISDIR(inode->i_mode));
837 * This is the basic name resolution function, turning a pathname into
838 * the final dentry. We expect 'base' to be positive and a directory.
840 * Returns 0 and nd will have valid dentry and mnt on success.
841 * Returns error and drops reference to input namei data on failure.
843 static int link_path_walk(const char *name, struct nameidata *nd)
848 unsigned int lookup_flags = nd->flags;
855 inode = nd->path.dentry->d_inode;
857 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
859 /* At this point we know we have a real path component. */
865 nd->flags |= LOOKUP_CONTINUE;
866 err = exec_permission(inode);
871 c = *(const unsigned char *)name;
873 hash = init_name_hash();
876 hash = partial_name_hash(c, hash);
877 c = *(const unsigned char *)name;
878 } while (c && (c != '/'));
879 this.len = name - (const char *) this.name;
880 this.hash = end_name_hash(hash);
882 /* remove trailing slashes? */
885 while (*++name == '/');
887 goto last_with_slashes;
890 * "." and ".." are special - ".." especially so because it has
891 * to be able to know about the current root directory and
892 * parent relationships.
894 if (this.name[0] == '.') switch (this.len) {
898 if (this.name[1] != '.')
901 inode = nd->path.dentry->d_inode;
906 /* This does the actual lookups.. */
907 err = do_lookup(nd, &this, &next);
912 inode = next.dentry->d_inode;
916 if (inode->i_op->follow_link) {
917 err = do_follow_link(&next, nd);
921 inode = nd->path.dentry->d_inode;
925 path_to_nameidata(&next, nd);
927 if (!inode->i_op->lookup)
930 /* here ends the main loop */
933 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
935 /* Clear LOOKUP_CONTINUE iff it was previously unset */
936 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
937 if (lookup_flags & LOOKUP_PARENT)
939 if (this.name[0] == '.') switch (this.len) {
943 if (this.name[1] != '.')
946 inode = nd->path.dentry->d_inode;
951 err = do_lookup(nd, &this, &next);
954 inode = next.dentry->d_inode;
955 if (follow_on_final(inode, lookup_flags)) {
956 err = do_follow_link(&next, nd);
959 inode = nd->path.dentry->d_inode;
961 path_to_nameidata(&next, nd);
965 if (lookup_flags & LOOKUP_DIRECTORY) {
967 if (!inode->i_op->lookup)
973 nd->last_type = LAST_NORM;
974 if (this.name[0] != '.')
977 nd->last_type = LAST_DOT;
978 else if (this.len == 2 && this.name[1] == '.')
979 nd->last_type = LAST_DOTDOT;
984 * We bypassed the ordinary revalidation routines.
985 * We may need to check the cached dentry for staleness.
987 if (nd->path.dentry && nd->path.dentry->d_sb &&
988 (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
990 /* Note: we do not d_invalidate() */
991 if (!nd->path.dentry->d_op->d_revalidate(
992 nd->path.dentry, nd))
998 path_put_conditional(&next, nd);
1001 path_put(&nd->path);
1006 static int path_walk(const char *name, struct nameidata *nd)
1008 struct path save = nd->path;
1011 current->total_link_count = 0;
1013 /* make sure the stuff we saved doesn't go away */
1016 result = link_path_walk(name, nd);
1017 if (result == -ESTALE) {
1018 /* nd->path had been dropped */
1019 current->total_link_count = 0;
1021 path_get(&nd->path);
1022 nd->flags |= LOOKUP_REVAL;
1023 result = link_path_walk(name, nd);
1031 static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
1037 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1040 nd->root.mnt = NULL;
1044 nd->path = nd->root;
1045 path_get(&nd->root);
1046 } else if (dfd == AT_FDCWD) {
1047 struct fs_struct *fs = current->fs;
1048 read_lock(&fs->lock);
1051 read_unlock(&fs->lock);
1053 struct dentry *dentry;
1055 file = fget_light(dfd, &fput_needed);
1060 dentry = file->f_path.dentry;
1063 if (!S_ISDIR(dentry->d_inode->i_mode))
1066 retval = file_permission(file, MAY_EXEC);
1070 nd->path = file->f_path;
1071 path_get(&file->f_path);
1073 fput_light(file, fput_needed);
1078 fput_light(file, fput_needed);
1083 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1084 static int do_path_lookup(int dfd, const char *name,
1085 unsigned int flags, struct nameidata *nd)
1087 int retval = path_init(dfd, name, flags, nd);
1089 retval = path_walk(name, nd);
1090 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1091 nd->path.dentry->d_inode))
1092 audit_inode(name, nd->path.dentry);
1094 path_put(&nd->root);
1095 nd->root.mnt = NULL;
1100 int path_lookup(const char *name, unsigned int flags,
1101 struct nameidata *nd)
1103 return do_path_lookup(AT_FDCWD, name, flags, nd);
1106 int kern_path(const char *name, unsigned int flags, struct path *path)
1108 struct nameidata nd;
1109 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1116 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1117 * @dentry: pointer to dentry of the base directory
1118 * @mnt: pointer to vfs mount of the base directory
1119 * @name: pointer to file name
1120 * @flags: lookup flags
1121 * @nd: pointer to nameidata
1123 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1124 const char *name, unsigned int flags,
1125 struct nameidata *nd)
1129 /* same as do_path_lookup */
1130 nd->last_type = LAST_ROOT;
1134 nd->path.dentry = dentry;
1136 path_get(&nd->path);
1137 nd->root = nd->path;
1138 path_get(&nd->root);
1140 retval = path_walk(name, nd);
1141 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1142 nd->path.dentry->d_inode))
1143 audit_inode(name, nd->path.dentry);
1145 path_put(&nd->root);
1146 nd->root.mnt = NULL;
1151 static struct dentry *__lookup_hash(struct qstr *name,
1152 struct dentry *base, struct nameidata *nd)
1154 struct dentry *dentry;
1155 struct inode *inode;
1158 inode = base->d_inode;
1161 * See if the low-level filesystem might want
1162 * to use its own hash..
1164 if (base->d_op && base->d_op->d_hash) {
1165 err = base->d_op->d_hash(base, name);
1166 dentry = ERR_PTR(err);
1171 dentry = __d_lookup(base, name);
1173 /* lockess __d_lookup may fail due to concurrent d_move()
1174 * in some unrelated directory, so try with d_lookup
1177 dentry = d_lookup(base, name);
1179 if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
1180 dentry = do_revalidate(dentry, nd);
1185 /* Don't create child dentry for a dead directory. */
1186 dentry = ERR_PTR(-ENOENT);
1187 if (IS_DEADDIR(inode))
1190 new = d_alloc(base, name);
1191 dentry = ERR_PTR(-ENOMEM);
1194 dentry = inode->i_op->lookup(inode, new, nd);
1205 * Restricted form of lookup. Doesn't follow links, single-component only,
1206 * needs parent already locked. Doesn't follow mounts.
1209 static struct dentry *lookup_hash(struct nameidata *nd)
1213 err = exec_permission(nd->path.dentry->d_inode);
1215 return ERR_PTR(err);
1216 return __lookup_hash(&nd->last, nd->path.dentry, nd);
1219 static int __lookup_one_len(const char *name, struct qstr *this,
1220 struct dentry *base, int len)
1230 hash = init_name_hash();
1232 c = *(const unsigned char *)name++;
1233 if (c == '/' || c == '\0')
1235 hash = partial_name_hash(c, hash);
1237 this->hash = end_name_hash(hash);
1242 * lookup_one_len - filesystem helper to lookup single pathname component
1243 * @name: pathname component to lookup
1244 * @base: base directory to lookup from
1245 * @len: maximum length @len should be interpreted to
1247 * Note that this routine is purely a helper for filesystem usage and should
1248 * not be called by generic code. Also note that by using this function the
1249 * nameidata argument is passed to the filesystem methods and a filesystem
1250 * using this helper needs to be prepared for that.
1252 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1257 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1259 err = __lookup_one_len(name, &this, base, len);
1261 return ERR_PTR(err);
1263 err = exec_permission(base->d_inode);
1265 return ERR_PTR(err);
1266 return __lookup_hash(&this, base, NULL);
1269 int user_path_at(int dfd, const char __user *name, unsigned flags,
1272 struct nameidata nd;
1273 char *tmp = getname(name);
1274 int err = PTR_ERR(tmp);
1277 BUG_ON(flags & LOOKUP_PARENT);
1279 err = do_path_lookup(dfd, tmp, flags, &nd);
1287 static int user_path_parent(int dfd, const char __user *path,
1288 struct nameidata *nd, char **name)
1290 char *s = getname(path);
1296 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1306 * It's inline, so penalty for filesystems that don't use sticky bit is
1309 static inline int check_sticky(struct inode *dir, struct inode *inode)
1311 uid_t fsuid = current_fsuid();
1313 if (!(dir->i_mode & S_ISVTX))
1315 if (inode->i_uid == fsuid)
1317 if (dir->i_uid == fsuid)
1319 return !capable(CAP_FOWNER);
1323 * Check whether we can remove a link victim from directory dir, check
1324 * whether the type of victim is right.
1325 * 1. We can't do it if dir is read-only (done in permission())
1326 * 2. We should have write and exec permissions on dir
1327 * 3. We can't remove anything from append-only dir
1328 * 4. We can't do anything with immutable dir (done in permission())
1329 * 5. If the sticky bit on dir is set we should either
1330 * a. be owner of dir, or
1331 * b. be owner of victim, or
1332 * c. have CAP_FOWNER capability
1333 * 6. If the victim is append-only or immutable we can't do antyhing with
1334 * links pointing to it.
1335 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1336 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1337 * 9. We can't remove a root or mountpoint.
1338 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1339 * nfs_async_unlink().
1341 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1345 if (!victim->d_inode)
1348 BUG_ON(victim->d_parent->d_inode != dir);
1349 audit_inode_child(victim->d_name.name, victim, dir);
1351 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1356 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1357 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1360 if (!S_ISDIR(victim->d_inode->i_mode))
1362 if (IS_ROOT(victim))
1364 } else if (S_ISDIR(victim->d_inode->i_mode))
1366 if (IS_DEADDIR(dir))
1368 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1373 /* Check whether we can create an object with dentry child in directory
1375 * 1. We can't do it if child already exists (open has special treatment for
1376 * this case, but since we are inlined it's OK)
1377 * 2. We can't do it if dir is read-only (done in permission())
1378 * 3. We should have write and exec permissions on dir
1379 * 4. We can't do it if dir is immutable (done in permission())
1381 static inline int may_create(struct inode *dir, struct dentry *child)
1385 if (IS_DEADDIR(dir))
1387 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1391 * O_DIRECTORY translates into forcing a directory lookup.
1393 static inline int lookup_flags(unsigned int f)
1395 unsigned long retval = LOOKUP_FOLLOW;
1398 retval &= ~LOOKUP_FOLLOW;
1400 if (f & O_DIRECTORY)
1401 retval |= LOOKUP_DIRECTORY;
1407 * p1 and p2 should be directories on the same fs.
1409 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1414 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1418 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1420 p = d_ancestor(p2, p1);
1422 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1423 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1427 p = d_ancestor(p1, p2);
1429 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1430 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1434 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1435 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1439 void unlock_rename(struct dentry *p1, struct dentry *p2)
1441 mutex_unlock(&p1->d_inode->i_mutex);
1443 mutex_unlock(&p2->d_inode->i_mutex);
1444 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1448 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1449 struct nameidata *nd)
1451 int error = may_create(dir, dentry);
1456 if (!dir->i_op->create)
1457 return -EACCES; /* shouldn't it be ENOSYS? */
1460 error = security_inode_create(dir, dentry, mode);
1463 error = dir->i_op->create(dir, dentry, mode, nd);
1465 fsnotify_create(dir, dentry);
1469 int may_open(struct path *path, int acc_mode, int flag)
1471 struct dentry *dentry = path->dentry;
1472 struct inode *inode = dentry->d_inode;
1478 switch (inode->i_mode & S_IFMT) {
1482 if (acc_mode & MAY_WRITE)
1487 if (path->mnt->mnt_flags & MNT_NODEV)
1496 error = inode_permission(inode, acc_mode);
1501 * An append-only file must be opened in append mode for writing.
1503 if (IS_APPEND(inode)) {
1504 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1510 /* O_NOATIME can only be set by the owner or superuser */
1511 if (flag & O_NOATIME && !is_owner_or_cap(inode))
1515 * Ensure there are no outstanding leases on the file.
1517 return break_lease(inode, flag);
1520 static int handle_truncate(struct path *path)
1522 struct inode *inode = path->dentry->d_inode;
1523 int error = get_write_access(inode);
1527 * Refuse to truncate files with mandatory locks held on them.
1529 error = locks_verify_locked(inode);
1531 error = security_path_truncate(path, 0,
1532 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
1534 error = do_truncate(path->dentry, 0,
1535 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1538 put_write_access(inode);
1543 * Be careful about ever adding any more callers of this
1544 * function. Its flags must be in the namei format, not
1545 * what get passed to sys_open().
1547 static int __open_namei_create(struct nameidata *nd, struct path *path,
1551 struct dentry *dir = nd->path.dentry;
1553 if (!IS_POSIXACL(dir->d_inode))
1554 mode &= ~current_umask();
1555 error = security_path_mknod(&nd->path, path->dentry, mode, 0);
1558 error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1560 mutex_unlock(&dir->d_inode->i_mutex);
1561 dput(nd->path.dentry);
1562 nd->path.dentry = path->dentry;
1565 /* Don't check for write permission, don't truncate */
1566 return may_open(&nd->path, 0, flag & ~O_TRUNC);
1570 * Note that while the flag value (low two bits) for sys_open means:
1575 * it is changed into
1576 * 00 - no permissions needed
1577 * 01 - read-permission
1578 * 10 - write-permission
1580 * for the internal routines (ie open_namei()/follow_link() etc)
1581 * This is more logical, and also allows the 00 "no perm needed"
1582 * to be used for symlinks (where the permissions are checked
1586 static inline int open_to_namei_flags(int flag)
1588 if ((flag+1) & O_ACCMODE)
1593 static int open_will_truncate(int flag, struct inode *inode)
1596 * We'll never write to the fs underlying
1599 if (special_file(inode->i_mode))
1601 return (flag & O_TRUNC);
1605 * Note that the low bits of the passed in "open_flag"
1606 * are not the same as in the local variable "flag". See
1607 * open_to_namei_flags() for more details.
1609 struct file *do_filp_open(int dfd, const char *pathname,
1610 int open_flag, int mode, int acc_mode)
1613 struct nameidata nd;
1619 int flag = open_to_namei_flags(open_flag);
1620 int force_reval = 0;
1623 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
1624 * check for O_DSYNC if the need any syncing at all we enforce it's
1625 * always set instead of having to deal with possibly weird behaviour
1626 * for malicious applications setting only __O_SYNC.
1628 if (open_flag & __O_SYNC)
1629 open_flag |= O_DSYNC;
1632 acc_mode = MAY_OPEN | ACC_MODE(open_flag);
1634 /* O_TRUNC implies we need access checks for write permissions */
1636 acc_mode |= MAY_WRITE;
1638 /* Allow the LSM permission hook to distinguish append
1639 access from general write access. */
1640 if (flag & O_APPEND)
1641 acc_mode |= MAY_APPEND;
1644 * The simplest case - just a plain lookup.
1646 if (!(flag & O_CREAT)) {
1647 filp = get_empty_filp();
1650 return ERR_PTR(-ENFILE);
1651 nd.intent.open.file = filp;
1652 filp->f_flags = open_flag;
1653 nd.intent.open.flags = flag;
1654 nd.intent.open.create_mode = 0;
1655 error = do_path_lookup(dfd, pathname,
1656 lookup_flags(flag)|LOOKUP_OPEN, &nd);
1657 if (IS_ERR(nd.intent.open.file)) {
1659 error = PTR_ERR(nd.intent.open.file);
1663 release_open_intent(&nd);
1665 return ERR_PTR(error);
1670 * Create - we need to know the parent.
1673 error = path_init(dfd, pathname, LOOKUP_PARENT, &nd);
1675 return ERR_PTR(error);
1677 nd.flags |= LOOKUP_REVAL;
1678 error = path_walk(pathname, &nd);
1682 return ERR_PTR(error);
1684 if (unlikely(!audit_dummy_context()))
1685 audit_inode(pathname, nd.path.dentry);
1688 * We have the parent and last component. First of all, check
1689 * that we are not asked to creat(2) an obvious directory - that
1693 if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
1697 filp = get_empty_filp();
1700 nd.intent.open.file = filp;
1701 filp->f_flags = open_flag;
1702 nd.intent.open.flags = flag;
1703 nd.intent.open.create_mode = mode;
1704 dir = nd.path.dentry;
1705 nd.flags &= ~LOOKUP_PARENT;
1706 nd.flags |= LOOKUP_CREATE | LOOKUP_OPEN;
1708 nd.flags |= LOOKUP_EXCL;
1709 mutex_lock(&dir->d_inode->i_mutex);
1710 path.dentry = lookup_hash(&nd);
1711 path.mnt = nd.path.mnt;
1714 error = PTR_ERR(path.dentry);
1715 if (IS_ERR(path.dentry)) {
1716 mutex_unlock(&dir->d_inode->i_mutex);
1720 if (IS_ERR(nd.intent.open.file)) {
1721 error = PTR_ERR(nd.intent.open.file);
1722 goto exit_mutex_unlock;
1725 /* Negative dentry, just create the file */
1726 if (!path.dentry->d_inode) {
1728 * This write is needed to ensure that a
1729 * ro->rw transition does not occur between
1730 * the time when the file is created and when
1731 * a permanent write count is taken through
1732 * the 'struct file' in nameidata_to_filp().
1734 error = mnt_want_write(nd.path.mnt);
1736 goto exit_mutex_unlock;
1737 error = __open_namei_create(&nd, &path, flag, mode);
1739 mnt_drop_write(nd.path.mnt);
1742 filp = nameidata_to_filp(&nd);
1743 mnt_drop_write(nd.path.mnt);
1746 if (!IS_ERR(filp)) {
1747 error = ima_file_check(filp, acc_mode);
1750 filp = ERR_PTR(error);
1757 * It already exists.
1759 mutex_unlock(&dir->d_inode->i_mutex);
1760 audit_inode(pathname, path.dentry);
1766 if (__follow_mount(&path)) {
1768 if (flag & O_NOFOLLOW)
1773 if (!path.dentry->d_inode)
1775 if (path.dentry->d_inode->i_op->follow_link)
1778 path_to_nameidata(&path, &nd);
1780 if (S_ISDIR(path.dentry->d_inode->i_mode))
1785 * 1. may_open() truncates a file
1786 * 2. a rw->ro mount transition occurs
1787 * 3. nameidata_to_filp() fails due to
1789 * That would be inconsistent, and should
1790 * be avoided. Taking this mnt write here
1791 * ensures that (2) can not occur.
1793 will_truncate = open_will_truncate(flag, nd.path.dentry->d_inode);
1794 if (will_truncate) {
1795 error = mnt_want_write(nd.path.mnt);
1799 error = may_open(&nd.path, acc_mode, flag);
1802 mnt_drop_write(nd.path.mnt);
1805 filp = nameidata_to_filp(&nd);
1806 if (!IS_ERR(filp)) {
1807 error = ima_file_check(filp, acc_mode);
1810 filp = ERR_PTR(error);
1813 if (!IS_ERR(filp)) {
1814 if (will_truncate) {
1815 error = handle_truncate(&nd.path);
1818 filp = ERR_PTR(error);
1823 * It is now safe to drop the mnt write
1824 * because the filp has had a write taken
1828 mnt_drop_write(nd.path.mnt);
1834 mutex_unlock(&dir->d_inode->i_mutex);
1836 path_put_conditional(&path, &nd);
1838 if (!IS_ERR(nd.intent.open.file))
1839 release_open_intent(&nd);
1844 return ERR_PTR(error);
1848 if (flag & O_NOFOLLOW)
1851 * This is subtle. Instead of calling do_follow_link() we do the
1852 * thing by hands. The reason is that this way we have zero link_count
1853 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1854 * After that we have the parent and last component, i.e.
1855 * we are in the same situation as after the first path_walk().
1856 * Well, almost - if the last component is normal we get its copy
1857 * stored in nd->last.name and we will have to putname() it when we
1858 * are done. Procfs-like symlinks just set LAST_BIND.
1860 nd.flags |= LOOKUP_PARENT;
1861 error = security_inode_follow_link(path.dentry, &nd);
1864 error = __do_follow_link(&path, &nd);
1867 /* Does someone understand code flow here? Or it is only
1868 * me so stupid? Anathema to whoever designed this non-sense
1869 * with "intent.open".
1871 release_open_intent(&nd);
1874 if (error == -ESTALE && !force_reval) {
1878 return ERR_PTR(error);
1880 nd.flags &= ~LOOKUP_PARENT;
1881 if (nd.last_type == LAST_BIND)
1884 if (nd.last_type != LAST_NORM)
1886 if (nd.last.name[nd.last.len]) {
1887 __putname(nd.last.name);
1892 __putname(nd.last.name);
1895 dir = nd.path.dentry;
1896 mutex_lock(&dir->d_inode->i_mutex);
1897 path.dentry = lookup_hash(&nd);
1898 path.mnt = nd.path.mnt;
1899 __putname(nd.last.name);
1904 * filp_open - open file and return file pointer
1906 * @filename: path to open
1907 * @flags: open flags as per the open(2) second argument
1908 * @mode: mode for the new file if O_CREAT is set, else ignored
1910 * This is the helper to open a file from kernelspace if you really
1911 * have to. But in generally you should not do this, so please move
1912 * along, nothing to see here..
1914 struct file *filp_open(const char *filename, int flags, int mode)
1916 return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
1918 EXPORT_SYMBOL(filp_open);
1921 * lookup_create - lookup a dentry, creating it if it doesn't exist
1922 * @nd: nameidata info
1923 * @is_dir: directory flag
1925 * Simple function to lookup and return a dentry and create it
1926 * if it doesn't exist. Is SMP-safe.
1928 * Returns with nd->path.dentry->d_inode->i_mutex locked.
1930 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1932 struct dentry *dentry = ERR_PTR(-EEXIST);
1934 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
1936 * Yucky last component or no last component at all?
1937 * (foo/., foo/.., /////)
1939 if (nd->last_type != LAST_NORM)
1941 nd->flags &= ~LOOKUP_PARENT;
1942 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
1943 nd->intent.open.flags = O_EXCL;
1946 * Do the final lookup.
1948 dentry = lookup_hash(nd);
1952 if (dentry->d_inode)
1955 * Special case - lookup gave negative, but... we had foo/bar/
1956 * From the vfs_mknod() POV we just have a negative dentry -
1957 * all is fine. Let's be bastards - you had / on the end, you've
1958 * been asking for (non-existent) directory. -ENOENT for you.
1960 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
1962 dentry = ERR_PTR(-ENOENT);
1967 dentry = ERR_PTR(-EEXIST);
1971 EXPORT_SYMBOL_GPL(lookup_create);
1973 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1975 int error = may_create(dir, dentry);
1980 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1983 if (!dir->i_op->mknod)
1986 error = devcgroup_inode_mknod(mode, dev);
1990 error = security_inode_mknod(dir, dentry, mode, dev);
1994 error = dir->i_op->mknod(dir, dentry, mode, dev);
1996 fsnotify_create(dir, dentry);
2000 static int may_mknod(mode_t mode)
2002 switch (mode & S_IFMT) {
2008 case 0: /* zero mode translates to S_IFREG */
2017 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2022 struct dentry *dentry;
2023 struct nameidata nd;
2028 error = user_path_parent(dfd, filename, &nd, &tmp);
2032 dentry = lookup_create(&nd, 0);
2033 if (IS_ERR(dentry)) {
2034 error = PTR_ERR(dentry);
2037 if (!IS_POSIXACL(nd.path.dentry->d_inode))
2038 mode &= ~current_umask();
2039 error = may_mknod(mode);
2042 error = mnt_want_write(nd.path.mnt);
2045 error = security_path_mknod(&nd.path, dentry, mode, dev);
2047 goto out_drop_write;
2048 switch (mode & S_IFMT) {
2049 case 0: case S_IFREG:
2050 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
2052 case S_IFCHR: case S_IFBLK:
2053 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
2054 new_decode_dev(dev));
2056 case S_IFIFO: case S_IFSOCK:
2057 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
2061 mnt_drop_write(nd.path.mnt);
2065 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2072 SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
2074 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2077 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2079 int error = may_create(dir, dentry);
2084 if (!dir->i_op->mkdir)
2087 mode &= (S_IRWXUGO|S_ISVTX);
2088 error = security_inode_mkdir(dir, dentry, mode);
2092 error = dir->i_op->mkdir(dir, dentry, mode);
2094 fsnotify_mkdir(dir, dentry);
2098 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
2102 struct dentry *dentry;
2103 struct nameidata nd;
2105 error = user_path_parent(dfd, pathname, &nd, &tmp);
2109 dentry = lookup_create(&nd, 1);
2110 error = PTR_ERR(dentry);
2114 if (!IS_POSIXACL(nd.path.dentry->d_inode))
2115 mode &= ~current_umask();
2116 error = mnt_want_write(nd.path.mnt);
2119 error = security_path_mkdir(&nd.path, dentry, mode);
2121 goto out_drop_write;
2122 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2124 mnt_drop_write(nd.path.mnt);
2128 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2135 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
2137 return sys_mkdirat(AT_FDCWD, pathname, mode);
2141 * We try to drop the dentry early: we should have
2142 * a usage count of 2 if we're the only user of this
2143 * dentry, and if that is true (possibly after pruning
2144 * the dcache), then we drop the dentry now.
2146 * A low-level filesystem can, if it choses, legally
2149 * if (!d_unhashed(dentry))
2152 * if it cannot handle the case of removing a directory
2153 * that is still in use by something else..
2155 void dentry_unhash(struct dentry *dentry)
2158 shrink_dcache_parent(dentry);
2159 spin_lock(&dcache_lock);
2160 spin_lock(&dentry->d_lock);
2161 if (atomic_read(&dentry->d_count) == 2)
2163 spin_unlock(&dentry->d_lock);
2164 spin_unlock(&dcache_lock);
2167 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2169 int error = may_delete(dir, dentry, 1);
2174 if (!dir->i_op->rmdir)
2177 mutex_lock(&dentry->d_inode->i_mutex);
2178 dentry_unhash(dentry);
2179 if (d_mountpoint(dentry))
2182 error = security_inode_rmdir(dir, dentry);
2184 error = dir->i_op->rmdir(dir, dentry);
2186 dentry->d_inode->i_flags |= S_DEAD;
2189 mutex_unlock(&dentry->d_inode->i_mutex);
2198 static long do_rmdir(int dfd, const char __user *pathname)
2202 struct dentry *dentry;
2203 struct nameidata nd;
2205 error = user_path_parent(dfd, pathname, &nd, &name);
2209 switch(nd.last_type) {
2221 nd.flags &= ~LOOKUP_PARENT;
2223 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2224 dentry = lookup_hash(&nd);
2225 error = PTR_ERR(dentry);
2228 error = mnt_want_write(nd.path.mnt);
2231 error = security_path_rmdir(&nd.path, dentry);
2234 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2236 mnt_drop_write(nd.path.mnt);
2240 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2247 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
2249 return do_rmdir(AT_FDCWD, pathname);
2252 int vfs_unlink(struct inode *dir, struct dentry *dentry)
2254 int error = may_delete(dir, dentry, 0);
2259 if (!dir->i_op->unlink)
2262 mutex_lock(&dentry->d_inode->i_mutex);
2263 if (d_mountpoint(dentry))
2266 error = security_inode_unlink(dir, dentry);
2268 error = dir->i_op->unlink(dir, dentry);
2270 mutex_unlock(&dentry->d_inode->i_mutex);
2272 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2273 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2274 fsnotify_link_count(dentry->d_inode);
2282 * Make sure that the actual truncation of the file will occur outside its
2283 * directory's i_mutex. Truncate can take a long time if there is a lot of
2284 * writeout happening, and we don't want to prevent access to the directory
2285 * while waiting on the I/O.
2287 static long do_unlinkat(int dfd, const char __user *pathname)
2291 struct dentry *dentry;
2292 struct nameidata nd;
2293 struct inode *inode = NULL;
2295 error = user_path_parent(dfd, pathname, &nd, &name);
2300 if (nd.last_type != LAST_NORM)
2303 nd.flags &= ~LOOKUP_PARENT;
2305 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2306 dentry = lookup_hash(&nd);
2307 error = PTR_ERR(dentry);
2308 if (!IS_ERR(dentry)) {
2309 /* Why not before? Because we want correct error value */
2310 if (nd.last.name[nd.last.len])
2312 inode = dentry->d_inode;
2314 atomic_inc(&inode->i_count);
2315 error = mnt_want_write(nd.path.mnt);
2318 error = security_path_unlink(&nd.path, dentry);
2321 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2323 mnt_drop_write(nd.path.mnt);
2327 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2329 iput(inode); /* truncate the inode here */
2336 error = !dentry->d_inode ? -ENOENT :
2337 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2341 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
2343 if ((flag & ~AT_REMOVEDIR) != 0)
2346 if (flag & AT_REMOVEDIR)
2347 return do_rmdir(dfd, pathname);
2349 return do_unlinkat(dfd, pathname);
2352 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
2354 return do_unlinkat(AT_FDCWD, pathname);
2357 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
2359 int error = may_create(dir, dentry);
2364 if (!dir->i_op->symlink)
2367 error = security_inode_symlink(dir, dentry, oldname);
2371 error = dir->i_op->symlink(dir, dentry, oldname);
2373 fsnotify_create(dir, dentry);
2377 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2378 int, newdfd, const char __user *, newname)
2383 struct dentry *dentry;
2384 struct nameidata nd;
2386 from = getname(oldname);
2388 return PTR_ERR(from);
2390 error = user_path_parent(newdfd, newname, &nd, &to);
2394 dentry = lookup_create(&nd, 0);
2395 error = PTR_ERR(dentry);
2399 error = mnt_want_write(nd.path.mnt);
2402 error = security_path_symlink(&nd.path, dentry, from);
2404 goto out_drop_write;
2405 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2407 mnt_drop_write(nd.path.mnt);
2411 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2419 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
2421 return sys_symlinkat(oldname, AT_FDCWD, newname);
2424 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2426 struct inode *inode = old_dentry->d_inode;
2432 error = may_create(dir, new_dentry);
2436 if (dir->i_sb != inode->i_sb)
2440 * A link to an append-only or immutable file cannot be created.
2442 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2444 if (!dir->i_op->link)
2446 if (S_ISDIR(inode->i_mode))
2449 error = security_inode_link(old_dentry, dir, new_dentry);
2453 mutex_lock(&inode->i_mutex);
2454 error = dir->i_op->link(old_dentry, dir, new_dentry);
2455 mutex_unlock(&inode->i_mutex);
2457 fsnotify_link(dir, inode, new_dentry);
2462 * Hardlinks are often used in delicate situations. We avoid
2463 * security-related surprises by not following symlinks on the
2466 * We don't follow them on the oldname either to be compatible
2467 * with linux 2.0, and to avoid hard-linking to directories
2468 * and other special files. --ADM
2470 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2471 int, newdfd, const char __user *, newname, int, flags)
2473 struct dentry *new_dentry;
2474 struct nameidata nd;
2475 struct path old_path;
2479 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2482 error = user_path_at(olddfd, oldname,
2483 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2488 error = user_path_parent(newdfd, newname, &nd, &to);
2492 if (old_path.mnt != nd.path.mnt)
2494 new_dentry = lookup_create(&nd, 0);
2495 error = PTR_ERR(new_dentry);
2496 if (IS_ERR(new_dentry))
2498 error = mnt_want_write(nd.path.mnt);
2501 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2503 goto out_drop_write;
2504 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2506 mnt_drop_write(nd.path.mnt);
2510 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2515 path_put(&old_path);
2520 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
2522 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
2526 * The worst of all namespace operations - renaming directory. "Perverted"
2527 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2529 * a) we can get into loop creation. Check is done in is_subdir().
2530 * b) race potential - two innocent renames can create a loop together.
2531 * That's where 4.4 screws up. Current fix: serialization on
2532 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
2534 * c) we have to lock _three_ objects - parents and victim (if it exists).
2535 * And that - after we got ->i_mutex on parents (until then we don't know
2536 * whether the target exists). Solution: try to be smart with locking
2537 * order for inodes. We rely on the fact that tree topology may change
2538 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
2539 * move will be locked. Thus we can rank directories by the tree
2540 * (ancestors first) and rank all non-directories after them.
2541 * That works since everybody except rename does "lock parent, lookup,
2542 * lock child" and rename is under ->s_vfs_rename_mutex.
2543 * HOWEVER, it relies on the assumption that any object with ->lookup()
2544 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2545 * we'd better make sure that there's no link(2) for them.
2546 * d) some filesystems don't support opened-but-unlinked directories,
2547 * either because of layout or because they are not ready to deal with
2548 * all cases correctly. The latter will be fixed (taking this sort of
2549 * stuff into VFS), but the former is not going away. Solution: the same
2550 * trick as in rmdir().
2551 * e) conversion from fhandle to dentry may come in the wrong moment - when
2552 * we are removing the target. Solution: we will have to grab ->i_mutex
2553 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2554 * ->i_mutex on parents, which works but leads to some truely excessive
2557 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2558 struct inode *new_dir, struct dentry *new_dentry)
2561 struct inode *target;
2564 * If we are going to change the parent - check write permissions,
2565 * we'll need to flip '..'.
2567 if (new_dir != old_dir) {
2568 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
2573 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2577 target = new_dentry->d_inode;
2579 mutex_lock(&target->i_mutex);
2580 dentry_unhash(new_dentry);
2582 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2585 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2588 target->i_flags |= S_DEAD;
2589 mutex_unlock(&target->i_mutex);
2590 if (d_unhashed(new_dentry))
2591 d_rehash(new_dentry);
2595 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2596 d_move(old_dentry,new_dentry);
2600 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2601 struct inode *new_dir, struct dentry *new_dentry)
2603 struct inode *target;
2606 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2611 target = new_dentry->d_inode;
2613 mutex_lock(&target->i_mutex);
2614 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2617 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2619 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2620 d_move(old_dentry, new_dentry);
2623 mutex_unlock(&target->i_mutex);
2628 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2629 struct inode *new_dir, struct dentry *new_dentry)
2632 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2633 const char *old_name;
2635 if (old_dentry->d_inode == new_dentry->d_inode)
2638 error = may_delete(old_dir, old_dentry, is_dir);
2642 if (!new_dentry->d_inode)
2643 error = may_create(new_dir, new_dentry);
2645 error = may_delete(new_dir, new_dentry, is_dir);
2649 if (!old_dir->i_op->rename)
2652 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2655 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2657 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2659 const char *new_name = old_dentry->d_name.name;
2660 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
2661 new_dentry->d_inode, old_dentry);
2663 fsnotify_oldname_free(old_name);
2668 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
2669 int, newdfd, const char __user *, newname)
2671 struct dentry *old_dir, *new_dir;
2672 struct dentry *old_dentry, *new_dentry;
2673 struct dentry *trap;
2674 struct nameidata oldnd, newnd;
2679 error = user_path_parent(olddfd, oldname, &oldnd, &from);
2683 error = user_path_parent(newdfd, newname, &newnd, &to);
2688 if (oldnd.path.mnt != newnd.path.mnt)
2691 old_dir = oldnd.path.dentry;
2693 if (oldnd.last_type != LAST_NORM)
2696 new_dir = newnd.path.dentry;
2697 if (newnd.last_type != LAST_NORM)
2700 oldnd.flags &= ~LOOKUP_PARENT;
2701 newnd.flags &= ~LOOKUP_PARENT;
2702 newnd.flags |= LOOKUP_RENAME_TARGET;
2704 trap = lock_rename(new_dir, old_dir);
2706 old_dentry = lookup_hash(&oldnd);
2707 error = PTR_ERR(old_dentry);
2708 if (IS_ERR(old_dentry))
2710 /* source must exist */
2712 if (!old_dentry->d_inode)
2714 /* unless the source is a directory trailing slashes give -ENOTDIR */
2715 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2717 if (oldnd.last.name[oldnd.last.len])
2719 if (newnd.last.name[newnd.last.len])
2722 /* source should not be ancestor of target */
2724 if (old_dentry == trap)
2726 new_dentry = lookup_hash(&newnd);
2727 error = PTR_ERR(new_dentry);
2728 if (IS_ERR(new_dentry))
2730 /* target should not be an ancestor of source */
2732 if (new_dentry == trap)
2735 error = mnt_want_write(oldnd.path.mnt);
2738 error = security_path_rename(&oldnd.path, old_dentry,
2739 &newnd.path, new_dentry);
2742 error = vfs_rename(old_dir->d_inode, old_dentry,
2743 new_dir->d_inode, new_dentry);
2745 mnt_drop_write(oldnd.path.mnt);
2751 unlock_rename(new_dir, old_dir);
2753 path_put(&newnd.path);
2756 path_put(&oldnd.path);
2762 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
2764 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
2767 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2771 len = PTR_ERR(link);
2776 if (len > (unsigned) buflen)
2778 if (copy_to_user(buffer, link, len))
2785 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2786 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2787 * using) it for any given inode is up to filesystem.
2789 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2791 struct nameidata nd;
2796 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2798 return PTR_ERR(cookie);
2800 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2801 if (dentry->d_inode->i_op->put_link)
2802 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2806 int vfs_follow_link(struct nameidata *nd, const char *link)
2808 return __vfs_follow_link(nd, link);
2811 /* get the link contents into pagecache */
2812 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2816 struct address_space *mapping = dentry->d_inode->i_mapping;
2817 page = read_mapping_page(mapping, 0, NULL);
2822 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
2826 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2828 struct page *page = NULL;
2829 char *s = page_getlink(dentry, &page);
2830 int res = vfs_readlink(dentry,buffer,buflen,s);
2833 page_cache_release(page);
2838 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2840 struct page *page = NULL;
2841 nd_set_link(nd, page_getlink(dentry, &page));
2845 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
2847 struct page *page = cookie;
2851 page_cache_release(page);
2856 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
2858 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
2860 struct address_space *mapping = inode->i_mapping;
2865 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
2867 flags |= AOP_FLAG_NOFS;
2870 err = pagecache_write_begin(NULL, mapping, 0, len-1,
2871 flags, &page, &fsdata);
2875 kaddr = kmap_atomic(page, KM_USER0);
2876 memcpy(kaddr, symname, len-1);
2877 kunmap_atomic(kaddr, KM_USER0);
2879 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2886 mark_inode_dirty(inode);
2892 int page_symlink(struct inode *inode, const char *symname, int len)
2894 return __page_symlink(inode, symname, len,
2895 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
2898 const struct inode_operations page_symlink_inode_operations = {
2899 .readlink = generic_readlink,
2900 .follow_link = page_follow_link_light,
2901 .put_link = page_put_link,
2904 EXPORT_SYMBOL(user_path_at);
2905 EXPORT_SYMBOL(follow_down);
2906 EXPORT_SYMBOL(follow_up);
2907 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2908 EXPORT_SYMBOL(getname);
2909 EXPORT_SYMBOL(lock_rename);
2910 EXPORT_SYMBOL(lookup_one_len);
2911 EXPORT_SYMBOL(page_follow_link_light);
2912 EXPORT_SYMBOL(page_put_link);
2913 EXPORT_SYMBOL(page_readlink);
2914 EXPORT_SYMBOL(__page_symlink);
2915 EXPORT_SYMBOL(page_symlink);
2916 EXPORT_SYMBOL(page_symlink_inode_operations);
2917 EXPORT_SYMBOL(path_lookup);
2918 EXPORT_SYMBOL(kern_path);
2919 EXPORT_SYMBOL(vfs_path_lookup);
2920 EXPORT_SYMBOL(inode_permission);
2921 EXPORT_SYMBOL(file_permission);
2922 EXPORT_SYMBOL(unlock_rename);
2923 EXPORT_SYMBOL(vfs_create);
2924 EXPORT_SYMBOL(vfs_follow_link);
2925 EXPORT_SYMBOL(vfs_link);
2926 EXPORT_SYMBOL(vfs_mkdir);
2927 EXPORT_SYMBOL(vfs_mknod);
2928 EXPORT_SYMBOL(generic_permission);
2929 EXPORT_SYMBOL(vfs_readlink);
2930 EXPORT_SYMBOL(vfs_rename);
2931 EXPORT_SYMBOL(vfs_rmdir);
2932 EXPORT_SYMBOL(vfs_symlink);
2933 EXPORT_SYMBOL(vfs_unlink);
2934 EXPORT_SYMBOL(dentry_unhash);
2935 EXPORT_SYMBOL(generic_readlink);