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