Merge with Linux 2.4.0-test6-pre2.
[linux-2.6/linux-mips.git] / fs / namei.c
blob97d9e2d22184cff0e11974875a7c42a138f067fb
1 /*
2 * linux/fs/namei.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * Some corrections by tytso.
9 */
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
17 #include <linux/init.h>
18 #include <linux/mm.h>
19 #include <linux/proc_fs.h>
20 #include <linux/smp_lock.h>
21 #include <linux/quotaops.h>
22 #include <linux/pagemap.h>
23 #include <linux/dcache.h>
25 #include <asm/uaccess.h>
26 #include <asm/unaligned.h>
27 #include <asm/semaphore.h>
28 #include <asm/page.h>
29 #include <asm/pgtable.h>
31 #include <asm/namei.h>
33 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
35 /* [Feb-1997 T. Schoebel-Theuer]
36 * Fundamental changes in the pathname lookup mechanisms (namei)
37 * were necessary because of omirr. The reason is that omirr needs
38 * to know the _real_ pathname, not the user-supplied one, in case
39 * of symlinks (and also when transname replacements occur).
41 * The new code replaces the old recursive symlink resolution with
42 * an iterative one (in case of non-nested symlink chains). It does
43 * this with calls to <fs>_follow_link().
44 * As a side effect, dir_namei(), _namei() and follow_link() are now
45 * replaced with a single function lookup_dentry() that can handle all
46 * the special cases of the former code.
48 * With the new dcache, the pathname is stored at each inode, at least as
49 * long as the refcount of the inode is positive. As a side effect, the
50 * size of the dcache depends on the inode cache and thus is dynamic.
52 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
53 * resolution to correspond with current state of the code.
55 * Note that the symlink resolution is not *completely* iterative.
56 * There is still a significant amount of tail- and mid- recursion in
57 * the algorithm. Also, note that <fs>_readlink() is not used in
58 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
59 * may return different results than <fs>_follow_link(). Many virtual
60 * filesystems (including /proc) exhibit this behavior.
63 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
64 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
65 * and the name already exists in form of a symlink, try to create the new
66 * name indicated by the symlink. The old code always complained that the
67 * name already exists, due to not following the symlink even if its target
68 * is nonexistent. The new semantics affects also mknod() and link() when
69 * the name is a symlink pointing to a non-existant name.
71 * I don't know which semantics is the right one, since I have no access
72 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
73 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
74 * "old" one. Personally, I think the new semantics is much more logical.
75 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
76 * file does succeed in both HP-UX and SunOs, but not in Solaris
77 * and in the old Linux semantics.
80 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
81 * semantics. See the comments in "open_namei" and "do_link" below.
83 * [10-Sep-98 Alan Modra] Another symlink change.
86 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
87 * inside the path - always follow.
88 * in the last component in creation/removal/renaming - never follow.
89 * if LOOKUP_FOLLOW passed - follow.
90 * if the pathname has trailing slashes - follow.
91 * otherwise - don't follow.
92 * (applied in that order).
94 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
95 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
96 * During the 2.4 we need to fix the userland stuff depending on it -
97 * hopefully we will be able to get rid of that wart in 2.5. So far only
98 * XEmacs seems to be relying on it...
101 /* In order to reduce some races, while at the same time doing additional
102 * checking and hopefully speeding things up, we copy filenames to the
103 * kernel data space before using them..
105 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
107 static inline int do_getname(const char *filename, char *page)
109 int retval;
110 unsigned long len = PAGE_SIZE;
112 if ((unsigned long) filename >= TASK_SIZE) {
113 if (!segment_eq(get_fs(), KERNEL_DS))
114 return -EFAULT;
115 } else if (TASK_SIZE - (unsigned long) filename < PAGE_SIZE)
116 len = TASK_SIZE - (unsigned long) filename;
118 retval = strncpy_from_user((char *)page, filename, len);
119 if (retval > 0) {
120 if (retval < len)
121 return 0;
122 return -ENAMETOOLONG;
123 } else if (!retval)
124 retval = -ENOENT;
125 return retval;
128 char * getname(const char * filename)
130 char *tmp, *result;
132 result = ERR_PTR(-ENOMEM);
133 tmp = __getname();
134 if (tmp) {
135 int retval = do_getname(filename, tmp);
137 result = tmp;
138 if (retval < 0) {
139 putname(tmp);
140 result = ERR_PTR(retval);
143 return result;
147 * permission()
149 * is used to check for read/write/execute permissions on a file.
150 * We use "fsuid" for this, letting us set arbitrary permissions
151 * for filesystem access without changing the "normal" uids which
152 * are used for other things..
154 int permission(struct inode * inode,int mask)
156 int mode = inode->i_mode;
158 if (inode->i_op && inode->i_op->permission) {
159 int retval;
160 lock_kernel();
161 retval = inode->i_op->permission(inode, mask);
162 unlock_kernel();
163 return retval;
166 if ((mask & S_IWOTH) && IS_RDONLY(inode) &&
167 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
168 return -EROFS; /* Nobody gets write access to a read-only fs */
170 if ((mask & S_IWOTH) && IS_IMMUTABLE(inode))
171 return -EACCES; /* Nobody gets write access to an immutable file */
173 if (current->fsuid == inode->i_uid)
174 mode >>= 6;
175 else if (in_group_p(inode->i_gid))
176 mode >>= 3;
178 if (((mode & mask & S_IRWXO) == mask) || capable(CAP_DAC_OVERRIDE))
179 return 0;
181 /* read and search access */
182 if ((mask == S_IROTH) ||
183 (S_ISDIR(mode) && !(mask & ~(S_IROTH | S_IXOTH))))
184 if (capable(CAP_DAC_READ_SEARCH))
185 return 0;
187 return -EACCES;
191 * get_write_access() gets write permission for a file.
192 * put_write_access() releases this write permission.
193 * This is used for regular files.
194 * We cannot support write (and maybe mmap read-write shared) accesses and
195 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
196 * can have the following values:
197 * 0: no writers, no VM_DENYWRITE mappings
198 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
199 * > 0: (i_writecount) users are writing to the file.
201 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
202 * except for the cases where we don't hold i_writecount yet. Then we need to
203 * use {get,deny}_write_access() - these functions check the sign and refuse
204 * to do the change if sign is wrong. Exclusion between them is provided by
205 * spinlock (arbitration_lock) and I'll rip the second arsehole to the first
206 * who will try to move it in struct inode - just leave it here.
208 static spinlock_t arbitration_lock = SPIN_LOCK_UNLOCKED;
209 int get_write_access(struct inode * inode)
211 spin_lock(&arbitration_lock);
212 if (atomic_read(&inode->i_writecount) < 0) {
213 spin_unlock(&arbitration_lock);
214 return -ETXTBSY;
216 atomic_inc(&inode->i_writecount);
217 spin_unlock(&arbitration_lock);
218 return 0;
220 int deny_write_access(struct file * file)
222 spin_lock(&arbitration_lock);
223 if (atomic_read(&file->f_dentry->d_inode->i_writecount) > 0) {
224 spin_unlock(&arbitration_lock);
225 return -ETXTBSY;
227 atomic_dec(&file->f_dentry->d_inode->i_writecount);
228 spin_unlock(&arbitration_lock);
229 return 0;
232 void path_release(struct nameidata *nd)
234 dput(nd->dentry);
235 mntput(nd->mnt);
239 * Internal lookup() using the new generic dcache.
240 * SMP-safe
242 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, int flags)
244 struct dentry * dentry = d_lookup(parent, name);
246 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
247 if (!dentry->d_op->d_revalidate(dentry, flags) && !d_invalidate(dentry)) {
248 dput(dentry);
249 dentry = NULL;
252 return dentry;
256 * This is called when everything else fails, and we actually have
257 * to go to the low-level filesystem to find out what we should do..
259 * We get the directory semaphore, and after getting that we also
260 * make sure that nobody added the entry to the dcache in the meantime..
261 * SMP-safe
263 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, int flags)
265 struct dentry * result;
266 struct inode *dir = parent->d_inode;
268 down(&dir->i_sem);
270 * First re-do the cached lookup just in case it was created
271 * while we waited for the directory semaphore..
273 * FIXME! This could use version numbering or similar to
274 * avoid unnecessary cache lookups.
276 result = d_lookup(parent, name);
277 if (!result) {
278 struct dentry * dentry = d_alloc(parent, name);
279 result = ERR_PTR(-ENOMEM);
280 if (dentry) {
281 lock_kernel();
282 result = dir->i_op->lookup(dir, dentry);
283 unlock_kernel();
284 if (result)
285 dput(dentry);
286 else
287 result = dentry;
289 up(&dir->i_sem);
290 return result;
294 * Uhhuh! Nasty case: the cache was re-populated while
295 * we waited on the semaphore. Need to revalidate.
297 up(&dir->i_sem);
298 if (result->d_op && result->d_op->d_revalidate) {
299 if (!result->d_op->d_revalidate(result, flags) && !d_invalidate(result)) {
300 dput(result);
301 result = ERR_PTR(-ENOENT);
304 return result;
307 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
309 int err;
310 if (current->link_count >= 8)
311 goto loop;
312 current->link_count++;
313 UPDATE_ATIME(dentry->d_inode);
314 err = dentry->d_inode->i_op->follow_link(dentry, nd);
315 current->link_count--;
316 return err;
317 loop:
318 path_release(nd);
319 return -ELOOP;
322 static inline int __follow_up(struct vfsmount **mnt, struct dentry **base)
324 struct vfsmount *parent;
325 struct dentry *dentry;
326 spin_lock(&dcache_lock);
327 parent=(*mnt)->mnt_parent;
328 if (parent == *mnt) {
329 spin_unlock(&dcache_lock);
330 return 0;
332 mntget(parent);
333 dentry=dget((*mnt)->mnt_mountpoint);
334 spin_unlock(&dcache_lock);
335 dput(*base);
336 *base = dentry;
337 mntput(*mnt);
338 *mnt = parent;
339 return 1;
342 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
344 return __follow_up(mnt, dentry);
347 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
349 struct list_head *p;
350 spin_lock(&dcache_lock);
351 p = (*dentry)->d_vfsmnt.next;
352 while (p != &(*dentry)->d_vfsmnt) {
353 struct vfsmount *tmp;
354 tmp = list_entry(p, struct vfsmount, mnt_clash);
355 if (tmp->mnt_parent == *mnt) {
356 *mnt = mntget(tmp);
357 spin_unlock(&dcache_lock);
358 mntput(tmp->mnt_parent);
359 /* tmp holds the mountpoint, so... */
360 dput(*dentry);
361 *dentry = dget(tmp->mnt_root);
362 return 1;
364 p = p->next;
366 spin_unlock(&dcache_lock);
367 return 0;
370 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
372 return __follow_down(mnt,dentry);
375 static inline void follow_dotdot(struct nameidata *nd)
377 while(1) {
378 struct vfsmount *parent;
379 struct dentry *dentry;
380 read_lock(&current->fs->lock);
381 if (nd->dentry == current->fs->root &&
382 nd->mnt == current->fs->rootmnt) {
383 read_unlock(&current->fs->lock);
384 break;
386 read_unlock(&current->fs->lock);
387 spin_lock(&dcache_lock);
388 if (nd->dentry != nd->mnt->mnt_root) {
389 dentry = dget(nd->dentry->d_parent);
390 spin_unlock(&dcache_lock);
391 dput(nd->dentry);
392 nd->dentry = dentry;
393 break;
395 parent=nd->mnt->mnt_parent;
396 if (parent == nd->mnt) {
397 spin_unlock(&dcache_lock);
398 break;
400 mntget(parent);
401 dentry=dget(nd->mnt->mnt_mountpoint);
402 spin_unlock(&dcache_lock);
403 dput(nd->dentry);
404 nd->dentry = dentry;
405 mntput(nd->mnt);
406 nd->mnt = parent;
410 * Name resolution.
412 * This is the basic name resolution function, turning a pathname
413 * into the final dentry.
415 * We expect 'base' to be positive and a directory.
417 int path_walk(const char * name, struct nameidata *nd)
419 struct dentry *dentry;
420 struct inode *inode;
421 int err;
422 unsigned int lookup_flags = nd->flags;
424 while (*name=='/')
425 name++;
426 if (!*name)
427 goto return_base;
429 inode = nd->dentry->d_inode;
430 if (current->link_count)
431 lookup_flags = LOOKUP_FOLLOW;
433 /* At this point we know we have a real path component. */
434 for(;;) {
435 unsigned long hash;
436 struct qstr this;
437 unsigned int c;
439 err = permission(inode, MAY_EXEC);
440 dentry = ERR_PTR(err);
441 if (err)
442 break;
444 this.name = name;
445 c = *(const unsigned char *)name;
447 hash = init_name_hash();
448 do {
449 name++;
450 hash = partial_name_hash(c, hash);
451 c = *(const unsigned char *)name;
452 } while (c && (c != '/'));
453 this.len = name - (const char *) this.name;
454 this.hash = end_name_hash(hash);
456 /* remove trailing slashes? */
457 if (!c)
458 goto last_component;
459 while (*++name == '/');
460 if (!*name)
461 goto last_with_slashes;
464 * "." and ".." are special - ".." especially so because it has
465 * to be able to know about the current root directory and
466 * parent relationships.
468 if (this.name[0] == '.') switch (this.len) {
469 default:
470 break;
471 case 2:
472 if (this.name[1] != '.')
473 break;
474 follow_dotdot(nd);
475 inode = nd->dentry->d_inode;
476 /* fallthrough */
477 case 1:
478 continue;
481 * See if the low-level filesystem might want
482 * to use its own hash..
484 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
485 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
486 if (err < 0)
487 break;
489 /* This does the actual lookups.. */
490 dentry = cached_lookup(nd->dentry, &this, LOOKUP_CONTINUE);
491 if (!dentry) {
492 dentry = real_lookup(nd->dentry, &this, LOOKUP_CONTINUE);
493 err = PTR_ERR(dentry);
494 if (IS_ERR(dentry))
495 break;
497 /* Check mountpoints.. */
498 while (d_mountpoint(dentry) && __follow_down(&nd->mnt, &dentry))
501 err = -ENOENT;
502 inode = dentry->d_inode;
503 if (!inode)
504 goto out_dput;
505 err = -ENOTDIR;
506 if (!inode->i_op)
507 goto out_dput;
509 if (inode->i_op->follow_link) {
510 err = do_follow_link(dentry, nd);
511 dput(dentry);
512 if (err)
513 goto return_err;
514 err = -ENOENT;
515 inode = nd->dentry->d_inode;
516 if (!inode)
517 break;
518 err = -ENOTDIR;
519 if (!inode->i_op)
520 break;
521 } else {
522 dput(nd->dentry);
523 nd->dentry = dentry;
525 err = -ENOTDIR;
526 if (!inode->i_op->lookup)
527 break;
528 continue;
529 /* here ends the main loop */
531 last_with_slashes:
532 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
533 last_component:
534 if (lookup_flags & LOOKUP_PARENT)
535 goto lookup_parent;
536 if (this.name[0] == '.') switch (this.len) {
537 default:
538 break;
539 case 2:
540 if (this.name[1] != '.')
541 break;
542 follow_dotdot(nd);
543 inode = nd->dentry->d_inode;
544 /* fallthrough */
545 case 1:
546 goto return_base;
548 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
549 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
550 if (err < 0)
551 break;
553 dentry = cached_lookup(nd->dentry, &this, 0);
554 if (!dentry) {
555 dentry = real_lookup(nd->dentry, &this, 0);
556 err = PTR_ERR(dentry);
557 if (IS_ERR(dentry))
558 break;
560 while (d_mountpoint(dentry) && __follow_down(&nd->mnt, &dentry))
562 inode = dentry->d_inode;
563 if ((lookup_flags & LOOKUP_FOLLOW)
564 && inode && inode->i_op && inode->i_op->follow_link) {
565 err = do_follow_link(dentry, nd);
566 dput(dentry);
567 if (err)
568 goto return_err;
569 inode = nd->dentry->d_inode;
570 } else {
571 dput(nd->dentry);
572 nd->dentry = dentry;
574 err = -ENOENT;
575 if (!inode)
576 goto no_inode;
577 if (lookup_flags & LOOKUP_DIRECTORY) {
578 err = -ENOTDIR;
579 if (!inode->i_op || !inode->i_op->lookup)
580 break;
582 goto return_base;
583 no_inode:
584 err = -ENOENT;
585 if (lookup_flags & (LOOKUP_POSITIVE|LOOKUP_DIRECTORY))
586 break;
587 goto return_base;
588 lookup_parent:
589 nd->last = this;
590 nd->last_type = LAST_NORM;
591 if (this.name[0] != '.')
592 goto return_base;
593 if (this.len == 1)
594 nd->last_type = LAST_DOT;
595 else if (this.len == 2 && this.name[1] == '.')
596 nd->last_type = LAST_DOTDOT;
597 return_base:
598 return 0;
599 out_dput:
600 dput(dentry);
601 break;
603 path_release(nd);
604 return_err:
605 return err;
608 /* SMP-safe */
609 /* returns 1 if everything is done */
610 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
612 if (path_walk(name, nd))
613 return 0;
615 if (!nd->dentry->d_inode) {
616 struct nameidata nd_root;
617 nd_root.last_type = LAST_ROOT;
618 nd_root.flags = nd->flags;
619 read_lock(&current->fs->lock);
620 nd_root.mnt = mntget(current->fs->rootmnt);
621 nd_root.dentry = dget(current->fs->root);
622 read_unlock(&current->fs->lock);
623 if (path_walk(name, &nd_root))
624 return 1;
625 if (nd_root.dentry->d_inode) {
626 path_release(nd);
627 nd->dentry = nd_root.dentry;
628 nd->mnt = nd_root.mnt;
629 nd->last = nd_root.last;
630 return 1;
632 path_release(&nd_root);
634 return 1;
637 void set_fs_altroot(void)
639 char *emul = __emul_prefix();
640 struct nameidata nd;
641 struct vfsmount *mnt = NULL, *oldmnt;
642 struct dentry *dentry = NULL, *olddentry;
643 if (emul) {
644 read_lock(&current->fs->lock);
645 nd.mnt = mntget(current->fs->rootmnt);
646 nd.dentry = dget(current->fs->root);
647 read_unlock(&current->fs->lock);
648 nd.flags = LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_POSITIVE;
649 if (path_walk(emul,&nd) == 0) {
650 mnt = nd.mnt;
651 dentry = nd.dentry;
654 write_lock(&current->fs->lock);
655 oldmnt = current->fs->altrootmnt;
656 olddentry = current->fs->altroot;
657 current->fs->altrootmnt = mnt;
658 current->fs->altroot = dentry;
659 write_unlock(&current->fs->lock);
660 if (olddentry) {
661 dput(olddentry);
662 mntput(oldmnt);
666 /* SMP-safe */
667 static inline int
668 walk_init_root(const char *name, struct nameidata *nd)
670 read_lock(&current->fs->lock);
671 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
672 nd->mnt = mntget(current->fs->altrootmnt);
673 nd->dentry = dget(current->fs->altroot);
674 read_unlock(&current->fs->lock);
675 if (__emul_lookup_dentry(name,nd))
676 return 0;
677 read_lock(&current->fs->lock);
679 nd->mnt = mntget(current->fs->rootmnt);
680 nd->dentry = dget(current->fs->root);
681 read_unlock(&current->fs->lock);
682 return 1;
685 /* SMP-safe */
686 int path_init(const char *name,unsigned int flags,struct nameidata *nd)
688 nd->last_type = LAST_ROOT; /* if there are only slashes... */
689 nd->flags = flags;
690 if (*name=='/')
691 return walk_init_root(name,nd);
692 read_lock(&current->fs->lock);
693 nd->mnt = mntget(current->fs->pwdmnt);
694 nd->dentry = dget(current->fs->pwd);
695 read_unlock(&current->fs->lock);
696 return 1;
700 * Restricted form of lookup. Doesn't follow links, single-component only,
701 * needs parent already locked. Doesn't follow mounts.
702 * SMP-safe.
704 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
706 struct dentry * dentry;
707 struct inode *inode;
708 int err;
710 inode = base->d_inode;
711 err = permission(inode, MAY_EXEC);
712 dentry = ERR_PTR(err);
713 if (err)
714 goto out;
717 * See if the low-level filesystem might want
718 * to use its own hash..
720 if (base->d_op && base->d_op->d_hash) {
721 err = base->d_op->d_hash(base, name);
722 dentry = ERR_PTR(err);
723 if (err < 0)
724 goto out;
727 dentry = cached_lookup(base, name, 0);
728 if (!dentry) {
729 struct dentry *new = d_alloc(base, name);
730 dentry = ERR_PTR(-ENOMEM);
731 if (!new)
732 goto out;
733 lock_kernel();
734 dentry = inode->i_op->lookup(inode, new);
735 unlock_kernel();
736 if (!dentry)
737 dentry = new;
738 else
739 dput(new);
741 out:
742 return dentry;
745 /* SMP-safe */
746 struct dentry * lookup_one(const char * name, struct dentry * base)
748 unsigned long hash;
749 struct qstr this;
750 unsigned int c;
752 this.name = name;
753 c = *(const unsigned char *)name;
754 if (!c)
755 goto access;
757 hash = init_name_hash();
758 do {
759 name++;
760 if (c == '/')
761 goto access;
762 hash = partial_name_hash(c, hash);
763 c = *(const unsigned char *)name;
764 } while (c);
765 this.len = name - (const char *) this.name;
766 this.hash = end_name_hash(hash);
768 return lookup_hash(&this, base);
769 access:
770 return ERR_PTR(-EACCES);
774 * namei()
776 * is used by most simple commands to get the inode of a specified name.
777 * Open, link etc use their own routines, but this is enough for things
778 * like 'chmod' etc.
780 * namei exists in two versions: namei/lnamei. The only difference is
781 * that namei follows links, while lnamei does not.
782 * SMP-safe
784 int __user_walk(const char *name, unsigned flags, struct nameidata *nd)
786 char *tmp;
787 int err;
789 tmp = getname(name);
790 err = PTR_ERR(tmp);
791 if (!IS_ERR(tmp)) {
792 err = 0;
793 if (path_init(tmp, flags, nd))
794 err = path_walk(tmp, nd);
795 putname(tmp);
797 return err;
801 * It's inline, so penalty for filesystems that don't use sticky bit is
802 * minimal.
804 static inline int check_sticky(struct inode *dir, struct inode *inode)
806 if (!(dir->i_mode & S_ISVTX))
807 return 0;
808 if (inode->i_uid == current->fsuid)
809 return 0;
810 if (dir->i_uid == current->fsuid)
811 return 0;
812 return !capable(CAP_FOWNER);
816 * Check whether we can remove a link victim from directory dir, check
817 * whether the type of victim is right.
818 * 1. We can't do it if dir is read-only (done in permission())
819 * 2. We should have write and exec permissions on dir
820 * 3. We can't remove anything from append-only dir
821 * 4. We can't do anything with immutable dir (done in permission())
822 * 5. If the sticky bit on dir is set we should either
823 * a. be owner of dir, or
824 * b. be owner of victim, or
825 * c. have CAP_FOWNER capability
826 * 6. If the victim is append-only or immutable we can't do antyhing with
827 * links pointing to it.
828 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
829 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
830 * 9. We can't remove a root or mountpoint.
832 static inline int may_delete(struct inode *dir,struct dentry *victim, int isdir)
834 int error;
835 if (!victim->d_inode || victim->d_parent->d_inode != dir)
836 return -ENOENT;
837 error = permission(dir,MAY_WRITE | MAY_EXEC);
838 if (error)
839 return error;
840 if (IS_APPEND(dir))
841 return -EPERM;
842 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
843 IS_IMMUTABLE(victim->d_inode))
844 return -EPERM;
845 if (isdir) {
846 if (!S_ISDIR(victim->d_inode->i_mode))
847 return -ENOTDIR;
848 if (IS_ROOT(victim))
849 return -EBUSY;
850 } else if (S_ISDIR(victim->d_inode->i_mode))
851 return -EISDIR;
852 return 0;
855 /* Check whether we can create an object with dentry child in directory
856 * dir.
857 * 1. We can't do it if child already exists (open has special treatment for
858 * this case, but since we are inlined it's OK)
859 * 2. We can't do it if dir is read-only (done in permission())
860 * 3. We should have write and exec permissions on dir
861 * 4. We can't do it if dir is immutable (done in permission())
863 static inline int may_create(struct inode *dir, struct dentry *child) {
864 if (child->d_inode)
865 return -EEXIST;
866 if (IS_DEADDIR(dir))
867 return -ENOENT;
868 return permission(dir,MAY_WRITE | MAY_EXEC);
872 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
873 * reasons.
875 * O_DIRECTORY translates into forcing a directory lookup.
877 static inline int lookup_flags(unsigned int f)
879 unsigned long retval = LOOKUP_FOLLOW;
881 if (f & O_NOFOLLOW)
882 retval &= ~LOOKUP_FOLLOW;
884 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
885 retval &= ~LOOKUP_FOLLOW;
887 if (f & O_DIRECTORY)
888 retval |= LOOKUP_DIRECTORY;
890 return retval;
893 int vfs_create(struct inode *dir, struct dentry *dentry, int mode)
895 int error;
897 mode &= S_IALLUGO & ~current->fs->umask;
898 mode |= S_IFREG;
900 down(&dir->i_zombie);
901 error = may_create(dir, dentry);
902 if (error)
903 goto exit_lock;
905 error = -EACCES; /* shouldn't it be ENOSYS? */
906 if (!dir->i_op || !dir->i_op->create)
907 goto exit_lock;
909 DQUOT_INIT(dir);
910 lock_kernel();
911 error = dir->i_op->create(dir, dentry, mode);
912 unlock_kernel();
913 exit_lock:
914 up(&dir->i_zombie);
915 return error;
919 * open_namei()
921 * namei for open - this is in fact almost the whole open-routine.
923 * Note that the low bits of "flag" aren't the same as in the open
924 * system call - they are 00 - no permissions needed
925 * 01 - read permission needed
926 * 10 - write permission needed
927 * 11 - read/write permissions needed
928 * which is a lot more logical, and also allows the "no perm" needed
929 * for symlinks (where the permissions are checked later).
930 * SMP-safe
932 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
934 int acc_mode, error = 0;
935 struct inode *inode;
936 struct dentry *dentry;
937 struct dentry *dir;
938 int count = 0;
940 acc_mode = ACC_MODE(flag);
943 * The simplest case - just a plain lookup.
945 if (!(flag & O_CREAT)) {
946 if (path_init(pathname, lookup_flags(flag), nd))
947 error = path_walk(pathname, nd);
948 if (error)
949 return error;
950 dentry = nd->dentry;
951 goto ok;
955 * Create - we need to know the parent.
957 if (path_init(pathname, LOOKUP_PARENT, nd))
958 error = path_walk(pathname, nd);
959 if (error)
960 return error;
963 * We have the parent and last component. First of all, check
964 * that we are not asked to creat(2) an obvious directory - that
965 * will not do.
967 error = -EISDIR;
968 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
969 goto exit;
971 dir = nd->dentry;
972 down(&dir->d_inode->i_sem);
973 dentry = lookup_hash(&nd->last, nd->dentry);
975 do_last:
976 error = PTR_ERR(dentry);
977 if (IS_ERR(dentry)) {
978 up(&dir->d_inode->i_sem);
979 goto exit;
982 /* Negative dentry, just create the file */
983 if (!dentry->d_inode) {
984 error = vfs_create(dir->d_inode, dentry, mode);
985 up(&dir->d_inode->i_sem);
986 dput(nd->dentry);
987 nd->dentry = dentry;
988 if (error)
989 goto exit;
990 /* Don't check for write permission, don't truncate */
991 acc_mode = 0;
992 flag &= ~O_TRUNC;
993 goto ok;
997 * It already exists.
999 up(&dir->d_inode->i_sem);
1001 error = -EEXIST;
1002 if (flag & O_EXCL)
1003 goto exit_dput;
1005 if (d_mountpoint(dentry)) {
1006 error = -ELOOP;
1007 if (flag & O_NOFOLLOW)
1008 goto exit_dput;
1009 do __follow_down(&nd->mnt,&dentry); while(d_mountpoint(dentry));
1011 error = -ENOENT;
1012 if (!dentry->d_inode)
1013 goto exit_dput;
1014 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1015 goto do_link;
1017 dput(nd->dentry);
1018 nd->dentry = dentry;
1019 error = -EISDIR;
1020 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1021 goto exit;
1023 error = -ENOENT;
1024 inode = dentry->d_inode;
1025 if (!inode)
1026 goto exit;
1028 error = -ELOOP;
1029 if (S_ISLNK(inode->i_mode))
1030 goto exit;
1032 error = -EISDIR;
1033 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1034 goto exit;
1036 error = permission(inode,acc_mode);
1037 if (error)
1038 goto exit;
1041 * FIFO's, sockets and device files are special: they don't
1042 * actually live on the filesystem itself, and as such you
1043 * can write to them even if the filesystem is read-only.
1045 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1046 flag &= ~O_TRUNC;
1047 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1048 error = -EACCES;
1049 if (IS_NODEV(inode))
1050 goto exit;
1052 flag &= ~O_TRUNC;
1053 } else {
1054 error = -EROFS;
1055 if (IS_RDONLY(inode) && (flag & 2))
1056 goto exit;
1059 * An append-only file must be opened in append mode for writing.
1061 error = -EPERM;
1062 if (IS_APPEND(inode)) {
1063 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1064 goto exit;
1065 if (flag & O_TRUNC)
1066 goto exit;
1069 if (flag & O_TRUNC) {
1070 error = get_write_access(inode);
1071 if (error)
1072 goto exit;
1075 * Refuse to truncate files with mandatory locks held on them.
1077 error = locks_verify_locked(inode);
1078 if (!error) {
1079 DQUOT_INIT(inode);
1081 error = do_truncate(dentry, 0);
1083 put_write_access(inode);
1084 if (error)
1085 goto exit;
1086 } else
1087 if (flag & FMODE_WRITE)
1088 DQUOT_INIT(inode);
1090 return 0;
1092 exit_dput:
1093 dput(dentry);
1094 exit:
1095 path_release(nd);
1096 return error;
1098 do_link:
1099 error = -ELOOP;
1100 if (flag & O_NOFOLLOW)
1101 goto exit_dput;
1103 * This is subtle. Instead of calling do_follow_link() we do the
1104 * thing by hands. The reason is that this way we have zero link_count
1105 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1106 * After that we have the parent and last component, i.e.
1107 * we are in the same situation as after the first path_walk().
1108 * Well, almost - if the last component is normal we get its copy
1109 * stored in nd->last.name and we will have to putname() it when we
1110 * are done. Procfs-like symlinks just set LAST_BIND.
1112 UPDATE_ATIME(dentry->d_inode);
1113 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1114 dput(dentry);
1115 if (error)
1116 return error;
1117 if (nd->last_type == LAST_BIND) {
1118 dentry = nd->dentry;
1119 goto ok;
1121 error = -EISDIR;
1122 if (nd->last_type != LAST_NORM)
1123 goto exit;
1124 if (nd->last.name[nd->last.len]) {
1125 putname(nd->last.name);
1126 goto exit;
1128 if (count++==32) {
1129 dentry = nd->dentry;
1130 putname(nd->last.name);
1131 goto ok;
1133 dir = nd->dentry;
1134 down(&dir->d_inode->i_sem);
1135 dentry = lookup_hash(&nd->last, nd->dentry);
1136 putname(nd->last.name);
1137 goto do_last;
1140 /* SMP-safe */
1141 static struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1143 struct dentry *dentry;
1145 down(&nd->dentry->d_inode->i_sem);
1146 dentry = ERR_PTR(-EEXIST);
1147 if (nd->last_type != LAST_NORM)
1148 goto fail;
1149 dentry = lookup_hash(&nd->last, nd->dentry);
1150 if (IS_ERR(dentry))
1151 goto fail;
1152 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1153 goto enoent;
1154 return dentry;
1155 enoent:
1156 dput(dentry);
1157 dentry = ERR_PTR(-ENOENT);
1158 fail:
1159 return dentry;
1162 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1164 int error = -EPERM;
1166 mode &= ~current->fs->umask;
1168 down(&dir->i_zombie);
1169 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1170 goto exit_lock;
1172 error = may_create(dir, dentry);
1173 if (error)
1174 goto exit_lock;
1176 error = -EPERM;
1177 if (!dir->i_op || !dir->i_op->mknod)
1178 goto exit_lock;
1180 DQUOT_INIT(dir);
1181 lock_kernel();
1182 error = dir->i_op->mknod(dir, dentry, mode, dev);
1183 unlock_kernel();
1184 exit_lock:
1185 up(&dir->i_zombie);
1186 return error;
1189 asmlinkage long sys_mknod(const char * filename, int mode, dev_t dev)
1191 int error = 0;
1192 char * tmp;
1193 struct dentry * dentry;
1194 struct nameidata nd;
1196 if (S_ISDIR(mode))
1197 return -EPERM;
1198 tmp = getname(filename);
1199 if (IS_ERR(tmp))
1200 return PTR_ERR(tmp);
1202 if (path_init(tmp, LOOKUP_PARENT, &nd))
1203 error = path_walk(tmp, &nd);
1204 if (error)
1205 goto out;
1206 dentry = lookup_create(&nd, 0);
1207 error = PTR_ERR(dentry);
1208 if (!IS_ERR(dentry)) {
1209 switch (mode & S_IFMT) {
1210 case 0: case S_IFREG:
1211 error = vfs_create(nd.dentry->d_inode,dentry,mode);
1212 break;
1213 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
1214 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,dev);
1215 break;
1216 case S_IFDIR:
1217 error = -EPERM;
1218 break;
1219 default:
1220 error = -EINVAL;
1222 dput(dentry);
1224 up(&nd.dentry->d_inode->i_sem);
1225 path_release(&nd);
1226 out:
1227 putname(tmp);
1229 return error;
1232 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1234 int error;
1236 down(&dir->i_zombie);
1237 error = may_create(dir, dentry);
1238 if (error)
1239 goto exit_lock;
1241 error = -EPERM;
1242 if (!dir->i_op || !dir->i_op->mkdir)
1243 goto exit_lock;
1245 DQUOT_INIT(dir);
1246 mode &= (S_IRWXUGO|S_ISVTX) & ~current->fs->umask;
1247 lock_kernel();
1248 error = dir->i_op->mkdir(dir, dentry, mode);
1249 unlock_kernel();
1251 exit_lock:
1252 up(&dir->i_zombie);
1253 return error;
1256 asmlinkage long sys_mkdir(const char * pathname, int mode)
1258 int error = 0;
1259 char * tmp;
1261 tmp = getname(pathname);
1262 error = PTR_ERR(tmp);
1263 if (!IS_ERR(tmp)) {
1264 struct dentry *dentry;
1265 struct nameidata nd;
1267 if (path_init(tmp, LOOKUP_PARENT, &nd))
1268 error = path_walk(tmp, &nd);
1269 if (error)
1270 goto out;
1271 dentry = lookup_create(&nd, 1);
1272 error = PTR_ERR(dentry);
1273 if (!IS_ERR(dentry)) {
1274 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1275 dput(dentry);
1277 up(&nd.dentry->d_inode->i_sem);
1278 path_release(&nd);
1279 out:
1280 putname(tmp);
1283 return error;
1287 * We try to drop the dentry early: we should have
1288 * a usage count of 2 if we're the only user of this
1289 * dentry, and if that is true (possibly after pruning
1290 * the dcache), then we drop the dentry now.
1292 * A low-level filesystem can, if it choses, legally
1293 * do a
1295 * if (!d_unhashed(dentry))
1296 * return -EBUSY;
1298 * if it cannot handle the case of removing a directory
1299 * that is still in use by something else..
1301 static void d_unhash(struct dentry *dentry)
1303 dget(dentry);
1304 switch (atomic_read(&dentry->d_count)) {
1305 default:
1306 shrink_dcache_parent(dentry);
1307 if (atomic_read(&dentry->d_count) != 2)
1308 break;
1309 case 2:
1310 d_drop(dentry);
1314 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1316 int error;
1318 error = may_delete(dir, dentry, 1);
1319 if (error)
1320 return error;
1322 if (!dir->i_op || !dir->i_op->rmdir)
1323 return -EPERM;
1325 DQUOT_INIT(dir);
1327 double_down(&dir->i_zombie, &dentry->d_inode->i_zombie);
1328 d_unhash(dentry);
1329 if (IS_DEADDIR(dir))
1330 error = -ENOENT;
1331 else if (d_mountpoint(dentry))
1332 error = -EBUSY;
1333 else {
1334 lock_kernel();
1335 error = dir->i_op->rmdir(dir, dentry);
1336 unlock_kernel();
1337 if (!error)
1338 dentry->d_inode->i_flags |= S_DEAD;
1340 double_up(&dir->i_zombie, &dentry->d_inode->i_zombie);
1341 if (!error)
1342 d_delete(dentry);
1343 dput(dentry);
1345 return error;
1348 asmlinkage long sys_rmdir(const char * pathname)
1350 int error = 0;
1351 char * name;
1352 struct dentry *dentry;
1353 struct nameidata nd;
1355 name = getname(pathname);
1356 if(IS_ERR(name))
1357 return PTR_ERR(name);
1359 if (path_init(name, LOOKUP_PARENT, &nd))
1360 error = path_walk(name, &nd);
1361 if (error)
1362 goto exit;
1364 switch(nd.last_type) {
1365 case LAST_DOTDOT:
1366 error = -ENOTEMPTY;
1367 goto exit1;
1368 case LAST_ROOT: case LAST_DOT:
1369 error = -EBUSY;
1370 goto exit1;
1372 down(&nd.dentry->d_inode->i_sem);
1373 dentry = lookup_hash(&nd.last, nd.dentry);
1374 error = PTR_ERR(dentry);
1375 if (!IS_ERR(dentry)) {
1376 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1377 dput(dentry);
1379 up(&nd.dentry->d_inode->i_sem);
1380 exit1:
1381 path_release(&nd);
1382 exit:
1383 putname(name);
1384 return error;
1387 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1389 int error;
1391 down(&dir->i_zombie);
1392 error = may_delete(dir, dentry, 0);
1393 if (!error) {
1394 error = -EPERM;
1395 if (dir->i_op && dir->i_op->unlink) {
1396 DQUOT_INIT(dir);
1397 if (d_mountpoint(dentry))
1398 error = -EBUSY;
1399 else {
1400 lock_kernel();
1401 error = dir->i_op->unlink(dir, dentry);
1402 unlock_kernel();
1403 if (!error)
1404 d_delete(dentry);
1408 up(&dir->i_zombie);
1409 return error;
1412 asmlinkage long sys_unlink(const char * pathname)
1414 int error = 0;
1415 char * name;
1416 struct dentry *dentry;
1417 struct nameidata nd;
1419 name = getname(pathname);
1420 if(IS_ERR(name))
1421 return PTR_ERR(name);
1423 if (path_init(name, LOOKUP_PARENT, &nd))
1424 error = path_walk(name, &nd);
1425 if (error)
1426 goto exit;
1427 error = -EISDIR;
1428 if (nd.last_type != LAST_NORM)
1429 goto exit1;
1430 down(&nd.dentry->d_inode->i_sem);
1431 dentry = lookup_hash(&nd.last, nd.dentry);
1432 error = PTR_ERR(dentry);
1433 if (!IS_ERR(dentry)) {
1434 /* Why not before? Because we want correct error value */
1435 if (nd.last.name[nd.last.len])
1436 goto slashes;
1437 error = vfs_unlink(nd.dentry->d_inode, dentry);
1438 exit2:
1439 dput(dentry);
1441 up(&nd.dentry->d_inode->i_sem);
1442 exit1:
1443 path_release(&nd);
1444 exit:
1445 putname(name);
1447 return error;
1449 slashes:
1450 error = !dentry->d_inode ? -ENOENT :
1451 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1452 goto exit2;
1455 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1457 int error;
1459 down(&dir->i_zombie);
1460 error = may_create(dir, dentry);
1461 if (error)
1462 goto exit_lock;
1464 error = -EPERM;
1465 if (!dir->i_op || !dir->i_op->symlink)
1466 goto exit_lock;
1468 DQUOT_INIT(dir);
1469 lock_kernel();
1470 error = dir->i_op->symlink(dir, dentry, oldname);
1471 unlock_kernel();
1473 exit_lock:
1474 up(&dir->i_zombie);
1475 return error;
1478 asmlinkage long sys_symlink(const char * oldname, const char * newname)
1480 int error = 0;
1481 char * from;
1482 char * to;
1484 from = getname(oldname);
1485 if(IS_ERR(from))
1486 return PTR_ERR(from);
1487 to = getname(newname);
1488 error = PTR_ERR(to);
1489 if (!IS_ERR(to)) {
1490 struct dentry *dentry;
1491 struct nameidata nd;
1493 if (path_init(to, LOOKUP_PARENT, &nd))
1494 error = path_walk(to, &nd);
1495 if (error)
1496 goto out;
1497 dentry = lookup_create(&nd, 0);
1498 error = PTR_ERR(dentry);
1499 if (!IS_ERR(dentry)) {
1500 error = vfs_symlink(nd.dentry->d_inode, dentry, from);
1501 dput(dentry);
1503 up(&nd.dentry->d_inode->i_sem);
1504 path_release(&nd);
1505 out:
1506 putname(to);
1508 putname(from);
1509 return error;
1512 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1514 struct inode *inode;
1515 int error;
1517 down(&dir->i_zombie);
1518 error = -ENOENT;
1519 inode = old_dentry->d_inode;
1520 if (!inode)
1521 goto exit_lock;
1523 error = may_create(dir, new_dentry);
1524 if (error)
1525 goto exit_lock;
1527 error = -EXDEV;
1528 if (dir->i_dev != inode->i_dev)
1529 goto exit_lock;
1532 * A link to an append-only or immutable file cannot be created.
1534 error = -EPERM;
1535 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1536 goto exit_lock;
1537 if (!dir->i_op || !dir->i_op->link)
1538 goto exit_lock;
1540 DQUOT_INIT(dir);
1541 lock_kernel();
1542 error = dir->i_op->link(old_dentry, dir, new_dentry);
1543 unlock_kernel();
1545 exit_lock:
1546 up(&dir->i_zombie);
1547 return error;
1551 * Hardlinks are often used in delicate situations. We avoid
1552 * security-related surprises by not following symlinks on the
1553 * newname. --KAB
1555 * We don't follow them on the oldname either to be compatible
1556 * with linux 2.0, and to avoid hard-linking to directories
1557 * and other special files. --ADM
1559 asmlinkage long sys_link(const char * oldname, const char * newname)
1561 int error;
1562 char * from;
1563 char * to;
1565 from = getname(oldname);
1566 if(IS_ERR(from))
1567 return PTR_ERR(from);
1568 to = getname(newname);
1569 error = PTR_ERR(to);
1570 if (!IS_ERR(to)) {
1571 struct dentry *new_dentry;
1572 struct nameidata nd, old_nd;
1574 error = 0;
1575 if (path_init(from, LOOKUP_POSITIVE, &old_nd))
1576 error = path_walk(from, &old_nd);
1577 if (error)
1578 goto exit;
1579 if (path_init(to, LOOKUP_PARENT, &nd))
1580 error = path_walk(to, &nd);
1581 if (error)
1582 goto out;
1583 error = -EXDEV;
1584 if (old_nd.mnt != nd.mnt)
1585 goto out;
1586 new_dentry = lookup_create(&nd, 0);
1587 error = PTR_ERR(new_dentry);
1588 if (!IS_ERR(new_dentry)) {
1589 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
1590 dput(new_dentry);
1592 up(&nd.dentry->d_inode->i_sem);
1593 path_release(&nd);
1594 out:
1595 path_release(&old_nd);
1596 exit:
1597 putname(to);
1599 putname(from);
1601 return error;
1605 * The worst of all namespace operations - renaming directory. "Perverted"
1606 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1607 * Problems:
1608 * a) we can get into loop creation. Check is done in is_subdir().
1609 * b) race potential - two innocent renames can create a loop together.
1610 * That's where 4.4 screws up. Current fix: serialization on
1611 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1612 * story.
1613 * c) we have to lock _three_ objects - parents and victim (if it exists).
1614 * And that - after we got ->i_sem on parents (until then we don't know
1615 * whether the target exists at all, let alone whether it is a directory
1616 * or not). Solution: ->i_zombie. Taken only after ->i_sem. Always taken
1617 * on link creation/removal of any kind. And taken (without ->i_sem) on
1618 * directory that will be removed (both in rmdir() and here).
1619 * d) some filesystems don't support opened-but-unlinked directories,
1620 * either because of layout or because they are not ready to deal with
1621 * all cases correctly. The latter will be fixed (taking this sort of
1622 * stuff into VFS), but the former is not going away. Solution: the same
1623 * trick as in rmdir().
1624 * e) conversion from fhandle to dentry may come in the wrong moment - when
1625 * we are removing the target. Solution: we will have to grab ->i_zombie
1626 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1627 * ->i_sem on parents, which works but leads to some truely excessive
1628 * locking].
1630 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
1631 struct inode *new_dir, struct dentry *new_dentry)
1633 int error;
1634 struct inode *target;
1636 if (old_dentry->d_inode == new_dentry->d_inode)
1637 return 0;
1639 error = may_delete(old_dir, old_dentry, 1);
1640 if (error)
1641 return error;
1643 if (new_dir->i_dev != old_dir->i_dev)
1644 return -EXDEV;
1646 if (!new_dentry->d_inode)
1647 error = may_create(new_dir, new_dentry);
1648 else
1649 error = may_delete(new_dir, new_dentry, 1);
1650 if (error)
1651 return error;
1653 if (!old_dir->i_op || !old_dir->i_op->rename)
1654 return -EPERM;
1657 * If we are going to change the parent - check write permissions,
1658 * we'll need to flip '..'.
1660 if (new_dir != old_dir) {
1661 error = permission(old_dentry->d_inode, MAY_WRITE);
1663 if (error)
1664 return error;
1666 DQUOT_INIT(old_dir);
1667 DQUOT_INIT(new_dir);
1668 down(&old_dir->i_sb->s_vfs_rename_sem);
1669 error = -EINVAL;
1670 if (is_subdir(new_dentry, old_dentry))
1671 goto out_unlock;
1672 target = new_dentry->d_inode;
1673 if (target) { /* Hastur! Hastur! Hastur! */
1674 triple_down(&old_dir->i_zombie,
1675 &new_dir->i_zombie,
1676 &target->i_zombie);
1677 d_unhash(new_dentry);
1678 } else
1679 double_down(&old_dir->i_zombie,
1680 &new_dir->i_zombie);
1681 if (IS_DEADDIR(old_dir)||IS_DEADDIR(new_dir))
1682 error = -ENOENT;
1683 else if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1684 error = -EBUSY;
1685 else
1686 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1687 if (target) {
1688 if (!error)
1689 target->i_flags |= S_DEAD;
1690 triple_up(&old_dir->i_zombie,
1691 &new_dir->i_zombie,
1692 &target->i_zombie);
1693 d_rehash(new_dentry);
1694 dput(new_dentry);
1695 } else
1696 double_up(&old_dir->i_zombie,
1697 &new_dir->i_zombie);
1699 if (!error)
1700 d_move(old_dentry,new_dentry);
1701 out_unlock:
1702 up(&old_dir->i_sb->s_vfs_rename_sem);
1703 return error;
1706 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
1707 struct inode *new_dir, struct dentry *new_dentry)
1709 int error;
1711 if (old_dentry->d_inode == new_dentry->d_inode)
1712 return 0;
1714 error = may_delete(old_dir, old_dentry, 0);
1715 if (error)
1716 return error;
1718 if (new_dir->i_dev != old_dir->i_dev)
1719 return -EXDEV;
1721 if (!new_dentry->d_inode)
1722 error = may_create(new_dir, new_dentry);
1723 else
1724 error = may_delete(new_dir, new_dentry, 0);
1725 if (error)
1726 return error;
1728 if (!old_dir->i_op || !old_dir->i_op->rename)
1729 return -EPERM;
1731 DQUOT_INIT(old_dir);
1732 DQUOT_INIT(new_dir);
1733 double_down(&old_dir->i_zombie, &new_dir->i_zombie);
1734 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1735 error = -EBUSY;
1736 else
1737 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1738 double_up(&old_dir->i_zombie, &new_dir->i_zombie);
1739 if (error)
1740 return error;
1741 /* The following d_move() should become unconditional */
1742 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME)) {
1743 d_move(old_dentry, new_dentry);
1745 return 0;
1748 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1749 struct inode *new_dir, struct dentry *new_dentry)
1751 if (S_ISDIR(old_dentry->d_inode->i_mode))
1752 return vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
1753 else
1754 return vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
1757 static inline int do_rename(const char * oldname, const char * newname)
1759 int error = 0;
1760 struct dentry * old_dir, * new_dir;
1761 struct dentry * old_dentry, *new_dentry;
1762 struct nameidata oldnd, newnd;
1764 if (path_init(oldname, LOOKUP_PARENT, &oldnd))
1765 error = path_walk(oldname, &oldnd);
1767 if (error)
1768 goto exit;
1770 if (path_init(newname, LOOKUP_PARENT, &newnd))
1771 error = path_walk(newname, &newnd);
1772 if (error)
1773 goto exit1;
1775 error = -EXDEV;
1776 if (oldnd.mnt != newnd.mnt)
1777 goto exit2;
1779 old_dir = oldnd.dentry;
1780 error = -EBUSY;
1781 if (oldnd.last_type != LAST_NORM)
1782 goto exit2;
1784 new_dir = newnd.dentry;
1785 if (newnd.last_type != LAST_NORM)
1786 goto exit2;
1788 double_lock(new_dir, old_dir);
1790 old_dentry = lookup_hash(&oldnd.last, old_dir);
1791 error = PTR_ERR(old_dentry);
1792 if (IS_ERR(old_dentry))
1793 goto exit3;
1794 /* source must exist */
1795 error = -ENOENT;
1796 if (!old_dentry->d_inode)
1797 goto exit4;
1798 /* unless the source is a directory trailing slashes give -ENOTDIR */
1799 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
1800 error = -ENOTDIR;
1801 if (oldnd.last.name[oldnd.last.len])
1802 goto exit4;
1803 if (newnd.last.name[newnd.last.len])
1804 goto exit4;
1806 new_dentry = lookup_hash(&newnd.last, new_dir);
1807 error = PTR_ERR(new_dentry);
1808 if (IS_ERR(new_dentry))
1809 goto exit4;
1811 lock_kernel();
1812 error = vfs_rename(old_dir->d_inode, old_dentry,
1813 new_dir->d_inode, new_dentry);
1814 unlock_kernel();
1816 dput(new_dentry);
1817 exit4:
1818 dput(old_dentry);
1819 exit3:
1820 double_up(&new_dir->d_inode->i_sem, &old_dir->d_inode->i_sem);
1821 exit2:
1822 path_release(&newnd);
1823 exit1:
1824 path_release(&oldnd);
1825 exit:
1826 return error;
1829 asmlinkage long sys_rename(const char * oldname, const char * newname)
1831 int error;
1832 char * from;
1833 char * to;
1835 from = getname(oldname);
1836 if(IS_ERR(from))
1837 return PTR_ERR(from);
1838 to = getname(newname);
1839 error = PTR_ERR(to);
1840 if (!IS_ERR(to)) {
1841 error = do_rename(from,to);
1842 putname(to);
1844 putname(from);
1845 return error;
1848 int vfs_readlink(struct dentry *dentry, char *buffer, int buflen, const char *link)
1850 int len;
1852 len = PTR_ERR(link);
1853 if (IS_ERR(link))
1854 goto out;
1856 len = strlen(link);
1857 if (len > (unsigned) buflen)
1858 len = buflen;
1859 if (copy_to_user(buffer, link, len))
1860 len = -EFAULT;
1861 out:
1862 return len;
1865 static inline int
1866 __vfs_follow_link(struct nameidata *nd, const char *link)
1868 int res = 0;
1869 char *name;
1870 if (IS_ERR(link))
1871 goto fail;
1873 if (*link == '/') {
1874 path_release(nd);
1875 if (!walk_init_root(link, nd))
1876 /* weird __emul_prefix() stuff did it */
1877 goto out;
1879 res = path_walk(link, nd);
1880 out:
1881 if (current->link_count || res || nd->last_type!=LAST_NORM)
1882 return res;
1884 * If it is an iterative symlinks resolution in open_namei() we
1885 * have to copy the last component. And all that crap because of
1886 * bloody create() on broken symlinks. Furrfu...
1888 name = __getname();
1889 if (IS_ERR(name))
1890 goto fail_name;
1891 strcpy(name, nd->last.name);
1892 nd->last.name = name;
1893 return 0;
1894 fail_name:
1895 link = name;
1896 fail:
1897 path_release(nd);
1898 return PTR_ERR(link);
1901 int vfs_follow_link(struct nameidata *nd, const char *link)
1903 return __vfs_follow_link(nd, link);
1906 /* get the link contents into pagecache */
1907 static char *page_getlink(struct dentry * dentry, struct page **ppage)
1909 struct page * page;
1910 struct address_space *mapping = dentry->d_inode->i_mapping;
1911 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
1912 NULL);
1913 if (IS_ERR(page))
1914 goto sync_fail;
1915 wait_on_page(page);
1916 if (!Page_Uptodate(page))
1917 goto async_fail;
1918 *ppage = page;
1919 return (char*) kmap(page);
1921 async_fail:
1922 page_cache_release(page);
1923 return ERR_PTR(-EIO);
1925 sync_fail:
1926 return (char*)page;
1929 int page_readlink(struct dentry *dentry, char *buffer, int buflen)
1931 struct page *page = NULL;
1932 char *s = page_getlink(dentry, &page);
1933 int res = vfs_readlink(dentry,buffer,buflen,s);
1934 if (page) {
1935 kunmap(page);
1936 page_cache_release(page);
1938 return res;
1941 int page_follow_link(struct dentry *dentry, struct nameidata *nd)
1943 struct page *page = NULL;
1944 char *s = page_getlink(dentry, &page);
1945 int res = __vfs_follow_link(nd, s);
1946 if (page) {
1947 kunmap(page);
1948 page_cache_release(page);
1950 return res;
1953 struct inode_operations page_symlink_inode_operations = {
1954 readlink: page_readlink,
1955 follow_link: page_follow_link,