Ignore more junk files.
[linux-2.6/linux-mips.git] / fs / namei.c
blobfcda2fd6162a19da009db9bbab42adcc85df8cc1
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/mm.h>
18 #include <linux/proc_fs.h>
19 #include <linux/smp_lock.h>
20 #include <linux/quotaops.h>
21 #include <linux/pagemap.h>
22 #include <linux/dcache.h>
24 #include <asm/uaccess.h>
25 #include <asm/unaligned.h>
26 #include <asm/semaphore.h>
27 #include <asm/page.h>
28 #include <asm/pgtable.h>
30 #include <asm/namei.h>
32 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
34 /* [Feb-1997 T. Schoebel-Theuer]
35 * Fundamental changes in the pathname lookup mechanisms (namei)
36 * were necessary because of omirr. The reason is that omirr needs
37 * to know the _real_ pathname, not the user-supplied one, in case
38 * of symlinks (and also when transname replacements occur).
40 * The new code replaces the old recursive symlink resolution with
41 * an iterative one (in case of non-nested symlink chains). It does
42 * this with calls to <fs>_follow_link().
43 * As a side effect, dir_namei(), _namei() and follow_link() are now
44 * replaced with a single function lookup_dentry() that can handle all
45 * the special cases of the former code.
47 * With the new dcache, the pathname is stored at each inode, at least as
48 * long as the refcount of the inode is positive. As a side effect, the
49 * size of the dcache depends on the inode cache and thus is dynamic.
51 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
52 * resolution to correspond with current state of the code.
54 * Note that the symlink resolution is not *completely* iterative.
55 * There is still a significant amount of tail- and mid- recursion in
56 * the algorithm. Also, note that <fs>_readlink() is not used in
57 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
58 * may return different results than <fs>_follow_link(). Many virtual
59 * filesystems (including /proc) exhibit this behavior.
62 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
63 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
64 * and the name already exists in form of a symlink, try to create the new
65 * name indicated by the symlink. The old code always complained that the
66 * name already exists, due to not following the symlink even if its target
67 * is nonexistent. The new semantics affects also mknod() and link() when
68 * the name is a symlink pointing to a non-existant name.
70 * I don't know which semantics is the right one, since I have no access
71 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
72 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
73 * "old" one. Personally, I think the new semantics is much more logical.
74 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
75 * file does succeed in both HP-UX and SunOs, but not in Solaris
76 * and in the old Linux semantics.
79 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
80 * semantics. See the comments in "open_namei" and "do_link" below.
82 * [10-Sep-98 Alan Modra] Another symlink change.
85 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
86 * inside the path - always follow.
87 * in the last component in creation/removal/renaming - never follow.
88 * if LOOKUP_FOLLOW passed - follow.
89 * if the pathname has trailing slashes - follow.
90 * otherwise - don't follow.
91 * (applied in that order).
93 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
94 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
95 * During the 2.4 we need to fix the userland stuff depending on it -
96 * hopefully we will be able to get rid of that wart in 2.5. So far only
97 * XEmacs seems to be relying on it...
100 /* In order to reduce some races, while at the same time doing additional
101 * checking and hopefully speeding things up, we copy filenames to the
102 * kernel data space before using them..
104 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
106 static inline int do_getname(const char *filename, char *page)
108 int retval;
109 unsigned long len = PAGE_SIZE;
111 if ((unsigned long) filename >= TASK_SIZE) {
112 if (!segment_eq(get_fs(), KERNEL_DS))
113 return -EFAULT;
114 } else if (TASK_SIZE - (unsigned long) filename < PAGE_SIZE)
115 len = TASK_SIZE - (unsigned long) filename;
117 retval = strncpy_from_user((char *)page, filename, len);
118 if (retval > 0) {
119 if (retval < len)
120 return 0;
121 return -ENAMETOOLONG;
122 } else if (!retval)
123 retval = -ENOENT;
124 return retval;
127 char * getname(const char * filename)
129 char *tmp, *result;
131 result = ERR_PTR(-ENOMEM);
132 tmp = __getname();
133 if (tmp) {
134 int retval = do_getname(filename, tmp);
136 result = tmp;
137 if (retval < 0) {
138 putname(tmp);
139 result = ERR_PTR(retval);
142 return result;
146 * permission()
148 * is used to check for read/write/execute permissions on a file.
149 * We use "fsuid" for this, letting us set arbitrary permissions
150 * for filesystem access without changing the "normal" uids which
151 * are used for other things..
153 int permission(struct inode * inode,int mask)
155 int mode = inode->i_mode;
157 if (inode->i_op && inode->i_op->permission) {
158 int retval;
159 lock_kernel();
160 retval = inode->i_op->permission(inode, mask);
161 unlock_kernel();
162 return retval;
165 if ((mask & S_IWOTH) && IS_RDONLY(inode) &&
166 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
167 return -EROFS; /* Nobody gets write access to a read-only fs */
169 if ((mask & S_IWOTH) && IS_IMMUTABLE(inode))
170 return -EACCES; /* Nobody gets write access to an immutable file */
172 if (current->fsuid == inode->i_uid)
173 mode >>= 6;
174 else if (in_group_p(inode->i_gid))
175 mode >>= 3;
177 if (((mode & mask & S_IRWXO) == mask) || capable(CAP_DAC_OVERRIDE))
178 return 0;
180 /* read and search access */
181 if ((mask == S_IROTH) ||
182 (S_ISDIR(mode) && !(mask & ~(S_IROTH | S_IXOTH))))
183 if (capable(CAP_DAC_READ_SEARCH))
184 return 0;
186 return -EACCES;
190 * get_write_access() gets write permission for a file.
191 * put_write_access() releases this write permission.
192 * This is used for regular files.
193 * We cannot support write (and maybe mmap read-write shared) accesses and
194 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
195 * can have the following values:
196 * 0: no writers, no VM_DENYWRITE mappings
197 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
198 * > 0: (i_writecount) users are writing to the file.
200 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
201 * except for the cases where we don't hold i_writecount yet. Then we need to
202 * use {get,deny}_write_access() - these functions check the sign and refuse
203 * to do the change if sign is wrong. Exclusion between them is provided by
204 * spinlock (arbitration_lock) and I'll rip the second arsehole to the first
205 * who will try to move it in struct inode - just leave it here.
207 static spinlock_t arbitration_lock = SPIN_LOCK_UNLOCKED;
208 int get_write_access(struct inode * inode)
210 spin_lock(&arbitration_lock);
211 if (atomic_read(&inode->i_writecount) < 0) {
212 spin_unlock(&arbitration_lock);
213 return -ETXTBSY;
215 atomic_inc(&inode->i_writecount);
216 spin_unlock(&arbitration_lock);
217 return 0;
219 int deny_write_access(struct file * file)
221 spin_lock(&arbitration_lock);
222 if (atomic_read(&file->f_dentry->d_inode->i_writecount) > 0) {
223 spin_unlock(&arbitration_lock);
224 return -ETXTBSY;
226 atomic_dec(&file->f_dentry->d_inode->i_writecount);
227 spin_unlock(&arbitration_lock);
228 return 0;
231 void path_release(struct nameidata *nd)
233 lock_kernel();
234 dput(nd->dentry);
235 mntput(nd->mnt);
236 unlock_kernel();
240 * Internal lookup() using the new generic dcache.
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..
262 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, int flags)
264 struct dentry * result;
265 struct inode *dir = parent->d_inode;
267 down(&dir->i_sem);
269 * First re-do the cached lookup just in case it was created
270 * while we waited for the directory semaphore..
272 * FIXME! This could use version numbering or similar to
273 * avoid unnecessary cache lookups.
275 result = d_lookup(parent, name);
276 if (!result) {
277 struct dentry * dentry = d_alloc(parent, name);
278 result = ERR_PTR(-ENOMEM);
279 if (dentry) {
280 result = dir->i_op->lookup(dir, dentry);
281 if (result)
282 dput(dentry);
283 else
284 result = dentry;
286 up(&dir->i_sem);
287 return result;
291 * Uhhuh! Nasty case: the cache was re-populated while
292 * we waited on the semaphore. Need to revalidate, but
293 * we're going to return this entry regardless (same
294 * as if it was busy).
296 up(&dir->i_sem);
297 if (result->d_op && result->d_op->d_revalidate)
298 result->d_op->d_revalidate(result, flags);
299 return result;
302 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
304 int err;
305 if (current->link_count >= 32)
306 goto loop;
307 current->link_count++;
308 UPDATE_ATIME(dentry->d_inode);
309 err = dentry->d_inode->i_op->follow_link(dentry, nd);
310 current->link_count--;
311 return err;
312 loop:
313 path_release(nd);
314 return -ELOOP;
317 static inline int __follow_up(struct vfsmount **mnt, struct dentry **base)
319 struct vfsmount *parent=(*mnt)->mnt_parent;
320 struct dentry *dentry;
321 if (parent == *mnt)
322 return 0;
323 mntget(parent);
324 dentry=dget((*mnt)->mnt_mountpoint);
325 dput(*base);
326 *base = dentry;
327 mntput(*mnt);
328 *mnt = parent;
329 return 1;
332 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
334 return __follow_up(mnt, dentry);
337 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
339 struct list_head *p = (*dentry)->d_vfsmnt.next;
340 while (p != &(*dentry)->d_vfsmnt) {
341 struct vfsmount *tmp;
342 tmp = list_entry(p, struct vfsmount, mnt_clash);
343 if (tmp->mnt_parent == *mnt) {
344 *mnt = mntget(tmp);
345 mntput(tmp->mnt_parent);
346 /* tmp holds the mountpoint, so... */
347 dput(*dentry);
348 *dentry = dget(tmp->mnt_root);
349 return 1;
351 p = p->next;
353 return 0;
356 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
358 return __follow_down(mnt,dentry);
361 static inline void follow_dotdot(struct nameidata *nd)
363 while(1) {
364 struct vfsmount *parent;
365 struct dentry *dentry;
366 if (nd->dentry == current->fs->root &&
367 nd->mnt == current->fs->rootmnt) {
368 break;
370 if (nd->dentry != nd->mnt->mnt_root) {
371 dentry = dget(nd->dentry->d_parent);
372 dput(nd->dentry);
373 nd->dentry = dentry;
374 break;
376 parent=nd->mnt->mnt_parent;
377 if (parent == nd->mnt) {
378 break;
380 mntget(parent);
381 dentry=dget(nd->mnt->mnt_mountpoint);
382 dput(nd->dentry);
383 nd->dentry = dentry;
384 mntput(nd->mnt);
385 nd->mnt = parent;
389 * Name resolution.
391 * This is the basic name resolution function, turning a pathname
392 * into the final dentry.
394 * We expect 'base' to be positive and a directory.
396 int path_walk(const char * name, struct nameidata *nd)
398 struct dentry *dentry;
399 struct inode *inode;
400 int err;
401 unsigned int lookup_flags = nd->flags;
403 while (*name=='/')
404 name++;
405 if (!*name)
406 goto return_base;
408 inode = nd->dentry->d_inode;
409 if (current->link_count)
410 lookup_flags = LOOKUP_FOLLOW;
412 /* At this point we know we have a real path component. */
413 for(;;) {
414 unsigned long hash;
415 struct qstr this;
416 unsigned int c;
418 err = permission(inode, MAY_EXEC);
419 dentry = ERR_PTR(err);
420 if (err)
421 break;
423 this.name = name;
424 c = *(const unsigned char *)name;
426 hash = init_name_hash();
427 do {
428 name++;
429 hash = partial_name_hash(c, hash);
430 c = *(const unsigned char *)name;
431 } while (c && (c != '/'));
432 this.len = name - (const char *) this.name;
433 this.hash = end_name_hash(hash);
435 /* remove trailing slashes? */
436 if (!c)
437 goto last_component;
438 while (*++name == '/');
439 if (!*name)
440 goto last_with_slashes;
443 * "." and ".." are special - ".." especially so because it has
444 * to be able to know about the current root directory and
445 * parent relationships.
447 if (this.name[0] == '.') switch (this.len) {
448 default:
449 break;
450 case 2:
451 if (this.name[1] != '.')
452 break;
453 follow_dotdot(nd);
454 inode = nd->dentry->d_inode;
455 /* fallthrough */
456 case 1:
457 continue;
460 * See if the low-level filesystem might want
461 * to use its own hash..
463 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
464 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
465 if (err < 0)
466 break;
468 /* This does the actual lookups.. */
469 dentry = cached_lookup(nd->dentry, &this, LOOKUP_CONTINUE);
470 if (!dentry) {
471 dentry = real_lookup(nd->dentry, &this, LOOKUP_CONTINUE);
472 err = PTR_ERR(dentry);
473 if (IS_ERR(dentry))
474 break;
476 /* Check mountpoints.. */
477 while (d_mountpoint(dentry) && __follow_down(&nd->mnt, &dentry))
480 err = -ENOENT;
481 inode = dentry->d_inode;
482 if (!inode)
483 goto out_dput;
484 err = -ENOTDIR;
485 if (!inode->i_op)
486 goto out_dput;
488 if (inode->i_op->follow_link) {
489 err = do_follow_link(dentry, nd);
490 dput(dentry);
491 if (err)
492 goto return_err;
493 err = -ENOENT;
494 inode = nd->dentry->d_inode;
495 if (!inode)
496 break;
497 err = -ENOTDIR;
498 if (!inode->i_op)
499 break;
500 } else {
501 dput(nd->dentry);
502 nd->dentry = dentry;
504 err = -ENOTDIR;
505 if (!inode->i_op->lookup)
506 break;
507 continue;
508 /* here ends the main loop */
510 last_with_slashes:
511 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
512 last_component:
513 if (lookup_flags & LOOKUP_PARENT)
514 goto lookup_parent;
515 if (this.name[0] == '.') switch (this.len) {
516 default:
517 break;
518 case 2:
519 if (this.name[1] != '.')
520 break;
521 follow_dotdot(nd);
522 inode = nd->dentry->d_inode;
523 /* fallthrough */
524 case 1:
525 goto return_base;
527 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
528 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
529 if (err < 0)
530 break;
532 dentry = cached_lookup(nd->dentry, &this, 0);
533 if (!dentry) {
534 dentry = real_lookup(nd->dentry, &this, 0);
535 err = PTR_ERR(dentry);
536 if (IS_ERR(dentry))
537 break;
539 while (d_mountpoint(dentry) && __follow_down(&nd->mnt, &dentry))
541 inode = dentry->d_inode;
542 if ((lookup_flags & LOOKUP_FOLLOW)
543 && inode && inode->i_op && inode->i_op->follow_link) {
544 err = do_follow_link(dentry, nd);
545 dput(dentry);
546 if (err)
547 goto return_err;
548 inode = nd->dentry->d_inode;
549 } else {
550 dput(nd->dentry);
551 nd->dentry = dentry;
553 err = -ENOENT;
554 if (!inode)
555 goto no_inode;
556 if (lookup_flags & LOOKUP_DIRECTORY) {
557 err = -ENOTDIR;
558 if (!inode->i_op || !inode->i_op->lookup)
559 break;
561 goto return_base;
562 no_inode:
563 err = -ENOENT;
564 if (lookup_flags & (LOOKUP_POSITIVE|LOOKUP_DIRECTORY))
565 break;
566 goto return_base;
567 lookup_parent:
568 nd->last = this;
569 nd->last_type = LAST_NORM;
570 if (this.name[0] != '.')
571 goto return_base;
572 if (this.len == 1)
573 nd->last_type = LAST_DOT;
574 else if (this.len == 2 && this.name[1] == '.')
575 nd->last_type = LAST_DOTDOT;
576 return_base:
577 return 0;
578 out_dput:
579 dput(dentry);
580 break;
582 path_release(nd);
583 return_err:
584 return err;
587 /* returns 1 if everything is done */
588 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
590 nd->mnt = mntget(current->fs->altrootmnt);
591 nd->dentry = dget(current->fs->altroot);
592 if (path_walk(name, nd))
593 return 0;
595 if (!nd->dentry->d_inode) {
596 struct nameidata nd_root;
597 nd_root.last_type = LAST_ROOT;
598 nd_root.flags = nd->flags;
599 nd_root.mnt = mntget(current->fs->rootmnt);
600 nd_root.dentry = dget(current->fs->root);
601 if (path_walk(name, &nd_root))
602 return 1;
603 if (nd_root.dentry->d_inode) {
604 path_release(nd);
605 nd->dentry = nd_root.dentry;
606 nd->mnt = nd_root.mnt;
607 nd->last = nd_root.last;
608 return 1;
610 path_release(&nd_root);
612 return 1;
615 void set_fs_altroot(void)
617 char *emul = __emul_prefix();
618 struct nameidata nd;
619 struct vfsmount *mnt = NULL, *oldmnt;
620 struct dentry *dentry = NULL, *olddentry;
621 if (emul) {
622 nd.mnt = mntget(current->fs->rootmnt);
623 nd.dentry = dget(current->fs->root);
624 nd.flags = LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_POSITIVE;
625 if (path_walk(emul,&nd) == 0) {
626 mnt = nd.mnt;
627 dentry = nd.dentry;
630 oldmnt = current->fs->altrootmnt;
631 olddentry = current->fs->altroot;
632 current->fs->altrootmnt = mnt;
633 current->fs->altroot = dentry;
634 if (olddentry) {
635 dput(olddentry);
636 mntput(oldmnt);
640 static inline int
641 walk_init_root(const char *name, struct nameidata *nd)
643 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT))
644 if (__emul_lookup_dentry(name,nd))
645 return 0;
646 nd->mnt = mntget(current->fs->rootmnt);
647 nd->dentry = dget(current->fs->root);
648 return 1;
651 int path_init(const char *name,unsigned int flags,struct nameidata *nd)
653 nd->last_type = LAST_ROOT; /* if there are only slashes... */
654 nd->flags = flags;
655 if (*name=='/')
656 return walk_init_root(name,nd);
657 nd->mnt = mntget(current->fs->pwdmnt);
658 nd->dentry = dget(current->fs->pwd);
659 return 1;
663 * Restricted form of lookup. Doesn't follow links, single-component only,
664 * needs parent already locked. Doesn't follow mounts.
666 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
668 struct dentry * dentry;
669 struct inode *inode;
670 int err;
672 inode = base->d_inode;
673 err = permission(inode, MAY_EXEC);
674 dentry = ERR_PTR(err);
675 if (err)
676 goto out;
679 * See if the low-level filesystem might want
680 * to use its own hash..
682 if (base->d_op && base->d_op->d_hash) {
683 err = base->d_op->d_hash(base, name);
684 dentry = ERR_PTR(err);
685 if (err < 0)
686 goto out;
689 dentry = cached_lookup(base, name, 0);
690 if (!dentry) {
691 struct dentry *new = d_alloc(base, name);
692 dentry = ERR_PTR(-ENOMEM);
693 if (!new)
694 goto out;
695 dentry = inode->i_op->lookup(inode, new);
696 if (!dentry)
697 dentry = new;
698 else
699 dput(new);
701 out:
702 return dentry;
705 struct dentry * lookup_one(const char * name, struct dentry * base)
707 unsigned long hash;
708 struct qstr this;
709 unsigned int c;
711 this.name = name;
712 c = *(const unsigned char *)name;
713 if (!c)
714 goto access;
716 hash = init_name_hash();
717 do {
718 name++;
719 if (c == '/')
720 goto access;
721 hash = partial_name_hash(c, hash);
722 c = *(const unsigned char *)name;
723 } while (c);
724 this.len = name - (const char *) this.name;
725 this.hash = end_name_hash(hash);
727 return lookup_hash(&this, base);
728 access:
729 return ERR_PTR(-EACCES);
733 * namei()
735 * is used by most simple commands to get the inode of a specified name.
736 * Open, link etc use their own routines, but this is enough for things
737 * like 'chmod' etc.
739 * namei exists in two versions: namei/lnamei. The only difference is
740 * that namei follows links, while lnamei does not.
742 int __user_walk(const char *name, unsigned flags, struct nameidata *nd)
744 char *tmp;
745 int err;
747 tmp = getname(name);
748 err = PTR_ERR(tmp);
749 if (!IS_ERR(tmp)) {
750 err = 0;
751 lock_kernel();
752 if (path_init(tmp, flags, nd))
753 err = path_walk(tmp, nd);
754 unlock_kernel();
755 putname(tmp);
757 return err;
761 * It's inline, so penalty for filesystems that don't use sticky bit is
762 * minimal.
764 static inline int check_sticky(struct inode *dir, struct inode *inode)
766 if (!(dir->i_mode & S_ISVTX))
767 return 0;
768 if (inode->i_uid == current->fsuid)
769 return 0;
770 if (dir->i_uid == current->fsuid)
771 return 0;
772 return !capable(CAP_FOWNER);
776 * Check whether we can remove a link victim from directory dir, check
777 * whether the type of victim is right.
778 * 1. We can't do it if dir is read-only (done in permission())
779 * 2. We should have write and exec permissions on dir
780 * 3. We can't remove anything from append-only dir
781 * 4. We can't do anything with immutable dir (done in permission())
782 * 5. If the sticky bit on dir is set we should either
783 * a. be owner of dir, or
784 * b. be owner of victim, or
785 * c. have CAP_FOWNER capability
786 * 6. If the victim is append-only or immutable we can't do antyhing with
787 * links pointing to it.
788 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
789 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
790 * 9. We can't remove a root or mountpoint.
792 static inline int may_delete(struct inode *dir,struct dentry *victim, int isdir)
794 int error;
795 if (!victim->d_inode || victim->d_parent->d_inode != dir)
796 return -ENOENT;
797 error = permission(dir,MAY_WRITE | MAY_EXEC);
798 if (error)
799 return error;
800 if (IS_APPEND(dir))
801 return -EPERM;
802 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
803 IS_IMMUTABLE(victim->d_inode))
804 return -EPERM;
805 if (isdir) {
806 if (!S_ISDIR(victim->d_inode->i_mode))
807 return -ENOTDIR;
808 if (IS_ROOT(victim))
809 return -EBUSY;
810 } else if (S_ISDIR(victim->d_inode->i_mode))
811 return -EISDIR;
812 return 0;
815 /* Check whether we can create an object with dentry child in directory
816 * dir.
817 * 1. We can't do it if child already exists (open has special treatment for
818 * this case, but since we are inlined it's OK)
819 * 2. We can't do it if dir is read-only (done in permission())
820 * 3. We should have write and exec permissions on dir
821 * 4. We can't do it if dir is immutable (done in permission())
823 static inline int may_create(struct inode *dir, struct dentry *child) {
824 if (child->d_inode)
825 return -EEXIST;
826 if (IS_DEADDIR(dir))
827 return -ENOENT;
828 return permission(dir,MAY_WRITE | MAY_EXEC);
832 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
833 * reasons.
835 * O_DIRECTORY translates into forcing a directory lookup.
837 static inline int lookup_flags(unsigned int f)
839 unsigned long retval = LOOKUP_FOLLOW;
841 if (f & O_NOFOLLOW)
842 retval &= ~LOOKUP_FOLLOW;
844 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
845 retval &= ~LOOKUP_FOLLOW;
847 if (f & O_DIRECTORY)
848 retval |= LOOKUP_DIRECTORY;
850 return retval;
853 int vfs_create(struct inode *dir, struct dentry *dentry, int mode)
855 int error;
857 mode &= S_IALLUGO & ~current->fs->umask;
858 mode |= S_IFREG;
860 down(&dir->i_zombie);
861 error = may_create(dir, dentry);
862 if (error)
863 goto exit_lock;
865 error = -EACCES; /* shouldn't it be ENOSYS? */
866 if (!dir->i_op || !dir->i_op->create)
867 goto exit_lock;
869 DQUOT_INIT(dir);
870 error = dir->i_op->create(dir, dentry, mode);
871 exit_lock:
872 up(&dir->i_zombie);
873 return error;
877 * open_namei()
879 * namei for open - this is in fact almost the whole open-routine.
881 * Note that the low bits of "flag" aren't the same as in the open
882 * system call - they are 00 - no permissions needed
883 * 01 - read permission needed
884 * 10 - write permission needed
885 * 11 - read/write permissions needed
886 * which is a lot more logical, and also allows the "no perm" needed
887 * for symlinks (where the permissions are checked later).
889 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
891 int acc_mode, error = 0;
892 struct inode *inode;
893 struct dentry *dentry;
894 struct dentry *dir;
895 int count = 0;
897 acc_mode = ACC_MODE(flag);
900 * The simplest case - just a plain lookup.
902 if (!(flag & O_CREAT)) {
903 if (path_init(pathname, lookup_flags(flag), nd))
904 error = path_walk(pathname, nd);
905 if (error)
906 return error;
907 dentry = nd->dentry;
908 goto ok;
912 * Create - we need to know the parent.
914 if (path_init(pathname, LOOKUP_PARENT, nd))
915 error = path_walk(pathname, nd);
916 if (error)
917 return error;
920 * We have the parent and last component. First of all, check
921 * that we are not asked to creat(2) an obvious directory - that
922 * will not do.
924 error = -EISDIR;
925 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
926 goto exit;
928 dir = nd->dentry;
929 down(&dir->d_inode->i_sem);
930 dentry = lookup_hash(&nd->last, nd->dentry);
932 do_last:
933 error = PTR_ERR(dentry);
934 if (IS_ERR(dentry)) {
935 up(&dir->d_inode->i_sem);
936 goto exit;
939 /* Negative dentry, just create the file */
940 if (!dentry->d_inode) {
941 error = vfs_create(dir->d_inode, dentry, mode);
942 up(&dir->d_inode->i_sem);
943 dput(nd->dentry);
944 nd->dentry = dentry;
945 if (error)
946 goto exit;
947 /* Don't check for write permission, don't truncate */
948 acc_mode = 0;
949 flag &= ~O_TRUNC;
950 goto ok;
954 * It already exists.
956 up(&dir->d_inode->i_sem);
958 error = -EEXIST;
959 if (flag & O_EXCL)
960 goto exit_dput;
962 if (d_mountpoint(dentry)) {
963 error = -ELOOP;
964 if (flag & O_NOFOLLOW)
965 goto exit_dput;
966 do __follow_down(&nd->mnt,&dentry); while(d_mountpoint(dentry));
968 error = -ENOENT;
969 if (!dentry->d_inode)
970 goto exit_dput;
971 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
972 goto do_link;
974 dput(nd->dentry);
975 nd->dentry = dentry;
976 error = -EISDIR;
977 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
978 goto exit;
980 error = -ENOENT;
981 inode = dentry->d_inode;
982 if (!inode)
983 goto exit;
985 error = -ELOOP;
986 if (S_ISLNK(inode->i_mode))
987 goto exit;
989 error = -EISDIR;
990 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
991 goto exit;
993 error = permission(inode,acc_mode);
994 if (error)
995 goto exit;
998 * FIFO's, sockets and device files are special: they don't
999 * actually live on the filesystem itself, and as such you
1000 * can write to them even if the filesystem is read-only.
1002 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1003 flag &= ~O_TRUNC;
1004 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1005 error = -EACCES;
1006 if (IS_NODEV(inode))
1007 goto exit;
1009 flag &= ~O_TRUNC;
1010 } else {
1011 error = -EROFS;
1012 if (IS_RDONLY(inode) && (flag & 2))
1013 goto exit;
1016 * An append-only file must be opened in append mode for writing.
1018 error = -EPERM;
1019 if (IS_APPEND(inode)) {
1020 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1021 goto exit;
1022 if (flag & O_TRUNC)
1023 goto exit;
1026 if (flag & O_TRUNC) {
1027 error = get_write_access(inode);
1028 if (error)
1029 goto exit;
1032 * Refuse to truncate files with mandatory locks held on them.
1034 error = locks_verify_locked(inode);
1035 if (!error) {
1036 DQUOT_INIT(inode);
1038 error = do_truncate(dentry, 0);
1040 put_write_access(inode);
1041 if (error)
1042 goto exit;
1043 } else
1044 if (flag & FMODE_WRITE)
1045 DQUOT_INIT(inode);
1047 return 0;
1049 exit_dput:
1050 dput(dentry);
1051 exit:
1052 path_release(nd);
1053 return error;
1055 do_link:
1056 error = -ELOOP;
1057 if (flag & O_NOFOLLOW)
1058 goto exit_dput;
1060 * This is subtle. Instead of calling do_follow_link() we do the
1061 * thing by hands. The reason is that this way we have zero link_count
1062 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1063 * After that we have the parent and last component, i.e.
1064 * we are in the same situation as after the first path_walk().
1065 * Well, almost - if the last component is normal we get its copy
1066 * stored in nd->last.name and we will have to putname() it when we
1067 * are done. Procfs-like symlinks just set LAST_BIND.
1069 UPDATE_ATIME(dentry->d_inode);
1070 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1071 dput(dentry);
1072 if (error)
1073 return error;
1074 if (nd->last_type == LAST_BIND) {
1075 dentry = nd->dentry;
1076 goto ok;
1078 error = -EISDIR;
1079 if (nd->last_type != LAST_NORM)
1080 goto exit;
1081 if (nd->last.name[nd->last.len]) {
1082 putname(nd->last.name);
1083 goto exit;
1085 if (count++==32) {
1086 dentry = nd->dentry;
1087 putname(nd->last.name);
1088 goto ok;
1090 dir = nd->dentry;
1091 down(&dir->d_inode->i_sem);
1092 dentry = lookup_hash(&nd->last, nd->dentry);
1093 putname(nd->last.name);
1094 goto do_last;
1097 static struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1099 struct dentry *dentry;
1101 down(&nd->dentry->d_inode->i_sem);
1102 dentry = ERR_PTR(-EEXIST);
1103 if (nd->last_type != LAST_NORM)
1104 goto fail;
1105 dentry = lookup_hash(&nd->last, nd->dentry);
1106 if (IS_ERR(dentry))
1107 goto fail;
1108 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1109 goto enoent;
1110 return dentry;
1111 enoent:
1112 dput(dentry);
1113 dentry = ERR_PTR(-ENOENT);
1114 fail:
1115 return dentry;
1118 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1120 int error = -EPERM;
1122 mode &= ~current->fs->umask;
1124 down(&dir->i_zombie);
1125 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1126 goto exit_lock;
1128 error = may_create(dir, dentry);
1129 if (error)
1130 goto exit_lock;
1132 error = -EPERM;
1133 if (!dir->i_op || !dir->i_op->mknod)
1134 goto exit_lock;
1136 DQUOT_INIT(dir);
1137 error = dir->i_op->mknod(dir, dentry, mode, dev);
1138 exit_lock:
1139 up(&dir->i_zombie);
1140 return error;
1143 asmlinkage long sys_mknod(const char * filename, int mode, dev_t dev)
1145 int error = 0;
1146 char * tmp;
1147 struct dentry * dentry;
1148 struct nameidata nd;
1150 if (S_ISDIR(mode))
1151 return -EPERM;
1152 tmp = getname(filename);
1153 if (IS_ERR(tmp))
1154 return PTR_ERR(tmp);
1156 lock_kernel();
1157 if (path_init(tmp, LOOKUP_PARENT, &nd))
1158 error = path_walk(tmp, &nd);
1159 if (error)
1160 goto out;
1161 dentry = lookup_create(&nd, 0);
1162 error = PTR_ERR(dentry);
1163 if (!IS_ERR(dentry)) {
1164 switch (mode & S_IFMT) {
1165 case 0: case S_IFREG:
1166 error = vfs_create(nd.dentry->d_inode,dentry,mode);
1167 break;
1168 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
1169 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,dev);
1170 break;
1171 case S_IFDIR:
1172 error = -EPERM;
1173 break;
1174 default:
1175 error = -EINVAL;
1177 dput(dentry);
1179 up(&nd.dentry->d_inode->i_sem);
1180 path_release(&nd);
1181 out:
1182 unlock_kernel();
1183 putname(tmp);
1185 return error;
1188 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1190 int error;
1192 down(&dir->i_zombie);
1193 error = may_create(dir, dentry);
1194 if (error)
1195 goto exit_lock;
1197 error = -EPERM;
1198 if (!dir->i_op || !dir->i_op->mkdir)
1199 goto exit_lock;
1201 DQUOT_INIT(dir);
1202 mode &= (S_IRWXUGO|S_ISVTX) & ~current->fs->umask;
1203 error = dir->i_op->mkdir(dir, dentry, mode);
1205 exit_lock:
1206 up(&dir->i_zombie);
1207 return error;
1210 asmlinkage long sys_mkdir(const char * pathname, int mode)
1212 int error = 0;
1213 char * tmp;
1215 tmp = getname(pathname);
1216 error = PTR_ERR(tmp);
1217 if (!IS_ERR(tmp)) {
1218 struct dentry *dentry;
1219 struct nameidata nd;
1221 lock_kernel();
1222 if (path_init(tmp, LOOKUP_PARENT, &nd))
1223 error = path_walk(tmp, &nd);
1224 if (error)
1225 goto out;
1226 dentry = lookup_create(&nd, 1);
1227 error = PTR_ERR(dentry);
1228 if (!IS_ERR(dentry)) {
1229 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1230 dput(dentry);
1232 up(&nd.dentry->d_inode->i_sem);
1233 path_release(&nd);
1234 out:
1235 unlock_kernel();
1236 putname(tmp);
1239 return error;
1243 * We try to drop the dentry early: we should have
1244 * a usage count of 2 if we're the only user of this
1245 * dentry, and if that is true (possibly after pruning
1246 * the dcache), then we drop the dentry now.
1248 * A low-level filesystem can, if it choses, legally
1249 * do a
1251 * if (!d_unhashed(dentry))
1252 * return -EBUSY;
1254 * if it cannot handle the case of removing a directory
1255 * that is still in use by something else..
1257 static void d_unhash(struct dentry *dentry)
1259 dget(dentry);
1260 switch (dentry->d_count) {
1261 default:
1262 shrink_dcache_parent(dentry);
1263 if (dentry->d_count != 2)
1264 break;
1265 case 2:
1266 d_drop(dentry);
1270 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1272 int error;
1274 error = may_delete(dir, dentry, 1);
1275 if (error)
1276 return error;
1278 if (!dir->i_op || !dir->i_op->rmdir)
1279 return -EPERM;
1281 DQUOT_INIT(dir);
1283 double_down(&dir->i_zombie, &dentry->d_inode->i_zombie);
1284 d_unhash(dentry);
1285 if (IS_DEADDIR(dir))
1286 error = -ENOENT;
1287 else if (d_mountpoint(dentry))
1288 error = -EBUSY;
1289 else {
1290 error = dir->i_op->rmdir(dir, dentry);
1291 if (!error)
1292 dentry->d_inode->i_flags |= S_DEAD;
1294 double_up(&dir->i_zombie, &dentry->d_inode->i_zombie);
1295 if (!error)
1296 d_delete(dentry);
1297 dput(dentry);
1299 return error;
1302 asmlinkage long sys_rmdir(const char * pathname)
1304 int error = 0;
1305 char * name;
1306 struct dentry *dentry;
1307 struct nameidata nd;
1309 name = getname(pathname);
1310 if(IS_ERR(name))
1311 return PTR_ERR(name);
1312 lock_kernel();
1314 if (path_init(name, LOOKUP_PARENT, &nd))
1315 error = path_walk(name, &nd);
1316 if (error)
1317 goto exit;
1319 switch(nd.last_type) {
1320 case LAST_DOTDOT:
1321 error = -ENOTEMPTY;
1322 goto exit1;
1323 case LAST_ROOT: case LAST_DOT:
1324 error = -EBUSY;
1325 goto exit1;
1327 down(&nd.dentry->d_inode->i_sem);
1328 dentry = lookup_hash(&nd.last, nd.dentry);
1329 error = PTR_ERR(dentry);
1330 if (!IS_ERR(dentry)) {
1331 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1332 dput(dentry);
1334 up(&nd.dentry->d_inode->i_sem);
1335 exit1:
1336 path_release(&nd);
1337 exit:
1338 unlock_kernel();
1339 putname(name);
1340 return error;
1343 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1345 int error;
1347 down(&dir->i_zombie);
1348 error = may_delete(dir, dentry, 0);
1349 if (!error) {
1350 error = -EPERM;
1351 if (dir->i_op && dir->i_op->unlink) {
1352 DQUOT_INIT(dir);
1353 if (d_mountpoint(dentry))
1354 error = -EBUSY;
1355 else {
1356 error = dir->i_op->unlink(dir, dentry);
1357 if (!error)
1358 d_delete(dentry);
1362 up(&dir->i_zombie);
1363 return error;
1366 asmlinkage long sys_unlink(const char * pathname)
1368 int error = 0;
1369 char * name;
1370 struct dentry *dentry;
1371 struct nameidata nd;
1373 name = getname(pathname);
1374 if(IS_ERR(name))
1375 return PTR_ERR(name);
1376 lock_kernel();
1378 if (path_init(name, LOOKUP_PARENT, &nd))
1379 error = path_walk(name, &nd);
1380 if (error)
1381 goto exit;
1382 error = -EISDIR;
1383 if (nd.last_type != LAST_NORM)
1384 goto exit1;
1385 down(&nd.dentry->d_inode->i_sem);
1386 dentry = lookup_hash(&nd.last, nd.dentry);
1387 error = PTR_ERR(dentry);
1388 if (!IS_ERR(dentry)) {
1389 /* Why not before? Because we want correct error value */
1390 if (nd.last.name[nd.last.len])
1391 goto slashes;
1392 error = vfs_unlink(nd.dentry->d_inode, dentry);
1393 exit2:
1394 dput(dentry);
1396 up(&nd.dentry->d_inode->i_sem);
1397 exit1:
1398 path_release(&nd);
1399 exit:
1400 unlock_kernel();
1401 putname(name);
1403 return error;
1405 slashes:
1406 error = !dentry->d_inode ? -ENOENT :
1407 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1408 goto exit2;
1411 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1413 int error;
1415 down(&dir->i_zombie);
1416 error = may_create(dir, dentry);
1417 if (error)
1418 goto exit_lock;
1420 error = -EPERM;
1421 if (!dir->i_op || !dir->i_op->symlink)
1422 goto exit_lock;
1424 DQUOT_INIT(dir);
1425 error = dir->i_op->symlink(dir, dentry, oldname);
1427 exit_lock:
1428 up(&dir->i_zombie);
1429 return error;
1432 asmlinkage long sys_symlink(const char * oldname, const char * newname)
1434 int error = 0;
1435 char * from;
1436 char * to;
1438 from = getname(oldname);
1439 if(IS_ERR(from))
1440 return PTR_ERR(from);
1441 to = getname(newname);
1442 error = PTR_ERR(to);
1443 if (!IS_ERR(to)) {
1444 struct dentry *dentry;
1445 struct nameidata nd;
1447 lock_kernel();
1448 if (path_init(to, LOOKUP_PARENT, &nd))
1449 error = path_walk(to, &nd);
1450 if (error)
1451 goto out;
1452 dentry = lookup_create(&nd, 0);
1453 error = PTR_ERR(dentry);
1454 if (!IS_ERR(dentry)) {
1455 error = vfs_symlink(nd.dentry->d_inode, dentry, from);
1456 dput(dentry);
1458 up(&nd.dentry->d_inode->i_sem);
1459 path_release(&nd);
1460 out:
1461 unlock_kernel();
1462 putname(to);
1464 putname(from);
1465 return error;
1468 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1470 struct inode *inode;
1471 int error;
1473 down(&dir->i_zombie);
1474 error = -ENOENT;
1475 inode = old_dentry->d_inode;
1476 if (!inode)
1477 goto exit_lock;
1479 error = may_create(dir, new_dentry);
1480 if (error)
1481 goto exit_lock;
1483 error = -EXDEV;
1484 if (dir->i_dev != inode->i_dev)
1485 goto exit_lock;
1488 * A link to an append-only or immutable file cannot be created.
1490 error = -EPERM;
1491 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1492 goto exit_lock;
1493 if (!dir->i_op || !dir->i_op->link)
1494 goto exit_lock;
1496 DQUOT_INIT(dir);
1497 error = dir->i_op->link(old_dentry, dir, new_dentry);
1499 exit_lock:
1500 up(&dir->i_zombie);
1501 return error;
1505 * Hardlinks are often used in delicate situations. We avoid
1506 * security-related surprises by not following symlinks on the
1507 * newname. --KAB
1509 * We don't follow them on the oldname either to be compatible
1510 * with linux 2.0, and to avoid hard-linking to directories
1511 * and other special files. --ADM
1513 asmlinkage long sys_link(const char * oldname, const char * newname)
1515 int error;
1516 char * from;
1517 char * to;
1519 from = getname(oldname);
1520 if(IS_ERR(from))
1521 return PTR_ERR(from);
1522 to = getname(newname);
1523 error = PTR_ERR(to);
1524 if (!IS_ERR(to)) {
1525 struct dentry *new_dentry;
1526 struct nameidata nd, old_nd;
1528 lock_kernel();
1529 error = 0;
1530 if (path_init(from, LOOKUP_POSITIVE, &old_nd))
1531 error = path_walk(from, &old_nd);
1532 if (error)
1533 goto exit;
1534 if (path_init(to, LOOKUP_PARENT, &nd))
1535 error = path_walk(to, &nd);
1536 if (error)
1537 goto out;
1538 error = -EXDEV;
1539 if (old_nd.mnt != nd.mnt)
1540 goto out;
1541 new_dentry = lookup_create(&nd, 0);
1542 error = PTR_ERR(new_dentry);
1543 if (!IS_ERR(new_dentry)) {
1544 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
1545 dput(new_dentry);
1547 up(&nd.dentry->d_inode->i_sem);
1548 path_release(&nd);
1549 out:
1550 path_release(&old_nd);
1551 exit:
1552 unlock_kernel();
1553 putname(to);
1555 putname(from);
1557 return error;
1561 * The worst of all namespace operations - renaming directory. "Perverted"
1562 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1563 * Problems:
1564 * a) we can get into loop creation. Check is done in is_subdir().
1565 * b) race potential - two innocent renames can create a loop together.
1566 * That's where 4.4 screws up. Current fix: serialization on
1567 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1568 * story.
1569 * c) we have to lock _three_ objects - parents and victim (if it exists).
1570 * And that - after we got ->i_sem on parents (until then we don't know
1571 * whether the target exists at all, let alone whether it is a directory
1572 * or not). Solution: ->i_zombie. Taken only after ->i_sem. Always taken
1573 * on link creation/removal of any kind. And taken (without ->i_sem) on
1574 * directory that will be removed (both in rmdir() and here).
1575 * d) some filesystems don't support opened-but-unlinked directories,
1576 * either because of layout or because they are not ready to deal with
1577 * all cases correctly. The latter will be fixed (taking this sort of
1578 * stuff into VFS), but the former is not going away. Solution: the same
1579 * trick as in rmdir().
1580 * e) conversion from fhandle to dentry may come in the wrong moment - when
1581 * we are removing the target. Solution: we will have to grab ->i_zombie
1582 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1583 * ->i_sem on parents, which works but leads to some truely excessive
1584 * locking].
1586 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
1587 struct inode *new_dir, struct dentry *new_dentry)
1589 int error;
1590 struct inode *target;
1592 if (old_dentry->d_inode == new_dentry->d_inode)
1593 return 0;
1595 error = may_delete(old_dir, old_dentry, 1);
1596 if (error)
1597 return error;
1599 if (new_dir->i_dev != old_dir->i_dev)
1600 return -EXDEV;
1602 if (!new_dentry->d_inode)
1603 error = may_create(new_dir, new_dentry);
1604 else
1605 error = may_delete(new_dir, new_dentry, 1);
1606 if (error)
1607 return error;
1609 if (!old_dir->i_op || !old_dir->i_op->rename)
1610 return -EPERM;
1613 * If we are going to change the parent - check write permissions,
1614 * we'll need to flip '..'.
1616 if (new_dir != old_dir) {
1617 error = permission(old_dentry->d_inode, MAY_WRITE);
1619 if (error)
1620 return error;
1622 DQUOT_INIT(old_dir);
1623 DQUOT_INIT(new_dir);
1624 down(&old_dir->i_sb->s_vfs_rename_sem);
1625 error = -EINVAL;
1626 if (is_subdir(new_dentry, old_dentry))
1627 goto out_unlock;
1628 target = new_dentry->d_inode;
1629 if (target) { /* Hastur! Hastur! Hastur! */
1630 triple_down(&old_dir->i_zombie,
1631 &new_dir->i_zombie,
1632 &target->i_zombie);
1633 d_unhash(new_dentry);
1634 } else
1635 double_down(&old_dir->i_zombie,
1636 &new_dir->i_zombie);
1637 if (IS_DEADDIR(old_dir)||IS_DEADDIR(new_dir))
1638 error = -ENOENT;
1639 else if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1640 error = -EBUSY;
1641 else
1642 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1643 if (target) {
1644 if (!error)
1645 target->i_flags |= S_DEAD;
1646 triple_up(&old_dir->i_zombie,
1647 &new_dir->i_zombie,
1648 &target->i_zombie);
1649 d_rehash(new_dentry);
1650 dput(new_dentry);
1651 } else
1652 double_up(&old_dir->i_zombie,
1653 &new_dir->i_zombie);
1655 if (!error)
1656 d_move(old_dentry,new_dentry);
1657 out_unlock:
1658 up(&old_dir->i_sb->s_vfs_rename_sem);
1659 return error;
1662 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
1663 struct inode *new_dir, struct dentry *new_dentry)
1665 int error;
1667 if (old_dentry->d_inode == new_dentry->d_inode)
1668 return 0;
1670 error = may_delete(old_dir, old_dentry, 0);
1671 if (error)
1672 return error;
1674 if (new_dir->i_dev != old_dir->i_dev)
1675 return -EXDEV;
1677 if (!new_dentry->d_inode)
1678 error = may_create(new_dir, new_dentry);
1679 else
1680 error = may_delete(new_dir, new_dentry, 0);
1681 if (error)
1682 return error;
1684 if (!old_dir->i_op || !old_dir->i_op->rename)
1685 return -EPERM;
1687 DQUOT_INIT(old_dir);
1688 DQUOT_INIT(new_dir);
1689 double_down(&old_dir->i_zombie, &new_dir->i_zombie);
1690 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1691 error = -EBUSY;
1692 else
1693 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1694 double_up(&old_dir->i_zombie, &new_dir->i_zombie);
1695 if (error)
1696 return error;
1697 /* The following d_move() should become unconditional */
1698 if (!(old_dir->i_sb->s_flags & MS_ODD_RENAME)) {
1699 d_move(old_dentry, new_dentry);
1701 return 0;
1704 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1705 struct inode *new_dir, struct dentry *new_dentry)
1707 if (S_ISDIR(old_dentry->d_inode->i_mode))
1708 return vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
1709 else
1710 return vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
1713 static inline int do_rename(const char * oldname, const char * newname)
1715 int error = 0;
1716 struct dentry * old_dir, * new_dir;
1717 struct dentry * old_dentry, *new_dentry;
1718 struct nameidata oldnd, newnd;
1720 if (path_init(oldname, LOOKUP_PARENT, &oldnd))
1721 error = path_walk(oldname, &oldnd);
1723 if (error)
1724 goto exit;
1726 if (path_init(newname, LOOKUP_PARENT, &newnd))
1727 error = path_walk(newname, &newnd);
1728 if (error)
1729 goto exit1;
1731 error = -EXDEV;
1732 if (oldnd.mnt != newnd.mnt)
1733 goto exit2;
1735 old_dir = oldnd.dentry;
1736 error = -EBUSY;
1737 if (oldnd.last_type != LAST_NORM)
1738 goto exit2;
1740 new_dir = newnd.dentry;
1741 if (newnd.last_type != LAST_NORM)
1742 goto exit2;
1744 double_lock(new_dir, old_dir);
1746 old_dentry = lookup_hash(&oldnd.last, old_dir);
1747 error = PTR_ERR(old_dentry);
1748 if (IS_ERR(old_dentry))
1749 goto exit3;
1750 /* source must exist */
1751 error = -ENOENT;
1752 if (!old_dentry->d_inode)
1753 goto exit4;
1754 /* unless the source is a directory trailing slashes give -ENOTDIR */
1755 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
1756 error = -ENOTDIR;
1757 if (oldnd.last.name[oldnd.last.len])
1758 goto exit4;
1759 if (newnd.last.name[newnd.last.len])
1760 goto exit4;
1762 new_dentry = lookup_hash(&newnd.last, new_dir);
1763 error = PTR_ERR(new_dentry);
1764 if (IS_ERR(new_dentry))
1765 goto exit4;
1767 error = vfs_rename(old_dir->d_inode, old_dentry,
1768 new_dir->d_inode, new_dentry);
1770 dput(new_dentry);
1771 exit4:
1772 dput(old_dentry);
1773 exit3:
1774 double_up(&new_dir->d_inode->i_sem, &old_dir->d_inode->i_sem);
1775 exit2:
1776 path_release(&newnd);
1777 exit1:
1778 path_release(&oldnd);
1779 exit:
1780 return error;
1783 asmlinkage long sys_rename(const char * oldname, const char * newname)
1785 int error;
1786 char * from;
1787 char * to;
1789 from = getname(oldname);
1790 if(IS_ERR(from))
1791 return PTR_ERR(from);
1792 to = getname(newname);
1793 error = PTR_ERR(to);
1794 if (!IS_ERR(to)) {
1795 lock_kernel();
1796 error = do_rename(from,to);
1797 unlock_kernel();
1798 putname(to);
1800 putname(from);
1801 return error;
1804 int vfs_readlink(struct dentry *dentry, char *buffer, int buflen, const char *link)
1806 int len;
1808 len = PTR_ERR(link);
1809 if (IS_ERR(link))
1810 goto out;
1812 len = strlen(link);
1813 if (len > (unsigned) buflen)
1814 len = buflen;
1815 if (copy_to_user(buffer, link, len))
1816 len = -EFAULT;
1817 out:
1818 return len;
1821 static inline int
1822 __vfs_follow_link(struct nameidata *nd, const char *link)
1824 int res = 0;
1825 char *name;
1826 if (IS_ERR(link))
1827 goto fail;
1829 if (*link == '/') {
1830 path_release(nd);
1831 if (!walk_init_root(link, nd))
1832 /* weird __emul_prefix() stuff did it */
1833 goto out;
1835 res = path_walk(link, nd);
1836 out:
1837 if (current->link_count || res || nd->last_type!=LAST_NORM)
1838 return res;
1840 * If it is an iterative symlinks resolution in open_namei() we
1841 * have to copy the last component. And all that crap because of
1842 * bloody create() on broken symlinks. Furrfu...
1844 name = __getname();
1845 if (IS_ERR(name))
1846 goto fail_name;
1847 strcpy(name, nd->last.name);
1848 nd->last.name = name;
1849 return 0;
1850 fail_name:
1851 link = name;
1852 fail:
1853 path_release(nd);
1854 return PTR_ERR(link);
1857 int vfs_follow_link(struct nameidata *nd, const char *link)
1859 return __vfs_follow_link(nd, link);
1862 /* get the link contents into pagecache */
1863 static char *page_getlink(struct dentry * dentry, struct page **ppage)
1865 struct page * page;
1866 struct address_space *mapping = dentry->d_inode->i_mapping;
1867 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
1868 dentry);
1869 if (IS_ERR(page))
1870 goto sync_fail;
1871 wait_on_page(page);
1872 if (!Page_Uptodate(page))
1873 goto async_fail;
1874 *ppage = page;
1875 return (char*) kmap(page);
1877 async_fail:
1878 page_cache_release(page);
1879 return ERR_PTR(-EIO);
1881 sync_fail:
1882 return (char*)page;
1885 int page_readlink(struct dentry *dentry, char *buffer, int buflen)
1887 struct page *page = NULL;
1888 char *s = page_getlink(dentry, &page);
1889 int res = vfs_readlink(dentry,buffer,buflen,s);
1890 if (page) {
1891 kunmap(page);
1892 page_cache_release(page);
1894 return res;
1897 int page_follow_link(struct dentry *dentry, struct nameidata *nd)
1899 struct page *page = NULL;
1900 char *s = page_getlink(dentry, &page);
1901 int res = __vfs_follow_link(nd, s);
1902 if (page) {
1903 kunmap(page);
1904 page_cache_release(page);
1906 return res;
1909 struct inode_operations page_symlink_inode_operations = {
1910 readlink: page_readlink,
1911 follow_link: page_follow_link,