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 8, 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 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 err = do_follow_link(dentry, nd);
548 inode = nd->dentry->d_inode;
559 if (!inode->i_op->lookup)
562 /* here ends the main loop */
565 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
567 if (lookup_flags & LOOKUP_PARENT)
569 if (this.name[0] == '.') switch (this.len) {
573 if (this.name[1] != '.')
576 inode = nd->dentry->d_inode;
581 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
582 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
586 dentry = cached_lookup(nd->dentry, &this, 0);
588 dentry = real_lookup(nd->dentry, &this, 0);
589 err = PTR_ERR(dentry);
593 while (d_mountpoint(dentry) && __follow_down(&nd->mnt, &dentry))
595 inode = dentry->d_inode;
596 if ((lookup_flags & LOOKUP_FOLLOW)
597 && inode && inode->i_op && inode->i_op->follow_link) {
598 err = do_follow_link(dentry, nd);
602 inode = nd->dentry->d_inode;
610 if (lookup_flags & LOOKUP_DIRECTORY) {
612 if (!inode->i_op || !inode->i_op->lookup)
618 if (lookup_flags & (LOOKUP_POSITIVE|LOOKUP_DIRECTORY))
623 nd->last_type = LAST_NORM;
624 if (this.name[0] != '.')
627 nd->last_type = LAST_DOT;
628 else if (this.len == 2 && this.name[1] == '.')
629 nd->last_type = LAST_DOTDOT;
632 * We bypassed the ordinary revalidation routines.
633 * Check the cached dentry for staleness.
636 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
638 if (!dentry->d_op->d_revalidate(dentry, 0)) {
639 d_invalidate(dentry);
654 int path_walk(const char * name, struct nameidata *nd)
656 current->total_link_count = 0;
657 return link_path_walk(name, nd);
661 /* returns 1 if everything is done */
662 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
664 if (path_walk(name, nd))
665 return 0; /* something went wrong... */
667 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
668 struct nameidata nd_root;
670 * NAME was not found in alternate root or it's a directory. Try to find
671 * it in the normal root:
673 nd_root.last_type = LAST_ROOT;
674 nd_root.flags = nd->flags;
675 read_lock(¤t->fs->lock);
676 nd_root.mnt = mntget(current->fs->rootmnt);
677 nd_root.dentry = dget(current->fs->root);
678 read_unlock(¤t->fs->lock);
679 if (path_walk(name, &nd_root))
681 if (nd_root.dentry->d_inode) {
683 nd->dentry = nd_root.dentry;
684 nd->mnt = nd_root.mnt;
685 nd->last = nd_root.last;
688 path_release(&nd_root);
693 void set_fs_altroot(void)
695 char *emul = __emul_prefix();
697 struct vfsmount *mnt = NULL, *oldmnt;
698 struct dentry *dentry = NULL, *olddentry;
700 read_lock(¤t->fs->lock);
701 nd.mnt = mntget(current->fs->rootmnt);
702 nd.dentry = dget(current->fs->root);
703 read_unlock(¤t->fs->lock);
704 nd.flags = LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_POSITIVE;
705 if (path_walk(emul,&nd) == 0) {
710 write_lock(¤t->fs->lock);
711 oldmnt = current->fs->altrootmnt;
712 olddentry = current->fs->altroot;
713 current->fs->altrootmnt = mnt;
714 current->fs->altroot = dentry;
715 write_unlock(¤t->fs->lock);
724 walk_init_root(const char *name, struct nameidata *nd)
726 read_lock(¤t->fs->lock);
727 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
728 nd->mnt = mntget(current->fs->altrootmnt);
729 nd->dentry = dget(current->fs->altroot);
730 read_unlock(¤t->fs->lock);
731 if (__emul_lookup_dentry(name,nd))
733 read_lock(¤t->fs->lock);
735 nd->mnt = mntget(current->fs->rootmnt);
736 nd->dentry = dget(current->fs->root);
737 read_unlock(¤t->fs->lock);
742 int path_lookup(const char *path, unsigned flags, struct nameidata *nd)
745 if (path_init(path, flags, nd))
746 error = path_walk(path, nd);
752 int path_init(const char *name, unsigned int flags, struct nameidata *nd)
754 nd->last_type = LAST_ROOT; /* if there are only slashes... */
757 return walk_init_root(name,nd);
758 read_lock(¤t->fs->lock);
759 nd->mnt = mntget(current->fs->pwdmnt);
760 nd->dentry = dget(current->fs->pwd);
761 read_unlock(¤t->fs->lock);
766 * Restricted form of lookup. Doesn't follow links, single-component only,
767 * needs parent already locked. Doesn't follow mounts.
770 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
772 struct dentry * dentry;
776 inode = base->d_inode;
777 err = permission(inode, MAY_EXEC);
778 dentry = ERR_PTR(err);
783 * See if the low-level filesystem might want
784 * to use its own hash..
786 if (base->d_op && base->d_op->d_hash) {
787 err = base->d_op->d_hash(base, name);
788 dentry = ERR_PTR(err);
793 dentry = cached_lookup(base, name, 0);
795 struct dentry *new = d_alloc(base, name);
796 dentry = ERR_PTR(-ENOMEM);
800 dentry = inode->i_op->lookup(inode, new);
812 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
823 hash = init_name_hash();
825 c = *(const unsigned char *)name++;
826 if (c == '/' || c == '\0')
828 hash = partial_name_hash(c, hash);
830 this.hash = end_name_hash(hash);
832 return lookup_hash(&this, base);
834 return ERR_PTR(-EACCES);
840 * is used by most simple commands to get the inode of a specified name.
841 * Open, link etc use their own routines, but this is enough for things
844 * namei exists in two versions: namei/lnamei. The only difference is
845 * that namei follows links, while lnamei does not.
848 int __user_walk(const char *name, unsigned flags, struct nameidata *nd)
857 err = path_lookup(tmp, flags, nd);
864 * It's inline, so penalty for filesystems that don't use sticky bit is
867 static inline int check_sticky(struct inode *dir, struct inode *inode)
869 if (!(dir->i_mode & S_ISVTX))
871 if (inode->i_uid == current->fsuid)
873 if (dir->i_uid == current->fsuid)
875 return !capable(CAP_FOWNER);
879 * Check whether we can remove a link victim from directory dir, check
880 * whether the type of victim is right.
881 * 1. We can't do it if dir is read-only (done in permission())
882 * 2. We should have write and exec permissions on dir
883 * 3. We can't remove anything from append-only dir
884 * 4. We can't do anything with immutable dir (done in permission())
885 * 5. If the sticky bit on dir is set we should either
886 * a. be owner of dir, or
887 * b. be owner of victim, or
888 * c. have CAP_FOWNER capability
889 * 6. If the victim is append-only or immutable we can't do antyhing with
890 * links pointing to it.
891 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
892 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
893 * 9. We can't remove a root or mountpoint.
895 static inline int may_delete(struct inode *dir,struct dentry *victim, int isdir)
898 if (!victim->d_inode || victim->d_parent->d_inode != dir)
900 error = permission(dir,MAY_WRITE | MAY_EXEC);
905 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
906 IS_IMMUTABLE(victim->d_inode))
909 if (!S_ISDIR(victim->d_inode->i_mode))
913 } else if (S_ISDIR(victim->d_inode->i_mode))
920 /* Check whether we can create an object with dentry child in directory
922 * 1. We can't do it if child already exists (open has special treatment for
923 * this case, but since we are inlined it's OK)
924 * 2. We can't do it if dir is read-only (done in permission())
925 * 3. We should have write and exec permissions on dir
926 * 4. We can't do it if dir is immutable (done in permission())
928 static inline int may_create(struct inode *dir, struct dentry *child) {
933 return permission(dir,MAY_WRITE | MAY_EXEC);
937 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
940 * O_DIRECTORY translates into forcing a directory lookup.
942 static inline int lookup_flags(unsigned int f)
944 unsigned long retval = LOOKUP_FOLLOW;
947 retval &= ~LOOKUP_FOLLOW;
949 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
950 retval &= ~LOOKUP_FOLLOW;
953 retval |= LOOKUP_DIRECTORY;
958 int vfs_create(struct inode *dir, struct dentry *dentry, int mode)
965 down(&dir->i_zombie);
966 error = may_create(dir, dentry);
970 error = -EACCES; /* shouldn't it be ENOSYS? */
971 if (!dir->i_op || !dir->i_op->create)
976 error = dir->i_op->create(dir, dentry, mode);
981 inode_dir_notify(dir, DN_CREATE);
988 * namei for open - this is in fact almost the whole open-routine.
990 * Note that the low bits of "flag" aren't the same as in the open
991 * system call - they are 00 - no permissions needed
992 * 01 - read permission needed
993 * 10 - write permission needed
994 * 11 - read/write permissions needed
995 * which is a lot more logical, and also allows the "no perm" needed
996 * for symlinks (where the permissions are checked later).
999 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1001 int acc_mode, error = 0;
1002 struct inode *inode;
1003 struct dentry *dentry;
1007 acc_mode = ACC_MODE(flag);
1010 * The simplest case - just a plain lookup.
1012 if (!(flag & O_CREAT)) {
1013 error = path_lookup(pathname, lookup_flags(flag), nd);
1016 dentry = nd->dentry;
1021 * Create - we need to know the parent.
1023 error = path_lookup(pathname, LOOKUP_PARENT, nd);
1028 * We have the parent and last component. First of all, check
1029 * that we are not asked to creat(2) an obvious directory - that
1033 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1037 down(&dir->d_inode->i_sem);
1038 dentry = lookup_hash(&nd->last, nd->dentry);
1041 error = PTR_ERR(dentry);
1042 if (IS_ERR(dentry)) {
1043 up(&dir->d_inode->i_sem);
1047 /* Negative dentry, just create the file */
1048 if (!dentry->d_inode) {
1049 error = vfs_create(dir->d_inode, dentry,
1050 mode & ~current->fs->umask);
1051 up(&dir->d_inode->i_sem);
1053 nd->dentry = dentry;
1056 /* Don't check for write permission, don't truncate */
1063 * It already exists.
1065 up(&dir->d_inode->i_sem);
1071 if (d_mountpoint(dentry)) {
1073 if (flag & O_NOFOLLOW)
1075 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1078 if (!dentry->d_inode)
1080 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1084 nd->dentry = dentry;
1086 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1090 inode = dentry->d_inode;
1095 if (S_ISLNK(inode->i_mode))
1099 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1102 error = permission(inode,acc_mode);
1107 * FIFO's, sockets and device files are special: they don't
1108 * actually live on the filesystem itself, and as such you
1109 * can write to them even if the filesystem is read-only.
1111 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1113 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1115 if (nd->mnt->mnt_flags & MNT_NODEV)
1121 if (IS_RDONLY(inode) && (flag & 2))
1125 * An append-only file must be opened in append mode for writing.
1128 if (IS_APPEND(inode)) {
1129 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1136 * Ensure there are no outstanding leases on the file.
1138 error = get_lease(inode, flag);
1142 if (flag & O_TRUNC) {
1143 error = get_write_access(inode);
1148 * Refuse to truncate files with mandatory locks held on them.
1150 error = locks_verify_locked(inode);
1154 error = do_truncate(dentry, 0);
1156 put_write_access(inode);
1160 if (flag & FMODE_WRITE)
1173 if (flag & O_NOFOLLOW)
1176 * This is subtle. Instead of calling do_follow_link() we do the
1177 * thing by hands. The reason is that this way we have zero link_count
1178 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1179 * After that we have the parent and last component, i.e.
1180 * we are in the same situation as after the first path_walk().
1181 * Well, almost - if the last component is normal we get its copy
1182 * stored in nd->last.name and we will have to putname() it when we
1183 * are done. Procfs-like symlinks just set LAST_BIND.
1185 UPDATE_ATIME(dentry->d_inode);
1186 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1190 if (nd->last_type == LAST_BIND) {
1191 dentry = nd->dentry;
1195 if (nd->last_type != LAST_NORM)
1197 if (nd->last.name[nd->last.len]) {
1198 putname(nd->last.name);
1203 putname(nd->last.name);
1207 down(&dir->d_inode->i_sem);
1208 dentry = lookup_hash(&nd->last, nd->dentry);
1209 putname(nd->last.name);
1214 static struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1216 struct dentry *dentry;
1218 down(&nd->dentry->d_inode->i_sem);
1219 dentry = ERR_PTR(-EEXIST);
1220 if (nd->last_type != LAST_NORM)
1222 dentry = lookup_hash(&nd->last, nd->dentry);
1225 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1230 dentry = ERR_PTR(-ENOENT);
1235 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1239 down(&dir->i_zombie);
1240 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1243 error = may_create(dir, dentry);
1248 if (!dir->i_op || !dir->i_op->mknod)
1253 error = dir->i_op->mknod(dir, dentry, mode, dev);
1258 inode_dir_notify(dir, DN_CREATE);
1262 asmlinkage long sys_mknod(const char * filename, int mode, dev_t dev)
1266 struct dentry * dentry;
1267 struct nameidata nd;
1271 tmp = getname(filename);
1273 return PTR_ERR(tmp);
1275 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1278 dentry = lookup_create(&nd, 0);
1279 error = PTR_ERR(dentry);
1281 mode &= ~current->fs->umask;
1282 if (!IS_ERR(dentry)) {
1283 switch (mode & S_IFMT) {
1284 case 0: case S_IFREG:
1285 error = vfs_create(nd.dentry->d_inode,dentry,mode);
1287 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
1288 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,dev);
1298 up(&nd.dentry->d_inode->i_sem);
1306 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1310 down(&dir->i_zombie);
1311 error = may_create(dir, dentry);
1316 if (!dir->i_op || !dir->i_op->mkdir)
1320 mode &= (S_IRWXUGO|S_ISVTX);
1322 error = dir->i_op->mkdir(dir, dentry, mode);
1328 inode_dir_notify(dir, DN_CREATE);
1332 asmlinkage long sys_mkdir(const char * pathname, int mode)
1337 tmp = getname(pathname);
1338 error = PTR_ERR(tmp);
1340 struct dentry *dentry;
1341 struct nameidata nd;
1343 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1346 dentry = lookup_create(&nd, 1);
1347 error = PTR_ERR(dentry);
1348 if (!IS_ERR(dentry)) {
1349 error = vfs_mkdir(nd.dentry->d_inode, dentry,
1350 mode & ~current->fs->umask);
1353 up(&nd.dentry->d_inode->i_sem);
1363 * We try to drop the dentry early: we should have
1364 * a usage count of 2 if we're the only user of this
1365 * dentry, and if that is true (possibly after pruning
1366 * the dcache), then we drop the dentry now.
1368 * A low-level filesystem can, if it choses, legally
1371 * if (!d_unhashed(dentry))
1374 * if it cannot handle the case of removing a directory
1375 * that is still in use by something else..
1377 static void d_unhash(struct dentry *dentry)
1380 spin_lock(&dcache_lock);
1381 switch (atomic_read(&dentry->d_count)) {
1383 spin_unlock(&dcache_lock);
1384 shrink_dcache_parent(dentry);
1385 spin_lock(&dcache_lock);
1386 if (atomic_read(&dentry->d_count) != 2)
1389 list_del_init(&dentry->d_hash);
1391 spin_unlock(&dcache_lock);
1394 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1398 error = may_delete(dir, dentry, 1);
1402 if (!dir->i_op || !dir->i_op->rmdir)
1407 double_down(&dir->i_zombie, &dentry->d_inode->i_zombie);
1409 if (d_mountpoint(dentry))
1413 error = dir->i_op->rmdir(dir, dentry);
1416 dentry->d_inode->i_flags |= S_DEAD;
1418 double_up(&dir->i_zombie, &dentry->d_inode->i_zombie);
1420 inode_dir_notify(dir, DN_DELETE);
1428 asmlinkage long sys_rmdir(const char * pathname)
1432 struct dentry *dentry;
1433 struct nameidata nd;
1435 name = getname(pathname);
1437 return PTR_ERR(name);
1439 error = path_lookup(name, LOOKUP_PARENT, &nd);
1443 switch(nd.last_type) {
1454 down(&nd.dentry->d_inode->i_sem);
1455 dentry = lookup_hash(&nd.last, nd.dentry);
1456 error = PTR_ERR(dentry);
1457 if (!IS_ERR(dentry)) {
1458 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1461 up(&nd.dentry->d_inode->i_sem);
1469 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1473 down(&dir->i_zombie);
1474 error = may_delete(dir, dentry, 0);
1477 if (dir->i_op && dir->i_op->unlink) {
1479 if (d_mountpoint(dentry))
1483 error = dir->i_op->unlink(dir, dentry);
1492 inode_dir_notify(dir, DN_DELETE);
1496 asmlinkage long sys_unlink(const char * pathname)
1500 struct dentry *dentry;
1501 struct nameidata nd;
1503 name = getname(pathname);
1505 return PTR_ERR(name);
1507 error = path_lookup(name, LOOKUP_PARENT, &nd);
1511 if (nd.last_type != LAST_NORM)
1513 down(&nd.dentry->d_inode->i_sem);
1514 dentry = lookup_hash(&nd.last, nd.dentry);
1515 error = PTR_ERR(dentry);
1516 if (!IS_ERR(dentry)) {
1517 /* Why not before? Because we want correct error value */
1518 if (nd.last.name[nd.last.len])
1520 error = vfs_unlink(nd.dentry->d_inode, dentry);
1524 up(&nd.dentry->d_inode->i_sem);
1533 error = !dentry->d_inode ? -ENOENT :
1534 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1538 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1542 down(&dir->i_zombie);
1543 error = may_create(dir, dentry);
1548 if (!dir->i_op || !dir->i_op->symlink)
1553 error = dir->i_op->symlink(dir, dentry, oldname);
1559 inode_dir_notify(dir, DN_CREATE);
1563 asmlinkage long sys_symlink(const char * oldname, const char * newname)
1569 from = getname(oldname);
1571 return PTR_ERR(from);
1572 to = getname(newname);
1573 error = PTR_ERR(to);
1575 struct dentry *dentry;
1576 struct nameidata nd;
1578 error = path_lookup(to, LOOKUP_PARENT, &nd);
1581 dentry = lookup_create(&nd, 0);
1582 error = PTR_ERR(dentry);
1583 if (!IS_ERR(dentry)) {
1584 error = vfs_symlink(nd.dentry->d_inode, dentry, from);
1587 up(&nd.dentry->d_inode->i_sem);
1596 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1598 struct inode *inode;
1601 down(&dir->i_zombie);
1603 inode = old_dentry->d_inode;
1607 error = may_create(dir, new_dentry);
1612 if (dir->i_dev != inode->i_dev)
1616 * A link to an append-only or immutable file cannot be created.
1619 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1621 if (!dir->i_op || !dir->i_op->link)
1626 error = dir->i_op->link(old_dentry, dir, new_dentry);
1632 inode_dir_notify(dir, DN_CREATE);
1637 * Hardlinks are often used in delicate situations. We avoid
1638 * security-related surprises by not following symlinks on the
1641 * We don't follow them on the oldname either to be compatible
1642 * with linux 2.0, and to avoid hard-linking to directories
1643 * and other special files. --ADM
1645 asmlinkage long sys_link(const char * oldname, const char * newname)
1650 to = getname(newname);
1651 error = PTR_ERR(to);
1653 struct dentry *new_dentry;
1654 struct nameidata nd, old_nd;
1656 error = __user_walk(oldname, LOOKUP_POSITIVE, &old_nd);
1659 error = path_lookup(to, LOOKUP_PARENT, &nd);
1663 if (old_nd.mnt != nd.mnt)
1665 new_dentry = lookup_create(&nd, 0);
1666 error = PTR_ERR(new_dentry);
1667 if (!IS_ERR(new_dentry)) {
1668 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
1671 up(&nd.dentry->d_inode->i_sem);
1675 path_release(&old_nd);
1683 * The worst of all namespace operations - renaming directory. "Perverted"
1684 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1686 * a) we can get into loop creation. Check is done in is_subdir().
1687 * b) race potential - two innocent renames can create a loop together.
1688 * That's where 4.4 screws up. Current fix: serialization on
1689 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1691 * c) we have to lock _three_ objects - parents and victim (if it exists).
1692 * And that - after we got ->i_sem on parents (until then we don't know
1693 * whether the target exists at all, let alone whether it is a directory
1694 * or not). Solution: ->i_zombie. Taken only after ->i_sem. Always taken
1695 * on link creation/removal of any kind. And taken (without ->i_sem) on
1696 * directory that will be removed (both in rmdir() and here).
1697 * d) some filesystems don't support opened-but-unlinked directories,
1698 * either because of layout or because they are not ready to deal with
1699 * all cases correctly. The latter will be fixed (taking this sort of
1700 * stuff into VFS), but the former is not going away. Solution: the same
1701 * trick as in rmdir().
1702 * e) conversion from fhandle to dentry may come in the wrong moment - when
1703 * we are removing the target. Solution: we will have to grab ->i_zombie
1704 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1705 * ->i_sem on parents, which works but leads to some truely excessive
1708 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
1709 struct inode *new_dir, struct dentry *new_dentry)
1712 struct inode *target;
1714 if (old_dentry->d_inode == new_dentry->d_inode)
1717 error = may_delete(old_dir, old_dentry, 1);
1721 if (new_dir->i_dev != old_dir->i_dev)
1724 if (!new_dentry->d_inode)
1725 error = may_create(new_dir, new_dentry);
1727 error = may_delete(new_dir, new_dentry, 1);
1731 if (!old_dir->i_op || !old_dir->i_op->rename)
1735 * If we are going to change the parent - check write permissions,
1736 * we'll need to flip '..'.
1738 if (new_dir != old_dir) {
1739 error = permission(old_dentry->d_inode, MAY_WRITE);
1744 DQUOT_INIT(old_dir);
1745 DQUOT_INIT(new_dir);
1746 down(&old_dir->i_sb->s_vfs_rename_sem);
1748 if (is_subdir(new_dentry, old_dentry))
1750 /* Don't eat your daddy, dear... */
1751 /* This also avoids locking issues */
1752 if (old_dentry->d_parent == new_dentry)
1754 target = new_dentry->d_inode;
1755 if (target) { /* Hastur! Hastur! Hastur! */
1756 triple_down(&old_dir->i_zombie,
1759 d_unhash(new_dentry);
1761 double_down(&old_dir->i_zombie,
1762 &new_dir->i_zombie);
1763 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1766 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1769 target->i_flags |= S_DEAD;
1770 triple_up(&old_dir->i_zombie,
1773 if (d_unhashed(new_dentry))
1774 d_rehash(new_dentry);
1777 double_up(&old_dir->i_zombie,
1778 &new_dir->i_zombie);
1781 d_move(old_dentry,new_dentry);
1783 up(&old_dir->i_sb->s_vfs_rename_sem);
1787 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
1788 struct inode *new_dir, struct dentry *new_dentry)
1792 if (old_dentry->d_inode == new_dentry->d_inode)
1795 error = may_delete(old_dir, old_dentry, 0);
1799 if (new_dir->i_dev != old_dir->i_dev)
1802 if (!new_dentry->d_inode)
1803 error = may_create(new_dir, new_dentry);
1805 error = may_delete(new_dir, new_dentry, 0);
1809 if (!old_dir->i_op || !old_dir->i_op->rename)
1812 DQUOT_INIT(old_dir);
1813 DQUOT_INIT(new_dir);
1814 double_down(&old_dir->i_zombie, &new_dir->i_zombie);
1815 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1818 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1819 double_up(&old_dir->i_zombie, &new_dir->i_zombie);
1822 /* The following d_move() should become unconditional */
1823 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME)) {
1824 d_move(old_dentry, new_dentry);
1829 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1830 struct inode *new_dir, struct dentry *new_dentry)
1833 if (S_ISDIR(old_dentry->d_inode->i_mode))
1834 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
1836 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
1838 if (old_dir == new_dir)
1839 inode_dir_notify(old_dir, DN_RENAME);
1841 inode_dir_notify(old_dir, DN_DELETE);
1842 inode_dir_notify(new_dir, DN_CREATE);
1848 static inline int do_rename(const char * oldname, const char * newname)
1851 struct dentry * old_dir, * new_dir;
1852 struct dentry * old_dentry, *new_dentry;
1853 struct nameidata oldnd, newnd;
1855 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
1859 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
1864 if (oldnd.mnt != newnd.mnt)
1867 old_dir = oldnd.dentry;
1869 if (oldnd.last_type != LAST_NORM)
1872 new_dir = newnd.dentry;
1873 if (newnd.last_type != LAST_NORM)
1876 double_lock(new_dir, old_dir);
1878 old_dentry = lookup_hash(&oldnd.last, old_dir);
1879 error = PTR_ERR(old_dentry);
1880 if (IS_ERR(old_dentry))
1882 /* source must exist */
1884 if (!old_dentry->d_inode)
1886 /* unless the source is a directory trailing slashes give -ENOTDIR */
1887 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
1889 if (oldnd.last.name[oldnd.last.len])
1891 if (newnd.last.name[newnd.last.len])
1894 new_dentry = lookup_hash(&newnd.last, new_dir);
1895 error = PTR_ERR(new_dentry);
1896 if (IS_ERR(new_dentry))
1900 error = vfs_rename(old_dir->d_inode, old_dentry,
1901 new_dir->d_inode, new_dentry);
1908 double_up(&new_dir->d_inode->i_sem, &old_dir->d_inode->i_sem);
1910 path_release(&newnd);
1912 path_release(&oldnd);
1917 asmlinkage long sys_rename(const char * oldname, const char * newname)
1923 from = getname(oldname);
1925 return PTR_ERR(from);
1926 to = getname(newname);
1927 error = PTR_ERR(to);
1929 error = do_rename(from,to);
1936 int vfs_readlink(struct dentry *dentry, char *buffer, int buflen, const char *link)
1940 len = PTR_ERR(link);
1945 if (len > (unsigned) buflen)
1947 if (copy_to_user(buffer, link, len))
1954 __vfs_follow_link(struct nameidata *nd, const char *link)
1963 if (!walk_init_root(link, nd))
1964 /* weird __emul_prefix() stuff did it */
1967 res = link_path_walk(link, nd);
1969 if (current->link_count || res || nd->last_type!=LAST_NORM)
1972 * If it is an iterative symlinks resolution in open_namei() we
1973 * have to copy the last component. And all that crap because of
1974 * bloody create() on broken symlinks. Furrfu...
1981 strcpy(name, nd->last.name);
1982 nd->last.name = name;
1986 return PTR_ERR(link);
1989 int vfs_follow_link(struct nameidata *nd, const char *link)
1991 return __vfs_follow_link(nd, link);
1994 /* get the link contents into pagecache */
1995 static char *page_getlink(struct dentry * dentry, struct page **ppage)
1998 struct address_space *mapping = dentry->d_inode->i_mapping;
1999 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2004 if (!Page_Uptodate(page))
2010 page_cache_release(page);
2011 return ERR_PTR(-EIO);
2017 int page_readlink(struct dentry *dentry, char *buffer, int buflen)
2019 struct page *page = NULL;
2020 char *s = page_getlink(dentry, &page);
2021 int res = vfs_readlink(dentry,buffer,buflen,s);
2024 page_cache_release(page);
2029 int page_follow_link(struct dentry *dentry, struct nameidata *nd)
2031 struct page *page = NULL;
2032 char *s = page_getlink(dentry, &page);
2033 int res = __vfs_follow_link(nd, s);
2036 page_cache_release(page);
2041 struct inode_operations page_symlink_inode_operations = {
2042 readlink: page_readlink,
2043 follow_link: page_follow_link,