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 bool 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 bool 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);
214 return rdd->err == 0;
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 inode *inode)
240 struct ovl_dir_cache *cache = od->cache;
242 WARN_ON(cache->refcount <= 0);
244 if (!cache->refcount) {
245 if (ovl_dir_cache(inode) == cache)
246 ovl_set_dir_cache(inode, NULL);
248 ovl_cache_free(&cache->entries);
253 static bool 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(const struct path *path, struct ovl_readdir_data *rdd)
270 struct ovl_cache_entry *p;
271 struct dentry *dentry, *dir = path->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(mnt_idmap(path->mnt), 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(const struct path *realpath,
295 struct ovl_readdir_data *rdd)
297 struct file *realfile;
300 realfile = ovl_path_open(realpath, O_RDONLY | O_LARGEFILE);
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, rdd);
322 static void ovl_dir_reset(struct file *file)
324 struct ovl_dir_file *od = file->private_data;
325 struct ovl_dir_cache *cache = od->cache;
326 struct inode *inode = file_inode(file);
329 if (cache && ovl_inode_version_get(inode) != cache->version) {
330 ovl_cache_put(od, inode);
334 is_real = ovl_dir_is_real(inode);
335 if (od->is_real != is_real) {
336 /* is_real can only become false when dir is copied up */
337 if (WARN_ON(is_real))
343 static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list,
344 struct rb_root *root)
347 struct path realpath;
348 struct ovl_readdir_data rdd = {
349 .ctx.actor = ovl_fill_merge,
357 for (idx = 0; idx != -1; idx = next) {
358 next = ovl_path_next(idx, dentry, &realpath);
359 rdd.is_upper = ovl_dentry_upper(dentry) == realpath.dentry;
362 err = ovl_dir_read(&realpath, &rdd);
367 * Insert lowest layer entries before upper ones, this
368 * allows offsets to be reasonably constant
370 list_add(&rdd.middle, rdd.list);
371 rdd.is_lowest = true;
372 err = ovl_dir_read(&realpath, &rdd);
373 list_del(&rdd.middle);
379 static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
384 list_for_each(p, &od->cache->entries) {
389 /* Cursor is safe since the cache is stable */
393 static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
396 struct ovl_dir_cache *cache;
397 struct inode *inode = d_inode(dentry);
399 cache = ovl_dir_cache(inode);
400 if (cache && ovl_inode_version_get(inode) == cache->version) {
401 WARN_ON(!cache->refcount);
405 ovl_set_dir_cache(d_inode(dentry), NULL);
407 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
409 return ERR_PTR(-ENOMEM);
412 INIT_LIST_HEAD(&cache->entries);
413 cache->root = RB_ROOT;
415 res = ovl_dir_read_merged(dentry, &cache->entries, &cache->root);
417 ovl_cache_free(&cache->entries);
422 cache->version = ovl_inode_version_get(inode);
423 ovl_set_dir_cache(inode, cache);
428 /* Map inode number to lower fs unique range */
429 static u64 ovl_remap_lower_ino(u64 ino, int xinobits, int fsid,
430 const char *name, int namelen, bool warn)
432 unsigned int xinoshift = 64 - xinobits;
434 if (unlikely(ino >> xinoshift)) {
436 pr_warn_ratelimited("d_ino too big (%.*s, ino=%llu, xinobits=%d)\n",
437 namelen, name, ino, xinobits);
443 * The lowest xinobit is reserved for mapping the non-peresistent inode
444 * numbers range, but this range is only exposed via st_ino, not here.
446 return ino | ((u64)fsid) << (xinoshift + 1);
450 * Set d_ino for upper entries. Non-upper entries should always report
451 * the uppermost real inode ino and should not call this function.
453 * When not all layer are on same fs, report real ino also for upper.
455 * When all layers are on the same fs, and upper has a reference to
456 * copy up origin, call vfs_getattr() on the overlay entry to make
457 * sure that d_ino will be consistent with st_ino from stat(2).
459 static int ovl_cache_update_ino(const struct path *path, struct ovl_cache_entry *p)
462 struct dentry *dir = path->dentry;
463 struct dentry *this = NULL;
464 enum ovl_path_type type;
465 u64 ino = p->real_ino;
466 int xinobits = ovl_xino_bits(dir->d_sb);
469 if (!ovl_same_dev(dir->d_sb))
472 if (p->name[0] == '.') {
477 if (p->len == 2 && p->name[1] == '.') {
478 /* we shall not be moved */
479 this = dget(dir->d_parent);
483 this = lookup_one(mnt_idmap(path->mnt), p->name, dir, p->len);
484 if (IS_ERR_OR_NULL(this) || !this->d_inode) {
485 /* Mark a stale entry */
486 p->is_whiteout = true;
496 type = ovl_path_type(this);
497 if (OVL_TYPE_ORIGIN(type)) {
499 struct path statpath = *path;
501 statpath.dentry = this;
502 err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
507 * Directory inode is always on overlay st_dev.
508 * Non-dir with ovl_same_dev() could be on pseudo st_dev in case
509 * of xino bits overflow.
511 WARN_ON_ONCE(S_ISDIR(stat.mode) &&
512 dir->d_sb->s_dev != stat.dev);
514 } else if (xinobits && !OVL_TYPE_UPPER(type)) {
515 ino = ovl_remap_lower_ino(ino, xinobits,
516 ovl_layer_lower(this)->fsid,
518 ovl_xino_warn(dir->d_sb));
527 pr_warn_ratelimited("failed to look up (%s) for ino (%i)\n",
532 static bool ovl_fill_plain(struct dir_context *ctx, const char *name,
533 int namelen, loff_t offset, u64 ino,
536 struct ovl_cache_entry *p;
537 struct ovl_readdir_data *rdd =
538 container_of(ctx, struct ovl_readdir_data, ctx);
541 p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
546 list_add_tail(&p->l_node, rdd->list);
551 static int ovl_dir_read_impure(const struct path *path, struct list_head *list,
552 struct rb_root *root)
555 struct path realpath;
556 struct ovl_cache_entry *p, *n;
557 struct ovl_readdir_data rdd = {
558 .ctx.actor = ovl_fill_plain,
563 INIT_LIST_HEAD(list);
565 ovl_path_upper(path->dentry, &realpath);
567 err = ovl_dir_read(&realpath, &rdd);
571 list_for_each_entry_safe(p, n, list, l_node) {
572 if (strcmp(p->name, ".") != 0 &&
573 strcmp(p->name, "..") != 0) {
574 err = ovl_cache_update_ino(path, p);
578 if (p->ino == p->real_ino) {
579 list_del(&p->l_node);
582 struct rb_node **newp = &root->rb_node;
583 struct rb_node *parent = NULL;
585 if (WARN_ON(ovl_cache_entry_find_link(p->name, p->len,
589 rb_link_node(&p->node, parent, newp);
590 rb_insert_color(&p->node, root);
596 static struct ovl_dir_cache *ovl_cache_get_impure(const struct path *path)
599 struct dentry *dentry = path->dentry;
600 struct inode *inode = d_inode(dentry);
601 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
602 struct ovl_dir_cache *cache;
604 cache = ovl_dir_cache(inode);
605 if (cache && ovl_inode_version_get(inode) == cache->version)
608 /* Impure cache is not refcounted, free it here */
609 ovl_dir_cache_free(inode);
610 ovl_set_dir_cache(inode, NULL);
612 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
614 return ERR_PTR(-ENOMEM);
616 res = ovl_dir_read_impure(path, &cache->entries, &cache->root);
618 ovl_cache_free(&cache->entries);
622 if (list_empty(&cache->entries)) {
624 * A good opportunity to get rid of an unneeded "impure" flag.
625 * Removing the "impure" xattr is best effort.
627 if (!ovl_want_write(dentry)) {
628 ovl_removexattr(ofs, ovl_dentry_upper(dentry),
630 ovl_drop_write(dentry);
632 ovl_clear_flag(OVL_IMPURE, inode);
637 cache->version = ovl_inode_version_get(inode);
638 ovl_set_dir_cache(inode, cache);
643 struct ovl_readdir_translate {
644 struct dir_context *orig_ctx;
645 struct ovl_dir_cache *cache;
646 struct dir_context ctx;
653 static bool ovl_fill_real(struct dir_context *ctx, const char *name,
654 int namelen, loff_t offset, u64 ino,
657 struct ovl_readdir_translate *rdt =
658 container_of(ctx, struct ovl_readdir_translate, ctx);
659 struct dir_context *orig_ctx = rdt->orig_ctx;
661 if (rdt->parent_ino && strcmp(name, "..") == 0) {
662 ino = rdt->parent_ino;
663 } else if (rdt->cache) {
664 struct ovl_cache_entry *p;
666 p = ovl_cache_entry_find(&rdt->cache->root, name, namelen);
669 } else if (rdt->xinobits) {
670 ino = ovl_remap_lower_ino(ino, rdt->xinobits, rdt->fsid,
671 name, namelen, rdt->xinowarn);
674 return orig_ctx->actor(orig_ctx, name, namelen, offset, ino, d_type);
677 static bool ovl_is_impure_dir(struct file *file)
679 struct ovl_dir_file *od = file->private_data;
680 struct inode *dir = file_inode(file);
683 * Only upper dir can be impure, but if we are in the middle of
684 * iterating a lower real dir, dir could be copied up and marked
685 * impure. We only want the impure cache if we started iterating
686 * a real upper dir to begin with.
688 return od->is_upper && ovl_test_flag(OVL_IMPURE, dir);
692 static int ovl_iterate_real(struct file *file, struct dir_context *ctx)
695 struct ovl_dir_file *od = file->private_data;
696 struct dentry *dir = file->f_path.dentry;
697 const struct ovl_layer *lower_layer = ovl_layer_lower(dir);
698 struct ovl_readdir_translate rdt = {
699 .ctx.actor = ovl_fill_real,
701 .xinobits = ovl_xino_bits(dir->d_sb),
702 .xinowarn = ovl_xino_warn(dir->d_sb),
705 if (rdt.xinobits && lower_layer)
706 rdt.fsid = lower_layer->fsid;
708 if (OVL_TYPE_MERGE(ovl_path_type(dir->d_parent))) {
710 struct path statpath = file->f_path;
712 statpath.dentry = dir->d_parent;
713 err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
717 WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev);
718 rdt.parent_ino = stat.ino;
721 if (ovl_is_impure_dir(file)) {
722 rdt.cache = ovl_cache_get_impure(&file->f_path);
723 if (IS_ERR(rdt.cache))
724 return PTR_ERR(rdt.cache);
727 err = iterate_dir(od->realfile, &rdt.ctx);
728 ctx->pos = rdt.ctx.pos;
734 static int ovl_iterate(struct file *file, struct dir_context *ctx)
736 struct ovl_dir_file *od = file->private_data;
737 struct dentry *dentry = file->f_path.dentry;
738 struct ovl_cache_entry *p;
739 const struct cred *old_cred;
742 old_cred = ovl_override_creds(dentry->d_sb);
748 * If parent is merge, then need to adjust d_ino for '..', if
749 * dir is impure then need to adjust d_ino for copied up
752 if (ovl_xino_bits(dentry->d_sb) ||
753 (ovl_same_fs(dentry->d_sb) &&
754 (ovl_is_impure_dir(file) ||
755 OVL_TYPE_MERGE(ovl_path_type(dentry->d_parent))))) {
756 err = ovl_iterate_real(file, ctx);
758 err = iterate_dir(od->realfile, ctx);
764 struct ovl_dir_cache *cache;
766 cache = ovl_cache_get(dentry);
767 err = PTR_ERR(cache);
772 ovl_seek_cursor(od, ctx->pos);
775 while (od->cursor != &od->cache->entries) {
776 p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
777 if (!p->is_whiteout) {
779 err = ovl_cache_update_ino(&file->f_path, p);
784 /* ovl_cache_update_ino() sets is_whiteout on stale entry */
785 if (!p->is_whiteout) {
786 if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
789 od->cursor = p->l_node.next;
794 revert_creds(old_cred);
798 static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
801 struct ovl_dir_file *od = file->private_data;
803 inode_lock(file_inode(file));
808 res = vfs_llseek(od->realfile, offset, origin);
809 file->f_pos = od->realfile->f_pos;
815 offset += file->f_pos;
825 if (offset != file->f_pos) {
826 file->f_pos = offset;
828 ovl_seek_cursor(od, offset);
833 inode_unlock(file_inode(file));
838 static struct file *ovl_dir_open_realfile(const struct file *file,
839 const struct path *realpath)
842 const struct cred *old_cred;
844 old_cred = ovl_override_creds(file_inode(file)->i_sb);
845 res = ovl_path_open(realpath, O_RDONLY | (file->f_flags & O_LARGEFILE));
846 revert_creds(old_cred);
852 * Like ovl_real_fdget(), returns upperfile if dir was copied up since open.
853 * Unlike ovl_real_fdget(), this caches upperfile in file->private_data.
855 * TODO: use same abstract type for file->private_data of dir and file so
856 * upperfile could also be cached for files as well.
858 struct file *ovl_dir_real_file(const struct file *file, bool want_upper)
861 struct ovl_dir_file *od = file->private_data;
862 struct dentry *dentry = file->f_path.dentry;
863 struct file *old, *realfile = od->realfile;
865 if (!OVL_TYPE_UPPER(ovl_path_type(dentry)))
866 return want_upper ? NULL : realfile;
869 * Need to check if we started out being a lower dir, but got copied up
872 realfile = READ_ONCE(od->upperfile);
874 struct path upperpath;
876 ovl_path_upper(dentry, &upperpath);
877 realfile = ovl_dir_open_realfile(file, &upperpath);
878 if (IS_ERR(realfile))
881 old = cmpxchg_release(&od->upperfile, NULL, realfile);
892 static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
895 struct file *realfile;
898 err = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
902 realfile = ovl_dir_real_file(file, true);
903 err = PTR_ERR_OR_ZERO(realfile);
905 /* Nothing to sync for lower */
906 if (!realfile || err)
909 return vfs_fsync_range(realfile, start, end, datasync);
912 static int ovl_dir_release(struct inode *inode, struct file *file)
914 struct ovl_dir_file *od = file->private_data;
918 ovl_cache_put(od, inode);
929 static int ovl_dir_open(struct inode *inode, struct file *file)
931 struct path realpath;
932 struct file *realfile;
933 struct ovl_dir_file *od;
934 enum ovl_path_type type;
936 od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
940 type = ovl_path_real(file->f_path.dentry, &realpath);
941 realfile = ovl_dir_open_realfile(file, &realpath);
942 if (IS_ERR(realfile)) {
944 return PTR_ERR(realfile);
946 od->realfile = realfile;
947 od->is_real = ovl_dir_is_real(inode);
948 od->is_upper = OVL_TYPE_UPPER(type);
949 file->private_data = od;
954 const struct file_operations ovl_dir_operations = {
955 .read = generic_read_dir,
956 .open = ovl_dir_open,
957 .iterate = ovl_iterate,
958 .llseek = ovl_dir_llseek,
959 .fsync = ovl_dir_fsync,
960 .release = ovl_dir_release,
963 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
966 struct ovl_cache_entry *p, *n;
967 struct rb_root root = RB_ROOT;
968 const struct cred *old_cred;
970 old_cred = ovl_override_creds(dentry->d_sb);
971 err = ovl_dir_read_merged(dentry, list, &root);
972 revert_creds(old_cred);
978 list_for_each_entry_safe(p, n, list, l_node) {
980 * Select whiteouts in upperdir, they should
981 * be cleared when deleting this directory.
983 if (p->is_whiteout) {
989 if (p->name[0] == '.') {
992 if (p->len == 2 && p->name[1] == '.')
999 list_del(&p->l_node);
1006 void ovl_cleanup_whiteouts(struct ovl_fs *ofs, struct dentry *upper,
1007 struct list_head *list)
1009 struct ovl_cache_entry *p;
1011 inode_lock_nested(upper->d_inode, I_MUTEX_CHILD);
1012 list_for_each_entry(p, list, l_node) {
1013 struct dentry *dentry;
1015 if (WARN_ON(!p->is_whiteout || !p->is_upper))
1018 dentry = ovl_lookup_upper(ofs, p->name, upper, p->len);
1019 if (IS_ERR(dentry)) {
1020 pr_err("lookup '%s/%.*s' failed (%i)\n",
1021 upper->d_name.name, p->len, p->name,
1022 (int) PTR_ERR(dentry));
1025 if (dentry->d_inode)
1026 ovl_cleanup(ofs, upper->d_inode, dentry);
1029 inode_unlock(upper->d_inode);
1032 static bool ovl_check_d_type(struct dir_context *ctx, const char *name,
1033 int namelen, loff_t offset, u64 ino,
1034 unsigned int d_type)
1036 struct ovl_readdir_data *rdd =
1037 container_of(ctx, struct ovl_readdir_data, ctx);
1039 /* Even if d_type is not supported, DT_DIR is returned for . and .. */
1040 if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
1043 if (d_type != DT_UNKNOWN)
1044 rdd->d_type_supported = true;
1050 * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
1051 * if error is encountered.
1053 int ovl_check_d_type_supported(const struct path *realpath)
1056 struct ovl_readdir_data rdd = {
1057 .ctx.actor = ovl_check_d_type,
1058 .d_type_supported = false,
1061 err = ovl_dir_read(realpath, &rdd);
1065 return rdd.d_type_supported;
1068 #define OVL_INCOMPATDIR_NAME "incompat"
1070 static int ovl_workdir_cleanup_recurse(struct ovl_fs *ofs, const struct path *path,
1074 struct inode *dir = path->dentry->d_inode;
1076 struct ovl_cache_entry *p;
1077 struct ovl_readdir_data rdd = {
1078 .ctx.actor = ovl_fill_plain,
1081 bool incompat = false;
1084 * The "work/incompat" directory is treated specially - if it is not
1085 * empty, instead of printing a generic error and mounting read-only,
1086 * we will error about incompat features and fail the mount.
1088 * When called from ovl_indexdir_cleanup(), path->dentry->d_name.name
1092 !strcmp(path->dentry->d_name.name, OVL_INCOMPATDIR_NAME))
1095 err = ovl_dir_read(path, &rdd);
1099 inode_lock_nested(dir, I_MUTEX_PARENT);
1100 list_for_each_entry(p, &list, l_node) {
1101 struct dentry *dentry;
1103 if (p->name[0] == '.') {
1106 if (p->len == 2 && p->name[1] == '.')
1108 } else if (incompat) {
1109 pr_err("overlay with incompat feature '%s' cannot be mounted\n",
1114 dentry = ovl_lookup_upper(ofs, p->name, path->dentry, p->len);
1117 if (dentry->d_inode)
1118 err = ovl_workdir_cleanup(ofs, dir, path->mnt, dentry, level);
1125 ovl_cache_free(&list);
1129 int ovl_workdir_cleanup(struct ovl_fs *ofs, struct inode *dir,
1130 struct vfsmount *mnt, struct dentry *dentry, int level)
1134 if (!d_is_dir(dentry) || level > 1) {
1135 return ovl_cleanup(ofs, dir, dentry);
1138 err = ovl_do_rmdir(ofs, dir, dentry);
1140 struct path path = { .mnt = mnt, .dentry = dentry };
1143 err = ovl_workdir_cleanup_recurse(ofs, &path, level + 1);
1144 inode_lock_nested(dir, I_MUTEX_PARENT);
1146 err = ovl_cleanup(ofs, dir, dentry);
1152 int ovl_indexdir_cleanup(struct ovl_fs *ofs)
1155 struct dentry *indexdir = ofs->indexdir;
1156 struct dentry *index = NULL;
1157 struct inode *dir = indexdir->d_inode;
1158 struct path path = { .mnt = ovl_upper_mnt(ofs), .dentry = indexdir };
1160 struct ovl_cache_entry *p;
1161 struct ovl_readdir_data rdd = {
1162 .ctx.actor = ovl_fill_plain,
1166 err = ovl_dir_read(&path, &rdd);
1170 inode_lock_nested(dir, I_MUTEX_PARENT);
1171 list_for_each_entry(p, &list, l_node) {
1172 if (p->name[0] == '.') {
1175 if (p->len == 2 && p->name[1] == '.')
1178 index = ovl_lookup_upper(ofs, p->name, indexdir, p->len);
1179 if (IS_ERR(index)) {
1180 err = PTR_ERR(index);
1184 /* Cleanup leftover from index create/cleanup attempt */
1185 if (index->d_name.name[0] == '#') {
1186 err = ovl_workdir_cleanup(ofs, dir, path.mnt, index, 1);
1191 err = ovl_verify_index(ofs, index);
1194 } else if (err == -ESTALE) {
1195 /* Cleanup stale index entries */
1196 err = ovl_cleanup(ofs, dir, index);
1197 } else if (err != -ENOENT) {
1199 * Abort mount to avoid corrupting the index if
1200 * an incompatible index entry was found or on out
1204 } else if (ofs->config.nfs_export) {
1206 * Whiteout orphan index to block future open by
1207 * handle after overlay nlink dropped to zero.
1209 err = ovl_cleanup_and_whiteout(ofs, dir, index);
1211 /* Cleanup orphan index entries */
1212 err = ovl_cleanup(ofs, dir, index);
1225 ovl_cache_free(&list);
1227 pr_err("failed index dir cleanup (%i)\n", err);