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