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