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