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