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