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