Merge tag 'mtd/for-4.16' of git://git.infradead.org/linux-mtd
[linux-2.6-microblaze.git] / fs / overlayfs / namei.c
1 /*
2  * Copyright (C) 2011 Novell Inc.
3  * Copyright (C) 2016 Red Hat, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  */
9
10 #include <linux/fs.h>
11 #include <linux/cred.h>
12 #include <linux/namei.h>
13 #include <linux/xattr.h>
14 #include <linux/ratelimit.h>
15 #include <linux/mount.h>
16 #include <linux/exportfs.h>
17 #include "overlayfs.h"
18
19 struct ovl_lookup_data {
20         struct qstr name;
21         bool is_dir;
22         bool opaque;
23         bool stop;
24         bool last;
25         char *redirect;
26 };
27
28 static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
29                               size_t prelen, const char *post)
30 {
31         int res;
32         char *s, *next, *buf = NULL;
33
34         res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0);
35         if (res < 0) {
36                 if (res == -ENODATA || res == -EOPNOTSUPP)
37                         return 0;
38                 goto fail;
39         }
40         buf = kzalloc(prelen + res + strlen(post) + 1, GFP_KERNEL);
41         if (!buf)
42                 return -ENOMEM;
43
44         if (res == 0)
45                 goto invalid;
46
47         res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res);
48         if (res < 0)
49                 goto fail;
50         if (res == 0)
51                 goto invalid;
52         if (buf[0] == '/') {
53                 for (s = buf; *s++ == '/'; s = next) {
54                         next = strchrnul(s, '/');
55                         if (s == next)
56                                 goto invalid;
57                 }
58         } else {
59                 if (strchr(buf, '/') != NULL)
60                         goto invalid;
61
62                 memmove(buf + prelen, buf, res);
63                 memcpy(buf, d->name.name, prelen);
64         }
65
66         strcat(buf, post);
67         kfree(d->redirect);
68         d->redirect = buf;
69         d->name.name = d->redirect;
70         d->name.len = strlen(d->redirect);
71
72         return 0;
73
74 err_free:
75         kfree(buf);
76         return 0;
77 fail:
78         pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
79         goto err_free;
80 invalid:
81         pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
82         goto err_free;
83 }
84
85 static int ovl_acceptable(void *ctx, struct dentry *dentry)
86 {
87         return 1;
88 }
89
90 static struct ovl_fh *ovl_get_origin_fh(struct dentry *dentry)
91 {
92         int res;
93         struct ovl_fh *fh = NULL;
94
95         res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
96         if (res < 0) {
97                 if (res == -ENODATA || res == -EOPNOTSUPP)
98                         return NULL;
99                 goto fail;
100         }
101         /* Zero size value means "copied up but origin unknown" */
102         if (res == 0)
103                 return NULL;
104
105         fh  = kzalloc(res, GFP_KERNEL);
106         if (!fh)
107                 return ERR_PTR(-ENOMEM);
108
109         res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, fh, res);
110         if (res < 0)
111                 goto fail;
112
113         if (res < sizeof(struct ovl_fh) || res < fh->len)
114                 goto invalid;
115
116         if (fh->magic != OVL_FH_MAGIC)
117                 goto invalid;
118
119         /* Treat larger version and unknown flags as "origin unknown" */
120         if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL)
121                 goto out;
122
123         /* Treat endianness mismatch as "origin unknown" */
124         if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
125             (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
126                 goto out;
127
128         return fh;
129
130 out:
131         kfree(fh);
132         return NULL;
133
134 fail:
135         pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res);
136         goto out;
137 invalid:
138         pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh);
139         goto out;
140 }
141
142 static struct dentry *ovl_get_origin(struct dentry *dentry,
143                                      struct vfsmount *mnt)
144 {
145         struct dentry *origin = NULL;
146         struct ovl_fh *fh = ovl_get_origin_fh(dentry);
147         int bytes;
148
149         if (IS_ERR_OR_NULL(fh))
150                 return (struct dentry *)fh;
151
152         /*
153          * Make sure that the stored uuid matches the uuid of the lower
154          * layer where file handle will be decoded.
155          */
156         if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid))
157                 goto out;
158
159         bytes = (fh->len - offsetof(struct ovl_fh, fid));
160         origin = exportfs_decode_fh(mnt, (struct fid *)fh->fid,
161                                     bytes >> 2, (int)fh->type,
162                                     ovl_acceptable, NULL);
163         if (IS_ERR(origin)) {
164                 /* Treat stale file handle as "origin unknown" */
165                 if (origin == ERR_PTR(-ESTALE))
166                         origin = NULL;
167                 goto out;
168         }
169
170         if (ovl_dentry_weird(origin) ||
171             ((d_inode(origin)->i_mode ^ d_inode(dentry)->i_mode) & S_IFMT))
172                 goto invalid;
173
174 out:
175         kfree(fh);
176         return origin;
177
178 invalid:
179         pr_warn_ratelimited("overlayfs: invalid origin (%pd2)\n", origin);
180         dput(origin);
181         origin = NULL;
182         goto out;
183 }
184
185 static bool ovl_is_opaquedir(struct dentry *dentry)
186 {
187         return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE);
188 }
189
190 static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
191                              const char *name, unsigned int namelen,
192                              size_t prelen, const char *post,
193                              struct dentry **ret)
194 {
195         struct dentry *this;
196         int err;
197
198         this = lookup_one_len_unlocked(name, base, namelen);
199         if (IS_ERR(this)) {
200                 err = PTR_ERR(this);
201                 this = NULL;
202                 if (err == -ENOENT || err == -ENAMETOOLONG)
203                         goto out;
204                 goto out_err;
205         }
206         if (!this->d_inode)
207                 goto put_and_out;
208
209         if (ovl_dentry_weird(this)) {
210                 /* Don't support traversing automounts and other weirdness */
211                 err = -EREMOTE;
212                 goto out_err;
213         }
214         if (ovl_is_whiteout(this)) {
215                 d->stop = d->opaque = true;
216                 goto put_and_out;
217         }
218         if (!d_can_lookup(this)) {
219                 d->stop = true;
220                 if (d->is_dir)
221                         goto put_and_out;
222                 goto out;
223         }
224         d->is_dir = true;
225         if (!d->last && ovl_is_opaquedir(this)) {
226                 d->stop = d->opaque = true;
227                 goto out;
228         }
229         err = ovl_check_redirect(this, d, prelen, post);
230         if (err)
231                 goto out_err;
232 out:
233         *ret = this;
234         return 0;
235
236 put_and_out:
237         dput(this);
238         this = NULL;
239         goto out;
240
241 out_err:
242         dput(this);
243         return err;
244 }
245
246 static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
247                             struct dentry **ret)
248 {
249         /* Counting down from the end, since the prefix can change */
250         size_t rem = d->name.len - 1;
251         struct dentry *dentry = NULL;
252         int err;
253
254         if (d->name.name[0] != '/')
255                 return ovl_lookup_single(base, d, d->name.name, d->name.len,
256                                          0, "", ret);
257
258         while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
259                 const char *s = d->name.name + d->name.len - rem;
260                 const char *next = strchrnul(s, '/');
261                 size_t thislen = next - s;
262                 bool end = !next[0];
263
264                 /* Verify we did not go off the rails */
265                 if (WARN_ON(s[-1] != '/'))
266                         return -EIO;
267
268                 err = ovl_lookup_single(base, d, s, thislen,
269                                         d->name.len - rem, next, &base);
270                 dput(dentry);
271                 if (err)
272                         return err;
273                 dentry = base;
274                 if (end)
275                         break;
276
277                 rem -= thislen + 1;
278
279                 if (WARN_ON(rem >= d->name.len))
280                         return -EIO;
281         }
282         *ret = dentry;
283         return 0;
284 }
285
286
287 static int ovl_check_origin(struct dentry *upperdentry,
288                             struct ovl_path *lower, unsigned int numlower,
289                             struct ovl_path **stackp, unsigned int *ctrp)
290 {
291         struct vfsmount *mnt;
292         struct dentry *origin = NULL;
293         int i;
294
295         for (i = 0; i < numlower; i++) {
296                 mnt = lower[i].layer->mnt;
297                 origin = ovl_get_origin(upperdentry, mnt);
298                 if (IS_ERR(origin))
299                         return PTR_ERR(origin);
300
301                 if (origin)
302                         break;
303         }
304
305         if (!origin)
306                 return 0;
307
308         BUG_ON(*ctrp);
309         if (!*stackp)
310                 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
311         if (!*stackp) {
312                 dput(origin);
313                 return -ENOMEM;
314         }
315         **stackp = (struct ovl_path){.dentry = origin, .layer = lower[i].layer};
316         *ctrp = 1;
317
318         return 0;
319 }
320
321 /*
322  * Verify that @fh matches the origin file handle stored in OVL_XATTR_ORIGIN.
323  * Return 0 on match, -ESTALE on mismatch, < 0 on error.
324  */
325 static int ovl_verify_origin_fh(struct dentry *dentry, const struct ovl_fh *fh)
326 {
327         struct ovl_fh *ofh = ovl_get_origin_fh(dentry);
328         int err = 0;
329
330         if (!ofh)
331                 return -ENODATA;
332
333         if (IS_ERR(ofh))
334                 return PTR_ERR(ofh);
335
336         if (fh->len != ofh->len || memcmp(fh, ofh, fh->len))
337                 err = -ESTALE;
338
339         kfree(ofh);
340         return err;
341 }
342
343 /*
344  * Verify that an inode matches the origin file handle stored in upper inode.
345  *
346  * If @set is true and there is no stored file handle, encode and store origin
347  * file handle in OVL_XATTR_ORIGIN.
348  *
349  * Return 0 on match, -ESTALE on mismatch, < 0 on error.
350  */
351 int ovl_verify_origin(struct dentry *dentry, struct dentry *origin,
352                       bool is_upper, bool set)
353 {
354         struct inode *inode;
355         struct ovl_fh *fh;
356         int err;
357
358         fh = ovl_encode_fh(origin, is_upper);
359         err = PTR_ERR(fh);
360         if (IS_ERR(fh))
361                 goto fail;
362
363         err = ovl_verify_origin_fh(dentry, fh);
364         if (set && err == -ENODATA)
365                 err = ovl_do_setxattr(dentry, OVL_XATTR_ORIGIN, fh, fh->len, 0);
366         if (err)
367                 goto fail;
368
369 out:
370         kfree(fh);
371         return err;
372
373 fail:
374         inode = d_inode(origin);
375         pr_warn_ratelimited("overlayfs: failed to verify origin (%pd2, ino=%lu, err=%i)\n",
376                             origin, inode ? inode->i_ino : 0, err);
377         goto out;
378 }
379
380 /*
381  * Verify that an index entry name matches the origin file handle stored in
382  * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
383  * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
384  */
385 int ovl_verify_index(struct dentry *index, struct ovl_path *lower,
386                      unsigned int numlower)
387 {
388         struct ovl_fh *fh = NULL;
389         size_t len;
390         struct ovl_path origin = { };
391         struct ovl_path *stack = &origin;
392         unsigned int ctr = 0;
393         int err;
394
395         if (!d_inode(index))
396                 return 0;
397
398         /*
399          * Directory index entries are going to be used for looking up
400          * redirected upper dirs by lower dir fh when decoding an overlay
401          * file handle of a merge dir. Whiteout index entries are going to be
402          * used as an indication that an exported overlay file handle should
403          * be treated as stale (i.e. after unlink of the overlay inode).
404          * We don't know the verification rules for directory and whiteout
405          * index entries, because they have not been implemented yet, so return
406          * EINVAL if those entries are found to abort the mount to avoid
407          * corrupting an index that was created by a newer kernel.
408          */
409         err = -EINVAL;
410         if (d_is_dir(index) || ovl_is_whiteout(index))
411                 goto fail;
412
413         if (index->d_name.len < sizeof(struct ovl_fh)*2)
414                 goto fail;
415
416         err = -ENOMEM;
417         len = index->d_name.len / 2;
418         fh = kzalloc(len, GFP_KERNEL);
419         if (!fh)
420                 goto fail;
421
422         err = -EINVAL;
423         if (hex2bin((u8 *)fh, index->d_name.name, len) || len != fh->len)
424                 goto fail;
425
426         err = ovl_verify_origin_fh(index, fh);
427         if (err)
428                 goto fail;
429
430         err = ovl_check_origin(index, lower, numlower, &stack, &ctr);
431         if (!err && !ctr)
432                 err = -ESTALE;
433         if (err)
434                 goto fail;
435
436         /* Check if index is orphan and don't warn before cleaning it */
437         if (d_inode(index)->i_nlink == 1 &&
438             ovl_get_nlink(origin.dentry, index, 0) == 0)
439                 err = -ENOENT;
440
441         dput(origin.dentry);
442 out:
443         kfree(fh);
444         return err;
445
446 fail:
447         pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
448                             index, d_inode(index)->i_mode & S_IFMT, err);
449         goto out;
450 }
451
452 /*
453  * Lookup in indexdir for the index entry of a lower real inode or a copy up
454  * origin inode. The index entry name is the hex representation of the lower
455  * inode file handle.
456  *
457  * If the index dentry in negative, then either no lower aliases have been
458  * copied up yet, or aliases have been copied up in older kernels and are
459  * not indexed.
460  *
461  * If the index dentry for a copy up origin inode is positive, but points
462  * to an inode different than the upper inode, then either the upper inode
463  * has been copied up and not indexed or it was indexed, but since then
464  * index dir was cleared. Either way, that index cannot be used to indentify
465  * the overlay inode.
466  */
467 int ovl_get_index_name(struct dentry *origin, struct qstr *name)
468 {
469         int err;
470         struct ovl_fh *fh;
471         char *n, *s;
472
473         fh = ovl_encode_fh(origin, false);
474         if (IS_ERR(fh))
475                 return PTR_ERR(fh);
476
477         err = -ENOMEM;
478         n = kzalloc(fh->len * 2, GFP_KERNEL);
479         if (n) {
480                 s  = bin2hex(n, fh, fh->len);
481                 *name = (struct qstr) QSTR_INIT(n, s - n);
482                 err = 0;
483         }
484         kfree(fh);
485
486         return err;
487
488 }
489
490 static struct dentry *ovl_lookup_index(struct dentry *dentry,
491                                        struct dentry *upper,
492                                        struct dentry *origin)
493 {
494         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
495         struct dentry *index;
496         struct inode *inode;
497         struct qstr name;
498         int err;
499
500         err = ovl_get_index_name(origin, &name);
501         if (err)
502                 return ERR_PTR(err);
503
504         index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
505         if (IS_ERR(index)) {
506                 err = PTR_ERR(index);
507                 if (err == -ENOENT) {
508                         index = NULL;
509                         goto out;
510                 }
511                 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%*s, err=%i);\n"
512                                     "overlayfs: mount with '-o index=off' to disable inodes index.\n",
513                                     d_inode(origin)->i_ino, name.len, name.name,
514                                     err);
515                 goto out;
516         }
517
518         inode = d_inode(index);
519         if (d_is_negative(index)) {
520                 goto out_dput;
521         } else if (upper && d_inode(upper) != inode) {
522                 goto out_dput;
523         } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
524                    ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
525                 /*
526                  * Index should always be of the same file type as origin
527                  * except for the case of a whiteout index. A whiteout
528                  * index should only exist if all lower aliases have been
529                  * unlinked, which means that finding a lower origin on lookup
530                  * whose index is a whiteout should be treated as an error.
531                  */
532                 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
533                                     index, d_inode(index)->i_mode & S_IFMT,
534                                     d_inode(origin)->i_mode & S_IFMT);
535                 goto fail;
536         }
537
538 out:
539         kfree(name.name);
540         return index;
541
542 out_dput:
543         dput(index);
544         index = NULL;
545         goto out;
546
547 fail:
548         dput(index);
549         index = ERR_PTR(-EIO);
550         goto out;
551 }
552
553 /*
554  * Returns next layer in stack starting from top.
555  * Returns -1 if this is the last layer.
556  */
557 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
558 {
559         struct ovl_entry *oe = dentry->d_fsdata;
560
561         BUG_ON(idx < 0);
562         if (idx == 0) {
563                 ovl_path_upper(dentry, path);
564                 if (path->dentry)
565                         return oe->numlower ? 1 : -1;
566                 idx++;
567         }
568         BUG_ON(idx > oe->numlower);
569         path->dentry = oe->lowerstack[idx - 1].dentry;
570         path->mnt = oe->lowerstack[idx - 1].layer->mnt;
571
572         return (idx < oe->numlower) ? idx + 1 : -1;
573 }
574
575 static int ovl_find_layer(struct ovl_fs *ofs, struct ovl_path *path)
576 {
577         int i;
578
579         for (i = 0; i < ofs->numlower; i++) {
580                 if (ofs->lower_layers[i].mnt == path->layer->mnt)
581                         break;
582         }
583
584         return i;
585 }
586
587 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
588                           unsigned int flags)
589 {
590         struct ovl_entry *oe;
591         const struct cred *old_cred;
592         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
593         struct ovl_entry *poe = dentry->d_parent->d_fsdata;
594         struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
595         struct ovl_path *stack = NULL;
596         struct dentry *upperdir, *upperdentry = NULL;
597         struct dentry *index = NULL;
598         unsigned int ctr = 0;
599         struct inode *inode = NULL;
600         bool upperopaque = false;
601         char *upperredirect = NULL;
602         struct dentry *this;
603         unsigned int i;
604         int err;
605         struct ovl_lookup_data d = {
606                 .name = dentry->d_name,
607                 .is_dir = false,
608                 .opaque = false,
609                 .stop = false,
610                 .last = !poe->numlower,
611                 .redirect = NULL,
612         };
613
614         if (dentry->d_name.len > ofs->namelen)
615                 return ERR_PTR(-ENAMETOOLONG);
616
617         old_cred = ovl_override_creds(dentry->d_sb);
618         upperdir = ovl_dentry_upper(dentry->d_parent);
619         if (upperdir) {
620                 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
621                 if (err)
622                         goto out;
623
624                 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
625                         dput(upperdentry);
626                         err = -EREMOTE;
627                         goto out;
628                 }
629                 if (upperdentry && !d.is_dir) {
630                         BUG_ON(!d.stop || d.redirect);
631                         /*
632                          * Lookup copy up origin by decoding origin file handle.
633                          * We may get a disconnected dentry, which is fine,
634                          * because we only need to hold the origin inode in
635                          * cache and use its inode number.  We may even get a
636                          * connected dentry, that is not under any of the lower
637                          * layers root.  That is also fine for using it's inode
638                          * number - it's the same as if we held a reference
639                          * to a dentry in lower layer that was moved under us.
640                          */
641                         err = ovl_check_origin(upperdentry, roe->lowerstack,
642                                                roe->numlower, &stack, &ctr);
643                         if (err)
644                                 goto out_put_upper;
645                 }
646
647                 if (d.redirect) {
648                         err = -ENOMEM;
649                         upperredirect = kstrdup(d.redirect, GFP_KERNEL);
650                         if (!upperredirect)
651                                 goto out_put_upper;
652                         if (d.redirect[0] == '/')
653                                 poe = roe;
654                 }
655                 upperopaque = d.opaque;
656         }
657
658         if (!d.stop && poe->numlower) {
659                 err = -ENOMEM;
660                 stack = kcalloc(ofs->numlower, sizeof(struct ovl_path),
661                                 GFP_KERNEL);
662                 if (!stack)
663                         goto out_put_upper;
664         }
665
666         for (i = 0; !d.stop && i < poe->numlower; i++) {
667                 struct ovl_path lower = poe->lowerstack[i];
668
669                 d.last = i == poe->numlower - 1;
670                 err = ovl_lookup_layer(lower.dentry, &d, &this);
671                 if (err)
672                         goto out_put;
673
674                 if (!this)
675                         continue;
676
677                 stack[ctr].dentry = this;
678                 stack[ctr].layer = lower.layer;
679                 ctr++;
680
681                 if (d.stop)
682                         break;
683
684                 /*
685                  * Following redirects can have security consequences: it's like
686                  * a symlink into the lower layer without the permission checks.
687                  * This is only a problem if the upper layer is untrusted (e.g
688                  * comes from an USB drive).  This can allow a non-readable file
689                  * or directory to become readable.
690                  *
691                  * Only following redirects when redirects are enabled disables
692                  * this attack vector when not necessary.
693                  */
694                 err = -EPERM;
695                 if (d.redirect && !ofs->config.redirect_follow) {
696                         pr_warn_ratelimited("overlay: refusing to follow redirect for (%pd2)\n", dentry);
697                         goto out_put;
698                 }
699
700                 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
701                         poe = roe;
702
703                         /* Find the current layer on the root dentry */
704                         i = ovl_find_layer(ofs, &lower);
705                         if (WARN_ON(i == ofs->numlower))
706                                 break;
707                 }
708         }
709
710         /* Lookup index by lower inode and verify it matches upper inode */
711         if (ctr && !d.is_dir && ovl_indexdir(dentry->d_sb)) {
712                 struct dentry *origin = stack[0].dentry;
713
714                 index = ovl_lookup_index(dentry, upperdentry, origin);
715                 if (IS_ERR(index)) {
716                         err = PTR_ERR(index);
717                         index = NULL;
718                         goto out_put;
719                 }
720         }
721
722         oe = ovl_alloc_entry(ctr);
723         err = -ENOMEM;
724         if (!oe)
725                 goto out_put;
726
727         oe->opaque = upperopaque;
728         memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
729         dentry->d_fsdata = oe;
730
731         if (upperdentry)
732                 ovl_dentry_set_upper_alias(dentry);
733         else if (index)
734                 upperdentry = dget(index);
735
736         if (upperdentry || ctr) {
737                 inode = ovl_get_inode(dentry, upperdentry, index);
738                 err = PTR_ERR(inode);
739                 if (IS_ERR(inode))
740                         goto out_free_oe;
741
742                 OVL_I(inode)->redirect = upperredirect;
743                 if (index)
744                         ovl_set_flag(OVL_INDEX, inode);
745         }
746
747         revert_creds(old_cred);
748         dput(index);
749         kfree(stack);
750         kfree(d.redirect);
751         d_add(dentry, inode);
752
753         return NULL;
754
755 out_free_oe:
756         dentry->d_fsdata = NULL;
757         kfree(oe);
758 out_put:
759         dput(index);
760         for (i = 0; i < ctr; i++)
761                 dput(stack[i].dentry);
762         kfree(stack);
763 out_put_upper:
764         dput(upperdentry);
765         kfree(upperredirect);
766 out:
767         kfree(d.redirect);
768         revert_creds(old_cred);
769         return ERR_PTR(err);
770 }
771
772 bool ovl_lower_positive(struct dentry *dentry)
773 {
774         struct ovl_entry *oe = dentry->d_fsdata;
775         struct ovl_entry *poe = dentry->d_parent->d_fsdata;
776         const struct qstr *name = &dentry->d_name;
777         unsigned int i;
778         bool positive = false;
779         bool done = false;
780
781         /*
782          * If dentry is negative, then lower is positive iff this is a
783          * whiteout.
784          */
785         if (!dentry->d_inode)
786                 return oe->opaque;
787
788         /* Negative upper -> positive lower */
789         if (!ovl_dentry_upper(dentry))
790                 return true;
791
792         /* Positive upper -> have to look up lower to see whether it exists */
793         for (i = 0; !done && !positive && i < poe->numlower; i++) {
794                 struct dentry *this;
795                 struct dentry *lowerdir = poe->lowerstack[i].dentry;
796
797                 this = lookup_one_len_unlocked(name->name, lowerdir,
798                                                name->len);
799                 if (IS_ERR(this)) {
800                         switch (PTR_ERR(this)) {
801                         case -ENOENT:
802                         case -ENAMETOOLONG:
803                                 break;
804
805                         default:
806                                 /*
807                                  * Assume something is there, we just couldn't
808                                  * access it.
809                                  */
810                                 positive = true;
811                                 break;
812                         }
813                 } else {
814                         if (this->d_inode) {
815                                 positive = !ovl_is_whiteout(this);
816                                 done = true;
817                         }
818                         dput(this);
819                 }
820         }
821
822         return positive;
823 }