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