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