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