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