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