Skip to contentSkip to conversation

erofs-utils: lib: add bitops header

PATCH Superseded Hongzhen Luo 2 participants 7 messages Jul 18, 2024 – Jul 22, 2024 Library code

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 erofs-utils: lib: add bitops header

Add bitops header for subsequent bloom filter implementation. This is borrowed from a part of the previous patch. See:
https://lore.kernel.org/all/[email protected]/.

---
 include/erofs/bitops.h | 42 ++++++++++++++++++++++++++++++++++++++++++
 lib/Makefile.am        |  1 +
 2 files changed, 43 insertions(+)
 create mode 100644 include/erofs/bitops.h
include/erofs/bitops.h new file +42 -0
new file mode 100644
index 0000000..ef60d6e
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
+#ifndef __EROFS_BITOPS_H
+#define __EROFS_BITOPS_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include "defs.h"
+
+struct bitmap {
+	unsigned long size;	/* size of bitmap in bits */
+	unsigned long *map;
+};
+
+static inline void set_bit(int nr, volatile unsigned long *addr)
+{
+	unsigned long mask = BIT_MASK(nr);
+	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
+
+	*p  |= mask;
+}
+
+static inline void clear_bit(int nr, volatile unsigned long *addr)
+{
+	unsigned long mask = BIT_MASK(nr);
+	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
+
+	*p &= ~mask;
+}
+
+static inline int test_bit(int nr, const volatile unsigned long *addr)
+{
+	return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
lib/Makefile.am +1 -0
index 2cb4cab..6b52470 100644
@@ -20,6 +20,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
       $(top_srcdir)/include/erofs/io.h \
       $(top_srcdir)/include/erofs/list.h \
       $(top_srcdir)/include/erofs/print.h \
+      $(top_srcdir)/include/erofs/bitops.h \
       $(top_srcdir)/include/erofs/tar.h \
       $(top_srcdir)/include/erofs/trace.h \
       $(top_srcdir)/include/erofs/xattr.h \
Signature · 1 line
2.43.5
#1 lore raw

PATCH 2/3 erofs-utils: lib: add bloom filter

Introduce following bloom filter helpers:

erofs_bloom_init
erofs_bloom_add
erofs_bloom_test
erofs_bloom_exit

---
 include/erofs/bloom_filter.h | 30 ++++++++++++
 include/erofs/internal.h     |  2 +
 lib/Makefile.am              |  2 +-
 lib/bloom_filter.c           | 92 ++++++++++++++++++++++++++++++++++++
 4 files changed, 125 insertions(+), 1 deletion(-)
 create mode 100644 include/erofs/bloom_filter.h
 create mode 100644 lib/bloom_filter.c
include/erofs/bloom_filter.h new file +30 -0
new file mode 100644
index 0000000..a0915e4
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
+#ifndef __EROFS_BLOOM_FILTER_H
+#define __EROFS_BLOOM_FILTER_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include "internal.h"
+#include "bitops.h"
+
+struct erofs_bloom_filter {
+        struct bitmap bmap;
+        unsigned long bitmap_mask;
+        u32 hash_seed;
+        u32 nr_funcs;
+};
+
+int erofs_bloom_init(struct erofs_sb_info *sbi, u32 nr_funcs,
+                     unsigned long entries, u32 seed);
+long erofs_bloom_add(struct erofs_sb_info *sbi, void *data, size_t length);
+long erofs_bloom_test(struct erofs_sb_info *sbi, void *data, size_t length);
+void erofs_bloom_exit(struct erofs_sb_info *sbi);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
\ No newline at end of file
include/erofs/internal.h +2 -0
index 708e51e..d3dd676 100644
@@ -74,6 +74,7 @@ struct erofs_xattr_prefix_item {
 #define EROFS_PACKED_NID_UNALLOCATED	-1
 
 struct erofs_mkfs_dfops;
+struct erofs_bloom_filter;
 struct erofs_sb_info {
 	struct erofs_device_info *devs;
 	char *devname;
@@ -134,6 +135,7 @@ struct erofs_sb_info {
 #endif
 	struct erofs_bufmgr *bmgr;
 	bool useqpl;
+	struct erofs_bloom_filter *bf;
 };
 
 #define EROFS_SUPER_END (EROFS_SUPER_OFFSET + sizeof(struct erofs_super_block))
lib/Makefile.am +1 -1
index 6b52470..78140e7 100644
@@ -35,7 +35,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \
 		      namei.c data.c compress.c compressor.c zmap.c decompress.c \
 		      compress_hints.c hashmap.c sha256.c blobchunk.c dir.c \
 		      fragments.c dedupe.c uuid_unparse.c uuid.c tar.c \
-		      block_list.c xxhash.c rebuild.c diskbuf.c
+		      block_list.c xxhash.c rebuild.c diskbuf.c bloom_filter.c
 
 liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include
 if ENABLE_LZ4
lib/bloom_filter.c new file +92 -0

93 lines not on this page · raw message

Signature · 1 line
2.43.5
#2 lore raw

Re: [PATCH 2/3] erofs-utils: lib: add bloom filter

Quoted 44 lines · Hongzhen Luo

Introduce following bloom filter helpers:

erofs_bloom_init
erofs_bloom_add
erofs_bloom_test
erofs_bloom_exit

---
  include/erofs/bloom_filter.h | 30 ++++++++++++
  include/erofs/internal.h     |  2 +
  lib/Makefile.am              |  2 +-
  lib/bloom_filter.c           | 92 ++++++++++++++++++++++++++++++++++++
  4 files changed, 125 insertions(+), 1 deletion(-)

create mode 100644 include/erofs/bloom_filter.h
create mode 100644 lib/bloom_filter.c

include/erofs/bloom_filter.h new file +21 -0
new file mode 100644
index 0000000..a0915e4
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
+#ifndef __EROFS_BLOOM_FILTER_H
+#define __EROFS_BLOOM_FILTER_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include "internal.h"
+#include "bitops.h"
+
+struct erofs_bloom_filter {
+        struct bitmap bmap;
+        unsigned long bitmap_mask;
+        u32 hash_seed;
+        u32 nr_funcs;
+};
+
+int erofs_bloom_init(struct erofs_sb_info *sbi, u32 nr_funcs,
+                     unsigned long entries, u32 seed);

include/erofs/bloom_filter.h line 30

Quoted 10 lines
include/erofs/bloom_filter.h new file +9 -0
+long erofs_bloom_add(struct erofs_sb_info *sbi, void *data, size_t length);
+long erofs_bloom_test(struct erofs_sb_info *sbi, void *data, size_t length);
+void erofs_bloom_exit(struct erofs_sb_info *sbi);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
\ No newline at end of file

What editor are you using? It lacks a blank line at the end of line.

Quoted 48 lines
include/erofs/internal.h +2 -0
index 708e51e..d3dd676 100644
@@ -74,6 +74,7 @@ struct erofs_xattr_prefix_item {
  #define EROFS_PACKED_NID_UNALLOCATED	-1
 
  struct erofs_mkfs_dfops;
+struct erofs_bloom_filter;
  struct erofs_sb_info {
  	struct erofs_device_info *devs;
  	char *devname;
@@ -134,6 +135,7 @@ struct erofs_sb_info {
  #endif
  	struct erofs_bufmgr *bmgr;
  	bool useqpl;
+	struct erofs_bloom_filter *bf;
  };
 
  #define EROFS_SUPER_END (EROFS_SUPER_OFFSET + sizeof(struct erofs_super_block))
lib/Makefile.am +1 -1
index 6b52470..78140e7 100644
@@ -35,7 +35,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \
  		      namei.c data.c compress.c compressor.c zmap.c decompress.c \
  		      compress_hints.c hashmap.c sha256.c blobchunk.c dir.c \
  		      fragments.c dedupe.c uuid_unparse.c uuid.c tar.c \
-		      block_list.c xxhash.c rebuild.c diskbuf.c
+		      block_list.c xxhash.c rebuild.c diskbuf.c bloom_filter.c
 
  liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include
  if ENABLE_LZ4
lib/bloom_filter.c new file +9 -0
new file mode 100644
index 0000000..c460261
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0
+#include "erofs/bloom_filter.h"
+#include "xxhash.h"
+#include <stdlib.h>
+
+static u32 erofs_bloom_hash(struct erofs_bloom_filter *bf, void *data,
+                            size_t length, u32 index)
+{
+        u32 h;

lib/bloom_filter.c line 19

Quoted 10 lines
lib/bloom_filter.c new file +10 -0
+
+        h = xxh32(data, length, bf->hash_seed + index);
+        return h & bf->bitmap_mask;
+}
+
+/* The optimal bit array size that minimizes the false positive is
+ * m * k / ln(2) where m is the # of elements inserted into the bloom
+ * filter and k is the # of hash functions. Here, 1.44 is used to approximate
+ * 1 / ln(2).
+ */

Wrong comment style.

Quoted 17 lines
lib/bloom_filter.c new file +17 -0
+int erofs_bloom_init(struct erofs_sb_info *sbi, u32 nr_funcs,
+                     unsigned long entries, u32 seed)
+{
+        struct erofs_bloom_filter *bf;
+
+        bf = calloc(1, sizeof(struct erofs_bloom_filter));
+        if (!bf)
+                return -EINVAL;
+
+        bf->nr_funcs = nr_funcs;
+        bf->hash_seed = seed;
+        bf->bmap.size = roundup_pow_of_two(((long)(entries * nr_funcs * 1.44)));
+        bf->bitmap_mask = bf->bmap.size - 1;
+        bf->bmap.map = calloc(BITS_TO_LONGS(bf->bmap.size), sizeof(long));
+        if (!bf->bmap.map) {
+                free(bf);
+                return -ENOMEM;

lib/bloom_filter.c line 46

Quoted 10 lines
lib/bloom_filter.c new file +10 -0
+        }
+        sbi->bf = bf;
+
+        return 0;
+}
+
+long erofs_bloom_add(struct erofs_sb_info *sbi, void *data, size_t length)
+{
+        u32 i, h;
+        struct erofs_bloom_filter *bf;

reverse chrismas tree makes this better, like:

	struct erofs_bloom_filter *bf;
	u32 i, h;
Quoted 37 lines
lib/bloom_filter.c new file +37 -0
+
+        bf = sbi->bf;
+        if (!bf)
+                return -EINVAL;
+
+        for (i = 0; i < bf->nr_funcs; i ++) {
+                h = erofs_bloom_hash(bf, data, length, i);
+                set_bit(h, bf->bmap.map);
+        }
+
+        return 0;
+}
+
+/*
+ * Return negative error code on failure, 0 if the key is not in the bloom filter
+ * and 1 if the key might be in the bloom filter.
+ */
+long erofs_bloom_test(struct erofs_sb_info *sbi, void *data, size_t length)
+{
+        u32 i, h;
+        struct erofs_bloom_filter *bf;
+
+        bf = sbi->bf;
+        if (!bf)
+                return -EINVAL;
+
+        for (i = 0; i < bf->nr_funcs; i ++) {
+                h = erofs_bloom_hash(bf, data, length, i);
+                if (!test_bit(h, bf->bmap.map))
+                        return 0;
+        }
+
+        return 1;
+}
+
+void erofs_bloom_exit(struct erofs_sb_info *sbi)
+{

lib/bloom_filter.c line 92

Quoted 10 lines
lib/bloom_filter.c new file +9 -0
+        if (sbi->bf) {
+                if (sbi->bf->bmap.map) {
+                        free(sbi->bf->bmap.map);
+                        sbi->bf->bmap.map = NULL;
+                }
+                free(sbi->bf);
+                sbi->bf = NULL;
+        }
+}
\ No newline at end of file

Same here.

#3 lore raw

PATCH 3/3 erofs-utils: lib: enhance erofs_rebuild_get_dentry with bloom filter

In reply to Hongzhen Luo

Add a bloom filter to exclude entries that are not in pwd->i_subdirs, thereby improving the performance of erofs_rebuild_get_dentry. Below are the results for different # of files in the same directory:

+---------+--------------------+
| # files | time reduction (%) |
+---------+--------------------+
|   1e4   |         55%        |
+---------+--------------------+
|   1e5   |         98%        |
+---------+--------------------+
|   2e5   |         98%        |
+---------+--------------------+
|   3e5   |         99%        |
+---------+--------------------+
---
 lib/rebuild.c | 61 ++++++++++++++++++++++++++++++++++++++++++++-------
 lib/super.c   |  2 ++
 mkfs/main.c   |  4 ++++
 3 files changed, 59 insertions(+), 8 deletions(-)
lib/rebuild.c +53 -8
index 9e8bf8f..3fd3ea0 100644
@@ -15,6 +15,7 @@
 #include "erofs/xattr.h"
 #include "erofs/blobchunk.h"
 #include "erofs/internal.h"
+#include "erofs/bloom_filter.h"
 #include "liberofs_uuid.h"
 
 #ifdef HAVE_LINUX_AUFS_TYPE_H
@@ -62,14 +63,48 @@ struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
 		char *path, bool aufs, bool *whout, bool *opq, bool to_head)
 {
 	struct erofs_dentry *d = NULL;
-	unsigned int len = strlen(path);
-	char *s = path;
+	unsigned int len, p = 0;
+	char *s;
+	struct erofs_sb_info *sbi = pwd->sbi;
+	char buf[4096];
 
 	*whout = false;
 	*opq = false;
 
+	s = pwd->i_srcpath;
+	len = strlen(pwd->i_srcpath);
+	/* normalize the pwd path, e.g., /./../xxx/yyy -> /xxx/yyy */
+	buf[p++] = '/';
+	while (s < pwd->i_srcpath + len) {
+		char *slash = memchr(s, '/', pwd->i_srcpath + len - s);
+		if (slash) {
+			if (s == slash) {
+				while(*++s == '/');
+				continue;;
+			}
+			*slash = '\0';
+		}
+		if (memcmp(s, ".", 2) && memcmp(s, "..", 3)) {
+			memcpy(buf + p, s, strlen(s));
+			p += strlen(s);
+			buf[p++] = '/';
+
+		}
+		if (slash) {
+			*slash = '/';
+			s = slash + 1;
+		} else{
+			break;
+		}
+	}
+	if (buf[p - 1] != '/')
+		buf[p++] = '/';
+
+	s = path;
+	len = strlen(path);
 	while (s < path + len) {
 		char *slash = memchr(s, '/', path + len - s);
+		int bloom, slen;
 
 		if (slash) {
 			if (s == slash) {
@@ -97,13 +132,19 @@ struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
 				}
 			}
 
-			list_for_each_entry(d, &pwd->i_subdirs, d_child) {
-				if (!strcmp(d->name, s)) {
-					if (d->type != EROFS_FT_DIR && slash)
-						return ERR_PTR(-EIO);
-					inode = d->inode;
-					break;
+			slen = strlen(s);
+			memcpy(buf + p, s, slen);
+			p += slen;
+			if ((bloom = erofs_bloom_test(sbi, buf, p)) > 0) {
+				list_for_each_entry(d, &pwd->i_subdirs, d_child) {
+					if (!strcmp(d->name, s)) {
+						if (d->type != EROFS_FT_DIR && slash)
+							return ERR_PTR(-EIO);
+						inode = d->inode;
+						break;
+					}
 				}
+
 			}
 
 			if (inode) {
@@ -124,6 +165,10 @@ struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
 					return d;
 				pwd = d->inode;
 			}
+			if (!bloom && erofs_bloom_add(sbi, buf, p))
+				return ERR_PTR(-EINVAL);
+			if (slash)
+				buf[p++] = '/';
 		}
 		if (slash) {
 			*slash = '/';
lib/super.c +2 -0
index 45233c4..7289236 100644
@@ -7,6 +7,7 @@
 #include "erofs/print.h"
 #include "erofs/xattr.h"
 #include "erofs/cache.h"
+#include "erofs/bloom_filter.h"
 
 static bool check_layout_compatibility(struct erofs_sb_info *sbi,
 				       struct erofs_super_block *dsb)
@@ -153,6 +154,7 @@ void erofs_put_super(struct erofs_sb_info *sbi)
 		erofs_buffer_exit(sbi->bmgr);
 		sbi->bmgr = NULL;
 	}
+	erofs_bloom_exit(sbi);
 }
 
 int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh,
mkfs/main.c +4 -0
index 20f12fc..27a2ea0 100644
@@ -31,6 +31,7 @@
 #include "../lib/liberofs_private.h"
 #include "../lib/liberofs_uuid.h"
 #include "../lib/compressor.h"
+#include "erofs/bloom_filter.h"
 
 static struct option long_options[] = {
 	{"version", no_argument, 0, 'V'},
@@ -1344,6 +1345,9 @@ int main(int argc, char **argv)
 	}
 
 	erofs_inode_manager_init();
+	srand(time(NULL));
+	/* 1000000 should be enough */
+	erofs_bloom_init(&g_sbi, 5, 1000000, rand());
 
 	if (tar_mode) {
 		root = erofs_rebuild_make_root(&g_sbi);
Signature · 1 line
2.43.5
#4 lore raw

Re: [PATCH 3/3] erofs-utils: lib: enhance erofs_rebuild_get_dentry with bloom filter

Quoted 44 lines · Hongzhen Luo

Add a bloom filter to exclude entries that are not in pwd->i_subdirs, thereby improving the performance of erofs_rebuild_get_dentry. Below are the results for different # of files in the same directory:

+---------+--------------------+
| # files | time reduction (%) |
+---------+--------------------+
|   1e4   |         55%        |
+---------+--------------------+
|   1e5   |         98%        |
+---------+--------------------+
|   2e5   |         98%        |
+---------+--------------------+
|   3e5   |         99%        |
+---------+--------------------+
---
  lib/rebuild.c | 61 ++++++++++++++++++++++++++++++++++++++++++++-------
  lib/super.c   |  2 ++
  mkfs/main.c   |  4 ++++
  3 files changed, 59 insertions(+), 8 deletions(-)
lib/rebuild.c +4 -2
index 9e8bf8f..3fd3ea0 100644
@@ -15,6 +15,7 @@
  #include "erofs/xattr.h"
  #include "erofs/blobchunk.h"
  #include "erofs/internal.h"
+#include "erofs/bloom_filter.h"
  #include "liberofs_uuid.h"
 
  #ifdef HAVE_LINUX_AUFS_TYPE_H
@@ -62,14 +63,48 @@ struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
  		char *path, bool aufs, bool *whout, bool *opq, bool to_head)
  {
  	struct erofs_dentry *d = NULL;
-	unsigned int len = strlen(path);
-	char *s = path;
+	unsigned int len, p = 0;
+	char *s;
+	struct erofs_sb_info *sbi = pwd->sbi;

lib/rebuild.c struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd, · line 78

Quoted 10 lines
lib/rebuild.c +6 -0
+	char buf[4096];
 
  	*whout = false;
  	*opq = false;
 
+	s = pwd->i_srcpath;
+	len = strlen(pwd->i_srcpath);
+	/* normalize the pwd path, e.g., /./../xxx/yyy -> /xxx/yyy */
+	buf[p++] = '/';
+	while (s < pwd->i_srcpath + len) {

Why should we need that?

Quoted 102 lines
lib/rebuild.c +43 -6
+		char *slash = memchr(s, '/', pwd->i_srcpath + len - s);
+		if (slash) {
+			if (s == slash) {
+				while(*++s == '/');
+				continue;;
+			}
+			*slash = '\0';
+		}
+		if (memcmp(s, ".", 2) && memcmp(s, "..", 3)) {
+			memcpy(buf + p, s, strlen(s));
+			p += strlen(s);
+			buf[p++] = '/';
+
+		}
+		if (slash) {
+			*slash = '/';
+			s = slash + 1;
+		} else{
+			break;
+		}
+	}
+	if (buf[p - 1] != '/')
+		buf[p++] = '/';
+
+	s = path;
+	len = strlen(path);
  	while (s < path + len) {
  		char *slash = memchr(s, '/', path + len - s);
+		int bloom, slen;
 
  		if (slash) {
  			if (s == slash) {
@@ -97,13 +132,19 @@ struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
  				}
  			}
 
-			list_for_each_entry(d, &pwd->i_subdirs, d_child) {
-				if (!strcmp(d->name, s)) {
-					if (d->type != EROFS_FT_DIR && slash)
-						return ERR_PTR(-EIO);
-					inode = d->inode;
-					break;
+			slen = strlen(s);
+			memcpy(buf + p, s, slen);
+			p += slen;
+			if ((bloom = erofs_bloom_test(sbi, buf, p)) > 0) {
+				list_for_each_entry(d, &pwd->i_subdirs, d_child) {
+					if (!strcmp(d->name, s)) {
+						if (d->type != EROFS_FT_DIR && slash)
+							return ERR_PTR(-EIO);
+						inode = d->inode;
+						break;
+					}
  				}
+
  			}
 
  			if (inode) {
@@ -124,6 +165,10 @@ struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
  					return d;
  				pwd = d->inode;
  			}
+			if (!bloom && erofs_bloom_add(sbi, buf, p))
+				return ERR_PTR(-EINVAL);
+			if (slash)
+				buf[p++] = '/';
  		}
  		if (slash) {
  			*slash = '/';
lib/super.c +2 -0
index 45233c4..7289236 100644
@@ -7,6 +7,7 @@
  #include "erofs/print.h"
  #include "erofs/xattr.h"
  #include "erofs/cache.h"
+#include "erofs/bloom_filter.h"
 
  static bool check_layout_compatibility(struct erofs_sb_info *sbi,
  				       struct erofs_super_block *dsb)
@@ -153,6 +154,7 @@ void erofs_put_super(struct erofs_sb_info *sbi)
  		erofs_buffer_exit(sbi->bmgr);
  		sbi->bmgr = NULL;
  	}
+	erofs_bloom_exit(sbi);
  }
 
  int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh,
mkfs/main.c +1 -0
index 20f12fc..27a2ea0 100644
@@ -31,6 +31,7 @@
  #include "../lib/liberofs_private.h"
  #include "../lib/liberofs_uuid.h"
  #include "../lib/compressor.h"
+#include "erofs/bloom_filter.h"
 
  static struct option long_options[] = {
  	{"version", no_argument, 0, 'V'},

mkfs/main.c int main(int argc, char **argv) · line 1350

Quoted 6 lines
mkfs/main.c +3 -0
@@ -1344,6 +1345,9 @@ int main(int argc, char **argv)
  	}
 
  	erofs_inode_manager_init();
+	srand(time(NULL));
+	/* 1000000 should be enough */
+	erofs_bloom_init(&g_sbi, 5, 1000000, rand());

I don't want to add any new init function anymore, please wrap all init functions into a common one.

Also what's the meaning of hard-coded 1000000?

Thanks,
Gao Xiang

#5 lore raw

Re: [PATCH 3/3] erofs-utils: lib: enhance erofs_rebuild_get_dentry with bloom filter

Quoted 49 lines · Gao Xiang
Quoted 46 lines · Hongzhen Luo

Add a bloom filter to exclude entries that are not in pwd->i_subdirs, thereby improving the performance of erofs_rebuild_get_dentry. Below are the results for different # of files in the same directory:

+---------+--------------------+
| # files | time reduction (%) |
+---------+--------------------+
|   1e4   |         55%        |
+---------+--------------------+
|   1e5   |         98%        |
+---------+--------------------+
|   2e5   |         98%        |
+---------+--------------------+
|   3e5   |         99%        |
+---------+--------------------+

---
  lib/rebuild.c | 61 ++++++++++++++++++++++++++++++++++++++++++++------- lib/super.c   |  2 ++
  mkfs/main.c   |  4 ++++

  3 files changed, 59 insertions(+), 8 deletions(-)
lib/rebuild.c +5 -2
index 9e8bf8f..3fd3ea0 100644
@@ -15,6 +15,7 @@
   #include "erofs/xattr.h"
   #include "erofs/blobchunk.h"
   #include "erofs/internal.h"
+#include "erofs/bloom_filter.h"
   #include "liberofs_uuid.h"
     #ifdef HAVE_LINUX_AUFS_TYPE_H
@@ -62,14 +63,48 @@ struct erofs_dentry 
 *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
           char *path, bool aufs, bool *whout, bool *opq, bool to_head)
   {
       struct erofs_dentry *d = NULL;
-    unsigned int len = strlen(path);
-    char *s = path;
+    unsigned int len, p = 0;
+    char *s;
+    struct erofs_sb_info *sbi = pwd->sbi;
+    char buf[4096];
         *whout = false;
Quoted 9 lines

lib/rebuild.c struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd, · line 78

Quoted 6 lines
lib/rebuild.c +4 -0
       *opq = false;
   +    s = pwd->i_srcpath;
+    len = strlen(pwd->i_srcpath);
+    /* normalize the pwd path, e.g., /./../xxx/yyy -> /xxx/yyy */
+    buf[p++] = '/';
+    while (s < pwd->i_srcpath + len) {

Why should we need that?

We need that because in the incremental build scenario, the i_srcpath of the inodes in tarerofs_parse_tar() could be "denormalized" (e.g., ./data/1.txt) while the path in erofs_rebuild_load_basedir() is "normalized" (e.g., /data/1.txt). This inconsistency might cause the bloom filter to determine that the path does not exist, thereby preventing it from traversing the /data directory, which would result in an error.

Quoted 98 lines
Quoted 98 lines
lib/rebuild.c +43 -5
+        char *slash = memchr(s, '/', pwd->i_srcpath + len - s);
+        if (slash) {
+            if (s == slash) {
+                while(*++s == '/');
+                continue;;
+            }
+            *slash = '\0';
+        }
+        if (memcmp(s, ".", 2) && memcmp(s, "..", 3)) {
+            memcpy(buf + p, s, strlen(s));
+            p += strlen(s);
+            buf[p++] = '/';
+
+        }
+        if (slash) {
+            *slash = '/';
+            s = slash + 1;
+        } else{
+            break;
+        }
+    }
+    if (buf[p - 1] != '/')
+        buf[p++] = '/';
+
+    s = path;
+    len = strlen(path);
       while (s < path + len) {
           char *slash = memchr(s, '/', path + len - s);
+        int bloom, slen;
             if (slash) {
               if (s == slash) {
@@ -97,13 +132,19 @@ struct erofs_dentry 
 *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
                   }
               }
   -            list_for_each_entry(d, &pwd->i_subdirs, d_child) {
-                if (!strcmp(d->name, s)) {
-                    if (d->type != EROFS_FT_DIR && slash)
-                        return ERR_PTR(-EIO);
-                    inode = d->inode;
-                    break;
+            slen = strlen(s);
+            memcpy(buf + p, s, slen);
+            p += slen;
+            if ((bloom = erofs_bloom_test(sbi, buf, p)) > 0) {
+                list_for_each_entry(d, &pwd->i_subdirs, d_child) {
+                    if (!strcmp(d->name, s)) {
+                        if (d->type != EROFS_FT_DIR && slash)
+                            return ERR_PTR(-EIO);
+                        inode = d->inode;
+                        break;
+                    }
                   }
+
               }
                 if (inode) {
@@ -124,6 +165,10 @@ struct erofs_dentry 
 *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
                       return d;
                   pwd = d->inode;
               }
+            if (!bloom && erofs_bloom_add(sbi, buf, p))
+                return ERR_PTR(-EINVAL);
+            if (slash)
+                buf[p++] = '/';
           }
           if (slash) {

              *slash = '/';

lib/super.c +2 -0
index 45233c4..7289236 100644
@@ -7,6 +7,7 @@
   #include "erofs/print.h"
   #include "erofs/xattr.h"
   #include "erofs/cache.h"
+#include "erofs/bloom_filter.h"
     static bool check_layout_compatibility(struct erofs_sb_info *sbi,
                          struct erofs_super_block *dsb)
@@ -153,6 +154,7 @@ void erofs_put_super(struct erofs_sb_info *sbi)
           erofs_buffer_exit(sbi->bmgr);
           sbi->bmgr = NULL;
       }
+    erofs_bloom_exit(sbi);
   }
     int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh,
mkfs/main.c +1 -0
index 20f12fc..27a2ea0 100644
@@ -31,6 +31,7 @@
   #include "../lib/liberofs_private.h"
   #include "../lib/liberofs_uuid.h"
   #include "../lib/compressor.h"
+#include "erofs/bloom_filter.h"
     static struct option long_options[] = {
       {"version", no_argument, 0, 'V'},
@@ -1344,6 +1345,9 @@ int main(int argc, char **argv)
Quoted 9 lines

mkfs/main.c int main(int argc, char **argv) · line 1350

Quoted 5 lines
mkfs/main.c +3 -0
       }
         erofs_inode_manager_init();
+    srand(time(NULL));
+    /* 1000000 should be enough */
+    erofs_bloom_init(&g_sbi, 5, 1000000, rand());

I don't want to add any new init function anymore, please wrap all init functions into a common one.

Sure. I plan to use an erofs_init_super() function to initialize the relevant fields of sbi. However, other initialization functions like erofs_buffer_init() are coupled with the main() function, so it might not be easy to wrap them into this function.

Quoted 2 lines

Also what's the meaning of hard-coded 1000000?

How about adding an option to mkfs to allow configuring the number of hash functions and the capacity of the bloom filter? If the user does not specify, a default value will be set. Below is the implementation:

struct erofs_configure {

// ...

u32 c_bloom_nrfuc;

unsigned long c_bloom_bitsize;

 };

Thanks,

Hongzhen Luo

Quoted 2 lines

Thanks,
Gao Xiang

#6 lore raw
In reply to Hongzhen Luo
Quoted 22 lines · Hongzhen Luo

Add bitops header for subsequent bloom filter implementation. This is borrowed from a part of the previous patch. See:
https://lore.kernel.org/all/[email protected]/.

---
  include/erofs/bitops.h | 42 ++++++++++++++++++++++++++++++++++++++++++
  lib/Makefile.am        |  1 +
  2 files changed, 43 insertions(+)

create mode 100644 include/erofs/bitops.h

include/erofs/bitops.h new file +5 -0
new file mode 100644
index 0000000..ef60d6e
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
+#ifndef __EROFS_BITOPS_H
+#define __EROFS_BITOPS_H
+
+#ifdef __cplusplus

include/erofs/bitops.h line 15

Quoted 10 lines
include/erofs/bitops.h new file +10 -0
+extern "C"
+{
+#endif
+
+#include "defs.h"
+
+struct bitmap {
+	unsigned long size;	/* size of bitmap in bits */
+	unsigned long *map;
+};

What is the meaning of this structure, please just fold it into the original structure.

include/erofs/bitops.h line 17

Quoted 2 lines
include/erofs/bitops.h new file +2 -0
+
+static inline void set_bit(int nr, volatile unsigned long *addr)

Since it's not atomic, please use __set_bit instead.

include/erofs/bitops.h line 25

Quoted 8 lines
include/erofs/bitops.h new file +8 -0
+{
+	unsigned long mask = BIT_MASK(nr);
+	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
+
+	*p  |= mask;
+}
+
+static inline void clear_bit(int nr, volatile unsigned long *addr)

please use __clear_bit instead.

include/erofs/bitops.h line 33

Quoted 8 lines
include/erofs/bitops.h new file +8 -0
+{
+	unsigned long mask = BIT_MASK(nr);
+	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
+
+	*p &= ~mask;
+}
+
+static inline int test_bit(int nr, const volatile unsigned long *addr)

please call it __test_bit instead, or use READ_ONCE to wrap it up.

Thanks,
Gao Xiang