914e77e10f0f3e6ef0867cb6f593c91d07a42c11
[sfrench/cifs-2.6.git] / fs / overlayfs / readdir.c
1 /*
2  *
3  * Copyright (C) 2011 Novell 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/slab.h>
12 #include <linux/namei.h>
13 #include <linux/file.h>
14 #include <linux/xattr.h>
15 #include <linux/rbtree.h>
16 #include <linux/security.h>
17 #include <linux/cred.h>
18 #include <linux/ratelimit.h>
19 #include "overlayfs.h"
20
21 struct ovl_cache_entry {
22         unsigned int len;
23         unsigned int type;
24         u64 real_ino;
25         u64 ino;
26         struct list_head l_node;
27         struct rb_node node;
28         struct ovl_cache_entry *next_maybe_whiteout;
29         bool is_upper;
30         bool is_whiteout;
31         char name[];
32 };
33
34 struct ovl_dir_cache {
35         long refcount;
36         u64 version;
37         struct list_head entries;
38         struct rb_root root;
39 };
40
41 struct ovl_readdir_data {
42         struct dir_context ctx;
43         struct dentry *dentry;
44         bool is_lowest;
45         struct rb_root *root;
46         struct list_head *list;
47         struct list_head middle;
48         struct ovl_cache_entry *first_maybe_whiteout;
49         int count;
50         int err;
51         bool is_upper;
52         bool d_type_supported;
53 };
54
55 struct ovl_dir_file {
56         bool is_real;
57         bool is_upper;
58         struct ovl_dir_cache *cache;
59         struct list_head *cursor;
60         struct file *realfile;
61         struct file *upperfile;
62 };
63
64 static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
65 {
66         return rb_entry(n, struct ovl_cache_entry, node);
67 }
68
69 static bool ovl_cache_entry_find_link(const char *name, int len,
70                                       struct rb_node ***link,
71                                       struct rb_node **parent)
72 {
73         bool found = false;
74         struct rb_node **newp = *link;
75
76         while (!found && *newp) {
77                 int cmp;
78                 struct ovl_cache_entry *tmp;
79
80                 *parent = *newp;
81                 tmp = ovl_cache_entry_from_node(*newp);
82                 cmp = strncmp(name, tmp->name, len);
83                 if (cmp > 0)
84                         newp = &tmp->node.rb_right;
85                 else if (cmp < 0 || len < tmp->len)
86                         newp = &tmp->node.rb_left;
87                 else
88                         found = true;
89         }
90         *link = newp;
91
92         return found;
93 }
94
95 static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
96                                                     const char *name, int len)
97 {
98         struct rb_node *node = root->rb_node;
99         int cmp;
100
101         while (node) {
102                 struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
103
104                 cmp = strncmp(name, p->name, len);
105                 if (cmp > 0)
106                         node = p->node.rb_right;
107                 else if (cmp < 0 || len < p->len)
108                         node = p->node.rb_left;
109                 else
110                         return p;
111         }
112
113         return NULL;
114 }
115
116 static bool ovl_calc_d_ino(struct ovl_readdir_data *rdd,
117                            struct ovl_cache_entry *p)
118 {
119         /* Don't care if not doing ovl_iter() */
120         if (!rdd->dentry)
121                 return false;
122
123         /* Always recalc d_ino for parent */
124         if (strcmp(p->name, "..") == 0)
125                 return true;
126
127         /* If this is lower, then native d_ino will do */
128         if (!rdd->is_upper)
129                 return false;
130
131         /*
132          * Recalc d_ino for '.' and for all entries if dir is impure (contains
133          * copied up entries)
134          */
135         if ((p->name[0] == '.' && p->len == 1) ||
136             ovl_test_flag(OVL_IMPURE, d_inode(rdd->dentry)))
137                 return true;
138
139         return false;
140 }
141
142 static struct ovl_cache_entry *ovl_cache_entry_new(struct ovl_readdir_data *rdd,
143                                                    const char *name, int len,
144                                                    u64 ino, unsigned int d_type)
145 {
146         struct ovl_cache_entry *p;
147         size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
148
149         p = kmalloc(size, GFP_KERNEL);
150         if (!p)
151                 return NULL;
152
153         memcpy(p->name, name, len);
154         p->name[len] = '\0';
155         p->len = len;
156         p->type = d_type;
157         p->real_ino = ino;
158         p->ino = ino;
159         /* Defer setting d_ino for upper entry to ovl_iterate() */
160         if (ovl_calc_d_ino(rdd, p))
161                 p->ino = 0;
162         p->is_upper = rdd->is_upper;
163         p->is_whiteout = false;
164
165         if (d_type == DT_CHR) {
166                 p->next_maybe_whiteout = rdd->first_maybe_whiteout;
167                 rdd->first_maybe_whiteout = p;
168         }
169         return p;
170 }
171
172 static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
173                                   const char *name, int len, u64 ino,
174                                   unsigned int d_type)
175 {
176         struct rb_node **newp = &rdd->root->rb_node;
177         struct rb_node *parent = NULL;
178         struct ovl_cache_entry *p;
179
180         if (ovl_cache_entry_find_link(name, len, &newp, &parent))
181                 return 0;
182
183         p = ovl_cache_entry_new(rdd, name, len, ino, d_type);
184         if (p == NULL) {
185                 rdd->err = -ENOMEM;
186                 return -ENOMEM;
187         }
188
189         list_add_tail(&p->l_node, rdd->list);
190         rb_link_node(&p->node, parent, newp);
191         rb_insert_color(&p->node, rdd->root);
192
193         return 0;
194 }
195
196 static int ovl_fill_lowest(struct ovl_readdir_data *rdd,
197                            const char *name, int namelen,
198                            loff_t offset, u64 ino, unsigned int d_type)
199 {
200         struct ovl_cache_entry *p;
201
202         p = ovl_cache_entry_find(rdd->root, name, namelen);
203         if (p) {
204                 list_move_tail(&p->l_node, &rdd->middle);
205         } else {
206                 p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
207                 if (p == NULL)
208                         rdd->err = -ENOMEM;
209                 else
210                         list_add_tail(&p->l_node, &rdd->middle);
211         }
212
213         return rdd->err;
214 }
215
216 void ovl_cache_free(struct list_head *list)
217 {
218         struct ovl_cache_entry *p;
219         struct ovl_cache_entry *n;
220
221         list_for_each_entry_safe(p, n, list, l_node)
222                 kfree(p);
223
224         INIT_LIST_HEAD(list);
225 }
226
227 void ovl_dir_cache_free(struct inode *inode)
228 {
229         struct ovl_dir_cache *cache = ovl_dir_cache(inode);
230
231         if (cache) {
232                 ovl_cache_free(&cache->entries);
233                 kfree(cache);
234         }
235 }
236
237 static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
238 {
239         struct ovl_dir_cache *cache = od->cache;
240
241         WARN_ON(cache->refcount <= 0);
242         cache->refcount--;
243         if (!cache->refcount) {
244                 if (ovl_dir_cache(d_inode(dentry)) == cache)
245                         ovl_set_dir_cache(d_inode(dentry), NULL);
246
247                 ovl_cache_free(&cache->entries);
248                 kfree(cache);
249         }
250 }
251
252 static int ovl_fill_merge(struct dir_context *ctx, const char *name,
253                           int namelen, loff_t offset, u64 ino,
254                           unsigned int d_type)
255 {
256         struct ovl_readdir_data *rdd =
257                 container_of(ctx, struct ovl_readdir_data, ctx);
258
259         rdd->count++;
260         if (!rdd->is_lowest)
261                 return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
262         else
263                 return ovl_fill_lowest(rdd, name, namelen, offset, ino, d_type);
264 }
265
266 static int ovl_check_whiteouts(struct dentry *dir, struct ovl_readdir_data *rdd)
267 {
268         int err;
269         struct ovl_cache_entry *p;
270         struct dentry *dentry;
271         const struct cred *old_cred;
272
273         old_cred = ovl_override_creds(rdd->dentry->d_sb);
274
275         err = down_write_killable(&dir->d_inode->i_rwsem);
276         if (!err) {
277                 while (rdd->first_maybe_whiteout) {
278                         p = rdd->first_maybe_whiteout;
279                         rdd->first_maybe_whiteout = p->next_maybe_whiteout;
280                         dentry = lookup_one_len(p->name, dir, p->len);
281                         if (!IS_ERR(dentry)) {
282                                 p->is_whiteout = ovl_is_whiteout(dentry);
283                                 dput(dentry);
284                         }
285                 }
286                 inode_unlock(dir->d_inode);
287         }
288         revert_creds(old_cred);
289
290         return err;
291 }
292
293 static inline int ovl_dir_read(struct path *realpath,
294                                struct ovl_readdir_data *rdd)
295 {
296         struct file *realfile;
297         int err;
298
299         realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
300         if (IS_ERR(realfile))
301                 return PTR_ERR(realfile);
302
303         rdd->first_maybe_whiteout = NULL;
304         rdd->ctx.pos = 0;
305         do {
306                 rdd->count = 0;
307                 rdd->err = 0;
308                 err = iterate_dir(realfile, &rdd->ctx);
309                 if (err >= 0)
310                         err = rdd->err;
311         } while (!err && rdd->count);
312
313         if (!err && rdd->first_maybe_whiteout && rdd->dentry)
314                 err = ovl_check_whiteouts(realpath->dentry, rdd);
315
316         fput(realfile);
317
318         return err;
319 }
320
321 /*
322  * Can we iterate real dir directly?
323  *
324  * Non-merge dir may contain whiteouts from a time it was a merge upper, before
325  * lower dir was removed under it and possibly before it was rotated from upper
326  * to lower layer.
327  */
328 static bool ovl_dir_is_real(struct dentry *dir)
329 {
330         return !ovl_test_flag(OVL_WHITEOUTS, d_inode(dir));
331 }
332
333 static void ovl_dir_reset(struct file *file)
334 {
335         struct ovl_dir_file *od = file->private_data;
336         struct ovl_dir_cache *cache = od->cache;
337         struct dentry *dentry = file->f_path.dentry;
338         bool is_real;
339
340         if (cache && ovl_dentry_version_get(dentry) != cache->version) {
341                 ovl_cache_put(od, dentry);
342                 od->cache = NULL;
343                 od->cursor = NULL;
344         }
345         is_real = ovl_dir_is_real(dentry);
346         if (od->is_real != is_real) {
347                 /* is_real can only become false when dir is copied up */
348                 if (WARN_ON(is_real))
349                         return;
350                 od->is_real = false;
351         }
352 }
353
354 static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list,
355         struct rb_root *root)
356 {
357         int err;
358         struct path realpath;
359         struct ovl_readdir_data rdd = {
360                 .ctx.actor = ovl_fill_merge,
361                 .dentry = dentry,
362                 .list = list,
363                 .root = root,
364                 .is_lowest = false,
365         };
366         int idx, next;
367
368         for (idx = 0; idx != -1; idx = next) {
369                 next = ovl_path_next(idx, dentry, &realpath);
370                 rdd.is_upper = ovl_dentry_upper(dentry) == realpath.dentry;
371
372                 if (next != -1) {
373                         err = ovl_dir_read(&realpath, &rdd);
374                         if (err)
375                                 break;
376                 } else {
377                         /*
378                          * Insert lowest layer entries before upper ones, this
379                          * allows offsets to be reasonably constant
380                          */
381                         list_add(&rdd.middle, rdd.list);
382                         rdd.is_lowest = true;
383                         err = ovl_dir_read(&realpath, &rdd);
384                         list_del(&rdd.middle);
385                 }
386         }
387         return err;
388 }
389
390 static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
391 {
392         struct list_head *p;
393         loff_t off = 0;
394
395         list_for_each(p, &od->cache->entries) {
396                 if (off >= pos)
397                         break;
398                 off++;
399         }
400         /* Cursor is safe since the cache is stable */
401         od->cursor = p;
402 }
403
404 static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
405 {
406         int res;
407         struct ovl_dir_cache *cache;
408
409         cache = ovl_dir_cache(d_inode(dentry));
410         if (cache && ovl_dentry_version_get(dentry) == cache->version) {
411                 WARN_ON(!cache->refcount);
412                 cache->refcount++;
413                 return cache;
414         }
415         ovl_set_dir_cache(d_inode(dentry), NULL);
416
417         cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
418         if (!cache)
419                 return ERR_PTR(-ENOMEM);
420
421         cache->refcount = 1;
422         INIT_LIST_HEAD(&cache->entries);
423         cache->root = RB_ROOT;
424
425         res = ovl_dir_read_merged(dentry, &cache->entries, &cache->root);
426         if (res) {
427                 ovl_cache_free(&cache->entries);
428                 kfree(cache);
429                 return ERR_PTR(res);
430         }
431
432         cache->version = ovl_dentry_version_get(dentry);
433         ovl_set_dir_cache(d_inode(dentry), cache);
434
435         return cache;
436 }
437
438 /*
439  * Set d_ino for upper entries. Non-upper entries should always report
440  * the uppermost real inode ino and should not call this function.
441  *
442  * When not all layer are on same fs, report real ino also for upper.
443  *
444  * When all layers are on the same fs, and upper has a reference to
445  * copy up origin, call vfs_getattr() on the overlay entry to make
446  * sure that d_ino will be consistent with st_ino from stat(2).
447  */
448 static int ovl_cache_update_ino(struct path *path, struct ovl_cache_entry *p)
449
450 {
451         struct dentry *dir = path->dentry;
452         struct dentry *this = NULL;
453         enum ovl_path_type type;
454         u64 ino = p->real_ino;
455         int err = 0;
456
457         if (!ovl_same_sb(dir->d_sb))
458                 goto out;
459
460         if (p->name[0] == '.') {
461                 if (p->len == 1) {
462                         this = dget(dir);
463                         goto get;
464                 }
465                 if (p->len == 2 && p->name[1] == '.') {
466                         /* we shall not be moved */
467                         this = dget(dir->d_parent);
468                         goto get;
469                 }
470         }
471         this = lookup_one_len(p->name, dir, p->len);
472         if (IS_ERR_OR_NULL(this) || !this->d_inode) {
473                 if (IS_ERR(this)) {
474                         err = PTR_ERR(this);
475                         this = NULL;
476                         goto fail;
477                 }
478                 goto out;
479         }
480
481 get:
482         type = ovl_path_type(this);
483         if (OVL_TYPE_ORIGIN(type)) {
484                 struct kstat stat;
485                 struct path statpath = *path;
486
487                 statpath.dentry = this;
488                 err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
489                 if (err)
490                         goto fail;
491
492                 WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev);
493                 ino = stat.ino;
494         }
495
496 out:
497         p->ino = ino;
498         dput(this);
499         return err;
500
501 fail:
502         pr_warn_ratelimited("overlay: failed to look up (%s) for ino (%i)\n",
503                             p->name, err);
504         goto out;
505 }
506
507 static int ovl_fill_plain(struct dir_context *ctx, const char *name,
508                           int namelen, loff_t offset, u64 ino,
509                           unsigned int d_type)
510 {
511         struct ovl_cache_entry *p;
512         struct ovl_readdir_data *rdd =
513                 container_of(ctx, struct ovl_readdir_data, ctx);
514
515         rdd->count++;
516         p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
517         if (p == NULL) {
518                 rdd->err = -ENOMEM;
519                 return -ENOMEM;
520         }
521         list_add_tail(&p->l_node, rdd->list);
522
523         return 0;
524 }
525
526 static int ovl_dir_read_impure(struct path *path,  struct list_head *list,
527                                struct rb_root *root)
528 {
529         int err;
530         struct path realpath;
531         struct ovl_cache_entry *p, *n;
532         struct ovl_readdir_data rdd = {
533                 .ctx.actor = ovl_fill_plain,
534                 .list = list,
535                 .root = root,
536         };
537
538         INIT_LIST_HEAD(list);
539         *root = RB_ROOT;
540         ovl_path_upper(path->dentry, &realpath);
541
542         err = ovl_dir_read(&realpath, &rdd);
543         if (err)
544                 return err;
545
546         list_for_each_entry_safe(p, n, list, l_node) {
547                 if (strcmp(p->name, ".") != 0 &&
548                     strcmp(p->name, "..") != 0) {
549                         err = ovl_cache_update_ino(path, p);
550                         if (err)
551                                 return err;
552                 }
553                 if (p->ino == p->real_ino) {
554                         list_del(&p->l_node);
555                         kfree(p);
556                 } else {
557                         struct rb_node **newp = &root->rb_node;
558                         struct rb_node *parent = NULL;
559
560                         if (WARN_ON(ovl_cache_entry_find_link(p->name, p->len,
561                                                               &newp, &parent)))
562                                 return -EIO;
563
564                         rb_link_node(&p->node, parent, newp);
565                         rb_insert_color(&p->node, root);
566                 }
567         }
568         return 0;
569 }
570
571 static struct ovl_dir_cache *ovl_cache_get_impure(struct path *path)
572 {
573         int res;
574         struct dentry *dentry = path->dentry;
575         struct ovl_dir_cache *cache;
576
577         cache = ovl_dir_cache(d_inode(dentry));
578         if (cache && ovl_dentry_version_get(dentry) == cache->version)
579                 return cache;
580
581         /* Impure cache is not refcounted, free it here */
582         ovl_dir_cache_free(d_inode(dentry));
583         ovl_set_dir_cache(d_inode(dentry), NULL);
584
585         cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
586         if (!cache)
587                 return ERR_PTR(-ENOMEM);
588
589         res = ovl_dir_read_impure(path, &cache->entries, &cache->root);
590         if (res) {
591                 ovl_cache_free(&cache->entries);
592                 kfree(cache);
593                 return ERR_PTR(res);
594         }
595         if (list_empty(&cache->entries)) {
596                 /* Good oportunity to get rid of an unnecessary "impure" flag */
597                 ovl_do_removexattr(ovl_dentry_upper(dentry), OVL_XATTR_IMPURE);
598                 ovl_clear_flag(OVL_IMPURE, d_inode(dentry));
599                 kfree(cache);
600                 return NULL;
601         }
602
603         cache->version = ovl_dentry_version_get(dentry);
604         ovl_set_dir_cache(d_inode(dentry), cache);
605
606         return cache;
607 }
608
609 struct ovl_readdir_translate {
610         struct dir_context *orig_ctx;
611         struct ovl_dir_cache *cache;
612         struct dir_context ctx;
613         u64 parent_ino;
614 };
615
616 static int ovl_fill_real(struct dir_context *ctx, const char *name,
617                            int namelen, loff_t offset, u64 ino,
618                            unsigned int d_type)
619 {
620         struct ovl_readdir_translate *rdt =
621                 container_of(ctx, struct ovl_readdir_translate, ctx);
622         struct dir_context *orig_ctx = rdt->orig_ctx;
623
624         if (rdt->parent_ino && strcmp(name, "..") == 0)
625                 ino = rdt->parent_ino;
626         else if (rdt->cache) {
627                 struct ovl_cache_entry *p;
628
629                 p = ovl_cache_entry_find(&rdt->cache->root, name, namelen);
630                 if (p)
631                         ino = p->ino;
632         }
633
634         return orig_ctx->actor(orig_ctx, name, namelen, offset, ino, d_type);
635 }
636
637 static int ovl_iterate_real(struct file *file, struct dir_context *ctx)
638 {
639         int err;
640         struct ovl_dir_file *od = file->private_data;
641         struct dentry *dir = file->f_path.dentry;
642         struct ovl_readdir_translate rdt = {
643                 .ctx.actor = ovl_fill_real,
644                 .orig_ctx = ctx,
645         };
646
647         if (OVL_TYPE_MERGE(ovl_path_type(dir->d_parent))) {
648                 struct kstat stat;
649                 struct path statpath = file->f_path;
650
651                 statpath.dentry = dir->d_parent;
652                 err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
653                 if (err)
654                         return err;
655
656                 WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev);
657                 rdt.parent_ino = stat.ino;
658         }
659
660         if (ovl_test_flag(OVL_IMPURE, d_inode(dir))) {
661                 rdt.cache = ovl_cache_get_impure(&file->f_path);
662                 if (IS_ERR(rdt.cache))
663                         return PTR_ERR(rdt.cache);
664         }
665
666         return iterate_dir(od->realfile, &rdt.ctx);
667 }
668
669
670 static int ovl_iterate(struct file *file, struct dir_context *ctx)
671 {
672         struct ovl_dir_file *od = file->private_data;
673         struct dentry *dentry = file->f_path.dentry;
674         struct ovl_cache_entry *p;
675         int err;
676
677         if (!ctx->pos)
678                 ovl_dir_reset(file);
679
680         if (od->is_real) {
681                 /*
682                  * If parent is merge, then need to adjust d_ino for '..', if
683                  * dir is impure then need to adjust d_ino for copied up
684                  * entries.
685                  */
686                 if (ovl_same_sb(dentry->d_sb) &&
687                     (ovl_test_flag(OVL_IMPURE, d_inode(dentry)) ||
688                      OVL_TYPE_MERGE(ovl_path_type(dentry->d_parent)))) {
689                         return ovl_iterate_real(file, ctx);
690                 }
691                 return iterate_dir(od->realfile, ctx);
692         }
693
694         if (!od->cache) {
695                 struct ovl_dir_cache *cache;
696
697                 cache = ovl_cache_get(dentry);
698                 if (IS_ERR(cache))
699                         return PTR_ERR(cache);
700
701                 od->cache = cache;
702                 ovl_seek_cursor(od, ctx->pos);
703         }
704
705         while (od->cursor != &od->cache->entries) {
706                 p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
707                 if (!p->is_whiteout) {
708                         if (!p->ino) {
709                                 err = ovl_cache_update_ino(&file->f_path, p);
710                                 if (err)
711                                         return err;
712                         }
713                         if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
714                                 break;
715                 }
716                 od->cursor = p->l_node.next;
717                 ctx->pos++;
718         }
719         return 0;
720 }
721
722 static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
723 {
724         loff_t res;
725         struct ovl_dir_file *od = file->private_data;
726
727         inode_lock(file_inode(file));
728         if (!file->f_pos)
729                 ovl_dir_reset(file);
730
731         if (od->is_real) {
732                 res = vfs_llseek(od->realfile, offset, origin);
733                 file->f_pos = od->realfile->f_pos;
734         } else {
735                 res = -EINVAL;
736
737                 switch (origin) {
738                 case SEEK_CUR:
739                         offset += file->f_pos;
740                         break;
741                 case SEEK_SET:
742                         break;
743                 default:
744                         goto out_unlock;
745                 }
746                 if (offset < 0)
747                         goto out_unlock;
748
749                 if (offset != file->f_pos) {
750                         file->f_pos = offset;
751                         if (od->cache)
752                                 ovl_seek_cursor(od, offset);
753                 }
754                 res = offset;
755         }
756 out_unlock:
757         inode_unlock(file_inode(file));
758
759         return res;
760 }
761
762 static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
763                          int datasync)
764 {
765         struct ovl_dir_file *od = file->private_data;
766         struct dentry *dentry = file->f_path.dentry;
767         struct file *realfile = od->realfile;
768
769         /*
770          * Need to check if we started out being a lower dir, but got copied up
771          */
772         if (!od->is_upper && OVL_TYPE_UPPER(ovl_path_type(dentry))) {
773                 struct inode *inode = file_inode(file);
774
775                 realfile = lockless_dereference(od->upperfile);
776                 if (!realfile) {
777                         struct path upperpath;
778
779                         ovl_path_upper(dentry, &upperpath);
780                         realfile = ovl_path_open(&upperpath, O_RDONLY);
781
782                         inode_lock(inode);
783                         if (!od->upperfile) {
784                                 if (IS_ERR(realfile)) {
785                                         inode_unlock(inode);
786                                         return PTR_ERR(realfile);
787                                 }
788                                 smp_store_release(&od->upperfile, realfile);
789                         } else {
790                                 /* somebody has beaten us to it */
791                                 if (!IS_ERR(realfile))
792                                         fput(realfile);
793                                 realfile = od->upperfile;
794                         }
795                         inode_unlock(inode);
796                 }
797         }
798
799         return vfs_fsync_range(realfile, start, end, datasync);
800 }
801
802 static int ovl_dir_release(struct inode *inode, struct file *file)
803 {
804         struct ovl_dir_file *od = file->private_data;
805
806         if (od->cache) {
807                 inode_lock(inode);
808                 ovl_cache_put(od, file->f_path.dentry);
809                 inode_unlock(inode);
810         }
811         fput(od->realfile);
812         if (od->upperfile)
813                 fput(od->upperfile);
814         kfree(od);
815
816         return 0;
817 }
818
819 static int ovl_dir_open(struct inode *inode, struct file *file)
820 {
821         struct path realpath;
822         struct file *realfile;
823         struct ovl_dir_file *od;
824         enum ovl_path_type type;
825
826         od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
827         if (!od)
828                 return -ENOMEM;
829
830         type = ovl_path_real(file->f_path.dentry, &realpath);
831         realfile = ovl_path_open(&realpath, file->f_flags);
832         if (IS_ERR(realfile)) {
833                 kfree(od);
834                 return PTR_ERR(realfile);
835         }
836         od->realfile = realfile;
837         od->is_real = ovl_dir_is_real(file->f_path.dentry);
838         od->is_upper = OVL_TYPE_UPPER(type);
839         file->private_data = od;
840
841         return 0;
842 }
843
844 const struct file_operations ovl_dir_operations = {
845         .read           = generic_read_dir,
846         .open           = ovl_dir_open,
847         .iterate        = ovl_iterate,
848         .llseek         = ovl_dir_llseek,
849         .fsync          = ovl_dir_fsync,
850         .release        = ovl_dir_release,
851 };
852
853 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
854 {
855         int err;
856         struct ovl_cache_entry *p, *n;
857         struct rb_root root = RB_ROOT;
858
859         err = ovl_dir_read_merged(dentry, list, &root);
860         if (err)
861                 return err;
862
863         err = 0;
864
865         list_for_each_entry_safe(p, n, list, l_node) {
866                 /*
867                  * Select whiteouts in upperdir, they should
868                  * be cleared when deleting this directory.
869                  */
870                 if (p->is_whiteout) {
871                         if (p->is_upper)
872                                 continue;
873                         goto del_entry;
874                 }
875
876                 if (p->name[0] == '.') {
877                         if (p->len == 1)
878                                 goto del_entry;
879                         if (p->len == 2 && p->name[1] == '.')
880                                 goto del_entry;
881                 }
882                 err = -ENOTEMPTY;
883                 break;
884
885 del_entry:
886                 list_del(&p->l_node);
887                 kfree(p);
888         }
889
890         return err;
891 }
892
893 void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
894 {
895         struct ovl_cache_entry *p;
896
897         inode_lock_nested(upper->d_inode, I_MUTEX_CHILD);
898         list_for_each_entry(p, list, l_node) {
899                 struct dentry *dentry;
900
901                 if (WARN_ON(!p->is_whiteout || !p->is_upper))
902                         continue;
903
904                 dentry = lookup_one_len(p->name, upper, p->len);
905                 if (IS_ERR(dentry)) {
906                         pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
907                                upper->d_name.name, p->len, p->name,
908                                (int) PTR_ERR(dentry));
909                         continue;
910                 }
911                 if (dentry->d_inode)
912                         ovl_cleanup(upper->d_inode, dentry);
913                 dput(dentry);
914         }
915         inode_unlock(upper->d_inode);
916 }
917
918 static int ovl_check_d_type(struct dir_context *ctx, const char *name,
919                           int namelen, loff_t offset, u64 ino,
920                           unsigned int d_type)
921 {
922         struct ovl_readdir_data *rdd =
923                 container_of(ctx, struct ovl_readdir_data, ctx);
924
925         /* Even if d_type is not supported, DT_DIR is returned for . and .. */
926         if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
927                 return 0;
928
929         if (d_type != DT_UNKNOWN)
930                 rdd->d_type_supported = true;
931
932         return 0;
933 }
934
935 /*
936  * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
937  * if error is encountered.
938  */
939 int ovl_check_d_type_supported(struct path *realpath)
940 {
941         int err;
942         struct ovl_readdir_data rdd = {
943                 .ctx.actor = ovl_check_d_type,
944                 .d_type_supported = false,
945         };
946
947         err = ovl_dir_read(realpath, &rdd);
948         if (err)
949                 return err;
950
951         return rdd.d_type_supported;
952 }
953
954 static void ovl_workdir_cleanup_recurse(struct path *path, int level)
955 {
956         int err;
957         struct inode *dir = path->dentry->d_inode;
958         LIST_HEAD(list);
959         struct rb_root root = RB_ROOT;
960         struct ovl_cache_entry *p;
961         struct ovl_readdir_data rdd = {
962                 .ctx.actor = ovl_fill_merge,
963                 .dentry = NULL,
964                 .list = &list,
965                 .root = &root,
966                 .is_lowest = false,
967         };
968
969         err = ovl_dir_read(path, &rdd);
970         if (err)
971                 goto out;
972
973         inode_lock_nested(dir, I_MUTEX_PARENT);
974         list_for_each_entry(p, &list, l_node) {
975                 struct dentry *dentry;
976
977                 if (p->name[0] == '.') {
978                         if (p->len == 1)
979                                 continue;
980                         if (p->len == 2 && p->name[1] == '.')
981                                 continue;
982                 }
983                 dentry = lookup_one_len(p->name, path->dentry, p->len);
984                 if (IS_ERR(dentry))
985                         continue;
986                 if (dentry->d_inode)
987                         ovl_workdir_cleanup(dir, path->mnt, dentry, level);
988                 dput(dentry);
989         }
990         inode_unlock(dir);
991 out:
992         ovl_cache_free(&list);
993 }
994
995 void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
996                          struct dentry *dentry, int level)
997 {
998         int err;
999
1000         if (!d_is_dir(dentry) || level > 1) {
1001                 ovl_cleanup(dir, dentry);
1002                 return;
1003         }
1004
1005         err = ovl_do_rmdir(dir, dentry);
1006         if (err) {
1007                 struct path path = { .mnt = mnt, .dentry = dentry };
1008
1009                 inode_unlock(dir);
1010                 ovl_workdir_cleanup_recurse(&path, level + 1);
1011                 inode_lock_nested(dir, I_MUTEX_PARENT);
1012                 ovl_cleanup(dir, dentry);
1013         }
1014 }
1015
1016 int ovl_indexdir_cleanup(struct dentry *dentry, struct vfsmount *mnt,
1017                          struct ovl_path *lower, unsigned int numlower)
1018 {
1019         int err;
1020         struct dentry *index = NULL;
1021         struct inode *dir = dentry->d_inode;
1022         struct path path = { .mnt = mnt, .dentry = dentry };
1023         LIST_HEAD(list);
1024         struct rb_root root = RB_ROOT;
1025         struct ovl_cache_entry *p;
1026         struct ovl_readdir_data rdd = {
1027                 .ctx.actor = ovl_fill_merge,
1028                 .dentry = NULL,
1029                 .list = &list,
1030                 .root = &root,
1031                 .is_lowest = false,
1032         };
1033
1034         err = ovl_dir_read(&path, &rdd);
1035         if (err)
1036                 goto out;
1037
1038         inode_lock_nested(dir, I_MUTEX_PARENT);
1039         list_for_each_entry(p, &list, l_node) {
1040                 if (p->name[0] == '.') {
1041                         if (p->len == 1)
1042                                 continue;
1043                         if (p->len == 2 && p->name[1] == '.')
1044                                 continue;
1045                 }
1046                 index = lookup_one_len(p->name, dentry, p->len);
1047                 if (IS_ERR(index)) {
1048                         err = PTR_ERR(index);
1049                         index = NULL;
1050                         break;
1051                 }
1052                 err = ovl_verify_index(index, lower, numlower);
1053                 /* Cleanup stale and orphan index entries */
1054                 if (err && (err == -ESTALE || err == -ENOENT))
1055                         err = ovl_cleanup(dir, index);
1056                 if (err)
1057                         break;
1058
1059                 dput(index);
1060                 index = NULL;
1061         }
1062         dput(index);
1063         inode_unlock(dir);
1064 out:
1065         ovl_cache_free(&list);
1066         if (err)
1067                 pr_err("overlayfs: failed index dir cleanup (%i)\n", err);
1068         return err;
1069 }