Skip to content

Commit 95e598e

Browse files
zhangyi089Miklos Szeredi
authored andcommitted
ovl: simplify ovl_check_empty_and_clear()
Filter out non-whiteout non-upper entries from list of merge dir entries while checking if merge dir is empty in ovl_check_empty_dir(). The remaining work for ovl_clear_empty() is to clear all entries on the list. [amir: split patch from rmdir bug fix] Signed-off-by: zhangyi (F) <[email protected]> Signed-off-by: Amir Goldstein <[email protected]> Signed-off-by: Miklos Szeredi <[email protected]>
1 parent b79e05a commit 95e598e

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

fs/overlayfs/dir.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
300300
{
301301
int err;
302302
struct dentry *ret = NULL;
303-
enum ovl_path_type type = ovl_path_type(dentry);
304303
LIST_HEAD(list);
305304

306305
err = ovl_check_empty_dir(dentry, &list);
@@ -313,13 +312,13 @@ static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
313312
* When removing an empty opaque directory, then it makes no sense to
314313
* replace it with an exact replica of itself.
315314
*
316-
* If no upperdentry then skip clearing whiteouts.
315+
* If upperdentry has whiteouts, clear them.
317316
*
318317
* Can race with copy-up, since we don't hold the upperdir mutex.
319318
* Doesn't matter, since copy-up can't create a non-empty directory
320319
* from an empty one.
321320
*/
322-
if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
321+
if (!list_empty(&list))
323322
ret = ovl_clear_empty(dentry, &list);
324323

325324
out_free:

fs/overlayfs/readdir.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct ovl_cache_entry {
2626
struct list_head l_node;
2727
struct rb_node node;
2828
struct ovl_cache_entry *next_maybe_whiteout;
29+
bool is_upper;
2930
bool is_whiteout;
3031
char name[];
3132
};
@@ -158,6 +159,7 @@ static struct ovl_cache_entry *ovl_cache_entry_new(struct ovl_readdir_data *rdd,
158159
/* Defer setting d_ino for upper entry to ovl_iterate() */
159160
if (ovl_calc_d_ino(rdd, p))
160161
p->ino = 0;
162+
p->is_upper = rdd->is_upper;
161163
p->is_whiteout = false;
162164

163165
if (d_type == DT_CHR) {
@@ -851,7 +853,7 @@ const struct file_operations ovl_dir_operations = {
851853
int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
852854
{
853855
int err;
854-
struct ovl_cache_entry *p;
856+
struct ovl_cache_entry *p, *n;
855857
struct rb_root root = RB_ROOT;
856858

857859
err = ovl_dir_read_merged(dentry, list, &root);
@@ -860,18 +862,29 @@ int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
860862

861863
err = 0;
862864

863-
list_for_each_entry(p, list, l_node) {
864-
if (p->is_whiteout)
865-
continue;
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+
}
866875

867876
if (p->name[0] == '.') {
868877
if (p->len == 1)
869-
continue;
878+
goto del_entry;
870879
if (p->len == 2 && p->name[1] == '.')
871-
continue;
880+
goto del_entry;
872881
}
873882
err = -ENOTEMPTY;
874883
break;
884+
885+
del_entry:
886+
list_del(&p->l_node);
887+
kfree(p);
875888
}
876889

877890
return err;
@@ -885,7 +898,7 @@ void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
885898
list_for_each_entry(p, list, l_node) {
886899
struct dentry *dentry;
887900

888-
if (!p->is_whiteout)
901+
if (WARN_ON(!p->is_whiteout || !p->is_upper))
889902
continue;
890903

891904
dentry = lookup_one_len(p->name, upper, p->len);

0 commit comments

Comments
 (0)