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