bin/144446: [patch] db(3) fails with large block sizes

Peter Jeremy peterjeremy at
Wed Mar 3 11:30:02 UTC 2010

>Number:         144446
>Category:       bin
>Synopsis:       [patch] db(3) fails with large block sizes
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Mar 03 11:30:01 UTC 2010
>Originator:     Peter Jeremy
>Release:        FreeBSD 8.0-STABLE amd64
Alcatel-Lucent Australia
System: FreeBSD 8.0-STABLE FreeBSD 8.0-STABLE #1: Wed Jan 27 06:55:10 EST 2010 root at amd64

	Whilst trying to port db(3) to a Solaris system, I have identified
	two issues with the existing hash(3) code.

	Firstly, when creating a new hash database, the bucket size
	defaults to the st_blksize of the file (hash/hash.c::init_hash()).
	There is no sanity checking to ensure that st_blksize is within
	valid limits (hash/hash.h defined MAX_BSIZE as 65536).

	In FreeBSD, st_blksize is currently hardwired to PAGE_SIZE in
	kern/vfs_vnops.c::vn_stat() so this is purely a theoretical
	issue on FreeBSD.  Solaris exposes the blocksize from the
	underlying filesystem - and in the case of ZFS, this is 128KB,
	which exceeds MAX_BSIZE.  In my case, the symptoms were that
	when sequentially reading the database (via DB->seq()), the
	returned keys were padded with 64KB of NULs.

	Secondly, when the bucket size is set to 64KB (MAX_BSIZE),
	non-trivial databases crash reporting:
	    "HASH: Out of overflow pages.  Increase page size"
	It's not clear what triggers this.

	Thirdly, whilst writing this PR, I've noticed that hash(3)
	states that the default hash table bucket size is 256 bytes.
	The actual default (as per DEF_BUCKET_SIZE in hash/hash.h)
	is 4096 bytes.

	First problem can't be reproduced on FreeBSD but occurs on
	at least Solaris and OpenSolaris with ZFS.

	The second problem can be reproduced by extending the db
	test tool (lib/libc/db/test/run.test, test20) to include
	a bucket size of 65536.  Based on the progression of
	fill factor's in test20, the logical fill factors are
	2735 3647 5471, however the test fails with each of these
	values as well as 8, 341 and 10001.  All other bucket
	sizes appear to work successfully - which suggests there
	is a problem with this particular bucket size.

	First patch adds check for excessive st_blksize.  Note that this
	assumes that st_blksize is a power of 2.  It might be reasonable
	to verify that st_blksize isn't too small but I'm not sure what
	the lower limit is.

Index: hash/hash.c
RCS file: /usr/ncvs/src/lib/libc/db/hash/hash.c,v
retrieving revision
diff -u -r1.21.2.2 hash.c
--- hash/hash.c	28 Aug 2009 19:48:06 -0000
+++ hash/hash.c	3 Mar 2010 09:39:45 -0000
@@ -293,6 +293,8 @@
 		if (stat(file, &statbuf))
 			return (NULL);
 		hashp->BSIZE = statbuf.st_blksize;
+		if (hashp->BSIZE > MAX_BSIZE)
+			hashp->BSIZE = MAX_BSIZE;
 		hashp->BSHIFT = __log2(hashp->BSIZE);

	This is simply a work-around for the second problem.  I'm not
	certain what the actual problem is bu this avoids it.

Index: hash/hash.h
RCS file: /usr/ncvs/src/lib/libc/db/hash/hash.h,v
retrieving revision
diff -u -r1.9.2.1 hash.h
--- hash/hash.h	3 Aug 2009 08:13:06 -0000
+++ hash/hash.h	3 Mar 2010 09:39:45 -0000
@@ -118,7 +118,7 @@
  * Constants
-#define	MAX_BSIZE		65536		/* 2^16 */
+#define	MAX_BSIZE		32768		/* 2^15 but should be 65536 */
 #define MIN_BUFFERS		6
 #define MINHDRSIZE		512
 #define DEF_BUFSIZE		65536		/* 64 K */

	The following updates the man page to contain the current
	default bucket size as per DEF_BUCKET_SIZE in hash/hash.h

Index: man/hash.3
RCS file: /usr/ncvs/src/lib/libc/db/man/hash.3,v
retrieving revision
diff -u -r1.9.10.1 hash.3
--- man/hash.3	3 Aug 2009 08:13:06 -0000
+++ man/hash.3	3 Mar 2010 09:50:52 -0000
@@ -78,7 +78,7 @@
 defines the
-table bucket size, and is, by default, 256 bytes.
+table bucket size, and is, by default, 4096 bytes.
 It may be preferable to increase the page size for disk-resident tables
 and tables with large data items.
 .It Va ffactor

More information about the freebsd-bugs mailing list