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