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