vfs: make gcc generate more obvious code for acl permission checking
[pandora-kernel.git] / fs / namei.c
1 /*
2  *  linux/fs/namei.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 /*
8  * Some corrections by tytso.
9  */
10
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12  * lookup logic.
13  */
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15  */
16
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/fs.h>
21 #include <linux/namei.h>
22 #include <linux/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 <linux/posix_acl.h>
36 #include <asm/uaccess.h>
37
38 #include "internal.h"
39
40 /* [Feb-1997 T. Schoebel-Theuer]
41  * Fundamental changes in the pathname lookup mechanisms (namei)
42  * were necessary because of omirr.  The reason is that omirr needs
43  * to know the _real_ pathname, not the user-supplied one, in case
44  * of symlinks (and also when transname replacements occur).
45  *
46  * The new code replaces the old recursive symlink resolution with
47  * an iterative one (in case of non-nested symlink chains).  It does
48  * this with calls to <fs>_follow_link().
49  * As a side effect, dir_namei(), _namei() and follow_link() are now 
50  * replaced with a single function lookup_dentry() that can handle all 
51  * the special cases of the former code.
52  *
53  * With the new dcache, the pathname is stored at each inode, at least as
54  * long as the refcount of the inode is positive.  As a side effect, the
55  * size of the dcache depends on the inode cache and thus is dynamic.
56  *
57  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
58  * resolution to correspond with current state of the code.
59  *
60  * Note that the symlink resolution is not *completely* iterative.
61  * There is still a significant amount of tail- and mid- recursion in
62  * the algorithm.  Also, note that <fs>_readlink() is not used in
63  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
64  * may return different results than <fs>_follow_link().  Many virtual
65  * filesystems (including /proc) exhibit this behavior.
66  */
67
68 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
69  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
70  * and the name already exists in form of a symlink, try to create the new
71  * name indicated by the symlink. The old code always complained that the
72  * name already exists, due to not following the symlink even if its target
73  * is nonexistent.  The new semantics affects also mknod() and link() when
74  * the name is a symlink pointing to a non-existent name.
75  *
76  * I don't know which semantics is the right one, since I have no access
77  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
78  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
79  * "old" one. Personally, I think the new semantics is much more logical.
80  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
81  * file does succeed in both HP-UX and SunOs, but not in Solaris
82  * and in the old Linux semantics.
83  */
84
85 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
86  * semantics.  See the comments in "open_namei" and "do_link" below.
87  *
88  * [10-Sep-98 Alan Modra] Another symlink change.
89  */
90
91 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
92  *      inside the path - always follow.
93  *      in the last component in creation/removal/renaming - never follow.
94  *      if LOOKUP_FOLLOW passed - follow.
95  *      if the pathname has trailing slashes - follow.
96  *      otherwise - don't follow.
97  * (applied in that order).
98  *
99  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
100  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
101  * During the 2.4 we need to fix the userland stuff depending on it -
102  * hopefully we will be able to get rid of that wart in 2.5. So far only
103  * XEmacs seems to be relying on it...
104  */
105 /*
106  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
107  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
108  * any extra contention...
109  */
110
111 /* In order to reduce some races, while at the same time doing additional
112  * checking and hopefully speeding things up, we copy filenames to the
113  * kernel data space before using them..
114  *
115  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
116  * PATH_MAX includes the nul terminator --RR.
117  */
118 static int do_getname(const char __user *filename, char *page)
119 {
120         int retval;
121         unsigned long len = PATH_MAX;
122
123         if (!segment_eq(get_fs(), KERNEL_DS)) {
124                 if ((unsigned long) filename >= TASK_SIZE)
125                         return -EFAULT;
126                 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
127                         len = TASK_SIZE - (unsigned long) filename;
128         }
129
130         retval = strncpy_from_user(page, filename, len);
131         if (retval > 0) {
132                 if (retval < len)
133                         return 0;
134                 return -ENAMETOOLONG;
135         } else if (!retval)
136                 retval = -ENOENT;
137         return retval;
138 }
139
140 static char *getname_flags(const char __user * filename, int flags)
141 {
142         char *tmp, *result;
143
144         result = ERR_PTR(-ENOMEM);
145         tmp = __getname();
146         if (tmp)  {
147                 int retval = do_getname(filename, tmp);
148
149                 result = tmp;
150                 if (retval < 0) {
151                         if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
152                                 __putname(tmp);
153                                 result = ERR_PTR(retval);
154                         }
155                 }
156         }
157         audit_getname(result);
158         return result;
159 }
160
161 char *getname(const char __user * filename)
162 {
163         return getname_flags(filename, 0);
164 }
165
166 #ifdef CONFIG_AUDITSYSCALL
167 void putname(const char *name)
168 {
169         if (unlikely(!audit_dummy_context()))
170                 audit_putname(name);
171         else
172                 __putname(name);
173 }
174 EXPORT_SYMBOL(putname);
175 #endif
176
177 static int check_acl(struct inode *inode, int mask)
178 {
179         struct posix_acl *acl;
180
181         /*
182          * Under RCU walk, we cannot even do a "get_cached_acl()",
183          * because that involves locking and getting a refcount on
184          * a cached ACL.
185          *
186          * So the only case we handle during RCU walking is the
187          * case of a cached "no ACL at all", which needs no locks
188          * or refcounts.
189          */
190         if (mask & MAY_NOT_BLOCK) {
191                 if (negative_cached_acl(inode, ACL_TYPE_ACCESS))
192                         return -EAGAIN;
193                 return -ECHILD;
194         }
195
196         acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
197
198         /*
199          * A filesystem can force a ACL callback by just never filling the
200          * ACL cache. But normally you'd fill the cache either at inode
201          * instantiation time, or on the first ->get_acl call.
202          *
203          * If the filesystem doesn't have a get_acl() function at all, we'll
204          * just create the negative cache entry.
205          */
206         if (acl == ACL_NOT_CACHED) {
207                 if (inode->i_op->get_acl) {
208                         acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
209                         if (IS_ERR(acl))
210                                 return PTR_ERR(acl);
211                 } else {
212                         set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
213                         return -EAGAIN;
214                 }
215         }
216
217         if (acl) {
218                 int error = posix_acl_permission(inode, acl, mask);
219                 posix_acl_release(acl);
220                 return error;
221         }
222
223         return -EAGAIN;
224 }
225
226 /*
227  * This does basic POSIX ACL permission checking
228  */
229 static int acl_permission_check(struct inode *inode, int mask)
230 {
231         unsigned int mode = inode->i_mode;
232
233         mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
234
235         if (current_user_ns() != inode_userns(inode))
236                 goto other_perms;
237
238         if (likely(current_fsuid() == inode->i_uid))
239                 mode >>= 6;
240         else {
241                 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
242                         int error = check_acl(inode, mask);
243                         if (error != -EAGAIN)
244                                 return error;
245                 }
246
247                 if (in_group_p(inode->i_gid))
248                         mode >>= 3;
249         }
250
251 other_perms:
252         /*
253          * If the DACs are ok we don't need any capability check.
254          */
255         if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
256                 return 0;
257         return -EACCES;
258 }
259
260 /**
261  * generic_permission -  check for access rights on a Posix-like filesystem
262  * @inode:      inode to check access rights for
263  * @mask:       right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
264  *
265  * Used to check for read/write/execute permissions on a file.
266  * We use "fsuid" for this, letting us set arbitrary permissions
267  * for filesystem access without changing the "normal" uids which
268  * are used for other things.
269  *
270  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
271  * request cannot be satisfied (eg. requires blocking or too much complexity).
272  * It would then be called again in ref-walk mode.
273  */
274 int generic_permission(struct inode *inode, int mask)
275 {
276         int ret;
277
278         /*
279          * Do the basic POSIX ACL permission checks.
280          */
281         ret = acl_permission_check(inode, mask);
282         if (ret != -EACCES)
283                 return ret;
284
285         if (S_ISDIR(inode->i_mode)) {
286                 /* DACs are overridable for directories */
287                 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
288                         return 0;
289                 if (!(mask & MAY_WRITE))
290                         if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
291                                 return 0;
292                 return -EACCES;
293         }
294         /*
295          * Read/write DACs are always overridable.
296          * Executable DACs are overridable when there is
297          * at least one exec bit set.
298          */
299         if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
300                 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
301                         return 0;
302
303         /*
304          * Searching includes executable on directories, else just read.
305          */
306         mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
307         if (mask == MAY_READ)
308                 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
309                         return 0;
310
311         return -EACCES;
312 }
313
314 /**
315  * inode_permission  -  check for access rights to a given inode
316  * @inode:      inode to check permission on
317  * @mask:       right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
318  *
319  * Used to check for read/write/execute permissions on an inode.
320  * We use "fsuid" for this, letting us set arbitrary permissions
321  * for filesystem access without changing the "normal" uids which
322  * are used for other things.
323  */
324 int inode_permission(struct inode *inode, int mask)
325 {
326         int retval;
327
328         if (mask & MAY_WRITE) {
329                 umode_t mode = inode->i_mode;
330
331                 /*
332                  * Nobody gets write access to a read-only fs.
333                  */
334                 if (IS_RDONLY(inode) &&
335                     (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
336                         return -EROFS;
337
338                 /*
339                  * Nobody gets write access to an immutable file.
340                  */
341                 if (IS_IMMUTABLE(inode))
342                         return -EACCES;
343         }
344
345         if (inode->i_op->permission)
346                 retval = inode->i_op->permission(inode, mask);
347         else
348                 retval = generic_permission(inode, mask);
349
350         if (retval)
351                 return retval;
352
353         retval = devcgroup_inode_permission(inode, mask);
354         if (retval)
355                 return retval;
356
357         return security_inode_permission(inode, mask);
358 }
359
360 /**
361  * path_get - get a reference to a path
362  * @path: path to get the reference to
363  *
364  * Given a path increment the reference count to the dentry and the vfsmount.
365  */
366 void path_get(struct path *path)
367 {
368         mntget(path->mnt);
369         dget(path->dentry);
370 }
371 EXPORT_SYMBOL(path_get);
372
373 /**
374  * path_put - put a reference to a path
375  * @path: path to put the reference to
376  *
377  * Given a path decrement the reference count to the dentry and the vfsmount.
378  */
379 void path_put(struct path *path)
380 {
381         dput(path->dentry);
382         mntput(path->mnt);
383 }
384 EXPORT_SYMBOL(path_put);
385
386 /*
387  * Path walking has 2 modes, rcu-walk and ref-walk (see
388  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
389  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
390  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
391  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
392  * got stuck, so ref-walk may continue from there. If this is not successful
393  * (eg. a seqcount has changed), then failure is returned and it's up to caller
394  * to restart the path walk from the beginning in ref-walk mode.
395  */
396
397 /**
398  * unlazy_walk - try to switch to ref-walk mode.
399  * @nd: nameidata pathwalk data
400  * @dentry: child of nd->path.dentry or NULL
401  * Returns: 0 on success, -ECHILD on failure
402  *
403  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
404  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
405  * @nd or NULL.  Must be called from rcu-walk context.
406  */
407 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
408 {
409         struct fs_struct *fs = current->fs;
410         struct dentry *parent = nd->path.dentry;
411         int want_root = 0;
412
413         BUG_ON(!(nd->flags & LOOKUP_RCU));
414         if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
415                 want_root = 1;
416                 spin_lock(&fs->lock);
417                 if (nd->root.mnt != fs->root.mnt ||
418                                 nd->root.dentry != fs->root.dentry)
419                         goto err_root;
420         }
421         spin_lock(&parent->d_lock);
422         if (!dentry) {
423                 if (!__d_rcu_to_refcount(parent, nd->seq))
424                         goto err_parent;
425                 BUG_ON(nd->inode != parent->d_inode);
426         } else {
427                 if (dentry->d_parent != parent)
428                         goto err_parent;
429                 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
430                 if (!__d_rcu_to_refcount(dentry, nd->seq))
431                         goto err_child;
432                 /*
433                  * If the sequence check on the child dentry passed, then
434                  * the child has not been removed from its parent. This
435                  * means the parent dentry must be valid and able to take
436                  * a reference at this point.
437                  */
438                 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
439                 BUG_ON(!parent->d_count);
440                 parent->d_count++;
441                 spin_unlock(&dentry->d_lock);
442         }
443         spin_unlock(&parent->d_lock);
444         if (want_root) {
445                 path_get(&nd->root);
446                 spin_unlock(&fs->lock);
447         }
448         mntget(nd->path.mnt);
449
450         rcu_read_unlock();
451         br_read_unlock(vfsmount_lock);
452         nd->flags &= ~LOOKUP_RCU;
453         return 0;
454
455 err_child:
456         spin_unlock(&dentry->d_lock);
457 err_parent:
458         spin_unlock(&parent->d_lock);
459 err_root:
460         if (want_root)
461                 spin_unlock(&fs->lock);
462         return -ECHILD;
463 }
464
465 /**
466  * release_open_intent - free up open intent resources
467  * @nd: pointer to nameidata
468  */
469 void release_open_intent(struct nameidata *nd)
470 {
471         struct file *file = nd->intent.open.file;
472
473         if (file && !IS_ERR(file)) {
474                 if (file->f_path.dentry == NULL)
475                         put_filp(file);
476                 else
477                         fput(file);
478         }
479 }
480
481 static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
482 {
483         return dentry->d_op->d_revalidate(dentry, nd);
484 }
485
486 /**
487  * complete_walk - successful completion of path walk
488  * @nd:  pointer nameidata
489  *
490  * If we had been in RCU mode, drop out of it and legitimize nd->path.
491  * Revalidate the final result, unless we'd already done that during
492  * the path walk or the filesystem doesn't ask for it.  Return 0 on
493  * success, -error on failure.  In case of failure caller does not
494  * need to drop nd->path.
495  */
496 static int complete_walk(struct nameidata *nd)
497 {
498         struct dentry *dentry = nd->path.dentry;
499         int status;
500
501         if (nd->flags & LOOKUP_RCU) {
502                 nd->flags &= ~LOOKUP_RCU;
503                 if (!(nd->flags & LOOKUP_ROOT))
504                         nd->root.mnt = NULL;
505                 spin_lock(&dentry->d_lock);
506                 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
507                         spin_unlock(&dentry->d_lock);
508                         rcu_read_unlock();
509                         br_read_unlock(vfsmount_lock);
510                         return -ECHILD;
511                 }
512                 BUG_ON(nd->inode != dentry->d_inode);
513                 spin_unlock(&dentry->d_lock);
514                 mntget(nd->path.mnt);
515                 rcu_read_unlock();
516                 br_read_unlock(vfsmount_lock);
517         }
518
519         if (likely(!(nd->flags & LOOKUP_JUMPED)))
520                 return 0;
521
522         if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
523                 return 0;
524
525         if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
526                 return 0;
527
528         /* Note: we do not d_invalidate() */
529         status = d_revalidate(dentry, nd);
530         if (status > 0)
531                 return 0;
532
533         if (!status)
534                 status = -ESTALE;
535
536         path_put(&nd->path);
537         return status;
538 }
539
540 static __always_inline void set_root(struct nameidata *nd)
541 {
542         if (!nd->root.mnt)
543                 get_fs_root(current->fs, &nd->root);
544 }
545
546 static int link_path_walk(const char *, struct nameidata *);
547
548 static __always_inline void set_root_rcu(struct nameidata *nd)
549 {
550         if (!nd->root.mnt) {
551                 struct fs_struct *fs = current->fs;
552                 unsigned seq;
553
554                 do {
555                         seq = read_seqcount_begin(&fs->seq);
556                         nd->root = fs->root;
557                         nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
558                 } while (read_seqcount_retry(&fs->seq, seq));
559         }
560 }
561
562 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
563 {
564         int ret;
565
566         if (IS_ERR(link))
567                 goto fail;
568
569         if (*link == '/') {
570                 set_root(nd);
571                 path_put(&nd->path);
572                 nd->path = nd->root;
573                 path_get(&nd->root);
574                 nd->flags |= LOOKUP_JUMPED;
575         }
576         nd->inode = nd->path.dentry->d_inode;
577
578         ret = link_path_walk(link, nd);
579         return ret;
580 fail:
581         path_put(&nd->path);
582         return PTR_ERR(link);
583 }
584
585 static void path_put_conditional(struct path *path, struct nameidata *nd)
586 {
587         dput(path->dentry);
588         if (path->mnt != nd->path.mnt)
589                 mntput(path->mnt);
590 }
591
592 static inline void path_to_nameidata(const struct path *path,
593                                         struct nameidata *nd)
594 {
595         if (!(nd->flags & LOOKUP_RCU)) {
596                 dput(nd->path.dentry);
597                 if (nd->path.mnt != path->mnt)
598                         mntput(nd->path.mnt);
599         }
600         nd->path.mnt = path->mnt;
601         nd->path.dentry = path->dentry;
602 }
603
604 static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
605 {
606         struct inode *inode = link->dentry->d_inode;
607         if (!IS_ERR(cookie) && inode->i_op->put_link)
608                 inode->i_op->put_link(link->dentry, nd, cookie);
609         path_put(link);
610 }
611
612 static __always_inline int
613 follow_link(struct path *link, struct nameidata *nd, void **p)
614 {
615         int error;
616         struct dentry *dentry = link->dentry;
617
618         BUG_ON(nd->flags & LOOKUP_RCU);
619
620         if (link->mnt == nd->path.mnt)
621                 mntget(link->mnt);
622
623         if (unlikely(current->total_link_count >= 40)) {
624                 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
625                 path_put(&nd->path);
626                 return -ELOOP;
627         }
628         cond_resched();
629         current->total_link_count++;
630
631         touch_atime(link->mnt, dentry);
632         nd_set_link(nd, NULL);
633
634         error = security_inode_follow_link(link->dentry, nd);
635         if (error) {
636                 *p = ERR_PTR(error); /* no ->put_link(), please */
637                 path_put(&nd->path);
638                 return error;
639         }
640
641         nd->last_type = LAST_BIND;
642         *p = dentry->d_inode->i_op->follow_link(dentry, nd);
643         error = PTR_ERR(*p);
644         if (!IS_ERR(*p)) {
645                 char *s = nd_get_link(nd);
646                 error = 0;
647                 if (s)
648                         error = __vfs_follow_link(nd, s);
649                 else if (nd->last_type == LAST_BIND) {
650                         nd->flags |= LOOKUP_JUMPED;
651                         nd->inode = nd->path.dentry->d_inode;
652                         if (nd->inode->i_op->follow_link) {
653                                 /* stepped on a _really_ weird one */
654                                 path_put(&nd->path);
655                                 error = -ELOOP;
656                         }
657                 }
658         }
659         return error;
660 }
661
662 static int follow_up_rcu(struct path *path)
663 {
664         struct vfsmount *parent;
665         struct dentry *mountpoint;
666
667         parent = path->mnt->mnt_parent;
668         if (parent == path->mnt)
669                 return 0;
670         mountpoint = path->mnt->mnt_mountpoint;
671         path->dentry = mountpoint;
672         path->mnt = parent;
673         return 1;
674 }
675
676 int follow_up(struct path *path)
677 {
678         struct vfsmount *parent;
679         struct dentry *mountpoint;
680
681         br_read_lock(vfsmount_lock);
682         parent = path->mnt->mnt_parent;
683         if (parent == path->mnt) {
684                 br_read_unlock(vfsmount_lock);
685                 return 0;
686         }
687         mntget(parent);
688         mountpoint = dget(path->mnt->mnt_mountpoint);
689         br_read_unlock(vfsmount_lock);
690         dput(path->dentry);
691         path->dentry = mountpoint;
692         mntput(path->mnt);
693         path->mnt = parent;
694         return 1;
695 }
696
697 /*
698  * Perform an automount
699  * - return -EISDIR to tell follow_managed() to stop and return the path we
700  *   were called with.
701  */
702 static int follow_automount(struct path *path, unsigned flags,
703                             bool *need_mntput)
704 {
705         struct vfsmount *mnt;
706         int err;
707
708         if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
709                 return -EREMOTE;
710
711         /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
712          * and this is the terminal part of the path.
713          */
714         if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_PARENT))
715                 return -EISDIR; /* we actually want to stop here */
716
717         /* We want to mount if someone is trying to open/create a file of any
718          * type under the mountpoint, wants to traverse through the mountpoint
719          * or wants to open the mounted directory.
720          *
721          * We don't want to mount if someone's just doing a stat and they've
722          * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
723          * appended a '/' to the name.
724          */
725         if (!(flags & LOOKUP_FOLLOW) &&
726             !(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
727                        LOOKUP_OPEN | LOOKUP_CREATE)))
728                 return -EISDIR;
729
730         current->total_link_count++;
731         if (current->total_link_count >= 40)
732                 return -ELOOP;
733
734         mnt = path->dentry->d_op->d_automount(path);
735         if (IS_ERR(mnt)) {
736                 /*
737                  * The filesystem is allowed to return -EISDIR here to indicate
738                  * it doesn't want to automount.  For instance, autofs would do
739                  * this so that its userspace daemon can mount on this dentry.
740                  *
741                  * However, we can only permit this if it's a terminal point in
742                  * the path being looked up; if it wasn't then the remainder of
743                  * the path is inaccessible and we should say so.
744                  */
745                 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
746                         return -EREMOTE;
747                 return PTR_ERR(mnt);
748         }
749
750         if (!mnt) /* mount collision */
751                 return 0;
752
753         if (!*need_mntput) {
754                 /* lock_mount() may release path->mnt on error */
755                 mntget(path->mnt);
756                 *need_mntput = true;
757         }
758         err = finish_automount(mnt, path);
759
760         switch (err) {
761         case -EBUSY:
762                 /* Someone else made a mount here whilst we were busy */
763                 return 0;
764         case 0:
765                 path_put(path);
766                 path->mnt = mnt;
767                 path->dentry = dget(mnt->mnt_root);
768                 return 0;
769         default:
770                 return err;
771         }
772
773 }
774
775 /*
776  * Handle a dentry that is managed in some way.
777  * - Flagged for transit management (autofs)
778  * - Flagged as mountpoint
779  * - Flagged as automount point
780  *
781  * This may only be called in refwalk mode.
782  *
783  * Serialization is taken care of in namespace.c
784  */
785 static int follow_managed(struct path *path, unsigned flags)
786 {
787         struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
788         unsigned managed;
789         bool need_mntput = false;
790         int ret = 0;
791
792         /* Given that we're not holding a lock here, we retain the value in a
793          * local variable for each dentry as we look at it so that we don't see
794          * the components of that value change under us */
795         while (managed = ACCESS_ONCE(path->dentry->d_flags),
796                managed &= DCACHE_MANAGED_DENTRY,
797                unlikely(managed != 0)) {
798                 /* Allow the filesystem to manage the transit without i_mutex
799                  * being held. */
800                 if (managed & DCACHE_MANAGE_TRANSIT) {
801                         BUG_ON(!path->dentry->d_op);
802                         BUG_ON(!path->dentry->d_op->d_manage);
803                         ret = path->dentry->d_op->d_manage(path->dentry, false);
804                         if (ret < 0)
805                                 break;
806                 }
807
808                 /* Transit to a mounted filesystem. */
809                 if (managed & DCACHE_MOUNTED) {
810                         struct vfsmount *mounted = lookup_mnt(path);
811                         if (mounted) {
812                                 dput(path->dentry);
813                                 if (need_mntput)
814                                         mntput(path->mnt);
815                                 path->mnt = mounted;
816                                 path->dentry = dget(mounted->mnt_root);
817                                 need_mntput = true;
818                                 continue;
819                         }
820
821                         /* Something is mounted on this dentry in another
822                          * namespace and/or whatever was mounted there in this
823                          * namespace got unmounted before we managed to get the
824                          * vfsmount_lock */
825                 }
826
827                 /* Handle an automount point */
828                 if (managed & DCACHE_NEED_AUTOMOUNT) {
829                         ret = follow_automount(path, flags, &need_mntput);
830                         if (ret < 0)
831                                 break;
832                         continue;
833                 }
834
835                 /* We didn't change the current path point */
836                 break;
837         }
838
839         if (need_mntput && path->mnt == mnt)
840                 mntput(path->mnt);
841         if (ret == -EISDIR)
842                 ret = 0;
843         return ret;
844 }
845
846 int follow_down_one(struct path *path)
847 {
848         struct vfsmount *mounted;
849
850         mounted = lookup_mnt(path);
851         if (mounted) {
852                 dput(path->dentry);
853                 mntput(path->mnt);
854                 path->mnt = mounted;
855                 path->dentry = dget(mounted->mnt_root);
856                 return 1;
857         }
858         return 0;
859 }
860
861 static inline bool managed_dentry_might_block(struct dentry *dentry)
862 {
863         return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
864                 dentry->d_op->d_manage(dentry, true) < 0);
865 }
866
867 /*
868  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
869  * we meet a managed dentry that would need blocking.
870  */
871 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
872                                struct inode **inode)
873 {
874         for (;;) {
875                 struct vfsmount *mounted;
876                 /*
877                  * Don't forget we might have a non-mountpoint managed dentry
878                  * that wants to block transit.
879                  */
880                 if (unlikely(managed_dentry_might_block(path->dentry)))
881                         return false;
882
883                 if (!d_mountpoint(path->dentry))
884                         break;
885
886                 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
887                 if (!mounted)
888                         break;
889                 path->mnt = mounted;
890                 path->dentry = mounted->mnt_root;
891                 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
892                 /*
893                  * Update the inode too. We don't need to re-check the
894                  * dentry sequence number here after this d_inode read,
895                  * because a mount-point is always pinned.
896                  */
897                 *inode = path->dentry->d_inode;
898         }
899         return true;
900 }
901
902 static void follow_mount_rcu(struct nameidata *nd)
903 {
904         while (d_mountpoint(nd->path.dentry)) {
905                 struct vfsmount *mounted;
906                 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
907                 if (!mounted)
908                         break;
909                 nd->path.mnt = mounted;
910                 nd->path.dentry = mounted->mnt_root;
911                 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
912         }
913 }
914
915 static int follow_dotdot_rcu(struct nameidata *nd)
916 {
917         set_root_rcu(nd);
918
919         while (1) {
920                 if (nd->path.dentry == nd->root.dentry &&
921                     nd->path.mnt == nd->root.mnt) {
922                         break;
923                 }
924                 if (nd->path.dentry != nd->path.mnt->mnt_root) {
925                         struct dentry *old = nd->path.dentry;
926                         struct dentry *parent = old->d_parent;
927                         unsigned seq;
928
929                         seq = read_seqcount_begin(&parent->d_seq);
930                         if (read_seqcount_retry(&old->d_seq, nd->seq))
931                                 goto failed;
932                         nd->path.dentry = parent;
933                         nd->seq = seq;
934                         break;
935                 }
936                 if (!follow_up_rcu(&nd->path))
937                         break;
938                 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
939         }
940         follow_mount_rcu(nd);
941         nd->inode = nd->path.dentry->d_inode;
942         return 0;
943
944 failed:
945         nd->flags &= ~LOOKUP_RCU;
946         if (!(nd->flags & LOOKUP_ROOT))
947                 nd->root.mnt = NULL;
948         rcu_read_unlock();
949         br_read_unlock(vfsmount_lock);
950         return -ECHILD;
951 }
952
953 /*
954  * Follow down to the covering mount currently visible to userspace.  At each
955  * point, the filesystem owning that dentry may be queried as to whether the
956  * caller is permitted to proceed or not.
957  */
958 int follow_down(struct path *path)
959 {
960         unsigned managed;
961         int ret;
962
963         while (managed = ACCESS_ONCE(path->dentry->d_flags),
964                unlikely(managed & DCACHE_MANAGED_DENTRY)) {
965                 /* Allow the filesystem to manage the transit without i_mutex
966                  * being held.
967                  *
968                  * We indicate to the filesystem if someone is trying to mount
969                  * something here.  This gives autofs the chance to deny anyone
970                  * other than its daemon the right to mount on its
971                  * superstructure.
972                  *
973                  * The filesystem may sleep at this point.
974                  */
975                 if (managed & DCACHE_MANAGE_TRANSIT) {
976                         BUG_ON(!path->dentry->d_op);
977                         BUG_ON(!path->dentry->d_op->d_manage);
978                         ret = path->dentry->d_op->d_manage(
979                                 path->dentry, false);
980                         if (ret < 0)
981                                 return ret == -EISDIR ? 0 : ret;
982                 }
983
984                 /* Transit to a mounted filesystem. */
985                 if (managed & DCACHE_MOUNTED) {
986                         struct vfsmount *mounted = lookup_mnt(path);
987                         if (!mounted)
988                                 break;
989                         dput(path->dentry);
990                         mntput(path->mnt);
991                         path->mnt = mounted;
992                         path->dentry = dget(mounted->mnt_root);
993                         continue;
994                 }
995
996                 /* Don't handle automount points here */
997                 break;
998         }
999         return 0;
1000 }
1001
1002 /*
1003  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1004  */
1005 static void follow_mount(struct path *path)
1006 {
1007         while (d_mountpoint(path->dentry)) {
1008                 struct vfsmount *mounted = lookup_mnt(path);
1009                 if (!mounted)
1010                         break;
1011                 dput(path->dentry);
1012                 mntput(path->mnt);
1013                 path->mnt = mounted;
1014                 path->dentry = dget(mounted->mnt_root);
1015         }
1016 }
1017
1018 static void follow_dotdot(struct nameidata *nd)
1019 {
1020         set_root(nd);
1021
1022         while(1) {
1023                 struct dentry *old = nd->path.dentry;
1024
1025                 if (nd->path.dentry == nd->root.dentry &&
1026                     nd->path.mnt == nd->root.mnt) {
1027                         break;
1028                 }
1029                 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1030                         /* rare case of legitimate dget_parent()... */
1031                         nd->path.dentry = dget_parent(nd->path.dentry);
1032                         dput(old);
1033                         break;
1034                 }
1035                 if (!follow_up(&nd->path))
1036                         break;
1037         }
1038         follow_mount(&nd->path);
1039         nd->inode = nd->path.dentry->d_inode;
1040 }
1041
1042 /*
1043  * Allocate a dentry with name and parent, and perform a parent
1044  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1045  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1046  * have verified that no child exists while under i_mutex.
1047  */
1048 static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1049                                 struct qstr *name, struct nameidata *nd)
1050 {
1051         struct inode *inode = parent->d_inode;
1052         struct dentry *dentry;
1053         struct dentry *old;
1054
1055         /* Don't create child dentry for a dead directory. */
1056         if (unlikely(IS_DEADDIR(inode)))
1057                 return ERR_PTR(-ENOENT);
1058
1059         dentry = d_alloc(parent, name);
1060         if (unlikely(!dentry))
1061                 return ERR_PTR(-ENOMEM);
1062
1063         old = inode->i_op->lookup(inode, dentry, nd);
1064         if (unlikely(old)) {
1065                 dput(dentry);
1066                 dentry = old;
1067         }
1068         return dentry;
1069 }
1070
1071 /*
1072  * We already have a dentry, but require a lookup to be performed on the parent
1073  * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
1074  * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
1075  * child exists while under i_mutex.
1076  */
1077 static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
1078                                      struct nameidata *nd)
1079 {
1080         struct inode *inode = parent->d_inode;
1081         struct dentry *old;
1082
1083         /* Don't create child dentry for a dead directory. */
1084         if (unlikely(IS_DEADDIR(inode)))
1085                 return ERR_PTR(-ENOENT);
1086
1087         old = inode->i_op->lookup(inode, dentry, nd);
1088         if (unlikely(old)) {
1089                 dput(dentry);
1090                 dentry = old;
1091         }
1092         return dentry;
1093 }
1094
1095 /*
1096  *  It's more convoluted than I'd like it to be, but... it's still fairly
1097  *  small and for now I'd prefer to have fast path as straight as possible.
1098  *  It _is_ time-critical.
1099  */
1100 static int do_lookup(struct nameidata *nd, struct qstr *name,
1101                         struct path *path, struct inode **inode)
1102 {
1103         struct vfsmount *mnt = nd->path.mnt;
1104         struct dentry *dentry, *parent = nd->path.dentry;
1105         int need_reval = 1;
1106         int status = 1;
1107         int err;
1108
1109         /*
1110          * Rename seqlock is not required here because in the off chance
1111          * of a false negative due to a concurrent rename, we're going to
1112          * do the non-racy lookup, below.
1113          */
1114         if (nd->flags & LOOKUP_RCU) {
1115                 unsigned seq;
1116                 *inode = nd->inode;
1117                 dentry = __d_lookup_rcu(parent, name, &seq, inode);
1118                 if (!dentry)
1119                         goto unlazy;
1120
1121                 /* Memory barrier in read_seqcount_begin of child is enough */
1122                 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1123                         return -ECHILD;
1124                 nd->seq = seq;
1125
1126                 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1127                         status = d_revalidate(dentry, nd);
1128                         if (unlikely(status <= 0)) {
1129                                 if (status != -ECHILD)
1130                                         need_reval = 0;
1131                                 goto unlazy;
1132                         }
1133                 }
1134                 if (unlikely(d_need_lookup(dentry)))
1135                         goto unlazy;
1136                 path->mnt = mnt;
1137                 path->dentry = dentry;
1138                 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1139                         goto unlazy;
1140                 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1141                         goto unlazy;
1142                 return 0;
1143 unlazy:
1144                 if (unlazy_walk(nd, dentry))
1145                         return -ECHILD;
1146         } else {
1147                 dentry = __d_lookup(parent, name);
1148         }
1149
1150         if (dentry && unlikely(d_need_lookup(dentry))) {
1151                 dput(dentry);
1152                 dentry = NULL;
1153         }
1154 retry:
1155         if (unlikely(!dentry)) {
1156                 struct inode *dir = parent->d_inode;
1157                 BUG_ON(nd->inode != dir);
1158
1159                 mutex_lock(&dir->i_mutex);
1160                 dentry = d_lookup(parent, name);
1161                 if (likely(!dentry)) {
1162                         dentry = d_alloc_and_lookup(parent, name, nd);
1163                         if (IS_ERR(dentry)) {
1164                                 mutex_unlock(&dir->i_mutex);
1165                                 return PTR_ERR(dentry);
1166                         }
1167                         /* known good */
1168                         need_reval = 0;
1169                         status = 1;
1170                 } else if (unlikely(d_need_lookup(dentry))) {
1171                         dentry = d_inode_lookup(parent, dentry, nd);
1172                         if (IS_ERR(dentry)) {
1173                                 mutex_unlock(&dir->i_mutex);
1174                                 return PTR_ERR(dentry);
1175                         }
1176                         /* known good */
1177                         need_reval = 0;
1178                         status = 1;
1179                 }
1180                 mutex_unlock(&dir->i_mutex);
1181         }
1182         if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1183                 status = d_revalidate(dentry, nd);
1184         if (unlikely(status <= 0)) {
1185                 if (status < 0) {
1186                         dput(dentry);
1187                         return status;
1188                 }
1189                 if (!d_invalidate(dentry)) {
1190                         dput(dentry);
1191                         dentry = NULL;
1192                         need_reval = 1;
1193                         goto retry;
1194                 }
1195         }
1196
1197         path->mnt = mnt;
1198         path->dentry = dentry;
1199         err = follow_managed(path, nd->flags);
1200         if (unlikely(err < 0)) {
1201                 path_put_conditional(path, nd);
1202                 return err;
1203         }
1204         *inode = path->dentry->d_inode;
1205         return 0;
1206 }
1207
1208 static inline int may_lookup(struct nameidata *nd)
1209 {
1210         if (nd->flags & LOOKUP_RCU) {
1211                 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1212                 if (err != -ECHILD)
1213                         return err;
1214                 if (unlazy_walk(nd, NULL))
1215                         return -ECHILD;
1216         }
1217         return inode_permission(nd->inode, MAY_EXEC);
1218 }
1219
1220 static inline int handle_dots(struct nameidata *nd, int type)
1221 {
1222         if (type == LAST_DOTDOT) {
1223                 if (nd->flags & LOOKUP_RCU) {
1224                         if (follow_dotdot_rcu(nd))
1225                                 return -ECHILD;
1226                 } else
1227                         follow_dotdot(nd);
1228         }
1229         return 0;
1230 }
1231
1232 static void terminate_walk(struct nameidata *nd)
1233 {
1234         if (!(nd->flags & LOOKUP_RCU)) {
1235                 path_put(&nd->path);
1236         } else {
1237                 nd->flags &= ~LOOKUP_RCU;
1238                 if (!(nd->flags & LOOKUP_ROOT))
1239                         nd->root.mnt = NULL;
1240                 rcu_read_unlock();
1241                 br_read_unlock(vfsmount_lock);
1242         }
1243 }
1244
1245 static inline int walk_component(struct nameidata *nd, struct path *path,
1246                 struct qstr *name, int type, int follow)
1247 {
1248         struct inode *inode;
1249         int err;
1250         /*
1251          * "." and ".." are special - ".." especially so because it has
1252          * to be able to know about the current root directory and
1253          * parent relationships.
1254          */
1255         if (unlikely(type != LAST_NORM))
1256                 return handle_dots(nd, type);
1257         err = do_lookup(nd, name, path, &inode);
1258         if (unlikely(err)) {
1259                 terminate_walk(nd);
1260                 return err;
1261         }
1262         if (!inode) {
1263                 path_to_nameidata(path, nd);
1264                 terminate_walk(nd);
1265                 return -ENOENT;
1266         }
1267         if (unlikely(inode->i_op->follow_link) && follow) {
1268                 if (nd->flags & LOOKUP_RCU) {
1269                         if (unlikely(unlazy_walk(nd, path->dentry))) {
1270                                 terminate_walk(nd);
1271                                 return -ECHILD;
1272                         }
1273                 }
1274                 BUG_ON(inode != path->dentry->d_inode);
1275                 return 1;
1276         }
1277         path_to_nameidata(path, nd);
1278         nd->inode = inode;
1279         return 0;
1280 }
1281
1282 /*
1283  * This limits recursive symlink follows to 8, while
1284  * limiting consecutive symlinks to 40.
1285  *
1286  * Without that kind of total limit, nasty chains of consecutive
1287  * symlinks can cause almost arbitrarily long lookups.
1288  */
1289 static inline int nested_symlink(struct path *path, struct nameidata *nd)
1290 {
1291         int res;
1292
1293         if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1294                 path_put_conditional(path, nd);
1295                 path_put(&nd->path);
1296                 return -ELOOP;
1297         }
1298         BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1299
1300         nd->depth++;
1301         current->link_count++;
1302
1303         do {
1304                 struct path link = *path;
1305                 void *cookie;
1306
1307                 res = follow_link(&link, nd, &cookie);
1308                 if (!res)
1309                         res = walk_component(nd, path, &nd->last,
1310                                              nd->last_type, LOOKUP_FOLLOW);
1311                 put_link(nd, &link, cookie);
1312         } while (res > 0);
1313
1314         current->link_count--;
1315         nd->depth--;
1316         return res;
1317 }
1318
1319 /*
1320  * Name resolution.
1321  * This is the basic name resolution function, turning a pathname into
1322  * the final dentry. We expect 'base' to be positive and a directory.
1323  *
1324  * Returns 0 and nd will have valid dentry and mnt on success.
1325  * Returns error and drops reference to input namei data on failure.
1326  */
1327 static int link_path_walk(const char *name, struct nameidata *nd)
1328 {
1329         struct path next;
1330         int err;
1331         
1332         while (*name=='/')
1333                 name++;
1334         if (!*name)
1335                 return 0;
1336
1337         /* At this point we know we have a real path component. */
1338         for(;;) {
1339                 unsigned long hash;
1340                 struct qstr this;
1341                 unsigned int c;
1342                 int type;
1343
1344                 err = may_lookup(nd);
1345                 if (err)
1346                         break;
1347
1348                 this.name = name;
1349                 c = *(const unsigned char *)name;
1350
1351                 hash = init_name_hash();
1352                 do {
1353                         name++;
1354                         hash = partial_name_hash(c, hash);
1355                         c = *(const unsigned char *)name;
1356                 } while (c && (c != '/'));
1357                 this.len = name - (const char *) this.name;
1358                 this.hash = end_name_hash(hash);
1359
1360                 type = LAST_NORM;
1361                 if (this.name[0] == '.') switch (this.len) {
1362                         case 2:
1363                                 if (this.name[1] == '.') {
1364                                         type = LAST_DOTDOT;
1365                                         nd->flags |= LOOKUP_JUMPED;
1366                                 }
1367                                 break;
1368                         case 1:
1369                                 type = LAST_DOT;
1370                 }
1371                 if (likely(type == LAST_NORM)) {
1372                         struct dentry *parent = nd->path.dentry;
1373                         nd->flags &= ~LOOKUP_JUMPED;
1374                         if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1375                                 err = parent->d_op->d_hash(parent, nd->inode,
1376                                                            &this);
1377                                 if (err < 0)
1378                                         break;
1379                         }
1380                 }
1381
1382                 /* remove trailing slashes? */
1383                 if (!c)
1384                         goto last_component;
1385                 while (*++name == '/');
1386                 if (!*name)
1387                         goto last_component;
1388
1389                 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1390                 if (err < 0)
1391                         return err;
1392
1393                 if (err) {
1394                         err = nested_symlink(&next, nd);
1395                         if (err)
1396                                 return err;
1397                 }
1398                 err = -ENOTDIR; 
1399                 if (!nd->inode->i_op->lookup)
1400                         break;
1401                 continue;
1402                 /* here ends the main loop */
1403
1404 last_component:
1405                 nd->last = this;
1406                 nd->last_type = type;
1407                 return 0;
1408         }
1409         terminate_walk(nd);
1410         return err;
1411 }
1412
1413 static int path_init(int dfd, const char *name, unsigned int flags,
1414                      struct nameidata *nd, struct file **fp)
1415 {
1416         int retval = 0;
1417         int fput_needed;
1418         struct file *file;
1419
1420         nd->last_type = LAST_ROOT; /* if there are only slashes... */
1421         nd->flags = flags | LOOKUP_JUMPED;
1422         nd->depth = 0;
1423         if (flags & LOOKUP_ROOT) {
1424                 struct inode *inode = nd->root.dentry->d_inode;
1425                 if (*name) {
1426                         if (!inode->i_op->lookup)
1427                                 return -ENOTDIR;
1428                         retval = inode_permission(inode, MAY_EXEC);
1429                         if (retval)
1430                                 return retval;
1431                 }
1432                 nd->path = nd->root;
1433                 nd->inode = inode;
1434                 if (flags & LOOKUP_RCU) {
1435                         br_read_lock(vfsmount_lock);
1436                         rcu_read_lock();
1437                         nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1438                 } else {
1439                         path_get(&nd->path);
1440                 }
1441                 return 0;
1442         }
1443
1444         nd->root.mnt = NULL;
1445
1446         if (*name=='/') {
1447                 if (flags & LOOKUP_RCU) {
1448                         br_read_lock(vfsmount_lock);
1449                         rcu_read_lock();
1450                         set_root_rcu(nd);
1451                 } else {
1452                         set_root(nd);
1453                         path_get(&nd->root);
1454                 }
1455                 nd->path = nd->root;
1456         } else if (dfd == AT_FDCWD) {
1457                 if (flags & LOOKUP_RCU) {
1458                         struct fs_struct *fs = current->fs;
1459                         unsigned seq;
1460
1461                         br_read_lock(vfsmount_lock);
1462                         rcu_read_lock();
1463
1464                         do {
1465                                 seq = read_seqcount_begin(&fs->seq);
1466                                 nd->path = fs->pwd;
1467                                 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1468                         } while (read_seqcount_retry(&fs->seq, seq));
1469                 } else {
1470                         get_fs_pwd(current->fs, &nd->path);
1471                 }
1472         } else {
1473                 struct dentry *dentry;
1474
1475                 file = fget_raw_light(dfd, &fput_needed);
1476                 retval = -EBADF;
1477                 if (!file)
1478                         goto out_fail;
1479
1480                 dentry = file->f_path.dentry;
1481
1482                 if (*name) {
1483                         retval = -ENOTDIR;
1484                         if (!S_ISDIR(dentry->d_inode->i_mode))
1485                                 goto fput_fail;
1486
1487                         retval = inode_permission(dentry->d_inode, MAY_EXEC);
1488                         if (retval)
1489                                 goto fput_fail;
1490                 }
1491
1492                 nd->path = file->f_path;
1493                 if (flags & LOOKUP_RCU) {
1494                         if (fput_needed)
1495                                 *fp = file;
1496                         nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1497                         br_read_lock(vfsmount_lock);
1498                         rcu_read_lock();
1499                 } else {
1500                         path_get(&file->f_path);
1501                         fput_light(file, fput_needed);
1502                 }
1503         }
1504
1505         nd->inode = nd->path.dentry->d_inode;
1506         return 0;
1507
1508 fput_fail:
1509         fput_light(file, fput_needed);
1510 out_fail:
1511         return retval;
1512 }
1513
1514 static inline int lookup_last(struct nameidata *nd, struct path *path)
1515 {
1516         if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1517                 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1518
1519         nd->flags &= ~LOOKUP_PARENT;
1520         return walk_component(nd, path, &nd->last, nd->last_type,
1521                                         nd->flags & LOOKUP_FOLLOW);
1522 }
1523
1524 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1525 static int path_lookupat(int dfd, const char *name,
1526                                 unsigned int flags, struct nameidata *nd)
1527 {
1528         struct file *base = NULL;
1529         struct path path;
1530         int err;
1531
1532         /*
1533          * Path walking is largely split up into 2 different synchronisation
1534          * schemes, rcu-walk and ref-walk (explained in
1535          * Documentation/filesystems/path-lookup.txt). These share much of the
1536          * path walk code, but some things particularly setup, cleanup, and
1537          * following mounts are sufficiently divergent that functions are
1538          * duplicated. Typically there is a function foo(), and its RCU
1539          * analogue, foo_rcu().
1540          *
1541          * -ECHILD is the error number of choice (just to avoid clashes) that
1542          * is returned if some aspect of an rcu-walk fails. Such an error must
1543          * be handled by restarting a traditional ref-walk (which will always
1544          * be able to complete).
1545          */
1546         err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1547
1548         if (unlikely(err))
1549                 return err;
1550
1551         current->total_link_count = 0;
1552         err = link_path_walk(name, nd);
1553
1554         if (!err && !(flags & LOOKUP_PARENT)) {
1555                 err = lookup_last(nd, &path);
1556                 while (err > 0) {
1557                         void *cookie;
1558                         struct path link = path;
1559                         nd->flags |= LOOKUP_PARENT;
1560                         err = follow_link(&link, nd, &cookie);
1561                         if (!err)
1562                                 err = lookup_last(nd, &path);
1563                         put_link(nd, &link, cookie);
1564                 }
1565         }
1566
1567         if (!err)
1568                 err = complete_walk(nd);
1569
1570         if (!err && nd->flags & LOOKUP_DIRECTORY) {
1571                 if (!nd->inode->i_op->lookup) {
1572                         path_put(&nd->path);
1573                         err = -ENOTDIR;
1574                 }
1575         }
1576
1577         if (base)
1578                 fput(base);
1579
1580         if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1581                 path_put(&nd->root);
1582                 nd->root.mnt = NULL;
1583         }
1584         return err;
1585 }
1586
1587 static int do_path_lookup(int dfd, const char *name,
1588                                 unsigned int flags, struct nameidata *nd)
1589 {
1590         int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1591         if (unlikely(retval == -ECHILD))
1592                 retval = path_lookupat(dfd, name, flags, nd);
1593         if (unlikely(retval == -ESTALE))
1594                 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1595
1596         if (likely(!retval)) {
1597                 if (unlikely(!audit_dummy_context())) {
1598                         if (nd->path.dentry && nd->inode)
1599                                 audit_inode(name, nd->path.dentry);
1600                 }
1601         }
1602         return retval;
1603 }
1604
1605 int kern_path_parent(const char *name, struct nameidata *nd)
1606 {
1607         return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
1608 }
1609
1610 int kern_path(const char *name, unsigned int flags, struct path *path)
1611 {
1612         struct nameidata nd;
1613         int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1614         if (!res)
1615                 *path = nd.path;
1616         return res;
1617 }
1618
1619 /**
1620  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1621  * @dentry:  pointer to dentry of the base directory
1622  * @mnt: pointer to vfs mount of the base directory
1623  * @name: pointer to file name
1624  * @flags: lookup flags
1625  * @path: pointer to struct path to fill
1626  */
1627 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1628                     const char *name, unsigned int flags,
1629                     struct path *path)
1630 {
1631         struct nameidata nd;
1632         int err;
1633         nd.root.dentry = dentry;
1634         nd.root.mnt = mnt;
1635         BUG_ON(flags & LOOKUP_PARENT);
1636         /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1637         err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1638         if (!err)
1639                 *path = nd.path;
1640         return err;
1641 }
1642
1643 static struct dentry *__lookup_hash(struct qstr *name,
1644                 struct dentry *base, struct nameidata *nd)
1645 {
1646         struct inode *inode = base->d_inode;
1647         struct dentry *dentry;
1648         int err;
1649
1650         err = inode_permission(inode, MAY_EXEC);
1651         if (err)
1652                 return ERR_PTR(err);
1653
1654         /*
1655          * Don't bother with __d_lookup: callers are for creat as
1656          * well as unlink, so a lot of the time it would cost
1657          * a double lookup.
1658          */
1659         dentry = d_lookup(base, name);
1660
1661         if (dentry && d_need_lookup(dentry)) {
1662                 /*
1663                  * __lookup_hash is called with the parent dir's i_mutex already
1664                  * held, so we are good to go here.
1665                  */
1666                 dentry = d_inode_lookup(base, dentry, nd);
1667                 if (IS_ERR(dentry))
1668                         return dentry;
1669         }
1670
1671         if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1672                 int status = d_revalidate(dentry, nd);
1673                 if (unlikely(status <= 0)) {
1674                         /*
1675                          * The dentry failed validation.
1676                          * If d_revalidate returned 0 attempt to invalidate
1677                          * the dentry otherwise d_revalidate is asking us
1678                          * to return a fail status.
1679                          */
1680                         if (status < 0) {
1681                                 dput(dentry);
1682                                 return ERR_PTR(status);
1683                         } else if (!d_invalidate(dentry)) {
1684                                 dput(dentry);
1685                                 dentry = NULL;
1686                         }
1687                 }
1688         }
1689
1690         if (!dentry)
1691                 dentry = d_alloc_and_lookup(base, name, nd);
1692
1693         return dentry;
1694 }
1695
1696 /*
1697  * Restricted form of lookup. Doesn't follow links, single-component only,
1698  * needs parent already locked. Doesn't follow mounts.
1699  * SMP-safe.
1700  */
1701 static struct dentry *lookup_hash(struct nameidata *nd)
1702 {
1703         return __lookup_hash(&nd->last, nd->path.dentry, nd);
1704 }
1705
1706 /**
1707  * lookup_one_len - filesystem helper to lookup single pathname component
1708  * @name:       pathname component to lookup
1709  * @base:       base directory to lookup from
1710  * @len:        maximum length @len should be interpreted to
1711  *
1712  * Note that this routine is purely a helper for filesystem usage and should
1713  * not be called by generic code.  Also note that by using this function the
1714  * nameidata argument is passed to the filesystem methods and a filesystem
1715  * using this helper needs to be prepared for that.
1716  */
1717 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1718 {
1719         struct qstr this;
1720         unsigned long hash;
1721         unsigned int c;
1722
1723         WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1724
1725         this.name = name;
1726         this.len = len;
1727         if (!len)
1728                 return ERR_PTR(-EACCES);
1729
1730         hash = init_name_hash();
1731         while (len--) {
1732                 c = *(const unsigned char *)name++;
1733                 if (c == '/' || c == '\0')
1734                         return ERR_PTR(-EACCES);
1735                 hash = partial_name_hash(c, hash);
1736         }
1737         this.hash = end_name_hash(hash);
1738         /*
1739          * See if the low-level filesystem might want
1740          * to use its own hash..
1741          */
1742         if (base->d_flags & DCACHE_OP_HASH) {
1743                 int err = base->d_op->d_hash(base, base->d_inode, &this);
1744                 if (err < 0)
1745                         return ERR_PTR(err);
1746         }
1747
1748         return __lookup_hash(&this, base, NULL);
1749 }
1750
1751 int user_path_at(int dfd, const char __user *name, unsigned flags,
1752                  struct path *path)
1753 {
1754         struct nameidata nd;
1755         char *tmp = getname_flags(name, flags);
1756         int err = PTR_ERR(tmp);
1757         if (!IS_ERR(tmp)) {
1758
1759                 BUG_ON(flags & LOOKUP_PARENT);
1760
1761                 err = do_path_lookup(dfd, tmp, flags, &nd);
1762                 putname(tmp);
1763                 if (!err)
1764                         *path = nd.path;
1765         }
1766         return err;
1767 }
1768
1769 static int user_path_parent(int dfd, const char __user *path,
1770                         struct nameidata *nd, char **name)
1771 {
1772         char *s = getname(path);
1773         int error;
1774
1775         if (IS_ERR(s))
1776                 return PTR_ERR(s);
1777
1778         error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1779         if (error)
1780                 putname(s);
1781         else
1782                 *name = s;
1783
1784         return error;
1785 }
1786
1787 /*
1788  * It's inline, so penalty for filesystems that don't use sticky bit is
1789  * minimal.
1790  */
1791 static inline int check_sticky(struct inode *dir, struct inode *inode)
1792 {
1793         uid_t fsuid = current_fsuid();
1794
1795         if (!(dir->i_mode & S_ISVTX))
1796                 return 0;
1797         if (current_user_ns() != inode_userns(inode))
1798                 goto other_userns;
1799         if (inode->i_uid == fsuid)
1800                 return 0;
1801         if (dir->i_uid == fsuid)
1802                 return 0;
1803
1804 other_userns:
1805         return !ns_capable(inode_userns(inode), CAP_FOWNER);
1806 }
1807
1808 /*
1809  *      Check whether we can remove a link victim from directory dir, check
1810  *  whether the type of victim is right.
1811  *  1. We can't do it if dir is read-only (done in permission())
1812  *  2. We should have write and exec permissions on dir
1813  *  3. We can't remove anything from append-only dir
1814  *  4. We can't do anything with immutable dir (done in permission())
1815  *  5. If the sticky bit on dir is set we should either
1816  *      a. be owner of dir, or
1817  *      b. be owner of victim, or
1818  *      c. have CAP_FOWNER capability
1819  *  6. If the victim is append-only or immutable we can't do antyhing with
1820  *     links pointing to it.
1821  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1822  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1823  *  9. We can't remove a root or mountpoint.
1824  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1825  *     nfs_async_unlink().
1826  */
1827 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1828 {
1829         int error;
1830
1831         if (!victim->d_inode)
1832                 return -ENOENT;
1833
1834         BUG_ON(victim->d_parent->d_inode != dir);
1835         audit_inode_child(victim, dir);
1836
1837         error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1838         if (error)
1839                 return error;
1840         if (IS_APPEND(dir))
1841                 return -EPERM;
1842         if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1843             IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1844                 return -EPERM;
1845         if (isdir) {
1846                 if (!S_ISDIR(victim->d_inode->i_mode))
1847                         return -ENOTDIR;
1848                 if (IS_ROOT(victim))
1849                         return -EBUSY;
1850         } else if (S_ISDIR(victim->d_inode->i_mode))
1851                 return -EISDIR;
1852         if (IS_DEADDIR(dir))
1853                 return -ENOENT;
1854         if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1855                 return -EBUSY;
1856         return 0;
1857 }
1858
1859 /*      Check whether we can create an object with dentry child in directory
1860  *  dir.
1861  *  1. We can't do it if child already exists (open has special treatment for
1862  *     this case, but since we are inlined it's OK)
1863  *  2. We can't do it if dir is read-only (done in permission())
1864  *  3. We should have write and exec permissions on dir
1865  *  4. We can't do it if dir is immutable (done in permission())
1866  */
1867 static inline int may_create(struct inode *dir, struct dentry *child)
1868 {
1869         if (child->d_inode)
1870                 return -EEXIST;
1871         if (IS_DEADDIR(dir))
1872                 return -ENOENT;
1873         return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1874 }
1875
1876 /*
1877  * p1 and p2 should be directories on the same fs.
1878  */
1879 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1880 {
1881         struct dentry *p;
1882
1883         if (p1 == p2) {
1884                 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1885                 return NULL;
1886         }
1887
1888         mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1889
1890         p = d_ancestor(p2, p1);
1891         if (p) {
1892                 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1893                 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1894                 return p;
1895         }
1896
1897         p = d_ancestor(p1, p2);
1898         if (p) {
1899                 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1900                 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1901                 return p;
1902         }
1903
1904         mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1905         mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1906         return NULL;
1907 }
1908
1909 void unlock_rename(struct dentry *p1, struct dentry *p2)
1910 {
1911         mutex_unlock(&p1->d_inode->i_mutex);
1912         if (p1 != p2) {
1913                 mutex_unlock(&p2->d_inode->i_mutex);
1914                 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1915         }
1916 }
1917
1918 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1919                 struct nameidata *nd)
1920 {
1921         int error = may_create(dir, dentry);
1922
1923         if (error)
1924                 return error;
1925
1926         if (!dir->i_op->create)
1927                 return -EACCES; /* shouldn't it be ENOSYS? */
1928         mode &= S_IALLUGO;
1929         mode |= S_IFREG;
1930         error = security_inode_create(dir, dentry, mode);
1931         if (error)
1932                 return error;
1933         error = dir->i_op->create(dir, dentry, mode, nd);
1934         if (!error)
1935                 fsnotify_create(dir, dentry);
1936         return error;
1937 }
1938
1939 static int may_open(struct path *path, int acc_mode, int flag)
1940 {
1941         struct dentry *dentry = path->dentry;
1942         struct inode *inode = dentry->d_inode;
1943         int error;
1944
1945         /* O_PATH? */
1946         if (!acc_mode)
1947                 return 0;
1948
1949         if (!inode)
1950                 return -ENOENT;
1951
1952         switch (inode->i_mode & S_IFMT) {
1953         case S_IFLNK:
1954                 return -ELOOP;
1955         case S_IFDIR:
1956                 if (acc_mode & MAY_WRITE)
1957                         return -EISDIR;
1958                 break;
1959         case S_IFBLK:
1960         case S_IFCHR:
1961                 if (path->mnt->mnt_flags & MNT_NODEV)
1962                         return -EACCES;
1963                 /*FALLTHRU*/
1964         case S_IFIFO:
1965         case S_IFSOCK:
1966                 flag &= ~O_TRUNC;
1967                 break;
1968         }
1969
1970         error = inode_permission(inode, acc_mode);
1971         if (error)
1972                 return error;
1973
1974         /*
1975          * An append-only file must be opened in append mode for writing.
1976          */
1977         if (IS_APPEND(inode)) {
1978                 if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
1979                         return -EPERM;
1980                 if (flag & O_TRUNC)
1981                         return -EPERM;
1982         }
1983
1984         /* O_NOATIME can only be set by the owner or superuser */
1985         if (flag & O_NOATIME && !inode_owner_or_capable(inode))
1986                 return -EPERM;
1987
1988         /*
1989          * Ensure there are no outstanding leases on the file.
1990          */
1991         return break_lease(inode, flag);
1992 }
1993
1994 static int handle_truncate(struct file *filp)
1995 {
1996         struct path *path = &filp->f_path;
1997         struct inode *inode = path->dentry->d_inode;
1998         int error = get_write_access(inode);
1999         if (error)
2000                 return error;
2001         /*
2002          * Refuse to truncate files with mandatory locks held on them.
2003          */
2004         error = locks_verify_locked(inode);
2005         if (!error)
2006                 error = security_path_truncate(path);
2007         if (!error) {
2008                 error = do_truncate(path->dentry, 0,
2009                                     ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2010                                     filp);
2011         }
2012         put_write_access(inode);
2013         return error;
2014 }
2015
2016 static inline int open_to_namei_flags(int flag)
2017 {
2018         if ((flag & O_ACCMODE) == 3)
2019                 flag--;
2020         return flag;
2021 }
2022
2023 /*
2024  * Handle the last step of open()
2025  */
2026 static struct file *do_last(struct nameidata *nd, struct path *path,
2027                             const struct open_flags *op, const char *pathname)
2028 {
2029         struct dentry *dir = nd->path.dentry;
2030         struct dentry *dentry;
2031         int open_flag = op->open_flag;
2032         int will_truncate = open_flag & O_TRUNC;
2033         int want_write = 0;
2034         int acc_mode = op->acc_mode;
2035         struct file *filp;
2036         int error;
2037
2038         nd->flags &= ~LOOKUP_PARENT;
2039         nd->flags |= op->intent;
2040
2041         switch (nd->last_type) {
2042         case LAST_DOTDOT:
2043         case LAST_DOT:
2044                 error = handle_dots(nd, nd->last_type);
2045                 if (error)
2046                         return ERR_PTR(error);
2047                 /* fallthrough */
2048         case LAST_ROOT:
2049                 error = complete_walk(nd);
2050                 if (error)
2051                         return ERR_PTR(error);
2052                 audit_inode(pathname, nd->path.dentry);
2053                 if (open_flag & O_CREAT) {
2054                         error = -EISDIR;
2055                         goto exit;
2056                 }
2057                 goto ok;
2058         case LAST_BIND:
2059                 error = complete_walk(nd);
2060                 if (error)
2061                         return ERR_PTR(error);
2062                 audit_inode(pathname, dir);
2063                 goto ok;
2064         }
2065
2066         if (!(open_flag & O_CREAT)) {
2067                 int symlink_ok = 0;
2068                 if (nd->last.name[nd->last.len])
2069                         nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2070                 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2071                         symlink_ok = 1;
2072                 /* we _can_ be in RCU mode here */
2073                 error = walk_component(nd, path, &nd->last, LAST_NORM,
2074                                         !symlink_ok);
2075                 if (error < 0)
2076                         return ERR_PTR(error);
2077                 if (error) /* symlink */
2078                         return NULL;
2079                 /* sayonara */
2080                 error = complete_walk(nd);
2081                 if (error)
2082                         return ERR_PTR(-ECHILD);
2083
2084                 error = -ENOTDIR;
2085                 if (nd->flags & LOOKUP_DIRECTORY) {
2086                         if (!nd->inode->i_op->lookup)
2087                                 goto exit;
2088                 }
2089                 audit_inode(pathname, nd->path.dentry);
2090                 goto ok;
2091         }
2092
2093         /* create side of things */
2094         error = complete_walk(nd);
2095         if (error)
2096                 return ERR_PTR(error);
2097
2098         audit_inode(pathname, dir);
2099         error = -EISDIR;
2100         /* trailing slashes? */
2101         if (nd->last.name[nd->last.len])
2102                 goto exit;
2103
2104         mutex_lock(&dir->d_inode->i_mutex);
2105
2106         dentry = lookup_hash(nd);
2107         error = PTR_ERR(dentry);
2108         if (IS_ERR(dentry)) {
2109                 mutex_unlock(&dir->d_inode->i_mutex);
2110                 goto exit;
2111         }
2112
2113         path->dentry = dentry;
2114         path->mnt = nd->path.mnt;
2115
2116         /* Negative dentry, just create the file */
2117         if (!dentry->d_inode) {
2118                 int mode = op->mode;
2119                 if (!IS_POSIXACL(dir->d_inode))
2120                         mode &= ~current_umask();
2121                 /*
2122                  * This write is needed to ensure that a
2123                  * rw->ro transition does not occur between
2124                  * the time when the file is created and when
2125                  * a permanent write count is taken through
2126                  * the 'struct file' in nameidata_to_filp().
2127                  */
2128                 error = mnt_want_write(nd->path.mnt);
2129                 if (error)
2130                         goto exit_mutex_unlock;
2131                 want_write = 1;
2132                 /* Don't check for write permission, don't truncate */
2133                 open_flag &= ~O_TRUNC;
2134                 will_truncate = 0;
2135                 acc_mode = MAY_OPEN;
2136                 error = security_path_mknod(&nd->path, dentry, mode, 0);
2137                 if (error)
2138                         goto exit_mutex_unlock;
2139                 error = vfs_create(dir->d_inode, dentry, mode, nd);
2140                 if (error)
2141                         goto exit_mutex_unlock;
2142                 mutex_unlock(&dir->d_inode->i_mutex);
2143                 dput(nd->path.dentry);
2144                 nd->path.dentry = dentry;
2145                 goto common;
2146         }
2147
2148         /*
2149          * It already exists.
2150          */
2151         mutex_unlock(&dir->d_inode->i_mutex);
2152         audit_inode(pathname, path->dentry);
2153
2154         error = -EEXIST;
2155         if (open_flag & O_EXCL)
2156                 goto exit_dput;
2157
2158         error = follow_managed(path, nd->flags);
2159         if (error < 0)
2160                 goto exit_dput;
2161
2162         error = -ENOENT;
2163         if (!path->dentry->d_inode)
2164                 goto exit_dput;
2165
2166         if (path->dentry->d_inode->i_op->follow_link)
2167                 return NULL;
2168
2169         path_to_nameidata(path, nd);
2170         nd->inode = path->dentry->d_inode;
2171         error = -EISDIR;
2172         if (S_ISDIR(nd->inode->i_mode))
2173                 goto exit;
2174 ok:
2175         if (!S_ISREG(nd->inode->i_mode))
2176                 will_truncate = 0;
2177
2178         if (will_truncate) {
2179                 error = mnt_want_write(nd->path.mnt);
2180                 if (error)
2181                         goto exit;
2182                 want_write = 1;
2183         }
2184 common:
2185         error = may_open(&nd->path, acc_mode, open_flag);
2186         if (error)
2187                 goto exit;
2188         filp = nameidata_to_filp(nd);
2189         if (!IS_ERR(filp)) {
2190                 error = ima_file_check(filp, op->acc_mode);
2191                 if (error) {
2192                         fput(filp);
2193                         filp = ERR_PTR(error);
2194                 }
2195         }
2196         if (!IS_ERR(filp)) {
2197                 if (will_truncate) {
2198                         error = handle_truncate(filp);
2199                         if (error) {
2200                                 fput(filp);
2201                                 filp = ERR_PTR(error);
2202                         }
2203                 }
2204         }
2205 out:
2206         if (want_write)
2207                 mnt_drop_write(nd->path.mnt);
2208         path_put(&nd->path);
2209         return filp;
2210
2211 exit_mutex_unlock:
2212         mutex_unlock(&dir->d_inode->i_mutex);
2213 exit_dput:
2214         path_put_conditional(path, nd);
2215 exit:
2216         filp = ERR_PTR(error);
2217         goto out;
2218 }
2219
2220 static struct file *path_openat(int dfd, const char *pathname,
2221                 struct nameidata *nd, const struct open_flags *op, int flags)
2222 {
2223         struct file *base = NULL;
2224         struct file *filp;
2225         struct path path;
2226         int error;
2227
2228         filp = get_empty_filp();
2229         if (!filp)
2230                 return ERR_PTR(-ENFILE);
2231
2232         filp->f_flags = op->open_flag;
2233         nd->intent.open.file = filp;
2234         nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2235         nd->intent.open.create_mode = op->mode;
2236
2237         error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
2238         if (unlikely(error))
2239                 goto out_filp;
2240
2241         current->total_link_count = 0;
2242         error = link_path_walk(pathname, nd);
2243         if (unlikely(error))
2244                 goto out_filp;
2245
2246         filp = do_last(nd, &path, op, pathname);
2247         while (unlikely(!filp)) { /* trailing symlink */
2248                 struct path link = path;
2249                 void *cookie;
2250                 if (!(nd->flags & LOOKUP_FOLLOW)) {
2251                         path_put_conditional(&path, nd);
2252                         path_put(&nd->path);
2253                         filp = ERR_PTR(-ELOOP);
2254                         break;
2255                 }
2256                 nd->flags |= LOOKUP_PARENT;
2257                 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2258                 error = follow_link(&link, nd, &cookie);
2259                 if (unlikely(error))
2260                         filp = ERR_PTR(error);
2261                 else
2262                         filp = do_last(nd, &path, op, pathname);
2263                 put_link(nd, &link, cookie);
2264         }
2265 out:
2266         if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2267                 path_put(&nd->root);
2268         if (base)
2269                 fput(base);
2270         release_open_intent(nd);
2271         return filp;
2272
2273 out_filp:
2274         filp = ERR_PTR(error);
2275         goto out;
2276 }
2277
2278 struct file *do_filp_open(int dfd, const char *pathname,
2279                 const struct open_flags *op, int flags)
2280 {
2281         struct nameidata nd;
2282         struct file *filp;
2283
2284         filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
2285         if (unlikely(filp == ERR_PTR(-ECHILD)))
2286                 filp = path_openat(dfd, pathname, &nd, op, flags);
2287         if (unlikely(filp == ERR_PTR(-ESTALE)))
2288                 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
2289         return filp;
2290 }
2291
2292 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2293                 const char *name, const struct open_flags *op, int flags)
2294 {
2295         struct nameidata nd;
2296         struct file *file;
2297
2298         nd.root.mnt = mnt;
2299         nd.root.dentry = dentry;
2300
2301         flags |= LOOKUP_ROOT;
2302
2303         if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
2304                 return ERR_PTR(-ELOOP);
2305
2306         file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2307         if (unlikely(file == ERR_PTR(-ECHILD)))
2308                 file = path_openat(-1, name, &nd, op, flags);
2309         if (unlikely(file == ERR_PTR(-ESTALE)))
2310                 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2311         return file;
2312 }
2313
2314 struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
2315 {
2316         struct dentry *dentry = ERR_PTR(-EEXIST);
2317         struct nameidata nd;
2318         int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2319         if (error)
2320                 return ERR_PTR(error);
2321
2322         /*
2323          * Yucky last component or no last component at all?
2324          * (foo/., foo/.., /////)
2325          */
2326         if (nd.last_type != LAST_NORM)
2327                 goto out;
2328         nd.flags &= ~LOOKUP_PARENT;
2329         nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2330         nd.intent.open.flags = O_EXCL;
2331
2332         /*
2333          * Do the final lookup.
2334          */
2335         mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2336         dentry = lookup_hash(&nd);
2337         if (IS_ERR(dentry))
2338                 goto fail;
2339
2340         if (dentry->d_inode)
2341                 goto eexist;
2342         /*
2343          * Special case - lookup gave negative, but... we had foo/bar/
2344          * From the vfs_mknod() POV we just have a negative dentry -
2345          * all is fine. Let's be bastards - you had / on the end, you've
2346          * been asking for (non-existent) directory. -ENOENT for you.
2347          */
2348         if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
2349                 dput(dentry);
2350                 dentry = ERR_PTR(-ENOENT);
2351                 goto fail;
2352         }
2353         *path = nd.path;
2354         return dentry;
2355 eexist:
2356         dput(dentry);
2357         dentry = ERR_PTR(-EEXIST);
2358 fail:
2359         mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2360 out:
2361         path_put(&nd.path);
2362         return dentry;
2363 }
2364 EXPORT_SYMBOL(kern_path_create);
2365
2366 struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2367 {
2368         char *tmp = getname(pathname);
2369         struct dentry *res;
2370         if (IS_ERR(tmp))
2371                 return ERR_CAST(tmp);
2372         res = kern_path_create(dfd, tmp, path, is_dir);
2373         putname(tmp);
2374         return res;
2375 }
2376 EXPORT_SYMBOL(user_path_create);
2377
2378 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2379 {
2380         int error = may_create(dir, dentry);
2381
2382         if (error)
2383                 return error;
2384
2385         if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2386             !ns_capable(inode_userns(dir), CAP_MKNOD))
2387                 return -EPERM;
2388
2389         if (!dir->i_op->mknod)
2390                 return -EPERM;
2391
2392         error = devcgroup_inode_mknod(mode, dev);
2393         if (error)
2394                 return error;
2395
2396         error = security_inode_mknod(dir, dentry, mode, dev);
2397         if (error)
2398                 return error;
2399
2400         error = dir->i_op->mknod(dir, dentry, mode, dev);
2401         if (!error)
2402                 fsnotify_create(dir, dentry);
2403         return error;
2404 }
2405
2406 static int may_mknod(mode_t mode)
2407 {
2408         switch (mode & S_IFMT) {
2409         case S_IFREG:
2410         case S_IFCHR:
2411         case S_IFBLK:
2412         case S_IFIFO:
2413         case S_IFSOCK:
2414         case 0: /* zero mode translates to S_IFREG */
2415                 return 0;
2416         case S_IFDIR:
2417                 return -EPERM;
2418         default:
2419                 return -EINVAL;
2420         }
2421 }
2422
2423 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2424                 unsigned, dev)
2425 {
2426         struct dentry *dentry;
2427         struct path path;
2428         int error;
2429
2430         if (S_ISDIR(mode))
2431                 return -EPERM;
2432
2433         dentry = user_path_create(dfd, filename, &path, 0);
2434         if (IS_ERR(dentry))
2435                 return PTR_ERR(dentry);
2436
2437         if (!IS_POSIXACL(path.dentry->d_inode))
2438                 mode &= ~current_umask();
2439         error = may_mknod(mode);
2440         if (error)
2441                 goto out_dput;
2442         error = mnt_want_write(path.mnt);
2443         if (error)
2444                 goto out_dput;
2445         error = security_path_mknod(&path, dentry, mode, dev);
2446         if (error)
2447                 goto out_drop_write;
2448         switch (mode & S_IFMT) {
2449                 case 0: case S_IFREG:
2450                         error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
2451                         break;
2452                 case S_IFCHR: case S_IFBLK:
2453                         error = vfs_mknod(path.dentry->d_inode,dentry,mode,
2454                                         new_decode_dev(dev));
2455                         break;
2456                 case S_IFIFO: case S_IFSOCK:
2457                         error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
2458                         break;
2459         }
2460 out_drop_write:
2461         mnt_drop_write(path.mnt);
2462 out_dput:
2463         dput(dentry);
2464         mutex_unlock(&path.dentry->d_inode->i_mutex);
2465         path_put(&path);
2466
2467         return error;
2468 }
2469
2470 SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
2471 {
2472         return sys_mknodat(AT_FDCWD, filename, mode, dev);
2473 }
2474
2475 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2476 {
2477         int error = may_create(dir, dentry);
2478
2479         if (error)
2480                 return error;
2481
2482         if (!dir->i_op->mkdir)
2483                 return -EPERM;
2484
2485         mode &= (S_IRWXUGO|S_ISVTX);
2486         error = security_inode_mkdir(dir, dentry, mode);
2487         if (error)
2488                 return error;
2489
2490         error = dir->i_op->mkdir(dir, dentry, mode);
2491         if (!error)
2492                 fsnotify_mkdir(dir, dentry);
2493         return error;
2494 }
2495
2496 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
2497 {
2498         struct dentry *dentry;
2499         struct path path;
2500         int error;
2501
2502         dentry = user_path_create(dfd, pathname, &path, 1);
2503         if (IS_ERR(dentry))
2504                 return PTR_ERR(dentry);
2505
2506         if (!IS_POSIXACL(path.dentry->d_inode))
2507                 mode &= ~current_umask();
2508         error = mnt_want_write(path.mnt);
2509         if (error)
2510                 goto out_dput;
2511         error = security_path_mkdir(&path, dentry, mode);
2512         if (error)
2513                 goto out_drop_write;
2514         error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
2515 out_drop_write:
2516         mnt_drop_write(path.mnt);
2517 out_dput:
2518         dput(dentry);
2519         mutex_unlock(&path.dentry->d_inode->i_mutex);
2520         path_put(&path);
2521         return error;
2522 }
2523
2524 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
2525 {
2526         return sys_mkdirat(AT_FDCWD, pathname, mode);
2527 }
2528
2529 /*
2530  * The dentry_unhash() helper will try to drop the dentry early: we
2531  * should have a usage count of 2 if we're the only user of this
2532  * dentry, and if that is true (possibly after pruning the dcache),
2533  * then we drop the dentry now.
2534  *
2535  * A low-level filesystem can, if it choses, legally
2536  * do a
2537  *
2538  *      if (!d_unhashed(dentry))
2539  *              return -EBUSY;
2540  *
2541  * if it cannot handle the case of removing a directory
2542  * that is still in use by something else..
2543  */
2544 void dentry_unhash(struct dentry *dentry)
2545 {
2546         shrink_dcache_parent(dentry);
2547         spin_lock(&dentry->d_lock);
2548         if (dentry->d_count == 1)
2549                 __d_drop(dentry);
2550         spin_unlock(&dentry->d_lock);
2551 }
2552
2553 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2554 {
2555         int error = may_delete(dir, dentry, 1);
2556
2557         if (error)
2558                 return error;
2559
2560         if (!dir->i_op->rmdir)
2561                 return -EPERM;
2562
2563         mutex_lock(&dentry->d_inode->i_mutex);
2564
2565         error = -EBUSY;
2566         if (d_mountpoint(dentry))
2567                 goto out;
2568
2569         error = security_inode_rmdir(dir, dentry);
2570         if (error)
2571                 goto out;
2572
2573         shrink_dcache_parent(dentry);
2574         error = dir->i_op->rmdir(dir, dentry);
2575         if (error)
2576                 goto out;
2577
2578         dentry->d_inode->i_flags |= S_DEAD;
2579         dont_mount(dentry);
2580
2581 out:
2582         mutex_unlock(&dentry->d_inode->i_mutex);
2583         if (!error)
2584                 d_delete(dentry);
2585         return error;
2586 }
2587
2588 static long do_rmdir(int dfd, const char __user *pathname)
2589 {
2590         int error = 0;
2591         char * name;
2592         struct dentry *dentry;
2593         struct nameidata nd;
2594
2595         error = user_path_parent(dfd, pathname, &nd, &name);
2596         if (error)
2597                 return error;
2598
2599         switch(nd.last_type) {
2600         case LAST_DOTDOT:
2601                 error = -ENOTEMPTY;
2602                 goto exit1;
2603         case LAST_DOT:
2604                 error = -EINVAL;
2605                 goto exit1;
2606         case LAST_ROOT:
2607                 error = -EBUSY;
2608                 goto exit1;
2609         }
2610
2611         nd.flags &= ~LOOKUP_PARENT;
2612
2613         mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2614         dentry = lookup_hash(&nd);
2615         error = PTR_ERR(dentry);
2616         if (IS_ERR(dentry))
2617                 goto exit2;
2618         if (!dentry->d_inode) {
2619                 error = -ENOENT;
2620                 goto exit3;
2621         }
2622         error = mnt_want_write(nd.path.mnt);
2623         if (error)
2624                 goto exit3;
2625         error = security_path_rmdir(&nd.path, dentry);
2626         if (error)
2627                 goto exit4;
2628         error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2629 exit4:
2630         mnt_drop_write(nd.path.mnt);
2631 exit3:
2632         dput(dentry);
2633 exit2:
2634         mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2635 exit1:
2636         path_put(&nd.path);
2637         putname(name);
2638         return error;
2639 }
2640
2641 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
2642 {
2643         return do_rmdir(AT_FDCWD, pathname);
2644 }
2645
2646 int vfs_unlink(struct inode *dir, struct dentry *dentry)
2647 {
2648         int error = may_delete(dir, dentry, 0);
2649
2650         if (error)
2651                 return error;
2652
2653         if (!dir->i_op->unlink)
2654                 return -EPERM;
2655
2656         mutex_lock(&dentry->d_inode->i_mutex);
2657         if (d_mountpoint(dentry))
2658                 error = -EBUSY;
2659         else {
2660                 error = security_inode_unlink(dir, dentry);
2661                 if (!error) {
2662                         error = dir->i_op->unlink(dir, dentry);
2663                         if (!error)
2664                                 dont_mount(dentry);
2665                 }
2666         }
2667         mutex_unlock(&dentry->d_inode->i_mutex);
2668
2669         /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2670         if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2671                 fsnotify_link_count(dentry->d_inode);
2672                 d_delete(dentry);
2673         }
2674
2675         return error;
2676 }
2677
2678 /*
2679  * Make sure that the actual truncation of the file will occur outside its
2680  * directory's i_mutex.  Truncate can take a long time if there is a lot of
2681  * writeout happening, and we don't want to prevent access to the directory
2682  * while waiting on the I/O.
2683  */
2684 static long do_unlinkat(int dfd, const char __user *pathname)
2685 {
2686         int error;
2687         char *name;
2688         struct dentry *dentry;
2689         struct nameidata nd;
2690         struct inode *inode = NULL;
2691
2692         error = user_path_parent(dfd, pathname, &nd, &name);
2693         if (error)
2694                 return error;
2695
2696         error = -EISDIR;
2697         if (nd.last_type != LAST_NORM)
2698                 goto exit1;
2699
2700         nd.flags &= ~LOOKUP_PARENT;
2701
2702         mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2703         dentry = lookup_hash(&nd);
2704         error = PTR_ERR(dentry);
2705         if (!IS_ERR(dentry)) {
2706                 /* Why not before? Because we want correct error value */
2707                 if (nd.last.name[nd.last.len])
2708                         goto slashes;
2709                 inode = dentry->d_inode;
2710                 if (!inode)
2711                         goto slashes;
2712                 ihold(inode);
2713                 error = mnt_want_write(nd.path.mnt);
2714                 if (error)
2715                         goto exit2;
2716                 error = security_path_unlink(&nd.path, dentry);
2717                 if (error)
2718                         goto exit3;
2719                 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2720 exit3:
2721                 mnt_drop_write(nd.path.mnt);
2722         exit2:
2723                 dput(dentry);
2724         }
2725         mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2726         if (inode)
2727                 iput(inode);    /* truncate the inode here */
2728 exit1:
2729         path_put(&nd.path);
2730         putname(name);
2731         return error;
2732
2733 slashes:
2734         error = !dentry->d_inode ? -ENOENT :
2735                 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2736         goto exit2;
2737 }
2738
2739 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
2740 {
2741         if ((flag & ~AT_REMOVEDIR) != 0)
2742                 return -EINVAL;
2743
2744         if (flag & AT_REMOVEDIR)
2745                 return do_rmdir(dfd, pathname);
2746
2747         return do_unlinkat(dfd, pathname);
2748 }
2749
2750 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
2751 {
2752         return do_unlinkat(AT_FDCWD, pathname);
2753 }
2754
2755 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
2756 {
2757         int error = may_create(dir, dentry);
2758
2759         if (error)
2760                 return error;
2761
2762         if (!dir->i_op->symlink)
2763                 return -EPERM;
2764
2765         error = security_inode_symlink(dir, dentry, oldname);
2766         if (error)
2767                 return error;
2768
2769         error = dir->i_op->symlink(dir, dentry, oldname);
2770         if (!error)
2771                 fsnotify_create(dir, dentry);
2772         return error;
2773 }
2774
2775 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2776                 int, newdfd, const char __user *, newname)
2777 {
2778         int error;
2779         char *from;
2780         struct dentry *dentry;
2781         struct path path;
2782
2783         from = getname(oldname);
2784         if (IS_ERR(from))
2785                 return PTR_ERR(from);
2786
2787         dentry = user_path_create(newdfd, newname, &path, 0);
2788         error = PTR_ERR(dentry);
2789         if (IS_ERR(dentry))
2790                 goto out_putname;
2791
2792         error = mnt_want_write(path.mnt);
2793         if (error)
2794                 goto out_dput;
2795         error = security_path_symlink(&path, dentry, from);
2796         if (error)
2797                 goto out_drop_write;
2798         error = vfs_symlink(path.dentry->d_inode, dentry, from);
2799 out_drop_write:
2800         mnt_drop_write(path.mnt);
2801 out_dput:
2802         dput(dentry);
2803         mutex_unlock(&path.dentry->d_inode->i_mutex);
2804         path_put(&path);
2805 out_putname:
2806         putname(from);
2807         return error;
2808 }
2809
2810 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
2811 {
2812         return sys_symlinkat(oldname, AT_FDCWD, newname);
2813 }
2814
2815 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2816 {
2817         struct inode *inode = old_dentry->d_inode;
2818         int error;
2819
2820         if (!inode)
2821                 return -ENOENT;
2822
2823         error = may_create(dir, new_dentry);
2824         if (error)
2825                 return error;
2826
2827         if (dir->i_sb != inode->i_sb)
2828                 return -EXDEV;
2829
2830         /*
2831          * A link to an append-only or immutable file cannot be created.
2832          */
2833         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2834                 return -EPERM;
2835         if (!dir->i_op->link)
2836                 return -EPERM;
2837         if (S_ISDIR(inode->i_mode))
2838                 return -EPERM;
2839
2840         error = security_inode_link(old_dentry, dir, new_dentry);
2841         if (error)
2842                 return error;
2843
2844         mutex_lock(&inode->i_mutex);
2845         /* Make sure we don't allow creating hardlink to an unlinked file */
2846         if (inode->i_nlink == 0)
2847                 error =  -ENOENT;
2848         else
2849                 error = dir->i_op->link(old_dentry, dir, new_dentry);
2850         mutex_unlock(&inode->i_mutex);
2851         if (!error)
2852                 fsnotify_link(dir, inode, new_dentry);
2853         return error;
2854 }
2855
2856 /*
2857  * Hardlinks are often used in delicate situations.  We avoid
2858  * security-related surprises by not following symlinks on the
2859  * newname.  --KAB
2860  *
2861  * We don't follow them on the oldname either to be compatible
2862  * with linux 2.0, and to avoid hard-linking to directories
2863  * and other special files.  --ADM
2864  */
2865 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2866                 int, newdfd, const char __user *, newname, int, flags)
2867 {
2868         struct dentry *new_dentry;
2869         struct path old_path, new_path;
2870         int how = 0;
2871         int error;
2872
2873         if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2874                 return -EINVAL;
2875         /*
2876          * To use null names we require CAP_DAC_READ_SEARCH
2877          * This ensures that not everyone will be able to create
2878          * handlink using the passed filedescriptor.
2879          */
2880         if (flags & AT_EMPTY_PATH) {
2881                 if (!capable(CAP_DAC_READ_SEARCH))
2882                         return -ENOENT;
2883                 how = LOOKUP_EMPTY;
2884         }
2885
2886         if (flags & AT_SYMLINK_FOLLOW)
2887                 how |= LOOKUP_FOLLOW;
2888
2889         error = user_path_at(olddfd, oldname, how, &old_path);
2890         if (error)
2891                 return error;
2892
2893         new_dentry = user_path_create(newdfd, newname, &new_path, 0);
2894         error = PTR_ERR(new_dentry);
2895         if (IS_ERR(new_dentry))
2896                 goto out;
2897
2898         error = -EXDEV;
2899         if (old_path.mnt != new_path.mnt)
2900                 goto out_dput;
2901         error = mnt_want_write(new_path.mnt);
2902         if (error)
2903                 goto out_dput;
2904         error = security_path_link(old_path.dentry, &new_path, new_dentry);
2905         if (error)
2906                 goto out_drop_write;
2907         error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
2908 out_drop_write:
2909         mnt_drop_write(new_path.mnt);
2910 out_dput:
2911         dput(new_dentry);
2912         mutex_unlock(&new_path.dentry->d_inode->i_mutex);
2913         path_put(&new_path);
2914 out:
2915         path_put(&old_path);
2916
2917         return error;
2918 }
2919
2920 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
2921 {
2922         return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
2923 }
2924
2925 /*
2926  * The worst of all namespace operations - renaming directory. "Perverted"
2927  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2928  * Problems:
2929  *      a) we can get into loop creation. Check is done in is_subdir().
2930  *      b) race potential - two innocent renames can create a loop together.
2931  *         That's where 4.4 screws up. Current fix: serialization on
2932  *         sb->s_vfs_rename_mutex. We might be more accurate, but that's another
2933  *         story.
2934  *      c) we have to lock _three_ objects - parents and victim (if it exists).
2935  *         And that - after we got ->i_mutex on parents (until then we don't know
2936  *         whether the target exists).  Solution: try to be smart with locking
2937  *         order for inodes.  We rely on the fact that tree topology may change
2938  *         only under ->s_vfs_rename_mutex _and_ that parent of the object we
2939  *         move will be locked.  Thus we can rank directories by the tree
2940  *         (ancestors first) and rank all non-directories after them.
2941  *         That works since everybody except rename does "lock parent, lookup,
2942  *         lock child" and rename is under ->s_vfs_rename_mutex.
2943  *         HOWEVER, it relies on the assumption that any object with ->lookup()
2944  *         has no more than 1 dentry.  If "hybrid" objects will ever appear,
2945  *         we'd better make sure that there's no link(2) for them.
2946  *      d) conversion from fhandle to dentry may come in the wrong moment - when
2947  *         we are removing the target. Solution: we will have to grab ->i_mutex
2948  *         in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2949  *         ->i_mutex on parents, which works but leads to some truly excessive
2950  *         locking].
2951  */
2952 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2953                           struct inode *new_dir, struct dentry *new_dentry)
2954 {
2955         int error = 0;
2956         struct inode *target = new_dentry->d_inode;
2957
2958         /*
2959          * If we are going to change the parent - check write permissions,
2960          * we'll need to flip '..'.
2961          */
2962         if (new_dir != old_dir) {
2963                 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
2964                 if (error)
2965                         return error;
2966         }
2967
2968         error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2969         if (error)
2970                 return error;
2971
2972         if (target)
2973                 mutex_lock(&target->i_mutex);
2974
2975         error = -EBUSY;
2976         if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
2977                 goto out;
2978
2979         if (target)
2980                 shrink_dcache_parent(new_dentry);
2981         error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2982         if (error)
2983                 goto out;
2984
2985         if (target) {
2986                 target->i_flags |= S_DEAD;
2987                 dont_mount(new_dentry);
2988         }
2989 out:
2990         if (target)
2991                 mutex_unlock(&target->i_mutex);
2992         if (!error)
2993                 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2994                         d_move(old_dentry,new_dentry);
2995         return error;
2996 }
2997
2998 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2999                             struct inode *new_dir, struct dentry *new_dentry)
3000 {
3001         struct inode *target = new_dentry->d_inode;
3002         int error;
3003
3004         error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3005         if (error)
3006                 return error;
3007
3008         dget(new_dentry);
3009         if (target)
3010                 mutex_lock(&target->i_mutex);
3011
3012         error = -EBUSY;
3013         if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3014                 goto out;
3015
3016         error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3017         if (error)
3018                 goto out;
3019
3020         if (target)
3021                 dont_mount(new_dentry);
3022         if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3023                 d_move(old_dentry, new_dentry);
3024 out:
3025         if (target)
3026                 mutex_unlock(&target->i_mutex);
3027         dput(new_dentry);
3028         return error;
3029 }
3030
3031 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3032                struct inode *new_dir, struct dentry *new_dentry)
3033 {
3034         int error;
3035         int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
3036         const unsigned char *old_name;
3037
3038         if (old_dentry->d_inode == new_dentry->d_inode)
3039                 return 0;
3040  
3041         error = may_delete(old_dir, old_dentry, is_dir);
3042         if (error)
3043                 return error;
3044
3045         if (!new_dentry->d_inode)
3046                 error = may_create(new_dir, new_dentry);
3047         else
3048                 error = may_delete(new_dir, new_dentry, is_dir);
3049         if (error)
3050                 return error;
3051
3052         if (!old_dir->i_op->rename)
3053                 return -EPERM;
3054
3055         old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3056
3057         if (is_dir)
3058                 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3059         else
3060                 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3061         if (!error)
3062                 fsnotify_move(old_dir, new_dir, old_name, is_dir,
3063                               new_dentry->d_inode, old_dentry);
3064         fsnotify_oldname_free(old_name);
3065
3066         return error;
3067 }
3068
3069 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3070                 int, newdfd, const char __user *, newname)
3071 {
3072         struct dentry *old_dir, *new_dir;
3073         struct dentry *old_dentry, *new_dentry;
3074         struct dentry *trap;
3075         struct nameidata oldnd, newnd;
3076         char *from;
3077         char *to;
3078         int error;
3079
3080         error = user_path_parent(olddfd, oldname, &oldnd, &from);
3081         if (error)
3082                 goto exit;
3083
3084         error = user_path_parent(newdfd, newname, &newnd, &to);
3085         if (error)
3086                 goto exit1;
3087
3088         error = -EXDEV;
3089         if (oldnd.path.mnt != newnd.path.mnt)
3090                 goto exit2;
3091
3092         old_dir = oldnd.path.dentry;
3093         error = -EBUSY;
3094         if (oldnd.last_type != LAST_NORM)
3095                 goto exit2;
3096
3097         new_dir = newnd.path.dentry;
3098         if (newnd.last_type != LAST_NORM)
3099                 goto exit2;
3100
3101         oldnd.flags &= ~LOOKUP_PARENT;
3102         newnd.flags &= ~LOOKUP_PARENT;
3103         newnd.flags |= LOOKUP_RENAME_TARGET;
3104
3105         trap = lock_rename(new_dir, old_dir);
3106
3107         old_dentry = lookup_hash(&oldnd);
3108         error = PTR_ERR(old_dentry);
3109         if (IS_ERR(old_dentry))
3110                 goto exit3;
3111         /* source must exist */
3112         error = -ENOENT;
3113         if (!old_dentry->d_inode)
3114                 goto exit4;
3115         /* unless the source is a directory trailing slashes give -ENOTDIR */
3116         if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3117                 error = -ENOTDIR;
3118                 if (oldnd.last.name[oldnd.last.len])
3119                         goto exit4;
3120                 if (newnd.last.name[newnd.last.len])
3121                         goto exit4;
3122         }
3123         /* source should not be ancestor of target */
3124         error = -EINVAL;
3125         if (old_dentry == trap)
3126                 goto exit4;
3127         new_dentry = lookup_hash(&newnd);
3128         error = PTR_ERR(new_dentry);
3129         if (IS_ERR(new_dentry))
3130                 goto exit4;
3131         /* target should not be an ancestor of source */
3132         error = -ENOTEMPTY;
3133         if (new_dentry == trap)
3134                 goto exit5;
3135
3136         error = mnt_want_write(oldnd.path.mnt);
3137         if (error)
3138                 goto exit5;
3139         error = security_path_rename(&oldnd.path, old_dentry,
3140                                      &newnd.path, new_dentry);
3141         if (error)
3142                 goto exit6;
3143         error = vfs_rename(old_dir->d_inode, old_dentry,
3144                                    new_dir->d_inode, new_dentry);
3145 exit6:
3146         mnt_drop_write(oldnd.path.mnt);
3147 exit5:
3148         dput(new_dentry);
3149 exit4:
3150         dput(old_dentry);
3151 exit3:
3152         unlock_rename(new_dir, old_dir);
3153 exit2:
3154         path_put(&newnd.path);
3155         putname(to);
3156 exit1:
3157         path_put(&oldnd.path);
3158         putname(from);
3159 exit:
3160         return error;
3161 }
3162
3163 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
3164 {
3165         return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3166 }
3167
3168 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3169 {
3170         int len;
3171
3172         len = PTR_ERR(link);
3173         if (IS_ERR(link))
3174                 goto out;
3175
3176         len = strlen(link);
3177         if (len > (unsigned) buflen)
3178                 len = buflen;
3179         if (copy_to_user(buffer, link, len))
3180                 len = -EFAULT;
3181 out:
3182         return len;
3183 }
3184
3185 /*
3186  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
3187  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
3188  * using) it for any given inode is up to filesystem.
3189  */
3190 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3191 {
3192         struct nameidata nd;
3193         void *cookie;
3194         int res;
3195
3196         nd.depth = 0;
3197         cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3198         if (IS_ERR(cookie))
3199                 return PTR_ERR(cookie);
3200
3201         res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3202         if (dentry->d_inode->i_op->put_link)
3203                 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3204         return res;
3205 }
3206
3207 int vfs_follow_link(struct nameidata *nd, const char *link)
3208 {
3209         return __vfs_follow_link(nd, link);
3210 }
3211
3212 /* get the link contents into pagecache */
3213 static char *page_getlink(struct dentry * dentry, struct page **ppage)
3214 {
3215         char *kaddr;
3216         struct page *page;
3217         struct address_space *mapping = dentry->d_inode->i_mapping;
3218         page = read_mapping_page(mapping, 0, NULL);
3219         if (IS_ERR(page))
3220                 return (char*)page;
3221         *ppage = page;
3222         kaddr = kmap(page);
3223         nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3224         return kaddr;
3225 }
3226
3227 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3228 {
3229         struct page *page = NULL;
3230         char *s = page_getlink(dentry, &page);
3231         int res = vfs_readlink(dentry,buffer,buflen,s);
3232         if (page) {
3233                 kunmap(page);
3234                 page_cache_release(page);
3235         }
3236         return res;
3237 }
3238
3239 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
3240 {
3241         struct page *page = NULL;
3242         nd_set_link(nd, page_getlink(dentry, &page));
3243         return page;
3244 }
3245
3246 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
3247 {
3248         struct page *page = cookie;
3249
3250         if (page) {
3251                 kunmap(page);
3252                 page_cache_release(page);
3253         }
3254 }
3255
3256 /*
3257  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3258  */
3259 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
3260 {
3261         struct address_space *mapping = inode->i_mapping;
3262         struct page *page;
3263         void *fsdata;
3264         int err;
3265         char *kaddr;
3266         unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3267         if (nofs)
3268                 flags |= AOP_FLAG_NOFS;
3269
3270 retry:
3271         err = pagecache_write_begin(NULL, mapping, 0, len-1,
3272                                 flags, &page, &fsdata);
3273         if (err)
3274                 goto fail;
3275
3276         kaddr = kmap_atomic(page, KM_USER0);
3277         memcpy(kaddr, symname, len-1);
3278         kunmap_atomic(kaddr, KM_USER0);
3279
3280         err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3281                                                         page, fsdata);
3282         if (err < 0)
3283                 goto fail;
3284         if (err < len-1)
3285                 goto retry;
3286
3287         mark_inode_dirty(inode);
3288         return 0;
3289 fail:
3290         return err;
3291 }
3292
3293 int page_symlink(struct inode *inode, const char *symname, int len)
3294 {
3295         return __page_symlink(inode, symname, len,
3296                         !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
3297 }
3298
3299 const struct inode_operations page_symlink_inode_operations = {
3300         .readlink       = generic_readlink,
3301         .follow_link    = page_follow_link_light,
3302         .put_link       = page_put_link,
3303 };
3304
3305 EXPORT_SYMBOL(user_path_at);
3306 EXPORT_SYMBOL(follow_down_one);
3307 EXPORT_SYMBOL(follow_down);
3308 EXPORT_SYMBOL(follow_up);
3309 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3310 EXPORT_SYMBOL(getname);
3311 EXPORT_SYMBOL(lock_rename);
3312 EXPORT_SYMBOL(lookup_one_len);
3313 EXPORT_SYMBOL(page_follow_link_light);
3314 EXPORT_SYMBOL(page_put_link);
3315 EXPORT_SYMBOL(page_readlink);
3316 EXPORT_SYMBOL(__page_symlink);
3317 EXPORT_SYMBOL(page_symlink);
3318 EXPORT_SYMBOL(page_symlink_inode_operations);
3319 EXPORT_SYMBOL(kern_path);
3320 EXPORT_SYMBOL(vfs_path_lookup);
3321 EXPORT_SYMBOL(inode_permission);
3322 EXPORT_SYMBOL(unlock_rename);
3323 EXPORT_SYMBOL(vfs_create);
3324 EXPORT_SYMBOL(vfs_follow_link);
3325 EXPORT_SYMBOL(vfs_link);
3326 EXPORT_SYMBOL(vfs_mkdir);
3327 EXPORT_SYMBOL(vfs_mknod);
3328 EXPORT_SYMBOL(generic_permission);
3329 EXPORT_SYMBOL(vfs_readlink);
3330 EXPORT_SYMBOL(vfs_rename);
3331 EXPORT_SYMBOL(vfs_rmdir);
3332 EXPORT_SYMBOL(vfs_symlink);
3333 EXPORT_SYMBOL(vfs_unlink);
3334 EXPORT_SYMBOL(dentry_unhash);
3335 EXPORT_SYMBOL(generic_readlink);