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