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