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