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