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