[PATCH] DVB: Documentation and Kconfig updazes
[linux-2.6/history.git] / fs / namei.c
blobe6320d133c5f34078811043beffcd51575f831ce
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/module.h>
19 #include <linux/slab.h>
20 #include <linux/fs.h>
21 #include <linux/namei.h>
22 #include <linux/quotaops.h>
23 #include <linux/pagemap.h>
24 #include <linux/dnotify.h>
25 #include <linux/smp_lock.h>
26 #include <linux/personality.h>
27 #include <linux/security.h>
28 #include <linux/mount.h>
29 #include <asm/namei.h>
30 #include <asm/uaccess.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 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
101 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
102 * any extra contention...
105 /* In order to reduce some races, while at the same time doing additional
106 * checking and hopefully speeding things up, we copy filenames to the
107 * kernel data space before using them..
109 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
110 * PATH_MAX includes the nul terminator --RR.
112 static inline int do_getname(const char __user *filename, char *page)
114 int retval;
115 unsigned long len = PATH_MAX;
117 if ((unsigned long) filename >= TASK_SIZE) {
118 if (!segment_eq(get_fs(), KERNEL_DS))
119 return -EFAULT;
120 } else if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
121 len = TASK_SIZE - (unsigned long) filename;
123 retval = strncpy_from_user((char *)page, filename, len);
124 if (retval > 0) {
125 if (retval < len)
126 return 0;
127 return -ENAMETOOLONG;
128 } else if (!retval)
129 retval = -ENOENT;
130 return retval;
133 char * getname(const char __user * filename)
135 char *tmp, *result;
137 result = ERR_PTR(-ENOMEM);
138 tmp = __getname();
139 if (tmp) {
140 int retval = do_getname(filename, tmp);
142 result = tmp;
143 if (retval < 0) {
144 putname(tmp);
145 result = ERR_PTR(retval);
148 return result;
152 * vfs_permission()
154 * is used to check for read/write/execute permissions on a file.
155 * We use "fsuid" for this, letting us set arbitrary permissions
156 * for filesystem access without changing the "normal" uids which
157 * are used for other things..
159 int vfs_permission(struct inode * inode, int mask)
161 umode_t mode = inode->i_mode;
163 if (mask & MAY_WRITE) {
165 * Nobody gets write access to a read-only fs.
167 if (IS_RDONLY(inode) &&
168 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
169 return -EROFS;
172 * Nobody gets write access to an immutable file.
174 if (IS_IMMUTABLE(inode))
175 return -EACCES;
178 if (current->fsuid == inode->i_uid)
179 mode >>= 6;
180 else if (in_group_p(inode->i_gid))
181 mode >>= 3;
184 * If the DACs are ok we don't need any capability check.
186 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
187 return 0;
190 * Read/write DACs are always overridable.
191 * Executable DACs are overridable if at least one exec bit is set.
193 if (!(mask & MAY_EXEC) ||
194 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
195 if (capable(CAP_DAC_OVERRIDE))
196 return 0;
199 * Searching includes executable on directories, else just read.
201 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
202 if (capable(CAP_DAC_READ_SEARCH))
203 return 0;
205 return -EACCES;
208 int permission(struct inode * inode,int mask, struct nameidata *nd)
210 int retval;
211 int submask;
213 /* Ordinary permission routines do not understand MAY_APPEND. */
214 submask = mask & ~MAY_APPEND;
216 if (inode->i_op && inode->i_op->permission)
217 retval = inode->i_op->permission(inode, submask, nd);
218 else
219 retval = vfs_permission(inode, submask);
220 if (retval)
221 return retval;
223 return security_inode_permission(inode, mask, nd);
227 * get_write_access() gets write permission for a file.
228 * put_write_access() releases this write permission.
229 * This is used for regular files.
230 * We cannot support write (and maybe mmap read-write shared) accesses and
231 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
232 * can have the following values:
233 * 0: no writers, no VM_DENYWRITE mappings
234 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
235 * > 0: (i_writecount) users are writing to the file.
237 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
238 * except for the cases where we don't hold i_writecount yet. Then we need to
239 * use {get,deny}_write_access() - these functions check the sign and refuse
240 * to do the change if sign is wrong. Exclusion between them is provided by
241 * the inode->i_lock spinlock.
244 int get_write_access(struct inode * inode)
246 spin_lock(&inode->i_lock);
247 if (atomic_read(&inode->i_writecount) < 0) {
248 spin_unlock(&inode->i_lock);
249 return -ETXTBSY;
251 atomic_inc(&inode->i_writecount);
252 spin_unlock(&inode->i_lock);
254 return 0;
257 int deny_write_access(struct file * file)
259 struct inode *inode = file->f_dentry->d_inode;
261 spin_lock(&inode->i_lock);
262 if (atomic_read(&inode->i_writecount) > 0) {
263 spin_unlock(&inode->i_lock);
264 return -ETXTBSY;
266 atomic_dec(&inode->i_writecount);
267 spin_unlock(&inode->i_lock);
269 return 0;
272 void path_release(struct nameidata *nd)
274 dput(nd->dentry);
275 mntput(nd->mnt);
279 * Internal lookup() using the new generic dcache.
280 * SMP-safe
282 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
284 struct dentry * dentry = __d_lookup(parent, name);
286 /* lockess __d_lookup may fail due to concurrent d_move()
287 * in some unrelated directory, so try with d_lookup
289 if (!dentry)
290 dentry = d_lookup(parent, name);
292 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
293 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
294 dput(dentry);
295 dentry = NULL;
298 return dentry;
302 * Short-cut version of permission(), for calling by
303 * path_walk(), when dcache lock is held. Combines parts
304 * of permission() and vfs_permission(), and tests ONLY for
305 * MAY_EXEC permission.
307 * If appropriate, check DAC only. If not appropriate, or
308 * short-cut DAC fails, then call permission() to do more
309 * complete permission check.
311 static inline int exec_permission_lite(struct inode *inode,
312 struct nameidata *nd)
314 umode_t mode = inode->i_mode;
316 if ((inode->i_op && inode->i_op->permission))
317 return -EAGAIN;
319 if (current->fsuid == inode->i_uid)
320 mode >>= 6;
321 else if (in_group_p(inode->i_gid))
322 mode >>= 3;
324 if (mode & MAY_EXEC)
325 goto ok;
327 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
328 goto ok;
330 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
331 goto ok;
333 return -EACCES;
335 return security_inode_permission(inode, MAY_EXEC, nd);
339 * This is called when everything else fails, and we actually have
340 * to go to the low-level filesystem to find out what we should do..
342 * We get the directory semaphore, and after getting that we also
343 * make sure that nobody added the entry to the dcache in the meantime..
344 * SMP-safe
346 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
348 struct dentry * result;
349 struct inode *dir = parent->d_inode;
351 down(&dir->i_sem);
353 * First re-do the cached lookup just in case it was created
354 * while we waited for the directory semaphore..
356 * FIXME! This could use version numbering or similar to
357 * avoid unnecessary cache lookups.
359 * The "dcache_lock" is purely to protect the RCU list walker
360 * from concurrent renames at this point (we mustn't get false
361 * negatives from the RCU list walk here, unlike the optimistic
362 * fast walk).
364 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
366 result = d_lookup(parent, name);
367 if (!result) {
368 struct dentry * dentry = d_alloc(parent, name);
369 result = ERR_PTR(-ENOMEM);
370 if (dentry) {
371 result = dir->i_op->lookup(dir, dentry, nd);
372 if (result)
373 dput(dentry);
374 else
375 result = dentry;
377 up(&dir->i_sem);
378 return result;
382 * Uhhuh! Nasty case: the cache was re-populated while
383 * we waited on the semaphore. Need to revalidate.
385 up(&dir->i_sem);
386 if (result->d_op && result->d_op->d_revalidate) {
387 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
388 dput(result);
389 result = ERR_PTR(-ENOENT);
392 return result;
396 * This limits recursive symlink follows to 8, while
397 * limiting consecutive symlinks to 40.
399 * Without that kind of total limit, nasty chains of consecutive
400 * symlinks can cause almost arbitrarily long lookups.
402 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
404 int err = -ELOOP;
405 if (current->link_count >= 5)
406 goto loop;
407 if (current->total_link_count >= 40)
408 goto loop;
409 cond_resched();
410 err = security_inode_follow_link(dentry, nd);
411 if (err)
412 goto loop;
413 current->link_count++;
414 current->total_link_count++;
415 touch_atime(nd->mnt, dentry);
416 err = dentry->d_inode->i_op->follow_link(dentry, nd);
417 current->link_count--;
418 return err;
419 loop:
420 path_release(nd);
421 return err;
424 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
426 struct vfsmount *parent;
427 struct dentry *mountpoint;
428 spin_lock(&vfsmount_lock);
429 parent=(*mnt)->mnt_parent;
430 if (parent == *mnt) {
431 spin_unlock(&vfsmount_lock);
432 return 0;
434 mntget(parent);
435 mountpoint=dget((*mnt)->mnt_mountpoint);
436 spin_unlock(&vfsmount_lock);
437 dput(*dentry);
438 *dentry = mountpoint;
439 mntput(*mnt);
440 *mnt = parent;
441 return 1;
444 /* no need for dcache_lock, as serialization is taken care in
445 * namespace.c
447 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
449 int res = 0;
450 while (d_mountpoint(*dentry)) {
451 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
452 if (!mounted)
453 break;
454 mntput(*mnt);
455 *mnt = mounted;
456 dput(*dentry);
457 *dentry = dget(mounted->mnt_root);
458 res = 1;
460 return res;
463 /* no need for dcache_lock, as serialization is taken care in
464 * namespace.c
466 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
468 struct vfsmount *mounted;
470 mounted = lookup_mnt(*mnt, *dentry);
471 if (mounted) {
472 mntput(*mnt);
473 *mnt = mounted;
474 dput(*dentry);
475 *dentry = dget(mounted->mnt_root);
476 return 1;
478 return 0;
481 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
483 return __follow_down(mnt,dentry);
486 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
488 while(1) {
489 struct vfsmount *parent;
490 struct dentry *old = *dentry;
492 read_lock(&current->fs->lock);
493 if (*dentry == current->fs->root &&
494 *mnt == current->fs->rootmnt) {
495 read_unlock(&current->fs->lock);
496 break;
498 read_unlock(&current->fs->lock);
499 spin_lock(&dcache_lock);
500 if (*dentry != (*mnt)->mnt_root) {
501 *dentry = dget((*dentry)->d_parent);
502 spin_unlock(&dcache_lock);
503 dput(old);
504 break;
506 spin_unlock(&dcache_lock);
507 spin_lock(&vfsmount_lock);
508 parent = (*mnt)->mnt_parent;
509 if (parent == *mnt) {
510 spin_unlock(&vfsmount_lock);
511 break;
513 mntget(parent);
514 *dentry = dget((*mnt)->mnt_mountpoint);
515 spin_unlock(&vfsmount_lock);
516 dput(old);
517 mntput(*mnt);
518 *mnt = parent;
520 follow_mount(mnt, dentry);
523 struct path {
524 struct vfsmount *mnt;
525 struct dentry *dentry;
529 * It's more convoluted than I'd like it to be, but... it's still fairly
530 * small and for now I'd prefer to have fast path as straight as possible.
531 * It _is_ time-critical.
533 static int do_lookup(struct nameidata *nd, struct qstr *name,
534 struct path *path)
536 struct vfsmount *mnt = nd->mnt;
537 struct dentry *dentry = __d_lookup(nd->dentry, name);
539 if (!dentry)
540 goto need_lookup;
541 if (dentry->d_op && dentry->d_op->d_revalidate)
542 goto need_revalidate;
543 done:
544 path->mnt = mnt;
545 path->dentry = dentry;
546 return 0;
548 need_lookup:
549 dentry = real_lookup(nd->dentry, name, nd);
550 if (IS_ERR(dentry))
551 goto fail;
552 goto done;
554 need_revalidate:
555 if (dentry->d_op->d_revalidate(dentry, nd))
556 goto done;
557 if (d_invalidate(dentry))
558 goto done;
559 dput(dentry);
560 goto need_lookup;
562 fail:
563 return PTR_ERR(dentry);
567 * Name resolution.
569 * This is the basic name resolution function, turning a pathname
570 * into the final dentry.
572 * We expect 'base' to be positive and a directory.
574 int fastcall link_path_walk(const char * name, struct nameidata *nd)
576 struct path next;
577 struct inode *inode;
578 int err;
579 unsigned int lookup_flags = nd->flags;
581 while (*name=='/')
582 name++;
583 if (!*name)
584 goto return_reval;
586 inode = nd->dentry->d_inode;
587 if (current->link_count)
588 lookup_flags = LOOKUP_FOLLOW;
590 /* At this point we know we have a real path component. */
591 for(;;) {
592 unsigned long hash;
593 struct qstr this;
594 unsigned int c;
596 err = exec_permission_lite(inode, nd);
597 if (err == -EAGAIN) {
598 err = permission(inode, MAY_EXEC, nd);
600 if (err)
601 break;
603 this.name = name;
604 c = *(const unsigned char *)name;
606 hash = init_name_hash();
607 do {
608 name++;
609 hash = partial_name_hash(c, hash);
610 c = *(const unsigned char *)name;
611 } while (c && (c != '/'));
612 this.len = name - (const char *) this.name;
613 this.hash = end_name_hash(hash);
615 /* remove trailing slashes? */
616 if (!c)
617 goto last_component;
618 while (*++name == '/');
619 if (!*name)
620 goto last_with_slashes;
623 * "." and ".." are special - ".." especially so because it has
624 * to be able to know about the current root directory and
625 * parent relationships.
627 if (this.name[0] == '.') switch (this.len) {
628 default:
629 break;
630 case 2:
631 if (this.name[1] != '.')
632 break;
633 follow_dotdot(&nd->mnt, &nd->dentry);
634 inode = nd->dentry->d_inode;
635 /* fallthrough */
636 case 1:
637 continue;
640 * See if the low-level filesystem might want
641 * to use its own hash..
643 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
644 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
645 if (err < 0)
646 break;
648 nd->flags |= LOOKUP_CONTINUE;
649 /* This does the actual lookups.. */
650 err = do_lookup(nd, &this, &next);
651 if (err)
652 break;
653 /* Check mountpoints.. */
654 follow_mount(&next.mnt, &next.dentry);
656 err = -ENOENT;
657 inode = next.dentry->d_inode;
658 if (!inode)
659 goto out_dput;
660 err = -ENOTDIR;
661 if (!inode->i_op)
662 goto out_dput;
664 if (inode->i_op->follow_link) {
665 mntget(next.mnt);
666 err = do_follow_link(next.dentry, nd);
667 dput(next.dentry);
668 mntput(next.mnt);
669 if (err)
670 goto return_err;
671 err = -ENOENT;
672 inode = nd->dentry->d_inode;
673 if (!inode)
674 break;
675 err = -ENOTDIR;
676 if (!inode->i_op)
677 break;
678 } else {
679 dput(nd->dentry);
680 nd->mnt = next.mnt;
681 nd->dentry = next.dentry;
683 err = -ENOTDIR;
684 if (!inode->i_op->lookup)
685 break;
686 continue;
687 /* here ends the main loop */
689 last_with_slashes:
690 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
691 last_component:
692 nd->flags &= ~LOOKUP_CONTINUE;
693 if (lookup_flags & LOOKUP_PARENT)
694 goto lookup_parent;
695 if (this.name[0] == '.') switch (this.len) {
696 default:
697 break;
698 case 2:
699 if (this.name[1] != '.')
700 break;
701 follow_dotdot(&nd->mnt, &nd->dentry);
702 inode = nd->dentry->d_inode;
703 /* fallthrough */
704 case 1:
705 goto return_reval;
707 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
708 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
709 if (err < 0)
710 break;
712 err = do_lookup(nd, &this, &next);
713 if (err)
714 break;
715 follow_mount(&next.mnt, &next.dentry);
716 inode = next.dentry->d_inode;
717 if ((lookup_flags & LOOKUP_FOLLOW)
718 && inode && inode->i_op && inode->i_op->follow_link) {
719 mntget(next.mnt);
720 err = do_follow_link(next.dentry, nd);
721 dput(next.dentry);
722 mntput(next.mnt);
723 if (err)
724 goto return_err;
725 inode = nd->dentry->d_inode;
726 } else {
727 dput(nd->dentry);
728 nd->mnt = next.mnt;
729 nd->dentry = next.dentry;
731 err = -ENOENT;
732 if (!inode)
733 break;
734 if (lookup_flags & LOOKUP_DIRECTORY) {
735 err = -ENOTDIR;
736 if (!inode->i_op || !inode->i_op->lookup)
737 break;
739 goto return_base;
740 lookup_parent:
741 nd->last = this;
742 nd->last_type = LAST_NORM;
743 if (this.name[0] != '.')
744 goto return_base;
745 if (this.len == 1)
746 nd->last_type = LAST_DOT;
747 else if (this.len == 2 && this.name[1] == '.')
748 nd->last_type = LAST_DOTDOT;
749 else
750 goto return_base;
751 return_reval:
753 * We bypassed the ordinary revalidation routines.
754 * We may need to check the cached dentry for staleness.
756 if (nd->dentry && nd->dentry->d_sb &&
757 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
758 err = -ESTALE;
759 /* Note: we do not d_invalidate() */
760 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
761 break;
763 return_base:
764 return 0;
765 out_dput:
766 dput(next.dentry);
767 break;
769 path_release(nd);
770 return_err:
771 return err;
774 int fastcall path_walk(const char * name, struct nameidata *nd)
776 current->total_link_count = 0;
777 return link_path_walk(name, nd);
780 /* SMP-safe */
781 /* returns 1 if everything is done */
782 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
784 if (path_walk(name, nd))
785 return 0; /* something went wrong... */
787 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
788 struct nameidata nd_root;
790 * NAME was not found in alternate root or it's a directory. Try to find
791 * it in the normal root:
793 nd_root.last_type = LAST_ROOT;
794 nd_root.flags = nd->flags;
795 memcpy(&nd_root.intent, &nd->intent, sizeof(nd_root.intent));
796 read_lock(&current->fs->lock);
797 nd_root.mnt = mntget(current->fs->rootmnt);
798 nd_root.dentry = dget(current->fs->root);
799 read_unlock(&current->fs->lock);
800 if (path_walk(name, &nd_root))
801 return 1;
802 if (nd_root.dentry->d_inode) {
803 path_release(nd);
804 nd->dentry = nd_root.dentry;
805 nd->mnt = nd_root.mnt;
806 nd->last = nd_root.last;
807 return 1;
809 path_release(&nd_root);
811 return 1;
814 void set_fs_altroot(void)
816 char *emul = __emul_prefix();
817 struct nameidata nd;
818 struct vfsmount *mnt = NULL, *oldmnt;
819 struct dentry *dentry = NULL, *olddentry;
820 int err;
822 if (!emul)
823 goto set_it;
824 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
825 if (!err) {
826 mnt = nd.mnt;
827 dentry = nd.dentry;
829 set_it:
830 write_lock(&current->fs->lock);
831 oldmnt = current->fs->altrootmnt;
832 olddentry = current->fs->altroot;
833 current->fs->altrootmnt = mnt;
834 current->fs->altroot = dentry;
835 write_unlock(&current->fs->lock);
836 if (olddentry) {
837 dput(olddentry);
838 mntput(oldmnt);
842 /* SMP-safe */
843 static inline int
844 walk_init_root(const char *name, struct nameidata *nd)
846 read_lock(&current->fs->lock);
847 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
848 nd->mnt = mntget(current->fs->altrootmnt);
849 nd->dentry = dget(current->fs->altroot);
850 read_unlock(&current->fs->lock);
851 if (__emul_lookup_dentry(name,nd))
852 return 0;
853 read_lock(&current->fs->lock);
855 nd->mnt = mntget(current->fs->rootmnt);
856 nd->dentry = dget(current->fs->root);
857 read_unlock(&current->fs->lock);
858 return 1;
861 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
863 nd->last_type = LAST_ROOT; /* if there are only slashes... */
864 nd->flags = flags;
866 read_lock(&current->fs->lock);
867 if (*name=='/') {
868 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
869 nd->mnt = mntget(current->fs->altrootmnt);
870 nd->dentry = dget(current->fs->altroot);
871 read_unlock(&current->fs->lock);
872 if (__emul_lookup_dentry(name,nd))
873 return 0;
874 read_lock(&current->fs->lock);
876 nd->mnt = mntget(current->fs->rootmnt);
877 nd->dentry = dget(current->fs->root);
879 else{
880 nd->mnt = mntget(current->fs->pwdmnt);
881 nd->dentry = dget(current->fs->pwd);
883 read_unlock(&current->fs->lock);
884 current->total_link_count = 0;
885 return link_path_walk(name, nd);
889 * Restricted form of lookup. Doesn't follow links, single-component only,
890 * needs parent already locked. Doesn't follow mounts.
891 * SMP-safe.
893 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
895 struct dentry * dentry;
896 struct inode *inode;
897 int err;
899 inode = base->d_inode;
900 err = permission(inode, MAY_EXEC, nd);
901 dentry = ERR_PTR(err);
902 if (err)
903 goto out;
906 * See if the low-level filesystem might want
907 * to use its own hash..
909 if (base->d_op && base->d_op->d_hash) {
910 err = base->d_op->d_hash(base, name);
911 dentry = ERR_PTR(err);
912 if (err < 0)
913 goto out;
916 dentry = cached_lookup(base, name, nd);
917 if (!dentry) {
918 struct dentry *new = d_alloc(base, name);
919 dentry = ERR_PTR(-ENOMEM);
920 if (!new)
921 goto out;
922 dentry = inode->i_op->lookup(inode, new, nd);
923 if (!dentry)
924 dentry = new;
925 else
926 dput(new);
928 out:
929 return dentry;
932 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
934 return __lookup_hash(name, base, NULL);
937 /* SMP-safe */
938 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
940 unsigned long hash;
941 struct qstr this;
942 unsigned int c;
944 this.name = name;
945 this.len = len;
946 if (!len)
947 goto access;
949 hash = init_name_hash();
950 while (len--) {
951 c = *(const unsigned char *)name++;
952 if (c == '/' || c == '\0')
953 goto access;
954 hash = partial_name_hash(c, hash);
956 this.hash = end_name_hash(hash);
958 return lookup_hash(&this, base);
959 access:
960 return ERR_PTR(-EACCES);
964 * namei()
966 * is used by most simple commands to get the inode of a specified name.
967 * Open, link etc use their own routines, but this is enough for things
968 * like 'chmod' etc.
970 * namei exists in two versions: namei/lnamei. The only difference is
971 * that namei follows links, while lnamei does not.
972 * SMP-safe
974 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
976 char *tmp = getname(name);
977 int err = PTR_ERR(tmp);
979 if (!IS_ERR(tmp)) {
980 err = path_lookup(tmp, flags, nd);
981 putname(tmp);
983 return err;
987 * It's inline, so penalty for filesystems that don't use sticky bit is
988 * minimal.
990 static inline int check_sticky(struct inode *dir, struct inode *inode)
992 if (!(dir->i_mode & S_ISVTX))
993 return 0;
994 if (inode->i_uid == current->fsuid)
995 return 0;
996 if (dir->i_uid == current->fsuid)
997 return 0;
998 return !capable(CAP_FOWNER);
1002 * Check whether we can remove a link victim from directory dir, check
1003 * whether the type of victim is right.
1004 * 1. We can't do it if dir is read-only (done in permission())
1005 * 2. We should have write and exec permissions on dir
1006 * 3. We can't remove anything from append-only dir
1007 * 4. We can't do anything with immutable dir (done in permission())
1008 * 5. If the sticky bit on dir is set we should either
1009 * a. be owner of dir, or
1010 * b. be owner of victim, or
1011 * c. have CAP_FOWNER capability
1012 * 6. If the victim is append-only or immutable we can't do antyhing with
1013 * links pointing to it.
1014 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1015 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1016 * 9. We can't remove a root or mountpoint.
1017 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1018 * nfs_async_unlink().
1020 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1022 int error;
1023 if (!victim->d_inode || victim->d_parent->d_inode != dir)
1024 return -ENOENT;
1025 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1026 if (error)
1027 return error;
1028 if (IS_APPEND(dir))
1029 return -EPERM;
1030 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1031 IS_IMMUTABLE(victim->d_inode))
1032 return -EPERM;
1033 if (isdir) {
1034 if (!S_ISDIR(victim->d_inode->i_mode))
1035 return -ENOTDIR;
1036 if (IS_ROOT(victim))
1037 return -EBUSY;
1038 } else if (S_ISDIR(victim->d_inode->i_mode))
1039 return -EISDIR;
1040 if (IS_DEADDIR(dir))
1041 return -ENOENT;
1042 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1043 return -EBUSY;
1044 return 0;
1047 /* Check whether we can create an object with dentry child in directory
1048 * dir.
1049 * 1. We can't do it if child already exists (open has special treatment for
1050 * this case, but since we are inlined it's OK)
1051 * 2. We can't do it if dir is read-only (done in permission())
1052 * 3. We should have write and exec permissions on dir
1053 * 4. We can't do it if dir is immutable (done in permission())
1055 static inline int may_create(struct inode *dir, struct dentry *child,
1056 struct nameidata *nd)
1058 if (child->d_inode)
1059 return -EEXIST;
1060 if (IS_DEADDIR(dir))
1061 return -ENOENT;
1062 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1066 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1067 * reasons.
1069 * O_DIRECTORY translates into forcing a directory lookup.
1071 static inline int lookup_flags(unsigned int f)
1073 unsigned long retval = LOOKUP_FOLLOW;
1075 if (f & O_NOFOLLOW)
1076 retval &= ~LOOKUP_FOLLOW;
1078 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1079 retval &= ~LOOKUP_FOLLOW;
1081 if (f & O_DIRECTORY)
1082 retval |= LOOKUP_DIRECTORY;
1084 return retval;
1088 * p1 and p2 should be directories on the same fs.
1090 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1092 struct dentry *p;
1094 if (p1 == p2) {
1095 down(&p1->d_inode->i_sem);
1096 return NULL;
1099 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1101 for (p = p1; p->d_parent != p; p = p->d_parent) {
1102 if (p->d_parent == p2) {
1103 down(&p2->d_inode->i_sem);
1104 down(&p1->d_inode->i_sem);
1105 return p;
1109 for (p = p2; p->d_parent != p; p = p->d_parent) {
1110 if (p->d_parent == p1) {
1111 down(&p1->d_inode->i_sem);
1112 down(&p2->d_inode->i_sem);
1113 return p;
1117 down(&p1->d_inode->i_sem);
1118 down(&p2->d_inode->i_sem);
1119 return NULL;
1122 void unlock_rename(struct dentry *p1, struct dentry *p2)
1124 up(&p1->d_inode->i_sem);
1125 if (p1 != p2) {
1126 up(&p2->d_inode->i_sem);
1127 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1131 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1132 struct nameidata *nd)
1134 int error = may_create(dir, dentry, nd);
1136 if (error)
1137 return error;
1139 if (!dir->i_op || !dir->i_op->create)
1140 return -EACCES; /* shouldn't it be ENOSYS? */
1141 mode &= S_IALLUGO;
1142 mode |= S_IFREG;
1143 error = security_inode_create(dir, dentry, mode);
1144 if (error)
1145 return error;
1146 DQUOT_INIT(dir);
1147 error = dir->i_op->create(dir, dentry, mode, nd);
1148 if (!error) {
1149 inode_dir_notify(dir, DN_CREATE);
1150 security_inode_post_create(dir, dentry, mode);
1152 return error;
1155 int may_open(struct nameidata *nd, int acc_mode, int flag)
1157 struct dentry *dentry = nd->dentry;
1158 struct inode *inode = dentry->d_inode;
1159 int error;
1161 if (!inode)
1162 return -ENOENT;
1164 if (S_ISLNK(inode->i_mode))
1165 return -ELOOP;
1167 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1168 return -EISDIR;
1170 error = permission(inode, acc_mode, nd);
1171 if (error)
1172 return error;
1175 * FIFO's, sockets and device files are special: they don't
1176 * actually live on the filesystem itself, and as such you
1177 * can write to them even if the filesystem is read-only.
1179 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1180 flag &= ~O_TRUNC;
1181 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1182 if (nd->mnt->mnt_flags & MNT_NODEV)
1183 return -EACCES;
1185 flag &= ~O_TRUNC;
1186 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1187 return -EROFS;
1189 * An append-only file must be opened in append mode for writing.
1191 if (IS_APPEND(inode)) {
1192 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1193 return -EPERM;
1194 if (flag & O_TRUNC)
1195 return -EPERM;
1199 * Ensure there are no outstanding leases on the file.
1201 error = break_lease(inode, flag);
1202 if (error)
1203 return error;
1205 if (flag & O_TRUNC) {
1206 error = get_write_access(inode);
1207 if (error)
1208 return error;
1211 * Refuse to truncate files with mandatory locks held on them.
1213 error = locks_verify_locked(inode);
1214 if (!error) {
1215 DQUOT_INIT(inode);
1217 error = do_truncate(dentry, 0);
1219 put_write_access(inode);
1220 if (error)
1221 return error;
1222 } else
1223 if (flag & FMODE_WRITE)
1224 DQUOT_INIT(inode);
1226 return 0;
1230 * open_namei()
1232 * namei for open - this is in fact almost the whole open-routine.
1234 * Note that the low bits of "flag" aren't the same as in the open
1235 * system call - they are 00 - no permissions needed
1236 * 01 - read permission needed
1237 * 10 - write permission needed
1238 * 11 - read/write permissions needed
1239 * which is a lot more logical, and also allows the "no perm" needed
1240 * for symlinks (where the permissions are checked later).
1241 * SMP-safe
1243 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1245 int acc_mode, error = 0;
1246 struct dentry *dentry;
1247 struct dentry *dir;
1248 int count = 0;
1250 acc_mode = ACC_MODE(flag);
1252 /* Allow the LSM permission hook to distinguish append
1253 access from general write access. */
1254 if (flag & O_APPEND)
1255 acc_mode |= MAY_APPEND;
1257 /* Fill in the open() intent data */
1258 nd->intent.open.flags = flag;
1259 nd->intent.open.create_mode = mode;
1262 * The simplest case - just a plain lookup.
1264 if (!(flag & O_CREAT)) {
1265 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1266 if (error)
1267 return error;
1268 goto ok;
1272 * Create - we need to know the parent.
1274 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1275 if (error)
1276 return error;
1279 * We have the parent and last component. First of all, check
1280 * that we are not asked to creat(2) an obvious directory - that
1281 * will not do.
1283 error = -EISDIR;
1284 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1285 goto exit;
1287 dir = nd->dentry;
1288 nd->flags &= ~LOOKUP_PARENT;
1289 down(&dir->d_inode->i_sem);
1290 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1292 do_last:
1293 error = PTR_ERR(dentry);
1294 if (IS_ERR(dentry)) {
1295 up(&dir->d_inode->i_sem);
1296 goto exit;
1299 /* Negative dentry, just create the file */
1300 if (!dentry->d_inode) {
1301 if (!IS_POSIXACL(dir->d_inode))
1302 mode &= ~current->fs->umask;
1303 error = vfs_create(dir->d_inode, dentry, mode, nd);
1304 up(&dir->d_inode->i_sem);
1305 dput(nd->dentry);
1306 nd->dentry = dentry;
1307 if (error)
1308 goto exit;
1309 /* Don't check for write permission, don't truncate */
1310 acc_mode = 0;
1311 flag &= ~O_TRUNC;
1312 goto ok;
1316 * It already exists.
1318 up(&dir->d_inode->i_sem);
1320 error = -EEXIST;
1321 if (flag & O_EXCL)
1322 goto exit_dput;
1324 if (d_mountpoint(dentry)) {
1325 error = -ELOOP;
1326 if (flag & O_NOFOLLOW)
1327 goto exit_dput;
1328 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1330 error = -ENOENT;
1331 if (!dentry->d_inode)
1332 goto exit_dput;
1333 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1334 goto do_link;
1336 dput(nd->dentry);
1337 nd->dentry = dentry;
1338 error = -EISDIR;
1339 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1340 goto exit;
1342 error = may_open(nd, acc_mode, flag);
1343 if (error)
1344 goto exit;
1345 return 0;
1347 exit_dput:
1348 dput(dentry);
1349 exit:
1350 path_release(nd);
1351 return error;
1353 do_link:
1354 error = -ELOOP;
1355 if (flag & O_NOFOLLOW)
1356 goto exit_dput;
1358 * This is subtle. Instead of calling do_follow_link() we do the
1359 * thing by hands. The reason is that this way we have zero link_count
1360 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1361 * After that we have the parent and last component, i.e.
1362 * we are in the same situation as after the first path_walk().
1363 * Well, almost - if the last component is normal we get its copy
1364 * stored in nd->last.name and we will have to putname() it when we
1365 * are done. Procfs-like symlinks just set LAST_BIND.
1367 nd->flags |= LOOKUP_PARENT;
1368 error = security_inode_follow_link(dentry, nd);
1369 if (error)
1370 goto exit_dput;
1371 touch_atime(nd->mnt, dentry);
1372 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1373 dput(dentry);
1374 if (error)
1375 return error;
1376 nd->flags &= ~LOOKUP_PARENT;
1377 if (nd->last_type == LAST_BIND) {
1378 dentry = nd->dentry;
1379 goto ok;
1381 error = -EISDIR;
1382 if (nd->last_type != LAST_NORM)
1383 goto exit;
1384 if (nd->last.name[nd->last.len]) {
1385 putname(nd->last.name);
1386 goto exit;
1388 error = -ELOOP;
1389 if (count++==32) {
1390 putname(nd->last.name);
1391 goto exit;
1393 dir = nd->dentry;
1394 down(&dir->d_inode->i_sem);
1395 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1396 putname(nd->last.name);
1397 goto do_last;
1401 * lookup_create - lookup a dentry, creating it if it doesn't exist
1402 * @nd: nameidata info
1403 * @is_dir: directory flag
1405 * Simple function to lookup and return a dentry and create it
1406 * if it doesn't exist. Is SMP-safe.
1408 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1410 struct dentry *dentry;
1412 down(&nd->dentry->d_inode->i_sem);
1413 dentry = ERR_PTR(-EEXIST);
1414 if (nd->last_type != LAST_NORM)
1415 goto fail;
1416 nd->flags &= ~LOOKUP_PARENT;
1417 dentry = lookup_hash(&nd->last, nd->dentry);
1418 if (IS_ERR(dentry))
1419 goto fail;
1420 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1421 goto enoent;
1422 return dentry;
1423 enoent:
1424 dput(dentry);
1425 dentry = ERR_PTR(-ENOENT);
1426 fail:
1427 return dentry;
1430 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1432 int error = may_create(dir, dentry, NULL);
1434 if (error)
1435 return error;
1437 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1438 return -EPERM;
1440 if (!dir->i_op || !dir->i_op->mknod)
1441 return -EPERM;
1443 error = security_inode_mknod(dir, dentry, mode, dev);
1444 if (error)
1445 return error;
1447 DQUOT_INIT(dir);
1448 error = dir->i_op->mknod(dir, dentry, mode, dev);
1449 if (!error) {
1450 inode_dir_notify(dir, DN_CREATE);
1451 security_inode_post_mknod(dir, dentry, mode, dev);
1453 return error;
1456 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1458 int error = 0;
1459 char * tmp;
1460 struct dentry * dentry;
1461 struct nameidata nd;
1463 if (S_ISDIR(mode))
1464 return -EPERM;
1465 tmp = getname(filename);
1466 if (IS_ERR(tmp))
1467 return PTR_ERR(tmp);
1469 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1470 if (error)
1471 goto out;
1472 dentry = lookup_create(&nd, 0);
1473 error = PTR_ERR(dentry);
1475 if (!IS_POSIXACL(nd.dentry->d_inode))
1476 mode &= ~current->fs->umask;
1477 if (!IS_ERR(dentry)) {
1478 switch (mode & S_IFMT) {
1479 case 0: case S_IFREG:
1480 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1481 break;
1482 case S_IFCHR: case S_IFBLK:
1483 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1484 new_decode_dev(dev));
1485 break;
1486 case S_IFIFO: case S_IFSOCK:
1487 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1488 break;
1489 case S_IFDIR:
1490 error = -EPERM;
1491 break;
1492 default:
1493 error = -EINVAL;
1495 dput(dentry);
1497 up(&nd.dentry->d_inode->i_sem);
1498 path_release(&nd);
1499 out:
1500 putname(tmp);
1502 return error;
1505 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1507 int error = may_create(dir, dentry, NULL);
1509 if (error)
1510 return error;
1512 if (!dir->i_op || !dir->i_op->mkdir)
1513 return -EPERM;
1515 mode &= (S_IRWXUGO|S_ISVTX);
1516 error = security_inode_mkdir(dir, dentry, mode);
1517 if (error)
1518 return error;
1520 DQUOT_INIT(dir);
1521 error = dir->i_op->mkdir(dir, dentry, mode);
1522 if (!error) {
1523 inode_dir_notify(dir, DN_CREATE);
1524 security_inode_post_mkdir(dir,dentry, mode);
1526 return error;
1529 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1531 int error = 0;
1532 char * tmp;
1534 tmp = getname(pathname);
1535 error = PTR_ERR(tmp);
1536 if (!IS_ERR(tmp)) {
1537 struct dentry *dentry;
1538 struct nameidata nd;
1540 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1541 if (error)
1542 goto out;
1543 dentry = lookup_create(&nd, 1);
1544 error = PTR_ERR(dentry);
1545 if (!IS_ERR(dentry)) {
1546 if (!IS_POSIXACL(nd.dentry->d_inode))
1547 mode &= ~current->fs->umask;
1548 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1549 dput(dentry);
1551 up(&nd.dentry->d_inode->i_sem);
1552 path_release(&nd);
1553 out:
1554 putname(tmp);
1557 return error;
1561 * We try to drop the dentry early: we should have
1562 * a usage count of 2 if we're the only user of this
1563 * dentry, and if that is true (possibly after pruning
1564 * the dcache), then we drop the dentry now.
1566 * A low-level filesystem can, if it choses, legally
1567 * do a
1569 * if (!d_unhashed(dentry))
1570 * return -EBUSY;
1572 * if it cannot handle the case of removing a directory
1573 * that is still in use by something else..
1575 static void d_unhash(struct dentry *dentry)
1577 dget(dentry);
1578 spin_lock(&dcache_lock);
1579 switch (atomic_read(&dentry->d_count)) {
1580 default:
1581 spin_unlock(&dcache_lock);
1582 shrink_dcache_parent(dentry);
1583 spin_lock(&dcache_lock);
1584 if (atomic_read(&dentry->d_count) != 2)
1585 break;
1586 case 2:
1587 __d_drop(dentry);
1589 spin_unlock(&dcache_lock);
1592 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1594 int error = may_delete(dir, dentry, 1);
1596 if (error)
1597 return error;
1599 if (!dir->i_op || !dir->i_op->rmdir)
1600 return -EPERM;
1602 DQUOT_INIT(dir);
1604 down(&dentry->d_inode->i_sem);
1605 d_unhash(dentry);
1606 if (d_mountpoint(dentry))
1607 error = -EBUSY;
1608 else {
1609 error = security_inode_rmdir(dir, dentry);
1610 if (!error) {
1611 error = dir->i_op->rmdir(dir, dentry);
1612 if (!error)
1613 dentry->d_inode->i_flags |= S_DEAD;
1616 up(&dentry->d_inode->i_sem);
1617 if (!error) {
1618 inode_dir_notify(dir, DN_DELETE);
1619 d_delete(dentry);
1621 dput(dentry);
1623 return error;
1626 asmlinkage long sys_rmdir(const char __user * pathname)
1628 int error = 0;
1629 char * name;
1630 struct dentry *dentry;
1631 struct nameidata nd;
1633 name = getname(pathname);
1634 if(IS_ERR(name))
1635 return PTR_ERR(name);
1637 error = path_lookup(name, LOOKUP_PARENT, &nd);
1638 if (error)
1639 goto exit;
1641 switch(nd.last_type) {
1642 case LAST_DOTDOT:
1643 error = -ENOTEMPTY;
1644 goto exit1;
1645 case LAST_DOT:
1646 error = -EINVAL;
1647 goto exit1;
1648 case LAST_ROOT:
1649 error = -EBUSY;
1650 goto exit1;
1652 down(&nd.dentry->d_inode->i_sem);
1653 dentry = lookup_hash(&nd.last, nd.dentry);
1654 error = PTR_ERR(dentry);
1655 if (!IS_ERR(dentry)) {
1656 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1657 dput(dentry);
1659 up(&nd.dentry->d_inode->i_sem);
1660 exit1:
1661 path_release(&nd);
1662 exit:
1663 putname(name);
1664 return error;
1667 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1669 int error = may_delete(dir, dentry, 0);
1671 if (error)
1672 return error;
1674 if (!dir->i_op || !dir->i_op->unlink)
1675 return -EPERM;
1677 DQUOT_INIT(dir);
1679 down(&dentry->d_inode->i_sem);
1680 if (d_mountpoint(dentry))
1681 error = -EBUSY;
1682 else {
1683 error = security_inode_unlink(dir, dentry);
1684 if (!error)
1685 error = dir->i_op->unlink(dir, dentry);
1687 up(&dentry->d_inode->i_sem);
1689 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1690 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1691 d_delete(dentry);
1692 inode_dir_notify(dir, DN_DELETE);
1694 return error;
1698 * Make sure that the actual truncation of the file will occur outside its
1699 * directory's i_sem. Truncate can take a long time if there is a lot of
1700 * writeout happening, and we don't want to prevent access to the directory
1701 * while waiting on the I/O.
1703 asmlinkage long sys_unlink(const char __user * pathname)
1705 int error = 0;
1706 char * name;
1707 struct dentry *dentry;
1708 struct nameidata nd;
1709 struct inode *inode = NULL;
1711 name = getname(pathname);
1712 if(IS_ERR(name))
1713 return PTR_ERR(name);
1715 error = path_lookup(name, LOOKUP_PARENT, &nd);
1716 if (error)
1717 goto exit;
1718 error = -EISDIR;
1719 if (nd.last_type != LAST_NORM)
1720 goto exit1;
1721 down(&nd.dentry->d_inode->i_sem);
1722 dentry = lookup_hash(&nd.last, nd.dentry);
1723 error = PTR_ERR(dentry);
1724 if (!IS_ERR(dentry)) {
1725 /* Why not before? Because we want correct error value */
1726 if (nd.last.name[nd.last.len])
1727 goto slashes;
1728 inode = dentry->d_inode;
1729 if (inode)
1730 atomic_inc(&inode->i_count);
1731 error = vfs_unlink(nd.dentry->d_inode, dentry);
1732 exit2:
1733 dput(dentry);
1735 up(&nd.dentry->d_inode->i_sem);
1736 exit1:
1737 path_release(&nd);
1738 exit:
1739 putname(name);
1741 if (inode)
1742 iput(inode); /* truncate the inode here */
1743 return error;
1745 slashes:
1746 error = !dentry->d_inode ? -ENOENT :
1747 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1748 goto exit2;
1751 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1753 int error = may_create(dir, dentry, NULL);
1755 if (error)
1756 return error;
1758 if (!dir->i_op || !dir->i_op->symlink)
1759 return -EPERM;
1761 error = security_inode_symlink(dir, dentry, oldname);
1762 if (error)
1763 return error;
1765 DQUOT_INIT(dir);
1766 error = dir->i_op->symlink(dir, dentry, oldname);
1767 if (!error) {
1768 inode_dir_notify(dir, DN_CREATE);
1769 security_inode_post_symlink(dir, dentry, oldname);
1771 return error;
1774 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1776 int error = 0;
1777 char * from;
1778 char * to;
1780 from = getname(oldname);
1781 if(IS_ERR(from))
1782 return PTR_ERR(from);
1783 to = getname(newname);
1784 error = PTR_ERR(to);
1785 if (!IS_ERR(to)) {
1786 struct dentry *dentry;
1787 struct nameidata nd;
1789 error = path_lookup(to, LOOKUP_PARENT, &nd);
1790 if (error)
1791 goto out;
1792 dentry = lookup_create(&nd, 0);
1793 error = PTR_ERR(dentry);
1794 if (!IS_ERR(dentry)) {
1795 error = vfs_symlink(nd.dentry->d_inode, dentry, from);
1796 dput(dentry);
1798 up(&nd.dentry->d_inode->i_sem);
1799 path_release(&nd);
1800 out:
1801 putname(to);
1803 putname(from);
1804 return error;
1807 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1809 struct inode *inode = old_dentry->d_inode;
1810 int error;
1812 if (!inode)
1813 return -ENOENT;
1815 error = may_create(dir, new_dentry, NULL);
1816 if (error)
1817 return error;
1819 if (dir->i_sb != inode->i_sb)
1820 return -EXDEV;
1823 * A link to an append-only or immutable file cannot be created.
1825 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1826 return -EPERM;
1827 if (!dir->i_op || !dir->i_op->link)
1828 return -EPERM;
1829 if (S_ISDIR(old_dentry->d_inode->i_mode))
1830 return -EPERM;
1832 error = security_inode_link(old_dentry, dir, new_dentry);
1833 if (error)
1834 return error;
1836 down(&old_dentry->d_inode->i_sem);
1837 DQUOT_INIT(dir);
1838 error = dir->i_op->link(old_dentry, dir, new_dentry);
1839 up(&old_dentry->d_inode->i_sem);
1840 if (!error) {
1841 inode_dir_notify(dir, DN_CREATE);
1842 security_inode_post_link(old_dentry, dir, new_dentry);
1844 return error;
1848 * Hardlinks are often used in delicate situations. We avoid
1849 * security-related surprises by not following symlinks on the
1850 * newname. --KAB
1852 * We don't follow them on the oldname either to be compatible
1853 * with linux 2.0, and to avoid hard-linking to directories
1854 * and other special files. --ADM
1856 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
1858 struct dentry *new_dentry;
1859 struct nameidata nd, old_nd;
1860 int error;
1861 char * to;
1863 to = getname(newname);
1864 if (IS_ERR(to))
1865 return PTR_ERR(to);
1867 error = __user_walk(oldname, 0, &old_nd);
1868 if (error)
1869 goto exit;
1870 error = path_lookup(to, LOOKUP_PARENT, &nd);
1871 if (error)
1872 goto out;
1873 error = -EXDEV;
1874 if (old_nd.mnt != nd.mnt)
1875 goto out_release;
1876 new_dentry = lookup_create(&nd, 0);
1877 error = PTR_ERR(new_dentry);
1878 if (!IS_ERR(new_dentry)) {
1879 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
1880 dput(new_dentry);
1882 up(&nd.dentry->d_inode->i_sem);
1883 out_release:
1884 path_release(&nd);
1885 out:
1886 path_release(&old_nd);
1887 exit:
1888 putname(to);
1890 return error;
1894 * The worst of all namespace operations - renaming directory. "Perverted"
1895 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1896 * Problems:
1897 * a) we can get into loop creation. Check is done in is_subdir().
1898 * b) race potential - two innocent renames can create a loop together.
1899 * That's where 4.4 screws up. Current fix: serialization on
1900 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1901 * story.
1902 * c) we have to lock _three_ objects - parents and victim (if it exists).
1903 * And that - after we got ->i_sem on parents (until then we don't know
1904 * whether the target exists). Solution: try to be smart with locking
1905 * order for inodes. We rely on the fact that tree topology may change
1906 * only under ->s_vfs_rename_sem _and_ that parent of the object we
1907 * move will be locked. Thus we can rank directories by the tree
1908 * (ancestors first) and rank all non-directories after them.
1909 * That works since everybody except rename does "lock parent, lookup,
1910 * lock child" and rename is under ->s_vfs_rename_sem.
1911 * HOWEVER, it relies on the assumption that any object with ->lookup()
1912 * has no more than 1 dentry. If "hybrid" objects will ever appear,
1913 * we'd better make sure that there's no link(2) for them.
1914 * d) some filesystems don't support opened-but-unlinked directories,
1915 * either because of layout or because they are not ready to deal with
1916 * all cases correctly. The latter will be fixed (taking this sort of
1917 * stuff into VFS), but the former is not going away. Solution: the same
1918 * trick as in rmdir().
1919 * e) conversion from fhandle to dentry may come in the wrong moment - when
1920 * we are removing the target. Solution: we will have to grab ->i_sem
1921 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1922 * ->i_sem on parents, which works but leads to some truely excessive
1923 * locking].
1925 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
1926 struct inode *new_dir, struct dentry *new_dentry)
1928 int error = 0;
1929 struct inode *target;
1932 * If we are going to change the parent - check write permissions,
1933 * we'll need to flip '..'.
1935 if (new_dir != old_dir) {
1936 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
1937 if (error)
1938 return error;
1941 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
1942 if (error)
1943 return error;
1945 target = new_dentry->d_inode;
1946 if (target) {
1947 down(&target->i_sem);
1948 d_unhash(new_dentry);
1950 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1951 error = -EBUSY;
1952 else
1953 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1954 if (target) {
1955 if (!error)
1956 target->i_flags |= S_DEAD;
1957 up(&target->i_sem);
1958 if (d_unhashed(new_dentry))
1959 d_rehash(new_dentry);
1960 dput(new_dentry);
1962 if (!error) {
1963 d_move(old_dentry,new_dentry);
1964 security_inode_post_rename(old_dir, old_dentry,
1965 new_dir, new_dentry);
1967 return error;
1970 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
1971 struct inode *new_dir, struct dentry *new_dentry)
1973 struct inode *target;
1974 int error;
1976 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
1977 if (error)
1978 return error;
1980 dget(new_dentry);
1981 target = new_dentry->d_inode;
1982 if (target)
1983 down(&target->i_sem);
1984 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1985 error = -EBUSY;
1986 else
1987 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1988 if (!error) {
1989 /* The following d_move() should become unconditional */
1990 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
1991 d_move(old_dentry, new_dentry);
1992 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
1994 if (target)
1995 up(&target->i_sem);
1996 dput(new_dentry);
1997 return error;
2000 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2001 struct inode *new_dir, struct dentry *new_dentry)
2003 int error;
2004 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2006 if (old_dentry->d_inode == new_dentry->d_inode)
2007 return 0;
2009 error = may_delete(old_dir, old_dentry, is_dir);
2010 if (error)
2011 return error;
2013 if (!new_dentry->d_inode)
2014 error = may_create(new_dir, new_dentry, NULL);
2015 else
2016 error = may_delete(new_dir, new_dentry, is_dir);
2017 if (error)
2018 return error;
2020 if (!old_dir->i_op || !old_dir->i_op->rename)
2021 return -EPERM;
2023 DQUOT_INIT(old_dir);
2024 DQUOT_INIT(new_dir);
2026 if (is_dir)
2027 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2028 else
2029 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2030 if (!error) {
2031 if (old_dir == new_dir)
2032 inode_dir_notify(old_dir, DN_RENAME);
2033 else {
2034 inode_dir_notify(old_dir, DN_DELETE);
2035 inode_dir_notify(new_dir, DN_CREATE);
2038 return error;
2041 static inline int do_rename(const char * oldname, const char * newname)
2043 int error = 0;
2044 struct dentry * old_dir, * new_dir;
2045 struct dentry * old_dentry, *new_dentry;
2046 struct dentry * trap;
2047 struct nameidata oldnd, newnd;
2049 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2050 if (error)
2051 goto exit;
2053 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2054 if (error)
2055 goto exit1;
2057 error = -EXDEV;
2058 if (oldnd.mnt != newnd.mnt)
2059 goto exit2;
2061 old_dir = oldnd.dentry;
2062 error = -EBUSY;
2063 if (oldnd.last_type != LAST_NORM)
2064 goto exit2;
2066 new_dir = newnd.dentry;
2067 if (newnd.last_type != LAST_NORM)
2068 goto exit2;
2070 trap = lock_rename(new_dir, old_dir);
2072 old_dentry = lookup_hash(&oldnd.last, old_dir);
2073 error = PTR_ERR(old_dentry);
2074 if (IS_ERR(old_dentry))
2075 goto exit3;
2076 /* source must exist */
2077 error = -ENOENT;
2078 if (!old_dentry->d_inode)
2079 goto exit4;
2080 /* unless the source is a directory trailing slashes give -ENOTDIR */
2081 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2082 error = -ENOTDIR;
2083 if (oldnd.last.name[oldnd.last.len])
2084 goto exit4;
2085 if (newnd.last.name[newnd.last.len])
2086 goto exit4;
2088 /* source should not be ancestor of target */
2089 error = -EINVAL;
2090 if (old_dentry == trap)
2091 goto exit4;
2092 new_dentry = lookup_hash(&newnd.last, new_dir);
2093 error = PTR_ERR(new_dentry);
2094 if (IS_ERR(new_dentry))
2095 goto exit4;
2096 /* target should not be an ancestor of source */
2097 error = -ENOTEMPTY;
2098 if (new_dentry == trap)
2099 goto exit5;
2101 error = vfs_rename(old_dir->d_inode, old_dentry,
2102 new_dir->d_inode, new_dentry);
2103 exit5:
2104 dput(new_dentry);
2105 exit4:
2106 dput(old_dentry);
2107 exit3:
2108 unlock_rename(new_dir, old_dir);
2109 exit2:
2110 path_release(&newnd);
2111 exit1:
2112 path_release(&oldnd);
2113 exit:
2114 return error;
2117 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2119 int error;
2120 char * from;
2121 char * to;
2123 from = getname(oldname);
2124 if(IS_ERR(from))
2125 return PTR_ERR(from);
2126 to = getname(newname);
2127 error = PTR_ERR(to);
2128 if (!IS_ERR(to)) {
2129 error = do_rename(from,to);
2130 putname(to);
2132 putname(from);
2133 return error;
2136 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2138 int len;
2140 len = PTR_ERR(link);
2141 if (IS_ERR(link))
2142 goto out;
2144 len = strlen(link);
2145 if (len > (unsigned) buflen)
2146 len = buflen;
2147 if (copy_to_user(buffer, link, len))
2148 len = -EFAULT;
2149 out:
2150 return len;
2153 static inline int
2154 __vfs_follow_link(struct nameidata *nd, const char *link)
2156 int res = 0;
2157 char *name;
2158 if (IS_ERR(link))
2159 goto fail;
2161 if (*link == '/') {
2162 path_release(nd);
2163 if (!walk_init_root(link, nd))
2164 /* weird __emul_prefix() stuff did it */
2165 goto out;
2167 res = link_path_walk(link, nd);
2168 out:
2169 if (current->link_count || res || nd->last_type!=LAST_NORM)
2170 return res;
2172 * If it is an iterative symlinks resolution in open_namei() we
2173 * have to copy the last component. And all that crap because of
2174 * bloody create() on broken symlinks. Furrfu...
2176 name = __getname();
2177 if (unlikely(!name)) {
2178 path_release(nd);
2179 return -ENOMEM;
2181 strcpy(name, nd->last.name);
2182 nd->last.name = name;
2183 return 0;
2184 fail:
2185 path_release(nd);
2186 return PTR_ERR(link);
2189 int vfs_follow_link(struct nameidata *nd, const char *link)
2191 return __vfs_follow_link(nd, link);
2194 /* get the link contents into pagecache */
2195 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2197 struct page * page;
2198 struct address_space *mapping = dentry->d_inode->i_mapping;
2199 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2200 NULL);
2201 if (IS_ERR(page))
2202 goto sync_fail;
2203 wait_on_page_locked(page);
2204 if (!PageUptodate(page))
2205 goto async_fail;
2206 *ppage = page;
2207 return kmap(page);
2209 async_fail:
2210 page_cache_release(page);
2211 return ERR_PTR(-EIO);
2213 sync_fail:
2214 return (char*)page;
2217 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2219 struct page *page = NULL;
2220 char *s = page_getlink(dentry, &page);
2221 int res = vfs_readlink(dentry,buffer,buflen,s);
2222 if (page) {
2223 kunmap(page);
2224 page_cache_release(page);
2226 return res;
2229 int page_follow_link(struct dentry *dentry, struct nameidata *nd)
2231 struct page *page = NULL;
2232 char *s = page_getlink(dentry, &page);
2233 int res = __vfs_follow_link(nd, s);
2234 if (page) {
2235 kunmap(page);
2236 page_cache_release(page);
2238 return res;
2241 int page_symlink(struct inode *inode, const char *symname, int len)
2243 struct address_space *mapping = inode->i_mapping;
2244 struct page *page = grab_cache_page(mapping, 0);
2245 int err = -ENOMEM;
2246 char *kaddr;
2248 if (!page)
2249 goto fail;
2250 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2251 if (err)
2252 goto fail_map;
2253 kaddr = kmap_atomic(page, KM_USER0);
2254 memcpy(kaddr, symname, len-1);
2255 kunmap_atomic(kaddr, KM_USER0);
2256 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2258 * Notice that we are _not_ going to block here - end of page is
2259 * unmapped, so this will only try to map the rest of page, see
2260 * that it is unmapped (typically even will not look into inode -
2261 * ->i_size will be enough for everything) and zero it out.
2262 * OTOH it's obviously correct and should make the page up-to-date.
2264 if (!PageUptodate(page)) {
2265 err = mapping->a_ops->readpage(NULL, page);
2266 wait_on_page_locked(page);
2267 } else {
2268 unlock_page(page);
2270 page_cache_release(page);
2271 if (err < 0)
2272 goto fail;
2273 mark_inode_dirty(inode);
2274 return 0;
2275 fail_map:
2276 unlock_page(page);
2277 page_cache_release(page);
2278 fail:
2279 return err;
2282 struct inode_operations page_symlink_inode_operations = {
2283 .readlink = page_readlink,
2284 .follow_link = page_follow_link,
2287 EXPORT_SYMBOL(__user_walk);
2288 EXPORT_SYMBOL(follow_down);
2289 EXPORT_SYMBOL(follow_up);
2290 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2291 EXPORT_SYMBOL(getname);
2292 EXPORT_SYMBOL(lock_rename);
2293 EXPORT_SYMBOL(lookup_create);
2294 EXPORT_SYMBOL(lookup_hash);
2295 EXPORT_SYMBOL(lookup_one_len);
2296 EXPORT_SYMBOL(page_follow_link);
2297 EXPORT_SYMBOL(page_readlink);
2298 EXPORT_SYMBOL(page_symlink);
2299 EXPORT_SYMBOL(page_symlink_inode_operations);
2300 EXPORT_SYMBOL(path_lookup);
2301 EXPORT_SYMBOL(path_release);
2302 EXPORT_SYMBOL(path_walk);
2303 EXPORT_SYMBOL(permission);
2304 EXPORT_SYMBOL(unlock_rename);
2305 EXPORT_SYMBOL(vfs_create);
2306 EXPORT_SYMBOL(vfs_follow_link);
2307 EXPORT_SYMBOL(vfs_link);
2308 EXPORT_SYMBOL(vfs_mkdir);
2309 EXPORT_SYMBOL(vfs_mknod);
2310 EXPORT_SYMBOL(vfs_permission);
2311 EXPORT_SYMBOL(vfs_readlink);
2312 EXPORT_SYMBOL(vfs_rename);
2313 EXPORT_SYMBOL(vfs_rmdir);
2314 EXPORT_SYMBOL(vfs_symlink);
2315 EXPORT_SYMBOL(vfs_unlink);