fix typo in assignment of fs default overflow gid
[linux-2.6-block.git] / fs / namei.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/namei.c
4  *
5  *  Copyright (C) 1991, 1992  Linus Torvalds
6  */
7
8 /*
9  * Some corrections by tytso.
10  */
11
12 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
13  * lookup logic.
14  */
15 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
16  */
17
18 #include <linux/init.h>
19 #include <linux/export.h>
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22 #include <linux/fs.h>
23 #include <linux/namei.h>
24 #include <linux/pagemap.h>
25 #include <linux/fsnotify.h>
26 #include <linux/personality.h>
27 #include <linux/security.h>
28 #include <linux/ima.h>
29 #include <linux/syscalls.h>
30 #include <linux/mount.h>
31 #include <linux/audit.h>
32 #include <linux/capability.h>
33 #include <linux/file.h>
34 #include <linux/fcntl.h>
35 #include <linux/device_cgroup.h>
36 #include <linux/fs_struct.h>
37 #include <linux/posix_acl.h>
38 #include <linux/hash.h>
39 #include <linux/bitops.h>
40 #include <linux/init_task.h>
41 #include <linux/uaccess.h>
42
43 #include "internal.h"
44 #include "mount.h"
45
46 /* [Feb-1997 T. Schoebel-Theuer]
47  * Fundamental changes in the pathname lookup mechanisms (namei)
48  * were necessary because of omirr.  The reason is that omirr needs
49  * to know the _real_ pathname, not the user-supplied one, in case
50  * of symlinks (and also when transname replacements occur).
51  *
52  * The new code replaces the old recursive symlink resolution with
53  * an iterative one (in case of non-nested symlink chains).  It does
54  * this with calls to <fs>_follow_link().
55  * As a side effect, dir_namei(), _namei() and follow_link() are now 
56  * replaced with a single function lookup_dentry() that can handle all 
57  * the special cases of the former code.
58  *
59  * With the new dcache, the pathname is stored at each inode, at least as
60  * long as the refcount of the inode is positive.  As a side effect, the
61  * size of the dcache depends on the inode cache and thus is dynamic.
62  *
63  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
64  * resolution to correspond with current state of the code.
65  *
66  * Note that the symlink resolution is not *completely* iterative.
67  * There is still a significant amount of tail- and mid- recursion in
68  * the algorithm.  Also, note that <fs>_readlink() is not used in
69  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
70  * may return different results than <fs>_follow_link().  Many virtual
71  * filesystems (including /proc) exhibit this behavior.
72  */
73
74 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
75  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
76  * and the name already exists in form of a symlink, try to create the new
77  * name indicated by the symlink. The old code always complained that the
78  * name already exists, due to not following the symlink even if its target
79  * is nonexistent.  The new semantics affects also mknod() and link() when
80  * the name is a symlink pointing to a non-existent name.
81  *
82  * I don't know which semantics is the right one, since I have no access
83  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
84  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
85  * "old" one. Personally, I think the new semantics is much more logical.
86  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
87  * file does succeed in both HP-UX and SunOs, but not in Solaris
88  * and in the old Linux semantics.
89  */
90
91 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
92  * semantics.  See the comments in "open_namei" and "do_link" below.
93  *
94  * [10-Sep-98 Alan Modra] Another symlink change.
95  */
96
97 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
98  *      inside the path - always follow.
99  *      in the last component in creation/removal/renaming - never follow.
100  *      if LOOKUP_FOLLOW passed - follow.
101  *      if the pathname has trailing slashes - follow.
102  *      otherwise - don't follow.
103  * (applied in that order).
104  *
105  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
106  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
107  * During the 2.4 we need to fix the userland stuff depending on it -
108  * hopefully we will be able to get rid of that wart in 2.5. So far only
109  * XEmacs seems to be relying on it...
110  */
111 /*
112  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
113  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
114  * any extra contention...
115  */
116
117 /* In order to reduce some races, while at the same time doing additional
118  * checking and hopefully speeding things up, we copy filenames to the
119  * kernel data space before using them..
120  *
121  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
122  * PATH_MAX includes the nul terminator --RR.
123  */
124
125 #define EMBEDDED_NAME_MAX       (PATH_MAX - offsetof(struct filename, iname))
126
127 struct filename *
128 getname_flags(const char __user *filename, int flags, int *empty)
129 {
130         struct filename *result;
131         char *kname;
132         int len;
133
134         result = audit_reusename(filename);
135         if (result)
136                 return result;
137
138         result = __getname();
139         if (unlikely(!result))
140                 return ERR_PTR(-ENOMEM);
141
142         /*
143          * First, try to embed the struct filename inside the names_cache
144          * allocation
145          */
146         kname = (char *)result->iname;
147         result->name = kname;
148
149         len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
150         if (unlikely(len < 0)) {
151                 __putname(result);
152                 return ERR_PTR(len);
153         }
154
155         /*
156          * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
157          * separate struct filename so we can dedicate the entire
158          * names_cache allocation for the pathname, and re-do the copy from
159          * userland.
160          */
161         if (unlikely(len == EMBEDDED_NAME_MAX)) {
162                 const size_t size = offsetof(struct filename, iname[1]);
163                 kname = (char *)result;
164
165                 /*
166                  * size is chosen that way we to guarantee that
167                  * result->iname[0] is within the same object and that
168                  * kname can't be equal to result->iname, no matter what.
169                  */
170                 result = kzalloc(size, GFP_KERNEL);
171                 if (unlikely(!result)) {
172                         __putname(kname);
173                         return ERR_PTR(-ENOMEM);
174                 }
175                 result->name = kname;
176                 len = strncpy_from_user(kname, filename, PATH_MAX);
177                 if (unlikely(len < 0)) {
178                         __putname(kname);
179                         kfree(result);
180                         return ERR_PTR(len);
181                 }
182                 if (unlikely(len == PATH_MAX)) {
183                         __putname(kname);
184                         kfree(result);
185                         return ERR_PTR(-ENAMETOOLONG);
186                 }
187         }
188
189         result->refcnt = 1;
190         /* The empty path is special. */
191         if (unlikely(!len)) {
192                 if (empty)
193                         *empty = 1;
194                 if (!(flags & LOOKUP_EMPTY)) {
195                         putname(result);
196                         return ERR_PTR(-ENOENT);
197                 }
198         }
199
200         result->uptr = filename;
201         result->aname = NULL;
202         audit_getname(result);
203         return result;
204 }
205
206 struct filename *
207 getname(const char __user * filename)
208 {
209         return getname_flags(filename, 0, NULL);
210 }
211
212 struct filename *
213 getname_kernel(const char * filename)
214 {
215         struct filename *result;
216         int len = strlen(filename) + 1;
217
218         result = __getname();
219         if (unlikely(!result))
220                 return ERR_PTR(-ENOMEM);
221
222         if (len <= EMBEDDED_NAME_MAX) {
223                 result->name = (char *)result->iname;
224         } else if (len <= PATH_MAX) {
225                 struct filename *tmp;
226
227                 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
228                 if (unlikely(!tmp)) {
229                         __putname(result);
230                         return ERR_PTR(-ENOMEM);
231                 }
232                 tmp->name = (char *)result;
233                 result = tmp;
234         } else {
235                 __putname(result);
236                 return ERR_PTR(-ENAMETOOLONG);
237         }
238         memcpy((char *)result->name, filename, len);
239         result->uptr = NULL;
240         result->aname = NULL;
241         result->refcnt = 1;
242         audit_getname(result);
243
244         return result;
245 }
246
247 void putname(struct filename *name)
248 {
249         BUG_ON(name->refcnt <= 0);
250
251         if (--name->refcnt > 0)
252                 return;
253
254         if (name->name != name->iname) {
255                 __putname(name->name);
256                 kfree(name);
257         } else
258                 __putname(name);
259 }
260
261 static int check_acl(struct inode *inode, int mask)
262 {
263 #ifdef CONFIG_FS_POSIX_ACL
264         struct posix_acl *acl;
265
266         if (mask & MAY_NOT_BLOCK) {
267                 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
268                 if (!acl)
269                         return -EAGAIN;
270                 /* no ->get_acl() calls in RCU mode... */
271                 if (is_uncached_acl(acl))
272                         return -ECHILD;
273                 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
274         }
275
276         acl = get_acl(inode, ACL_TYPE_ACCESS);
277         if (IS_ERR(acl))
278                 return PTR_ERR(acl);
279         if (acl) {
280                 int error = posix_acl_permission(inode, acl, mask);
281                 posix_acl_release(acl);
282                 return error;
283         }
284 #endif
285
286         return -EAGAIN;
287 }
288
289 /*
290  * This does the basic permission checking
291  */
292 static int acl_permission_check(struct inode *inode, int mask)
293 {
294         unsigned int mode = inode->i_mode;
295
296         if (likely(uid_eq(current_fsuid(), inode->i_uid)))
297                 mode >>= 6;
298         else {
299                 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
300                         int error = check_acl(inode, mask);
301                         if (error != -EAGAIN)
302                                 return error;
303                 }
304
305                 if (in_group_p(inode->i_gid))
306                         mode >>= 3;
307         }
308
309         /*
310          * If the DACs are ok we don't need any capability check.
311          */
312         if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
313                 return 0;
314         return -EACCES;
315 }
316
317 /**
318  * generic_permission -  check for access rights on a Posix-like filesystem
319  * @inode:      inode to check access rights for
320  * @mask:       right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
321  *
322  * Used to check for read/write/execute permissions on a file.
323  * We use "fsuid" for this, letting us set arbitrary permissions
324  * for filesystem access without changing the "normal" uids which
325  * are used for other things.
326  *
327  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
328  * request cannot be satisfied (eg. requires blocking or too much complexity).
329  * It would then be called again in ref-walk mode.
330  */
331 int generic_permission(struct inode *inode, int mask)
332 {
333         int ret;
334
335         /*
336          * Do the basic permission checks.
337          */
338         ret = acl_permission_check(inode, mask);
339         if (ret != -EACCES)
340                 return ret;
341
342         if (S_ISDIR(inode->i_mode)) {
343                 /* DACs are overridable for directories */
344                 if (!(mask & MAY_WRITE))
345                         if (capable_wrt_inode_uidgid(inode,
346                                                      CAP_DAC_READ_SEARCH))
347                                 return 0;
348                 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
349                         return 0;
350                 return -EACCES;
351         }
352
353         /*
354          * Searching includes executable on directories, else just read.
355          */
356         mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
357         if (mask == MAY_READ)
358                 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
359                         return 0;
360         /*
361          * Read/write DACs are always overridable.
362          * Executable DACs are overridable when there is
363          * at least one exec bit set.
364          */
365         if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
366                 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
367                         return 0;
368
369         return -EACCES;
370 }
371 EXPORT_SYMBOL(generic_permission);
372
373 /*
374  * We _really_ want to just do "generic_permission()" without
375  * even looking at the inode->i_op values. So we keep a cache
376  * flag in inode->i_opflags, that says "this has not special
377  * permission function, use the fast case".
378  */
379 static inline int do_inode_permission(struct inode *inode, int mask)
380 {
381         if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
382                 if (likely(inode->i_op->permission))
383                         return inode->i_op->permission(inode, mask);
384
385                 /* This gets set once for the inode lifetime */
386                 spin_lock(&inode->i_lock);
387                 inode->i_opflags |= IOP_FASTPERM;
388                 spin_unlock(&inode->i_lock);
389         }
390         return generic_permission(inode, mask);
391 }
392
393 /**
394  * __inode_permission - Check for access rights to a given inode
395  * @inode: Inode to check permission on
396  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
397  *
398  * Check for read/write/execute permissions on an inode.
399  *
400  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
401  *
402  * This does not check for a read-only file system.  You probably want
403  * inode_permission().
404  */
405 int __inode_permission(struct inode *inode, int mask)
406 {
407         int retval;
408
409         if (unlikely(mask & MAY_WRITE)) {
410                 /*
411                  * Nobody gets write access to an immutable file.
412                  */
413                 if (IS_IMMUTABLE(inode))
414                         return -EPERM;
415
416                 /*
417                  * Updating mtime will likely cause i_uid and i_gid to be
418                  * written back improperly if their true value is unknown
419                  * to the vfs.
420                  */
421                 if (HAS_UNMAPPED_ID(inode))
422                         return -EACCES;
423         }
424
425         retval = do_inode_permission(inode, mask);
426         if (retval)
427                 return retval;
428
429         retval = devcgroup_inode_permission(inode, mask);
430         if (retval)
431                 return retval;
432
433         return security_inode_permission(inode, mask);
434 }
435 EXPORT_SYMBOL(__inode_permission);
436
437 /**
438  * sb_permission - Check superblock-level permissions
439  * @sb: Superblock of inode to check permission on
440  * @inode: Inode to check permission on
441  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
442  *
443  * Separate out file-system wide checks from inode-specific permission checks.
444  */
445 static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
446 {
447         if (unlikely(mask & MAY_WRITE)) {
448                 umode_t mode = inode->i_mode;
449
450                 /* Nobody gets write access to a read-only fs. */
451                 if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
452                         return -EROFS;
453         }
454         return 0;
455 }
456
457 /**
458  * inode_permission - Check for access rights to a given inode
459  * @inode: Inode to check permission on
460  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
461  *
462  * Check for read/write/execute permissions on an inode.  We use fs[ug]id for
463  * this, letting us set arbitrary permissions for filesystem access without
464  * changing the "normal" UIDs which are used for other things.
465  *
466  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
467  */
468 int inode_permission(struct inode *inode, int mask)
469 {
470         int retval;
471
472         retval = sb_permission(inode->i_sb, inode, mask);
473         if (retval)
474                 return retval;
475         return __inode_permission(inode, mask);
476 }
477 EXPORT_SYMBOL(inode_permission);
478
479 /**
480  * path_get - get a reference to a path
481  * @path: path to get the reference to
482  *
483  * Given a path increment the reference count to the dentry and the vfsmount.
484  */
485 void path_get(const struct path *path)
486 {
487         mntget(path->mnt);
488         dget(path->dentry);
489 }
490 EXPORT_SYMBOL(path_get);
491
492 /**
493  * path_put - put a reference to a path
494  * @path: path to put the reference to
495  *
496  * Given a path decrement the reference count to the dentry and the vfsmount.
497  */
498 void path_put(const struct path *path)
499 {
500         dput(path->dentry);
501         mntput(path->mnt);
502 }
503 EXPORT_SYMBOL(path_put);
504
505 #define EMBEDDED_LEVELS 2
506 struct nameidata {
507         struct path     path;
508         struct qstr     last;
509         struct path     root;
510         struct inode    *inode; /* path.dentry.d_inode */
511         unsigned int    flags;
512         unsigned        seq, m_seq;
513         int             last_type;
514         unsigned        depth;
515         int             total_link_count;
516         struct saved {
517                 struct path link;
518                 struct delayed_call done;
519                 const char *name;
520                 unsigned seq;
521         } *stack, internal[EMBEDDED_LEVELS];
522         struct filename *name;
523         struct nameidata *saved;
524         struct inode    *link_inode;
525         unsigned        root_seq;
526         int             dfd;
527 } __randomize_layout;
528
529 static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
530 {
531         struct nameidata *old = current->nameidata;
532         p->stack = p->internal;
533         p->dfd = dfd;
534         p->name = name;
535         p->total_link_count = old ? old->total_link_count : 0;
536         p->saved = old;
537         current->nameidata = p;
538 }
539
540 static void restore_nameidata(void)
541 {
542         struct nameidata *now = current->nameidata, *old = now->saved;
543
544         current->nameidata = old;
545         if (old)
546                 old->total_link_count = now->total_link_count;
547         if (now->stack != now->internal)
548                 kfree(now->stack);
549 }
550
551 static int __nd_alloc_stack(struct nameidata *nd)
552 {
553         struct saved *p;
554
555         if (nd->flags & LOOKUP_RCU) {
556                 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
557                                   GFP_ATOMIC);
558                 if (unlikely(!p))
559                         return -ECHILD;
560         } else {
561                 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
562                                   GFP_KERNEL);
563                 if (unlikely(!p))
564                         return -ENOMEM;
565         }
566         memcpy(p, nd->internal, sizeof(nd->internal));
567         nd->stack = p;
568         return 0;
569 }
570
571 /**
572  * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
573  * @path: nameidate to verify
574  *
575  * Rename can sometimes move a file or directory outside of a bind
576  * mount, path_connected allows those cases to be detected.
577  */
578 static bool path_connected(const struct path *path)
579 {
580         struct vfsmount *mnt = path->mnt;
581
582         /* Only bind mounts can have disconnected paths */
583         if (mnt->mnt_root == mnt->mnt_sb->s_root)
584                 return true;
585
586         return is_subdir(path->dentry, mnt->mnt_root);
587 }
588
589 static inline int nd_alloc_stack(struct nameidata *nd)
590 {
591         if (likely(nd->depth != EMBEDDED_LEVELS))
592                 return 0;
593         if (likely(nd->stack != nd->internal))
594                 return 0;
595         return __nd_alloc_stack(nd);
596 }
597
598 static void drop_links(struct nameidata *nd)
599 {
600         int i = nd->depth;
601         while (i--) {
602                 struct saved *last = nd->stack + i;
603                 do_delayed_call(&last->done);
604                 clear_delayed_call(&last->done);
605         }
606 }
607
608 static void terminate_walk(struct nameidata *nd)
609 {
610         drop_links(nd);
611         if (!(nd->flags & LOOKUP_RCU)) {
612                 int i;
613                 path_put(&nd->path);
614                 for (i = 0; i < nd->depth; i++)
615                         path_put(&nd->stack[i].link);
616                 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
617                         path_put(&nd->root);
618                         nd->root.mnt = NULL;
619                 }
620         } else {
621                 nd->flags &= ~LOOKUP_RCU;
622                 if (!(nd->flags & LOOKUP_ROOT))
623                         nd->root.mnt = NULL;
624                 rcu_read_unlock();
625         }
626         nd->depth = 0;
627 }
628
629 /* path_put is needed afterwards regardless of success or failure */
630 static bool legitimize_path(struct nameidata *nd,
631                             struct path *path, unsigned seq)
632 {
633         int res = __legitimize_mnt(path->mnt, nd->m_seq);
634         if (unlikely(res)) {
635                 if (res > 0)
636                         path->mnt = NULL;
637                 path->dentry = NULL;
638                 return false;
639         }
640         if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
641                 path->dentry = NULL;
642                 return false;
643         }
644         return !read_seqcount_retry(&path->dentry->d_seq, seq);
645 }
646
647 static bool legitimize_links(struct nameidata *nd)
648 {
649         int i;
650         for (i = 0; i < nd->depth; i++) {
651                 struct saved *last = nd->stack + i;
652                 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
653                         drop_links(nd);
654                         nd->depth = i + 1;
655                         return false;
656                 }
657         }
658         return true;
659 }
660
661 /*
662  * Path walking has 2 modes, rcu-walk and ref-walk (see
663  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
664  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
665  * normal reference counts on dentries and vfsmounts to transition to ref-walk
666  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
667  * got stuck, so ref-walk may continue from there. If this is not successful
668  * (eg. a seqcount has changed), then failure is returned and it's up to caller
669  * to restart the path walk from the beginning in ref-walk mode.
670  */
671
672 /**
673  * unlazy_walk - try to switch to ref-walk mode.
674  * @nd: nameidata pathwalk data
675  * Returns: 0 on success, -ECHILD on failure
676  *
677  * unlazy_walk attempts to legitimize the current nd->path and nd->root
678  * for ref-walk mode.
679  * Must be called from rcu-walk context.
680  * Nothing should touch nameidata between unlazy_walk() failure and
681  * terminate_walk().
682  */
683 static int unlazy_walk(struct nameidata *nd)
684 {
685         struct dentry *parent = nd->path.dentry;
686
687         BUG_ON(!(nd->flags & LOOKUP_RCU));
688
689         nd->flags &= ~LOOKUP_RCU;
690         if (unlikely(!legitimize_links(nd)))
691                 goto out2;
692         if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
693                 goto out1;
694         if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
695                 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq)))
696                         goto out;
697         }
698         rcu_read_unlock();
699         BUG_ON(nd->inode != parent->d_inode);
700         return 0;
701
702 out2:
703         nd->path.mnt = NULL;
704         nd->path.dentry = NULL;
705 out1:
706         if (!(nd->flags & LOOKUP_ROOT))
707                 nd->root.mnt = NULL;
708 out:
709         rcu_read_unlock();
710         return -ECHILD;
711 }
712
713 /**
714  * unlazy_child - try to switch to ref-walk mode.
715  * @nd: nameidata pathwalk data
716  * @dentry: child of nd->path.dentry
717  * @seq: seq number to check dentry against
718  * Returns: 0 on success, -ECHILD on failure
719  *
720  * unlazy_child attempts to legitimize the current nd->path, nd->root and dentry
721  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
722  * @nd.  Must be called from rcu-walk context.
723  * Nothing should touch nameidata between unlazy_child() failure and
724  * terminate_walk().
725  */
726 static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned seq)
727 {
728         BUG_ON(!(nd->flags & LOOKUP_RCU));
729
730         nd->flags &= ~LOOKUP_RCU;
731         if (unlikely(!legitimize_links(nd)))
732                 goto out2;
733         if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
734                 goto out2;
735         if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
736                 goto out1;
737
738         /*
739          * We need to move both the parent and the dentry from the RCU domain
740          * to be properly refcounted. And the sequence number in the dentry
741          * validates *both* dentry counters, since we checked the sequence
742          * number of the parent after we got the child sequence number. So we
743          * know the parent must still be valid if the child sequence number is
744          */
745         if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
746                 goto out;
747         if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) {
748                 rcu_read_unlock();
749                 dput(dentry);
750                 goto drop_root_mnt;
751         }
752         /*
753          * Sequence counts matched. Now make sure that the root is
754          * still valid and get it if required.
755          */
756         if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
757                 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
758                         rcu_read_unlock();
759                         dput(dentry);
760                         return -ECHILD;
761                 }
762         }
763
764         rcu_read_unlock();
765         return 0;
766
767 out2:
768         nd->path.mnt = NULL;
769 out1:
770         nd->path.dentry = NULL;
771 out:
772         rcu_read_unlock();
773 drop_root_mnt:
774         if (!(nd->flags & LOOKUP_ROOT))
775                 nd->root.mnt = NULL;
776         return -ECHILD;
777 }
778
779 static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
780 {
781         if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
782                 return dentry->d_op->d_revalidate(dentry, flags);
783         else
784                 return 1;
785 }
786
787 /**
788  * complete_walk - successful completion of path walk
789  * @nd:  pointer nameidata
790  *
791  * If we had been in RCU mode, drop out of it and legitimize nd->path.
792  * Revalidate the final result, unless we'd already done that during
793  * the path walk or the filesystem doesn't ask for it.  Return 0 on
794  * success, -error on failure.  In case of failure caller does not
795  * need to drop nd->path.
796  */
797 static int complete_walk(struct nameidata *nd)
798 {
799         struct dentry *dentry = nd->path.dentry;
800         int status;
801
802         if (nd->flags & LOOKUP_RCU) {
803                 if (!(nd->flags & LOOKUP_ROOT))
804                         nd->root.mnt = NULL;
805                 if (unlikely(unlazy_walk(nd)))
806                         return -ECHILD;
807         }
808
809         if (likely(!(nd->flags & LOOKUP_JUMPED)))
810                 return 0;
811
812         if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
813                 return 0;
814
815         status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
816         if (status > 0)
817                 return 0;
818
819         if (!status)
820                 status = -ESTALE;
821
822         return status;
823 }
824
825 static void set_root(struct nameidata *nd)
826 {
827         struct fs_struct *fs = current->fs;
828
829         if (nd->flags & LOOKUP_RCU) {
830                 unsigned seq;
831
832                 do {
833                         seq = read_seqcount_begin(&fs->seq);
834                         nd->root = fs->root;
835                         nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
836                 } while (read_seqcount_retry(&fs->seq, seq));
837         } else {
838                 get_fs_root(fs, &nd->root);
839         }
840 }
841
842 static void path_put_conditional(struct path *path, struct nameidata *nd)
843 {
844         dput(path->dentry);
845         if (path->mnt != nd->path.mnt)
846                 mntput(path->mnt);
847 }
848
849 static inline void path_to_nameidata(const struct path *path,
850                                         struct nameidata *nd)
851 {
852         if (!(nd->flags & LOOKUP_RCU)) {
853                 dput(nd->path.dentry);
854                 if (nd->path.mnt != path->mnt)
855                         mntput(nd->path.mnt);
856         }
857         nd->path.mnt = path->mnt;
858         nd->path.dentry = path->dentry;
859 }
860
861 static int nd_jump_root(struct nameidata *nd)
862 {
863         if (nd->flags & LOOKUP_RCU) {
864                 struct dentry *d;
865                 nd->path = nd->root;
866                 d = nd->path.dentry;
867                 nd->inode = d->d_inode;
868                 nd->seq = nd->root_seq;
869                 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
870                         return -ECHILD;
871         } else {
872                 path_put(&nd->path);
873                 nd->path = nd->root;
874                 path_get(&nd->path);
875                 nd->inode = nd->path.dentry->d_inode;
876         }
877         nd->flags |= LOOKUP_JUMPED;
878         return 0;
879 }
880
881 /*
882  * Helper to directly jump to a known parsed path from ->get_link,
883  * caller must have taken a reference to path beforehand.
884  */
885 void nd_jump_link(struct path *path)
886 {
887         struct nameidata *nd = current->nameidata;
888         path_put(&nd->path);
889
890         nd->path = *path;
891         nd->inode = nd->path.dentry->d_inode;
892         nd->flags |= LOOKUP_JUMPED;
893 }
894
895 static inline void put_link(struct nameidata *nd)
896 {
897         struct saved *last = nd->stack + --nd->depth;
898         do_delayed_call(&last->done);
899         if (!(nd->flags & LOOKUP_RCU))
900                 path_put(&last->link);
901 }
902
903 int sysctl_protected_symlinks __read_mostly = 0;
904 int sysctl_protected_hardlinks __read_mostly = 0;
905
906 /**
907  * may_follow_link - Check symlink following for unsafe situations
908  * @nd: nameidata pathwalk data
909  *
910  * In the case of the sysctl_protected_symlinks sysctl being enabled,
911  * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
912  * in a sticky world-writable directory. This is to protect privileged
913  * processes from failing races against path names that may change out
914  * from under them by way of other users creating malicious symlinks.
915  * It will permit symlinks to be followed only when outside a sticky
916  * world-writable directory, or when the uid of the symlink and follower
917  * match, or when the directory owner matches the symlink's owner.
918  *
919  * Returns 0 if following the symlink is allowed, -ve on error.
920  */
921 static inline int may_follow_link(struct nameidata *nd)
922 {
923         const struct inode *inode;
924         const struct inode *parent;
925         kuid_t puid;
926
927         if (!sysctl_protected_symlinks)
928                 return 0;
929
930         /* Allowed if owner and follower match. */
931         inode = nd->link_inode;
932         if (uid_eq(current_cred()->fsuid, inode->i_uid))
933                 return 0;
934
935         /* Allowed if parent directory not sticky and world-writable. */
936         parent = nd->inode;
937         if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
938                 return 0;
939
940         /* Allowed if parent directory and link owner match. */
941         puid = parent->i_uid;
942         if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
943                 return 0;
944
945         if (nd->flags & LOOKUP_RCU)
946                 return -ECHILD;
947
948         audit_log_link_denied("follow_link", &nd->stack[0].link);
949         return -EACCES;
950 }
951
952 /**
953  * safe_hardlink_source - Check for safe hardlink conditions
954  * @inode: the source inode to hardlink from
955  *
956  * Return false if at least one of the following conditions:
957  *    - inode is not a regular file
958  *    - inode is setuid
959  *    - inode is setgid and group-exec
960  *    - access failure for read and write
961  *
962  * Otherwise returns true.
963  */
964 static bool safe_hardlink_source(struct inode *inode)
965 {
966         umode_t mode = inode->i_mode;
967
968         /* Special files should not get pinned to the filesystem. */
969         if (!S_ISREG(mode))
970                 return false;
971
972         /* Setuid files should not get pinned to the filesystem. */
973         if (mode & S_ISUID)
974                 return false;
975
976         /* Executable setgid files should not get pinned to the filesystem. */
977         if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
978                 return false;
979
980         /* Hardlinking to unreadable or unwritable sources is dangerous. */
981         if (inode_permission(inode, MAY_READ | MAY_WRITE))
982                 return false;
983
984         return true;
985 }
986
987 /**
988  * may_linkat - Check permissions for creating a hardlink
989  * @link: the source to hardlink from
990  *
991  * Block hardlink when all of:
992  *  - sysctl_protected_hardlinks enabled
993  *  - fsuid does not match inode
994  *  - hardlink source is unsafe (see safe_hardlink_source() above)
995  *  - not CAP_FOWNER in a namespace with the inode owner uid mapped
996  *
997  * Returns 0 if successful, -ve on error.
998  */
999 static int may_linkat(struct path *link)
1000 {
1001         struct inode *inode;
1002
1003         if (!sysctl_protected_hardlinks)
1004                 return 0;
1005
1006         inode = link->dentry->d_inode;
1007
1008         /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1009          * otherwise, it must be a safe source.
1010          */
1011         if (safe_hardlink_source(inode) || inode_owner_or_capable(inode))
1012                 return 0;
1013
1014         audit_log_link_denied("linkat", link);
1015         return -EPERM;
1016 }
1017
1018 static __always_inline
1019 const char *get_link(struct nameidata *nd)
1020 {
1021         struct saved *last = nd->stack + nd->depth - 1;
1022         struct dentry *dentry = last->link.dentry;
1023         struct inode *inode = nd->link_inode;
1024         int error;
1025         const char *res;
1026
1027         if (!(nd->flags & LOOKUP_RCU)) {
1028                 touch_atime(&last->link);
1029                 cond_resched();
1030         } else if (atime_needs_update_rcu(&last->link, inode)) {
1031                 if (unlikely(unlazy_walk(nd)))
1032                         return ERR_PTR(-ECHILD);
1033                 touch_atime(&last->link);
1034         }
1035
1036         error = security_inode_follow_link(dentry, inode,
1037                                            nd->flags & LOOKUP_RCU);
1038         if (unlikely(error))
1039                 return ERR_PTR(error);
1040
1041         nd->last_type = LAST_BIND;
1042         res = inode->i_link;
1043         if (!res) {
1044                 const char * (*get)(struct dentry *, struct inode *,
1045                                 struct delayed_call *);
1046                 get = inode->i_op->get_link;
1047                 if (nd->flags & LOOKUP_RCU) {
1048                         res = get(NULL, inode, &last->done);
1049                         if (res == ERR_PTR(-ECHILD)) {
1050                                 if (unlikely(unlazy_walk(nd)))
1051                                         return ERR_PTR(-ECHILD);
1052                                 res = get(dentry, inode, &last->done);
1053                         }
1054                 } else {
1055                         res = get(dentry, inode, &last->done);
1056                 }
1057                 if (IS_ERR_OR_NULL(res))
1058                         return res;
1059         }
1060         if (*res == '/') {
1061                 if (!nd->root.mnt)
1062                         set_root(nd);
1063                 if (unlikely(nd_jump_root(nd)))
1064                         return ERR_PTR(-ECHILD);
1065                 while (unlikely(*++res == '/'))
1066                         ;
1067         }
1068         if (!*res)
1069                 res = NULL;
1070         return res;
1071 }
1072
1073 /*
1074  * follow_up - Find the mountpoint of path's vfsmount
1075  *
1076  * Given a path, find the mountpoint of its source file system.
1077  * Replace @path with the path of the mountpoint in the parent mount.
1078  * Up is towards /.
1079  *
1080  * Return 1 if we went up a level and 0 if we were already at the
1081  * root.
1082  */
1083 int follow_up(struct path *path)
1084 {
1085         struct mount *mnt = real_mount(path->mnt);
1086         struct mount *parent;
1087         struct dentry *mountpoint;
1088
1089         read_seqlock_excl(&mount_lock);
1090         parent = mnt->mnt_parent;
1091         if (parent == mnt) {
1092                 read_sequnlock_excl(&mount_lock);
1093                 return 0;
1094         }
1095         mntget(&parent->mnt);
1096         mountpoint = dget(mnt->mnt_mountpoint);
1097         read_sequnlock_excl(&mount_lock);
1098         dput(path->dentry);
1099         path->dentry = mountpoint;
1100         mntput(path->mnt);
1101         path->mnt = &parent->mnt;
1102         return 1;
1103 }
1104 EXPORT_SYMBOL(follow_up);
1105
1106 /*
1107  * Perform an automount
1108  * - return -EISDIR to tell follow_managed() to stop and return the path we
1109  *   were called with.
1110  */
1111 static int follow_automount(struct path *path, struct nameidata *nd,
1112                             bool *need_mntput)
1113 {
1114         struct vfsmount *mnt;
1115         int err;
1116
1117         if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1118                 return -EREMOTE;
1119
1120         /* We don't want to mount if someone's just doing a stat -
1121          * unless they're stat'ing a directory and appended a '/' to
1122          * the name.
1123          *
1124          * We do, however, want to mount if someone wants to open or
1125          * create a file of any type under the mountpoint, wants to
1126          * traverse through the mountpoint or wants to open the
1127          * mounted directory.  Also, autofs may mark negative dentries
1128          * as being automount points.  These will need the attentions
1129          * of the daemon to instantiate them before they can be used.
1130          */
1131         if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
1132                            LOOKUP_OPEN | LOOKUP_CREATE |
1133                            LOOKUP_AUTOMOUNT))) {
1134                 /* Positive dentry that isn't meant to trigger an
1135                  * automount, EISDIR will allow it to be used,
1136                  * otherwise there's no mount here "now" so return
1137                  * ENOENT.
1138                  */
1139                 if (path->dentry->d_inode)
1140                         return -EISDIR;
1141                 else
1142                         return -ENOENT;
1143         }
1144
1145         nd->total_link_count++;
1146         if (nd->total_link_count >= 40)
1147                 return -ELOOP;
1148
1149         mnt = path->dentry->d_op->d_automount(path);
1150         if (IS_ERR(mnt)) {
1151                 /*
1152                  * The filesystem is allowed to return -EISDIR here to indicate
1153                  * it doesn't want to automount.  For instance, autofs would do
1154                  * this so that its userspace daemon can mount on this dentry.
1155                  *
1156                  * However, we can only permit this if it's a terminal point in
1157                  * the path being looked up; if it wasn't then the remainder of
1158                  * the path is inaccessible and we should say so.
1159                  */
1160                 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
1161                         return -EREMOTE;
1162                 return PTR_ERR(mnt);
1163         }
1164
1165         if (!mnt) /* mount collision */
1166                 return 0;
1167
1168         if (!*need_mntput) {
1169                 /* lock_mount() may release path->mnt on error */
1170                 mntget(path->mnt);
1171                 *need_mntput = true;
1172         }
1173         err = finish_automount(mnt, path);
1174
1175         switch (err) {
1176         case -EBUSY:
1177                 /* Someone else made a mount here whilst we were busy */
1178                 return 0;
1179         case 0:
1180                 path_put(path);
1181                 path->mnt = mnt;
1182                 path->dentry = dget(mnt->mnt_root);
1183                 return 0;
1184         default:
1185                 return err;
1186         }
1187
1188 }
1189
1190 /*
1191  * Handle a dentry that is managed in some way.
1192  * - Flagged for transit management (autofs)
1193  * - Flagged as mountpoint
1194  * - Flagged as automount point
1195  *
1196  * This may only be called in refwalk mode.
1197  *
1198  * Serialization is taken care of in namespace.c
1199  */
1200 static int follow_managed(struct path *path, struct nameidata *nd)
1201 {
1202         struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
1203         unsigned managed;
1204         bool need_mntput = false;
1205         int ret = 0;
1206
1207         /* Given that we're not holding a lock here, we retain the value in a
1208          * local variable for each dentry as we look at it so that we don't see
1209          * the components of that value change under us */
1210         while (managed = READ_ONCE(path->dentry->d_flags),
1211                managed &= DCACHE_MANAGED_DENTRY,
1212                unlikely(managed != 0)) {
1213                 /* Allow the filesystem to manage the transit without i_mutex
1214                  * being held. */
1215                 if (managed & DCACHE_MANAGE_TRANSIT) {
1216                         BUG_ON(!path->dentry->d_op);
1217                         BUG_ON(!path->dentry->d_op->d_manage);
1218                         ret = path->dentry->d_op->d_manage(path, false);
1219                         if (ret < 0)
1220                                 break;
1221                 }
1222
1223                 /* Transit to a mounted filesystem. */
1224                 if (managed & DCACHE_MOUNTED) {
1225                         struct vfsmount *mounted = lookup_mnt(path);
1226                         if (mounted) {
1227                                 dput(path->dentry);
1228                                 if (need_mntput)
1229                                         mntput(path->mnt);
1230                                 path->mnt = mounted;
1231                                 path->dentry = dget(mounted->mnt_root);
1232                                 need_mntput = true;
1233                                 continue;
1234                         }
1235
1236                         /* Something is mounted on this dentry in another
1237                          * namespace and/or whatever was mounted there in this
1238                          * namespace got unmounted before lookup_mnt() could
1239                          * get it */
1240                 }
1241
1242                 /* Handle an automount point */
1243                 if (managed & DCACHE_NEED_AUTOMOUNT) {
1244                         ret = follow_automount(path, nd, &need_mntput);
1245                         if (ret < 0)
1246                                 break;
1247                         continue;
1248                 }
1249
1250                 /* We didn't change the current path point */
1251                 break;
1252         }
1253
1254         if (need_mntput && path->mnt == mnt)
1255                 mntput(path->mnt);
1256         if (ret == -EISDIR || !ret)
1257                 ret = 1;
1258         if (need_mntput)
1259                 nd->flags |= LOOKUP_JUMPED;
1260         if (unlikely(ret < 0))
1261                 path_put_conditional(path, nd);
1262         return ret;
1263 }
1264
1265 int follow_down_one(struct path *path)
1266 {
1267         struct vfsmount *mounted;
1268
1269         mounted = lookup_mnt(path);
1270         if (mounted) {
1271                 dput(path->dentry);
1272                 mntput(path->mnt);
1273                 path->mnt = mounted;
1274                 path->dentry = dget(mounted->mnt_root);
1275                 return 1;
1276         }
1277         return 0;
1278 }
1279 EXPORT_SYMBOL(follow_down_one);
1280
1281 static inline int managed_dentry_rcu(const struct path *path)
1282 {
1283         return (path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1284                 path->dentry->d_op->d_manage(path, true) : 0;
1285 }
1286
1287 /*
1288  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
1289  * we meet a managed dentry that would need blocking.
1290  */
1291 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1292                                struct inode **inode, unsigned *seqp)
1293 {
1294         for (;;) {
1295                 struct mount *mounted;
1296                 /*
1297                  * Don't forget we might have a non-mountpoint managed dentry
1298                  * that wants to block transit.
1299                  */
1300                 switch (managed_dentry_rcu(path)) {
1301                 case -ECHILD:
1302                 default:
1303                         return false;
1304                 case -EISDIR:
1305                         return true;
1306                 case 0:
1307                         break;
1308                 }
1309
1310                 if (!d_mountpoint(path->dentry))
1311                         return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1312
1313                 mounted = __lookup_mnt(path->mnt, path->dentry);
1314                 if (!mounted)
1315                         break;
1316                 path->mnt = &mounted->mnt;
1317                 path->dentry = mounted->mnt.mnt_root;
1318                 nd->flags |= LOOKUP_JUMPED;
1319                 *seqp = read_seqcount_begin(&path->dentry->d_seq);
1320                 /*
1321                  * Update the inode too. We don't need to re-check the
1322                  * dentry sequence number here after this d_inode read,
1323                  * because a mount-point is always pinned.
1324                  */
1325                 *inode = path->dentry->d_inode;
1326         }
1327         return !read_seqretry(&mount_lock, nd->m_seq) &&
1328                 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1329 }
1330
1331 static int follow_dotdot_rcu(struct nameidata *nd)
1332 {
1333         struct inode *inode = nd->inode;
1334
1335         while (1) {
1336                 if (path_equal(&nd->path, &nd->root))
1337                         break;
1338                 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1339                         struct dentry *old = nd->path.dentry;
1340                         struct dentry *parent = old->d_parent;
1341                         unsigned seq;
1342
1343                         inode = parent->d_inode;
1344                         seq = read_seqcount_begin(&parent->d_seq);
1345                         if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1346                                 return -ECHILD;
1347                         nd->path.dentry = parent;
1348                         nd->seq = seq;
1349                         if (unlikely(!path_connected(&nd->path)))
1350                                 return -ENOENT;
1351                         break;
1352                 } else {
1353                         struct mount *mnt = real_mount(nd->path.mnt);
1354                         struct mount *mparent = mnt->mnt_parent;
1355                         struct dentry *mountpoint = mnt->mnt_mountpoint;
1356                         struct inode *inode2 = mountpoint->d_inode;
1357                         unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1358                         if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1359                                 return -ECHILD;
1360                         if (&mparent->mnt == nd->path.mnt)
1361                                 break;
1362                         /* we know that mountpoint was pinned */
1363                         nd->path.dentry = mountpoint;
1364                         nd->path.mnt = &mparent->mnt;
1365                         inode = inode2;
1366                         nd->seq = seq;
1367                 }
1368         }
1369         while (unlikely(d_mountpoint(nd->path.dentry))) {
1370                 struct mount *mounted;
1371                 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1372                 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1373                         return -ECHILD;
1374                 if (!mounted)
1375                         break;
1376                 nd->path.mnt = &mounted->mnt;
1377                 nd->path.dentry = mounted->mnt.mnt_root;
1378                 inode = nd->path.dentry->d_inode;
1379                 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1380         }
1381         nd->inode = inode;
1382         return 0;
1383 }
1384
1385 /*
1386  * Follow down to the covering mount currently visible to userspace.  At each
1387  * point, the filesystem owning that dentry may be queried as to whether the
1388  * caller is permitted to proceed or not.
1389  */
1390 int follow_down(struct path *path)
1391 {
1392         unsigned managed;
1393         int ret;
1394
1395         while (managed = READ_ONCE(path->dentry->d_flags),
1396                unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1397                 /* Allow the filesystem to manage the transit without i_mutex
1398                  * being held.
1399                  *
1400                  * We indicate to the filesystem if someone is trying to mount
1401                  * something here.  This gives autofs the chance to deny anyone
1402                  * other than its daemon the right to mount on its
1403                  * superstructure.
1404                  *
1405                  * The filesystem may sleep at this point.
1406                  */
1407                 if (managed & DCACHE_MANAGE_TRANSIT) {
1408                         BUG_ON(!path->dentry->d_op);
1409                         BUG_ON(!path->dentry->d_op->d_manage);
1410                         ret = path->dentry->d_op->d_manage(path, false);
1411                         if (ret < 0)
1412                                 return ret == -EISDIR ? 0 : ret;
1413                 }
1414
1415                 /* Transit to a mounted filesystem. */
1416                 if (managed & DCACHE_MOUNTED) {
1417                         struct vfsmount *mounted = lookup_mnt(path);
1418                         if (!mounted)
1419                                 break;
1420                         dput(path->dentry);
1421                         mntput(path->mnt);
1422                         path->mnt = mounted;
1423                         path->dentry = dget(mounted->mnt_root);
1424                         continue;
1425                 }
1426
1427                 /* Don't handle automount points here */
1428                 break;
1429         }
1430         return 0;
1431 }
1432 EXPORT_SYMBOL(follow_down);
1433
1434 /*
1435  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1436  */
1437 static void follow_mount(struct path *path)
1438 {
1439         while (d_mountpoint(path->dentry)) {
1440                 struct vfsmount *mounted = lookup_mnt(path);
1441                 if (!mounted)
1442                         break;
1443                 dput(path->dentry);
1444                 mntput(path->mnt);
1445                 path->mnt = mounted;
1446                 path->dentry = dget(mounted->mnt_root);
1447         }
1448 }
1449
1450 static int path_parent_directory(struct path *path)
1451 {
1452         struct dentry *old = path->dentry;
1453         /* rare case of legitimate dget_parent()... */
1454         path->dentry = dget_parent(path->dentry);
1455         dput(old);
1456         if (unlikely(!path_connected(path)))
1457                 return -ENOENT;
1458         return 0;
1459 }
1460
1461 static int follow_dotdot(struct nameidata *nd)
1462 {
1463         while(1) {
1464                 if (nd->path.dentry == nd->root.dentry &&
1465                     nd->path.mnt == nd->root.mnt) {
1466                         break;
1467                 }
1468                 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1469                         int ret = path_parent_directory(&nd->path);
1470                         if (ret)
1471                                 return ret;
1472                         break;
1473                 }
1474                 if (!follow_up(&nd->path))
1475                         break;
1476         }
1477         follow_mount(&nd->path);
1478         nd->inode = nd->path.dentry->d_inode;
1479         return 0;
1480 }
1481
1482 /*
1483  * This looks up the name in dcache and possibly revalidates the found dentry.
1484  * NULL is returned if the dentry does not exist in the cache.
1485  */
1486 static struct dentry *lookup_dcache(const struct qstr *name,
1487                                     struct dentry *dir,
1488                                     unsigned int flags)
1489 {
1490         struct dentry *dentry = d_lookup(dir, name);
1491         if (dentry) {
1492                 int error = d_revalidate(dentry, flags);
1493                 if (unlikely(error <= 0)) {
1494                         if (!error)
1495                                 d_invalidate(dentry);
1496                         dput(dentry);
1497                         return ERR_PTR(error);
1498                 }
1499         }
1500         return dentry;
1501 }
1502
1503 /*
1504  * Call i_op->lookup on the dentry.  The dentry must be negative and
1505  * unhashed.
1506  *
1507  * dir->d_inode->i_mutex must be held
1508  */
1509 static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1510                                   unsigned int flags)
1511 {
1512         struct dentry *old;
1513
1514         /* Don't create child dentry for a dead directory. */
1515         if (unlikely(IS_DEADDIR(dir))) {
1516                 dput(dentry);
1517                 return ERR_PTR(-ENOENT);
1518         }
1519
1520         old = dir->i_op->lookup(dir, dentry, flags);
1521         if (unlikely(old)) {
1522                 dput(dentry);
1523                 dentry = old;
1524         }
1525         return dentry;
1526 }
1527
1528 static struct dentry *__lookup_hash(const struct qstr *name,
1529                 struct dentry *base, unsigned int flags)
1530 {
1531         struct dentry *dentry = lookup_dcache(name, base, flags);
1532
1533         if (dentry)
1534                 return dentry;
1535
1536         dentry = d_alloc(base, name);
1537         if (unlikely(!dentry))
1538                 return ERR_PTR(-ENOMEM);
1539
1540         return lookup_real(base->d_inode, dentry, flags);
1541 }
1542
1543 static int lookup_fast(struct nameidata *nd,
1544                        struct path *path, struct inode **inode,
1545                        unsigned *seqp)
1546 {
1547         struct vfsmount *mnt = nd->path.mnt;
1548         struct dentry *dentry, *parent = nd->path.dentry;
1549         int status = 1;
1550         int err;
1551
1552         /*
1553          * Rename seqlock is not required here because in the off chance
1554          * of a false negative due to a concurrent rename, the caller is
1555          * going to fall back to non-racy lookup.
1556          */
1557         if (nd->flags & LOOKUP_RCU) {
1558                 unsigned seq;
1559                 bool negative;
1560                 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
1561                 if (unlikely(!dentry)) {
1562                         if (unlazy_walk(nd))
1563                                 return -ECHILD;
1564                         return 0;
1565                 }
1566
1567                 /*
1568                  * This sequence count validates that the inode matches
1569                  * the dentry name information from lookup.
1570                  */
1571                 *inode = d_backing_inode(dentry);
1572                 negative = d_is_negative(dentry);
1573                 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
1574                         return -ECHILD;
1575
1576                 /*
1577                  * This sequence count validates that the parent had no
1578                  * changes while we did the lookup of the dentry above.
1579                  *
1580                  * The memory barrier in read_seqcount_begin of child is
1581                  *  enough, we can use __read_seqcount_retry here.
1582                  */
1583                 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
1584                         return -ECHILD;
1585
1586                 *seqp = seq;
1587                 status = d_revalidate(dentry, nd->flags);
1588                 if (likely(status > 0)) {
1589                         /*
1590                          * Note: do negative dentry check after revalidation in
1591                          * case that drops it.
1592                          */
1593                         if (unlikely(negative))
1594                                 return -ENOENT;
1595                         path->mnt = mnt;
1596                         path->dentry = dentry;
1597                         if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
1598                                 return 1;
1599                 }
1600                 if (unlazy_child(nd, dentry, seq))
1601                         return -ECHILD;
1602                 if (unlikely(status == -ECHILD))
1603                         /* we'd been told to redo it in non-rcu mode */
1604                         status = d_revalidate(dentry, nd->flags);
1605         } else {
1606                 dentry = __d_lookup(parent, &nd->last);
1607                 if (unlikely(!dentry))
1608                         return 0;
1609                 status = d_revalidate(dentry, nd->flags);
1610         }
1611         if (unlikely(status <= 0)) {
1612                 if (!status)
1613                         d_invalidate(dentry);
1614                 dput(dentry);
1615                 return status;
1616         }
1617         if (unlikely(d_is_negative(dentry))) {
1618                 dput(dentry);
1619                 return -ENOENT;
1620         }
1621
1622         path->mnt = mnt;
1623         path->dentry = dentry;
1624         err = follow_managed(path, nd);
1625         if (likely(err > 0))
1626                 *inode = d_backing_inode(path->dentry);
1627         return err;
1628 }
1629
1630 /* Fast lookup failed, do it the slow way */
1631 static struct dentry *lookup_slow(const struct qstr *name,
1632                                   struct dentry *dir,
1633                                   unsigned int flags)
1634 {
1635         struct dentry *dentry = ERR_PTR(-ENOENT), *old;
1636         struct inode *inode = dir->d_inode;
1637         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1638
1639         inode_lock_shared(inode);
1640         /* Don't go there if it's already dead */
1641         if (unlikely(IS_DEADDIR(inode)))
1642                 goto out;
1643 again:
1644         dentry = d_alloc_parallel(dir, name, &wq);
1645         if (IS_ERR(dentry))
1646                 goto out;
1647         if (unlikely(!d_in_lookup(dentry))) {
1648                 if (!(flags & LOOKUP_NO_REVAL)) {
1649                         int error = d_revalidate(dentry, flags);
1650                         if (unlikely(error <= 0)) {
1651                                 if (!error) {
1652                                         d_invalidate(dentry);
1653                                         dput(dentry);
1654                                         goto again;
1655                                 }
1656                                 dput(dentry);
1657                                 dentry = ERR_PTR(error);
1658                         }
1659                 }
1660         } else {
1661                 old = inode->i_op->lookup(inode, dentry, flags);
1662                 d_lookup_done(dentry);
1663                 if (unlikely(old)) {
1664                         dput(dentry);
1665                         dentry = old;
1666                 }
1667         }
1668 out:
1669         inode_unlock_shared(inode);
1670         return dentry;
1671 }
1672
1673 static inline int may_lookup(struct nameidata *nd)
1674 {
1675         if (nd->flags & LOOKUP_RCU) {
1676                 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1677                 if (err != -ECHILD)
1678                         return err;
1679                 if (unlazy_walk(nd))
1680                         return -ECHILD;
1681         }
1682         return inode_permission(nd->inode, MAY_EXEC);
1683 }
1684
1685 static inline int handle_dots(struct nameidata *nd, int type)
1686 {
1687         if (type == LAST_DOTDOT) {
1688                 if (!nd->root.mnt)
1689                         set_root(nd);
1690                 if (nd->flags & LOOKUP_RCU) {
1691                         return follow_dotdot_rcu(nd);
1692                 } else
1693                         return follow_dotdot(nd);
1694         }
1695         return 0;
1696 }
1697
1698 static int pick_link(struct nameidata *nd, struct path *link,
1699                      struct inode *inode, unsigned seq)
1700 {
1701         int error;
1702         struct saved *last;
1703         if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
1704                 path_to_nameidata(link, nd);
1705                 return -ELOOP;
1706         }
1707         if (!(nd->flags & LOOKUP_RCU)) {
1708                 if (link->mnt == nd->path.mnt)
1709                         mntget(link->mnt);
1710         }
1711         error = nd_alloc_stack(nd);
1712         if (unlikely(error)) {
1713                 if (error == -ECHILD) {
1714                         if (unlikely(!legitimize_path(nd, link, seq))) {
1715                                 drop_links(nd);
1716                                 nd->depth = 0;
1717                                 nd->flags &= ~LOOKUP_RCU;
1718                                 nd->path.mnt = NULL;
1719                                 nd->path.dentry = NULL;
1720                                 if (!(nd->flags & LOOKUP_ROOT))
1721                                         nd->root.mnt = NULL;
1722                                 rcu_read_unlock();
1723                         } else if (likely(unlazy_walk(nd)) == 0)
1724                                 error = nd_alloc_stack(nd);
1725                 }
1726                 if (error) {
1727                         path_put(link);
1728                         return error;
1729                 }
1730         }
1731
1732         last = nd->stack + nd->depth++;
1733         last->link = *link;
1734         clear_delayed_call(&last->done);
1735         nd->link_inode = inode;
1736         last->seq = seq;
1737         return 1;
1738 }
1739
1740 enum {WALK_FOLLOW = 1, WALK_MORE = 2};
1741
1742 /*
1743  * Do we need to follow links? We _really_ want to be able
1744  * to do this check without having to look at inode->i_op,
1745  * so we keep a cache of "no, this doesn't need follow_link"
1746  * for the common case.
1747  */
1748 static inline int step_into(struct nameidata *nd, struct path *path,
1749                             int flags, struct inode *inode, unsigned seq)
1750 {
1751         if (!(flags & WALK_MORE) && nd->depth)
1752                 put_link(nd);
1753         if (likely(!d_is_symlink(path->dentry)) ||
1754            !(flags & WALK_FOLLOW || nd->flags & LOOKUP_FOLLOW)) {
1755                 /* not a symlink or should not follow */
1756                 path_to_nameidata(path, nd);
1757                 nd->inode = inode;
1758                 nd->seq = seq;
1759                 return 0;
1760         }
1761         /* make sure that d_is_symlink above matches inode */
1762         if (nd->flags & LOOKUP_RCU) {
1763                 if (read_seqcount_retry(&path->dentry->d_seq, seq))
1764                         return -ECHILD;
1765         }
1766         return pick_link(nd, path, inode, seq);
1767 }
1768
1769 static int walk_component(struct nameidata *nd, int flags)
1770 {
1771         struct path path;
1772         struct inode *inode;
1773         unsigned seq;
1774         int err;
1775         /*
1776          * "." and ".." are special - ".." especially so because it has
1777          * to be able to know about the current root directory and
1778          * parent relationships.
1779          */
1780         if (unlikely(nd->last_type != LAST_NORM)) {
1781                 err = handle_dots(nd, nd->last_type);
1782                 if (!(flags & WALK_MORE) && nd->depth)
1783                         put_link(nd);
1784                 return err;
1785         }
1786         err = lookup_fast(nd, &path, &inode, &seq);
1787         if (unlikely(err <= 0)) {
1788                 if (err < 0)
1789                         return err;
1790                 path.dentry = lookup_slow(&nd->last, nd->path.dentry,
1791                                           nd->flags);
1792                 if (IS_ERR(path.dentry))
1793                         return PTR_ERR(path.dentry);
1794
1795                 path.mnt = nd->path.mnt;
1796                 err = follow_managed(&path, nd);
1797                 if (unlikely(err < 0))
1798                         return err;
1799
1800                 if (unlikely(d_is_negative(path.dentry))) {
1801                         path_to_nameidata(&path, nd);
1802                         return -ENOENT;
1803                 }
1804
1805                 seq = 0;        /* we are already out of RCU mode */
1806                 inode = d_backing_inode(path.dentry);
1807         }
1808
1809         return step_into(nd, &path, flags, inode, seq);
1810 }
1811
1812 /*
1813  * We can do the critical dentry name comparison and hashing
1814  * operations one word at a time, but we are limited to:
1815  *
1816  * - Architectures with fast unaligned word accesses. We could
1817  *   do a "get_unaligned()" if this helps and is sufficiently
1818  *   fast.
1819  *
1820  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1821  *   do not trap on the (extremely unlikely) case of a page
1822  *   crossing operation.
1823  *
1824  * - Furthermore, we need an efficient 64-bit compile for the
1825  *   64-bit case in order to generate the "number of bytes in
1826  *   the final mask". Again, that could be replaced with a
1827  *   efficient population count instruction or similar.
1828  */
1829 #ifdef CONFIG_DCACHE_WORD_ACCESS
1830
1831 #include <asm/word-at-a-time.h>
1832
1833 #ifdef HASH_MIX
1834
1835 /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1836
1837 #elif defined(CONFIG_64BIT)
1838 /*
1839  * Register pressure in the mixing function is an issue, particularly
1840  * on 32-bit x86, but almost any function requires one state value and
1841  * one temporary.  Instead, use a function designed for two state values
1842  * and no temporaries.
1843  *
1844  * This function cannot create a collision in only two iterations, so
1845  * we have two iterations to achieve avalanche.  In those two iterations,
1846  * we have six layers of mixing, which is enough to spread one bit's
1847  * influence out to 2^6 = 64 state bits.
1848  *
1849  * Rotate constants are scored by considering either 64 one-bit input
1850  * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
1851  * probability of that delta causing a change to each of the 128 output
1852  * bits, using a sample of random initial states.
1853  *
1854  * The Shannon entropy of the computed probabilities is then summed
1855  * to produce a score.  Ideally, any input change has a 50% chance of
1856  * toggling any given output bit.
1857  *
1858  * Mixing scores (in bits) for (12,45):
1859  * Input delta: 1-bit      2-bit
1860  * 1 round:     713.3    42542.6
1861  * 2 rounds:   2753.7   140389.8
1862  * 3 rounds:   5954.1   233458.2
1863  * 4 rounds:   7862.6   256672.2
1864  * Perfect:    8192     258048
1865  *            (64*128) (64*63/2 * 128)
1866  */
1867 #define HASH_MIX(x, y, a)       \
1868         (       x ^= (a),       \
1869         y ^= x, x = rol64(x,12),\
1870         x += y, y = rol64(y,45),\
1871         y *= 9                  )
1872
1873 /*
1874  * Fold two longs into one 32-bit hash value.  This must be fast, but
1875  * latency isn't quite as critical, as there is a fair bit of additional
1876  * work done before the hash value is used.
1877  */
1878 static inline unsigned int fold_hash(unsigned long x, unsigned long y)
1879 {
1880         y ^= x * GOLDEN_RATIO_64;
1881         y *= GOLDEN_RATIO_64;
1882         return y >> 32;
1883 }
1884
1885 #else   /* 32-bit case */
1886
1887 /*
1888  * Mixing scores (in bits) for (7,20):
1889  * Input delta: 1-bit      2-bit
1890  * 1 round:     330.3     9201.6
1891  * 2 rounds:   1246.4    25475.4
1892  * 3 rounds:   1907.1    31295.1
1893  * 4 rounds:   2042.3    31718.6
1894  * Perfect:    2048      31744
1895  *            (32*64)   (32*31/2 * 64)
1896  */
1897 #define HASH_MIX(x, y, a)       \
1898         (       x ^= (a),       \
1899         y ^= x, x = rol32(x, 7),\
1900         x += y, y = rol32(y,20),\
1901         y *= 9                  )
1902
1903 static inline unsigned int fold_hash(unsigned long x, unsigned long y)
1904 {
1905         /* Use arch-optimized multiply if one exists */
1906         return __hash_32(y ^ __hash_32(x));
1907 }
1908
1909 #endif
1910
1911 /*
1912  * Return the hash of a string of known length.  This is carfully
1913  * designed to match hash_name(), which is the more critical function.
1914  * In particular, we must end by hashing a final word containing 0..7
1915  * payload bytes, to match the way that hash_name() iterates until it
1916  * finds the delimiter after the name.
1917  */
1918 unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
1919 {
1920         unsigned long a, x = 0, y = (unsigned long)salt;
1921
1922         for (;;) {
1923                 if (!len)
1924                         goto done;
1925                 a = load_unaligned_zeropad(name);
1926                 if (len < sizeof(unsigned long))
1927                         break;
1928                 HASH_MIX(x, y, a);
1929                 name += sizeof(unsigned long);
1930                 len -= sizeof(unsigned long);
1931         }
1932         x ^= a & bytemask_from_count(len);
1933 done:
1934         return fold_hash(x, y);
1935 }
1936 EXPORT_SYMBOL(full_name_hash);
1937
1938 /* Return the "hash_len" (hash and length) of a null-terminated string */
1939 u64 hashlen_string(const void *salt, const char *name)
1940 {
1941         unsigned long a = 0, x = 0, y = (unsigned long)salt;
1942         unsigned long adata, mask, len;
1943         const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1944
1945         len = 0;
1946         goto inside;
1947
1948         do {
1949                 HASH_MIX(x, y, a);
1950                 len += sizeof(unsigned long);
1951 inside:
1952                 a = load_unaligned_zeropad(name+len);
1953         } while (!has_zero(a, &adata, &constants));
1954
1955         adata = prep_zero_mask(a, adata, &constants);
1956         mask = create_zero_mask(adata);
1957         x ^= a & zero_bytemask(mask);
1958
1959         return hashlen_create(fold_hash(x, y), len + find_zero(mask));
1960 }
1961 EXPORT_SYMBOL(hashlen_string);
1962
1963 /*
1964  * Calculate the length and hash of the path component, and
1965  * return the "hash_len" as the result.
1966  */
1967 static inline u64 hash_name(const void *salt, const char *name)
1968 {
1969         unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
1970         unsigned long adata, bdata, mask, len;
1971         const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1972
1973         len = 0;
1974         goto inside;
1975
1976         do {
1977                 HASH_MIX(x, y, a);
1978                 len += sizeof(unsigned long);
1979 inside:
1980                 a = load_unaligned_zeropad(name+len);
1981                 b = a ^ REPEAT_BYTE('/');
1982         } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1983
1984         adata = prep_zero_mask(a, adata, &constants);
1985         bdata = prep_zero_mask(b, bdata, &constants);
1986         mask = create_zero_mask(adata | bdata);
1987         x ^= a & zero_bytemask(mask);
1988
1989         return hashlen_create(fold_hash(x, y), len + find_zero(mask));
1990 }
1991
1992 #else   /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
1993
1994 /* Return the hash of a string of known length */
1995 unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
1996 {
1997         unsigned long hash = init_name_hash(salt);
1998         while (len--)
1999                 hash = partial_name_hash((unsigned char)*name++, hash);
2000         return end_name_hash(hash);
2001 }
2002 EXPORT_SYMBOL(full_name_hash);
2003
2004 /* Return the "hash_len" (hash and length) of a null-terminated string */
2005 u64 hashlen_string(const void *salt, const char *name)
2006 {
2007         unsigned long hash = init_name_hash(salt);
2008         unsigned long len = 0, c;
2009
2010         c = (unsigned char)*name;
2011         while (c) {
2012                 len++;
2013                 hash = partial_name_hash(c, hash);
2014                 c = (unsigned char)name[len];
2015         }
2016         return hashlen_create(end_name_hash(hash), len);
2017 }
2018 EXPORT_SYMBOL(hashlen_string);
2019
2020 /*
2021  * We know there's a real path component here of at least
2022  * one character.
2023  */
2024 static inline u64 hash_name(const void *salt, const char *name)
2025 {
2026         unsigned long hash = init_name_hash(salt);
2027         unsigned long len = 0, c;
2028
2029         c = (unsigned char)*name;
2030         do {
2031                 len++;
2032                 hash = partial_name_hash(c, hash);
2033                 c = (unsigned char)name[len];
2034         } while (c && c != '/');
2035         return hashlen_create(end_name_hash(hash), len);
2036 }
2037
2038 #endif
2039
2040 /*
2041  * Name resolution.
2042  * This is the basic name resolution function, turning a pathname into
2043  * the final dentry. We expect 'base' to be positive and a directory.
2044  *
2045  * Returns 0 and nd will have valid dentry and mnt on success.
2046  * Returns error and drops reference to input namei data on failure.
2047  */
2048 static int link_path_walk(const char *name, struct nameidata *nd)
2049 {
2050         int err;
2051
2052         while (*name=='/')
2053                 name++;
2054         if (!*name)
2055                 return 0;
2056
2057         /* At this point we know we have a real path component. */
2058         for(;;) {
2059                 u64 hash_len;
2060                 int type;
2061
2062                 err = may_lookup(nd);
2063                 if (err)
2064                         return err;
2065
2066                 hash_len = hash_name(nd->path.dentry, name);
2067
2068                 type = LAST_NORM;
2069                 if (name[0] == '.') switch (hashlen_len(hash_len)) {
2070                         case 2:
2071                                 if (name[1] == '.') {
2072                                         type = LAST_DOTDOT;
2073                                         nd->flags |= LOOKUP_JUMPED;
2074                                 }
2075                                 break;
2076                         case 1:
2077                                 type = LAST_DOT;
2078                 }
2079                 if (likely(type == LAST_NORM)) {
2080                         struct dentry *parent = nd->path.dentry;
2081                         nd->flags &= ~LOOKUP_JUMPED;
2082                         if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
2083                                 struct qstr this = { { .hash_len = hash_len }, .name = name };
2084                                 err = parent->d_op->d_hash(parent, &this);
2085                                 if (err < 0)
2086                                         return err;
2087                                 hash_len = this.hash_len;
2088                                 name = this.name;
2089                         }
2090                 }
2091
2092                 nd->last.hash_len = hash_len;
2093                 nd->last.name = name;
2094                 nd->last_type = type;
2095
2096                 name += hashlen_len(hash_len);
2097                 if (!*name)
2098                         goto OK;
2099                 /*
2100                  * If it wasn't NUL, we know it was '/'. Skip that
2101                  * slash, and continue until no more slashes.
2102                  */
2103                 do {
2104                         name++;
2105                 } while (unlikely(*name == '/'));
2106                 if (unlikely(!*name)) {
2107 OK:
2108                         /* pathname body, done */
2109                         if (!nd->depth)
2110                                 return 0;
2111                         name = nd->stack[nd->depth - 1].name;
2112                         /* trailing symlink, done */
2113                         if (!name)
2114                                 return 0;
2115                         /* last component of nested symlink */
2116                         err = walk_component(nd, WALK_FOLLOW);
2117                 } else {
2118                         /* not the last component */
2119                         err = walk_component(nd, WALK_FOLLOW | WALK_MORE);
2120                 }
2121                 if (err < 0)
2122                         return err;
2123
2124                 if (err) {
2125                         const char *s = get_link(nd);
2126
2127                         if (IS_ERR(s))
2128                                 return PTR_ERR(s);
2129                         err = 0;
2130                         if (unlikely(!s)) {
2131                                 /* jumped */
2132                                 put_link(nd);
2133                         } else {
2134                                 nd->stack[nd->depth - 1].name = name;
2135                                 name = s;
2136                                 continue;
2137                         }
2138                 }
2139                 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2140                         if (nd->flags & LOOKUP_RCU) {
2141                                 if (unlazy_walk(nd))
2142                                         return -ECHILD;
2143                         }
2144                         return -ENOTDIR;
2145                 }
2146         }
2147 }
2148
2149 static const char *path_init(struct nameidata *nd, unsigned flags)
2150 {
2151         const char *s = nd->name->name;
2152
2153         if (!*s)
2154                 flags &= ~LOOKUP_RCU;
2155
2156         nd->last_type = LAST_ROOT; /* if there are only slashes... */
2157         nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
2158         nd->depth = 0;
2159         if (flags & LOOKUP_ROOT) {
2160                 struct dentry *root = nd->root.dentry;
2161                 struct inode *inode = root->d_inode;
2162                 if (*s && unlikely(!d_can_lookup(root)))
2163                         return ERR_PTR(-ENOTDIR);
2164                 nd->path = nd->root;
2165                 nd->inode = inode;
2166                 if (flags & LOOKUP_RCU) {
2167                         rcu_read_lock();
2168                         nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2169                         nd->root_seq = nd->seq;
2170                         nd->m_seq = read_seqbegin(&mount_lock);
2171                 } else {
2172                         path_get(&nd->path);
2173                 }
2174                 return s;
2175         }
2176
2177         nd->root.mnt = NULL;
2178         nd->path.mnt = NULL;
2179         nd->path.dentry = NULL;
2180
2181         nd->m_seq = read_seqbegin(&mount_lock);
2182         if (*s == '/') {
2183                 if (flags & LOOKUP_RCU)
2184                         rcu_read_lock();
2185                 set_root(nd);
2186                 if (likely(!nd_jump_root(nd)))
2187                         return s;
2188                 nd->root.mnt = NULL;
2189                 rcu_read_unlock();
2190                 return ERR_PTR(-ECHILD);
2191         } else if (nd->dfd == AT_FDCWD) {
2192                 if (flags & LOOKUP_RCU) {
2193                         struct fs_struct *fs = current->fs;
2194                         unsigned seq;
2195
2196                         rcu_read_lock();
2197
2198                         do {
2199                                 seq = read_seqcount_begin(&fs->seq);
2200                                 nd->path = fs->pwd;
2201                                 nd->inode = nd->path.dentry->d_inode;
2202                                 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2203                         } while (read_seqcount_retry(&fs->seq, seq));
2204                 } else {
2205                         get_fs_pwd(current->fs, &nd->path);
2206                         nd->inode = nd->path.dentry->d_inode;
2207                 }
2208                 return s;
2209         } else {
2210                 /* Caller must check execute permissions on the starting path component */
2211                 struct fd f = fdget_raw(nd->dfd);
2212                 struct dentry *dentry;
2213
2214                 if (!f.file)
2215                         return ERR_PTR(-EBADF);
2216
2217                 dentry = f.file->f_path.dentry;
2218
2219                 if (*s) {
2220                         if (!d_can_lookup(dentry)) {
2221                                 fdput(f);
2222                                 return ERR_PTR(-ENOTDIR);
2223                         }
2224                 }
2225
2226                 nd->path = f.file->f_path;
2227                 if (flags & LOOKUP_RCU) {
2228                         rcu_read_lock();
2229                         nd->inode = nd->path.dentry->d_inode;
2230                         nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
2231                 } else {
2232                         path_get(&nd->path);
2233                         nd->inode = nd->path.dentry->d_inode;
2234                 }
2235                 fdput(f);
2236                 return s;
2237         }
2238 }
2239
2240 static const char *trailing_symlink(struct nameidata *nd)
2241 {
2242         const char *s;
2243         int error = may_follow_link(nd);
2244         if (unlikely(error))
2245                 return ERR_PTR(error);
2246         nd->flags |= LOOKUP_PARENT;
2247         nd->stack[0].name = NULL;
2248         s = get_link(nd);
2249         return s ? s : "";
2250 }
2251
2252 static inline int lookup_last(struct nameidata *nd)
2253 {
2254         if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2255                 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2256
2257         nd->flags &= ~LOOKUP_PARENT;
2258         return walk_component(nd, 0);
2259 }
2260
2261 static int handle_lookup_down(struct nameidata *nd)
2262 {
2263         struct path path = nd->path;
2264         struct inode *inode = nd->inode;
2265         unsigned seq = nd->seq;
2266         int err;
2267
2268         if (nd->flags & LOOKUP_RCU) {
2269                 /*
2270                  * don't bother with unlazy_walk on failure - we are
2271                  * at the very beginning of walk, so we lose nothing
2272                  * if we simply redo everything in non-RCU mode
2273                  */
2274                 if (unlikely(!__follow_mount_rcu(nd, &path, &inode, &seq)))
2275                         return -ECHILD;
2276         } else {
2277                 dget(path.dentry);
2278                 err = follow_managed(&path, nd);
2279                 if (unlikely(err < 0))
2280                         return err;
2281                 inode = d_backing_inode(path.dentry);
2282                 seq = 0;
2283         }
2284         path_to_nameidata(&path, nd);
2285         nd->inode = inode;
2286         nd->seq = seq;
2287         return 0;
2288 }
2289
2290 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2291 static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
2292 {
2293         const char *s = path_init(nd, flags);
2294         int err;
2295
2296         if (IS_ERR(s))
2297                 return PTR_ERR(s);
2298
2299         if (unlikely(flags & LOOKUP_DOWN)) {
2300                 err = handle_lookup_down(nd);
2301                 if (unlikely(err < 0)) {
2302                         terminate_walk(nd);
2303                         return err;
2304                 }
2305         }
2306
2307         while (!(err = link_path_walk(s, nd))
2308                 && ((err = lookup_last(nd)) > 0)) {
2309                 s = trailing_symlink(nd);
2310                 if (IS_ERR(s)) {
2311                         err = PTR_ERR(s);
2312                         break;
2313                 }
2314         }
2315         if (!err)
2316                 err = complete_walk(nd);
2317
2318         if (!err && nd->flags & LOOKUP_DIRECTORY)
2319                 if (!d_can_lookup(nd->path.dentry))
2320                         err = -ENOTDIR;
2321         if (!err) {
2322                 *path = nd->path;
2323                 nd->path.mnt = NULL;
2324                 nd->path.dentry = NULL;
2325         }
2326         terminate_walk(nd);
2327         return err;
2328 }
2329
2330 static int filename_lookup(int dfd, struct filename *name, unsigned flags,
2331                            struct path *path, struct path *root)
2332 {
2333         int retval;
2334         struct nameidata nd;
2335         if (IS_ERR(name))
2336                 return PTR_ERR(name);
2337         if (unlikely(root)) {
2338                 nd.root = *root;
2339                 flags |= LOOKUP_ROOT;
2340         }
2341         set_nameidata(&nd, dfd, name);
2342         retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
2343         if (unlikely(retval == -ECHILD))
2344                 retval = path_lookupat(&nd, flags, path);
2345         if (unlikely(retval == -ESTALE))
2346                 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
2347
2348         if (likely(!retval))
2349                 audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
2350         restore_nameidata();
2351         putname(name);
2352         return retval;
2353 }
2354
2355 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2356 static int path_parentat(struct nameidata *nd, unsigned flags,
2357                                 struct path *parent)
2358 {
2359         const char *s = path_init(nd, flags);
2360         int err;
2361         if (IS_ERR(s))
2362                 return PTR_ERR(s);
2363         err = link_path_walk(s, nd);
2364         if (!err)
2365                 err = complete_walk(nd);
2366         if (!err) {
2367                 *parent = nd->path;
2368                 nd->path.mnt = NULL;
2369                 nd->path.dentry = NULL;
2370         }
2371         terminate_walk(nd);
2372         return err;
2373 }
2374
2375 static struct filename *filename_parentat(int dfd, struct filename *name,
2376                                 unsigned int flags, struct path *parent,
2377                                 struct qstr *last, int *type)
2378 {
2379         int retval;
2380         struct nameidata nd;
2381
2382         if (IS_ERR(name))
2383                 return name;
2384         set_nameidata(&nd, dfd, name);
2385         retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
2386         if (unlikely(retval == -ECHILD))
2387                 retval = path_parentat(&nd, flags, parent);
2388         if (unlikely(retval == -ESTALE))
2389                 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
2390         if (likely(!retval)) {
2391                 *last = nd.last;
2392                 *type = nd.last_type;
2393                 audit_inode(name, parent->dentry, LOOKUP_PARENT);
2394         } else {
2395                 putname(name);
2396                 name = ERR_PTR(retval);
2397         }
2398         restore_nameidata();
2399         return name;
2400 }
2401
2402 /* does lookup, returns the object with parent locked */
2403 struct dentry *kern_path_locked(const char *name, struct path *path)
2404 {
2405         struct filename *filename;
2406         struct dentry *d;
2407         struct qstr last;
2408         int type;
2409
2410         filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2411                                     &last, &type);
2412         if (IS_ERR(filename))
2413                 return ERR_CAST(filename);
2414         if (unlikely(type != LAST_NORM)) {
2415                 path_put(path);
2416                 putname(filename);
2417                 return ERR_PTR(-EINVAL);
2418         }
2419         inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
2420         d = __lookup_hash(&last, path->dentry, 0);
2421         if (IS_ERR(d)) {
2422                 inode_unlock(path->dentry->d_inode);
2423                 path_put(path);
2424         }
2425         putname(filename);
2426         return d;
2427 }
2428
2429 int kern_path(const char *name, unsigned int flags, struct path *path)
2430 {
2431         return filename_lookup(AT_FDCWD, getname_kernel(name),
2432                                flags, path, NULL);
2433 }
2434 EXPORT_SYMBOL(kern_path);
2435
2436 /**
2437  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2438  * @dentry:  pointer to dentry of the base directory
2439  * @mnt: pointer to vfs mount of the base directory
2440  * @name: pointer to file name
2441  * @flags: lookup flags
2442  * @path: pointer to struct path to fill
2443  */
2444 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2445                     const char *name, unsigned int flags,
2446                     struct path *path)
2447 {
2448         struct path root = {.mnt = mnt, .dentry = dentry};
2449         /* the first argument of filename_lookup() is ignored with root */
2450         return filename_lookup(AT_FDCWD, getname_kernel(name),
2451                                flags , path, &root);
2452 }
2453 EXPORT_SYMBOL(vfs_path_lookup);
2454
2455 /**
2456  * lookup_one_len - filesystem helper to lookup single pathname component
2457  * @name:       pathname component to lookup
2458  * @base:       base directory to lookup from
2459  * @len:        maximum length @len should be interpreted to
2460  *
2461  * Note that this routine is purely a helper for filesystem usage and should
2462  * not be called by generic code.
2463  *
2464  * The caller must hold base->i_mutex.
2465  */
2466 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2467 {
2468         struct qstr this;
2469         unsigned int c;
2470         int err;
2471
2472         WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2473
2474         this.name = name;
2475         this.len = len;
2476         this.hash = full_name_hash(base, name, len);
2477         if (!len)
2478                 return ERR_PTR(-EACCES);
2479
2480         if (unlikely(name[0] == '.')) {
2481                 if (len < 2 || (len == 2 && name[1] == '.'))
2482                         return ERR_PTR(-EACCES);
2483         }
2484
2485         while (len--) {
2486                 c = *(const unsigned char *)name++;
2487                 if (c == '/' || c == '\0')
2488                         return ERR_PTR(-EACCES);
2489         }
2490         /*
2491          * See if the low-level filesystem might want
2492          * to use its own hash..
2493          */
2494         if (base->d_flags & DCACHE_OP_HASH) {
2495                 int err = base->d_op->d_hash(base, &this);
2496                 if (err < 0)
2497                         return ERR_PTR(err);
2498         }
2499
2500         err = inode_permission(base->d_inode, MAY_EXEC);
2501         if (err)
2502                 return ERR_PTR(err);
2503
2504         return __lookup_hash(&this, base, 0);
2505 }
2506 EXPORT_SYMBOL(lookup_one_len);
2507
2508 /**
2509  * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2510  * @name:       pathname component to lookup
2511  * @base:       base directory to lookup from
2512  * @len:        maximum length @len should be interpreted to
2513  *
2514  * Note that this routine is purely a helper for filesystem usage and should
2515  * not be called by generic code.
2516  *
2517  * Unlike lookup_one_len, it should be called without the parent
2518  * i_mutex held, and will take the i_mutex itself if necessary.
2519  */
2520 struct dentry *lookup_one_len_unlocked(const char *name,
2521                                        struct dentry *base, int len)
2522 {
2523         struct qstr this;
2524         unsigned int c;
2525         int err;
2526         struct dentry *ret;
2527
2528         this.name = name;
2529         this.len = len;
2530         this.hash = full_name_hash(base, name, len);
2531         if (!len)
2532                 return ERR_PTR(-EACCES);
2533
2534         if (unlikely(name[0] == '.')) {
2535                 if (len < 2 || (len == 2 && name[1] == '.'))
2536                         return ERR_PTR(-EACCES);
2537         }
2538
2539         while (len--) {
2540                 c = *(const unsigned char *)name++;
2541                 if (c == '/' || c == '\0')
2542                         return ERR_PTR(-EACCES);
2543         }
2544         /*
2545          * See if the low-level filesystem might want
2546          * to use its own hash..
2547          */
2548         if (base->d_flags & DCACHE_OP_HASH) {
2549                 int err = base->d_op->d_hash(base, &this);
2550                 if (err < 0)
2551                         return ERR_PTR(err);
2552         }
2553
2554         err = inode_permission(base->d_inode, MAY_EXEC);
2555         if (err)
2556                 return ERR_PTR(err);
2557
2558         ret = lookup_dcache(&this, base, 0);
2559         if (!ret)
2560                 ret = lookup_slow(&this, base, 0);
2561         return ret;
2562 }
2563 EXPORT_SYMBOL(lookup_one_len_unlocked);
2564
2565 #ifdef CONFIG_UNIX98_PTYS
2566 int path_pts(struct path *path)
2567 {
2568         /* Find something mounted on "pts" in the same directory as
2569          * the input path.
2570          */
2571         struct dentry *child, *parent;
2572         struct qstr this;
2573         int ret;
2574
2575         ret = path_parent_directory(path);
2576         if (ret)
2577                 return ret;
2578
2579         parent = path->dentry;
2580         this.name = "pts";
2581         this.len = 3;
2582         child = d_hash_and_lookup(parent, &this);
2583         if (!child)
2584                 return -ENOENT;
2585
2586         path->dentry = child;
2587         dput(parent);
2588         follow_mount(path);
2589         return 0;
2590 }
2591 #endif
2592
2593 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2594                  struct path *path, int *empty)
2595 {
2596         return filename_lookup(dfd, getname_flags(name, flags, empty),
2597                                flags, path, NULL);
2598 }
2599 EXPORT_SYMBOL(user_path_at_empty);
2600
2601 /**
2602  * mountpoint_last - look up last component for umount
2603  * @nd:   pathwalk nameidata - currently pointing at parent directory of "last"
2604  *
2605  * This is a special lookup_last function just for umount. In this case, we
2606  * need to resolve the path without doing any revalidation.
2607  *
2608  * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2609  * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2610  * in almost all cases, this lookup will be served out of the dcache. The only
2611  * cases where it won't are if nd->last refers to a symlink or the path is
2612  * bogus and it doesn't exist.
2613  *
2614  * Returns:
2615  * -error: if there was an error during lookup. This includes -ENOENT if the
2616  *         lookup found a negative dentry.
2617  *
2618  * 0:      if we successfully resolved nd->last and found it to not to be a
2619  *         symlink that needs to be followed.
2620  *
2621  * 1:      if we successfully resolved nd->last and found it to be a symlink
2622  *         that needs to be followed.
2623  */
2624 static int
2625 mountpoint_last(struct nameidata *nd)
2626 {
2627         int error = 0;
2628         struct dentry *dir = nd->path.dentry;
2629         struct path path;
2630
2631         /* If we're in rcuwalk, drop out of it to handle last component */
2632         if (nd->flags & LOOKUP_RCU) {
2633                 if (unlazy_walk(nd))
2634                         return -ECHILD;
2635         }
2636
2637         nd->flags &= ~LOOKUP_PARENT;
2638
2639         if (unlikely(nd->last_type != LAST_NORM)) {
2640                 error = handle_dots(nd, nd->last_type);
2641                 if (error)
2642                         return error;
2643                 path.dentry = dget(nd->path.dentry);
2644         } else {
2645                 path.dentry = d_lookup(dir, &nd->last);
2646                 if (!path.dentry) {
2647                         /*
2648                          * No cached dentry. Mounted dentries are pinned in the
2649                          * cache, so that means that this dentry is probably
2650                          * a symlink or the path doesn't actually point
2651                          * to a mounted dentry.
2652                          */
2653                         path.dentry = lookup_slow(&nd->last, dir,
2654                                              nd->flags | LOOKUP_NO_REVAL);
2655                         if (IS_ERR(path.dentry))
2656                                 return PTR_ERR(path.dentry);
2657                 }
2658         }
2659         if (d_is_negative(path.dentry)) {
2660                 dput(path.dentry);
2661                 return -ENOENT;
2662         }
2663         path.mnt = nd->path.mnt;
2664         return step_into(nd, &path, 0, d_backing_inode(path.dentry), 0);
2665 }
2666
2667 /**
2668  * path_mountpoint - look up a path to be umounted
2669  * @nd:         lookup context
2670  * @flags:      lookup flags
2671  * @path:       pointer to container for result
2672  *
2673  * Look up the given name, but don't attempt to revalidate the last component.
2674  * Returns 0 and "path" will be valid on success; Returns error otherwise.
2675  */
2676 static int
2677 path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
2678 {
2679         const char *s = path_init(nd, flags);
2680         int err;
2681         if (IS_ERR(s))
2682                 return PTR_ERR(s);
2683         while (!(err = link_path_walk(s, nd)) &&
2684                 (err = mountpoint_last(nd)) > 0) {
2685                 s = trailing_symlink(nd);
2686                 if (IS_ERR(s)) {
2687                         err = PTR_ERR(s);
2688                         break;
2689                 }
2690         }
2691         if (!err) {
2692                 *path = nd->path;
2693                 nd->path.mnt = NULL;
2694                 nd->path.dentry = NULL;
2695                 follow_mount(path);
2696         }
2697         terminate_walk(nd);
2698         return err;
2699 }
2700
2701 static int
2702 filename_mountpoint(int dfd, struct filename *name, struct path *path,
2703                         unsigned int flags)
2704 {
2705         struct nameidata nd;
2706         int error;
2707         if (IS_ERR(name))
2708                 return PTR_ERR(name);
2709         set_nameidata(&nd, dfd, name);
2710         error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
2711         if (unlikely(error == -ECHILD))
2712                 error = path_mountpoint(&nd, flags, path);
2713         if (unlikely(error == -ESTALE))
2714                 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
2715         if (likely(!error))
2716                 audit_inode(name, path->dentry, 0);
2717         restore_nameidata();
2718         putname(name);
2719         return error;
2720 }
2721
2722 /**
2723  * user_path_mountpoint_at - lookup a path from userland in order to umount it
2724  * @dfd:        directory file descriptor
2725  * @name:       pathname from userland
2726  * @flags:      lookup flags
2727  * @path:       pointer to container to hold result
2728  *
2729  * A umount is a special case for path walking. We're not actually interested
2730  * in the inode in this situation, and ESTALE errors can be a problem. We
2731  * simply want track down the dentry and vfsmount attached at the mountpoint
2732  * and avoid revalidating the last component.
2733  *
2734  * Returns 0 and populates "path" on success.
2735  */
2736 int
2737 user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
2738                         struct path *path)
2739 {
2740         return filename_mountpoint(dfd, getname(name), path, flags);
2741 }
2742
2743 int
2744 kern_path_mountpoint(int dfd, const char *name, struct path *path,
2745                         unsigned int flags)
2746 {
2747         return filename_mountpoint(dfd, getname_kernel(name), path, flags);
2748 }
2749 EXPORT_SYMBOL(kern_path_mountpoint);
2750
2751 int __check_sticky(struct inode *dir, struct inode *inode)
2752 {
2753         kuid_t fsuid = current_fsuid();
2754
2755         if (uid_eq(inode->i_uid, fsuid))
2756                 return 0;
2757         if (uid_eq(dir->i_uid, fsuid))
2758                 return 0;
2759         return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
2760 }
2761 EXPORT_SYMBOL(__check_sticky);
2762
2763 /*
2764  *      Check whether we can remove a link victim from directory dir, check
2765  *  whether the type of victim is right.
2766  *  1. We can't do it if dir is read-only (done in permission())
2767  *  2. We should have write and exec permissions on dir
2768  *  3. We can't remove anything from append-only dir
2769  *  4. We can't do anything with immutable dir (done in permission())
2770  *  5. If the sticky bit on dir is set we should either
2771  *      a. be owner of dir, or
2772  *      b. be owner of victim, or
2773  *      c. have CAP_FOWNER capability
2774  *  6. If the victim is append-only or immutable we can't do antyhing with
2775  *     links pointing to it.
2776  *  7. If the victim has an unknown uid or gid we can't change the inode.
2777  *  8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2778  *  9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2779  * 10. We can't remove a root or mountpoint.
2780  * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
2781  *     nfs_async_unlink().
2782  */
2783 static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
2784 {
2785         struct inode *inode = d_backing_inode(victim);
2786         int error;
2787
2788         if (d_is_negative(victim))
2789                 return -ENOENT;
2790         BUG_ON(!inode);
2791
2792         BUG_ON(victim->d_parent->d_inode != dir);
2793         audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
2794
2795         error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
2796         if (error)
2797                 return error;
2798         if (IS_APPEND(dir))
2799                 return -EPERM;
2800
2801         if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2802             IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
2803                 return -EPERM;
2804         if (isdir) {
2805                 if (!d_is_dir(victim))
2806                         return -ENOTDIR;
2807                 if (IS_ROOT(victim))
2808                         return -EBUSY;
2809         } else if (d_is_dir(victim))
2810                 return -EISDIR;
2811         if (IS_DEADDIR(dir))
2812                 return -ENOENT;
2813         if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2814                 return -EBUSY;
2815         return 0;
2816 }
2817
2818 /*      Check whether we can create an object with dentry child in directory
2819  *  dir.
2820  *  1. We can't do it if child already exists (open has special treatment for
2821  *     this case, but since we are inlined it's OK)
2822  *  2. We can't do it if dir is read-only (done in permission())
2823  *  3. We can't do it if the fs can't represent the fsuid or fsgid.
2824  *  4. We should have write and exec permissions on dir
2825  *  5. We can't do it if dir is immutable (done in permission())
2826  */
2827 static inline int may_create(struct inode *dir, struct dentry *child)
2828 {
2829         struct user_namespace *s_user_ns;
2830         audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
2831         if (child->d_inode)
2832                 return -EEXIST;
2833         if (IS_DEADDIR(dir))
2834                 return -ENOENT;
2835         s_user_ns = dir->i_sb->s_user_ns;
2836         if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2837             !kgid_has_mapping(s_user_ns, current_fsgid()))
2838                 return -EOVERFLOW;
2839         return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2840 }
2841
2842 /*
2843  * p1 and p2 should be directories on the same fs.
2844  */
2845 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2846 {
2847         struct dentry *p;
2848
2849         if (p1 == p2) {
2850                 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2851                 return NULL;
2852         }
2853
2854         mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
2855
2856         p = d_ancestor(p2, p1);
2857         if (p) {
2858                 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2859                 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
2860                 return p;
2861         }
2862
2863         p = d_ancestor(p1, p2);
2864         if (p) {
2865                 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2866                 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
2867                 return p;
2868         }
2869
2870         inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2871         inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
2872         return NULL;
2873 }
2874 EXPORT_SYMBOL(lock_rename);
2875
2876 void unlock_rename(struct dentry *p1, struct dentry *p2)
2877 {
2878         inode_unlock(p1->d_inode);
2879         if (p1 != p2) {
2880                 inode_unlock(p2->d_inode);
2881                 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
2882         }
2883 }
2884 EXPORT_SYMBOL(unlock_rename);
2885
2886 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2887                 bool want_excl)
2888 {
2889         int error = may_create(dir, dentry);
2890         if (error)
2891                 return error;
2892
2893         if (!dir->i_op->create)
2894                 return -EACCES; /* shouldn't it be ENOSYS? */
2895         mode &= S_IALLUGO;
2896         mode |= S_IFREG;
2897         error = security_inode_create(dir, dentry, mode);
2898         if (error)
2899                 return error;
2900         error = dir->i_op->create(dir, dentry, mode, want_excl);
2901         if (!error)
2902                 fsnotify_create(dir, dentry);
2903         return error;
2904 }
2905 EXPORT_SYMBOL(vfs_create);
2906
2907 bool may_open_dev(const struct path *path)
2908 {
2909         return !(path->mnt->mnt_flags & MNT_NODEV) &&
2910                 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2911 }
2912
2913 static int may_open(const struct path *path, int acc_mode, int flag)
2914 {
2915         struct dentry *dentry = path->dentry;
2916         struct inode *inode = dentry->d_inode;
2917         int error;
2918
2919         if (!inode)
2920                 return -ENOENT;
2921
2922         switch (inode->i_mode & S_IFMT) {
2923         case S_IFLNK:
2924                 return -ELOOP;
2925         case S_IFDIR:
2926                 if (acc_mode & MAY_WRITE)
2927                         return -EISDIR;
2928                 break;
2929         case S_IFBLK:
2930         case S_IFCHR:
2931                 if (!may_open_dev(path))
2932                         return -EACCES;
2933                 /*FALLTHRU*/
2934         case S_IFIFO:
2935         case S_IFSOCK:
2936                 flag &= ~O_TRUNC;
2937                 break;
2938         }
2939
2940         error = inode_permission(inode, MAY_OPEN | acc_mode);
2941         if (error)
2942                 return error;
2943
2944         /*
2945          * An append-only file must be opened in append mode for writing.
2946          */
2947         if (IS_APPEND(inode)) {
2948                 if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2949                         return -EPERM;
2950                 if (flag & O_TRUNC)
2951                         return -EPERM;
2952         }
2953
2954         /* O_NOATIME can only be set by the owner or superuser */
2955         if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2956                 return -EPERM;
2957
2958         return 0;
2959 }
2960
2961 static int handle_truncate(struct file *filp)
2962 {
2963         const struct path *path = &filp->f_path;
2964         struct inode *inode = path->dentry->d_inode;
2965         int error = get_write_access(inode);
2966         if (error)
2967                 return error;
2968         /*
2969          * Refuse to truncate files with mandatory locks held on them.
2970          */
2971         error = locks_verify_locked(filp);
2972         if (!error)
2973                 error = security_path_truncate(path);
2974         if (!error) {
2975                 error = do_truncate(path->dentry, 0,
2976                                     ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2977                                     filp);
2978         }
2979         put_write_access(inode);
2980         return error;
2981 }
2982
2983 static inline int open_to_namei_flags(int flag)
2984 {
2985         if ((flag & O_ACCMODE) == 3)
2986                 flag--;
2987         return flag;
2988 }
2989
2990 static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
2991 {
2992         struct user_namespace *s_user_ns;
2993         int error = security_path_mknod(dir, dentry, mode, 0);
2994         if (error)
2995                 return error;
2996
2997         s_user_ns = dir->dentry->d_sb->s_user_ns;
2998         if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2999             !kgid_has_mapping(s_user_ns, current_fsgid()))
3000                 return -EOVERFLOW;
3001
3002         error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
3003         if (error)
3004                 return error;
3005
3006         return security_inode_create(dir->dentry->d_inode, dentry, mode);
3007 }
3008
3009 /*
3010  * Attempt to atomically look up, create and open a file from a negative
3011  * dentry.
3012  *
3013  * Returns 0 if successful.  The file will have been created and attached to
3014  * @file by the filesystem calling finish_open().
3015  *
3016  * Returns 1 if the file was looked up only or didn't need creating.  The
3017  * caller will need to perform the open themselves.  @path will have been
3018  * updated to point to the new dentry.  This may be negative.
3019  *
3020  * Returns an error code otherwise.
3021  */
3022 static int atomic_open(struct nameidata *nd, struct dentry *dentry,
3023                         struct path *path, struct file *file,
3024                         const struct open_flags *op,
3025                         int open_flag, umode_t mode,
3026                         int *opened)
3027 {
3028         struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
3029         struct inode *dir =  nd->path.dentry->d_inode;
3030         int error;
3031
3032         if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
3033                 open_flag &= ~O_TRUNC;
3034
3035         if (nd->flags & LOOKUP_DIRECTORY)
3036                 open_flag |= O_DIRECTORY;
3037
3038         file->f_path.dentry = DENTRY_NOT_SET;
3039         file->f_path.mnt = nd->path.mnt;
3040         error = dir->i_op->atomic_open(dir, dentry, file,
3041                                        open_to_namei_flags(open_flag),
3042                                        mode, opened);
3043         d_lookup_done(dentry);
3044         if (!error) {
3045                 /*
3046                  * We didn't have the inode before the open, so check open
3047                  * permission here.
3048                  */
3049                 int acc_mode = op->acc_mode;
3050                 if (*opened & FILE_CREATED) {
3051                         WARN_ON(!(open_flag & O_CREAT));
3052                         fsnotify_create(dir, dentry);
3053                         acc_mode = 0;
3054                 }
3055                 error = may_open(&file->f_path, acc_mode, open_flag);
3056                 if (WARN_ON(error > 0))
3057                         error = -EINVAL;
3058         } else if (error > 0) {
3059                 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
3060                         error = -EIO;
3061                 } else {
3062                         if (file->f_path.dentry) {
3063                                 dput(dentry);
3064                                 dentry = file->f_path.dentry;
3065                         }
3066                         if (*opened & FILE_CREATED)
3067                                 fsnotify_create(dir, dentry);
3068                         if (unlikely(d_is_negative(dentry))) {
3069                                 error = -ENOENT;
3070                         } else {
3071                                 path->dentry = dentry;
3072                                 path->mnt = nd->path.mnt;
3073                                 return 1;
3074                         }
3075                 }
3076         }
3077         dput(dentry);
3078         return error;
3079 }
3080
3081 /*
3082  * Look up and maybe create and open the last component.
3083  *
3084  * Must be called with i_mutex held on parent.
3085  *
3086  * Returns 0 if the file was successfully atomically created (if necessary) and
3087  * opened.  In this case the file will be returned attached to @file.
3088  *
3089  * Returns 1 if the file was not completely opened at this time, though lookups
3090  * and creations will have been performed and the dentry returned in @path will
3091  * be positive upon return if O_CREAT was specified.  If O_CREAT wasn't
3092  * specified then a negative dentry may be returned.
3093  *
3094  * An error code is returned otherwise.
3095  *
3096  * FILE_CREATE will be set in @*opened if the dentry was created and will be
3097  * cleared otherwise prior to returning.
3098  */
3099 static int lookup_open(struct nameidata *nd, struct path *path,
3100                         struct file *file,
3101                         const struct open_flags *op,
3102                         bool got_write, int *opened)
3103 {
3104         struct dentry *dir = nd->path.dentry;
3105         struct inode *dir_inode = dir->d_inode;
3106         int open_flag = op->open_flag;
3107         struct dentry *dentry;
3108         int error, create_error = 0;
3109         umode_t mode = op->mode;
3110         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
3111
3112         if (unlikely(IS_DEADDIR(dir_inode)))
3113                 return -ENOENT;
3114
3115         *opened &= ~FILE_CREATED;
3116         dentry = d_lookup(dir, &nd->last);
3117         for (;;) {
3118                 if (!dentry) {
3119                         dentry = d_alloc_parallel(dir, &nd->last, &wq);
3120                         if (IS_ERR(dentry))
3121                                 return PTR_ERR(dentry);
3122                 }
3123                 if (d_in_lookup(dentry))
3124                         break;
3125
3126                 error = d_revalidate(dentry, nd->flags);
3127                 if (likely(error > 0))
3128                         break;
3129                 if (error)
3130                         goto out_dput;
3131                 d_invalidate(dentry);
3132                 dput(dentry);
3133                 dentry = NULL;
3134         }
3135         if (dentry->d_inode) {
3136                 /* Cached positive dentry: will open in f_op->open */
3137                 goto out_no_open;
3138         }
3139
3140         /*
3141          * Checking write permission is tricky, bacuse we don't know if we are
3142          * going to actually need it: O_CREAT opens should work as long as the
3143          * file exists.  But checking existence breaks atomicity.  The trick is
3144          * to check access and if not granted clear O_CREAT from the flags.
3145          *
3146          * Another problem is returing the "right" error value (e.g. for an
3147          * O_EXCL open we want to return EEXIST not EROFS).
3148          */
3149         if (open_flag & O_CREAT) {
3150                 if (!IS_POSIXACL(dir->d_inode))
3151                         mode &= ~current_umask();
3152                 if (unlikely(!got_write)) {
3153                         create_error = -EROFS;
3154                         open_flag &= ~O_CREAT;
3155                         if (open_flag & (O_EXCL | O_TRUNC))
3156                                 goto no_open;
3157                         /* No side effects, safe to clear O_CREAT */
3158                 } else {
3159                         create_error = may_o_create(&nd->path, dentry, mode);
3160                         if (create_error) {
3161                                 open_flag &= ~O_CREAT;
3162                                 if (open_flag & O_EXCL)
3163                                         goto no_open;
3164                         }
3165                 }
3166         } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
3167                    unlikely(!got_write)) {
3168                 /*
3169                  * No O_CREATE -> atomicity not a requirement -> fall
3170                  * back to lookup + open
3171                  */
3172                 goto no_open;
3173         }
3174
3175         if (dir_inode->i_op->atomic_open) {
3176                 error = atomic_open(nd, dentry, path, file, op, open_flag,
3177                                     mode, opened);
3178                 if (unlikely(error == -ENOENT) && create_error)
3179                         error = create_error;
3180                 return error;
3181         }
3182
3183 no_open:
3184         if (d_in_lookup(dentry)) {
3185                 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3186                                                              nd->flags);
3187                 d_lookup_done(dentry);
3188                 if (unlikely(res)) {
3189                         if (IS_ERR(res)) {
3190                                 error = PTR_ERR(res);
3191                                 goto out_dput;
3192                         }
3193                         dput(dentry);
3194                         dentry = res;
3195                 }
3196         }
3197
3198         /* Negative dentry, just create the file */
3199         if (!dentry->d_inode && (open_flag & O_CREAT)) {
3200                 *opened |= FILE_CREATED;
3201                 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
3202                 if (!dir_inode->i_op->create) {
3203                         error = -EACCES;
3204                         goto out_dput;
3205                 }
3206                 error = dir_inode->i_op->create(dir_inode, dentry, mode,
3207                                                 open_flag & O_EXCL);
3208                 if (error)
3209                         goto out_dput;
3210                 fsnotify_create(dir_inode, dentry);
3211         }
3212         if (unlikely(create_error) && !dentry->d_inode) {
3213                 error = create_error;
3214                 goto out_dput;
3215         }
3216 out_no_open:
3217         path->dentry = dentry;
3218         path->mnt = nd->path.mnt;
3219         return 1;
3220
3221 out_dput:
3222         dput(dentry);
3223         return error;
3224 }
3225
3226 /*
3227  * Handle the last step of open()
3228  */
3229 static int do_last(struct nameidata *nd,
3230                    struct file *file, const struct open_flags *op,
3231                    int *opened)
3232 {
3233         struct dentry *dir = nd->path.dentry;
3234         int open_flag = op->open_flag;
3235         bool will_truncate = (open_flag & O_TRUNC) != 0;
3236         bool got_write = false;
3237         int acc_mode = op->acc_mode;
3238         unsigned seq;
3239         struct inode *inode;
3240         struct path path;
3241         int error;
3242
3243         nd->flags &= ~LOOKUP_PARENT;
3244         nd->flags |= op->intent;
3245
3246         if (nd->last_type != LAST_NORM) {
3247                 error = handle_dots(nd, nd->last_type);
3248                 if (unlikely(error))
3249                         return error;
3250                 goto finish_open;
3251         }
3252
3253         if (!(open_flag & O_CREAT)) {
3254                 if (nd->last.name[nd->last.len])
3255                         nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3256                 /* we _can_ be in RCU mode here */
3257                 error = lookup_fast(nd, &path, &inode, &seq);
3258                 if (likely(error > 0))
3259                         goto finish_lookup;
3260
3261                 if (error < 0)
3262                         return error;
3263
3264                 BUG_ON(nd->inode != dir->d_inode);
3265                 BUG_ON(nd->flags & LOOKUP_RCU);
3266         } else {
3267                 /* create side of things */
3268                 /*
3269                  * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3270                  * has been cleared when we got to the last component we are
3271                  * about to look up
3272                  */
3273                 error = complete_walk(nd);
3274                 if (error)
3275                         return error;
3276
3277                 audit_inode(nd->name, dir, LOOKUP_PARENT);
3278                 /* trailing slashes? */
3279                 if (unlikely(nd->last.name[nd->last.len]))
3280                         return -EISDIR;
3281         }
3282
3283         if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
3284                 error = mnt_want_write(nd->path.mnt);
3285                 if (!error)
3286                         got_write = true;
3287                 /*
3288                  * do _not_ fail yet - we might not need that or fail with
3289                  * a different error; let lookup_open() decide; we'll be
3290                  * dropping this one anyway.
3291                  */
3292         }
3293         if (open_flag & O_CREAT)
3294                 inode_lock(dir->d_inode);
3295         else
3296                 inode_lock_shared(dir->d_inode);
3297         error = lookup_open(nd, &path, file, op, got_write, opened);
3298         if (open_flag & O_CREAT)
3299                 inode_unlock(dir->d_inode);
3300         else
3301                 inode_unlock_shared(dir->d_inode);
3302
3303         if (error <= 0) {
3304                 if (error)
3305                         goto out;
3306
3307                 if ((*opened & FILE_CREATED) ||
3308                     !S_ISREG(file_inode(file)->i_mode))
3309                         will_truncate = false;
3310
3311                 audit_inode(nd->name, file->f_path.dentry, 0);
3312                 goto opened;
3313         }
3314
3315         if (*opened & FILE_CREATED) {
3316                 /* Don't check for write permission, don't truncate */
3317                 open_flag &= ~O_TRUNC;
3318                 will_truncate = false;
3319                 acc_mode = 0;
3320                 path_to_nameidata(&path, nd);
3321                 goto finish_open_created;
3322         }
3323
3324         /*
3325          * If atomic_open() acquired write access it is dropped now due to
3326          * possible mount and symlink following (this might be optimized away if
3327          * necessary...)
3328          */
3329         if (got_write) {
3330                 mnt_drop_write(nd->path.mnt);
3331                 got_write = false;
3332         }
3333
3334         error = follow_managed(&path, nd);
3335         if (unlikely(error < 0))
3336                 return error;
3337
3338         if (unlikely(d_is_negative(path.dentry))) {
3339                 path_to_nameidata(&path, nd);
3340                 return -ENOENT;
3341         }
3342
3343         /*
3344          * create/update audit record if it already exists.
3345          */
3346         audit_inode(nd->name, path.dentry, 0);
3347
3348         if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3349                 path_to_nameidata(&path, nd);
3350                 return -EEXIST;
3351         }
3352
3353         seq = 0;        /* out of RCU mode, so the value doesn't matter */
3354         inode = d_backing_inode(path.dentry);
3355 finish_lookup:
3356         error = step_into(nd, &path, 0, inode, seq);
3357         if (unlikely(error))
3358                 return error;
3359 finish_open:
3360         /* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
3361         error = complete_walk(nd);
3362         if (error)
3363                 return error;
3364         audit_inode(nd->name, nd->path.dentry, 0);
3365         error = -EISDIR;
3366         if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
3367                 goto out;
3368         error = -ENOTDIR;
3369         if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
3370                 goto out;
3371         if (!d_is_reg(nd->path.dentry))
3372                 will_truncate = false;
3373
3374         if (will_truncate) {
3375                 error = mnt_want_write(nd->path.mnt);
3376                 if (error)
3377                         goto out;
3378                 got_write = true;
3379         }
3380 finish_open_created:
3381         error = may_open(&nd->path, acc_mode, open_flag);
3382         if (error)
3383                 goto out;
3384         BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3385         error = vfs_open(&nd->path, file, current_cred());
3386         if (error)
3387                 goto out;
3388         *opened |= FILE_OPENED;
3389 opened:
3390         error = open_check_o_direct(file);
3391         if (!error)
3392                 error = ima_file_check(file, op->acc_mode, *opened);
3393         if (!error && will_truncate)
3394                 error = handle_truncate(file);
3395 out:
3396         if (unlikely(error) && (*opened & FILE_OPENED))
3397                 fput(file);
3398         if (unlikely(error > 0)) {
3399                 WARN_ON(1);
3400                 error = -EINVAL;
3401         }
3402         if (got_write)
3403                 mnt_drop_write(nd->path.mnt);
3404         return error;
3405 }
3406
3407 struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag)
3408 {
3409         struct dentry *child = NULL;
3410         struct inode *dir = dentry->d_inode;
3411         struct inode *inode;
3412         int error;
3413
3414         /* we want directory to be writable */
3415         error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
3416         if (error)
3417                 goto out_err;
3418         error = -EOPNOTSUPP;
3419         if (!dir->i_op->tmpfile)
3420                 goto out_err;
3421         error = -ENOMEM;
3422         child = d_alloc(dentry, &slash_name);
3423         if (unlikely(!child))
3424                 goto out_err;
3425         error = dir->i_op->tmpfile(dir, child, mode);
3426         if (error)
3427                 goto out_err;
3428         error = -ENOENT;
3429         inode = child->d_inode;
3430         if (unlikely(!inode))
3431                 goto out_err;
3432         if (!(open_flag & O_EXCL)) {
3433                 spin_lock(&inode->i_lock);
3434                 inode->i_state |= I_LINKABLE;
3435                 spin_unlock(&inode->i_lock);
3436         }
3437         return child;
3438
3439 out_err:
3440         dput(child);
3441         return ERR_PTR(error);
3442 }
3443 EXPORT_SYMBOL(vfs_tmpfile);
3444
3445 static int do_tmpfile(struct nameidata *nd, unsigned flags,
3446                 const struct open_flags *op,
3447                 struct file *file, int *opened)
3448 {
3449         struct dentry *child;
3450         struct path path;
3451         int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
3452         if (unlikely(error))
3453                 return error;
3454         error = mnt_want_write(path.mnt);
3455         if (unlikely(error))
3456                 goto out;
3457         child = vfs_tmpfile(path.dentry, op->mode, op->open_flag);
3458         error = PTR_ERR(child);
3459         if (IS_ERR(child))
3460                 goto out2;
3461         dput(path.dentry);
3462         path.dentry = child;
3463         audit_inode(nd->name, child, 0);
3464         /* Don't check for other permissions, the inode was just created */
3465         error = may_open(&path, 0, op->open_flag);
3466         if (error)
3467                 goto out2;
3468         file->f_path.mnt = path.mnt;
3469         error = finish_open(file, child, NULL, opened);
3470         if (error)
3471                 goto out2;
3472         error = open_check_o_direct(file);
3473         if (error)
3474                 fput(file);
3475 out2:
3476         mnt_drop_write(path.mnt);
3477 out:
3478         path_put(&path);
3479         return error;
3480 }
3481
3482 static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3483 {
3484         struct path path;
3485         int error = path_lookupat(nd, flags, &path);
3486         if (!error) {
3487                 audit_inode(nd->name, path.dentry, 0);
3488                 error = vfs_open(&path, file, current_cred());
3489                 path_put(&path);
3490         }
3491         return error;
3492 }
3493
3494 static struct file *path_openat(struct nameidata *nd,
3495                         const struct open_flags *op, unsigned flags)
3496 {
3497         const char *s;
3498         struct file *file;
3499         int opened = 0;
3500         int error;
3501
3502         file = get_empty_filp();
3503         if (IS_ERR(file))
3504                 return file;
3505
3506         file->f_flags = op->open_flag;
3507
3508         if (unlikely(file->f_flags & __O_TMPFILE)) {
3509                 error = do_tmpfile(nd, flags, op, file, &opened);
3510                 goto out2;
3511         }
3512
3513         if (unlikely(file->f_flags & O_PATH)) {
3514                 error = do_o_path(nd, flags, file);
3515                 if (!error)
3516                         opened |= FILE_OPENED;
3517                 goto out2;
3518         }
3519
3520         s = path_init(nd, flags);
3521         if (IS_ERR(s)) {
3522                 put_filp(file);
3523                 return ERR_CAST(s);
3524         }
3525         while (!(error = link_path_walk(s, nd)) &&
3526                 (error = do_last(nd, file, op, &opened)) > 0) {
3527                 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3528                 s = trailing_symlink(nd);
3529                 if (IS_ERR(s)) {
3530                         error = PTR_ERR(s);
3531                         break;
3532                 }
3533         }
3534         terminate_walk(nd);
3535 out2:
3536         if (!(opened & FILE_OPENED)) {
3537                 BUG_ON(!error);
3538                 put_filp(file);
3539         }
3540         if (unlikely(error)) {
3541                 if (error == -EOPENSTALE) {
3542                         if (flags & LOOKUP_RCU)
3543                                 error = -ECHILD;
3544                         else
3545                                 error = -ESTALE;
3546                 }
3547                 file = ERR_PTR(error);
3548         }
3549         return file;
3550 }
3551
3552 struct file *do_filp_open(int dfd, struct filename *pathname,
3553                 const struct open_flags *op)
3554 {
3555         struct nameidata nd;
3556         int flags = op->lookup_flags;
3557         struct file *filp;
3558
3559         set_nameidata(&nd, dfd, pathname);
3560         filp = path_openat(&nd, op, flags | LOOKUP_RCU);
3561         if (unlikely(filp == ERR_PTR(-ECHILD)))
3562                 filp = path_openat(&nd, op, flags);
3563         if (unlikely(filp == ERR_PTR(-ESTALE)))
3564                 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
3565         restore_nameidata();
3566         return filp;
3567 }
3568
3569 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3570                 const char *name, const struct open_flags *op)
3571 {
3572         struct nameidata nd;
3573         struct file *file;
3574         struct filename *filename;
3575         int flags = op->lookup_flags | LOOKUP_ROOT;
3576
3577         nd.root.mnt = mnt;
3578         nd.root.dentry = dentry;
3579
3580         if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
3581                 return ERR_PTR(-ELOOP);
3582
3583         filename = getname_kernel(name);
3584         if (IS_ERR(filename))
3585                 return ERR_CAST(filename);
3586
3587         set_nameidata(&nd, -1, filename);
3588         file = path_openat(&nd, op, flags | LOOKUP_RCU);
3589         if (unlikely(file == ERR_PTR(-ECHILD)))
3590                 file = path_openat(&nd, op, flags);
3591         if (unlikely(file == ERR_PTR(-ESTALE)))
3592                 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
3593         restore_nameidata();
3594         putname(filename);
3595         return file;
3596 }
3597
3598 static struct dentry *filename_create(int dfd, struct filename *name,
3599                                 struct path *path, unsigned int lookup_flags)
3600 {
3601         struct dentry *dentry = ERR_PTR(-EEXIST);
3602         struct qstr last;
3603         int type;
3604         int err2;
3605         int error;
3606         bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3607
3608         /*
3609          * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3610          * other flags passed in are ignored!
3611          */
3612         lookup_flags &= LOOKUP_REVAL;
3613
3614         name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3615         if (IS_ERR(name))
3616                 return ERR_CAST(name);
3617
3618         /*
3619          * Yucky last component or no last component at all?
3620          * (foo/., foo/.., /////)
3621          */
3622         if (unlikely(type != LAST_NORM))
3623                 goto out;
3624
3625         /* don't fail immediately if it's r/o, at least try to report other errors */
3626         err2 = mnt_want_write(path->mnt);
3627         /*
3628          * Do the final lookup.
3629          */
3630         lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3631         inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
3632         dentry = __lookup_hash(&last, path->dentry, lookup_flags);
3633         if (IS_ERR(dentry))
3634                 goto unlock;
3635
3636         error = -EEXIST;
3637         if (d_is_positive(dentry))
3638                 goto fail;
3639
3640         /*
3641          * Special case - lookup gave negative, but... we had foo/bar/
3642          * From the vfs_mknod() POV we just have a negative dentry -
3643          * all is fine. Let's be bastards - you had / on the end, you've
3644          * been asking for (non-existent) directory. -ENOENT for you.
3645          */
3646         if (unlikely(!is_dir && last.name[last.len])) {
3647                 error = -ENOENT;
3648                 goto fail;
3649         }
3650         if (unlikely(err2)) {
3651                 error = err2;
3652                 goto fail;
3653         }
3654         putname(name);
3655         return dentry;
3656 fail:
3657         dput(dentry);
3658         dentry = ERR_PTR(error);
3659 unlock:
3660         inode_unlock(path->dentry->d_inode);
3661         if (!err2)
3662                 mnt_drop_write(path->mnt);
3663 out:
3664         path_put(path);
3665         putname(name);
3666         return dentry;
3667 }
3668
3669 struct dentry *kern_path_create(int dfd, const char *pathname,
3670                                 struct path *path, unsigned int lookup_flags)
3671 {
3672         return filename_create(dfd, getname_kernel(pathname),
3673                                 path, lookup_flags);
3674 }
3675 EXPORT_SYMBOL(kern_path_create);
3676
3677 void done_path_create(struct path *path, struct dentry *dentry)
3678 {
3679         dput(dentry);
3680         inode_unlock(path->dentry->d_inode);
3681         mnt_drop_write(path->mnt);
3682         path_put(path);
3683 }
3684 EXPORT_SYMBOL(done_path_create);
3685
3686 inline struct dentry *user_path_create(int dfd, const char __user *pathname,
3687                                 struct path *path, unsigned int lookup_flags)
3688 {
3689         return filename_create(dfd, getname(pathname), path, lookup_flags);
3690 }
3691 EXPORT_SYMBOL(user_path_create);
3692
3693 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3694 {
3695         int error = may_create(dir, dentry);
3696
3697         if (error)
3698                 return error;
3699
3700         if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
3701                 return -EPERM;
3702
3703         if (!dir->i_op->mknod)
3704                 return -EPERM;
3705
3706         error = devcgroup_inode_mknod(mode, dev);
3707         if (error)
3708                 return error;
3709
3710         error = security_inode_mknod(dir, dentry, mode, dev);
3711         if (error)
3712                 return error;
3713
3714         error = dir->i_op->mknod(dir, dentry, mode, dev);
3715         if (!error)
3716                 fsnotify_create(dir, dentry);
3717         return error;
3718 }
3719 EXPORT_SYMBOL(vfs_mknod);
3720
3721 static int may_mknod(umode_t mode)
3722 {
3723         switch (mode & S_IFMT) {
3724         case S_IFREG:
3725         case S_IFCHR:
3726         case S_IFBLK:
3727         case S_IFIFO:
3728         case S_IFSOCK:
3729         case 0: /* zero mode translates to S_IFREG */
3730                 return 0;
3731         case S_IFDIR:
3732                 return -EPERM;
3733         default:
3734                 return -EINVAL;
3735         }
3736 }
3737
3738 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3739                 unsigned, dev)
3740 {
3741         struct dentry *dentry;
3742         struct path path;
3743         int error;
3744         unsigned int lookup_flags = 0;
3745
3746         error = may_mknod(mode);
3747         if (error)
3748                 return error;
3749 retry:
3750         dentry = user_path_create(dfd, filename, &path, lookup_flags);
3751         if (IS_ERR(dentry))
3752                 return PTR_ERR(dentry);
3753
3754         if (!IS_POSIXACL(path.dentry->d_inode))
3755                 mode &= ~current_umask();
3756         error = security_path_mknod(&path, dentry, mode, dev);
3757         if (error)
3758                 goto out;
3759         switch (mode & S_IFMT) {
3760                 case 0: case S_IFREG:
3761                         error = vfs_create(path.dentry->d_inode,dentry,mode,true);
3762                         if (!error)
3763                                 ima_post_path_mknod(dentry);
3764                         break;
3765                 case S_IFCHR: case S_IFBLK:
3766                         error = vfs_mknod(path.dentry->d_inode,dentry,mode,
3767                                         new_decode_dev(dev));
3768                         break;
3769                 case S_IFIFO: case S_IFSOCK:
3770                         error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
3771                         break;
3772         }
3773 out:
3774         done_path_create(&path, dentry);
3775         if (retry_estale(error, lookup_flags)) {
3776                 lookup_flags |= LOOKUP_REVAL;
3777                 goto retry;
3778         }
3779         return error;
3780 }
3781
3782 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
3783 {
3784         return sys_mknodat(AT_FDCWD, filename, mode, dev);
3785 }
3786
3787 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3788 {
3789         int error = may_create(dir, dentry);
3790         unsigned max_links = dir->i_sb->s_max_links;
3791
3792         if (error)
3793                 return error;
3794
3795         if (!dir->i_op->mkdir)
3796                 return -EPERM;
3797
3798         mode &= (S_IRWXUGO|S_ISVTX);
3799         error = security_inode_mkdir(dir, dentry, mode);
3800         if (error)
3801                 return error;
3802
3803         if (max_links && dir->i_nlink >= max_links)
3804                 return -EMLINK;
3805
3806         error = dir->i_op->mkdir(dir, dentry, mode);
3807         if (!error)
3808                 fsnotify_mkdir(dir, dentry);
3809         return error;
3810 }
3811 EXPORT_SYMBOL(vfs_mkdir);
3812
3813 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3814 {
3815         struct dentry *dentry;
3816         struct path path;
3817         int error;
3818         unsigned int lookup_flags = LOOKUP_DIRECTORY;
3819
3820 retry:
3821         dentry = user_path_create(dfd, pathname, &path, lookup_flags);
3822         if (IS_ERR(dentry))
3823                 return PTR_ERR(dentry);
3824
3825         if (!IS_POSIXACL(path.dentry->d_inode))
3826                 mode &= ~current_umask();
3827         error = security_path_mkdir(&path, dentry, mode);
3828         if (!error)
3829                 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3830         done_path_create(&path, dentry);
3831         if (retry_estale(error, lookup_flags)) {
3832                 lookup_flags |= LOOKUP_REVAL;
3833                 goto retry;
3834         }
3835         return error;
3836 }
3837
3838 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
3839 {
3840         return sys_mkdirat(AT_FDCWD, pathname, mode);
3841 }
3842
3843 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3844 {
3845         int error = may_delete(dir, dentry, 1);
3846
3847         if (error)
3848                 return error;
3849
3850         if (!dir->i_op->rmdir)
3851                 return -EPERM;
3852
3853         dget(dentry);
3854         inode_lock(dentry->d_inode);
3855
3856         error = -EBUSY;
3857         if (is_local_mountpoint(dentry))
3858                 goto out;
3859
3860         error = security_inode_rmdir(dir, dentry);
3861         if (error)
3862                 goto out;
3863
3864         shrink_dcache_parent(dentry);
3865         error = dir->i_op->rmdir(dir, dentry);
3866         if (error)
3867                 goto out;
3868
3869         dentry->d_inode->i_flags |= S_DEAD;
3870         dont_mount(dentry);
3871         detach_mounts(dentry);
3872
3873 out:
3874         inode_unlock(dentry->d_inode);
3875         dput(dentry);
3876         if (!error)
3877                 d_delete(dentry);
3878         return error;
3879 }
3880 EXPORT_SYMBOL(vfs_rmdir);
3881
3882 static long do_rmdir(int dfd, const char __user *pathname)
3883 {
3884         int error = 0;
3885         struct filename *name;
3886         struct dentry *dentry;
3887         struct path path;
3888         struct qstr last;
3889         int type;
3890         unsigned int lookup_flags = 0;
3891 retry:
3892         name = filename_parentat(dfd, getname(pathname), lookup_flags,
3893                                 &path, &last, &type);
3894         if (IS_ERR(name))
3895                 return PTR_ERR(name);
3896
3897         switch (type) {
3898         case LAST_DOTDOT:
3899                 error = -ENOTEMPTY;
3900                 goto exit1;
3901         case LAST_DOT:
3902                 error = -EINVAL;
3903                 goto exit1;
3904         case LAST_ROOT:
3905                 error = -EBUSY;
3906                 goto exit1;
3907         }
3908
3909         error = mnt_want_write(path.mnt);
3910         if (error)
3911                 goto exit1;
3912
3913         inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
3914         dentry = __lookup_hash(&last, path.dentry, lookup_flags);
3915         error = PTR_ERR(dentry);
3916         if (IS_ERR(dentry))
3917                 goto exit2;
3918         if (!dentry->d_inode) {
3919                 error = -ENOENT;
3920                 goto exit3;
3921         }
3922         error = security_path_rmdir(&path, dentry);
3923         if (error)
3924                 goto exit3;
3925         error = vfs_rmdir(path.dentry->d_inode, dentry);
3926 exit3:
3927         dput(dentry);
3928 exit2:
3929         inode_unlock(path.dentry->d_inode);
3930         mnt_drop_write(path.mnt);
3931 exit1:
3932         path_put(&path);
3933         putname(name);
3934         if (retry_estale(error, lookup_flags)) {
3935                 lookup_flags |= LOOKUP_REVAL;
3936                 goto retry;
3937         }
3938         return error;
3939 }
3940
3941 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
3942 {
3943         return do_rmdir(AT_FDCWD, pathname);
3944 }
3945
3946 /**
3947  * vfs_unlink - unlink a filesystem object
3948  * @dir:        parent directory
3949  * @dentry:     victim
3950  * @delegated_inode: returns victim inode, if the inode is delegated.
3951  *
3952  * The caller must hold dir->i_mutex.
3953  *
3954  * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3955  * return a reference to the inode in delegated_inode.  The caller
3956  * should then break the delegation on that inode and retry.  Because
3957  * breaking a delegation may take a long time, the caller should drop
3958  * dir->i_mutex before doing so.
3959  *
3960  * Alternatively, a caller may pass NULL for delegated_inode.  This may
3961  * be appropriate for callers that expect the underlying filesystem not
3962  * to be NFS exported.
3963  */
3964 int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
3965 {
3966         struct inode *target = dentry->d_inode;
3967         int error = may_delete(dir, dentry, 0);
3968
3969         if (error)
3970                 return error;
3971
3972         if (!dir->i_op->unlink)
3973                 return -EPERM;
3974
3975         inode_lock(target);
3976         if (is_local_mountpoint(dentry))
3977                 error = -EBUSY;
3978         else {
3979                 error = security_inode_unlink(dir, dentry);
3980                 if (!error) {
3981                         error = try_break_deleg(target, delegated_inode);
3982                         if (error)
3983                                 goto out;
3984                         error = dir->i_op->unlink(dir, dentry);
3985                         if (!error) {
3986                                 dont_mount(dentry);
3987                                 detach_mounts(dentry);
3988                         }
3989                 }
3990         }
3991 out:
3992         inode_unlock(target);
3993
3994         /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3995         if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3996                 fsnotify_link_count(target);
3997                 d_delete(dentry);
3998         }
3999
4000         return error;
4001 }
4002 EXPORT_SYMBOL(vfs_unlink);
4003
4004 /*
4005  * Make sure that the actual truncation of the file will occur outside its
4006  * directory's i_mutex.  Truncate can take a long time if there is a lot of
4007  * writeout happening, and we don't want to prevent access to the directory
4008  * while waiting on the I/O.
4009  */
4010 long do_unlinkat(int dfd, struct filename *name)
4011 {
4012         int error;
4013         struct dentry *dentry;
4014         struct path path;
4015         struct qstr last;
4016         int type;
4017         struct inode *inode = NULL;
4018         struct inode *delegated_inode = NULL;
4019         unsigned int lookup_flags = 0;
4020 retry:
4021         name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
4022         if (IS_ERR(name))
4023                 return PTR_ERR(name);
4024
4025         error = -EISDIR;
4026         if (type != LAST_NORM)
4027                 goto exit1;
4028
4029         error = mnt_want_write(path.mnt);
4030         if (error)
4031                 goto exit1;
4032 retry_deleg:
4033         inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
4034         dentry = __lookup_hash(&last, path.dentry, lookup_flags);
4035         error = PTR_ERR(dentry);
4036         if (!IS_ERR(dentry)) {
4037                 /* Why not before? Because we want correct error value */
4038                 if (last.name[last.len])
4039                         goto slashes;
4040                 inode = dentry->d_inode;
4041                 if (d_is_negative(dentry))
4042                         goto slashes;
4043                 ihold(inode);
4044                 error = security_path_unlink(&path, dentry);
4045                 if (error)
4046                         goto exit2;
4047                 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
4048 exit2:
4049                 dput(dentry);
4050         }
4051         inode_unlock(path.dentry->d_inode);
4052         if (inode)
4053                 iput(inode);    /* truncate the inode here */
4054         inode = NULL;
4055         if (delegated_inode) {
4056                 error = break_deleg_wait(&delegated_inode);
4057                 if (!error)
4058                         goto retry_deleg;
4059         }
4060         mnt_drop_write(path.mnt);
4061 exit1:
4062         path_put(&path);
4063         if (retry_estale(error, lookup_flags)) {
4064                 lookup_flags |= LOOKUP_REVAL;
4065                 inode = NULL;
4066                 goto retry;
4067         }
4068         putname(name);
4069         return error;
4070
4071 slashes:
4072         if (d_is_negative(dentry))
4073                 error = -ENOENT;
4074         else if (d_is_dir(dentry))
4075                 error = -EISDIR;
4076         else
4077                 error = -ENOTDIR;
4078         goto exit2;
4079 }
4080
4081 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
4082 {
4083         if ((flag & ~AT_REMOVEDIR) != 0)
4084                 return -EINVAL;
4085
4086         if (flag & AT_REMOVEDIR)
4087                 return do_rmdir(dfd, pathname);
4088
4089         return do_unlinkat(dfd, getname(pathname));
4090 }
4091
4092 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
4093 {
4094         return do_unlinkat(AT_FDCWD, getname(pathname));
4095 }
4096
4097 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
4098 {
4099         int error = may_create(dir, dentry);
4100
4101         if (error)
4102                 return error;
4103
4104         if (!dir->i_op->symlink)
4105                 return -EPERM;
4106
4107         error = security_inode_symlink(dir, dentry, oldname);
4108         if (error)
4109                 return error;
4110
4111         error = dir->i_op->symlink(dir, dentry, oldname);
4112         if (!error)
4113                 fsnotify_create(dir, dentry);
4114         return error;
4115 }
4116 EXPORT_SYMBOL(vfs_symlink);
4117
4118 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4119                 int, newdfd, const char __user *, newname)
4120 {
4121         int error;
4122         struct filename *from;
4123         struct dentry *dentry;
4124         struct path path;
4125         unsigned int lookup_flags = 0;
4126
4127         from = getname(oldname);
4128         if (IS_ERR(from))
4129                 return PTR_ERR(from);
4130 retry:
4131         dentry = user_path_create(newdfd, newname, &path, lookup_flags);
4132         error = PTR_ERR(dentry);
4133         if (IS_ERR(dentry))
4134                 goto out_putname;
4135
4136         error = security_path_symlink(&path, dentry, from->name);
4137         if (!error)
4138                 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
4139         done_path_create(&path, dentry);
4140         if (retry_estale(error, lookup_flags)) {
4141                 lookup_flags |= LOOKUP_REVAL;
4142                 goto retry;
4143         }
4144 out_putname:
4145         putname(from);
4146         return error;
4147 }
4148
4149 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
4150 {
4151         return sys_symlinkat(oldname, AT_FDCWD, newname);
4152 }
4153
4154 /**
4155  * vfs_link - create a new link
4156  * @old_dentry: object to be linked
4157  * @dir:        new parent
4158  * @new_dentry: where to create the new link
4159  * @delegated_inode: returns inode needing a delegation break
4160  *
4161  * The caller must hold dir->i_mutex
4162  *
4163  * If vfs_link discovers a delegation on the to-be-linked file in need
4164  * of breaking, it will return -EWOULDBLOCK and return a reference to the
4165  * inode in delegated_inode.  The caller should then break the delegation
4166  * and retry.  Because breaking a delegation may take a long time, the
4167  * caller should drop the i_mutex before doing so.
4168  *
4169  * Alternatively, a caller may pass NULL for delegated_inode.  This may
4170  * be appropriate for callers that expect the underlying filesystem not
4171  * to be NFS exported.
4172  */
4173 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
4174 {
4175         struct inode *inode = old_dentry->d_inode;
4176         unsigned max_links = dir->i_sb->s_max_links;
4177         int error;
4178
4179         if (!inode)
4180                 return -ENOENT;
4181
4182         error = may_create(dir, new_dentry);
4183         if (error)
4184                 return error;
4185
4186         if (dir->i_sb != inode->i_sb)
4187                 return -EXDEV;
4188
4189         /*
4190          * A link to an append-only or immutable file cannot be created.
4191          */
4192         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4193                 return -EPERM;
4194         /*
4195          * Updating the link count will likely cause i_uid and i_gid to
4196          * be writen back improperly if their true value is unknown to
4197          * the vfs.
4198          */
4199         if (HAS_UNMAPPED_ID(inode))
4200                 return -EPERM;
4201         if (!dir->i_op->link)
4202                 return -EPERM;
4203         if (S_ISDIR(inode->i_mode))
4204                 return -EPERM;
4205
4206         error = security_inode_link(old_dentry, dir, new_dentry);
4207         if (error)
4208                 return error;
4209
4210         inode_lock(inode);
4211         /* Make sure we don't allow creating hardlink to an unlinked file */
4212         if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
4213                 error =  -ENOENT;
4214         else if (max_links && inode->i_nlink >= max_links)
4215                 error = -EMLINK;
4216         else {
4217                 error = try_break_deleg(inode, delegated_inode);
4218                 if (!error)
4219                         error = dir->i_op->link(old_dentry, dir, new_dentry);
4220         }
4221
4222         if (!error && (inode->i_state & I_LINKABLE)) {
4223                 spin_lock(&inode->i_lock);
4224                 inode->i_state &= ~I_LINKABLE;
4225                 spin_unlock(&inode->i_lock);
4226         }
4227         inode_unlock(inode);
4228         if (!error)
4229                 fsnotify_link(dir, inode, new_dentry);
4230         return error;
4231 }
4232 EXPORT_SYMBOL(vfs_link);
4233
4234 /*
4235  * Hardlinks are often used in delicate situations.  We avoid
4236  * security-related surprises by not following symlinks on the
4237  * newname.  --KAB
4238  *
4239  * We don't follow them on the oldname either to be compatible
4240  * with linux 2.0, and to avoid hard-linking to directories
4241  * and other special files.  --ADM
4242  */
4243 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4244                 int, newdfd, const char __user *, newname, int, flags)
4245 {
4246         struct dentry *new_dentry;
4247         struct path old_path, new_path;
4248         struct inode *delegated_inode = NULL;
4249         int how = 0;
4250         int error;
4251
4252         if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
4253                 return -EINVAL;
4254         /*
4255          * To use null names we require CAP_DAC_READ_SEARCH
4256          * This ensures that not everyone will be able to create
4257          * handlink using the passed filedescriptor.
4258          */
4259         if (flags & AT_EMPTY_PATH) {
4260                 if (!capable(CAP_DAC_READ_SEARCH))
4261                         return -ENOENT;
4262                 how = LOOKUP_EMPTY;
4263         }
4264
4265         if (flags & AT_SYMLINK_FOLLOW)
4266                 how |= LOOKUP_FOLLOW;
4267 retry:
4268         error = user_path_at(olddfd, oldname, how, &old_path);
4269         if (error)
4270                 return error;
4271
4272         new_dentry = user_path_create(newdfd, newname, &new_path,
4273                                         (how & LOOKUP_REVAL));
4274         error = PTR_ERR(new_dentry);
4275         if (IS_ERR(new_dentry))
4276                 goto out;
4277
4278         error = -EXDEV;
4279         if (old_path.mnt != new_path.mnt)
4280                 goto out_dput;
4281         error = may_linkat(&old_path);
4282         if (unlikely(error))
4283                 goto out_dput;
4284         error = security_path_link(old_path.dentry, &new_path, new_dentry);
4285         if (error)
4286                 goto out_dput;
4287         error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
4288 out_dput:
4289         done_path_create(&new_path, new_dentry);
4290         if (delegated_inode) {
4291                 error = break_deleg_wait(&delegated_inode);
4292                 if (!error) {
4293                         path_put(&old_path);
4294                         goto retry;
4295                 }
4296         }
4297         if (retry_estale(error, how)) {
4298                 path_put(&old_path);
4299                 how |= LOOKUP_REVAL;
4300                 goto retry;
4301         }
4302 out:
4303         path_put(&old_path);
4304
4305         return error;
4306 }
4307
4308 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
4309 {
4310         return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
4311 }
4312
4313 /**
4314  * vfs_rename - rename a filesystem object
4315  * @old_dir:    parent of source
4316  * @old_dentry: source
4317  * @new_dir:    parent of destination
4318  * @new_dentry: destination
4319  * @delegated_inode: returns an inode needing a delegation break
4320  * @flags:      rename flags
4321  *
4322  * The caller must hold multiple mutexes--see lock_rename()).
4323  *
4324  * If vfs_rename discovers a delegation in need of breaking at either
4325  * the source or destination, it will return -EWOULDBLOCK and return a
4326  * reference to the inode in delegated_inode.  The caller should then
4327  * break the delegation and retry.  Because breaking a delegation may
4328  * take a long time, the caller should drop all locks before doing
4329  * so.
4330  *
4331  * Alternatively, a caller may pass NULL for delegated_inode.  This may
4332  * be appropriate for callers that expect the underlying filesystem not
4333  * to be NFS exported.
4334  *
4335  * The worst of all namespace operations - renaming directory. "Perverted"
4336  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4337  * Problems:
4338  *
4339  *      a) we can get into loop creation.
4340  *      b) race potential - two innocent renames can create a loop together.
4341  *         That's where 4.4 screws up. Current fix: serialization on
4342  *         sb->s_vfs_rename_mutex. We might be more accurate, but that's another
4343  *         story.
4344  *      c) we have to lock _four_ objects - parents and victim (if it exists),
4345  *         and source (if it is not a directory).
4346  *         And that - after we got ->i_mutex on parents (until then we don't know
4347  *         whether the target exists).  Solution: try to be smart with locking
4348  *         order for inodes.  We rely on the fact that tree topology may change
4349  *         only under ->s_vfs_rename_mutex _and_ that parent of the object we
4350  *         move will be locked.  Thus we can rank directories by the tree
4351  *         (ancestors first) and rank all non-directories after them.
4352  *         That works since everybody except rename does "lock parent, lookup,
4353  *         lock child" and rename is under ->s_vfs_rename_mutex.
4354  *         HOWEVER, it relies on the assumption that any object with ->lookup()
4355  *         has no more than 1 dentry.  If "hybrid" objects will ever appear,
4356  *         we'd better make sure that there's no link(2) for them.
4357  *      d) conversion from fhandle to dentry may come in the wrong moment - when
4358  *         we are removing the target. Solution: we will have to grab ->i_mutex
4359  *         in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4360  *         ->i_mutex on parents, which works but leads to some truly excessive
4361  *         locking].
4362  */
4363 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4364                struct inode *new_dir, struct dentry *new_dentry,
4365                struct inode **delegated_inode, unsigned int flags)
4366 {
4367         int error;
4368         bool is_dir = d_is_dir(old_dentry);
4369         struct inode *source = old_dentry->d_inode;
4370         struct inode *target = new_dentry->d_inode;
4371         bool new_is_dir = false;
4372         unsigned max_links = new_dir->i_sb->s_max_links;
4373         struct name_snapshot old_name;
4374
4375         if (source == target)
4376                 return 0;
4377
4378         error = may_delete(old_dir, old_dentry, is_dir);
4379         if (error)
4380                 return error;
4381
4382         if (!target) {
4383                 error = may_create(new_dir, new_dentry);
4384         } else {
4385                 new_is_dir = d_is_dir(new_dentry);
4386
4387                 if (!(flags & RENAME_EXCHANGE))
4388                         error = may_delete(new_dir, new_dentry, is_dir);
4389                 else
4390                         error = may_delete(new_dir, new_dentry, new_is_dir);
4391         }
4392         if (error)
4393                 return error;
4394
4395         if (!old_dir->i_op->rename)
4396                 return -EPERM;
4397
4398         /*
4399          * If we are going to change the parent - check write permissions,
4400          * we'll need to flip '..'.
4401          */
4402         if (new_dir != old_dir) {
4403                 if (is_dir) {
4404                         error = inode_permission(source, MAY_WRITE);
4405                         if (error)
4406                                 return error;
4407                 }
4408                 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4409                         error = inode_permission(target, MAY_WRITE);
4410                         if (error)
4411                                 return error;
4412                 }
4413         }
4414
4415         error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4416                                       flags);
4417         if (error)
4418                 return error;
4419
4420         take_dentry_name_snapshot(&old_name, old_dentry);
4421         dget(new_dentry);
4422         if (!is_dir || (flags & RENAME_EXCHANGE))
4423                 lock_two_nondirectories(source, target);
4424         else if (target)
4425                 inode_lock(target);
4426
4427         error = -EBUSY;
4428         if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
4429                 goto out;
4430
4431         if (max_links && new_dir != old_dir) {
4432                 error = -EMLINK;
4433                 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
4434                         goto out;
4435                 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4436                     old_dir->i_nlink >= max_links)
4437                         goto out;
4438         }
4439         if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4440                 shrink_dcache_parent(new_dentry);
4441         if (!is_dir) {
4442                 error = try_break_deleg(source, delegated_inode);
4443                 if (error)
4444                         goto out;
4445         }
4446         if (target && !new_is_dir) {
4447                 error = try_break_deleg(target, delegated_inode);
4448                 if (error)
4449                         goto out;
4450         }
4451         error = old_dir->i_op->rename(old_dir, old_dentry,
4452                                        new_dir, new_dentry, flags);
4453         if (error)
4454                 goto out;
4455
4456         if (!(flags & RENAME_EXCHANGE) && target) {
4457                 if (is_dir)
4458                         target->i_flags |= S_DEAD;
4459                 dont_mount(new_dentry);
4460                 detach_mounts(new_dentry);
4461         }
4462         if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4463                 if (!(flags & RENAME_EXCHANGE))
4464                         d_move(old_dentry, new_dentry);
4465                 else
4466                         d_exchange(old_dentry, new_dentry);
4467         }
4468 out:
4469         if (!is_dir || (flags & RENAME_EXCHANGE))
4470                 unlock_two_nondirectories(source, target);
4471         else if (target)
4472                 inode_unlock(target);
4473         dput(new_dentry);
4474         if (!error) {
4475                 fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
4476                               !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4477                 if (flags & RENAME_EXCHANGE) {
4478                         fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4479                                       new_is_dir, NULL, new_dentry);
4480                 }
4481         }
4482         release_dentry_name_snapshot(&old_name);
4483
4484         return error;
4485 }
4486 EXPORT_SYMBOL(vfs_rename);
4487
4488 SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4489                 int, newdfd, const char __user *, newname, unsigned int, flags)
4490 {
4491         struct dentry *old_dentry, *new_dentry;
4492         struct dentry *trap;
4493         struct path old_path, new_path;
4494         struct qstr old_last, new_last;
4495         int old_type, new_type;
4496         struct inode *delegated_inode = NULL;
4497         struct filename *from;
4498         struct filename *to;
4499         unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
4500         bool should_retry = false;
4501         int error;
4502
4503         if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
4504                 return -EINVAL;
4505
4506         if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4507             (flags & RENAME_EXCHANGE))
4508                 return -EINVAL;
4509
4510         if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4511                 return -EPERM;
4512
4513         if (flags & RENAME_EXCHANGE)
4514                 target_flags = 0;
4515
4516 retry:
4517         from = filename_parentat(olddfd, getname(oldname), lookup_flags,
4518                                 &old_path, &old_last, &old_type);
4519         if (IS_ERR(from)) {
4520                 error = PTR_ERR(from);
4521                 goto exit;
4522         }
4523
4524         to = filename_parentat(newdfd, getname(newname), lookup_flags,
4525                                 &new_path, &new_last, &new_type);
4526         if (IS_ERR(to)) {
4527                 error = PTR_ERR(to);
4528                 goto exit1;
4529         }
4530
4531         error = -EXDEV;
4532         if (old_path.mnt != new_path.mnt)
4533                 goto exit2;
4534
4535         error = -EBUSY;
4536         if (old_type != LAST_NORM)
4537                 goto exit2;
4538
4539         if (flags & RENAME_NOREPLACE)
4540                 error = -EEXIST;
4541         if (new_type != LAST_NORM)
4542                 goto exit2;
4543
4544         error = mnt_want_write(old_path.mnt);
4545         if (error)
4546                 goto exit2;
4547
4548 retry_deleg:
4549         trap = lock_rename(new_path.dentry, old_path.dentry);
4550
4551         old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
4552         error = PTR_ERR(old_dentry);
4553         if (IS_ERR(old_dentry))
4554                 goto exit3;
4555         /* source must exist */
4556         error = -ENOENT;
4557         if (d_is_negative(old_dentry))
4558                 goto exit4;
4559         new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
4560         error = PTR_ERR(new_dentry);
4561         if (IS_ERR(new_dentry))
4562                 goto exit4;
4563         error = -EEXIST;
4564         if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4565                 goto exit5;
4566         if (flags & RENAME_EXCHANGE) {
4567                 error = -ENOENT;
4568                 if (d_is_negative(new_dentry))
4569                         goto exit5;
4570
4571                 if (!d_is_dir(new_dentry)) {
4572                         error = -ENOTDIR;
4573                         if (new_last.name[new_last.len])
4574                                 goto exit5;
4575                 }
4576         }
4577         /* unless the source is a directory trailing slashes give -ENOTDIR */
4578         if (!d_is_dir(old_dentry)) {
4579                 error = -ENOTDIR;
4580                 if (old_last.name[old_last.len])
4581                         goto exit5;
4582                 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
4583                         goto exit5;
4584         }
4585         /* source should not be ancestor of target */
4586         error = -EINVAL;
4587         if (old_dentry == trap)
4588                 goto exit5;
4589         /* target should not be an ancestor of source */
4590         if (!(flags & RENAME_EXCHANGE))
4591                 error = -ENOTEMPTY;
4592         if (new_dentry == trap)
4593                 goto exit5;
4594
4595         error = security_path_rename(&old_path, old_dentry,
4596                                      &new_path, new_dentry, flags);
4597         if (error)
4598                 goto exit5;
4599         error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4600                            new_path.dentry->d_inode, new_dentry,
4601                            &delegated_inode, flags);
4602 exit5:
4603         dput(new_dentry);
4604 exit4:
4605         dput(old_dentry);
4606 exit3:
4607         unlock_rename(new_path.dentry, old_path.dentry);
4608         if (delegated_inode) {
4609                 error = break_deleg_wait(&delegated_inode);
4610                 if (!error)
4611                         goto retry_deleg;
4612         }
4613         mnt_drop_write(old_path.mnt);
4614 exit2:
4615         if (retry_estale(error, lookup_flags))
4616                 should_retry = true;
4617         path_put(&new_path);
4618         putname(to);
4619 exit1:
4620         path_put(&old_path);
4621         putname(from);
4622         if (should_retry) {
4623                 should_retry = false;
4624                 lookup_flags |= LOOKUP_REVAL;
4625                 goto retry;
4626         }
4627 exit:
4628         return error;
4629 }
4630
4631 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4632                 int, newdfd, const char __user *, newname)
4633 {
4634         return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4635 }
4636
4637 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
4638 {
4639         return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
4640 }
4641
4642 int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4643 {
4644         int error = may_create(dir, dentry);
4645         if (error)
4646                 return error;
4647
4648         if (!dir->i_op->mknod)
4649                 return -EPERM;
4650
4651         return dir->i_op->mknod(dir, dentry,
4652                                 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4653 }
4654 EXPORT_SYMBOL(vfs_whiteout);
4655
4656 int readlink_copy(char __user *buffer, int buflen, const char *link)
4657 {
4658         int len = PTR_ERR(link);
4659         if (IS_ERR(link))
4660                 goto out;
4661
4662         len = strlen(link);
4663         if (len > (unsigned) buflen)
4664                 len = buflen;
4665         if (copy_to_user(buffer, link, len))
4666                 len = -EFAULT;
4667 out:
4668         return len;
4669 }
4670
4671 /*
4672  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
4673  * have ->get_link() not calling nd_jump_link().  Using (or not using) it
4674  * for any given inode is up to filesystem.
4675  */
4676 static int generic_readlink(struct dentry *dentry, char __user *buffer,
4677                             int buflen)
4678 {
4679         DEFINE_DELAYED_CALL(done);
4680         struct inode *inode = d_inode(dentry);
4681         const char *link = inode->i_link;
4682         int res;
4683
4684         if (!link) {
4685                 link = inode->i_op->get_link(dentry, inode, &done);
4686                 if (IS_ERR(link))
4687                         return PTR_ERR(link);
4688         }
4689         res = readlink_copy(buffer, buflen, link);
4690         do_delayed_call(&done);
4691         return res;
4692 }
4693
4694 /**
4695  * vfs_readlink - copy symlink body into userspace buffer
4696  * @dentry: dentry on which to get symbolic link
4697  * @buffer: user memory pointer
4698  * @buflen: size of buffer
4699  *
4700  * Does not touch atime.  That's up to the caller if necessary
4701  *
4702  * Does not call security hook.
4703  */
4704 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4705 {
4706         struct inode *inode = d_inode(dentry);
4707
4708         if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
4709                 if (unlikely(inode->i_op->readlink))
4710                         return inode->i_op->readlink(dentry, buffer, buflen);
4711
4712                 if (!d_is_symlink(dentry))
4713                         return -EINVAL;
4714
4715                 spin_lock(&inode->i_lock);
4716                 inode->i_opflags |= IOP_DEFAULT_READLINK;
4717                 spin_unlock(&inode->i_lock);
4718         }
4719
4720         return generic_readlink(dentry, buffer, buflen);
4721 }
4722 EXPORT_SYMBOL(vfs_readlink);
4723
4724 /**
4725  * vfs_get_link - get symlink body
4726  * @dentry: dentry on which to get symbolic link
4727  * @done: caller needs to free returned data with this
4728  *
4729  * Calls security hook and i_op->get_link() on the supplied inode.
4730  *
4731  * It does not touch atime.  That's up to the caller if necessary.
4732  *
4733  * Does not work on "special" symlinks like /proc/$$/fd/N
4734  */
4735 const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4736 {
4737         const char *res = ERR_PTR(-EINVAL);
4738         struct inode *inode = d_inode(dentry);
4739
4740         if (d_is_symlink(dentry)) {
4741                 res = ERR_PTR(security_inode_readlink(dentry));
4742                 if (!res)
4743                         res = inode->i_op->get_link(dentry, inode, done);
4744         }
4745         return res;
4746 }
4747 EXPORT_SYMBOL(vfs_get_link);
4748
4749 /* get the link contents into pagecache */
4750 const char *page_get_link(struct dentry *dentry, struct inode *inode,
4751                           struct delayed_call *callback)
4752 {
4753         char *kaddr;
4754         struct page *page;
4755         struct address_space *mapping = inode->i_mapping;
4756
4757         if (!dentry) {
4758                 page = find_get_page(mapping, 0);
4759                 if (!page)
4760                         return ERR_PTR(-ECHILD);
4761                 if (!PageUptodate(page)) {
4762                         put_page(page);
4763                         return ERR_PTR(-ECHILD);
4764                 }
4765         } else {
4766                 page = read_mapping_page(mapping, 0, NULL);
4767                 if (IS_ERR(page))
4768                         return (char*)page;
4769         }
4770         set_delayed_call(callback, page_put_link, page);
4771         BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4772         kaddr = page_address(page);
4773         nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
4774         return kaddr;
4775 }
4776
4777 EXPORT_SYMBOL(page_get_link);
4778
4779 void page_put_link(void *arg)
4780 {
4781         put_page(arg);
4782 }
4783 EXPORT_SYMBOL(page_put_link);
4784
4785 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4786 {
4787         DEFINE_DELAYED_CALL(done);
4788         int res = readlink_copy(buffer, buflen,
4789                                 page_get_link(dentry, d_inode(dentry),
4790                                               &done));
4791         do_delayed_call(&done);
4792         return res;
4793 }
4794 EXPORT_SYMBOL(page_readlink);
4795
4796 /*
4797  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4798  */
4799 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
4800 {
4801         struct address_space *mapping = inode->i_mapping;
4802         struct page *page;
4803         void *fsdata;
4804         int err;
4805         unsigned int flags = 0;
4806         if (nofs)
4807                 flags |= AOP_FLAG_NOFS;
4808
4809 retry:
4810         err = pagecache_write_begin(NULL, mapping, 0, len-1,
4811                                 flags, &page, &fsdata);
4812         if (err)
4813                 goto fail;
4814
4815         memcpy(page_address(page), symname, len-1);
4816
4817         err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4818                                                         page, fsdata);
4819         if (err < 0)
4820                 goto fail;
4821         if (err < len-1)
4822                 goto retry;
4823
4824         mark_inode_dirty(inode);
4825         return 0;
4826 fail:
4827         return err;
4828 }
4829 EXPORT_SYMBOL(__page_symlink);
4830
4831 int page_symlink(struct inode *inode, const char *symname, int len)
4832 {
4833         return __page_symlink(inode, symname, len,
4834                         !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
4835 }
4836 EXPORT_SYMBOL(page_symlink);
4837
4838 const struct inode_operations page_symlink_inode_operations = {
4839         .get_link       = page_get_link,
4840 };
4841 EXPORT_SYMBOL(page_symlink_inode_operations);