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