This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / fs / namei.c
1 /*
2  *  linux/fs/namei.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 /*
8  * Some corrections by tytso.
9  */
10
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12  * lookup logic.
13  */
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15  */
16
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 <linux/audit.h>
30 #include <linux/vs_base.h>
31
32 #include <asm/namei.h>
33 #include <asm/uaccess.h>
34
35 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
36
37 /* [Feb-1997 T. Schoebel-Theuer]
38  * Fundamental changes in the pathname lookup mechanisms (namei)
39  * were necessary because of omirr.  The reason is that omirr needs
40  * to know the _real_ pathname, not the user-supplied one, in case
41  * of symlinks (and also when transname replacements occur).
42  *
43  * The new code replaces the old recursive symlink resolution with
44  * an iterative one (in case of non-nested symlink chains).  It does
45  * this with calls to <fs>_follow_link().
46  * As a side effect, dir_namei(), _namei() and follow_link() are now 
47  * replaced with a single function lookup_dentry() that can handle all 
48  * the special cases of the former code.
49  *
50  * With the new dcache, the pathname is stored at each inode, at least as
51  * long as the refcount of the inode is positive.  As a side effect, the
52  * size of the dcache depends on the inode cache and thus is dynamic.
53  *
54  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
55  * resolution to correspond with current state of the code.
56  *
57  * Note that the symlink resolution is not *completely* iterative.
58  * There is still a significant amount of tail- and mid- recursion in
59  * the algorithm.  Also, note that <fs>_readlink() is not used in
60  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
61  * may return different results than <fs>_follow_link().  Many virtual
62  * filesystems (including /proc) exhibit this behavior.
63  */
64
65 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
66  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
67  * and the name already exists in form of a symlink, try to create the new
68  * name indicated by the symlink. The old code always complained that the
69  * name already exists, due to not following the symlink even if its target
70  * is nonexistent.  The new semantics affects also mknod() and link() when
71  * the name is a symlink pointing to a non-existant name.
72  *
73  * I don't know which semantics is the right one, since I have no access
74  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
75  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
76  * "old" one. Personally, I think the new semantics is much more logical.
77  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
78  * file does succeed in both HP-UX and SunOs, but not in Solaris
79  * and in the old Linux semantics.
80  */
81
82 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
83  * semantics.  See the comments in "open_namei" and "do_link" below.
84  *
85  * [10-Sep-98 Alan Modra] Another symlink change.
86  */
87
88 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
89  *      inside the path - always follow.
90  *      in the last component in creation/removal/renaming - never follow.
91  *      if LOOKUP_FOLLOW passed - follow.
92  *      if the pathname has trailing slashes - follow.
93  *      otherwise - don't follow.
94  * (applied in that order).
95  *
96  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
97  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
98  * During the 2.4 we need to fix the userland stuff depending on it -
99  * hopefully we will be able to get rid of that wart in 2.5. So far only
100  * XEmacs seems to be relying on it...
101  */
102 /*
103  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
104  * implemented.  Let's see if raised priority of ->s_vfs_rename_sem gives
105  * any extra contention...
106  */
107
108 /* In order to reduce some races, while at the same time doing additional
109  * checking and hopefully speeding things up, we copy filenames to the
110  * kernel data space before using them..
111  *
112  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
113  * PATH_MAX includes the nul terminator --RR.
114  */
115 static inline int do_getname(const char __user *filename, char *page)
116 {
117         int retval;
118         unsigned long len = PATH_MAX;
119
120         if ((unsigned long) filename >= TASK_SIZE) {
121                 if (!segment_eq(get_fs(), KERNEL_DS))
122                         return -EFAULT;
123         } else if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
124                 len = TASK_SIZE - (unsigned long) filename;
125
126         retval = strncpy_from_user((char *)page, filename, len);
127         if (retval > 0) {
128                 if (retval < len)
129                         return 0;
130                 return -ENAMETOOLONG;
131         } else if (!retval)
132                 retval = -ENOENT;
133         return retval;
134 }
135
136 char * getname(const char __user * filename)
137 {
138         char *tmp, *result;
139
140         result = ERR_PTR(-ENOMEM);
141         tmp = __getname();
142         if (tmp)  {
143                 int retval = do_getname(filename, tmp);
144
145                 result = tmp;
146                 if (retval < 0) {
147                         __putname(tmp);
148                         result = ERR_PTR(retval);
149                 }
150         }
151         if (unlikely(current->audit_context) && !IS_ERR(result) && result)
152                 audit_getname(result);
153         return result;
154 }
155
156 /*
157  *      vfs_permission()
158  *
159  * is used to check for read/write/execute permissions on a file.
160  * We use "fsuid" for this, letting us set arbitrary permissions
161  * for filesystem access without changing the "normal" uids which
162  * are used for other things..
163  */
164 int vfs_permission(struct inode * inode, int mask)
165 {
166         umode_t                 mode = inode->i_mode;
167
168         /* Prevent vservers from escaping chroot() barriers */
169         if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN))
170                 return -EACCES;
171
172         if (mask & MAY_WRITE) {
173                 /*
174                  * Nobody gets write access to a read-only fs.
175                  */
176                 if (IS_RDONLY(inode) &&
177                     (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
178                         return -EROFS;
179
180                 /*
181                  * Nobody gets write access to an immutable file.
182                  */
183                 if (IS_IMMUTABLE(inode))
184                         return -EACCES;
185         }
186
187         if (current->fsuid == inode->i_uid)
188                 mode >>= 6;
189         else if (in_group_p(inode->i_gid))
190                 mode >>= 3;
191
192         /*
193          * If the DACs are ok we don't need any capability check.
194          */
195         if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
196                 return 0;
197
198         /*
199          * Read/write DACs are always overridable.
200          * Executable DACs are overridable if at least one exec bit is set.
201          */
202         if (!(mask & MAY_EXEC) ||
203             (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
204                 if (capable(CAP_DAC_OVERRIDE))
205                         return 0;
206
207         /*
208          * Searching includes executable on directories, else just read.
209          */
210         if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
211                 if (capable(CAP_DAC_READ_SEARCH))
212                         return 0;
213
214         return -EACCES;
215 }
216
217 int permission(struct inode * inode,int mask, struct nameidata *nd)
218 {
219         int retval;
220         int submask;
221         umode_t mode = inode->i_mode;
222
223         /* Ordinary permission routines do not understand MAY_APPEND. */
224         submask = mask & ~MAY_APPEND;
225
226         if (nd && (mask & MAY_WRITE) && MNT_IS_RDONLY(nd->mnt) &&
227                 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
228                 return -EROFS;
229
230         if (inode->i_op && inode->i_op->permission)
231                 retval = inode->i_op->permission(inode, submask, nd);
232         else
233                 retval = vfs_permission(inode, submask);
234         if (retval)
235                 return retval;
236
237         return security_inode_permission(inode, mask, nd);
238 }
239
240 /*
241  * get_write_access() gets write permission for a file.
242  * put_write_access() releases this write permission.
243  * This is used for regular files.
244  * We cannot support write (and maybe mmap read-write shared) accesses and
245  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
246  * can have the following values:
247  * 0: no writers, no VM_DENYWRITE mappings
248  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
249  * > 0: (i_writecount) users are writing to the file.
250  *
251  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
252  * except for the cases where we don't hold i_writecount yet. Then we need to
253  * use {get,deny}_write_access() - these functions check the sign and refuse
254  * to do the change if sign is wrong. Exclusion between them is provided by
255  * the inode->i_lock spinlock.
256  */
257
258 int get_write_access(struct inode * inode)
259 {
260         spin_lock(&inode->i_lock);
261         if (atomic_read(&inode->i_writecount) < 0) {
262                 spin_unlock(&inode->i_lock);
263                 return -ETXTBSY;
264         }
265         atomic_inc(&inode->i_writecount);
266         spin_unlock(&inode->i_lock);
267
268         return 0;
269 }
270
271 int deny_write_access(struct file * file)
272 {
273         struct inode *inode = file->f_dentry->d_inode;
274
275         spin_lock(&inode->i_lock);
276         if (atomic_read(&inode->i_writecount) > 0) {
277                 spin_unlock(&inode->i_lock);
278                 return -ETXTBSY;
279         }
280         atomic_dec(&inode->i_writecount);
281         spin_unlock(&inode->i_lock);
282
283         return 0;
284 }
285
286 void path_release(struct nameidata *nd)
287 {
288         dput(nd->dentry);
289         mntput(nd->mnt);
290 }
291
292 /*
293  * umount() mustn't call path_release()/mntput() as that would clear
294  * mnt_expiry_mark
295  */
296 void path_release_on_umount(struct nameidata *nd)
297 {
298         dput(nd->dentry);
299         _mntput(nd->mnt);
300 }
301
302 /*
303  * Internal lookup() using the new generic dcache.
304  * SMP-safe
305  */
306 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
307 {
308         struct dentry * dentry = __d_lookup(parent, name);
309
310         /* lockess __d_lookup may fail due to concurrent d_move() 
311          * in some unrelated directory, so try with d_lookup
312          */
313         if (!dentry)
314                 dentry = d_lookup(parent, name);
315
316         if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
317                 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
318                         dput(dentry);
319                         dentry = NULL;
320                 }
321         }
322         return dentry;
323 }
324
325 /*
326  * Short-cut version of permission(), for calling by
327  * path_walk(), when dcache lock is held.  Combines parts
328  * of permission() and vfs_permission(), and tests ONLY for
329  * MAY_EXEC permission.
330  *
331  * If appropriate, check DAC only.  If not appropriate, or
332  * short-cut DAC fails, then call permission() to do more
333  * complete permission check.
334  */
335 static inline int exec_permission_lite(struct inode *inode,
336                                        struct nameidata *nd)
337 {
338         umode_t mode = inode->i_mode;
339
340         if (inode->i_op && inode->i_op->permission)
341                 return -EAGAIN;
342
343         if (current->fsuid == inode->i_uid)
344                 mode >>= 6;
345         else if (in_group_p(inode->i_gid))
346                 mode >>= 3;
347
348         if (mode & MAY_EXEC)
349                 goto ok;
350
351         if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
352                 goto ok;
353
354         if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
355                 goto ok;
356
357         if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
358                 goto ok;
359
360         return -EACCES;
361 ok:
362         return security_inode_permission(inode, MAY_EXEC, nd);
363 }
364
365 /*
366  * This is called when everything else fails, and we actually have
367  * to go to the low-level filesystem to find out what we should do..
368  *
369  * We get the directory semaphore, and after getting that we also
370  * make sure that nobody added the entry to the dcache in the meantime..
371  * SMP-safe
372  */
373 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
374 {
375         struct dentry * result;
376         struct inode *dir = parent->d_inode;
377
378         down(&dir->i_sem);
379         /*
380          * First re-do the cached lookup just in case it was created
381          * while we waited for the directory semaphore..
382          *
383          * FIXME! This could use version numbering or similar to
384          * avoid unnecessary cache lookups.
385          *
386          * The "dcache_lock" is purely to protect the RCU list walker
387          * from concurrent renames at this point (we mustn't get false
388          * negatives from the RCU list walk here, unlike the optimistic
389          * fast walk).
390          *
391          * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
392          */
393         result = d_lookup(parent, name);
394         if (!result) {
395                 struct dentry * dentry = d_alloc(parent, name);
396                 result = ERR_PTR(-ENOMEM);
397                 if (dentry) {
398                         result = dir->i_op->lookup(dir, dentry, nd);
399                         if (result)
400                                 dput(dentry);
401                         else
402                                 result = dentry;
403                 }
404                 up(&dir->i_sem);
405                 return result;
406         }
407
408         /*
409          * Uhhuh! Nasty case: the cache was re-populated while
410          * we waited on the semaphore. Need to revalidate.
411          */
412         up(&dir->i_sem);
413         if (result->d_op && result->d_op->d_revalidate) {
414                 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
415                         dput(result);
416                         result = ERR_PTR(-ENOENT);
417                 }
418         }
419         return result;
420 }
421
422 static int __emul_lookup_dentry(const char *, struct nameidata *);
423
424 /* SMP-safe */
425 static inline int
426 walk_init_root(const char *name, struct nameidata *nd)
427 {
428         read_lock(&current->fs->lock);
429         if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
430                 nd->mnt = mntget(current->fs->altrootmnt);
431                 nd->dentry = dget(current->fs->altroot);
432                 read_unlock(&current->fs->lock);
433                 if (__emul_lookup_dentry(name,nd))
434                         return 0;
435                 read_lock(&current->fs->lock);
436         }
437         nd->mnt = mntget(current->fs->rootmnt);
438         nd->dentry = dget(current->fs->root);
439         read_unlock(&current->fs->lock);
440         return 1;
441 }
442
443 static inline int __vfs_follow_link(struct nameidata *nd, const char *link)
444 {
445         int res = 0;
446         char *name;
447         if (IS_ERR(link))
448                 goto fail;
449
450         if (*link == '/') {
451                 path_release(nd);
452                 if (!walk_init_root(link, nd))
453                         /* weird __emul_prefix() stuff did it */
454                         goto out;
455         }
456         res = link_path_walk(link, nd);
457 out:
458         if (nd->depth || res || nd->last_type!=LAST_NORM)
459                 return res;
460         /*
461          * If it is an iterative symlinks resolution in open_namei() we
462          * have to copy the last component. And all that crap because of
463          * bloody create() on broken symlinks. Furrfu...
464          */
465         name = __getname();
466         if (unlikely(!name)) {
467                 path_release(nd);
468                 return -ENOMEM;
469         }
470         strcpy(name, nd->last.name);
471         nd->last.name = name;
472         return 0;
473 fail:
474         path_release(nd);
475         return PTR_ERR(link);
476 }
477
478 /*
479  * This limits recursive symlink follows to 8, while
480  * limiting consecutive symlinks to 40.
481  *
482  * Without that kind of total limit, nasty chains of consecutive
483  * symlinks can cause almost arbitrarily long lookups. 
484  */
485 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
486 {
487         int err = -ELOOP;
488         if (current->link_count >= MAX_NESTED_LINKS)
489                 goto loop;
490         if (current->total_link_count >= 40)
491                 goto loop;
492         BUG_ON(nd->depth >= MAX_NESTED_LINKS);
493         cond_resched();
494         err = security_inode_follow_link(dentry, nd);
495         if (err)
496                 goto loop;
497         current->link_count++;
498         current->total_link_count++;
499         nd->depth++;
500         touch_atime(nd->mnt, dentry);
501         nd_set_link(nd, NULL);
502         err = dentry->d_inode->i_op->follow_link(dentry, nd);
503         if (!err) {
504                 char *s = nd_get_link(nd);
505                 if (s)
506                         err = __vfs_follow_link(nd, s);
507                 if (dentry->d_inode->i_op->put_link)
508                         dentry->d_inode->i_op->put_link(dentry, nd);
509         }
510         current->link_count--;
511         nd->depth--;
512         return err;
513 loop:
514         path_release(nd);
515         return err;
516 }
517
518 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
519 {
520         struct vfsmount *parent;
521         struct dentry *mountpoint;
522         spin_lock(&vfsmount_lock);
523         parent=(*mnt)->mnt_parent;
524         if (parent == *mnt) {
525                 spin_unlock(&vfsmount_lock);
526                 return 0;
527         }
528         mntget(parent);
529         mountpoint=dget((*mnt)->mnt_mountpoint);
530         spin_unlock(&vfsmount_lock);
531         dput(*dentry);
532         *dentry = mountpoint;
533         mntput(*mnt);
534         *mnt = parent;
535         return 1;
536 }
537
538 /* no need for dcache_lock, as serialization is taken care in
539  * namespace.c
540  */
541 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
542 {
543         int res = 0;
544         while (d_mountpoint(*dentry)) {
545                 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
546                 if (!mounted)
547                         break;
548                 mntput(*mnt);
549                 *mnt = mounted;
550                 dput(*dentry);
551                 *dentry = dget(mounted->mnt_root);
552                 res = 1;
553         }
554         return res;
555 }
556
557 /* no need for dcache_lock, as serialization is taken care in
558  * namespace.c
559  */
560 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
561 {
562         struct vfsmount *mounted;
563
564         mounted = lookup_mnt(*mnt, *dentry);
565         if (mounted) {
566                 mntput(*mnt);
567                 *mnt = mounted;
568                 dput(*dentry);
569                 *dentry = dget(mounted->mnt_root);
570                 return 1;
571         }
572         return 0;
573 }
574
575 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
576 {
577         return __follow_down(mnt,dentry);
578 }
579  
580 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
581 {
582         while(1) {
583                 struct vfsmount *parent;
584                 struct dentry *old = *dentry;
585
586                 read_lock(&current->fs->lock);
587                 if (*dentry == current->fs->root &&
588                     *mnt == current->fs->rootmnt) {
589                         read_unlock(&current->fs->lock);
590                         break;
591                 }
592                 read_unlock(&current->fs->lock);
593                 spin_lock(&dcache_lock);
594                 if (*dentry != (*mnt)->mnt_root) {
595                         *dentry = dget((*dentry)->d_parent);
596                         spin_unlock(&dcache_lock);
597                         dput(old);
598                         break;
599                 }
600                 spin_unlock(&dcache_lock);
601                 spin_lock(&vfsmount_lock);
602                 parent = (*mnt)->mnt_parent;
603                 if (parent == *mnt) {
604                         spin_unlock(&vfsmount_lock);
605                         break;
606                 }
607                 mntget(parent);
608                 *dentry = dget((*mnt)->mnt_mountpoint);
609                 spin_unlock(&vfsmount_lock);
610                 dput(old);
611                 mntput(*mnt);
612                 *mnt = parent;
613         }
614         follow_mount(mnt, dentry);
615 }
616
617 struct path {
618         struct vfsmount *mnt;
619         struct dentry *dentry;
620 };
621
622 /*
623  *  It's more convoluted than I'd like it to be, but... it's still fairly
624  *  small and for now I'd prefer to have fast path as straight as possible.
625  *  It _is_ time-critical.
626  */
627 static int do_lookup(struct nameidata *nd, struct qstr *name,
628                      struct path *path, int atomic)
629 {
630         struct vfsmount *mnt = nd->mnt;
631         struct dentry *dentry = __d_lookup(nd->dentry, name);
632
633         if (!dentry)
634                 goto need_lookup;
635         if (dentry->d_op && dentry->d_op->d_revalidate)
636                 goto need_revalidate;
637 done:
638         path->mnt = mnt;
639         path->dentry = dentry;
640         return 0;
641
642 need_lookup:
643         if (atomic)
644                 return -EWOULDBLOCKIO;
645         dentry = real_lookup(nd->dentry, name, nd);
646         if (IS_ERR(dentry))
647                 goto fail;
648         goto done;
649
650 need_revalidate:
651         if (atomic)
652                 return -EWOULDBLOCKIO;
653         if (dentry->d_op->d_revalidate(dentry, nd))
654                 goto done;
655         if (d_invalidate(dentry))
656                 goto done;
657         dput(dentry);
658         goto need_lookup;
659
660 fail:
661         return PTR_ERR(dentry);
662 }
663
664 /*
665  * Name resolution.
666  *
667  * This is the basic name resolution function, turning a pathname
668  * into the final dentry.
669  *
670  * We expect 'base' to be positive and a directory.
671  */
672 int fastcall link_path_walk(const char * name, struct nameidata *nd)
673 {
674         struct path next;
675         struct inode *inode;
676         int err, atomic;
677         unsigned int lookup_flags = nd->flags;
678
679         atomic = (lookup_flags & LOOKUP_ATOMIC);
680
681         while (*name=='/')
682                 name++;
683         if (!*name)
684                 goto return_reval;
685
686         inode = nd->dentry->d_inode;
687         if (nd->depth)
688                 lookup_flags = LOOKUP_FOLLOW;
689
690         /* At this point we know we have a real path component. */
691         for(;;) {
692                 unsigned long hash;
693                 struct qstr this;
694                 unsigned int c;
695
696                 err = exec_permission_lite(inode, nd);
697                 if (err == -EAGAIN) { 
698                         err = permission(inode, MAY_EXEC, nd);
699                 }
700                 if (err)
701                         break;
702
703                 this.name = name;
704                 c = *(const unsigned char *)name;
705
706                 hash = init_name_hash();
707                 do {
708                         name++;
709                         hash = partial_name_hash(c, hash);
710                         c = *(const unsigned char *)name;
711                 } while (c && (c != '/'));
712                 this.len = name - (const char *) this.name;
713                 this.hash = end_name_hash(hash);
714
715                 /* remove trailing slashes? */
716                 if (!c)
717                         goto last_component;
718                 while (*++name == '/');
719                 if (!*name)
720                         goto last_with_slashes;
721
722                 /*
723                  * "." and ".." are special - ".." especially so because it has
724                  * to be able to know about the current root directory and
725                  * parent relationships.
726                  */
727                 if (this.name[0] == '.') switch (this.len) {
728                         default:
729                                 break;
730                         case 2: 
731                                 if (this.name[1] != '.')
732                                         break;
733                                 follow_dotdot(&nd->mnt, &nd->dentry);
734                                 inode = nd->dentry->d_inode;
735                                 /* fallthrough */
736                         case 1:
737                                 continue;
738                 }
739                 /*
740                  * See if the low-level filesystem might want
741                  * to use its own hash..
742                  */
743                 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
744                         err = nd->dentry->d_op->d_hash(nd->dentry, &this);
745                         if (err < 0)
746                                 break;
747                 }
748                 nd->flags |= LOOKUP_CONTINUE;
749                 /* This does the actual lookups.. */
750                 err = do_lookup(nd, &this, &next, atomic);
751                 if (err)
752                         break;
753                 /* Check mountpoints.. */
754                 follow_mount(&next.mnt, &next.dentry);
755
756                 err = -ENOENT;
757                 inode = next.dentry->d_inode;
758                 if (!inode)
759                         goto out_dput;
760                 err = -ENOTDIR; 
761                 if (!inode->i_op)
762                         goto out_dput;
763
764                 if (inode->i_op->follow_link) {
765                         mntget(next.mnt);
766                         err = do_follow_link(next.dentry, nd);
767                         dput(next.dentry);
768                         mntput(next.mnt);
769                         if (err)
770                                 goto return_err;
771                         err = -ENOENT;
772                         inode = nd->dentry->d_inode;
773                         if (!inode)
774                                 break;
775                         err = -ENOTDIR; 
776                         if (!inode->i_op)
777                                 break;
778                 } else {
779                         dput(nd->dentry);
780                         nd->mnt = next.mnt;
781                         nd->dentry = next.dentry;
782                 }
783                 err = -ENOTDIR; 
784                 if (!inode->i_op->lookup)
785                         break;
786                 continue;
787                 /* here ends the main loop */
788
789 last_with_slashes:
790                 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
791 last_component:
792                 nd->flags &= ~LOOKUP_CONTINUE;
793                 if (lookup_flags & LOOKUP_PARENT)
794                         goto lookup_parent;
795                 if (this.name[0] == '.') switch (this.len) {
796                         default:
797                                 break;
798                         case 2: 
799                                 if (this.name[1] != '.')
800                                         break;
801                                 follow_dotdot(&nd->mnt, &nd->dentry);
802                                 inode = nd->dentry->d_inode;
803                                 /* fallthrough */
804                         case 1:
805                                 goto return_reval;
806                 }
807                 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
808                         err = nd->dentry->d_op->d_hash(nd->dentry, &this);
809                         if (err < 0)
810                                 break;
811                 }
812                 err = do_lookup(nd, &this, &next, atomic);
813                 if (err)
814                         break;
815                 follow_mount(&next.mnt, &next.dentry);
816                 inode = next.dentry->d_inode;
817                 if ((lookup_flags & LOOKUP_FOLLOW)
818                     && inode && inode->i_op && inode->i_op->follow_link) {
819                         mntget(next.mnt);
820                         err = do_follow_link(next.dentry, nd);
821                         dput(next.dentry);
822                         mntput(next.mnt);
823                         if (err)
824                                 goto return_err;
825                         inode = nd->dentry->d_inode;
826                 } else {
827                         dput(nd->dentry);
828                         nd->mnt = next.mnt;
829                         nd->dentry = next.dentry;
830                 }
831                 err = -ENOENT;
832                 if (!inode)
833                         break;
834                 if (lookup_flags & LOOKUP_DIRECTORY) {
835                         err = -ENOTDIR; 
836                         if (!inode->i_op || !inode->i_op->lookup)
837                                 break;
838                 }
839                 goto return_base;
840 lookup_parent:
841                 nd->last = this;
842                 nd->last_type = LAST_NORM;
843                 if (this.name[0] != '.')
844                         goto return_base;
845                 if (this.len == 1)
846                         nd->last_type = LAST_DOT;
847                 else if (this.len == 2 && this.name[1] == '.')
848                         nd->last_type = LAST_DOTDOT;
849                 else
850                         goto return_base;
851 return_reval:
852                 /*
853                  * We bypassed the ordinary revalidation routines.
854                  * We may need to check the cached dentry for staleness.
855                  */
856                 if (nd->dentry && nd->dentry->d_sb &&
857                     (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
858                         err = -ESTALE;
859                         /* Note: we do not d_invalidate() */
860                         if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
861                                 break;
862                 }
863 return_base:
864                 return 0;
865 out_dput:
866                 dput(next.dentry);
867                 break;
868         }
869         path_release(nd);
870 return_err:
871         return err;
872 }
873
874 int fastcall path_walk(const char * name, struct nameidata *nd)
875 {
876         current->total_link_count = 0;
877         return link_path_walk(name, nd);
878 }
879
880 /* SMP-safe */
881 /* returns 1 if everything is done */
882 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
883 {
884         if (path_walk(name, nd))
885                 return 0;               /* something went wrong... */
886
887         if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
888                 struct dentry *old_dentry = nd->dentry;
889                 struct vfsmount *old_mnt = nd->mnt;
890                 struct qstr last = nd->last;
891                 int last_type = nd->last_type;
892                 /*
893                  * NAME was not found in alternate root or it's a directory.  Try to find
894                  * it in the normal root:
895                  */
896                 nd->last_type = LAST_ROOT;
897                 read_lock(&current->fs->lock);
898                 nd->mnt = mntget(current->fs->rootmnt);
899                 nd->dentry = dget(current->fs->root);
900                 read_unlock(&current->fs->lock);
901                 if (path_walk(name, nd) == 0) {
902                         if (nd->dentry->d_inode) {
903                                 dput(old_dentry);
904                                 mntput(old_mnt);
905                                 return 1;
906                         }
907                         path_release(nd);
908                 }
909                 nd->dentry = old_dentry;
910                 nd->mnt = old_mnt;
911                 nd->last = last;
912                 nd->last_type = last_type;
913         }
914         return 1;
915 }
916
917 void set_fs_altroot(void)
918 {
919         char *emul = __emul_prefix();
920         struct nameidata nd;
921         struct vfsmount *mnt = NULL, *oldmnt;
922         struct dentry *dentry = NULL, *olddentry;
923         int err;
924
925         if (!emul)
926                 goto set_it;
927         err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
928         if (!err) {
929                 mnt = nd.mnt;
930                 dentry = nd.dentry;
931         }
932 set_it:
933         write_lock(&current->fs->lock);
934         oldmnt = current->fs->altrootmnt;
935         olddentry = current->fs->altroot;
936         current->fs->altrootmnt = mnt;
937         current->fs->altroot = dentry;
938         write_unlock(&current->fs->lock);
939         if (olddentry) {
940                 dput(olddentry);
941                 mntput(oldmnt);
942         }
943 }
944
945 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
946 {
947         int retval;
948
949         nd->last_type = LAST_ROOT; /* if there are only slashes... */
950         nd->flags = flags;
951         nd->depth = 0;
952
953         read_lock(&current->fs->lock);
954         if (*name=='/') {
955                 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
956                         nd->mnt = mntget(current->fs->altrootmnt);
957                         nd->dentry = dget(current->fs->altroot);
958                         read_unlock(&current->fs->lock);
959                         if (__emul_lookup_dentry(name,nd))
960                                 return 0;
961                         read_lock(&current->fs->lock);
962                 }
963                 nd->mnt = mntget(current->fs->rootmnt);
964                 nd->dentry = dget(current->fs->root);
965         } else {
966                 nd->mnt = mntget(current->fs->pwdmnt);
967                 nd->dentry = dget(current->fs->pwd);
968         }
969         read_unlock(&current->fs->lock);
970         current->total_link_count = 0;
971         retval = link_path_walk(name, nd);
972         if (unlikely(current->audit_context
973                      && nd && nd->dentry && nd->dentry->d_inode))
974                 audit_inode(name,
975                             nd->dentry->d_inode->i_ino,
976                             nd->dentry->d_inode->i_rdev);
977         return retval;
978 }
979
980 /*
981  * Restricted form of lookup. Doesn't follow links, single-component only,
982  * needs parent already locked. Doesn't follow mounts.
983  * SMP-safe.
984  */
985 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
986 {
987         struct dentry * dentry;
988         struct inode *inode;
989         int err;
990
991         inode = base->d_inode;
992         err = permission(inode, MAY_EXEC, nd);
993         dentry = ERR_PTR(err);
994         if (err)
995                 goto out;
996
997         /*
998          * See if the low-level filesystem might want
999          * to use its own hash..
1000          */
1001         if (base->d_op && base->d_op->d_hash) {
1002                 err = base->d_op->d_hash(base, name);
1003                 dentry = ERR_PTR(err);
1004                 if (err < 0)
1005                         goto out;
1006         }
1007
1008         dentry = cached_lookup(base, name, nd);
1009         if (!dentry) {
1010                 struct dentry *new = d_alloc(base, name);
1011                 dentry = ERR_PTR(-ENOMEM);
1012                 if (!new)
1013                         goto out;
1014                 dentry = inode->i_op->lookup(inode, new, nd);
1015                 if (!dentry)
1016                         dentry = new;
1017                 else
1018                         dput(new);
1019         }
1020 out:
1021         return dentry;
1022 }
1023
1024 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
1025 {
1026         return __lookup_hash(name, base, NULL);
1027 }
1028
1029 /* SMP-safe */
1030 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1031 {
1032         unsigned long hash;
1033         struct qstr this;
1034         unsigned int c;
1035
1036         this.name = name;
1037         this.len = len;
1038         if (!len)
1039                 goto access;
1040
1041         hash = init_name_hash();
1042         while (len--) {
1043                 c = *(const unsigned char *)name++;
1044                 if (c == '/' || c == '\0')
1045                         goto access;
1046                 hash = partial_name_hash(c, hash);
1047         }
1048         this.hash = end_name_hash(hash);
1049
1050         return lookup_hash(&this, base);
1051 access:
1052         return ERR_PTR(-EACCES);
1053 }
1054
1055 /*
1056  *      namei()
1057  *
1058  * is used by most simple commands to get the inode of a specified name.
1059  * Open, link etc use their own routines, but this is enough for things
1060  * like 'chmod' etc.
1061  *
1062  * namei exists in two versions: namei/lnamei. The only difference is
1063  * that namei follows links, while lnamei does not.
1064  * SMP-safe
1065  */
1066 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1067 {
1068         char *tmp = getname(name);
1069         int err = PTR_ERR(tmp);
1070
1071         if (!IS_ERR(tmp)) {
1072                 err = path_lookup(tmp, flags, nd);
1073                 putname(tmp);
1074         }
1075         return err;
1076 }
1077
1078 /*
1079  * It's inline, so penalty for filesystems that don't use sticky bit is
1080  * minimal.
1081  */
1082 static inline int check_sticky(struct inode *dir, struct inode *inode)
1083 {
1084         if (!(dir->i_mode & S_ISVTX))
1085                 return 0;
1086         if (inode->i_uid == current->fsuid)
1087                 return 0;
1088         if (dir->i_uid == current->fsuid)
1089                 return 0;
1090         return !capable(CAP_FOWNER);
1091 }
1092
1093 /*
1094  *      Check whether we can remove a link victim from directory dir, check
1095  *  whether the type of victim is right.
1096  *  1. We can't do it if dir is read-only (done in permission())
1097  *  2. We should have write and exec permissions on dir
1098  *  3. We can't remove anything from append-only dir
1099  *  4. We can't do anything with immutable dir (done in permission())
1100  *  5. If the sticky bit on dir is set we should either
1101  *      a. be owner of dir, or
1102  *      b. be owner of victim, or
1103  *      c. have CAP_FOWNER capability
1104  *  6. If the victim is append-only or immutable we can't do antyhing with
1105  *     links pointing to it.
1106  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1107  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1108  *  9. We can't remove a root or mountpoint.
1109  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1110  *     nfs_async_unlink().
1111  */
1112 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1113 {
1114         int error;
1115
1116         if (!victim->d_inode)
1117                 return -ENOENT;
1118
1119         BUG_ON(victim->d_parent->d_inode != dir);
1120
1121         error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1122         if (error)
1123                 return error;
1124         if (IS_APPEND(dir))
1125                 return -EPERM;
1126         if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1127                 IS_IXORUNLINK(victim->d_inode))
1128                 return -EPERM;
1129         if (isdir) {
1130                 if (!S_ISDIR(victim->d_inode->i_mode))
1131                         return -ENOTDIR;
1132                 if (IS_ROOT(victim))
1133                         return -EBUSY;
1134         } else if (S_ISDIR(victim->d_inode->i_mode))
1135                 return -EISDIR;
1136         if (IS_DEADDIR(dir))
1137                 return -ENOENT;
1138         if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1139                 return -EBUSY;
1140         return 0;
1141 }
1142
1143 /*      Check whether we can create an object with dentry child in directory
1144  *  dir.
1145  *  1. We can't do it if child already exists (open has special treatment for
1146  *     this case, but since we are inlined it's OK)
1147  *  2. We can't do it if dir is read-only (done in permission())
1148  *  3. We should have write and exec permissions on dir
1149  *  4. We can't do it if dir is immutable (done in permission())
1150  */
1151 static inline int may_create(struct inode *dir, struct dentry *child,
1152                              struct nameidata *nd)
1153 {
1154         if (child->d_inode)
1155                 return -EEXIST;
1156         if (IS_DEADDIR(dir))
1157                 return -ENOENT;
1158         return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1159 }
1160
1161 static inline int mnt_may_create(struct vfsmount *mnt, struct inode *dir, struct dentry *child) {
1162        if (child->d_inode)
1163                return -EEXIST;
1164        if (IS_DEADDIR(dir))
1165                return -ENOENT;
1166        if (mnt->mnt_flags & MNT_RDONLY)
1167                return -EROFS;
1168        return 0;
1169 }
1170
1171 static inline int mnt_may_unlink(struct vfsmount *mnt, struct inode *dir, struct dentry *child) {
1172        if (!child->d_inode)
1173                return -ENOENT;
1174        if (mnt->mnt_flags & MNT_RDONLY)
1175                return -EROFS;
1176        return 0;
1177 }
1178
1179 /* 
1180  * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1181  * reasons.
1182  *
1183  * O_DIRECTORY translates into forcing a directory lookup.
1184  */
1185 static inline int lookup_flags(unsigned int f)
1186 {
1187         unsigned long retval = LOOKUP_FOLLOW;
1188
1189         if (f & O_NOFOLLOW)
1190                 retval &= ~LOOKUP_FOLLOW;
1191         
1192         if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1193                 retval &= ~LOOKUP_FOLLOW;
1194         
1195         if (f & O_DIRECTORY)
1196                 retval |= LOOKUP_DIRECTORY;
1197         if (f & O_ATOMICLOOKUP)
1198                 retval |= LOOKUP_ATOMIC;
1199
1200         return retval;
1201 }
1202
1203 /*
1204  * p1 and p2 should be directories on the same fs.
1205  */
1206 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1207 {
1208         struct dentry *p;
1209
1210         if (p1 == p2) {
1211                 down(&p1->d_inode->i_sem);
1212                 return NULL;
1213         }
1214
1215         down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1216
1217         for (p = p1; p->d_parent != p; p = p->d_parent) {
1218                 if (p->d_parent == p2) {
1219                         down(&p2->d_inode->i_sem);
1220                         down(&p1->d_inode->i_sem);
1221                         return p;
1222                 }
1223         }
1224
1225         for (p = p2; p->d_parent != p; p = p->d_parent) {
1226                 if (p->d_parent == p1) {
1227                         down(&p1->d_inode->i_sem);
1228                         down(&p2->d_inode->i_sem);
1229                         return p;
1230                 }
1231         }
1232
1233         down(&p1->d_inode->i_sem);
1234         down(&p2->d_inode->i_sem);
1235         return NULL;
1236 }
1237
1238 void unlock_rename(struct dentry *p1, struct dentry *p2)
1239 {
1240         up(&p1->d_inode->i_sem);
1241         if (p1 != p2) {
1242                 up(&p2->d_inode->i_sem);
1243                 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1244         }
1245 }
1246
1247 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1248                 struct nameidata *nd)
1249 {
1250         int error = may_create(dir, dentry, nd);
1251
1252         if (error)
1253                 return error;
1254
1255         if (!dir->i_op || !dir->i_op->create)
1256                 return -EACCES; /* shouldn't it be ENOSYS? */
1257         mode &= S_IALLUGO;
1258         mode |= S_IFREG;
1259         error = security_inode_create(dir, dentry, mode);
1260         if (error)
1261                 return error;
1262         DQUOT_INIT(dir);
1263         error = dir->i_op->create(dir, dentry, mode, nd);
1264         if (!error) {
1265                 inode_dir_notify(dir, DN_CREATE);
1266                 security_inode_post_create(dir, dentry, mode);
1267         }
1268         return error;
1269 }
1270
1271 int may_open(struct nameidata *nd, int acc_mode, int flag)
1272 {
1273         struct dentry *dentry = nd->dentry;
1274         struct inode *inode = dentry->d_inode;
1275         int error;
1276
1277         if (!inode)
1278                 return -ENOENT;
1279
1280         if (S_ISLNK(inode->i_mode))
1281                 return -ELOOP;
1282         
1283         if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1284                 return -EISDIR;
1285
1286         error = permission(inode, acc_mode, nd);
1287         if (error)
1288                 return error;
1289
1290         /*
1291          * FIFO's, sockets and device files are special: they don't
1292          * actually live on the filesystem itself, and as such you
1293          * can write to them even if the filesystem is read-only.
1294          */
1295         if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1296                 flag &= ~O_TRUNC;
1297         } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1298                 if (nd->mnt->mnt_flags & MNT_NODEV)
1299                         return -EACCES;
1300
1301                 flag &= ~O_TRUNC;
1302         } else if ((IS_RDONLY(inode) || (nd && MNT_IS_RDONLY(nd->mnt)))
1303                 && (flag & FMODE_WRITE))
1304                 return -EROFS;
1305         /*
1306          * An append-only file must be opened in append mode for writing.
1307          */
1308         if (IS_APPEND(inode)) {
1309                 if  ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1310                         return -EPERM;
1311                 if (flag & O_TRUNC)
1312                         return -EPERM;
1313         }
1314
1315         /* O_NOATIME can only be set by the owner or superuser */
1316         if (flag & O_NOATIME)
1317                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1318                         return -EPERM;
1319
1320         /*
1321          * Ensure there are no outstanding leases on the file.
1322          */
1323         error = break_lease(inode, flag);
1324         if (error)
1325                 return error;
1326
1327         if (flag & O_TRUNC) {
1328                 error = get_write_access(inode);
1329                 if (error)
1330                         return error;
1331
1332                 /*
1333                  * Refuse to truncate files with mandatory locks held on them.
1334                  */
1335                 error = locks_verify_locked(inode);
1336                 if (!error) {
1337                         DQUOT_INIT(inode);
1338                         
1339                         error = do_truncate(dentry, 0);
1340                 }
1341                 put_write_access(inode);
1342                 if (error)
1343                         return error;
1344         } else
1345                 if (flag & FMODE_WRITE)
1346                         DQUOT_INIT(inode);
1347
1348         return 0;
1349 }
1350
1351 /*
1352  *      open_namei()
1353  *
1354  * namei for open - this is in fact almost the whole open-routine.
1355  *
1356  * Note that the low bits of "flag" aren't the same as in the open
1357  * system call - they are 00 - no permissions needed
1358  *                        01 - read permission needed
1359  *                        10 - write permission needed
1360  *                        11 - read/write permissions needed
1361  * which is a lot more logical, and also allows the "no perm" needed
1362  * for symlinks (where the permissions are checked later).
1363  * SMP-safe
1364  */
1365 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1366 {
1367         int acc_mode, error = 0;
1368         struct dentry *dentry;
1369         struct dentry *dir;
1370         int count = 0;
1371
1372         acc_mode = ACC_MODE(flag);
1373
1374         /* Allow the LSM permission hook to distinguish append 
1375            access from general write access. */
1376         if (flag & O_APPEND)
1377                 acc_mode |= MAY_APPEND;
1378
1379         /* Fill in the open() intent data */
1380         nd->intent.open.flags = flag;
1381         nd->intent.open.create_mode = mode;
1382
1383         /*
1384          * The simplest case - just a plain lookup.
1385          */
1386         if (!(flag & O_CREAT)) {
1387                 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1388                 if (error)
1389                         return error;
1390                 goto ok;
1391         }
1392
1393         /*
1394          * Create - we need to know the parent.
1395          */
1396         error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1397         if (error)
1398                 return error;
1399
1400         /*
1401          * We have the parent and last component. First of all, check
1402          * that we are not asked to creat(2) an obvious directory - that
1403          * will not do.
1404          */
1405         error = -EISDIR;
1406         if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1407                 goto exit;
1408
1409         dir = nd->dentry;
1410         nd->flags &= ~LOOKUP_PARENT;
1411         down(&dir->d_inode->i_sem);
1412         dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1413
1414 do_last:
1415         error = PTR_ERR(dentry);
1416         if (IS_ERR(dentry)) {
1417                 up(&dir->d_inode->i_sem);
1418                 goto exit;
1419         }
1420
1421         /* Negative dentry, just create the file */
1422         if (!dentry->d_inode) {
1423                 if (!IS_POSIXACL(dir->d_inode))
1424                         mode &= ~current->fs->umask;
1425                 error = vfs_create(dir->d_inode, dentry, mode, nd);
1426                 up(&dir->d_inode->i_sem);
1427                 dput(nd->dentry);
1428                 nd->dentry = dentry;
1429                 if (error)
1430                         goto exit;
1431                 /* Don't check for write permission, don't truncate */
1432                 acc_mode = 0;
1433                 flag &= ~O_TRUNC;
1434                 goto ok;
1435         }
1436
1437         /*
1438          * It already exists.
1439          */
1440         up(&dir->d_inode->i_sem);
1441
1442         error = -EEXIST;
1443         if (flag & O_EXCL)
1444                 goto exit_dput;
1445
1446         if (d_mountpoint(dentry)) {
1447                 error = -ELOOP;
1448                 if (flag & O_NOFOLLOW)
1449                         goto exit_dput;
1450                 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1451         }
1452         error = -ENOENT;
1453         if (!dentry->d_inode)
1454                 goto exit_dput;
1455         if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1456                 goto do_link;
1457
1458         dput(nd->dentry);
1459         nd->dentry = dentry;
1460         error = -EISDIR;
1461         if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1462                 goto exit;
1463 ok:
1464         error = may_open(nd, acc_mode, flag);
1465         if (error)
1466                 goto exit;
1467         return 0;
1468
1469 exit_dput:
1470         dput(dentry);
1471 exit:
1472         path_release(nd);
1473         return error;
1474
1475 do_link:
1476         error = -ELOOP;
1477         if (flag & O_NOFOLLOW)
1478                 goto exit_dput;
1479         /*
1480          * This is subtle. Instead of calling do_follow_link() we do the
1481          * thing by hands. The reason is that this way we have zero link_count
1482          * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1483          * After that we have the parent and last component, i.e.
1484          * we are in the same situation as after the first path_walk().
1485          * Well, almost - if the last component is normal we get its copy
1486          * stored in nd->last.name and we will have to putname() it when we
1487          * are done. Procfs-like symlinks just set LAST_BIND.
1488          */
1489         nd->flags |= LOOKUP_PARENT;
1490         error = security_inode_follow_link(dentry, nd);
1491         if (error)
1492                 goto exit_dput;
1493         touch_atime(nd->mnt, dentry);
1494         nd_set_link(nd, NULL);
1495         error = dentry->d_inode->i_op->follow_link(dentry, nd);
1496         if (!error) {
1497                 char *s = nd_get_link(nd);
1498                 if (s)
1499                         error = __vfs_follow_link(nd, s);
1500                 if (dentry->d_inode->i_op->put_link)
1501                         dentry->d_inode->i_op->put_link(dentry, nd);
1502         }
1503         dput(dentry);
1504         if (error)
1505                 return error;
1506         nd->flags &= ~LOOKUP_PARENT;
1507         if (nd->last_type == LAST_BIND) {
1508                 dentry = nd->dentry;
1509                 goto ok;
1510         }
1511         error = -EISDIR;
1512         if (nd->last_type != LAST_NORM)
1513                 goto exit;
1514         if (nd->last.name[nd->last.len]) {
1515                 putname(nd->last.name);
1516                 goto exit;
1517         }
1518         error = -ELOOP;
1519         if (count++==32) {
1520                 putname(nd->last.name);
1521                 goto exit;
1522         }
1523         dir = nd->dentry;
1524         down(&dir->d_inode->i_sem);
1525         dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1526         putname(nd->last.name);
1527         goto do_last;
1528 }
1529
1530 /**
1531  * lookup_create - lookup a dentry, creating it if it doesn't exist
1532  * @nd: nameidata info
1533  * @is_dir: directory flag
1534  *
1535  * Simple function to lookup and return a dentry and create it
1536  * if it doesn't exist.  Is SMP-safe.
1537  */
1538 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1539 {
1540         struct dentry *dentry;
1541         int error;
1542
1543         down(&nd->dentry->d_inode->i_sem);
1544         error = -EEXIST;
1545         if (nd->last_type != LAST_NORM)
1546                 goto out;
1547         nd->flags &= ~LOOKUP_PARENT;
1548         dentry = lookup_hash(&nd->last, nd->dentry);
1549         if (IS_ERR(dentry))
1550                 goto ret;
1551         error = mnt_may_create(nd->mnt, nd->dentry->d_inode, dentry);
1552         if (error)
1553                 goto fail;
1554         error = -ENOENT;
1555         if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1556                 goto fail;
1557 ret:
1558         return dentry;
1559 fail:
1560         dput(dentry);
1561 out:
1562         return ERR_PTR(error);
1563 }
1564
1565 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1566 {
1567         int error = may_create(dir, dentry, NULL);
1568
1569         if (error)
1570                 return error;
1571
1572         if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1573                 return -EPERM;
1574
1575         if (!dir->i_op || !dir->i_op->mknod)
1576                 return -EPERM;
1577
1578         error = security_inode_mknod(dir, dentry, mode, dev);
1579         if (error)
1580                 return error;
1581
1582         DQUOT_INIT(dir);
1583         error = dir->i_op->mknod(dir, dentry, mode, dev);
1584         if (!error) {
1585                 inode_dir_notify(dir, DN_CREATE);
1586                 security_inode_post_mknod(dir, dentry, mode, dev);
1587         }
1588         return error;
1589 }
1590
1591 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1592 {
1593         int error = 0;
1594         char * tmp;
1595         struct dentry * dentry;
1596         struct nameidata nd;
1597
1598         if (S_ISDIR(mode))
1599                 return -EPERM;
1600         tmp = getname(filename);
1601         if (IS_ERR(tmp))
1602                 return PTR_ERR(tmp);
1603
1604         error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1605         if (error)
1606                 goto out;
1607         dentry = lookup_create(&nd, 0);
1608         error = PTR_ERR(dentry);
1609
1610         if (!IS_POSIXACL(nd.dentry->d_inode))
1611                 mode &= ~current->fs->umask;
1612         if (!IS_ERR(dentry)) {
1613                 switch (mode & S_IFMT) {
1614                 case 0: case S_IFREG:
1615                         error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1616                         break;
1617                 case S_IFCHR: case S_IFBLK:
1618                         error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1619                                         new_decode_dev(dev));
1620                         break;
1621                 case S_IFIFO: case S_IFSOCK:
1622                         error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1623                         break;
1624                 case S_IFDIR:
1625                         error = -EPERM;
1626                         break;
1627                 default:
1628                         error = -EINVAL;
1629                 }
1630                 dput(dentry);
1631         }
1632         up(&nd.dentry->d_inode->i_sem);
1633         path_release(&nd);
1634 out:
1635         putname(tmp);
1636
1637         return error;
1638 }
1639
1640 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1641 {
1642         int error = may_create(dir, dentry, NULL);
1643
1644         if (error)
1645                 return error;
1646
1647         if (!dir->i_op || !dir->i_op->mkdir)
1648                 return -EPERM;
1649
1650         mode &= (S_IRWXUGO|S_ISVTX);
1651         error = security_inode_mkdir(dir, dentry, mode);
1652         if (error)
1653                 return error;
1654
1655         DQUOT_INIT(dir);
1656         error = dir->i_op->mkdir(dir, dentry, mode);
1657         if (!error) {
1658                 inode_dir_notify(dir, DN_CREATE);
1659                 security_inode_post_mkdir(dir,dentry, mode);
1660         }
1661         return error;
1662 }
1663
1664 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1665 {
1666         int error = 0;
1667         char * tmp;
1668
1669         tmp = getname(pathname);
1670         error = PTR_ERR(tmp);
1671         if (!IS_ERR(tmp)) {
1672                 struct dentry *dentry;
1673                 struct nameidata nd;
1674
1675                 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1676                 if (error)
1677                         goto out;
1678                 dentry = lookup_create(&nd, 1);
1679                 error = PTR_ERR(dentry);
1680                 if (!IS_ERR(dentry)) {
1681                         if (!IS_POSIXACL(nd.dentry->d_inode))
1682                                 mode &= ~current->fs->umask;
1683                         error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1684                         dput(dentry);
1685                 }
1686                 up(&nd.dentry->d_inode->i_sem);
1687                 path_release(&nd);
1688 out:
1689                 putname(tmp);
1690         }
1691
1692         return error;
1693 }
1694
1695 /*
1696  * We try to drop the dentry early: we should have
1697  * a usage count of 2 if we're the only user of this
1698  * dentry, and if that is true (possibly after pruning
1699  * the dcache), then we drop the dentry now.
1700  *
1701  * A low-level filesystem can, if it choses, legally
1702  * do a
1703  *
1704  *      if (!d_unhashed(dentry))
1705  *              return -EBUSY;
1706  *
1707  * if it cannot handle the case of removing a directory
1708  * that is still in use by something else..
1709  */
1710 void dentry_unhash(struct dentry *dentry)
1711 {
1712         dget(dentry);
1713         spin_lock(&dcache_lock);
1714         switch (atomic_read(&dentry->d_count)) {
1715         default:
1716                 spin_unlock(&dcache_lock);
1717                 shrink_dcache_parent(dentry);
1718                 spin_lock(&dcache_lock);
1719                 if (atomic_read(&dentry->d_count) != 2)
1720                         break;
1721         case 2:
1722                 __d_drop(dentry);
1723         }
1724         spin_unlock(&dcache_lock);
1725 }
1726
1727 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1728 {
1729         int error = may_delete(dir, dentry, 1);
1730
1731         if (error)
1732                 return error;
1733
1734         if (!dir->i_op || !dir->i_op->rmdir)
1735                 return -EPERM;
1736
1737         DQUOT_INIT(dir);
1738
1739         down(&dentry->d_inode->i_sem);
1740         dentry_unhash(dentry);
1741         if (d_mountpoint(dentry))
1742                 error = -EBUSY;
1743         else {
1744                 error = security_inode_rmdir(dir, dentry);
1745                 if (!error) {
1746                         error = dir->i_op->rmdir(dir, dentry);
1747                         if (!error)
1748                                 dentry->d_inode->i_flags |= S_DEAD;
1749                 }
1750         }
1751         up(&dentry->d_inode->i_sem);
1752         if (!error) {
1753                 inode_dir_notify(dir, DN_DELETE);
1754                 d_delete(dentry);
1755         }
1756         dput(dentry);
1757
1758         return error;
1759 }
1760
1761 asmlinkage long sys_rmdir(const char __user * pathname)
1762 {
1763         int error = 0;
1764         char * name;
1765         struct dentry *dentry;
1766         struct nameidata nd;
1767
1768         name = getname(pathname);
1769         if(IS_ERR(name))
1770                 return PTR_ERR(name);
1771
1772         error = path_lookup(name, LOOKUP_PARENT, &nd);
1773         if (error)
1774                 goto exit;
1775
1776         switch(nd.last_type) {
1777                 case LAST_DOTDOT:
1778                         error = -ENOTEMPTY;
1779                         goto exit1;
1780                 case LAST_DOT:
1781                         error = -EINVAL;
1782                         goto exit1;
1783                 case LAST_ROOT:
1784                         error = -EBUSY;
1785                         goto exit1;
1786         }
1787         down(&nd.dentry->d_inode->i_sem);
1788         dentry = lookup_hash(&nd.last, nd.dentry);
1789         error = PTR_ERR(dentry);
1790         if (!IS_ERR(dentry)) {
1791                 error = mnt_may_unlink(nd.mnt, nd.dentry->d_inode, dentry);
1792                 if (error)
1793                         goto exit2;
1794                 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1795         exit2:
1796                 dput(dentry);
1797         }
1798         up(&nd.dentry->d_inode->i_sem);
1799 exit1:
1800         path_release(&nd);
1801 exit:
1802         putname(name);
1803         return error;
1804 }
1805
1806 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1807 {
1808         int error = may_delete(dir, dentry, 0);
1809
1810         if (error)
1811                 return error;
1812
1813         if (!dir->i_op || !dir->i_op->unlink)
1814                 return -EPERM;
1815
1816         DQUOT_INIT(dir);
1817
1818         down(&dentry->d_inode->i_sem);
1819         if (d_mountpoint(dentry))
1820                 error = -EBUSY;
1821         else {
1822                 error = security_inode_unlink(dir, dentry);
1823                 if (!error)
1824                         error = dir->i_op->unlink(dir, dentry);
1825         }
1826         up(&dentry->d_inode->i_sem);
1827
1828         /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1829         if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1830                 d_delete(dentry);
1831                 inode_dir_notify(dir, DN_DELETE);
1832         }
1833         return error;
1834 }
1835
1836 /*
1837  * Make sure that the actual truncation of the file will occur outside its
1838  * directory's i_sem.  Truncate can take a long time if there is a lot of
1839  * writeout happening, and we don't want to prevent access to the directory
1840  * while waiting on the I/O.
1841  */
1842 asmlinkage long sys_unlink(const char __user * pathname)
1843 {
1844         int error = 0;
1845         char * name;
1846         struct dentry *dentry;
1847         struct nameidata nd;
1848         struct inode *inode = NULL;
1849
1850         name = getname(pathname);
1851         if(IS_ERR(name))
1852                 return PTR_ERR(name);
1853
1854         error = path_lookup(name, LOOKUP_PARENT, &nd);
1855         if (error)
1856                 goto exit;
1857         error = -EISDIR;
1858         if (nd.last_type != LAST_NORM)
1859                 goto exit1;
1860         down(&nd.dentry->d_inode->i_sem);
1861         dentry = lookup_hash(&nd.last, nd.dentry);
1862         error = PTR_ERR(dentry);
1863         if (!IS_ERR(dentry)) {
1864                 /* Why not before? Because we want correct error value */
1865                 if (nd.last.name[nd.last.len])
1866                         goto slashes;
1867                 error = mnt_may_unlink(nd.mnt, nd.dentry->d_inode, dentry);
1868                 if (error)
1869                         goto exit2;
1870                 inode = dentry->d_inode;
1871                 if (inode)
1872                         atomic_inc(&inode->i_count);
1873                 error = vfs_unlink(nd.dentry->d_inode, dentry);
1874         exit2:
1875                 dput(dentry);
1876         }
1877         up(&nd.dentry->d_inode->i_sem);
1878         if (inode)
1879                 iput(inode);    /* truncate the inode here */
1880 exit1:
1881         path_release(&nd);
1882 exit:
1883         putname(name);
1884         return error;
1885
1886 slashes:
1887         error = !dentry->d_inode ? -ENOENT :
1888                 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1889         goto exit2;
1890 }
1891
1892 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
1893 {
1894         int error = may_create(dir, dentry, NULL);
1895
1896         if (error)
1897                 return error;
1898
1899         if (!dir->i_op || !dir->i_op->symlink)
1900                 return -EPERM;
1901
1902         error = security_inode_symlink(dir, dentry, oldname);
1903         if (error)
1904                 return error;
1905
1906         DQUOT_INIT(dir);
1907         error = dir->i_op->symlink(dir, dentry, oldname);
1908         if (!error) {
1909                 inode_dir_notify(dir, DN_CREATE);
1910                 security_inode_post_symlink(dir, dentry, oldname);
1911         }
1912         return error;
1913 }
1914
1915 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1916 {
1917         int error = 0;
1918         char * from;
1919         char * to;
1920
1921         from = getname(oldname);
1922         if(IS_ERR(from))
1923                 return PTR_ERR(from);
1924         to = getname(newname);
1925         error = PTR_ERR(to);
1926         if (!IS_ERR(to)) {
1927                 struct dentry *dentry;
1928                 struct nameidata nd;
1929
1930                 error = path_lookup(to, LOOKUP_PARENT, &nd);
1931                 if (error)
1932                         goto out;
1933                 dentry = lookup_create(&nd, 0);
1934                 error = PTR_ERR(dentry);
1935                 if (!IS_ERR(dentry)) {
1936                         error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
1937                         dput(dentry);
1938                 }
1939                 up(&nd.dentry->d_inode->i_sem);
1940                 path_release(&nd);
1941 out:
1942                 putname(to);
1943         }
1944         putname(from);
1945         return error;
1946 }
1947
1948 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1949 {
1950         struct inode *inode = old_dentry->d_inode;
1951         int error;
1952
1953         if (!inode)
1954                 return -ENOENT;
1955
1956         error = may_create(dir, new_dentry, NULL);
1957         if (error)
1958                 return error;
1959
1960         if (dir->i_sb != inode->i_sb)
1961                 return -EXDEV;
1962
1963         /*
1964          * A link to an append-only or immutable file cannot be created.
1965          */
1966         if (IS_APPEND(inode) || IS_IXORUNLINK(inode))
1967                 return -EPERM;
1968         if (!dir->i_op || !dir->i_op->link)
1969                 return -EPERM;
1970         if (S_ISDIR(old_dentry->d_inode->i_mode))
1971                 return -EPERM;
1972
1973         error = security_inode_link(old_dentry, dir, new_dentry);
1974         if (error)
1975                 return error;
1976
1977         down(&old_dentry->d_inode->i_sem);
1978         DQUOT_INIT(dir);
1979         error = dir->i_op->link(old_dentry, dir, new_dentry);
1980         up(&old_dentry->d_inode->i_sem);
1981         if (!error) {
1982                 inode_dir_notify(dir, DN_CREATE);
1983                 security_inode_post_link(old_dentry, dir, new_dentry);
1984         }
1985         return error;
1986 }
1987
1988 /*
1989  * Hardlinks are often used in delicate situations.  We avoid
1990  * security-related surprises by not following symlinks on the
1991  * newname.  --KAB
1992  *
1993  * We don't follow them on the oldname either to be compatible
1994  * with linux 2.0, and to avoid hard-linking to directories
1995  * and other special files.  --ADM
1996  */
1997 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
1998 {
1999         struct dentry *new_dentry;
2000         struct nameidata nd, old_nd;
2001         int error;
2002         char * to;
2003
2004         to = getname(newname);
2005         if (IS_ERR(to))
2006                 return PTR_ERR(to);
2007
2008         error = __user_walk(oldname, 0, &old_nd);
2009         if (error)
2010                 goto exit;
2011         error = path_lookup(to, LOOKUP_PARENT, &nd);
2012         if (error)
2013                 goto out;
2014         /*
2015          * We allow hard-links to be created to a bind-mount as long
2016          * as the bind-mount is not read-only.  Checking for cross-dev
2017          * links is subsumed by the superblock check in vfs_link().
2018          */
2019         error = -EROFS;
2020         if (MNT_IS_RDONLY(old_nd.mnt))
2021                 goto out_release;
2022         new_dentry = lookup_create(&nd, 0);
2023         error = PTR_ERR(new_dentry);
2024         if (!IS_ERR(new_dentry)) {
2025                 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2026                 dput(new_dentry);
2027         }
2028         up(&nd.dentry->d_inode->i_sem);
2029 out_release:
2030         path_release(&nd);
2031 out:
2032         path_release(&old_nd);
2033 exit:
2034         putname(to);
2035
2036         return error;
2037 }
2038
2039 /*
2040  * The worst of all namespace operations - renaming directory. "Perverted"
2041  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2042  * Problems:
2043  *      a) we can get into loop creation. Check is done in is_subdir().
2044  *      b) race potential - two innocent renames can create a loop together.
2045  *         That's where 4.4 screws up. Current fix: serialization on
2046  *         sb->s_vfs_rename_sem. We might be more accurate, but that's another
2047  *         story.
2048  *      c) we have to lock _three_ objects - parents and victim (if it exists).
2049  *         And that - after we got ->i_sem on parents (until then we don't know
2050  *         whether the target exists).  Solution: try to be smart with locking
2051  *         order for inodes.  We rely on the fact that tree topology may change
2052  *         only under ->s_vfs_rename_sem _and_ that parent of the object we
2053  *         move will be locked.  Thus we can rank directories by the tree
2054  *         (ancestors first) and rank all non-directories after them.
2055  *         That works since everybody except rename does "lock parent, lookup,
2056  *         lock child" and rename is under ->s_vfs_rename_sem.
2057  *         HOWEVER, it relies on the assumption that any object with ->lookup()
2058  *         has no more than 1 dentry.  If "hybrid" objects will ever appear,
2059  *         we'd better make sure that there's no link(2) for them.
2060  *      d) some filesystems don't support opened-but-unlinked directories,
2061  *         either because of layout or because they are not ready to deal with
2062  *         all cases correctly. The latter will be fixed (taking this sort of
2063  *         stuff into VFS), but the former is not going away. Solution: the same
2064  *         trick as in rmdir().
2065  *      e) conversion from fhandle to dentry may come in the wrong moment - when
2066  *         we are removing the target. Solution: we will have to grab ->i_sem
2067  *         in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2068  *         ->i_sem on parents, which works but leads to some truely excessive
2069  *         locking].
2070  */
2071 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2072                struct inode *new_dir, struct dentry *new_dentry)
2073 {
2074         int error = 0;
2075         struct inode *target;
2076
2077         /*
2078          * If we are going to change the parent - check write permissions,
2079          * we'll need to flip '..'.
2080          */
2081         if (new_dir != old_dir) {
2082                 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2083                 if (error)
2084                         return error;
2085         }
2086
2087         error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2088         if (error)
2089                 return error;
2090
2091         target = new_dentry->d_inode;
2092         if (target) {
2093                 down(&target->i_sem);
2094                 dentry_unhash(new_dentry);
2095         }
2096         if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2097                 error = -EBUSY;
2098         else 
2099                 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2100         if (target) {
2101                 if (!error)
2102                         target->i_flags |= S_DEAD;
2103                 up(&target->i_sem);
2104                 if (d_unhashed(new_dentry))
2105                         d_rehash(new_dentry);
2106                 dput(new_dentry);
2107         }
2108         if (!error) {
2109                 d_move(old_dentry,new_dentry);
2110                 security_inode_post_rename(old_dir, old_dentry,
2111                                            new_dir, new_dentry);
2112         }
2113         return error;
2114 }
2115
2116 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2117                struct inode *new_dir, struct dentry *new_dentry)
2118 {
2119         struct inode *target;
2120         int error;
2121
2122         error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2123         if (error)
2124                 return error;
2125
2126         dget(new_dentry);
2127         target = new_dentry->d_inode;
2128         if (target)
2129                 down(&target->i_sem);
2130         if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2131                 error = -EBUSY;
2132         else
2133                 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2134         if (!error) {
2135                 /* The following d_move() should become unconditional */
2136                 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2137                         d_move(old_dentry, new_dentry);
2138                 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
2139         }
2140         if (target)
2141                 up(&target->i_sem);
2142         dput(new_dentry);
2143         return error;
2144 }
2145
2146 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2147                struct inode *new_dir, struct dentry *new_dentry)
2148 {
2149         int error;
2150         int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2151
2152         if (old_dentry->d_inode == new_dentry->d_inode)
2153                 return 0;
2154  
2155         error = may_delete(old_dir, old_dentry, is_dir);
2156         if (error)
2157                 return error;
2158
2159         if (!new_dentry->d_inode)
2160                 error = may_create(new_dir, new_dentry, NULL);
2161         else
2162                 error = may_delete(new_dir, new_dentry, is_dir);
2163         if (error)
2164                 return error;
2165
2166         if (!old_dir->i_op || !old_dir->i_op->rename)
2167                 return -EPERM;
2168
2169         DQUOT_INIT(old_dir);
2170         DQUOT_INIT(new_dir);
2171
2172         if (is_dir)
2173                 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2174         else
2175                 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2176         if (!error) {
2177                 if (old_dir == new_dir)
2178                         inode_dir_notify(old_dir, DN_RENAME);
2179                 else {
2180                         inode_dir_notify(old_dir, DN_DELETE);
2181                         inode_dir_notify(new_dir, DN_CREATE);
2182                 }
2183         }
2184         return error;
2185 }
2186
2187 static inline int do_rename(const char * oldname, const char * newname)
2188 {
2189         int error = 0;
2190         struct dentry * old_dir, * new_dir;
2191         struct dentry * old_dentry, *new_dentry;
2192         struct dentry * trap;
2193         struct nameidata oldnd, newnd;
2194
2195         error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2196         if (error)
2197                 goto exit;
2198
2199         error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2200         if (error)
2201                 goto exit1;
2202
2203         error = -EXDEV;
2204         if (oldnd.mnt != newnd.mnt)
2205                 goto exit2;
2206
2207         old_dir = oldnd.dentry;
2208         error = -EBUSY;
2209         if (oldnd.last_type != LAST_NORM)
2210                 goto exit2;
2211
2212         new_dir = newnd.dentry;
2213         if (newnd.last_type != LAST_NORM)
2214                 goto exit2;
2215
2216         trap = lock_rename(new_dir, old_dir);
2217
2218         old_dentry = lookup_hash(&oldnd.last, old_dir);
2219         error = PTR_ERR(old_dentry);
2220         if (IS_ERR(old_dentry))
2221                 goto exit3;
2222         /* source must exist */
2223         error = -ENOENT;
2224         if (!old_dentry->d_inode)
2225                 goto exit4;
2226         /* unless the source is a directory trailing slashes give -ENOTDIR */
2227         if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2228                 error = -ENOTDIR;
2229                 if (oldnd.last.name[oldnd.last.len])
2230                         goto exit4;
2231                 if (newnd.last.name[newnd.last.len])
2232                         goto exit4;
2233         }
2234         /* source should not be ancestor of target */
2235         error = -EINVAL;
2236         if (old_dentry == trap)
2237                 goto exit4;
2238         error = -EROFS;
2239         if (MNT_IS_RDONLY(newnd.mnt))
2240                 goto exit4;
2241         new_dentry = lookup_hash(&newnd.last, new_dir);
2242         error = PTR_ERR(new_dentry);
2243         if (IS_ERR(new_dentry))
2244                 goto exit4;
2245         /* target should not be an ancestor of source */
2246         error = -ENOTEMPTY;
2247         if (new_dentry == trap)
2248                 goto exit5;
2249
2250         error = vfs_rename(old_dir->d_inode, old_dentry,
2251                                    new_dir->d_inode, new_dentry);
2252 exit5:
2253         dput(new_dentry);
2254 exit4:
2255         dput(old_dentry);
2256 exit3:
2257         unlock_rename(new_dir, old_dir);
2258 exit2:
2259         path_release(&newnd);
2260 exit1:
2261         path_release(&oldnd);
2262 exit:
2263         return error;
2264 }
2265
2266 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2267 {
2268         int error;
2269         char * from;
2270         char * to;
2271
2272         from = getname(oldname);
2273         if(IS_ERR(from))
2274                 return PTR_ERR(from);
2275         to = getname(newname);
2276         error = PTR_ERR(to);
2277         if (!IS_ERR(to)) {
2278                 error = do_rename(from,to);
2279                 putname(to);
2280         }
2281         putname(from);
2282         return error;
2283 }
2284
2285 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2286 {
2287         int len;
2288
2289         len = PTR_ERR(link);
2290         if (IS_ERR(link))
2291                 goto out;
2292
2293         len = strlen(link);
2294         if (len > (unsigned) buflen)
2295                 len = buflen;
2296         if (copy_to_user(buffer, link, len))
2297                 len = -EFAULT;
2298 out:
2299         return len;
2300 }
2301
2302 /*
2303  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
2304  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
2305  * using) it for any given inode is up to filesystem.
2306  */
2307 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2308 {
2309         struct nameidata nd;
2310         int res;
2311         nd.depth = 0;
2312         res = dentry->d_inode->i_op->follow_link(dentry, &nd);
2313         if (!res) {
2314                 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2315                 if (dentry->d_inode->i_op->put_link)
2316                         dentry->d_inode->i_op->put_link(dentry, &nd);
2317         }
2318         return res;
2319 }
2320
2321 int vfs_follow_link(struct nameidata *nd, const char *link)
2322 {
2323         return __vfs_follow_link(nd, link);
2324 }
2325
2326 /* get the link contents into pagecache */
2327 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2328 {
2329         struct page * page;
2330         struct address_space *mapping = dentry->d_inode->i_mapping;
2331         page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2332                                 NULL);
2333         if (IS_ERR(page))
2334                 goto sync_fail;
2335         wait_on_page_locked(page);
2336         if (!PageUptodate(page))
2337                 goto async_fail;
2338         *ppage = page;
2339         return kmap(page);
2340
2341 async_fail:
2342         page_cache_release(page);
2343         return ERR_PTR(-EIO);
2344
2345 sync_fail:
2346         return (char*)page;
2347 }
2348
2349 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2350 {
2351         struct page *page = NULL;
2352         char *s = page_getlink(dentry, &page);
2353         int res = vfs_readlink(dentry,buffer,buflen,s);
2354         if (page) {
2355                 kunmap(page);
2356                 page_cache_release(page);
2357         }
2358         return res;
2359 }
2360
2361 int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2362 {
2363         struct page *page;
2364         nd_set_link(nd, page_getlink(dentry, &page));
2365         return 0;
2366 }
2367
2368 void page_put_link(struct dentry *dentry, struct nameidata *nd)
2369 {
2370         if (!IS_ERR(nd_get_link(nd))) {
2371                 struct page *page;
2372                 page = find_get_page(dentry->d_inode->i_mapping, 0);
2373                 if (!page)
2374                         BUG();
2375                 kunmap(page);
2376                 page_cache_release(page);
2377                 page_cache_release(page);
2378         }
2379 }
2380
2381 int page_symlink(struct inode *inode, const char *symname, int len)
2382 {
2383         struct address_space *mapping = inode->i_mapping;
2384         struct page *page = grab_cache_page(mapping, 0);
2385         int err = -ENOMEM;
2386         char *kaddr;
2387
2388         if (!page)
2389                 goto fail;
2390         err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2391         if (err)
2392                 goto fail_map;
2393         kaddr = kmap_atomic(page, KM_USER0);
2394         memcpy(kaddr, symname, len-1);
2395         kunmap_atomic(kaddr, KM_USER0);
2396         mapping->a_ops->commit_write(NULL, page, 0, len-1);
2397         /*
2398          * Notice that we are _not_ going to block here - end of page is
2399          * unmapped, so this will only try to map the rest of page, see
2400          * that it is unmapped (typically even will not look into inode -
2401          * ->i_size will be enough for everything) and zero it out.
2402          * OTOH it's obviously correct and should make the page up-to-date.
2403          */
2404         if (!PageUptodate(page)) {
2405                 err = mapping->a_ops->readpage(NULL, page);
2406                 wait_on_page_locked(page);
2407         } else {
2408                 unlock_page(page);
2409         }
2410         page_cache_release(page);
2411         if (err < 0)
2412                 goto fail;
2413         mark_inode_dirty(inode);
2414         return 0;
2415 fail_map:
2416         unlock_page(page);
2417         page_cache_release(page);
2418 fail:
2419         return err;
2420 }
2421
2422 struct inode_operations page_symlink_inode_operations = {
2423         .readlink       = generic_readlink,
2424         .follow_link    = page_follow_link_light,
2425         .put_link       = page_put_link,
2426 };
2427
2428 EXPORT_SYMBOL(__user_walk);
2429 EXPORT_SYMBOL(follow_down);
2430 EXPORT_SYMBOL(follow_up);
2431 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2432 EXPORT_SYMBOL(getname);
2433 EXPORT_SYMBOL(lock_rename);
2434 EXPORT_SYMBOL(lookup_hash);
2435 EXPORT_SYMBOL(lookup_one_len);
2436 EXPORT_SYMBOL(page_follow_link_light);
2437 EXPORT_SYMBOL(page_put_link);
2438 EXPORT_SYMBOL(page_readlink);
2439 EXPORT_SYMBOL(page_symlink);
2440 EXPORT_SYMBOL(page_symlink_inode_operations);
2441 EXPORT_SYMBOL(path_lookup);
2442 EXPORT_SYMBOL(path_release);
2443 EXPORT_SYMBOL(path_walk);
2444 EXPORT_SYMBOL(permission);
2445 EXPORT_SYMBOL(unlock_rename);
2446 EXPORT_SYMBOL(vfs_create);
2447 EXPORT_SYMBOL(vfs_follow_link);
2448 EXPORT_SYMBOL(vfs_link);
2449 EXPORT_SYMBOL(vfs_mkdir);
2450 EXPORT_SYMBOL(vfs_mknod);
2451 EXPORT_SYMBOL(vfs_permission);
2452 EXPORT_SYMBOL(vfs_readlink);
2453 EXPORT_SYMBOL(vfs_rename);
2454 EXPORT_SYMBOL(vfs_rmdir);
2455 EXPORT_SYMBOL(vfs_symlink);
2456 EXPORT_SYMBOL(vfs_unlink);
2457 EXPORT_SYMBOL(dentry_unhash);
2458 EXPORT_SYMBOL(generic_readlink);