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/smp_lock.h>
26 #include <linux/personality.h>
27 #include <linux/security.h>
28 #include <linux/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <linux/capability.h>
32 #include <linux/file.h>
33 #include <linux/fcntl.h>
34 #include <linux/namei.h>
35 #include <linux/proc_fs.h>
36 #include <linux/vs_base.h>
37 #include <linux/vserver/inode.h>
38 #include <linux/vserver/debug.h>
39 #include <asm/namei.h>
40 #include <asm/uaccess.h>
42 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
44 /* [Feb-1997 T. Schoebel-Theuer]
45 * Fundamental changes in the pathname lookup mechanisms (namei)
46 * were necessary because of omirr. The reason is that omirr needs
47 * to know the _real_ pathname, not the user-supplied one, in case
48 * of symlinks (and also when transname replacements occur).
50 * The new code replaces the old recursive symlink resolution with
51 * an iterative one (in case of non-nested symlink chains). It does
52 * this with calls to <fs>_follow_link().
53 * As a side effect, dir_namei(), _namei() and follow_link() are now
54 * replaced with a single function lookup_dentry() that can handle all
55 * the special cases of the former code.
57 * With the new dcache, the pathname is stored at each inode, at least as
58 * long as the refcount of the inode is positive. As a side effect, the
59 * size of the dcache depends on the inode cache and thus is dynamic.
61 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
62 * resolution to correspond with current state of the code.
64 * Note that the symlink resolution is not *completely* iterative.
65 * There is still a significant amount of tail- and mid- recursion in
66 * the algorithm. Also, note that <fs>_readlink() is not used in
67 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
68 * may return different results than <fs>_follow_link(). Many virtual
69 * filesystems (including /proc) exhibit this behavior.
72 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
73 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
74 * and the name already exists in form of a symlink, try to create the new
75 * name indicated by the symlink. The old code always complained that the
76 * name already exists, due to not following the symlink even if its target
77 * is nonexistent. The new semantics affects also mknod() and link() when
78 * the name is a symlink pointing to a non-existant name.
80 * I don't know which semantics is the right one, since I have no access
81 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
82 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
83 * "old" one. Personally, I think the new semantics is much more logical.
84 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
85 * file does succeed in both HP-UX and SunOs, but not in Solaris
86 * and in the old Linux semantics.
89 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
90 * semantics. See the comments in "open_namei" and "do_link" below.
92 * [10-Sep-98 Alan Modra] Another symlink change.
95 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
96 * inside the path - always follow.
97 * in the last component in creation/removal/renaming - never follow.
98 * if LOOKUP_FOLLOW passed - follow.
99 * if the pathname has trailing slashes - follow.
100 * otherwise - don't follow.
101 * (applied in that order).
103 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
104 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
105 * During the 2.4 we need to fix the userland stuff depending on it -
106 * hopefully we will be able to get rid of that wart in 2.5. So far only
107 * XEmacs seems to be relying on it...
110 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
111 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
112 * any extra contention...
115 /* In order to reduce some races, while at the same time doing additional
116 * checking and hopefully speeding things up, we copy filenames to the
117 * kernel data space before using them..
119 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
120 * PATH_MAX includes the nul terminator --RR.
122 static int do_getname(const char __user *filename, char *page)
125 unsigned long len = PATH_MAX;
127 if (!segment_eq(get_fs(), KERNEL_DS)) {
128 if ((unsigned long) filename >= TASK_SIZE)
130 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
131 len = TASK_SIZE - (unsigned long) filename;
134 retval = strncpy_from_user(page, filename, len);
138 return -ENAMETOOLONG;
144 char * getname(const char __user * filename)
148 result = ERR_PTR(-ENOMEM);
151 int retval = do_getname(filename, tmp);
156 result = ERR_PTR(retval);
159 audit_getname(result);
163 #ifdef CONFIG_AUDITSYSCALL
164 void putname(const char *name)
166 if (unlikely(current->audit_context))
171 EXPORT_SYMBOL(putname);
176 * generic_permission - check for access rights on a Posix-like filesystem
177 * @inode: inode to check access rights for
178 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
179 * @check_acl: optional callback to check for Posix ACLs
181 * Used to check for read/write/execute permissions on a file.
182 * We use "fsuid" for this, letting us set arbitrary permissions
183 * for filesystem access without changing the "normal" uids which
184 * are used for other things..
186 int generic_permission(struct inode *inode, int mask,
187 int (*check_acl)(struct inode *inode, int mask))
189 umode_t mode = inode->i_mode;
191 if (current->fsuid == inode->i_uid)
194 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
195 int error = check_acl(inode, mask);
196 if (error == -EACCES)
197 goto check_capabilities;
198 else if (error != -EAGAIN)
202 if (in_group_p(inode->i_gid))
207 * If the DACs are ok we don't need any capability check.
209 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
214 * Read/write DACs are always overridable.
215 * Executable DACs are overridable if at least one exec bit is set.
217 if (!(mask & MAY_EXEC) ||
218 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
219 if (capable(CAP_DAC_OVERRIDE))
223 * Searching includes executable on directories, else just read.
225 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
226 if (capable(CAP_DAC_READ_SEARCH))
232 static inline int vx_barrier(struct inode *inode)
234 if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN|VX_IDENT)) {
235 vxwprintk(1, "xid=%d did hit the barrier.",
242 static inline int xid_permission(struct inode *inode, int mask, struct nameidata *nd)
244 if (vx_barrier(inode))
246 if (inode->i_xid == 0)
248 #ifdef CONFIG_VSERVER_FILESHARING
249 /* MEF: PlanetLab FS module assumes that any file that can be
250 * named (e.g., via a cross mount) is not hidden from another
251 * context or the admin context.
253 if (vx_check(inode->i_xid,VX_STATIC|VX_DYNAMIC))
256 if (vx_check(inode->i_xid, VX_ADMIN|VX_WATCH|VX_IDENT))
259 vxwprintk(1, "xid=%d denied access to %p[#%d,%lu] »%s«.",
260 vx_current_xid(), inode, inode->i_xid, inode->i_ino,
265 int permission(struct inode *inode, int mask, struct nameidata *nd)
269 if (mask & MAY_WRITE) {
270 umode_t mode = inode->i_mode;
273 * Nobody gets write access to a read-only fs.
275 if ((IS_RDONLY(inode) || (nd && MNT_IS_RDONLY(nd->mnt))) &&
276 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
280 * Nobody gets write access to an immutable file.
282 if (IS_IMMUTABLE(inode))
287 /* Ordinary permission routines do not understand MAY_APPEND. */
288 submask = mask & ~MAY_APPEND;
289 if ((retval = xid_permission(inode, mask, nd)))
291 if (inode->i_op && inode->i_op->permission)
292 retval = inode->i_op->permission(inode, submask, nd);
294 retval = generic_permission(inode, submask, NULL);
298 return security_inode_permission(inode, mask, nd);
302 * vfs_permission - check for access rights to a given path
303 * @nd: lookup result that describes the path
304 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
306 * Used to check for read/write/execute permissions on a path.
307 * We use "fsuid" for this, letting us set arbitrary permissions
308 * for filesystem access without changing the "normal" uids which
309 * are used for other things.
311 int vfs_permission(struct nameidata *nd, int mask)
313 return permission(nd->dentry->d_inode, mask, nd);
317 * file_permission - check for additional access rights to a given file
318 * @file: file to check access rights for
319 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
321 * Used to check for read/write/execute permissions on an already opened
325 * Do not use this function in new code. All access checks should
326 * be done using vfs_permission().
328 int file_permission(struct file *file, int mask)
330 return permission(file->f_dentry->d_inode, mask, NULL);
334 * get_write_access() gets write permission for a file.
335 * put_write_access() releases this write permission.
336 * This is used for regular files.
337 * We cannot support write (and maybe mmap read-write shared) accesses and
338 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
339 * can have the following values:
340 * 0: no writers, no VM_DENYWRITE mappings
341 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
342 * > 0: (i_writecount) users are writing to the file.
344 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
345 * except for the cases where we don't hold i_writecount yet. Then we need to
346 * use {get,deny}_write_access() - these functions check the sign and refuse
347 * to do the change if sign is wrong. Exclusion between them is provided by
348 * the inode->i_lock spinlock.
351 int get_write_access(struct inode * inode)
353 spin_lock(&inode->i_lock);
354 if (atomic_read(&inode->i_writecount) < 0) {
355 spin_unlock(&inode->i_lock);
358 atomic_inc(&inode->i_writecount);
359 spin_unlock(&inode->i_lock);
364 int deny_write_access(struct file * file)
366 struct inode *inode = file->f_dentry->d_inode;
368 spin_lock(&inode->i_lock);
369 if (atomic_read(&inode->i_writecount) > 0) {
370 spin_unlock(&inode->i_lock);
373 atomic_dec(&inode->i_writecount);
374 spin_unlock(&inode->i_lock);
379 void path_release(struct nameidata *nd)
386 * umount() mustn't call path_release()/mntput() as that would clear
389 void path_release_on_umount(struct nameidata *nd)
392 mntput_no_expire(nd->mnt);
396 * release_open_intent - free up open intent resources
397 * @nd: pointer to nameidata
399 void release_open_intent(struct nameidata *nd)
401 if (nd->intent.open.file->f_dentry == NULL)
402 put_filp(nd->intent.open.file);
404 fput(nd->intent.open.file);
408 * Internal lookup() using the new generic dcache.
411 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
413 struct dentry * dentry = __d_lookup(parent, name);
415 /* lockess __d_lookup may fail due to concurrent d_move()
416 * in some unrelated directory, so try with d_lookup
419 dentry = d_lookup(parent, name);
421 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
422 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
431 * Short-cut version of permission(), for calling by
432 * path_walk(), when dcache lock is held. Combines parts
433 * of permission() and generic_permission(), and tests ONLY for
434 * MAY_EXEC permission.
436 * If appropriate, check DAC only. If not appropriate, or
437 * short-cut DAC fails, then call permission() to do more
438 * complete permission check.
440 static int exec_permission_lite(struct inode *inode,
441 struct nameidata *nd)
443 umode_t mode = inode->i_mode;
445 if (vx_barrier(inode))
447 if (inode->i_op && inode->i_op->permission)
450 if (current->fsuid == inode->i_uid)
452 else if (in_group_p(inode->i_gid))
458 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
461 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
464 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
469 return security_inode_permission(inode, MAY_EXEC, nd);
473 * This is called when everything else fails, and we actually have
474 * to go to the low-level filesystem to find out what we should do..
476 * We get the directory semaphore, and after getting that we also
477 * make sure that nobody added the entry to the dcache in the meantime..
480 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
482 struct dentry * result;
483 struct inode *dir = parent->d_inode;
485 mutex_lock(&dir->i_mutex);
487 * First re-do the cached lookup just in case it was created
488 * while we waited for the directory semaphore..
490 * FIXME! This could use version numbering or similar to
491 * avoid unnecessary cache lookups.
493 * The "dcache_lock" is purely to protect the RCU list walker
494 * from concurrent renames at this point (we mustn't get false
495 * negatives from the RCU list walk here, unlike the optimistic
498 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
500 result = d_lookup(parent, name);
502 struct dentry * dentry = d_alloc(parent, name);
503 result = ERR_PTR(-ENOMEM);
505 result = dir->i_op->lookup(dir, dentry, nd);
511 mutex_unlock(&dir->i_mutex);
516 * Uhhuh! Nasty case: the cache was re-populated while
517 * we waited on the semaphore. Need to revalidate.
519 mutex_unlock(&dir->i_mutex);
520 if (result->d_op && result->d_op->d_revalidate) {
521 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
523 result = ERR_PTR(-ENOENT);
529 static int __emul_lookup_dentry(const char *, struct nameidata *);
532 static __always_inline int
533 walk_init_root(const char *name, struct nameidata *nd)
535 read_lock(¤t->fs->lock);
536 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
537 nd->mnt = mntget(current->fs->altrootmnt);
538 nd->dentry = dget(current->fs->altroot);
539 read_unlock(¤t->fs->lock);
540 if (__emul_lookup_dentry(name,nd))
542 read_lock(¤t->fs->lock);
544 nd->mnt = mntget(current->fs->rootmnt);
545 nd->dentry = dget(current->fs->root);
546 read_unlock(¤t->fs->lock);
550 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
559 if (!walk_init_root(link, nd))
560 /* weird __emul_prefix() stuff did it */
563 res = link_path_walk(link, nd);
565 if (nd->depth || res || nd->last_type!=LAST_NORM)
568 * If it is an iterative symlinks resolution in open_namei() we
569 * have to copy the last component. And all that crap because of
570 * bloody create() on broken symlinks. Furrfu...
573 if (unlikely(!name)) {
577 strcpy(name, nd->last.name);
578 nd->last.name = name;
582 return PTR_ERR(link);
586 struct vfsmount *mnt;
587 struct dentry *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)
601 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
602 error = PTR_ERR(cookie);
603 if (!IS_ERR(cookie)) {
604 char *s = nd_get_link(nd);
607 error = __vfs_follow_link(nd, s);
608 if (dentry->d_inode->i_op->put_link)
609 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
617 static inline void dput_path(struct path *path, struct nameidata *nd)
620 if (path->mnt != nd->mnt)
624 static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
627 if (nd->mnt != path->mnt)
630 nd->dentry = path->dentry;
634 * This limits recursive symlink follows to 8, while
635 * limiting consecutive symlinks to 40.
637 * Without that kind of total limit, nasty chains of consecutive
638 * symlinks can cause almost arbitrarily long lookups.
640 static inline int do_follow_link(struct path *path, struct nameidata *nd)
643 if (current->link_count >= MAX_NESTED_LINKS)
645 if (current->total_link_count >= 40)
647 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
649 err = security_inode_follow_link(path->dentry, nd);
652 current->link_count++;
653 current->total_link_count++;
655 err = __do_follow_link(path, nd);
656 current->link_count--;
665 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
667 struct vfsmount *parent;
668 struct dentry *mountpoint;
669 spin_lock(&vfsmount_lock);
670 parent=(*mnt)->mnt_parent;
671 if (parent == *mnt) {
672 spin_unlock(&vfsmount_lock);
676 mountpoint=dget((*mnt)->mnt_mountpoint);
677 spin_unlock(&vfsmount_lock);
679 *dentry = mountpoint;
685 /* no need for dcache_lock, as serialization is taken care in
688 static int __follow_mount(struct path *path)
691 while (d_mountpoint(path->dentry)) {
692 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
699 path->dentry = dget(mounted->mnt_root);
705 static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
707 while (d_mountpoint(*dentry)) {
708 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
714 *dentry = dget(mounted->mnt_root);
718 /* no need for dcache_lock, as serialization is taken care in
721 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
723 struct vfsmount *mounted;
725 mounted = lookup_mnt(*mnt, *dentry);
730 *dentry = dget(mounted->mnt_root);
736 static __always_inline void follow_dotdot(struct nameidata *nd)
739 struct vfsmount *parent;
740 struct dentry *old = nd->dentry;
742 read_lock(¤t->fs->lock);
743 if (nd->dentry == current->fs->root &&
744 nd->mnt == current->fs->rootmnt) {
745 read_unlock(¤t->fs->lock);
746 /* for sane '/' avoid follow_mount() */
749 read_unlock(¤t->fs->lock);
750 spin_lock(&dcache_lock);
751 if (nd->dentry != nd->mnt->mnt_root) {
752 nd->dentry = dget(nd->dentry->d_parent);
753 spin_unlock(&dcache_lock);
757 spin_unlock(&dcache_lock);
758 spin_lock(&vfsmount_lock);
759 parent = nd->mnt->mnt_parent;
760 if (parent == nd->mnt) {
761 spin_unlock(&vfsmount_lock);
765 nd->dentry = dget(nd->mnt->mnt_mountpoint);
766 spin_unlock(&vfsmount_lock);
771 follow_mount(&nd->mnt, &nd->dentry);
775 * It's more convoluted than I'd like it to be, but... it's still fairly
776 * small and for now I'd prefer to have fast path as straight as possible.
777 * It _is_ time-critical.
779 static int do_lookup(struct nameidata *nd, struct qstr *name,
782 struct vfsmount *mnt = nd->mnt;
783 struct dentry *dentry = __d_lookup(nd->dentry, name);
788 if (dentry->d_op && dentry->d_op->d_revalidate)
789 goto need_revalidate;
790 inode = dentry->d_inode;
793 #ifdef CONFIG_VSERVER_FILESHARING
794 /* MEF: PlanetLab FS module assumes that any file that can be
795 * named (e.g., via a cross mount) is not hidden from another
796 * context or the admin context.
798 if (vx_check(inode->i_xid,VX_STATIC|VX_DYNAMIC|VX_ADMIN)) {
801 else /* do the following check */
803 if (!vx_check(inode->i_xid, VX_WATCH|VX_ADMIN|VX_HOSTID|VX_IDENT))
805 if (inode->i_sb->s_magic == PROC_SUPER_MAGIC) {
806 struct proc_dir_entry *de = PDE(inode);
808 if (de && !vx_hide_check(0, de->vx_flags))
813 path->dentry = dentry;
814 __follow_mount(path);
817 vxwprintk(1, "xid=%d did lookup hidden %p[#%d,%lu] »%s«.",
818 vx_current_xid(), inode, inode->i_xid, inode->i_ino,
819 vxd_path(dentry, mnt));
824 dentry = real_lookup(nd->dentry, name, nd);
830 if (dentry->d_op->d_revalidate(dentry, nd))
832 if (d_invalidate(dentry))
838 return PTR_ERR(dentry);
843 * This is the basic name resolution function, turning a pathname into
844 * the final dentry. We expect 'base' to be positive and a directory.
846 * Returns 0 and nd will have valid dentry and mnt on success.
847 * Returns error and drops reference to input namei data on failure.
849 static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
854 unsigned int lookup_flags = nd->flags;
861 inode = nd->dentry->d_inode;
863 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
865 /* At this point we know we have a real path component. */
871 nd->flags |= LOOKUP_CONTINUE;
872 err = exec_permission_lite(inode, nd);
874 err = vfs_permission(nd, MAY_EXEC);
879 c = *(const unsigned char *)name;
881 hash = init_name_hash();
884 hash = partial_name_hash(c, hash);
885 c = *(const unsigned char *)name;
886 } while (c && (c != '/'));
887 this.len = name - (const char *) this.name;
888 this.hash = end_name_hash(hash);
890 /* remove trailing slashes? */
893 while (*++name == '/');
895 goto last_with_slashes;
898 * "." and ".." are special - ".." especially so because it has
899 * to be able to know about the current root directory and
900 * parent relationships.
902 if (this.name[0] == '.') switch (this.len) {
906 if (this.name[1] != '.')
909 inode = nd->dentry->d_inode;
915 * See if the low-level filesystem might want
916 * to use its own hash..
918 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
919 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
923 /* This does the actual lookups.. */
924 err = do_lookup(nd, &this, &next);
929 inode = next.dentry->d_inode;
936 if (inode->i_op->follow_link) {
937 err = do_follow_link(&next, nd);
941 inode = nd->dentry->d_inode;
948 path_to_nameidata(&next, nd);
950 if (!inode->i_op->lookup)
953 /* here ends the main loop */
956 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
958 /* Clear LOOKUP_CONTINUE iff it was previously unset */
959 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
960 if (lookup_flags & LOOKUP_PARENT)
962 if (this.name[0] == '.') switch (this.len) {
966 if (this.name[1] != '.')
969 inode = nd->dentry->d_inode;
974 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
975 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
979 err = do_lookup(nd, &this, &next);
982 inode = next.dentry->d_inode;
983 if ((lookup_flags & LOOKUP_FOLLOW)
984 && inode && inode->i_op && inode->i_op->follow_link) {
985 err = do_follow_link(&next, nd);
988 inode = nd->dentry->d_inode;
990 path_to_nameidata(&next, nd);
994 if (lookup_flags & LOOKUP_DIRECTORY) {
996 if (!inode->i_op || !inode->i_op->lookup)
1002 nd->last_type = LAST_NORM;
1003 if (this.name[0] != '.')
1006 nd->last_type = LAST_DOT;
1007 else if (this.len == 2 && this.name[1] == '.')
1008 nd->last_type = LAST_DOTDOT;
1013 * We bypassed the ordinary revalidation routines.
1014 * We may need to check the cached dentry for staleness.
1016 if (nd->dentry && nd->dentry->d_sb &&
1017 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
1019 /* Note: we do not d_invalidate() */
1020 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
1026 dput_path(&next, nd);
1035 * Wrapper to retry pathname resolution whenever the underlying
1036 * file system returns an ESTALE.
1038 * Retry the whole path once, forcing real lookup requests
1039 * instead of relying on the dcache.
1041 int fastcall link_path_walk(const char *name, struct nameidata *nd)
1043 struct nameidata save = *nd;
1046 /* make sure the stuff we saved doesn't go away */
1050 result = __link_path_walk(name, nd);
1051 if (result == -ESTALE) {
1055 nd->flags |= LOOKUP_REVAL;
1056 result = __link_path_walk(name, nd);
1065 int fastcall path_walk(const char * name, struct nameidata *nd)
1067 current->total_link_count = 0;
1068 return link_path_walk(name, nd);
1072 * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
1073 * everything is done. Returns 0 and drops input nd, if lookup failed;
1075 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
1077 if (path_walk(name, nd))
1078 return 0; /* something went wrong... */
1080 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
1081 struct dentry *old_dentry = nd->dentry;
1082 struct vfsmount *old_mnt = nd->mnt;
1083 struct qstr last = nd->last;
1084 int last_type = nd->last_type;
1086 * NAME was not found in alternate root or it's a directory. Try to find
1087 * it in the normal root:
1089 nd->last_type = LAST_ROOT;
1090 read_lock(¤t->fs->lock);
1091 nd->mnt = mntget(current->fs->rootmnt);
1092 nd->dentry = dget(current->fs->root);
1093 read_unlock(¤t->fs->lock);
1094 if (path_walk(name, nd) == 0) {
1095 if (nd->dentry->d_inode) {
1102 nd->dentry = old_dentry;
1105 nd->last_type = last_type;
1110 void set_fs_altroot(void)
1112 char *emul = __emul_prefix();
1113 struct nameidata nd;
1114 struct vfsmount *mnt = NULL, *oldmnt;
1115 struct dentry *dentry = NULL, *olddentry;
1120 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
1126 write_lock(¤t->fs->lock);
1127 oldmnt = current->fs->altrootmnt;
1128 olddentry = current->fs->altroot;
1129 current->fs->altrootmnt = mnt;
1130 current->fs->altroot = dentry;
1131 write_unlock(¤t->fs->lock);
1138 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1139 static int fastcall do_path_lookup(int dfd, const char *name,
1140 unsigned int flags, struct nameidata *nd)
1146 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1151 read_lock(¤t->fs->lock);
1152 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
1153 nd->mnt = mntget(current->fs->altrootmnt);
1154 nd->dentry = dget(current->fs->altroot);
1155 read_unlock(¤t->fs->lock);
1156 if (__emul_lookup_dentry(name,nd))
1157 goto out; /* found in altroot */
1158 read_lock(¤t->fs->lock);
1160 nd->mnt = mntget(current->fs->rootmnt);
1161 nd->dentry = dget(current->fs->root);
1162 read_unlock(¤t->fs->lock);
1163 } else if (dfd == AT_FDCWD) {
1164 read_lock(¤t->fs->lock);
1165 nd->mnt = mntget(current->fs->pwdmnt);
1166 nd->dentry = dget(current->fs->pwd);
1167 read_unlock(¤t->fs->lock);
1169 struct dentry *dentry;
1171 file = fget_light(dfd, &fput_needed);
1176 dentry = file->f_dentry;
1179 if (!S_ISDIR(dentry->d_inode->i_mode))
1182 retval = file_permission(file, MAY_EXEC);
1186 nd->mnt = mntget(file->f_vfsmnt);
1187 nd->dentry = dget(dentry);
1189 fput_light(file, fput_needed);
1191 current->total_link_count = 0;
1192 retval = link_path_walk(name, nd);
1194 if (likely(retval == 0)) {
1195 if (unlikely(current->audit_context && nd && nd->dentry &&
1196 nd->dentry->d_inode))
1197 audit_inode(name, nd->dentry->d_inode, flags);
1203 fput_light(file, fput_needed);
1207 int fastcall path_lookup(const char *name, unsigned int flags,
1208 struct nameidata *nd)
1210 return do_path_lookup(AT_FDCWD, name, flags, nd);
1213 static int __path_lookup_intent_open(int dfd, const char *name,
1214 unsigned int lookup_flags, struct nameidata *nd,
1215 int open_flags, int create_mode)
1217 struct file *filp = get_empty_filp();
1222 nd->intent.open.file = filp;
1223 nd->intent.open.flags = open_flags;
1224 nd->intent.open.create_mode = create_mode;
1225 err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
1226 if (IS_ERR(nd->intent.open.file)) {
1228 err = PTR_ERR(nd->intent.open.file);
1231 } else if (err != 0)
1232 release_open_intent(nd);
1237 * path_lookup_open - lookup a file path with open intent
1238 * @dfd: the directory to use as base, or AT_FDCWD
1239 * @name: pointer to file name
1240 * @lookup_flags: lookup intent flags
1241 * @nd: pointer to nameidata
1242 * @open_flags: open intent flags
1244 int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
1245 struct nameidata *nd, int open_flags)
1247 return __path_lookup_intent_open(dfd, name, lookup_flags, nd,
1252 * path_lookup_create - lookup a file path with open + create intent
1253 * @dfd: the directory to use as base, or AT_FDCWD
1254 * @name: pointer to file name
1255 * @lookup_flags: lookup intent flags
1256 * @nd: pointer to nameidata
1257 * @open_flags: open intent flags
1258 * @create_mode: create intent flags
1260 static int path_lookup_create(int dfd, const char *name,
1261 unsigned int lookup_flags, struct nameidata *nd,
1262 int open_flags, int create_mode)
1264 return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE,
1265 nd, open_flags, create_mode);
1268 int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
1269 struct nameidata *nd, int open_flags)
1271 char *tmp = getname(name);
1272 int err = PTR_ERR(tmp);
1275 err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
1282 * Restricted form of lookup. Doesn't follow links, single-component only,
1283 * needs parent already locked. Doesn't follow mounts.
1286 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
1288 struct dentry * dentry;
1289 struct inode *inode;
1292 inode = base->d_inode;
1293 err = permission(inode, MAY_EXEC, nd);
1294 dentry = ERR_PTR(err);
1299 * See if the low-level filesystem might want
1300 * to use its own hash..
1302 if (base->d_op && base->d_op->d_hash) {
1303 err = base->d_op->d_hash(base, name);
1304 dentry = ERR_PTR(err);
1309 dentry = cached_lookup(base, name, nd);
1311 struct dentry *new = d_alloc(base, name);
1312 dentry = ERR_PTR(-ENOMEM);
1315 dentry = inode->i_op->lookup(inode, new, nd);
1325 struct dentry * lookup_hash(struct nameidata *nd)
1327 return __lookup_hash(&nd->last, nd->dentry, nd);
1331 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1342 hash = init_name_hash();
1344 c = *(const unsigned char *)name++;
1345 if (c == '/' || c == '\0')
1347 hash = partial_name_hash(c, hash);
1349 this.hash = end_name_hash(hash);
1351 return __lookup_hash(&this, base, NULL);
1353 return ERR_PTR(-EACCES);
1359 * is used by most simple commands to get the inode of a specified name.
1360 * Open, link etc use their own routines, but this is enough for things
1363 * namei exists in two versions: namei/lnamei. The only difference is
1364 * that namei follows links, while lnamei does not.
1367 int fastcall __user_walk_fd(int dfd, const char __user *name, unsigned flags,
1368 struct nameidata *nd)
1370 char *tmp = getname(name);
1371 int err = PTR_ERR(tmp);
1374 err = do_path_lookup(dfd, tmp, flags, nd);
1380 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1382 return __user_walk_fd(AT_FDCWD, name, flags, nd);
1386 * It's inline, so penalty for filesystems that don't use sticky bit is
1389 static inline int check_sticky(struct inode *dir, struct inode *inode)
1391 if (!(dir->i_mode & S_ISVTX))
1393 if (inode->i_uid == current->fsuid)
1395 if (dir->i_uid == current->fsuid)
1397 return !capable(CAP_FOWNER);
1401 * Check whether we can remove a link victim from directory dir, check
1402 * whether the type of victim is right.
1403 * 1. We can't do it if dir is read-only (done in permission())
1404 * 2. We should have write and exec permissions on dir
1405 * 3. We can't remove anything from append-only dir
1406 * 4. We can't do anything with immutable dir (done in permission())
1407 * 5. If the sticky bit on dir is set we should either
1408 * a. be owner of dir, or
1409 * b. be owner of victim, or
1410 * c. have CAP_FOWNER capability
1411 * 6. If the victim is append-only or immutable we can't do antyhing with
1412 * links pointing to it.
1413 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1414 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1415 * 9. We can't remove a root or mountpoint.
1416 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1417 * nfs_async_unlink().
1419 static int may_delete(struct inode *dir, struct dentry *victim,
1420 int isdir, struct nameidata *nd)
1424 if (!victim->d_inode)
1427 BUG_ON(victim->d_parent->d_inode != dir);
1429 error = permission(dir,MAY_WRITE | MAY_EXEC, nd);
1434 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1435 IS_IXORUNLINK(victim->d_inode))
1438 if (!S_ISDIR(victim->d_inode->i_mode))
1440 if (IS_ROOT(victim))
1442 } else if (S_ISDIR(victim->d_inode->i_mode))
1444 if (IS_DEADDIR(dir))
1446 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1451 /* Check whether we can create an object with dentry child in directory
1453 * 1. We can't do it if child already exists (open has special treatment for
1454 * this case, but since we are inlined it's OK)
1455 * 2. We can't do it if dir is read-only (done in permission())
1456 * 3. We should have write and exec permissions on dir
1457 * 4. We can't do it if dir is immutable (done in permission())
1459 static inline int may_create(struct inode *dir, struct dentry *child,
1460 struct nameidata *nd)
1464 if (IS_DEADDIR(dir))
1466 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1470 * O_DIRECTORY translates into forcing a directory lookup.
1472 static inline int lookup_flags(unsigned int f)
1474 unsigned long retval = LOOKUP_FOLLOW;
1477 retval &= ~LOOKUP_FOLLOW;
1479 if (f & O_DIRECTORY)
1480 retval |= LOOKUP_DIRECTORY;
1486 * p1 and p2 should be directories on the same fs.
1488 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1493 mutex_lock(&p1->d_inode->i_mutex);
1497 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1499 for (p = p1; p->d_parent != p; p = p->d_parent) {
1500 if (p->d_parent == p2) {
1501 mutex_lock(&p2->d_inode->i_mutex);
1502 mutex_lock(&p1->d_inode->i_mutex);
1507 for (p = p2; p->d_parent != p; p = p->d_parent) {
1508 if (p->d_parent == p1) {
1509 mutex_lock(&p1->d_inode->i_mutex);
1510 mutex_lock(&p2->d_inode->i_mutex);
1515 mutex_lock(&p1->d_inode->i_mutex);
1516 mutex_lock(&p2->d_inode->i_mutex);
1520 void unlock_rename(struct dentry *p1, struct dentry *p2)
1522 mutex_unlock(&p1->d_inode->i_mutex);
1524 mutex_unlock(&p2->d_inode->i_mutex);
1525 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1529 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1530 struct nameidata *nd)
1532 int error = may_create(dir, dentry, nd);
1537 if (!dir->i_op || !dir->i_op->create)
1538 return -EACCES; /* shouldn't it be ENOSYS? */
1541 error = security_inode_create(dir, dentry, mode);
1545 error = dir->i_op->create(dir, dentry, mode, nd);
1547 fsnotify_create(dir, dentry->d_name.name);
1551 int may_open(struct nameidata *nd, int acc_mode, int flag)
1553 struct dentry *dentry = nd->dentry;
1554 struct inode *inode = dentry->d_inode;
1560 if (S_ISLNK(inode->i_mode))
1563 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1566 error = vfs_permission(nd, acc_mode);
1571 * FIFO's, sockets and device files are special: they don't
1572 * actually live on the filesystem itself, and as such you
1573 * can write to them even if the filesystem is read-only.
1575 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1577 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1578 if (nd->mnt->mnt_flags & MNT_NODEV)
1582 } else if ((IS_RDONLY(inode) || MNT_IS_RDONLY(nd->mnt))
1583 && (flag & FMODE_WRITE))
1586 * An append-only file must be opened in append mode for writing.
1588 if (IS_APPEND(inode)) {
1589 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1595 /* O_NOATIME can only be set by the owner or superuser */
1596 if (flag & O_NOATIME)
1597 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1601 * Ensure there are no outstanding leases on the file.
1603 error = break_lease(inode, flag);
1607 if (flag & O_TRUNC) {
1608 error = get_write_access(inode);
1613 * Refuse to truncate files with mandatory locks held on them.
1615 error = locks_verify_locked(inode);
1619 error = do_truncate(dentry, 0, ATTR_MTIME|ATTR_CTIME, NULL);
1621 put_write_access(inode);
1625 if (flag & FMODE_WRITE)
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 if (!IS_POSIXACL(dir->d_inode))
1713 mode &= ~current->fs->umask;
1714 error = vfs_create(dir->d_inode, path.dentry, mode, nd);
1715 mutex_unlock(&dir->d_inode->i_mutex);
1717 nd->dentry = path.dentry;
1720 /* Don't check for write permission, don't truncate */
1727 * It already exists.
1729 mutex_unlock(&dir->d_inode->i_mutex);
1735 if (__follow_mount(&path)) {
1737 if (flag & O_NOFOLLOW)
1741 if (!path.dentry->d_inode)
1743 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
1746 path_to_nameidata(&path, nd);
1748 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
1751 error = may_open(nd, acc_mode, flag);
1757 dput_path(&path, nd);
1759 if (!IS_ERR(nd->intent.open.file))
1760 release_open_intent(nd);
1766 if (flag & O_NOFOLLOW)
1769 * This is subtle. Instead of calling do_follow_link() we do the
1770 * thing by hands. The reason is that this way we have zero link_count
1771 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1772 * After that we have the parent and last component, i.e.
1773 * we are in the same situation as after the first path_walk().
1774 * Well, almost - if the last component is normal we get its copy
1775 * stored in nd->last.name and we will have to putname() it when we
1776 * are done. Procfs-like symlinks just set LAST_BIND.
1778 nd->flags |= LOOKUP_PARENT;
1779 error = security_inode_follow_link(path.dentry, nd);
1782 error = __do_follow_link(&path, nd);
1784 /* Does someone understand code flow here? Or it is only
1785 * me so stupid? Anathema to whoever designed this non-sense
1786 * with "intent.open".
1788 release_open_intent(nd);
1791 nd->flags &= ~LOOKUP_PARENT;
1792 if (nd->last_type == LAST_BIND)
1795 if (nd->last_type != LAST_NORM)
1797 if (nd->last.name[nd->last.len]) {
1798 __putname(nd->last.name);
1803 __putname(nd->last.name);
1807 mutex_lock(&dir->d_inode->i_mutex);
1808 path.dentry = lookup_hash(nd);
1810 __putname(nd->last.name);
1815 * lookup_create - lookup a dentry, creating it if it doesn't exist
1816 * @nd: nameidata info
1817 * @is_dir: directory flag
1819 * Simple function to lookup and return a dentry and create it
1820 * if it doesn't exist. Is SMP-safe.
1822 * Returns with nd->dentry->d_inode->i_mutex locked.
1824 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1826 struct dentry *dentry = ERR_PTR(-EEXIST);
1828 mutex_lock(&nd->dentry->d_inode->i_mutex);
1830 * Yucky last component or no last component at all?
1831 * (foo/., foo/.., /////)
1833 if (nd->last_type != LAST_NORM)
1835 nd->flags &= ~LOOKUP_PARENT;
1838 * Do the final lookup.
1840 dentry = lookup_hash(nd);
1845 * Special case - lookup gave negative, but... we had foo/bar/
1846 * From the vfs_mknod() POV we just have a negative dentry -
1847 * all is fine. Let's be bastards - you had / on the end, you've
1848 * been asking for (non-existent) directory. -ENOENT for you.
1850 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1855 dentry = ERR_PTR(-ENOENT);
1859 EXPORT_SYMBOL_GPL(lookup_create);
1861 int vfs_mknod(struct inode *dir, struct dentry *dentry,
1862 int mode, dev_t dev, struct nameidata *nd)
1864 int error = may_create(dir, dentry, nd);
1869 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1872 if (!dir->i_op || !dir->i_op->mknod)
1875 error = security_inode_mknod(dir, dentry, mode, dev);
1880 error = dir->i_op->mknod(dir, dentry, mode, dev);
1882 fsnotify_create(dir, dentry->d_name.name);
1886 asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
1891 struct dentry * dentry;
1892 struct nameidata nd;
1896 tmp = getname(filename);
1898 return PTR_ERR(tmp);
1900 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
1903 dentry = lookup_create(&nd, 0);
1904 error = PTR_ERR(dentry);
1906 if (!IS_POSIXACL(nd.dentry->d_inode))
1907 mode &= ~current->fs->umask;
1908 if (!IS_ERR(dentry)) {
1909 switch (mode & S_IFMT) {
1910 case 0: case S_IFREG:
1911 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1913 case S_IFCHR: case S_IFBLK:
1914 error = vfs_mknod(nd.dentry->d_inode, dentry, mode,
1915 new_decode_dev(dev), &nd);
1917 case S_IFIFO: case S_IFSOCK:
1918 error = vfs_mknod(nd.dentry->d_inode, dentry, mode,
1929 mutex_unlock(&nd.dentry->d_inode->i_mutex);
1937 asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
1939 return sys_mknodat(AT_FDCWD, filename, mode, dev);
1942 int vfs_mkdir(struct inode *dir, struct dentry *dentry,
1943 int mode, struct nameidata *nd)
1945 int error = may_create(dir, dentry, nd);
1950 if (!dir->i_op || !dir->i_op->mkdir)
1953 mode &= (S_IRWXUGO|S_ISVTX);
1954 error = security_inode_mkdir(dir, dentry, mode);
1959 error = dir->i_op->mkdir(dir, dentry, mode);
1961 fsnotify_mkdir(dir, dentry->d_name.name);
1965 asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
1970 tmp = getname(pathname);
1971 error = PTR_ERR(tmp);
1973 struct dentry *dentry;
1974 struct nameidata nd;
1976 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
1979 dentry = lookup_create(&nd, 1);
1980 error = PTR_ERR(dentry);
1981 if (!IS_ERR(dentry)) {
1982 if (!IS_POSIXACL(nd.dentry->d_inode))
1983 mode &= ~current->fs->umask;
1984 error = vfs_mkdir(nd.dentry->d_inode, dentry,
1988 mutex_unlock(&nd.dentry->d_inode->i_mutex);
1997 asmlinkage long sys_mkdir(const char __user *pathname, int mode)
1999 return sys_mkdirat(AT_FDCWD, pathname, mode);
2003 * We try to drop the dentry early: we should have
2004 * a usage count of 2 if we're the only user of this
2005 * dentry, and if that is true (possibly after pruning
2006 * the dcache), then we drop the dentry now.
2008 * A low-level filesystem can, if it choses, legally
2011 * if (!d_unhashed(dentry))
2014 * if it cannot handle the case of removing a directory
2015 * that is still in use by something else..
2017 void dentry_unhash(struct dentry *dentry)
2020 if (atomic_read(&dentry->d_count))
2021 shrink_dcache_parent(dentry);
2022 spin_lock(&dcache_lock);
2023 spin_lock(&dentry->d_lock);
2024 if (atomic_read(&dentry->d_count) == 2)
2026 spin_unlock(&dentry->d_lock);
2027 spin_unlock(&dcache_lock);
2030 int vfs_rmdir(struct inode *dir, struct dentry *dentry,
2031 struct nameidata *nd)
2033 int error = may_delete(dir, dentry, 1, nd);
2038 if (!dir->i_op || !dir->i_op->rmdir)
2043 mutex_lock(&dentry->d_inode->i_mutex);
2044 dentry_unhash(dentry);
2045 if (d_mountpoint(dentry))
2048 error = security_inode_rmdir(dir, dentry);
2050 error = dir->i_op->rmdir(dir, dentry);
2052 dentry->d_inode->i_flags |= S_DEAD;
2055 mutex_unlock(&dentry->d_inode->i_mutex);
2064 static long do_rmdir(int dfd, const char __user *pathname)
2068 struct dentry *dentry;
2069 struct nameidata nd;
2071 name = getname(pathname);
2073 return PTR_ERR(name);
2075 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
2079 switch(nd.last_type) {
2090 mutex_lock(&nd.dentry->d_inode->i_mutex);
2091 dentry = lookup_hash(&nd);
2092 error = PTR_ERR(dentry);
2093 if (!IS_ERR(dentry)) {
2094 error = vfs_rmdir(nd.dentry->d_inode, dentry, &nd);
2097 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2105 asmlinkage long sys_rmdir(const char __user *pathname)
2107 return do_rmdir(AT_FDCWD, pathname);
2110 int vfs_unlink(struct inode *dir, struct dentry *dentry,
2111 struct nameidata *nd)
2113 int error = may_delete(dir, dentry, 0, nd);
2118 if (!dir->i_op || !dir->i_op->unlink)
2123 mutex_lock(&dentry->d_inode->i_mutex);
2124 if (d_mountpoint(dentry))
2127 error = security_inode_unlink(dir, dentry);
2129 error = dir->i_op->unlink(dir, dentry);
2131 mutex_unlock(&dentry->d_inode->i_mutex);
2133 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2134 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2142 * Make sure that the actual truncation of the file will occur outside its
2143 * directory's i_mutex. Truncate can take a long time if there is a lot of
2144 * writeout happening, and we don't want to prevent access to the directory
2145 * while waiting on the I/O.
2147 static long do_unlinkat(int dfd, const char __user *pathname)
2151 struct dentry *dentry;
2152 struct nameidata nd;
2153 struct inode *inode = NULL;
2155 name = getname(pathname);
2157 return PTR_ERR(name);
2159 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
2163 if (nd.last_type != LAST_NORM)
2165 mutex_lock(&nd.dentry->d_inode->i_mutex);
2166 dentry = lookup_hash(&nd);
2167 error = PTR_ERR(dentry);
2168 if (!IS_ERR(dentry)) {
2169 /* Why not before? Because we want correct error value */
2170 if (nd.last.name[nd.last.len])
2172 inode = dentry->d_inode;
2174 atomic_inc(&inode->i_count);
2175 error = vfs_unlink(nd.dentry->d_inode, dentry, &nd);
2179 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2181 iput(inode); /* truncate the inode here */
2189 error = !dentry->d_inode ? -ENOENT :
2190 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2194 asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
2196 if ((flag & ~AT_REMOVEDIR) != 0)
2199 if (flag & AT_REMOVEDIR)
2200 return do_rmdir(dfd, pathname);
2202 return do_unlinkat(dfd, pathname);
2205 asmlinkage long sys_unlink(const char __user *pathname)
2207 return do_unlinkat(AT_FDCWD, pathname);
2210 int vfs_symlink(struct inode *dir, struct dentry *dentry,
2211 const char *oldname, int mode, struct nameidata *nd)
2213 int error = may_create(dir, dentry, nd);
2218 if (!dir->i_op || !dir->i_op->symlink)
2221 error = security_inode_symlink(dir, dentry, oldname);
2226 error = dir->i_op->symlink(dir, dentry, oldname);
2228 fsnotify_create(dir, dentry->d_name.name);
2232 asmlinkage long sys_symlinkat(const char __user *oldname,
2233 int newdfd, const char __user *newname)
2239 from = getname(oldname);
2241 return PTR_ERR(from);
2242 to = getname(newname);
2243 error = PTR_ERR(to);
2245 struct dentry *dentry;
2246 struct nameidata nd;
2248 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2251 dentry = lookup_create(&nd, 0);
2252 error = PTR_ERR(dentry);
2253 if (!IS_ERR(dentry)) {
2254 error = vfs_symlink(nd.dentry->d_inode, dentry,
2255 from, S_IALLUGO, &nd);
2258 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2267 asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
2269 return sys_symlinkat(oldname, AT_FDCWD, newname);
2272 int vfs_link(struct dentry *old_dentry, struct inode *dir,
2273 struct dentry *new_dentry, struct nameidata *nd)
2275 struct inode *inode = old_dentry->d_inode;
2281 error = may_create(dir, new_dentry, nd);
2285 if (dir->i_sb != inode->i_sb)
2289 * A link to an append-only or immutable file cannot be created.
2291 if (IS_APPEND(inode) || IS_IXORUNLINK(inode))
2293 if (!dir->i_op || !dir->i_op->link)
2295 if (S_ISDIR(old_dentry->d_inode->i_mode))
2298 error = security_inode_link(old_dentry, dir, new_dentry);
2302 mutex_lock(&old_dentry->d_inode->i_mutex);
2304 error = dir->i_op->link(old_dentry, dir, new_dentry);
2305 mutex_unlock(&old_dentry->d_inode->i_mutex);
2307 fsnotify_create(dir, new_dentry->d_name.name);
2312 * Hardlinks are often used in delicate situations. We avoid
2313 * security-related surprises by not following symlinks on the
2316 * We don't follow them on the oldname either to be compatible
2317 * with linux 2.0, and to avoid hard-linking to directories
2318 * and other special files. --ADM
2320 asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
2321 int newdfd, const char __user *newname,
2324 struct dentry *new_dentry;
2325 struct nameidata nd, old_nd;
2332 to = getname(newname);
2336 error = __user_walk_fd(olddfd, oldname, 0, &old_nd);
2339 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2343 * We allow hard-links to be created to a bind-mount as long
2344 * as the bind-mount is not read-only. Checking for cross-dev
2345 * links is subsumed by the superblock check in vfs_link().
2348 if (MNT_IS_RDONLY(old_nd.mnt))
2350 new_dentry = lookup_create(&nd, 0);
2351 error = PTR_ERR(new_dentry);
2352 if (!IS_ERR(new_dentry)) {
2353 error = vfs_link(old_nd.dentry, nd.dentry->d_inode,
2357 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2361 path_release(&old_nd);
2368 asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
2370 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
2374 * The worst of all namespace operations - renaming directory. "Perverted"
2375 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2377 * a) we can get into loop creation. Check is done in is_subdir().
2378 * b) race potential - two innocent renames can create a loop together.
2379 * That's where 4.4 screws up. Current fix: serialization on
2380 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
2382 * c) we have to lock _three_ objects - parents and victim (if it exists).
2383 * And that - after we got ->i_mutex on parents (until then we don't know
2384 * whether the target exists). Solution: try to be smart with locking
2385 * order for inodes. We rely on the fact that tree topology may change
2386 * only under ->s_vfs_rename_sem _and_ that parent of the object we
2387 * move will be locked. Thus we can rank directories by the tree
2388 * (ancestors first) and rank all non-directories after them.
2389 * That works since everybody except rename does "lock parent, lookup,
2390 * lock child" and rename is under ->s_vfs_rename_sem.
2391 * HOWEVER, it relies on the assumption that any object with ->lookup()
2392 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2393 * we'd better make sure that there's no link(2) for them.
2394 * d) some filesystems don't support opened-but-unlinked directories,
2395 * either because of layout or because they are not ready to deal with
2396 * all cases correctly. The latter will be fixed (taking this sort of
2397 * stuff into VFS), but the former is not going away. Solution: the same
2398 * trick as in rmdir().
2399 * e) conversion from fhandle to dentry may come in the wrong moment - when
2400 * we are removing the target. Solution: we will have to grab ->i_mutex
2401 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2402 * ->i_mutex on parents, which works but leads to some truely excessive
2405 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2406 struct inode *new_dir, struct dentry *new_dentry)
2409 struct inode *target;
2412 * If we are going to change the parent - check write permissions,
2413 * we'll need to flip '..'.
2415 if (new_dir != old_dir) {
2416 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2421 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2425 target = new_dentry->d_inode;
2427 mutex_lock(&target->i_mutex);
2428 dentry_unhash(new_dentry);
2430 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2433 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2436 target->i_flags |= S_DEAD;
2437 mutex_unlock(&target->i_mutex);
2438 if (d_unhashed(new_dentry))
2439 d_rehash(new_dentry);
2443 d_move(old_dentry,new_dentry);
2447 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2448 struct inode *new_dir, struct dentry *new_dentry)
2450 struct inode *target;
2453 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2458 target = new_dentry->d_inode;
2460 mutex_lock(&target->i_mutex);
2461 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2464 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2466 /* The following d_move() should become unconditional */
2467 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2468 d_move(old_dentry, new_dentry);
2471 mutex_unlock(&target->i_mutex);
2476 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2477 struct inode *new_dir, struct dentry *new_dentry)
2480 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2481 const char *old_name;
2483 if (old_dentry->d_inode == new_dentry->d_inode)
2486 error = may_delete(old_dir, old_dentry, is_dir, NULL);
2490 if (!new_dentry->d_inode)
2491 error = may_create(new_dir, new_dentry, NULL);
2493 error = may_delete(new_dir, new_dentry, is_dir, NULL);
2497 if (!old_dir->i_op || !old_dir->i_op->rename)
2500 DQUOT_INIT(old_dir);
2501 DQUOT_INIT(new_dir);
2503 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2506 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2508 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2510 const char *new_name = old_dentry->d_name.name;
2511 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
2512 new_dentry->d_inode, old_dentry->d_inode);
2514 fsnotify_oldname_free(old_name);
2519 static int do_rename(int olddfd, const char *oldname,
2520 int newdfd, const char *newname)
2523 struct dentry * old_dir, * new_dir;
2524 struct dentry * old_dentry, *new_dentry;
2525 struct dentry * trap;
2526 struct nameidata oldnd, newnd;
2528 error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd);
2532 error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
2537 if (oldnd.mnt != newnd.mnt)
2540 old_dir = oldnd.dentry;
2542 if (oldnd.last_type != LAST_NORM)
2545 new_dir = newnd.dentry;
2546 if (newnd.last_type != LAST_NORM)
2549 trap = lock_rename(new_dir, old_dir);
2551 old_dentry = lookup_hash(&oldnd);
2552 error = PTR_ERR(old_dentry);
2553 if (IS_ERR(old_dentry))
2555 /* source must exist */
2557 if (!old_dentry->d_inode)
2559 /* unless the source is a directory trailing slashes give -ENOTDIR */
2560 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2562 if (oldnd.last.name[oldnd.last.len])
2564 if (newnd.last.name[newnd.last.len])
2567 /* source should not be ancestor of target */
2569 if (old_dentry == trap)
2572 if (MNT_IS_RDONLY(newnd.mnt))
2574 new_dentry = lookup_hash(&newnd);
2575 error = PTR_ERR(new_dentry);
2576 if (IS_ERR(new_dentry))
2578 /* target should not be an ancestor of source */
2580 if (new_dentry == trap)
2583 error = vfs_rename(old_dir->d_inode, old_dentry,
2584 new_dir->d_inode, new_dentry);
2590 unlock_rename(new_dir, old_dir);
2592 path_release(&newnd);
2594 path_release(&oldnd);
2599 asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
2600 int newdfd, const char __user *newname)
2606 from = getname(oldname);
2608 return PTR_ERR(from);
2609 to = getname(newname);
2610 error = PTR_ERR(to);
2612 error = do_rename(olddfd, from, newdfd, to);
2619 asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
2621 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
2624 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2628 len = PTR_ERR(link);
2633 if (len > (unsigned) buflen)
2635 if (copy_to_user(buffer, link, len))
2642 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2643 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2644 * using) it for any given inode is up to filesystem.
2646 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2648 struct nameidata nd;
2652 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2653 if (!IS_ERR(cookie)) {
2654 int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2655 if (dentry->d_inode->i_op->put_link)
2656 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2657 cookie = ERR_PTR(res);
2659 return PTR_ERR(cookie);
2662 int vfs_follow_link(struct nameidata *nd, const char *link)
2664 return __vfs_follow_link(nd, link);
2667 /* get the link contents into pagecache */
2668 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2671 struct address_space *mapping = dentry->d_inode->i_mapping;
2672 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2676 wait_on_page_locked(page);
2677 if (!PageUptodate(page))
2683 page_cache_release(page);
2684 return ERR_PTR(-EIO);
2690 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2692 struct page *page = NULL;
2693 char *s = page_getlink(dentry, &page);
2694 int res = vfs_readlink(dentry,buffer,buflen,s);
2697 page_cache_release(page);
2702 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2704 struct page *page = NULL;
2705 nd_set_link(nd, page_getlink(dentry, &page));
2709 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
2711 struct page *page = cookie;
2715 page_cache_release(page);
2719 int __page_symlink(struct inode *inode, const char *symname, int len,
2722 struct address_space *mapping = inode->i_mapping;
2727 page = find_or_create_page(mapping, 0, gfp_mask);
2730 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2733 kaddr = kmap_atomic(page, KM_USER0);
2734 memcpy(kaddr, symname, len-1);
2735 kunmap_atomic(kaddr, KM_USER0);
2736 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2738 * Notice that we are _not_ going to block here - end of page is
2739 * unmapped, so this will only try to map the rest of page, see
2740 * that it is unmapped (typically even will not look into inode -
2741 * ->i_size will be enough for everything) and zero it out.
2742 * OTOH it's obviously correct and should make the page up-to-date.
2744 if (!PageUptodate(page)) {
2745 err = mapping->a_ops->readpage(NULL, page);
2746 wait_on_page_locked(page);
2750 page_cache_release(page);
2753 mark_inode_dirty(inode);
2757 page_cache_release(page);
2762 int page_symlink(struct inode *inode, const char *symname, int len)
2764 return __page_symlink(inode, symname, len,
2765 mapping_gfp_mask(inode->i_mapping));
2768 struct inode_operations page_symlink_inode_operations = {
2769 .readlink = generic_readlink,
2770 .follow_link = page_follow_link_light,
2771 .put_link = page_put_link,
2774 EXPORT_SYMBOL(__user_walk);
2775 EXPORT_SYMBOL(__user_walk_fd);
2776 EXPORT_SYMBOL(follow_down);
2777 EXPORT_SYMBOL(follow_up);
2778 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2779 EXPORT_SYMBOL(getname);
2780 EXPORT_SYMBOL(lock_rename);
2781 EXPORT_SYMBOL(lookup_hash);
2782 EXPORT_SYMBOL(lookup_one_len);
2783 EXPORT_SYMBOL(page_follow_link_light);
2784 EXPORT_SYMBOL(page_put_link);
2785 EXPORT_SYMBOL(page_readlink);
2786 EXPORT_SYMBOL(__page_symlink);
2787 EXPORT_SYMBOL(page_symlink);
2788 EXPORT_SYMBOL(page_symlink_inode_operations);
2789 EXPORT_SYMBOL(path_lookup);
2790 EXPORT_SYMBOL(path_release);
2791 EXPORT_SYMBOL(path_walk);
2792 EXPORT_SYMBOL(permission);
2793 EXPORT_SYMBOL(vfs_permission);
2794 EXPORT_SYMBOL(file_permission);
2795 EXPORT_SYMBOL(unlock_rename);
2796 EXPORT_SYMBOL(vfs_create);
2797 EXPORT_SYMBOL(vfs_follow_link);
2798 EXPORT_SYMBOL(vfs_link);
2799 EXPORT_SYMBOL(vfs_mkdir);
2800 EXPORT_SYMBOL(vfs_mknod);
2801 EXPORT_SYMBOL(generic_permission);
2802 EXPORT_SYMBOL(vfs_readlink);
2803 EXPORT_SYMBOL(vfs_rename);
2804 EXPORT_SYMBOL(vfs_rmdir);
2805 EXPORT_SYMBOL(vfs_symlink);
2806 EXPORT_SYMBOL(vfs_unlink);
2807 EXPORT_SYMBOL(dentry_unhash);
2808 EXPORT_SYMBOL(generic_readlink);