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 static int fastcall link_path_walk(const char *name, struct nameidata *nd);
112 /* In order to reduce some races, while at the same time doing additional
113 * checking and hopefully speeding things up, we copy filenames to the
114 * kernel data space before using them..
116 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
117 * PATH_MAX includes the nul terminator --RR.
119 static int do_getname(const char __user *filename, char *page)
122 unsigned long len = PATH_MAX;
124 if (!segment_eq(get_fs(), KERNEL_DS)) {
125 if ((unsigned long) filename >= TASK_SIZE)
127 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
128 len = TASK_SIZE - (unsigned long) filename;
131 retval = strncpy_from_user(page, filename, len);
135 return -ENAMETOOLONG;
141 char * getname(const char __user * filename)
145 result = ERR_PTR(-ENOMEM);
148 int retval = do_getname(filename, tmp);
153 result = ERR_PTR(retval);
156 audit_getname(result);
160 #ifdef CONFIG_AUDITSYSCALL
161 void putname(const char *name)
163 if (unlikely(!audit_dummy_context()))
168 EXPORT_SYMBOL(putname);
173 * generic_permission - check for access rights on a Posix-like filesystem
174 * @inode: inode to check access rights for
175 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
176 * @check_acl: optional callback to check for Posix ACLs
178 * Used to check for read/write/execute permissions on a file.
179 * We use "fsuid" for this, letting us set arbitrary permissions
180 * for filesystem access without changing the "normal" uids which
181 * are used for other things..
183 int generic_permission(struct inode *inode, int mask,
184 int (*check_acl)(struct inode *inode, int mask))
186 umode_t mode = inode->i_mode;
188 if (current->fsuid == inode->i_uid)
191 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
192 int error = check_acl(inode, mask);
193 if (error == -EACCES)
194 goto check_capabilities;
195 else if (error != -EAGAIN)
199 if (in_group_p(inode->i_gid))
204 * If the DACs are ok we don't need any capability check.
206 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
211 * Read/write DACs are always overridable.
212 * Executable DACs are overridable if at least one exec bit is set.
214 if (!(mask & MAY_EXEC) ||
215 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
216 if (capable(CAP_DAC_OVERRIDE))
220 * Searching includes executable on directories, else just read.
222 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
223 if (capable(CAP_DAC_READ_SEARCH))
229 int permission(struct inode *inode, int mask, struct nameidata *nd)
231 umode_t mode = inode->i_mode;
234 if (mask & MAY_WRITE) {
237 * Nobody gets write access to a read-only fs.
239 if (IS_RDONLY(inode) &&
240 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
244 * Nobody gets write access to an immutable file.
246 if (IS_IMMUTABLE(inode))
252 * MAY_EXEC on regular files requires special handling: We override
253 * filesystem execute permissions if the mode bits aren't set or
254 * the fs is mounted with the "noexec" flag.
256 if ((mask & MAY_EXEC) && S_ISREG(mode) && (!(mode & S_IXUGO) ||
257 (nd && nd->mnt && (nd->mnt->mnt_flags & MNT_NOEXEC))))
260 /* Ordinary permission routines do not understand MAY_APPEND. */
261 submask = mask & ~MAY_APPEND;
262 if (inode->i_op && inode->i_op->permission)
263 retval = inode->i_op->permission(inode, submask, nd);
265 retval = generic_permission(inode, submask, NULL);
269 return security_inode_permission(inode, mask, nd);
273 * vfs_permission - check for access rights to a given path
274 * @nd: lookup result that describes the path
275 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
277 * Used to check for read/write/execute permissions on a path.
278 * We use "fsuid" for this, letting us set arbitrary permissions
279 * for filesystem access without changing the "normal" uids which
280 * are used for other things.
282 int vfs_permission(struct nameidata *nd, int mask)
284 return permission(nd->dentry->d_inode, mask, nd);
288 * file_permission - check for additional access rights to a given file
289 * @file: file to check access rights for
290 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
292 * Used to check for read/write/execute permissions on an already opened
296 * Do not use this function in new code. All access checks should
297 * be done using vfs_permission().
299 int file_permission(struct file *file, int mask)
301 return permission(file->f_path.dentry->d_inode, mask, NULL);
305 * get_write_access() gets write permission for a file.
306 * put_write_access() releases this write permission.
307 * This is used for regular files.
308 * We cannot support write (and maybe mmap read-write shared) accesses and
309 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
310 * can have the following values:
311 * 0: no writers, no VM_DENYWRITE mappings
312 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
313 * > 0: (i_writecount) users are writing to the file.
315 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
316 * except for the cases where we don't hold i_writecount yet. Then we need to
317 * use {get,deny}_write_access() - these functions check the sign and refuse
318 * to do the change if sign is wrong. Exclusion between them is provided by
319 * the inode->i_lock spinlock.
322 int get_write_access(struct inode * inode)
324 spin_lock(&inode->i_lock);
325 if (atomic_read(&inode->i_writecount) < 0) {
326 spin_unlock(&inode->i_lock);
329 atomic_inc(&inode->i_writecount);
330 spin_unlock(&inode->i_lock);
335 int deny_write_access(struct file * file)
337 struct inode *inode = file->f_path.dentry->d_inode;
339 spin_lock(&inode->i_lock);
340 if (atomic_read(&inode->i_writecount) > 0) {
341 spin_unlock(&inode->i_lock);
344 atomic_dec(&inode->i_writecount);
345 spin_unlock(&inode->i_lock);
350 void path_release(struct nameidata *nd)
357 * umount() mustn't call path_release()/mntput() as that would clear
360 void path_release_on_umount(struct nameidata *nd)
363 mntput_no_expire(nd->mnt);
367 * release_open_intent - free up open intent resources
368 * @nd: pointer to nameidata
370 void release_open_intent(struct nameidata *nd)
372 if (nd->intent.open.file->f_path.dentry == NULL)
373 put_filp(nd->intent.open.file);
375 fput(nd->intent.open.file);
378 static inline struct dentry *
379 do_revalidate(struct dentry *dentry, struct nameidata *nd)
381 int status = dentry->d_op->d_revalidate(dentry, nd);
382 if (unlikely(status <= 0)) {
384 * The dentry failed validation.
385 * If d_revalidate returned 0 attempt to invalidate
386 * the dentry otherwise d_revalidate is asking us
387 * to return a fail status.
390 if (!d_invalidate(dentry)) {
396 dentry = ERR_PTR(status);
403 * Internal lookup() using the new generic dcache.
406 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
408 struct dentry * dentry = __d_lookup(parent, name);
410 /* lockess __d_lookup may fail due to concurrent d_move()
411 * in some unrelated directory, so try with d_lookup
414 dentry = d_lookup(parent, name);
416 if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
417 dentry = do_revalidate(dentry, nd);
423 * Short-cut version of permission(), for calling by
424 * path_walk(), when dcache lock is held. Combines parts
425 * of permission() and generic_permission(), and tests ONLY for
426 * MAY_EXEC permission.
428 * If appropriate, check DAC only. If not appropriate, or
429 * short-cut DAC fails, then call permission() to do more
430 * complete permission check.
432 static int exec_permission_lite(struct inode *inode,
433 struct nameidata *nd)
435 umode_t mode = inode->i_mode;
437 if (inode->i_op && inode->i_op->permission)
440 if (current->fsuid == inode->i_uid)
442 else if (in_group_p(inode->i_gid))
448 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
451 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
454 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
459 return security_inode_permission(inode, MAY_EXEC, nd);
463 * This is called when everything else fails, and we actually have
464 * to go to the low-level filesystem to find out what we should do..
466 * We get the directory semaphore, and after getting that we also
467 * make sure that nobody added the entry to the dcache in the meantime..
470 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
472 struct dentry * result;
473 struct inode *dir = parent->d_inode;
475 mutex_lock(&dir->i_mutex);
477 * First re-do the cached lookup just in case it was created
478 * while we waited for the directory semaphore..
480 * FIXME! This could use version numbering or similar to
481 * avoid unnecessary cache lookups.
483 * The "dcache_lock" is purely to protect the RCU list walker
484 * from concurrent renames at this point (we mustn't get false
485 * negatives from the RCU list walk here, unlike the optimistic
488 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
490 result = d_lookup(parent, name);
492 struct dentry * dentry = d_alloc(parent, name);
493 result = ERR_PTR(-ENOMEM);
495 result = dir->i_op->lookup(dir, dentry, nd);
501 mutex_unlock(&dir->i_mutex);
506 * Uhhuh! Nasty case: the cache was re-populated while
507 * we waited on the semaphore. Need to revalidate.
509 mutex_unlock(&dir->i_mutex);
510 if (result->d_op && result->d_op->d_revalidate) {
511 result = do_revalidate(result, nd);
513 result = ERR_PTR(-ENOENT);
518 static int __emul_lookup_dentry(const char *, struct nameidata *);
521 static __always_inline int
522 walk_init_root(const char *name, struct nameidata *nd)
524 struct fs_struct *fs = current->fs;
526 read_lock(&fs->lock);
527 if (fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
528 nd->mnt = mntget(fs->altrootmnt);
529 nd->dentry = dget(fs->altroot);
530 read_unlock(&fs->lock);
531 if (__emul_lookup_dentry(name,nd))
533 read_lock(&fs->lock);
535 nd->mnt = mntget(fs->rootmnt);
536 nd->dentry = dget(fs->root);
537 read_unlock(&fs->lock);
541 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
550 if (!walk_init_root(link, nd))
551 /* weird __emul_prefix() stuff did it */
554 res = link_path_walk(link, nd);
556 if (nd->depth || res || nd->last_type!=LAST_NORM)
559 * If it is an iterative symlinks resolution in open_namei() we
560 * have to copy the last component. And all that crap because of
561 * bloody create() on broken symlinks. Furrfu...
564 if (unlikely(!name)) {
568 strcpy(name, nd->last.name);
569 nd->last.name = name;
573 return PTR_ERR(link);
576 static inline void dput_path(struct path *path, struct nameidata *nd)
579 if (path->mnt != nd->mnt)
583 static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
586 if (nd->mnt != path->mnt)
589 nd->dentry = path->dentry;
592 static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
596 struct dentry *dentry = path->dentry;
598 touch_atime(path->mnt, dentry);
599 nd_set_link(nd, NULL);
601 if (path->mnt != nd->mnt) {
602 path_to_nameidata(path, nd);
606 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
607 error = PTR_ERR(cookie);
608 if (!IS_ERR(cookie)) {
609 char *s = nd_get_link(nd);
612 error = __vfs_follow_link(nd, s);
613 if (dentry->d_inode->i_op->put_link)
614 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
623 * This limits recursive symlink follows to 8, while
624 * limiting consecutive symlinks to 40.
626 * Without that kind of total limit, nasty chains of consecutive
627 * symlinks can cause almost arbitrarily long lookups.
629 static inline int do_follow_link(struct path *path, struct nameidata *nd)
632 if (current->link_count >= MAX_NESTED_LINKS)
634 if (current->total_link_count >= 40)
636 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
638 err = security_inode_follow_link(path->dentry, nd);
641 current->link_count++;
642 current->total_link_count++;
644 err = __do_follow_link(path, nd);
645 current->link_count--;
654 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
656 struct vfsmount *parent;
657 struct dentry *mountpoint;
658 spin_lock(&vfsmount_lock);
659 parent=(*mnt)->mnt_parent;
660 if (parent == *mnt) {
661 spin_unlock(&vfsmount_lock);
665 mountpoint=dget((*mnt)->mnt_mountpoint);
666 spin_unlock(&vfsmount_lock);
668 *dentry = mountpoint;
674 /* no need for dcache_lock, as serialization is taken care in
677 static int __follow_mount(struct path *path)
680 while (d_mountpoint(path->dentry)) {
681 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
688 path->dentry = dget(mounted->mnt_root);
694 static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
696 while (d_mountpoint(*dentry)) {
697 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
703 *dentry = dget(mounted->mnt_root);
707 /* no need for dcache_lock, as serialization is taken care in
710 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
712 struct vfsmount *mounted;
714 mounted = lookup_mnt(*mnt, *dentry);
719 *dentry = dget(mounted->mnt_root);
725 static __always_inline void follow_dotdot(struct nameidata *nd)
727 struct fs_struct *fs = current->fs;
730 struct vfsmount *parent;
731 struct dentry *old = nd->dentry;
733 read_lock(&fs->lock);
734 if (nd->dentry == fs->root &&
735 nd->mnt == fs->rootmnt) {
736 read_unlock(&fs->lock);
739 read_unlock(&fs->lock);
740 spin_lock(&dcache_lock);
741 if (nd->dentry != nd->mnt->mnt_root) {
742 nd->dentry = dget(nd->dentry->d_parent);
743 spin_unlock(&dcache_lock);
747 spin_unlock(&dcache_lock);
748 spin_lock(&vfsmount_lock);
749 parent = nd->mnt->mnt_parent;
750 if (parent == nd->mnt) {
751 spin_unlock(&vfsmount_lock);
755 nd->dentry = dget(nd->mnt->mnt_mountpoint);
756 spin_unlock(&vfsmount_lock);
761 follow_mount(&nd->mnt, &nd->dentry);
765 * It's more convoluted than I'd like it to be, but... it's still fairly
766 * small and for now I'd prefer to have fast path as straight as possible.
767 * It _is_ time-critical.
769 static int do_lookup(struct nameidata *nd, struct qstr *name,
772 struct vfsmount *mnt = nd->mnt;
773 struct dentry *dentry = __d_lookup(nd->dentry, name);
777 if (dentry->d_op && dentry->d_op->d_revalidate)
778 goto need_revalidate;
781 path->dentry = dentry;
782 __follow_mount(path);
786 dentry = real_lookup(nd->dentry, name, nd);
792 dentry = do_revalidate(dentry, nd);
800 return PTR_ERR(dentry);
805 * This is the basic name resolution function, turning a pathname into
806 * the final dentry. We expect 'base' to be positive and a directory.
808 * Returns 0 and nd will have valid dentry and mnt on success.
809 * Returns error and drops reference to input namei data on failure.
811 static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
816 unsigned int lookup_flags = nd->flags;
823 inode = nd->dentry->d_inode;
825 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
827 /* At this point we know we have a real path component. */
833 nd->flags |= LOOKUP_CONTINUE;
834 err = exec_permission_lite(inode, nd);
836 err = vfs_permission(nd, MAY_EXEC);
841 c = *(const unsigned char *)name;
843 hash = init_name_hash();
846 hash = partial_name_hash(c, hash);
847 c = *(const unsigned char *)name;
848 } while (c && (c != '/'));
849 this.len = name - (const char *) this.name;
850 this.hash = end_name_hash(hash);
852 /* remove trailing slashes? */
855 while (*++name == '/');
857 goto last_with_slashes;
860 * "." and ".." are special - ".." especially so because it has
861 * to be able to know about the current root directory and
862 * parent relationships.
864 if (this.name[0] == '.') switch (this.len) {
868 if (this.name[1] != '.')
871 inode = nd->dentry->d_inode;
877 * See if the low-level filesystem might want
878 * to use its own hash..
880 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
881 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
885 /* This does the actual lookups.. */
886 err = do_lookup(nd, &this, &next);
891 inode = next.dentry->d_inode;
898 if (inode->i_op->follow_link) {
899 err = do_follow_link(&next, nd);
903 inode = nd->dentry->d_inode;
910 path_to_nameidata(&next, nd);
912 if (!inode->i_op->lookup)
915 /* here ends the main loop */
918 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
920 /* Clear LOOKUP_CONTINUE iff it was previously unset */
921 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
922 if (lookup_flags & LOOKUP_PARENT)
924 if (this.name[0] == '.') switch (this.len) {
928 if (this.name[1] != '.')
931 inode = nd->dentry->d_inode;
936 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
937 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
941 err = do_lookup(nd, &this, &next);
944 inode = next.dentry->d_inode;
945 if ((lookup_flags & LOOKUP_FOLLOW)
946 && inode && inode->i_op && inode->i_op->follow_link) {
947 err = do_follow_link(&next, nd);
950 inode = nd->dentry->d_inode;
952 path_to_nameidata(&next, nd);
956 if (lookup_flags & LOOKUP_DIRECTORY) {
958 if (!inode->i_op || !inode->i_op->lookup)
964 nd->last_type = LAST_NORM;
965 if (this.name[0] != '.')
968 nd->last_type = LAST_DOT;
969 else if (this.len == 2 && this.name[1] == '.')
970 nd->last_type = LAST_DOTDOT;
975 * We bypassed the ordinary revalidation routines.
976 * We may need to check the cached dentry for staleness.
978 if (nd->dentry && nd->dentry->d_sb &&
979 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
981 /* Note: we do not d_invalidate() */
982 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
988 dput_path(&next, nd);
997 * Wrapper to retry pathname resolution whenever the underlying
998 * file system returns an ESTALE.
1000 * Retry the whole path once, forcing real lookup requests
1001 * instead of relying on the dcache.
1003 static int fastcall link_path_walk(const char *name, struct nameidata *nd)
1005 struct nameidata save = *nd;
1008 /* make sure the stuff we saved doesn't go away */
1012 result = __link_path_walk(name, nd);
1013 if (result == -ESTALE) {
1017 nd->flags |= LOOKUP_REVAL;
1018 result = __link_path_walk(name, nd);
1027 static int fastcall path_walk(const char * name, struct nameidata *nd)
1029 current->total_link_count = 0;
1030 return link_path_walk(name, nd);
1034 * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
1035 * everything is done. Returns 0 and drops input nd, if lookup failed;
1037 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
1039 if (path_walk(name, nd))
1040 return 0; /* something went wrong... */
1042 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
1043 struct dentry *old_dentry = nd->dentry;
1044 struct vfsmount *old_mnt = nd->mnt;
1045 struct qstr last = nd->last;
1046 int last_type = nd->last_type;
1047 struct fs_struct *fs = current->fs;
1050 * NAME was not found in alternate root or it's a directory.
1051 * Try to find it in the normal root:
1053 nd->last_type = LAST_ROOT;
1054 read_lock(&fs->lock);
1055 nd->mnt = mntget(fs->rootmnt);
1056 nd->dentry = dget(fs->root);
1057 read_unlock(&fs->lock);
1058 if (path_walk(name, nd) == 0) {
1059 if (nd->dentry->d_inode) {
1066 nd->dentry = old_dentry;
1069 nd->last_type = last_type;
1074 void set_fs_altroot(void)
1076 char *emul = __emul_prefix();
1077 struct nameidata nd;
1078 struct vfsmount *mnt = NULL, *oldmnt;
1079 struct dentry *dentry = NULL, *olddentry;
1081 struct fs_struct *fs = current->fs;
1085 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
1091 write_lock(&fs->lock);
1092 oldmnt = fs->altrootmnt;
1093 olddentry = fs->altroot;
1094 fs->altrootmnt = mnt;
1095 fs->altroot = dentry;
1096 write_unlock(&fs->lock);
1103 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1104 static int fastcall do_path_lookup(int dfd, const char *name,
1105 unsigned int flags, struct nameidata *nd)
1110 struct fs_struct *fs = current->fs;
1112 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1117 read_lock(&fs->lock);
1118 if (fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
1119 nd->mnt = mntget(fs->altrootmnt);
1120 nd->dentry = dget(fs->altroot);
1121 read_unlock(&fs->lock);
1122 if (__emul_lookup_dentry(name,nd))
1123 goto out; /* found in altroot */
1124 read_lock(&fs->lock);
1126 nd->mnt = mntget(fs->rootmnt);
1127 nd->dentry = dget(fs->root);
1128 read_unlock(&fs->lock);
1129 } else if (dfd == AT_FDCWD) {
1130 read_lock(&fs->lock);
1131 nd->mnt = mntget(fs->pwdmnt);
1132 nd->dentry = dget(fs->pwd);
1133 read_unlock(&fs->lock);
1135 struct dentry *dentry;
1137 file = fget_light(dfd, &fput_needed);
1142 dentry = file->f_path.dentry;
1145 if (!S_ISDIR(dentry->d_inode->i_mode))
1148 retval = file_permission(file, MAY_EXEC);
1152 nd->mnt = mntget(file->f_path.mnt);
1153 nd->dentry = dget(dentry);
1155 fput_light(file, fput_needed);
1158 retval = path_walk(name, nd);
1160 if (unlikely(!retval && !audit_dummy_context() && nd->dentry &&
1161 nd->dentry->d_inode))
1162 audit_inode(name, nd->dentry->d_inode);
1167 fput_light(file, fput_needed);
1171 int fastcall path_lookup(const char *name, unsigned int flags,
1172 struct nameidata *nd)
1174 return do_path_lookup(AT_FDCWD, name, flags, nd);
1178 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1179 * @dentry: pointer to dentry of the base directory
1180 * @mnt: pointer to vfs mount of the base directory
1181 * @name: pointer to file name
1182 * @flags: lookup flags
1183 * @nd: pointer to nameidata
1185 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1186 const char *name, unsigned int flags,
1187 struct nameidata *nd)
1191 /* same as do_path_lookup */
1192 nd->last_type = LAST_ROOT;
1196 nd->mnt = mntget(mnt);
1197 nd->dentry = dget(dentry);
1199 retval = path_walk(name, nd);
1200 if (unlikely(!retval && !audit_dummy_context() && nd->dentry &&
1201 nd->dentry->d_inode))
1202 audit_inode(name, nd->dentry->d_inode);
1208 static int __path_lookup_intent_open(int dfd, const char *name,
1209 unsigned int lookup_flags, struct nameidata *nd,
1210 int open_flags, int create_mode)
1212 struct file *filp = get_empty_filp();
1217 nd->intent.open.file = filp;
1218 nd->intent.open.flags = open_flags;
1219 nd->intent.open.create_mode = create_mode;
1220 err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
1221 if (IS_ERR(nd->intent.open.file)) {
1223 err = PTR_ERR(nd->intent.open.file);
1226 } else if (err != 0)
1227 release_open_intent(nd);
1232 * path_lookup_open - lookup a file path with open intent
1233 * @dfd: the directory to use as base, or AT_FDCWD
1234 * @name: pointer to file name
1235 * @lookup_flags: lookup intent flags
1236 * @nd: pointer to nameidata
1237 * @open_flags: open intent flags
1239 int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
1240 struct nameidata *nd, int open_flags)
1242 return __path_lookup_intent_open(dfd, name, lookup_flags, nd,
1247 * path_lookup_create - lookup a file path with open + create intent
1248 * @dfd: the directory to use as base, or AT_FDCWD
1249 * @name: pointer to file name
1250 * @lookup_flags: lookup intent flags
1251 * @nd: pointer to nameidata
1252 * @open_flags: open intent flags
1253 * @create_mode: create intent flags
1255 static int path_lookup_create(int dfd, const char *name,
1256 unsigned int lookup_flags, struct nameidata *nd,
1257 int open_flags, int create_mode)
1259 return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE,
1260 nd, open_flags, create_mode);
1263 int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
1264 struct nameidata *nd, int open_flags)
1266 char *tmp = getname(name);
1267 int err = PTR_ERR(tmp);
1270 err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
1276 static struct dentry *__lookup_hash(struct qstr *name,
1277 struct dentry *base, struct nameidata *nd)
1279 struct dentry *dentry;
1280 struct inode *inode;
1283 inode = base->d_inode;
1286 * See if the low-level filesystem might want
1287 * to use its own hash..
1289 if (base->d_op && base->d_op->d_hash) {
1290 err = base->d_op->d_hash(base, name);
1291 dentry = ERR_PTR(err);
1296 dentry = cached_lookup(base, name, nd);
1298 struct dentry *new = d_alloc(base, name);
1299 dentry = ERR_PTR(-ENOMEM);
1302 dentry = inode->i_op->lookup(inode, new, nd);
1313 * Restricted form of lookup. Doesn't follow links, single-component only,
1314 * needs parent already locked. Doesn't follow mounts.
1317 static struct dentry *lookup_hash(struct nameidata *nd)
1321 err = permission(nd->dentry->d_inode, MAY_EXEC, nd);
1323 return ERR_PTR(err);
1324 return __lookup_hash(&nd->last, nd->dentry, nd);
1327 static int __lookup_one_len(const char *name, struct qstr *this,
1328 struct dentry *base, int len)
1338 hash = init_name_hash();
1340 c = *(const unsigned char *)name++;
1341 if (c == '/' || c == '\0')
1343 hash = partial_name_hash(c, hash);
1345 this->hash = end_name_hash(hash);
1350 * lookup_one_len: filesystem helper to lookup single pathname component
1351 * @name: pathname component to lookup
1352 * @base: base directory to lookup from
1353 * @len: maximum length @len should be interpreted to
1355 * Note that this routine is purely a helper for filesystem useage and should
1356 * not be called by generic code. Also note that by using this function to
1357 * nameidata argument is passed to the filesystem methods and a filesystem
1358 * using this helper needs to be prepared for that.
1360 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1365 err = __lookup_one_len(name, &this, base, len);
1367 return ERR_PTR(err);
1369 err = permission(base->d_inode, MAY_EXEC, NULL);
1371 return ERR_PTR(err);
1372 return __lookup_hash(&this, base, NULL);
1376 * lookup_one_noperm - bad hack for sysfs
1377 * @name: pathname component to lookup
1378 * @base: base directory to lookup from
1380 * This is a variant of lookup_one_len that doesn't perform any permission
1381 * checks. It's a horrible hack to work around the braindead sysfs
1382 * architecture and should not be used anywhere else.
1384 * DON'T USE THIS FUNCTION EVER, thanks.
1386 struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
1391 err = __lookup_one_len(name, &this, base, strlen(name));
1393 return ERR_PTR(err);
1394 return __lookup_hash(&this, base, NULL);
1397 int fastcall __user_walk_fd(int dfd, const char __user *name, unsigned flags,
1398 struct nameidata *nd)
1400 char *tmp = getname(name);
1401 int err = PTR_ERR(tmp);
1404 err = do_path_lookup(dfd, tmp, flags, nd);
1410 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1412 return __user_walk_fd(AT_FDCWD, name, flags, nd);
1416 * It's inline, so penalty for filesystems that don't use sticky bit is
1419 static inline int check_sticky(struct inode *dir, struct inode *inode)
1421 if (!(dir->i_mode & S_ISVTX))
1423 if (inode->i_uid == current->fsuid)
1425 if (dir->i_uid == current->fsuid)
1427 return !capable(CAP_FOWNER);
1431 * Check whether we can remove a link victim from directory dir, check
1432 * whether the type of victim is right.
1433 * 1. We can't do it if dir is read-only (done in permission())
1434 * 2. We should have write and exec permissions on dir
1435 * 3. We can't remove anything from append-only dir
1436 * 4. We can't do anything with immutable dir (done in permission())
1437 * 5. If the sticky bit on dir is set we should either
1438 * a. be owner of dir, or
1439 * b. be owner of victim, or
1440 * c. have CAP_FOWNER capability
1441 * 6. If the victim is append-only or immutable we can't do antyhing with
1442 * links pointing to it.
1443 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1444 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1445 * 9. We can't remove a root or mountpoint.
1446 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1447 * nfs_async_unlink().
1449 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1453 if (!victim->d_inode)
1456 BUG_ON(victim->d_parent->d_inode != dir);
1457 audit_inode_child(victim->d_name.name, victim->d_inode, dir);
1459 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1464 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1465 IS_IMMUTABLE(victim->d_inode))
1468 if (!S_ISDIR(victim->d_inode->i_mode))
1470 if (IS_ROOT(victim))
1472 } else if (S_ISDIR(victim->d_inode->i_mode))
1474 if (IS_DEADDIR(dir))
1476 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1481 /* Check whether we can create an object with dentry child in directory
1483 * 1. We can't do it if child already exists (open has special treatment for
1484 * this case, but since we are inlined it's OK)
1485 * 2. We can't do it if dir is read-only (done in permission())
1486 * 3. We should have write and exec permissions on dir
1487 * 4. We can't do it if dir is immutable (done in permission())
1489 static inline int may_create(struct inode *dir, struct dentry *child,
1490 struct nameidata *nd)
1494 if (IS_DEADDIR(dir))
1496 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1500 * O_DIRECTORY translates into forcing a directory lookup.
1502 static inline int lookup_flags(unsigned int f)
1504 unsigned long retval = LOOKUP_FOLLOW;
1507 retval &= ~LOOKUP_FOLLOW;
1509 if (f & O_DIRECTORY)
1510 retval |= LOOKUP_DIRECTORY;
1516 * p1 and p2 should be directories on the same fs.
1518 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1523 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1527 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1529 for (p = p1; p->d_parent != p; p = p->d_parent) {
1530 if (p->d_parent == p2) {
1531 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1532 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1537 for (p = p2; p->d_parent != p; p = p->d_parent) {
1538 if (p->d_parent == p1) {
1539 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1540 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1545 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1546 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1550 void unlock_rename(struct dentry *p1, struct dentry *p2)
1552 mutex_unlock(&p1->d_inode->i_mutex);
1554 mutex_unlock(&p2->d_inode->i_mutex);
1555 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1559 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1560 struct nameidata *nd)
1562 int error = may_create(dir, dentry, nd);
1567 if (!dir->i_op || !dir->i_op->create)
1568 return -EACCES; /* shouldn't it be ENOSYS? */
1571 error = security_inode_create(dir, dentry, mode);
1575 error = dir->i_op->create(dir, dentry, mode, nd);
1577 fsnotify_create(dir, dentry);
1581 int may_open(struct nameidata *nd, int acc_mode, int flag)
1583 struct dentry *dentry = nd->dentry;
1584 struct inode *inode = dentry->d_inode;
1590 if (S_ISLNK(inode->i_mode))
1593 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1596 error = vfs_permission(nd, acc_mode);
1601 * FIFO's, sockets and device files are special: they don't
1602 * actually live on the filesystem itself, and as such you
1603 * can write to them even if the filesystem is read-only.
1605 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1607 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1608 if (nd->mnt->mnt_flags & MNT_NODEV)
1612 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1615 * An append-only file must be opened in append mode for writing.
1617 if (IS_APPEND(inode)) {
1618 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1624 /* O_NOATIME can only be set by the owner or superuser */
1625 if (flag & O_NOATIME)
1626 if (!is_owner_or_cap(inode))
1630 * Ensure there are no outstanding leases on the file.
1632 error = break_lease(inode, flag);
1636 if (flag & O_TRUNC) {
1637 error = get_write_access(inode);
1642 * Refuse to truncate files with mandatory locks held on them.
1644 error = locks_verify_locked(inode);
1648 error = do_truncate(dentry, 0, ATTR_MTIME|ATTR_CTIME, NULL);
1650 put_write_access(inode);
1654 if (flag & FMODE_WRITE)
1660 static int open_namei_create(struct nameidata *nd, struct path *path,
1664 struct dentry *dir = nd->dentry;
1666 if (!IS_POSIXACL(dir->d_inode))
1667 mode &= ~current->fs->umask;
1668 error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1669 mutex_unlock(&dir->d_inode->i_mutex);
1671 nd->dentry = path->dentry;
1674 /* Don't check for write permission, don't truncate */
1675 return may_open(nd, 0, flag & ~O_TRUNC);
1681 * namei for open - this is in fact almost the whole open-routine.
1683 * Note that the low bits of "flag" aren't the same as in the open
1684 * system call - they are 00 - no permissions needed
1685 * 01 - read permission needed
1686 * 10 - write permission needed
1687 * 11 - read/write permissions needed
1688 * which is a lot more logical, and also allows the "no perm" needed
1689 * for symlinks (where the permissions are checked later).
1692 int open_namei(int dfd, const char *pathname, int flag,
1693 int mode, struct nameidata *nd)
1695 int acc_mode, error;
1700 acc_mode = ACC_MODE(flag);
1702 /* O_TRUNC implies we need access checks for write permissions */
1704 acc_mode |= MAY_WRITE;
1706 /* Allow the LSM permission hook to distinguish append
1707 access from general write access. */
1708 if (flag & O_APPEND)
1709 acc_mode |= MAY_APPEND;
1712 * The simplest case - just a plain lookup.
1714 if (!(flag & O_CREAT)) {
1715 error = path_lookup_open(dfd, pathname, lookup_flags(flag),
1723 * Create - we need to know the parent.
1725 error = path_lookup_create(dfd,pathname,LOOKUP_PARENT,nd,flag,mode);
1730 * We have the parent and last component. First of all, check
1731 * that we are not asked to creat(2) an obvious directory - that
1735 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1739 nd->flags &= ~LOOKUP_PARENT;
1740 mutex_lock(&dir->d_inode->i_mutex);
1741 path.dentry = lookup_hash(nd);
1745 error = PTR_ERR(path.dentry);
1746 if (IS_ERR(path.dentry)) {
1747 mutex_unlock(&dir->d_inode->i_mutex);
1751 if (IS_ERR(nd->intent.open.file)) {
1752 mutex_unlock(&dir->d_inode->i_mutex);
1753 error = PTR_ERR(nd->intent.open.file);
1757 /* Negative dentry, just create the file */
1758 if (!path.dentry->d_inode) {
1759 error = open_namei_create(nd, &path, flag, mode);
1766 * It already exists.
1768 mutex_unlock(&dir->d_inode->i_mutex);
1769 audit_inode(pathname, path.dentry->d_inode);
1775 if (__follow_mount(&path)) {
1777 if (flag & O_NOFOLLOW)
1782 if (!path.dentry->d_inode)
1784 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
1787 path_to_nameidata(&path, nd);
1789 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
1792 error = may_open(nd, acc_mode, flag);
1798 dput_path(&path, nd);
1800 if (!IS_ERR(nd->intent.open.file))
1801 release_open_intent(nd);
1807 if (flag & O_NOFOLLOW)
1810 * This is subtle. Instead of calling do_follow_link() we do the
1811 * thing by hands. The reason is that this way we have zero link_count
1812 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1813 * After that we have the parent and last component, i.e.
1814 * we are in the same situation as after the first path_walk().
1815 * Well, almost - if the last component is normal we get its copy
1816 * stored in nd->last.name and we will have to putname() it when we
1817 * are done. Procfs-like symlinks just set LAST_BIND.
1819 nd->flags |= LOOKUP_PARENT;
1820 error = security_inode_follow_link(path.dentry, nd);
1823 error = __do_follow_link(&path, nd);
1825 /* Does someone understand code flow here? Or it is only
1826 * me so stupid? Anathema to whoever designed this non-sense
1827 * with "intent.open".
1829 release_open_intent(nd);
1832 nd->flags &= ~LOOKUP_PARENT;
1833 if (nd->last_type == LAST_BIND)
1836 if (nd->last_type != LAST_NORM)
1838 if (nd->last.name[nd->last.len]) {
1839 __putname(nd->last.name);
1844 __putname(nd->last.name);
1848 mutex_lock(&dir->d_inode->i_mutex);
1849 path.dentry = lookup_hash(nd);
1851 __putname(nd->last.name);
1856 * lookup_create - lookup a dentry, creating it if it doesn't exist
1857 * @nd: nameidata info
1858 * @is_dir: directory flag
1860 * Simple function to lookup and return a dentry and create it
1861 * if it doesn't exist. Is SMP-safe.
1863 * Returns with nd->dentry->d_inode->i_mutex locked.
1865 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1867 struct dentry *dentry = ERR_PTR(-EEXIST);
1869 mutex_lock_nested(&nd->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
1871 * Yucky last component or no last component at all?
1872 * (foo/., foo/.., /////)
1874 if (nd->last_type != LAST_NORM)
1876 nd->flags &= ~LOOKUP_PARENT;
1877 nd->flags |= LOOKUP_CREATE;
1878 nd->intent.open.flags = O_EXCL;
1881 * Do the final lookup.
1883 dentry = lookup_hash(nd);
1888 * Special case - lookup gave negative, but... we had foo/bar/
1889 * From the vfs_mknod() POV we just have a negative dentry -
1890 * all is fine. Let's be bastards - you had / on the end, you've
1891 * been asking for (non-existent) directory. -ENOENT for you.
1893 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1898 dentry = ERR_PTR(-ENOENT);
1902 EXPORT_SYMBOL_GPL(lookup_create);
1904 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1906 int error = may_create(dir, dentry, NULL);
1911 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1914 if (!dir->i_op || !dir->i_op->mknod)
1917 error = security_inode_mknod(dir, dentry, mode, dev);
1922 error = dir->i_op->mknod(dir, dentry, mode, dev);
1924 fsnotify_create(dir, dentry);
1928 asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
1933 struct dentry * dentry;
1934 struct nameidata nd;
1938 tmp = getname(filename);
1940 return PTR_ERR(tmp);
1942 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
1945 dentry = lookup_create(&nd, 0);
1946 error = PTR_ERR(dentry);
1948 if (!IS_POSIXACL(nd.dentry->d_inode))
1949 mode &= ~current->fs->umask;
1950 if (!IS_ERR(dentry)) {
1951 switch (mode & S_IFMT) {
1952 case 0: case S_IFREG:
1953 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1955 case S_IFCHR: case S_IFBLK:
1956 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1957 new_decode_dev(dev));
1959 case S_IFIFO: case S_IFSOCK:
1960 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1970 mutex_unlock(&nd.dentry->d_inode->i_mutex);
1978 asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
1980 return sys_mknodat(AT_FDCWD, filename, mode, dev);
1983 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1985 int error = may_create(dir, dentry, NULL);
1990 if (!dir->i_op || !dir->i_op->mkdir)
1993 mode &= (S_IRWXUGO|S_ISVTX);
1994 error = security_inode_mkdir(dir, dentry, mode);
1999 error = dir->i_op->mkdir(dir, dentry, mode);
2001 fsnotify_mkdir(dir, dentry);
2005 asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
2009 struct dentry *dentry;
2010 struct nameidata nd;
2012 tmp = getname(pathname);
2013 error = PTR_ERR(tmp);
2017 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
2020 dentry = lookup_create(&nd, 1);
2021 error = PTR_ERR(dentry);
2025 if (!IS_POSIXACL(nd.dentry->d_inode))
2026 mode &= ~current->fs->umask;
2027 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
2030 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2038 asmlinkage long sys_mkdir(const char __user *pathname, int mode)
2040 return sys_mkdirat(AT_FDCWD, pathname, mode);
2044 * We try to drop the dentry early: we should have
2045 * a usage count of 2 if we're the only user of this
2046 * dentry, and if that is true (possibly after pruning
2047 * the dcache), then we drop the dentry now.
2049 * A low-level filesystem can, if it choses, legally
2052 * if (!d_unhashed(dentry))
2055 * if it cannot handle the case of removing a directory
2056 * that is still in use by something else..
2058 void dentry_unhash(struct dentry *dentry)
2061 shrink_dcache_parent(dentry);
2062 spin_lock(&dcache_lock);
2063 spin_lock(&dentry->d_lock);
2064 if (atomic_read(&dentry->d_count) == 2)
2066 spin_unlock(&dentry->d_lock);
2067 spin_unlock(&dcache_lock);
2070 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2072 int error = may_delete(dir, dentry, 1);
2077 if (!dir->i_op || !dir->i_op->rmdir)
2082 mutex_lock(&dentry->d_inode->i_mutex);
2083 dentry_unhash(dentry);
2084 if (d_mountpoint(dentry))
2087 error = security_inode_rmdir(dir, dentry);
2089 error = dir->i_op->rmdir(dir, dentry);
2091 dentry->d_inode->i_flags |= S_DEAD;
2094 mutex_unlock(&dentry->d_inode->i_mutex);
2103 static long do_rmdir(int dfd, const char __user *pathname)
2107 struct dentry *dentry;
2108 struct nameidata nd;
2110 name = getname(pathname);
2112 return PTR_ERR(name);
2114 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
2118 switch(nd.last_type) {
2129 mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2130 dentry = lookup_hash(&nd);
2131 error = PTR_ERR(dentry);
2134 error = vfs_rmdir(nd.dentry->d_inode, dentry);
2137 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2145 asmlinkage long sys_rmdir(const char __user *pathname)
2147 return do_rmdir(AT_FDCWD, pathname);
2150 int vfs_unlink(struct inode *dir, struct dentry *dentry)
2152 int error = may_delete(dir, dentry, 0);
2157 if (!dir->i_op || !dir->i_op->unlink)
2162 mutex_lock(&dentry->d_inode->i_mutex);
2163 if (d_mountpoint(dentry))
2166 error = security_inode_unlink(dir, dentry);
2168 error = dir->i_op->unlink(dir, dentry);
2170 mutex_unlock(&dentry->d_inode->i_mutex);
2172 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2173 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2181 * Make sure that the actual truncation of the file will occur outside its
2182 * directory's i_mutex. Truncate can take a long time if there is a lot of
2183 * writeout happening, and we don't want to prevent access to the directory
2184 * while waiting on the I/O.
2186 static long do_unlinkat(int dfd, const char __user *pathname)
2190 struct dentry *dentry;
2191 struct nameidata nd;
2192 struct inode *inode = NULL;
2194 name = getname(pathname);
2196 return PTR_ERR(name);
2198 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
2202 if (nd.last_type != LAST_NORM)
2204 mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2205 dentry = lookup_hash(&nd);
2206 error = PTR_ERR(dentry);
2207 if (!IS_ERR(dentry)) {
2208 /* Why not before? Because we want correct error value */
2209 if (nd.last.name[nd.last.len])
2211 inode = dentry->d_inode;
2213 atomic_inc(&inode->i_count);
2214 error = vfs_unlink(nd.dentry->d_inode, dentry);
2218 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2220 iput(inode); /* truncate the inode here */
2228 error = !dentry->d_inode ? -ENOENT :
2229 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2233 asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
2235 if ((flag & ~AT_REMOVEDIR) != 0)
2238 if (flag & AT_REMOVEDIR)
2239 return do_rmdir(dfd, pathname);
2241 return do_unlinkat(dfd, pathname);
2244 asmlinkage long sys_unlink(const char __user *pathname)
2246 return do_unlinkat(AT_FDCWD, pathname);
2249 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
2251 int error = may_create(dir, dentry, NULL);
2256 if (!dir->i_op || !dir->i_op->symlink)
2259 error = security_inode_symlink(dir, dentry, oldname);
2264 error = dir->i_op->symlink(dir, dentry, oldname);
2266 fsnotify_create(dir, dentry);
2270 asmlinkage long sys_symlinkat(const char __user *oldname,
2271 int newdfd, const char __user *newname)
2276 struct dentry *dentry;
2277 struct nameidata nd;
2279 from = getname(oldname);
2281 return PTR_ERR(from);
2282 to = getname(newname);
2283 error = PTR_ERR(to);
2287 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2290 dentry = lookup_create(&nd, 0);
2291 error = PTR_ERR(dentry);
2295 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
2298 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2307 asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
2309 return sys_symlinkat(oldname, AT_FDCWD, newname);
2312 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2314 struct inode *inode = old_dentry->d_inode;
2320 error = may_create(dir, new_dentry, NULL);
2324 if (dir->i_sb != inode->i_sb)
2328 * A link to an append-only or immutable file cannot be created.
2330 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2332 if (!dir->i_op || !dir->i_op->link)
2334 if (S_ISDIR(old_dentry->d_inode->i_mode))
2337 error = security_inode_link(old_dentry, dir, new_dentry);
2341 mutex_lock(&old_dentry->d_inode->i_mutex);
2343 error = dir->i_op->link(old_dentry, dir, new_dentry);
2344 mutex_unlock(&old_dentry->d_inode->i_mutex);
2346 fsnotify_create(dir, new_dentry);
2351 * Hardlinks are often used in delicate situations. We avoid
2352 * security-related surprises by not following symlinks on the
2355 * We don't follow them on the oldname either to be compatible
2356 * with linux 2.0, and to avoid hard-linking to directories
2357 * and other special files. --ADM
2359 asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
2360 int newdfd, const char __user *newname,
2363 struct dentry *new_dentry;
2364 struct nameidata nd, old_nd;
2368 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2371 to = getname(newname);
2375 error = __user_walk_fd(olddfd, oldname,
2376 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2380 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2384 if (old_nd.mnt != nd.mnt)
2386 new_dentry = lookup_create(&nd, 0);
2387 error = PTR_ERR(new_dentry);
2388 if (IS_ERR(new_dentry))
2390 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2393 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2397 path_release(&old_nd);
2404 asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
2406 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
2410 * The worst of all namespace operations - renaming directory. "Perverted"
2411 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2413 * a) we can get into loop creation. Check is done in is_subdir().
2414 * b) race potential - two innocent renames can create a loop together.
2415 * That's where 4.4 screws up. Current fix: serialization on
2416 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
2418 * c) we have to lock _three_ objects - parents and victim (if it exists).
2419 * And that - after we got ->i_mutex on parents (until then we don't know
2420 * whether the target exists). Solution: try to be smart with locking
2421 * order for inodes. We rely on the fact that tree topology may change
2422 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
2423 * move will be locked. Thus we can rank directories by the tree
2424 * (ancestors first) and rank all non-directories after them.
2425 * That works since everybody except rename does "lock parent, lookup,
2426 * lock child" and rename is under ->s_vfs_rename_mutex.
2427 * HOWEVER, it relies on the assumption that any object with ->lookup()
2428 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2429 * we'd better make sure that there's no link(2) for them.
2430 * d) some filesystems don't support opened-but-unlinked directories,
2431 * either because of layout or because they are not ready to deal with
2432 * all cases correctly. The latter will be fixed (taking this sort of
2433 * stuff into VFS), but the former is not going away. Solution: the same
2434 * trick as in rmdir().
2435 * e) conversion from fhandle to dentry may come in the wrong moment - when
2436 * we are removing the target. Solution: we will have to grab ->i_mutex
2437 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2438 * ->i_mutex on parents, which works but leads to some truely excessive
2441 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2442 struct inode *new_dir, struct dentry *new_dentry)
2445 struct inode *target;
2448 * If we are going to change the parent - check write permissions,
2449 * we'll need to flip '..'.
2451 if (new_dir != old_dir) {
2452 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2457 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2461 target = new_dentry->d_inode;
2463 mutex_lock(&target->i_mutex);
2464 dentry_unhash(new_dentry);
2466 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2469 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2472 target->i_flags |= S_DEAD;
2473 mutex_unlock(&target->i_mutex);
2474 if (d_unhashed(new_dentry))
2475 d_rehash(new_dentry);
2479 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2480 d_move(old_dentry,new_dentry);
2484 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2485 struct inode *new_dir, struct dentry *new_dentry)
2487 struct inode *target;
2490 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2495 target = new_dentry->d_inode;
2497 mutex_lock(&target->i_mutex);
2498 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2501 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2503 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2504 d_move(old_dentry, new_dentry);
2507 mutex_unlock(&target->i_mutex);
2512 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2513 struct inode *new_dir, struct dentry *new_dentry)
2516 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2517 const char *old_name;
2519 if (old_dentry->d_inode == new_dentry->d_inode)
2522 error = may_delete(old_dir, old_dentry, is_dir);
2526 if (!new_dentry->d_inode)
2527 error = may_create(new_dir, new_dentry, NULL);
2529 error = may_delete(new_dir, new_dentry, is_dir);
2533 if (!old_dir->i_op || !old_dir->i_op->rename)
2536 DQUOT_INIT(old_dir);
2537 DQUOT_INIT(new_dir);
2539 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2542 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2544 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2546 const char *new_name = old_dentry->d_name.name;
2547 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
2548 new_dentry->d_inode, old_dentry->d_inode);
2550 fsnotify_oldname_free(old_name);
2555 static int do_rename(int olddfd, const char *oldname,
2556 int newdfd, const char *newname)
2559 struct dentry * old_dir, * new_dir;
2560 struct dentry * old_dentry, *new_dentry;
2561 struct dentry * trap;
2562 struct nameidata oldnd, newnd;
2564 error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd);
2568 error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
2573 if (oldnd.mnt != newnd.mnt)
2576 old_dir = oldnd.dentry;
2578 if (oldnd.last_type != LAST_NORM)
2581 new_dir = newnd.dentry;
2582 if (newnd.last_type != LAST_NORM)
2585 trap = lock_rename(new_dir, old_dir);
2587 old_dentry = lookup_hash(&oldnd);
2588 error = PTR_ERR(old_dentry);
2589 if (IS_ERR(old_dentry))
2591 /* source must exist */
2593 if (!old_dentry->d_inode)
2595 /* unless the source is a directory trailing slashes give -ENOTDIR */
2596 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2598 if (oldnd.last.name[oldnd.last.len])
2600 if (newnd.last.name[newnd.last.len])
2603 /* source should not be ancestor of target */
2605 if (old_dentry == trap)
2607 new_dentry = lookup_hash(&newnd);
2608 error = PTR_ERR(new_dentry);
2609 if (IS_ERR(new_dentry))
2611 /* target should not be an ancestor of source */
2613 if (new_dentry == trap)
2616 error = vfs_rename(old_dir->d_inode, old_dentry,
2617 new_dir->d_inode, new_dentry);
2623 unlock_rename(new_dir, old_dir);
2625 path_release(&newnd);
2627 path_release(&oldnd);
2632 asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
2633 int newdfd, const char __user *newname)
2639 from = getname(oldname);
2641 return PTR_ERR(from);
2642 to = getname(newname);
2643 error = PTR_ERR(to);
2645 error = do_rename(olddfd, from, newdfd, to);
2652 asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
2654 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
2657 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2661 len = PTR_ERR(link);
2666 if (len > (unsigned) buflen)
2668 if (copy_to_user(buffer, link, len))
2675 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2676 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2677 * using) it for any given inode is up to filesystem.
2679 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2681 struct nameidata nd;
2685 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2686 if (!IS_ERR(cookie)) {
2687 int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2688 if (dentry->d_inode->i_op->put_link)
2689 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2690 cookie = ERR_PTR(res);
2692 return PTR_ERR(cookie);
2695 int vfs_follow_link(struct nameidata *nd, const char *link)
2697 return __vfs_follow_link(nd, link);
2700 /* get the link contents into pagecache */
2701 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2704 struct address_space *mapping = dentry->d_inode->i_mapping;
2705 page = read_mapping_page(mapping, 0, NULL);
2712 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2714 struct page *page = NULL;
2715 char *s = page_getlink(dentry, &page);
2716 int res = vfs_readlink(dentry,buffer,buflen,s);
2719 page_cache_release(page);
2724 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2726 struct page *page = NULL;
2727 nd_set_link(nd, page_getlink(dentry, &page));
2731 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
2733 struct page *page = cookie;
2737 page_cache_release(page);
2741 int __page_symlink(struct inode *inode, const char *symname, int len,
2744 struct address_space *mapping = inode->i_mapping;
2751 err = pagecache_write_begin(NULL, mapping, 0, len-1,
2752 AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
2756 kaddr = kmap_atomic(page, KM_USER0);
2757 memcpy(kaddr, symname, len-1);
2758 kunmap_atomic(kaddr, KM_USER0);
2760 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2767 mark_inode_dirty(inode);
2773 int page_symlink(struct inode *inode, const char *symname, int len)
2775 return __page_symlink(inode, symname, len,
2776 mapping_gfp_mask(inode->i_mapping));
2779 const struct inode_operations page_symlink_inode_operations = {
2780 .readlink = generic_readlink,
2781 .follow_link = page_follow_link_light,
2782 .put_link = page_put_link,
2785 EXPORT_SYMBOL(__user_walk);
2786 EXPORT_SYMBOL(__user_walk_fd);
2787 EXPORT_SYMBOL(follow_down);
2788 EXPORT_SYMBOL(follow_up);
2789 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2790 EXPORT_SYMBOL(getname);
2791 EXPORT_SYMBOL(lock_rename);
2792 EXPORT_SYMBOL(lookup_one_len);
2793 EXPORT_SYMBOL(page_follow_link_light);
2794 EXPORT_SYMBOL(page_put_link);
2795 EXPORT_SYMBOL(page_readlink);
2796 EXPORT_SYMBOL(__page_symlink);
2797 EXPORT_SYMBOL(page_symlink);
2798 EXPORT_SYMBOL(page_symlink_inode_operations);
2799 EXPORT_SYMBOL(path_lookup);
2800 EXPORT_SYMBOL(vfs_path_lookup);
2801 EXPORT_SYMBOL(path_release);
2802 EXPORT_SYMBOL(permission);
2803 EXPORT_SYMBOL(vfs_permission);
2804 EXPORT_SYMBOL(file_permission);
2805 EXPORT_SYMBOL(unlock_rename);
2806 EXPORT_SYMBOL(vfs_create);
2807 EXPORT_SYMBOL(vfs_follow_link);
2808 EXPORT_SYMBOL(vfs_link);
2809 EXPORT_SYMBOL(vfs_mkdir);
2810 EXPORT_SYMBOL(vfs_mknod);
2811 EXPORT_SYMBOL(generic_permission);
2812 EXPORT_SYMBOL(vfs_readlink);
2813 EXPORT_SYMBOL(vfs_rename);
2814 EXPORT_SYMBOL(vfs_rmdir);
2815 EXPORT_SYMBOL(vfs_symlink);
2816 EXPORT_SYMBOL(vfs_unlink);
2817 EXPORT_SYMBOL(dentry_unhash);
2818 EXPORT_SYMBOL(generic_readlink);