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