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/quotaops.h>
23 #include <linux/pagemap.h>
24 #include <linux/fsnotify.h>
25 #include <linux/personality.h>
26 #include <linux/security.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/namei.h>
34 #include <asm/namei.h>
35 #include <asm/uaccess.h>
37 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
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);
171 * generic_permission - check for access rights on a Posix-like filesystem
172 * @inode: inode to check access rights for
173 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
174 * @check_acl: optional callback to check for Posix ACLs
176 * Used to check for read/write/execute permissions on a file.
177 * We use "fsuid" for this, letting us set arbitrary permissions
178 * for filesystem access without changing the "normal" uids which
179 * are used for other things..
181 int generic_permission(struct inode *inode, int mask,
182 int (*check_acl)(struct inode *inode, int mask))
184 umode_t mode = inode->i_mode;
186 if (current->fsuid == inode->i_uid)
189 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
190 int error = check_acl(inode, mask);
191 if (error == -EACCES)
192 goto check_capabilities;
193 else if (error != -EAGAIN)
197 if (in_group_p(inode->i_gid))
202 * If the DACs are ok we don't need any capability check.
204 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
209 * Read/write DACs are always overridable.
210 * Executable DACs are overridable if at least one exec bit is set.
212 if (!(mask & MAY_EXEC) ||
213 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
214 if (capable(CAP_DAC_OVERRIDE))
218 * Searching includes executable on directories, else just read.
220 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
221 if (capable(CAP_DAC_READ_SEARCH))
227 int permission(struct inode *inode, int mask, struct nameidata *nd)
229 umode_t mode = inode->i_mode;
232 if (mask & MAY_WRITE) {
235 * Nobody gets write access to a read-only fs.
237 if (IS_RDONLY(inode) &&
238 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
242 * Nobody gets write access to an immutable file.
244 if (IS_IMMUTABLE(inode))
250 * MAY_EXEC on regular files requires special handling: We override
251 * filesystem execute permissions if the mode bits aren't set or
252 * the fs is mounted with the "noexec" flag.
254 if ((mask & MAY_EXEC) && S_ISREG(mode) && (!(mode & S_IXUGO) ||
255 (nd && nd->mnt && (nd->mnt->mnt_flags & MNT_NOEXEC))))
258 /* Ordinary permission routines do not understand MAY_APPEND. */
259 submask = mask & ~MAY_APPEND;
260 if (inode->i_op && inode->i_op->permission)
261 retval = inode->i_op->permission(inode, submask, nd);
263 retval = generic_permission(inode, submask, NULL);
267 return security_inode_permission(inode, mask, nd);
271 * vfs_permission - check for access rights to a given path
272 * @nd: lookup result that describes the path
273 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
275 * Used to check for read/write/execute permissions on a path.
276 * We use "fsuid" for this, letting us set arbitrary permissions
277 * for filesystem access without changing the "normal" uids which
278 * are used for other things.
280 int vfs_permission(struct nameidata *nd, int mask)
282 return permission(nd->dentry->d_inode, mask, nd);
286 * file_permission - check for additional access rights to a given file
287 * @file: file to check access rights for
288 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
290 * Used to check for read/write/execute permissions on an already opened
294 * Do not use this function in new code. All access checks should
295 * be done using vfs_permission().
297 int file_permission(struct file *file, int mask)
299 return permission(file->f_path.dentry->d_inode, mask, NULL);
303 * get_write_access() gets write permission for a file.
304 * put_write_access() releases this write permission.
305 * This is used for regular files.
306 * We cannot support write (and maybe mmap read-write shared) accesses and
307 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
308 * can have the following values:
309 * 0: no writers, no VM_DENYWRITE mappings
310 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
311 * > 0: (i_writecount) users are writing to the file.
313 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
314 * except for the cases where we don't hold i_writecount yet. Then we need to
315 * use {get,deny}_write_access() - these functions check the sign and refuse
316 * to do the change if sign is wrong. Exclusion between them is provided by
317 * the inode->i_lock spinlock.
320 int get_write_access(struct inode * inode)
322 spin_lock(&inode->i_lock);
323 if (atomic_read(&inode->i_writecount) < 0) {
324 spin_unlock(&inode->i_lock);
327 atomic_inc(&inode->i_writecount);
328 spin_unlock(&inode->i_lock);
333 int deny_write_access(struct file * file)
335 struct inode *inode = file->f_path.dentry->d_inode;
337 spin_lock(&inode->i_lock);
338 if (atomic_read(&inode->i_writecount) > 0) {
339 spin_unlock(&inode->i_lock);
342 atomic_dec(&inode->i_writecount);
343 spin_unlock(&inode->i_lock);
348 void path_release(struct nameidata *nd)
355 * umount() mustn't call path_release()/mntput() as that would clear
358 void path_release_on_umount(struct nameidata *nd)
361 mntput_no_expire(nd->mnt);
365 * release_open_intent - free up open intent resources
366 * @nd: pointer to nameidata
368 void release_open_intent(struct nameidata *nd)
370 if (nd->intent.open.file->f_path.dentry == NULL)
371 put_filp(nd->intent.open.file);
373 fput(nd->intent.open.file);
376 static inline struct dentry *
377 do_revalidate(struct dentry *dentry, struct nameidata *nd)
379 int status = dentry->d_op->d_revalidate(dentry, nd);
380 if (unlikely(status <= 0)) {
382 * The dentry failed validation.
383 * If d_revalidate returned 0 attempt to invalidate
384 * the dentry otherwise d_revalidate is asking us
385 * to return a fail status.
388 if (!d_invalidate(dentry)) {
394 dentry = ERR_PTR(status);
401 * Internal lookup() using the new generic dcache.
404 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
406 struct dentry * dentry = __d_lookup(parent, name);
408 /* lockess __d_lookup may fail due to concurrent d_move()
409 * in some unrelated directory, so try with d_lookup
412 dentry = d_lookup(parent, name);
414 if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
415 dentry = do_revalidate(dentry, nd);
421 * Short-cut version of permission(), for calling by
422 * path_walk(), when dcache lock is held. Combines parts
423 * of permission() and generic_permission(), and tests ONLY for
424 * MAY_EXEC permission.
426 * If appropriate, check DAC only. If not appropriate, or
427 * short-cut DAC fails, then call permission() to do more
428 * complete permission check.
430 static int exec_permission_lite(struct inode *inode,
431 struct nameidata *nd)
433 umode_t mode = inode->i_mode;
435 if (inode->i_op && inode->i_op->permission)
438 if (current->fsuid == inode->i_uid)
440 else if (in_group_p(inode->i_gid))
446 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
449 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
452 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
457 return security_inode_permission(inode, MAY_EXEC, nd);
461 * This is called when everything else fails, and we actually have
462 * to go to the low-level filesystem to find out what we should do..
464 * We get the directory semaphore, and after getting that we also
465 * make sure that nobody added the entry to the dcache in the meantime..
468 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
470 struct dentry * result;
471 struct inode *dir = parent->d_inode;
473 mutex_lock(&dir->i_mutex);
475 * First re-do the cached lookup just in case it was created
476 * while we waited for the directory semaphore..
478 * FIXME! This could use version numbering or similar to
479 * avoid unnecessary cache lookups.
481 * The "dcache_lock" is purely to protect the RCU list walker
482 * from concurrent renames at this point (we mustn't get false
483 * negatives from the RCU list walk here, unlike the optimistic
486 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
488 result = d_lookup(parent, name);
490 struct dentry * dentry = d_alloc(parent, name);
491 result = ERR_PTR(-ENOMEM);
493 result = dir->i_op->lookup(dir, dentry, nd);
499 mutex_unlock(&dir->i_mutex);
504 * Uhhuh! Nasty case: the cache was re-populated while
505 * we waited on the semaphore. Need to revalidate.
507 mutex_unlock(&dir->i_mutex);
508 if (result->d_op && result->d_op->d_revalidate) {
509 result = do_revalidate(result, nd);
511 result = ERR_PTR(-ENOENT);
516 static int __emul_lookup_dentry(const char *, struct nameidata *);
519 static __always_inline int
520 walk_init_root(const char *name, struct nameidata *nd)
522 struct fs_struct *fs = current->fs;
524 read_lock(&fs->lock);
525 if (fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
526 nd->mnt = mntget(fs->altrootmnt);
527 nd->dentry = dget(fs->altroot);
528 read_unlock(&fs->lock);
529 if (__emul_lookup_dentry(name,nd))
531 read_lock(&fs->lock);
533 nd->mnt = mntget(fs->rootmnt);
534 nd->dentry = dget(fs->root);
535 read_unlock(&fs->lock);
539 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
548 if (!walk_init_root(link, nd))
549 /* weird __emul_prefix() stuff did it */
552 res = link_path_walk(link, nd);
554 if (nd->depth || res || nd->last_type!=LAST_NORM)
557 * If it is an iterative symlinks resolution in open_namei() we
558 * have to copy the last component. And all that crap because of
559 * bloody create() on broken symlinks. Furrfu...
562 if (unlikely(!name)) {
566 strcpy(name, nd->last.name);
567 nd->last.name = name;
571 return PTR_ERR(link);
574 static inline void dput_path(struct path *path, struct nameidata *nd)
577 if (path->mnt != nd->mnt)
581 static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
584 if (nd->mnt != path->mnt)
587 nd->dentry = path->dentry;
590 static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
594 struct dentry *dentry = path->dentry;
596 touch_atime(path->mnt, dentry);
597 nd_set_link(nd, NULL);
599 if (path->mnt != nd->mnt) {
600 path_to_nameidata(path, nd);
604 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
605 error = PTR_ERR(cookie);
606 if (!IS_ERR(cookie)) {
607 char *s = nd_get_link(nd);
610 error = __vfs_follow_link(nd, s);
611 if (dentry->d_inode->i_op->put_link)
612 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
621 * This limits recursive symlink follows to 8, while
622 * limiting consecutive symlinks to 40.
624 * Without that kind of total limit, nasty chains of consecutive
625 * symlinks can cause almost arbitrarily long lookups.
627 static inline int do_follow_link(struct path *path, struct nameidata *nd)
630 if (current->link_count >= MAX_NESTED_LINKS)
632 if (current->total_link_count >= 40)
634 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
636 err = security_inode_follow_link(path->dentry, nd);
639 current->link_count++;
640 current->total_link_count++;
642 err = __do_follow_link(path, nd);
643 current->link_count--;
652 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
654 struct vfsmount *parent;
655 struct dentry *mountpoint;
656 spin_lock(&vfsmount_lock);
657 parent=(*mnt)->mnt_parent;
658 if (parent == *mnt) {
659 spin_unlock(&vfsmount_lock);
663 mountpoint=dget((*mnt)->mnt_mountpoint);
664 spin_unlock(&vfsmount_lock);
666 *dentry = mountpoint;
672 /* no need for dcache_lock, as serialization is taken care in
675 static int __follow_mount(struct path *path)
678 while (d_mountpoint(path->dentry)) {
679 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
686 path->dentry = dget(mounted->mnt_root);
692 static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
694 while (d_mountpoint(*dentry)) {
695 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
701 *dentry = dget(mounted->mnt_root);
705 /* no need for dcache_lock, as serialization is taken care in
708 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
710 struct vfsmount *mounted;
712 mounted = lookup_mnt(*mnt, *dentry);
717 *dentry = dget(mounted->mnt_root);
723 static __always_inline void follow_dotdot(struct nameidata *nd)
725 struct fs_struct *fs = current->fs;
728 struct vfsmount *parent;
729 struct dentry *old = nd->dentry;
731 read_lock(&fs->lock);
732 if (nd->dentry == fs->root &&
733 nd->mnt == fs->rootmnt) {
734 read_unlock(&fs->lock);
737 read_unlock(&fs->lock);
738 spin_lock(&dcache_lock);
739 if (nd->dentry != nd->mnt->mnt_root) {
740 nd->dentry = dget(nd->dentry->d_parent);
741 spin_unlock(&dcache_lock);
745 spin_unlock(&dcache_lock);
746 spin_lock(&vfsmount_lock);
747 parent = nd->mnt->mnt_parent;
748 if (parent == nd->mnt) {
749 spin_unlock(&vfsmount_lock);
753 nd->dentry = dget(nd->mnt->mnt_mountpoint);
754 spin_unlock(&vfsmount_lock);
759 follow_mount(&nd->mnt, &nd->dentry);
763 * It's more convoluted than I'd like it to be, but... it's still fairly
764 * small and for now I'd prefer to have fast path as straight as possible.
765 * It _is_ time-critical.
767 static int do_lookup(struct nameidata *nd, struct qstr *name,
770 struct vfsmount *mnt = nd->mnt;
771 struct dentry *dentry = __d_lookup(nd->dentry, name);
775 if (dentry->d_op && dentry->d_op->d_revalidate)
776 goto need_revalidate;
779 path->dentry = dentry;
780 __follow_mount(path);
784 dentry = real_lookup(nd->dentry, name, nd);
790 dentry = do_revalidate(dentry, nd);
798 return PTR_ERR(dentry);
803 * This is the basic name resolution function, turning a pathname into
804 * the final dentry. We expect 'base' to be positive and a directory.
806 * Returns 0 and nd will have valid dentry and mnt on success.
807 * Returns error and drops reference to input namei data on failure.
809 static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
814 unsigned int lookup_flags = nd->flags;
821 inode = nd->dentry->d_inode;
823 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
825 /* At this point we know we have a real path component. */
831 nd->flags |= LOOKUP_CONTINUE;
832 err = exec_permission_lite(inode, nd);
834 err = vfs_permission(nd, MAY_EXEC);
839 c = *(const unsigned char *)name;
841 hash = init_name_hash();
844 hash = partial_name_hash(c, hash);
845 c = *(const unsigned char *)name;
846 } while (c && (c != '/'));
847 this.len = name - (const char *) this.name;
848 this.hash = end_name_hash(hash);
850 /* remove trailing slashes? */
853 while (*++name == '/');
855 goto last_with_slashes;
858 * "." and ".." are special - ".." especially so because it has
859 * to be able to know about the current root directory and
860 * parent relationships.
862 if (this.name[0] == '.') switch (this.len) {
866 if (this.name[1] != '.')
869 inode = nd->dentry->d_inode;
875 * See if the low-level filesystem might want
876 * to use its own hash..
878 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
879 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
883 /* This does the actual lookups.. */
884 err = do_lookup(nd, &this, &next);
889 inode = next.dentry->d_inode;
896 if (inode->i_op->follow_link) {
897 err = do_follow_link(&next, nd);
901 inode = nd->dentry->d_inode;
908 path_to_nameidata(&next, nd);
910 if (!inode->i_op->lookup)
913 /* here ends the main loop */
916 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
918 /* Clear LOOKUP_CONTINUE iff it was previously unset */
919 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
920 if (lookup_flags & LOOKUP_PARENT)
922 if (this.name[0] == '.') switch (this.len) {
926 if (this.name[1] != '.')
929 inode = nd->dentry->d_inode;
934 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
935 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
939 err = do_lookup(nd, &this, &next);
942 inode = next.dentry->d_inode;
943 if ((lookup_flags & LOOKUP_FOLLOW)
944 && inode && inode->i_op && inode->i_op->follow_link) {
945 err = do_follow_link(&next, nd);
948 inode = nd->dentry->d_inode;
950 path_to_nameidata(&next, nd);
954 if (lookup_flags & LOOKUP_DIRECTORY) {
956 if (!inode->i_op || !inode->i_op->lookup)
962 nd->last_type = LAST_NORM;
963 if (this.name[0] != '.')
966 nd->last_type = LAST_DOT;
967 else if (this.len == 2 && this.name[1] == '.')
968 nd->last_type = LAST_DOTDOT;
973 * We bypassed the ordinary revalidation routines.
974 * We may need to check the cached dentry for staleness.
976 if (nd->dentry && nd->dentry->d_sb &&
977 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
979 /* Note: we do not d_invalidate() */
980 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
986 dput_path(&next, nd);
995 * Wrapper to retry pathname resolution whenever the underlying
996 * file system returns an ESTALE.
998 * Retry the whole path once, forcing real lookup requests
999 * instead of relying on the dcache.
1001 int fastcall link_path_walk(const char *name, struct nameidata *nd)
1003 struct nameidata save = *nd;
1006 /* make sure the stuff we saved doesn't go away */
1010 result = __link_path_walk(name, nd);
1011 if (result == -ESTALE) {
1015 nd->flags |= LOOKUP_REVAL;
1016 result = __link_path_walk(name, nd);
1025 int fastcall path_walk(const char * name, struct nameidata *nd)
1027 current->total_link_count = 0;
1028 return link_path_walk(name, nd);
1032 * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
1033 * everything is done. Returns 0 and drops input nd, if lookup failed;
1035 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
1037 if (path_walk(name, nd))
1038 return 0; /* something went wrong... */
1040 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
1041 struct dentry *old_dentry = nd->dentry;
1042 struct vfsmount *old_mnt = nd->mnt;
1043 struct qstr last = nd->last;
1044 int last_type = nd->last_type;
1045 struct fs_struct *fs = current->fs;
1048 * NAME was not found in alternate root or it's a directory.
1049 * Try to find it in the normal root:
1051 nd->last_type = LAST_ROOT;
1052 read_lock(&fs->lock);
1053 nd->mnt = mntget(fs->rootmnt);
1054 nd->dentry = dget(fs->root);
1055 read_unlock(&fs->lock);
1056 if (path_walk(name, nd) == 0) {
1057 if (nd->dentry->d_inode) {
1064 nd->dentry = old_dentry;
1067 nd->last_type = last_type;
1072 void set_fs_altroot(void)
1074 char *emul = __emul_prefix();
1075 struct nameidata nd;
1076 struct vfsmount *mnt = NULL, *oldmnt;
1077 struct dentry *dentry = NULL, *olddentry;
1079 struct fs_struct *fs = current->fs;
1083 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
1089 write_lock(&fs->lock);
1090 oldmnt = fs->altrootmnt;
1091 olddentry = fs->altroot;
1092 fs->altrootmnt = mnt;
1093 fs->altroot = dentry;
1094 write_unlock(&fs->lock);
1101 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1102 static int fastcall do_path_lookup(int dfd, const char *name,
1103 unsigned int flags, struct nameidata *nd)
1108 struct fs_struct *fs = current->fs;
1110 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1115 read_lock(&fs->lock);
1116 if (fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
1117 nd->mnt = mntget(fs->altrootmnt);
1118 nd->dentry = dget(fs->altroot);
1119 read_unlock(&fs->lock);
1120 if (__emul_lookup_dentry(name,nd))
1121 goto out; /* found in altroot */
1122 read_lock(&fs->lock);
1124 nd->mnt = mntget(fs->rootmnt);
1125 nd->dentry = dget(fs->root);
1126 read_unlock(&fs->lock);
1127 } else if (dfd == AT_FDCWD) {
1128 read_lock(&fs->lock);
1129 nd->mnt = mntget(fs->pwdmnt);
1130 nd->dentry = dget(fs->pwd);
1131 read_unlock(&fs->lock);
1133 struct dentry *dentry;
1135 file = fget_light(dfd, &fput_needed);
1140 dentry = file->f_path.dentry;
1143 if (!S_ISDIR(dentry->d_inode->i_mode))
1146 retval = file_permission(file, MAY_EXEC);
1150 nd->mnt = mntget(file->f_path.mnt);
1151 nd->dentry = dget(dentry);
1153 fput_light(file, fput_needed);
1156 retval = path_walk(name, nd);
1158 if (unlikely(!retval && !audit_dummy_context() && nd->dentry &&
1159 nd->dentry->d_inode))
1160 audit_inode(name, nd->dentry->d_inode);
1165 fput_light(file, fput_needed);
1169 int fastcall path_lookup(const char *name, unsigned int flags,
1170 struct nameidata *nd)
1172 return do_path_lookup(AT_FDCWD, name, flags, nd);
1175 static int __path_lookup_intent_open(int dfd, const char *name,
1176 unsigned int lookup_flags, struct nameidata *nd,
1177 int open_flags, int create_mode)
1179 struct file *filp = get_empty_filp();
1184 nd->intent.open.file = filp;
1185 nd->intent.open.flags = open_flags;
1186 nd->intent.open.create_mode = create_mode;
1187 err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
1188 if (IS_ERR(nd->intent.open.file)) {
1190 err = PTR_ERR(nd->intent.open.file);
1193 } else if (err != 0)
1194 release_open_intent(nd);
1199 * path_lookup_open - lookup a file path with open intent
1200 * @dfd: the directory to use as base, or AT_FDCWD
1201 * @name: pointer to file name
1202 * @lookup_flags: lookup intent flags
1203 * @nd: pointer to nameidata
1204 * @open_flags: open intent flags
1206 int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
1207 struct nameidata *nd, int open_flags)
1209 return __path_lookup_intent_open(dfd, name, lookup_flags, nd,
1214 * path_lookup_create - lookup a file path with open + create intent
1215 * @dfd: the directory to use as base, or AT_FDCWD
1216 * @name: pointer to file name
1217 * @lookup_flags: lookup intent flags
1218 * @nd: pointer to nameidata
1219 * @open_flags: open intent flags
1220 * @create_mode: create intent flags
1222 static int path_lookup_create(int dfd, const char *name,
1223 unsigned int lookup_flags, struct nameidata *nd,
1224 int open_flags, int create_mode)
1226 return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE,
1227 nd, open_flags, create_mode);
1230 int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
1231 struct nameidata *nd, int open_flags)
1233 char *tmp = getname(name);
1234 int err = PTR_ERR(tmp);
1237 err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
1243 static inline struct dentry *__lookup_hash_kern(struct qstr *name, struct dentry *base, struct nameidata *nd)
1245 struct dentry *dentry;
1246 struct inode *inode;
1249 inode = base->d_inode;
1252 * See if the low-level filesystem might want
1253 * to use its own hash..
1255 if (base->d_op && base->d_op->d_hash) {
1256 err = base->d_op->d_hash(base, name);
1257 dentry = ERR_PTR(err);
1262 dentry = cached_lookup(base, name, nd);
1264 struct dentry *new = d_alloc(base, name);
1265 dentry = ERR_PTR(-ENOMEM);
1268 dentry = inode->i_op->lookup(inode, new, nd);
1279 * Restricted form of lookup. Doesn't follow links, single-component only,
1280 * needs parent already locked. Doesn't follow mounts.
1283 static inline struct dentry * __lookup_hash(struct qstr *name, struct dentry *base, struct nameidata *nd)
1285 struct dentry *dentry;
1286 struct inode *inode;
1289 inode = base->d_inode;
1291 err = permission(inode, MAY_EXEC, nd);
1292 dentry = ERR_PTR(err);
1296 dentry = __lookup_hash_kern(name, base, nd);
1301 static struct dentry *lookup_hash(struct nameidata *nd)
1303 return __lookup_hash(&nd->last, nd->dentry, nd);
1307 static inline int __lookup_one_len(const char *name, struct qstr *this, struct dentry *base, int len)
1317 hash = init_name_hash();
1319 c = *(const unsigned char *)name++;
1320 if (c == '/' || c == '\0')
1322 hash = partial_name_hash(c, hash);
1324 this->hash = end_name_hash(hash);
1328 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1333 err = __lookup_one_len(name, &this, base, len);
1335 return ERR_PTR(err);
1336 return __lookup_hash(&this, base, NULL);
1339 struct dentry *lookup_one_len_kern(const char *name, struct dentry *base, int len)
1344 err = __lookup_one_len(name, &this, base, len);
1346 return ERR_PTR(err);
1347 return __lookup_hash_kern(&this, base, NULL);
1350 int fastcall __user_walk_fd(int dfd, const char __user *name, unsigned flags,
1351 struct nameidata *nd)
1353 char *tmp = getname(name);
1354 int err = PTR_ERR(tmp);
1357 err = do_path_lookup(dfd, tmp, flags, nd);
1363 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1365 return __user_walk_fd(AT_FDCWD, name, flags, nd);
1369 * It's inline, so penalty for filesystems that don't use sticky bit is
1372 static inline int check_sticky(struct inode *dir, struct inode *inode)
1374 if (!(dir->i_mode & S_ISVTX))
1376 if (inode->i_uid == current->fsuid)
1378 if (dir->i_uid == current->fsuid)
1380 return !capable(CAP_FOWNER);
1384 * Check whether we can remove a link victim from directory dir, check
1385 * whether the type of victim is right.
1386 * 1. We can't do it if dir is read-only (done in permission())
1387 * 2. We should have write and exec permissions on dir
1388 * 3. We can't remove anything from append-only dir
1389 * 4. We can't do anything with immutable dir (done in permission())
1390 * 5. If the sticky bit on dir is set we should either
1391 * a. be owner of dir, or
1392 * b. be owner of victim, or
1393 * c. have CAP_FOWNER capability
1394 * 6. If the victim is append-only or immutable we can't do antyhing with
1395 * links pointing to it.
1396 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1397 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1398 * 9. We can't remove a root or mountpoint.
1399 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1400 * nfs_async_unlink().
1402 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1406 if (!victim->d_inode)
1409 BUG_ON(victim->d_parent->d_inode != dir);
1410 audit_inode_child(victim->d_name.name, victim->d_inode, dir);
1412 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1417 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1418 IS_IMMUTABLE(victim->d_inode))
1421 if (!S_ISDIR(victim->d_inode->i_mode))
1423 if (IS_ROOT(victim))
1425 } else if (S_ISDIR(victim->d_inode->i_mode))
1427 if (IS_DEADDIR(dir))
1429 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1434 /* Check whether we can create an object with dentry child in directory
1436 * 1. We can't do it if child already exists (open has special treatment for
1437 * this case, but since we are inlined it's OK)
1438 * 2. We can't do it if dir is read-only (done in permission())
1439 * 3. We should have write and exec permissions on dir
1440 * 4. We can't do it if dir is immutable (done in permission())
1442 static inline int may_create(struct inode *dir, struct dentry *child,
1443 struct nameidata *nd)
1447 if (IS_DEADDIR(dir))
1449 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1453 * O_DIRECTORY translates into forcing a directory lookup.
1455 static inline int lookup_flags(unsigned int f)
1457 unsigned long retval = LOOKUP_FOLLOW;
1460 retval &= ~LOOKUP_FOLLOW;
1462 if (f & O_DIRECTORY)
1463 retval |= LOOKUP_DIRECTORY;
1469 * p1 and p2 should be directories on the same fs.
1471 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1476 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1480 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1482 for (p = p1; p->d_parent != p; p = p->d_parent) {
1483 if (p->d_parent == p2) {
1484 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1485 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1490 for (p = p2; p->d_parent != p; p = p->d_parent) {
1491 if (p->d_parent == p1) {
1492 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1493 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1498 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1499 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1503 void unlock_rename(struct dentry *p1, struct dentry *p2)
1505 mutex_unlock(&p1->d_inode->i_mutex);
1507 mutex_unlock(&p2->d_inode->i_mutex);
1508 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1512 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1513 struct nameidata *nd)
1515 int error = may_create(dir, dentry, nd);
1520 if (!dir->i_op || !dir->i_op->create)
1521 return -EACCES; /* shouldn't it be ENOSYS? */
1524 error = security_inode_create(dir, dentry, mode);
1528 error = dir->i_op->create(dir, dentry, mode, nd);
1530 fsnotify_create(dir, dentry);
1534 int may_open(struct nameidata *nd, int acc_mode, int flag)
1536 struct dentry *dentry = nd->dentry;
1537 struct inode *inode = dentry->d_inode;
1543 if (S_ISLNK(inode->i_mode))
1546 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1549 error = vfs_permission(nd, acc_mode);
1554 * FIFO's, sockets and device files are special: they don't
1555 * actually live on the filesystem itself, and as such you
1556 * can write to them even if the filesystem is read-only.
1558 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1560 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1561 if (nd->mnt->mnt_flags & MNT_NODEV)
1565 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1568 * An append-only file must be opened in append mode for writing.
1570 if (IS_APPEND(inode)) {
1571 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1577 /* O_NOATIME can only be set by the owner or superuser */
1578 if (flag & O_NOATIME)
1579 if (!is_owner_or_cap(inode))
1583 * Ensure there are no outstanding leases on the file.
1585 error = break_lease(inode, flag);
1589 if (flag & O_TRUNC) {
1590 error = get_write_access(inode);
1595 * Refuse to truncate files with mandatory locks held on them.
1597 error = locks_verify_locked(inode);
1601 error = do_truncate(dentry, 0, ATTR_MTIME|ATTR_CTIME, NULL);
1603 put_write_access(inode);
1607 if (flag & FMODE_WRITE)
1613 static int open_namei_create(struct nameidata *nd, struct path *path,
1617 struct dentry *dir = nd->dentry;
1619 if (!IS_POSIXACL(dir->d_inode))
1620 mode &= ~current->fs->umask;
1621 error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1622 mutex_unlock(&dir->d_inode->i_mutex);
1624 nd->dentry = path->dentry;
1627 /* Don't check for write permission, don't truncate */
1628 return may_open(nd, 0, flag & ~O_TRUNC);
1634 * namei for open - this is in fact almost the whole open-routine.
1636 * Note that the low bits of "flag" aren't the same as in the open
1637 * system call - they are 00 - no permissions needed
1638 * 01 - read permission needed
1639 * 10 - write permission needed
1640 * 11 - read/write permissions needed
1641 * which is a lot more logical, and also allows the "no perm" needed
1642 * for symlinks (where the permissions are checked later).
1645 int open_namei(int dfd, const char *pathname, int flag,
1646 int mode, struct nameidata *nd)
1648 int acc_mode, error;
1653 acc_mode = ACC_MODE(flag);
1655 /* O_TRUNC implies we need access checks for write permissions */
1657 acc_mode |= MAY_WRITE;
1659 /* Allow the LSM permission hook to distinguish append
1660 access from general write access. */
1661 if (flag & O_APPEND)
1662 acc_mode |= MAY_APPEND;
1665 * The simplest case - just a plain lookup.
1667 if (!(flag & O_CREAT)) {
1668 error = path_lookup_open(dfd, pathname, lookup_flags(flag),
1676 * Create - we need to know the parent.
1678 error = path_lookup_create(dfd,pathname,LOOKUP_PARENT,nd,flag,mode);
1683 * We have the parent and last component. First of all, check
1684 * that we are not asked to creat(2) an obvious directory - that
1688 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1692 nd->flags &= ~LOOKUP_PARENT;
1693 mutex_lock(&dir->d_inode->i_mutex);
1694 path.dentry = lookup_hash(nd);
1698 error = PTR_ERR(path.dentry);
1699 if (IS_ERR(path.dentry)) {
1700 mutex_unlock(&dir->d_inode->i_mutex);
1704 if (IS_ERR(nd->intent.open.file)) {
1705 mutex_unlock(&dir->d_inode->i_mutex);
1706 error = PTR_ERR(nd->intent.open.file);
1710 /* Negative dentry, just create the file */
1711 if (!path.dentry->d_inode) {
1712 error = open_namei_create(nd, &path, flag, mode);
1719 * It already exists.
1721 mutex_unlock(&dir->d_inode->i_mutex);
1722 audit_inode(pathname, path.dentry->d_inode);
1728 if (__follow_mount(&path)) {
1730 if (flag & O_NOFOLLOW)
1735 if (!path.dentry->d_inode)
1737 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
1740 path_to_nameidata(&path, nd);
1742 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
1745 error = may_open(nd, acc_mode, flag);
1751 dput_path(&path, nd);
1753 if (!IS_ERR(nd->intent.open.file))
1754 release_open_intent(nd);
1760 if (flag & O_NOFOLLOW)
1763 * This is subtle. Instead of calling do_follow_link() we do the
1764 * thing by hands. The reason is that this way we have zero link_count
1765 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1766 * After that we have the parent and last component, i.e.
1767 * we are in the same situation as after the first path_walk().
1768 * Well, almost - if the last component is normal we get its copy
1769 * stored in nd->last.name and we will have to putname() it when we
1770 * are done. Procfs-like symlinks just set LAST_BIND.
1772 nd->flags |= LOOKUP_PARENT;
1773 error = security_inode_follow_link(path.dentry, nd);
1776 error = __do_follow_link(&path, nd);
1778 /* Does someone understand code flow here? Or it is only
1779 * me so stupid? Anathema to whoever designed this non-sense
1780 * with "intent.open".
1782 release_open_intent(nd);
1785 nd->flags &= ~LOOKUP_PARENT;
1786 if (nd->last_type == LAST_BIND)
1789 if (nd->last_type != LAST_NORM)
1791 if (nd->last.name[nd->last.len]) {
1792 __putname(nd->last.name);
1797 __putname(nd->last.name);
1801 mutex_lock(&dir->d_inode->i_mutex);
1802 path.dentry = lookup_hash(nd);
1804 __putname(nd->last.name);
1809 * lookup_create - lookup a dentry, creating it if it doesn't exist
1810 * @nd: nameidata info
1811 * @is_dir: directory flag
1813 * Simple function to lookup and return a dentry and create it
1814 * if it doesn't exist. Is SMP-safe.
1816 * Returns with nd->dentry->d_inode->i_mutex locked.
1818 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1820 struct dentry *dentry = ERR_PTR(-EEXIST);
1822 mutex_lock_nested(&nd->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
1824 * Yucky last component or no last component at all?
1825 * (foo/., foo/.., /////)
1827 if (nd->last_type != LAST_NORM)
1829 nd->flags &= ~LOOKUP_PARENT;
1830 nd->flags |= LOOKUP_CREATE;
1831 nd->intent.open.flags = O_EXCL;
1834 * Do the final lookup.
1836 dentry = lookup_hash(nd);
1841 * Special case - lookup gave negative, but... we had foo/bar/
1842 * From the vfs_mknod() POV we just have a negative dentry -
1843 * all is fine. Let's be bastards - you had / on the end, you've
1844 * been asking for (non-existent) directory. -ENOENT for you.
1846 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1851 dentry = ERR_PTR(-ENOENT);
1855 EXPORT_SYMBOL_GPL(lookup_create);
1857 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1859 int error = may_create(dir, dentry, NULL);
1864 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1867 if (!dir->i_op || !dir->i_op->mknod)
1870 error = security_inode_mknod(dir, dentry, mode, dev);
1875 error = dir->i_op->mknod(dir, dentry, mode, dev);
1877 fsnotify_create(dir, dentry);
1881 asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
1886 struct dentry * dentry;
1887 struct nameidata nd;
1891 tmp = getname(filename);
1893 return PTR_ERR(tmp);
1895 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
1898 dentry = lookup_create(&nd, 0);
1899 error = PTR_ERR(dentry);
1901 if (!IS_POSIXACL(nd.dentry->d_inode))
1902 mode &= ~current->fs->umask;
1903 if (!IS_ERR(dentry)) {
1904 switch (mode & S_IFMT) {
1905 case 0: case S_IFREG:
1906 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1908 case S_IFCHR: case S_IFBLK:
1909 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1910 new_decode_dev(dev));
1912 case S_IFIFO: case S_IFSOCK:
1913 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1923 mutex_unlock(&nd.dentry->d_inode->i_mutex);
1931 asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
1933 return sys_mknodat(AT_FDCWD, filename, mode, dev);
1936 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1938 int error = may_create(dir, dentry, NULL);
1943 if (!dir->i_op || !dir->i_op->mkdir)
1946 mode &= (S_IRWXUGO|S_ISVTX);
1947 error = security_inode_mkdir(dir, dentry, mode);
1952 error = dir->i_op->mkdir(dir, dentry, mode);
1954 fsnotify_mkdir(dir, dentry);
1958 asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
1962 struct dentry *dentry;
1963 struct nameidata nd;
1965 tmp = getname(pathname);
1966 error = PTR_ERR(tmp);
1970 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
1973 dentry = lookup_create(&nd, 1);
1974 error = PTR_ERR(dentry);
1978 if (!IS_POSIXACL(nd.dentry->d_inode))
1979 mode &= ~current->fs->umask;
1980 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1983 mutex_unlock(&nd.dentry->d_inode->i_mutex);
1991 asmlinkage long sys_mkdir(const char __user *pathname, int mode)
1993 return sys_mkdirat(AT_FDCWD, pathname, mode);
1997 * We try to drop the dentry early: we should have
1998 * a usage count of 2 if we're the only user of this
1999 * dentry, and if that is true (possibly after pruning
2000 * the dcache), then we drop the dentry now.
2002 * A low-level filesystem can, if it choses, legally
2005 * if (!d_unhashed(dentry))
2008 * if it cannot handle the case of removing a directory
2009 * that is still in use by something else..
2011 void dentry_unhash(struct dentry *dentry)
2014 shrink_dcache_parent(dentry);
2015 spin_lock(&dcache_lock);
2016 spin_lock(&dentry->d_lock);
2017 if (atomic_read(&dentry->d_count) == 2)
2019 spin_unlock(&dentry->d_lock);
2020 spin_unlock(&dcache_lock);
2023 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2025 int error = may_delete(dir, dentry, 1);
2030 if (!dir->i_op || !dir->i_op->rmdir)
2035 mutex_lock(&dentry->d_inode->i_mutex);
2036 dentry_unhash(dentry);
2037 if (d_mountpoint(dentry))
2040 error = security_inode_rmdir(dir, dentry);
2042 error = dir->i_op->rmdir(dir, dentry);
2044 dentry->d_inode->i_flags |= S_DEAD;
2047 mutex_unlock(&dentry->d_inode->i_mutex);
2056 static long do_rmdir(int dfd, const char __user *pathname)
2060 struct dentry *dentry;
2061 struct nameidata nd;
2063 name = getname(pathname);
2065 return PTR_ERR(name);
2067 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
2071 switch(nd.last_type) {
2082 mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2083 dentry = lookup_hash(&nd);
2084 error = PTR_ERR(dentry);
2087 error = vfs_rmdir(nd.dentry->d_inode, dentry);
2090 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2098 asmlinkage long sys_rmdir(const char __user *pathname)
2100 return do_rmdir(AT_FDCWD, pathname);
2103 int vfs_unlink(struct inode *dir, struct dentry *dentry)
2105 int error = may_delete(dir, dentry, 0);
2110 if (!dir->i_op || !dir->i_op->unlink)
2115 mutex_lock(&dentry->d_inode->i_mutex);
2116 if (d_mountpoint(dentry))
2119 error = security_inode_unlink(dir, dentry);
2121 error = dir->i_op->unlink(dir, dentry);
2123 mutex_unlock(&dentry->d_inode->i_mutex);
2125 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2126 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2134 * Make sure that the actual truncation of the file will occur outside its
2135 * directory's i_mutex. Truncate can take a long time if there is a lot of
2136 * writeout happening, and we don't want to prevent access to the directory
2137 * while waiting on the I/O.
2139 static long do_unlinkat(int dfd, const char __user *pathname)
2143 struct dentry *dentry;
2144 struct nameidata nd;
2145 struct inode *inode = NULL;
2147 name = getname(pathname);
2149 return PTR_ERR(name);
2151 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
2155 if (nd.last_type != LAST_NORM)
2157 mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2158 dentry = lookup_hash(&nd);
2159 error = PTR_ERR(dentry);
2160 if (!IS_ERR(dentry)) {
2161 /* Why not before? Because we want correct error value */
2162 if (nd.last.name[nd.last.len])
2164 inode = dentry->d_inode;
2166 atomic_inc(&inode->i_count);
2167 error = vfs_unlink(nd.dentry->d_inode, dentry);
2171 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2173 iput(inode); /* truncate the inode here */
2181 error = !dentry->d_inode ? -ENOENT :
2182 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2186 asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
2188 if ((flag & ~AT_REMOVEDIR) != 0)
2191 if (flag & AT_REMOVEDIR)
2192 return do_rmdir(dfd, pathname);
2194 return do_unlinkat(dfd, pathname);
2197 asmlinkage long sys_unlink(const char __user *pathname)
2199 return do_unlinkat(AT_FDCWD, pathname);
2202 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
2204 int error = may_create(dir, dentry, NULL);
2209 if (!dir->i_op || !dir->i_op->symlink)
2212 error = security_inode_symlink(dir, dentry, oldname);
2217 error = dir->i_op->symlink(dir, dentry, oldname);
2219 fsnotify_create(dir, dentry);
2223 asmlinkage long sys_symlinkat(const char __user *oldname,
2224 int newdfd, const char __user *newname)
2229 struct dentry *dentry;
2230 struct nameidata nd;
2232 from = getname(oldname);
2234 return PTR_ERR(from);
2235 to = getname(newname);
2236 error = PTR_ERR(to);
2240 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2243 dentry = lookup_create(&nd, 0);
2244 error = PTR_ERR(dentry);
2248 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
2251 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2260 asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
2262 return sys_symlinkat(oldname, AT_FDCWD, newname);
2265 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2267 struct inode *inode = old_dentry->d_inode;
2273 error = may_create(dir, new_dentry, NULL);
2277 if (dir->i_sb != inode->i_sb)
2281 * A link to an append-only or immutable file cannot be created.
2283 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2285 if (!dir->i_op || !dir->i_op->link)
2287 if (S_ISDIR(old_dentry->d_inode->i_mode))
2290 error = security_inode_link(old_dentry, dir, new_dentry);
2294 mutex_lock(&old_dentry->d_inode->i_mutex);
2296 error = dir->i_op->link(old_dentry, dir, new_dentry);
2297 mutex_unlock(&old_dentry->d_inode->i_mutex);
2299 fsnotify_create(dir, new_dentry);
2304 * Hardlinks are often used in delicate situations. We avoid
2305 * security-related surprises by not following symlinks on the
2308 * We don't follow them on the oldname either to be compatible
2309 * with linux 2.0, and to avoid hard-linking to directories
2310 * and other special files. --ADM
2312 asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
2313 int newdfd, const char __user *newname,
2316 struct dentry *new_dentry;
2317 struct nameidata nd, old_nd;
2321 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2324 to = getname(newname);
2328 error = __user_walk_fd(olddfd, oldname,
2329 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2333 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2337 if (old_nd.mnt != nd.mnt)
2339 new_dentry = lookup_create(&nd, 0);
2340 error = PTR_ERR(new_dentry);
2341 if (IS_ERR(new_dentry))
2343 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2346 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2350 path_release(&old_nd);
2357 asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
2359 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
2363 * The worst of all namespace operations - renaming directory. "Perverted"
2364 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2366 * a) we can get into loop creation. Check is done in is_subdir().
2367 * b) race potential - two innocent renames can create a loop together.
2368 * That's where 4.4 screws up. Current fix: serialization on
2369 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
2371 * c) we have to lock _three_ objects - parents and victim (if it exists).
2372 * And that - after we got ->i_mutex on parents (until then we don't know
2373 * whether the target exists). Solution: try to be smart with locking
2374 * order for inodes. We rely on the fact that tree topology may change
2375 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
2376 * move will be locked. Thus we can rank directories by the tree
2377 * (ancestors first) and rank all non-directories after them.
2378 * That works since everybody except rename does "lock parent, lookup,
2379 * lock child" and rename is under ->s_vfs_rename_mutex.
2380 * HOWEVER, it relies on the assumption that any object with ->lookup()
2381 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2382 * we'd better make sure that there's no link(2) for them.
2383 * d) some filesystems don't support opened-but-unlinked directories,
2384 * either because of layout or because they are not ready to deal with
2385 * all cases correctly. The latter will be fixed (taking this sort of
2386 * stuff into VFS), but the former is not going away. Solution: the same
2387 * trick as in rmdir().
2388 * e) conversion from fhandle to dentry may come in the wrong moment - when
2389 * we are removing the target. Solution: we will have to grab ->i_mutex
2390 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2391 * ->i_mutex on parents, which works but leads to some truely excessive
2394 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2395 struct inode *new_dir, struct dentry *new_dentry)
2398 struct inode *target;
2401 * If we are going to change the parent - check write permissions,
2402 * we'll need to flip '..'.
2404 if (new_dir != old_dir) {
2405 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2410 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2414 target = new_dentry->d_inode;
2416 mutex_lock(&target->i_mutex);
2417 dentry_unhash(new_dentry);
2419 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2422 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2425 target->i_flags |= S_DEAD;
2426 mutex_unlock(&target->i_mutex);
2427 if (d_unhashed(new_dentry))
2428 d_rehash(new_dentry);
2432 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2433 d_move(old_dentry,new_dentry);
2437 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2438 struct inode *new_dir, struct dentry *new_dentry)
2440 struct inode *target;
2443 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2448 target = new_dentry->d_inode;
2450 mutex_lock(&target->i_mutex);
2451 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2454 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2456 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2457 d_move(old_dentry, new_dentry);
2460 mutex_unlock(&target->i_mutex);
2465 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2466 struct inode *new_dir, struct dentry *new_dentry)
2469 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2470 const char *old_name;
2472 if (old_dentry->d_inode == new_dentry->d_inode)
2475 error = may_delete(old_dir, old_dentry, is_dir);
2479 if (!new_dentry->d_inode)
2480 error = may_create(new_dir, new_dentry, NULL);
2482 error = may_delete(new_dir, new_dentry, is_dir);
2486 if (!old_dir->i_op || !old_dir->i_op->rename)
2489 DQUOT_INIT(old_dir);
2490 DQUOT_INIT(new_dir);
2492 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2495 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2497 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2499 const char *new_name = old_dentry->d_name.name;
2500 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
2501 new_dentry->d_inode, old_dentry->d_inode);
2503 fsnotify_oldname_free(old_name);
2508 static int do_rename(int olddfd, const char *oldname,
2509 int newdfd, const char *newname)
2512 struct dentry * old_dir, * new_dir;
2513 struct dentry * old_dentry, *new_dentry;
2514 struct dentry * trap;
2515 struct nameidata oldnd, newnd;
2517 error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd);
2521 error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
2526 if (oldnd.mnt != newnd.mnt)
2529 old_dir = oldnd.dentry;
2531 if (oldnd.last_type != LAST_NORM)
2534 new_dir = newnd.dentry;
2535 if (newnd.last_type != LAST_NORM)
2538 trap = lock_rename(new_dir, old_dir);
2540 old_dentry = lookup_hash(&oldnd);
2541 error = PTR_ERR(old_dentry);
2542 if (IS_ERR(old_dentry))
2544 /* source must exist */
2546 if (!old_dentry->d_inode)
2548 /* unless the source is a directory trailing slashes give -ENOTDIR */
2549 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2551 if (oldnd.last.name[oldnd.last.len])
2553 if (newnd.last.name[newnd.last.len])
2556 /* source should not be ancestor of target */
2558 if (old_dentry == trap)
2560 new_dentry = lookup_hash(&newnd);
2561 error = PTR_ERR(new_dentry);
2562 if (IS_ERR(new_dentry))
2564 /* target should not be an ancestor of source */
2566 if (new_dentry == trap)
2569 error = vfs_rename(old_dir->d_inode, old_dentry,
2570 new_dir->d_inode, new_dentry);
2576 unlock_rename(new_dir, old_dir);
2578 path_release(&newnd);
2580 path_release(&oldnd);
2585 asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
2586 int newdfd, const char __user *newname)
2592 from = getname(oldname);
2594 return PTR_ERR(from);
2595 to = getname(newname);
2596 error = PTR_ERR(to);
2598 error = do_rename(olddfd, from, newdfd, to);
2605 asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
2607 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
2610 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2614 len = PTR_ERR(link);
2619 if (len > (unsigned) buflen)
2621 if (copy_to_user(buffer, link, len))
2628 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2629 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2630 * using) it for any given inode is up to filesystem.
2632 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2634 struct nameidata nd;
2638 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2639 if (!IS_ERR(cookie)) {
2640 int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2641 if (dentry->d_inode->i_op->put_link)
2642 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2643 cookie = ERR_PTR(res);
2645 return PTR_ERR(cookie);
2648 int vfs_follow_link(struct nameidata *nd, const char *link)
2650 return __vfs_follow_link(nd, link);
2653 /* get the link contents into pagecache */
2654 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2657 struct address_space *mapping = dentry->d_inode->i_mapping;
2658 page = read_mapping_page(mapping, 0, NULL);
2665 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2667 struct page *page = NULL;
2668 char *s = page_getlink(dentry, &page);
2669 int res = vfs_readlink(dentry,buffer,buflen,s);
2672 page_cache_release(page);
2677 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2679 struct page *page = NULL;
2680 nd_set_link(nd, page_getlink(dentry, &page));
2684 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
2686 struct page *page = cookie;
2690 page_cache_release(page);
2694 int __page_symlink(struct inode *inode, const char *symname, int len,
2697 struct address_space *mapping = inode->i_mapping;
2704 page = find_or_create_page(mapping, 0, gfp_mask);
2707 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2708 if (err == AOP_TRUNCATED_PAGE) {
2709 page_cache_release(page);
2714 kaddr = kmap_atomic(page, KM_USER0);
2715 memcpy(kaddr, symname, len-1);
2716 kunmap_atomic(kaddr, KM_USER0);
2717 err = mapping->a_ops->commit_write(NULL, page, 0, len-1);
2718 if (err == AOP_TRUNCATED_PAGE) {
2719 page_cache_release(page);
2725 * Notice that we are _not_ going to block here - end of page is
2726 * unmapped, so this will only try to map the rest of page, see
2727 * that it is unmapped (typically even will not look into inode -
2728 * ->i_size will be enough for everything) and zero it out.
2729 * OTOH it's obviously correct and should make the page up-to-date.
2731 if (!PageUptodate(page)) {
2732 err = mapping->a_ops->readpage(NULL, page);
2733 if (err != AOP_TRUNCATED_PAGE)
2734 wait_on_page_locked(page);
2738 page_cache_release(page);
2741 mark_inode_dirty(inode);
2745 page_cache_release(page);
2750 int page_symlink(struct inode *inode, const char *symname, int len)
2752 return __page_symlink(inode, symname, len,
2753 mapping_gfp_mask(inode->i_mapping));
2756 const struct inode_operations page_symlink_inode_operations = {
2757 .readlink = generic_readlink,
2758 .follow_link = page_follow_link_light,
2759 .put_link = page_put_link,
2762 EXPORT_SYMBOL(__user_walk);
2763 EXPORT_SYMBOL(__user_walk_fd);
2764 EXPORT_SYMBOL(follow_down);
2765 EXPORT_SYMBOL(follow_up);
2766 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2767 EXPORT_SYMBOL(getname);
2768 EXPORT_SYMBOL(lock_rename);
2769 EXPORT_SYMBOL(lookup_one_len);
2770 EXPORT_SYMBOL(page_follow_link_light);
2771 EXPORT_SYMBOL(page_put_link);
2772 EXPORT_SYMBOL(page_readlink);
2773 EXPORT_SYMBOL(__page_symlink);
2774 EXPORT_SYMBOL(page_symlink);
2775 EXPORT_SYMBOL(page_symlink_inode_operations);
2776 EXPORT_SYMBOL(path_lookup);
2777 EXPORT_SYMBOL(path_release);
2778 EXPORT_SYMBOL(path_walk);
2779 EXPORT_SYMBOL(permission);
2780 EXPORT_SYMBOL(vfs_permission);
2781 EXPORT_SYMBOL(file_permission);
2782 EXPORT_SYMBOL(unlock_rename);
2783 EXPORT_SYMBOL(vfs_create);
2784 EXPORT_SYMBOL(vfs_follow_link);
2785 EXPORT_SYMBOL(vfs_link);
2786 EXPORT_SYMBOL(vfs_mkdir);
2787 EXPORT_SYMBOL(vfs_mknod);
2788 EXPORT_SYMBOL(generic_permission);
2789 EXPORT_SYMBOL(vfs_readlink);
2790 EXPORT_SYMBOL(vfs_rename);
2791 EXPORT_SYMBOL(vfs_rmdir);
2792 EXPORT_SYMBOL(vfs_symlink);
2793 EXPORT_SYMBOL(vfs_unlink);
2794 EXPORT_SYMBOL(dentry_unhash);
2795 EXPORT_SYMBOL(generic_readlink);