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