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