[PATCH] Fix bugs in analog tv i2c-helper chipset drivers
[linux-2.6/history.git] / fs / namei.c
blobfd7bf97afda5be52fd20d0fc4acbb358a6d6b18d
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_READ|MAY_WRITE)) || (inode->i_mode & S_IXUGO))
194 if (capable(CAP_DAC_OVERRIDE))
195 return 0;
198 * Searching includes executable on directories, else just read.
200 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
201 if (capable(CAP_DAC_READ_SEARCH))
202 return 0;
204 return -EACCES;
207 int permission(struct inode * inode,int mask, struct nameidata *nd)
209 int retval;
210 int submask;
212 /* Ordinary permission routines do not understand MAY_APPEND. */
213 submask = mask & ~MAY_APPEND;
215 if (inode->i_op && inode->i_op->permission)
216 retval = inode->i_op->permission(inode, submask, nd);
217 else
218 retval = vfs_permission(inode, submask);
219 if (retval)
220 return retval;
222 return security_inode_permission(inode, mask, nd);
226 * get_write_access() gets write permission for a file.
227 * put_write_access() releases this write permission.
228 * This is used for regular files.
229 * We cannot support write (and maybe mmap read-write shared) accesses and
230 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
231 * can have the following values:
232 * 0: no writers, no VM_DENYWRITE mappings
233 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
234 * > 0: (i_writecount) users are writing to the file.
236 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
237 * except for the cases where we don't hold i_writecount yet. Then we need to
238 * use {get,deny}_write_access() - these functions check the sign and refuse
239 * to do the change if sign is wrong. Exclusion between them is provided by
240 * spinlock (arbitration_lock) and I'll rip the second arsehole to the first
241 * who will try to move it in struct inode - just leave it here.
243 static spinlock_t arbitration_lock = SPIN_LOCK_UNLOCKED;
244 int get_write_access(struct inode * inode)
246 spin_lock(&arbitration_lock);
247 if (atomic_read(&inode->i_writecount) < 0) {
248 spin_unlock(&arbitration_lock);
249 return -ETXTBSY;
251 atomic_inc(&inode->i_writecount);
252 spin_unlock(&arbitration_lock);
253 return 0;
255 int deny_write_access(struct file * file)
257 spin_lock(&arbitration_lock);
258 if (atomic_read(&file->f_dentry->d_inode->i_writecount) > 0) {
259 spin_unlock(&arbitration_lock);
260 return -ETXTBSY;
262 atomic_dec(&file->f_dentry->d_inode->i_writecount);
263 spin_unlock(&arbitration_lock);
264 return 0;
267 void path_release(struct nameidata *nd)
269 dput(nd->dentry);
270 mntput(nd->mnt);
274 * Internal lookup() using the new generic dcache.
275 * SMP-safe
277 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
279 struct dentry * dentry = __d_lookup(parent, name);
281 /* lockess __d_lookup may fail due to concurrent d_move()
282 * in some unrelated directory, so try with d_lookup
284 if (!dentry)
285 dentry = d_lookup(parent, name);
287 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
288 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
289 dput(dentry);
290 dentry = NULL;
293 return dentry;
297 * Short-cut version of permission(), for calling by
298 * path_walk(), when dcache lock is held. Combines parts
299 * of permission() and vfs_permission(), and tests ONLY for
300 * MAY_EXEC permission.
302 * If appropriate, check DAC only. If not appropriate, or
303 * short-cut DAC fails, then call permission() to do more
304 * complete permission check.
306 static inline int exec_permission_lite(struct inode *inode,
307 struct nameidata *nd)
309 umode_t mode = inode->i_mode;
311 if ((inode->i_op && inode->i_op->permission))
312 return -EAGAIN;
314 if (current->fsuid == inode->i_uid)
315 mode >>= 6;
316 else if (in_group_p(inode->i_gid))
317 mode >>= 3;
319 if (mode & MAY_EXEC)
320 goto ok;
322 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
323 goto ok;
325 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
326 goto ok;
328 return -EACCES;
330 return security_inode_permission(inode, MAY_EXEC, nd);
334 * This is called when everything else fails, and we actually have
335 * to go to the low-level filesystem to find out what we should do..
337 * We get the directory semaphore, and after getting that we also
338 * make sure that nobody added the entry to the dcache in the meantime..
339 * SMP-safe
341 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
343 struct dentry * result;
344 struct inode *dir = parent->d_inode;
346 down(&dir->i_sem);
348 * First re-do the cached lookup just in case it was created
349 * while we waited for the directory semaphore..
351 * FIXME! This could use version numbering or similar to
352 * avoid unnecessary cache lookups.
354 * The "dcache_lock" is purely to protect the RCU list walker
355 * from concurrent renames at this point (we mustn't get false
356 * negatives from the RCU list walk here, unlike the optimistic
357 * fast walk).
359 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
361 result = d_lookup(parent, name);
362 if (!result) {
363 struct dentry * dentry = d_alloc(parent, name);
364 result = ERR_PTR(-ENOMEM);
365 if (dentry) {
366 result = dir->i_op->lookup(dir, dentry, nd);
367 if (result)
368 dput(dentry);
369 else
370 result = dentry;
372 up(&dir->i_sem);
373 return result;
377 * Uhhuh! Nasty case: the cache was re-populated while
378 * we waited on the semaphore. Need to revalidate.
380 up(&dir->i_sem);
381 if (result->d_op && result->d_op->d_revalidate) {
382 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
383 dput(result);
384 result = ERR_PTR(-ENOENT);
387 return result;
391 * This limits recursive symlink follows to 8, while
392 * limiting consecutive symlinks to 40.
394 * Without that kind of total limit, nasty chains of consecutive
395 * symlinks can cause almost arbitrarily long lookups.
397 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
399 int err = -ELOOP;
400 if (current->link_count >= 5)
401 goto loop;
402 if (current->total_link_count >= 40)
403 goto loop;
404 cond_resched();
405 err = security_inode_follow_link(dentry, nd);
406 if (err)
407 goto loop;
408 current->link_count++;
409 current->total_link_count++;
410 update_atime(dentry->d_inode);
411 err = dentry->d_inode->i_op->follow_link(dentry, nd);
412 current->link_count--;
413 return err;
414 loop:
415 path_release(nd);
416 return err;
419 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
421 struct vfsmount *parent;
422 struct dentry *mountpoint;
423 spin_lock(&dcache_lock);
424 parent=(*mnt)->mnt_parent;
425 if (parent == *mnt) {
426 spin_unlock(&dcache_lock);
427 return 0;
429 mntget(parent);
430 mountpoint=dget((*mnt)->mnt_mountpoint);
431 spin_unlock(&dcache_lock);
432 dput(*dentry);
433 *dentry = mountpoint;
434 mntput(*mnt);
435 *mnt = parent;
436 return 1;
439 /* no need for dcache_lock, as serialization is taken care in
440 * namespace.c
442 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
444 int res = 0;
445 while (d_mountpoint(*dentry)) {
446 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
447 if (!mounted)
448 break;
449 *mnt = mounted;
450 dput(*dentry);
451 mntput(mounted->mnt_parent);
452 *dentry = dget(mounted->mnt_root);
453 res = 1;
455 return res;
458 /* no need for dcache_lock, as serialization is taken care in
459 * namespace.c
461 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
463 struct vfsmount *mounted;
465 mounted = lookup_mnt(*mnt, *dentry);
466 if (mounted) {
467 *mnt = mounted;
468 dput(*dentry);
469 mntput(mounted->mnt_parent);
470 *dentry = dget(mounted->mnt_root);
471 return 1;
473 return 0;
476 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
478 return __follow_down(mnt,dentry);
481 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
483 while(1) {
484 struct vfsmount *parent;
485 struct dentry *old = *dentry;
487 read_lock(&current->fs->lock);
488 if (*dentry == current->fs->root &&
489 *mnt == current->fs->rootmnt) {
490 read_unlock(&current->fs->lock);
491 break;
493 read_unlock(&current->fs->lock);
494 spin_lock(&dcache_lock);
495 if (*dentry != (*mnt)->mnt_root) {
496 *dentry = dget((*dentry)->d_parent);
497 spin_unlock(&dcache_lock);
498 dput(old);
499 break;
501 parent = (*mnt)->mnt_parent;
502 if (parent == *mnt) {
503 spin_unlock(&dcache_lock);
504 break;
506 mntget(parent);
507 *dentry = dget((*mnt)->mnt_mountpoint);
508 spin_unlock(&dcache_lock);
509 dput(old);
510 mntput(*mnt);
511 *mnt = parent;
513 follow_mount(mnt, dentry);
516 struct path {
517 struct vfsmount *mnt;
518 struct dentry *dentry;
522 * It's more convoluted than I'd like it to be, but... it's still fairly
523 * small and for now I'd prefer to have fast path as straight as possible.
524 * It _is_ time-critical.
526 static int do_lookup(struct nameidata *nd, struct qstr *name,
527 struct path *path)
529 struct vfsmount *mnt = nd->mnt;
530 struct dentry *dentry = __d_lookup(nd->dentry, name);
532 if (!dentry)
533 goto need_lookup;
534 if (dentry->d_op && dentry->d_op->d_revalidate)
535 goto need_revalidate;
536 done:
537 path->mnt = mnt;
538 path->dentry = dentry;
539 return 0;
541 need_lookup:
542 dentry = real_lookup(nd->dentry, name, nd);
543 if (IS_ERR(dentry))
544 goto fail;
545 goto done;
547 need_revalidate:
548 if (dentry->d_op->d_revalidate(dentry, nd))
549 goto done;
550 if (d_invalidate(dentry))
551 goto done;
552 dput(dentry);
553 goto need_lookup;
555 fail:
556 return PTR_ERR(dentry);
560 * Name resolution.
562 * This is the basic name resolution function, turning a pathname
563 * into the final dentry.
565 * We expect 'base' to be positive and a directory.
567 int link_path_walk(const char * name, struct nameidata *nd)
569 struct path next;
570 struct inode *inode;
571 int err;
572 unsigned int lookup_flags = nd->flags;
574 while (*name=='/')
575 name++;
576 if (!*name)
577 goto return_reval;
579 inode = nd->dentry->d_inode;
580 if (current->link_count)
581 lookup_flags = LOOKUP_FOLLOW;
583 /* At this point we know we have a real path component. */
584 for(;;) {
585 unsigned long hash;
586 struct qstr this;
587 unsigned int c;
589 err = exec_permission_lite(inode, nd);
590 if (err == -EAGAIN) {
591 err = permission(inode, MAY_EXEC, nd);
593 if (err)
594 break;
596 this.name = name;
597 c = *(const unsigned char *)name;
599 hash = init_name_hash();
600 do {
601 name++;
602 hash = partial_name_hash(c, hash);
603 c = *(const unsigned char *)name;
604 } while (c && (c != '/'));
605 this.len = name - (const char *) this.name;
606 this.hash = end_name_hash(hash);
608 /* remove trailing slashes? */
609 if (!c)
610 goto last_component;
611 while (*++name == '/');
612 if (!*name)
613 goto last_with_slashes;
616 * "." and ".." are special - ".." especially so because it has
617 * to be able to know about the current root directory and
618 * parent relationships.
620 if (this.name[0] == '.') switch (this.len) {
621 default:
622 break;
623 case 2:
624 if (this.name[1] != '.')
625 break;
626 follow_dotdot(&nd->mnt, &nd->dentry);
627 inode = nd->dentry->d_inode;
628 /* fallthrough */
629 case 1:
630 continue;
633 * See if the low-level filesystem might want
634 * to use its own hash..
636 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
637 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
638 if (err < 0)
639 break;
641 nd->flags |= LOOKUP_CONTINUE;
642 /* This does the actual lookups.. */
643 err = do_lookup(nd, &this, &next);
644 if (err)
645 break;
646 /* Check mountpoints.. */
647 follow_mount(&next.mnt, &next.dentry);
649 err = -ENOENT;
650 inode = next.dentry->d_inode;
651 if (!inode)
652 goto out_dput;
653 err = -ENOTDIR;
654 if (!inode->i_op)
655 goto out_dput;
657 if (inode->i_op->follow_link) {
658 mntget(next.mnt);
659 err = do_follow_link(next.dentry, nd);
660 dput(next.dentry);
661 mntput(next.mnt);
662 if (err)
663 goto return_err;
664 err = -ENOENT;
665 inode = nd->dentry->d_inode;
666 if (!inode)
667 break;
668 err = -ENOTDIR;
669 if (!inode->i_op)
670 break;
671 } else {
672 dput(nd->dentry);
673 nd->mnt = next.mnt;
674 nd->dentry = next.dentry;
676 err = -ENOTDIR;
677 if (!inode->i_op->lookup)
678 break;
679 continue;
680 /* here ends the main loop */
682 last_with_slashes:
683 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
684 last_component:
685 nd->flags &= ~LOOKUP_CONTINUE;
686 if (lookup_flags & LOOKUP_PARENT)
687 goto lookup_parent;
688 if (this.name[0] == '.') switch (this.len) {
689 default:
690 break;
691 case 2:
692 if (this.name[1] != '.')
693 break;
694 follow_dotdot(&nd->mnt, &nd->dentry);
695 inode = nd->dentry->d_inode;
696 /* fallthrough */
697 case 1:
698 goto return_reval;
700 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
701 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
702 if (err < 0)
703 break;
705 err = do_lookup(nd, &this, &next);
706 if (err)
707 break;
708 follow_mount(&next.mnt, &next.dentry);
709 inode = next.dentry->d_inode;
710 if ((lookup_flags & LOOKUP_FOLLOW)
711 && inode && inode->i_op && inode->i_op->follow_link) {
712 mntget(next.mnt);
713 err = do_follow_link(next.dentry, nd);
714 dput(next.dentry);
715 mntput(next.mnt);
716 if (err)
717 goto return_err;
718 inode = nd->dentry->d_inode;
719 } else {
720 dput(nd->dentry);
721 nd->mnt = next.mnt;
722 nd->dentry = next.dentry;
724 err = -ENOENT;
725 if (!inode)
726 break;
727 if (lookup_flags & LOOKUP_DIRECTORY) {
728 err = -ENOTDIR;
729 if (!inode->i_op || !inode->i_op->lookup)
730 break;
732 goto return_base;
733 lookup_parent:
734 nd->last = this;
735 nd->last_type = LAST_NORM;
736 if (this.name[0] != '.')
737 goto return_base;
738 if (this.len == 1)
739 nd->last_type = LAST_DOT;
740 else if (this.len == 2 && this.name[1] == '.')
741 nd->last_type = LAST_DOTDOT;
742 else
743 goto return_base;
744 return_reval:
746 * We bypassed the ordinary revalidation routines.
747 * We may need to check the cached dentry for staleness.
749 if (nd->dentry && nd->dentry->d_sb &&
750 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
751 err = -ESTALE;
752 /* Note: we do not d_invalidate() */
753 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
754 break;
756 return_base:
757 return 0;
758 out_dput:
759 dput(next.dentry);
760 break;
762 path_release(nd);
763 return_err:
764 return err;
767 int path_walk(const char * name, struct nameidata *nd)
769 current->total_link_count = 0;
770 return link_path_walk(name, nd);
773 /* SMP-safe */
774 /* returns 1 if everything is done */
775 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
777 if (path_walk(name, nd))
778 return 0; /* something went wrong... */
780 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
781 struct nameidata nd_root;
783 * NAME was not found in alternate root or it's a directory. Try to find
784 * it in the normal root:
786 nd_root.last_type = LAST_ROOT;
787 nd_root.flags = nd->flags;
788 memcpy(&nd_root.intent, &nd->intent, sizeof(nd_root.intent));
789 read_lock(&current->fs->lock);
790 nd_root.mnt = mntget(current->fs->rootmnt);
791 nd_root.dentry = dget(current->fs->root);
792 read_unlock(&current->fs->lock);
793 if (path_walk(name, &nd_root))
794 return 1;
795 if (nd_root.dentry->d_inode) {
796 path_release(nd);
797 nd->dentry = nd_root.dentry;
798 nd->mnt = nd_root.mnt;
799 nd->last = nd_root.last;
800 return 1;
802 path_release(&nd_root);
804 return 1;
807 void set_fs_altroot(void)
809 char *emul = __emul_prefix();
810 struct nameidata nd;
811 struct vfsmount *mnt = NULL, *oldmnt;
812 struct dentry *dentry = NULL, *olddentry;
813 int err;
815 if (!emul)
816 goto set_it;
817 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
818 if (!err) {
819 mnt = nd.mnt;
820 dentry = nd.dentry;
822 set_it:
823 write_lock(&current->fs->lock);
824 oldmnt = current->fs->altrootmnt;
825 olddentry = current->fs->altroot;
826 current->fs->altrootmnt = mnt;
827 current->fs->altroot = dentry;
828 write_unlock(&current->fs->lock);
829 if (olddentry) {
830 dput(olddentry);
831 mntput(oldmnt);
835 /* SMP-safe */
836 static inline int
837 walk_init_root(const char *name, struct nameidata *nd)
839 read_lock(&current->fs->lock);
840 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
841 nd->mnt = mntget(current->fs->altrootmnt);
842 nd->dentry = dget(current->fs->altroot);
843 read_unlock(&current->fs->lock);
844 if (__emul_lookup_dentry(name,nd))
845 return 0;
846 read_lock(&current->fs->lock);
848 nd->mnt = mntget(current->fs->rootmnt);
849 nd->dentry = dget(current->fs->root);
850 read_unlock(&current->fs->lock);
851 return 1;
854 int path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
856 nd->last_type = LAST_ROOT; /* if there are only slashes... */
857 nd->flags = flags;
859 read_lock(&current->fs->lock);
860 if (*name=='/') {
861 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
862 nd->mnt = mntget(current->fs->altrootmnt);
863 nd->dentry = dget(current->fs->altroot);
864 read_unlock(&current->fs->lock);
865 if (__emul_lookup_dentry(name,nd))
866 return 0;
867 read_lock(&current->fs->lock);
869 nd->mnt = mntget(current->fs->rootmnt);
870 nd->dentry = dget(current->fs->root);
872 else{
873 nd->mnt = mntget(current->fs->pwdmnt);
874 nd->dentry = dget(current->fs->pwd);
876 read_unlock(&current->fs->lock);
877 current->total_link_count = 0;
878 return link_path_walk(name, nd);
882 * Restricted form of lookup. Doesn't follow links, single-component only,
883 * needs parent already locked. Doesn't follow mounts.
884 * SMP-safe.
886 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
888 struct dentry * dentry;
889 struct inode *inode;
890 int err;
892 inode = base->d_inode;
893 err = permission(inode, MAY_EXEC, nd);
894 dentry = ERR_PTR(err);
895 if (err)
896 goto out;
899 * See if the low-level filesystem might want
900 * to use its own hash..
902 if (base->d_op && base->d_op->d_hash) {
903 err = base->d_op->d_hash(base, name);
904 dentry = ERR_PTR(err);
905 if (err < 0)
906 goto out;
909 dentry = cached_lookup(base, name, nd);
910 if (!dentry) {
911 struct dentry *new = d_alloc(base, name);
912 dentry = ERR_PTR(-ENOMEM);
913 if (!new)
914 goto out;
915 dentry = inode->i_op->lookup(inode, new, nd);
916 if (!dentry)
917 dentry = new;
918 else
919 dput(new);
921 out:
922 return dentry;
925 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
927 return __lookup_hash(name, base, NULL);
930 /* SMP-safe */
931 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
933 unsigned long hash;
934 struct qstr this;
935 unsigned int c;
937 this.name = name;
938 this.len = len;
939 if (!len)
940 goto access;
942 hash = init_name_hash();
943 while (len--) {
944 c = *(const unsigned char *)name++;
945 if (c == '/' || c == '\0')
946 goto access;
947 hash = partial_name_hash(c, hash);
949 this.hash = end_name_hash(hash);
951 return lookup_hash(&this, base);
952 access:
953 return ERR_PTR(-EACCES);
957 * namei()
959 * is used by most simple commands to get the inode of a specified name.
960 * Open, link etc use their own routines, but this is enough for things
961 * like 'chmod' etc.
963 * namei exists in two versions: namei/lnamei. The only difference is
964 * that namei follows links, while lnamei does not.
965 * SMP-safe
967 int __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
969 char *tmp = getname(name);
970 int err = PTR_ERR(tmp);
972 if (!IS_ERR(tmp)) {
973 err = path_lookup(tmp, flags, nd);
974 putname(tmp);
976 return err;
980 * It's inline, so penalty for filesystems that don't use sticky bit is
981 * minimal.
983 static inline int check_sticky(struct inode *dir, struct inode *inode)
985 if (!(dir->i_mode & S_ISVTX))
986 return 0;
987 if (inode->i_uid == current->fsuid)
988 return 0;
989 if (dir->i_uid == current->fsuid)
990 return 0;
991 return !capable(CAP_FOWNER);
995 * Check whether we can remove a link victim from directory dir, check
996 * whether the type of victim is right.
997 * 1. We can't do it if dir is read-only (done in permission())
998 * 2. We should have write and exec permissions on dir
999 * 3. We can't remove anything from append-only dir
1000 * 4. We can't do anything with immutable dir (done in permission())
1001 * 5. If the sticky bit on dir is set we should either
1002 * a. be owner of dir, or
1003 * b. be owner of victim, or
1004 * c. have CAP_FOWNER capability
1005 * 6. If the victim is append-only or immutable we can't do antyhing with
1006 * links pointing to it.
1007 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1008 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1009 * 9. We can't remove a root or mountpoint.
1010 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1011 * nfs_async_unlink().
1013 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1015 int error;
1016 if (!victim->d_inode || victim->d_parent->d_inode != dir)
1017 return -ENOENT;
1018 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1019 if (error)
1020 return error;
1021 if (IS_APPEND(dir))
1022 return -EPERM;
1023 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1024 IS_IMMUTABLE(victim->d_inode))
1025 return -EPERM;
1026 if (isdir) {
1027 if (!S_ISDIR(victim->d_inode->i_mode))
1028 return -ENOTDIR;
1029 if (IS_ROOT(victim))
1030 return -EBUSY;
1031 } else if (S_ISDIR(victim->d_inode->i_mode))
1032 return -EISDIR;
1033 if (IS_DEADDIR(dir))
1034 return -ENOENT;
1035 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1036 return -EBUSY;
1037 return 0;
1040 /* Check whether we can create an object with dentry child in directory
1041 * dir.
1042 * 1. We can't do it if child already exists (open has special treatment for
1043 * this case, but since we are inlined it's OK)
1044 * 2. We can't do it if dir is read-only (done in permission())
1045 * 3. We should have write and exec permissions on dir
1046 * 4. We can't do it if dir is immutable (done in permission())
1048 static inline int may_create(struct inode *dir, struct dentry *child,
1049 struct nameidata *nd)
1051 if (child->d_inode)
1052 return -EEXIST;
1053 if (IS_DEADDIR(dir))
1054 return -ENOENT;
1055 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1059 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1060 * reasons.
1062 * O_DIRECTORY translates into forcing a directory lookup.
1064 static inline int lookup_flags(unsigned int f)
1066 unsigned long retval = LOOKUP_FOLLOW;
1068 if (f & O_NOFOLLOW)
1069 retval &= ~LOOKUP_FOLLOW;
1071 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1072 retval &= ~LOOKUP_FOLLOW;
1074 if (f & O_DIRECTORY)
1075 retval |= LOOKUP_DIRECTORY;
1077 return retval;
1081 * p1 and p2 should be directories on the same fs.
1083 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1085 struct dentry *p;
1087 if (p1 == p2) {
1088 down(&p1->d_inode->i_sem);
1089 return NULL;
1092 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1094 for (p = p1; p->d_parent != p; p = p->d_parent) {
1095 if (p->d_parent == p2) {
1096 down(&p2->d_inode->i_sem);
1097 down(&p1->d_inode->i_sem);
1098 return p;
1102 for (p = p2; p->d_parent != p; p = p->d_parent) {
1103 if (p->d_parent == p1) {
1104 down(&p1->d_inode->i_sem);
1105 down(&p2->d_inode->i_sem);
1106 return p;
1110 down(&p1->d_inode->i_sem);
1111 down(&p2->d_inode->i_sem);
1112 return NULL;
1115 void unlock_rename(struct dentry *p1, struct dentry *p2)
1117 up(&p1->d_inode->i_sem);
1118 if (p1 != p2) {
1119 up(&p2->d_inode->i_sem);
1120 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1124 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1125 struct nameidata *nd)
1127 int error = may_create(dir, dentry, nd);
1129 if (error)
1130 return error;
1132 if (!dir->i_op || !dir->i_op->create)
1133 return -EACCES; /* shouldn't it be ENOSYS? */
1134 mode &= S_IALLUGO;
1135 mode |= S_IFREG;
1136 error = security_inode_create(dir, dentry, mode);
1137 if (error)
1138 return error;
1139 DQUOT_INIT(dir);
1140 error = dir->i_op->create(dir, dentry, mode, nd);
1141 if (!error) {
1142 inode_dir_notify(dir, DN_CREATE);
1143 security_inode_post_create(dir, dentry, mode);
1145 return error;
1148 int may_open(struct nameidata *nd, int acc_mode, int flag)
1150 struct dentry *dentry = nd->dentry;
1151 struct inode *inode = dentry->d_inode;
1152 int error;
1154 if (!inode)
1155 return -ENOENT;
1157 if (S_ISLNK(inode->i_mode))
1158 return -ELOOP;
1160 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1161 return -EISDIR;
1163 error = permission(inode, acc_mode, nd);
1164 if (error)
1165 return error;
1168 * FIFO's, sockets and device files are special: they don't
1169 * actually live on the filesystem itself, and as such you
1170 * can write to them even if the filesystem is read-only.
1172 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1173 flag &= ~O_TRUNC;
1174 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1175 if (nd->mnt->mnt_flags & MNT_NODEV)
1176 return -EACCES;
1178 flag &= ~O_TRUNC;
1179 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1180 return -EROFS;
1182 * An append-only file must be opened in append mode for writing.
1184 if (IS_APPEND(inode)) {
1185 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1186 return -EPERM;
1187 if (flag & O_TRUNC)
1188 return -EPERM;
1192 * Ensure there are no outstanding leases on the file.
1194 error = break_lease(inode, flag);
1195 if (error)
1196 return error;
1198 if (flag & O_TRUNC) {
1199 error = get_write_access(inode);
1200 if (error)
1201 return error;
1204 * Refuse to truncate files with mandatory locks held on them.
1206 error = locks_verify_locked(inode);
1207 if (!error) {
1208 DQUOT_INIT(inode);
1210 error = do_truncate(dentry, 0);
1212 put_write_access(inode);
1213 if (error)
1214 return error;
1215 } else
1216 if (flag & FMODE_WRITE)
1217 DQUOT_INIT(inode);
1219 return 0;
1223 * open_namei()
1225 * namei for open - this is in fact almost the whole open-routine.
1227 * Note that the low bits of "flag" aren't the same as in the open
1228 * system call - they are 00 - no permissions needed
1229 * 01 - read permission needed
1230 * 10 - write permission needed
1231 * 11 - read/write permissions needed
1232 * which is a lot more logical, and also allows the "no perm" needed
1233 * for symlinks (where the permissions are checked later).
1234 * SMP-safe
1236 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1238 int acc_mode, error = 0;
1239 struct dentry *dentry;
1240 struct dentry *dir;
1241 int count = 0;
1243 acc_mode = ACC_MODE(flag);
1245 /* Allow the LSM permission hook to distinguish append
1246 access from general write access. */
1247 if (flag & O_APPEND)
1248 acc_mode |= MAY_APPEND;
1250 /* Fill in the open() intent data */
1251 nd->intent.open.flags = flag;
1252 nd->intent.open.create_mode = mode;
1255 * The simplest case - just a plain lookup.
1257 if (!(flag & O_CREAT)) {
1258 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1259 if (error)
1260 return error;
1261 dentry = nd->dentry;
1262 goto ok;
1266 * Create - we need to know the parent.
1268 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1269 if (error)
1270 return error;
1273 * We have the parent and last component. First of all, check
1274 * that we are not asked to creat(2) an obvious directory - that
1275 * will not do.
1277 error = -EISDIR;
1278 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1279 goto exit;
1281 dir = nd->dentry;
1282 nd->flags &= ~LOOKUP_PARENT;
1283 down(&dir->d_inode->i_sem);
1284 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1286 do_last:
1287 error = PTR_ERR(dentry);
1288 if (IS_ERR(dentry)) {
1289 up(&dir->d_inode->i_sem);
1290 goto exit;
1293 /* Negative dentry, just create the file */
1294 if (!dentry->d_inode) {
1295 if (!IS_POSIXACL(dir->d_inode))
1296 mode &= ~current->fs->umask;
1297 error = vfs_create(dir->d_inode, dentry, mode, nd);
1298 up(&dir->d_inode->i_sem);
1299 dput(nd->dentry);
1300 nd->dentry = dentry;
1301 if (error)
1302 goto exit;
1303 /* Don't check for write permission, don't truncate */
1304 acc_mode = 0;
1305 flag &= ~O_TRUNC;
1306 goto ok;
1310 * It already exists.
1312 up(&dir->d_inode->i_sem);
1314 error = -EEXIST;
1315 if (flag & O_EXCL)
1316 goto exit_dput;
1318 if (d_mountpoint(dentry)) {
1319 error = -ELOOP;
1320 if (flag & O_NOFOLLOW)
1321 goto exit_dput;
1322 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1324 error = -ENOENT;
1325 if (!dentry->d_inode)
1326 goto exit_dput;
1327 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1328 goto do_link;
1330 dput(nd->dentry);
1331 nd->dentry = dentry;
1332 error = -EISDIR;
1333 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1334 goto exit;
1336 error = may_open(nd, acc_mode, flag);
1337 if (error)
1338 goto exit;
1339 return 0;
1341 exit_dput:
1342 dput(dentry);
1343 exit:
1344 path_release(nd);
1345 return error;
1347 do_link:
1348 error = -ELOOP;
1349 if (flag & O_NOFOLLOW)
1350 goto exit_dput;
1352 * This is subtle. Instead of calling do_follow_link() we do the
1353 * thing by hands. The reason is that this way we have zero link_count
1354 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1355 * After that we have the parent and last component, i.e.
1356 * we are in the same situation as after the first path_walk().
1357 * Well, almost - if the last component is normal we get its copy
1358 * stored in nd->last.name and we will have to putname() it when we
1359 * are done. Procfs-like symlinks just set LAST_BIND.
1361 nd->flags |= LOOKUP_PARENT;
1362 error = security_inode_follow_link(dentry, nd);
1363 if (error)
1364 goto exit_dput;
1365 update_atime(dentry->d_inode);
1366 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1367 dput(dentry);
1368 if (error)
1369 return error;
1370 nd->flags &= ~LOOKUP_PARENT;
1371 if (nd->last_type == LAST_BIND) {
1372 dentry = nd->dentry;
1373 goto ok;
1375 error = -EISDIR;
1376 if (nd->last_type != LAST_NORM)
1377 goto exit;
1378 if (nd->last.name[nd->last.len]) {
1379 putname(nd->last.name);
1380 goto exit;
1382 error = -ELOOP;
1383 if (count++==32) {
1384 putname(nd->last.name);
1385 goto exit;
1387 dir = nd->dentry;
1388 down(&dir->d_inode->i_sem);
1389 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1390 putname(nd->last.name);
1391 goto do_last;
1395 * lookup_create - lookup a dentry, creating it if it doesn't exist
1396 * @nd: nameidata info
1397 * @is_dir: directory flag
1399 * Simple function to lookup and return a dentry and create it
1400 * if it doesn't exist. Is SMP-safe.
1402 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1404 struct dentry *dentry;
1406 down(&nd->dentry->d_inode->i_sem);
1407 dentry = ERR_PTR(-EEXIST);
1408 if (nd->last_type != LAST_NORM)
1409 goto fail;
1410 nd->flags &= ~LOOKUP_PARENT;
1411 dentry = lookup_hash(&nd->last, nd->dentry);
1412 if (IS_ERR(dentry))
1413 goto fail;
1414 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1415 goto enoent;
1416 return dentry;
1417 enoent:
1418 dput(dentry);
1419 dentry = ERR_PTR(-ENOENT);
1420 fail:
1421 return dentry;
1424 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1426 int error = may_create(dir, dentry, NULL);
1428 if (error)
1429 return error;
1431 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1432 return -EPERM;
1434 if (!dir->i_op || !dir->i_op->mknod)
1435 return -EPERM;
1437 error = security_inode_mknod(dir, dentry, mode, dev);
1438 if (error)
1439 return error;
1441 DQUOT_INIT(dir);
1442 error = dir->i_op->mknod(dir, dentry, mode, dev);
1443 if (!error) {
1444 inode_dir_notify(dir, DN_CREATE);
1445 security_inode_post_mknod(dir, dentry, mode, dev);
1447 return error;
1450 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1452 int error = 0;
1453 char * tmp;
1454 struct dentry * dentry;
1455 struct nameidata nd;
1457 if (S_ISDIR(mode))
1458 return -EPERM;
1459 tmp = getname(filename);
1460 if (IS_ERR(tmp))
1461 return PTR_ERR(tmp);
1463 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1464 if (error)
1465 goto out;
1466 dentry = lookup_create(&nd, 0);
1467 error = PTR_ERR(dentry);
1469 if (!IS_POSIXACL(nd.dentry->d_inode))
1470 mode &= ~current->fs->umask;
1471 if (!IS_ERR(dentry)) {
1472 switch (mode & S_IFMT) {
1473 case 0: case S_IFREG:
1474 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1475 break;
1476 case S_IFCHR: case S_IFBLK:
1477 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1478 new_decode_dev(dev));
1479 break;
1480 case S_IFIFO: case S_IFSOCK:
1481 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1482 break;
1483 case S_IFDIR:
1484 error = -EPERM;
1485 break;
1486 default:
1487 error = -EINVAL;
1489 dput(dentry);
1491 up(&nd.dentry->d_inode->i_sem);
1492 path_release(&nd);
1493 out:
1494 putname(tmp);
1496 return error;
1499 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1501 int error = may_create(dir, dentry, NULL);
1503 if (error)
1504 return error;
1506 if (!dir->i_op || !dir->i_op->mkdir)
1507 return -EPERM;
1509 mode &= (S_IRWXUGO|S_ISVTX);
1510 error = security_inode_mkdir(dir, dentry, mode);
1511 if (error)
1512 return error;
1514 DQUOT_INIT(dir);
1515 error = dir->i_op->mkdir(dir, dentry, mode);
1516 if (!error) {
1517 inode_dir_notify(dir, DN_CREATE);
1518 security_inode_post_mkdir(dir,dentry, mode);
1520 return error;
1523 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1525 int error = 0;
1526 char * tmp;
1528 tmp = getname(pathname);
1529 error = PTR_ERR(tmp);
1530 if (!IS_ERR(tmp)) {
1531 struct dentry *dentry;
1532 struct nameidata nd;
1534 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1535 if (error)
1536 goto out;
1537 dentry = lookup_create(&nd, 1);
1538 error = PTR_ERR(dentry);
1539 if (!IS_ERR(dentry)) {
1540 if (!IS_POSIXACL(nd.dentry->d_inode))
1541 mode &= ~current->fs->umask;
1542 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1543 dput(dentry);
1545 up(&nd.dentry->d_inode->i_sem);
1546 path_release(&nd);
1547 out:
1548 putname(tmp);
1551 return error;
1555 * We try to drop the dentry early: we should have
1556 * a usage count of 2 if we're the only user of this
1557 * dentry, and if that is true (possibly after pruning
1558 * the dcache), then we drop the dentry now.
1560 * A low-level filesystem can, if it choses, legally
1561 * do a
1563 * if (!d_unhashed(dentry))
1564 * return -EBUSY;
1566 * if it cannot handle the case of removing a directory
1567 * that is still in use by something else..
1569 static void d_unhash(struct dentry *dentry)
1571 dget(dentry);
1572 spin_lock(&dcache_lock);
1573 switch (atomic_read(&dentry->d_count)) {
1574 default:
1575 spin_unlock(&dcache_lock);
1576 shrink_dcache_parent(dentry);
1577 spin_lock(&dcache_lock);
1578 if (atomic_read(&dentry->d_count) != 2)
1579 break;
1580 case 2:
1581 __d_drop(dentry);
1583 spin_unlock(&dcache_lock);
1586 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1588 int error = may_delete(dir, dentry, 1);
1590 if (error)
1591 return error;
1593 if (!dir->i_op || !dir->i_op->rmdir)
1594 return -EPERM;
1596 DQUOT_INIT(dir);
1598 down(&dentry->d_inode->i_sem);
1599 d_unhash(dentry);
1600 if (d_mountpoint(dentry))
1601 error = -EBUSY;
1602 else {
1603 error = security_inode_rmdir(dir, dentry);
1604 if (!error) {
1605 error = dir->i_op->rmdir(dir, dentry);
1606 if (!error)
1607 dentry->d_inode->i_flags |= S_DEAD;
1610 up(&dentry->d_inode->i_sem);
1611 if (!error) {
1612 inode_dir_notify(dir, DN_DELETE);
1613 d_delete(dentry);
1615 dput(dentry);
1617 return error;
1620 asmlinkage long sys_rmdir(const char __user * pathname)
1622 int error = 0;
1623 char * name;
1624 struct dentry *dentry;
1625 struct nameidata nd;
1627 name = getname(pathname);
1628 if(IS_ERR(name))
1629 return PTR_ERR(name);
1631 error = path_lookup(name, LOOKUP_PARENT, &nd);
1632 if (error)
1633 goto exit;
1635 switch(nd.last_type) {
1636 case LAST_DOTDOT:
1637 error = -ENOTEMPTY;
1638 goto exit1;
1639 case LAST_DOT:
1640 error = -EINVAL;
1641 goto exit1;
1642 case LAST_ROOT:
1643 error = -EBUSY;
1644 goto exit1;
1646 down(&nd.dentry->d_inode->i_sem);
1647 dentry = lookup_hash(&nd.last, nd.dentry);
1648 error = PTR_ERR(dentry);
1649 if (!IS_ERR(dentry)) {
1650 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1651 dput(dentry);
1653 up(&nd.dentry->d_inode->i_sem);
1654 exit1:
1655 path_release(&nd);
1656 exit:
1657 putname(name);
1658 return error;
1661 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1663 int error = may_delete(dir, dentry, 0);
1665 if (error)
1666 return error;
1668 if (!dir->i_op || !dir->i_op->unlink)
1669 return -EPERM;
1671 DQUOT_INIT(dir);
1673 down(&dentry->d_inode->i_sem);
1674 if (d_mountpoint(dentry))
1675 error = -EBUSY;
1676 else {
1677 error = security_inode_unlink(dir, dentry);
1678 if (!error)
1679 error = dir->i_op->unlink(dir, dentry);
1681 up(&dentry->d_inode->i_sem);
1683 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1684 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1685 d_delete(dentry);
1686 inode_dir_notify(dir, DN_DELETE);
1688 return error;
1692 * Make sure that the actual truncation of the file will occur outside its
1693 * directory's i_sem. Truncate can take a long time if there is a lot of
1694 * writeout happening, and we don't want to prevent access to the directory
1695 * while waiting on the I/O.
1697 asmlinkage long sys_unlink(const char __user * pathname)
1699 int error = 0;
1700 char * name;
1701 struct dentry *dentry;
1702 struct nameidata nd;
1703 struct inode *inode = NULL;
1705 name = getname(pathname);
1706 if(IS_ERR(name))
1707 return PTR_ERR(name);
1709 error = path_lookup(name, LOOKUP_PARENT, &nd);
1710 if (error)
1711 goto exit;
1712 error = -EISDIR;
1713 if (nd.last_type != LAST_NORM)
1714 goto exit1;
1715 down(&nd.dentry->d_inode->i_sem);
1716 dentry = lookup_hash(&nd.last, nd.dentry);
1717 error = PTR_ERR(dentry);
1718 if (!IS_ERR(dentry)) {
1719 /* Why not before? Because we want correct error value */
1720 if (nd.last.name[nd.last.len])
1721 goto slashes;
1722 inode = dentry->d_inode;
1723 if (inode)
1724 atomic_inc(&inode->i_count);
1725 error = vfs_unlink(nd.dentry->d_inode, dentry);
1726 exit2:
1727 dput(dentry);
1729 up(&nd.dentry->d_inode->i_sem);
1730 exit1:
1731 path_release(&nd);
1732 exit:
1733 putname(name);
1735 if (inode)
1736 iput(inode); /* truncate the inode here */
1737 return error;
1739 slashes:
1740 error = !dentry->d_inode ? -ENOENT :
1741 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1742 goto exit2;
1745 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1747 int error = may_create(dir, dentry, NULL);
1749 if (error)
1750 return error;
1752 if (!dir->i_op || !dir->i_op->symlink)
1753 return -EPERM;
1755 error = security_inode_symlink(dir, dentry, oldname);
1756 if (error)
1757 return error;
1759 DQUOT_INIT(dir);
1760 error = dir->i_op->symlink(dir, dentry, oldname);
1761 if (!error) {
1762 inode_dir_notify(dir, DN_CREATE);
1763 security_inode_post_symlink(dir, dentry, oldname);
1765 return error;
1768 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1770 int error = 0;
1771 char * from;
1772 char * to;
1774 from = getname(oldname);
1775 if(IS_ERR(from))
1776 return PTR_ERR(from);
1777 to = getname(newname);
1778 error = PTR_ERR(to);
1779 if (!IS_ERR(to)) {
1780 struct dentry *dentry;
1781 struct nameidata nd;
1783 error = path_lookup(to, LOOKUP_PARENT, &nd);
1784 if (error)
1785 goto out;
1786 dentry = lookup_create(&nd, 0);
1787 error = PTR_ERR(dentry);
1788 if (!IS_ERR(dentry)) {
1789 error = vfs_symlink(nd.dentry->d_inode, dentry, from);
1790 dput(dentry);
1792 up(&nd.dentry->d_inode->i_sem);
1793 path_release(&nd);
1794 out:
1795 putname(to);
1797 putname(from);
1798 return error;
1801 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1803 struct inode *inode = old_dentry->d_inode;
1804 int error;
1806 if (!inode)
1807 return -ENOENT;
1809 error = may_create(dir, new_dentry, NULL);
1810 if (error)
1811 return error;
1813 if (dir->i_sb != inode->i_sb)
1814 return -EXDEV;
1817 * A link to an append-only or immutable file cannot be created.
1819 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1820 return -EPERM;
1821 if (!dir->i_op || !dir->i_op->link)
1822 return -EPERM;
1823 if (S_ISDIR(old_dentry->d_inode->i_mode))
1824 return -EPERM;
1826 error = security_inode_link(old_dentry, dir, new_dentry);
1827 if (error)
1828 return error;
1830 down(&old_dentry->d_inode->i_sem);
1831 DQUOT_INIT(dir);
1832 error = dir->i_op->link(old_dentry, dir, new_dentry);
1833 up(&old_dentry->d_inode->i_sem);
1834 if (!error) {
1835 inode_dir_notify(dir, DN_CREATE);
1836 security_inode_post_link(old_dentry, dir, new_dentry);
1838 return error;
1842 * Hardlinks are often used in delicate situations. We avoid
1843 * security-related surprises by not following symlinks on the
1844 * newname. --KAB
1846 * We don't follow them on the oldname either to be compatible
1847 * with linux 2.0, and to avoid hard-linking to directories
1848 * and other special files. --ADM
1850 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
1852 struct dentry *new_dentry;
1853 struct nameidata nd, old_nd;
1854 int error;
1855 char * to;
1857 to = getname(newname);
1858 if (IS_ERR(to))
1859 return PTR_ERR(to);
1861 error = __user_walk(oldname, 0, &old_nd);
1862 if (error)
1863 goto exit;
1864 error = path_lookup(to, LOOKUP_PARENT, &nd);
1865 if (error)
1866 goto out;
1867 error = -EXDEV;
1868 if (old_nd.mnt != nd.mnt)
1869 goto out_release;
1870 new_dentry = lookup_create(&nd, 0);
1871 error = PTR_ERR(new_dentry);
1872 if (!IS_ERR(new_dentry)) {
1873 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
1874 dput(new_dentry);
1876 up(&nd.dentry->d_inode->i_sem);
1877 out_release:
1878 path_release(&nd);
1879 out:
1880 path_release(&old_nd);
1881 exit:
1882 putname(to);
1884 return error;
1888 * The worst of all namespace operations - renaming directory. "Perverted"
1889 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1890 * Problems:
1891 * a) we can get into loop creation. Check is done in is_subdir().
1892 * b) race potential - two innocent renames can create a loop together.
1893 * That's where 4.4 screws up. Current fix: serialization on
1894 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1895 * story.
1896 * c) we have to lock _three_ objects - parents and victim (if it exists).
1897 * And that - after we got ->i_sem on parents (until then we don't know
1898 * whether the target exists). Solution: try to be smart with locking
1899 * order for inodes. We rely on the fact that tree topology may change
1900 * only under ->s_vfs_rename_sem _and_ that parent of the object we
1901 * move will be locked. Thus we can rank directories by the tree
1902 * (ancestors first) and rank all non-directories after them.
1903 * That works since everybody except rename does "lock parent, lookup,
1904 * lock child" and rename is under ->s_vfs_rename_sem.
1905 * HOWEVER, it relies on the assumption that any object with ->lookup()
1906 * has no more than 1 dentry. If "hybrid" objects will ever appear,
1907 * we'd better make sure that there's no link(2) for them.
1908 * d) some filesystems don't support opened-but-unlinked directories,
1909 * either because of layout or because they are not ready to deal with
1910 * all cases correctly. The latter will be fixed (taking this sort of
1911 * stuff into VFS), but the former is not going away. Solution: the same
1912 * trick as in rmdir().
1913 * e) conversion from fhandle to dentry may come in the wrong moment - when
1914 * we are removing the target. Solution: we will have to grab ->i_sem
1915 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1916 * ->i_sem on parents, which works but leads to some truely excessive
1917 * locking].
1919 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
1920 struct inode *new_dir, struct dentry *new_dentry)
1922 int error = 0;
1923 struct inode *target;
1926 * If we are going to change the parent - check write permissions,
1927 * we'll need to flip '..'.
1929 if (new_dir != old_dir) {
1930 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
1931 if (error)
1932 return error;
1935 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
1936 if (error)
1937 return error;
1939 target = new_dentry->d_inode;
1940 if (target) {
1941 down(&target->i_sem);
1942 d_unhash(new_dentry);
1944 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1945 error = -EBUSY;
1946 else
1947 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1948 if (target) {
1949 if (!error)
1950 target->i_flags |= S_DEAD;
1951 up(&target->i_sem);
1952 if (d_unhashed(new_dentry))
1953 d_rehash(new_dentry);
1954 dput(new_dentry);
1956 if (!error) {
1957 d_move(old_dentry,new_dentry);
1958 security_inode_post_rename(old_dir, old_dentry,
1959 new_dir, new_dentry);
1961 return error;
1964 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
1965 struct inode *new_dir, struct dentry *new_dentry)
1967 struct inode *target;
1968 int error;
1970 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
1971 if (error)
1972 return error;
1974 dget(new_dentry);
1975 target = new_dentry->d_inode;
1976 if (target)
1977 down(&target->i_sem);
1978 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1979 error = -EBUSY;
1980 else
1981 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1982 if (!error) {
1983 /* The following d_move() should become unconditional */
1984 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
1985 d_move(old_dentry, new_dentry);
1986 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
1988 if (target)
1989 up(&target->i_sem);
1990 dput(new_dentry);
1991 return error;
1994 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1995 struct inode *new_dir, struct dentry *new_dentry)
1997 int error;
1998 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2000 if (old_dentry->d_inode == new_dentry->d_inode)
2001 return 0;
2003 error = may_delete(old_dir, old_dentry, is_dir);
2004 if (error)
2005 return error;
2007 if (!new_dentry->d_inode)
2008 error = may_create(new_dir, new_dentry, NULL);
2009 else
2010 error = may_delete(new_dir, new_dentry, is_dir);
2011 if (error)
2012 return error;
2014 if (!old_dir->i_op || !old_dir->i_op->rename)
2015 return -EPERM;
2017 DQUOT_INIT(old_dir);
2018 DQUOT_INIT(new_dir);
2020 if (is_dir)
2021 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2022 else
2023 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2024 if (!error) {
2025 if (old_dir == new_dir)
2026 inode_dir_notify(old_dir, DN_RENAME);
2027 else {
2028 inode_dir_notify(old_dir, DN_DELETE);
2029 inode_dir_notify(new_dir, DN_CREATE);
2032 return error;
2035 static inline int do_rename(const char * oldname, const char * newname)
2037 int error = 0;
2038 struct dentry * old_dir, * new_dir;
2039 struct dentry * old_dentry, *new_dentry;
2040 struct dentry * trap;
2041 struct nameidata oldnd, newnd;
2043 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2044 if (error)
2045 goto exit;
2047 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2048 if (error)
2049 goto exit1;
2051 error = -EXDEV;
2052 if (oldnd.mnt != newnd.mnt)
2053 goto exit2;
2055 old_dir = oldnd.dentry;
2056 error = -EBUSY;
2057 if (oldnd.last_type != LAST_NORM)
2058 goto exit2;
2060 new_dir = newnd.dentry;
2061 if (newnd.last_type != LAST_NORM)
2062 goto exit2;
2064 trap = lock_rename(new_dir, old_dir);
2066 old_dentry = lookup_hash(&oldnd.last, old_dir);
2067 error = PTR_ERR(old_dentry);
2068 if (IS_ERR(old_dentry))
2069 goto exit3;
2070 /* source must exist */
2071 error = -ENOENT;
2072 if (!old_dentry->d_inode)
2073 goto exit4;
2074 /* unless the source is a directory trailing slashes give -ENOTDIR */
2075 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2076 error = -ENOTDIR;
2077 if (oldnd.last.name[oldnd.last.len])
2078 goto exit4;
2079 if (newnd.last.name[newnd.last.len])
2080 goto exit4;
2082 /* source should not be ancestor of target */
2083 error = -EINVAL;
2084 if (old_dentry == trap)
2085 goto exit4;
2086 new_dentry = lookup_hash(&newnd.last, new_dir);
2087 error = PTR_ERR(new_dentry);
2088 if (IS_ERR(new_dentry))
2089 goto exit4;
2090 /* target should not be an ancestor of source */
2091 error = -ENOTEMPTY;
2092 if (new_dentry == trap)
2093 goto exit5;
2095 error = vfs_rename(old_dir->d_inode, old_dentry,
2096 new_dir->d_inode, new_dentry);
2097 exit5:
2098 dput(new_dentry);
2099 exit4:
2100 dput(old_dentry);
2101 exit3:
2102 unlock_rename(new_dir, old_dir);
2103 exit2:
2104 path_release(&newnd);
2105 exit1:
2106 path_release(&oldnd);
2107 exit:
2108 return error;
2111 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2113 int error;
2114 char * from;
2115 char * to;
2117 from = getname(oldname);
2118 if(IS_ERR(from))
2119 return PTR_ERR(from);
2120 to = getname(newname);
2121 error = PTR_ERR(to);
2122 if (!IS_ERR(to)) {
2123 error = do_rename(from,to);
2124 putname(to);
2126 putname(from);
2127 return error;
2130 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2132 int len;
2134 len = PTR_ERR(link);
2135 if (IS_ERR(link))
2136 goto out;
2138 len = strlen(link);
2139 if (len > (unsigned) buflen)
2140 len = buflen;
2141 if (copy_to_user(buffer, link, len))
2142 len = -EFAULT;
2143 out:
2144 return len;
2147 static inline int
2148 __vfs_follow_link(struct nameidata *nd, const char *link)
2150 int res = 0;
2151 char *name;
2152 if (IS_ERR(link))
2153 goto fail;
2155 if (*link == '/') {
2156 path_release(nd);
2157 if (!walk_init_root(link, nd))
2158 /* weird __emul_prefix() stuff did it */
2159 goto out;
2161 res = link_path_walk(link, nd);
2162 out:
2163 if (current->link_count || res || nd->last_type!=LAST_NORM)
2164 return res;
2166 * If it is an iterative symlinks resolution in open_namei() we
2167 * have to copy the last component. And all that crap because of
2168 * bloody create() on broken symlinks. Furrfu...
2170 name = __getname();
2171 if (unlikely(!name)) {
2172 path_release(nd);
2173 return -ENOMEM;
2175 strcpy(name, nd->last.name);
2176 nd->last.name = name;
2177 return 0;
2178 fail:
2179 path_release(nd);
2180 return PTR_ERR(link);
2183 int vfs_follow_link(struct nameidata *nd, const char *link)
2185 return __vfs_follow_link(nd, link);
2188 /* get the link contents into pagecache */
2189 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2191 struct page * page;
2192 struct address_space *mapping = dentry->d_inode->i_mapping;
2193 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2194 NULL);
2195 if (IS_ERR(page))
2196 goto sync_fail;
2197 wait_on_page_locked(page);
2198 if (!PageUptodate(page))
2199 goto async_fail;
2200 *ppage = page;
2201 return kmap(page);
2203 async_fail:
2204 page_cache_release(page);
2205 return ERR_PTR(-EIO);
2207 sync_fail:
2208 return (char*)page;
2211 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2213 struct page *page = NULL;
2214 char *s = page_getlink(dentry, &page);
2215 int res = vfs_readlink(dentry,buffer,buflen,s);
2216 if (page) {
2217 kunmap(page);
2218 page_cache_release(page);
2220 return res;
2223 int page_follow_link(struct dentry *dentry, struct nameidata *nd)
2225 struct page *page = NULL;
2226 char *s = page_getlink(dentry, &page);
2227 int res = __vfs_follow_link(nd, s);
2228 if (page) {
2229 kunmap(page);
2230 page_cache_release(page);
2232 return res;
2235 int page_symlink(struct inode *inode, const char *symname, int len)
2237 struct address_space *mapping = inode->i_mapping;
2238 struct page *page = grab_cache_page(mapping, 0);
2239 int err = -ENOMEM;
2240 char *kaddr;
2242 if (!page)
2243 goto fail;
2244 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2245 if (err)
2246 goto fail_map;
2247 kaddr = kmap_atomic(page, KM_USER0);
2248 memcpy(kaddr, symname, len-1);
2249 kunmap_atomic(kaddr, KM_USER0);
2250 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2252 * Notice that we are _not_ going to block here - end of page is
2253 * unmapped, so this will only try to map the rest of page, see
2254 * that it is unmapped (typically even will not look into inode -
2255 * ->i_size will be enough for everything) and zero it out.
2256 * OTOH it's obviously correct and should make the page up-to-date.
2258 if (!PageUptodate(page)) {
2259 err = mapping->a_ops->readpage(NULL, page);
2260 wait_on_page_locked(page);
2261 } else {
2262 unlock_page(page);
2264 page_cache_release(page);
2265 if (err < 0)
2266 goto fail;
2267 mark_inode_dirty(inode);
2268 return 0;
2269 fail_map:
2270 unlock_page(page);
2271 page_cache_release(page);
2272 fail:
2273 return err;
2276 struct inode_operations page_symlink_inode_operations = {
2277 .readlink = page_readlink,
2278 .follow_link = page_follow_link,
2281 EXPORT_SYMBOL(__user_walk);
2282 EXPORT_SYMBOL(follow_down);
2283 EXPORT_SYMBOL(follow_up);
2284 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2285 EXPORT_SYMBOL(getname);
2286 EXPORT_SYMBOL(lock_rename);
2287 EXPORT_SYMBOL(lookup_create);
2288 EXPORT_SYMBOL(lookup_hash);
2289 EXPORT_SYMBOL(lookup_one_len);
2290 EXPORT_SYMBOL(page_follow_link);
2291 EXPORT_SYMBOL(page_readlink);
2292 EXPORT_SYMBOL(page_symlink);
2293 EXPORT_SYMBOL(page_symlink_inode_operations);
2294 EXPORT_SYMBOL(path_lookup);
2295 EXPORT_SYMBOL(path_release);
2296 EXPORT_SYMBOL(path_walk);
2297 EXPORT_SYMBOL(permission);
2298 EXPORT_SYMBOL(unlock_rename);
2299 EXPORT_SYMBOL(vfs_create);
2300 EXPORT_SYMBOL(vfs_follow_link);
2301 EXPORT_SYMBOL(vfs_link);
2302 EXPORT_SYMBOL(vfs_mkdir);
2303 EXPORT_SYMBOL(vfs_mknod);
2304 EXPORT_SYMBOL(vfs_permission);
2305 EXPORT_SYMBOL(vfs_readlink);
2306 EXPORT_SYMBOL(vfs_rename);
2307 EXPORT_SYMBOL(vfs_rmdir);
2308 EXPORT_SYMBOL(vfs_symlink);
2309 EXPORT_SYMBOL(vfs_unlink);