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/slab.h>
20 #include <linux/quotaops.h>
21 #include <linux/pagemap.h>
22 #include <linux/dnotify.h>
23 #include <linux/smp_lock.h>
24 #include <linux/personality.h>
26 #include <asm/namei.h>
27 #include <asm/uaccess.h>
29 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
31 /* [Feb-1997 T. Schoebel-Theuer]
32 * Fundamental changes in the pathname lookup mechanisms (namei)
33 * were necessary because of omirr. The reason is that omirr needs
34 * to know the _real_ pathname, not the user-supplied one, in case
35 * of symlinks (and also when transname replacements occur).
37 * The new code replaces the old recursive symlink resolution with
38 * an iterative one (in case of non-nested symlink chains). It does
39 * this with calls to <fs>_follow_link().
40 * As a side effect, dir_namei(), _namei() and follow_link() are now
41 * replaced with a single function lookup_dentry() that can handle all
42 * the special cases of the former code.
44 * With the new dcache, the pathname is stored at each inode, at least as
45 * long as the refcount of the inode is positive. As a side effect, the
46 * size of the dcache depends on the inode cache and thus is dynamic.
48 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
49 * resolution to correspond with current state of the code.
51 * Note that the symlink resolution is not *completely* iterative.
52 * There is still a significant amount of tail- and mid- recursion in
53 * the algorithm. Also, note that <fs>_readlink() is not used in
54 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
55 * may return different results than <fs>_follow_link(). Many virtual
56 * filesystems (including /proc) exhibit this behavior.
59 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
60 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
61 * and the name already exists in form of a symlink, try to create the new
62 * name indicated by the symlink. The old code always complained that the
63 * name already exists, due to not following the symlink even if its target
64 * is nonexistent. The new semantics affects also mknod() and link() when
65 * the name is a symlink pointing to a non-existant name.
67 * I don't know which semantics is the right one, since I have no access
68 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
69 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
70 * "old" one. Personally, I think the new semantics is much more logical.
71 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
72 * file does succeed in both HP-UX and SunOs, but not in Solaris
73 * and in the old Linux semantics.
76 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
77 * semantics. See the comments in "open_namei" and "do_link" below.
79 * [10-Sep-98 Alan Modra] Another symlink change.
82 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
83 * inside the path - always follow.
84 * in the last component in creation/removal/renaming - never follow.
85 * if LOOKUP_FOLLOW passed - follow.
86 * if the pathname has trailing slashes - follow.
87 * otherwise - don't follow.
88 * (applied in that order).
90 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
91 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
92 * During the 2.4 we need to fix the userland stuff depending on it -
93 * hopefully we will be able to get rid of that wart in 2.5. So far only
94 * XEmacs seems to be relying on it...
97 /* In order to reduce some races, while at the same time doing additional
98 * checking and hopefully speeding things up, we copy filenames to the
99 * kernel data space before using them..
101 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
102 * PATH_MAX includes the nul terminator --RR.
104 static inline int do_getname(const char *filename, char *page)
107 unsigned long len = PATH_MAX;
109 if ((unsigned long) filename >= TASK_SIZE) {
110 if (!segment_eq(get_fs(), KERNEL_DS))
112 } else if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
113 len = TASK_SIZE - (unsigned long) filename;
115 retval = strncpy_from_user((char *)page, filename, len);
119 return -ENAMETOOLONG;
125 char * getname(const char * filename)
129 result = ERR_PTR(-ENOMEM);
132 int retval = do_getname(filename, tmp);
137 result = ERR_PTR(retval);
146 * is used to check for read/write/execute permissions on a file.
147 * We use "fsuid" for this, letting us set arbitrary permissions
148 * for filesystem access without changing the "normal" uids which
149 * are used for other things..
151 int vfs_permission(struct inode * inode, int mask)
153 umode_t mode = inode->i_mode;
155 if (mask & MAY_WRITE) {
157 * Nobody gets write access to a read-only fs.
159 if (IS_RDONLY(inode) &&
160 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
164 * Nobody gets write access to an immutable file.
166 if (IS_IMMUTABLE(inode))
170 if (current->fsuid == inode->i_uid)
172 else if (in_group_p(inode->i_gid))
176 * If the DACs are ok we don't need any capability check.
178 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
182 * Read/write DACs are always overridable.
183 * Executable DACs are overridable if at least one exec bit is set.
185 if ((mask & (MAY_READ|MAY_WRITE)) || (inode->i_mode & S_IXUGO))
186 if (capable(CAP_DAC_OVERRIDE))
190 * Searching includes executable on directories, else just read.
192 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
193 if (capable(CAP_DAC_READ_SEARCH))
199 int permission(struct inode * inode,int mask)
201 if (inode->i_op && inode->i_op->permission) {
204 retval = inode->i_op->permission(inode, mask);
208 return vfs_permission(inode, mask);
212 * get_write_access() gets write permission for a file.
213 * put_write_access() releases this write permission.
214 * This is used for regular files.
215 * We cannot support write (and maybe mmap read-write shared) accesses and
216 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
217 * can have the following values:
218 * 0: no writers, no VM_DENYWRITE mappings
219 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
220 * > 0: (i_writecount) users are writing to the file.
222 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
223 * except for the cases where we don't hold i_writecount yet. Then we need to
224 * use {get,deny}_write_access() - these functions check the sign and refuse
225 * to do the change if sign is wrong. Exclusion between them is provided by
226 * spinlock (arbitration_lock) and I'll rip the second arsehole to the first
227 * who will try to move it in struct inode - just leave it here.
229 static spinlock_t arbitration_lock = SPIN_LOCK_UNLOCKED;
230 int get_write_access(struct inode * inode)
232 spin_lock(&arbitration_lock);
233 if (atomic_read(&inode->i_writecount) < 0) {
234 spin_unlock(&arbitration_lock);
237 atomic_inc(&inode->i_writecount);
238 spin_unlock(&arbitration_lock);
241 int deny_write_access(struct file * file)
243 spin_lock(&arbitration_lock);
244 if (atomic_read(&file->f_dentry->d_inode->i_writecount) > 0) {
245 spin_unlock(&arbitration_lock);
248 atomic_dec(&file->f_dentry->d_inode->i_writecount);
249 spin_unlock(&arbitration_lock);
253 void path_release(struct nameidata *nd)
260 * Internal lookup() using the new generic dcache.
263 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, int flags)
265 struct dentry * dentry = d_lookup(parent, name);
267 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
268 if (!dentry->d_op->d_revalidate(dentry, flags) && !d_invalidate(dentry)) {
277 * This is called when everything else fails, and we actually have
278 * to go to the low-level filesystem to find out what we should do..
280 * We get the directory semaphore, and after getting that we also
281 * make sure that nobody added the entry to the dcache in the meantime..
284 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, int flags)
286 struct dentry * result;
287 struct inode *dir = parent->d_inode;
291 * First re-do the cached lookup just in case it was created
292 * while we waited for the directory semaphore..
294 * FIXME! This could use version numbering or similar to
295 * avoid unnecessary cache lookups.
297 result = d_lookup(parent, name);
299 struct dentry * dentry = d_alloc(parent, name);
300 result = ERR_PTR(-ENOMEM);
303 result = dir->i_op->lookup(dir, dentry);
315 * Uhhuh! Nasty case: the cache was re-populated while
316 * we waited on the semaphore. Need to revalidate.
319 if (result->d_op && result->d_op->d_revalidate) {
320 if (!result->d_op->d_revalidate(result, flags) && !d_invalidate(result)) {
322 result = ERR_PTR(-ENOENT);
329 * This limits recursive symlink follows to 5, while
330 * limiting consecutive symlinks to 40.
332 * Without that kind of total limit, nasty chains of consecutive
333 * symlinks can cause almost arbitrarily long lookups.
335 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
338 if (current->link_count >= 5)
340 if (current->total_link_count >= 40)
342 if (current->need_resched) {
343 current->state = TASK_RUNNING;
346 current->link_count++;
347 current->total_link_count++;
348 UPDATE_ATIME(dentry->d_inode);
349 err = dentry->d_inode->i_op->follow_link(dentry, nd);
350 current->link_count--;
357 static inline int __follow_up(struct vfsmount **mnt, struct dentry **base)
359 struct vfsmount *parent;
360 struct dentry *dentry;
361 spin_lock(&dcache_lock);
362 parent=(*mnt)->mnt_parent;
363 if (parent == *mnt) {
364 spin_unlock(&dcache_lock);
368 dentry=dget((*mnt)->mnt_mountpoint);
369 spin_unlock(&dcache_lock);
377 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
379 return __follow_up(mnt, dentry);
382 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
384 struct vfsmount *mounted;
386 spin_lock(&dcache_lock);
387 mounted = lookup_mnt(*mnt, *dentry);
389 *mnt = mntget(mounted);
390 spin_unlock(&dcache_lock);
392 mntput(mounted->mnt_parent);
393 *dentry = dget(mounted->mnt_root);
396 spin_unlock(&dcache_lock);
400 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
402 return __follow_down(mnt,dentry);
405 static inline void follow_dotdot(struct nameidata *nd)
408 struct vfsmount *parent;
409 struct dentry *dentry;
410 read_lock(¤t->fs->lock);
411 if (nd->dentry == current->fs->root &&
412 nd->mnt == current->fs->rootmnt) {
413 read_unlock(¤t->fs->lock);
416 read_unlock(¤t->fs->lock);
417 spin_lock(&dcache_lock);
418 if (nd->dentry != nd->mnt->mnt_root) {
419 dentry = dget(nd->dentry->d_parent);
420 spin_unlock(&dcache_lock);
425 parent=nd->mnt->mnt_parent;
426 if (parent == nd->mnt) {
427 spin_unlock(&dcache_lock);
431 dentry=dget(nd->mnt->mnt_mountpoint);
432 spin_unlock(&dcache_lock);
438 while (d_mountpoint(nd->dentry) && __follow_down(&nd->mnt, &nd->dentry))
445 * This is the basic name resolution function, turning a pathname
446 * into the final dentry.
448 * We expect 'base' to be positive and a directory.
450 int fastcall link_path_walk(const char * name, struct nameidata *nd)
452 struct dentry *dentry;
455 unsigned int lookup_flags = nd->flags;
462 inode = nd->dentry->d_inode;
463 if (current->link_count)
464 lookup_flags = LOOKUP_FOLLOW;
466 /* At this point we know we have a real path component. */
472 err = permission(inode, MAY_EXEC);
473 dentry = ERR_PTR(err);
478 c = *(const unsigned char *)name;
480 hash = init_name_hash();
483 hash = partial_name_hash(c, hash);
484 c = *(const unsigned char *)name;
485 } while (c && (c != '/'));
486 this.len = name - (const char *) this.name;
487 this.hash = end_name_hash(hash);
489 /* remove trailing slashes? */
492 while (*++name == '/');
494 goto last_with_slashes;
497 * "." and ".." are special - ".." especially so because it has
498 * to be able to know about the current root directory and
499 * parent relationships.
501 if (this.name[0] == '.') switch (this.len) {
505 if (this.name[1] != '.')
508 inode = nd->dentry->d_inode;
514 * See if the low-level filesystem might want
515 * to use its own hash..
517 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
518 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
522 /* This does the actual lookups.. */
523 dentry = cached_lookup(nd->dentry, &this, LOOKUP_CONTINUE);
525 dentry = real_lookup(nd->dentry, &this, LOOKUP_CONTINUE);
526 err = PTR_ERR(dentry);
530 /* Check mountpoints.. */
531 while (d_mountpoint(dentry) && __follow_down(&nd->mnt, &dentry))
535 inode = dentry->d_inode;
542 if (inode->i_op->follow_link) {
543 struct vfsmount *mnt = mntget(nd->mnt);
544 err = do_follow_link(dentry, nd);
550 inode = nd->dentry->d_inode;
561 if (!inode->i_op->lookup)
564 /* here ends the main loop */
567 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
569 if (lookup_flags & LOOKUP_PARENT)
571 if (this.name[0] == '.') switch (this.len) {
575 if (this.name[1] != '.')
578 inode = nd->dentry->d_inode;
583 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
584 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
588 dentry = cached_lookup(nd->dentry, &this, nd->flags);
590 dentry = real_lookup(nd->dentry, &this, nd->flags);
591 err = PTR_ERR(dentry);
595 while (d_mountpoint(dentry) && __follow_down(&nd->mnt, &dentry))
597 inode = dentry->d_inode;
598 if ((lookup_flags & LOOKUP_FOLLOW)
599 && inode && inode->i_op && inode->i_op->follow_link) {
600 struct vfsmount *mnt = mntget(nd->mnt);
601 err = do_follow_link(dentry, nd);
606 inode = nd->dentry->d_inode;
614 if (lookup_flags & LOOKUP_DIRECTORY) {
616 if (!inode->i_op || !inode->i_op->lookup)
622 if (lookup_flags & (LOOKUP_POSITIVE|LOOKUP_DIRECTORY))
627 nd->last_type = LAST_NORM;
628 if (this.name[0] != '.')
631 nd->last_type = LAST_DOT;
632 else if (this.len == 2 && this.name[1] == '.')
633 nd->last_type = LAST_DOTDOT;
638 * We bypassed the ordinary revalidation routines.
639 * Check the cached dentry for staleness.
642 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
644 if (!dentry->d_op->d_revalidate(dentry, 0)) {
645 d_invalidate(dentry);
660 int fastcall path_walk(const char * name, struct nameidata *nd)
662 current->total_link_count = 0;
663 return link_path_walk(name, nd);
667 /* returns 1 if everything is done */
668 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
670 if (path_walk(name, nd))
671 return 0; /* something went wrong... */
673 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
674 struct nameidata nd_root;
676 * NAME was not found in alternate root or it's a directory. Try to find
677 * it in the normal root:
679 nd_root.last_type = LAST_ROOT;
680 nd_root.flags = nd->flags;
681 read_lock(¤t->fs->lock);
682 nd_root.mnt = mntget(current->fs->rootmnt);
683 nd_root.dentry = dget(current->fs->root);
684 read_unlock(¤t->fs->lock);
685 if (path_walk(name, &nd_root))
687 if (nd_root.dentry->d_inode) {
689 nd->dentry = nd_root.dentry;
690 nd->mnt = nd_root.mnt;
691 nd->last = nd_root.last;
694 path_release(&nd_root);
699 void set_fs_altroot(void)
701 char *emul = __emul_prefix();
703 struct vfsmount *mnt = NULL, *oldmnt;
704 struct dentry *dentry = NULL, *olddentry;
706 read_lock(¤t->fs->lock);
707 nd.mnt = mntget(current->fs->rootmnt);
708 nd.dentry = dget(current->fs->root);
709 read_unlock(¤t->fs->lock);
710 nd.flags = LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_POSITIVE;
711 if (path_walk(emul,&nd) == 0) {
716 write_lock(¤t->fs->lock);
717 oldmnt = current->fs->altrootmnt;
718 olddentry = current->fs->altroot;
719 current->fs->altrootmnt = mnt;
720 current->fs->altroot = dentry;
721 write_unlock(¤t->fs->lock);
730 walk_init_root(const char *name, struct nameidata *nd)
732 read_lock(¤t->fs->lock);
733 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
734 nd->mnt = mntget(current->fs->altrootmnt);
735 nd->dentry = dget(current->fs->altroot);
736 read_unlock(¤t->fs->lock);
737 if (__emul_lookup_dentry(name,nd))
739 read_lock(¤t->fs->lock);
741 nd->mnt = mntget(current->fs->rootmnt);
742 nd->dentry = dget(current->fs->root);
743 read_unlock(¤t->fs->lock);
748 int fastcall path_lookup(const char *path, unsigned flags, struct nameidata *nd)
751 if (path_init(path, flags, nd))
752 error = path_walk(path, nd);
758 int fastcall path_init(const char *name, unsigned int flags, struct nameidata *nd)
760 nd->last_type = LAST_ROOT; /* if there are only slashes... */
763 return walk_init_root(name,nd);
764 read_lock(¤t->fs->lock);
765 nd->mnt = mntget(current->fs->pwdmnt);
766 nd->dentry = dget(current->fs->pwd);
767 read_unlock(¤t->fs->lock);
772 * Restricted form of lookup. Doesn't follow links, single-component only,
773 * needs parent already locked. Doesn't follow mounts.
776 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
778 struct dentry * dentry;
782 inode = base->d_inode;
783 err = permission(inode, MAY_EXEC);
784 dentry = ERR_PTR(err);
789 * See if the low-level filesystem might want
790 * to use its own hash..
792 if (base->d_op && base->d_op->d_hash) {
793 err = base->d_op->d_hash(base, name);
794 dentry = ERR_PTR(err);
799 dentry = cached_lookup(base, name, 0);
801 struct dentry *new = d_alloc(base, name);
802 dentry = ERR_PTR(-ENOMEM);
806 dentry = inode->i_op->lookup(inode, new);
818 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
829 hash = init_name_hash();
831 c = *(const unsigned char *)name++;
832 if (c == '/' || c == '\0')
834 hash = partial_name_hash(c, hash);
836 this.hash = end_name_hash(hash);
838 return lookup_hash(&this, base);
840 return ERR_PTR(-EACCES);
846 * is used by most simple commands to get the inode of a specified name.
847 * Open, link etc use their own routines, but this is enough for things
850 * namei exists in two versions: namei/lnamei. The only difference is
851 * that namei follows links, while lnamei does not.
854 int fastcall __user_walk(const char *name, unsigned flags, struct nameidata *nd)
863 err = path_lookup(tmp, flags, nd);
870 * It's inline, so penalty for filesystems that don't use sticky bit is
873 static inline int check_sticky(struct inode *dir, struct inode *inode)
875 if (!(dir->i_mode & S_ISVTX))
877 if (inode->i_uid == current->fsuid)
879 if (dir->i_uid == current->fsuid)
881 return !capable(CAP_FOWNER);
885 * Check whether we can remove a link victim from directory dir, check
886 * whether the type of victim is right.
887 * 1. We can't do it if dir is read-only (done in permission())
888 * 2. We should have write and exec permissions on dir
889 * 3. We can't remove anything from append-only dir
890 * 4. We can't do anything with immutable dir (done in permission())
891 * 5. If the sticky bit on dir is set we should either
892 * a. be owner of dir, or
893 * b. be owner of victim, or
894 * c. have CAP_FOWNER capability
895 * 6. If the victim is append-only or immutable we can't do antyhing with
896 * links pointing to it.
897 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
898 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
899 * 9. We can't remove a root or mountpoint.
901 static inline int may_delete(struct inode *dir,struct dentry *victim, int isdir)
904 if (!victim->d_inode || victim->d_parent->d_inode != dir)
906 error = permission(dir,MAY_WRITE | MAY_EXEC);
911 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
912 IS_IMMUTABLE(victim->d_inode))
915 if (!S_ISDIR(victim->d_inode->i_mode))
919 } else if (S_ISDIR(victim->d_inode->i_mode))
926 /* Check whether we can create an object with dentry child in directory
928 * 1. We can't do it if child already exists (open has special treatment for
929 * this case, but since we are inlined it's OK)
930 * 2. We can't do it if dir is read-only (done in permission())
931 * 3. We should have write and exec permissions on dir
932 * 4. We can't do it if dir is immutable (done in permission())
934 static inline int may_create(struct inode *dir, struct dentry *child) {
939 return permission(dir,MAY_WRITE | MAY_EXEC);
943 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
946 * O_DIRECTORY translates into forcing a directory lookup.
948 static inline int lookup_flags(unsigned int f)
950 unsigned long retval = LOOKUP_FOLLOW;
953 retval &= ~LOOKUP_FOLLOW;
955 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
956 retval &= ~LOOKUP_FOLLOW;
959 retval |= LOOKUP_DIRECTORY;
964 int vfs_create(struct inode *dir, struct dentry *dentry, int mode)
971 down(&dir->i_zombie);
972 error = may_create(dir, dentry);
976 error = -EACCES; /* shouldn't it be ENOSYS? */
977 if (!dir->i_op || !dir->i_op->create)
982 error = dir->i_op->create(dir, dentry, mode);
987 inode_dir_notify(dir, DN_CREATE);
994 * namei for open - this is in fact almost the whole open-routine.
996 * Note that the low bits of "flag" aren't the same as in the open
997 * system call - they are 00 - no permissions needed
998 * 01 - read permission needed
999 * 10 - write permission needed
1000 * 11 - read/write permissions needed
1001 * which is a lot more logical, and also allows the "no perm" needed
1002 * for symlinks (where the permissions are checked later).
1005 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1007 int acc_mode, error = 0;
1008 struct inode *inode;
1009 struct dentry *dentry;
1010 struct vfsmount *mnt;
1014 acc_mode = ACC_MODE(flag);
1017 * The simplest case - just a plain lookup.
1019 if (!(flag & O_CREAT)) {
1020 error = path_lookup(pathname, lookup_flags(flag), nd);
1023 dentry = nd->dentry;
1028 * Create - we need to know the parent.
1030 error = path_lookup(pathname, LOOKUP_PARENT, nd);
1035 * We have the parent and last component. First of all, check
1036 * that we are not asked to creat(2) an obvious directory - that
1040 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1044 down(&dir->d_inode->i_sem);
1045 dentry = lookup_hash(&nd->last, nd->dentry);
1048 error = PTR_ERR(dentry);
1049 if (IS_ERR(dentry)) {
1050 up(&dir->d_inode->i_sem);
1054 /* Negative dentry, just create the file */
1055 if (!dentry->d_inode) {
1056 error = vfs_create(dir->d_inode, dentry,
1057 mode & ~current->fs->umask);
1058 up(&dir->d_inode->i_sem);
1060 nd->dentry = dentry;
1063 /* Don't check for write permission, don't truncate */
1070 * It already exists.
1072 up(&dir->d_inode->i_sem);
1078 if (d_mountpoint(dentry)) {
1080 if (flag & O_NOFOLLOW)
1082 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1085 if (!dentry->d_inode)
1087 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1091 nd->dentry = dentry;
1093 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1097 inode = dentry->d_inode;
1102 if (S_ISLNK(inode->i_mode))
1106 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1109 error = permission(inode,acc_mode);
1114 * FIFO's, sockets and device files are special: they don't
1115 * actually live on the filesystem itself, and as such you
1116 * can write to them even if the filesystem is read-only.
1118 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1120 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1122 if (nd->mnt->mnt_flags & MNT_NODEV)
1128 if (IS_RDONLY(inode) && (flag & 2))
1132 * An append-only file must be opened in append mode for writing.
1135 if (IS_APPEND(inode)) {
1136 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1143 * Ensure there are no outstanding leases on the file.
1145 error = get_lease(inode, flag);
1149 if (flag & O_TRUNC) {
1150 error = get_write_access(inode);
1155 * Refuse to truncate files with mandatory locks held on them.
1157 error = locks_verify_locked(inode);
1161 error = do_truncate(dentry, 0);
1163 put_write_access(inode);
1167 if (flag & FMODE_WRITE)
1180 if (flag & O_NOFOLLOW)
1183 * This is subtle. Instead of calling do_follow_link() we do the
1184 * thing by hands. The reason is that this way we have zero link_count
1185 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1186 * After that we have the parent and last component, i.e.
1187 * we are in the same situation as after the first path_walk().
1188 * Well, almost - if the last component is normal we get its copy
1189 * stored in nd->last.name and we will have to putname() it when we
1190 * are done. Procfs-like symlinks just set LAST_BIND.
1192 UPDATE_ATIME(dentry->d_inode);
1193 mnt = mntget(nd->mnt);
1194 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1199 if (nd->last_type == LAST_BIND) {
1200 dentry = nd->dentry;
1204 if (nd->last_type != LAST_NORM)
1206 if (nd->last.name[nd->last.len]) {
1207 putname(nd->last.name);
1212 putname(nd->last.name);
1216 down(&dir->d_inode->i_sem);
1217 dentry = lookup_hash(&nd->last, nd->dentry);
1218 putname(nd->last.name);
1223 static struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1225 struct dentry *dentry;
1227 down(&nd->dentry->d_inode->i_sem);
1228 dentry = ERR_PTR(-EEXIST);
1229 if (nd->last_type != LAST_NORM)
1231 dentry = lookup_hash(&nd->last, nd->dentry);
1234 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1239 dentry = ERR_PTR(-ENOENT);
1244 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1248 down(&dir->i_zombie);
1249 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1252 error = may_create(dir, dentry);
1257 if (!dir->i_op || !dir->i_op->mknod)
1262 error = dir->i_op->mknod(dir, dentry, mode, dev);
1267 inode_dir_notify(dir, DN_CREATE);
1271 asmlinkage long sys_mknod(const char * filename, int mode, dev_t dev)
1275 struct dentry * dentry;
1276 struct nameidata nd;
1280 tmp = getname(filename);
1282 return PTR_ERR(tmp);
1284 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1287 dentry = lookup_create(&nd, 0);
1288 error = PTR_ERR(dentry);
1290 mode &= ~current->fs->umask;
1291 if (!IS_ERR(dentry)) {
1292 switch (mode & S_IFMT) {
1293 case 0: case S_IFREG:
1294 error = vfs_create(nd.dentry->d_inode,dentry,mode);
1296 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
1297 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,dev);
1307 up(&nd.dentry->d_inode->i_sem);
1315 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1319 down(&dir->i_zombie);
1320 error = may_create(dir, dentry);
1325 if (!dir->i_op || !dir->i_op->mkdir)
1329 mode &= (S_IRWXUGO|S_ISVTX);
1331 error = dir->i_op->mkdir(dir, dentry, mode);
1337 inode_dir_notify(dir, DN_CREATE);
1341 asmlinkage long sys_mkdir(const char * pathname, int mode)
1346 tmp = getname(pathname);
1347 error = PTR_ERR(tmp);
1349 struct dentry *dentry;
1350 struct nameidata nd;
1352 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1355 dentry = lookup_create(&nd, 1);
1356 error = PTR_ERR(dentry);
1357 if (!IS_ERR(dentry)) {
1358 error = vfs_mkdir(nd.dentry->d_inode, dentry,
1359 mode & ~current->fs->umask);
1362 up(&nd.dentry->d_inode->i_sem);
1372 * We try to drop the dentry early: we should have
1373 * a usage count of 2 if we're the only user of this
1374 * dentry, and if that is true (possibly after pruning
1375 * the dcache), then we drop the dentry now.
1377 * A low-level filesystem can, if it choses, legally
1380 * if (!d_unhashed(dentry))
1383 * if it cannot handle the case of removing a directory
1384 * that is still in use by something else..
1386 static void d_unhash(struct dentry *dentry)
1389 spin_lock(&dcache_lock);
1390 switch (atomic_read(&dentry->d_count)) {
1392 spin_unlock(&dcache_lock);
1393 shrink_dcache_parent(dentry);
1394 spin_lock(&dcache_lock);
1395 if (atomic_read(&dentry->d_count) != 2)
1398 list_del_init(&dentry->d_hash);
1400 spin_unlock(&dcache_lock);
1403 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1407 error = may_delete(dir, dentry, 1);
1411 if (!dir->i_op || !dir->i_op->rmdir)
1416 double_down(&dir->i_zombie, &dentry->d_inode->i_zombie);
1418 if (d_mountpoint(dentry))
1422 error = dir->i_op->rmdir(dir, dentry);
1425 dentry->d_inode->i_flags |= S_DEAD;
1427 double_up(&dir->i_zombie, &dentry->d_inode->i_zombie);
1429 inode_dir_notify(dir, DN_DELETE);
1437 asmlinkage long sys_rmdir(const char * pathname)
1441 struct dentry *dentry;
1442 struct nameidata nd;
1444 name = getname(pathname);
1446 return PTR_ERR(name);
1448 error = path_lookup(name, LOOKUP_PARENT, &nd);
1452 switch(nd.last_type) {
1463 down(&nd.dentry->d_inode->i_sem);
1464 dentry = lookup_hash(&nd.last, nd.dentry);
1465 error = PTR_ERR(dentry);
1466 if (!IS_ERR(dentry)) {
1467 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1470 up(&nd.dentry->d_inode->i_sem);
1478 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1481 struct inode *inode;
1483 error = may_delete(dir, dentry, 0);
1487 inode = dentry->d_inode;
1488 atomic_inc(&inode->i_count);
1489 double_down(&dir->i_zombie, &inode->i_zombie);
1492 if (dir->i_op && dir->i_op->unlink) {
1494 if (d_mountpoint(dentry))
1498 error = dir->i_op->unlink(dir, dentry);
1502 double_up(&dir->i_zombie, &inode->i_zombie);
1507 inode_dir_notify(dir, DN_DELETE);
1512 asmlinkage long sys_unlink(const char * pathname)
1516 struct dentry *dentry;
1517 struct nameidata nd;
1519 name = getname(pathname);
1521 return PTR_ERR(name);
1523 error = path_lookup(name, LOOKUP_PARENT, &nd);
1527 if (nd.last_type != LAST_NORM)
1529 down(&nd.dentry->d_inode->i_sem);
1530 dentry = lookup_hash(&nd.last, nd.dentry);
1531 error = PTR_ERR(dentry);
1532 if (!IS_ERR(dentry)) {
1533 /* Why not before? Because we want correct error value */
1534 if (nd.last.name[nd.last.len])
1536 error = vfs_unlink(nd.dentry->d_inode, dentry);
1540 up(&nd.dentry->d_inode->i_sem);
1549 error = !dentry->d_inode ? -ENOENT :
1550 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1554 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1558 down(&dir->i_zombie);
1559 error = may_create(dir, dentry);
1564 if (!dir->i_op || !dir->i_op->symlink)
1569 error = dir->i_op->symlink(dir, dentry, oldname);
1575 inode_dir_notify(dir, DN_CREATE);
1579 asmlinkage long sys_symlink(const char * oldname, const char * newname)
1585 from = getname(oldname);
1587 return PTR_ERR(from);
1588 to = getname(newname);
1589 error = PTR_ERR(to);
1591 struct dentry *dentry;
1592 struct nameidata nd;
1594 error = path_lookup(to, LOOKUP_PARENT, &nd);
1597 dentry = lookup_create(&nd, 0);
1598 error = PTR_ERR(dentry);
1599 if (!IS_ERR(dentry)) {
1600 error = vfs_symlink(nd.dentry->d_inode, dentry, from);
1603 up(&nd.dentry->d_inode->i_sem);
1612 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1614 struct inode *inode;
1618 inode = old_dentry->d_inode;
1623 if (dir->i_dev != inode->i_dev)
1626 double_down(&dir->i_zombie, &old_dentry->d_inode->i_zombie);
1628 error = may_create(dir, new_dentry);
1633 * A link to an append-only or immutable file cannot be created.
1636 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1638 if (!dir->i_op || !dir->i_op->link)
1643 error = dir->i_op->link(old_dentry, dir, new_dentry);
1647 double_up(&dir->i_zombie, &old_dentry->d_inode->i_zombie);
1649 inode_dir_notify(dir, DN_CREATE);
1655 * Hardlinks are often used in delicate situations. We avoid
1656 * security-related surprises by not following symlinks on the
1659 * We don't follow them on the oldname either to be compatible
1660 * with linux 2.0, and to avoid hard-linking to directories
1661 * and other special files. --ADM
1663 asmlinkage long sys_link(const char * oldname, const char * newname)
1668 to = getname(newname);
1669 error = PTR_ERR(to);
1671 struct dentry *new_dentry;
1672 struct nameidata nd, old_nd;
1674 error = __user_walk(oldname, LOOKUP_POSITIVE, &old_nd);
1677 error = path_lookup(to, LOOKUP_PARENT, &nd);
1681 if (old_nd.mnt != nd.mnt)
1683 new_dentry = lookup_create(&nd, 0);
1684 error = PTR_ERR(new_dentry);
1685 if (!IS_ERR(new_dentry)) {
1686 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
1689 up(&nd.dentry->d_inode->i_sem);
1693 path_release(&old_nd);
1701 * The worst of all namespace operations - renaming directory. "Perverted"
1702 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1704 * a) we can get into loop creation. Check is done in is_subdir().
1705 * b) race potential - two innocent renames can create a loop together.
1706 * That's where 4.4 screws up. Current fix: serialization on
1707 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1709 * c) we have to lock _three_ objects - parents and victim (if it exists).
1710 * And that - after we got ->i_sem on parents (until then we don't know
1711 * whether the target exists at all, let alone whether it is a directory
1712 * or not). Solution: ->i_zombie. Taken only after ->i_sem. Always taken
1713 * on link creation/removal of any kind. And taken (without ->i_sem) on
1714 * directory that will be removed (both in rmdir() and here).
1715 * d) some filesystems don't support opened-but-unlinked directories,
1716 * either because of layout or because they are not ready to deal with
1717 * all cases correctly. The latter will be fixed (taking this sort of
1718 * stuff into VFS), but the former is not going away. Solution: the same
1719 * trick as in rmdir().
1720 * e) conversion from fhandle to dentry may come in the wrong moment - when
1721 * we are removing the target. Solution: we will have to grab ->i_zombie
1722 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1723 * ->i_sem on parents, which works but leads to some truely excessive
1726 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
1727 struct inode *new_dir, struct dentry *new_dentry)
1730 struct inode *target;
1732 if (old_dentry->d_inode == new_dentry->d_inode)
1735 error = may_delete(old_dir, old_dentry, 1);
1739 if (new_dir->i_dev != old_dir->i_dev)
1742 if (!new_dentry->d_inode)
1743 error = may_create(new_dir, new_dentry);
1745 error = may_delete(new_dir, new_dentry, 1);
1749 if (!old_dir->i_op || !old_dir->i_op->rename)
1753 * If we are going to change the parent - check write permissions,
1754 * we'll need to flip '..'.
1756 if (new_dir != old_dir) {
1757 error = permission(old_dentry->d_inode, MAY_WRITE);
1762 DQUOT_INIT(old_dir);
1763 DQUOT_INIT(new_dir);
1764 down(&old_dir->i_sb->s_vfs_rename_sem);
1766 if (is_subdir(new_dentry, old_dentry))
1768 /* Don't eat your daddy, dear... */
1769 /* This also avoids locking issues */
1770 if (old_dentry->d_parent == new_dentry)
1772 target = new_dentry->d_inode;
1773 if (target) { /* Hastur! Hastur! Hastur! */
1774 triple_down(&old_dir->i_zombie,
1777 d_unhash(new_dentry);
1779 double_down(&old_dir->i_zombie,
1780 &new_dir->i_zombie);
1781 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1784 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1787 target->i_flags |= S_DEAD;
1788 triple_up(&old_dir->i_zombie,
1791 if (d_unhashed(new_dentry))
1792 d_rehash(new_dentry);
1795 double_up(&old_dir->i_zombie,
1796 &new_dir->i_zombie);
1799 d_move(old_dentry,new_dentry);
1801 up(&old_dir->i_sb->s_vfs_rename_sem);
1805 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
1806 struct inode *new_dir, struct dentry *new_dentry)
1810 if (old_dentry->d_inode == new_dentry->d_inode)
1813 error = may_delete(old_dir, old_dentry, 0);
1817 if (new_dir->i_dev != old_dir->i_dev)
1820 if (!new_dentry->d_inode)
1821 error = may_create(new_dir, new_dentry);
1823 error = may_delete(new_dir, new_dentry, 0);
1827 if (!old_dir->i_op || !old_dir->i_op->rename)
1830 DQUOT_INIT(old_dir);
1831 DQUOT_INIT(new_dir);
1832 double_down(&old_dir->i_zombie, &new_dir->i_zombie);
1833 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1836 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1837 double_up(&old_dir->i_zombie, &new_dir->i_zombie);
1840 /* The following d_move() should become unconditional */
1841 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME)) {
1842 d_move(old_dentry, new_dentry);
1847 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1848 struct inode *new_dir, struct dentry *new_dentry)
1851 if (S_ISDIR(old_dentry->d_inode->i_mode))
1852 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
1854 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
1856 if (old_dir == new_dir)
1857 inode_dir_notify(old_dir, DN_RENAME);
1859 inode_dir_notify(old_dir, DN_DELETE);
1860 inode_dir_notify(new_dir, DN_CREATE);
1866 static inline int do_rename(const char * oldname, const char * newname)
1869 struct dentry * old_dir, * new_dir;
1870 struct dentry * old_dentry, *new_dentry;
1871 struct nameidata oldnd, newnd;
1873 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
1877 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
1882 if (oldnd.mnt != newnd.mnt)
1885 old_dir = oldnd.dentry;
1887 if (oldnd.last_type != LAST_NORM)
1890 new_dir = newnd.dentry;
1891 if (newnd.last_type != LAST_NORM)
1894 double_lock(new_dir, old_dir);
1896 old_dentry = lookup_hash(&oldnd.last, old_dir);
1897 error = PTR_ERR(old_dentry);
1898 if (IS_ERR(old_dentry))
1900 /* source must exist */
1902 if (!old_dentry->d_inode)
1904 /* unless the source is a directory trailing slashes give -ENOTDIR */
1905 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
1907 if (oldnd.last.name[oldnd.last.len])
1909 if (newnd.last.name[newnd.last.len])
1912 new_dentry = lookup_hash(&newnd.last, new_dir);
1913 error = PTR_ERR(new_dentry);
1914 if (IS_ERR(new_dentry))
1918 error = vfs_rename(old_dir->d_inode, old_dentry,
1919 new_dir->d_inode, new_dentry);
1926 double_up(&new_dir->d_inode->i_sem, &old_dir->d_inode->i_sem);
1928 path_release(&newnd);
1930 path_release(&oldnd);
1935 asmlinkage long sys_rename(const char * oldname, const char * newname)
1941 from = getname(oldname);
1943 return PTR_ERR(from);
1944 to = getname(newname);
1945 error = PTR_ERR(to);
1947 error = do_rename(from,to);
1954 int vfs_readlink(struct dentry *dentry, char *buffer, int buflen, const char *link)
1958 len = PTR_ERR(link);
1963 if (len > (unsigned) buflen)
1965 if (copy_to_user(buffer, link, len))
1972 __vfs_follow_link(struct nameidata *nd, const char *link)
1981 if (!walk_init_root(link, nd))
1982 /* weird __emul_prefix() stuff did it */
1985 res = link_path_walk(link, nd);
1987 if (current->link_count || res || nd->last_type!=LAST_NORM)
1990 * If it is an iterative symlinks resolution in open_namei() we
1991 * have to copy the last component. And all that crap because of
1992 * bloody create() on broken symlinks. Furrfu...
1999 strcpy(name, nd->last.name);
2000 nd->last.name = name;
2004 return PTR_ERR(link);
2007 int vfs_follow_link(struct nameidata *nd, const char *link)
2009 return __vfs_follow_link(nd, link);
2012 /* get the link contents into pagecache */
2013 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2016 struct address_space *mapping = dentry->d_inode->i_mapping;
2017 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2022 if (!Page_Uptodate(page))
2028 page_cache_release(page);
2029 return ERR_PTR(-EIO);
2035 int page_readlink(struct dentry *dentry, char *buffer, int buflen)
2037 struct page *page = NULL;
2038 char *s = page_getlink(dentry, &page);
2039 int res = vfs_readlink(dentry,buffer,buflen,s);
2042 page_cache_release(page);
2047 int page_follow_link(struct dentry *dentry, struct nameidata *nd)
2049 struct page *page = NULL;
2050 char *s = page_getlink(dentry, &page);
2051 int res = __vfs_follow_link(nd, s);
2054 page_cache_release(page);
2059 struct inode_operations page_symlink_inode_operations = {
2060 readlink: page_readlink,
2061 follow_link: page_follow_link,