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