1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2011 Novell Inc.
8 #include <linux/slab.h>
9 #include <linux/namei.h>
10 #include <linux/file.h>
11 #include <linux/xattr.h>
12 #include <linux/rbtree.h>
13 #include <linux/security.h>
14 #include <linux/cred.h>
15 #include <linux/ratelimit.h>
16 #include "overlayfs.h"
18 struct ovl_cache_entry {
23 struct list_head l_node;
25 struct ovl_cache_entry *next_maybe_whiteout;
31 struct ovl_dir_cache {
34 struct list_head entries;
38 struct ovl_readdir_data {
39 struct dir_context ctx;
40 struct dentry *dentry;
43 struct list_head *list;
44 struct list_head middle;
45 struct ovl_cache_entry *first_maybe_whiteout;
49 bool d_type_supported;
55 struct ovl_dir_cache *cache;
56 struct list_head *cursor;
57 struct file *realfile;
58 struct file *upperfile;
61 static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
63 return rb_entry(n, struct ovl_cache_entry, node);
66 static bool ovl_cache_entry_find_link(const char *name, int len,
67 struct rb_node ***link,
68 struct rb_node **parent)
71 struct rb_node **newp = *link;
73 while (!found && *newp) {
75 struct ovl_cache_entry *tmp;
78 tmp = ovl_cache_entry_from_node(*newp);
79 cmp = strncmp(name, tmp->name, len);
81 newp = &tmp->node.rb_right;
82 else if (cmp < 0 || len < tmp->len)
83 newp = &tmp->node.rb_left;
92 static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
93 const char *name, int len)
95 struct rb_node *node = root->rb_node;
99 struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
101 cmp = strncmp(name, p->name, len);
103 node = p->node.rb_right;
104 else if (cmp < 0 || len < p->len)
105 node = p->node.rb_left;
113 static bool ovl_calc_d_ino(struct ovl_readdir_data *rdd,
114 struct ovl_cache_entry *p)
116 /* Don't care if not doing ovl_iter() */
120 /* Always recalc d_ino when remapping lower inode numbers */
121 if (ovl_xino_bits(rdd->dentry->d_sb))
124 /* Always recalc d_ino for parent */
125 if (strcmp(p->name, "..") == 0)
128 /* If this is lower, then native d_ino will do */
133 * Recalc d_ino for '.' and for all entries if dir is impure (contains
136 if ((p->name[0] == '.' && p->len == 1) ||
137 ovl_test_flag(OVL_IMPURE, d_inode(rdd->dentry)))
143 static struct ovl_cache_entry *ovl_cache_entry_new(struct ovl_readdir_data *rdd,
144 const char *name, int len,
145 u64 ino, unsigned int d_type)
147 struct ovl_cache_entry *p;
148 size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
150 p = kmalloc(size, GFP_KERNEL);
154 memcpy(p->name, name, len);
160 /* Defer setting d_ino for upper entry to ovl_iterate() */
161 if (ovl_calc_d_ino(rdd, p))
163 p->is_upper = rdd->is_upper;
164 p->is_whiteout = false;
166 if (d_type == DT_CHR) {
167 p->next_maybe_whiteout = rdd->first_maybe_whiteout;
168 rdd->first_maybe_whiteout = p;
173 static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
174 const char *name, int len, u64 ino,
177 struct rb_node **newp = &rdd->root->rb_node;
178 struct rb_node *parent = NULL;
179 struct ovl_cache_entry *p;
181 if (ovl_cache_entry_find_link(name, len, &newp, &parent))
184 p = ovl_cache_entry_new(rdd, name, len, ino, d_type);
190 list_add_tail(&p->l_node, rdd->list);
191 rb_link_node(&p->node, parent, newp);
192 rb_insert_color(&p->node, rdd->root);
197 static int ovl_fill_lowest(struct ovl_readdir_data *rdd,
198 const char *name, int namelen,
199 loff_t offset, u64 ino, unsigned int d_type)
201 struct ovl_cache_entry *p;
203 p = ovl_cache_entry_find(rdd->root, name, namelen);
205 list_move_tail(&p->l_node, &rdd->middle);
207 p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
211 list_add_tail(&p->l_node, &rdd->middle);
217 void ovl_cache_free(struct list_head *list)
219 struct ovl_cache_entry *p;
220 struct ovl_cache_entry *n;
222 list_for_each_entry_safe(p, n, list, l_node)
225 INIT_LIST_HEAD(list);
228 void ovl_dir_cache_free(struct inode *inode)
230 struct ovl_dir_cache *cache = ovl_dir_cache(inode);
233 ovl_cache_free(&cache->entries);
238 static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
240 struct ovl_dir_cache *cache = od->cache;
242 WARN_ON(cache->refcount <= 0);
244 if (!cache->refcount) {
245 if (ovl_dir_cache(d_inode(dentry)) == cache)
246 ovl_set_dir_cache(d_inode(dentry), NULL);
248 ovl_cache_free(&cache->entries);
253 static int ovl_fill_merge(struct dir_context *ctx, const char *name,
254 int namelen, loff_t offset, u64 ino,
257 struct ovl_readdir_data *rdd =
258 container_of(ctx, struct ovl_readdir_data, ctx);
262 return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
264 return ovl_fill_lowest(rdd, name, namelen, offset, ino, d_type);
267 static int ovl_check_whiteouts(struct dentry *dir, struct ovl_readdir_data *rdd)
270 struct ovl_cache_entry *p;
271 struct dentry *dentry;
272 const struct cred *old_cred;
274 old_cred = ovl_override_creds(rdd->dentry->d_sb);
276 err = down_write_killable(&dir->d_inode->i_rwsem);
278 while (rdd->first_maybe_whiteout) {
279 p = rdd->first_maybe_whiteout;
280 rdd->first_maybe_whiteout = p->next_maybe_whiteout;
281 dentry = lookup_one_len(p->name, dir, p->len);
282 if (!IS_ERR(dentry)) {
283 p->is_whiteout = ovl_is_whiteout(dentry);
287 inode_unlock(dir->d_inode);
289 revert_creds(old_cred);
294 static inline int ovl_dir_read(struct path *realpath,
295 struct ovl_readdir_data *rdd)
297 struct file *realfile;
300 realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
301 if (IS_ERR(realfile))
302 return PTR_ERR(realfile);
304 rdd->first_maybe_whiteout = NULL;
309 err = iterate_dir(realfile, &rdd->ctx);
312 } while (!err && rdd->count);
314 if (!err && rdd->first_maybe_whiteout && rdd->dentry)
315 err = ovl_check_whiteouts(realpath->dentry, rdd);
323 * Can we iterate real dir directly?
325 * Non-merge dir may contain whiteouts from a time it was a merge upper, before
326 * lower dir was removed under it and possibly before it was rotated from upper
329 static bool ovl_dir_is_real(struct dentry *dir)
331 return !ovl_test_flag(OVL_WHITEOUTS, d_inode(dir));
334 static void ovl_dir_reset(struct file *file)
336 struct ovl_dir_file *od = file->private_data;
337 struct ovl_dir_cache *cache = od->cache;
338 struct dentry *dentry = file->f_path.dentry;
341 if (cache && ovl_dentry_version_get(dentry) != cache->version) {
342 ovl_cache_put(od, dentry);
346 is_real = ovl_dir_is_real(dentry);
347 if (od->is_real != is_real) {
348 /* is_real can only become false when dir is copied up */
349 if (WARN_ON(is_real))
355 static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list,
356 struct rb_root *root)
359 struct path realpath;
360 struct ovl_readdir_data rdd = {
361 .ctx.actor = ovl_fill_merge,
369 for (idx = 0; idx != -1; idx = next) {
370 next = ovl_path_next(idx, dentry, &realpath);
371 rdd.is_upper = ovl_dentry_upper(dentry) == realpath.dentry;
374 err = ovl_dir_read(&realpath, &rdd);
379 * Insert lowest layer entries before upper ones, this
380 * allows offsets to be reasonably constant
382 list_add(&rdd.middle, rdd.list);
383 rdd.is_lowest = true;
384 err = ovl_dir_read(&realpath, &rdd);
385 list_del(&rdd.middle);
391 static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
396 list_for_each(p, &od->cache->entries) {
401 /* Cursor is safe since the cache is stable */
405 static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
408 struct ovl_dir_cache *cache;
410 cache = ovl_dir_cache(d_inode(dentry));
411 if (cache && ovl_dentry_version_get(dentry) == cache->version) {
412 WARN_ON(!cache->refcount);
416 ovl_set_dir_cache(d_inode(dentry), NULL);
418 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
420 return ERR_PTR(-ENOMEM);
423 INIT_LIST_HEAD(&cache->entries);
424 cache->root = RB_ROOT;
426 res = ovl_dir_read_merged(dentry, &cache->entries, &cache->root);
428 ovl_cache_free(&cache->entries);
433 cache->version = ovl_dentry_version_get(dentry);
434 ovl_set_dir_cache(d_inode(dentry), cache);
439 /* Map inode number to lower fs unique range */
440 static u64 ovl_remap_lower_ino(u64 ino, int xinobits, int fsid,
441 const char *name, int namelen)
443 if (ino >> (64 - xinobits)) {
444 pr_warn_ratelimited("overlayfs: d_ino too big (%.*s, ino=%llu, xinobits=%d)\n",
445 namelen, name, ino, xinobits);
449 return ino | ((u64)fsid) << (64 - xinobits);
453 * Set d_ino for upper entries. Non-upper entries should always report
454 * the uppermost real inode ino and should not call this function.
456 * When not all layer are on same fs, report real ino also for upper.
458 * When all layers are on the same fs, and upper has a reference to
459 * copy up origin, call vfs_getattr() on the overlay entry to make
460 * sure that d_ino will be consistent with st_ino from stat(2).
462 static int ovl_cache_update_ino(struct path *path, struct ovl_cache_entry *p)
465 struct dentry *dir = path->dentry;
466 struct dentry *this = NULL;
467 enum ovl_path_type type;
468 u64 ino = p->real_ino;
469 int xinobits = ovl_xino_bits(dir->d_sb);
472 if (!ovl_same_sb(dir->d_sb) && !xinobits)
475 if (p->name[0] == '.') {
480 if (p->len == 2 && p->name[1] == '.') {
481 /* we shall not be moved */
482 this = dget(dir->d_parent);
486 this = lookup_one_len(p->name, dir, p->len);
487 if (IS_ERR_OR_NULL(this) || !this->d_inode) {
497 type = ovl_path_type(this);
498 if (OVL_TYPE_ORIGIN(type)) {
500 struct path statpath = *path;
502 statpath.dentry = this;
503 err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
507 WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev);
509 } else if (xinobits && !OVL_TYPE_UPPER(type)) {
510 ino = ovl_remap_lower_ino(ino, xinobits,
511 ovl_layer_lower(this)->fsid,
521 pr_warn_ratelimited("overlayfs: failed to look up (%s) for ino (%i)\n",
526 static int ovl_fill_plain(struct dir_context *ctx, const char *name,
527 int namelen, loff_t offset, u64 ino,
530 struct ovl_cache_entry *p;
531 struct ovl_readdir_data *rdd =
532 container_of(ctx, struct ovl_readdir_data, ctx);
535 p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
540 list_add_tail(&p->l_node, rdd->list);
545 static int ovl_dir_read_impure(struct path *path, struct list_head *list,
546 struct rb_root *root)
549 struct path realpath;
550 struct ovl_cache_entry *p, *n;
551 struct ovl_readdir_data rdd = {
552 .ctx.actor = ovl_fill_plain,
557 INIT_LIST_HEAD(list);
559 ovl_path_upper(path->dentry, &realpath);
561 err = ovl_dir_read(&realpath, &rdd);
565 list_for_each_entry_safe(p, n, list, l_node) {
566 if (strcmp(p->name, ".") != 0 &&
567 strcmp(p->name, "..") != 0) {
568 err = ovl_cache_update_ino(path, p);
572 if (p->ino == p->real_ino) {
573 list_del(&p->l_node);
576 struct rb_node **newp = &root->rb_node;
577 struct rb_node *parent = NULL;
579 if (WARN_ON(ovl_cache_entry_find_link(p->name, p->len,
583 rb_link_node(&p->node, parent, newp);
584 rb_insert_color(&p->node, root);
590 static struct ovl_dir_cache *ovl_cache_get_impure(struct path *path)
593 struct dentry *dentry = path->dentry;
594 struct ovl_dir_cache *cache;
596 cache = ovl_dir_cache(d_inode(dentry));
597 if (cache && ovl_dentry_version_get(dentry) == cache->version)
600 /* Impure cache is not refcounted, free it here */
601 ovl_dir_cache_free(d_inode(dentry));
602 ovl_set_dir_cache(d_inode(dentry), NULL);
604 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
606 return ERR_PTR(-ENOMEM);
608 res = ovl_dir_read_impure(path, &cache->entries, &cache->root);
610 ovl_cache_free(&cache->entries);
614 if (list_empty(&cache->entries)) {
616 * A good opportunity to get rid of an unneeded "impure" flag.
617 * Removing the "impure" xattr is best effort.
619 if (!ovl_want_write(dentry)) {
620 ovl_do_removexattr(ovl_dentry_upper(dentry),
622 ovl_drop_write(dentry);
624 ovl_clear_flag(OVL_IMPURE, d_inode(dentry));
629 cache->version = ovl_dentry_version_get(dentry);
630 ovl_set_dir_cache(d_inode(dentry), cache);
635 struct ovl_readdir_translate {
636 struct dir_context *orig_ctx;
637 struct ovl_dir_cache *cache;
638 struct dir_context ctx;
644 static int ovl_fill_real(struct dir_context *ctx, const char *name,
645 int namelen, loff_t offset, u64 ino,
648 struct ovl_readdir_translate *rdt =
649 container_of(ctx, struct ovl_readdir_translate, ctx);
650 struct dir_context *orig_ctx = rdt->orig_ctx;
652 if (rdt->parent_ino && strcmp(name, "..") == 0) {
653 ino = rdt->parent_ino;
654 } else if (rdt->cache) {
655 struct ovl_cache_entry *p;
657 p = ovl_cache_entry_find(&rdt->cache->root, name, namelen);
660 } else if (rdt->xinobits) {
661 ino = ovl_remap_lower_ino(ino, rdt->xinobits, rdt->fsid,
665 return orig_ctx->actor(orig_ctx, name, namelen, offset, ino, d_type);
668 static bool ovl_is_impure_dir(struct file *file)
670 struct ovl_dir_file *od = file->private_data;
671 struct inode *dir = d_inode(file->f_path.dentry);
674 * Only upper dir can be impure, but if we are in the middle of
675 * iterating a lower real dir, dir could be copied up and marked
676 * impure. We only want the impure cache if we started iterating
677 * a real upper dir to begin with.
679 return od->is_upper && ovl_test_flag(OVL_IMPURE, dir);
683 static int ovl_iterate_real(struct file *file, struct dir_context *ctx)
686 struct ovl_dir_file *od = file->private_data;
687 struct dentry *dir = file->f_path.dentry;
688 struct ovl_layer *lower_layer = ovl_layer_lower(dir);
689 struct ovl_readdir_translate rdt = {
690 .ctx.actor = ovl_fill_real,
692 .xinobits = ovl_xino_bits(dir->d_sb),
695 if (rdt.xinobits && lower_layer)
696 rdt.fsid = lower_layer->fsid;
698 if (OVL_TYPE_MERGE(ovl_path_type(dir->d_parent))) {
700 struct path statpath = file->f_path;
702 statpath.dentry = dir->d_parent;
703 err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
707 WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev);
708 rdt.parent_ino = stat.ino;
711 if (ovl_is_impure_dir(file)) {
712 rdt.cache = ovl_cache_get_impure(&file->f_path);
713 if (IS_ERR(rdt.cache))
714 return PTR_ERR(rdt.cache);
717 err = iterate_dir(od->realfile, &rdt.ctx);
718 ctx->pos = rdt.ctx.pos;
724 static int ovl_iterate(struct file *file, struct dir_context *ctx)
726 struct ovl_dir_file *od = file->private_data;
727 struct dentry *dentry = file->f_path.dentry;
728 struct ovl_cache_entry *p;
736 * If parent is merge, then need to adjust d_ino for '..', if
737 * dir is impure then need to adjust d_ino for copied up
740 if (ovl_xino_bits(dentry->d_sb) ||
741 (ovl_same_sb(dentry->d_sb) &&
742 (ovl_is_impure_dir(file) ||
743 OVL_TYPE_MERGE(ovl_path_type(dentry->d_parent))))) {
744 return ovl_iterate_real(file, ctx);
746 return iterate_dir(od->realfile, ctx);
750 struct ovl_dir_cache *cache;
752 cache = ovl_cache_get(dentry);
754 return PTR_ERR(cache);
757 ovl_seek_cursor(od, ctx->pos);
760 while (od->cursor != &od->cache->entries) {
761 p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
762 if (!p->is_whiteout) {
764 err = ovl_cache_update_ino(&file->f_path, p);
768 if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
771 od->cursor = p->l_node.next;
777 static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
780 struct ovl_dir_file *od = file->private_data;
782 inode_lock(file_inode(file));
787 res = vfs_llseek(od->realfile, offset, origin);
788 file->f_pos = od->realfile->f_pos;
794 offset += file->f_pos;
804 if (offset != file->f_pos) {
805 file->f_pos = offset;
807 ovl_seek_cursor(od, offset);
812 inode_unlock(file_inode(file));
817 static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
820 struct ovl_dir_file *od = file->private_data;
821 struct dentry *dentry = file->f_path.dentry;
822 struct file *realfile = od->realfile;
824 /* Nothing to sync for lower */
825 if (!OVL_TYPE_UPPER(ovl_path_type(dentry)))
829 * Need to check if we started out being a lower dir, but got copied up
832 struct inode *inode = file_inode(file);
834 realfile = READ_ONCE(od->upperfile);
836 struct path upperpath;
838 ovl_path_upper(dentry, &upperpath);
839 realfile = ovl_path_open(&upperpath, O_RDONLY);
842 if (!od->upperfile) {
843 if (IS_ERR(realfile)) {
845 return PTR_ERR(realfile);
847 smp_store_release(&od->upperfile, realfile);
849 /* somebody has beaten us to it */
850 if (!IS_ERR(realfile))
852 realfile = od->upperfile;
858 return vfs_fsync_range(realfile, start, end, datasync);
861 static int ovl_dir_release(struct inode *inode, struct file *file)
863 struct ovl_dir_file *od = file->private_data;
867 ovl_cache_put(od, file->f_path.dentry);
878 static int ovl_dir_open(struct inode *inode, struct file *file)
880 struct path realpath;
881 struct file *realfile;
882 struct ovl_dir_file *od;
883 enum ovl_path_type type;
885 od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
889 type = ovl_path_real(file->f_path.dentry, &realpath);
890 realfile = ovl_path_open(&realpath, file->f_flags);
891 if (IS_ERR(realfile)) {
893 return PTR_ERR(realfile);
895 od->realfile = realfile;
896 od->is_real = ovl_dir_is_real(file->f_path.dentry);
897 od->is_upper = OVL_TYPE_UPPER(type);
898 file->private_data = od;
903 const struct file_operations ovl_dir_operations = {
904 .read = generic_read_dir,
905 .open = ovl_dir_open,
906 .iterate = ovl_iterate,
907 .llseek = ovl_dir_llseek,
908 .fsync = ovl_dir_fsync,
909 .release = ovl_dir_release,
912 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
915 struct ovl_cache_entry *p, *n;
916 struct rb_root root = RB_ROOT;
917 const struct cred *old_cred;
919 old_cred = ovl_override_creds(dentry->d_sb);
920 err = ovl_dir_read_merged(dentry, list, &root);
921 revert_creds(old_cred);
927 list_for_each_entry_safe(p, n, list, l_node) {
929 * Select whiteouts in upperdir, they should
930 * be cleared when deleting this directory.
932 if (p->is_whiteout) {
938 if (p->name[0] == '.') {
941 if (p->len == 2 && p->name[1] == '.')
948 list_del(&p->l_node);
955 void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
957 struct ovl_cache_entry *p;
959 inode_lock_nested(upper->d_inode, I_MUTEX_CHILD);
960 list_for_each_entry(p, list, l_node) {
961 struct dentry *dentry;
963 if (WARN_ON(!p->is_whiteout || !p->is_upper))
966 dentry = lookup_one_len(p->name, upper, p->len);
967 if (IS_ERR(dentry)) {
968 pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
969 upper->d_name.name, p->len, p->name,
970 (int) PTR_ERR(dentry));
974 ovl_cleanup(upper->d_inode, dentry);
977 inode_unlock(upper->d_inode);
980 static int ovl_check_d_type(struct dir_context *ctx, const char *name,
981 int namelen, loff_t offset, u64 ino,
984 struct ovl_readdir_data *rdd =
985 container_of(ctx, struct ovl_readdir_data, ctx);
987 /* Even if d_type is not supported, DT_DIR is returned for . and .. */
988 if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
991 if (d_type != DT_UNKNOWN)
992 rdd->d_type_supported = true;
998 * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
999 * if error is encountered.
1001 int ovl_check_d_type_supported(struct path *realpath)
1004 struct ovl_readdir_data rdd = {
1005 .ctx.actor = ovl_check_d_type,
1006 .d_type_supported = false,
1009 err = ovl_dir_read(realpath, &rdd);
1013 return rdd.d_type_supported;
1016 static void ovl_workdir_cleanup_recurse(struct path *path, int level)
1019 struct inode *dir = path->dentry->d_inode;
1021 struct rb_root root = RB_ROOT;
1022 struct ovl_cache_entry *p;
1023 struct ovl_readdir_data rdd = {
1024 .ctx.actor = ovl_fill_merge,
1031 err = ovl_dir_read(path, &rdd);
1035 inode_lock_nested(dir, I_MUTEX_PARENT);
1036 list_for_each_entry(p, &list, l_node) {
1037 struct dentry *dentry;
1039 if (p->name[0] == '.') {
1042 if (p->len == 2 && p->name[1] == '.')
1045 dentry = lookup_one_len(p->name, path->dentry, p->len);
1048 if (dentry->d_inode)
1049 ovl_workdir_cleanup(dir, path->mnt, dentry, level);
1054 ovl_cache_free(&list);
1057 void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
1058 struct dentry *dentry, int level)
1062 if (!d_is_dir(dentry) || level > 1) {
1063 ovl_cleanup(dir, dentry);
1067 err = ovl_do_rmdir(dir, dentry);
1069 struct path path = { .mnt = mnt, .dentry = dentry };
1072 ovl_workdir_cleanup_recurse(&path, level + 1);
1073 inode_lock_nested(dir, I_MUTEX_PARENT);
1074 ovl_cleanup(dir, dentry);
1078 int ovl_indexdir_cleanup(struct ovl_fs *ofs)
1081 struct dentry *indexdir = ofs->indexdir;
1082 struct dentry *index = NULL;
1083 struct inode *dir = indexdir->d_inode;
1084 struct path path = { .mnt = ofs->upper_mnt, .dentry = indexdir };
1086 struct rb_root root = RB_ROOT;
1087 struct ovl_cache_entry *p;
1088 struct ovl_readdir_data rdd = {
1089 .ctx.actor = ovl_fill_merge,
1096 err = ovl_dir_read(&path, &rdd);
1100 inode_lock_nested(dir, I_MUTEX_PARENT);
1101 list_for_each_entry(p, &list, l_node) {
1102 if (p->name[0] == '.') {
1105 if (p->len == 2 && p->name[1] == '.')
1108 index = lookup_one_len(p->name, indexdir, p->len);
1109 if (IS_ERR(index)) {
1110 err = PTR_ERR(index);
1114 err = ovl_verify_index(ofs, index);
1117 } else if (err == -ESTALE) {
1118 /* Cleanup stale index entries */
1119 err = ovl_cleanup(dir, index);
1120 } else if (err != -ENOENT) {
1122 * Abort mount to avoid corrupting the index if
1123 * an incompatible index entry was found or on out
1127 } else if (ofs->config.nfs_export) {
1129 * Whiteout orphan index to block future open by
1130 * handle after overlay nlink dropped to zero.
1132 err = ovl_cleanup_and_whiteout(indexdir, dir, index);
1134 /* Cleanup orphan index entries */
1135 err = ovl_cleanup(dir, index);
1148 ovl_cache_free(&list);
1150 pr_err("overlayfs: failed index dir cleanup (%i)\n", err);