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