Rest of the merge.
[linux-2.6/linux-mips.git] / fs / namei.c
blob102f0e43276bc9c7e46a44b03470e5eac602910e
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.
15 #include <linux/mm.h>
16 #include <linux/proc_fs.h>
17 #include <linux/smp_lock.h>
18 #include <linux/quotaops.h>
19 #include <linux/pagemap.h>
20 #include <linux/dcache.h>
22 #include <asm/uaccess.h>
23 #include <asm/unaligned.h>
24 #include <asm/semaphore.h>
25 #include <asm/page.h>
26 #include <asm/pgtable.h>
28 #include <asm/namei.h>
30 /* This can be removed after the beta phase. */
31 #define CACHE_SUPERVISE /* debug the correctness of dcache entries */
32 #undef DEBUG /* some other debugging */
35 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
37 /* [Feb-1997 T. Schoebel-Theuer]
38 * Fundamental changes in the pathname lookup mechanisms (namei)
39 * were necessary because of omirr. The reason is that omirr needs
40 * to know the _real_ pathname, not the user-supplied one, in case
41 * of symlinks (and also when transname replacements occur).
43 * The new code replaces the old recursive symlink resolution with
44 * an iterative one (in case of non-nested symlink chains). It does
45 * this with calls to <fs>_follow_link().
46 * As a side effect, dir_namei(), _namei() and follow_link() are now
47 * replaced with a single function lookup_dentry() that can handle all
48 * the special cases of the former code.
50 * With the new dcache, the pathname is stored at each inode, at least as
51 * long as the refcount of the inode is positive. As a side effect, the
52 * size of the dcache depends on the inode cache and thus is dynamic.
54 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
55 * resolution to correspond with current state of the code.
57 * Note that the symlink resolution is not *completely* iterative.
58 * There is still a significant amount of tail- and mid- recursion in
59 * the algorithm. Also, note that <fs>_readlink() is not used in
60 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
61 * may return different results than <fs>_follow_link(). Many virtual
62 * filesystems (including /proc) exhibit this behavior.
65 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
66 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
67 * and the name already exists in form of a symlink, try to create the new
68 * name indicated by the symlink. The old code always complained that the
69 * name already exists, due to not following the symlink even if its target
70 * is nonexistent. The new semantics affects also mknod() and link() when
71 * the name is a symlink pointing to a non-existant name.
73 * I don't know which semantics is the right one, since I have no access
74 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
75 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
76 * "old" one. Personally, I think the new semantics is much more logical.
77 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
78 * file does succeed in both HP-UX and SunOs, but not in Solaris
79 * and in the old Linux semantics.
82 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
83 * semantics. See the comments in "open_namei" and "do_link" below.
85 * [10-Sep-98 Alan Modra] Another symlink change.
88 /* In order to reduce some races, while at the same time doing additional
89 * checking and hopefully speeding things up, we copy filenames to the
90 * kernel data space before using them..
92 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
94 static inline int do_getname(const char *filename, char *page)
96 int retval;
97 unsigned long len = PAGE_SIZE;
99 if ((unsigned long) filename >= TASK_SIZE) {
100 if (!segment_eq(get_fs(), KERNEL_DS))
101 return -EFAULT;
102 } else if (TASK_SIZE - (unsigned long) filename < PAGE_SIZE)
103 len = TASK_SIZE - (unsigned long) filename;
105 retval = strncpy_from_user((char *)page, filename, len);
106 if (retval > 0) {
107 if (retval < len)
108 return 0;
109 return -ENAMETOOLONG;
110 } else if (!retval)
111 retval = -ENOENT;
112 return retval;
115 char * getname(const char * filename)
117 char *tmp, *result;
119 result = ERR_PTR(-ENOMEM);
120 tmp = __getname();
121 if (tmp) {
122 int retval = do_getname(filename, tmp);
124 result = tmp;
125 if (retval < 0) {
126 putname(tmp);
127 result = ERR_PTR(retval);
130 return result;
134 * permission()
136 * is used to check for read/write/execute permissions on a file.
137 * We use "fsuid" for this, letting us set arbitrary permissions
138 * for filesystem access without changing the "normal" uids which
139 * are used for other things..
141 int permission(struct inode * inode,int mask)
143 int mode = inode->i_mode;
145 if (inode->i_op && inode->i_op->permission)
146 return inode->i_op->permission(inode, mask);
147 else if ((mask & S_IWOTH) && IS_RDONLY(inode) &&
148 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
149 return -EROFS; /* Nobody gets write access to a read-only fs */
150 else if ((mask & S_IWOTH) && IS_IMMUTABLE(inode))
151 return -EACCES; /* Nobody gets write access to an immutable file */
152 else if (current->fsuid == inode->i_uid)
153 mode >>= 6;
154 else if (in_group_p(inode->i_gid))
155 mode >>= 3;
156 if (((mode & mask & S_IRWXO) == mask) || capable(CAP_DAC_OVERRIDE))
157 return 0;
158 /* read and search access */
159 if ((mask == S_IROTH) ||
160 (S_ISDIR(mode) && !(mask & ~(S_IROTH | S_IXOTH))))
161 if (capable(CAP_DAC_READ_SEARCH))
162 return 0;
163 return -EACCES;
167 * get_write_access() gets write permission for a file.
168 * put_write_access() releases this write permission.
169 * This is used for regular files.
170 * We cannot support write (and maybe mmap read-write shared) accesses and
171 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
172 * can have the following values:
173 * 0: no writers, no VM_DENYWRITE mappings
174 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
175 * > 0: (i_writecount) users are writing to the file.
177 * WARNING: as soon as we will move get_write_access(), do_mmap() or
178 * prepare_binfmt() out of the big lock we will need a spinlock protecting
179 * the checks in all 3. For the time being it is not needed.
181 int get_write_access(struct inode * inode)
183 if (atomic_read(&inode->i_writecount) < 0)
184 return -ETXTBSY;
185 atomic_inc(&inode->i_writecount);
186 return 0;
189 void put_write_access(struct inode * inode)
191 atomic_dec(&inode->i_writecount);
195 * "." and ".." are special - ".." especially so because it has to be able
196 * to know about the current root directory and parent relationships
198 static struct dentry * reserved_lookup(struct dentry * parent, struct qstr * name)
200 struct dentry *result = NULL;
201 if (name->name[0] == '.') {
202 switch (name->len) {
203 default:
204 break;
205 case 2:
206 if (name->name[1] != '.')
207 break;
209 if (parent != current->fs->root)
210 parent = parent->d_covers->d_parent;
211 /* fallthrough */
212 case 1:
213 result = parent;
216 return dget(result);
220 * Internal lookup() using the new generic dcache.
222 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, int flags)
224 struct dentry * dentry = d_lookup(parent, name);
226 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
227 if (!dentry->d_op->d_revalidate(dentry, flags) && !d_invalidate(dentry)) {
228 dput(dentry);
229 dentry = NULL;
232 return dentry;
236 * This is called when everything else fails, and we actually have
237 * to go to the low-level filesystem to find out what we should do..
239 * We get the directory semaphore, and after getting that we also
240 * make sure that nobody added the entry to the dcache in the meantime..
242 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, int flags)
244 struct dentry * result;
245 struct inode *dir = parent->d_inode;
247 down(&dir->i_sem);
249 * First re-do the cached lookup just in case it was created
250 * while we waited for the directory semaphore..
252 * FIXME! This could use version numbering or similar to
253 * avoid unnecessary cache lookups.
255 result = d_lookup(parent, name);
256 if (!result) {
257 struct dentry * dentry = d_alloc(parent, name);
258 result = ERR_PTR(-ENOMEM);
259 if (dentry) {
260 result = dir->i_op->lookup(dir, dentry);
261 if (result)
262 dput(dentry);
263 else
264 result = dentry;
266 up(&dir->i_sem);
267 return result;
271 * Uhhuh! Nasty case: the cache was re-populated while
272 * we waited on the semaphore. Need to revalidate, but
273 * we're going to return this entry regardless (same
274 * as if it was busy).
276 up(&dir->i_sem);
277 if (result->d_op && result->d_op->d_revalidate)
278 result->d_op->d_revalidate(result, flags);
279 return result;
282 static struct dentry * do_follow_link(struct dentry *base, struct dentry *dentry, unsigned int follow)
284 struct inode * inode = dentry->d_inode;
286 if ((follow & LOOKUP_FOLLOW)
287 && inode && inode->i_op && inode->i_op->follow_link) {
288 if (current->link_count < 32) {
289 struct dentry * result;
291 current->link_count++;
292 /* This eats the base */
293 result = inode->i_op->follow_link(dentry, base, follow);
294 current->link_count--;
295 dput(dentry);
296 return result;
298 dput(dentry);
299 dentry = ERR_PTR(-ELOOP);
301 dput(base);
302 return dentry;
305 static inline struct dentry * follow_mount(struct dentry * dentry)
307 struct dentry * mnt = dentry->d_mounts;
309 if (mnt != dentry) {
310 dget(mnt);
311 dput(dentry);
312 dentry = mnt;
314 return dentry;
318 * Name resolution.
320 * This is the basic name resolution function, turning a pathname
321 * into the final dentry.
323 struct dentry * lookup_dentry(const char * name, struct dentry * base, unsigned int lookup_flags)
325 struct dentry * dentry;
326 struct inode *inode;
328 if (*name == '/') {
329 if (base)
330 dput(base);
331 do {
332 name++;
333 } while (*name == '/');
334 __prefix_lookup_dentry(name, lookup_flags);
335 base = dget(current->fs->root);
336 } else if (!base) {
337 base = dget(current->fs->pwd);
340 if (!*name)
341 goto return_base;
343 inode = base->d_inode;
344 lookup_flags &= LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_SLASHOK;
346 /* At this point we know we have a real path component. */
347 for(;;) {
348 int err;
349 unsigned long hash;
350 struct qstr this;
351 unsigned int flags;
352 unsigned int c;
354 err = permission(inode, MAY_EXEC);
355 dentry = ERR_PTR(err);
356 if (err)
357 break;
359 this.name = name;
360 c = *(const unsigned char *)name;
362 hash = init_name_hash();
363 do {
364 name++;
365 hash = partial_name_hash(c, hash);
366 c = *(const unsigned char *)name;
367 } while (c && (c != '/'));
368 this.len = name - (const char *) this.name;
369 this.hash = end_name_hash(hash);
371 /* remove trailing slashes? */
372 flags = lookup_flags;
373 if (c) {
374 char tmp;
376 flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
377 do {
378 tmp = *++name;
379 } while (tmp == '/');
380 if (tmp)
381 flags |= LOOKUP_CONTINUE;
385 * See if the low-level filesystem might want
386 * to use its own hash..
388 if (base->d_op && base->d_op->d_hash) {
389 int error;
390 error = base->d_op->d_hash(base, &this);
391 if (error < 0) {
392 dentry = ERR_PTR(error);
393 break;
397 /* This does the actual lookups.. */
398 dentry = reserved_lookup(base, &this);
399 if (!dentry) {
400 dentry = cached_lookup(base, &this, flags);
401 if (!dentry) {
402 dentry = real_lookup(base, &this, flags);
403 if (IS_ERR(dentry))
404 break;
408 /* Check mountpoints.. */
409 dentry = follow_mount(dentry);
411 base = do_follow_link(base, dentry, flags);
412 if (IS_ERR(base))
413 goto return_base;
415 inode = base->d_inode;
416 if (flags & LOOKUP_DIRECTORY) {
417 if (!inode)
418 goto no_inode;
419 dentry = ERR_PTR(-ENOTDIR);
420 if (!inode->i_op || !inode->i_op->lookup)
421 break;
422 if (flags & LOOKUP_CONTINUE)
423 continue;
425 return_base:
426 return base;
428 * The case of a nonexisting file is special.
430 * In the middle of a pathname lookup (ie when
431 * LOOKUP_CONTINUE is set), it's an obvious
432 * error and returns ENOENT.
434 * At the end of a pathname lookup it's legal,
435 * and we return a negative dentry. However, we
436 * get here only if there were trailing slashes,
437 * which is legal only if we know it's supposed
438 * to be a directory (ie "mkdir"). Thus the
439 * LOOKUP_SLASHOK flag.
441 no_inode:
442 dentry = ERR_PTR(-ENOENT);
443 if (flags & LOOKUP_CONTINUE)
444 break;
445 if (flags & LOOKUP_SLASHOK)
446 goto return_base;
447 break;
449 dput(base);
450 return dentry;
454 * namei()
456 * is used by most simple commands to get the inode of a specified name.
457 * Open, link etc use their own routines, but this is enough for things
458 * like 'chmod' etc.
460 * namei exists in two versions: namei/lnamei. The only difference is
461 * that namei follows links, while lnamei does not.
463 struct dentry * __namei(const char *pathname, unsigned int lookup_flags)
465 char *name;
466 struct dentry *dentry;
468 name = getname(pathname);
469 dentry = (struct dentry *) name;
470 if (!IS_ERR(name)) {
471 dentry = lookup_dentry(name, NULL, lookup_flags);
472 putname(name);
473 if (!IS_ERR(dentry)) {
474 if (!dentry->d_inode) {
475 dput(dentry);
476 dentry = ERR_PTR(-ENOENT);
480 return dentry;
484 * It's inline, so penalty for filesystems that don't use sticky bit is
485 * minimal.
487 static inline int check_sticky(struct inode *dir, struct inode *inode)
489 if (!(dir->i_mode & S_ISVTX))
490 return 0;
491 if (inode->i_uid == current->fsuid)
492 return 0;
493 if (dir->i_uid == current->fsuid)
494 return 0;
495 return !capable(CAP_FOWNER);
499 * Check whether we can remove a link victim from directory dir, check
500 * whether the type of victim is right.
501 * 1. We can't do it if dir is read-only (done in permission())
502 * 2. We should have write and exec permissions on dir
503 * 3. We can't remove anything from append-only dir
504 * 4. We can't do anything with immutable dir (done in permission())
505 * 5. If the sticky bit on dir is set we should either
506 * a. be owner of dir, or
507 * b. be owner of victim, or
508 * c. have CAP_FOWNER capability
509 * 6. If the victim is append-only or immutable we can't do antyhing with
510 * links pointing to it.
511 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
512 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
513 * 9. We can't remove a root or mountpoint.
515 static inline int may_delete(struct inode *dir,struct dentry *victim, int isdir)
517 int error;
518 if (!victim->d_inode || victim->d_parent->d_inode != dir)
519 return -ENOENT;
520 error = permission(dir,MAY_WRITE | MAY_EXEC);
521 if (error)
522 return error;
523 if (IS_APPEND(dir))
524 return -EPERM;
525 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
526 IS_IMMUTABLE(victim->d_inode))
527 return -EPERM;
528 if (isdir) {
529 if (!S_ISDIR(victim->d_inode->i_mode))
530 return -ENOTDIR;
531 if (IS_ROOT(victim))
532 return -EBUSY;
533 if (victim->d_mounts != victim->d_covers)
534 return -EBUSY;
535 } else if (S_ISDIR(victim->d_inode->i_mode))
536 return -EISDIR;
537 return 0;
540 /* Check whether we can create an object with dentry child in directory
541 * dir.
542 * 1. We can't do it if child already exists (open has special treatment for
543 * this case, but since we are inlined it's OK)
544 * 2. We can't do it if dir is read-only (done in permission())
545 * 3. We should have write and exec permissions on dir
546 * 4. We can't do it if dir is immutable (done in permission())
548 static inline int may_create(struct inode *dir, struct dentry *child) {
549 if (child->d_inode)
550 return -EEXIST;
551 return permission(dir,MAY_WRITE | MAY_EXEC);
555 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
556 * reasons.
558 * O_DIRECTORY translates into forcing a directory lookup.
560 static inline int lookup_flags(unsigned int f)
562 unsigned long retval = LOOKUP_FOLLOW;
564 if (f & O_NOFOLLOW)
565 retval &= ~LOOKUP_FOLLOW;
567 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
568 retval &= ~LOOKUP_FOLLOW;
570 if (f & O_DIRECTORY)
571 retval |= LOOKUP_DIRECTORY;
573 return retval;
576 int vfs_create(struct inode *dir, struct dentry *dentry, int mode)
578 int error;
580 error = may_create(dir, dentry);
581 if (error)
582 goto exit_lock;
584 error = -EACCES; /* shouldn't it be ENOSYS? */
585 if (!dir->i_op || !dir->i_op->create)
586 goto exit_lock;
588 DQUOT_INIT(dir);
589 error = dir->i_op->create(dir, dentry, mode);
590 exit_lock:
591 return error;
595 * open_namei()
597 * namei for open - this is in fact almost the whole open-routine.
599 * Note that the low bits of "flag" aren't the same as in the open
600 * system call - they are 00 - no permissions needed
601 * 01 - read permission needed
602 * 10 - write permission needed
603 * 11 - read/write permissions needed
604 * which is a lot more logical, and also allows the "no perm" needed
605 * for symlinks (where the permissions are checked later).
607 struct dentry * open_namei(const char * pathname, int flag, int mode)
609 int acc_mode, error;
610 struct inode *inode;
611 struct dentry *dentry;
613 mode &= S_IALLUGO & ~current->fs->umask;
614 mode |= S_IFREG;
616 dentry = lookup_dentry(pathname, NULL, lookup_flags(flag));
617 if (IS_ERR(dentry))
618 return dentry;
620 acc_mode = ACC_MODE(flag);
621 if (flag & O_CREAT) {
622 struct dentry *dir;
624 if (dentry->d_inode) {
625 if (!(flag & O_EXCL))
626 goto nocreate;
627 error = -EEXIST;
628 goto exit;
631 dir = lock_parent(dentry);
632 if (!check_parent(dir, dentry)) {
634 * Really nasty race happened. What's the
635 * right error code? We had a dentry, but
636 * before we could use it it was removed
637 * by somebody else. We could just re-try
638 * everything, I guess.
640 * ENOENT is definitely wrong.
642 error = -ENOENT;
643 unlock_dir(dir);
644 goto exit;
648 * Somebody might have created the file while we
649 * waited for the directory lock.. So we have to
650 * re-do the existence test.
652 if (dentry->d_inode) {
653 error = 0;
654 if (flag & O_EXCL)
655 error = -EEXIST;
656 } else {
657 error = vfs_create(dir->d_inode, dentry,mode);
658 /* Don't check for write permission, don't truncate */
659 acc_mode = 0;
660 flag &= ~O_TRUNC;
662 unlock_dir(dir);
663 if (error)
664 goto exit;
667 nocreate:
668 error = -ENOENT;
669 inode = dentry->d_inode;
670 if (!inode)
671 goto exit;
673 error = -ELOOP;
674 if (S_ISLNK(inode->i_mode))
675 goto exit;
677 error = -EISDIR;
678 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
679 goto exit;
681 error = permission(inode,acc_mode);
682 if (error)
683 goto exit;
686 * FIFO's, sockets and device files are special: they don't
687 * actually live on the filesystem itself, and as such you
688 * can write to them even if the filesystem is read-only.
690 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
691 flag &= ~O_TRUNC;
692 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
693 error = -EACCES;
694 if (IS_NODEV(inode))
695 goto exit;
697 flag &= ~O_TRUNC;
698 } else {
699 error = -EROFS;
700 if (IS_RDONLY(inode) && (flag & 2))
701 goto exit;
704 * An append-only file must be opened in append mode for writing.
706 error = -EPERM;
707 if (IS_APPEND(inode)) {
708 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
709 goto exit;
710 if (flag & O_TRUNC)
711 goto exit;
714 if (flag & O_TRUNC) {
715 error = get_write_access(inode);
716 if (error)
717 goto exit;
720 * Refuse to truncate files with mandatory locks held on them.
722 error = locks_verify_locked(inode);
723 if (!error) {
724 DQUOT_INIT(inode);
726 error = do_truncate(dentry, 0);
728 put_write_access(inode);
729 if (error)
730 goto exit;
731 } else
732 if (flag & FMODE_WRITE)
733 DQUOT_INIT(inode);
735 return dentry;
737 exit:
738 dput(dentry);
739 return ERR_PTR(error);
742 struct dentry * do_mknod(const char * filename, int mode, dev_t dev)
744 int error;
745 struct dentry *dir;
746 struct dentry *dentry, *retval;
748 mode &= ~current->fs->umask;
749 dentry = lookup_dentry(filename, NULL, LOOKUP_FOLLOW);
750 if (IS_ERR(dentry))
751 return dentry;
753 dir = lock_parent(dentry);
754 error = -ENOENT;
755 if (!check_parent(dir, dentry))
756 goto exit_lock;
758 error = may_create(dir->d_inode, dentry);
759 if (error)
760 goto exit_lock;
762 error = -EPERM;
763 if (!dir->d_inode->i_op || !dir->d_inode->i_op->mknod)
764 goto exit_lock;
766 DQUOT_INIT(dir->d_inode);
767 error = dir->d_inode->i_op->mknod(dir->d_inode, dentry, mode, dev);
768 exit_lock:
769 retval = ERR_PTR(error);
770 if (!error)
771 retval = dget(dentry);
772 unlock_dir(dir);
773 dput(dentry);
774 return retval;
777 asmlinkage long sys_mknod(const char * filename, int mode, dev_t dev)
779 int error;
780 char * tmp;
781 struct dentry * dentry;
783 if (S_ISDIR(mode) || (!S_ISFIFO(mode) && !capable(CAP_MKNOD)))
784 return -EPERM;
785 tmp = getname(filename);
786 if (IS_ERR(tmp))
787 return PTR_ERR(tmp);
789 error = -EINVAL;
790 lock_kernel();
791 switch (mode & S_IFMT) {
792 case 0:
793 mode |= S_IFREG; /* fallthrough */
794 case S_IFREG:
795 mode &= ~current->fs->umask;
796 dentry = lookup_dentry(filename, NULL, LOOKUP_FOLLOW);
797 if (IS_ERR(dentry))
798 error = PTR_ERR(dentry);
799 else {
800 struct dentry *dir = lock_parent(dentry);
801 error = -ENOENT;
802 if (check_parent(dir, dentry))
803 error = vfs_create(dir->d_inode, dentry, mode);
804 dput(dentry);
806 break;
807 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
808 dentry = do_mknod(tmp,mode,dev);
809 error = PTR_ERR(dentry);
810 if (!IS_ERR(dentry)) {
811 dput(dentry);
812 error = 0;
814 break;
816 unlock_kernel();
817 putname(tmp);
819 return error;
822 static inline int do_mkdir(const char * pathname, int mode)
824 int error;
825 struct dentry *dir;
826 struct dentry *dentry;
828 dentry = lookup_dentry(pathname, NULL, LOOKUP_SLASHOK);
829 error = PTR_ERR(dentry);
830 if (IS_ERR(dentry))
831 goto exit;
834 * EEXIST is kind of a strange error code to
835 * return, but basically if the dentry was moved
836 * or unlinked while we locked the parent, we
837 * do know that it _did_ exist before, and as
838 * such it makes perfect sense.. In contrast,
839 * ENOENT doesn't make sense for mkdir.
841 dir = lock_parent(dentry);
842 error = -EEXIST;
843 if (!check_parent(dir, dentry))
844 goto exit_lock;
846 error = may_create(dir->d_inode, dentry);
847 if (error)
848 goto exit_lock;
850 error = -EPERM;
851 if (!dir->d_inode->i_op || !dir->d_inode->i_op->mkdir)
852 goto exit_lock;
854 DQUOT_INIT(dir->d_inode);
855 mode &= (S_IRWXUGO|S_ISVTX) & ~current->fs->umask;
856 error = dir->d_inode->i_op->mkdir(dir->d_inode, dentry, mode);
858 exit_lock:
859 unlock_dir(dir);
860 dput(dentry);
861 exit:
862 return error;
865 asmlinkage long sys_mkdir(const char * pathname, int mode)
867 int error;
868 char * tmp;
870 tmp = getname(pathname);
871 if(IS_ERR(tmp))
872 return PTR_ERR(tmp);
873 lock_kernel();
874 error = do_mkdir(tmp,mode);
875 unlock_kernel();
876 putname(tmp);
878 return error;
881 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
883 int error;
885 error = may_delete(dir, dentry, 1);
886 if (error)
887 return error;
889 if (!dir->i_op || !dir->i_op->rmdir)
890 return -EPERM;
892 DQUOT_INIT(dir);
895 * We try to drop the dentry early: we should have
896 * a usage count of 2 if we're the only user of this
897 * dentry, and if that is true (possibly after pruning
898 * the dcache), then we drop the dentry now.
900 * A low-level filesystem can, if it choses, legally
901 * do a
903 * if (!list_empty(&dentry->d_hash))
904 * return -EBUSY;
906 * if it cannot handle the case of removing a directory
907 * that is still in use by something else..
909 switch (dentry->d_count) {
910 default:
911 shrink_dcache_parent(dentry);
912 if (dentry->d_count != 2)
913 break;
914 case 2:
915 d_drop(dentry);
918 error = dir->i_op->rmdir(dir, dentry);
920 return error;
923 static inline int do_rmdir(const char * name)
925 int error;
926 struct dentry *dir;
927 struct dentry *dentry;
929 dentry = lookup_dentry(name, NULL, 0);
930 error = PTR_ERR(dentry);
931 if (IS_ERR(dentry))
932 goto exit;
934 error = -ENOENT;
935 if (!dentry->d_inode)
936 goto exit_dput;
938 dir = dget(dentry->d_parent);
941 * The dentry->d_count stuff confuses d_delete() enough to
942 * not kill the inode from under us while it is locked. This
943 * wouldn't be needed, except the dentry semaphore is really
944 * in the inode, not in the dentry..
946 dentry->d_count++;
947 double_lock(dir, dentry);
949 error = -ENOENT;
950 if (check_parent(dir, dentry))
951 error = vfs_rmdir(dir->d_inode, dentry);
953 double_unlock(dentry, dir);
954 exit_dput:
955 dput(dentry);
956 exit:
957 return error;
960 asmlinkage long sys_rmdir(const char * pathname)
962 int error;
963 char * tmp;
965 tmp = getname(pathname);
966 if(IS_ERR(tmp))
967 return PTR_ERR(tmp);
968 lock_kernel();
969 error = do_rmdir(tmp);
970 unlock_kernel();
972 putname(tmp);
974 return error;
977 int vfs_unlink(struct inode *dir, struct dentry *dentry)
979 int error;
981 error = may_delete(dir, dentry, 0);
982 if (!error) {
983 error = -EPERM;
984 if (dir->i_op && dir->i_op->unlink) {
985 DQUOT_INIT(dir);
986 error = dir->i_op->unlink(dir, dentry);
989 return error;
992 static inline int do_unlink(const char * name)
994 int error;
995 struct dentry *dir;
996 struct dentry *dentry;
998 dentry = lookup_dentry(name, NULL, 0);
999 error = PTR_ERR(dentry);
1000 if (IS_ERR(dentry))
1001 goto exit;
1003 dir = lock_parent(dentry);
1004 error = -ENOENT;
1005 if (check_parent(dir, dentry))
1006 error = vfs_unlink(dir->d_inode, dentry);
1008 unlock_dir(dir);
1009 dput(dentry);
1010 exit:
1011 return error;
1014 asmlinkage long sys_unlink(const char * pathname)
1016 int error;
1017 char * tmp;
1019 tmp = getname(pathname);
1020 if(IS_ERR(tmp))
1021 return PTR_ERR(tmp);
1022 lock_kernel();
1023 error = do_unlink(tmp);
1024 unlock_kernel();
1025 putname(tmp);
1027 return error;
1030 static inline int do_symlink(const char * oldname, const char * newname)
1032 int error;
1033 struct dentry *dir;
1034 struct dentry *dentry;
1036 dentry = lookup_dentry(newname, NULL, 0);
1038 error = PTR_ERR(dentry);
1039 if (IS_ERR(dentry))
1040 goto exit;
1042 dir = lock_parent(dentry);
1043 error = -ENOENT;
1044 if (!check_parent(dir, dentry))
1045 goto exit_lock;
1047 error = may_create(dir->d_inode, dentry);
1048 if (error)
1049 goto exit_lock;
1051 error = -EPERM;
1052 if (!dir->d_inode->i_op || !dir->d_inode->i_op->symlink)
1053 goto exit_lock;
1055 DQUOT_INIT(dir->d_inode);
1056 error = dir->d_inode->i_op->symlink(dir->d_inode, dentry, oldname);
1058 exit_lock:
1059 unlock_dir(dir);
1060 dput(dentry);
1061 exit:
1062 return error;
1065 asmlinkage long sys_symlink(const char * oldname, const char * newname)
1067 int error;
1068 char * from;
1069 char * to;
1071 from = getname(oldname);
1072 if(IS_ERR(from))
1073 return PTR_ERR(from);
1074 to = getname(newname);
1075 error = PTR_ERR(to);
1076 if (!IS_ERR(to)) {
1077 lock_kernel();
1078 error = do_symlink(from,to);
1079 unlock_kernel();
1080 putname(to);
1082 putname(from);
1083 return error;
1086 static inline int do_link(const char * oldname, const char * newname)
1088 struct dentry *old_dentry, *new_dentry, *dir;
1089 struct inode *inode;
1090 int error;
1093 * Hardlinks are often used in delicate situations. We avoid
1094 * security-related surprises by not following symlinks on the
1095 * newname. --KAB
1097 * We don't follow them on the oldname either to be compatible
1098 * with linux 2.0, and to avoid hard-linking to directories
1099 * and other special files. --ADM
1101 old_dentry = lookup_dentry(oldname, NULL, 0);
1102 error = PTR_ERR(old_dentry);
1103 if (IS_ERR(old_dentry))
1104 goto exit;
1106 new_dentry = lookup_dentry(newname, NULL, 0);
1107 error = PTR_ERR(new_dentry);
1108 if (IS_ERR(new_dentry))
1109 goto exit_old;
1111 dir = lock_parent(new_dentry);
1112 error = -ENOENT;
1113 if (!check_parent(dir, new_dentry))
1114 goto exit_lock;
1116 error = -ENOENT;
1117 inode = old_dentry->d_inode;
1118 if (!inode)
1119 goto exit_lock;
1121 error = may_create(dir->d_inode, new_dentry);
1122 if (error)
1123 goto exit_lock;
1125 error = -EXDEV;
1126 if (dir->d_inode->i_dev != inode->i_dev)
1127 goto exit_lock;
1130 * A link to an append-only or immutable file cannot be created.
1132 error = -EPERM;
1133 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1134 goto exit_lock;
1136 error = -EPERM;
1137 if (!dir->d_inode->i_op || !dir->d_inode->i_op->link)
1138 goto exit_lock;
1140 DQUOT_INIT(dir->d_inode);
1141 error = dir->d_inode->i_op->link(old_dentry, dir->d_inode, new_dentry);
1143 exit_lock:
1144 unlock_dir(dir);
1145 dput(new_dentry);
1146 exit_old:
1147 dput(old_dentry);
1148 exit:
1149 return error;
1152 asmlinkage long sys_link(const char * oldname, const char * newname)
1154 int error;
1155 char * from;
1156 char * to;
1158 from = getname(oldname);
1159 if(IS_ERR(from))
1160 return PTR_ERR(from);
1161 to = getname(newname);
1162 error = PTR_ERR(to);
1163 if (!IS_ERR(to)) {
1164 lock_kernel();
1165 error = do_link(from,to);
1166 unlock_kernel();
1167 putname(to);
1169 putname(from);
1171 return error;
1174 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
1175 struct inode *new_dir, struct dentry *new_dentry)
1177 int error;
1178 int need_rehash = 0;
1180 if (old_dentry->d_inode == new_dentry->d_inode)
1181 return 0;
1183 error = may_delete(old_dir, old_dentry, 1);
1184 if (error)
1185 return error;
1187 if (new_dir->i_dev != old_dir->i_dev)
1188 return -EXDEV;
1190 if (!new_dentry->d_inode)
1191 error = may_create(new_dir, new_dentry);
1192 else
1193 error = may_delete(new_dir, new_dentry, 1);
1194 if (error)
1195 return error;
1197 if (!old_dir->i_op || !old_dir->i_op->rename)
1198 return -EPERM;
1201 * If we are going to change the parent - check write permissions,
1202 * we'll need to flip '..'.
1204 if (new_dir != old_dir) {
1205 error = permission(old_dentry->d_inode, MAY_WRITE);
1207 if (error)
1208 return error;
1210 DQUOT_INIT(old_dir);
1211 DQUOT_INIT(new_dir);
1212 down(&old_dir->i_sb->s_vfs_rename_sem);
1213 error = -EINVAL;
1214 if (is_subdir(new_dentry, old_dentry))
1215 goto out_unlock;
1216 if (new_dentry->d_inode) {
1217 error = -EBUSY;
1218 if (d_invalidate(new_dentry)<0)
1219 goto out_unlock;
1220 need_rehash = 1;
1222 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1223 if (need_rehash)
1224 d_rehash(new_dentry);
1225 if (!error)
1226 d_move(old_dentry,new_dentry);
1227 out_unlock:
1228 up(&old_dir->i_sb->s_vfs_rename_sem);
1229 return error;
1232 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
1233 struct inode *new_dir, struct dentry *new_dentry)
1235 int error;
1237 if (old_dentry->d_inode == new_dentry->d_inode)
1238 return 0;
1240 error = may_delete(old_dir, old_dentry, 0);
1241 if (error)
1242 return error;
1244 if (new_dir->i_dev != old_dir->i_dev)
1245 return -EXDEV;
1247 if (!new_dentry->d_inode)
1248 error = may_create(new_dir, new_dentry);
1249 else
1250 error = may_delete(new_dir, new_dentry, 0);
1251 if (error)
1252 return error;
1254 if (!old_dir->i_op || !old_dir->i_op->rename)
1255 return -EPERM;
1257 DQUOT_INIT(old_dir);
1258 DQUOT_INIT(new_dir);
1259 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1260 if (error)
1261 return error;
1262 /* The following d_move() should become unconditional */
1263 if (!(old_dir->i_sb->s_flags & MS_ODD_RENAME)) {
1264 d_move(old_dentry, new_dentry);
1266 return 0;
1269 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1270 struct inode *new_dir, struct dentry *new_dentry)
1272 if (S_ISDIR(old_dentry->d_inode->i_mode))
1273 return vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
1274 else
1275 return vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
1278 static inline int do_rename(const char * oldname, const char * newname)
1280 int error;
1281 struct dentry * old_dir, * new_dir;
1282 struct dentry * old_dentry, *new_dentry;
1284 old_dentry = lookup_dentry(oldname, NULL, 0);
1286 error = PTR_ERR(old_dentry);
1287 if (IS_ERR(old_dentry))
1288 goto exit;
1290 error = -ENOENT;
1291 if (!old_dentry->d_inode)
1292 goto exit_old;
1295 unsigned int flags = 0;
1296 if (S_ISDIR(old_dentry->d_inode->i_mode))
1297 flags = LOOKUP_SLASHOK;
1298 new_dentry = lookup_dentry(newname, NULL, flags);
1301 error = PTR_ERR(new_dentry);
1302 if (IS_ERR(new_dentry))
1303 goto exit_old;
1305 new_dir = get_parent(new_dentry);
1306 old_dir = get_parent(old_dentry);
1308 double_lock(new_dir, old_dir);
1310 error = -ENOENT;
1311 if (check_parent(old_dir, old_dentry) && check_parent(new_dir, new_dentry))
1312 error = vfs_rename(old_dir->d_inode, old_dentry,
1313 new_dir->d_inode, new_dentry);
1315 double_unlock(new_dir, old_dir);
1316 dput(new_dentry);
1317 exit_old:
1318 dput(old_dentry);
1319 exit:
1320 return error;
1323 asmlinkage long sys_rename(const char * oldname, const char * newname)
1325 int error;
1326 char * from;
1327 char * to;
1329 from = getname(oldname);
1330 if(IS_ERR(from))
1331 return PTR_ERR(from);
1332 to = getname(newname);
1333 error = PTR_ERR(to);
1334 if (!IS_ERR(to)) {
1335 lock_kernel();
1336 error = do_rename(from,to);
1337 unlock_kernel();
1338 putname(to);
1340 putname(from);
1341 return error;
1344 int vfs_readlink(struct dentry *dentry, char *buffer, int buflen, const char *link)
1346 u32 len;
1348 len = PTR_ERR(link);
1349 if (IS_ERR(link))
1350 goto out;
1352 len = strlen(link);
1353 if (len > buflen)
1354 len = buflen;
1355 copy_to_user(buffer, link, len);
1356 out:
1357 return len;
1360 static inline struct dentry *
1361 __vfs_follow_link(struct dentry *dentry, struct dentry *base,
1362 unsigned follow, const char *link)
1364 struct dentry *result;
1365 UPDATE_ATIME(dentry->d_inode);
1367 if (IS_ERR(link))
1368 goto fail;
1370 result = lookup_dentry(link, base, follow);
1371 return result;
1373 fail:
1374 dput(base);
1375 return (struct dentry *)link;
1378 struct dentry *
1379 vfs_follow_link(struct dentry *dentry, struct dentry *base,
1380 unsigned int follow, const char *link)
1382 return __vfs_follow_link(dentry,base,follow,link);
1385 /* get the link contents into pagecache */
1386 static char *page_getlink(struct dentry * dentry, struct page **ppage)
1388 struct page * page;
1389 struct address_space *mapping = dentry->d_inode->i_mapping;
1390 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
1391 dentry);
1392 if (IS_ERR(page))
1393 goto sync_fail;
1394 wait_on_page(page);
1395 if (!Page_Uptodate(page))
1396 goto async_fail;
1397 *ppage = page;
1398 return (char*) kmap(page);
1400 async_fail:
1401 page_cache_release(page);
1402 return ERR_PTR(-EIO);
1404 sync_fail:
1405 return (char*)page;
1408 int page_readlink(struct dentry *dentry, char *buffer, int buflen)
1410 struct page *page = NULL;
1411 char *s = page_getlink(dentry, &page);
1412 int res = vfs_readlink(dentry,buffer,buflen,s);
1413 if (page) {
1414 kunmap(page);
1415 page_cache_release(page);
1417 return res;
1420 struct dentry *
1421 page_follow_link(struct dentry *dentry, struct dentry *base, unsigned int follow)
1423 struct page *page = NULL;
1424 char *s = page_getlink(dentry, &page);
1425 struct dentry *res = __vfs_follow_link(dentry,base,follow,s);
1426 if (page) {
1427 kunmap(page);
1428 page_cache_release(page);
1430 return res;
1433 struct inode_operations page_symlink_inode_operations = {
1434 readlink: page_readlink,
1435 follow_link: page_follow_link,