[PATCH] fix make rpm versioning
[linux-2.6/history.git] / fs / namei.c
blob8810138eaaec922967d224446dfe42859087d1e9
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/slab.h>
19 #include <linux/fs.h>
20 #include <linux/namei.h>
21 #include <linux/quotaops.h>
22 #include <linux/pagemap.h>
23 #include <linux/dnotify.h>
24 #include <linux/smp_lock.h>
25 #include <linux/personality.h>
26 #include <linux/security.h>
27 #include <linux/mount.h>
28 #include <asm/namei.h>
29 #include <asm/uaccess.h>
31 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
33 /* [Feb-1997 T. Schoebel-Theuer]
34 * Fundamental changes in the pathname lookup mechanisms (namei)
35 * were necessary because of omirr. The reason is that omirr needs
36 * to know the _real_ pathname, not the user-supplied one, in case
37 * of symlinks (and also when transname replacements occur).
39 * The new code replaces the old recursive symlink resolution with
40 * an iterative one (in case of non-nested symlink chains). It does
41 * this with calls to <fs>_follow_link().
42 * As a side effect, dir_namei(), _namei() and follow_link() are now
43 * replaced with a single function lookup_dentry() that can handle all
44 * the special cases of the former code.
46 * With the new dcache, the pathname is stored at each inode, at least as
47 * long as the refcount of the inode is positive. As a side effect, the
48 * size of the dcache depends on the inode cache and thus is dynamic.
50 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
51 * resolution to correspond with current state of the code.
53 * Note that the symlink resolution is not *completely* iterative.
54 * There is still a significant amount of tail- and mid- recursion in
55 * the algorithm. Also, note that <fs>_readlink() is not used in
56 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
57 * may return different results than <fs>_follow_link(). Many virtual
58 * filesystems (including /proc) exhibit this behavior.
61 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
62 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
63 * and the name already exists in form of a symlink, try to create the new
64 * name indicated by the symlink. The old code always complained that the
65 * name already exists, due to not following the symlink even if its target
66 * is nonexistent. The new semantics affects also mknod() and link() when
67 * the name is a symlink pointing to a non-existant name.
69 * I don't know which semantics is the right one, since I have no access
70 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
71 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
72 * "old" one. Personally, I think the new semantics is much more logical.
73 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
74 * file does succeed in both HP-UX and SunOs, but not in Solaris
75 * and in the old Linux semantics.
78 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
79 * semantics. See the comments in "open_namei" and "do_link" below.
81 * [10-Sep-98 Alan Modra] Another symlink change.
84 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
85 * inside the path - always follow.
86 * in the last component in creation/removal/renaming - never follow.
87 * if LOOKUP_FOLLOW passed - follow.
88 * if the pathname has trailing slashes - follow.
89 * otherwise - don't follow.
90 * (applied in that order).
92 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
93 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
94 * During the 2.4 we need to fix the userland stuff depending on it -
95 * hopefully we will be able to get rid of that wart in 2.5. So far only
96 * XEmacs seems to be relying on it...
99 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
100 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
101 * any extra contention...
104 /* In order to reduce some races, while at the same time doing additional
105 * checking and hopefully speeding things up, we copy filenames to the
106 * kernel data space before using them..
108 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
109 * PATH_MAX includes the nul terminator --RR.
111 static inline int do_getname(const char __user *filename, char *page)
113 int retval;
114 unsigned long len = PATH_MAX;
116 if ((unsigned long) filename >= TASK_SIZE) {
117 if (!segment_eq(get_fs(), KERNEL_DS))
118 return -EFAULT;
119 } else if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
120 len = TASK_SIZE - (unsigned long) filename;
122 retval = strncpy_from_user((char *)page, filename, len);
123 if (retval > 0) {
124 if (retval < len)
125 return 0;
126 return -ENAMETOOLONG;
127 } else if (!retval)
128 retval = -ENOENT;
129 return retval;
132 char * getname(const char __user * filename)
134 char *tmp, *result;
136 result = ERR_PTR(-ENOMEM);
137 tmp = __getname();
138 if (tmp) {
139 int retval = do_getname(filename, tmp);
141 result = tmp;
142 if (retval < 0) {
143 putname(tmp);
144 result = ERR_PTR(retval);
147 return result;
151 * vfs_permission()
153 * is used to check for read/write/execute permissions on a file.
154 * We use "fsuid" for this, letting us set arbitrary permissions
155 * for filesystem access without changing the "normal" uids which
156 * are used for other things..
158 int vfs_permission(struct inode * inode, int mask)
160 umode_t mode = inode->i_mode;
162 if (mask & MAY_WRITE) {
164 * Nobody gets write access to a read-only fs.
166 if (IS_RDONLY(inode) &&
167 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
168 return -EROFS;
171 * Nobody gets write access to an immutable file.
173 if (IS_IMMUTABLE(inode))
174 return -EACCES;
177 if (current->fsuid == inode->i_uid)
178 mode >>= 6;
179 else if (in_group_p(inode->i_gid))
180 mode >>= 3;
183 * If the DACs are ok we don't need any capability check.
185 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
186 return 0;
189 * Read/write DACs are always overridable.
190 * Executable DACs are overridable if at least one exec bit is set.
192 if ((mask & (MAY_READ|MAY_WRITE)) || (inode->i_mode & S_IXUGO))
193 if (capable(CAP_DAC_OVERRIDE))
194 return 0;
197 * Searching includes executable on directories, else just read.
199 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
200 if (capable(CAP_DAC_READ_SEARCH))
201 return 0;
203 return -EACCES;
206 int permission(struct inode * inode,int mask, struct nameidata *nd)
208 int retval;
209 int submask;
211 /* Ordinary permission routines do not understand MAY_APPEND. */
212 submask = mask & ~MAY_APPEND;
214 if (inode->i_op && inode->i_op->permission)
215 retval = inode->i_op->permission(inode, submask, nd);
216 else
217 retval = vfs_permission(inode, submask);
218 if (retval)
219 return retval;
221 return security_inode_permission(inode, mask);
225 * get_write_access() gets write permission for a file.
226 * put_write_access() releases this write permission.
227 * This is used for regular files.
228 * We cannot support write (and maybe mmap read-write shared) accesses and
229 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
230 * can have the following values:
231 * 0: no writers, no VM_DENYWRITE mappings
232 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
233 * > 0: (i_writecount) users are writing to the file.
235 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
236 * except for the cases where we don't hold i_writecount yet. Then we need to
237 * use {get,deny}_write_access() - these functions check the sign and refuse
238 * to do the change if sign is wrong. Exclusion between them is provided by
239 * spinlock (arbitration_lock) and I'll rip the second arsehole to the first
240 * who will try to move it in struct inode - just leave it here.
242 static spinlock_t arbitration_lock = SPIN_LOCK_UNLOCKED;
243 int get_write_access(struct inode * inode)
245 spin_lock(&arbitration_lock);
246 if (atomic_read(&inode->i_writecount) < 0) {
247 spin_unlock(&arbitration_lock);
248 return -ETXTBSY;
250 atomic_inc(&inode->i_writecount);
251 spin_unlock(&arbitration_lock);
252 return 0;
254 int deny_write_access(struct file * file)
256 spin_lock(&arbitration_lock);
257 if (atomic_read(&file->f_dentry->d_inode->i_writecount) > 0) {
258 spin_unlock(&arbitration_lock);
259 return -ETXTBSY;
261 atomic_dec(&file->f_dentry->d_inode->i_writecount);
262 spin_unlock(&arbitration_lock);
263 return 0;
266 void path_release(struct nameidata *nd)
268 dput(nd->dentry);
269 mntput(nd->mnt);
273 * Internal lookup() using the new generic dcache.
274 * SMP-safe
276 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
278 struct dentry * dentry = __d_lookup(parent, name);
280 /* lockess __d_lookup may fail due to concurrent d_move()
281 * in some unrelated directory, so try with d_lookup
283 if (!dentry)
284 dentry = d_lookup(parent, name);
286 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
287 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
288 dput(dentry);
289 dentry = NULL;
292 return dentry;
296 * Short-cut version of permission(), for calling by
297 * path_walk(), when dcache lock is held. Combines parts
298 * of permission() and vfs_permission(), and tests ONLY for
299 * MAY_EXEC permission.
301 * If appropriate, check DAC only. If not appropriate, or
302 * short-cut DAC fails, then call permission() to do more
303 * complete permission check.
305 static inline int exec_permission_lite(struct inode *inode)
307 umode_t mode = inode->i_mode;
309 if ((inode->i_op && inode->i_op->permission))
310 return -EAGAIN;
312 if (current->fsuid == inode->i_uid)
313 mode >>= 6;
314 else if (in_group_p(inode->i_gid))
315 mode >>= 3;
317 if (mode & MAY_EXEC)
318 goto ok;
320 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
321 goto ok;
323 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
324 goto ok;
326 return -EACCES;
328 return security_inode_permission(inode, MAY_EXEC);
332 * This is called when everything else fails, and we actually have
333 * to go to the low-level filesystem to find out what we should do..
335 * We get the directory semaphore, and after getting that we also
336 * make sure that nobody added the entry to the dcache in the meantime..
337 * SMP-safe
339 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
341 struct dentry * result;
342 struct inode *dir = parent->d_inode;
344 down(&dir->i_sem);
346 * First re-do the cached lookup just in case it was created
347 * while we waited for the directory semaphore..
349 * FIXME! This could use version numbering or similar to
350 * avoid unnecessary cache lookups.
352 * The "dcache_lock" is purely to protect the RCU list walker
353 * from concurrent renames at this point (we mustn't get false
354 * negatives from the RCU list walk here, unlike the optimistic
355 * fast walk).
357 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
359 result = d_lookup(parent, name);
360 if (!result) {
361 struct dentry * dentry = d_alloc(parent, name);
362 result = ERR_PTR(-ENOMEM);
363 if (dentry) {
364 result = dir->i_op->lookup(dir, dentry, nd);
365 if (result)
366 dput(dentry);
367 else
368 result = dentry;
370 up(&dir->i_sem);
371 return result;
375 * Uhhuh! Nasty case: the cache was re-populated while
376 * we waited on the semaphore. Need to revalidate.
378 up(&dir->i_sem);
379 if (result->d_op && result->d_op->d_revalidate) {
380 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
381 dput(result);
382 result = ERR_PTR(-ENOENT);
385 return result;
389 * This limits recursive symlink follows to 8, while
390 * limiting consecutive symlinks to 40.
392 * Without that kind of total limit, nasty chains of consecutive
393 * symlinks can cause almost arbitrarily long lookups.
395 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
397 int err = -ELOOP;
398 if (current->link_count >= 5)
399 goto loop;
400 if (current->total_link_count >= 40)
401 goto loop;
402 cond_resched();
403 err = security_inode_follow_link(dentry, nd);
404 if (err)
405 goto loop;
406 current->link_count++;
407 current->total_link_count++;
408 update_atime(dentry->d_inode);
409 err = dentry->d_inode->i_op->follow_link(dentry, nd);
410 current->link_count--;
411 return err;
412 loop:
413 path_release(nd);
414 return err;
417 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
419 struct vfsmount *parent;
420 struct dentry *mountpoint;
421 spin_lock(&dcache_lock);
422 parent=(*mnt)->mnt_parent;
423 if (parent == *mnt) {
424 spin_unlock(&dcache_lock);
425 return 0;
427 mntget(parent);
428 mountpoint=dget((*mnt)->mnt_mountpoint);
429 spin_unlock(&dcache_lock);
430 dput(*dentry);
431 *dentry = mountpoint;
432 mntput(*mnt);
433 *mnt = parent;
434 return 1;
437 /* no need for dcache_lock, as serialization is taken care in
438 * namespace.c
440 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
442 int res = 0;
443 while (d_mountpoint(*dentry)) {
444 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
445 if (!mounted)
446 break;
447 *mnt = mounted;
448 dput(*dentry);
449 mntput(mounted->mnt_parent);
450 *dentry = dget(mounted->mnt_root);
451 res = 1;
453 return res;
456 /* no need for dcache_lock, as serialization is taken care in
457 * namespace.c
459 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
461 struct vfsmount *mounted;
463 mounted = lookup_mnt(*mnt, *dentry);
464 if (mounted) {
465 *mnt = mounted;
466 dput(*dentry);
467 mntput(mounted->mnt_parent);
468 *dentry = dget(mounted->mnt_root);
469 return 1;
471 return 0;
474 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
476 return __follow_down(mnt,dentry);
479 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
481 while(1) {
482 struct vfsmount *parent;
483 struct dentry *old = *dentry;
485 read_lock(&current->fs->lock);
486 if (*dentry == current->fs->root &&
487 *mnt == current->fs->rootmnt) {
488 read_unlock(&current->fs->lock);
489 break;
491 read_unlock(&current->fs->lock);
492 spin_lock(&dcache_lock);
493 if (*dentry != (*mnt)->mnt_root) {
494 *dentry = dget((*dentry)->d_parent);
495 spin_unlock(&dcache_lock);
496 dput(old);
497 break;
499 parent = (*mnt)->mnt_parent;
500 if (parent == *mnt) {
501 spin_unlock(&dcache_lock);
502 break;
504 mntget(parent);
505 *dentry = dget((*mnt)->mnt_mountpoint);
506 spin_unlock(&dcache_lock);
507 dput(old);
508 mntput(*mnt);
509 *mnt = parent;
511 follow_mount(mnt, dentry);
514 struct path {
515 struct vfsmount *mnt;
516 struct dentry *dentry;
520 * It's more convoluted than I'd like it to be, but... it's still fairly
521 * small and for now I'd prefer to have fast path as straight as possible.
522 * It _is_ time-critical.
524 static int do_lookup(struct nameidata *nd, struct qstr *name,
525 struct path *path)
527 struct vfsmount *mnt = nd->mnt;
528 struct dentry *dentry = __d_lookup(nd->dentry, name);
530 if (!dentry)
531 goto need_lookup;
532 if (dentry->d_op && dentry->d_op->d_revalidate)
533 goto need_revalidate;
534 done:
535 path->mnt = mnt;
536 path->dentry = dentry;
537 return 0;
539 need_lookup:
540 dentry = real_lookup(nd->dentry, name, nd);
541 if (IS_ERR(dentry))
542 goto fail;
543 goto done;
545 need_revalidate:
546 if (dentry->d_op->d_revalidate(dentry, nd))
547 goto done;
548 if (d_invalidate(dentry))
549 goto done;
550 dput(dentry);
551 goto need_lookup;
553 fail:
554 return PTR_ERR(dentry);
558 * Name resolution.
560 * This is the basic name resolution function, turning a pathname
561 * into the final dentry.
563 * We expect 'base' to be positive and a directory.
565 int link_path_walk(const char * name, struct nameidata *nd)
567 struct path next;
568 struct inode *inode;
569 int err;
570 unsigned int lookup_flags = nd->flags;
572 while (*name=='/')
573 name++;
574 if (!*name)
575 goto return_base;
577 inode = nd->dentry->d_inode;
578 if (current->link_count)
579 lookup_flags = LOOKUP_FOLLOW;
581 /* At this point we know we have a real path component. */
582 for(;;) {
583 unsigned long hash;
584 struct qstr this;
585 unsigned int c;
587 err = exec_permission_lite(inode);
588 if (err == -EAGAIN) {
589 err = permission(inode, MAY_EXEC, nd);
591 if (err)
592 break;
594 this.name = name;
595 c = *(const unsigned char *)name;
597 hash = init_name_hash();
598 do {
599 name++;
600 hash = partial_name_hash(c, hash);
601 c = *(const unsigned char *)name;
602 } while (c && (c != '/'));
603 this.len = name - (const char *) this.name;
604 this.hash = end_name_hash(hash);
606 /* remove trailing slashes? */
607 if (!c)
608 goto last_component;
609 while (*++name == '/');
610 if (!*name)
611 goto last_with_slashes;
614 * "." and ".." are special - ".." especially so because it has
615 * to be able to know about the current root directory and
616 * parent relationships.
618 if (this.name[0] == '.') switch (this.len) {
619 default:
620 break;
621 case 2:
622 if (this.name[1] != '.')
623 break;
624 follow_dotdot(&nd->mnt, &nd->dentry);
625 inode = nd->dentry->d_inode;
626 /* fallthrough */
627 case 1:
628 continue;
631 * See if the low-level filesystem might want
632 * to use its own hash..
634 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
635 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
636 if (err < 0)
637 break;
639 nd->flags |= LOOKUP_CONTINUE;
640 /* This does the actual lookups.. */
641 err = do_lookup(nd, &this, &next);
642 if (err)
643 break;
644 /* Check mountpoints.. */
645 follow_mount(&next.mnt, &next.dentry);
647 err = -ENOENT;
648 inode = next.dentry->d_inode;
649 if (!inode)
650 goto out_dput;
651 err = -ENOTDIR;
652 if (!inode->i_op)
653 goto out_dput;
655 if (inode->i_op->follow_link) {
656 mntget(next.mnt);
657 err = do_follow_link(next.dentry, nd);
658 dput(next.dentry);
659 mntput(next.mnt);
660 if (err)
661 goto return_err;
662 err = -ENOENT;
663 inode = nd->dentry->d_inode;
664 if (!inode)
665 break;
666 err = -ENOTDIR;
667 if (!inode->i_op)
668 break;
669 } else {
670 dput(nd->dentry);
671 nd->mnt = next.mnt;
672 nd->dentry = next.dentry;
674 err = -ENOTDIR;
675 if (!inode->i_op->lookup)
676 break;
677 continue;
678 /* here ends the main loop */
680 last_with_slashes:
681 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
682 last_component:
683 nd->flags &= ~LOOKUP_CONTINUE;
684 if (lookup_flags & LOOKUP_PARENT)
685 goto lookup_parent;
686 if (this.name[0] == '.') switch (this.len) {
687 default:
688 break;
689 case 2:
690 if (this.name[1] != '.')
691 break;
692 follow_dotdot(&nd->mnt, &nd->dentry);
693 inode = nd->dentry->d_inode;
694 /* fallthrough */
695 case 1:
696 goto return_base;
698 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
699 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
700 if (err < 0)
701 break;
703 err = do_lookup(nd, &this, &next);
704 if (err)
705 break;
706 follow_mount(&next.mnt, &next.dentry);
707 inode = next.dentry->d_inode;
708 if ((lookup_flags & LOOKUP_FOLLOW)
709 && inode && inode->i_op && inode->i_op->follow_link) {
710 mntget(next.mnt);
711 err = do_follow_link(next.dentry, nd);
712 dput(next.dentry);
713 mntput(next.mnt);
714 if (err)
715 goto return_err;
716 inode = nd->dentry->d_inode;
717 } else {
718 dput(nd->dentry);
719 nd->mnt = next.mnt;
720 nd->dentry = next.dentry;
722 err = -ENOENT;
723 if (!inode)
724 break;
725 if (lookup_flags & LOOKUP_DIRECTORY) {
726 err = -ENOTDIR;
727 if (!inode->i_op || !inode->i_op->lookup)
728 break;
730 goto return_base;
731 lookup_parent:
732 nd->last = this;
733 nd->last_type = LAST_NORM;
734 if (this.name[0] != '.')
735 goto return_base;
736 if (this.len == 1)
737 nd->last_type = LAST_DOT;
738 else if (this.len == 2 && this.name[1] == '.')
739 nd->last_type = LAST_DOTDOT;
740 return_base:
741 return 0;
742 out_dput:
743 dput(next.dentry);
744 break;
746 path_release(nd);
747 return_err:
748 return err;
751 int path_walk(const char * name, struct nameidata *nd)
753 current->total_link_count = 0;
754 return link_path_walk(name, nd);
757 /* SMP-safe */
758 /* returns 1 if everything is done */
759 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
761 if (path_walk(name, nd))
762 return 0; /* something went wrong... */
764 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
765 struct nameidata nd_root;
767 * NAME was not found in alternate root or it's a directory. Try to find
768 * it in the normal root:
770 nd_root.last_type = LAST_ROOT;
771 nd_root.flags = nd->flags;
772 memcpy(&nd_root.intent, &nd->intent, sizeof(nd_root.intent));
773 read_lock(&current->fs->lock);
774 nd_root.mnt = mntget(current->fs->rootmnt);
775 nd_root.dentry = dget(current->fs->root);
776 read_unlock(&current->fs->lock);
777 if (path_walk(name, &nd_root))
778 return 1;
779 if (nd_root.dentry->d_inode) {
780 path_release(nd);
781 nd->dentry = nd_root.dentry;
782 nd->mnt = nd_root.mnt;
783 nd->last = nd_root.last;
784 return 1;
786 path_release(&nd_root);
788 return 1;
791 void set_fs_altroot(void)
793 char *emul = __emul_prefix();
794 struct nameidata nd;
795 struct vfsmount *mnt = NULL, *oldmnt;
796 struct dentry *dentry = NULL, *olddentry;
797 int err;
799 if (!emul)
800 goto set_it;
801 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
802 if (!err) {
803 mnt = nd.mnt;
804 dentry = nd.dentry;
806 set_it:
807 write_lock(&current->fs->lock);
808 oldmnt = current->fs->altrootmnt;
809 olddentry = current->fs->altroot;
810 current->fs->altrootmnt = mnt;
811 current->fs->altroot = dentry;
812 write_unlock(&current->fs->lock);
813 if (olddentry) {
814 dput(olddentry);
815 mntput(oldmnt);
819 /* SMP-safe */
820 static inline int
821 walk_init_root(const char *name, struct nameidata *nd)
823 read_lock(&current->fs->lock);
824 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
825 nd->mnt = mntget(current->fs->altrootmnt);
826 nd->dentry = dget(current->fs->altroot);
827 read_unlock(&current->fs->lock);
828 if (__emul_lookup_dentry(name,nd))
829 return 0;
830 read_lock(&current->fs->lock);
832 nd->mnt = mntget(current->fs->rootmnt);
833 nd->dentry = dget(current->fs->root);
834 read_unlock(&current->fs->lock);
835 return 1;
838 int path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
840 nd->last_type = LAST_ROOT; /* if there are only slashes... */
841 nd->flags = flags;
843 read_lock(&current->fs->lock);
844 if (*name=='/') {
845 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
846 nd->mnt = mntget(current->fs->altrootmnt);
847 nd->dentry = dget(current->fs->altroot);
848 read_unlock(&current->fs->lock);
849 if (__emul_lookup_dentry(name,nd))
850 return 0;
851 read_lock(&current->fs->lock);
853 nd->mnt = mntget(current->fs->rootmnt);
854 nd->dentry = dget(current->fs->root);
856 else{
857 nd->mnt = mntget(current->fs->pwdmnt);
858 nd->dentry = dget(current->fs->pwd);
860 read_unlock(&current->fs->lock);
861 current->total_link_count = 0;
862 return link_path_walk(name, nd);
866 * Restricted form of lookup. Doesn't follow links, single-component only,
867 * needs parent already locked. Doesn't follow mounts.
868 * SMP-safe.
870 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
872 struct dentry * dentry;
873 struct inode *inode;
874 int err;
876 inode = base->d_inode;
877 err = permission(inode, MAY_EXEC, nd);
878 dentry = ERR_PTR(err);
879 if (err)
880 goto out;
883 * See if the low-level filesystem might want
884 * to use its own hash..
886 if (base->d_op && base->d_op->d_hash) {
887 err = base->d_op->d_hash(base, name);
888 dentry = ERR_PTR(err);
889 if (err < 0)
890 goto out;
893 dentry = cached_lookup(base, name, nd);
894 if (!dentry) {
895 struct dentry *new = d_alloc(base, name);
896 dentry = ERR_PTR(-ENOMEM);
897 if (!new)
898 goto out;
899 dentry = inode->i_op->lookup(inode, new, nd);
900 if (!dentry)
901 dentry = new;
902 else
903 dput(new);
905 out:
906 return dentry;
909 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
911 return __lookup_hash(name, base, NULL);
914 /* SMP-safe */
915 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
917 unsigned long hash;
918 struct qstr this;
919 unsigned int c;
921 this.name = name;
922 this.len = len;
923 if (!len)
924 goto access;
926 hash = init_name_hash();
927 while (len--) {
928 c = *(const unsigned char *)name++;
929 if (c == '/' || c == '\0')
930 goto access;
931 hash = partial_name_hash(c, hash);
933 this.hash = end_name_hash(hash);
935 return lookup_hash(&this, base);
936 access:
937 return ERR_PTR(-EACCES);
941 * namei()
943 * is used by most simple commands to get the inode of a specified name.
944 * Open, link etc use their own routines, but this is enough for things
945 * like 'chmod' etc.
947 * namei exists in two versions: namei/lnamei. The only difference is
948 * that namei follows links, while lnamei does not.
949 * SMP-safe
951 int __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
953 char *tmp = getname(name);
954 int err = PTR_ERR(tmp);
956 if (!IS_ERR(tmp)) {
957 err = path_lookup(tmp, flags, nd);
958 putname(tmp);
960 return err;
964 * It's inline, so penalty for filesystems that don't use sticky bit is
965 * minimal.
967 static inline int check_sticky(struct inode *dir, struct inode *inode)
969 if (!(dir->i_mode & S_ISVTX))
970 return 0;
971 if (inode->i_uid == current->fsuid)
972 return 0;
973 if (dir->i_uid == current->fsuid)
974 return 0;
975 return !capable(CAP_FOWNER);
979 * Check whether we can remove a link victim from directory dir, check
980 * whether the type of victim is right.
981 * 1. We can't do it if dir is read-only (done in permission())
982 * 2. We should have write and exec permissions on dir
983 * 3. We can't remove anything from append-only dir
984 * 4. We can't do anything with immutable dir (done in permission())
985 * 5. If the sticky bit on dir is set we should either
986 * a. be owner of dir, or
987 * b. be owner of victim, or
988 * c. have CAP_FOWNER capability
989 * 6. If the victim is append-only or immutable we can't do antyhing with
990 * links pointing to it.
991 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
992 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
993 * 9. We can't remove a root or mountpoint.
994 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
995 * nfs_async_unlink().
997 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
999 int error;
1000 if (!victim->d_inode || victim->d_parent->d_inode != dir)
1001 return -ENOENT;
1002 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1003 if (error)
1004 return error;
1005 if (IS_APPEND(dir))
1006 return -EPERM;
1007 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1008 IS_IMMUTABLE(victim->d_inode))
1009 return -EPERM;
1010 if (isdir) {
1011 if (!S_ISDIR(victim->d_inode->i_mode))
1012 return -ENOTDIR;
1013 if (IS_ROOT(victim))
1014 return -EBUSY;
1015 } else if (S_ISDIR(victim->d_inode->i_mode))
1016 return -EISDIR;
1017 if (IS_DEADDIR(dir))
1018 return -ENOENT;
1019 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1020 return -EBUSY;
1021 return 0;
1024 /* Check whether we can create an object with dentry child in directory
1025 * dir.
1026 * 1. We can't do it if child already exists (open has special treatment for
1027 * this case, but since we are inlined it's OK)
1028 * 2. We can't do it if dir is read-only (done in permission())
1029 * 3. We should have write and exec permissions on dir
1030 * 4. We can't do it if dir is immutable (done in permission())
1032 static inline int may_create(struct inode *dir, struct dentry *child,
1033 struct nameidata *nd)
1035 if (child->d_inode)
1036 return -EEXIST;
1037 if (IS_DEADDIR(dir))
1038 return -ENOENT;
1039 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1043 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1044 * reasons.
1046 * O_DIRECTORY translates into forcing a directory lookup.
1048 static inline int lookup_flags(unsigned int f)
1050 unsigned long retval = LOOKUP_FOLLOW;
1052 if (f & O_NOFOLLOW)
1053 retval &= ~LOOKUP_FOLLOW;
1055 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1056 retval &= ~LOOKUP_FOLLOW;
1058 if (f & O_DIRECTORY)
1059 retval |= LOOKUP_DIRECTORY;
1061 return retval;
1065 * p1 and p2 should be directories on the same fs.
1067 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1069 struct dentry *p;
1071 if (p1 == p2) {
1072 down(&p1->d_inode->i_sem);
1073 return NULL;
1076 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1078 for (p = p1; p->d_parent != p; p = p->d_parent) {
1079 if (p->d_parent == p2) {
1080 down(&p2->d_inode->i_sem);
1081 down(&p1->d_inode->i_sem);
1082 return p;
1086 for (p = p2; p->d_parent != p; p = p->d_parent) {
1087 if (p->d_parent == p1) {
1088 down(&p1->d_inode->i_sem);
1089 down(&p2->d_inode->i_sem);
1090 return p;
1094 down(&p1->d_inode->i_sem);
1095 down(&p2->d_inode->i_sem);
1096 return NULL;
1099 void unlock_rename(struct dentry *p1, struct dentry *p2)
1101 up(&p1->d_inode->i_sem);
1102 if (p1 != p2) {
1103 up(&p2->d_inode->i_sem);
1104 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1108 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1109 struct nameidata *nd)
1111 int error = may_create(dir, dentry, nd);
1113 if (error)
1114 return error;
1116 if (!dir->i_op || !dir->i_op->create)
1117 return -EACCES; /* shouldn't it be ENOSYS? */
1118 mode &= S_IALLUGO;
1119 mode |= S_IFREG;
1120 error = security_inode_create(dir, dentry, mode);
1121 if (error)
1122 return error;
1123 DQUOT_INIT(dir);
1124 error = dir->i_op->create(dir, dentry, mode, nd);
1125 if (!error) {
1126 inode_dir_notify(dir, DN_CREATE);
1127 security_inode_post_create(dir, dentry, mode);
1129 return error;
1132 int may_open(struct nameidata *nd, int acc_mode, int flag)
1134 struct dentry *dentry = nd->dentry;
1135 struct inode *inode = dentry->d_inode;
1136 int error;
1138 if (!inode)
1139 return -ENOENT;
1141 if (S_ISLNK(inode->i_mode))
1142 return -ELOOP;
1144 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1145 return -EISDIR;
1147 error = permission(inode, acc_mode, nd);
1148 if (error)
1149 return error;
1152 * FIFO's, sockets and device files are special: they don't
1153 * actually live on the filesystem itself, and as such you
1154 * can write to them even if the filesystem is read-only.
1156 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1157 flag &= ~O_TRUNC;
1158 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1159 if (nd->mnt->mnt_flags & MNT_NODEV)
1160 return -EACCES;
1162 flag &= ~O_TRUNC;
1163 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1164 return -EROFS;
1166 * An append-only file must be opened in append mode for writing.
1168 if (IS_APPEND(inode)) {
1169 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1170 return -EPERM;
1171 if (flag & O_TRUNC)
1172 return -EPERM;
1176 * Ensure there are no outstanding leases on the file.
1178 error = break_lease(inode, flag);
1179 if (error)
1180 return error;
1182 if (flag & O_TRUNC) {
1183 error = get_write_access(inode);
1184 if (error)
1185 return error;
1188 * Refuse to truncate files with mandatory locks held on them.
1190 error = locks_verify_locked(inode);
1191 if (!error) {
1192 DQUOT_INIT(inode);
1194 error = do_truncate(dentry, 0);
1196 put_write_access(inode);
1197 if (error)
1198 return error;
1199 } else
1200 if (flag & FMODE_WRITE)
1201 DQUOT_INIT(inode);
1203 return 0;
1207 * open_namei()
1209 * namei for open - this is in fact almost the whole open-routine.
1211 * Note that the low bits of "flag" aren't the same as in the open
1212 * system call - they are 00 - no permissions needed
1213 * 01 - read permission needed
1214 * 10 - write permission needed
1215 * 11 - read/write permissions needed
1216 * which is a lot more logical, and also allows the "no perm" needed
1217 * for symlinks (where the permissions are checked later).
1218 * SMP-safe
1220 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1222 int acc_mode, error = 0;
1223 struct dentry *dentry;
1224 struct dentry *dir;
1225 int count = 0;
1227 acc_mode = ACC_MODE(flag);
1229 /* Allow the LSM permission hook to distinguish append
1230 access from general write access. */
1231 if (flag & O_APPEND)
1232 acc_mode |= MAY_APPEND;
1234 /* Fill in the open() intent data */
1235 nd->intent.open.flags = flag;
1236 nd->intent.open.create_mode = mode;
1239 * The simplest case - just a plain lookup.
1241 if (!(flag & O_CREAT)) {
1242 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1243 if (error)
1244 return error;
1245 dentry = nd->dentry;
1246 goto ok;
1250 * Create - we need to know the parent.
1252 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1253 if (error)
1254 return error;
1257 * We have the parent and last component. First of all, check
1258 * that we are not asked to creat(2) an obvious directory - that
1259 * will not do.
1261 error = -EISDIR;
1262 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1263 goto exit;
1265 dir = nd->dentry;
1266 nd->flags &= ~LOOKUP_PARENT;
1267 down(&dir->d_inode->i_sem);
1268 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1270 do_last:
1271 error = PTR_ERR(dentry);
1272 if (IS_ERR(dentry)) {
1273 up(&dir->d_inode->i_sem);
1274 goto exit;
1277 /* Negative dentry, just create the file */
1278 if (!dentry->d_inode) {
1279 if (!IS_POSIXACL(dir->d_inode))
1280 mode &= ~current->fs->umask;
1281 error = vfs_create(dir->d_inode, dentry, mode, nd);
1282 up(&dir->d_inode->i_sem);
1283 dput(nd->dentry);
1284 nd->dentry = dentry;
1285 if (error)
1286 goto exit;
1287 /* Don't check for write permission, don't truncate */
1288 acc_mode = 0;
1289 flag &= ~O_TRUNC;
1290 goto ok;
1294 * It already exists.
1296 up(&dir->d_inode->i_sem);
1298 error = -EEXIST;
1299 if (flag & O_EXCL)
1300 goto exit_dput;
1302 if (d_mountpoint(dentry)) {
1303 error = -ELOOP;
1304 if (flag & O_NOFOLLOW)
1305 goto exit_dput;
1306 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1308 error = -ENOENT;
1309 if (!dentry->d_inode)
1310 goto exit_dput;
1311 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1312 goto do_link;
1314 dput(nd->dentry);
1315 nd->dentry = dentry;
1316 error = -EISDIR;
1317 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1318 goto exit;
1320 error = may_open(nd, acc_mode, flag);
1321 if (error)
1322 goto exit;
1323 return 0;
1325 exit_dput:
1326 dput(dentry);
1327 exit:
1328 path_release(nd);
1329 return error;
1331 do_link:
1332 error = -ELOOP;
1333 if (flag & O_NOFOLLOW)
1334 goto exit_dput;
1336 * This is subtle. Instead of calling do_follow_link() we do the
1337 * thing by hands. The reason is that this way we have zero link_count
1338 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1339 * After that we have the parent and last component, i.e.
1340 * we are in the same situation as after the first path_walk().
1341 * Well, almost - if the last component is normal we get its copy
1342 * stored in nd->last.name and we will have to putname() it when we
1343 * are done. Procfs-like symlinks just set LAST_BIND.
1345 nd->flags |= LOOKUP_PARENT;
1346 error = security_inode_follow_link(dentry, nd);
1347 if (error)
1348 goto exit_dput;
1349 update_atime(dentry->d_inode);
1350 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1351 dput(dentry);
1352 if (error)
1353 return error;
1354 nd->flags &= ~LOOKUP_PARENT;
1355 if (nd->last_type == LAST_BIND) {
1356 dentry = nd->dentry;
1357 goto ok;
1359 error = -EISDIR;
1360 if (nd->last_type != LAST_NORM)
1361 goto exit;
1362 if (nd->last.name[nd->last.len]) {
1363 putname(nd->last.name);
1364 goto exit;
1366 error = -ELOOP;
1367 if (count++==32) {
1368 putname(nd->last.name);
1369 goto exit;
1371 dir = nd->dentry;
1372 down(&dir->d_inode->i_sem);
1373 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1374 putname(nd->last.name);
1375 goto do_last;
1378 /* SMP-safe */
1379 static struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1381 struct dentry *dentry;
1383 down(&nd->dentry->d_inode->i_sem);
1384 dentry = ERR_PTR(-EEXIST);
1385 if (nd->last_type != LAST_NORM)
1386 goto fail;
1387 nd->flags &= ~LOOKUP_PARENT;
1388 dentry = lookup_hash(&nd->last, nd->dentry);
1389 if (IS_ERR(dentry))
1390 goto fail;
1391 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1392 goto enoent;
1393 return dentry;
1394 enoent:
1395 dput(dentry);
1396 dentry = ERR_PTR(-ENOENT);
1397 fail:
1398 return dentry;
1401 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1403 int error = may_create(dir, dentry, NULL);
1405 if (error)
1406 return error;
1408 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1409 return -EPERM;
1411 if (!dir->i_op || !dir->i_op->mknod)
1412 return -EPERM;
1414 error = security_inode_mknod(dir, dentry, mode, dev);
1415 if (error)
1416 return error;
1418 DQUOT_INIT(dir);
1419 error = dir->i_op->mknod(dir, dentry, mode, dev);
1420 if (!error) {
1421 inode_dir_notify(dir, DN_CREATE);
1422 security_inode_post_mknod(dir, dentry, mode, dev);
1424 return error;
1427 asmlinkage long sys_mknod(const char __user * filename, int mode, dev_t dev)
1429 int error = 0;
1430 char * tmp;
1431 struct dentry * dentry;
1432 struct nameidata nd;
1434 if (S_ISDIR(mode))
1435 return -EPERM;
1436 tmp = getname(filename);
1437 if (IS_ERR(tmp))
1438 return PTR_ERR(tmp);
1440 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1441 if (error)
1442 goto out;
1443 dentry = lookup_create(&nd, 0);
1444 error = PTR_ERR(dentry);
1446 if (!IS_POSIXACL(nd.dentry->d_inode))
1447 mode &= ~current->fs->umask;
1448 if (!IS_ERR(dentry)) {
1449 switch (mode & S_IFMT) {
1450 case 0: case S_IFREG:
1451 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1452 break;
1453 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
1454 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,dev);
1455 break;
1456 case S_IFDIR:
1457 error = -EPERM;
1458 break;
1459 default:
1460 error = -EINVAL;
1462 dput(dentry);
1464 up(&nd.dentry->d_inode->i_sem);
1465 path_release(&nd);
1466 out:
1467 putname(tmp);
1469 return error;
1472 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1474 int error = may_create(dir, dentry, NULL);
1476 if (error)
1477 return error;
1479 if (!dir->i_op || !dir->i_op->mkdir)
1480 return -EPERM;
1482 mode &= (S_IRWXUGO|S_ISVTX);
1483 error = security_inode_mkdir(dir, dentry, mode);
1484 if (error)
1485 return error;
1487 DQUOT_INIT(dir);
1488 error = dir->i_op->mkdir(dir, dentry, mode);
1489 if (!error) {
1490 inode_dir_notify(dir, DN_CREATE);
1491 security_inode_post_mkdir(dir,dentry, mode);
1493 return error;
1496 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1498 int error = 0;
1499 char * tmp;
1501 tmp = getname(pathname);
1502 error = PTR_ERR(tmp);
1503 if (!IS_ERR(tmp)) {
1504 struct dentry *dentry;
1505 struct nameidata nd;
1507 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1508 if (error)
1509 goto out;
1510 dentry = lookup_create(&nd, 1);
1511 error = PTR_ERR(dentry);
1512 if (!IS_ERR(dentry)) {
1513 if (!IS_POSIXACL(nd.dentry->d_inode))
1514 mode &= ~current->fs->umask;
1515 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1516 dput(dentry);
1518 up(&nd.dentry->d_inode->i_sem);
1519 path_release(&nd);
1520 out:
1521 putname(tmp);
1524 return error;
1528 * We try to drop the dentry early: we should have
1529 * a usage count of 2 if we're the only user of this
1530 * dentry, and if that is true (possibly after pruning
1531 * the dcache), then we drop the dentry now.
1533 * A low-level filesystem can, if it choses, legally
1534 * do a
1536 * if (!d_unhashed(dentry))
1537 * return -EBUSY;
1539 * if it cannot handle the case of removing a directory
1540 * that is still in use by something else..
1542 static void d_unhash(struct dentry *dentry)
1544 dget(dentry);
1545 spin_lock(&dcache_lock);
1546 switch (atomic_read(&dentry->d_count)) {
1547 default:
1548 spin_unlock(&dcache_lock);
1549 shrink_dcache_parent(dentry);
1550 spin_lock(&dcache_lock);
1551 if (atomic_read(&dentry->d_count) != 2)
1552 break;
1553 case 2:
1554 __d_drop(dentry);
1556 spin_unlock(&dcache_lock);
1559 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1561 int error = may_delete(dir, dentry, 1);
1563 if (error)
1564 return error;
1566 if (!dir->i_op || !dir->i_op->rmdir)
1567 return -EPERM;
1569 DQUOT_INIT(dir);
1571 down(&dentry->d_inode->i_sem);
1572 d_unhash(dentry);
1573 if (d_mountpoint(dentry))
1574 error = -EBUSY;
1575 else {
1576 error = security_inode_rmdir(dir, dentry);
1577 if (!error) {
1578 error = dir->i_op->rmdir(dir, dentry);
1579 if (!error)
1580 dentry->d_inode->i_flags |= S_DEAD;
1583 up(&dentry->d_inode->i_sem);
1584 if (!error) {
1585 inode_dir_notify(dir, DN_DELETE);
1586 d_delete(dentry);
1588 dput(dentry);
1590 return error;
1593 asmlinkage long sys_rmdir(const char __user * pathname)
1595 int error = 0;
1596 char * name;
1597 struct dentry *dentry;
1598 struct nameidata nd;
1600 name = getname(pathname);
1601 if(IS_ERR(name))
1602 return PTR_ERR(name);
1604 error = path_lookup(name, LOOKUP_PARENT, &nd);
1605 if (error)
1606 goto exit;
1608 switch(nd.last_type) {
1609 case LAST_DOTDOT:
1610 error = -ENOTEMPTY;
1611 goto exit1;
1612 case LAST_DOT:
1613 error = -EINVAL;
1614 goto exit1;
1615 case LAST_ROOT:
1616 error = -EBUSY;
1617 goto exit1;
1619 down(&nd.dentry->d_inode->i_sem);
1620 dentry = lookup_hash(&nd.last, nd.dentry);
1621 error = PTR_ERR(dentry);
1622 if (!IS_ERR(dentry)) {
1623 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1624 dput(dentry);
1626 up(&nd.dentry->d_inode->i_sem);
1627 exit1:
1628 path_release(&nd);
1629 exit:
1630 putname(name);
1631 return error;
1634 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1636 int error = may_delete(dir, dentry, 0);
1638 if (error)
1639 return error;
1641 if (!dir->i_op || !dir->i_op->unlink)
1642 return -EPERM;
1644 DQUOT_INIT(dir);
1646 down(&dentry->d_inode->i_sem);
1647 if (d_mountpoint(dentry))
1648 error = -EBUSY;
1649 else {
1650 error = security_inode_unlink(dir, dentry);
1651 if (!error)
1652 error = dir->i_op->unlink(dir, dentry);
1654 up(&dentry->d_inode->i_sem);
1656 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1657 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1658 d_delete(dentry);
1659 inode_dir_notify(dir, DN_DELETE);
1661 return error;
1665 * Make sure that the actual truncation of the file will occur outside its
1666 * directory's i_sem. Truncate can take a long time if there is a lot of
1667 * writeout happening, and we don't want to prevent access to the directory
1668 * while waiting on the I/O.
1670 asmlinkage long sys_unlink(const char __user * pathname)
1672 int error = 0;
1673 char * name;
1674 struct dentry *dentry;
1675 struct nameidata nd;
1676 struct inode *inode = NULL;
1678 name = getname(pathname);
1679 if(IS_ERR(name))
1680 return PTR_ERR(name);
1682 error = path_lookup(name, LOOKUP_PARENT, &nd);
1683 if (error)
1684 goto exit;
1685 error = -EISDIR;
1686 if (nd.last_type != LAST_NORM)
1687 goto exit1;
1688 down(&nd.dentry->d_inode->i_sem);
1689 dentry = lookup_hash(&nd.last, nd.dentry);
1690 error = PTR_ERR(dentry);
1691 if (!IS_ERR(dentry)) {
1692 /* Why not before? Because we want correct error value */
1693 if (nd.last.name[nd.last.len])
1694 goto slashes;
1695 inode = dentry->d_inode;
1696 if (inode)
1697 atomic_inc(&inode->i_count);
1698 error = vfs_unlink(nd.dentry->d_inode, dentry);
1699 exit2:
1700 dput(dentry);
1702 up(&nd.dentry->d_inode->i_sem);
1703 exit1:
1704 path_release(&nd);
1705 exit:
1706 putname(name);
1708 if (inode)
1709 iput(inode); /* truncate the inode here */
1710 return error;
1712 slashes:
1713 error = !dentry->d_inode ? -ENOENT :
1714 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1715 goto exit2;
1718 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1720 int error = may_create(dir, dentry, NULL);
1722 if (error)
1723 return error;
1725 if (!dir->i_op || !dir->i_op->symlink)
1726 return -EPERM;
1728 error = security_inode_symlink(dir, dentry, oldname);
1729 if (error)
1730 return error;
1732 DQUOT_INIT(dir);
1733 error = dir->i_op->symlink(dir, dentry, oldname);
1734 if (!error) {
1735 inode_dir_notify(dir, DN_CREATE);
1736 security_inode_post_symlink(dir, dentry, oldname);
1738 return error;
1741 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1743 int error = 0;
1744 char * from;
1745 char * to;
1747 from = getname(oldname);
1748 if(IS_ERR(from))
1749 return PTR_ERR(from);
1750 to = getname(newname);
1751 error = PTR_ERR(to);
1752 if (!IS_ERR(to)) {
1753 struct dentry *dentry;
1754 struct nameidata nd;
1756 error = path_lookup(to, LOOKUP_PARENT, &nd);
1757 if (error)
1758 goto out;
1759 dentry = lookup_create(&nd, 0);
1760 error = PTR_ERR(dentry);
1761 if (!IS_ERR(dentry)) {
1762 error = vfs_symlink(nd.dentry->d_inode, dentry, from);
1763 dput(dentry);
1765 up(&nd.dentry->d_inode->i_sem);
1766 path_release(&nd);
1767 out:
1768 putname(to);
1770 putname(from);
1771 return error;
1774 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1776 struct inode *inode = old_dentry->d_inode;
1777 int error;
1779 if (!inode)
1780 return -ENOENT;
1782 error = may_create(dir, new_dentry, NULL);
1783 if (error)
1784 return error;
1786 if (dir->i_sb != inode->i_sb)
1787 return -EXDEV;
1790 * A link to an append-only or immutable file cannot be created.
1792 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1793 return -EPERM;
1794 if (!dir->i_op || !dir->i_op->link)
1795 return -EPERM;
1796 if (S_ISDIR(old_dentry->d_inode->i_mode))
1797 return -EPERM;
1799 error = security_inode_link(old_dentry, dir, new_dentry);
1800 if (error)
1801 return error;
1803 down(&old_dentry->d_inode->i_sem);
1804 DQUOT_INIT(dir);
1805 error = dir->i_op->link(old_dentry, dir, new_dentry);
1806 up(&old_dentry->d_inode->i_sem);
1807 if (!error) {
1808 inode_dir_notify(dir, DN_CREATE);
1809 security_inode_post_link(old_dentry, dir, new_dentry);
1811 return error;
1815 * Hardlinks are often used in delicate situations. We avoid
1816 * security-related surprises by not following symlinks on the
1817 * newname. --KAB
1819 * We don't follow them on the oldname either to be compatible
1820 * with linux 2.0, and to avoid hard-linking to directories
1821 * and other special files. --ADM
1823 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
1825 struct dentry *new_dentry;
1826 struct nameidata nd, old_nd;
1827 int error;
1828 char * to;
1830 to = getname(newname);
1831 if (IS_ERR(to))
1832 return PTR_ERR(to);
1834 error = __user_walk(oldname, 0, &old_nd);
1835 if (error)
1836 goto exit;
1837 error = path_lookup(to, LOOKUP_PARENT, &nd);
1838 if (error)
1839 goto out;
1840 error = -EXDEV;
1841 if (old_nd.mnt != nd.mnt)
1842 goto out_release;
1843 new_dentry = lookup_create(&nd, 0);
1844 error = PTR_ERR(new_dentry);
1845 if (!IS_ERR(new_dentry)) {
1846 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
1847 dput(new_dentry);
1849 up(&nd.dentry->d_inode->i_sem);
1850 out_release:
1851 path_release(&nd);
1852 out:
1853 path_release(&old_nd);
1854 exit:
1855 putname(to);
1857 return error;
1861 * The worst of all namespace operations - renaming directory. "Perverted"
1862 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1863 * Problems:
1864 * a) we can get into loop creation. Check is done in is_subdir().
1865 * b) race potential - two innocent renames can create a loop together.
1866 * That's where 4.4 screws up. Current fix: serialization on
1867 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1868 * story.
1869 * c) we have to lock _three_ objects - parents and victim (if it exists).
1870 * And that - after we got ->i_sem on parents (until then we don't know
1871 * whether the target exists). Solution: try to be smart with locking
1872 * order for inodes. We rely on the fact that tree topology may change
1873 * only under ->s_vfs_rename_sem _and_ that parent of the object we
1874 * move will be locked. Thus we can rank directories by the tree
1875 * (ancestors first) and rank all non-directories after them.
1876 * That works since everybody except rename does "lock parent, lookup,
1877 * lock child" and rename is under ->s_vfs_rename_sem.
1878 * HOWEVER, it relies on the assumption that any object with ->lookup()
1879 * has no more than 1 dentry. If "hybrid" objects will ever appear,
1880 * we'd better make sure that there's no link(2) for them.
1881 * d) some filesystems don't support opened-but-unlinked directories,
1882 * either because of layout or because they are not ready to deal with
1883 * all cases correctly. The latter will be fixed (taking this sort of
1884 * stuff into VFS), but the former is not going away. Solution: the same
1885 * trick as in rmdir().
1886 * e) conversion from fhandle to dentry may come in the wrong moment - when
1887 * we are removing the target. Solution: we will have to grab ->i_sem
1888 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1889 * ->i_sem on parents, which works but leads to some truely excessive
1890 * locking].
1892 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
1893 struct inode *new_dir, struct dentry *new_dentry)
1895 int error = 0;
1896 struct inode *target;
1899 * If we are going to change the parent - check write permissions,
1900 * we'll need to flip '..'.
1902 if (new_dir != old_dir) {
1903 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
1904 if (error)
1905 return error;
1908 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
1909 if (error)
1910 return error;
1912 target = new_dentry->d_inode;
1913 if (target) {
1914 down(&target->i_sem);
1915 d_unhash(new_dentry);
1917 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1918 error = -EBUSY;
1919 else
1920 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1921 if (target) {
1922 if (!error)
1923 target->i_flags |= S_DEAD;
1924 up(&target->i_sem);
1925 if (d_unhashed(new_dentry))
1926 d_rehash(new_dentry);
1927 dput(new_dentry);
1929 if (!error) {
1930 d_move(old_dentry,new_dentry);
1931 security_inode_post_rename(old_dir, old_dentry,
1932 new_dir, new_dentry);
1934 return error;
1937 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
1938 struct inode *new_dir, struct dentry *new_dentry)
1940 struct inode *target;
1941 int error;
1943 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
1944 if (error)
1945 return error;
1947 dget(new_dentry);
1948 target = new_dentry->d_inode;
1949 if (target)
1950 down(&target->i_sem);
1951 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1952 error = -EBUSY;
1953 else
1954 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1955 if (!error) {
1956 /* The following d_move() should become unconditional */
1957 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
1958 d_move(old_dentry, new_dentry);
1959 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
1961 if (target)
1962 up(&target->i_sem);
1963 dput(new_dentry);
1964 return error;
1967 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1968 struct inode *new_dir, struct dentry *new_dentry)
1970 int error;
1971 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1973 if (old_dentry->d_inode == new_dentry->d_inode)
1974 return 0;
1976 error = may_delete(old_dir, old_dentry, is_dir);
1977 if (error)
1978 return error;
1980 if (!new_dentry->d_inode)
1981 error = may_create(new_dir, new_dentry, NULL);
1982 else
1983 error = may_delete(new_dir, new_dentry, is_dir);
1984 if (error)
1985 return error;
1987 if (!old_dir->i_op || !old_dir->i_op->rename)
1988 return -EPERM;
1990 DQUOT_INIT(old_dir);
1991 DQUOT_INIT(new_dir);
1993 if (is_dir)
1994 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
1995 else
1996 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
1997 if (!error) {
1998 if (old_dir == new_dir)
1999 inode_dir_notify(old_dir, DN_RENAME);
2000 else {
2001 inode_dir_notify(old_dir, DN_DELETE);
2002 inode_dir_notify(new_dir, DN_CREATE);
2005 return error;
2008 static inline int do_rename(const char * oldname, const char * newname)
2010 int error = 0;
2011 struct dentry * old_dir, * new_dir;
2012 struct dentry * old_dentry, *new_dentry;
2013 struct dentry * trap;
2014 struct nameidata oldnd, newnd;
2016 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2017 if (error)
2018 goto exit;
2020 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2021 if (error)
2022 goto exit1;
2024 error = -EXDEV;
2025 if (oldnd.mnt != newnd.mnt)
2026 goto exit2;
2028 old_dir = oldnd.dentry;
2029 error = -EBUSY;
2030 if (oldnd.last_type != LAST_NORM)
2031 goto exit2;
2033 new_dir = newnd.dentry;
2034 if (newnd.last_type != LAST_NORM)
2035 goto exit2;
2037 trap = lock_rename(new_dir, old_dir);
2039 old_dentry = lookup_hash(&oldnd.last, old_dir);
2040 error = PTR_ERR(old_dentry);
2041 if (IS_ERR(old_dentry))
2042 goto exit3;
2043 /* source must exist */
2044 error = -ENOENT;
2045 if (!old_dentry->d_inode)
2046 goto exit4;
2047 /* unless the source is a directory trailing slashes give -ENOTDIR */
2048 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2049 error = -ENOTDIR;
2050 if (oldnd.last.name[oldnd.last.len])
2051 goto exit4;
2052 if (newnd.last.name[newnd.last.len])
2053 goto exit4;
2055 /* source should not be ancestor of target */
2056 error = -EINVAL;
2057 if (old_dentry == trap)
2058 goto exit4;
2059 new_dentry = lookup_hash(&newnd.last, new_dir);
2060 error = PTR_ERR(new_dentry);
2061 if (IS_ERR(new_dentry))
2062 goto exit4;
2063 /* target should not be an ancestor of source */
2064 error = -ENOTEMPTY;
2065 if (new_dentry == trap)
2066 goto exit5;
2068 error = vfs_rename(old_dir->d_inode, old_dentry,
2069 new_dir->d_inode, new_dentry);
2070 exit5:
2071 dput(new_dentry);
2072 exit4:
2073 dput(old_dentry);
2074 exit3:
2075 unlock_rename(new_dir, old_dir);
2076 exit2:
2077 path_release(&newnd);
2078 exit1:
2079 path_release(&oldnd);
2080 exit:
2081 return error;
2084 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2086 int error;
2087 char * from;
2088 char * to;
2090 from = getname(oldname);
2091 if(IS_ERR(from))
2092 return PTR_ERR(from);
2093 to = getname(newname);
2094 error = PTR_ERR(to);
2095 if (!IS_ERR(to)) {
2096 error = do_rename(from,to);
2097 putname(to);
2099 putname(from);
2100 return error;
2103 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2105 int len;
2107 len = PTR_ERR(link);
2108 if (IS_ERR(link))
2109 goto out;
2111 len = strlen(link);
2112 if (len > (unsigned) buflen)
2113 len = buflen;
2114 if (copy_to_user(buffer, link, len))
2115 len = -EFAULT;
2116 out:
2117 return len;
2120 static inline int
2121 __vfs_follow_link(struct nameidata *nd, const char *link)
2123 int res = 0;
2124 char *name;
2125 if (IS_ERR(link))
2126 goto fail;
2128 if (*link == '/') {
2129 path_release(nd);
2130 if (!walk_init_root(link, nd))
2131 /* weird __emul_prefix() stuff did it */
2132 goto out;
2134 res = link_path_walk(link, nd);
2135 out:
2136 if (current->link_count || res || nd->last_type!=LAST_NORM)
2137 return res;
2139 * If it is an iterative symlinks resolution in open_namei() we
2140 * have to copy the last component. And all that crap because of
2141 * bloody create() on broken symlinks. Furrfu...
2143 name = __getname();
2144 if (unlikely(!name)) {
2145 path_release(nd);
2146 return -ENOMEM;
2148 strcpy(name, nd->last.name);
2149 nd->last.name = name;
2150 return 0;
2151 fail:
2152 path_release(nd);
2153 return PTR_ERR(link);
2156 int vfs_follow_link(struct nameidata *nd, const char *link)
2158 return __vfs_follow_link(nd, link);
2161 /* get the link contents into pagecache */
2162 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2164 struct page * page;
2165 struct address_space *mapping = dentry->d_inode->i_mapping;
2166 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2167 NULL);
2168 if (IS_ERR(page))
2169 goto sync_fail;
2170 wait_on_page_locked(page);
2171 if (!PageUptodate(page))
2172 goto async_fail;
2173 *ppage = page;
2174 return kmap(page);
2176 async_fail:
2177 page_cache_release(page);
2178 return ERR_PTR(-EIO);
2180 sync_fail:
2181 return (char*)page;
2184 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2186 struct page *page = NULL;
2187 char *s = page_getlink(dentry, &page);
2188 int res = vfs_readlink(dentry,buffer,buflen,s);
2189 if (page) {
2190 kunmap(page);
2191 page_cache_release(page);
2193 return res;
2196 int page_follow_link(struct dentry *dentry, struct nameidata *nd)
2198 struct page *page = NULL;
2199 char *s = page_getlink(dentry, &page);
2200 int res = __vfs_follow_link(nd, s);
2201 if (page) {
2202 kunmap(page);
2203 page_cache_release(page);
2205 return res;
2208 int page_symlink(struct inode *inode, const char *symname, int len)
2210 struct address_space *mapping = inode->i_mapping;
2211 struct page *page = grab_cache_page(mapping, 0);
2212 int err = -ENOMEM;
2213 char *kaddr;
2215 if (!page)
2216 goto fail;
2217 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2218 if (err)
2219 goto fail_map;
2220 kaddr = kmap_atomic(page, KM_USER0);
2221 memcpy(kaddr, symname, len-1);
2222 kunmap_atomic(kaddr, KM_USER0);
2223 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2225 * Notice that we are _not_ going to block here - end of page is
2226 * unmapped, so this will only try to map the rest of page, see
2227 * that it is unmapped (typically even will not look into inode -
2228 * ->i_size will be enough for everything) and zero it out.
2229 * OTOH it's obviously correct and should make the page up-to-date.
2231 if (!PageUptodate(page)) {
2232 err = mapping->a_ops->readpage(NULL, page);
2233 wait_on_page_locked(page);
2234 } else {
2235 unlock_page(page);
2237 page_cache_release(page);
2238 if (err < 0)
2239 goto fail;
2240 mark_inode_dirty(inode);
2241 return 0;
2242 fail_map:
2243 unlock_page(page);
2244 page_cache_release(page);
2245 fail:
2246 return err;
2249 struct inode_operations page_symlink_inode_operations = {
2250 .readlink = page_readlink,
2251 .follow_link = page_follow_link,