[Zrouter-src-freebsd] ZRouter.org: push to FreeBSD HEAD tree

zrouter-src-freebsd at zrouter.org zrouter-src-freebsd at zrouter.org
Sat Apr 21 18:38:53 UTC 2012


details:   http://zrouter.org/hg/FreeBSD/head//rev/455aabca3842
changeset: 461:455aabca3842
user:      Aleksandr Rybalko <ray at ddteam.net>
date:      Sat Apr 21 21:38:59 2012 +0300
description:
FreeBSD HEAD @svn 234539r.

diffstat:

 head/UPDATING                                                       |    17 +-
 head/contrib/jemalloc/FREEBSD-diffs                                 |     8 +-
 head/contrib/jemalloc/VERSION                                       |     2 +-
 head/contrib/jemalloc/doc/jemalloc.3                                |     8 +-
 head/contrib/jemalloc/include/jemalloc/internal/atomic.h            |    46 +-
 head/contrib/jemalloc/include/jemalloc/internal/jemalloc_internal.h |     7 +
 head/contrib/jemalloc/include/jemalloc/jemalloc.h                   |     6 +-
 head/contrib/jemalloc/include/jemalloc/jemalloc_defs.h              |     7 +-
 head/include/malloc_np.h                                            |    29 +-
 head/include/stdlib.h                                               |     9 +-
 head/lib/csu/amd64/Makefile                                         |     8 +-
 head/lib/csu/arm/Makefile                                           |     8 +-
 head/lib/csu/i386-elf/Makefile                                      |     8 +-
 head/lib/csu/ia64/Makefile                                          |     8 +-
 head/lib/csu/mips/Makefile                                          |     8 +-
 head/lib/csu/powerpc/Makefile                                       |     8 +-
 head/lib/csu/powerpc64/Makefile                                     |     8 +-
 head/lib/libc/Makefile                                              |     3 +-
 head/lib/libc/gen/tls.c                                             |    25 +-
 head/lib/libc/stdio/fputws.c                                        |     6 +-
 head/lib/libc/stdio/printf.3                                        |    48 +-
 head/lib/libc/stdio/snprintf.c                                      |    17 +-
 head/lib/libc/stdio/vfprintf.c                                      |     5 +-
 head/lib/libc/stdio/vfwprintf.c                                     |     5 +-
 head/lib/libc/stdio/vsnprintf.c                                     |    10 +-
 head/lib/libc/stdio/vswprintf.c                                     |    11 +-
 head/lib/libc/stdlib/Makefile.inc                                   |    18 +-
 head/lib/libc/stdlib/Symbol.map                                     |    14 +-
 head/lib/libc/stdlib/aligned_alloc.3                                |   126 -
 head/lib/libc/stdlib/jemalloc/Makefile.inc                          |    46 +
 head/lib/libc/stdlib/jemalloc/Symbol.map                            |    35 +
 head/lib/libc/stdlib/malloc.3                                       |   591 -
 head/lib/libc/stdlib/malloc.c                                       |  6270 ----------
 head/lib/libc/stdlib/ql.h                                           |   122 -
 head/lib/libc/stdlib/qr.h                                           |   106 -
 head/lib/libc/stdlib/rb.h                                           |  1002 -
 head/lib/libc/stdlib/reallocf.3                                     |    82 +
 head/lib/libkse/Makefile                                            |     4 +-
 head/lib/libmagic/Makefile                                          |     4 +-
 head/lib/libmagic/config.h                                          |    82 +-
 head/lib/libthr/thread/thr_private.h                                |     4 +-
 head/lib/libulog/ulog_login.c                                       |     4 +-
 head/lib/libusb/libusb10.c                                          |    12 +-
 head/lib/libusb/libusb10_desc.c                                     |    10 +-
 head/lib/libusb/libusb10_io.c                                       |    22 +-
 head/lib/libusb/libusb20.c                                          |     7 +-
 head/lib/libusb/libusb20_desc.c                                     |    16 +-
 head/lib/libusb/libusb20_ugen20.c                                   |     4 +-
 head/libexec/ulog-helper/ulog-helper.c                              |    26 +-
 head/sbin/geom/class/multipath/geom_multipath.c                     |    19 +-
 head/sbin/geom/class/multipath/gmultipath.8                         |    38 +-
 head/sbin/geom/class/raid/graid.8                                   |     7 +-
 head/sbin/growfs/growfs.c                                           |     6 +-
 head/sbin/iscontrol/Makefile                                        |     5 +-
 head/sbin/iscontrol/iscontrol.c                                     |    61 +-
 head/share/man/man4/iscsi_initiator.4                               |    14 +-
 head/share/man/man9/Makefile                                        |     3 +-
 head/share/man/man9/swi.9                                           |    57 +-
 head/share/misc/bsd-family-tree                                     |    18 +-
 head/share/misc/committers-src.dot                                  |     4 +-
 head/share/misc/scsi_modes                                          |    27 +-
 head/sys/amd64/conf/GENERIC                                         |     8 +-
 head/sys/boot/efi/Makefile.inc                                      |     9 +-
 head/sys/boot/i386/efi/Makefile                                     |    13 +-
 head/sys/cam/ata/ata_da.c                                           |     7 +-
 head/sys/cam/scsi/scsi_da.c                                         |     6 +-
 head/sys/conf/files                                                 |     3 +-
 head/sys/contrib/rdma/rdma_cma.c                                    |     4 +-
 head/sys/dev/amr/amr.c                                              |    47 +-
 head/sys/dev/asr/asr.c                                              |    24 +-
 head/sys/dev/ath/ah_osdep.c                                         |    30 +-
 head/sys/dev/ath/ah_osdep.h                                         |    30 +-
 head/sys/dev/ath/ath_hal/ah_eeprom_v4k.c                            |    14 +-
 head/sys/dev/ath/ath_hal/ar5210/ar5210_reset.c                      |    10 +-
 head/sys/dev/ath/ath_hal/ar5211/ar5211_reset.c                      |    10 +-
 head/sys/dev/ath/ath_hal/ar5212/ar5212_reset.c                      |     9 +-
 head/sys/dev/ath/ath_hal/ar5312/ar5312_reset.c                      |     5 +-
 head/sys/dev/ath/ath_hal/ar5416/ar5416_reset.c                      |     9 +-
 head/sys/dev/ath/ath_hal/ar9002/ar9280_attach.c                     |     3 +-
 head/sys/dev/ath/ath_hal/ar9002/ar9285_attach.c                     |    14 +-
 head/sys/dev/mmc/mmc.c                                              |    66 +-
 head/sys/dev/mmc/mmcbrvar.h                                         |     6 +-
 head/sys/dev/mmc/mmcreg.h                                           |    11 +-
 head/sys/dev/mmc/mmcsd.c                                            |    44 +-
 head/sys/dev/mmc/mmcvar.h                                           |     5 +-
 head/sys/dev/nxge/if_nxge.c                                         |     4 +-
 head/sys/dev/usb/serial/umodem.c                                    |     4 +-
 head/sys/fs/coda/coda_subr.c                                        |    11 +-
 head/sys/fs/ext2fs/ext2_vfsops.c                                    |    28 +-
 head/sys/fs/msdosfs/msdosfs_vfsops.c                                |    15 +-
 head/sys/fs/nfsclient/nfs_clsubs.c                                  |    13 +-
 head/sys/fs/nfsclient/nfs_clvfsops.c                                |    14 +-
 head/sys/fs/nfsserver/nfs_nfsdport.c                                |     4 +-
 head/sys/fs/tmpfs/tmpfs_subr.c                                      |     7 +-
 head/sys/geom/multipath/g_multipath.c                               |   118 +-
 head/sys/geom/part/g_part.c                                         |     5 +-
 head/sys/geom/part/g_part.h                                         |     5 +-
 head/sys/geom/part/g_part_gpt.c                                     |     8 +-
 head/sys/geom/part/g_part_mbr.c                                     |     4 +-
 head/sys/geom/raid/g_raid.c                                         |    25 +-
 head/sys/geom/raid/g_raid.h                                         |     6 +-
 head/sys/geom/raid/md_intel.c                                       |    43 +-
 head/sys/geom/raid/md_jmicron.c                                     |    13 +-
 head/sys/geom/raid/md_nvidia.c                                      |    18 +-
 head/sys/geom/raid/md_promise.c                                     |    17 +-
 head/sys/geom/raid/md_sii.c                                         |    13 +-
 head/sys/geom/raid/tr_raid5.c                                       |   376 +
 head/sys/i386/conf/GENERIC                                          |     6 +-
 head/sys/kern/kern_condvar.c                                        |    22 +-
 head/sys/kern/kern_ktrace.c                                         |     9 +-
 head/sys/kern/kern_racct.c                                          |    27 +-
 head/sys/kern/kern_rctl.c                                           |    33 +-
 head/sys/kern/kern_synch.c                                          |    10 +-
 head/sys/kern/kern_thr.c                                            |     4 +-
 head/sys/kern/subr_acl_nfs4.c                                       |     5 +-
 head/sys/kern/subr_trap.c                                           |     6 +-
 head/sys/kern/vfs_default.c                                         |    17 +-
 head/sys/kern/vfs_mount.c                                           |    16 +-
 head/sys/kern/vfs_subr.c                                            |   481 +-
 head/sys/kern/vfs_syscalls.c                                        |     6 +-
 head/sys/mips/atheros/ar71xx_fixup.c                                |   153 +
 head/sys/mips/atheros/ar71xx_fixup.h                                |    36 +
 head/sys/mips/atheros/ar71xx_gpio.c                                 |    12 +-
 head/sys/mips/atheros/ar71xx_pci.c                                  |    86 +-
 head/sys/mips/atheros/ar724x_pci.c                                  |    94 +-
 head/sys/mips/atheros/files.ar71xx                                  |     3 +-
 head/sys/mips/mips/nexus.c                                          |     7 +-
 head/sys/modules/geom/geom_raid/Makefile                            |     4 +-
 head/sys/modules/iscsi/initiator/Makefile                           |    14 +-
 head/sys/modules/mmcsd/Makefile                                     |     4 +-
 head/sys/modules/uart/Makefile                                      |    17 +-
 head/sys/net/bridgestp.c                                            |    88 +-
 head/sys/net/bridgestp.h                                            |     7 +-
 head/sys/net/if.c                                                   |    14 +-
 head/sys/net/if_bridge.c                                            |    61 +-
 head/sys/netinet/sctp_indata.c                                      |    12 +-
 head/sys/netinet/sctp_input.c                                       |    37 +-
 head/sys/netinet/sctp_output.c                                      |    34 +-
 head/sys/netinet/sctp_usrreq.c                                      |     4 +-
 head/sys/netinet/sctp_var.h                                         |     4 +-
 head/sys/netinet/sctputil.c                                         |    12 +-
 head/sys/nfsclient/nfs_subs.c                                       |    12 +-
 head/sys/nfsclient/nfs_vfsops.c                                     |    13 +-
 head/sys/powerpc/aim/swtch32.S                                      |     3 +-
 head/sys/powerpc/aim/swtch64.S                                      |     3 +-
 head/sys/sys/diskmbr.h                                              |     4 +-
 head/sys/sys/gpt.h                                                  |     9 +-
 head/sys/sys/ktrace.h                                               |    10 +-
 head/sys/sys/mount.h                                                |    47 +-
 head/sys/sys/vnode.h                                                |     6 +-
 head/sys/ufs/ffs/ffs_snapshot.c                                     |    38 +-
 head/sys/ufs/ffs/ffs_softdep.c                                      |    16 +-
 head/sys/ufs/ffs/ffs_vfsops.c                                       |    68 +-
 head/sys/ufs/ufs/quota.h                                            |     9 +-
 head/sys/ufs/ufs/ufs_inode.c                                        |    10 +-
 head/sys/ufs/ufs/ufs_quota.c                                        |    69 +-
 head/sys/ufs/ufs/ufs_vnops.c                                        |     7 +-
 head/usr.bin/calendar/calendars/calendar.freebsd                    |     3 +-
 head/usr.bin/file/Makefile                                          |     4 +-
 head/usr.bin/kdump/kdump.1                                          |     6 +-
 head/usr.bin/kdump/kdump.c                                          |    17 +-
 head/usr.bin/top/machine.c                                          |     6 +-
 162 files changed, 2632 insertions(+), 9402 deletions(-)

diffs (17147 lines):

diff -r 18bc588aa6ad -r 455aabca3842 head/UPDATING
--- a/head/UPDATING	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/UPDATING	Sat Apr 21 21:38:59 2012 +0300
@@ -19,8 +19,19 @@
 	includes various WITNESS- related kernel options, INVARIANTS, malloc
 	debugging flags in userland, and various verbose features in the
 	kernel.  Many developers choose to disable these features on build
-	machines to maximize performance.  (To disable malloc debugging, run
-	ln -s aj /etc/malloc.conf.)
+	machines to maximize performance.  (To completely disable malloc
+	debugging, define MALLOC_PRODUCTION in /etc/make.conf, or to merely
+	disable the most expensive debugging functionality run
+	"ln -s 'abort:false,junk:false' /etc/malloc.conf".)
+
+20120417:
+	The malloc(3) implementation embedded in libc now uses sources imported
+	as contrib/jemalloc.  The most disruptive API change is to
+	/etc/malloc.conf.  If your system has an old-style /etc/malloc.conf,
+	delete it prior to installworld, and optionally re-create it using the
+	new format after rebooting.  See malloc.conf(5) for details
+	(specifically the TUNING section and the "opt.*" entries in the MALLCTL
+	NAMESPACE section).
 
 20120328:
 	Big-endian MIPS TARGET_ARCH values no longer end in "eb".  mips64eb
@@ -1576,4 +1587,4 @@
 Contact Warner Losh if you have any questions about your use of
 this document.
 
-$FreeBSD: head/UPDATING 233644 2012-03-29 02:54:35Z jmallett $
+$FreeBSD: head/UPDATING 234396 2012-04-17 20:35:54Z jasone $
diff -r 18bc588aa6ad -r 455aabca3842 head/contrib/jemalloc/FREEBSD-diffs
--- a/head/contrib/jemalloc/FREEBSD-diffs	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/contrib/jemalloc/FREEBSD-diffs	Sat Apr 21 21:38:59 2012 +0300
@@ -45,17 +45,19 @@
 +  </refsect1>
  </refentry>
 diff --git a/include/jemalloc/internal/jemalloc_internal.h.in b/include/jemalloc/internal/jemalloc_internal.h.in
-index aa21aa5..e0f5fed 100644
+index 905653a..b235a0d 100644
 --- a/include/jemalloc/internal/jemalloc_internal.h.in
 +++ b/include/jemalloc/internal/jemalloc_internal.h.in
-@@ -1,3 +1,6 @@
+@@ -1,5 +1,8 @@
+ #ifndef JEMALLOC_INTERNAL_H
+ #define JEMALLOC_INTERNAL_H
 +#include "libc_private.h"
 +#include "namespace.h"
 +
  #include <sys/mman.h>
  #include <sys/param.h>
  #include <sys/syscall.h>
-@@ -33,6 +36,9 @@
+@@ -35,6 +38,9 @@
  #include <pthread.h>
  #include <math.h>
  
diff -r 18bc588aa6ad -r 455aabca3842 head/contrib/jemalloc/VERSION
--- a/head/contrib/jemalloc/VERSION	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/contrib/jemalloc/VERSION	Sat Apr 21 21:38:59 2012 +0300
@@ -1,1 +1,1 @@
-1.0.0-258-g9ef7f5dc34ff02f50d401e41c8d9a4a928e7c2aa
+1.0.0-266-gb57d3ec571c6551231be62b7bf92c084a8c8291c
diff -r 18bc588aa6ad -r 455aabca3842 head/contrib/jemalloc/doc/jemalloc.3
--- a/head/contrib/jemalloc/doc/jemalloc.3	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/contrib/jemalloc/doc/jemalloc.3	Sat Apr 21 21:38:59 2012 +0300
@@ -2,12 +2,12 @@
 .\"     Title: JEMALLOC
 .\"    Author: Jason Evans
 .\" Generator: DocBook XSL Stylesheets v1.76.1 <http://docbook.sf.net/>
-.\"      Date: 04/16/2012
+.\"      Date: 04/17/2012
 .\"    Manual: User Manual
-.\"    Source: jemalloc 1.0.0-258-g9ef7f5dc34ff02f50d401e41c8d9a4a928e7c2aa
+.\"    Source: jemalloc 1.0.0-266-gb57d3ec571c6551231be62b7bf92c084a8c8291c
 .\"  Language: English
 .\"
-.TH "JEMALLOC" "3" "04/16/2012" "jemalloc 1.0.0-258-g9ef7f5dc34" "User Manual"
+.TH "JEMALLOC" "3" "04/17/2012" "jemalloc 1.0.0-266-gb57d3ec571" "User Manual"
 .\" -----------------------------------------------------------------
 .\" * Define some portability stuff
 .\" -----------------------------------------------------------------
@@ -31,7 +31,7 @@
 jemalloc \- general purpose memory allocation functions
 .SH "LIBRARY"
 .PP
-This manual describes jemalloc 1\&.0\&.0\-258\-g9ef7f5dc34ff02f50d401e41c8d9a4a928e7c2aa\&. More information can be found at the
+This manual describes jemalloc 1\&.0\&.0\-266\-gb57d3ec571c6551231be62b7bf92c084a8c8291c\&. More information can be found at the
 \m[blue]\fBjemalloc website\fR\m[]\&\s-2\u[1]\d\s+2\&.
 .PP
 The following configuration options are enabled in libc\*(Aqs built\-in jemalloc:
diff -r 18bc588aa6ad -r 455aabca3842 head/contrib/jemalloc/include/jemalloc/internal/atomic.h
--- a/head/contrib/jemalloc/include/jemalloc/internal/atomic.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/contrib/jemalloc/include/jemalloc/internal/atomic.h	Sat Apr 21 21:38:59 2012 +0300
@@ -32,7 +32,8 @@
 #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_ATOMIC_C_))
 /******************************************************************************/
 /* 64-bit operations. */
-#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
+#if (LG_SIZEOF_PTR == 3 || LG_SIZEOF_INT == 3)
+#  ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
 JEMALLOC_INLINE uint64_t
 atomic_add_uint64(uint64_t *p, uint64_t x)
 {
@@ -60,7 +61,7 @@
 
 	return (OSAtomicAdd64(-((int64_t)x), (int64_t *)p));
 }
-#elif (defined(__amd64__) || defined(__x86_64__))
+#  elif (defined(__amd64__) || defined(__x86_64__))
 JEMALLOC_INLINE uint64_t
 atomic_add_uint64(uint64_t *p, uint64_t x)
 {
@@ -87,7 +88,29 @@
 
 	return (x);
 }
-#elif (defined(JE_FORCE_SYNC_COMPARE_AND_SWAP_8))
+#  elif (defined(JEMALLOC_ATOMIC9))
+JEMALLOC_INLINE uint64_t
+atomic_add_uint64(uint64_t *p, uint64_t x)
+{
+
+	/*
+	 * atomic_fetchadd_64() doesn't exist, but we only ever use this
+	 * function on LP64 systems, so atomic_fetchadd_long() will do.
+	 */
+	assert(sizeof(uint64_t) == sizeof(unsigned long));
+
+	return (atomic_fetchadd_long(p, (unsigned long)x) + x);
+}
+
+JEMALLOC_INLINE uint64_t
+atomic_sub_uint64(uint64_t *p, uint64_t x)
+{
+
+	assert(sizeof(uint64_t) == sizeof(unsigned long));
+
+	return (atomic_fetchadd_long(p, (unsigned long)(-(long)x)) - x);
+}
+#  elif (defined(JE_FORCE_SYNC_COMPARE_AND_SWAP_8))
 JEMALLOC_INLINE uint64_t
 atomic_add_uint64(uint64_t *p, uint64_t x)
 {
@@ -101,8 +124,7 @@
 
 	return (__sync_sub_and_fetch(p, x));
 }
-#else
-#  if (LG_SIZEOF_PTR == 3)
+#  else
 #    error "Missing implementation for 64-bit atomic operations"
 #  endif
 #endif
@@ -164,6 +186,20 @@
 
 	return (x);
 }
+#elif (defined(JEMALLOC_ATOMIC9))
+JEMALLOC_INLINE uint32_t
+atomic_add_uint32(uint32_t *p, uint32_t x)
+{
+
+	return (atomic_fetchadd_32(p, x) + x);
+}
+
+JEMALLOC_INLINE uint32_t
+atomic_sub_uint32(uint32_t *p, uint32_t x)
+{
+
+	return (atomic_fetchadd_32(p, (uint32_t)(-(int32_t)x)) - x);
+}
 #elif (defined(JE_FORCE_SYNC_COMPARE_AND_SWAP_4))
 JEMALLOC_INLINE uint32_t
 atomic_add_uint32(uint32_t *p, uint32_t x)
diff -r 18bc588aa6ad -r 455aabca3842 head/contrib/jemalloc/include/jemalloc/internal/jemalloc_internal.h
--- a/head/contrib/jemalloc/include/jemalloc/internal/jemalloc_internal.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/contrib/jemalloc/include/jemalloc/internal/jemalloc_internal.h	Sat Apr 21 21:38:59 2012 +0300
@@ -1,3 +1,5 @@
+#ifndef JEMALLOC_INTERNAL_H
+#define JEMALLOC_INTERNAL_H
 #include "libc_private.h"
 #include "namespace.h"
 
@@ -165,6 +167,10 @@
 #endif
     ;
 
+#ifdef JEMALLOC_ATOMIC9
+#include <machine/atomic.h>
+#endif
+
 #if (defined(JEMALLOC_OSATOMIC) || defined(JEMALLOC_OSSPIN))
 #include <libkern/OSAtomic.h>
 #endif
@@ -874,3 +880,4 @@
 
 #undef JEMALLOC_H_INLINES
 /******************************************************************************/
+#endif /* JEMALLOC_INTERNAL_H */
diff -r 18bc588aa6ad -r 455aabca3842 head/contrib/jemalloc/include/jemalloc/jemalloc.h
--- a/head/contrib/jemalloc/include/jemalloc/jemalloc.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/contrib/jemalloc/include/jemalloc/jemalloc.h	Sat Apr 21 21:38:59 2012 +0300
@@ -7,12 +7,12 @@
 #include <limits.h>
 #include <strings.h>
 
-#define	JEMALLOC_VERSION "1.0.0-258-g9ef7f5dc34ff02f50d401e41c8d9a4a928e7c2aa"
+#define	JEMALLOC_VERSION "1.0.0-266-gb57d3ec571c6551231be62b7bf92c084a8c8291c"
 #define	JEMALLOC_VERSION_MAJOR 1
 #define	JEMALLOC_VERSION_MINOR 0
 #define	JEMALLOC_VERSION_BUGFIX 0
-#define	JEMALLOC_VERSION_NREV 258
-#define	JEMALLOC_VERSION_GID "9ef7f5dc34ff02f50d401e41c8d9a4a928e7c2aa"
+#define	JEMALLOC_VERSION_NREV 266
+#define	JEMALLOC_VERSION_GID "b57d3ec571c6551231be62b7bf92c084a8c8291c"
 
 #include "jemalloc_defs.h"
 #include "jemalloc_FreeBSD.h"
diff -r 18bc588aa6ad -r 455aabca3842 head/contrib/jemalloc/include/jemalloc/jemalloc_defs.h
--- a/head/contrib/jemalloc/include/jemalloc/jemalloc_defs.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/contrib/jemalloc/include/jemalloc/jemalloc_defs.h	Sat Apr 21 21:38:59 2012 +0300
@@ -48,6 +48,9 @@
  */
 #define CPU_SPINWAIT __asm__ volatile("pause")
 
+/* Defined if the equivalent of FreeBSD's atomic(9) functions are available. */
+#define JEMALLOC_ATOMIC9 1
+
 /*
  * Defined if OSAtomic*() functions are available, as provided by Darwin, and
  * documented in the atomic(3) manual page.
@@ -60,7 +63,7 @@
  * __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 not being defined (which means the
  * functions are defined in libgcc instead of being inlines)
  */
-#define JE_FORCE_SYNC_COMPARE_AND_SWAP_4 
+/* #undef JE_FORCE_SYNC_COMPARE_AND_SWAP_4 */
 
 /*
  * Defined if __sync_add_and_fetch(uint64_t *, uint64_t) and
@@ -68,7 +71,7 @@
  * __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 not being defined (which means the
  * functions are defined in libgcc instead of being inlines)
  */
-#define JE_FORCE_SYNC_COMPARE_AND_SWAP_8 
+/* #undef JE_FORCE_SYNC_COMPARE_AND_SWAP_8 */
 
 /*
  * Defined if OSSpin*() functions are available, as provided by Darwin, and
diff -r 18bc588aa6ad -r 455aabca3842 head/include/malloc_np.h
--- a/head/include/malloc_np.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/include/malloc_np.h	Sat Apr 21 21:38:59 2012 +0300
@@ -26,16 +26,43 @@
  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * $FreeBSD: head/include/malloc_np.h 160056 2006-06-30 20:57:41Z jasone $
+ * $FreeBSD: head/include/malloc_np.h 234370 2012-04-17 07:22:14Z jasone $
  */
 
 #ifndef _MALLOC_NP_H_
 #define	_MALLOC_NP_H_
 #include <sys/cdefs.h>
 #include <sys/types.h>
+#include <strings.h>
 
 __BEGIN_DECLS
 size_t	malloc_usable_size(const void *ptr);
+
+void	malloc_stats_print(void (*write_cb)(void *, const char *),
+    void *cbopaque, const char *opts);
+int	mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp,
+    size_t newlen);
+int	mallctlnametomib(const char *name, size_t *mibp, size_t *miblenp);
+int	mallctlbymib(const size_t *mib, size_t miblen, void *oldp,
+    size_t *oldlenp, void *newp, size_t newlen);
+
+#define	ALLOCM_LG_ALIGN(la)	(la)
+#define	ALLOCM_ALIGN(a)	(ffsl(a)-1)
+#define	ALLOCM_ZERO	((int)0x40)
+#define	ALLOCM_NO_MOVE	((int)0x80)
+
+#define	ALLOCM_SUCCESS		0
+#define	ALLOCM_ERR_OOM		1
+#define	ALLOCM_ERR_NOT_MOVED	2
+
+int	allocm(void **ptr, size_t *rsize, size_t size, int flags)
+    __attribute__(nonnull(1));
+int	rallocm(void **ptr, size_t *rsize, size_t size, size_t extra,
+    int flags) __attribute__(nonnull(1));
+int	sallocm(const void *ptr, size_t *rsize, int flags)
+    __attribute__(nonnull(1));
+int	dallocm(void *ptr, int flags) __attribute__(nonnull(1));
+int	nallocm(size_t *rsize, size_t size, int flags);
 __END_DECLS
 
 #endif /* _MALLOC_NP_H_ */
diff -r 18bc588aa6ad -r 455aabca3842 head/include/stdlib.h
--- a/head/include/stdlib.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/include/stdlib.h	Sat Apr 21 21:38:59 2012 +0300
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)stdlib.h	8.5 (Berkeley) 5/19/95
- * $FreeBSD: head/include/stdlib.h 233600 2012-03-28 12:11:54Z theraven $
+ * $FreeBSD: head/include/stdlib.h 234370 2012-04-17 07:22:14Z jasone $
  */
 
 #ifndef _STDLIB_H_
@@ -155,7 +155,7 @@
  * If we're in a mode greater than C99, expose C11 functions.
  */
 #if __ISO_C_VISIBLE >= 2011 || __cplusplus >= 201103L
-void *	aligned_alloc(size_t, size_t);
+void *	aligned_alloc(size_t, size_t) __malloc_like;
 int	at_quick_exit(void (*)(void));
 _Noreturn void
 	quick_exit(int);
@@ -228,9 +228,8 @@
 #endif /* __XSI_VISIBLE */
 
 #if __BSD_VISIBLE
-extern const char *_malloc_options;
-extern void (*_malloc_message)(const char *, const char *, const char *,
-	    const char *);
+extern const char *malloc_conf;
+extern void (*malloc_message)(void *, const char *);
 
 /*
  * The alloca() function can't be implemented in C, and on some
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/csu/amd64/Makefile
--- a/head/lib/csu/amd64/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/csu/amd64/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-# $FreeBSD: head/lib/csu/amd64/Makefile 232832 2012-03-11 20:04:09Z kib $
+# $FreeBSD: head/lib/csu/amd64/Makefile 234502 2012-04-20 21:00:39Z dim $
 
 .PATH: ${.CURDIR}/../common
 
@@ -22,21 +22,21 @@
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 crt1.o: crt1.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} crt1.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} crt1.s
 
 gcrt1.s: crt1.c
 	${CC} ${CFLAGS} -DGCRT -S -o ${.TARGET} ${.CURDIR}/crt1.c
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 gcrt1.o: gcrt1.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} gcrt1.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} gcrt1.s
 
 Scrt1.s: crt1.c
 	${CC} ${CFLAGS} -fPIC -DPIC -S -o ${.TARGET} ${.CURDIR}/crt1.c
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 Scrt1.o: Scrt1.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} Scrt1.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} Scrt1.s
 
 realinstall:
 	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/csu/arm/Makefile
--- a/head/lib/csu/arm/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/csu/arm/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-# $FreeBSD: head/lib/csu/arm/Makefile 232832 2012-03-11 20:04:09Z kib $
+# $FreeBSD: head/lib/csu/arm/Makefile 234502 2012-04-20 21:00:39Z dim $
 
 .PATH: ${.CURDIR}/../common
 
@@ -21,21 +21,21 @@
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 crt1.o: crt1.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} crt1.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} crt1.s
 
 gcrt1.s: crt1.c
 	${CC} ${CFLAGS} -DGCRT -S -o ${.TARGET} ${.CURDIR}/crt1.c
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 gcrt1.o: gcrt1.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} gcrt1.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} gcrt1.s
 
 Scrt1.s: crt1.c
 	${CC} ${CFLAGS} -fPIC -DPIC -S -o ${.TARGET} ${.CURDIR}/crt1.c
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 Scrt1.o: Scrt1.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} Scrt1.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} Scrt1.s
 
 realinstall:
 	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/csu/i386-elf/Makefile
--- a/head/lib/csu/i386-elf/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/csu/i386-elf/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-# $FreeBSD: head/lib/csu/i386-elf/Makefile 232832 2012-03-11 20:04:09Z kib $
+# $FreeBSD: head/lib/csu/i386-elf/Makefile 234502 2012-04-20 21:00:39Z dim $
 
 .PATH: ${.CURDIR}/../common
 
@@ -21,7 +21,7 @@
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 gcrt1_c.o: gcrt1_c.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} gcrt1_c.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} gcrt1_c.s
 
 gcrt1.o: gcrt1_c.o crt1_s.o
 	${LD} ${LDFLAGS} -o gcrt1.o -r crt1_s.o gcrt1_c.o
@@ -31,7 +31,7 @@
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 crt1_c.o: crt1_c.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} crt1_c.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} crt1_c.s
 
 crt1.o:	crt1_c.o crt1_s.o
 	${LD} ${LDFLAGS} -o crt1.o -r crt1_s.o crt1_c.o
@@ -42,7 +42,7 @@
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 Scrt1_c.o: Scrt1_c.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} Scrt1_c.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} Scrt1_c.s
 
 Scrt1.o: Scrt1_c.o crt1_s.o
 	${LD} ${LDFLAGS} -o Scrt1.o -r crt1_s.o Scrt1_c.o
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/csu/ia64/Makefile
--- a/head/lib/csu/ia64/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/csu/ia64/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-# $FreeBSD$
+# $FreeBSD: head/lib/csu/ia64/Makefile 234502 2012-04-20 21:00:39Z dim $
 
 .PATH: ${.CURDIR}/../common
 
@@ -24,7 +24,7 @@
 	sed -i "" -e '/\.note\.ABI-tag/s/progbits/note/' ${.TARGET}
 
 crtbrand.o: crtbrand.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} crtbrand.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} crtbrand.s
 
 crt1.o: crt1_.o crtbrand.o
 	${LD} ${LDFLAGS} -r -o ${.TARGET} crt1_.o crtbrand.o
@@ -37,7 +37,7 @@
 	sed -i "" -e '/\.note\.ABI-tag/s/progbits/note/' ${.TARGET}
 
 gcrtbrand.o: gcrtbrand.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} gcrtbrand.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} gcrtbrand.s
 
 gcrt1.o: gcrt1_.o gcrtbrand.o
 	${LD} ${LDFLAGS} -r -o ${.TARGET} ${.ALLSRC}
@@ -50,7 +50,7 @@
 	sed -i "" -e '/\.note\.ABI-tag/s/progbits/note/' ${.TARGET}
 
 Scrtbrand.o: Scrtbrand.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} Scrtbrand.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} Scrtbrand.s
 
 Scrt1.o: Scrt1_.o Scrtbrand.o
 	${LD} ${LDFLAGS} -r -o ${.TARGET} ${.ALLSRC}
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/csu/mips/Makefile
--- a/head/lib/csu/mips/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/csu/mips/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-# $FreeBSD: head/lib/csu/mips/Makefile 232832 2012-03-11 20:04:09Z kib $
+# $FreeBSD: head/lib/csu/mips/Makefile 234502 2012-04-20 21:00:39Z dim $
 
 .PATH: ${.CURDIR}/../common
 
@@ -21,21 +21,21 @@
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 crt1.o: crt1.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} crt1.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} crt1.s
 
 gcrt1.s: crt1.c
 	${CC} ${CFLAGS} -DGCRT -S -o ${.TARGET} ${.CURDIR}/crt1.c
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 gcrt1.o: gcrt1.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} gcrt1.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} gcrt1.s
 
 Scrt1.s: crt1.c
 	${CC} ${CFLAGS} -fPIC -DPIC -S -o ${.TARGET} ${.CURDIR}/crt1.c
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 Scrt1.o: Scrt1.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} Scrt1.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} Scrt1.s
 
 realinstall:
 	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/csu/powerpc/Makefile
--- a/head/lib/csu/powerpc/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/csu/powerpc/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-# $FreeBSD: head/lib/csu/powerpc/Makefile 234356 2012-04-16 21:36:55Z dim $
+# $FreeBSD: head/lib/csu/powerpc/Makefile 234502 2012-04-20 21:00:39Z dim $
 
 .PATH: ${.CURDIR}/../common
 
@@ -22,21 +22,21 @@
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 crt1.o: crt1.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} crt1.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} crt1.s
 
 gcrt1.s: crt1.c
 	${CC} ${CFLAGS} -DGCRT -S -o ${.TARGET} ${.CURDIR}/crt1.c
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 gcrt1.o: gcrt1.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} gcrt1.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} gcrt1.s
 
 Scrt1.s: crt1.c
 	${CC} ${CFLAGS} -fPIC -DPIC -S -o ${.TARGET} ${.CURDIR}/crt1.c
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 Scrt1.o: Scrt1.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} Scrt1.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} Scrt1.s
 
 realinstall:
 	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/csu/powerpc64/Makefile
--- a/head/lib/csu/powerpc64/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/csu/powerpc64/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-# $FreeBSD: head/lib/csu/powerpc64/Makefile 232932 2012-03-13 18:59:19Z nwhitehorn $
+# $FreeBSD: head/lib/csu/powerpc64/Makefile 234502 2012-04-20 21:00:39Z dim $
 
 .PATH: ${.CURDIR}/../common
 
@@ -22,21 +22,21 @@
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 crt1.o: crt1.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} crt1.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} crt1.s
 
 gcrt1.s: crt1.c
 	${CC} ${CFLAGS} -DGCRT -S -o ${.TARGET} ${.CURDIR}/crt1.c
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 gcrt1.o: gcrt1.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} gcrt1.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} gcrt1.s
 
 Scrt1.s: crt1.c
 	${CC} ${CFLAGS} -fPIC -DPIC -S -o ${.TARGET} ${.CURDIR}/crt1.c
 	sed ${SED_FIX_NOTE} ${.TARGET}
 
 Scrt1.o: Scrt1.s
-	${CC} ${CFLAGS} -c -o ${.TARGET} Scrt1.s
+	${CC} ${ACFLAGS} -c -o ${.TARGET} Scrt1.s
 
 realinstall:
 	${INSTALL} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/Makefile
--- a/head/lib/libc/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libc/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,5 +1,5 @@
 #	@(#)Makefile	8.2 (Berkeley) 2/3/94
-# $FreeBSD: head/lib/libc/Makefile 229368 2012-01-03 07:14:01Z ed $
+# $FreeBSD: head/lib/libc/Makefile 234370 2012-04-17 07:22:14Z jasone $
 
 SHLIBDIR?= /lib
 
@@ -79,6 +79,7 @@
 .include "${.CURDIR}/resolv/Makefile.inc"
 .include "${.CURDIR}/stdio/Makefile.inc"
 .include "${.CURDIR}/stdlib/Makefile.inc"
+.include "${.CURDIR}/stdlib/jemalloc/Makefile.inc"
 .include "${.CURDIR}/stdtime/Makefile.inc"
 .include "${.CURDIR}/string/Makefile.inc"
 .include "${.CURDIR}/sys/Makefile.inc"
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/gen/tls.c
--- a/head/lib/libc/gen/tls.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libc/gen/tls.c	Sat Apr 21 21:38:59 2012 +0300
@@ -23,7 +23,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- *	$FreeBSD: head/lib/libc/gen/tls.c 232582 2012-03-06 03:42:54Z gonzo $
+ *	$FreeBSD: head/lib/libc/gen/tls.c 234370 2012-04-17 07:22:14Z jasone $
  */
 
 /*
@@ -39,6 +39,11 @@
 
 #include "libc_private.h"
 
+/* Provided by jemalloc to avoid bootstrapping issues. */
+void	*a0malloc(size_t size);
+void	*a0calloc(size_t num, size_t size);
+void	a0free(void *ptr);
+
 __weak_reference(__libc_allocate_tls, _rtld_allocate_tls);
 __weak_reference(__libc_free_tls, _rtld_free_tls);
 
@@ -120,8 +125,8 @@
 
 	tls = (Elf_Addr **)((Elf_Addr)tcb + tcbsize - TLS_TCB_SIZE);
 	dtv = tls[0];
-	free(dtv);
-	free(tcb);
+	a0free(dtv);
+	a0free(tcb);
 }
 
 /*
@@ -137,18 +142,18 @@
 	if (oldtcb != NULL && tcbsize == TLS_TCB_SIZE)
 		return (oldtcb);
 
-	tcb = calloc(1, tls_static_space + tcbsize - TLS_TCB_SIZE);
+	tcb = a0calloc(1, tls_static_space + tcbsize - TLS_TCB_SIZE);
 	tls = (Elf_Addr **)(tcb + tcbsize - TLS_TCB_SIZE);
 
 	if (oldtcb != NULL) {
 		memcpy(tls, oldtcb, tls_static_space);
-		free(oldtcb);
+		a0free(oldtcb);
 
 		/* Adjust the DTV. */
 		dtv = tls[0];
 		dtv[2] = (Elf_Addr)tls + TLS_TCB_SIZE;
 	} else {
-		dtv = malloc(3 * sizeof(Elf_Addr));
+		dtv = a0malloc(3 * sizeof(Elf_Addr));
 		tls[0] = dtv;
 		dtv[0] = 1;
 		dtv[1] = 1;
@@ -189,8 +194,8 @@
 	dtv = ((Elf_Addr**)tcb)[1];
 	tlsend = (Elf_Addr) tcb;
 	tlsstart = tlsend - size;
-	free((void*) tlsstart);
-	free(dtv);
+	a0free((void*) tlsstart);
+	a0free(dtv);
 }
 
 /*
@@ -208,8 +213,8 @@
 
 	if (tcbsize < 2 * sizeof(Elf_Addr))
 		tcbsize = 2 * sizeof(Elf_Addr);
-	tls = calloc(1, size + tcbsize);
-	dtv = malloc(3 * sizeof(Elf_Addr));
+	tls = a0calloc(1, size + tcbsize);
+	dtv = a0malloc(3 * sizeof(Elf_Addr));
 
 	segbase = (Elf_Addr)(tls + size);
 	((Elf_Addr*)segbase)[0] = segbase;
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdio/fputws.c
--- a/head/lib/libc/stdio/fputws.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libc/stdio/fputws.c	Sat Apr 21 21:38:59 2012 +0300
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/libc/stdio/fputws.c 227753 2011-11-20 14:45:42Z theraven $");
+__FBSDID("$FreeBSD: head/lib/libc/stdio/fputws.c 234536 2012-04-21 07:31:27Z das $");
 
 #include "namespace.h"
 #include <errno.h>
@@ -61,8 +61,8 @@
 	uio.uio_iov = &iov;
 	uio.uio_iovcnt = 1;
 	iov.iov_base = buf;
+	wsp = ws;
 	do {
-		wsp = ws;
 		nbytes = l->__wcsnrtombs(buf, &wsp, SIZE_T_MAX, sizeof(buf),
 		    &fp->_mbstate);
 		if (nbytes == (size_t)-1)
@@ -70,7 +70,7 @@
 		iov.iov_len = uio.uio_resid = nbytes;
 		if (__sfvwrite(fp, &uio) != 0)
 			goto error;
-	} while (ws != NULL);
+	} while (wsp != NULL);
 	FUNLOCKFILE(fp);
 	return (0);
 
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdio/printf.3
--- a/head/lib/libc/stdio/printf.3	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libc/stdio/printf.3	Sat Apr 21 21:38:59 2012 +0300
@@ -30,7 +30,7 @@
 .\" SUCH DAMAGE.
 .\"
 .\"     @(#)printf.3	8.1 (Berkeley) 6/4/93
-.\" $FreeBSD$
+.\" $FreeBSD: head/lib/libc/stdio/printf.3 234530 2012-04-21 06:09:09Z das $
 .\"
 .Dd December 2, 2009
 .Dt PRINTF 3
@@ -113,20 +113,6 @@
 .Xr stdarg 3 )
 are converted for output.
 .Pp
-These functions return the number of characters printed
-(not including the trailing
-.Ql \e0
-used to end output to strings) or a negative value if an output error occurs,
-except for
-.Fn snprintf
-and
-.Fn vsnprintf ,
-which return the number of characters that would have been printed if the
-.Fa size
-were unlimited
-(again, not including the final
-.Ql \e0 ) .
-.Pp
 The
 .Fn asprintf
 and
@@ -164,15 +150,19 @@
 .Fa size
 argument, the string was too short
 and some of the printed characters were discarded.
-The output is always null-terminated.
+The output is always null-terminated, unless
+.Fa size
+is 0.
 .Pp
 The
 .Fn sprintf
 and
 .Fn vsprintf
 functions
-effectively assume an infinite
-.Fa size .
+effectively assume a
+.Fa size
+of
+.Dv INT_MAX + 1.
 .Pp
 The format string is composed of zero or more directives:
 ordinary
@@ -670,6 +660,21 @@
 a numeric field; if the result of a conversion is wider than the field
 width, the
 field is expanded to contain the conversion result.
+.Sh RETURN VALUES
+These functions return the number of characters printed
+(not including the trailing
+.Ql \e0
+used to end output to strings),
+except for
+.Fn snprintf
+and
+.Fn vsnprintf ,
+which return the number of characters that would have been printed if the
+.Fa size
+were unlimited
+(again, not including the final
+.Ql \e0 ) .
+These functions return a negative value if an error occurs.
 .Sh EXAMPLES
 To print a date and time in the form
 .Dq Li "Sunday, July 3, 10:02" ,
@@ -771,6 +776,13 @@
 An invalid wide character code was encountered.
 .It Bq Er ENOMEM
 Insufficient storage space is available.
+.It Bq Er EOVERFLOW
+The
+.Fa size
+argument exceeds
+.Dv INT_MAX + 1 ,
+or the return value would be too large to be represented by an
+.Vt int .
 .El
 .Sh SEE ALSO
 .Xr printf 1 ,
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdio/snprintf.c
--- a/head/lib/libc/stdio/snprintf.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libc/stdio/snprintf.c	Sat Apr 21 21:38:59 2012 +0300
@@ -39,8 +39,9 @@
 static char sccsid[] = "@(#)snprintf.c	8.1 (Berkeley) 6/4/93";
 #endif /* LIBC_SCCS and not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/libc/stdio/snprintf.c 227753 2011-11-20 14:45:42Z theraven $");
+__FBSDID("$FreeBSD: head/lib/libc/stdio/snprintf.c 234531 2012-04-21 06:10:18Z das $");
 
+#include <errno.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdarg.h>
@@ -59,8 +60,11 @@
 	on = n;
 	if (n != 0)
 		n--;
-	if (n > INT_MAX)
-		n = INT_MAX;
+	if (n > INT_MAX) {
+		errno = EOVERFLOW;
+		*str = '\0';
+		return (EOF);
+	}
 	va_start(ap, fmt);
 	f._flags = __SWR | __SSTR;
 	f._bf._base = f._p = (unsigned char *)str;
@@ -84,8 +88,11 @@
 	on = n;
 	if (n != 0)
 		n--;
-	if (n > INT_MAX)
-		n = INT_MAX;
+	if (n > INT_MAX) {
+		errno = EOVERFLOW;
+		*str = '\0';
+		return (EOF);
+	}
 	va_start(ap, fmt);
 	f._flags = __SWR | __SSTR;
 	f._bf._base = f._p = (unsigned char *)str;
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdio/vfprintf.c
--- a/head/lib/libc/stdio/vfprintf.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libc/stdio/vfprintf.c	Sat Apr 21 21:38:59 2012 +0300
@@ -39,7 +39,7 @@
 static char sccsid[] = "@(#)vfprintf.c	8.1 (Berkeley) 6/4/93";
 #endif /* LIBC_SCCS and not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/libc/stdio/vfprintf.c 227753 2011-11-20 14:45:42Z theraven $");
+__FBSDID("$FreeBSD: head/lib/libc/stdio/vfprintf.c 234531 2012-04-21 06:10:18Z das $");
 
 /*
  * Actual printf innards.
@@ -51,6 +51,7 @@
 #include <sys/types.h>
 
 #include <ctype.h>
+#include <errno.h>
 #include <limits.h>
 #include <locale.h>
 #include <stddef.h>
@@ -480,6 +481,7 @@
 		if ((n = fmt - cp) != 0) {
 			if ((unsigned)ret + n > INT_MAX) {
 				ret = EOF;
+				errno = EOVERFLOW;
 				goto error;
 			}
 			PRINT(cp, n);
@@ -935,6 +937,7 @@
 		prsize = width > realsz ? width : realsz;
 		if ((unsigned)ret + prsize > INT_MAX) {
 			ret = EOF;
+			errno = EOVERFLOW;
 			goto error;
 		}
 
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdio/vfwprintf.c
--- a/head/lib/libc/stdio/vfwprintf.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libc/stdio/vfwprintf.c	Sat Apr 21 21:38:59 2012 +0300
@@ -41,7 +41,7 @@
 #endif /* LIBC_SCCS and not lint */
 #endif
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/libc/stdio/vfwprintf.c 227753 2011-11-20 14:45:42Z theraven $");
+__FBSDID("$FreeBSD: head/lib/libc/stdio/vfwprintf.c 234531 2012-04-21 06:10:18Z das $");
 
 /*
  * Actual wprintf innards.
@@ -54,6 +54,7 @@
 #include <sys/types.h>
 
 #include <ctype.h>
+#include <errno.h>
 #include <limits.h>
 #include <locale.h>
 #include <stdarg.h>
@@ -553,6 +554,7 @@
 		if ((n = fmt - cp) != 0) {
 			if ((unsigned)ret + n > INT_MAX) {
 				ret = EOF;
+				errno = EOVERFLOW;
 				goto error;
 			}
 			PRINT(cp, n);
@@ -1003,6 +1005,7 @@
 		prsize = width > realsz ? width : realsz;
 		if ((unsigned)ret + prsize > INT_MAX) {
 			ret = EOF;
+			errno = EOVERFLOW;
 			goto error;
 		}
 
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdio/vsnprintf.c
--- a/head/lib/libc/stdio/vsnprintf.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libc/stdio/vsnprintf.c	Sat Apr 21 21:38:59 2012 +0300
@@ -39,8 +39,9 @@
 static char sccsid[] = "@(#)vsnprintf.c	8.1 (Berkeley) 6/4/93";
 #endif /* LIBC_SCCS and not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/libc/stdio/vsnprintf.c 227753 2011-11-20 14:45:42Z theraven $");
+__FBSDID("$FreeBSD: head/lib/libc/stdio/vsnprintf.c 234531 2012-04-21 06:10:18Z das $");
 
+#include <errno.h>
 #include <limits.h>
 #include <stdio.h>
 #include "local.h"
@@ -59,8 +60,11 @@
 	on = n;
 	if (n != 0)
 		n--;
-	if (n > INT_MAX)
-		n = INT_MAX;
+	if (n > INT_MAX) {
+		errno = EOVERFLOW;
+		*str = '\0';
+		return (EOF);
+	}
 	/* Stdio internals do not deal correctly with zero length buffer */
 	if (n == 0) {
 		if (on > 0)
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdio/vswprintf.c
--- a/head/lib/libc/stdio/vswprintf.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libc/stdio/vswprintf.c	Sat Apr 21 21:38:59 2012 +0300
@@ -36,9 +36,10 @@
 #if 0
 __FBSDID("FreeBSD: src/lib/libc/stdio/vasprintf.c,v 1.16 2002/08/21 16:19:57 mike Exp ");
 #endif
-__FBSDID("$FreeBSD: head/lib/libc/stdio/vswprintf.c 227753 2011-11-20 14:45:42Z theraven $");
+__FBSDID("$FreeBSD: head/lib/libc/stdio/vswprintf.c 234531 2012-04-21 06:10:18Z das $");
 
 #include <errno.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <wchar.h>
@@ -61,11 +62,17 @@
 		errno = EINVAL;
 		return (-1);
 	}
+	if (n - 1 > INT_MAX) {
+		errno = EOVERFLOW;
+		*s = L'\0';
+		return (-1);
+	}
 
 	f._flags = __SWR | __SSTR | __SALC;
 	f._bf._base = f._p = (unsigned char *)malloc(128);
 	if (f._bf._base == NULL) {
 		errno = ENOMEM;
+		*s = L'\0';
 		return (-1);
 	}
 	f._bf._size = f._w = 127;		/* Leave room for the NUL */
@@ -74,6 +81,7 @@
 		sverrno = errno;
 		free(f._bf._base);
 		errno = sverrno;
+		*s = L'\0';
 		return (-1);
 	}
 	*f._p = '\0';
@@ -87,6 +95,7 @@
 	free(f._bf._base);
 	if (nwc == (size_t)-1) {
 		errno = EILSEQ;
+		*s = L'\0';
 		return (-1);
 	}
 	if (nwc == n) {
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdlib/Makefile.inc
--- a/head/lib/libc/stdlib/Makefile.inc	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libc/stdlib/Makefile.inc	Sat Apr 21 21:38:59 2012 +0300
@@ -1,5 +1,5 @@
 #	from @(#)Makefile.inc	8.3 (Berkeley) 2/4/95
-# $FreeBSD: head/lib/libc/stdlib/Makefile.inc 229848 2012-01-09 06:36:28Z ed $
+# $FreeBSD: head/lib/libc/stdlib/Makefile.inc 234370 2012-04-17 07:22:14Z jasone $
 
 # machine-independent stdlib sources
 .PATH: ${.CURDIR}/${LIBC_ARCH}/stdlib ${.CURDIR}/stdlib
@@ -7,7 +7,7 @@
 MISRCS+=_Exit.c a64l.c abort.c abs.c atexit.c atof.c atoi.c atol.c atoll.c \
 	bsearch.c div.c exit.c getenv.c getopt.c getopt_long.c \
 	getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c \
-	insque.c l64a.c labs.c ldiv.c llabs.c lldiv.c lsearch.c malloc.c \
+	insque.c l64a.c labs.c ldiv.c llabs.c lldiv.c lsearch.c \
 	merge.c ptsname.c qsort.c qsort_r.c quick_exit.c radixsort.c rand.c \
 	random.c reallocf.c realpath.c remque.c strfmon.c strtoimax.c \
 	strtol.c strtoll.c strtoq.c strtoul.c strtonum.c strtoull.c \
@@ -18,18 +18,17 @@
 # machine-dependent stdlib sources
 .sinclude "${.CURDIR}/${LIBC_ARCH}/stdlib/Makefile.inc"
 
-MAN+=	a64l.3 abort.3 abs.3 aligned_alloc.3 alloca.3 atexit.3 atof.3 \
+MAN+=	a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 \
 	atoi.3 atol.3 at_quick_exit.3 bsearch.3 \
 	div.3 exit.3 getenv.3 getopt.3 getopt_long.3 getsubopt.3 \
 	hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 llabs.3 lldiv.3 \
-	lsearch.3 malloc.3 memory.3 ptsname.3 qsort.3 \
+	lsearch.3 memory.3 ptsname.3 qsort.3 \
 	quick_exit.3 \
-	radixsort.3 rand.3 random.3 \
+	radixsort.3 rand.3 random.3 reallocf.3 \
 	realpath.3 strfmon.3 strtod.3 strtol.3 strtonum.3 strtoul.3 system.3 \
 	tsearch.3
 
 MLINKS+=a64l.3 l64a.3 a64l.3 l64a_r.3
-MLINKS+=aligned_alloc.3 posix_memalign.3
 MLINKS+=atol.3 atoll.3
 MLINKS+=exit.3 _Exit.3
 MLINKS+=getenv.3 putenv.3 getenv.3 setenv.3 getenv.3 unsetenv.3
@@ -46,11 +45,4 @@
 MLINKS+=strtod.3 strtof.3 strtod.3 strtold.3
 MLINKS+=strtol.3 strtoll.3 strtol.3 strtoq.3 strtol.3 strtoimax.3
 MLINKS+=strtoul.3 strtoull.3 strtoul.3 strtouq.3 strtoul.3 strtoumax.3
-MLINKS+=malloc.3 calloc.3 malloc.3 free.3 malloc.3 malloc.conf.5 \
-	malloc.3 realloc.3 malloc.3 reallocf.3 malloc.3 malloc_usable_size.3
 MLINKS+=tsearch.3 tdelete.3 tsearch.3 tfind.3 tsearch.3 twalk.3
-
-.if defined(MALLOC_PRODUCTION)
-CFLAGS+=	-DMALLOC_PRODUCTION
-.endif
-
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdlib/Symbol.map
--- a/head/lib/libc/stdlib/Symbol.map	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libc/stdlib/Symbol.map	Sat Apr 21 21:38:59 2012 +0300
@@ -1,5 +1,5 @@
 /*
- * $FreeBSD: head/lib/libc/stdlib/Symbol.map 229848 2012-01-09 06:36:28Z ed $
+ * $FreeBSD: head/lib/libc/stdlib/Symbol.map 234370 2012-04-17 07:22:14Z jasone $
  */
 
 FBSD_1.0 {
@@ -47,14 +47,6 @@
 	lldiv;
 	lsearch;
 	lfind;
-	_malloc_options;
-	_malloc_message;
-	malloc;
-	posix_memalign;
-	calloc;
-	realloc;
-	free;
-	malloc_usable_size;
 	mergesort;
 	putenv;
 	qsort_r;
@@ -93,7 +85,6 @@
 };
 
 FBSD_1.3 {
-	aligned_alloc;
 	at_quick_exit;
 	atof_l;
 	atoi_l;
@@ -114,9 +105,6 @@
 };
 
 FBSDprivate_1.0 {
-	_malloc_thread_cleanup;
-	_malloc_prefork;
-	_malloc_postfork;
 	__system;
 	_system;
 };
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdlib/aligned_alloc.3
--- a/head/lib/libc/stdlib/aligned_alloc.3	Sat Apr 21 21:30:13 2012 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,126 +0,0 @@
-.\" Copyright (C) 2006 Jason Evans <jasone at FreeBSD.org>.
-.\" All rights reserved.
-.\"
-.\" Redistribution and use in source and binary forms, with or without
-.\" modification, are permitted provided that the following conditions
-.\" are met:
-.\" 1. Redistributions of source code must retain the above copyright
-.\"    notice(s), this list of conditions and the following disclaimer as
-.\"    the first lines of this file unmodified other than the possible
-.\"    addition of one or more copyright notices.
-.\" 2. Redistributions in binary form must reproduce the above copyright
-.\"    notice(s), this list of conditions and the following disclaimer in
-.\"    the documentation and/or other materials provided with the
-.\"    distribution.
-.\"
-.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
-.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
-.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
-.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
-.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
-.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-.\"
-.\" $FreeBSD: head/lib/libc/stdlib/aligned_alloc.3 229848 2012-01-09 06:36:28Z ed $
-.\"
-.Dd January 7, 2011
-.Dt ALIGNED_ALLOC 3
-.Os
-.Sh NAME
-.Nm aligned_alloc ,
-.Nm posix_memalign
-.Nd aligned memory allocation
-.Sh LIBRARY
-.Lb libc
-.Sh SYNOPSIS
-.In stdlib.h
-.Ft void *
-.Fn aligned_alloc "size_t alignment" "size_t size"
-.Ft int
-.Fn posix_memalign "void **ptr" "size_t alignment" "size_t size"
-.Sh DESCRIPTION
-The
-.Fn aligned_alloc
-and
-.Fn posix_memalign
-functions allocate
-.Fa size
-bytes of memory such that the allocation's base address is an even multiple of
-.Fa alignment .
-The
-.Fn aligned_alloc
-function returns the allocation, while the
-.Fn posix_memalign
-function stores the allocation in the value pointed to by
-.Fa ptr .
-.Pp
-The requested
-.Fa alignment
-must be a power of 2 at least as large as
-.Fn sizeof "void *" .
-.Pp
-Memory that is allocated via
-.Fn aligned_alloc
-and
-.Fn posix_memalign
-can be used as an argument in subsequent calls to
-.Xr realloc 3 ,
-.Xr reallocf 3 ,
-and
-.Xr free 3 .
-.Sh RETURN VALUES
-The
-.Fn aligned_alloc
-function returns a pointer to the allocation if successful; otherwise a
-NULL pointer is returned and
-.Va errno
-is set to an error value.
-.Pp
-The
-.Fn posix_memalign
-function returns the value 0 if successful; otherwise it returns an error value.
-.Sh ERRORS
-The
-.Fn aligned_alloc
-and
-.Fn posix_memalign
-functions will fail if:
-.Bl -tag -width Er
-.It Bq Er EINVAL
-The
-.Fa alignment
-parameter is not a power of 2 at least as large as
-.Fn sizeof "void *" .
-.It Bq Er ENOMEM
-Memory allocation error.
-.El
-.Sh SEE ALSO
-.Xr free 3 ,
-.Xr malloc 3 ,
-.Xr realloc 3 ,
-.Xr reallocf 3 ,
-.Xr valloc 3
-.Sh STANDARDS
-The
-.Fn aligned_alloc
-function conforms to
-.St -isoC-2011 .
-.Pp
-The
-.Fn posix_memalign
-function conforms to
-.St -p1003.1-2001 .
-.Sh HISTORY
-The
-.Fn posix_memalign
-function first appeared in
-.Fx 7.0 .
-.Pp
-The
-.Fn aligned_alloc
-function first appeared in
-.Fx 10.0 .
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdlib/jemalloc/Makefile.inc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/lib/libc/stdlib/jemalloc/Makefile.inc	Sat Apr 21 21:38:59 2012 +0300
@@ -0,0 +1,46 @@
+# $FreeBSD: head/lib/libc/stdlib/jemalloc/Makefile.inc 234370 2012-04-17 07:22:14Z jasone $
+
+.PATH: ${.CURDIR}/stdlib/jemalloc
+
+JEMALLOCSRCS:= jemalloc.c arena.c atomic.c base.c bitmap.c chunk.c \
+	chunk_dss.c chunk_mmap.c ckh.c ctl.c extent.c hash.c huge.c mb.c \
+	mutex.c prof.c quarantine.c rtree.c stats.c tcache.c util.c tsd.c
+
+SYM_MAPS+=${.CURDIR}/stdlib/jemalloc/Symbol.map
+
+CFLAGS+=-I${.CURDIR}/../../contrib/jemalloc/include
+
+.for src in ${JEMALLOCSRCS}
+MISRCS+=jemalloc_${src}
+CLEANFILES+=jemalloc_${src}
+jemalloc_${src}:
+	ln -sf ${.CURDIR}/../../contrib/jemalloc/src/${src} ${.TARGET}
+.endfor
+
+MAN+=jemalloc.3
+CLEANFILES+=jemalloc.3
+jemalloc.3:
+	ln -sf ${.CURDIR}/../../contrib/jemalloc/doc/jemalloc.3 ${.TARGET}
+
+MLINKS+= \
+	jemalloc.3 malloc.3 \
+	jemalloc.3 calloc.3 \
+	jemalloc.3 posix_memalign.3 \
+	jemalloc.3 aligned_alloc.3 \
+	jemalloc.3 realloc.3 \
+	jemalloc.3 free.3 \
+	jemalloc.3 malloc_usable_size.3 \
+	jemalloc.3 malloc_stats_print.3 \
+	jemalloc.3 mallctl.3 \
+	jemalloc.3 mallctlnametomib.3 \
+	jemalloc.3 mallctlbymib.3 \
+	jemalloc.3 allocm.3 \
+	jemalloc.3 rallocm.3 \
+	jemalloc.3 sallocm.3 \
+	jemalloc.3 dallocm.3 \
+	jemalloc.3 nallocm.3 \
+	jemalloc.3 malloc.conf.5
+
+.if defined(MALLOC_PRODUCTION)
+CFLAGS+=	-DMALLOC_PRODUCTION
+.endif
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdlib/jemalloc/Symbol.map
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/lib/libc/stdlib/jemalloc/Symbol.map	Sat Apr 21 21:38:59 2012 +0300
@@ -0,0 +1,35 @@
+/*
+ * $FreeBSD: head/lib/libc/stdlib/jemalloc/Symbol.map 234370 2012-04-17 07:22:14Z jasone $
+ */
+
+FBSD_1.0 {
+	_malloc_options;
+	_malloc_message;
+	malloc;
+	posix_memalign;
+	calloc;
+	realloc;
+	free;
+	malloc_usable_size;
+};
+
+FBSD_1.3 {
+	malloc_conf;
+	malloc_message;
+	aligned_alloc;
+	malloc_stats_print;
+	mallctl;
+	mallctlnametomib;
+	mallctlbymib;
+	allocm;
+	rallocm;
+	sallocm;
+	dallocm;
+	nallocm;
+};
+
+FBSDprivate_1.0 {
+	_malloc_thread_cleanup;
+	_malloc_prefork;
+	_malloc_postfork;
+};
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdlib/malloc.3
--- a/head/lib/libc/stdlib/malloc.3	Sat Apr 21 21:30:13 2012 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,591 +0,0 @@
-.\" Copyright (c) 1980, 1991, 1993
-.\"	The Regents of the University of California.  All rights reserved.
-.\"
-.\" This code is derived from software contributed to Berkeley by
-.\" the American National Standards Committee X3, on Information
-.\" Processing Systems.
-.\"
-.\" Redistribution and use in source and binary forms, with or without
-.\" modification, are permitted provided that the following conditions
-.\" are met:
-.\" 1. Redistributions of source code must retain the above copyright
-.\"    notice, this list of conditions and the following disclaimer.
-.\" 2. Redistributions in binary form must reproduce the above copyright
-.\"    notice, this list of conditions and the following disclaimer in the
-.\"    documentation and/or other materials provided with the distribution.
-.\" 3. Neither the name of the University nor the names of its contributors
-.\"    may be used to endorse or promote products derived from this software
-.\"    without specific prior written permission.
-.\"
-.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-.\" SUCH DAMAGE.
-.\"
-.\"     @(#)malloc.3	8.1 (Berkeley) 6/4/93
-.\" $FreeBSD: head/lib/libc/stdlib/malloc.3 219377 2011-03-07 15:19:17Z rstone $
-.\"
-.Dd January 31, 2010
-.Dt MALLOC 3
-.Os
-.Sh NAME
-.Nm malloc , calloc , realloc , free , reallocf , malloc_usable_size
-.Nd general purpose memory allocation functions
-.Sh LIBRARY
-.Lb libc
-.Sh SYNOPSIS
-.In stdlib.h
-.Ft void *
-.Fn malloc "size_t size"
-.Ft void *
-.Fn calloc "size_t number" "size_t size"
-.Ft void *
-.Fn realloc "void *ptr" "size_t size"
-.Ft void *
-.Fn reallocf "void *ptr" "size_t size"
-.Ft void
-.Fn free "void *ptr"
-.Ft const char *
-.Va _malloc_options ;
-.Ft void
-.Fn \*(lp*_malloc_message\*(rp "const char *p1" "const char *p2" "const char *p3" "const char *p4"
-.In malloc_np.h
-.Ft size_t
-.Fn malloc_usable_size "const void *ptr"
-.Sh DESCRIPTION
-The
-.Fn malloc
-function allocates
-.Fa size
-bytes of uninitialized memory.
-The allocated space is suitably aligned (after possible pointer coercion)
-for storage of any type of object.
-.Pp
-The
-.Fn calloc
-function allocates space for
-.Fa number
-objects,
-each
-.Fa size
-bytes in length.
-The result is identical to calling
-.Fn malloc
-with an argument of
-.Dq "number * size" ,
-with the exception that the allocated memory is explicitly initialized
-to zero bytes.
-.Pp
-The
-.Fn realloc
-function changes the size of the previously allocated memory referenced by
-.Fa ptr
-to
-.Fa size
-bytes.
-The contents of the memory are unchanged up to the lesser of the new and
-old sizes.
-If the new size is larger,
-the contents of the newly allocated portion of the memory are undefined.
-Upon success, the memory referenced by
-.Fa ptr
-is freed and a pointer to the newly allocated memory is returned.
-Note that
-.Fn realloc
-and
-.Fn reallocf
-may move the memory allocation, resulting in a different return value than
-.Fa ptr .
-If
-.Fa ptr
-is
-.Dv NULL ,
-the
-.Fn realloc
-function behaves identically to
-.Fn malloc
-for the specified size.
-.Pp
-The
-.Fn reallocf
-function is identical to the
-.Fn realloc
-function, except that it
-will free the passed pointer when the requested memory cannot be allocated.
-This is a
-.Fx
-specific API designed to ease the problems with traditional coding styles
-for
-.Fn realloc
-causing memory leaks in libraries.
-.Pp
-The
-.Fn free
-function causes the allocated memory referenced by
-.Fa ptr
-to be made available for future allocations.
-If
-.Fa ptr
-is
-.Dv NULL ,
-no action occurs.
-.Pp
-The
-.Fn malloc_usable_size
-function returns the usable size of the allocation pointed to by
-.Fa ptr .
-The return value may be larger than the size that was requested during
-allocation.
-The
-.Fn malloc_usable_size
-function is not a mechanism for in-place
-.Fn realloc ;
-rather it is provided solely as a tool for introspection purposes.
-Any discrepancy between the requested allocation size and the size reported by
-.Fn malloc_usable_size
-should not be depended on, since such behavior is entirely
-implementation-dependent.
-.Sh TUNING
-Once, when the first call is made to one of these memory allocation
-routines, various flags will be set or reset, which affects the
-workings of this allocator implementation.
-.Pp
-The
-.Dq name
-of the file referenced by the symbolic link named
-.Pa /etc/malloc.conf ,
-the value of the environment variable
-.Ev MALLOC_OPTIONS ,
-and the string pointed to by the global variable
-.Va _malloc_options
-will be interpreted, in that order, from left to right as flags.
-.Pp
-Each flag is a single letter, optionally prefixed by a non-negative base 10
-integer repetition count.
-For example,
-.Dq 3N
-is equivalent to
-.Dq NNN .
-Some flags control parameter magnitudes, where uppercase increases the
-magnitude, and lowercase decreases the magnitude.
-Other flags control boolean parameters, where uppercase indicates that a
-behavior is set, or on, and lowercase means that a behavior is not set, or off.
-.Bl -tag -width indent
-.It A
-All warnings (except for the warning about unknown
-flags being set) become fatal.
-The process will call
-.Xr abort 3
-in these cases.
-.It C
-Double/halve the size of the maximum size class that is a multiple of the
-cacheline size (64).
-Above this size, subpage spacing (256 bytes) is used for size classes.
-The default value is 512 bytes.
-.It D
-Use
-.Xr sbrk 2
-to acquire memory in the data storage segment (DSS).
-This option is enabled by default.
-See the
-.Dq M
-option for related information and interactions.
-.It E
-Double/halve the size of the maximum medium size class.
-The valid range is from one page to one half chunk.
-The default value is 32 KiB.
-.It F
-Halve/double the per-arena minimum ratio of active to dirty pages.
-Some dirty unused pages may be allowed to accumulate, within the limit set by
-the ratio, before informing the kernel about at least half of those pages via
-.Xr madvise 2 .
-This provides the kernel with sufficient information to recycle dirty pages if
-physical memory becomes scarce and the pages remain unused.
-The default minimum ratio is 32:1;
-.Ev MALLOC_OPTIONS=6F
-will disable dirty page purging.
-.It G
-Double/halve the approximate interval (counted in terms of
-thread-specific cache allocation/deallocation events) between full
-thread-specific cache garbage collection sweeps.
-Garbage collection is actually performed incrementally, one size
-class at a time, in order to avoid large collection pauses.
-The default sweep interval is 8192;
-.Ev MALLOC_OPTIONS=14g
-will disable garbage collection.
-.It H
-Double/halve the number of thread-specific cache slots per size
-class.
-When there are multiple threads, each thread uses a
-thread-specific cache for small and medium objects.
-Thread-specific caching allows many allocations to be satisfied
-without performing any thread synchronization, at the cost of
-increased memory use.
-See the
-.Dq G
-option for related tuning information.
-The default number of cache slots is 128;
-.Ev MALLOC_OPTIONS=7h
-will disable thread-specific caching.
-Note that one cache slot per size class is not a valid
-configuration due to implementation details.
-.It J
-Each byte of new memory allocated by
-.Fn malloc ,
-.Fn realloc ,
-or
-.Fn reallocf
-will be initialized to 0xa5.
-All memory returned by
-.Fn free ,
-.Fn realloc ,
-or
-.Fn reallocf
-will be initialized to 0x5a.
-This is intended for debugging and will impact performance negatively.
-.It K
-Double/halve the virtual memory chunk size.
-The default chunk size is 4 MiB.
-.It M
-Use
-.Xr mmap 2
-to acquire anonymously mapped memory.
-This option is enabled by default.
-If both the
-.Dq D
-and
-.Dq M
-options are enabled, the allocator prefers anonymous mappings over the DSS,
-but allocation only fails if memory cannot be acquired via either method.
-If neither option is enabled, then the
-.Dq M
-option is implicitly enabled in order to assure that there is a method for
-acquiring memory.
-.It N
-Double/halve the number of arenas.
-The default number of arenas is two times the number of CPUs, or one if there
-is a single CPU.
-.It P
-Various statistics are printed at program exit via an
-.Xr atexit 3
-function.
-This has the potential to cause deadlock for a multi-threaded process that exits
-while one or more threads are executing in the memory allocation functions.
-Therefore, this option should only be used with care; it is primarily intended
-as a performance tuning aid during application development.
-.It Q
-Double/halve the size of the maximum size class that is a multiple of the
-quantum (8 or 16 bytes, depending on architecture).
-Above this size, cacheline spacing is used for size classes.
-The default value is 128 bytes.
-.It U
-Generate
-.Dq utrace
-entries for
-.Xr ktrace 1 ,
-for all operations.
-Consult the source for details on this option.
-.It V
-Attempting to allocate zero bytes will return a
-.Dv NULL
-pointer instead of a valid pointer.
-(The default behavior is to make a minimal allocation and return a
-pointer to it.)
-This option is provided for System V compatibility.
-This option is incompatible with the
-.Dq X
-option.
-.It X
-Rather than return failure for any allocation function, display a diagnostic
-message on
-.Dv STDERR_FILENO
-and cause the program to drop core (using
-.Xr abort 3 ) .
-This option should be set at compile time by including the following in the
-source code:
-.Bd -literal -offset indent
-_malloc_options = "X";
-.Ed
-.It Z
-Each byte of new memory allocated by
-.Fn malloc ,
-.Fn realloc ,
-or
-.Fn reallocf
-will be initialized to 0.
-Note that this initialization only happens once for each byte, so
-.Fn realloc
-and
-.Fn reallocf
-calls do not zero memory that was previously allocated.
-This is intended for debugging and will impact performance negatively.
-.El
-.Pp
-The
-.Dq J
-and
-.Dq Z
-options are intended for testing and debugging.
-An application which changes its behavior when these options are used
-is flawed.
-.Sh IMPLEMENTATION NOTES
-Traditionally, allocators have used
-.Xr sbrk 2
-to obtain memory, which is suboptimal for several reasons, including race
-conditions, increased fragmentation, and artificial limitations on maximum
-usable memory.
-This allocator uses both
-.Xr sbrk 2
-and
-.Xr mmap 2
-by default, but it can be configured at run time to use only one or the other.
-If resource limits are not a primary concern, the preferred configuration is
-.Ev MALLOC_OPTIONS=dM
-or
-.Ev MALLOC_OPTIONS=DM .
-When so configured, the
-.Ar datasize
-resource limit has little practical effect for typical applications; use
-.Ev MALLOC_OPTIONS=Dm
-if that is a concern.
-Regardless of allocator configuration, the
-.Ar vmemoryuse
-resource limit can be used to bound the total virtual memory used by a
-process, as described in
-.Xr limits 1 .
-.Pp
-This allocator uses multiple arenas in order to reduce lock contention for
-threaded programs on multi-processor systems.
-This works well with regard to threading scalability, but incurs some costs.
-There is a small fixed per-arena overhead, and additionally, arenas manage
-memory completely independently of each other, which means a small fixed
-increase in overall memory fragmentation.
-These overheads are not generally an issue, given the number of arenas normally
-used.
-Note that using substantially more arenas than the default is not likely to
-improve performance, mainly due to reduced cache performance.
-However, it may make sense to reduce the number of arenas if an application
-does not make much use of the allocation functions.
-.Pp
-In addition to multiple arenas, this allocator supports thread-specific caching
-for small and medium objects, in order to make it possible to completely avoid
-synchronization for most small and medium allocation requests.
-Such caching allows very fast allocation in the common case, but it increases
-memory usage and fragmentation, since a bounded number of objects can remain
-allocated in each thread cache.
-.Pp
-Memory is conceptually broken into equal-sized chunks, where the chunk size is
-a power of two that is greater than the page size.
-Chunks are always aligned to multiples of the chunk size.
-This alignment makes it possible to find metadata for user objects very
-quickly.
-.Pp
-User objects are broken into four categories according to size: small, medium,
-large, and huge.
-Small objects are smaller than one page.
-Medium objects range from one page to an upper limit determined at run time (see
-the
-.Dq E
-option).
-Large objects are smaller than the chunk size.
-Huge objects are a multiple of the chunk size.
-Small, medium, and large objects are managed by arenas; huge objects are managed
-separately in a single data structure that is shared by all threads.
-Huge objects are used by applications infrequently enough that this single
-data structure is not a scalability issue.
-.Pp
-Each chunk that is managed by an arena tracks its contents as runs of
-contiguous pages (unused, backing a set of small or medium objects, or backing
-one large object).
-The combination of chunk alignment and chunk page maps makes it possible to
-determine all metadata regarding small and large allocations in constant time.
-.Pp
-Small and medium objects are managed in groups by page runs.
-Each run maintains a bitmap that tracks which regions are in use.
-Allocation requests that are no more than half the quantum (8 or 16, depending
-on architecture) are rounded up to the nearest power of two.
-Allocation requests that are more than half the quantum, but no more than the
-minimum cacheline-multiple size class (see the
-.Dq Q
-option) are rounded up to the nearest multiple of the quantum.
-Allocation requests that are more than the minimum cacheline-multiple size
-class, but no more than the minimum subpage-multiple size class (see the
-.Dq C
-option) are rounded up to the nearest multiple of the cacheline size (64).
-Allocation requests that are more than the minimum subpage-multiple size class,
-but no more than the maximum subpage-multiple size class are rounded up to the
-nearest multiple of the subpage size (256).
-Allocation requests that are more than the maximum subpage-multiple size class,
-but no more than the maximum medium size class (see the
-.Dq M
-option) are rounded up to the nearest medium size class; spacing is an
-automatically determined power of two and ranges from the subpage size to the
-page size.
-Allocation requests that are more than the maximum medium size class, but small
-enough to fit in an arena-managed chunk (see the
-.Dq K
-option), are rounded up to the nearest run size.
-Allocation requests that are too large to fit in an arena-managed chunk are
-rounded up to the nearest multiple of the chunk size.
-.Pp
-Allocations are packed tightly together, which can be an issue for
-multi-threaded applications.
-If you need to assure that allocations do not suffer from cacheline sharing,
-round your allocation requests up to the nearest multiple of the cacheline
-size.
-.Sh DEBUGGING MALLOC PROBLEMS
-The first thing to do is to set the
-.Dq A
-option.
-This option forces a coredump (if possible) at the first sign of trouble,
-rather than the normal policy of trying to continue if at all possible.
-.Pp
-It is probably also a good idea to recompile the program with suitable
-options and symbols for debugger support.
-.Pp
-If the program starts to give unusual results, coredump or generally behave
-differently without emitting any of the messages mentioned in the next
-section, it is likely because it depends on the storage being filled with
-zero bytes.
-Try running it with the
-.Dq Z
-option set;
-if that improves the situation, this diagnosis has been confirmed.
-If the program still misbehaves,
-the likely problem is accessing memory outside the allocated area.
-.Pp
-Alternatively, if the symptoms are not easy to reproduce, setting the
-.Dq J
-option may help provoke the problem.
-.Pp
-In truly difficult cases, the
-.Dq U
-option, if supported by the kernel, can provide a detailed trace of
-all calls made to these functions.
-.Pp
-Unfortunately this implementation does not provide much detail about
-the problems it detects; the performance impact for storing such information
-would be prohibitive.
-There are a number of allocator implementations available on the Internet
-which focus on detecting and pinpointing problems by trading performance for
-extra sanity checks and detailed diagnostics.
-.Sh DIAGNOSTIC MESSAGES
-If any of the memory allocation/deallocation functions detect an error or
-warning condition, a message will be printed to file descriptor
-.Dv STDERR_FILENO .
-Errors will result in the process dumping core.
-If the
-.Dq A
-option is set, all warnings are treated as errors.
-.Pp
-The
-.Va _malloc_message
-variable allows the programmer to override the function which emits the text
-strings forming the errors and warnings if for some reason the
-.Dv STDERR_FILENO
-file descriptor is not suitable for this.
-Please note that doing anything which tries to allocate memory in this function
-is likely to result in a crash or deadlock.
-.Pp
-All messages are prefixed by
-.Dq Ao Ar progname Ac Ns Li : (malloc) .
-.Sh RETURN VALUES
-The
-.Fn malloc
-and
-.Fn calloc
-functions return a pointer to the allocated memory if successful; otherwise
-a
-.Dv NULL
-pointer is returned and
-.Va errno
-is set to
-.Er ENOMEM .
-.Pp
-The
-.Fn realloc
-and
-.Fn reallocf
-functions return a pointer, possibly identical to
-.Fa ptr ,
-to the allocated memory
-if successful; otherwise a
-.Dv NULL
-pointer is returned, and
-.Va errno
-is set to
-.Er ENOMEM
-if the error was the result of an allocation failure.
-The
-.Fn realloc
-function always leaves the original buffer intact
-when an error occurs, whereas
-.Fn reallocf
-deallocates it in this case.
-.Pp
-The
-.Fn free
-function returns no value.
-.Pp
-The
-.Fn malloc_usable_size
-function returns the usable size of the allocation pointed to by
-.Fa ptr .
-.Sh ENVIRONMENT
-The following environment variables affect the execution of the allocation
-functions:
-.Bl -tag -width ".Ev MALLOC_OPTIONS"
-.It Ev MALLOC_OPTIONS
-If the environment variable
-.Ev MALLOC_OPTIONS
-is set, the characters it contains will be interpreted as flags to the
-allocation functions.
-.El
-.Sh EXAMPLES
-To dump core whenever a problem occurs:
-.Bd -literal -offset indent
-ln -s 'A' /etc/malloc.conf
-.Ed
-.Pp
-To specify in the source that a program does no return value checking
-on calls to these functions:
-.Bd -literal -offset indent
-_malloc_options = "X";
-.Ed
-.Sh SEE ALSO
-.Xr limits 1 ,
-.Xr madvise 2 ,
-.Xr mmap 2 ,
-.Xr sbrk 2 ,
-.Xr alloca 3 ,
-.Xr atexit 3 ,
-.Xr getpagesize 3 ,
-.Xr getpagesizes 3 ,
-.Xr memory 3 ,
-.Xr posix_memalign 3
-.Sh STANDARDS
-The
-.Fn malloc ,
-.Fn calloc ,
-.Fn realloc
-and
-.Fn free
-functions conform to
-.St -isoC .
-.Sh HISTORY
-The
-.Fn reallocf
-function first appeared in
-.Fx 3.0 .
-.Pp
-The
-.Fn malloc_usable_size
-function first appeared in
-.Fx 7.0 .
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdlib/malloc.c
--- a/head/lib/libc/stdlib/malloc.c	Sat Apr 21 21:30:13 2012 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,6270 +0,0 @@
-/*-
- * Copyright (C) 2006-2010 Jason Evans <jasone at FreeBSD.org>.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice(s), this list of conditions and the following disclaimer as
- *    the first lines of this file unmodified other than the possible
- *    addition of one or more copyright notices.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice(s), this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- *******************************************************************************
- *
- * This allocator implementation is designed to provide scalable performance
- * for multi-threaded programs on multi-processor systems.  The following
- * features are included for this purpose:
- *
- *   + Multiple arenas are used if there are multiple CPUs, which reduces lock
- *     contention and cache sloshing.
- *
- *   + Thread-specific caching is used if there are multiple threads, which
- *     reduces the amount of locking.
- *
- *   + Cache line sharing between arenas is avoided for internal data
- *     structures.
- *
- *   + Memory is managed in chunks and runs (chunks can be split into runs),
- *     rather than as individual pages.  This provides a constant-time
- *     mechanism for associating allocations with particular arenas.
- *
- * Allocation requests are rounded up to the nearest size class, and no record
- * of the original request size is maintained.  Allocations are broken into
- * categories according to size class.  Assuming runtime defaults, 4 KiB pages
- * and a 16 byte quantum on a 32-bit system, the size classes in each category
- * are as follows:
- *
- *   |========================================|
- *   | Category | Subcategory      |     Size |
- *   |========================================|
- *   | Small    | Tiny             |        2 |
- *   |          |                  |        4 |
- *   |          |                  |        8 |
- *   |          |------------------+----------|
- *   |          | Quantum-spaced   |       16 |
- *   |          |                  |       32 |
- *   |          |                  |       48 |
- *   |          |                  |      ... |
- *   |          |                  |       96 |
- *   |          |                  |      112 |
- *   |          |                  |      128 |
- *   |          |------------------+----------|
- *   |          | Cacheline-spaced |      192 |
- *   |          |                  |      256 |
- *   |          |                  |      320 |
- *   |          |                  |      384 |
- *   |          |                  |      448 |
- *   |          |                  |      512 |
- *   |          |------------------+----------|
- *   |          | Sub-page         |      760 |
- *   |          |                  |     1024 |
- *   |          |                  |     1280 |
- *   |          |                  |      ... |
- *   |          |                  |     3328 |
- *   |          |                  |     3584 |
- *   |          |                  |     3840 |
- *   |========================================|
- *   | Medium                      |    4 KiB |
- *   |                             |    6 KiB |
- *   |                             |    8 KiB |
- *   |                             |      ... |
- *   |                             |   28 KiB |
- *   |                             |   30 KiB |
- *   |                             |   32 KiB |
- *   |========================================|
- *   | Large                       |   36 KiB |
- *   |                             |   40 KiB |
- *   |                             |   44 KiB |
- *   |                             |      ... |
- *   |                             | 1012 KiB |
- *   |                             | 1016 KiB |
- *   |                             | 1020 KiB |
- *   |========================================|
- *   | Huge                        |    1 MiB |
- *   |                             |    2 MiB |
- *   |                             |    3 MiB |
- *   |                             |      ... |
- *   |========================================|
- *
- * Different mechanisms are used accoding to category:
- *
- *   Small/medium : Each size class is segregated into its own set of runs.
- *                  Each run maintains a bitmap of which regions are
- *                  free/allocated.
- *
- *   Large : Each allocation is backed by a dedicated run.  Metadata are stored
- *           in the associated arena chunk header maps.
- *
- *   Huge : Each allocation is backed by a dedicated contiguous set of chunks.
- *          Metadata are stored in a separate red-black tree.
- *
- *******************************************************************************
- */
-
-/*
- * MALLOC_PRODUCTION disables assertions and statistics gathering.  It also
- * defaults the A and J runtime options to off.  These settings are appropriate
- * for production systems.
- */
-/* #define	MALLOC_PRODUCTION */
-
-#ifndef MALLOC_PRODUCTION
-   /*
-    * MALLOC_DEBUG enables assertions and other sanity checks, and disables
-    * inline functions.
-    */
-#  define MALLOC_DEBUG
-
-   /* MALLOC_STATS enables statistics calculation. */
-#  define MALLOC_STATS
-#endif
-
-/*
- * MALLOC_TINY enables support for tiny objects, which are smaller than one
- * quantum.
- */
-#define	MALLOC_TINY
-
-/*
- * MALLOC_TCACHE enables a thread-specific caching layer for small and medium
- * objects.  This makes it possible to allocate/deallocate objects without any
- * locking when the cache is in the steady state.
- */
-#define	MALLOC_TCACHE
-
-/*
- * MALLOC_DSS enables use of sbrk(2) to allocate chunks from the data storage
- * segment (DSS).  In an ideal world, this functionality would be completely
- * unnecessary, but we are burdened by history and the lack of resource limits
- * for anonymous mapped memory.
- */
-#define	MALLOC_DSS
-
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/libc/stdlib/malloc.c 229848 2012-01-09 06:36:28Z ed $");
-
-#include "libc_private.h"
-#ifdef MALLOC_DEBUG
-#  define _LOCK_DEBUG
-#endif
-#include "spinlock.h"
-#include "namespace.h"
-#include <sys/mman.h>
-#include <sys/param.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/sysctl.h>
-#include <sys/uio.h>
-#include <sys/ktrace.h> /* Must come after several other sys/ includes. */
-
-#include <machine/cpufunc.h>
-#include <machine/param.h>
-#include <machine/vmparam.h>
-
-#include <errno.h>
-#include <limits.h>
-#include <link.h>
-#include <pthread.h>
-#include <sched.h>
-#include <stdarg.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdint.h>
-#include <inttypes.h>
-#include <stdlib.h>
-#include <string.h>
-#include <strings.h>
-#include <unistd.h>
-
-#include "un-namespace.h"
-
-#include "libc_private.h"
-
-#define	RB_COMPACT
-#include "rb.h"
-#if (defined(MALLOC_TCACHE) && defined(MALLOC_STATS))
-#include "qr.h"
-#include "ql.h"
-#endif
-
-#ifdef MALLOC_DEBUG
-   /* Disable inlining to make debugging easier. */
-#  define inline
-#endif
-
-/* Size of stack-allocated buffer passed to strerror_r(). */
-#define	STRERROR_BUF		64
-
-/*
- * Minimum alignment of allocations is 2^LG_QUANTUM bytes.
- */
-#ifdef __i386__
-#  define LG_QUANTUM		4
-#  define LG_SIZEOF_PTR		2
-#  define CPU_SPINWAIT		__asm__ volatile("pause")
-#  ifdef __clang__
-#    define TLS_MODEL		/* clang does not support tls_model yet */
-#  else
-#    define TLS_MODEL		__attribute__((tls_model("initial-exec")))
-#  endif
-#endif
-#ifdef __ia64__
-#  define LG_QUANTUM		4
-#  define LG_SIZEOF_PTR		3
-#  define TLS_MODEL		/* default */
-#endif
-#ifdef __alpha__
-#  define LG_QUANTUM		4
-#  define LG_SIZEOF_PTR		3
-#  define NO_TLS
-#endif
-#ifdef __sparc64__
-#  define LG_QUANTUM		4
-#  define LG_SIZEOF_PTR		3
-#  define TLS_MODEL		__attribute__((tls_model("initial-exec")))
-#endif
-#ifdef __amd64__
-#  define LG_QUANTUM		4
-#  define LG_SIZEOF_PTR		3
-#  define CPU_SPINWAIT		__asm__ volatile("pause")
-#  ifdef __clang__
-#    define TLS_MODEL		/* clang does not support tls_model yet */
-#  else
-#    define TLS_MODEL		__attribute__((tls_model("initial-exec")))
-#  endif
-#endif
-#ifdef __arm__
-#  define LG_QUANTUM		3
-#  define LG_SIZEOF_PTR		2
-#  define NO_TLS
-#endif
-#ifdef __mips__
-#  define LG_QUANTUM		3
-#  define LG_SIZEOF_PTR		2
-#  define NO_TLS
-#endif
-#ifdef __powerpc64__
-#  define LG_QUANTUM		4
-#  define LG_SIZEOF_PTR		3
-#  define TLS_MODEL		/* default */
-#elif defined(__powerpc__)
-#  define LG_QUANTUM		4
-#  define LG_SIZEOF_PTR		2
-#  define TLS_MODEL		/* default */
-#endif
-#ifdef __s390x__
-#  define LG_QUANTUM		4
-#endif
-
-#define	QUANTUM			((size_t)(1U << LG_QUANTUM))
-#define	QUANTUM_MASK		(QUANTUM - 1)
-
-#define	SIZEOF_PTR		(1U << LG_SIZEOF_PTR)
-
-/* sizeof(int) == (1U << LG_SIZEOF_INT). */
-#ifndef LG_SIZEOF_INT
-#  define LG_SIZEOF_INT	2
-#endif
-
-/* We can't use TLS in non-PIC programs, since TLS relies on loader magic. */
-#if (!defined(PIC) && !defined(NO_TLS))
-#  define NO_TLS
-#endif
-
-#ifdef NO_TLS
-   /* MALLOC_TCACHE requires TLS. */
-#  ifdef MALLOC_TCACHE
-#    undef MALLOC_TCACHE
-#  endif
-#endif
-
-/*
- * Size and alignment of memory chunks that are allocated by the OS's virtual
- * memory system.
- */
-#define	LG_CHUNK_DEFAULT	22
-
-/*
- * The minimum ratio of active:dirty pages per arena is computed as:
- *
- *   (nactive >> opt_lg_dirty_mult) >= ndirty
- *
- * So, supposing that opt_lg_dirty_mult is 5, there can be no less than 32
- * times as many active pages as dirty pages.
- */
-#define	LG_DIRTY_MULT_DEFAULT	5
-
-/*
- * Maximum size of L1 cache line.  This is used to avoid cache line aliasing.
- * In addition, this controls the spacing of cacheline-spaced size classes.
- */
-#define	LG_CACHELINE		6
-#define	CACHELINE		((size_t)(1U << LG_CACHELINE))
-#define	CACHELINE_MASK		(CACHELINE - 1)
-
-/*
- * Subpages are an artificially designated partitioning of pages.  Their only
- * purpose is to support subpage-spaced size classes.
- *
- * There must be at least 4 subpages per page, due to the way size classes are
- * handled.
- */
-#define	LG_SUBPAGE		8
-#define	SUBPAGE			((size_t)(1U << LG_SUBPAGE))
-#define	SUBPAGE_MASK		(SUBPAGE - 1)
-
-#ifdef MALLOC_TINY
-   /* Smallest size class to support. */
-#  define LG_TINY_MIN		1
-#endif
-
-/*
- * Maximum size class that is a multiple of the quantum, but not (necessarily)
- * a power of 2.  Above this size, allocations are rounded up to the nearest
- * power of 2.
- */
-#define	LG_QSPACE_MAX_DEFAULT	7
-
-/*
- * Maximum size class that is a multiple of the cacheline, but not (necessarily)
- * a power of 2.  Above this size, allocations are rounded up to the nearest
- * power of 2.
- */
-#define	LG_CSPACE_MAX_DEFAULT	9
-
-/*
- * Maximum medium size class.  This must not be more than 1/4 of a chunk
- * (LG_MEDIUM_MAX_DEFAULT <= LG_CHUNK_DEFAULT - 2).
- */
-#define	LG_MEDIUM_MAX_DEFAULT	15
-
-/*
- * RUN_MAX_OVRHD indicates maximum desired run header overhead.  Runs are sized
- * as small as possible such that this setting is still honored, without
- * violating other constraints.  The goal is to make runs as small as possible
- * without exceeding a per run external fragmentation threshold.
- *
- * We use binary fixed point math for overhead computations, where the binary
- * point is implicitly RUN_BFP bits to the left.
- *
- * Note that it is possible to set RUN_MAX_OVRHD low enough that it cannot be
- * honored for some/all object sizes, since there is one bit of header overhead
- * per object (plus a constant).  This constraint is relaxed (ignored) for runs
- * that are so small that the per-region overhead is greater than:
- *
- *   (RUN_MAX_OVRHD / (reg_size << (3+RUN_BFP))
- */
-#define	RUN_BFP			12
-/*                                    \/   Implicit binary fixed point. */
-#define	RUN_MAX_OVRHD		0x0000003dU
-#define	RUN_MAX_OVRHD_RELAX	0x00001800U
-
-/* Put a cap on small object run size.  This overrides RUN_MAX_OVRHD. */
-#define	RUN_MAX_SMALL							\
-	(arena_maxclass <= (1U << (CHUNK_MAP_LG_PG_RANGE + PAGE_SHIFT))	\
-	    ? arena_maxclass : (1U << (CHUNK_MAP_LG_PG_RANGE +		\
-	    PAGE_SHIFT)))
-
-/*
- * Hyper-threaded CPUs may need a special instruction inside spin loops in
- * order to yield to another virtual CPU.  If no such instruction is defined
- * above, make CPU_SPINWAIT a no-op.
- */
-#ifndef CPU_SPINWAIT
-#  define CPU_SPINWAIT
-#endif
-
-/*
- * Adaptive spinning must eventually switch to blocking, in order to avoid the
- * potential for priority inversion deadlock.  Backing off past a certain point
- * can actually waste time.
- */
-#define	LG_SPIN_LIMIT		11
-
-#ifdef MALLOC_TCACHE
-   /*
-    * Default number of cache slots for each bin in the thread cache (0:
-    * disabled).
-    */
-#  define LG_TCACHE_NSLOTS_DEFAULT	7
-   /*
-    * (1U << opt_lg_tcache_gc_sweep) is the approximate number of
-    * allocation events between full GC sweeps (-1: disabled).  Integer
-    * rounding may cause the actual number to be slightly higher, since GC is
-    * performed incrementally.
-    */
-#  define LG_TCACHE_GC_SWEEP_DEFAULT	13
-#endif
-
-/******************************************************************************/
-
-/*
- * Mutexes based on spinlocks.  We can't use normal pthread spinlocks in all
- * places, because they require malloc()ed memory, which causes bootstrapping
- * issues in some cases.
- */
-typedef struct {
-	spinlock_t	lock;
-} malloc_mutex_t;
-
-/* Set to true once the allocator has been initialized. */
-static bool malloc_initialized = false;
-
-/* Used to avoid initialization races. */
-static malloc_mutex_t init_lock = {_SPINLOCK_INITIALIZER};
-
-/******************************************************************************/
-/*
- * Statistics data structures.
- */
-
-#ifdef MALLOC_STATS
-
-#ifdef MALLOC_TCACHE
-typedef struct tcache_bin_stats_s tcache_bin_stats_t;
-struct tcache_bin_stats_s {
-	/*
-	 * Number of allocation requests that corresponded to the size of this
-	 * bin.
-	 */
-	uint64_t	nrequests;
-};
-#endif
-
-typedef struct malloc_bin_stats_s malloc_bin_stats_t;
-struct malloc_bin_stats_s {
-	/*
-	 * Number of allocation requests that corresponded to the size of this
-	 * bin.
-	 */
-	uint64_t	nrequests;
-
-#ifdef MALLOC_TCACHE
-	/* Number of tcache fills from this bin. */
-	uint64_t	nfills;
-
-	/* Number of tcache flushes to this bin. */
-	uint64_t	nflushes;
-#endif
-
-	/* Total number of runs created for this bin's size class. */
-	uint64_t	nruns;
-
-	/*
-	 * Total number of runs reused by extracting them from the runs tree for
-	 * this bin's size class.
-	 */
-	uint64_t	reruns;
-
-	/* High-water mark for this bin. */
-	size_t		highruns;
-
-	/* Current number of runs in this bin. */
-	size_t		curruns;
-};
-
-typedef struct malloc_large_stats_s malloc_large_stats_t;
-struct malloc_large_stats_s {
-	/*
-	 * Number of allocation requests that corresponded to this size class.
-	 */
-	uint64_t	nrequests;
-
-	/* High-water mark for this size class. */
-	size_t		highruns;
-
-	/* Current number of runs of this size class. */
-	size_t		curruns;
-};
-
-typedef struct arena_stats_s arena_stats_t;
-struct arena_stats_s {
-	/* Number of bytes currently mapped. */
-	size_t		mapped;
-
-	/*
-	 * Total number of purge sweeps, total number of madvise calls made,
-	 * and total pages purged in order to keep dirty unused memory under
-	 * control.
-	 */
-	uint64_t	npurge;
-	uint64_t	nmadvise;
-	uint64_t	purged;
-
-	/* Per-size-category statistics. */
-	size_t		allocated_small;
-	uint64_t	nmalloc_small;
-	uint64_t	ndalloc_small;
-
-	size_t		allocated_medium;
-	uint64_t	nmalloc_medium;
-	uint64_t	ndalloc_medium;
-
-	size_t		allocated_large;
-	uint64_t	nmalloc_large;
-	uint64_t	ndalloc_large;
-
-	/*
-	 * One element for each possible size class, including sizes that
-	 * overlap with bin size classes.  This is necessary because ipalloc()
-	 * sometimes has to use such large objects in order to assure proper
-	 * alignment.
-	 */
-	malloc_large_stats_t	*lstats;
-};
-
-typedef struct chunk_stats_s chunk_stats_t;
-struct chunk_stats_s {
-	/* Number of chunks that were allocated. */
-	uint64_t	nchunks;
-
-	/* High-water mark for number of chunks allocated. */
-	size_t		highchunks;
-
-	/*
-	 * Current number of chunks allocated.  This value isn't maintained for
-	 * any other purpose, so keep track of it in order to be able to set
-	 * highchunks.
-	 */
-	size_t		curchunks;
-};
-
-#endif /* #ifdef MALLOC_STATS */
-
-/******************************************************************************/
-/*
- * Extent data structures.
- */
-
-/* Tree of extents. */
-typedef struct extent_node_s extent_node_t;
-struct extent_node_s {
-#ifdef MALLOC_DSS
-	/* Linkage for the size/address-ordered tree. */
-	rb_node(extent_node_t) link_szad;
-#endif
-
-	/* Linkage for the address-ordered tree. */
-	rb_node(extent_node_t) link_ad;
-
-	/* Pointer to the extent that this tree node is responsible for. */
-	void	*addr;
-
-	/* Total region size. */
-	size_t	size;
-};
-typedef rb_tree(extent_node_t) extent_tree_t;
-
-/******************************************************************************/
-/*
- * Arena data structures.
- */
-
-typedef struct arena_s arena_t;
-typedef struct arena_bin_s arena_bin_t;
-
-/* Each element of the chunk map corresponds to one page within the chunk. */
-typedef struct arena_chunk_map_s arena_chunk_map_t;
-struct arena_chunk_map_s {
-	/*
-	 * Linkage for run trees.  There are two disjoint uses:
-	 *
-	 * 1) arena_t's runs_avail tree.
-	 * 2) arena_run_t conceptually uses this linkage for in-use non-full
-	 *    runs, rather than directly embedding linkage.
-	 */
-	rb_node(arena_chunk_map_t)	link;
-
-	/*
-	 * Run address (or size) and various flags are stored together.  The bit
-	 * layout looks like (assuming 32-bit system):
-	 *
-	 *   ???????? ???????? ????cccc ccccdzla
-	 *
-	 * ? : Unallocated: Run address for first/last pages, unset for internal
-	 *                  pages.
-	 *     Small/medium: Don't care.
-	 *     Large: Run size for first page, unset for trailing pages.
-	 * - : Unused.
-	 * c : refcount (could overflow for PAGE_SIZE >= 128 KiB)
-	 * d : dirty?
-	 * z : zeroed?
-	 * l : large?
-	 * a : allocated?
-	 *
-	 * Following are example bit patterns for the three types of runs.
-	 *
-	 * p : run page offset
-	 * s : run size
-	 * x : don't care
-	 * - : 0
-	 * [dzla] : bit set
-	 *
-	 *   Unallocated:
-	 *     ssssssss ssssssss ssss---- --------
-	 *     xxxxxxxx xxxxxxxx xxxx---- ----d---
-	 *     ssssssss ssssssss ssss---- -----z--
-	 *
-	 *   Small/medium:
-	 *     pppppppp ppppcccc cccccccc cccc---a
-	 *     pppppppp ppppcccc cccccccc cccc---a
-	 *     pppppppp ppppcccc cccccccc cccc---a
-	 *
-	 *   Large:
-	 *     ssssssss ssssssss ssss---- ------la
-	 *     -------- -------- -------- ------la
-	 *     -------- -------- -------- ------la
-	 */
-	size_t				bits;
-#define	CHUNK_MAP_PG_MASK	((size_t)0xfff00000U)
-#define	CHUNK_MAP_PG_SHIFT	20
-#define	CHUNK_MAP_LG_PG_RANGE	12
-
-#define	CHUNK_MAP_RC_MASK	((size_t)0xffff0U)
-#define	CHUNK_MAP_RC_ONE	((size_t)0x00010U)
-
-#define	CHUNK_MAP_FLAGS_MASK	((size_t)0xfU)
-#define	CHUNK_MAP_DIRTY		((size_t)0x8U)
-#define	CHUNK_MAP_ZEROED	((size_t)0x4U)
-#define	CHUNK_MAP_LARGE		((size_t)0x2U)
-#define	CHUNK_MAP_ALLOCATED	((size_t)0x1U)
-#define	CHUNK_MAP_KEY		(CHUNK_MAP_DIRTY | CHUNK_MAP_ALLOCATED)
-};
-typedef rb_tree(arena_chunk_map_t) arena_avail_tree_t;
-typedef rb_tree(arena_chunk_map_t) arena_run_tree_t;
-
-/* Arena chunk header. */
-typedef struct arena_chunk_s arena_chunk_t;
-struct arena_chunk_s {
-	/* Arena that owns the chunk. */
-	arena_t		*arena;
-
-	/* Linkage for the arena's chunks_dirty tree. */
-	rb_node(arena_chunk_t) link_dirty;
-
-	/*
-	 * True if the chunk is currently in the chunks_dirty tree, due to
-	 * having at some point contained one or more dirty pages.  Removal
-	 * from chunks_dirty is lazy, so (dirtied && ndirty == 0) is possible.
-	 */
-	bool		dirtied;
-
-	/* Number of dirty pages. */
-	size_t		ndirty;
-
-	/* Map of pages within chunk that keeps track of free/large/small. */
-	arena_chunk_map_t map[1]; /* Dynamically sized. */
-};
-typedef rb_tree(arena_chunk_t) arena_chunk_tree_t;
-
-typedef struct arena_run_s arena_run_t;
-struct arena_run_s {
-#ifdef MALLOC_DEBUG
-	uint32_t	magic;
-#  define ARENA_RUN_MAGIC 0x384adf93
-#endif
-
-	/* Bin this run is associated with. */
-	arena_bin_t	*bin;
-
-	/* Index of first element that might have a free region. */
-	unsigned	regs_minelm;
-
-	/* Number of free regions in run. */
-	unsigned	nfree;
-
-	/* Bitmask of in-use regions (0: in use, 1: free). */
-	unsigned	regs_mask[1]; /* Dynamically sized. */
-};
-
-struct arena_bin_s {
-	/*
-	 * Current run being used to service allocations of this bin's size
-	 * class.
-	 */
-	arena_run_t	*runcur;
-
-	/*
-	 * Tree of non-full runs.  This tree is used when looking for an
-	 * existing run when runcur is no longer usable.  We choose the
-	 * non-full run that is lowest in memory; this policy tends to keep
-	 * objects packed well, and it can also help reduce the number of
-	 * almost-empty chunks.
-	 */
-	arena_run_tree_t runs;
-
-	/* Size of regions in a run for this bin's size class. */
-	size_t		reg_size;
-
-	/* Total size of a run for this bin's size class. */
-	size_t		run_size;
-
-	/* Total number of regions in a run for this bin's size class. */
-	uint32_t	nregs;
-
-	/* Number of elements in a run's regs_mask for this bin's size class. */
-	uint32_t	regs_mask_nelms;
-
-	/* Offset of first region in a run for this bin's size class. */
-	uint32_t	reg0_offset;
-
-#ifdef MALLOC_STATS
-	/* Bin statistics. */
-	malloc_bin_stats_t stats;
-#endif
-};
-
-#ifdef MALLOC_TCACHE
-typedef struct tcache_s tcache_t;
-#endif
-
-struct arena_s {
-#ifdef MALLOC_DEBUG
-	uint32_t		magic;
-#  define ARENA_MAGIC 0x947d3d24
-#endif
-
-	/* All operations on this arena require that lock be locked. */
-	pthread_mutex_t		lock;
-
-#ifdef MALLOC_STATS
-	arena_stats_t		stats;
-#  ifdef MALLOC_TCACHE
-	/*
-	 * List of tcaches for extant threads associated with this arena.
-	 * Stats from these are merged incrementally, and at exit.
-	 */
-	ql_head(tcache_t)	tcache_ql;
-#  endif
-#endif
-
-	/* Tree of dirty-page-containing chunks this arena manages. */
-	arena_chunk_tree_t	chunks_dirty;
-
-	/*
-	 * In order to avoid rapid chunk allocation/deallocation when an arena
-	 * oscillates right on the cusp of needing a new chunk, cache the most
-	 * recently freed chunk.  The spare is left in the arena's chunk trees
-	 * until it is deleted.
-	 *
-	 * There is one spare chunk per arena, rather than one spare total, in
-	 * order to avoid interactions between multiple threads that could make
-	 * a single spare inadequate.
-	 */
-	arena_chunk_t		*spare;
-
-	/* Number of pages in active runs. */
-	size_t			nactive;
-
-	/*
-	 * Current count of pages within unused runs that are potentially
-	 * dirty, and for which madvise(... MADV_FREE) has not been called.  By
-	 * tracking this, we can institute a limit on how much dirty unused
-	 * memory is mapped for each arena.
-	 */
-	size_t			ndirty;
-
-	/*
-	 * Size/address-ordered tree of this arena's available runs.  This tree
-	 * is used for first-best-fit run allocation.
-	 */
-	arena_avail_tree_t	runs_avail;
-
-	/*
-	 * bins is used to store trees of free regions of the following sizes,
-	 * assuming a 16-byte quantum, 4 KiB page size, and default
-	 * MALLOC_OPTIONS.
-	 *
-	 *   bins[i] |   size |
-	 *   --------+--------+
-	 *        0  |      2 |
-	 *        1  |      4 |
-	 *        2  |      8 |
-	 *   --------+--------+
-	 *        3  |     16 |
-	 *        4  |     32 |
-	 *        5  |     48 |
-	 *           :        :
-	 *        8  |     96 |
-	 *        9  |    112 |
-	 *       10  |    128 |
-	 *   --------+--------+
-	 *       11  |    192 |
-	 *       12  |    256 |
-	 *       13  |    320 |
-	 *       14  |    384 |
-	 *       15  |    448 |
-	 *       16  |    512 |
-	 *   --------+--------+
-	 *       17  |    768 |
-	 *       18  |   1024 |
-	 *       19  |   1280 |
-	 *           :        :
-	 *       27  |   3328 |
-	 *       28  |   3584 |
-	 *       29  |   3840 |
-	 *   --------+--------+
-	 *       30  |  4 KiB |
-	 *       31  |  6 KiB |
-	 *       33  |  8 KiB |
-	 *           :        :
-	 *       43  | 28 KiB |
-	 *       44  | 30 KiB |
-	 *       45  | 32 KiB |
-	 *   --------+--------+
-	 */
-	arena_bin_t		bins[1]; /* Dynamically sized. */
-};
-
-/******************************************************************************/
-/*
- * Thread cache data structures.
- */
-
-#ifdef MALLOC_TCACHE
-typedef struct tcache_bin_s tcache_bin_t;
-struct tcache_bin_s {
-#  ifdef MALLOC_STATS
-	tcache_bin_stats_t tstats;
-#  endif
-	unsigned	low_water;	/* Min # cached since last GC. */
-	unsigned	high_water;	/* Max # cached since last GC. */
-	unsigned	ncached;	/* # of cached objects. */
-	void		*slots[1];	/* Dynamically sized. */
-};
-
-struct tcache_s {
-#  ifdef MALLOC_STATS
-	ql_elm(tcache_t) link;		/* Used for aggregating stats. */
-#  endif
-	arena_t		*arena;		/* This thread's arena. */
-	unsigned	ev_cnt;		/* Event count since incremental GC. */
-	unsigned	next_gc_bin;	/* Next bin to GC. */
-	tcache_bin_t	*tbins[1];	/* Dynamically sized. */
-};
-#endif
-
-/******************************************************************************/
-/*
- * Data.
- */
-
-/* Number of CPUs. */
-static unsigned		ncpus;
-
-/* Various bin-related settings. */
-#ifdef MALLOC_TINY		/* Number of (2^n)-spaced tiny bins. */
-#  define		ntbins	((unsigned)(LG_QUANTUM - LG_TINY_MIN))
-#else
-#  define		ntbins	0
-#endif
-static unsigned		nqbins; /* Number of quantum-spaced bins. */
-static unsigned		ncbins; /* Number of cacheline-spaced bins. */
-static unsigned		nsbins; /* Number of subpage-spaced bins. */
-static unsigned		nmbins; /* Number of medium bins. */
-static unsigned		nbins;
-static unsigned		mbin0; /* mbin offset (nbins - nmbins). */
-#ifdef MALLOC_TINY
-#  define		tspace_max	((size_t)(QUANTUM >> 1))
-#endif
-#define			qspace_min	QUANTUM
-static size_t		qspace_max;
-static size_t		cspace_min;
-static size_t		cspace_max;
-static size_t		sspace_min;
-static size_t		sspace_max;
-#define			small_maxclass	sspace_max
-#define			medium_min	PAGE_SIZE
-static size_t		medium_max;
-#define			bin_maxclass	medium_max
-
-/*
- * Soft limit on the number of medium size classes.  Spacing between medium
- * size classes never exceeds pagesize, which can force more than NBINS_MAX
- * medium size classes.
- */
-#define	NMBINS_MAX	16
-/* Spacing between medium size classes. */
-static size_t		lg_mspace;
-static size_t		mspace_mask;
-
-static uint8_t const	*small_size2bin;
-/*
- * const_small_size2bin is a static constant lookup table that in the common
- * case can be used as-is for small_size2bin.  For dynamically linked programs,
- * this avoids a page of memory overhead per process.
- */
-#define	S2B_1(i)	i,
-#define	S2B_2(i)	S2B_1(i) S2B_1(i)
-#define	S2B_4(i)	S2B_2(i) S2B_2(i)
-#define	S2B_8(i)	S2B_4(i) S2B_4(i)
-#define	S2B_16(i)	S2B_8(i) S2B_8(i)
-#define	S2B_32(i)	S2B_16(i) S2B_16(i)
-#define	S2B_64(i)	S2B_32(i) S2B_32(i)
-#define	S2B_128(i)	S2B_64(i) S2B_64(i)
-#define	S2B_256(i)	S2B_128(i) S2B_128(i)
-static const uint8_t	const_small_size2bin[PAGE_SIZE - 255] = {
-	S2B_1(0xffU)		/*    0 */
-#if (LG_QUANTUM == 4)
-/* 64-bit system ************************/
-#  ifdef MALLOC_TINY
-	S2B_2(0)		/*    2 */
-	S2B_2(1)		/*    4 */
-	S2B_4(2)		/*    8 */
-	S2B_8(3)		/*   16 */
-#    define S2B_QMIN 3
-#  else
-	S2B_16(0)		/*   16 */
-#    define S2B_QMIN 0
-#  endif
-	S2B_16(S2B_QMIN + 1)	/*   32 */
-	S2B_16(S2B_QMIN + 2)	/*   48 */
-	S2B_16(S2B_QMIN + 3)	/*   64 */
-	S2B_16(S2B_QMIN + 4)	/*   80 */
-	S2B_16(S2B_QMIN + 5)	/*   96 */
-	S2B_16(S2B_QMIN + 6)	/*  112 */
-	S2B_16(S2B_QMIN + 7)	/*  128 */
-#  define S2B_CMIN (S2B_QMIN + 8)
-#else
-/* 32-bit system ************************/
-#  ifdef MALLOC_TINY
-	S2B_2(0)		/*    2 */
-	S2B_2(1)		/*    4 */
-	S2B_4(2)		/*    8 */
-#    define S2B_QMIN 2
-#  else
-	S2B_8(0)		/*    8 */
-#    define S2B_QMIN 0
-#  endif
-	S2B_8(S2B_QMIN + 1)	/*   16 */
-	S2B_8(S2B_QMIN + 2)	/*   24 */
-	S2B_8(S2B_QMIN + 3)	/*   32 */
-	S2B_8(S2B_QMIN + 4)	/*   40 */
-	S2B_8(S2B_QMIN + 5)	/*   48 */
-	S2B_8(S2B_QMIN + 6)	/*   56 */
-	S2B_8(S2B_QMIN + 7)	/*   64 */
-	S2B_8(S2B_QMIN + 8)	/*   72 */
-	S2B_8(S2B_QMIN + 9)	/*   80 */
-	S2B_8(S2B_QMIN + 10)	/*   88 */
-	S2B_8(S2B_QMIN + 11)	/*   96 */
-	S2B_8(S2B_QMIN + 12)	/*  104 */
-	S2B_8(S2B_QMIN + 13)	/*  112 */
-	S2B_8(S2B_QMIN + 14)	/*  120 */
-	S2B_8(S2B_QMIN + 15)	/*  128 */
-#  define S2B_CMIN (S2B_QMIN + 16)
-#endif
-/****************************************/
-	S2B_64(S2B_CMIN + 0)	/*  192 */
-	S2B_64(S2B_CMIN + 1)	/*  256 */
-	S2B_64(S2B_CMIN + 2)	/*  320 */
-	S2B_64(S2B_CMIN + 3)	/*  384 */
-	S2B_64(S2B_CMIN + 4)	/*  448 */
-	S2B_64(S2B_CMIN + 5)	/*  512 */
-#  define S2B_SMIN (S2B_CMIN + 6)
-	S2B_256(S2B_SMIN + 0)	/*  768 */
-	S2B_256(S2B_SMIN + 1)	/* 1024 */
-	S2B_256(S2B_SMIN + 2)	/* 1280 */
-	S2B_256(S2B_SMIN + 3)	/* 1536 */
-	S2B_256(S2B_SMIN + 4)	/* 1792 */
-	S2B_256(S2B_SMIN + 5)	/* 2048 */
-	S2B_256(S2B_SMIN + 6)	/* 2304 */
-	S2B_256(S2B_SMIN + 7)	/* 2560 */
-	S2B_256(S2B_SMIN + 8)	/* 2816 */
-	S2B_256(S2B_SMIN + 9)	/* 3072 */
-	S2B_256(S2B_SMIN + 10)	/* 3328 */
-	S2B_256(S2B_SMIN + 11)	/* 3584 */
-	S2B_256(S2B_SMIN + 12)	/* 3840 */
-#if (PAGE_SHIFT == 13)
-	S2B_256(S2B_SMIN + 13)	/* 4096 */
-	S2B_256(S2B_SMIN + 14)	/* 4352 */
-	S2B_256(S2B_SMIN + 15)	/* 4608 */
-	S2B_256(S2B_SMIN + 16)	/* 4864 */
-	S2B_256(S2B_SMIN + 17)	/* 5120 */
-	S2B_256(S2B_SMIN + 18)	/* 5376 */
-	S2B_256(S2B_SMIN + 19)	/* 5632 */
-	S2B_256(S2B_SMIN + 20)	/* 5888 */
-	S2B_256(S2B_SMIN + 21)	/* 6144 */
-	S2B_256(S2B_SMIN + 22)	/* 6400 */
-	S2B_256(S2B_SMIN + 23)	/* 6656 */
-	S2B_256(S2B_SMIN + 24)	/* 6912 */
-	S2B_256(S2B_SMIN + 25)	/* 7168 */
-	S2B_256(S2B_SMIN + 26)	/* 7424 */
-	S2B_256(S2B_SMIN + 27)	/* 7680 */
-	S2B_256(S2B_SMIN + 28)	/* 7936 */
-#endif
-};
-#undef S2B_1
-#undef S2B_2
-#undef S2B_4
-#undef S2B_8
-#undef S2B_16
-#undef S2B_32
-#undef S2B_64
-#undef S2B_128
-#undef S2B_256
-#undef S2B_QMIN
-#undef S2B_CMIN
-#undef S2B_SMIN
-
-/* Various chunk-related settings. */
-static size_t		chunksize;
-static size_t		chunksize_mask; /* (chunksize - 1). */
-static size_t		chunk_npages;
-static size_t		arena_chunk_header_npages;
-static size_t		arena_maxclass; /* Max size class for arenas. */
-
-/********/
-/*
- * Chunks.
- */
-
-/* Protects chunk-related data structures. */
-static malloc_mutex_t	huge_mtx;
-
-/* Tree of chunks that are stand-alone huge allocations. */
-static extent_tree_t	huge;
-
-#ifdef MALLOC_DSS
-/*
- * Protects sbrk() calls.  This avoids malloc races among threads, though it
- * does not protect against races with threads that call sbrk() directly.
- */
-static malloc_mutex_t	dss_mtx;
-/* Base address of the DSS. */
-static void		*dss_base;
-/* Current end of the DSS, or ((void *)-1) if the DSS is exhausted. */
-static void		*dss_prev;
-/* Current upper limit on DSS addresses. */
-static void		*dss_max;
-
-/*
- * Trees of chunks that were previously allocated (trees differ only in node
- * ordering).  These are used when allocating chunks, in an attempt to re-use
- * address space.  Depending on function, different tree orderings are needed,
- * which is why there are two trees with the same contents.
- */
-static extent_tree_t	dss_chunks_szad;
-static extent_tree_t	dss_chunks_ad;
-#endif
-
-#ifdef MALLOC_STATS
-/* Huge allocation statistics. */
-static uint64_t		huge_nmalloc;
-static uint64_t		huge_ndalloc;
-static size_t		huge_allocated;
-#endif
-
-/****************************/
-/*
- * base (internal allocation).
- */
-
-/*
- * Current pages that are being used for internal memory allocations.  These
- * pages are carved up in cacheline-size quanta, so that there is no chance of
- * false cache line sharing.
- */
-static void		*base_pages;
-static void		*base_next_addr;
-static void		*base_past_addr; /* Addr immediately past base_pages. */
-static extent_node_t	*base_nodes;
-static malloc_mutex_t	base_mtx;
-#ifdef MALLOC_STATS
-static size_t		base_mapped;
-#endif
-
-/********/
-/*
- * Arenas.
- */
-
-/*
- * Arenas that are used to service external requests.  Not all elements of the
- * arenas array are necessarily used; arenas are created lazily as needed.
- */
-static arena_t		**arenas;
-static unsigned		narenas;
-#ifndef NO_TLS
-static unsigned		next_arena;
-#endif
-static pthread_mutex_t	arenas_lock; /* Protects arenas initialization. */
-
-#ifndef NO_TLS
-/*
- * Map of _pthread_self() --> arenas[???], used for selecting an arena to use
- * for allocations.
- */
-static __thread arena_t		*arenas_map TLS_MODEL;
-#endif
-
-#ifdef MALLOC_TCACHE
-/* Map of thread-specific caches. */
-static __thread tcache_t	*tcache_tls TLS_MODEL;
-
-/*
- * Number of cache slots for each bin in the thread cache, or 0 if tcache is
- * disabled.
- */
-size_t				tcache_nslots;
-
-/* Number of tcache allocation/deallocation events between incremental GCs. */
-unsigned			tcache_gc_incr;
-#endif
-
-/*
- * Used by chunk_alloc_mmap() to decide whether to attempt the fast path and
- * potentially avoid some system calls.  We can get away without TLS here,
- * since the state of mmap_unaligned only affects performance, rather than
- * correct function.
- */
-#ifndef NO_TLS
-static __thread bool	mmap_unaligned TLS_MODEL;
-#else
-static		bool	mmap_unaligned;
-#endif
-
-#ifdef MALLOC_STATS
-static malloc_mutex_t	chunks_mtx;
-/* Chunk statistics. */
-static chunk_stats_t	stats_chunks;
-#endif
-
-/*******************************/
-/*
- * Runtime configuration options.
- */
-const char	*_malloc_options;
-
-#ifndef MALLOC_PRODUCTION
-static bool	opt_abort = true;
-static bool	opt_junk = true;
-#else
-static bool	opt_abort = false;
-static bool	opt_junk = false;
-#endif
-#ifdef MALLOC_TCACHE
-static size_t	opt_lg_tcache_nslots = LG_TCACHE_NSLOTS_DEFAULT;
-static ssize_t	opt_lg_tcache_gc_sweep = LG_TCACHE_GC_SWEEP_DEFAULT;
-#endif
-#ifdef MALLOC_DSS
-static bool	opt_dss = true;
-static bool	opt_mmap = true;
-#endif
-static ssize_t	opt_lg_dirty_mult = LG_DIRTY_MULT_DEFAULT;
-static bool	opt_stats_print = false;
-static size_t	opt_lg_qspace_max = LG_QSPACE_MAX_DEFAULT;
-static size_t	opt_lg_cspace_max = LG_CSPACE_MAX_DEFAULT;
-static size_t	opt_lg_medium_max = LG_MEDIUM_MAX_DEFAULT;
-static size_t	opt_lg_chunk = LG_CHUNK_DEFAULT;
-static bool	opt_utrace = false;
-static bool	opt_sysv = false;
-static bool	opt_xmalloc = false;
-static bool	opt_zero = false;
-static int	opt_narenas_lshift = 0;
-
-typedef struct {
-	void	*p;
-	size_t	s;
-	void	*r;
-} malloc_utrace_t;
-
-#define	UTRACE(a, b, c)							\
-	if (opt_utrace) {						\
-		malloc_utrace_t ut;					\
-		ut.p = (a);						\
-		ut.s = (b);						\
-		ut.r = (c);						\
-		utrace(&ut, sizeof(ut));				\
-	}
-
-/******************************************************************************/
-/*
- * Begin function prototypes for non-inline static functions.
- */
-
-static void	malloc_mutex_init(malloc_mutex_t *mutex);
-static bool	malloc_spin_init(pthread_mutex_t *lock);
-#ifdef MALLOC_TINY
-static size_t	pow2_ceil(size_t x);
-#endif
-static void	wrtmessage(const char *p1, const char *p2, const char *p3,
-		const char *p4);
-#ifdef MALLOC_STATS
-static void	malloc_printf(const char *format, ...);
-#endif
-static char	*umax2s(uintmax_t x, unsigned base, char *s);
-#ifdef MALLOC_DSS
-static bool	base_pages_alloc_dss(size_t minsize);
-#endif
-static bool	base_pages_alloc_mmap(size_t minsize);
-static bool	base_pages_alloc(size_t minsize);
-static void	*base_alloc(size_t size);
-static void	*base_calloc(size_t number, size_t size);
-static extent_node_t *base_node_alloc(void);
-static void	base_node_dealloc(extent_node_t *node);
-static void	*pages_map(void *addr, size_t size);
-static void	pages_unmap(void *addr, size_t size);
-#ifdef MALLOC_DSS
-static void	*chunk_alloc_dss(size_t size, bool *zero);
-static void	*chunk_recycle_dss(size_t size, bool *zero);
-#endif
-static void	*chunk_alloc_mmap_slow(size_t size, bool unaligned);
-static void	*chunk_alloc_mmap(size_t size);
-static void	*chunk_alloc(size_t size, bool *zero);
-#ifdef MALLOC_DSS
-static extent_node_t *chunk_dealloc_dss_record(void *chunk, size_t size);
-static bool	chunk_dealloc_dss(void *chunk, size_t size);
-#endif
-static void	chunk_dealloc_mmap(void *chunk, size_t size);
-static void	chunk_dealloc(void *chunk, size_t size);
-#ifndef NO_TLS
-static arena_t	*choose_arena_hard(void);
-#endif
-static void	arena_run_split(arena_t *arena, arena_run_t *run, size_t size,
-    bool large, bool zero);
-static arena_chunk_t *arena_chunk_alloc(arena_t *arena);
-static void	arena_chunk_dealloc(arena_t *arena, arena_chunk_t *chunk);
-static arena_run_t *arena_run_alloc(arena_t *arena, size_t size, bool large,
-    bool zero);
-static void	arena_purge(arena_t *arena);
-static void	arena_run_dalloc(arena_t *arena, arena_run_t *run, bool dirty);
-static void	arena_run_trim_head(arena_t *arena, arena_chunk_t *chunk,
-    arena_run_t *run, size_t oldsize, size_t newsize);
-static void	arena_run_trim_tail(arena_t *arena, arena_chunk_t *chunk,
-    arena_run_t *run, size_t oldsize, size_t newsize, bool dirty);
-static arena_run_t *arena_bin_nonfull_run_get(arena_t *arena, arena_bin_t *bin);
-static void	*arena_bin_malloc_hard(arena_t *arena, arena_bin_t *bin);
-static size_t	arena_bin_run_size_calc(arena_bin_t *bin, size_t min_run_size);
-#ifdef MALLOC_TCACHE
-static void	tcache_bin_fill(tcache_t *tcache, tcache_bin_t *tbin,
-    size_t binind);
-static void	*tcache_alloc_hard(tcache_t *tcache, tcache_bin_t *tbin,
-    size_t binind);
-#endif
-static void	*arena_malloc_medium(arena_t *arena, size_t size, bool zero);
-static void	*arena_malloc_large(arena_t *arena, size_t size, bool zero);
-static void	*arena_palloc(arena_t *arena, size_t alignment, size_t size,
-    size_t alloc_size);
-static bool	arena_is_large(const void *ptr);
-static size_t	arena_salloc(const void *ptr);
-static void
-arena_dalloc_bin_run(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run,
-    arena_bin_t *bin);
-#ifdef MALLOC_STATS
-static void	arena_stats_print(arena_t *arena);
-#endif
-static void	stats_print_atexit(void);
-#ifdef MALLOC_TCACHE
-static void	tcache_bin_flush(tcache_bin_t *tbin, size_t binind,
-    unsigned rem);
-#endif
-static void	arena_dalloc_large(arena_t *arena, arena_chunk_t *chunk,
-    void *ptr);
-#ifdef MALLOC_TCACHE
-static void	arena_dalloc_hard(arena_t *arena, arena_chunk_t *chunk,
-    void *ptr, arena_chunk_map_t *mapelm, tcache_t *tcache);
-#endif
-static void	arena_ralloc_large_shrink(arena_t *arena, arena_chunk_t *chunk,
-    void *ptr, size_t size, size_t oldsize);
-static bool	arena_ralloc_large_grow(arena_t *arena, arena_chunk_t *chunk,
-    void *ptr, size_t size, size_t oldsize);
-static bool	arena_ralloc_large(void *ptr, size_t size, size_t oldsize);
-static void	*arena_ralloc(void *ptr, size_t size, size_t oldsize);
-static bool	arena_new(arena_t *arena, unsigned ind);
-static arena_t	*arenas_extend(unsigned ind);
-#ifdef MALLOC_TCACHE
-static tcache_bin_t	*tcache_bin_create(arena_t *arena);
-static void	tcache_bin_destroy(tcache_t *tcache, tcache_bin_t *tbin,
-    unsigned binind);
-#  ifdef MALLOC_STATS
-static void	tcache_stats_merge(tcache_t *tcache, arena_t *arena);
-#  endif
-static tcache_t	*tcache_create(arena_t *arena);
-static void	tcache_destroy(tcache_t *tcache);
-#endif
-static void	*huge_malloc(size_t size, bool zero);
-static void	*huge_palloc(size_t alignment, size_t size);
-static void	*huge_ralloc(void *ptr, size_t size, size_t oldsize);
-static void	huge_dalloc(void *ptr);
-static void	malloc_stats_print(void);
-#ifdef MALLOC_DEBUG
-static void	small_size2bin_validate(void);
-#endif
-static bool	small_size2bin_init(void);
-static bool	small_size2bin_init_hard(void);
-static unsigned	malloc_ncpus(void);
-static bool	malloc_init_hard(void);
-
-/*
- * End function prototypes.
- */
-/******************************************************************************/
-
-static void
-wrtmessage(const char *p1, const char *p2, const char *p3, const char *p4)
-{
-
-	if (_write(STDERR_FILENO, p1, strlen(p1)) < 0
-	    || _write(STDERR_FILENO, p2, strlen(p2)) < 0
-	    || _write(STDERR_FILENO, p3, strlen(p3)) < 0
-	    || _write(STDERR_FILENO, p4, strlen(p4)) < 0)
-		return;
-}
-
-void	(*_malloc_message)(const char *p1, const char *p2, const char *p3,
-	    const char *p4) = wrtmessage;
-
-/*
- * We don't want to depend on vsnprintf() for production builds, since that can
- * cause unnecessary bloat for static binaries.  umax2s() provides minimal
- * integer printing functionality, so that malloc_printf() use can be limited to
- * MALLOC_STATS code.
- */
-#define	UMAX2S_BUFSIZE	65
-static char *
-umax2s(uintmax_t x, unsigned base, char *s)
-{
-	unsigned i;
-
-	i = UMAX2S_BUFSIZE - 1;
-	s[i] = '\0';
-	switch (base) {
-	case 10:
-		do {
-			i--;
-			s[i] = "0123456789"[x % 10];
-			x /= 10;
-		} while (x > 0);
-		break;
-	case 16:
-		do {
-			i--;
-			s[i] = "0123456789abcdef"[x & 0xf];
-			x >>= 4;
-		} while (x > 0);
-		break;
-	default:
-		do {
-			i--;
-			s[i] = "0123456789abcdefghijklmnopqrstuvwxyz"[x % base];
-			x /= base;
-		} while (x > 0);
-	}
-
-	return (&s[i]);
-}
-
-/*
- * Define a custom assert() in order to reduce the chances of deadlock during
- * assertion failure.
- */
-#ifdef MALLOC_DEBUG
-#  define assert(e) do {						\
-	if (!(e)) {							\
-		char line_buf[UMAX2S_BUFSIZE];				\
-		_malloc_message(_getprogname(), ": (malloc) ",		\
-		    __FILE__, ":");					\
-		_malloc_message(umax2s(__LINE__, 10, line_buf),		\
-		    ": Failed assertion: ", "\"", #e);			\
-		_malloc_message("\"\n", "", "", "");			\
-		abort();						\
-	}								\
-} while (0)
-#else
-#define assert(e)
-#endif
-
-#ifdef MALLOC_STATS
-/*
- * Print to stderr in such a way as to (hopefully) avoid memory allocation.
- */
-static void
-malloc_printf(const char *format, ...)
-{
-	char buf[4096];
-	va_list ap;
-
-	va_start(ap, format);
-	vsnprintf(buf, sizeof(buf), format, ap);
-	va_end(ap);
-	_malloc_message(buf, "", "", "");
-}
-#endif
-
-/******************************************************************************/
-/*
- * Begin mutex.  We can't use normal pthread mutexes in all places, because
- * they require malloc()ed memory, which causes bootstrapping issues in some
- * cases.
- */
-
-static void
-malloc_mutex_init(malloc_mutex_t *mutex)
-{
-	static const spinlock_t lock = _SPINLOCK_INITIALIZER;
-
-	mutex->lock = lock;
-}
-
-static inline void
-malloc_mutex_lock(malloc_mutex_t *mutex)
-{
-
-	if (__isthreaded)
-		_SPINLOCK(&mutex->lock);
-}
-
-static inline void
-malloc_mutex_unlock(malloc_mutex_t *mutex)
-{
-
-	if (__isthreaded)
-		_SPINUNLOCK(&mutex->lock);
-}
-
-/*
- * End mutex.
- */
-/******************************************************************************/
-/*
- * Begin spin lock.  Spin locks here are actually adaptive mutexes that block
- * after a period of spinning, because unbounded spinning would allow for
- * priority inversion.
- */
-
-/*
- * We use an unpublished interface to initialize pthread mutexes with an
- * allocation callback, in order to avoid infinite recursion.
- */
-int	_pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
-    void *(calloc_cb)(size_t, size_t));
-
-__weak_reference(_pthread_mutex_init_calloc_cb_stub,
-    _pthread_mutex_init_calloc_cb);
-
-int
-_pthread_mutex_init_calloc_cb_stub(pthread_mutex_t *mutex,
-    void *(calloc_cb)(size_t, size_t))
-{
-
-	return (0);
-}
-
-static bool
-malloc_spin_init(pthread_mutex_t *lock)
-{
-
-	if (_pthread_mutex_init_calloc_cb(lock, base_calloc) != 0)
-		return (true);
-
-	return (false);
-}
-
-static inline void
-malloc_spin_lock(pthread_mutex_t *lock)
-{
-
-	if (__isthreaded) {
-		if (_pthread_mutex_trylock(lock) != 0) {
-			/* Exponentially back off if there are multiple CPUs. */
-			if (ncpus > 1) {
-				unsigned i;
-				volatile unsigned j;
-
-				for (i = 1; i <= LG_SPIN_LIMIT; i++) {
-					for (j = 0; j < (1U << i); j++) {
-						CPU_SPINWAIT;
-					}
-
-					if (_pthread_mutex_trylock(lock) == 0)
-						return;
-				}
-			}
-
-			/*
-			 * Spinning failed.  Block until the lock becomes
-			 * available, in order to avoid indefinite priority
-			 * inversion.
-			 */
-			_pthread_mutex_lock(lock);
-		}
-	}
-}
-
-static inline void
-malloc_spin_unlock(pthread_mutex_t *lock)
-{
-
-	if (__isthreaded)
-		_pthread_mutex_unlock(lock);
-}
-
-/*
- * End spin lock.
- */
-/******************************************************************************/
-/*
- * Begin Utility functions/macros.
- */
-
-/* Return the chunk address for allocation address a. */
-#define	CHUNK_ADDR2BASE(a)						\
-	((void *)((uintptr_t)(a) & ~chunksize_mask))
-
-/* Return the chunk offset of address a. */
-#define	CHUNK_ADDR2OFFSET(a)						\
-	((size_t)((uintptr_t)(a) & chunksize_mask))
-
-/* Return the smallest chunk multiple that is >= s. */
-#define	CHUNK_CEILING(s)						\
-	(((s) + chunksize_mask) & ~chunksize_mask)
-
-/* Return the smallest quantum multiple that is >= a. */
-#define	QUANTUM_CEILING(a)						\
-	(((a) + QUANTUM_MASK) & ~QUANTUM_MASK)
-
-/* Return the smallest cacheline multiple that is >= s. */
-#define	CACHELINE_CEILING(s)						\
-	(((s) + CACHELINE_MASK) & ~CACHELINE_MASK)
-
-/* Return the smallest subpage multiple that is >= s. */
-#define	SUBPAGE_CEILING(s)						\
-	(((s) + SUBPAGE_MASK) & ~SUBPAGE_MASK)
-
-/* Return the smallest medium size class that is >= s. */
-#define	MEDIUM_CEILING(s)						\
-	(((s) + mspace_mask) & ~mspace_mask)
-
-/* Return the smallest pagesize multiple that is >= s. */
-#define	PAGE_CEILING(s)							\
-	(((s) + PAGE_MASK) & ~PAGE_MASK)
-
-#ifdef MALLOC_TINY
-/* Compute the smallest power of 2 that is >= x. */
-static size_t
-pow2_ceil(size_t x)
-{
-
-	x--;
-	x |= x >> 1;
-	x |= x >> 2;
-	x |= x >> 4;
-	x |= x >> 8;
-	x |= x >> 16;
-#if (SIZEOF_PTR == 8)
-	x |= x >> 32;
-#endif
-	x++;
-	return (x);
-}
-#endif
-
-/******************************************************************************/
-
-#ifdef MALLOC_DSS
-static bool
-base_pages_alloc_dss(size_t minsize)
-{
-
-	/*
-	 * Do special DSS allocation here, since base allocations don't need to
-	 * be chunk-aligned.
-	 */
-	malloc_mutex_lock(&dss_mtx);
-	if (dss_prev != (void *)-1) {
-		intptr_t incr;
-		size_t csize = CHUNK_CEILING(minsize);
-
-		do {
-			/* Get the current end of the DSS. */
-			dss_max = sbrk(0);
-
-			/*
-			 * Calculate how much padding is necessary to
-			 * chunk-align the end of the DSS.  Don't worry about
-			 * dss_max not being chunk-aligned though.
-			 */
-			incr = (intptr_t)chunksize
-			    - (intptr_t)CHUNK_ADDR2OFFSET(dss_max);
-			assert(incr >= 0);
-			if ((size_t)incr < minsize)
-				incr += csize;
-
-			dss_prev = sbrk(incr);
-			if (dss_prev == dss_max) {
-				/* Success. */
-				dss_max = (void *)((intptr_t)dss_prev + incr);
-				base_pages = dss_prev;
-				base_next_addr = base_pages;
-				base_past_addr = dss_max;
-#ifdef MALLOC_STATS
-				base_mapped += incr;
-#endif
-				malloc_mutex_unlock(&dss_mtx);
-				return (false);
-			}
-		} while (dss_prev != (void *)-1);
-	}
-	malloc_mutex_unlock(&dss_mtx);
-
-	return (true);
-}
-#endif
-
-static bool
-base_pages_alloc_mmap(size_t minsize)
-{
-	size_t csize;
-
-	assert(minsize != 0);
-	csize = PAGE_CEILING(minsize);
-	base_pages = pages_map(NULL, csize);
-	if (base_pages == NULL)
-		return (true);
-	base_next_addr = base_pages;
-	base_past_addr = (void *)((uintptr_t)base_pages + csize);
-#ifdef MALLOC_STATS
-	base_mapped += csize;
-#endif
-
-	return (false);
-}
-
-static bool
-base_pages_alloc(size_t minsize)
-{
-
-#ifdef MALLOC_DSS
-	if (opt_mmap && minsize != 0)
-#endif
-	{
-		if (base_pages_alloc_mmap(minsize) == false)
-			return (false);
-	}
-
-#ifdef MALLOC_DSS
-	if (opt_dss) {
-		if (base_pages_alloc_dss(minsize) == false)
-			return (false);
-	}
-
-#endif
-
-	return (true);
-}
-
-static void *
-base_alloc(size_t size)
-{
-	void *ret;
-	size_t csize;
-
-	/* Round size up to nearest multiple of the cacheline size. */
-	csize = CACHELINE_CEILING(size);
-
-	malloc_mutex_lock(&base_mtx);
-	/* Make sure there's enough space for the allocation. */
-	if ((uintptr_t)base_next_addr + csize > (uintptr_t)base_past_addr) {
-		if (base_pages_alloc(csize)) {
-			malloc_mutex_unlock(&base_mtx);
-			return (NULL);
-		}
-	}
-	/* Allocate. */
-	ret = base_next_addr;
-	base_next_addr = (void *)((uintptr_t)base_next_addr + csize);
-	malloc_mutex_unlock(&base_mtx);
-
-	return (ret);
-}
-
-static void *
-base_calloc(size_t number, size_t size)
-{
-	void *ret;
-
-	ret = base_alloc(number * size);
-	if (ret != NULL)
-		memset(ret, 0, number * size);
-
-	return (ret);
-}
-
-static extent_node_t *
-base_node_alloc(void)
-{
-	extent_node_t *ret;
-
-	malloc_mutex_lock(&base_mtx);
-	if (base_nodes != NULL) {
-		ret = base_nodes;
-		base_nodes = *(extent_node_t **)ret;
-		malloc_mutex_unlock(&base_mtx);
-	} else {
-		malloc_mutex_unlock(&base_mtx);
-		ret = (extent_node_t *)base_alloc(sizeof(extent_node_t));
-	}
-
-	return (ret);
-}
-
-static void
-base_node_dealloc(extent_node_t *node)
-{
-
-	malloc_mutex_lock(&base_mtx);
-	*(extent_node_t **)node = base_nodes;
-	base_nodes = node;
-	malloc_mutex_unlock(&base_mtx);
-}
-
-/*
- * End Utility functions/macros.
- */
-/******************************************************************************/
-/*
- * Begin extent tree code.
- */
-
-#ifdef MALLOC_DSS
-static inline int
-extent_szad_comp(extent_node_t *a, extent_node_t *b)
-{
-	int ret;
-	size_t a_size = a->size;
-	size_t b_size = b->size;
-
-	ret = (a_size > b_size) - (a_size < b_size);
-	if (ret == 0) {
-		uintptr_t a_addr = (uintptr_t)a->addr;
-		uintptr_t b_addr = (uintptr_t)b->addr;
-
-		ret = (a_addr > b_addr) - (a_addr < b_addr);
-	}
-
-	return (ret);
-}
-
-/* Wrap red-black tree macros in functions. */
-rb_gen(__unused static, extent_tree_szad_, extent_tree_t, extent_node_t,
-    link_szad, extent_szad_comp)
-#endif
-
-static inline int
-extent_ad_comp(extent_node_t *a, extent_node_t *b)
-{
-	uintptr_t a_addr = (uintptr_t)a->addr;
-	uintptr_t b_addr = (uintptr_t)b->addr;
-
-	return ((a_addr > b_addr) - (a_addr < b_addr));
-}
-
-/* Wrap red-black tree macros in functions. */
-rb_gen(__unused static, extent_tree_ad_, extent_tree_t, extent_node_t, link_ad,
-    extent_ad_comp)
-
-/*
- * End extent tree code.
- */
-/******************************************************************************/
-/*
- * Begin chunk management functions.
- */
-
-static void *
-pages_map(void *addr, size_t size)
-{
-	void *ret;
-
-	/*
-	 * We don't use MAP_FIXED here, because it can cause the *replacement*
-	 * of existing mappings, and we only want to create new mappings.
-	 */
-	ret = mmap(addr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON,
-	    -1, 0);
-	assert(ret != NULL);
-
-	if (ret == MAP_FAILED)
-		ret = NULL;
-	else if (addr != NULL && ret != addr) {
-		/*
-		 * We succeeded in mapping memory, but not in the right place.
-		 */
-		if (munmap(ret, size) == -1) {
-			char buf[STRERROR_BUF];
-
-			strerror_r(errno, buf, sizeof(buf));
-			_malloc_message(_getprogname(),
-			    ": (malloc) Error in munmap(): ", buf, "\n");
-			if (opt_abort)
-				abort();
-		}
-		ret = NULL;
-	}
-
-	assert(ret == NULL || (addr == NULL && ret != addr)
-	    || (addr != NULL && ret == addr));
-	return (ret);
-}
-
-static void
-pages_unmap(void *addr, size_t size)
-{
-
-	if (munmap(addr, size) == -1) {
-		char buf[STRERROR_BUF];
-
-		strerror_r(errno, buf, sizeof(buf));
-		_malloc_message(_getprogname(),
-		    ": (malloc) Error in munmap(): ", buf, "\n");
-		if (opt_abort)
-			abort();
-	}
-}
-
-#ifdef MALLOC_DSS
-static void *
-chunk_alloc_dss(size_t size, bool *zero)
-{
-	void *ret;
-
-	ret = chunk_recycle_dss(size, zero);
-	if (ret != NULL)
-		return (ret);
-
-	/*
-	 * sbrk() uses a signed increment argument, so take care not to
-	 * interpret a huge allocation request as a negative increment.
-	 */
-	if ((intptr_t)size < 0)
-		return (NULL);
-
-	malloc_mutex_lock(&dss_mtx);
-	if (dss_prev != (void *)-1) {
-		intptr_t incr;
-
-		/*
-		 * The loop is necessary to recover from races with other
-		 * threads that are using the DSS for something other than
-		 * malloc.
-		 */
-		do {
-			/* Get the current end of the DSS. */
-			dss_max = sbrk(0);
-
-			/*
-			 * Calculate how much padding is necessary to
-			 * chunk-align the end of the DSS.
-			 */
-			incr = (intptr_t)size
-			    - (intptr_t)CHUNK_ADDR2OFFSET(dss_max);
-			if (incr == (intptr_t)size)
-				ret = dss_max;
-			else {
-				ret = (void *)((intptr_t)dss_max + incr);
-				incr += size;
-			}
-
-			dss_prev = sbrk(incr);
-			if (dss_prev == dss_max) {
-				/* Success. */
-				dss_max = (void *)((intptr_t)dss_prev + incr);
-				malloc_mutex_unlock(&dss_mtx);
-				*zero = true;
-				return (ret);
-			}
-		} while (dss_prev != (void *)-1);
-	}
-	malloc_mutex_unlock(&dss_mtx);
-
-	return (NULL);
-}
-
-static void *
-chunk_recycle_dss(size_t size, bool *zero)
-{
-	extent_node_t *node, key;
-
-	key.addr = NULL;
-	key.size = size;
-	malloc_mutex_lock(&dss_mtx);
-	node = extent_tree_szad_nsearch(&dss_chunks_szad, &key);
-	if (node != NULL) {
-		void *ret = node->addr;
-
-		/* Remove node from the tree. */
-		extent_tree_szad_remove(&dss_chunks_szad, node);
-		if (node->size == size) {
-			extent_tree_ad_remove(&dss_chunks_ad, node);
-			base_node_dealloc(node);
-		} else {
-			/*
-			 * Insert the remainder of node's address range as a
-			 * smaller chunk.  Its position within dss_chunks_ad
-			 * does not change.
-			 */
-			assert(node->size > size);
-			node->addr = (void *)((uintptr_t)node->addr + size);
-			node->size -= size;
-			extent_tree_szad_insert(&dss_chunks_szad, node);
-		}
-		malloc_mutex_unlock(&dss_mtx);
-
-		if (*zero)
-			memset(ret, 0, size);
-		return (ret);
-	}
-	malloc_mutex_unlock(&dss_mtx);
-
-	return (NULL);
-}
-#endif
-
-static void *
-chunk_alloc_mmap_slow(size_t size, bool unaligned)
-{
-	void *ret;
-	size_t offset;
-
-	/* Beware size_t wrap-around. */
-	if (size + chunksize <= size)
-		return (NULL);
-
-	ret = pages_map(NULL, size + chunksize);
-	if (ret == NULL)
-		return (NULL);
-
-	/* Clean up unneeded leading/trailing space. */
-	offset = CHUNK_ADDR2OFFSET(ret);
-	if (offset != 0) {
-		/* Note that mmap() returned an unaligned mapping. */
-		unaligned = true;
-
-		/* Leading space. */
-		pages_unmap(ret, chunksize - offset);
-
-		ret = (void *)((uintptr_t)ret +
-		    (chunksize - offset));
-
-		/* Trailing space. */
-		pages_unmap((void *)((uintptr_t)ret + size),
-		    offset);
-	} else {
-		/* Trailing space only. */
-		pages_unmap((void *)((uintptr_t)ret + size),
-		    chunksize);
-	}
-
-	/*
-	 * If mmap() returned an aligned mapping, reset mmap_unaligned so that
-	 * the next chunk_alloc_mmap() execution tries the fast allocation
-	 * method.
-	 */
-	if (unaligned == false)
-		mmap_unaligned = false;
-
-	return (ret);
-}
-
-static void *
-chunk_alloc_mmap(size_t size)
-{
-	void *ret;
-
-	/*
-	 * Ideally, there would be a way to specify alignment to mmap() (like
-	 * NetBSD has), but in the absence of such a feature, we have to work
-	 * hard to efficiently create aligned mappings.  The reliable, but
-	 * slow method is to create a mapping that is over-sized, then trim the
-	 * excess.  However, that always results in at least one call to
-	 * pages_unmap().
-	 *
-	 * A more optimistic approach is to try mapping precisely the right
-	 * amount, then try to append another mapping if alignment is off.  In
-	 * practice, this works out well as long as the application is not
-	 * interleaving mappings via direct mmap() calls.  If we do run into a
-	 * situation where there is an interleaved mapping and we are unable to
-	 * extend an unaligned mapping, our best option is to switch to the
-	 * slow method until mmap() returns another aligned mapping.  This will
-	 * tend to leave a gap in the memory map that is too small to cause
-	 * later problems for the optimistic method.
-	 *
-	 * Another possible confounding factor is address space layout
-	 * randomization (ASLR), which causes mmap(2) to disregard the
-	 * requested address.  mmap_unaligned tracks whether the previous
-	 * chunk_alloc_mmap() execution received any unaligned or relocated
-	 * mappings, and if so, the current execution will immediately fall
-	 * back to the slow method.  However, we keep track of whether the fast
-	 * method would have succeeded, and if so, we make a note to try the
-	 * fast method next time.
-	 */
-
-	if (mmap_unaligned == false) {
-		size_t offset;
-
-		ret = pages_map(NULL, size);
-		if (ret == NULL)
-			return (NULL);
-
-		offset = CHUNK_ADDR2OFFSET(ret);
-		if (offset != 0) {
-			mmap_unaligned = true;
-			/* Try to extend chunk boundary. */
-			if (pages_map((void *)((uintptr_t)ret + size),
-			    chunksize - offset) == NULL) {
-				/*
-				 * Extension failed.  Clean up, then revert to
-				 * the reliable-but-expensive method.
-				 */
-				pages_unmap(ret, size);
-				ret = chunk_alloc_mmap_slow(size, true);
-			} else {
-				/* Clean up unneeded leading space. */
-				pages_unmap(ret, chunksize - offset);
-				ret = (void *)((uintptr_t)ret + (chunksize -
-				    offset));
-			}
-		}
-	} else
-		ret = chunk_alloc_mmap_slow(size, false);
-
-	return (ret);
-}
-
-/*
- * If the caller specifies (*zero == false), it is still possible to receive
- * zeroed memory, in which case *zero is toggled to true.  arena_chunk_alloc()
- * takes advantage of this to avoid demanding zeroed chunks, but taking
- * advantage of them if they are returned.
- */
-static void *
-chunk_alloc(size_t size, bool *zero)
-{
-	void *ret;
-
-	assert(size != 0);
-	assert((size & chunksize_mask) == 0);
-
-#ifdef MALLOC_DSS
-	if (opt_mmap)
-#endif
-	{
-		ret = chunk_alloc_mmap(size);
-		if (ret != NULL) {
-			*zero = true;
-			goto RETURN;
-		}
-	}
-
-#ifdef MALLOC_DSS
-	if (opt_dss) {
-		ret = chunk_alloc_dss(size, zero);
-		if (ret != NULL)
-			goto RETURN;
-	}
-#endif
-
-	/* All strategies for allocation failed. */
-	ret = NULL;
-RETURN:
-#ifdef MALLOC_STATS
-	if (ret != NULL) {
-		malloc_mutex_lock(&chunks_mtx);
-		stats_chunks.nchunks += (size / chunksize);
-		stats_chunks.curchunks += (size / chunksize);
-		if (stats_chunks.curchunks > stats_chunks.highchunks)
-			stats_chunks.highchunks = stats_chunks.curchunks;
-		malloc_mutex_unlock(&chunks_mtx);
-	}
-#endif
-
-	assert(CHUNK_ADDR2BASE(ret) == ret);
-	return (ret);
-}
-
-#ifdef MALLOC_DSS
-static extent_node_t *
-chunk_dealloc_dss_record(void *chunk, size_t size)
-{
-	extent_node_t *node, *prev, key;
-
-	key.addr = (void *)((uintptr_t)chunk + size);
-	node = extent_tree_ad_nsearch(&dss_chunks_ad, &key);
-	/* Try to coalesce forward. */
-	if (node != NULL && node->addr == key.addr) {
-		/*
-		 * Coalesce chunk with the following address range.  This does
-		 * not change the position within dss_chunks_ad, so only
-		 * remove/insert from/into dss_chunks_szad.
-		 */
-		extent_tree_szad_remove(&dss_chunks_szad, node);
-		node->addr = chunk;
-		node->size += size;
-		extent_tree_szad_insert(&dss_chunks_szad, node);
-	} else {
-		/*
-		 * Coalescing forward failed, so insert a new node.  Drop
-		 * dss_mtx during node allocation, since it is possible that a
-		 * new base chunk will be allocated.
-		 */
-		malloc_mutex_unlock(&dss_mtx);
-		node = base_node_alloc();
-		malloc_mutex_lock(&dss_mtx);
-		if (node == NULL)
-			return (NULL);
-		node->addr = chunk;
-		node->size = size;
-		extent_tree_ad_insert(&dss_chunks_ad, node);
-		extent_tree_szad_insert(&dss_chunks_szad, node);
-	}
-
-	/* Try to coalesce backward. */
-	prev = extent_tree_ad_prev(&dss_chunks_ad, node);
-	if (prev != NULL && (void *)((uintptr_t)prev->addr + prev->size) ==
-	    chunk) {
-		/*
-		 * Coalesce chunk with the previous address range.  This does
-		 * not change the position within dss_chunks_ad, so only
-		 * remove/insert node from/into dss_chunks_szad.
-		 */
-		extent_tree_szad_remove(&dss_chunks_szad, prev);
-		extent_tree_ad_remove(&dss_chunks_ad, prev);
-
-		extent_tree_szad_remove(&dss_chunks_szad, node);
-		node->addr = prev->addr;
-		node->size += prev->size;
-		extent_tree_szad_insert(&dss_chunks_szad, node);
-
-		base_node_dealloc(prev);
-	}
-
-	return (node);
-}
-
-static bool
-chunk_dealloc_dss(void *chunk, size_t size)
-{
-	bool ret;
-
-	malloc_mutex_lock(&dss_mtx);
-	if ((uintptr_t)chunk >= (uintptr_t)dss_base
-	    && (uintptr_t)chunk < (uintptr_t)dss_max) {
-		extent_node_t *node;
-
-		/* Try to coalesce with other unused chunks. */
-		node = chunk_dealloc_dss_record(chunk, size);
-		if (node != NULL) {
-			chunk = node->addr;
-			size = node->size;
-		}
-
-		/* Get the current end of the DSS. */
-		dss_max = sbrk(0);
-
-		/*
-		 * Try to shrink the DSS if this chunk is at the end of the
-		 * DSS.  The sbrk() call here is subject to a race condition
-		 * with threads that use brk(2) or sbrk(2) directly, but the
-		 * alternative would be to leak memory for the sake of poorly
-		 * designed multi-threaded programs.
-		 */
-		if ((void *)((uintptr_t)chunk + size) == dss_max
-		    && (dss_prev = sbrk(-(intptr_t)size)) == dss_max) {
-			/* Success. */
-			dss_max = (void *)((intptr_t)dss_prev - (intptr_t)size);
-
-			if (node != NULL) {
-				extent_tree_szad_remove(&dss_chunks_szad, node);
-				extent_tree_ad_remove(&dss_chunks_ad, node);
-				base_node_dealloc(node);
-			}
-		} else
-			madvise(chunk, size, MADV_FREE);
-
-		ret = false;
-		goto RETURN;
-	}
-
-	ret = true;
-RETURN:
-	malloc_mutex_unlock(&dss_mtx);
-	return (ret);
-}
-#endif
-
-static void
-chunk_dealloc_mmap(void *chunk, size_t size)
-{
-
-	pages_unmap(chunk, size);
-}
-
-static void
-chunk_dealloc(void *chunk, size_t size)
-{
-
-	assert(chunk != NULL);
-	assert(CHUNK_ADDR2BASE(chunk) == chunk);
-	assert(size != 0);
-	assert((size & chunksize_mask) == 0);
-
-#ifdef MALLOC_STATS
-	malloc_mutex_lock(&chunks_mtx);
-	stats_chunks.curchunks -= (size / chunksize);
-	malloc_mutex_unlock(&chunks_mtx);
-#endif
-
-#ifdef MALLOC_DSS
-	if (opt_dss) {
-		if (chunk_dealloc_dss(chunk, size) == false)
-			return;
-	}
-
-	if (opt_mmap)
-#endif
-		chunk_dealloc_mmap(chunk, size);
-}
-
-/*
- * End chunk management functions.
- */
-/******************************************************************************/
-/*
- * Begin arena.
- */
-
-/*
- * Choose an arena based on a per-thread value (fast-path code, calls slow-path
- * code if necessary).
- */
-static inline arena_t *
-choose_arena(void)
-{
-	arena_t *ret;
-
-	/*
-	 * We can only use TLS if this is a PIC library, since for the static
-	 * library version, libc's malloc is used by TLS allocation, which
-	 * introduces a bootstrapping issue.
-	 */
-#ifndef NO_TLS
-	if (__isthreaded == false) {
-	    /* Avoid the overhead of TLS for single-threaded operation. */
-	    return (arenas[0]);
-	}
-
-	ret = arenas_map;
-	if (ret == NULL) {
-		ret = choose_arena_hard();
-		assert(ret != NULL);
-	}
-#else
-	if (__isthreaded && narenas > 1) {
-		unsigned long ind;
-
-		/*
-		 * Hash _pthread_self() to one of the arenas.  There is a prime
-		 * number of arenas, so this has a reasonable chance of
-		 * working.  Even so, the hashing can be easily thwarted by
-		 * inconvenient _pthread_self() values.  Without specific
-		 * knowledge of how _pthread_self() calculates values, we can't
-		 * easily do much better than this.
-		 */
-		ind = (unsigned long) _pthread_self() % narenas;
-
-		/*
-		 * Optimistially assume that arenas[ind] has been initialized.
-		 * At worst, we find out that some other thread has already
-		 * done so, after acquiring the lock in preparation.  Note that
-		 * this lazy locking also has the effect of lazily forcing
-		 * cache coherency; without the lock acquisition, there's no
-		 * guarantee that modification of arenas[ind] by another thread
-		 * would be seen on this CPU for an arbitrary amount of time.
-		 *
-		 * In general, this approach to modifying a synchronized value
-		 * isn't a good idea, but in this case we only ever modify the
-		 * value once, so things work out well.
-		 */
-		ret = arenas[ind];
-		if (ret == NULL) {
-			/*
-			 * Avoid races with another thread that may have already
-			 * initialized arenas[ind].
-			 */
-			malloc_spin_lock(&arenas_lock);
-			if (arenas[ind] == NULL)
-				ret = arenas_extend((unsigned)ind);
-			else
-				ret = arenas[ind];
-			malloc_spin_unlock(&arenas_lock);
-		}
-	} else
-		ret = arenas[0];
-#endif
-
-	assert(ret != NULL);
-	return (ret);
-}
-
-#ifndef NO_TLS
-/*
- * Choose an arena based on a per-thread value (slow-path code only, called
- * only by choose_arena()).
- */
-static arena_t *
-choose_arena_hard(void)
-{
-	arena_t *ret;
-
-	assert(__isthreaded);
-
-	if (narenas > 1) {
-		malloc_spin_lock(&arenas_lock);
-		if ((ret = arenas[next_arena]) == NULL)
-			ret = arenas_extend(next_arena);
-		next_arena = (next_arena + 1) % narenas;
-		malloc_spin_unlock(&arenas_lock);
-	} else
-		ret = arenas[0];
-
-	arenas_map = ret;
-
-	return (ret);
-}
-#endif
-
-static inline int
-arena_chunk_comp(arena_chunk_t *a, arena_chunk_t *b)
-{
-	uintptr_t a_chunk = (uintptr_t)a;
-	uintptr_t b_chunk = (uintptr_t)b;
-
-	assert(a != NULL);
-	assert(b != NULL);
-
-	return ((a_chunk > b_chunk) - (a_chunk < b_chunk));
-}
-
-/* Wrap red-black tree macros in functions. */
-rb_gen(__unused static, arena_chunk_tree_dirty_, arena_chunk_tree_t,
-    arena_chunk_t, link_dirty, arena_chunk_comp)
-
-static inline int
-arena_run_comp(arena_chunk_map_t *a, arena_chunk_map_t *b)
-{
-	uintptr_t a_mapelm = (uintptr_t)a;
-	uintptr_t b_mapelm = (uintptr_t)b;
-
-	assert(a != NULL);
-	assert(b != NULL);
-
-	return ((a_mapelm > b_mapelm) - (a_mapelm < b_mapelm));
-}
-
-/* Wrap red-black tree macros in functions. */
-rb_gen(__unused static, arena_run_tree_, arena_run_tree_t, arena_chunk_map_t,
-    link, arena_run_comp)
-
-static inline int
-arena_avail_comp(arena_chunk_map_t *a, arena_chunk_map_t *b)
-{
-	int ret;
-	size_t a_size = a->bits & ~PAGE_MASK;
-	size_t b_size = b->bits & ~PAGE_MASK;
-
-	ret = (a_size > b_size) - (a_size < b_size);
-	if (ret == 0) {
-		uintptr_t a_mapelm, b_mapelm;
-
-		if ((a->bits & CHUNK_MAP_KEY) != CHUNK_MAP_KEY)
-			a_mapelm = (uintptr_t)a;
-		else {
-			/*
-			 * Treat keys as though they are lower than anything
-			 * else.
-			 */
-			a_mapelm = 0;
-		}
-		b_mapelm = (uintptr_t)b;
-
-		ret = (a_mapelm > b_mapelm) - (a_mapelm < b_mapelm);
-	}
-
-	return (ret);
-}
-
-/* Wrap red-black tree macros in functions. */
-rb_gen(__unused static, arena_avail_tree_, arena_avail_tree_t,
-    arena_chunk_map_t, link, arena_avail_comp)
-
-static inline void
-arena_run_rc_incr(arena_run_t *run, arena_bin_t *bin, const void *ptr)
-{
-	arena_chunk_t *chunk;
-	arena_t *arena;
-	size_t pagebeg, pageend, i;
-
-	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
-	arena = chunk->arena;
-	pagebeg = ((uintptr_t)ptr - (uintptr_t)chunk) >> PAGE_SHIFT;
-	pageend = ((uintptr_t)ptr + (uintptr_t)(bin->reg_size - 1) -
-	    (uintptr_t)chunk) >> PAGE_SHIFT;
-
-	for (i = pagebeg; i <= pageend; i++) {
-		size_t mapbits = chunk->map[i].bits;
-
-		if (mapbits & CHUNK_MAP_DIRTY) {
-			assert((mapbits & CHUNK_MAP_RC_MASK) == 0);
-			chunk->ndirty--;
-			arena->ndirty--;
-			mapbits ^= CHUNK_MAP_DIRTY;
-		}
-		assert((mapbits & CHUNK_MAP_RC_MASK) != CHUNK_MAP_RC_MASK);
-		mapbits += CHUNK_MAP_RC_ONE;
-		chunk->map[i].bits = mapbits;
-	}
-}
-
-static inline void
-arena_run_rc_decr(arena_run_t *run, arena_bin_t *bin, const void *ptr)
-{
-	arena_chunk_t *chunk;
-	arena_t *arena;
-	size_t pagebeg, pageend, mapbits, i;
-	bool dirtier = false;
-
-	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
-	arena = chunk->arena;
-	pagebeg = ((uintptr_t)ptr - (uintptr_t)chunk) >> PAGE_SHIFT;
-	pageend = ((uintptr_t)ptr + (uintptr_t)(bin->reg_size - 1) -
-	    (uintptr_t)chunk) >> PAGE_SHIFT;
-
-	/* First page. */
-	mapbits = chunk->map[pagebeg].bits;
-	mapbits -= CHUNK_MAP_RC_ONE;
-	if ((mapbits & CHUNK_MAP_RC_MASK) == 0) {
-		dirtier = true;
-		assert((mapbits & CHUNK_MAP_DIRTY) == 0);
-		mapbits |= CHUNK_MAP_DIRTY;
-		chunk->ndirty++;
-		arena->ndirty++;
-	}
-	chunk->map[pagebeg].bits = mapbits;
-
-	if (pageend - pagebeg >= 1) {
-		/*
-		 * Interior pages are completely consumed by the object being
-		 * deallocated, which means that the pages can be
-		 * unconditionally marked dirty.
-		 */
-		for (i = pagebeg + 1; i < pageend; i++) {
-			mapbits = chunk->map[i].bits;
-			mapbits -= CHUNK_MAP_RC_ONE;
-			assert((mapbits & CHUNK_MAP_RC_MASK) == 0);
-			dirtier = true;
-			assert((mapbits & CHUNK_MAP_DIRTY) == 0);
-			mapbits |= CHUNK_MAP_DIRTY;
-			chunk->ndirty++;
-			arena->ndirty++;
-			chunk->map[i].bits = mapbits;
-		}
-
-		/* Last page. */
-		mapbits = chunk->map[pageend].bits;
-		mapbits -= CHUNK_MAP_RC_ONE;
-		if ((mapbits & CHUNK_MAP_RC_MASK) == 0) {
-			dirtier = true;
-			assert((mapbits & CHUNK_MAP_DIRTY) == 0);
-			mapbits |= CHUNK_MAP_DIRTY;
-			chunk->ndirty++;
-			arena->ndirty++;
-		}
-		chunk->map[pageend].bits = mapbits;
-	}
-
-	if (dirtier) {
-		if (chunk->dirtied == false) {
-			arena_chunk_tree_dirty_insert(&arena->chunks_dirty,
-			    chunk);
-			chunk->dirtied = true;
-		}
-
-		/* Enforce opt_lg_dirty_mult. */
-		if (opt_lg_dirty_mult >= 0 && (arena->nactive >>
-		    opt_lg_dirty_mult) < arena->ndirty)
-			arena_purge(arena);
-	}
-}
-
-static inline void *
-arena_run_reg_alloc(arena_run_t *run, arena_bin_t *bin)
-{
-	void *ret;
-	unsigned i, mask, bit, regind;
-
-	assert(run->magic == ARENA_RUN_MAGIC);
-	assert(run->regs_minelm < bin->regs_mask_nelms);
-
-	/*
-	 * Move the first check outside the loop, so that run->regs_minelm can
-	 * be updated unconditionally, without the possibility of updating it
-	 * multiple times.
-	 */
-	i = run->regs_minelm;
-	mask = run->regs_mask[i];
-	if (mask != 0) {
-		/* Usable allocation found. */
-		bit = ffs((int)mask) - 1;
-
-		regind = ((i << (LG_SIZEOF_INT + 3)) + bit);
-		assert(regind < bin->nregs);
-		ret = (void *)(((uintptr_t)run) + bin->reg0_offset
-		    + (bin->reg_size * regind));
-
-		/* Clear bit. */
-		mask ^= (1U << bit);
-		run->regs_mask[i] = mask;
-
-		arena_run_rc_incr(run, bin, ret);
-
-		return (ret);
-	}
-
-	for (i++; i < bin->regs_mask_nelms; i++) {
-		mask = run->regs_mask[i];
-		if (mask != 0) {
-			/* Usable allocation found. */
-			bit = ffs((int)mask) - 1;
-
-			regind = ((i << (LG_SIZEOF_INT + 3)) + bit);
-			assert(regind < bin->nregs);
-			ret = (void *)(((uintptr_t)run) + bin->reg0_offset
-			    + (bin->reg_size * regind));
-
-			/* Clear bit. */
-			mask ^= (1U << bit);
-			run->regs_mask[i] = mask;
-
-			/*
-			 * Make a note that nothing before this element
-			 * contains a free region.
-			 */
-			run->regs_minelm = i; /* Low payoff: + (mask == 0); */
-
-			arena_run_rc_incr(run, bin, ret);
-
-			return (ret);
-		}
-	}
-	/* Not reached. */
-	assert(0);
-	return (NULL);
-}
-
-static inline void
-arena_run_reg_dalloc(arena_run_t *run, arena_bin_t *bin, void *ptr, size_t size)
-{
-	unsigned shift, diff, regind, elm, bit;
-
-	assert(run->magic == ARENA_RUN_MAGIC);
-
-	/*
-	 * Avoid doing division with a variable divisor if possible.  Using
-	 * actual division here can reduce allocator throughput by over 20%!
-	 */
-	diff = (unsigned)((uintptr_t)ptr - (uintptr_t)run - bin->reg0_offset);
-
-	/* Rescale (factor powers of 2 out of the numerator and denominator). */
-	shift = ffs(size) - 1;
-	diff >>= shift;
-	size >>= shift;
-
-	if (size == 1) {
-		/* The divisor was a power of 2. */
-		regind = diff;
-	} else {
-		/*
-		 * To divide by a number D that is not a power of two we
-		 * multiply by (2^21 / D) and then right shift by 21 positions.
-		 *
-		 *   X / D
-		 *
-		 * becomes
-		 *
-		 *   (X * size_invs[D - 3]) >> SIZE_INV_SHIFT
-		 *
-		 * We can omit the first three elements, because we never
-		 * divide by 0, and 1 and 2 are both powers of two, which are
-		 * handled above.
-		 */
-#define	SIZE_INV_SHIFT 21
-#define	SIZE_INV(s) (((1U << SIZE_INV_SHIFT) / (s)) + 1)
-		static const unsigned size_invs[] = {
-		    SIZE_INV(3),
-		    SIZE_INV(4), SIZE_INV(5), SIZE_INV(6), SIZE_INV(7),
-		    SIZE_INV(8), SIZE_INV(9), SIZE_INV(10), SIZE_INV(11),
-		    SIZE_INV(12), SIZE_INV(13), SIZE_INV(14), SIZE_INV(15),
-		    SIZE_INV(16), SIZE_INV(17), SIZE_INV(18), SIZE_INV(19),
-		    SIZE_INV(20), SIZE_INV(21), SIZE_INV(22), SIZE_INV(23),
-		    SIZE_INV(24), SIZE_INV(25), SIZE_INV(26), SIZE_INV(27),
-		    SIZE_INV(28), SIZE_INV(29), SIZE_INV(30), SIZE_INV(31)
-		};
-
-		if (size <= ((sizeof(size_invs) / sizeof(unsigned)) + 2))
-			regind = (diff * size_invs[size - 3]) >> SIZE_INV_SHIFT;
-		else
-			regind = diff / size;
-#undef SIZE_INV
-#undef SIZE_INV_SHIFT
-	}
-	assert(diff == regind * size);
-	assert(regind < bin->nregs);
-
-	elm = regind >> (LG_SIZEOF_INT + 3);
-	if (elm < run->regs_minelm)
-		run->regs_minelm = elm;
-	bit = regind - (elm << (LG_SIZEOF_INT + 3));
-	assert((run->regs_mask[elm] & (1U << bit)) == 0);
-	run->regs_mask[elm] |= (1U << bit);
-
-	arena_run_rc_decr(run, bin, ptr);
-}
-
-static void
-arena_run_split(arena_t *arena, arena_run_t *run, size_t size, bool large,
-    bool zero)
-{
-	arena_chunk_t *chunk;
-	size_t old_ndirty, run_ind, total_pages, need_pages, rem_pages, i;
-
-	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run);
-	old_ndirty = chunk->ndirty;
-	run_ind = (unsigned)(((uintptr_t)run - (uintptr_t)chunk)
-	    >> PAGE_SHIFT);
-	total_pages = (chunk->map[run_ind].bits & ~PAGE_MASK) >>
-	    PAGE_SHIFT;
-	need_pages = (size >> PAGE_SHIFT);
-	assert(need_pages > 0);
-	assert(need_pages <= total_pages);
-	rem_pages = total_pages - need_pages;
-
-	arena_avail_tree_remove(&arena->runs_avail, &chunk->map[run_ind]);
-	arena->nactive += need_pages;
-
-	/* Keep track of trailing unused pages for later use. */
-	if (rem_pages > 0) {
-		chunk->map[run_ind+need_pages].bits = (rem_pages <<
-		    PAGE_SHIFT) | (chunk->map[run_ind+need_pages].bits &
-		    CHUNK_MAP_FLAGS_MASK);
-		chunk->map[run_ind+total_pages-1].bits = (rem_pages <<
-		    PAGE_SHIFT) | (chunk->map[run_ind+total_pages-1].bits &
-		    CHUNK_MAP_FLAGS_MASK);
-		arena_avail_tree_insert(&arena->runs_avail,
-		    &chunk->map[run_ind+need_pages]);
-	}
-
-	for (i = 0; i < need_pages; i++) {
-		/* Zero if necessary. */
-		if (zero) {
-			if ((chunk->map[run_ind + i].bits & CHUNK_MAP_ZEROED)
-			    == 0) {
-				memset((void *)((uintptr_t)chunk + ((run_ind
-				    + i) << PAGE_SHIFT)), 0, PAGE_SIZE);
-				/* CHUNK_MAP_ZEROED is cleared below. */
-			}
-		}
-
-		/* Update dirty page accounting. */
-		if (chunk->map[run_ind + i].bits & CHUNK_MAP_DIRTY) {
-			chunk->ndirty--;
-			arena->ndirty--;
-			/* CHUNK_MAP_DIRTY is cleared below. */
-		}
-
-		/* Initialize the chunk map. */
-		if (large) {
-			chunk->map[run_ind + i].bits = CHUNK_MAP_LARGE
-			    | CHUNK_MAP_ALLOCATED;
-		} else {
-			chunk->map[run_ind + i].bits = (i << CHUNK_MAP_PG_SHIFT)
-			    | CHUNK_MAP_ALLOCATED;
-		}
-	}
-
-	if (large) {
-		/*
-		 * Set the run size only in the first element for large runs.
-		 * This is primarily a debugging aid, since the lack of size
-		 * info for trailing pages only matters if the application
-		 * tries to operate on an interior pointer.
-		 */
-		chunk->map[run_ind].bits |= size;
-	} else {
-		/*
-		 * Initialize the first page's refcount to 1, so that the run
-		 * header is protected from dirty page purging.
-		 */
-		chunk->map[run_ind].bits += CHUNK_MAP_RC_ONE;
-	}
-}
-
-static arena_chunk_t *
-arena_chunk_alloc(arena_t *arena)
-{
-	arena_chunk_t *chunk;
-	size_t i;
-
-	if (arena->spare != NULL) {
-		chunk = arena->spare;
-		arena->spare = NULL;
-	} else {
-		bool zero;
-		size_t zeroed;
-
-		zero = false;
-		chunk = (arena_chunk_t *)chunk_alloc(chunksize, &zero);
-		if (chunk == NULL)
-			return (NULL);
-#ifdef MALLOC_STATS
-		arena->stats.mapped += chunksize;
-#endif
-
-		chunk->arena = arena;
-		chunk->dirtied = false;
-
-		/*
-		 * Claim that no pages are in use, since the header is merely
-		 * overhead.
-		 */
-		chunk->ndirty = 0;
-
-		/*
-		 * Initialize the map to contain one maximal free untouched run.
-		 * Mark the pages as zeroed iff chunk_alloc() returned a zeroed
-		 * chunk.
-		 */
-		zeroed = zero ? CHUNK_MAP_ZEROED : 0;
-		for (i = 0; i < arena_chunk_header_npages; i++)
-			chunk->map[i].bits = 0;
-		chunk->map[i].bits = arena_maxclass | zeroed;
-		for (i++; i < chunk_npages-1; i++)
-			chunk->map[i].bits = zeroed;
-		chunk->map[chunk_npages-1].bits = arena_maxclass | zeroed;
-	}
-
-	/* Insert the run into the runs_avail tree. */
-	arena_avail_tree_insert(&arena->runs_avail,
-	    &chunk->map[arena_chunk_header_npages]);
-
-	return (chunk);
-}
-
-static void
-arena_chunk_dealloc(arena_t *arena, arena_chunk_t *chunk)
-{
-
-	if (arena->spare != NULL) {
-		if (arena->spare->dirtied) {
-			arena_chunk_tree_dirty_remove(
-			    &chunk->arena->chunks_dirty, arena->spare);
-			arena->ndirty -= arena->spare->ndirty;
-		}
-		chunk_dealloc((void *)arena->spare, chunksize);
-#ifdef MALLOC_STATS
-		arena->stats.mapped -= chunksize;
-#endif
-	}
-
-	/*
-	 * Remove run from runs_avail, regardless of whether this chunk
-	 * will be cached, so that the arena does not use it.  Dirty page
-	 * flushing only uses the chunks_dirty tree, so leaving this chunk in
-	 * the chunks_* trees is sufficient for that purpose.
-	 */
-	arena_avail_tree_remove(&arena->runs_avail,
-	    &chunk->map[arena_chunk_header_npages]);
-
-	arena->spare = chunk;
-}
-
-static arena_run_t *
-arena_run_alloc(arena_t *arena, size_t size, bool large, bool zero)
-{
-	arena_chunk_t *chunk;
-	arena_run_t *run;
-	arena_chunk_map_t *mapelm, key;
-
-	assert(size <= arena_maxclass);
-	assert((size & PAGE_MASK) == 0);
-
-	/* Search the arena's chunks for the lowest best fit. */
-	key.bits = size | CHUNK_MAP_KEY;
-	mapelm = arena_avail_tree_nsearch(&arena->runs_avail, &key);
-	if (mapelm != NULL) {
-		arena_chunk_t *run_chunk = CHUNK_ADDR2BASE(mapelm);
-		size_t pageind = ((uintptr_t)mapelm - (uintptr_t)run_chunk->map)
-		    / sizeof(arena_chunk_map_t);
-
-		run = (arena_run_t *)((uintptr_t)run_chunk + (pageind
-		    << PAGE_SHIFT));
-		arena_run_split(arena, run, size, large, zero);
-		return (run);
-	}
-
-	/*
-	 * No usable runs.  Create a new chunk from which to allocate the run.
-	 */
-	chunk = arena_chunk_alloc(arena);
-	if (chunk == NULL)
-		return (NULL);
-	run = (arena_run_t *)((uintptr_t)chunk + (arena_chunk_header_npages <<
-	    PAGE_SHIFT));
-	/* Update page map. */
-	arena_run_split(arena, run, size, large, zero);
-	return (run);
-}
-
-#ifdef MALLOC_DEBUG
-static arena_chunk_t *
-chunks_dirty_iter_cb(arena_chunk_tree_t *tree, arena_chunk_t *chunk, void *arg)
-{
-	size_t *ndirty = (size_t *)arg;
-
-	assert(chunk->dirtied);
-	*ndirty += chunk->ndirty;
-	return (NULL);
-}
-#endif
-
-static void
-arena_purge(arena_t *arena)
-{
-	arena_chunk_t *chunk;
-	size_t i, npages;
-#ifdef MALLOC_DEBUG
-	size_t ndirty = 0;
-
-	arena_chunk_tree_dirty_iter(&arena->chunks_dirty, NULL,
-	    chunks_dirty_iter_cb, (void *)&ndirty);
-	assert(ndirty == arena->ndirty);
-#endif
-	assert((arena->nactive >> opt_lg_dirty_mult) < arena->ndirty);
-
-#ifdef MALLOC_STATS
-	arena->stats.npurge++;
-#endif
-
-	/*
-	 * Iterate downward through chunks until enough dirty memory has been
-	 * purged.  Terminate as soon as possible in order to minimize the
-	 * number of system calls, even if a chunk has only been partially
-	 * purged.
-	 */
-
-	while ((arena->nactive >> (opt_lg_dirty_mult + 1)) < arena->ndirty) {
-		chunk = arena_chunk_tree_dirty_last(&arena->chunks_dirty);
-		assert(chunk != NULL);
-
-		for (i = chunk_npages - 1; chunk->ndirty > 0; i--) {
-			assert(i >= arena_chunk_header_npages);
-			if (chunk->map[i].bits & CHUNK_MAP_DIRTY) {
-				chunk->map[i].bits ^= CHUNK_MAP_DIRTY;
-				/* Find adjacent dirty run(s). */
-				for (npages = 1; i > arena_chunk_header_npages
-				    && (chunk->map[i - 1].bits &
-				    CHUNK_MAP_DIRTY); npages++) {
-					i--;
-					chunk->map[i].bits ^= CHUNK_MAP_DIRTY;
-				}
-				chunk->ndirty -= npages;
-				arena->ndirty -= npages;
-
-				madvise((void *)((uintptr_t)chunk + (i <<
-				    PAGE_SHIFT)), (npages << PAGE_SHIFT),
-				    MADV_FREE);
-#ifdef MALLOC_STATS
-				arena->stats.nmadvise++;
-				arena->stats.purged += npages;
-#endif
-				if ((arena->nactive >> (opt_lg_dirty_mult + 1))
-				    >= arena->ndirty)
-					break;
-			}
-		}
-
-		if (chunk->ndirty == 0) {
-			arena_chunk_tree_dirty_remove(&arena->chunks_dirty,
-			    chunk);
-			chunk->dirtied = false;
-		}
-	}
-}
-
-static void
-arena_run_dalloc(arena_t *arena, arena_run_t *run, bool dirty)
-{
-	arena_chunk_t *chunk;
-	size_t size, run_ind, run_pages;
-
-	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run);
-	run_ind = (size_t)(((uintptr_t)run - (uintptr_t)chunk)
-	    >> PAGE_SHIFT);
-	assert(run_ind >= arena_chunk_header_npages);
-	assert(run_ind < chunk_npages);
-	if ((chunk->map[run_ind].bits & CHUNK_MAP_LARGE) != 0)
-		size = chunk->map[run_ind].bits & ~PAGE_MASK;
-	else
-		size = run->bin->run_size;
-	run_pages = (size >> PAGE_SHIFT);
-	arena->nactive -= run_pages;
-
-	/* Mark pages as unallocated in the chunk map. */
-	if (dirty) {
-		size_t i;
-
-		for (i = 0; i < run_pages; i++) {
-			/*
-			 * When (dirty == true), *all* pages within the run
-			 * need to have their dirty bits set, because only
-			 * small runs can create a mixture of clean/dirty
-			 * pages, but such runs are passed to this function
-			 * with (dirty == false).
-			 */
-			assert((chunk->map[run_ind + i].bits & CHUNK_MAP_DIRTY)
-			    == 0);
-			chunk->ndirty++;
-			arena->ndirty++;
-			chunk->map[run_ind + i].bits = CHUNK_MAP_DIRTY;
-		}
-	} else {
-		size_t i;
-
-		for (i = 0; i < run_pages; i++) {
-			chunk->map[run_ind + i].bits &= ~(CHUNK_MAP_LARGE |
-			    CHUNK_MAP_ALLOCATED);
-		}
-	}
-	chunk->map[run_ind].bits = size | (chunk->map[run_ind].bits &
-	    CHUNK_MAP_FLAGS_MASK);
-	chunk->map[run_ind+run_pages-1].bits = size |
-	    (chunk->map[run_ind+run_pages-1].bits & CHUNK_MAP_FLAGS_MASK);
-
-	/* Try to coalesce forward. */
-	if (run_ind + run_pages < chunk_npages &&
-	    (chunk->map[run_ind+run_pages].bits & CHUNK_MAP_ALLOCATED) == 0) {
-		size_t nrun_size = chunk->map[run_ind+run_pages].bits &
-		    ~PAGE_MASK;
-
-		/*
-		 * Remove successor from runs_avail; the coalesced run is
-		 * inserted later.
-		 */
-		arena_avail_tree_remove(&arena->runs_avail,
-		    &chunk->map[run_ind+run_pages]);
-
-		size += nrun_size;
-		run_pages = size >> PAGE_SHIFT;
-
-		assert((chunk->map[run_ind+run_pages-1].bits & ~PAGE_MASK)
-		    == nrun_size);
-		chunk->map[run_ind].bits = size | (chunk->map[run_ind].bits &
-		    CHUNK_MAP_FLAGS_MASK);
-		chunk->map[run_ind+run_pages-1].bits = size |
-		    (chunk->map[run_ind+run_pages-1].bits &
-		    CHUNK_MAP_FLAGS_MASK);
-	}
-
-	/* Try to coalesce backward. */
-	if (run_ind > arena_chunk_header_npages && (chunk->map[run_ind-1].bits &
-	    CHUNK_MAP_ALLOCATED) == 0) {
-		size_t prun_size = chunk->map[run_ind-1].bits & ~PAGE_MASK;
-
-		run_ind -= prun_size >> PAGE_SHIFT;
-
-		/*
-		 * Remove predecessor from runs_avail; the coalesced run is
-		 * inserted later.
-		 */
-		arena_avail_tree_remove(&arena->runs_avail,
-		    &chunk->map[run_ind]);
-
-		size += prun_size;
-		run_pages = size >> PAGE_SHIFT;
-
-		assert((chunk->map[run_ind].bits & ~PAGE_MASK) == prun_size);
-		chunk->map[run_ind].bits = size | (chunk->map[run_ind].bits &
-		    CHUNK_MAP_FLAGS_MASK);
-		chunk->map[run_ind+run_pages-1].bits = size |
-		    (chunk->map[run_ind+run_pages-1].bits &
-		    CHUNK_MAP_FLAGS_MASK);
-	}
-
-	/* Insert into runs_avail, now that coalescing is complete. */
-	arena_avail_tree_insert(&arena->runs_avail, &chunk->map[run_ind]);
-
-	/*
-	 * Deallocate chunk if it is now completely unused.  The bit
-	 * manipulation checks whether the first run is unallocated and extends
-	 * to the end of the chunk.
-	 */
-	if ((chunk->map[arena_chunk_header_npages].bits & (~PAGE_MASK |
-	    CHUNK_MAP_ALLOCATED)) == arena_maxclass)
-		arena_chunk_dealloc(arena, chunk);
-
-	/*
-	 * It is okay to do dirty page processing even if the chunk was
-	 * deallocated above, since in that case it is the spare.  Waiting
-	 * until after possible chunk deallocation to do dirty processing
-	 * allows for an old spare to be fully deallocated, thus decreasing the
-	 * chances of spuriously crossing the dirty page purging threshold.
-	 */
-	if (dirty) {
-		if (chunk->dirtied == false) {
-			arena_chunk_tree_dirty_insert(&arena->chunks_dirty,
-			    chunk);
-			chunk->dirtied = true;
-		}
-
-		/* Enforce opt_lg_dirty_mult. */
-		if (opt_lg_dirty_mult >= 0 && (arena->nactive >>
-		    opt_lg_dirty_mult) < arena->ndirty)
-			arena_purge(arena);
-	}
-}
-
-static void
-arena_run_trim_head(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run,
-    size_t oldsize, size_t newsize)
-{
-	size_t pageind = ((uintptr_t)run - (uintptr_t)chunk) >> PAGE_SHIFT;
-	size_t head_npages = (oldsize - newsize) >> PAGE_SHIFT;
-
-	assert(oldsize > newsize);
-
-	/*
-	 * Update the chunk map so that arena_run_dalloc() can treat the
-	 * leading run as separately allocated.
-	 */
-	assert((chunk->map[pageind].bits & CHUNK_MAP_DIRTY) == 0);
-	chunk->map[pageind].bits = (oldsize - newsize) | CHUNK_MAP_LARGE |
-	    CHUNK_MAP_ALLOCATED;
-	assert((chunk->map[pageind+head_npages].bits & CHUNK_MAP_DIRTY) == 0);
-	chunk->map[pageind+head_npages].bits = newsize | CHUNK_MAP_LARGE |
-	    CHUNK_MAP_ALLOCATED;
-
-	arena_run_dalloc(arena, run, false);
-}
-
-static void
-arena_run_trim_tail(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run,
-    size_t oldsize, size_t newsize, bool dirty)
-{
-	size_t pageind = ((uintptr_t)run - (uintptr_t)chunk) >> PAGE_SHIFT;
-	size_t npages = newsize >> PAGE_SHIFT;
-
-	assert(oldsize > newsize);
-
-	/*
-	 * Update the chunk map so that arena_run_dalloc() can treat the
-	 * trailing run as separately allocated.
-	 */
-	assert((chunk->map[pageind].bits & CHUNK_MAP_DIRTY) == 0);
-	chunk->map[pageind].bits = newsize | CHUNK_MAP_LARGE |
-	    CHUNK_MAP_ALLOCATED;
-	assert((chunk->map[pageind+npages].bits & CHUNK_MAP_DIRTY) == 0);
-	chunk->map[pageind+npages].bits = (oldsize - newsize) | CHUNK_MAP_LARGE
-	    | CHUNK_MAP_ALLOCATED;
-
-	arena_run_dalloc(arena, (arena_run_t *)((uintptr_t)run + newsize),
-	    dirty);
-}
-
-static arena_run_t *
-arena_bin_nonfull_run_get(arena_t *arena, arena_bin_t *bin)
-{
-	arena_chunk_map_t *mapelm;
-	arena_run_t *run;
-	unsigned i, remainder;
-
-	/* Look for a usable run. */
-	mapelm = arena_run_tree_first(&bin->runs);
-	if (mapelm != NULL) {
-		arena_chunk_t *chunk;
-		size_t pageind;
-
-		/* run is guaranteed to have available space. */
-		arena_run_tree_remove(&bin->runs, mapelm);
-
-		chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(mapelm);
-		pageind = (((uintptr_t)mapelm - (uintptr_t)chunk->map) /
-		    sizeof(arena_chunk_map_t));
-		run = (arena_run_t *)((uintptr_t)chunk + (uintptr_t)((pageind -
-		    ((mapelm->bits & CHUNK_MAP_PG_MASK) >> CHUNK_MAP_PG_SHIFT))
-		    << PAGE_SHIFT));
-#ifdef MALLOC_STATS
-		bin->stats.reruns++;
-#endif
-		return (run);
-	}
-	/* No existing runs have any space available. */
-
-	/* Allocate a new run. */
-	run = arena_run_alloc(arena, bin->run_size, false, false);
-	if (run == NULL)
-		return (NULL);
-
-	/* Initialize run internals. */
-	run->bin = bin;
-
-	for (i = 0; i < bin->regs_mask_nelms - 1; i++)
-		run->regs_mask[i] = UINT_MAX;
-	remainder = bin->nregs & ((1U << (LG_SIZEOF_INT + 3)) - 1);
-	if (remainder == 0)
-		run->regs_mask[i] = UINT_MAX;
-	else {
-		/* The last element has spare bits that need to be unset. */
-		run->regs_mask[i] = (UINT_MAX >> ((1U << (LG_SIZEOF_INT + 3))
-		    - remainder));
-	}
-
-	run->regs_minelm = 0;
-
-	run->nfree = bin->nregs;
-#ifdef MALLOC_DEBUG
-	run->magic = ARENA_RUN_MAGIC;
-#endif
-
-#ifdef MALLOC_STATS
-	bin->stats.nruns++;
-	bin->stats.curruns++;
-	if (bin->stats.curruns > bin->stats.highruns)
-		bin->stats.highruns = bin->stats.curruns;
-#endif
-	return (run);
-}
-
-/* bin->runcur must have space available before this function is called. */
-static inline void *
-arena_bin_malloc_easy(arena_t *arena, arena_bin_t *bin, arena_run_t *run)
-{
-	void *ret;
-
-	assert(run->magic == ARENA_RUN_MAGIC);
-	assert(run->nfree > 0);
-
-	ret = arena_run_reg_alloc(run, bin);
-	assert(ret != NULL);
-	run->nfree--;
-
-	return (ret);
-}
-
-/* Re-fill bin->runcur, then call arena_bin_malloc_easy(). */
-static void *
-arena_bin_malloc_hard(arena_t *arena, arena_bin_t *bin)
-{
-
-	bin->runcur = arena_bin_nonfull_run_get(arena, bin);
-	if (bin->runcur == NULL)
-		return (NULL);
-	assert(bin->runcur->magic == ARENA_RUN_MAGIC);
-	assert(bin->runcur->nfree > 0);
-
-	return (arena_bin_malloc_easy(arena, bin, bin->runcur));
-}
-
-/*
- * Calculate bin->run_size such that it meets the following constraints:
- *
- *   *) bin->run_size >= min_run_size
- *   *) bin->run_size <= arena_maxclass
- *   *) bin->run_size <= RUN_MAX_SMALL
- *   *) run header overhead <= RUN_MAX_OVRHD (or header overhead relaxed).
- *   *) run header size < PAGE_SIZE
- *
- * bin->nregs, bin->regs_mask_nelms, and bin->reg0_offset are
- * also calculated here, since these settings are all interdependent.
- */
-static size_t
-arena_bin_run_size_calc(arena_bin_t *bin, size_t min_run_size)
-{
-	size_t try_run_size, good_run_size;
-	unsigned good_nregs, good_mask_nelms, good_reg0_offset;
-	unsigned try_nregs, try_mask_nelms, try_reg0_offset;
-
-	assert(min_run_size >= PAGE_SIZE);
-	assert(min_run_size <= arena_maxclass);
-	assert(min_run_size <= RUN_MAX_SMALL);
-
-	/*
-	 * Calculate known-valid settings before entering the run_size
-	 * expansion loop, so that the first part of the loop always copies
-	 * valid settings.
-	 *
-	 * The do..while loop iteratively reduces the number of regions until
-	 * the run header and the regions no longer overlap.  A closed formula
-	 * would be quite messy, since there is an interdependency between the
-	 * header's mask length and the number of regions.
-	 */
-	try_run_size = min_run_size;
-	try_nregs = ((try_run_size - sizeof(arena_run_t)) / bin->reg_size)
-	    + 1; /* Counter-act try_nregs-- in loop. */
-	do {
-		try_nregs--;
-		try_mask_nelms = (try_nregs >> (LG_SIZEOF_INT + 3)) +
-		    ((try_nregs & ((1U << (LG_SIZEOF_INT + 3)) - 1)) ? 1 : 0);
-		try_reg0_offset = try_run_size - (try_nregs * bin->reg_size);
-	} while (sizeof(arena_run_t) + (sizeof(unsigned) * (try_mask_nelms - 1))
-	    > try_reg0_offset);
-
-	/* run_size expansion loop. */
-	do {
-		/*
-		 * Copy valid settings before trying more aggressive settings.
-		 */
-		good_run_size = try_run_size;
-		good_nregs = try_nregs;
-		good_mask_nelms = try_mask_nelms;
-		good_reg0_offset = try_reg0_offset;
-
-		/* Try more aggressive settings. */
-		try_run_size += PAGE_SIZE;
-		try_nregs = ((try_run_size - sizeof(arena_run_t)) /
-		    bin->reg_size) + 1; /* Counter-act try_nregs-- in loop. */
-		do {
-			try_nregs--;
-			try_mask_nelms = (try_nregs >> (LG_SIZEOF_INT + 3)) +
-			    ((try_nregs & ((1U << (LG_SIZEOF_INT + 3)) - 1)) ?
-			    1 : 0);
-			try_reg0_offset = try_run_size - (try_nregs *
-			    bin->reg_size);
-		} while (sizeof(arena_run_t) + (sizeof(unsigned) *
-		    (try_mask_nelms - 1)) > try_reg0_offset);
-	} while (try_run_size <= arena_maxclass && try_run_size <= RUN_MAX_SMALL
-	    && RUN_MAX_OVRHD * (bin->reg_size << 3) > RUN_MAX_OVRHD_RELAX
-	    && (try_reg0_offset << RUN_BFP) > RUN_MAX_OVRHD * try_run_size
-	    && (sizeof(arena_run_t) + (sizeof(unsigned) * (try_mask_nelms - 1)))
-	    < PAGE_SIZE);
-
-	assert(sizeof(arena_run_t) + (sizeof(unsigned) * (good_mask_nelms - 1))
-	    <= good_reg0_offset);
-	assert((good_mask_nelms << (LG_SIZEOF_INT + 3)) >= good_nregs);
-
-	/* Copy final settings. */
-	bin->run_size = good_run_size;
-	bin->nregs = good_nregs;
-	bin->regs_mask_nelms = good_mask_nelms;
-	bin->reg0_offset = good_reg0_offset;
-
-	return (good_run_size);
-}
-
-#ifdef MALLOC_TCACHE
-static inline void
-tcache_event(tcache_t *tcache)
-{
-
-	if (tcache_gc_incr == 0)
-		return;
-
-	tcache->ev_cnt++;
-	assert(tcache->ev_cnt <= tcache_gc_incr);
-	if (tcache->ev_cnt >= tcache_gc_incr) {
-		size_t binind = tcache->next_gc_bin;
-		tcache_bin_t *tbin = tcache->tbins[binind];
-
-		if (tbin != NULL) {
-			if (tbin->high_water == 0) {
-				/*
-				 * This bin went completely unused for an
-				 * entire GC cycle, so throw away the tbin.
-				 */
-				assert(tbin->ncached == 0);
-				tcache_bin_destroy(tcache, tbin, binind);
-				tcache->tbins[binind] = NULL;
-			} else {
-				if (tbin->low_water > 0) {
-					/*
-					 * Flush (ceiling) half of the objects
-					 * below the low water mark.
-					 */
-					tcache_bin_flush(tbin, binind,
-					    tbin->ncached - (tbin->low_water >>
-					    1) - (tbin->low_water & 1));
-				}
-				tbin->low_water = tbin->ncached;
-				tbin->high_water = tbin->ncached;
-			}
-		}
-
-		tcache->next_gc_bin++;
-		if (tcache->next_gc_bin == nbins)
-			tcache->next_gc_bin = 0;
-		tcache->ev_cnt = 0;
-	}
-}
-
-static inline void *
-tcache_bin_alloc(tcache_bin_t *tbin)
-{
-
-	if (tbin->ncached == 0)
-		return (NULL);
-	tbin->ncached--;
-	if (tbin->ncached < tbin->low_water)
-		tbin->low_water = tbin->ncached;
-	return (tbin->slots[tbin->ncached]);
-}
-
-static void
-tcache_bin_fill(tcache_t *tcache, tcache_bin_t *tbin, size_t binind)
-{
-	arena_t *arena;
-	arena_bin_t *bin;
-	arena_run_t *run;
-	void *ptr;
-	unsigned i;
-
-	assert(tbin->ncached == 0);
-
-	arena = tcache->arena;
-	bin = &arena->bins[binind];
-	malloc_spin_lock(&arena->lock);
-	for (i = 0; i < (tcache_nslots >> 1); i++) {
-		if ((run = bin->runcur) != NULL && run->nfree > 0)
-			ptr = arena_bin_malloc_easy(arena, bin, run);
-		else
-			ptr = arena_bin_malloc_hard(arena, bin);
-		if (ptr == NULL)
-			break;
-		/*
-		 * Fill tbin such that the objects lowest in memory are used
-		 * first.
-		 */
-		tbin->slots[(tcache_nslots >> 1) - 1 - i] = ptr;
-	}
-#ifdef MALLOC_STATS
-	bin->stats.nfills++;
-	bin->stats.nrequests += tbin->tstats.nrequests;
-	if (bin->reg_size <= small_maxclass) {
-		arena->stats.nmalloc_small += (i - tbin->ncached);
-		arena->stats.allocated_small += (i - tbin->ncached) *
-		    bin->reg_size;
-		arena->stats.nmalloc_small += tbin->tstats.nrequests;
-	} else {
-		arena->stats.nmalloc_medium += (i - tbin->ncached);
-		arena->stats.allocated_medium += (i - tbin->ncached) *
-		    bin->reg_size;
-		arena->stats.nmalloc_medium += tbin->tstats.nrequests;
-	}
-	tbin->tstats.nrequests = 0;
-#endif
-	malloc_spin_unlock(&arena->lock);
-	tbin->ncached = i;
-	if (tbin->ncached > tbin->high_water)
-		tbin->high_water = tbin->ncached;
-}
-
-static inline void *
-tcache_alloc(tcache_t *tcache, size_t size, bool zero)
-{
-	void *ret;
-	tcache_bin_t *tbin;
-	size_t binind;
-
-	if (size <= small_maxclass)
-		binind = small_size2bin[size];
-	else {
-		binind = mbin0 + ((MEDIUM_CEILING(size) - medium_min) >>
-		    lg_mspace);
-	}
-	assert(binind < nbins);
-	tbin = tcache->tbins[binind];
-	if (tbin == NULL) {
-		tbin = tcache_bin_create(tcache->arena);
-		if (tbin == NULL)
-			return (NULL);
-		tcache->tbins[binind] = tbin;
-	}
-
-	ret = tcache_bin_alloc(tbin);
-	if (ret == NULL) {
-		ret = tcache_alloc_hard(tcache, tbin, binind);
-		if (ret == NULL)
-			return (NULL);
-	}
-
-	if (zero == false) {
-		if (opt_junk)
-			memset(ret, 0xa5, size);
-		else if (opt_zero)
-			memset(ret, 0, size);
-	} else
-		memset(ret, 0, size);
-
-#ifdef MALLOC_STATS
-	tbin->tstats.nrequests++;
-#endif
-	tcache_event(tcache);
-	return (ret);
-}
-
-static void *
-tcache_alloc_hard(tcache_t *tcache, tcache_bin_t *tbin, size_t binind)
-{
-	void *ret;
-
-	tcache_bin_fill(tcache, tbin, binind);
-	ret = tcache_bin_alloc(tbin);
-
-	return (ret);
-}
-#endif
-
-static inline void *
-arena_malloc_small(arena_t *arena, size_t size, bool zero)
-{
-	void *ret;
-	arena_bin_t *bin;
-	arena_run_t *run;
-	size_t binind;
-
-	binind = small_size2bin[size];
-	assert(binind < mbin0);
-	bin = &arena->bins[binind];
-	size = bin->reg_size;
-
-	malloc_spin_lock(&arena->lock);
-	if ((run = bin->runcur) != NULL && run->nfree > 0)
-		ret = arena_bin_malloc_easy(arena, bin, run);
-	else
-		ret = arena_bin_malloc_hard(arena, bin);
-
-	if (ret == NULL) {
-		malloc_spin_unlock(&arena->lock);
-		return (NULL);
-	}
-
-#ifdef MALLOC_STATS
-#  ifdef MALLOC_TCACHE
-	if (__isthreaded == false) {
-#  endif
-		bin->stats.nrequests++;
-		arena->stats.nmalloc_small++;
-#  ifdef MALLOC_TCACHE
-	}
-#  endif
-	arena->stats.allocated_small += size;
-#endif
-	malloc_spin_unlock(&arena->lock);
-
-	if (zero == false) {
-		if (opt_junk)
-			memset(ret, 0xa5, size);
-		else if (opt_zero)
-			memset(ret, 0, size);
-	} else
-		memset(ret, 0, size);
-
-	return (ret);
-}
-
-static void *
-arena_malloc_medium(arena_t *arena, size_t size, bool zero)
-{
-	void *ret;
-	arena_bin_t *bin;
-	arena_run_t *run;
-	size_t binind;
-
-	size = MEDIUM_CEILING(size);
-	binind = mbin0 + ((size - medium_min) >> lg_mspace);
-	assert(binind < nbins);
-	bin = &arena->bins[binind];
-	assert(bin->reg_size == size);
-
-	malloc_spin_lock(&arena->lock);
-	if ((run = bin->runcur) != NULL && run->nfree > 0)
-		ret = arena_bin_malloc_easy(arena, bin, run);
-	else
-		ret = arena_bin_malloc_hard(arena, bin);
-
-	if (ret == NULL) {
-		malloc_spin_unlock(&arena->lock);
-		return (NULL);
-	}
-
-#ifdef MALLOC_STATS
-#  ifdef MALLOC_TCACHE
-	if (__isthreaded == false) {
-#  endif
-		bin->stats.nrequests++;
-		arena->stats.nmalloc_medium++;
-#  ifdef MALLOC_TCACHE
-	}
-#  endif
-	arena->stats.allocated_medium += size;
-#endif
-	malloc_spin_unlock(&arena->lock);
-
-	if (zero == false) {
-		if (opt_junk)
-			memset(ret, 0xa5, size);
-		else if (opt_zero)
-			memset(ret, 0, size);
-	} else
-		memset(ret, 0, size);
-
-	return (ret);
-}
-
-static void *
-arena_malloc_large(arena_t *arena, size_t size, bool zero)
-{
-	void *ret;
-
-	/* Large allocation. */
-	size = PAGE_CEILING(size);
-	malloc_spin_lock(&arena->lock);
-	ret = (void *)arena_run_alloc(arena, size, true, zero);
-	if (ret == NULL) {
-		malloc_spin_unlock(&arena->lock);
-		return (NULL);
-	}
-#ifdef MALLOC_STATS
-	arena->stats.nmalloc_large++;
-	arena->stats.allocated_large += size;
-	arena->stats.lstats[(size >> PAGE_SHIFT) - 1].nrequests++;
-	arena->stats.lstats[(size >> PAGE_SHIFT) - 1].curruns++;
-	if (arena->stats.lstats[(size >> PAGE_SHIFT) - 1].curruns >
-	    arena->stats.lstats[(size >> PAGE_SHIFT) - 1].highruns) {
-		arena->stats.lstats[(size >> PAGE_SHIFT) - 1].highruns =
-		    arena->stats.lstats[(size >> PAGE_SHIFT) - 1].curruns;
-	}
-#endif
-	malloc_spin_unlock(&arena->lock);
-
-	if (zero == false) {
-		if (opt_junk)
-			memset(ret, 0xa5, size);
-		else if (opt_zero)
-			memset(ret, 0, size);
-	}
-
-	return (ret);
-}
-
-static inline void *
-arena_malloc(size_t size, bool zero)
-{
-
-	assert(size != 0);
-	assert(QUANTUM_CEILING(size) <= arena_maxclass);
-
-	if (size <= bin_maxclass) {
-#ifdef MALLOC_TCACHE
-		if (__isthreaded && tcache_nslots) {
-			tcache_t *tcache = tcache_tls;
-			if ((uintptr_t)tcache > (uintptr_t)1)
-				return (tcache_alloc(tcache, size, zero));
-			else if (tcache == NULL) {
-				tcache = tcache_create(choose_arena());
-				if (tcache == NULL)
-					return (NULL);
-				return (tcache_alloc(tcache, size, zero));
-			}
-		}
-#endif
-		if (size <= small_maxclass) {
-			return (arena_malloc_small(choose_arena(), size,
-			    zero));
-		} else {
-			return (arena_malloc_medium(choose_arena(),
-			    size, zero));
-		}
-	} else
-		return (arena_malloc_large(choose_arena(), size, zero));
-}
-
-static inline void *
-imalloc(size_t size)
-{
-
-	assert(size != 0);
-
-	if (size <= arena_maxclass)
-		return (arena_malloc(size, false));
-	else
-		return (huge_malloc(size, false));
-}
-
-static inline void *
-icalloc(size_t size)
-{
-
-	if (size <= arena_maxclass)
-		return (arena_malloc(size, true));
-	else
-		return (huge_malloc(size, true));
-}
-
-/* Only handles large allocations that require more than page alignment. */
-static void *
-arena_palloc(arena_t *arena, size_t alignment, size_t size, size_t alloc_size)
-{
-	void *ret;
-	size_t offset;
-	arena_chunk_t *chunk;
-
-	assert((size & PAGE_MASK) == 0);
-	assert((alignment & PAGE_MASK) == 0);
-
-	malloc_spin_lock(&arena->lock);
-	ret = (void *)arena_run_alloc(arena, alloc_size, true, false);
-	if (ret == NULL) {
-		malloc_spin_unlock(&arena->lock);
-		return (NULL);
-	}
-
-	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ret);
-
-	offset = (uintptr_t)ret & (alignment - 1);
-	assert((offset & PAGE_MASK) == 0);
-	assert(offset < alloc_size);
-	if (offset == 0)
-		arena_run_trim_tail(arena, chunk, ret, alloc_size, size, false);
-	else {
-		size_t leadsize, trailsize;
-
-		leadsize = alignment - offset;
-		if (leadsize > 0) {
-			arena_run_trim_head(arena, chunk, ret, alloc_size,
-			    alloc_size - leadsize);
-			ret = (void *)((uintptr_t)ret + leadsize);
-		}
-
-		trailsize = alloc_size - leadsize - size;
-		if (trailsize != 0) {
-			/* Trim trailing space. */
-			assert(trailsize < alloc_size);
-			arena_run_trim_tail(arena, chunk, ret, size + trailsize,
-			    size, false);
-		}
-	}
-
-#ifdef MALLOC_STATS
-	arena->stats.nmalloc_large++;
-	arena->stats.allocated_large += size;
-	arena->stats.lstats[(size >> PAGE_SHIFT) - 1].nrequests++;
-	arena->stats.lstats[(size >> PAGE_SHIFT) - 1].curruns++;
-	if (arena->stats.lstats[(size >> PAGE_SHIFT) - 1].curruns >
-	    arena->stats.lstats[(size >> PAGE_SHIFT) - 1].highruns) {
-		arena->stats.lstats[(size >> PAGE_SHIFT) - 1].highruns =
-		    arena->stats.lstats[(size >> PAGE_SHIFT) - 1].curruns;
-	}
-#endif
-	malloc_spin_unlock(&arena->lock);
-
-	if (opt_junk)
-		memset(ret, 0xa5, size);
-	else if (opt_zero)
-		memset(ret, 0, size);
-	return (ret);
-}
-
-static inline void *
-ipalloc(size_t alignment, size_t size)
-{
-	void *ret;
-	size_t ceil_size;
-
-	/*
-	 * Round size up to the nearest multiple of alignment.
-	 *
-	 * This done, we can take advantage of the fact that for each small
-	 * size class, every object is aligned at the smallest power of two
-	 * that is non-zero in the base two representation of the size.  For
-	 * example:
-	 *
-	 *   Size |   Base 2 | Minimum alignment
-	 *   -----+----------+------------------
-	 *     96 |  1100000 |  32
-	 *    144 | 10100000 |  32
-	 *    192 | 11000000 |  64
-	 *
-	 * Depending on runtime settings, it is possible that arena_malloc()
-	 * will further round up to a power of two, but that never causes
-	 * correctness issues.
-	 */
-	ceil_size = (size + (alignment - 1)) & (-alignment);
-	/*
-	 * (ceil_size < size) protects against the combination of maximal
-	 * alignment and size greater than maximal alignment.
-	 */
-	if (ceil_size < size) {
-		/* size_t overflow. */
-		return (NULL);
-	}
-
-	if (ceil_size <= PAGE_SIZE || (alignment <= PAGE_SIZE
-	    && ceil_size <= arena_maxclass))
-		ret = arena_malloc(ceil_size, false);
-	else {
-		size_t run_size;
-
-		/*
-		 * We can't achieve subpage alignment, so round up alignment
-		 * permanently; it makes later calculations simpler.
-		 */
-		alignment = PAGE_CEILING(alignment);
-		ceil_size = PAGE_CEILING(size);
-		/*
-		 * (ceil_size < size) protects against very large sizes within
-		 * PAGE_SIZE of SIZE_T_MAX.
-		 *
-		 * (ceil_size + alignment < ceil_size) protects against the
-		 * combination of maximal alignment and ceil_size large enough
-		 * to cause overflow.  This is similar to the first overflow
-		 * check above, but it needs to be repeated due to the new
-		 * ceil_size value, which may now be *equal* to maximal
-		 * alignment, whereas before we only detected overflow if the
-		 * original size was *greater* than maximal alignment.
-		 */
-		if (ceil_size < size || ceil_size + alignment < ceil_size) {
-			/* size_t overflow. */
-			return (NULL);
-		}
-
-		/*
-		 * Calculate the size of the over-size run that arena_palloc()
-		 * would need to allocate in order to guarantee the alignment.
-		 */
-		if (ceil_size >= alignment)
-			run_size = ceil_size + alignment - PAGE_SIZE;
-		else {
-			/*
-			 * It is possible that (alignment << 1) will cause
-			 * overflow, but it doesn't matter because we also
-			 * subtract PAGE_SIZE, which in the case of overflow
-			 * leaves us with a very large run_size.  That causes
-			 * the first conditional below to fail, which means
-			 * that the bogus run_size value never gets used for
-			 * anything important.
-			 */
-			run_size = (alignment << 1) - PAGE_SIZE;
-		}
-
-		if (run_size <= arena_maxclass) {
-			ret = arena_palloc(choose_arena(), alignment, ceil_size,
-			    run_size);
-		} else if (alignment <= chunksize)
-			ret = huge_malloc(ceil_size, false);
-		else
-			ret = huge_palloc(alignment, ceil_size);
-	}
-
-	assert(((uintptr_t)ret & (alignment - 1)) == 0);
-	return (ret);
-}
-
-static bool
-arena_is_large(const void *ptr)
-{
-	arena_chunk_t *chunk;
-	size_t pageind, mapbits;
-
-	assert(ptr != NULL);
-	assert(CHUNK_ADDR2BASE(ptr) != ptr);
-
-	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
-	pageind = (((uintptr_t)ptr - (uintptr_t)chunk) >> PAGE_SHIFT);
-	mapbits = chunk->map[pageind].bits;
-	assert((mapbits & CHUNK_MAP_ALLOCATED) != 0);
-	return ((mapbits & CHUNK_MAP_LARGE) != 0);
-}
-
-/* Return the size of the allocation pointed to by ptr. */
-static size_t
-arena_salloc(const void *ptr)
-{
-	size_t ret;
-	arena_chunk_t *chunk;
-	size_t pageind, mapbits;
-
-	assert(ptr != NULL);
-	assert(CHUNK_ADDR2BASE(ptr) != ptr);
-
-	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
-	pageind = (((uintptr_t)ptr - (uintptr_t)chunk) >> PAGE_SHIFT);
-	mapbits = chunk->map[pageind].bits;
-	assert((mapbits & CHUNK_MAP_ALLOCATED) != 0);
-	if ((mapbits & CHUNK_MAP_LARGE) == 0) {
-		arena_run_t *run = (arena_run_t *)((uintptr_t)chunk +
-		    (uintptr_t)((pageind - ((mapbits & CHUNK_MAP_PG_MASK) >>
-		    CHUNK_MAP_PG_SHIFT)) << PAGE_SHIFT));
-		assert(run->magic == ARENA_RUN_MAGIC);
-		ret = run->bin->reg_size;
-	} else {
-		ret = mapbits & ~PAGE_MASK;
-		assert(ret != 0);
-	}
-
-	return (ret);
-}
-
-static inline size_t
-isalloc(const void *ptr)
-{
-	size_t ret;
-	arena_chunk_t *chunk;
-
-	assert(ptr != NULL);
-
-	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
-	if (chunk != ptr) {
-		/* Region. */
-		assert(chunk->arena->magic == ARENA_MAGIC);
-
-		ret = arena_salloc(ptr);
-	} else {
-		extent_node_t *node, key;
-
-		/* Chunk (huge allocation). */
-
-		malloc_mutex_lock(&huge_mtx);
-
-		/* Extract from tree of huge allocations. */
-		key.addr = __DECONST(void *, ptr);
-		node = extent_tree_ad_search(&huge, &key);
-		assert(node != NULL);
-
-		ret = node->size;
-
-		malloc_mutex_unlock(&huge_mtx);
-	}
-
-	return (ret);
-}
-
-static inline void
-arena_dalloc_bin(arena_t *arena, arena_chunk_t *chunk, void *ptr,
-    arena_chunk_map_t *mapelm)
-{
-	size_t pageind;
-	arena_run_t *run;
-	arena_bin_t *bin;
-	size_t size;
-
-	pageind = (((uintptr_t)ptr - (uintptr_t)chunk) >> PAGE_SHIFT);
-	run = (arena_run_t *)((uintptr_t)chunk + (uintptr_t)((pageind -
-	    ((mapelm->bits & CHUNK_MAP_PG_MASK) >> CHUNK_MAP_PG_SHIFT)) <<
-	    PAGE_SHIFT));
-	assert(run->magic == ARENA_RUN_MAGIC);
-	bin = run->bin;
-	size = bin->reg_size;
-
-	if (opt_junk)
-		memset(ptr, 0x5a, size);
-
-	arena_run_reg_dalloc(run, bin, ptr, size);
-	run->nfree++;
-
-	if (run->nfree == bin->nregs)
-		arena_dalloc_bin_run(arena, chunk, run, bin);
-	else if (run->nfree == 1 && run != bin->runcur) {
-		/*
-		 * Make sure that bin->runcur always refers to the lowest
-		 * non-full run, if one exists.
-		 */
-		if (bin->runcur == NULL)
-			bin->runcur = run;
-		else if ((uintptr_t)run < (uintptr_t)bin->runcur) {
-			/* Switch runcur. */
-			if (bin->runcur->nfree > 0) {
-				arena_chunk_t *runcur_chunk =
-				    CHUNK_ADDR2BASE(bin->runcur);
-				size_t runcur_pageind =
-				    (((uintptr_t)bin->runcur -
-				    (uintptr_t)runcur_chunk)) >> PAGE_SHIFT;
-				arena_chunk_map_t *runcur_mapelm =
-				    &runcur_chunk->map[runcur_pageind];
-
-				/* Insert runcur. */
-				arena_run_tree_insert(&bin->runs,
-				    runcur_mapelm);
-			}
-			bin->runcur = run;
-		} else {
-			size_t run_pageind = (((uintptr_t)run -
-			    (uintptr_t)chunk)) >> PAGE_SHIFT;
-			arena_chunk_map_t *run_mapelm =
-			    &chunk->map[run_pageind];
-
-			assert(arena_run_tree_search(&bin->runs, run_mapelm) ==
-			    NULL);
-			arena_run_tree_insert(&bin->runs, run_mapelm);
-		}
-	}
-
-#ifdef MALLOC_STATS
-	if (size <= small_maxclass) {
-		arena->stats.allocated_small -= size;
-		arena->stats.ndalloc_small++;
-	} else {
-		arena->stats.allocated_medium -= size;
-		arena->stats.ndalloc_medium++;
-	}
-#endif
-}
-
-static void
-arena_dalloc_bin_run(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run,
-    arena_bin_t *bin)
-{
-	size_t run_ind;
-
-	/* Deallocate run. */
-	if (run == bin->runcur)
-		bin->runcur = NULL;
-	else if (bin->nregs != 1) {
-		size_t run_pageind = (((uintptr_t)run -
-		    (uintptr_t)chunk)) >> PAGE_SHIFT;
-		arena_chunk_map_t *run_mapelm =
-		    &chunk->map[run_pageind];
-		/*
-		 * This block's conditional is necessary because if the
-		 * run only contains one region, then it never gets
-		 * inserted into the non-full runs tree.
-		 */
-		arena_run_tree_remove(&bin->runs, run_mapelm);
-	}
-	/*
-	 * Mark the first page as dirty.  The dirty bit for every other page in
-	 * the run is already properly set, which means we can call
-	 * arena_run_dalloc(..., false), thus potentially avoiding the needless
-	 * creation of many dirty pages.
-	 */
-	run_ind = (size_t)(((uintptr_t)run - (uintptr_t)chunk) >> PAGE_SHIFT);
-	assert((chunk->map[run_ind].bits & CHUNK_MAP_DIRTY) == 0);
-	chunk->map[run_ind].bits |= CHUNK_MAP_DIRTY;
-	chunk->ndirty++;
-	arena->ndirty++;
-
-#ifdef MALLOC_DEBUG
-	run->magic = 0;
-#endif
-	arena_run_dalloc(arena, run, false);
-#ifdef MALLOC_STATS
-	bin->stats.curruns--;
-#endif
-
-	if (chunk->dirtied == false) {
-		arena_chunk_tree_dirty_insert(&arena->chunks_dirty, chunk);
-		chunk->dirtied = true;
-	}
-	/* Enforce opt_lg_dirty_mult. */
-	if (opt_lg_dirty_mult >= 0 && (arena->nactive >> opt_lg_dirty_mult) <
-	    arena->ndirty)
-		arena_purge(arena);
-}
-
-#ifdef MALLOC_STATS
-static void
-arena_stats_print(arena_t *arena)
-{
-
-	malloc_printf("dirty pages: %zu:%zu active:dirty, %"PRIu64" sweep%s,"
-	    " %"PRIu64" madvise%s, %"PRIu64" purged\n",
-	    arena->nactive, arena->ndirty,
-	    arena->stats.npurge, arena->stats.npurge == 1 ? "" : "s",
-	    arena->stats.nmadvise, arena->stats.nmadvise == 1 ? "" : "s",
-	    arena->stats.purged);
-
-	malloc_printf("            allocated      nmalloc      ndalloc\n");
-	malloc_printf("small:   %12zu %12"PRIu64" %12"PRIu64"\n",
-	    arena->stats.allocated_small, arena->stats.nmalloc_small,
-	    arena->stats.ndalloc_small);
-	malloc_printf("medium:  %12zu %12"PRIu64" %12"PRIu64"\n",
-	    arena->stats.allocated_medium, arena->stats.nmalloc_medium,
-	    arena->stats.ndalloc_medium);
-	malloc_printf("large:   %12zu %12"PRIu64" %12"PRIu64"\n",
-	    arena->stats.allocated_large, arena->stats.nmalloc_large,
-	    arena->stats.ndalloc_large);
-	malloc_printf("total:   %12zu %12"PRIu64" %12"PRIu64"\n",
-	    arena->stats.allocated_small + arena->stats.allocated_medium +
-	    arena->stats.allocated_large, arena->stats.nmalloc_small +
-	    arena->stats.nmalloc_medium + arena->stats.nmalloc_large,
-	    arena->stats.ndalloc_small + arena->stats.ndalloc_medium +
-	    arena->stats.ndalloc_large);
-	malloc_printf("mapped:  %12zu\n", arena->stats.mapped);
-
-	if (arena->stats.nmalloc_small + arena->stats.nmalloc_medium > 0) {
-		unsigned i, gap_start;
-#ifdef MALLOC_TCACHE
-		malloc_printf("bins:     bin    size regs pgs  requests    "
-		    "nfills  nflushes   newruns    reruns maxruns curruns\n");
-#else
-		malloc_printf("bins:     bin    size regs pgs  requests   "
-		    "newruns    reruns maxruns curruns\n");
-#endif
-		for (i = 0, gap_start = UINT_MAX; i < nbins; i++) {
-			if (arena->bins[i].stats.nruns == 0) {
-				if (gap_start == UINT_MAX)
-					gap_start = i;
-			} else {
-				if (gap_start != UINT_MAX) {
-					if (i > gap_start + 1) {
-						/*
-						 * Gap of more than one size
-						 * class.
-						 */
-						malloc_printf("[%u..%u]\n",
-						    gap_start, i - 1);
-					} else {
-						/* Gap of one size class. */
-						malloc_printf("[%u]\n",
-						    gap_start);
-					}
-					gap_start = UINT_MAX;
-				}
-				malloc_printf(
-				    "%13u %1s %5u %4u %3u %9"PRIu64" %9"PRIu64
-#ifdef MALLOC_TCACHE
-				    " %9"PRIu64" %9"PRIu64
-#endif
-				    " %9"PRIu64" %7zu %7zu\n",
-				    i,
-				    i < ntbins ? "T" : i < ntbins + nqbins ?
-				    "Q" : i < ntbins + nqbins + ncbins ? "C" :
-				    i < ntbins + nqbins + ncbins + nsbins ? "S"
-				    : "M",
-				    arena->bins[i].reg_size,
-				    arena->bins[i].nregs,
-				    arena->bins[i].run_size >> PAGE_SHIFT,
-				    arena->bins[i].stats.nrequests,
-#ifdef MALLOC_TCACHE
-				    arena->bins[i].stats.nfills,
-				    arena->bins[i].stats.nflushes,
-#endif
-				    arena->bins[i].stats.nruns,
-				    arena->bins[i].stats.reruns,
-				    arena->bins[i].stats.highruns,
-				    arena->bins[i].stats.curruns);
-			}
-		}
-		if (gap_start != UINT_MAX) {
-			if (i > gap_start + 1) {
-				/* Gap of more than one size class. */
-				malloc_printf("[%u..%u]\n", gap_start, i - 1);
-			} else {
-				/* Gap of one size class. */
-				malloc_printf("[%u]\n", gap_start);
-			}
-		}
-	}
-
-	if (arena->stats.nmalloc_large > 0) {
-		size_t i;
-		ssize_t gap_start;
-		size_t nlclasses = (chunksize - PAGE_SIZE) >> PAGE_SHIFT;
-
-		malloc_printf(
-		    "large:   size pages nrequests   maxruns   curruns\n");
-
-		for (i = 0, gap_start = -1; i < nlclasses; i++) {
-			if (arena->stats.lstats[i].nrequests == 0) {
-				if (gap_start == -1)
-					gap_start = i;
-			} else {
-				if (gap_start != -1) {
-					malloc_printf("[%zu]\n", i - gap_start);
-					gap_start = -1;
-				}
-				malloc_printf(
-				    "%13zu %5zu %9"PRIu64" %9zu %9zu\n",
-				    (i+1) << PAGE_SHIFT, i+1,
-				    arena->stats.lstats[i].nrequests,
-				    arena->stats.lstats[i].highruns,
-				    arena->stats.lstats[i].curruns);
-			}
-		}
-		if (gap_start != -1)
-			malloc_printf("[%zu]\n", i - gap_start);
-	}
-}
-#endif
-
-static void
-stats_print_atexit(void)
-{
-
-#if (defined(MALLOC_TCACHE) && defined(MALLOC_STATS))
-	unsigned i;
-
-	/*
-	 * Merge stats from extant threads.  This is racy, since individual
-	 * threads do not lock when recording tcache stats events.  As a
-	 * consequence, the final stats may be slightly out of date by the time
-	 * they are reported, if other threads continue to allocate.
-	 */
-	for (i = 0; i < narenas; i++) {
-		arena_t *arena = arenas[i];
-		if (arena != NULL) {
-			tcache_t *tcache;
-
-			malloc_spin_lock(&arena->lock);
-			ql_foreach(tcache, &arena->tcache_ql, link) {
-				tcache_stats_merge(tcache, arena);
-			}
-			malloc_spin_unlock(&arena->lock);
-		}
-	}
-#endif
-	malloc_stats_print();
-}
-
-#ifdef MALLOC_TCACHE
-static void
-tcache_bin_flush(tcache_bin_t *tbin, size_t binind, unsigned rem)
-{
-	arena_chunk_t *chunk;
-	arena_t *arena;
-	void *ptr;
-	unsigned i, ndeferred, ncached;
-
-	for (ndeferred = tbin->ncached - rem; ndeferred > 0;) {
-		ncached = ndeferred;
-		/* Lock the arena associated with the first object. */
-		chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(tbin->slots[0]);
-		arena = chunk->arena;
-		malloc_spin_lock(&arena->lock);
-		/* Deallocate every object that belongs to the locked arena. */
-		for (i = ndeferred = 0; i < ncached; i++) {
-			ptr = tbin->slots[i];
-			chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
-			if (chunk->arena == arena) {
-				size_t pageind = (((uintptr_t)ptr -
-				    (uintptr_t)chunk) >> PAGE_SHIFT);
-				arena_chunk_map_t *mapelm =
-				    &chunk->map[pageind];
-				arena_dalloc_bin(arena, chunk, ptr, mapelm);
-			} else {
-				/*
-				 * This object was allocated via a different
-				 * arena than the one that is currently locked.
-				 * Stash the object, so that it can be handled
-				 * in a future pass.
-				 */
-				tbin->slots[ndeferred] = ptr;
-				ndeferred++;
-			}
-		}
-#ifdef MALLOC_STATS
-		arena->bins[binind].stats.nflushes++;
-		{
-			arena_bin_t *bin = &arena->bins[binind];
-			bin->stats.nrequests += tbin->tstats.nrequests;
-			if (bin->reg_size <= small_maxclass) {
-				arena->stats.nmalloc_small +=
-				    tbin->tstats.nrequests;
-			} else {
-				arena->stats.nmalloc_medium +=
-				    tbin->tstats.nrequests;
-			}
-			tbin->tstats.nrequests = 0;
-		}
-#endif
-		malloc_spin_unlock(&arena->lock);
-	}
-
-	if (rem > 0) {
-		/*
-		 * Shift the remaining valid pointers to the base of the slots
-		 * array.
-		 */
-		memmove(&tbin->slots[0], &tbin->slots[tbin->ncached - rem],
-		    rem * sizeof(void *));
-	}
-	tbin->ncached = rem;
-}
-
-static inline void
-tcache_dalloc(tcache_t *tcache, void *ptr)
-{
-	arena_t *arena;
-	arena_chunk_t *chunk;
-	arena_run_t *run;
-	arena_bin_t *bin;
-	tcache_bin_t *tbin;
-	size_t pageind, binind;
-	arena_chunk_map_t *mapelm;
-
-	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
-	arena = chunk->arena;
-	pageind = (((uintptr_t)ptr - (uintptr_t)chunk) >> PAGE_SHIFT);
-	mapelm = &chunk->map[pageind];
-	run = (arena_run_t *)((uintptr_t)chunk + (uintptr_t)((pageind -
-	    ((mapelm->bits & CHUNK_MAP_PG_MASK) >> CHUNK_MAP_PG_SHIFT)) <<
-	    PAGE_SHIFT));
-	assert(run->magic == ARENA_RUN_MAGIC);
-	bin = run->bin;
-	binind = ((uintptr_t)bin - (uintptr_t)&arena->bins) /
-	    sizeof(arena_bin_t);
-	assert(binind < nbins);
-
-	if (opt_junk)
-		memset(ptr, 0x5a, arena->bins[binind].reg_size);
-
-	tbin = tcache->tbins[binind];
-	if (tbin == NULL) {
-		tbin = tcache_bin_create(choose_arena());
-		if (tbin == NULL) {
-			malloc_spin_lock(&arena->lock);
-			arena_dalloc_bin(arena, chunk, ptr, mapelm);
-			malloc_spin_unlock(&arena->lock);
-			return;
-		}
-		tcache->tbins[binind] = tbin;
-	}
-
-	if (tbin->ncached == tcache_nslots)
-		tcache_bin_flush(tbin, binind, (tcache_nslots >> 1));
-	assert(tbin->ncached < tcache_nslots);
-	tbin->slots[tbin->ncached] = ptr;
-	tbin->ncached++;
-	if (tbin->ncached > tbin->high_water)
-		tbin->high_water = tbin->ncached;
-
-	tcache_event(tcache);
-}
-#endif
-
-static void
-arena_dalloc_large(arena_t *arena, arena_chunk_t *chunk, void *ptr)
-{
-
-	/* Large allocation. */
-	malloc_spin_lock(&arena->lock);
-
-#ifndef MALLOC_STATS
-	if (opt_junk)
-#endif
-	{
-		size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >>
-		    PAGE_SHIFT;
-		size_t size = chunk->map[pageind].bits & ~PAGE_MASK;
-
-#ifdef MALLOC_STATS
-		if (opt_junk)
-#endif
-			memset(ptr, 0x5a, size);
-#ifdef MALLOC_STATS
-		arena->stats.ndalloc_large++;
-		arena->stats.allocated_large -= size;
-		arena->stats.lstats[(size >> PAGE_SHIFT) - 1].curruns--;
-#endif
-	}
-
-	arena_run_dalloc(arena, (arena_run_t *)ptr, true);
-	malloc_spin_unlock(&arena->lock);
-}
-
-static inline void
-arena_dalloc(arena_t *arena, arena_chunk_t *chunk, void *ptr)
-{
-	size_t pageind;
-	arena_chunk_map_t *mapelm;
-
-	assert(arena != NULL);
-	assert(arena->magic == ARENA_MAGIC);
-	assert(chunk->arena == arena);
-	assert(ptr != NULL);
-	assert(CHUNK_ADDR2BASE(ptr) != ptr);
-
-	pageind = (((uintptr_t)ptr - (uintptr_t)chunk) >> PAGE_SHIFT);
-	mapelm = &chunk->map[pageind];
-	assert((mapelm->bits & CHUNK_MAP_ALLOCATED) != 0);
-	if ((mapelm->bits & CHUNK_MAP_LARGE) == 0) {
-		/* Small allocation. */
-#ifdef MALLOC_TCACHE
-		if (__isthreaded && tcache_nslots) {
-			tcache_t *tcache = tcache_tls;
-			if ((uintptr_t)tcache > (uintptr_t)1)
-				tcache_dalloc(tcache, ptr);
-			else {
-				arena_dalloc_hard(arena, chunk, ptr, mapelm,
-				    tcache);
-			}
-		} else {
-#endif
-			malloc_spin_lock(&arena->lock);
-			arena_dalloc_bin(arena, chunk, ptr, mapelm);
-			malloc_spin_unlock(&arena->lock);
-#ifdef MALLOC_TCACHE
-		}
-#endif
-	} else
-		arena_dalloc_large(arena, chunk, ptr);
-}
-
-#ifdef MALLOC_TCACHE
-static void
-arena_dalloc_hard(arena_t *arena, arena_chunk_t *chunk, void *ptr,
-    arena_chunk_map_t *mapelm, tcache_t *tcache)
-{
-
-	if (tcache == NULL) {
-		tcache = tcache_create(arena);
-		if (tcache == NULL) {
-			malloc_spin_lock(&arena->lock);
-			arena_dalloc_bin(arena, chunk, ptr, mapelm);
-			malloc_spin_unlock(&arena->lock);
-		} else
-			tcache_dalloc(tcache, ptr);
-	} else {
-		/* This thread is currently exiting, so directly deallocate. */
-		assert(tcache == (void *)(uintptr_t)1);
-		malloc_spin_lock(&arena->lock);
-		arena_dalloc_bin(arena, chunk, ptr, mapelm);
-		malloc_spin_unlock(&arena->lock);
-	}
-}
-#endif
-
-static inline void
-idalloc(void *ptr)
-{
-	arena_chunk_t *chunk;
-
-	assert(ptr != NULL);
-
-	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
-	if (chunk != ptr)
-		arena_dalloc(chunk->arena, chunk, ptr);
-	else
-		huge_dalloc(ptr);
-}
-
-static void
-arena_ralloc_large_shrink(arena_t *arena, arena_chunk_t *chunk, void *ptr,
-    size_t size, size_t oldsize)
-{
-
-	assert(size < oldsize);
-
-	/*
-	 * Shrink the run, and make trailing pages available for other
-	 * allocations.
-	 */
-	malloc_spin_lock(&arena->lock);
-	arena_run_trim_tail(arena, chunk, (arena_run_t *)ptr, oldsize, size,
-	    true);
-#ifdef MALLOC_STATS
-	arena->stats.ndalloc_large++;
-	arena->stats.allocated_large -= oldsize;
-	arena->stats.lstats[(oldsize >> PAGE_SHIFT) - 1].curruns--;
-
-	arena->stats.nmalloc_large++;
-	arena->stats.allocated_large += size;
-	arena->stats.lstats[(size >> PAGE_SHIFT) - 1].nrequests++;
-	arena->stats.lstats[(size >> PAGE_SHIFT) - 1].curruns++;
-	if (arena->stats.lstats[(size >> PAGE_SHIFT) - 1].curruns >
-	    arena->stats.lstats[(size >> PAGE_SHIFT) - 1].highruns) {
-		arena->stats.lstats[(size >> PAGE_SHIFT) - 1].highruns =
-		    arena->stats.lstats[(size >> PAGE_SHIFT) - 1].curruns;
-	}
-#endif
-	malloc_spin_unlock(&arena->lock);
-}
-
-static bool
-arena_ralloc_large_grow(arena_t *arena, arena_chunk_t *chunk, void *ptr,
-    size_t size, size_t oldsize)
-{
-	size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> PAGE_SHIFT;
-	size_t npages = oldsize >> PAGE_SHIFT;
-
-	assert(oldsize == (chunk->map[pageind].bits & ~PAGE_MASK));
-
-	/* Try to extend the run. */
-	assert(size > oldsize);
-	malloc_spin_lock(&arena->lock);
-	if (pageind + npages < chunk_npages && (chunk->map[pageind+npages].bits
-	    & CHUNK_MAP_ALLOCATED) == 0 && (chunk->map[pageind+npages].bits &
-	    ~PAGE_MASK) >= size - oldsize) {
-		/*
-		 * The next run is available and sufficiently large.  Split the
-		 * following run, then merge the first part with the existing
-		 * allocation.
-		 */
-		arena_run_split(arena, (arena_run_t *)((uintptr_t)chunk +
-		    ((pageind+npages) << PAGE_SHIFT)), size - oldsize, true,
-		    false);
-
-		chunk->map[pageind].bits = size | CHUNK_MAP_LARGE |
-		    CHUNK_MAP_ALLOCATED;
-		chunk->map[pageind+npages].bits = CHUNK_MAP_LARGE |
-		    CHUNK_MAP_ALLOCATED;
-
-#ifdef MALLOC_STATS
-		arena->stats.ndalloc_large++;
-		arena->stats.allocated_large -= oldsize;
-		arena->stats.lstats[(oldsize >> PAGE_SHIFT) - 1].curruns--;
-
-		arena->stats.nmalloc_large++;
-		arena->stats.allocated_large += size;
-		arena->stats.lstats[(size >> PAGE_SHIFT) - 1].nrequests++;
-		arena->stats.lstats[(size >> PAGE_SHIFT) - 1].curruns++;
-		if (arena->stats.lstats[(size >> PAGE_SHIFT) - 1].curruns >
-		    arena->stats.lstats[(size >> PAGE_SHIFT) - 1].highruns) {
-			arena->stats.lstats[(size >> PAGE_SHIFT) - 1].highruns =
-			    arena->stats.lstats[(size >> PAGE_SHIFT) - 1].curruns;
-		}
-#endif
-		malloc_spin_unlock(&arena->lock);
-		return (false);
-	}
-	malloc_spin_unlock(&arena->lock);
-
-	return (true);
-}
-
-/*
- * Try to resize a large allocation, in order to avoid copying.  This will
- * always fail if growing an object, and the following run is already in use.
- */
-static bool
-arena_ralloc_large(void *ptr, size_t size, size_t oldsize)
-{
-	size_t psize;
-
-	psize = PAGE_CEILING(size);
-	if (psize == oldsize) {
-		/* Same size class. */
-		if (opt_junk && size < oldsize) {
-			memset((void *)((uintptr_t)ptr + size), 0x5a, oldsize -
-			    size);
-		}
-		return (false);
-	} else {
-		arena_chunk_t *chunk;
-		arena_t *arena;
-
-		chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
-		arena = chunk->arena;
-		assert(arena->magic == ARENA_MAGIC);
-
-		if (psize < oldsize) {
-			/* Fill before shrinking in order avoid a race. */
-			if (opt_junk) {
-				memset((void *)((uintptr_t)ptr + size), 0x5a,
-				    oldsize - size);
-			}
-			arena_ralloc_large_shrink(arena, chunk, ptr, psize,
-			    oldsize);
-			return (false);
-		} else {
-			bool ret = arena_ralloc_large_grow(arena, chunk, ptr,
-			    psize, oldsize);
-			if (ret == false && opt_zero) {
-				memset((void *)((uintptr_t)ptr + oldsize), 0,
-				    size - oldsize);
-			}
-			return (ret);
-		}
-	}
-}
-
-static void *
-arena_ralloc(void *ptr, size_t size, size_t oldsize)
-{
-	void *ret;
-	size_t copysize;
-
-	/*
-	 * Try to avoid moving the allocation.
-	 *
-	 * posix_memalign() can cause allocation of "large" objects that are
-	 * smaller than bin_maxclass (in order to meet alignment requirements).
-	 * Therefore, do not assume that (oldsize <= bin_maxclass) indicates
-	 * ptr refers to a bin-allocated object.
-	 */
-	if (oldsize <= arena_maxclass) {
-		if (arena_is_large(ptr) == false ) {
-			if (size <= small_maxclass) {
-				if (oldsize <= small_maxclass &&
-				    small_size2bin[size] ==
-				    small_size2bin[oldsize])
-					goto IN_PLACE;
-			} else if (size <= bin_maxclass) {
-				if (small_maxclass < oldsize && oldsize <=
-				    bin_maxclass && MEDIUM_CEILING(size) ==
-				    MEDIUM_CEILING(oldsize))
-					goto IN_PLACE;
-			}
-		} else {
-			assert(size <= arena_maxclass);
-			if (size > bin_maxclass) {
-				if (arena_ralloc_large(ptr, size, oldsize) ==
-				    false)
-					return (ptr);
-			}
-		}
-	}
-
-	/* Try to avoid moving the allocation. */
-	if (size <= small_maxclass) {
-		if (oldsize <= small_maxclass && small_size2bin[size] ==
-		    small_size2bin[oldsize])
-			goto IN_PLACE;
-	} else if (size <= bin_maxclass) {
-		if (small_maxclass < oldsize && oldsize <= bin_maxclass &&
-		    MEDIUM_CEILING(size) == MEDIUM_CEILING(oldsize))
-			goto IN_PLACE;
-	} else {
-		if (bin_maxclass < oldsize && oldsize <= arena_maxclass) {
-			assert(size > bin_maxclass);
-			if (arena_ralloc_large(ptr, size, oldsize) == false)
-				return (ptr);
-		}
-	}
-
-	/*
-	 * If we get here, then size and oldsize are different enough that we
-	 * need to move the object.  In that case, fall back to allocating new
-	 * space and copying.
-	 */
-	ret = arena_malloc(size, false);
-	if (ret == NULL)
-		return (NULL);
-
-	/* Junk/zero-filling were already done by arena_malloc(). */
-	copysize = (size < oldsize) ? size : oldsize;
-	memcpy(ret, ptr, copysize);
-	idalloc(ptr);
-	return (ret);
-IN_PLACE:
-	if (opt_junk && size < oldsize)
-		memset((void *)((uintptr_t)ptr + size), 0x5a, oldsize - size);
-	else if (opt_zero && size > oldsize)
-		memset((void *)((uintptr_t)ptr + oldsize), 0, size - oldsize);
-	return (ptr);
-}
-
-static inline void *
-iralloc(void *ptr, size_t size)
-{
-	size_t oldsize;
-
-	assert(ptr != NULL);
-	assert(size != 0);
-
-	oldsize = isalloc(ptr);
-
-	if (size <= arena_maxclass)
-		return (arena_ralloc(ptr, size, oldsize));
-	else
-		return (huge_ralloc(ptr, size, oldsize));
-}
-
-static bool
-arena_new(arena_t *arena, unsigned ind)
-{
-	unsigned i;
-	arena_bin_t *bin;
-	size_t prev_run_size;
-
-	if (malloc_spin_init(&arena->lock))
-		return (true);
-
-#ifdef MALLOC_STATS
-	memset(&arena->stats, 0, sizeof(arena_stats_t));
-	arena->stats.lstats = (malloc_large_stats_t *)base_alloc(
-	    sizeof(malloc_large_stats_t) * ((chunksize - PAGE_SIZE) >>
-	        PAGE_SHIFT));
-	if (arena->stats.lstats == NULL)
-		return (true);
-	memset(arena->stats.lstats, 0, sizeof(malloc_large_stats_t) *
-	    ((chunksize - PAGE_SIZE) >> PAGE_SHIFT));
-#  ifdef MALLOC_TCACHE
-	ql_new(&arena->tcache_ql);
-#  endif
-#endif
-
-	/* Initialize chunks. */
-	arena_chunk_tree_dirty_new(&arena->chunks_dirty);
-	arena->spare = NULL;
-
-	arena->nactive = 0;
-	arena->ndirty = 0;
-
-	arena_avail_tree_new(&arena->runs_avail);
-
-	/* Initialize bins. */
-	prev_run_size = PAGE_SIZE;
-
-	i = 0;
-#ifdef MALLOC_TINY
-	/* (2^n)-spaced tiny bins. */
-	for (; i < ntbins; i++) {
-		bin = &arena->bins[i];
-		bin->runcur = NULL;
-		arena_run_tree_new(&bin->runs);
-
-		bin->reg_size = (1U << (LG_TINY_MIN + i));
-
-		prev_run_size = arena_bin_run_size_calc(bin, prev_run_size);
-
-#ifdef MALLOC_STATS
-		memset(&bin->stats, 0, sizeof(malloc_bin_stats_t));
-#endif
-	}
-#endif
-
-	/* Quantum-spaced bins. */
-	for (; i < ntbins + nqbins; i++) {
-		bin = &arena->bins[i];
-		bin->runcur = NULL;
-		arena_run_tree_new(&bin->runs);
-
-		bin->reg_size = (i - ntbins + 1) << LG_QUANTUM;
-
-		prev_run_size = arena_bin_run_size_calc(bin, prev_run_size);
-
-#ifdef MALLOC_STATS
-		memset(&bin->stats, 0, sizeof(malloc_bin_stats_t));
-#endif
-	}
-
-	/* Cacheline-spaced bins. */
-	for (; i < ntbins + nqbins + ncbins; i++) {
-		bin = &arena->bins[i];
-		bin->runcur = NULL;
-		arena_run_tree_new(&bin->runs);
-
-		bin->reg_size = cspace_min + ((i - (ntbins + nqbins)) <<
-		    LG_CACHELINE);
-
-		prev_run_size = arena_bin_run_size_calc(bin, prev_run_size);
-
-#ifdef MALLOC_STATS
-		memset(&bin->stats, 0, sizeof(malloc_bin_stats_t));
-#endif
-	}
-
-	/* Subpage-spaced bins. */
-	for (; i < ntbins + nqbins + ncbins + nsbins; i++) {
-		bin = &arena->bins[i];
-		bin->runcur = NULL;
-		arena_run_tree_new(&bin->runs);
-
-		bin->reg_size = sspace_min + ((i - (ntbins + nqbins + ncbins))
-		    << LG_SUBPAGE);
-
-		prev_run_size = arena_bin_run_size_calc(bin, prev_run_size);
-
-#ifdef MALLOC_STATS
-		memset(&bin->stats, 0, sizeof(malloc_bin_stats_t));
-#endif
-	}
-
-	/* Medium bins. */
-	for (; i < nbins; i++) {
-		bin = &arena->bins[i];
-		bin->runcur = NULL;
-		arena_run_tree_new(&bin->runs);
-
-		bin->reg_size = medium_min + ((i - (ntbins + nqbins + ncbins +
-		    nsbins)) << lg_mspace);
-
-		prev_run_size = arena_bin_run_size_calc(bin, prev_run_size);
-
-#ifdef MALLOC_STATS
-		memset(&bin->stats, 0, sizeof(malloc_bin_stats_t));
-#endif
-	}
-
-#ifdef MALLOC_DEBUG
-	arena->magic = ARENA_MAGIC;
-#endif
-
-	return (false);
-}
-
-/* Create a new arena and insert it into the arenas array at index ind. */
-static arena_t *
-arenas_extend(unsigned ind)
-{
-	arena_t *ret;
-
-	/* Allocate enough space for trailing bins. */
-	ret = (arena_t *)base_alloc(sizeof(arena_t)
-	    + (sizeof(arena_bin_t) * (nbins - 1)));
-	if (ret != NULL && arena_new(ret, ind) == false) {
-		arenas[ind] = ret;
-		return (ret);
-	}
-	/* Only reached if there is an OOM error. */
-
-	/*
-	 * OOM here is quite inconvenient to propagate, since dealing with it
-	 * would require a check for failure in the fast path.  Instead, punt
-	 * by using arenas[0].  In practice, this is an extremely unlikely
-	 * failure.
-	 */
-	_malloc_message(_getprogname(),
-	    ": (malloc) Error initializing arena\n", "", "");
-	if (opt_abort)
-		abort();
-
-	return (arenas[0]);
-}
-
-#ifdef MALLOC_TCACHE
-static tcache_bin_t *
-tcache_bin_create(arena_t *arena)
-{
-	tcache_bin_t *ret;
-	size_t tsize;
-
-	tsize = sizeof(tcache_bin_t) + (sizeof(void *) * (tcache_nslots - 1));
-	if (tsize <= small_maxclass)
-		ret = (tcache_bin_t *)arena_malloc_small(arena, tsize, false);
-	else if (tsize <= bin_maxclass)
-		ret = (tcache_bin_t *)arena_malloc_medium(arena, tsize, false);
-	else
-		ret = (tcache_bin_t *)imalloc(tsize);
-	if (ret == NULL)
-		return (NULL);
-#ifdef MALLOC_STATS
-	memset(&ret->tstats, 0, sizeof(tcache_bin_stats_t));
-#endif
-	ret->low_water = 0;
-	ret->high_water = 0;
-	ret->ncached = 0;
-
-	return (ret);
-}
-
-static void
-tcache_bin_destroy(tcache_t *tcache, tcache_bin_t *tbin, unsigned binind)
-{
-	arena_t *arena;
-	arena_chunk_t *chunk;
-	size_t pageind, tsize;
-	arena_chunk_map_t *mapelm;
-
-	chunk = CHUNK_ADDR2BASE(tbin);
-	arena = chunk->arena;
-	pageind = (((uintptr_t)tbin - (uintptr_t)chunk) >> PAGE_SHIFT);
-	mapelm = &chunk->map[pageind];
-
-#ifdef MALLOC_STATS
-	if (tbin->tstats.nrequests != 0) {
-		arena_t *arena = tcache->arena;
-		arena_bin_t *bin = &arena->bins[binind];
-		malloc_spin_lock(&arena->lock);
-		bin->stats.nrequests += tbin->tstats.nrequests;
-		if (bin->reg_size <= small_maxclass)
-			arena->stats.nmalloc_small += tbin->tstats.nrequests;
-		else
-			arena->stats.nmalloc_medium += tbin->tstats.nrequests;
-		malloc_spin_unlock(&arena->lock);
-	}
-#endif
-
-	assert(tbin->ncached == 0);
-	tsize = sizeof(tcache_bin_t) + (sizeof(void *) * (tcache_nslots - 1));
-	if (tsize <= bin_maxclass) {
-		malloc_spin_lock(&arena->lock);
-		arena_dalloc_bin(arena, chunk, tbin, mapelm);
-		malloc_spin_unlock(&arena->lock);
-	} else
-		idalloc(tbin);
-}
-
-#ifdef MALLOC_STATS
-static void
-tcache_stats_merge(tcache_t *tcache, arena_t *arena)
-{
-	unsigned i;
-
-	/* Merge and reset tcache stats. */
-	for (i = 0; i < mbin0; i++) {
-		arena_bin_t *bin = &arena->bins[i];
-		tcache_bin_t *tbin = tcache->tbins[i];
-		if (tbin != NULL) {
-			bin->stats.nrequests += tbin->tstats.nrequests;
-			arena->stats.nmalloc_small += tbin->tstats.nrequests;
-			tbin->tstats.nrequests = 0;
-		}
-	}
-	for (; i < nbins; i++) {
-		arena_bin_t *bin = &arena->bins[i];
-		tcache_bin_t *tbin = tcache->tbins[i];
-		if (tbin != NULL) {
-			bin->stats.nrequests += tbin->tstats.nrequests;
-			arena->stats.nmalloc_medium += tbin->tstats.nrequests;
-			tbin->tstats.nrequests = 0;
-		}
-	}
-}
-#endif
-
-static tcache_t *
-tcache_create(arena_t *arena)
-{
-	tcache_t *tcache;
-
-	if (sizeof(tcache_t) + (sizeof(tcache_bin_t *) * (nbins - 1)) <=
-	    small_maxclass) {
-		tcache = (tcache_t *)arena_malloc_small(arena, sizeof(tcache_t)
-		    + (sizeof(tcache_bin_t *) * (nbins - 1)), true);
-	} else if (sizeof(tcache_t) + (sizeof(tcache_bin_t *) * (nbins - 1)) <=
-	    bin_maxclass) {
-		tcache = (tcache_t *)arena_malloc_medium(arena, sizeof(tcache_t)
-		    + (sizeof(tcache_bin_t *) * (nbins - 1)), true);
-	} else {
-		tcache = (tcache_t *)icalloc(sizeof(tcache_t) +
-		    (sizeof(tcache_bin_t *) * (nbins - 1)));
-	}
-
-	if (tcache == NULL)
-		return (NULL);
-
-#ifdef MALLOC_STATS
-	/* Link into list of extant tcaches. */
-	malloc_spin_lock(&arena->lock);
-	ql_elm_new(tcache, link);
-	ql_tail_insert(&arena->tcache_ql, tcache, link);
-	malloc_spin_unlock(&arena->lock);
-#endif
-
-	tcache->arena = arena;
-
-	tcache_tls = tcache;
-
-	return (tcache);
-}
-
-static void
-tcache_destroy(tcache_t *tcache)
-{
-	unsigned i;
-
-#ifdef MALLOC_STATS
-	/* Unlink from list of extant tcaches. */
-	malloc_spin_lock(&tcache->arena->lock);
-	ql_remove(&tcache->arena->tcache_ql, tcache, link);
-	tcache_stats_merge(tcache, tcache->arena);
-	malloc_spin_unlock(&tcache->arena->lock);
-#endif
-
-	for (i = 0; i < nbins; i++) {
-		tcache_bin_t *tbin = tcache->tbins[i];
-		if (tbin != NULL) {
-			tcache_bin_flush(tbin, i, 0);
-			tcache_bin_destroy(tcache, tbin, i);
-		}
-	}
-
-	if (arena_salloc(tcache) <= bin_maxclass) {
-		arena_chunk_t *chunk = CHUNK_ADDR2BASE(tcache);
-		arena_t *arena = chunk->arena;
-		size_t pageind = (((uintptr_t)tcache - (uintptr_t)chunk) >>
-		    PAGE_SHIFT);
-		arena_chunk_map_t *mapelm = &chunk->map[pageind];
-
-		malloc_spin_lock(&arena->lock);
-		arena_dalloc_bin(arena, chunk, tcache, mapelm);
-		malloc_spin_unlock(&arena->lock);
-	} else
-		idalloc(tcache);
-}
-#endif
-
-/*
- * End arena.
- */
-/******************************************************************************/
-/*
- * Begin general internal functions.
- */
-
-static void *
-huge_malloc(size_t size, bool zero)
-{
-	void *ret;
-	size_t csize;
-	extent_node_t *node;
-
-	/* Allocate one or more contiguous chunks for this request. */
-
-	csize = CHUNK_CEILING(size);
-	if (csize == 0) {
-		/* size is large enough to cause size_t wrap-around. */
-		return (NULL);
-	}
-
-	/* Allocate an extent node with which to track the chunk. */
-	node = base_node_alloc();
-	if (node == NULL)
-		return (NULL);
-
-	ret = chunk_alloc(csize, &zero);
-	if (ret == NULL) {
-		base_node_dealloc(node);
-		return (NULL);
-	}
-
-	/* Insert node into huge. */
-	node->addr = ret;
-	node->size = csize;
-
-	malloc_mutex_lock(&huge_mtx);
-	extent_tree_ad_insert(&huge, node);
-#ifdef MALLOC_STATS
-	huge_nmalloc++;
-	huge_allocated += csize;
-#endif
-	malloc_mutex_unlock(&huge_mtx);
-
-	if (zero == false) {
-		if (opt_junk)
-			memset(ret, 0xa5, csize);
-		else if (opt_zero)
-			memset(ret, 0, csize);
-	}
-
-	return (ret);
-}
-
-/* Only handles large allocations that require more than chunk alignment. */
-static void *
-huge_palloc(size_t alignment, size_t size)
-{
-	void *ret;
-	size_t alloc_size, chunk_size, offset;
-	extent_node_t *node;
-	bool zero;
-
-	/*
-	 * This allocation requires alignment that is even larger than chunk
-	 * alignment.  This means that huge_malloc() isn't good enough.
-	 *
-	 * Allocate almost twice as many chunks as are demanded by the size or
-	 * alignment, in order to assure the alignment can be achieved, then
-	 * unmap leading and trailing chunks.
-	 */
-	assert(alignment >= chunksize);
-
-	chunk_size = CHUNK_CEILING(size);
-
-	if (size >= alignment)
-		alloc_size = chunk_size + alignment - chunksize;
-	else
-		alloc_size = (alignment << 1) - chunksize;
-
-	/* Allocate an extent node with which to track the chunk. */
-	node = base_node_alloc();
-	if (node == NULL)
-		return (NULL);
-
-	zero = false;
-	ret = chunk_alloc(alloc_size, &zero);
-	if (ret == NULL) {
-		base_node_dealloc(node);
-		return (NULL);
-	}
-
-	offset = (uintptr_t)ret & (alignment - 1);
-	assert((offset & chunksize_mask) == 0);
-	assert(offset < alloc_size);
-	if (offset == 0) {
-		/* Trim trailing space. */
-		chunk_dealloc((void *)((uintptr_t)ret + chunk_size), alloc_size
-		    - chunk_size);
-	} else {
-		size_t trailsize;
-
-		/* Trim leading space. */
-		chunk_dealloc(ret, alignment - offset);
-
-		ret = (void *)((uintptr_t)ret + (alignment - offset));
-
-		trailsize = alloc_size - (alignment - offset) - chunk_size;
-		if (trailsize != 0) {
-		    /* Trim trailing space. */
-		    assert(trailsize < alloc_size);
-		    chunk_dealloc((void *)((uintptr_t)ret + chunk_size),
-			trailsize);
-		}
-	}
-
-	/* Insert node into huge. */
-	node->addr = ret;
-	node->size = chunk_size;
-
-	malloc_mutex_lock(&huge_mtx);
-	extent_tree_ad_insert(&huge, node);
-#ifdef MALLOC_STATS
-	huge_nmalloc++;
-	huge_allocated += chunk_size;
-#endif
-	malloc_mutex_unlock(&huge_mtx);
-
-	if (opt_junk)
-		memset(ret, 0xa5, chunk_size);
-	else if (opt_zero)
-		memset(ret, 0, chunk_size);
-
-	return (ret);
-}
-
-static void *
-huge_ralloc(void *ptr, size_t size, size_t oldsize)
-{
-	void *ret;
-	size_t copysize;
-
-	/* Avoid moving the allocation if the size class would not change. */
-	if (oldsize > arena_maxclass &&
-	    CHUNK_CEILING(size) == CHUNK_CEILING(oldsize)) {
-		if (opt_junk && size < oldsize) {
-			memset((void *)((uintptr_t)ptr + size), 0x5a, oldsize
-			    - size);
-		} else if (opt_zero && size > oldsize) {
-			memset((void *)((uintptr_t)ptr + oldsize), 0, size
-			    - oldsize);
-		}
-		return (ptr);
-	}
-
-	/*
-	 * If we get here, then size and oldsize are different enough that we
-	 * need to use a different size class.  In that case, fall back to
-	 * allocating new space and copying.
-	 */
-	ret = huge_malloc(size, false);
-	if (ret == NULL)
-		return (NULL);
-
-	copysize = (size < oldsize) ? size : oldsize;
-	memcpy(ret, ptr, copysize);
-	idalloc(ptr);
-	return (ret);
-}
-
-static void
-huge_dalloc(void *ptr)
-{
-	extent_node_t *node, key;
-
-	malloc_mutex_lock(&huge_mtx);
-
-	/* Extract from tree of huge allocations. */
-	key.addr = ptr;
-	node = extent_tree_ad_search(&huge, &key);
-	assert(node != NULL);
-	assert(node->addr == ptr);
-	extent_tree_ad_remove(&huge, node);
-
-#ifdef MALLOC_STATS
-	huge_ndalloc++;
-	huge_allocated -= node->size;
-#endif
-
-	malloc_mutex_unlock(&huge_mtx);
-
-	/* Unmap chunk. */
-#ifdef MALLOC_DSS
-	if (opt_dss && opt_junk)
-		memset(node->addr, 0x5a, node->size);
-#endif
-	chunk_dealloc(node->addr, node->size);
-
-	base_node_dealloc(node);
-}
-
-static void
-malloc_stats_print(void)
-{
-	char s[UMAX2S_BUFSIZE];
-
-	_malloc_message("___ Begin malloc statistics ___\n", "", "", "");
-	_malloc_message("Assertions ",
-#ifdef NDEBUG
-	    "disabled",
-#else
-	    "enabled",
-#endif
-	    "\n", "");
-	_malloc_message("Boolean MALLOC_OPTIONS: ", opt_abort ? "A" : "a", "", "");
-#ifdef MALLOC_DSS
-	_malloc_message(opt_dss ? "D" : "d", "", "", "");
-#endif
-	_malloc_message(opt_junk ? "J" : "j", "", "", "");
-#ifdef MALLOC_DSS
-	_malloc_message(opt_mmap ? "M" : "m", "", "", "");
-#endif
-	_malloc_message("P", "", "", "");
-	_malloc_message(opt_utrace ? "U" : "u", "", "", "");
-	_malloc_message(opt_sysv ? "V" : "v", "", "", "");
-	_malloc_message(opt_xmalloc ? "X" : "x", "", "", "");
-	_malloc_message(opt_zero ? "Z" : "z", "", "", "");
-	_malloc_message("\n", "", "", "");
-
-	_malloc_message("CPUs: ", umax2s(ncpus, 10, s), "\n", "");
-	_malloc_message("Max arenas: ", umax2s(narenas, 10, s), "\n", "");
-	_malloc_message("Pointer size: ", umax2s(sizeof(void *), 10, s), "\n", "");
-	_malloc_message("Quantum size: ", umax2s(QUANTUM, 10, s), "\n", "");
-	_malloc_message("Cacheline size (assumed): ",
-	    umax2s(CACHELINE, 10, s), "\n", "");
-	_malloc_message("Subpage spacing: ", umax2s(SUBPAGE, 10, s), "\n", "");
-	_malloc_message("Medium spacing: ", umax2s((1U << lg_mspace), 10, s), "\n",
-	    "");
-#ifdef MALLOC_TINY
-	_malloc_message("Tiny 2^n-spaced sizes: [", umax2s((1U << LG_TINY_MIN), 10,
-	    s), "..", "");
-	_malloc_message(umax2s((qspace_min >> 1), 10, s), "]\n", "", "");
-#endif
-	_malloc_message("Quantum-spaced sizes: [", umax2s(qspace_min, 10, s), "..",
-	    "");
-	_malloc_message(umax2s(qspace_max, 10, s), "]\n", "", "");
-	_malloc_message("Cacheline-spaced sizes: [",
-	    umax2s(cspace_min, 10, s), "..", "");
-	_malloc_message(umax2s(cspace_max, 10, s), "]\n", "", "");
-	_malloc_message("Subpage-spaced sizes: [", umax2s(sspace_min, 10, s), "..",
-	    "");
-	_malloc_message(umax2s(sspace_max, 10, s), "]\n", "", "");
-	_malloc_message("Medium sizes: [", umax2s(medium_min, 10, s), "..", "");
-	_malloc_message(umax2s(medium_max, 10, s), "]\n", "", "");
-	if (opt_lg_dirty_mult >= 0) {
-		_malloc_message("Min active:dirty page ratio per arena: ",
-		    umax2s((1U << opt_lg_dirty_mult), 10, s), ":1\n", "");
-	} else {
-		_malloc_message("Min active:dirty page ratio per arena: N/A\n", "",
-		    "", "");
-	}
-#ifdef MALLOC_TCACHE
-	_malloc_message("Thread cache slots per size class: ",
-	    tcache_nslots ? umax2s(tcache_nslots, 10, s) : "N/A", "\n", "");
-	_malloc_message("Thread cache GC sweep interval: ",
-	    (tcache_nslots && tcache_gc_incr > 0) ?
-	    umax2s((1U << opt_lg_tcache_gc_sweep), 10, s) : "N/A", "", "");
-	_malloc_message(" (increment interval: ",
-	    (tcache_nslots && tcache_gc_incr > 0) ?  umax2s(tcache_gc_incr, 10, s)
-	    : "N/A", ")\n", "");
-#endif
-	_malloc_message("Chunk size: ", umax2s(chunksize, 10, s), "", "");
-	_malloc_message(" (2^", umax2s(opt_lg_chunk, 10, s), ")\n", "");
-
-#ifdef MALLOC_STATS
-	{
-		size_t allocated, mapped;
-		unsigned i;
-		arena_t *arena;
-
-		/* Calculate and print allocated/mapped stats. */
-
-		/* arenas. */
-		for (i = 0, allocated = 0; i < narenas; i++) {
-			if (arenas[i] != NULL) {
-				malloc_spin_lock(&arenas[i]->lock);
-				allocated += arenas[i]->stats.allocated_small;
-				allocated += arenas[i]->stats.allocated_large;
-				malloc_spin_unlock(&arenas[i]->lock);
-			}
-		}
-
-		/* huge/base. */
-		malloc_mutex_lock(&huge_mtx);
-		allocated += huge_allocated;
-		mapped = stats_chunks.curchunks * chunksize;
-		malloc_mutex_unlock(&huge_mtx);
-
-		malloc_mutex_lock(&base_mtx);
-		mapped += base_mapped;
-		malloc_mutex_unlock(&base_mtx);
-
-		malloc_printf("Allocated: %zu, mapped: %zu\n", allocated,
-		    mapped);
-
-		/* Print chunk stats. */
-		{
-			chunk_stats_t chunks_stats;
-
-			malloc_mutex_lock(&huge_mtx);
-			chunks_stats = stats_chunks;
-			malloc_mutex_unlock(&huge_mtx);
-
-			malloc_printf("chunks: nchunks   "
-			    "highchunks    curchunks\n");
-			malloc_printf("  %13"PRIu64"%13zu%13zu\n",
-			    chunks_stats.nchunks, chunks_stats.highchunks,
-			    chunks_stats.curchunks);
-		}
-
-		/* Print chunk stats. */
-		malloc_printf(
-		    "huge: nmalloc      ndalloc    allocated\n");
-		malloc_printf(" %12"PRIu64" %12"PRIu64" %12zu\n", huge_nmalloc,
-		    huge_ndalloc, huge_allocated);
-
-		/* Print stats for each arena. */
-		for (i = 0; i < narenas; i++) {
-			arena = arenas[i];
-			if (arena != NULL) {
-				malloc_printf("\narenas[%u]:\n", i);
-				malloc_spin_lock(&arena->lock);
-				arena_stats_print(arena);
-				malloc_spin_unlock(&arena->lock);
-			}
-		}
-	}
-#endif /* #ifdef MALLOC_STATS */
-	_malloc_message("--- End malloc statistics ---\n", "", "", "");
-}
-
-#ifdef MALLOC_DEBUG
-static void
-small_size2bin_validate(void)
-{
-	size_t i, size, binind;
-
-	assert(small_size2bin[0] == 0xffU);
-	i = 1;
-#  ifdef MALLOC_TINY
-	/* Tiny. */
-	for (; i < (1U << LG_TINY_MIN); i++) {
-		size = pow2_ceil(1U << LG_TINY_MIN);
-		binind = ffs((int)(size >> (LG_TINY_MIN + 1)));
-		assert(small_size2bin[i] == binind);
-	}
-	for (; i < qspace_min; i++) {
-		size = pow2_ceil(i);
-		binind = ffs((int)(size >> (LG_TINY_MIN + 1)));
-		assert(small_size2bin[i] == binind);
-	}
-#  endif
-	/* Quantum-spaced. */
-	for (; i <= qspace_max; i++) {
-		size = QUANTUM_CEILING(i);
-		binind = ntbins + (size >> LG_QUANTUM) - 1;
-		assert(small_size2bin[i] == binind);
-	}
-	/* Cacheline-spaced. */
-	for (; i <= cspace_max; i++) {
-		size = CACHELINE_CEILING(i);
-		binind = ntbins + nqbins + ((size - cspace_min) >>
-		    LG_CACHELINE);
-		assert(small_size2bin[i] == binind);
-	}
-	/* Sub-page. */
-	for (; i <= sspace_max; i++) {
-		size = SUBPAGE_CEILING(i);
-		binind = ntbins + nqbins + ncbins + ((size - sspace_min)
-		    >> LG_SUBPAGE);
-		assert(small_size2bin[i] == binind);
-	}
-}
-#endif
-
-static bool
-small_size2bin_init(void)
-{
-
-	if (opt_lg_qspace_max != LG_QSPACE_MAX_DEFAULT
-	    || opt_lg_cspace_max != LG_CSPACE_MAX_DEFAULT
-	    || sizeof(const_small_size2bin) != small_maxclass + 1)
-		return (small_size2bin_init_hard());
-
-	small_size2bin = const_small_size2bin;
-#ifdef MALLOC_DEBUG
-	assert(sizeof(const_small_size2bin) == small_maxclass + 1);
-	small_size2bin_validate();
-#endif
-	return (false);
-}
-
-static bool
-small_size2bin_init_hard(void)
-{
-	size_t i, size, binind;
-	uint8_t *custom_small_size2bin;
-
-	assert(opt_lg_qspace_max != LG_QSPACE_MAX_DEFAULT
-	    || opt_lg_cspace_max != LG_CSPACE_MAX_DEFAULT
-	    || sizeof(const_small_size2bin) != small_maxclass + 1);
-
-	custom_small_size2bin = (uint8_t *)base_alloc(small_maxclass + 1);
-	if (custom_small_size2bin == NULL)
-		return (true);
-
-	custom_small_size2bin[0] = 0xffU;
-	i = 1;
-#ifdef MALLOC_TINY
-	/* Tiny. */
-	for (; i < (1U << LG_TINY_MIN); i++) {
-		size = pow2_ceil(1U << LG_TINY_MIN);
-		binind = ffs((int)(size >> (LG_TINY_MIN + 1)));
-		custom_small_size2bin[i] = binind;
-	}
-	for (; i < qspace_min; i++) {
-		size = pow2_ceil(i);
-		binind = ffs((int)(size >> (LG_TINY_MIN + 1)));
-		custom_small_size2bin[i] = binind;
-	}
-#endif
-	/* Quantum-spaced. */
-	for (; i <= qspace_max; i++) {
-		size = QUANTUM_CEILING(i);
-		binind = ntbins + (size >> LG_QUANTUM) - 1;
-		custom_small_size2bin[i] = binind;
-	}
-	/* Cacheline-spaced. */
-	for (; i <= cspace_max; i++) {
-		size = CACHELINE_CEILING(i);
-		binind = ntbins + nqbins + ((size - cspace_min) >>
-		    LG_CACHELINE);
-		custom_small_size2bin[i] = binind;
-	}
-	/* Sub-page. */
-	for (; i <= sspace_max; i++) {
-		size = SUBPAGE_CEILING(i);
-		binind = ntbins + nqbins + ncbins + ((size - sspace_min) >>
-		    LG_SUBPAGE);
-		custom_small_size2bin[i] = binind;
-	}
-
-	small_size2bin = custom_small_size2bin;
-#ifdef MALLOC_DEBUG
-	small_size2bin_validate();
-#endif
-	return (false);
-}
-
-static unsigned
-malloc_ncpus(void)
-{
-	int mib[2];
-	unsigned ret;
-	int error;
-	size_t len;
-
-	error = _elf_aux_info(AT_NCPUS, &ret, sizeof(ret));
-	if (error != 0 || ret == 0) {
-		mib[0] = CTL_HW;
-		mib[1] = HW_NCPU;
-		len = sizeof(ret);
-		if (sysctl(mib, 2, &ret, &len, (void *)NULL, 0) == -1) {
-			/* Error. */
-			ret = 1;
-		}
-	}
-
-	return (ret);
-}
-
-/*
- * FreeBSD's pthreads implementation calls malloc(3), so the malloc
- * implementation has to take pains to avoid infinite recursion during
- * initialization.
- */
-static inline bool
-malloc_init(void)
-{
-
-	if (malloc_initialized == false)
-		return (malloc_init_hard());
-
-	return (false);
-}
-
-static bool
-malloc_init_hard(void)
-{
-	unsigned i;
-	int linklen;
-	char buf[PATH_MAX + 1];
-	const char *opts;
-
-	malloc_mutex_lock(&init_lock);
-	if (malloc_initialized) {
-		/*
-		 * Another thread initialized the allocator before this one
-		 * acquired init_lock.
-		 */
-		malloc_mutex_unlock(&init_lock);
-		return (false);
-	}
-
-	/* Get number of CPUs. */
-	ncpus = malloc_ncpus();
-
-	/*
-	 * Increase the chunk size to the largest page size that is greater
-	 * than the default chunk size and less than or equal to 4MB.
-	 */
-	{
-		size_t pagesizes[MAXPAGESIZES];
-		int k, nsizes;
-
-		nsizes = getpagesizes(pagesizes, MAXPAGESIZES);
-		for (k = 0; k < nsizes; k++)
-			if (pagesizes[k] <= (1LU << 22))
-				while ((1LU << opt_lg_chunk) < pagesizes[k])
-					opt_lg_chunk++;
-	}
-
-	for (i = 0; i < 3; i++) {
-		unsigned j;
-
-		/* Get runtime configuration. */
-		switch (i) {
-		case 0:
-			if ((linklen = readlink("/etc/malloc.conf", buf,
-						sizeof(buf) - 1)) != -1) {
-				/*
-				 * Use the contents of the "/etc/malloc.conf"
-				 * symbolic link's name.
-				 */
-				buf[linklen] = '\0';
-				opts = buf;
-			} else {
-				/* No configuration specified. */
-				buf[0] = '\0';
-				opts = buf;
-			}
-			break;
-		case 1:
-			if (issetugid() == 0 && (opts =
-			    getenv("MALLOC_OPTIONS")) != NULL) {
-				/*
-				 * Do nothing; opts is already initialized to
-				 * the value of the MALLOC_OPTIONS environment
-				 * variable.
-				 */
-			} else {
-				/* No configuration specified. */
-				buf[0] = '\0';
-				opts = buf;
-			}
-			break;
-		case 2:
-			if (_malloc_options != NULL) {
-				/*
-				 * Use options that were compiled into the
-				 * program.
-				 */
-				opts = _malloc_options;
-			} else {
-				/* No configuration specified. */
-				buf[0] = '\0';
-				opts = buf;
-			}
-			break;
-		default:
-			/* NOTREACHED */
-			assert(false);
-			buf[0] = '\0';
-			opts = buf;
-		}
-
-		for (j = 0; opts[j] != '\0'; j++) {
-			unsigned k, nreps;
-			bool nseen;
-
-			/* Parse repetition count, if any. */
-			for (nreps = 0, nseen = false;; j++, nseen = true) {
-				switch (opts[j]) {
-					case '0': case '1': case '2': case '3':
-					case '4': case '5': case '6': case '7':
-					case '8': case '9':
-						nreps *= 10;
-						nreps += opts[j] - '0';
-						break;
-					default:
-						goto MALLOC_OUT;
-				}
-			}
-MALLOC_OUT:
-			if (nseen == false)
-				nreps = 1;
-
-			for (k = 0; k < nreps; k++) {
-				switch (opts[j]) {
-				case 'a':
-					opt_abort = false;
-					break;
-				case 'A':
-					opt_abort = true;
-					break;
-				case 'c':
-					if (opt_lg_cspace_max - 1 >
-					    opt_lg_qspace_max &&
-					    opt_lg_cspace_max >
-					    LG_CACHELINE)
-						opt_lg_cspace_max--;
-					break;
-				case 'C':
-					if (opt_lg_cspace_max < PAGE_SHIFT
-					    - 1)
-						opt_lg_cspace_max++;
-					break;
-				case 'd':
-#ifdef MALLOC_DSS
-					opt_dss = false;
-#endif
-					break;
-				case 'D':
-#ifdef MALLOC_DSS
-					opt_dss = true;
-#endif
-					break;
-				case 'e':
-					if (opt_lg_medium_max > PAGE_SHIFT)
-						opt_lg_medium_max--;
-					break;
-				case 'E':
-					if (opt_lg_medium_max + 1 <
-					    opt_lg_chunk)
-						opt_lg_medium_max++;
-					break;
-				case 'f':
-					if (opt_lg_dirty_mult + 1 <
-					    (sizeof(size_t) << 3))
-						opt_lg_dirty_mult++;
-					break;
-				case 'F':
-					if (opt_lg_dirty_mult >= 0)
-						opt_lg_dirty_mult--;
-					break;
-#ifdef MALLOC_TCACHE
-				case 'g':
-					if (opt_lg_tcache_gc_sweep >= 0)
-						opt_lg_tcache_gc_sweep--;
-					break;
-				case 'G':
-					if (opt_lg_tcache_gc_sweep + 1 <
-					    (sizeof(size_t) << 3))
-						opt_lg_tcache_gc_sweep++;
-					break;
-				case 'h':
-					if (opt_lg_tcache_nslots > 0)
-						opt_lg_tcache_nslots--;
-					break;
-				case 'H':
-					if (opt_lg_tcache_nslots + 1 <
-					    (sizeof(size_t) << 3))
-						opt_lg_tcache_nslots++;
-					break;
-#endif
-				case 'j':
-					opt_junk = false;
-					break;
-				case 'J':
-					opt_junk = true;
-					break;
-				case 'k':
-					/*
-					 * Chunks always require at least one
-					 * header page, plus enough room to
-					 * hold a run for the largest medium
-					 * size class (one page more than the
-					 * size).
-					 */
-					if ((1U << (opt_lg_chunk - 1)) >=
-					    (2U << PAGE_SHIFT) + (1U <<
-					    opt_lg_medium_max))
-						opt_lg_chunk--;
-					break;
-				case 'K':
-					if (opt_lg_chunk + 1 <
-					    (sizeof(size_t) << 3))
-						opt_lg_chunk++;
-					break;
-				case 'm':
-#ifdef MALLOC_DSS
-					opt_mmap = false;
-#endif
-					break;
-				case 'M':
-#ifdef MALLOC_DSS
-					opt_mmap = true;
-#endif
-					break;
-				case 'n':
-					opt_narenas_lshift--;
-					break;
-				case 'N':
-					opt_narenas_lshift++;
-					break;
-				case 'p':
-					opt_stats_print = false;
-					break;
-				case 'P':
-					opt_stats_print = true;
-					break;
-				case 'q':
-					if (opt_lg_qspace_max > LG_QUANTUM)
-						opt_lg_qspace_max--;
-					break;
-				case 'Q':
-					if (opt_lg_qspace_max + 1 <
-					    opt_lg_cspace_max)
-						opt_lg_qspace_max++;
-					break;
-				case 'u':
-					opt_utrace = false;
-					break;
-				case 'U':
-					opt_utrace = true;
-					break;
-				case 'v':
-					opt_sysv = false;
-					break;
-				case 'V':
-					opt_sysv = true;
-					break;
-				case 'x':
-					opt_xmalloc = false;
-					break;
-				case 'X':
-					opt_xmalloc = true;
-					break;
-				case 'z':
-					opt_zero = false;
-					break;
-				case 'Z':
-					opt_zero = true;
-					break;
-				default: {
-					char cbuf[2];
-
-					cbuf[0] = opts[j];
-					cbuf[1] = '\0';
-					_malloc_message(_getprogname(),
-					    ": (malloc) Unsupported character "
-					    "in malloc options: '", cbuf,
-					    "'\n");
-				}
-				}
-			}
-		}
-	}
-
-#ifdef MALLOC_DSS
-	/* Make sure that there is some method for acquiring memory. */
-	if (opt_dss == false && opt_mmap == false)
-		opt_mmap = true;
-#endif
-	if (opt_stats_print) {
-		/* Print statistics at exit. */
-		atexit(stats_print_atexit);
-	}
-
-
-	/* Set variables according to the value of opt_lg_[qc]space_max. */
-	qspace_max = (1U << opt_lg_qspace_max);
-	cspace_min = CACHELINE_CEILING(qspace_max);
-	if (cspace_min == qspace_max)
-		cspace_min += CACHELINE;
-	cspace_max = (1U << opt_lg_cspace_max);
-	sspace_min = SUBPAGE_CEILING(cspace_max);
-	if (sspace_min == cspace_max)
-		sspace_min += SUBPAGE;
-	assert(sspace_min < PAGE_SIZE);
-	sspace_max = PAGE_SIZE - SUBPAGE;
-	medium_max = (1U << opt_lg_medium_max);
-
-#ifdef MALLOC_TINY
-	assert(LG_QUANTUM >= LG_TINY_MIN);
-#endif
-	assert(ntbins <= LG_QUANTUM);
-	nqbins = qspace_max >> LG_QUANTUM;
-	ncbins = ((cspace_max - cspace_min) >> LG_CACHELINE) + 1;
-	nsbins = ((sspace_max - sspace_min) >> LG_SUBPAGE) + 1;
-
-	/*
-	 * Compute medium size class spacing and the number of medium size
-	 * classes.  Limit spacing to no more than pagesize, but if possible
-	 * use the smallest spacing that does not exceed NMBINS_MAX medium size
-	 * classes.
-	 */
-	lg_mspace = LG_SUBPAGE;
-	nmbins = ((medium_max - medium_min) >> lg_mspace) + 1;
-	while (lg_mspace < PAGE_SHIFT && nmbins > NMBINS_MAX) {
-		lg_mspace = lg_mspace + 1;
-		nmbins = ((medium_max - medium_min) >> lg_mspace) + 1;
-	}
-	mspace_mask = (1U << lg_mspace) - 1U;
-
-	mbin0 = ntbins + nqbins + ncbins + nsbins;
-	nbins = mbin0 + nmbins;
-	/*
-	 * The small_size2bin lookup table uses uint8_t to encode each bin
-	 * index, so we cannot support more than 256 small size classes.  This
-	 * limit is difficult to exceed (not even possible with 16B quantum and
-	 * 4KiB pages), and such configurations are impractical, but
-	 * nonetheless we need to protect against this case in order to avoid
-	 * undefined behavior.
-	 */
-	if (mbin0 > 256) {
-	    char line_buf[UMAX2S_BUFSIZE];
-	    _malloc_message(_getprogname(),
-	        ": (malloc) Too many small size classes (",
-	        umax2s(mbin0, 10, line_buf), " > max 256)\n");
-	    abort();
-	}
-
-	if (small_size2bin_init()) {
-		malloc_mutex_unlock(&init_lock);
-		return (true);
-	}
-
-#ifdef MALLOC_TCACHE
-	if (opt_lg_tcache_nslots > 0) {
-		tcache_nslots = (1U << opt_lg_tcache_nslots);
-
-		/* Compute incremental GC event threshold. */
-		if (opt_lg_tcache_gc_sweep >= 0) {
-			tcache_gc_incr = ((1U << opt_lg_tcache_gc_sweep) /
-			    nbins) + (((1U << opt_lg_tcache_gc_sweep) % nbins ==
-			    0) ? 0 : 1);
-		} else
-			tcache_gc_incr = 0;
-	} else
-		tcache_nslots = 0;
-#endif
-
-	/* Set variables according to the value of opt_lg_chunk. */
-	chunksize = (1LU << opt_lg_chunk);
-	chunksize_mask = chunksize - 1;
-	chunk_npages = (chunksize >> PAGE_SHIFT);
-	{
-		size_t header_size;
-
-		/*
-		 * Compute the header size such that it is large enough to
-		 * contain the page map.
-		 */
-		header_size = sizeof(arena_chunk_t) +
-		    (sizeof(arena_chunk_map_t) * (chunk_npages - 1));
-		arena_chunk_header_npages = (header_size >> PAGE_SHIFT) +
-		    ((header_size & PAGE_MASK) != 0);
-	}
-	arena_maxclass = chunksize - (arena_chunk_header_npages <<
-	    PAGE_SHIFT);
-
-	UTRACE((void *)(intptr_t)(-1), 0, 0);
-
-#ifdef MALLOC_STATS
-	malloc_mutex_init(&chunks_mtx);
-	memset(&stats_chunks, 0, sizeof(chunk_stats_t));
-#endif
-
-	/* Various sanity checks that regard configuration. */
-	assert(chunksize >= PAGE_SIZE);
-
-	/* Initialize chunks data. */
-	malloc_mutex_init(&huge_mtx);
-	extent_tree_ad_new(&huge);
-#ifdef MALLOC_DSS
-	malloc_mutex_init(&dss_mtx);
-	dss_base = sbrk(0);
-	dss_prev = dss_base;
-	dss_max = dss_base;
-	extent_tree_szad_new(&dss_chunks_szad);
-	extent_tree_ad_new(&dss_chunks_ad);
-#endif
-#ifdef MALLOC_STATS
-	huge_nmalloc = 0;
-	huge_ndalloc = 0;
-	huge_allocated = 0;
-#endif
-
-	/* Initialize base allocation data structures. */
-#ifdef MALLOC_STATS
-	base_mapped = 0;
-#endif
-#ifdef MALLOC_DSS
-	/*
-	 * Allocate a base chunk here, since it doesn't actually have to be
-	 * chunk-aligned.  Doing this before allocating any other chunks allows
-	 * the use of space that would otherwise be wasted.
-	 */
-	if (opt_dss)
-		base_pages_alloc(0);
-#endif
-	base_nodes = NULL;
-	malloc_mutex_init(&base_mtx);
-
-	if (ncpus > 1) {
-		/*
-		 * For SMP systems, create more than one arena per CPU by
-		 * default.
-		 */
-#ifdef MALLOC_TCACHE
-		if (tcache_nslots) {
-			/*
-			 * Only large object allocation/deallocation is
-			 * guaranteed to acquire an arena mutex, so we can get
-			 * away with fewer arenas than without thread caching.
-			 */
-			opt_narenas_lshift += 1;
-		} else {
-#endif
-			/*
-			 * All allocations must acquire an arena mutex, so use
-			 * plenty of arenas.
-			 */
-			opt_narenas_lshift += 2;
-#ifdef MALLOC_TCACHE
-		}
-#endif
-	}
-
-	/* Determine how many arenas to use. */
-	narenas = ncpus;
-	if (opt_narenas_lshift > 0) {
-		if ((narenas << opt_narenas_lshift) > narenas)
-			narenas <<= opt_narenas_lshift;
-		/*
-		 * Make sure not to exceed the limits of what base_alloc() can
-		 * handle.
-		 */
-		if (narenas * sizeof(arena_t *) > chunksize)
-			narenas = chunksize / sizeof(arena_t *);
-	} else if (opt_narenas_lshift < 0) {
-		if ((narenas >> -opt_narenas_lshift) < narenas)
-			narenas >>= -opt_narenas_lshift;
-		/* Make sure there is at least one arena. */
-		if (narenas == 0)
-			narenas = 1;
-	}
-
-#ifdef NO_TLS
-	if (narenas > 1) {
-		static const unsigned primes[] = {1, 3, 5, 7, 11, 13, 17, 19,
-		    23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
-		    89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149,
-		    151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211,
-		    223, 227, 229, 233, 239, 241, 251, 257, 263};
-		unsigned nprimes, parenas;
-
-		/*
-		 * Pick a prime number of hash arenas that is more than narenas
-		 * so that direct hashing of pthread_self() pointers tends to
-		 * spread allocations evenly among the arenas.
-		 */
-		assert((narenas & 1) == 0); /* narenas must be even. */
-		nprimes = (sizeof(primes) >> LG_SIZEOF_INT);
-		parenas = primes[nprimes - 1]; /* In case not enough primes. */
-		for (i = 1; i < nprimes; i++) {
-			if (primes[i] > narenas) {
-				parenas = primes[i];
-				break;
-			}
-		}
-		narenas = parenas;
-	}
-#endif
-
-#ifndef NO_TLS
-	next_arena = 0;
-#endif
-
-	/* Allocate and initialize arenas. */
-	arenas = (arena_t **)base_alloc(sizeof(arena_t *) * narenas);
-	if (arenas == NULL) {
-		malloc_mutex_unlock(&init_lock);
-		return (true);
-	}
-	/*
-	 * Zero the array.  In practice, this should always be pre-zeroed,
-	 * since it was just mmap()ed, but let's be sure.
-	 */
-	memset(arenas, 0, sizeof(arena_t *) * narenas);
-
-	/*
-	 * Initialize one arena here.  The rest are lazily created in
-	 * choose_arena_hard().
-	 */
-	arenas_extend(0);
-	if (arenas[0] == NULL) {
-		malloc_mutex_unlock(&init_lock);
-		return (true);
-	}
-#ifndef NO_TLS
-	/*
-	 * Assign the initial arena to the initial thread, in order to avoid
-	 * spurious creation of an extra arena if the application switches to
-	 * threaded mode.
-	 */
-	arenas_map = arenas[0];
-#endif
-	malloc_spin_init(&arenas_lock);
-
-	malloc_initialized = true;
-	malloc_mutex_unlock(&init_lock);
-	return (false);
-}
-
-/*
- * End general internal functions.
- */
-/******************************************************************************/
-/*
- * Begin malloc(3)-compatible functions.
- */
-
-void *
-malloc(size_t size)
-{
-	void *ret;
-
-	if (malloc_init()) {
-		ret = NULL;
-		goto OOM;
-	}
-
-	if (size == 0) {
-		if (opt_sysv == false)
-			size = 1;
-		else {
-			if (opt_xmalloc) {
-				_malloc_message(_getprogname(),
-				    ": (malloc) Error in malloc(): "
-				    "invalid size 0\n", "", "");
-				abort();
-			}
-			ret = NULL;
-			goto RETURN;
-		}
-	}
-
-	ret = imalloc(size);
-
-OOM:
-	if (ret == NULL) {
-		if (opt_xmalloc) {
-			_malloc_message(_getprogname(),
-			    ": (malloc) Error in malloc(): out of memory\n", "",
-			    "");
-			abort();
-		}
-		errno = ENOMEM;
-	}
-
-RETURN:
-	UTRACE(0, size, ret);
-	return (ret);
-}
-
-int
-posix_memalign(void **memptr, size_t alignment, size_t size)
-{
-	int ret;
-	void *result;
-
-	if (malloc_init())
-		result = NULL;
-	else {
-		if (size == 0) {
-			if (opt_sysv == false)
-				size = 1;
-			else {
-				if (opt_xmalloc) {
-					_malloc_message(_getprogname(),
-					    ": (malloc) Error in "
-					    "posix_memalign(): invalid "
-					    "size 0\n", "", "");
-					abort();
-				}
-				result = NULL;
-				*memptr = NULL;
-				ret = 0;
-				goto RETURN;
-			}
-		}
-
-		/* Make sure that alignment is a large enough power of 2. */
-		if (((alignment - 1) & alignment) != 0
-		    || alignment < sizeof(void *)) {
-			if (opt_xmalloc) {
-				_malloc_message(_getprogname(),
-				    ": (malloc) Error in posix_memalign(): "
-				    "invalid alignment\n", "", "");
-				abort();
-			}
-			result = NULL;
-			ret = EINVAL;
-			goto RETURN;
-		}
-
-		result = ipalloc(alignment, size);
-	}
-
-	if (result == NULL) {
-		if (opt_xmalloc) {
-			_malloc_message(_getprogname(),
-			": (malloc) Error in posix_memalign(): out of memory\n",
-			"", "");
-			abort();
-		}
-		ret = ENOMEM;
-		goto RETURN;
-	}
-
-	*memptr = result;
-	ret = 0;
-
-RETURN:
-	UTRACE(0, size, result);
-	return (ret);
-}
-
-void *
-aligned_alloc(size_t alignment, size_t size)
-{
-	void *memptr;
-	int ret;
-
-	ret = posix_memalign(&memptr, alignment, size);
-	if (ret != 0) {
-		errno = ret;
-		return (NULL);
-	}
-	return (memptr);
-}
-
-void *
-calloc(size_t num, size_t size)
-{
-	void *ret;
-	size_t num_size;
-
-	if (malloc_init()) {
-		num_size = 0;
-		ret = NULL;
-		goto RETURN;
-	}
-
-	num_size = num * size;
-	if (num_size == 0) {
-		if ((opt_sysv == false) && ((num == 0) || (size == 0)))
-			num_size = 1;
-		else {
-			ret = NULL;
-			goto RETURN;
-		}
-	/*
-	 * Try to avoid division here.  We know that it isn't possible to
-	 * overflow during multiplication if neither operand uses any of the
-	 * most significant half of the bits in a size_t.
-	 */
-	} else if (((num | size) & (SIZE_T_MAX << (sizeof(size_t) << 2)))
-	    && (num_size / size != num)) {
-		/* size_t overflow. */
-		ret = NULL;
-		goto RETURN;
-	}
-
-	ret = icalloc(num_size);
-
-RETURN:
-	if (ret == NULL) {
-		if (opt_xmalloc) {
-			_malloc_message(_getprogname(),
-			    ": (malloc) Error in calloc(): out of memory\n", "",
-			    "");
-			abort();
-		}
-		errno = ENOMEM;
-	}
-
-	UTRACE(0, num_size, ret);
-	return (ret);
-}
-
-void *
-realloc(void *ptr, size_t size)
-{
-	void *ret;
-
-	if (size == 0) {
-		if (opt_sysv == false)
-			size = 1;
-		else {
-			if (ptr != NULL)
-				idalloc(ptr);
-			ret = NULL;
-			goto RETURN;
-		}
-	}
-
-	if (ptr != NULL) {
-		assert(malloc_initialized);
-
-		ret = iralloc(ptr, size);
-
-		if (ret == NULL) {
-			if (opt_xmalloc) {
-				_malloc_message(_getprogname(),
-				    ": (malloc) Error in realloc(): out of "
-				    "memory\n", "", "");
-				abort();
-			}
-			errno = ENOMEM;
-		}
-	} else {
-		if (malloc_init())
-			ret = NULL;
-		else
-			ret = imalloc(size);
-
-		if (ret == NULL) {
-			if (opt_xmalloc) {
-				_malloc_message(_getprogname(),
-				    ": (malloc) Error in realloc(): out of "
-				    "memory\n", "", "");
-				abort();
-			}
-			errno = ENOMEM;
-		}
-	}
-
-RETURN:
-	UTRACE(ptr, size, ret);
-	return (ret);
-}
-
-void
-free(void *ptr)
-{
-
-	UTRACE(ptr, 0, 0);
-	if (ptr != NULL) {
-		assert(malloc_initialized);
-
-		idalloc(ptr);
-	}
-}
-
-/*
- * End malloc(3)-compatible functions.
- */
-/******************************************************************************/
-/*
- * Begin non-standard functions.
- */
-
-size_t
-malloc_usable_size(const void *ptr)
-{
-
-	assert(ptr != NULL);
-
-	return (isalloc(ptr));
-}
-
-/*
- * End non-standard functions.
- */
-/******************************************************************************/
-/*
- * Begin library-private functions.
- */
-
-/*
- * We provide an unpublished interface in order to receive notifications from
- * the pthreads library whenever a thread exits.  This allows us to clean up
- * thread caches.
- */
-void
-_malloc_thread_cleanup(void)
-{
-
-#ifdef MALLOC_TCACHE
-	tcache_t *tcache = tcache_tls;
-
-	if (tcache != NULL) {
-		assert(tcache != (void *)(uintptr_t)1);
-		tcache_destroy(tcache);
-		tcache_tls = (void *)(uintptr_t)1;
-	}
-#endif
-}
-
-/*
- * The following functions are used by threading libraries for protection of
- * malloc during fork().  These functions are only called if the program is
- * running in threaded mode, so there is no need to check whether the program
- * is threaded here.
- */
-
-void
-_malloc_prefork(void)
-{
-	unsigned i;
-
-	/* Acquire all mutexes in a safe order. */
-	malloc_spin_lock(&arenas_lock);
-	for (i = 0; i < narenas; i++) {
-		if (arenas[i] != NULL)
-			malloc_spin_lock(&arenas[i]->lock);
-	}
-
-	malloc_mutex_lock(&base_mtx);
-
-	malloc_mutex_lock(&huge_mtx);
-
-#ifdef MALLOC_DSS
-	malloc_mutex_lock(&dss_mtx);
-#endif
-}
-
-void
-_malloc_postfork(void)
-{
-	unsigned i;
-
-	/* Release all mutexes, now that fork() has completed. */
-
-#ifdef MALLOC_DSS
-	malloc_mutex_unlock(&dss_mtx);
-#endif
-
-	malloc_mutex_unlock(&huge_mtx);
-
-	malloc_mutex_unlock(&base_mtx);
-
-	for (i = 0; i < narenas; i++) {
-		if (arenas[i] != NULL)
-			malloc_spin_unlock(&arenas[i]->lock);
-	}
-	malloc_spin_unlock(&arenas_lock);
-}
-
-/*
- * End library-private functions.
- */
-/******************************************************************************/
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdlib/ql.h
--- a/head/lib/libc/stdlib/ql.h	Sat Apr 21 21:30:13 2012 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,122 +0,0 @@
-/******************************************************************************
- *
- * Copyright (C) 2002 Jason Evans <jasone at FreeBSD.org>.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice(s), this list of conditions and the following disclaimer
- *    unmodified other than the allowable addition of one or more
- *    copyright notices.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice(s), this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************/
-
-#ifndef QL_H_
-#define	QL_H_
-
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/libc/stdlib/ql.h 203329 2010-01-31 23:16:10Z jasone $");
-
-/*
- * List definitions.
- */
-#define ql_head(a_type)							\
-struct {								\
-	a_type *qlh_first;						\
-}
-
-#define ql_head_initializer(a_head) {NULL}
-
-#define ql_elm(a_type)	qr(a_type)
-
-/* List functions. */
-#define ql_new(a_head) do {						\
-	(a_head)->qlh_first = NULL;					\
-} while (0)
-
-#define ql_elm_new(a_elm, a_field) qr_new((a_elm), a_field)
-
-#define ql_first(a_head) ((a_head)->qlh_first)
-
-#define ql_last(a_head, a_field)					\
-	((ql_first(a_head) != NULL)					\
-	    ? qr_prev(ql_first(a_head), a_field) : NULL)
-
-#define ql_next(a_head, a_elm, a_field)					\
-	((ql_last(a_head, a_field) != (a_elm))				\
-	    ? qr_next((a_elm), a_field)	: NULL)
-
-#define ql_prev(a_head, a_elm, a_field)					\
-	((ql_first(a_head) != (a_elm)) ? qr_prev((a_elm), a_field)	\
-				       : NULL)
-
-#define ql_before_insert(a_head, a_qlelm, a_elm, a_field) do {		\
-	qr_before_insert((a_qlelm), (a_elm), a_field);			\
-	if (ql_first(a_head) == (a_qlelm)) {				\
-		ql_first(a_head) = (a_elm);				\
-	}								\
-} while (0)
-
-#define ql_after_insert(a_qlelm, a_elm, a_field)			\
-	qr_after_insert((a_qlelm), (a_elm), a_field)
-
-#define ql_head_insert(a_head, a_elm, a_field) do {			\
-	if (ql_first(a_head) != NULL) {					\
-		qr_before_insert(ql_first(a_head), (a_elm), a_field);	\
-	}								\
-	ql_first(a_head) = (a_elm);					\
-} while (0)
-
-#define ql_tail_insert(a_head, a_elm, a_field) do {			\
-	if (ql_first(a_head) != NULL) {					\
-		qr_before_insert(ql_first(a_head), (a_elm), a_field);	\
-	}								\
-	ql_first(a_head) = qr_next((a_elm), a_field);			\
-} while (0)
-
-#define ql_remove(a_head, a_elm, a_field) do {				\
-	if (ql_first(a_head) == (a_elm)) {				\
-		ql_first(a_head) = qr_next(ql_first(a_head), a_field);	\
-	}								\
-	if (ql_first(a_head) != (a_elm)) {				\
-		qr_remove((a_elm), a_field);				\
-	} else {							\
-		ql_first(a_head) = NULL;				\
-	}								\
-} while (0)
-
-#define ql_head_remove(a_head, a_type, a_field) do {			\
-	a_type *t = ql_first(a_head);					\
-	ql_remove((a_head), t, a_field);				\
-} while (0)
-
-#define ql_tail_remove(a_head, a_type, a_field) do {			\
-	a_type *t = ql_last(a_head, a_field);				\
-	ql_remove((a_head), t, a_field);				\
-} while (0)
-
-#define ql_foreach(a_var, a_head, a_field)				\
-	qr_foreach((a_var), ql_first(a_head), a_field)
-
-#define ql_reverse_foreach(a_var, a_head, a_field)			\
-	qr_reverse_foreach((a_var), ql_first(a_head), a_field)
-
-#endif /* QL_H_ */
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdlib/qr.h
--- a/head/lib/libc/stdlib/qr.h	Sat Apr 21 21:30:13 2012 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,106 +0,0 @@
-/******************************************************************************
- *
- * Copyright (C) 2002 Jason Evans <jasone at FreeBSD.org>.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice(s), this list of conditions and the following disclaimer
- *    unmodified other than the allowable addition of one or more
- *    copyright notices.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice(s), this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************/
-
-#ifndef QR_H_
-#define	QR_H_
-
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/libc/stdlib/qr.h 203329 2010-01-31 23:16:10Z jasone $");
-
-/* Ring definitions. */
-#define qr(a_type)							\
-struct {								\
-	a_type	*qre_next;						\
-	a_type	*qre_prev;						\
-}
-
-/* Ring functions. */
-#define qr_new(a_qr, a_field) do {					\
-	(a_qr)->a_field.qre_next = (a_qr);				\
-	(a_qr)->a_field.qre_prev = (a_qr);				\
-} while (0)
-
-#define qr_next(a_qr, a_field) ((a_qr)->a_field.qre_next)
-
-#define qr_prev(a_qr, a_field) ((a_qr)->a_field.qre_prev)
-
-#define qr_before_insert(a_qrelm, a_qr, a_field) do {			\
-	(a_qr)->a_field.qre_prev = (a_qrelm)->a_field.qre_prev;		\
-	(a_qr)->a_field.qre_next = (a_qrelm);				\
-	(a_qr)->a_field.qre_prev->a_field.qre_next = (a_qr);		\
-	(a_qrelm)->a_field.qre_prev = (a_qr);				\
-} while (0)
-
-#define qr_after_insert(a_qrelm, a_qr, a_field)				\
-    do									\
-    {									\
-	(a_qr)->a_field.qre_next = (a_qrelm)->a_field.qre_next;		\
-	(a_qr)->a_field.qre_prev = (a_qrelm);				\
-	(a_qr)->a_field.qre_next->a_field.qre_prev = (a_qr);		\
-	(a_qrelm)->a_field.qre_next = (a_qr);				\
-    } while (0)
-
-#define qr_meld(a_qr_a, a_qr_b, a_field) do {				\
-	void *t;							\
-	(a_qr_a)->a_field.qre_prev->a_field.qre_next = (a_qr_b);	\
-	(a_qr_b)->a_field.qre_prev->a_field.qre_next = (a_qr_a);	\
-	t = (a_qr_a)->a_field.qre_prev;					\
-	(a_qr_a)->a_field.qre_prev = (a_qr_b)->a_field.qre_prev;	\
-	(a_qr_b)->a_field.qre_prev = t;					\
-} while (0)
-
-/* qr_meld() and qr_split() are functionally equivalent, so there's no need to
- * have two copies of the code. */
-#define qr_split(a_qr_a, a_qr_b, a_field)				\
-	qr_meld((a_qr_a), (a_qr_b), a_field)
-
-#define qr_remove(a_qr, a_field) do {					\
-	(a_qr)->a_field.qre_prev->a_field.qre_next			\
-	    = (a_qr)->a_field.qre_next;					\
-	(a_qr)->a_field.qre_next->a_field.qre_prev			\
-	    = (a_qr)->a_field.qre_prev;					\
-	(a_qr)->a_field.qre_next = (a_qr);				\
-	(a_qr)->a_field.qre_prev = (a_qr);				\
-} while (0)
-
-#define qr_foreach(var, a_qr, a_field)					\
-	for ((var) = (a_qr);						\
-	    (var) != NULL;						\
-	    (var) = (((var)->a_field.qre_next != (a_qr))		\
-	    ? (var)->a_field.qre_next : NULL))
-
-#define qr_reverse_foreach(var, a_qr, a_field)				\
-	for ((var) = ((a_qr) != NULL) ? qr_prev(a_qr, a_field) : NULL;	\
-	    (var) != NULL;						\
-	    (var) = (((var) != (a_qr))					\
-	    ? (var)->a_field.qre_prev : NULL))
-
-#endif /* QR_H_ */
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdlib/rb.h
--- a/head/lib/libc/stdlib/rb.h	Sat Apr 21 21:30:13 2012 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1002 +0,0 @@
-/*-
- *******************************************************************************
- *
- * Copyright (C) 2008-2010 Jason Evans <jasone at FreeBSD.org>.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice(s), this list of conditions and the following disclaimer
- *    unmodified other than the allowable addition of one or more
- *    copyright notices.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice(s), this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- *
- * cpp macro implementation of left-leaning 2-3 red-black trees.  Parent
- * pointers are not used, and color bits are stored in the least significant
- * bit of right-child pointers (if RB_COMPACT is defined), thus making node
- * linkage as compact as is possible for red-black trees.
- *
- * Usage:
- *
- *   #include <stdint.h>
- *   #include <stdbool.h>
- *   #define NDEBUG // (Optional, see assert(3).)
- *   #include <assert.h>
- *   #define RB_COMPACT // (Optional, embed color bits in right-child pointers.)
- *   #include <rb.h>
- *   ...
- *
- *******************************************************************************
- */
-
-#ifndef RB_H_
-#define	RB_H_
-
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/libc/stdlib/rb.h 204493 2010-02-28 22:57:13Z jasone $");
-
-#ifdef RB_COMPACT
-/* Node structure. */
-#define	rb_node(a_type)							\
-struct {								\
-    a_type *rbn_left;							\
-    a_type *rbn_right_red;						\
-}
-#else
-#define	rb_node(a_type)							\
-struct {								\
-    a_type *rbn_left;							\
-    a_type *rbn_right;							\
-    bool rbn_red;							\
-}
-#endif
-
-/* Root structure. */
-#define	rb_tree(a_type)							\
-struct {								\
-    a_type *rbt_root;							\
-    a_type rbt_nil;							\
-}
-
-/* Left accessors. */
-#define	rbtn_left_get(a_type, a_field, a_node)				\
-    ((a_node)->a_field.rbn_left)
-#define	rbtn_left_set(a_type, a_field, a_node, a_left) do {		\
-    (a_node)->a_field.rbn_left = a_left;				\
-} while (0)
-
-#ifdef RB_COMPACT
-/* Right accessors. */
-#define	rbtn_right_get(a_type, a_field, a_node)				\
-    ((a_type *) (((intptr_t) (a_node)->a_field.rbn_right_red)		\
-      & ((ssize_t)-2)))
-#define	rbtn_right_set(a_type, a_field, a_node, a_right) do {		\
-    (a_node)->a_field.rbn_right_red = (a_type *) (((uintptr_t) a_right)	\
-      | (((uintptr_t) (a_node)->a_field.rbn_right_red) & ((size_t)1)));	\
-} while (0)
-
-/* Color accessors. */
-#define	rbtn_red_get(a_type, a_field, a_node)				\
-    ((bool) (((uintptr_t) (a_node)->a_field.rbn_right_red)		\
-      & ((size_t)1)))
-#define	rbtn_color_set(a_type, a_field, a_node, a_red) do {		\
-    (a_node)->a_field.rbn_right_red = (a_type *) ((((intptr_t)		\
-      (a_node)->a_field.rbn_right_red) & ((ssize_t)-2))			\
-      | ((ssize_t)a_red));						\
-} while (0)
-#define	rbtn_red_set(a_type, a_field, a_node) do {			\
-    (a_node)->a_field.rbn_right_red = (a_type *) (((uintptr_t)		\
-      (a_node)->a_field.rbn_right_red) | ((size_t)1));			\
-} while (0)
-#define	rbtn_black_set(a_type, a_field, a_node) do {			\
-    (a_node)->a_field.rbn_right_red = (a_type *) (((intptr_t)		\
-      (a_node)->a_field.rbn_right_red) & ((ssize_t)-2));		\
-} while (0)
-#else
-/* Right accessors. */
-#define	rbtn_right_get(a_type, a_field, a_node)				\
-    ((a_node)->a_field.rbn_right)
-#define	rbtn_right_set(a_type, a_field, a_node, a_right) do {		\
-    (a_node)->a_field.rbn_right = a_right;				\
-} while (0)
-
-/* Color accessors. */
-#define	rbtn_red_get(a_type, a_field, a_node)				\
-    ((a_node)->a_field.rbn_red)
-#define	rbtn_color_set(a_type, a_field, a_node, a_red) do {		\
-    (a_node)->a_field.rbn_red = (a_red);				\
-} while (0)
-#define	rbtn_red_set(a_type, a_field, a_node) do {			\
-    (a_node)->a_field.rbn_red = true;					\
-} while (0)
-#define	rbtn_black_set(a_type, a_field, a_node) do {			\
-    (a_node)->a_field.rbn_red = false;					\
-} while (0)
-#endif
-
-/* Node initializer. */
-#define	rbt_node_new(a_type, a_field, a_rbt, a_node) do {		\
-    rbtn_left_set(a_type, a_field, (a_node), &(a_rbt)->rbt_nil);	\
-    rbtn_right_set(a_type, a_field, (a_node), &(a_rbt)->rbt_nil);	\
-    rbtn_red_set(a_type, a_field, (a_node));				\
-} while (0)
-
-/* Tree initializer. */
-#define	rb_new(a_type, a_field, a_rbt) do {				\
-    (a_rbt)->rbt_root = &(a_rbt)->rbt_nil;				\
-    rbt_node_new(a_type, a_field, a_rbt, &(a_rbt)->rbt_nil);		\
-    rbtn_black_set(a_type, a_field, &(a_rbt)->rbt_nil);			\
-} while (0)
-
-/* Internal utility macros. */
-#define	rbtn_first(a_type, a_field, a_rbt, a_root, r_node) do {		\
-    (r_node) = (a_root);						\
-    if ((r_node) != &(a_rbt)->rbt_nil) {				\
-	for (;								\
-	  rbtn_left_get(a_type, a_field, (r_node)) != &(a_rbt)->rbt_nil;\
-	  (r_node) = rbtn_left_get(a_type, a_field, (r_node))) {	\
-	}								\
-    }									\
-} while (0)
-
-#define	rbtn_last(a_type, a_field, a_rbt, a_root, r_node) do {		\
-    (r_node) = (a_root);						\
-    if ((r_node) != &(a_rbt)->rbt_nil) {				\
-	for (; rbtn_right_get(a_type, a_field, (r_node)) !=		\
-	  &(a_rbt)->rbt_nil; (r_node) = rbtn_right_get(a_type, a_field,	\
-	  (r_node))) {							\
-	}								\
-    }									\
-} while (0)
-
-#define	rbtn_rotate_left(a_type, a_field, a_node, r_node) do {		\
-    (r_node) = rbtn_right_get(a_type, a_field, (a_node));		\
-    rbtn_right_set(a_type, a_field, (a_node),				\
-      rbtn_left_get(a_type, a_field, (r_node)));			\
-    rbtn_left_set(a_type, a_field, (r_node), (a_node));			\
-} while (0)
-
-#define	rbtn_rotate_right(a_type, a_field, a_node, r_node) do {		\
-    (r_node) = rbtn_left_get(a_type, a_field, (a_node));		\
-    rbtn_left_set(a_type, a_field, (a_node),				\
-      rbtn_right_get(a_type, a_field, (r_node)));			\
-    rbtn_right_set(a_type, a_field, (r_node), (a_node));		\
-} while (0)
-
-/*
- * The rb_proto() macro generates function prototypes that correspond to the
- * functions generated by an equivalently parameterized call to rb_gen().
- */
-
-#define	rb_proto(a_attr, a_prefix, a_rbt_type, a_type)			\
-a_attr void								\
-a_prefix##new(a_rbt_type *rbtree);					\
-a_attr a_type *								\
-a_prefix##first(a_rbt_type *rbtree);					\
-a_attr a_type *								\
-a_prefix##last(a_rbt_type *rbtree);					\
-a_attr a_type *								\
-a_prefix##next(a_rbt_type *rbtree, a_type *node);			\
-a_attr a_type *								\
-a_prefix##prev(a_rbt_type *rbtree, a_type *node);			\
-a_attr a_type *								\
-a_prefix##search(a_rbt_type *rbtree, a_type *key);			\
-a_attr a_type *								\
-a_prefix##nsearch(a_rbt_type *rbtree, a_type *key);			\
-a_attr a_type *								\
-a_prefix##psearch(a_rbt_type *rbtree, a_type *key);			\
-a_attr void								\
-a_prefix##insert(a_rbt_type *rbtree, a_type *node);			\
-a_attr void								\
-a_prefix##remove(a_rbt_type *rbtree, a_type *node);			\
-a_attr a_type *								\
-a_prefix##iter(a_rbt_type *rbtree, a_type *start, a_type *(*cb)(	\
-  a_rbt_type *, a_type *, void *), void *arg);				\
-a_attr a_type *								\
-a_prefix##reverse_iter(a_rbt_type *rbtree, a_type *start,		\
-  a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg);
-
-/*
- * The rb_gen() macro generates a type-specific red-black tree implementation,
- * based on the above cpp macros.
- *
- * Arguments:
- *
- *   a_attr    : Function attribute for generated functions (ex: static).
- *   a_prefix  : Prefix for generated functions (ex: extree_).
- *   a_rb_type : Type for red-black tree data structure (ex: extree_t).
- *   a_type    : Type for red-black tree node data structure (ex:
- *               extree_node_t).
- *   a_field   : Name of red-black tree node linkage (ex: extree_link).
- *   a_cmp     : Node comparison function name, with the following prototype:
- *                 int (a_cmp *)(a_type *a_node, a_type *a_other);
- *                                       ^^^^^^
- *                                    or a_key
- *               Interpretation of comparision function return values:
- *                 -1 : a_node <  a_other
- *                  0 : a_node == a_other
- *                  1 : a_node >  a_other
- *               In all cases, the a_node or a_key macro argument is the first
- *               argument to the comparison function, which makes it possible
- *               to write comparison functions that treat the first argument
- *               specially.
- *
- * Assuming the following setup:
- *
- *   typedef struct ex_node_s ex_node_t;
- *   struct ex_node_s {
- *       rb_node(ex_node_t) ex_link;
- *   };
- *   typedef rb(ex_node_t) ex_t;
- *   rb_gen(static, ex_, ex_t, ex_node_t, ex_link, ex_cmp, 1297, 1301)
- *
- * The following API is generated:
- *
- *   static void
- *   ex_new(ex_t *extree);
- *       Description: Initialize a red-black tree structure.
- *       Args:
- *         extree: Pointer to an uninitialized red-black tree object.
- *
- *   static ex_node_t *
- *   ex_first(ex_t *extree);
- *   static ex_node_t *
- *   ex_last(ex_t *extree);
- *       Description: Get the first/last node in extree.
- *       Args:
- *         extree: Pointer to an initialized red-black tree object.
- *       Ret: First/last node in extree, or NULL if extree is empty.
- *
- *   static ex_node_t *
- *   ex_next(ex_t *extree, ex_node_t *node);
- *   static ex_node_t *
- *   ex_prev(ex_t *extree, ex_node_t *node);
- *       Description: Get node's successor/predecessor.
- *       Args:
- *         extree: Pointer to an initialized red-black tree object.
- *         node : A node in extree.
- *       Ret: node's successor/predecessor in extree, or NULL if node is
- *            last/first.
- *
- *   static ex_node_t *
- *   ex_search(ex_t *extree, ex_node_t *key);
- *       Description: Search for node that matches key.
- *       Args:
- *         extree: Pointer to an initialized red-black tree object.
- *         key  : Search key.
- *       Ret: Node in extree that matches key, or NULL if no match.
- *
- *   static ex_node_t *
- *   ex_nsearch(ex_t *extree, ex_node_t *key);
- *   static ex_node_t *
- *   ex_psearch(ex_t *extree, ex_node_t *key);
- *       Description: Search for node that matches key.  If no match is found,
- *                    return what would be key's successor/predecessor, were
- *                    key in extree.
- *       Args:
- *         extree: Pointer to an initialized red-black tree object.
- *         key   : Search key.
- *       Ret: Node in extree that matches key, or if no match, hypothetical
- *            node's successor/predecessor (NULL if no successor/predecessor).
- *
- *   static void
- *   ex_insert(ex_t *extree, ex_node_t *node);
- *       Description: Insert node into extree.
- *       Args:
- *         extree: Pointer to an initialized red-black tree object.
- *         node  : Node to be inserted into extree.
- *
- *   static void
- *   ex_remove(ex_t *extree, ex_node_t *node);
- *       Description: Remove node from extree.
- *       Args:
- *         extree: Pointer to an initialized red-black tree object.
- *         node  : Node in extree to be removed.
- *
- *   static ex_node_t *
- *   ex_iter(ex_t *extree, ex_node_t *start, ex_node_t *(*cb)(ex_t *,
- *     ex_node_t *, void *), void *arg);
- *   static ex_node_t *
- *   ex_reverse_iter(ex_t *extree, ex_node_t *start, ex_node *(*cb)(ex_t *,
- *     ex_node_t *, void *), void *arg);
- *       Description: Iterate forward/backward over extree, starting at node.
- *                    If extree is modified, iteration must be immediately
- *                    terminated by the callback function that causes the
- *                    modification.
- *       Args:
- *         extree: Pointer to an initialized red-black tree object.
- *         start : Node at which to start iteration, or NULL to start at
- *                 first/last node.
- *         cb    : Callback function, which is called for each node during
- *                 iteration.  Under normal circumstances the callback function
- *                 should return NULL, which causes iteration to continue.  If a
- *                 callback function returns non-NULL, iteration is immediately
- *                 terminated and the non-NULL return value is returned by the
- *                 iterator.  This is useful for re-starting iteration after
- *                 modifying extree.
- *         arg   : Opaque pointer passed to cb().
- *       Ret: NULL if iteration completed, or the non-NULL callback return value
- *            that caused termination of the iteration.
- */
-#define	rb_gen(a_attr, a_prefix, a_rbt_type, a_type, a_field, a_cmp)	\
-a_attr void								\
-a_prefix##new(a_rbt_type *rbtree) {					\
-    rb_new(a_type, a_field, rbtree);					\
-}									\
-a_attr a_type *								\
-a_prefix##first(a_rbt_type *rbtree) {					\
-    a_type *ret;							\
-    rbtn_first(a_type, a_field, rbtree, rbtree->rbt_root, ret);		\
-    if (ret == &rbtree->rbt_nil) {					\
-	ret = NULL;							\
-    }									\
-    return (ret);							\
-}									\
-a_attr a_type *								\
-a_prefix##last(a_rbt_type *rbtree) {					\
-    a_type *ret;							\
-    rbtn_last(a_type, a_field, rbtree, rbtree->rbt_root, ret);		\
-    if (ret == &rbtree->rbt_nil) {					\
-	ret = NULL;							\
-    }									\
-    return (ret);							\
-}									\
-a_attr a_type *								\
-a_prefix##next(a_rbt_type *rbtree, a_type *node) {			\
-    a_type *ret;							\
-    if (rbtn_right_get(a_type, a_field, node) != &rbtree->rbt_nil) {	\
-	rbtn_first(a_type, a_field, rbtree, rbtn_right_get(a_type,	\
-	  a_field, node), ret);						\
-    } else {								\
-	a_type *tnode = rbtree->rbt_root;				\
-	assert(tnode != &rbtree->rbt_nil);				\
-	ret = &rbtree->rbt_nil;						\
-	while (true) {							\
-	    int cmp = (a_cmp)(node, tnode);				\
-	    if (cmp < 0) {						\
-		ret = tnode;						\
-		tnode = rbtn_left_get(a_type, a_field, tnode);		\
-	    } else if (cmp > 0) {					\
-		tnode = rbtn_right_get(a_type, a_field, tnode);		\
-	    } else {							\
-		break;							\
-	    }								\
-	    assert(tnode != &rbtree->rbt_nil);				\
-	}								\
-    }									\
-    if (ret == &rbtree->rbt_nil) {					\
-	ret = (NULL);							\
-    }									\
-    return (ret);							\
-}									\
-a_attr a_type *								\
-a_prefix##prev(a_rbt_type *rbtree, a_type *node) {			\
-    a_type *ret;							\
-    if (rbtn_left_get(a_type, a_field, node) != &rbtree->rbt_nil) {	\
-	rbtn_last(a_type, a_field, rbtree, rbtn_left_get(a_type,	\
-	  a_field, node), ret);						\
-    } else {								\
-	a_type *tnode = rbtree->rbt_root;				\
-	assert(tnode != &rbtree->rbt_nil);				\
-	ret = &rbtree->rbt_nil;						\
-	while (true) {							\
-	    int cmp = (a_cmp)(node, tnode);				\
-	    if (cmp < 0) {						\
-		tnode = rbtn_left_get(a_type, a_field, tnode);		\
-	    } else if (cmp > 0) {					\
-		ret = tnode;						\
-		tnode = rbtn_right_get(a_type, a_field, tnode);		\
-	    } else {							\
-		break;							\
-	    }								\
-	    assert(tnode != &rbtree->rbt_nil);				\
-	}								\
-    }									\
-    if (ret == &rbtree->rbt_nil) {					\
-	ret = (NULL);							\
-    }									\
-    return (ret);							\
-}									\
-a_attr a_type *								\
-a_prefix##search(a_rbt_type *rbtree, a_type *key) {			\
-    a_type *ret;							\
-    int cmp;								\
-    ret = rbtree->rbt_root;						\
-    while (ret != &rbtree->rbt_nil					\
-      && (cmp = (a_cmp)(key, ret)) != 0) {				\
-	if (cmp < 0) {							\
-	    ret = rbtn_left_get(a_type, a_field, ret);			\
-	} else {							\
-	    ret = rbtn_right_get(a_type, a_field, ret);			\
-	}								\
-    }									\
-    if (ret == &rbtree->rbt_nil) {					\
-	ret = (NULL);							\
-    }									\
-    return (ret);							\
-}									\
-a_attr a_type *								\
-a_prefix##nsearch(a_rbt_type *rbtree, a_type *key) {			\
-    a_type *ret;							\
-    a_type *tnode = rbtree->rbt_root;					\
-    ret = &rbtree->rbt_nil;						\
-    while (tnode != &rbtree->rbt_nil) {					\
-	int cmp = (a_cmp)(key, tnode);					\
-	if (cmp < 0) {							\
-	    ret = tnode;						\
-	    tnode = rbtn_left_get(a_type, a_field, tnode);		\
-	} else if (cmp > 0) {						\
-	    tnode = rbtn_right_get(a_type, a_field, tnode);		\
-	} else {							\
-	    ret = tnode;						\
-	    break;							\
-	}								\
-    }									\
-    if (ret == &rbtree->rbt_nil) {					\
-	ret = (NULL);							\
-    }									\
-    return (ret);							\
-}									\
-a_attr a_type *								\
-a_prefix##psearch(a_rbt_type *rbtree, a_type *key) {			\
-    a_type *ret;							\
-    a_type *tnode = rbtree->rbt_root;					\
-    ret = &rbtree->rbt_nil;						\
-    while (tnode != &rbtree->rbt_nil) {					\
-	int cmp = (a_cmp)(key, tnode);					\
-	if (cmp < 0) {							\
-	    tnode = rbtn_left_get(a_type, a_field, tnode);		\
-	} else if (cmp > 0) {						\
-	    ret = tnode;						\
-	    tnode = rbtn_right_get(a_type, a_field, tnode);		\
-	} else {							\
-	    ret = tnode;						\
-	    break;							\
-	}								\
-    }									\
-    if (ret == &rbtree->rbt_nil) {					\
-	ret = (NULL);							\
-    }									\
-    return (ret);							\
-}									\
-a_attr void								\
-a_prefix##insert(a_rbt_type *rbtree, a_type *node) {			\
-    struct {								\
-	a_type *node;							\
-	int cmp;							\
-    } path[sizeof(void *) << 4], *pathp;				\
-    rbt_node_new(a_type, a_field, rbtree, node);			\
-    /* Wind. */								\
-    path->node = rbtree->rbt_root;					\
-    for (pathp = path; pathp->node != &rbtree->rbt_nil; pathp++) {	\
-	int cmp = pathp->cmp = a_cmp(node, pathp->node);		\
-	assert(cmp != 0);						\
-	if (cmp < 0) {							\
-	    pathp[1].node = rbtn_left_get(a_type, a_field,		\
-	      pathp->node);						\
-	} else {							\
-	    pathp[1].node = rbtn_right_get(a_type, a_field,		\
-	      pathp->node);						\
-	}								\
-    }									\
-    pathp->node = node;							\
-    /* Unwind. */							\
-    for (pathp--; (uintptr_t)pathp >= (uintptr_t)path; pathp--) {	\
-	a_type *cnode = pathp->node;					\
-	if (pathp->cmp < 0) {						\
-	    a_type *left = pathp[1].node;				\
-	    rbtn_left_set(a_type, a_field, cnode, left);		\
-	    if (rbtn_red_get(a_type, a_field, left)) {			\
-		a_type *leftleft = rbtn_left_get(a_type, a_field, left);\
-		if (rbtn_red_get(a_type, a_field, leftleft)) {		\
-		    /* Fix up 4-node. */				\
-		    a_type *tnode;					\
-		    rbtn_black_set(a_type, a_field, leftleft);		\
-		    rbtn_rotate_right(a_type, a_field, cnode, tnode);	\
-		    cnode = tnode;					\
-		}							\
-	    } else {							\
-		return;							\
-	    }								\
-	} else {							\
-	    a_type *right = pathp[1].node;				\
-	    rbtn_right_set(a_type, a_field, cnode, right);		\
-	    if (rbtn_red_get(a_type, a_field, right)) {			\
-		a_type *left = rbtn_left_get(a_type, a_field, cnode);	\
-		if (rbtn_red_get(a_type, a_field, left)) {		\
-		    /* Split 4-node. */					\
-		    rbtn_black_set(a_type, a_field, left);		\
-		    rbtn_black_set(a_type, a_field, right);		\
-		    rbtn_red_set(a_type, a_field, cnode);		\
-		} else {						\
-		    /* Lean left. */					\
-		    a_type *tnode;					\
-		    bool tred = rbtn_red_get(a_type, a_field, cnode);	\
-		    rbtn_rotate_left(a_type, a_field, cnode, tnode);	\
-		    rbtn_color_set(a_type, a_field, tnode, tred);	\
-		    rbtn_red_set(a_type, a_field, cnode);		\
-		    cnode = tnode;					\
-		}							\
-	    } else {							\
-		return;							\
-	    }								\
-	}								\
-	pathp->node = cnode;						\
-    }									\
-    /* Set root, and make it black. */					\
-    rbtree->rbt_root = path->node;					\
-    rbtn_black_set(a_type, a_field, rbtree->rbt_root);			\
-}									\
-a_attr void								\
-a_prefix##remove(a_rbt_type *rbtree, a_type *node) {			\
-    struct {								\
-	a_type *node;							\
-	int cmp;							\
-    } *pathp, *nodep, path[sizeof(void *) << 4];			\
-    /* Wind. */								\
-    nodep = NULL; /* Silence compiler warning. */			\
-    path->node = rbtree->rbt_root;					\
-    for (pathp = path; pathp->node != &rbtree->rbt_nil; pathp++) {	\
-	int cmp = pathp->cmp = a_cmp(node, pathp->node);		\
-	if (cmp < 0) {							\
-	    pathp[1].node = rbtn_left_get(a_type, a_field,		\
-	      pathp->node);						\
-	} else {							\
-	    pathp[1].node = rbtn_right_get(a_type, a_field,		\
-	      pathp->node);						\
-	    if (cmp == 0) {						\
-	        /* Find node's successor, in preparation for swap. */	\
-		pathp->cmp = 1;						\
-		nodep = pathp;						\
-		for (pathp++; pathp->node != &rbtree->rbt_nil;		\
-		  pathp++) {						\
-		    pathp->cmp = -1;					\
-		    pathp[1].node = rbtn_left_get(a_type, a_field,	\
-		      pathp->node);					\
-		}							\
-		break;							\
-	    }								\
-	}								\
-    }									\
-    assert(nodep->node == node);					\
-    pathp--;								\
-    if (pathp->node != node) {						\
-	/* Swap node with its successor. */				\
-	bool tred = rbtn_red_get(a_type, a_field, pathp->node);		\
-	rbtn_color_set(a_type, a_field, pathp->node,			\
-	  rbtn_red_get(a_type, a_field, node));				\
-	rbtn_left_set(a_type, a_field, pathp->node,			\
-	  rbtn_left_get(a_type, a_field, node));			\
-	/* If node's successor is its right child, the following code */\
-	/* will do the wrong thing for the right child pointer.       */\
-	/* However, it doesn't matter, because the pointer will be    */\
-	/* properly set when the successor is pruned.                 */\
-	rbtn_right_set(a_type, a_field, pathp->node,			\
-	  rbtn_right_get(a_type, a_field, node));			\
-	rbtn_color_set(a_type, a_field, node, tred);			\
-	/* The pruned leaf node's child pointers are never accessed   */\
-	/* again, so don't bother setting them to nil.                */\
-	nodep->node = pathp->node;					\
-	pathp->node = node;						\
-	if (nodep == path) {						\
-	    rbtree->rbt_root = nodep->node;				\
-	} else {							\
-	    if (nodep[-1].cmp < 0) {					\
-		rbtn_left_set(a_type, a_field, nodep[-1].node,		\
-		  nodep->node);						\
-	    } else {							\
-		rbtn_right_set(a_type, a_field, nodep[-1].node,		\
-		  nodep->node);						\
-	    }								\
-	}								\
-    } else {								\
-	a_type *left = rbtn_left_get(a_type, a_field, node);		\
-	if (left != &rbtree->rbt_nil) {					\
-	    /* node has no successor, but it has a left child.        */\
-	    /* Splice node out, without losing the left child.        */\
-	    assert(rbtn_red_get(a_type, a_field, node) == false);	\
-	    assert(rbtn_red_get(a_type, a_field, left));		\
-	    rbtn_black_set(a_type, a_field, left);			\
-	    if (pathp == path) {					\
-		rbtree->rbt_root = left;				\
-	    } else {							\
-		if (pathp[-1].cmp < 0) {				\
-		    rbtn_left_set(a_type, a_field, pathp[-1].node,	\
-		      left);						\
-		} else {						\
-		    rbtn_right_set(a_type, a_field, pathp[-1].node,	\
-		      left);						\
-		}							\
-	    }								\
-	    return;							\
-	} else if (pathp == path) {					\
-	    /* The tree only contained one node. */			\
-	    rbtree->rbt_root = &rbtree->rbt_nil;			\
-	    return;							\
-	}								\
-    }									\
-    if (rbtn_red_get(a_type, a_field, pathp->node)) {			\
-	/* Prune red node, which requires no fixup. */			\
-	assert(pathp[-1].cmp < 0);					\
-	rbtn_left_set(a_type, a_field, pathp[-1].node,			\
-	  &rbtree->rbt_nil);						\
-	return;								\
-    }									\
-    /* The node to be pruned is black, so unwind until balance is     */\
-    /* restored.                                                      */\
-    pathp->node = &rbtree->rbt_nil;					\
-    for (pathp--; (uintptr_t)pathp >= (uintptr_t)path; pathp--) {	\
-	assert(pathp->cmp != 0);					\
-	if (pathp->cmp < 0) {						\
-	    rbtn_left_set(a_type, a_field, pathp->node,			\
-	      pathp[1].node);						\
-	    assert(rbtn_red_get(a_type, a_field, pathp[1].node)		\
-	      == false);						\
-	    if (rbtn_red_get(a_type, a_field, pathp->node)) {		\
-		a_type *right = rbtn_right_get(a_type, a_field,		\
-		  pathp->node);						\
-		a_type *rightleft = rbtn_left_get(a_type, a_field,	\
-		  right);						\
-		a_type *tnode;						\
-		if (rbtn_red_get(a_type, a_field, rightleft)) {		\
-		    /* In the following diagrams, ||, //, and \\      */\
-		    /* indicate the path to the removed node.         */\
-		    /*                                                */\
-		    /*      ||                                        */\
-		    /*    pathp(r)                                    */\
-		    /*  //        \                                   */\
-		    /* (b)        (b)                                 */\
-		    /*           /                                    */\
-		    /*          (r)                                   */\
-		    /*                                                */\
-		    rbtn_black_set(a_type, a_field, pathp->node);	\
-		    rbtn_rotate_right(a_type, a_field, right, tnode);	\
-		    rbtn_right_set(a_type, a_field, pathp->node, tnode);\
-		    rbtn_rotate_left(a_type, a_field, pathp->node,	\
-		      tnode);						\
-		} else {						\
-		    /*      ||                                        */\
-		    /*    pathp(r)                                    */\
-		    /*  //        \                                   */\
-		    /* (b)        (b)                                 */\
-		    /*           /                                    */\
-		    /*          (b)                                   */\
-		    /*                                                */\
-		    rbtn_rotate_left(a_type, a_field, pathp->node,	\
-		      tnode);						\
-		}							\
-		/* Balance restored, but rotation modified subtree    */\
-		/* root.                                              */\
-		assert((uintptr_t)pathp > (uintptr_t)path);		\
-		if (pathp[-1].cmp < 0) {				\
-		    rbtn_left_set(a_type, a_field, pathp[-1].node,	\
-		      tnode);						\
-		} else {						\
-		    rbtn_right_set(a_type, a_field, pathp[-1].node,	\
-		      tnode);						\
-		}							\
-		return;							\
-	    } else {							\
-		a_type *right = rbtn_right_get(a_type, a_field,		\
-		  pathp->node);						\
-		a_type *rightleft = rbtn_left_get(a_type, a_field,	\
-		  right);						\
-		if (rbtn_red_get(a_type, a_field, rightleft)) {		\
-		    /*      ||                                        */\
-		    /*    pathp(b)                                    */\
-		    /*  //        \                                   */\
-		    /* (b)        (b)                                 */\
-		    /*           /                                    */\
-		    /*          (r)                                   */\
-		    a_type *tnode;					\
-		    rbtn_black_set(a_type, a_field, rightleft);		\
-		    rbtn_rotate_right(a_type, a_field, right, tnode);	\
-		    rbtn_right_set(a_type, a_field, pathp->node, tnode);\
-		    rbtn_rotate_left(a_type, a_field, pathp->node,	\
-		      tnode);						\
-		    /* Balance restored, but rotation modified        */\
-		    /* subree root, which may actually be the tree    */\
-		    /* root.                                          */\
-		    if (pathp == path) {				\
-			/* Set root. */					\
-			rbtree->rbt_root = tnode;			\
-		    } else {						\
-			if (pathp[-1].cmp < 0) {			\
-			    rbtn_left_set(a_type, a_field,		\
-			      pathp[-1].node, tnode);			\
-			} else {					\
-			    rbtn_right_set(a_type, a_field,		\
-			      pathp[-1].node, tnode);			\
-			}						\
-		    }							\
-		    return;						\
-		} else {						\
-		    /*      ||                                        */\
-		    /*    pathp(b)                                    */\
-		    /*  //        \                                   */\
-		    /* (b)        (b)                                 */\
-		    /*           /                                    */\
-		    /*          (b)                                   */\
-		    a_type *tnode;					\
-		    rbtn_red_set(a_type, a_field, pathp->node);		\
-		    rbtn_rotate_left(a_type, a_field, pathp->node,	\
-		      tnode);						\
-		    pathp->node = tnode;				\
-		}							\
-	    }								\
-	} else {							\
-	    a_type *left;						\
-	    rbtn_right_set(a_type, a_field, pathp->node,		\
-	      pathp[1].node);						\
-	    left = rbtn_left_get(a_type, a_field, pathp->node);		\
-	    if (rbtn_red_get(a_type, a_field, left)) {			\
-		a_type *tnode;						\
-		a_type *leftright = rbtn_right_get(a_type, a_field,	\
-		  left);						\
-		a_type *leftrightleft = rbtn_left_get(a_type, a_field,	\
-		  leftright);						\
-		if (rbtn_red_get(a_type, a_field, leftrightleft)) {	\
-		    /*      ||                                        */\
-		    /*    pathp(b)                                    */\
-		    /*   /        \\                                  */\
-		    /* (r)        (b)                                 */\
-		    /*   \                                            */\
-		    /*   (b)                                          */\
-		    /*   /                                            */\
-		    /* (r)                                            */\
-		    a_type *unode;					\
-		    rbtn_black_set(a_type, a_field, leftrightleft);	\
-		    rbtn_rotate_right(a_type, a_field, pathp->node,	\
-		      unode);						\
-		    rbtn_rotate_right(a_type, a_field, pathp->node,	\
-		      tnode);						\
-		    rbtn_right_set(a_type, a_field, unode, tnode);	\
-		    rbtn_rotate_left(a_type, a_field, unode, tnode);	\
-		} else {						\
-		    /*      ||                                        */\
-		    /*    pathp(b)                                    */\
-		    /*   /        \\                                  */\
-		    /* (r)        (b)                                 */\
-		    /*   \                                            */\
-		    /*   (b)                                          */\
-		    /*   /                                            */\
-		    /* (b)                                            */\
-		    assert(leftright != &rbtree->rbt_nil);		\
-		    rbtn_red_set(a_type, a_field, leftright);		\
-		    rbtn_rotate_right(a_type, a_field, pathp->node,	\
-		      tnode);						\
-		    rbtn_black_set(a_type, a_field, tnode);		\
-		}							\
-		/* Balance restored, but rotation modified subtree    */\
-		/* root, which may actually be the tree root.         */\
-		if (pathp == path) {					\
-		    /* Set root. */					\
-		    rbtree->rbt_root = tnode;				\
-		} else {						\
-		    if (pathp[-1].cmp < 0) {				\
-			rbtn_left_set(a_type, a_field, pathp[-1].node,	\
-			  tnode);					\
-		    } else {						\
-			rbtn_right_set(a_type, a_field, pathp[-1].node,	\
-			  tnode);					\
-		    }							\
-		}							\
-		return;							\
-	    } else if (rbtn_red_get(a_type, a_field, pathp->node)) {	\
-		a_type *leftleft = rbtn_left_get(a_type, a_field, left);\
-		if (rbtn_red_get(a_type, a_field, leftleft)) {		\
-		    /*        ||                                      */\
-		    /*      pathp(r)                                  */\
-		    /*     /        \\                                */\
-		    /*   (b)        (b)                               */\
-		    /*   /                                            */\
-		    /* (r)                                            */\
-		    a_type *tnode;					\
-		    rbtn_black_set(a_type, a_field, pathp->node);	\
-		    rbtn_red_set(a_type, a_field, left);		\
-		    rbtn_black_set(a_type, a_field, leftleft);		\
-		    rbtn_rotate_right(a_type, a_field, pathp->node,	\
-		      tnode);						\
-		    /* Balance restored, but rotation modified        */\
-		    /* subtree root.                                  */\
-		    assert((uintptr_t)pathp > (uintptr_t)path);		\
-		    if (pathp[-1].cmp < 0) {				\
-			rbtn_left_set(a_type, a_field, pathp[-1].node,	\
-			  tnode);					\
-		    } else {						\
-			rbtn_right_set(a_type, a_field, pathp[-1].node,	\
-			  tnode);					\
-		    }							\
-		    return;						\
-		} else {						\
-		    /*        ||                                      */\
-		    /*      pathp(r)                                  */\
-		    /*     /        \\                                */\
-		    /*   (b)        (b)                               */\
-		    /*   /                                            */\
-		    /* (b)                                            */\
-		    rbtn_red_set(a_type, a_field, left);		\
-		    rbtn_black_set(a_type, a_field, pathp->node);	\
-		    /* Balance restored. */				\
-		    return;						\
-		}							\
-	    } else {							\
-		a_type *leftleft = rbtn_left_get(a_type, a_field, left);\
-		if (rbtn_red_get(a_type, a_field, leftleft)) {		\
-		    /*               ||                               */\
-		    /*             pathp(b)                           */\
-		    /*            /        \\                         */\
-		    /*          (b)        (b)                        */\
-		    /*          /                                     */\
-		    /*        (r)                                     */\
-		    a_type *tnode;					\
-		    rbtn_black_set(a_type, a_field, leftleft);		\
-		    rbtn_rotate_right(a_type, a_field, pathp->node,	\
-		      tnode);						\
-		    /* Balance restored, but rotation modified        */\
-		    /* subtree root, which may actually be the tree   */\
-		    /* root.                                          */\
-		    if (pathp == path) {				\
-			/* Set root. */					\
-			rbtree->rbt_root = tnode;			\
-		    } else {						\
-			if (pathp[-1].cmp < 0) {			\
-			    rbtn_left_set(a_type, a_field,		\
-			      pathp[-1].node, tnode);			\
-			} else {					\
-			    rbtn_right_set(a_type, a_field,		\
-			      pathp[-1].node, tnode);			\
-			}						\
-		    }							\
-		    return;						\
-		} else {						\
-		    /*               ||                               */\
-		    /*             pathp(b)                           */\
-		    /*            /        \\                         */\
-		    /*          (b)        (b)                        */\
-		    /*          /                                     */\
-		    /*        (b)                                     */\
-		    rbtn_red_set(a_type, a_field, left);		\
-		}							\
-	    }								\
-	}								\
-    }									\
-    /* Set root. */							\
-    rbtree->rbt_root = path->node;					\
-    assert(rbtn_red_get(a_type, a_field, rbtree->rbt_root) == false);	\
-}									\
-a_attr a_type *								\
-a_prefix##iter_recurse(a_rbt_type *rbtree, a_type *node,		\
-  a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg) {		\
-    if (node == &rbtree->rbt_nil) {					\
-	return (&rbtree->rbt_nil);					\
-    } else {								\
-	a_type *ret;							\
-	if ((ret = a_prefix##iter_recurse(rbtree, rbtn_left_get(a_type,	\
-	  a_field, node), cb, arg)) != &rbtree->rbt_nil			\
-	  || (ret = cb(rbtree, node, arg)) != NULL) {			\
-	    return (ret);						\
-	}								\
-	return (a_prefix##iter_recurse(rbtree, rbtn_right_get(a_type,	\
-	  a_field, node), cb, arg));					\
-    }									\
-}									\
-a_attr a_type *								\
-a_prefix##iter_start(a_rbt_type *rbtree, a_type *start, a_type *node,	\
-  a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg) {		\
-    int cmp = a_cmp(start, node);					\
-    if (cmp < 0) {							\
-	a_type *ret;							\
-	if ((ret = a_prefix##iter_start(rbtree, start,			\
-	  rbtn_left_get(a_type, a_field, node), cb, arg)) !=		\
-	  &rbtree->rbt_nil || (ret = cb(rbtree, node, arg)) != NULL) {	\
-	    return (ret);						\
-	}								\
-	return (a_prefix##iter_recurse(rbtree, rbtn_right_get(a_type,	\
-	  a_field, node), cb, arg));					\
-    } else if (cmp > 0) {						\
-	return (a_prefix##iter_start(rbtree, start,			\
-	  rbtn_right_get(a_type, a_field, node), cb, arg));		\
-    } else {								\
-	a_type *ret;							\
-	if ((ret = cb(rbtree, node, arg)) != NULL) {			\
-	    return (ret);						\
-	}								\
-	return (a_prefix##iter_recurse(rbtree, rbtn_right_get(a_type,	\
-	  a_field, node), cb, arg));					\
-    }									\
-}									\
-a_attr a_type *								\
-a_prefix##iter(a_rbt_type *rbtree, a_type *start, a_type *(*cb)(	\
-  a_rbt_type *, a_type *, void *), void *arg) {				\
-    a_type *ret;							\
-    if (start != NULL) {						\
-	ret = a_prefix##iter_start(rbtree, start, rbtree->rbt_root,	\
-	  cb, arg);							\
-    } else {								\
-	ret = a_prefix##iter_recurse(rbtree, rbtree->rbt_root, cb, arg);\
-    }									\
-    if (ret == &rbtree->rbt_nil) {					\
-	ret = NULL;							\
-    }									\
-    return (ret);							\
-}									\
-a_attr a_type *								\
-a_prefix##reverse_iter_recurse(a_rbt_type *rbtree, a_type *node,	\
-  a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg) {		\
-    if (node == &rbtree->rbt_nil) {					\
-	return (&rbtree->rbt_nil);					\
-    } else {								\
-	a_type *ret;							\
-	if ((ret = a_prefix##reverse_iter_recurse(rbtree,		\
-	  rbtn_right_get(a_type, a_field, node), cb, arg)) !=		\
-	  &rbtree->rbt_nil || (ret = cb(rbtree, node, arg)) != NULL) {	\
-	    return (ret);						\
-	}								\
-	return (a_prefix##reverse_iter_recurse(rbtree,			\
-	  rbtn_left_get(a_type, a_field, node), cb, arg));		\
-    }									\
-}									\
-a_attr a_type *								\
-a_prefix##reverse_iter_start(a_rbt_type *rbtree, a_type *start,		\
-  a_type *node, a_type *(*cb)(a_rbt_type *, a_type *, void *),		\
-  void *arg) {								\
-    int cmp = a_cmp(start, node);					\
-    if (cmp > 0) {							\
-	a_type *ret;							\
-	if ((ret = a_prefix##reverse_iter_start(rbtree, start,		\
-	  rbtn_right_get(a_type, a_field, node), cb, arg)) !=		\
-	  &rbtree->rbt_nil || (ret = cb(rbtree, node, arg)) != NULL) {	\
-	    return (ret);						\
-	}								\
-	return (a_prefix##reverse_iter_recurse(rbtree,			\
-	  rbtn_left_get(a_type, a_field, node), cb, arg));		\
-    } else if (cmp < 0) {						\
-	return (a_prefix##reverse_iter_start(rbtree, start,		\
-	  rbtn_left_get(a_type, a_field, node), cb, arg));		\
-    } else {								\
-	a_type *ret;							\
-	if ((ret = cb(rbtree, node, arg)) != NULL) {			\
-	    return (ret);						\
-	}								\
-	return (a_prefix##reverse_iter_recurse(rbtree,			\
-	  rbtn_left_get(a_type, a_field, node), cb, arg));		\
-    }									\
-}									\
-a_attr a_type *								\
-a_prefix##reverse_iter(a_rbt_type *rbtree, a_type *start,		\
-  a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg) {		\
-    a_type *ret;							\
-    if (start != NULL) {						\
-	ret = a_prefix##reverse_iter_start(rbtree, start,		\
-	  rbtree->rbt_root, cb, arg);					\
-    } else {								\
-	ret = a_prefix##reverse_iter_recurse(rbtree, rbtree->rbt_root,	\
-	  cb, arg);							\
-    }									\
-    if (ret == &rbtree->rbt_nil) {					\
-	ret = NULL;							\
-    }									\
-    return (ret);							\
-}
-
-#endif /* RB_H_ */
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libc/stdlib/reallocf.3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/lib/libc/stdlib/reallocf.3	Sat Apr 21 21:38:59 2012 +0300
@@ -0,0 +1,82 @@
+.\" Copyright (c) 1980, 1991, 1993
+.\"	The Regents of the University of California.  All rights reserved.
+.\"
+.\" This code is derived from software contributed to Berkeley by
+.\" the American National Standards Committee X3, on Information
+.\" Processing Systems.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\" 3. Neither the name of the University nor the names of its contributors
+.\"    may be used to endorse or promote products derived from this software
+.\"    without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\"     @(#)malloc.3	8.1 (Berkeley) 6/4/93
+.\" $FreeBSD: head/lib/libc/stdlib/reallocf.3 234370 2012-04-17 07:22:14Z jasone $
+.\"
+.Dd January 31, 2010
+.Dt MALLOC 3
+.Os
+.Sh NAME
+.Nm reallocf
+.Nd memory reallocation function
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In stdlib.h
+.Ft void *
+.Fn reallocf "void *ptr" "size_t size"
+.Sh DESCRIPTION
+The
+.Fn reallocf
+function is identical to the
+.Fn realloc
+function, except that it
+will free the passed pointer when the requested memory cannot be allocated.
+This is a
+.Fx
+specific API designed to ease the problems with traditional coding styles
+for
+.Fn realloc
+causing memory leaks in libraries.
+.Sh RETURN VALUES
+The
+.Fn reallocf
+function returns a pointer, possibly identical to
+.Fa ptr ,
+to the allocated memory
+if successful; otherwise a
+.Dv NULL
+pointer is returned, and
+.Va errno
+is set to
+.Er ENOMEM
+if the error was the result of an allocation failure.
+The
+.Fn reallocf
+function deletes the original buffer when an error occurs.
+.Sh SEE ALSO
+.Xr realloc 3
+.Sh HISTORY
+The
+.Fn reallocf
+function first appeared in
+.Fx 3.0 .
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libkse/Makefile
--- a/head/lib/libkse/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libkse/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-# $FreeBSD$
+# $FreeBSD: head/lib/libkse/Makefile 234412 2012-04-18 07:02:53Z pluknet $
 #
 # All library objects contain FreeBSD revision strings by default; they may be
 # excluded as a space-saving measure.  To produce a library that does
@@ -38,7 +38,7 @@
 
 PRECIOUSLIB=
 
-.path: ${.CURDIR}/arch/${MACHINE_CPUARCH}/${MACHINE_CPUARCH}
+.PATH: ${.CURDIR}/arch/${MACHINE_CPUARCH}/${MACHINE_CPUARCH}
 
 .include "${.CURDIR}/arch/${MACHINE_CPUARCH}/Makefile.inc"
 .include "${.CURDIR}/support/Makefile.inc"
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libmagic/Makefile
--- a/head/lib/libmagic/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libmagic/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,5 +1,5 @@
-# $FreeBSD$
-# Copyright (c) David E. O'Brien, 2000-2004
+# $FreeBSD: head/lib/libmagic/Makefile 234449 2012-04-19 03:20:13Z obrien $
+# Copyright (c) David E. O'Brien, 2000-2004, 2006, 2009
 
 CONTRDIR=	${.CURDIR}/../../contrib/file
 .PATH: ${CONTRDIR}
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libmagic/config.h
--- a/head/lib/libmagic/config.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libmagic/config.h	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-/* $FreeBSD$ */
+/* $FreeBSD: head/lib/libmagic/config.h 234449 2012-04-19 03:20:13Z obrien $ */
 
 /* config.h.  Generated from config.h.in by configure.  */
 /* config.h.in.  Generated from configure.ac by autoheader.  */
@@ -15,6 +15,14 @@
 /* HAVE_DAYLIGHT */
 /* #undef HAVE_DAYLIGHT */
 
+/* Define to 1 if you have the declaration of `daylight', and to 0 if you
+   don't. */
+#define HAVE_DECL_DAYLIGHT 0
+
+/* Define to 1 if you have the declaration of `tzname', and to 0 if you don't.
+   */
+#define HAVE_DECL_TZNAME 1
+
 /* Define to 1 if you have the <dlfcn.h> header file. */
 #define HAVE_DLFCN_H 1
 
@@ -24,9 +32,15 @@
 /* Define to 1 if you have the <fcntl.h> header file. */
 #define HAVE_FCNTL_H 1
 
+/* Define to 1 if you have the `fork' function. */
+#define HAVE_FORK 1
+
 /* Define to 1 if fseeko (and presumably ftello) exists and is declared. */
 #define HAVE_FSEEKO 1
 
+/* Define to 1 if you have the `getline' function. */
+#define HAVE_GETLINE 1
+
 /* Define to 1 if you have the <getopt.h> header file. */
 #define HAVE_GETOPT_H 1
 
@@ -42,6 +56,9 @@
 /* Define to 1 if you have the <inttypes.h> header file. */
 #define HAVE_INTTYPES_H 1
 
+/* Define to 1 if you have the `gnurx' library (-lgnurx). */
+/* #undef HAVE_LIBGNURX */
+
 /* Define to 1 if you have the `z' library (-lz). */
 #define HAVE_LIBZ 1
 
@@ -66,6 +83,9 @@
 /* Define to 1 if you have the `mmap' function. */
 #define HAVE_MMAP 1
 
+/* Define to 1 if the system has the type `pid_t'. */
+#define HAVE_PID_T 1
+
 /* Define to 1 if you have the <stdint.h> header file. */
 #define HAVE_STDINT_H 1
 
@@ -99,13 +119,13 @@
 /* HAVE_STRUCT_OPTION */
 #define HAVE_STRUCT_OPTION 1
 
-/* Define to 1 if `st_rdev' is member of `struct stat'. */
+/* Define to 1 if `st_rdev' is a member of `struct stat'. */
 #define HAVE_STRUCT_STAT_ST_RDEV 1
 
-/* Define to 1 if `tm_gmtoff' is member of `struct tm'. */
+/* Define to 1 if `tm_gmtoff' is a member of `struct tm'. */
 #define HAVE_STRUCT_TM_TM_GMTOFF 1
 
-/* Define to 1 if `tm_zone' is member of `struct tm'. */
+/* Define to 1 if `tm_zone' is a member of `struct tm'. */
 #define HAVE_STRUCT_TM_TM_ZONE 1
 
 /* Define to 1 if you have the <sys/mman.h> header file. */
@@ -174,6 +194,10 @@
 /* Define to 1 if you have the <zlib.h> header file. */
 #define HAVE_ZLIB_H 1
 
+/* Define to the sub-directory in which libtool stores uninstalled libraries.
+   */
+#define LT_OBJDIR ".libs/"
+
 /* Define to 1 if `major', `minor', and `makedev' are declared in <mkdev.h>.
    */
 /* #undef MAJOR_IN_MKDEV */
@@ -195,13 +219,16 @@
 #define PACKAGE_NAME "file"
 
 /* Define to the full name and version of this package. */
-#define PACKAGE_STRING "file 5.03"
+#define PACKAGE_STRING "file 5.11"
 
 /* Define to the one symbol short name of this package. */
 #define PACKAGE_TARNAME "file"
 
+/* Define to the home page for this package. */
+#define PACKAGE_URL ""
+
 /* Define to the version of this package. */
-#define PACKAGE_VERSION "5.03"
+#define PACKAGE_VERSION "5.11"
 
 /* The size of `long long', as computed by sizeof. */
 #define SIZEOF_LONG_LONG 8
@@ -212,25 +239,50 @@
 /* Define to 1 if your <sys/time.h> declares `struct tm'. */
 /* #undef TM_IN_SYS_TIME */
 
+/* Enable extensions on AIX 3, Interix.  */
+#ifndef _ALL_SOURCE
+# define _ALL_SOURCE 1
+#endif
+/* Enable GNU extensions on systems that have them.  */
+#ifndef _GNU_SOURCE
+# define _GNU_SOURCE 1
+#endif
+/* Enable threading extensions on Solaris.  */
+#ifndef _POSIX_PTHREAD_SEMANTICS
+# define _POSIX_PTHREAD_SEMANTICS 1
+#endif
+/* Enable extensions on HP NonStop.  */
+#ifndef _TANDEM_SOURCE
+# define _TANDEM_SOURCE 1
+#endif
+/* Enable general extensions on Solaris.  */
+#ifndef __EXTENSIONS__
+# define __EXTENSIONS__ 1
+#endif
+
+
 /* Version number of package */
-#define VERSION "5.03"
+#define VERSION "5.11"
 
 /* Number of bits in a file offset, on hosts where this is settable. */
 /* #undef _FILE_OFFSET_BITS */
 
-/* Enable GNU extensions on systems that have them.  */
-#ifndef __FreeBSD__
-#ifndef _GNU_SOURCE
-# define _GNU_SOURCE 1
-#endif
-#endif
-
 /* Define to 1 to make fseeko visible on some hosts (e.g. glibc 2.2). */
 /* #undef _LARGEFILE_SOURCE */
 
 /* Define for large files, on AIX-style hosts. */
 /* #undef _LARGE_FILES */
 
+/* Define to 1 if on MINIX. */
+/* #undef _MINIX */
+
+/* Define to 2 if the system does not provide POSIX.1 features except with
+   this defined. */
+/* #undef _POSIX_1_SOURCE */
+
+/* Define to 1 if you need to in order for `stat' and other things to work. */
+/* #undef _POSIX_SOURCE */
+
 /* Define to empty if `const' does not conform to ANSI C. */
 /* #undef const */
 
@@ -244,6 +296,7 @@
 /* #undef size_t */
 
 
+#ifdef _FREEBSD_UNUSED_
 #ifndef HAVE_UINT8_T
 typedef unsigned char uint8_t;
 #endif
@@ -270,4 +323,5 @@
 typedef long int64_t;
 #endif
 #endif
+#endif /* _FREEBSD_UNUSED_ */
 
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libthr/thread/thr_private.h
--- a/head/lib/libthr/thread/thr_private.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libthr/thread/thr_private.h	Sat Apr 21 21:38:59 2012 +0300
@@ -26,7 +26,7 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * $FreeBSD: head/lib/libthr/thread/thr_private.h 233912 2012-04-05 02:24:08Z davidxu $
+ * $FreeBSD: head/lib/libthr/thread/thr_private.h 234424 2012-04-18 16:29:55Z gnn $
  */
 
 #ifndef _THR_PRIVATE_H
@@ -80,7 +80,7 @@
 TAILQ_HEAD(mutex_queue, pthread_mutex);
 
 /* Signal to do cancellation */
-#define	SIGCANCEL		32
+#define	SIGCANCEL		SIGTHR
 
 /*
  * Kernel fatal error handler macro.
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libulog/ulog_login.c
--- a/head/lib/libulog/ulog_login.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libulog/ulog_login.c	Sat Apr 21 21:38:59 2012 +0300
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/lib/libulog/ulog_login.c 234462 2012-04-19 15:28:15Z ed $");
 
 #include <sys/param.h>
 #include <sys/time.h>
@@ -55,7 +55,7 @@
 	SHA1_Init(&c);
 	SHA1_Update(&c, "libulog", 7);
 	SHA1_Update(&c, utx->ut_line, sizeof utx->ut_line);
-	SHA_Final(id, &c);
+	SHA1_Final(id, &c);
 
 	memcpy(utx->ut_id, id, MIN(sizeof utx->ut_id, sizeof id));
 }
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libusb/libusb10.c
--- a/head/lib/libusb/libusb10.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libusb/libusb10.c	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-/* $FreeBSD: head/lib/libusb/libusb10.c 234193 2012-04-12 18:06:30Z hselasky $ */
+/* $FreeBSD: head/lib/libusb/libusb10.c 234491 2012-04-20 14:29:45Z hselasky $ */
 /*-
  * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
  * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
@@ -651,17 +651,17 @@
 
 static struct libusb20_transfer *
 libusb10_get_transfer(struct libusb20_device *pdev,
-    uint8_t endpoint, uint8_t index)
+    uint8_t endpoint, uint8_t xfer_index)
 {
-	index &= 1;			/* double buffering */
+	xfer_index &= 1;	/* double buffering */
 
-	index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
+	xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
 
 	if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
 		/* this is an IN endpoint */
-		index |= 2;
+		xfer_index |= 2;
 	}
-	return (libusb20_tr_get_pointer(pdev, index));
+	return (libusb20_tr_get_pointer(pdev, xfer_index));
 }
 
 int
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libusb/libusb10_desc.c
--- a/head/lib/libusb/libusb10_desc.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libusb/libusb10_desc.c	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-/* $FreeBSD: head/lib/libusb/libusb10_desc.c 227404 2011-11-09 19:03:26Z hselasky $ */
+/* $FreeBSD: head/lib/libusb/libusb10_desc.c 234491 2012-04-20 14:29:45Z hselasky $ */
 /*-
  * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
  *
@@ -390,8 +390,8 @@
     struct libusb_bos_descriptor **bos)
 {
 	struct libusb_bos_descriptor *ptr;
-	struct libusb_usb_2_0_device_capability_descriptor *dcap_20;
-	struct libusb_ss_usb_device_capability_descriptor *ss_cap;
+	struct libusb_usb_2_0_device_capability_descriptor *dcap_20 = NULL;
+	struct libusb_ss_usb_device_capability_descriptor *ss_cap = NULL;
 
 	if (buf == NULL || bos == NULL || len < 1)
 		return (LIBUSB_ERROR_INVALID_PARAM);
@@ -438,7 +438,7 @@
 		    dtype == LIBUSB_DT_DEVICE_CAPABILITY) {
 			switch (((const uint8_t *)buf)[2]) {
 			case LIBUSB_USB_2_0_EXTENSION_DEVICE_CAPABILITY:
-				if (ptr->usb_2_0_ext_cap != NULL)
+				if (ptr->usb_2_0_ext_cap != NULL || dcap_20 == NULL)
 					break;
 				if (dlen < LIBUSB_USB_2_0_EXTENSION_DEVICE_CAPABILITY_SIZE)
 					break;
@@ -455,7 +455,7 @@
 				break;
 
 			case LIBUSB_SS_USB_DEVICE_CAPABILITY:
-				if (ptr->ss_usb_cap != NULL)
+				if (ptr->ss_usb_cap != NULL || ss_cap == NULL)
 					break;
 				if (dlen < LIBUSB_SS_USB_DEVICE_CAPABILITY_SIZE)
 					break;
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libusb/libusb10_io.c
--- a/head/lib/libusb/libusb10_io.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libusb/libusb10_io.c	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-/* $FreeBSD$ */
+/* $FreeBSD: head/lib/libusb/libusb10_io.c 234491 2012-04-20 14:29:45Z hselasky $ */
 /*-
  * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
  *
@@ -481,7 +481,7 @@
 {
 	libusb_context *ctx;
 	struct libusb_transfer *xfer;
-	volatile int complet;
+	int done;
 	int ret;
 
 	if (devh == NULL)
@@ -502,15 +502,15 @@
 	xfer->timeout = timeout;
 	xfer->buffer = data;
 	xfer->length = length;
-	xfer->user_data = (void *)&complet;
+	xfer->user_data = (void *)&done;
 	xfer->callback = libusb10_do_transfer_cb;
-	complet = 0;
+	done = 0;
 
 	if ((ret = libusb_submit_transfer(xfer)) < 0) {
 		libusb_free_transfer(xfer);
 		return (ret);
 	}
-	while (complet == 0) {
+	while (done == 0) {
 		if ((ret = libusb_handle_events(ctx)) < 0) {
 			libusb_cancel_transfer(xfer);
 			usleep(1000);	/* nice it */
@@ -581,7 +581,7 @@
 }
 
 uint8_t *
-libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t index)
+libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t off)
 {
 	uint8_t *ptr;
 	uint32_t n;
@@ -589,35 +589,35 @@
 	if (transfer->num_iso_packets < 0)
 		return (NULL);
 
-	if (index >= (uint32_t)transfer->num_iso_packets)
+	if (off >= (uint32_t)transfer->num_iso_packets)
 		return (NULL);
 
 	ptr = transfer->buffer;
 	if (ptr == NULL)
 		return (NULL);
 
-	for (n = 0; n != index; n++) {
+	for (n = 0; n != off; n++) {
 		ptr += transfer->iso_packet_desc[n].length;
 	}
 	return (ptr);
 }
 
 uint8_t *
-libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t index)
+libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t off)
 {
 	uint8_t *ptr;
 
 	if (transfer->num_iso_packets < 0)
 		return (NULL);
 
-	if (index >= (uint32_t)transfer->num_iso_packets)
+	if (off >= (uint32_t)transfer->num_iso_packets)
 		return (NULL);
 
 	ptr = transfer->buffer;
 	if (ptr == NULL)
 		return (NULL);
 
-	ptr += transfer->iso_packet_desc[0].length * index;
+	ptr += transfer->iso_packet_desc[0].length * off;
 
 	return (ptr);
 }
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libusb/libusb20.c
--- a/head/lib/libusb/libusb20.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libusb/libusb20.c	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-/* $FreeBSD: head/lib/libusb/libusb20.c 225659 2011-09-19 08:52:06Z hselasky $ */
+/* $FreeBSD: head/lib/libusb/libusb20.c 234491 2012-04-20 14:29:45Z hselasky $ */
 /*-
  * Copyright (c) 2008-2009 Hans Petter Selasky. All rights reserved.
  *
@@ -948,9 +948,8 @@
 	}
 
 	error = pdev->methods->get_config_index(pdev, &cfg_index);
-	if (error) {
-		cfg_index = 0 - 1;	/* current config index */
-	}
+	if (error)
+		cfg_index = 0xFF;	/* current config index */
 	if (do_close) {
 		if (libusb20_dev_close(pdev)) {
 			/* ignore */
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libusb/libusb20_desc.c
--- a/head/lib/libusb/libusb20_desc.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libusb/libusb20_desc.c	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-/* $FreeBSD: head/lib/libusb/libusb20_desc.c 227404 2011-11-09 19:03:26Z hselasky $ */
+/* $FreeBSD: head/lib/libusb/libusb20_desc.c 234491 2012-04-20 14:29:45Z hselasky $ */
 /*-
  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
  *
@@ -69,7 +69,7 @@
 	uint16_t niface_no_alt;
 	uint16_t niface;
 	uint16_t nendpoint;
-	uint8_t iface_no;
+	uint16_t iface_no;
 
 	ptr = config_desc;
 	if (ptr[1] != LIBUSB20_DT_CONFIG) {
@@ -82,7 +82,7 @@
 	niface_no_alt = 0;
 	nendpoint = 0;
 	niface = 0;
-	iface_no = 0 - 1;
+	iface_no = 0xFFFF;
 	ptr = NULL;
 
 	/* get "wTotalLength" and setup "pcdesc" */
@@ -155,7 +155,7 @@
 
 	/* reset states */
 	niface = 0;
-	iface_no = 0 - 1;
+	iface_no = 0xFFFF;
 	ptr = NULL;
 	lub_interface--;
 	lub_endpoint--;
@@ -450,7 +450,7 @@
 						 * and should be
 						 * correct:
 						 */
-						ps->len = 0 - 1;
+						ps->len = 0xFFFF;
 					}
 					src_len = libusb20_me_get_1(pd, 0);
 					src_ptr = LIBUSB20_ADD_BYTES(ps->ptr, 1);
@@ -465,7 +465,7 @@
 				case LIBUSB20_ME_IS_DECODED:
 					/* reserve 3 length bytes */
 					src_len = libusb20_me_encode(NULL,
-					    0 - 1 - 3, ps->ptr);
+					    0xFFFF - 3, ps->ptr);
 					src_ptr = NULL;
 					break;
 
@@ -476,7 +476,7 @@
 				}
 
 				if (src_len > 0xFE) {
-					if (src_len > (uint16_t)(0 - 1 - 3))
+					if (src_len > (0xFFFF - 3))
 						/* overflow */
 						goto done;
 
@@ -516,7 +516,7 @@
 						uint16_t dummy;
 
 						dummy = libusb20_me_encode(buf,
-						    0 - 1 - 3, ps->ptr);
+						    0xFFFF - 3, ps->ptr);
 					} else {
 						bcopy(src_ptr, buf, src_len);
 					}
diff -r 18bc588aa6ad -r 455aabca3842 head/lib/libusb/libusb20_ugen20.c
--- a/head/lib/libusb/libusb20_ugen20.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/lib/libusb/libusb20_ugen20.c	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-/* $FreeBSD: head/lib/libusb/libusb20_ugen20.c 223495 2011-06-24 11:14:09Z hselasky $ */
+/* $FreeBSD: head/lib/libusb/libusb20_ugen20.c 234491 2012-04-20 14:29:45Z hselasky $ */
 /*-
  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
  *
@@ -104,7 +104,7 @@
 		temp += (*ptr - '0');
 		if (temp >= 1000000) {
 			/* catch overflow early */
-			return (0 - 1);
+			return (0xFFFFFFFF);
 		}
 		ptr++;
 	}
diff -r 18bc588aa6ad -r 455aabca3842 head/libexec/ulog-helper/ulog-helper.c
--- a/head/libexec/ulog-helper/ulog-helper.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/libexec/ulog-helper/ulog-helper.c	Sat Apr 21 21:38:59 2012 +0300
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/libexec/ulog-helper/ulog-helper.c 223367 2011-06-21 17:59:51Z ed $");
+__FBSDID("$FreeBSD: head/libexec/ulog-helper/ulog-helper.c 234469 2012-04-19 21:12:08Z ed $");
 
 #include <pwd.h>
 #include <unistd.h>
@@ -46,6 +46,28 @@
  * username.  It does allow users to log arbitrary hostnames.
  */
 
+static const char *
+get_username(void)
+{
+	const struct passwd *pw;
+	const char *login;
+	uid_t uid;
+
+	/*
+	 * Attempt to determine the username corresponding to this login
+	 * session.  First, validate the results of getlogin() against
+	 * the password database.  If getlogin() returns invalid data,
+	 * return an arbitrary username corresponding to this uid.
+	 */
+	uid = getuid();
+	if ((login = getlogin()) != NULL && (pw = getpwnam(login)) != NULL &&
+	    pw->pw_uid == uid)
+		return (login);
+	if ((pw = getpwuid(uid)) != NULL)
+		return (pw->pw_name);
+	return (NULL);
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -57,7 +79,7 @@
 
 	if ((argc == 2 || argc == 3) && strcmp(argv[1], "login") == 0) {
 		/* Username. */
-		user = user_from_uid(getuid(), 1);
+		user = get_username();
 		if (user == NULL)
 			return (EX_OSERR);
 
diff -r 18bc588aa6ad -r 455aabca3842 head/sbin/geom/class/multipath/geom_multipath.c
--- a/head/sbin/geom/class/multipath/geom_multipath.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sbin/geom/class/multipath/geom_multipath.c	Sat Apr 21 21:38:59 2012 +0300
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/geom/class/multipath/geom_multipath.c 227473 2011-11-12 20:01:30Z mav $");
+__FBSDID("$FreeBSD: head/sbin/geom/class/multipath/geom_multipath.c 234415 2012-04-18 09:42:14Z mav $");
 #include <sys/param.h>
 #include <errno.h>
 #include <paths.h>
@@ -55,17 +55,28 @@
 		"create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
 		{
 			{ 'A', "active_active", NULL, G_TYPE_BOOL },
+			{ 'R', "active_read", NULL, G_TYPE_BOOL },
 			G_OPT_SENTINEL
 		},
-		"[-vA] name prov ..."
+		"[-vAR] name prov ..."
 	},
 	{
 		"label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, mp_main,
 		{
 			{ 'A', "active_active", NULL, G_TYPE_BOOL },
+			{ 'R', "active_read", NULL, G_TYPE_BOOL },
 			G_OPT_SENTINEL
 		},
-		"[-vA] name prov ..."
+		"[-vAR] name prov ..."
+	},
+	{ "configure", G_FLAG_VERBOSE, NULL,
+		{
+			{ 'A', "active_active", NULL, G_TYPE_BOOL },
+			{ 'P', "active_passive", NULL, G_TYPE_BOOL },
+			{ 'R', "active_read", NULL, G_TYPE_BOOL },
+			G_OPT_SENTINEL
+		},
+		"[-vAPR] name"
 	},
 	{
 		"add", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
@@ -196,6 +207,8 @@
 	}
 	strlcpy(md.md_uuid, ptr, sizeof (md.md_uuid));
 	md.md_active_active = gctl_get_int(req, "active_active");
+	if (gctl_get_int(req, "active_read"))
+		md.md_active_active = 2;
 	free(ptr);
 
 	/*
diff -r 18bc588aa6ad -r 455aabca3842 head/sbin/geom/class/multipath/gmultipath.8
--- a/head/sbin/geom/class/multipath/gmultipath.8	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sbin/geom/class/multipath/gmultipath.8	Sat Apr 21 21:38:59 2012 +0300
@@ -22,9 +22,9 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/sbin/geom/class/multipath/gmultipath.8 233458 2012-03-25 09:21:09Z joel $
+.\" $FreeBSD: head/sbin/geom/class/multipath/gmultipath.8 234415 2012-04-18 09:42:14Z mav $
 .\"
-.Dd October 31, 2011
+.Dd April 18, 2012
 .Dt GMULTIPATH 8
 .Os
 .Sh NAME
@@ -33,15 +33,19 @@
 .Sh SYNOPSIS
 .Nm
 .Cm create
-.Op Fl Av
+.Op Fl ARv
 .Ar name
 .Ar prov ...
 .Nm
 .Cm label
-.Op Fl Av
+.Op Fl ARv
 .Ar name
 .Ar prov ...
 .Nm
+.Cm configure
+.Op Fl APRv
+.Ar name
+.Nm
 .Cm add
 .Op Fl v
 .Ar name prov
@@ -121,7 +125,9 @@
 sector sizes.
 .Pp
 .Fl A
-option enables Active/Active mode, otherwise Active/Passive mode is used
+option enables Active/Active mode,
+.Fl R
+option enables Active/Read mode, otherwise Active/Passive mode is used
 by default.
 .It Cm label
 Create multipath device with
@@ -134,8 +140,19 @@
 Providers with no matching metadata detected will not be added to the device.
 .Pp
 .Fl A
-option enables Active/Active mode, otherwise Active/Passive mode is used
+option enables Active/Active mode,
+.Fl R
+option enables Active/Read mode, otherwise Active/Passive mode is used
 by default.
+.It Cm configure
+Configure the given multipath device.
+.Pp
+.Fl A
+option enables Active/Active mode,
+.Fl P
+option enables Active/Passive mode,
+.Fl R
+option enables Active/Read mode.
 .It Cm add
 Add the given provider as a path to the given multipath device.
 Should normally be used only for devices created with
@@ -221,7 +238,8 @@
 system operator who will use tools and knowledge of their own storage
 subsystem to make the correct configuration selection.
 .Pp
-There are Active/Passive and Active/Active operation modes supported.
+There are Active/Passive, Active/Read and Active/Active operation modes
+supported.
 In Active/Passive mode only one path has I/O moving on it
 at any point in time.
 This I/O continues until an I/O is returned with
@@ -231,6 +249,12 @@
 In Active/Active mode all paths not marked FAIL may handle I/O same time.
 Requests are distributed between paths to equalize load.
 For capable devices it allows to utilize bandwidth of all paths.
+In Active/Read mode all paths not marked FAIL may handle reads same time,
+but unlike Active/Active only one path handles write requests at any
+point in time.
+It allows to closer follow original write request order if above layer
+needs it for data consistency (not waiting for requisite write completion
+before sending dependent write).
 .Pp
 When new devices are added to the system the
 .Nm MULTIPATH
diff -r 18bc588aa6ad -r 455aabca3842 head/sbin/geom/class/raid/graid.8
--- a/head/sbin/geom/class/raid/graid.8	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sbin/geom/class/raid/graid.8	Sat Apr 21 21:38:59 2012 +0300
@@ -22,9 +22,9 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/sbin/geom/class/raid/graid.8 229886 2012-01-09 23:01:42Z jimharris $
+.\" $FreeBSD: head/sbin/geom/class/raid/graid.8 234458 2012-04-19 12:30:12Z mav $
 .\"
-.Dd October 26, 2011
+.Dd April 19, 2012
 .Dt GRAID 8
 .Os
 .Sh NAME
@@ -242,7 +242,8 @@
 The GEOM RAID class follows a modular design, allowing different RAID levels
 to be used.
 Support for the following RAID levels is currently implemented: RAID0, RAID1,
-RAID1E, RAID10, SINGLE, CONCAT.
+RAID1E, RAID5, RAID10, SINGLE, CONCAT.
+RAID5 support is read-only and only for volumes in optimal state.
 .Sh RAID LEVEL MIGRATION
 The GEOM RAID class has no support for RAID level migration, allowed by some
 metadata formats.
diff -r 18bc588aa6ad -r 455aabca3842 head/sbin/growfs/growfs.c
--- a/head/sbin/growfs/growfs.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sbin/growfs/growfs.c	Sat Apr 21 21:38:59 2012 +0300
@@ -47,7 +47,7 @@
 #endif /* not lint */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/growfs/growfs.c 234314 2012-04-15 15:15:16Z trasz $");
+__FBSDID("$FreeBSD: head/sbin/growfs/growfs.c 234420 2012-04-18 13:50:17Z trasz $");
 
 #include <sys/param.h>
 #include <sys/ioctl.h>
@@ -1235,7 +1235,7 @@
 		columns = 80;	/* last resort */
 
 	DBG_LEAVE;
-	return columns;
+	return (columns);
 }
 
 /*
@@ -1526,7 +1526,7 @@
 	DBG_CLOSE;
 
 	DBG_LEAVE;
-	return 0;
+	return (0);
 }
 
 /*
diff -r 18bc588aa6ad -r 455aabca3842 head/sbin/iscontrol/Makefile
--- a/head/sbin/iscontrol/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sbin/iscontrol/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-# $FreeBSD: head/sbin/iscontrol/Makefile 234233 2012-04-13 18:21:56Z jpaetzel $
+# $FreeBSD: head/sbin/iscontrol/Makefile 234425 2012-04-18 16:47:57Z jpaetzel $
 
 SRCS= iscontrol.c pdu.c fsm.c config.c login.c auth_subr.c misc.c
 PROG= iscontrol
@@ -7,8 +7,7 @@
 S= ${.CURDIR}/../../sys
 
 WARNS?=	3
-CFLAGS += -I$S
-CFLAGS += -g -DDEBUG
+CFLAGS+=	-I$S
 
 MAN= iscsi.conf.5 iscontrol.8
 
diff -r 18bc588aa6ad -r 455aabca3842 head/sbin/iscontrol/iscontrol.c
--- a/head/sbin/iscontrol/iscontrol.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sbin/iscontrol/iscontrol.c	Sat Apr 21 21:38:59 2012 +0300
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/iscontrol/iscontrol.c 234233 2012-04-13 18:21:56Z jpaetzel $");
+__FBSDID("$FreeBSD: head/sbin/iscontrol/iscontrol.c 234425 2012-04-18 16:47:57Z jpaetzel $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -44,13 +44,15 @@
 #include <arpa/inet.h>
 #include <sys/ioctl.h>
 #include <netdb.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <string.h>
+#include <err.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <libgen.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <time.h>
+#include <unistd.h>
 #include <camlib.h>
 
 #include <dev/iscsi/initiator/iscsi.h>
@@ -111,6 +113,13 @@
      .immediateData		= TRUE,
 };
 
+static void
+usage(const char *pname)
+{
+	fprintf(stderr, "usage: %s " USAGE "\n", pname);
+	exit(1);
+}
+
 int
 lookup(token_t *tbl, char *m)
 {
@@ -135,8 +144,8 @@
      iscsidev = "/dev/"ISCSIDEV;
      fd = NULL;
      pname = vv[0];
-     if((p = strrchr(pname, '/')) != NULL)
-	  pname = p + 1;
+     if ((pname = basename(pname)) == NULL)
+	  err(1, "basename");
 
      kw = ta = 0;
      disco = 0;
@@ -145,17 +154,21 @@
       | check for driver & controller version match
       */
      n = 0;
-     if(sysctlbyname("net.iscsi_initiator.driver_version", 0, &n, 0, 0) != 0)
-	  perror("sysctlbyname");
+#define VERSION_OID_S	"net.iscsi_initiator.driver_version"
+     if (sysctlbyname(VERSION_OID_S, 0, &n, 0, 0) != 0) {
+	  if (errno == ENOENT)
+		errx(1, "sysctlbyname(\"" VERSION_OID_S "\") "
+			"failed; is the iscsi driver loaded?");
+	  err(1, "sysctlbyname(\"" VERSION_OID_S "\")");
+     }
      v = malloc(n+1);
-     if(sysctlbyname("net.iscsi_initiator.driver_version", v, &n, 0, 0) != 0)
-	  perror("sysctlbyname");
+     if (v == NULL)
+	  err(1, "malloc");
+     if (sysctlbyname(VERSION_OID_S, v, &n, 0, 0) != 0)
+	  err(1, "sysctlbyname");
 
-     if(strncmp(version, v, 3)) {
-	  fprintf(stderr, "versions missmatch\n");
-	  exit(1);
-     }
-
+     if (strncmp(version, v, 3) != 0)
+	  errx(1, "versions mismatch");
 
      while((ch = getopt(cc, vv, OPTIONS)) != -1) {
 	  switch(ch) {
@@ -164,10 +177,8 @@
 	       break;
 	  case 'c':
 	       fd = fopen(optarg, "r");
-	       if(fd == NULL) {
-		    perror(optarg);
-		    exit(1);
-	       }
+	       if (fd == NULL)
+		    err(1, "fopen(\"%s\")", optarg);
 	       break;
 	  case 'd':
 	       disco = 1;
@@ -182,9 +193,7 @@
 	       pidfile = optarg;
 	       break;
 	  default:
-	  badu:
-	       fprintf(stderr, "Usage: %s %s\n", pname, USAGE);
-	       exit(1);
+	       usage(pname);
 	  }
      }
      if(fd == NULL)
@@ -205,8 +214,8 @@
 	  op->targetAddress = ta;
 
      if(op->targetAddress == NULL) {
-	  fprintf(stderr, "No target!\n");
-	  goto badu;
+	  warnx("no target specified!");
+	  usage(pname);
      }
      q = op->targetAddress;
      if(*q == '[' && (q = strchr(q, ']')) != NULL) {
@@ -224,7 +233,7 @@
 	  op->targetPortalGroupTag = atoi(p);
      }
      if(op->initiatorName == 0) {
-	  char	hostname[256];
+	  char	hostname[MAXHOSTNAMELEN];
 
 	  if(op->iqn) {
 	       if(gethostname(hostname, sizeof(hostname)) == 0)
diff -r 18bc588aa6ad -r 455aabca3842 head/share/man/man4/iscsi_initiator.4
--- a/head/share/man/man4/iscsi_initiator.4	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/share/man/man4/iscsi_initiator.4	Sat Apr 21 21:38:59 2012 +0300
@@ -22,11 +22,11 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/share/man/man4/iscsi_initiator.4 234233 2012-04-13 18:21:56Z jpaetzel $
+.\" $FreeBSD: head/share/man/man4/iscsi_initiator.4 234538 2012-04-21 11:51:31Z joel $
 .\"
 .Dd August 3, 2010
+.Dt ISCSI_INITIATOR 4
 .Os
-.Dt ISCSI_INITIATOR 4
 .Sh NAME
 .Nm iscsi_initiator
 .Nd kernel driver for the iSCSI protocol
@@ -75,9 +75,9 @@
 .Em n .
 .It Va net.iscsi.n.stats
 are some statistics for session
-.EM n
+.Em n
 .It Va net.iscsi.n.pid
-is the 
+is the
 .Em "process id"
 of the userland side of session
 .Em n ,
@@ -85,11 +85,11 @@
 .Xr iscontrol 8 .
 .El
 .Sh FILES
-The 
+The
 .Nm
 driver creates the following:
+.Pp
 .Bl -tag -width ".Pa /dev/iscsi%dxx" -compact
-.Pp
 .It Pa /dev/iscsi
 used to create new sessions.
 .It Pa /dev/iscsi%d
@@ -102,7 +102,7 @@
 .Sh STANDARDS
 iSCSI RFC 3720
 .\" .Sh HISTORY
-.\" .Sh AUTHORS
+.Sh AUTHORS
 This software was written by Daniel Braniss <danny at cs.huji.ac.il>
 .Sh BUGS
 The lun discovery method is old-fashioned.
diff -r 18bc588aa6ad -r 455aabca3842 head/share/man/man9/Makefile
--- a/head/share/man/man9/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/share/man/man9/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-# $FreeBSD: head/share/man/man9/Makefile 233505 2012-03-26 09:34:17Z melifaro $
+# $FreeBSD: head/share/man/man9/Makefile 234463 2012-04-19 15:29:09Z pluknet $
 
 MAN=	accept_filter.9 \
 	accf_data.9 \
@@ -1250,6 +1250,7 @@
 	store.9 suword32.9 \
 	store.9 suword64.9
 MLINKS+=swi.9 swi_add.9 \
+	swi.9 swi_remove.9 \
 	swi.9 swi_sched.9
 MLINKS+=sx.9 sx_assert.9 \
 	sx.9 sx_destroy.9 \
diff -r 18bc588aa6ad -r 455aabca3842 head/share/man/man9/swi.9
--- a/head/share/man/man9/swi.9	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/share/man/man9/swi.9	Sat Apr 21 21:38:59 2012 +0300
@@ -22,23 +22,22 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/share/man/man9/swi.9 234188 2012-04-12 16:02:45Z pluknet $
+.\" $FreeBSD: head/share/man/man9/swi.9 234465 2012-04-19 16:13:15Z pluknet $
 .\"
-.Dd April 12, 2012
+.Dd April 19, 2012
 .Dt SWI 9
 .Os
 .Sh NAME
 .Nm swi_add ,
+.Nm swi_remove ,
 .Nm swi_sched
 .Nd register and schedule software interrupt handlers
 .Sh SYNOPSIS
 .In sys/param.h
 .In sys/bus.h
 .In sys/interrupt.h
-.Vt "extern struct ithd *tty_ithd" ;
-.Vt "extern struct ithd *clk_ithd" ;
-.Vt "extern void *net_ih" ;
-.Vt "extern void *softclock_ih" ;
+.Vt "extern struct intr_event *tty_intr_event" ;
+.Vt "extern struct intr_event *clk_intr_event" ;
 .Vt "extern void *vm_ih" ;
 .Ft int
 .Fo swi_add
@@ -50,6 +49,8 @@
 .Fa "enum intr_type flags"
 .Fa "void **cookiep"
 .Fc
+.Ft int
+.Fn swi_remove "void *cookie"
 .Ft void
 .Fn swi_sched "void *cookie" "int flags"
 .Sh DESCRIPTION
@@ -114,6 +115,14 @@
 and is used to schedule the handler for execution later on.
 .Pp
 The
+.Fn swi_remove
+function is used to teardown an interrupt handler pointed to by the
+.Fa cookie
+argument.
+It detaches the interrupt handler from the associated interrupt event
+and frees its memory.
+.Pp
+The
 .Fn swi_sched
 function is used to schedule an interrupt handler and its associated thread to
 run.
@@ -141,30 +150,29 @@
 .El
 .Pp
 The
-.Va tty_ithd
+.Va tty_intr_event
 and
-.Va clk_ithd
-variables contain pointers to the software interrupt threads for the tty and
+.Va clk_intr_event
+variables contain pointers to the software interrupt handlers for the tty and
 clock software interrupts, respectively.
-.Va tty_ithd
+.Va tty_intr_event
 is used to hang tty software interrupt handlers off of the same thread.
-.Va clk_ithd
+.Va clk_intr_event
 is used to hang delayed handlers off of the clock software interrupt thread so
 that the functionality of
 .Fn setdelayed
 can be obtained in conjunction with
 .Dv SWI_DELAY .
 The
-.Va net_ih ,
-.Va softclock_ih ,
-and
 .Va vm_ih
-handler cookies are used to schedule software interrupt threads to run for the
-networking stack, clock interrupt, and VM subsystem respectively.
+handler cookie is used to schedule software interrupt threads to run for the
+VM subsystem.
 .Sh RETURN VALUES
 The
 .Fn swi_add
-function returns zero on success and non-zero on failure.
+and
+.Fn swi_remove
+functions return zero on success and non-zero on failure.
 .Sh ERRORS
 The
 .Fn swi_add
@@ -201,6 +209,17 @@
 already has at least one handler, or the interrupt event already has an
 exclusive handler.
 .El
+.Pp
+The
+.Fn swi_remove
+function will fail if:
+.Bl -tag -width Er
+.It Bq Er EINVAL
+A software interrupt handler pointed to by
+.Fa cookie
+is
+.Dv NULL .
+.El
 .Sh SEE ALSO
 .Xr ithread 9 ,
 .Xr taskqueue 9
@@ -221,6 +240,10 @@
 .Fn schedsoft*
 functions which date back to at least
 .Bx 4.4 .
+The
+.Fn swi_remove
+function first appeared in
+.Fx 6.1 .
 .Sh BUGS
 Most of the global variables described in this manual page should not be
 global, or at the very least should not be declared in
diff -r 18bc588aa6ad -r 455aabca3842 head/share/misc/bsd-family-tree
--- a/head/share/misc/bsd-family-tree	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/share/misc/bsd-family-tree	Sat Apr 21 21:38:59 2012 +0300
@@ -246,13 +246,14 @@
  |     |        |     |      | NetBSD 5.1      |                       |
  |  FreeBSD  FreeBSD  |      |                 |                       |
  |    8.2      7.4    |      |                 |               DragonFly 2.10.1
- |     v              |      |             OpenBSD 4.9                 |
- |                 Mac OS X  |                 |                       |
- |                   10.7    |                 |                       |
- |                    |      |             OpenBSD 5.0                 |
- +--FreeBSD           |      |                 |                       |
- |    9.0             |      |                 |               DragonFly 3.0.1
- |     v              |      |                 |                       |
+ |     |              |      |             OpenBSD 4.9                 |
+ |     `-----.     Mac OS X  |                 |                       |
+ |            \      10.7    |                 |                       |
+ |             |      |      |             OpenBSD 5.0                 |
+ *--FreeBSD    |      |      |                 |                       |
+ |    9.0      |      |      |                 |               DragonFly 3.0.1
+ |     v    FreeBSD   |      |                 |                       |
+ |            8.3     |      |                 |                       |
  |                    |      |                 |                       |
 FreeBSD 10 -current   |  NetBSD -current  OpenBSD -current             |
  |                    |      |                 |                       |
@@ -544,6 +545,7 @@
 OpenBSD 5.0		2011-11-01 [OBD]
 FreeBSD 9.0		2012-01-12 [FBD]
 DragonFly 3.0.1		2012-02-21 [DFB]
+FreeBSD 8.3		2012-04-18 [FBD]
 
 Bibliography
 ------------------------
@@ -604,4 +606,4 @@
 Copyright (c) 1997-2012 Wolfram Schneider <wosch at FreeBSD.ORG>
 URL: http://cvsweb.freebsd.org/src/share/misc/bsd-family-tree
 
-$FreeBSD: head/share/misc/bsd-family-tree 232000 2012-02-22 17:52:53Z maxim $
+$FreeBSD: head/share/misc/bsd-family-tree 234423 2012-04-18 15:23:21Z maxim $
diff -r 18bc588aa6ad -r 455aabca3842 head/share/misc/committers-src.dot
--- a/head/share/misc/committers-src.dot	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/share/misc/committers-src.dot	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-# $FreeBSD: head/share/misc/committers-src.dot 234236 2012-04-13 20:58:01Z jasone $
+# $FreeBSD: head/share/misc/committers-src.dot 234430 2012-04-18 18:35:19Z dteske $
 
 # This file is meant to list all FreeBSD src committers and describe the
 # mentor-mentee relationships between them.
@@ -127,6 +127,7 @@
 dg [label="David Greenman\ndg at FreeBSD.org\n1993/06/14"]
 dim [label="Dimitry Andric\ndim at FreeBSD.org\n2010/08/30"]
 dougb [label="Doug Barton\ndougb at FreeBSD.org\n2000/10/26"]
+dteske [label="Devin Teske\ndteske at FreeBSD.org\n2012/04/10"]
 dwmalone [label="David Malone\ndwmalone at FreeBSD.org\n2000/07/11"]
 ed [label="Ed Schouten\ned at FreeBSD.org\n2008/05/22"]
 edwin [label="Edwin Groothuis\nedwin at FreeBSD.org\n2007/06/25"]
@@ -362,6 +363,7 @@
 eivind -> rwatson
 
 emaste -> rstone
+emaste -> dteske
 
 emax -> markus
 
diff -r 18bc588aa6ad -r 455aabca3842 head/share/misc/scsi_modes
--- a/head/share/misc/scsi_modes	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/share/misc/scsi_modes	Sat Apr 21 21:38:59 2012 +0300
@@ -29,7 +29,7 @@
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 #
-# $FreeBSD: head/share/misc/scsi_modes 225950 2011-10-03 20:32:55Z ken $
+# $FreeBSD: head/share/misc/scsi_modes 234376 2012-04-17 11:13:03Z mav $
 #
 # The ordering is alphabetical by page name, as it appears in the SCSI spec.
 
@@ -92,6 +92,25 @@
 	{Reserved} *i1
 }
 
+0x15 "Extended Page";
+
+0x16 "Extended Device-Type Specific Page";
+
+0x1c "Informational Exceptions Control Page" {
+	{PERF} t1
+	{Reserved} *t1
+	{EBF} t1
+	{EWasc} t1
+	{DExcpt} t1
+	{TEST} t1
+	{EBACKERR} t1
+	{LogErr} t1
+	{Reserved} *t4
+	{MRIE} b4
+	{Interval Timer} i4
+	{Report Count} i4
+}
+
 0x09 "Peripheral Device Page" {
 	{Interface Identifier} i2
 	{Reserved} *i1
@@ -100,7 +119,7 @@
 	{Reserved} *i1
 }
 
-0x1a "Power Control" {
+0x1a "Power Condition Page" {
 	{Reserved} *i1
 	{Reserved} *t6
 	{Idle} t1
@@ -109,6 +128,10 @@
 	{Standby Condition Timer} i4
 }
 
+0x18 "Protocol-Specific LUN Page";
+
+0x19 "Protocol-Specific Port Page";
+
 # DIRECT ACCESS DEVICES
 0x08 "Caching Page" {
 	{IC} t1
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/amd64/conf/GENERIC
--- a/head/sys/amd64/conf/GENERIC	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/amd64/conf/GENERIC	Sat Apr 21 21:38:59 2012 +0300
@@ -16,12 +16,13 @@
 # If you are in doubt as to the purpose or necessity of a line, check first
 # in NOTES.
 #
-# $FreeBSD: head/sys/amd64/conf/GENERIC 233427 2012-03-24 18:08:28Z marius $
+# $FreeBSD: head/sys/amd64/conf/GENERIC 234504 2012-04-20 21:37:42Z brooks $
 
 cpu		HAMMER
 ident		GENERIC
 
 makeoptions	DEBUG=-g		# Build kernel with gdb(1) debug symbols
+makeoptions	WITH_CTF=1		# Run ctfconvert(1) for DTrace support
 
 options 	SCHED_ULE		# ULE scheduler
 options 	PREEMPTION		# Enable kernel thread preemption
@@ -63,8 +64,8 @@
 options 	CAPABILITY_MODE		# Capsicum capability mode
 options 	CAPABILITIES		# Capsicum capabilities
 options 	MAC			# TrustedBSD MAC Framework
-#options 	KDTRACE_FRAME		# Ensure frames are compiled in
-#options 	KDTRACE_HOOKS		# Kernel DTrace hooks
+options 	KDTRACE_FRAME		# Ensure frames are compiled in
+options 	KDTRACE_HOOKS		# Kernel DTrace hooks
 options 	INCLUDE_CONFIG_FILE     # Include this file in kernel
 
 # Debugging support.  Always need this:
@@ -74,6 +75,7 @@
 # For full debugger support use this instead:
 options 	DDB			# Support DDB.
 options 	GDB			# Support remote GDB.
+options 	DDB_CTF			# kernel ELF linker loads CTF data
 options 	DEADLKRES		# Enable the deadlock resolver
 options 	INVARIANTS		# Enable calls of extra sanity checking
 options 	INVARIANT_SUPPORT	# Extra sanity checks of internal structures, required by INVARIANTS
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/boot/efi/Makefile.inc
--- a/head/sys/boot/efi/Makefile.inc	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/boot/efi/Makefile.inc	Sat Apr 21 21:38:59 2012 +0300
@@ -1,7 +1,14 @@
-# $FreeBSD$
+# $FreeBSD: head/sys/boot/efi/Makefile.inc 234493 2012-04-20 15:01:23Z marcel $
 
 BINDIR?=	/boot
 
+.if ${MACHINE_CPUARCH} == "amd64"
+CFLAGS+=        -m32 -march=i386
+ACFLAGS+=       -m32
+LDFLAGS+=       -m elf_i386_fbsd
+AFLAGS+=        --32
+.endif
+
 # Options used when building app-specific efi components
 CFLAGS+=	-ffreestanding -fshort-wchar -Wformat
 LDFLAGS+=	-nostdlib
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/boot/i386/efi/Makefile
--- a/head/sys/boot/i386/efi/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/boot/i386/efi/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-# $FreeBSD$
+# $FreeBSD: head/sys/boot/i386/efi/Makefile 234493 2012-04-20 15:01:23Z marcel $
 
 NO_MAN=
 WITHOUT_SSP=
@@ -35,7 +35,7 @@
 FILES=	loader.efi
 FILESMODE_loader.efi=	${BINMODE}
 
-LDSCRIPT=	${.CURDIR}/ldscript.${MACHINE_ARCH}
+LDSCRIPT=	${.CURDIR}/ldscript.i386
 LDFLAGS=	-Wl,-T${LDSCRIPT} -shared -symbolic
 
 ${PROG}:	${LDSCRIPT}
@@ -64,6 +64,13 @@
 CFLAGS+=	-I${.CURDIR}/../btx/lib
 
 DPADD=		${LIBFICL} ${LIBEFI} ${LIBSTAND}
-LDADD=		${LIBFICL} ${LIBEFI} -lstand
+LDADD=		${LIBFICL} ${LIBEFI} ${LIBSTAND}
 
 .include <bsd.prog.mk>
+
+.if ${MACHINE_CPUARCH} == "amd64"
+CFLAGS+=        -I.
+beforedepend ${OBJS}: machine
+machine:
+	ln -sf ${.CURDIR}/../../../i386/include machine
+.endif
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/cam/ata/ata_da.c
--- a/head/sys/cam/ata/ata_da.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/cam/ata/ata_da.c	Sat Apr 21 21:38:59 2012 +0300
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/cam/ata/ata_da.c 230921 2012-02-02 19:02:15Z mav $");
+__FBSDID("$FreeBSD: head/sys/cam/ata/ata_da.c 234414 2012-04-18 08:55:26Z mav $");
 
 #include "opt_ada.h"
 #include "opt_ata.h"
@@ -456,17 +456,16 @@
 	struct	cam_periph *periph;
 	struct	ada_softc *softc;
 	union ccb *ccb;
-	int error;
 
 	periph = (struct cam_periph *)dp->d_drv1;
 	if (periph == NULL)
 		return (ENXIO);	
 
 	cam_periph_lock(periph);
-	if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
+	if (cam_periph_hold(periph, PRIBIO) != 0) {
 		cam_periph_unlock(periph);
 		cam_periph_release(periph);
-		return (error);
+		return (0);
 	}
 
 	softc = (struct ada_softc *)periph->softc;
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/cam/scsi/scsi_da.c
--- a/head/sys/cam/scsi/scsi_da.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/cam/scsi/scsi_da.c	Sat Apr 21 21:38:59 2012 +0300
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_da.c 234177 2012-04-12 12:58:14Z trasz $");
+__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_da.c 234374 2012-04-17 10:44:28Z trasz $");
 
 #include <sys/param.h>
 
@@ -938,7 +938,9 @@
 	if (error != 0)
 		xpt_print(periph->path, "unable to retrieve capacity data");
 
-	if (periph->flags & CAM_PERIPH_INVALID)
+	if (periph->flags & CAM_PERIPH_INVALID ||
+	    softc->disk->d_sectorsize == 0 ||
+	    softc->disk->d_mediasize == 0)
 		error = ENXIO;
 
 	if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/conf/files
--- a/head/sys/conf/files	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/conf/files	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-# $FreeBSD: head/sys/conf/files 234248 2012-04-13 23:07:32Z marius $
+# $FreeBSD: head/sys/conf/files 234458 2012-04-19 12:30:12Z mav $
 #
 # The long compile-with and dependency lines are required because of
 # limitations in config: backslash-newline doesn't work in strings, and
@@ -2419,6 +2419,7 @@
 geom/raid/tr_raid0.c		optional geom_raid
 geom/raid/tr_raid1.c		optional geom_raid
 geom/raid/tr_raid1e.c		optional geom_raid
+geom/raid/tr_raid5.c		optional geom_raid
 geom/raid3/g_raid3.c		optional geom_raid3
 geom/raid3/g_raid3_ctl.c	optional geom_raid3
 geom/shsec/g_shsec.c		optional geom_shsec
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/contrib/rdma/rdma_cma.c
--- a/head/sys/contrib/rdma/rdma_cma.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/contrib/rdma/rdma_cma.c	Sat Apr 21 21:38:59 2012 +0300
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sys/contrib/rdma/rdma_cma.c 234507 2012-04-20 21:52:57Z dim $");
 
 #include <sys/param.h>
 #include <sys/condvar.h>
@@ -1252,7 +1252,7 @@
 		*sin = iw_event->local_addr;
 		sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr;
 		*sin = iw_event->remote_addr;
-		switch (iw_event->status) {
+		switch ((int)iw_event->status) {
 		case 0:
 			event.event = RDMA_CM_EVENT_ESTABLISHED;
 			break;
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/amr/amr.c
--- a/head/sys/dev/amr/amr.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/amr/amr.c	Sat Apr 21 21:38:59 2012 +0300
@@ -56,7 +56,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/amr/amr.c 227293 2011-11-07 06:44:47Z ed $");
+__FBSDID("$FreeBSD: head/sys/dev/amr/amr.c 234501 2012-04-20 20:27:31Z jhb $");
 
 /*
  * Driver for the AMI MegaRaid family of controllers.
@@ -535,6 +535,31 @@
     amr_startup(sc);
 }
 
+/*
+ * Bug-for-bug compatibility with Linux!
+ * Some apps will send commands with inlen and outlen set to 0,
+ * even though they expect data to be transfered to them from the
+ * card.  Linux accidentally allows this by allocating a 4KB
+ * buffer for the transfer anyways, but it then throws it away
+ * without copying it back to the app.
+ * 
+ * The amr(4) firmware relies on this feature.  In fact, it assumes
+ * the buffer is always a power of 2 up to a max of 64k.  There is
+ * also at least one case where it assumes a buffer less than 16k is
+ * greater than 16k.  Force a minimum buffer size of 32k and round
+ * sizes between 32k and 64k up to 64k as a workaround.
+ */
+static unsigned long
+amr_ioctl_buffer_length(unsigned long len)
+{
+
+    if (len <= 32 * 1024)
+	return (32 * 1024);
+    if (len <= 64 * 1024)
+	return (64 * 1024);
+    return (len);
+}
+
 int
 amr_linux_ioctl_int(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag,
     struct thread *td)
@@ -664,16 +689,7 @@
 	    error = ENOIOCTL;
 	    break;
 	} else {
-	    /*
-	     * Bug-for-bug compatibility with Linux!
-	     * Some apps will send commands with inlen and outlen set to 0,
-	     * even though they expect data to be transfered to them from the
-	     * card.  Linux accidentally allows this by allocating a 4KB
-	     * buffer for the transfer anyways, but it then throws it away
-	     * without copying it back to the app.
-	     */
-	    if (!len)
-		len = 4096;
+	    len = amr_ioctl_buffer_length(imax(ali.inlen, ali.outlen));
 
 	    dp = malloc(len, M_AMR, M_WAITOK | M_ZERO);
 
@@ -703,7 +719,7 @@
 	    status = ac->ac_status;
 	    error = copyout(&status, &((struct amr_mailbox *)&((struct amr_linux_ioctl *)addr)->mbox[0])->mb_status, sizeof(status));
 	    if (ali.outlen) {
-		error = copyout(dp, (void *)(uintptr_t)mb->mb_physaddr, len);
+		error = copyout(dp, (void *)(uintptr_t)mb->mb_physaddr, ali.outlen);
 		if (error)
 		    break;
 	    }
@@ -750,7 +766,7 @@
     struct amr_command		*ac;
     struct amr_mailbox_ioctl	*mbi;
     void			*dp, *au_buffer;
-    unsigned long		au_length;
+    unsigned long		au_length, real_length;
     unsigned char		*au_cmd;
     int				*au_statusp, au_direction;
     int				error;
@@ -842,8 +858,9 @@
     }
 
     /* handle inbound data buffer */
+    real_length = amr_ioctl_buffer_length(au_length);
     if (au_length != 0 && au_cmd[0] != 0x06) {
-	if ((dp = malloc(au_length, M_AMR, M_WAITOK|M_ZERO)) == NULL) {
+	if ((dp = malloc(real_length, M_AMR, M_WAITOK|M_ZERO)) == NULL) {
 	    error = ENOMEM;
 	    goto out;
 	}
@@ -902,7 +919,7 @@
 
     /* build the command */
     ac->ac_data = dp;
-    ac->ac_length = au_length;
+    ac->ac_length = real_length;
     ac->ac_flags |= AMR_CMD_DATAIN|AMR_CMD_DATAOUT;
 
     /* run the command */
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/asr/asr.c
--- a/head/sys/dev/asr/asr.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/asr/asr.c	Sat Apr 21 21:38:59 2012 +0300
@@ -160,7 +160,7 @@
 
 #include	<dev/asr/sys_info.h>
 
-__FBSDID("$FreeBSD: head/sys/dev/asr/asr.c 232854 2012-03-12 08:03:51Z scottl $");
+__FBSDID("$FreeBSD: head/sys/dev/asr/asr.c 234503 2012-04-20 21:17:33Z dim $");
 
 #define	ASR_VERSION	1
 #define	ASR_REVISION	'1'
@@ -378,11 +378,12 @@
 	u_int16_t		ha_Msgs_Count;
 
 	/* Links into other parents and HBAs */
-	struct Asr_softc      * ha_next;       /* HBA list */
+	STAILQ_ENTRY(Asr_softc) ha_next;       /* HBA list */
 	struct cdev *ha_devt;
 } Asr_softc_t;
 
-static Asr_softc_t *Asr_softc_list;
+static STAILQ_HEAD(, Asr_softc) Asr_softc_list =
+	STAILQ_HEAD_INITIALIZER(Asr_softc_list);
 
 /*
  *	Prototypes of the routines we have in this object.
@@ -1959,7 +1960,7 @@
 {
 	PI2O_EXEC_SYS_TAB_SET_MESSAGE Message_Ptr;
 	PI2O_SET_SYSTAB_HEADER	      SystemTable;
-	Asr_softc_t		    * ha;
+	Asr_softc_t		    * ha, *next;
 	PI2O_SGE_SIMPLE_ELEMENT	      sg;
 	int			      retVal;
 
@@ -1967,7 +1968,7 @@
 	  sizeof(I2O_SET_SYSTAB_HEADER), M_TEMP, M_WAITOK | M_ZERO)) == NULL) {
 		return (ENOMEM);
 	}
-	for (ha = Asr_softc_list; ha; ha = ha->ha_next) {
+	STAILQ_FOREACH(ha, &Asr_softc_list, ha_next) {
 		++SystemTable->NumberEntries;
 	}
 	if ((Message_Ptr = (PI2O_EXEC_SYS_TAB_SET_MESSAGE)malloc (
@@ -1998,9 +1999,9 @@
 	      &(Message_Ptr->StdMessageFrame)) & 0xF0) >> 2));
 	SG(sg, 0, I2O_SGL_FLAGS_DIR, SystemTable, sizeof(I2O_SET_SYSTAB_HEADER));
 	++sg;
-	for (ha = Asr_softc_list; ha; ha = ha->ha_next) {
+	STAILQ_FOREACH_SAFE(ha, &Asr_softc_list, ha_next, next) {
 		SG(sg, 0,
-		  ((ha->ha_next)
+		  ((next)
 		    ? (I2O_SGL_FLAGS_DIR)
 		    : (I2O_SGL_FLAGS_DIR | I2O_SGL_FLAGS_END_OF_BUFFER)),
 		  &(ha->ha_SystemTable), sizeof(ha->ha_SystemTable));
@@ -2396,7 +2397,7 @@
 {
 	PI2O_EXEC_STATUS_GET_REPLY status;
 	PI2O_LCT_ENTRY		 Device;
-	Asr_softc_t		 *sc, **ha;
+	Asr_softc_t		 *sc;
 	struct scsi_inquiry_data *iq;
 	int			 bus, size, unit;
 	int			 error;
@@ -2405,7 +2406,7 @@
 	unit = device_get_unit(dev);
 	sc->ha_dev = dev;
 
-	if (Asr_softc_list == NULL) {
+	if (STAILQ_EMPTY(&Asr_softc_list)) {
 		/*
 		 *	Fixup the OS revision as saved in the dptsig for the
 		 *	engine (dptioctl.h) to pick up.
@@ -2417,8 +2418,7 @@
 	 */
 	LIST_INIT(&(sc->ha_ccb));
 	/* Link us into the HA list */
-	for (ha = &Asr_softc_list; *ha; ha = &((*ha)->ha_next));
-		*(ha) = sc;
+	STAILQ_INSERT_TAIL(&Asr_softc_list, sc, ha_next);
 
 	/*
 	 *	This is the real McCoy!
@@ -2700,7 +2700,7 @@
 
 	ccb->ccb_h.spriv_ptr0 = sc = (struct Asr_softc *)cam_sim_softc(sim);
 
-	switch (ccb->ccb_h.func_code) {
+	switch ((int)ccb->ccb_h.func_code) {
 
 	/* Common cases first */
 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/ath/ah_osdep.c
--- a/head/sys/dev/ath/ah_osdep.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/ath/ah_osdep.c	Sat Apr 21 21:38:59 2012 +0300
@@ -26,7 +26,7 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  * THE POSSIBILITY OF SUCH DAMAGES.
  *
- * $FreeBSD: head/sys/dev/ath/ah_osdep.c 233887 2012-04-04 20:46:20Z adrian $
+ * $FreeBSD: head/sys/dev/ath/ah_osdep.c 234450 2012-04-19 03:26:21Z adrian $
  */
 #include "opt_ah.h"
 
@@ -267,12 +267,7 @@
 	}
 	if (ah->ah_config.ah_serialise_reg_war)
 		mtx_lock_spin(&ah_regser_mtx);
-#if _BYTE_ORDER == _BIG_ENDIAN
-	if (OS_REG_UNSWAPPED(reg))
-		bus_space_write_4(tag, h, reg, val);
-	else
-#endif
-		bus_space_write_stream_4(tag, h, reg, val);
+	bus_space_write_4(tag, h, reg, val);
 	if (ah->ah_config.ah_serialise_reg_war)
 		mtx_unlock_spin(&ah_regser_mtx);
 }
@@ -286,12 +281,7 @@
 
 	if (ah->ah_config.ah_serialise_reg_war)
 		mtx_lock_spin(&ah_regser_mtx);
-#if _BYTE_ORDER == _BIG_ENDIAN
-	if (OS_REG_UNSWAPPED(reg))
-		val = bus_space_read_4(tag, h, reg);
-	else
-#endif
-		val = bus_space_read_stream_4(tag, h, reg);
+	val = bus_space_read_4(tag, h, reg);
 	if (ah->ah_config.ah_serialise_reg_war)
 		mtx_unlock_spin(&ah_regser_mtx);
 	if (ath_hal_alq) {
@@ -343,12 +333,7 @@
 
 	if (ah->ah_config.ah_serialise_reg_war)
 		mtx_lock_spin(&ah_regser_mtx);
-#if _BYTE_ORDER == _BIG_ENDIAN
-	if (OS_REG_UNSWAPPED(reg))
-		bus_space_write_4(tag, h, reg, val);
-	else
-#endif
-		bus_space_write_stream_4(tag, h, reg, val);
+	bus_space_write_4(tag, h, reg, val);
 	if (ah->ah_config.ah_serialise_reg_war)
 		mtx_unlock_spin(&ah_regser_mtx);
 }
@@ -362,12 +347,7 @@
 
 	if (ah->ah_config.ah_serialise_reg_war)
 		mtx_lock_spin(&ah_regser_mtx);
-#if _BYTE_ORDER == _BIG_ENDIAN
-	if (OS_REG_UNSWAPPED(reg))
-		val = bus_space_read_4(tag, h, reg);
-	else
-#endif
-		val = bus_space_read_stream_4(tag, h, reg);
+	val = bus_space_read_4(tag, h, reg);
 	if (ah->ah_config.ah_serialise_reg_war)
 		mtx_unlock_spin(&ah_regser_mtx);
 	return val;
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/ath/ah_osdep.h
--- a/head/sys/dev/ath/ah_osdep.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/ath/ah_osdep.h	Sat Apr 21 21:38:59 2012 +0300
@@ -26,7 +26,7 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  * THE POSSIBILITY OF SUCH DAMAGES.
  *
- * $FreeBSD: head/sys/dev/ath/ah_osdep.h 195418 2009-07-06 20:51:54Z sam $
+ * $FreeBSD: head/sys/dev/ath/ah_osdep.h 234450 2012-04-19 03:26:21Z adrian $
  */
 #ifndef _ATH_AH_OSDEP_H_
 #define _ATH_AH_OSDEP_H_
@@ -97,39 +97,13 @@
 extern	void ath_hal_reg_write(struct ath_hal *ah, u_int reg, u_int32_t val);
 extern	u_int32_t ath_hal_reg_read(struct ath_hal *ah, u_int reg);
 #else
-/*
- * The hardware registers are native little-endian byte order.
- * Big-endian hosts are handled by enabling hardware byte-swap
- * of register reads and writes at reset.  But the PCI clock
- * domain registers are not byte swapped!  Thus, on big-endian
- * platforms we have to explicitly byte-swap those registers.
- * Most of this code is collapsed at compile time because the
- * register values are constants.
- */
-#if _BYTE_ORDER == _BIG_ENDIAN
-#define OS_REG_WRITE(_ah, _reg, _val) do {				\
-	if (OS_REG_UNSWAPPED(_reg))					\
-		bus_space_write_4((bus_space_tag_t)(_ah)->ah_st,	\
-		    (bus_space_handle_t)(_ah)->ah_sh, (_reg), (_val));	\
-	else								\
-		bus_space_write_stream_4((bus_space_tag_t)(_ah)->ah_st,	\
-		    (bus_space_handle_t)(_ah)->ah_sh, (_reg), (_val));	\
-} while (0)
-#define OS_REG_READ(_ah, _reg)						\
-	(OS_REG_UNSWAPPED(_reg) ?					\
-		bus_space_read_4((bus_space_tag_t)(_ah)->ah_st,		\
-		    (bus_space_handle_t)(_ah)->ah_sh, (_reg)) :		\
-		bus_space_read_stream_4((bus_space_tag_t)(_ah)->ah_st,	\
-		    (bus_space_handle_t)(_ah)->ah_sh, (_reg)))
-#else /* _BYTE_ORDER == _LITTLE_ENDIAN */
 #define	OS_REG_WRITE(_ah, _reg, _val)					\
 	bus_space_write_4((bus_space_tag_t)(_ah)->ah_st,		\
 	    (bus_space_handle_t)(_ah)->ah_sh, (_reg), (_val))
 #define	OS_REG_READ(_ah, _reg)						\
 	bus_space_read_4((bus_space_tag_t)(_ah)->ah_st,			\
 	    (bus_space_handle_t)(_ah)->ah_sh, (_reg))
-#endif /* _BYTE_ORDER */
-#endif /* AH_DEBUG || AH_REGFUNC || AH_DEBUG_ALQ */
+#endif
 
 #ifdef AH_DEBUG_ALQ
 extern	void OS_MARK(struct ath_hal *, u_int id, u_int32_t value);
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/ath/ath_hal/ah_eeprom_v4k.c
--- a/head/sys/dev/ath/ath_hal/ah_eeprom_v4k.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/ath/ath_hal/ah_eeprom_v4k.c	Sat Apr 21 21:38:59 2012 +0300
@@ -15,7 +15,7 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
- * $FreeBSD: head/sys/dev/ath/ath_hal/ah_eeprom_v4k.c 224624 2011-08-03 06:51:14Z adrian $
+ * $FreeBSD: head/sys/dev/ath/ath_hal/ah_eeprom_v4k.c 234508 2012-04-20 21:56:13Z adrian $
  */
 #include "opt_ah.h"
 
@@ -298,12 +298,12 @@
 			    "%s Error reading Eeprom MAGIC\n", __func__);
 			return HAL_EEREAD;
 		}
-	}
-	HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s Eeprom Magic = 0x%x\n",
-	    __func__, magic);
-	if (magic != AR5416_EEPROM_MAGIC) {
-		HALDEBUG(ah, HAL_DEBUG_ANY, "Bad magic number\n");
-		return HAL_EEMAGIC;
+		HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s Eeprom Magic = 0x%x\n",
+		    __func__, magic);
+		if (magic != AR5416_EEPROM_MAGIC) {
+			HALDEBUG(ah, HAL_DEBUG_ANY, "Bad magic number\n");
+			return HAL_EEMAGIC;
+		}
 	}
 
 	ee = ath_hal_malloc(sizeof(HAL_EEPROM_v4k));
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/ath/ath_hal/ar5210/ar5210_reset.c
--- a/head/sys/dev/ath/ath_hal/ar5210/ar5210_reset.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/ath/ath_hal/ar5210/ar5210_reset.c	Sat Apr 21 21:38:59 2012 +0300
@@ -14,7 +14,7 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
- * $FreeBSD: head/sys/dev/ath/ath_hal/ar5210/ar5210_reset.c 208643 2010-05-29 16:11:51Z rpaulo $
+ * $FreeBSD: head/sys/dev/ath/ath_hal/ar5210/ar5210_reset.c 234450 2012-04-19 03:26:21Z adrian $
  */
 #include "opt_ah.h"
 
@@ -594,12 +594,10 @@
         if ((resetMask & AR_RC_RMAC) == 0) {
 		if (isBigEndian()) {
 			/*
-			 * Set CFG, little-endian for register
-			 * and descriptor accesses.
+			 * Set CFG, little-endian for descriptor accesses.
 			 */
-			mask = INIT_CONFIG_STATUS |
-				AR_CFG_SWTD | AR_CFG_SWRD | AR_CFG_SWRG;
-			OS_REG_WRITE(ah, AR_CFG, LE_READ_4(&mask));
+			mask = INIT_CONFIG_STATUS | AR_CFG_SWTD | AR_CFG_SWRD;
+			OS_REG_WRITE(ah, AR_CFG, mask);
 		} else
 			OS_REG_WRITE(ah, AR_CFG, INIT_CONFIG_STATUS);
 	}
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/ath/ath_hal/ar5211/ar5211_reset.c
--- a/head/sys/dev/ath/ath_hal/ar5211/ar5211_reset.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/ath/ath_hal/ar5211/ar5211_reset.c	Sat Apr 21 21:38:59 2012 +0300
@@ -14,7 +14,7 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
- * $FreeBSD: head/sys/dev/ath/ath_hal/ar5211/ar5211_reset.c 208644 2010-05-29 16:14:02Z rpaulo $
+ * $FreeBSD: head/sys/dev/ath/ath_hal/ar5211/ar5211_reset.c 234450 2012-04-19 03:26:21Z adrian $
  */
 #include "opt_ah.h"
 
@@ -764,12 +764,10 @@
         if ((resetMask & AR_RC_MAC) == 0) {
 		if (isBigEndian()) {
 			/*
-			 * Set CFG, little-endian for register
-			 * and descriptor accesses.
+			 * Set CFG, little-endian for descriptor accesses.
 			 */
-			mask = INIT_CONFIG_STATUS |
-				AR_CFG_SWTD | AR_CFG_SWRD | AR_CFG_SWRG;
-			OS_REG_WRITE(ah, AR_CFG, LE_READ_4(&mask));
+			mask = INIT_CONFIG_STATUS | AR_CFG_SWTD | AR_CFG_SWRD;
+			OS_REG_WRITE(ah, AR_CFG, mask);
 		} else
 			OS_REG_WRITE(ah, AR_CFG, INIT_CONFIG_STATUS);
 	}
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/ath/ath_hal/ar5212/ar5212_reset.c
--- a/head/sys/dev/ath/ath_hal/ar5212/ar5212_reset.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/ath/ath_hal/ar5212/ar5212_reset.c	Sat Apr 21 21:38:59 2012 +0300
@@ -14,7 +14,7 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
- * $FreeBSD: head/sys/dev/ath/ath_hal/ar5212/ar5212_reset.c 226760 2011-10-25 23:13:36Z adrian $
+ * $FreeBSD: head/sys/dev/ath/ath_hal/ar5212/ar5212_reset.c 234450 2012-04-19 03:26:21Z adrian $
  */
 #include "opt_ah.h"
 
@@ -1273,14 +1273,13 @@
         if ((resetMask & AR_RC_MAC) == 0) {
 		if (isBigEndian()) {
 			/*
-			 * Set CFG, little-endian for register
-			 * and descriptor accesses.
+			 * Set CFG, little-endian for descriptor accesses.
 			 */
-			mask = INIT_CONFIG_STATUS | AR_CFG_SWRD | AR_CFG_SWRG;
+			mask = INIT_CONFIG_STATUS | AR_CFG_SWRD;
 #ifndef AH_NEED_DESC_SWAP
 			mask |= AR_CFG_SWTD;
 #endif
-			OS_REG_WRITE(ah, AR_CFG, LE_READ_4(&mask));
+			OS_REG_WRITE(ah, AR_CFG, mask);
 		} else
 			OS_REG_WRITE(ah, AR_CFG, INIT_CONFIG_STATUS);
 		if (ar5212SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE))
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/ath/ath_hal/ar5312/ar5312_reset.c
--- a/head/sys/dev/ath/ath_hal/ar5312/ar5312_reset.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/ath/ath_hal/ar5312/ar5312_reset.c	Sat Apr 21 21:38:59 2012 +0300
@@ -14,7 +14,7 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
- * $FreeBSD: head/sys/dev/ath/ath_hal/ar5312/ar5312_reset.c 187831 2009-01-28 18:00:22Z sam $
+ * $FreeBSD: head/sys/dev/ath/ath_hal/ar5312/ar5312_reset.c 234450 2012-04-19 03:26:21Z adrian $
  */
 #include "opt_ah.h"
 
@@ -740,8 +740,7 @@
         if ((resetMask & AR_RC_MAC) == 0) {
 		if (isBigEndian()) {
 			/*
-			 * Set CFG, little-endian for register
-			 * and descriptor accesses.
+			 * Set CFG, little-endian for descriptor accesses.
 			 */
 #ifdef AH_NEED_DESC_SWAP
 			mask = INIT_CONFIG_STATUS | AR_CFG_SWRD;
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/ath/ath_hal/ar5416/ar5416_reset.c
--- a/head/sys/dev/ath/ath_hal/ar5416/ar5416_reset.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/ath/ath_hal/ar5416/ar5416_reset.c	Sat Apr 21 21:38:59 2012 +0300
@@ -14,7 +14,7 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
- * $FreeBSD: head/sys/dev/ath/ath_hal/ar5416/ar5416_reset.c 233329 2012-03-22 21:47:14Z adrian $
+ * $FreeBSD: head/sys/dev/ath/ath_hal/ar5416/ar5416_reset.c 234450 2012-04-19 03:26:21Z adrian $
  */
 #include "opt_ah.h"
 
@@ -1387,16 +1387,15 @@
 	if (type == HAL_RESET_COLD) {
 		if (isBigEndian()) {
 			/*
-			 * Set CFG, little-endian for register
-			 * and descriptor accesses.
+			 * Set CFG, little-endian for descriptor accesses.
 			 */
-			mask = INIT_CONFIG_STATUS | AR_CFG_SWRD | AR_CFG_SWRG;
+			mask = INIT_CONFIG_STATUS | AR_CFG_SWRD;
 #ifndef AH_NEED_DESC_SWAP
 			mask |= AR_CFG_SWTD;
 #endif
 			HALDEBUG(ah, HAL_DEBUG_RESET,
 			    "%s Applying descriptor swap\n", __func__);
-			OS_REG_WRITE(ah, AR_CFG, LE_READ_4(&mask));
+			OS_REG_WRITE(ah, AR_CFG, mask);
 		} else
 			OS_REG_WRITE(ah, AR_CFG, INIT_CONFIG_STATUS);
 	}
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/ath/ath_hal/ar9002/ar9280_attach.c
--- a/head/sys/dev/ath/ath_hal/ar9002/ar9280_attach.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/ath/ath_hal/ar9002/ar9280_attach.c	Sat Apr 21 21:38:59 2012 +0300
@@ -14,7 +14,7 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
- * $FreeBSD: head/sys/dev/ath/ath_hal/ar9002/ar9280_attach.c 230847 2012-01-31 22:31:16Z adrian $
+ * $FreeBSD: head/sys/dev/ath/ath_hal/ar9002/ar9280_attach.c 234510 2012-04-20 22:07:21Z adrian $
  */
 #include "opt_ah.h"
 
@@ -169,7 +169,6 @@
 
 	ar5416InitState(AH5416(ah), devid, sc, st, sh, status);
 
-
 	/*
 	 * Use the "local" EEPROM data given to us by the higher layers.
 	 * This is a private copy out of system flash. The Linux ath9k
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/ath/ath_hal/ar9002/ar9285_attach.c
--- a/head/sys/dev/ath/ath_hal/ar9002/ar9285_attach.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/ath/ath_hal/ar9002/ar9285_attach.c	Sat Apr 21 21:38:59 2012 +0300
@@ -14,7 +14,7 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
- * $FreeBSD: head/sys/dev/ath/ath_hal/ar9002/ar9285_attach.c 225883 2011-09-30 05:17:57Z adrian $
+ * $FreeBSD: head/sys/dev/ath/ath_hal/ar9002/ar9285_attach.c 234508 2012-04-20 21:56:13Z adrian $
  */
 #include "opt_ah.h"
 
@@ -134,6 +134,18 @@
 
 	ar5416InitState(AH5416(ah), devid, sc, st, sh, status);
 
+	/*
+	 * Use the "local" EEPROM data given to us by the higher layers.
+	 * This is a private copy out of system flash. The Linux ath9k
+	 * commit for the initial AR9130 support mentions MMIO flash
+	 * access is "unreliable." -adrian
+	 */
+	if (eepromdata != AH_NULL) {
+		AH_PRIVATE(ah)->ah_eepromRead = ath_hal_EepromDataRead;
+		AH_PRIVATE(ah)->ah_eepromWrite = NULL;
+		ah->ah_eepromdata = eepromdata;
+	}
+
 	/* XXX override with 9285 specific state */
 	/* override 5416 methods for our needs */
 	AH5416(ah)->ah_initPLL = ar9280InitPLL;
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/mmc/mmc.c
--- a/head/sys/dev/mmc/mmc.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/mmc/mmc.c	Sat Apr 21 21:38:59 2012 +0300
@@ -51,7 +51,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/mmc/mmc.c 227309 2011-11-07 15:43:11Z ed $");
+__FBSDID("$FreeBSD: head/sys/dev/mmc/mmc.c 234524 2012-04-21 01:51:16Z marius $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -101,6 +101,7 @@
 	uint32_t tran_speed;	/* Max speed in normal mode */
 	uint32_t hs_tran_speed;	/* Max speed in high speed mode */
 	uint32_t erase_sector;	/* Card native erase sector size */
+	char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */
 };
 
 #define CMD_RETRIES	3
@@ -140,6 +141,7 @@
 static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
 static void mmc_scan(struct mmc_softc *sc);
 static int mmc_delete_cards(struct mmc_softc *sc);
+static void mmc_format_card_id_string(struct mmc_ivars *ivar);
 
 static void
 mmc_ms_delay(int ms)
@@ -606,6 +608,13 @@
 
 	if (mmcbr_get_mode(sc->dev) == mode_sd) {
 		memset(&cmd, 0, sizeof(struct mmc_command));
+		cmd.opcode = ACMD_SET_CLR_CARD_DETECT;
+		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
+		cmd.arg = SD_CLR_CARD_DETECT;
+		err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
+		if (err != 0)
+			return (err);
+		memset(&cmd, 0, sizeof(struct mmc_command));
 		cmd.opcode = ACMD_SET_BUS_WIDTH;
 		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
 		switch (width) {
@@ -788,15 +797,52 @@
 	cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
 }
 
+static void
+mmc_format_card_id_string(struct mmc_ivars *ivar)
+{
+	char oidstr[8];
+	uint8_t c1;
+	uint8_t c2;
+
+	/*
+	 * Format a card ID string for use by the mmcsd driver, it's what
+	 * appears between the <> in the following:
+	 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
+	 * 22.5MHz/4bit/128-block
+	 *
+	 * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
+	 * and our max formatted length is currently 55 bytes if every field
+	 * contains the largest value.
+	 *
+	 * Sometimes the oid is two printable ascii chars; when it's not,
+	 * format it as 0xnnnn instead.
+	 */
+	c1 = (ivar->cid.oid >> 8) & 0x0ff;
+	c2 = ivar->cid.oid & 0x0ff;
+	if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
+		snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
+	else
+		snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid);
+	snprintf(ivar->card_id_string, sizeof(ivar->card_id_string),
+	    "%s%s %s %d.%d SN %d MFG %02d/%04d by %d %s",
+	    ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "",
+	    ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
+	    ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year,
+	    ivar->cid.mid, oidstr);
+}
+
 static const int exp[8] = {
 	1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
 };
+
 static const int mant[16] = {
-	10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
+	0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
 };
+
 static const int cur_min[8] = {
 	500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
 };
+
 static const int cur_max[8] = {
 	1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
 };
@@ -1080,13 +1126,7 @@
 {
 	device_printf(dev, "Card at relative address %d%s:\n",
 	    ivar->rca, newcard ? " added" : "");
-	device_printf(dev, " card: %s%s (0x%x/0x%x/\"%s\" rev %d.%d "
-	    "m/d %02d.%04d s/n %08x)\n",
-	    ivar->mode == mode_sd ? "SD" : "MMC",
-	    ivar->high_cap ? " High Capacity" : "", 
-	    ivar->cid.mid, ivar->cid.oid,
-	    ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
-	    ivar->cid.mdt_month, ivar->cid.mdt_year, ivar->cid.psn);
+	device_printf(dev, " card: %s\n", ivar->card_id_string);
 	device_printf(dev, " bus: %ubit, %uMHz%s\n",
 	    (ivar->bus_width == bus_width_1 ? 1 :
 	    (ivar->bus_width == bus_width_4 ? 4 : 8)),
@@ -1188,6 +1228,7 @@
 			if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
 			    (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
 				ivar->bus_width = bus_width_4;
+			mmc_format_card_id_string(ivar);
 			if (bootverbose || mmc_debug)
 				mmc_log_card(sc->dev, ivar, newcard);
 			if (newcard) {
@@ -1245,6 +1286,7 @@
 			ivar->bus_width = bus_width_1;
 			ivar->timing = bus_timing_normal;
 		}
+		mmc_format_card_id_string(ivar);
 		if (bootverbose || mmc_debug)
 			mmc_log_card(sc->dev, ivar, newcard);
 		if (newcard) {
@@ -1477,6 +1519,9 @@
 	case MMC_IVAR_MAX_DATA:
 		*result = mmcbr_get_max_data(bus);
 		break;
+	case MMC_IVAR_CARD_ID_STRING:
+		*(char **)result = ivar->card_id_string;
+		break;
 	}
 	return (0);
 }
@@ -1527,7 +1572,7 @@
 	DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
 	DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
 
-	{0, 0},
+	DEVMETHOD_END
 };
 
 static driver_t mmc_driver = {
@@ -1537,6 +1582,5 @@
 };
 static devclass_t mmc_devclass;
 
-
 DRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, NULL, NULL);
 DRIVER_MODULE(mmc, sdhci, mmc_driver, mmc_devclass, NULL, NULL);
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/mmc/mmcbrvar.h
--- a/head/sys/dev/mmc/mmcbrvar.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/mmc/mmcbrvar.h	Sat Apr 21 21:38:59 2012 +0300
@@ -49,13 +49,14 @@
  * or the SD Card Association to disclose or distribute any technical
  * information, know-how or other confidential information to any third party.
  *
- * "$FreeBSD$"
+ * "$FreeBSD: head/sys/dev/mmc/mmcbrvar.h 234524 2012-04-21 01:51:16Z marius $"
  */
 
 #ifndef DEV_MMC_MMCBRVAR_H
 #define DEV_MMC_MMCBRVAR_H
 
 #include <dev/mmc/bridge.h>
+#include <dev/mmc/mmcreg.h>
 #include "mmcbr_if.h"
 
 enum mmcbr_device_ivars {
@@ -72,8 +73,7 @@
     MMCBR_IVAR_VDD,
     MMCBR_IVAR_CAPS,
     MMCBR_IVAR_TIMING,
-    MMCBR_IVAR_MAX_DATA,
-//    MMCBR_IVAR_,
+    MMCBR_IVAR_MAX_DATA
 };
 
 /*
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/mmc/mmcreg.h
--- a/head/sys/dev/mmc/mmcreg.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/mmc/mmcreg.h	Sat Apr 21 21:38:59 2012 +0300
@@ -48,7 +48,7 @@
  * or the SD Card Association to disclose or distribute any technical
  * information, know-how or other confidential information to any third party.
  *
- * $FreeBSD$
+ * $FreeBSD: head/sys/dev/mmc/mmcreg.h 234524 2012-04-21 01:51:16Z marius $
  */
 
 #ifndef DEV_MMC_MMCREG_H
@@ -139,8 +139,8 @@
 #define	R1_READY_FOR_DATA (1u << 8)		/* sx, a */
 #define	R1_APP_CMD (1u << 5)			/* sr, c */
 #define	R1_AKE_SEQ_ERROR (1u << 3)		/* er, c */
-#define	R1_STATUS(x)            (x & 0xFFFFE000
-#define	R1_CURRENT_STATE(x) ((x) & R1_CURRENT_STATE_MASK) >> 9
+#define	R1_STATUS(x)		((x) & 0xFFFFE000)
+#define	R1_CURRENT_STATE(x)	(((x) & R1_CURRENT_STATE_MASK) >> 9)
 #define	R1_STATE_IDLE	0
 #define	R1_STATE_READY	1
 #define	R1_STATE_IDENT	2
@@ -330,6 +330,9 @@
 #define SD_SWITCH_HS_MODE	1
 #define SD_SWITCH_NOCHANGE	0xF
 
+#define	SD_CLR_CARD_DETECT	0
+#define	SD_SET_CARD_DETECT	1
+
 #define	SD_MAX_HS		50000000
 
 /* OCR bits */
@@ -380,7 +383,7 @@
 	uint8_t fwrev;
 };
 
-struct mmc_csd 
+struct mmc_csd
 {
 	uint8_t csd_structure;
 	uint8_t spec_vers;
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/mmc/mmcsd.c
--- a/head/sys/dev/mmc/mmcsd.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/mmc/mmcsd.c	Sat Apr 21 21:38:59 2012 +0300
@@ -51,7 +51,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/mmc/mmcsd.c 224868 2011-08-14 16:17:00Z mav $");
+__FBSDID("$FreeBSD: head/sys/dev/mmc/mmcsd.c 234524 2012-04-21 01:51:16Z marius $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -66,11 +66,17 @@
 #include <sys/mutex.h>
 #include <geom/geom_disk.h>
 
+#include <dev/mmc/mmcbrvar.h>
+#include <dev/mmc/mmcreg.h>
 #include <dev/mmc/mmcvar.h>
-#include <dev/mmc/mmcreg.h>
 
 #include "mmcbus_if.h"
 
+#if __FreeBSD_version < 800002
+#define	kproc_create	kthread_create
+#define	kproc_exit	kthread_exit
+#endif
+
 struct mmcsd_softc {
 	device_t dev;
 	struct mtx sc_mtx;
@@ -95,7 +101,6 @@
 	off_t offset, size_t length);
 static void mmcsd_task(void *arg);
 
-static const char *mmcsd_card_name(device_t dev);
 static int mmcsd_bus_bit_width(device_t dev);
 
 #define MMCSD_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
@@ -122,6 +127,8 @@
 	struct mmcsd_softc *sc;
 	struct disk *d;
 	intmax_t mb;
+	uint32_t speed;
+	uint32_t maxblocks;
 	char unit;
 
 	sc = device_get_softc(dev);
@@ -157,11 +164,22 @@
 		unit = 'G';
 		mb /= 1024;
 	}
-	device_printf(dev, "%ju%cB <%s Memory Card>%s at %s %dMHz/%dbit\n",
-	    mb, unit, mmcsd_card_name(dev),
+	/*
+	 * Report the clock speed of the underlying hardware, which might be
+	 * different than what the card reports due to hardware limitations.
+	 * Report how many blocks the hardware transfers at once, but clip the
+	 * number to MAXPHYS since the system won't initiate larger transfers.
+	 */
+	speed = mmcbr_get_clock(device_get_parent(dev));
+	maxblocks = mmc_get_max_data(dev);
+	if (maxblocks > MAXPHYS)
+		maxblocks = MAXPHYS;
+	device_printf(dev, "%ju%cB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
+	    mb, unit, mmc_get_card_id_string(dev),
 	    mmc_get_read_only(dev) ? " (read-only)" : "",
 	    device_get_nameunit(device_get_parent(dev)),
-	    mmc_get_tran_speed(dev) / 1000000, mmcsd_bus_bit_width(dev));
+	    speed / 1000000, (speed / 100000) % 10,
+	    mmcsd_bus_bit_width(dev), maxblocks);
 	disk_create(d, DISK_VERSION);
 	bioq_init(&sc->bio_queue);
 
@@ -500,16 +518,6 @@
 	kproc_exit(0);
 }
 
-static const char *
-mmcsd_card_name(device_t dev)
-{
-	if (mmc_get_card_type(dev) == mode_mmc)
-		return ("MMC");
-	if (mmc_get_high_cap(dev))
-		return ("SDHC");
-	return ("SD");
-}
-
 static int
 mmcsd_bus_bit_width(device_t dev)
 {
@@ -526,7 +534,7 @@
 	DEVMETHOD(device_detach, mmcsd_detach),
 	DEVMETHOD(device_suspend, mmcsd_suspend),
 	DEVMETHOD(device_resume, mmcsd_resume),
-	{0, 0},
+	DEVMETHOD_END
 };
 
 static driver_t mmcsd_driver = {
@@ -536,4 +544,4 @@
 };
 static devclass_t mmcsd_devclass;
 
-DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, 0, 0);
+DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, NULL, NULL);
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/mmc/mmcvar.h
--- a/head/sys/dev/mmc/mmcvar.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/mmc/mmcvar.h	Sat Apr 21 21:38:59 2012 +0300
@@ -49,7 +49,7 @@
  * or the SD Card Association to disclose or distribute any technical
  * information, know-how or other confidential information to any third party.
  *
- * "$FreeBSD: head/sys/dev/mmc/mmcvar.h 222475 2011-05-30 06:23:51Z jchandra $"
+ * "$FreeBSD: head/sys/dev/mmc/mmcvar.h 234524 2012-04-21 01:51:16Z marius $"
  */
 
 #ifndef DEV_MMC_MMCVAR_H
@@ -69,7 +69,7 @@
     MMC_IVAR_BUS_WIDTH,
     MMC_IVAR_ERASE_SECTOR,
     MMC_IVAR_MAX_DATA,
-//    MMC_IVAR_,
+    MMC_IVAR_CARD_ID_STRING
 };
 
 /*
@@ -89,5 +89,6 @@
 MMC_ACCESSOR(bus_width, BUS_WIDTH, int)
 MMC_ACCESSOR(erase_sector, ERASE_SECTOR, int)
 MMC_ACCESSOR(max_data, MAX_DATA, int)
+MMC_ACCESSOR(card_id_string, CARD_ID_STRING, const char *)
 
 #endif /* DEV_MMC_MMCVAR_H */
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/nxge/if_nxge.c
--- a/head/sys/dev/nxge/if_nxge.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/nxge/if_nxge.c	Sat Apr 21 21:38:59 2012 +0300
@@ -23,7 +23,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD$
+ * $FreeBSD: head/sys/dev/nxge/if_nxge.c 234506 2012-04-20 21:43:19Z dim $
  */
 
 #include <dev/nxge/if_nxge.h>
@@ -1272,7 +1272,7 @@
 	lldev  = xge_hal_device_private(hldev);
 	ifnetp = lldev->ifnetp;
 
-	switch(item->event_type) {
+	switch((int)item->event_type) {
 	    case XGE_LL_EVENT_TRY_XMIT_AGAIN:
 	        if(lldev->initialized) {
 	            if(xge_hal_channel_dtr_count(lldev->fifo_channel[0]) > 0) {
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/dev/usb/serial/umodem.c
--- a/head/sys/dev/usb/serial/umodem.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/dev/usb/serial/umodem.c	Sat Apr 21 21:38:59 2012 +0300
@@ -1,7 +1,7 @@
 /*	$NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $	*/
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/usb/serial/umodem.c 233774 2012-04-02 10:50:42Z hselasky $");
+__FBSDID("$FreeBSD: head/sys/dev/usb/serial/umodem.c 234418 2012-04-18 12:50:13Z thomas $");
 
 /*-
  * Copyright (c) 2003, M. Warner Losh <imp at FreeBSD.org>.
@@ -139,7 +139,7 @@
 };
 
 /*
- * As speeds for umodem deivces increase, these numbers will need to
+ * As speeds for umodem devices increase, these numbers will need to
  * be increased. They should be good for G3 speeds and below.
  *
  * TODO: The TTY buffers should be increased!
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/fs/coda/coda_subr.c
--- a/head/sys/fs/coda/coda_subr.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/fs/coda/coda_subr.c	Sat Apr 21 21:38:59 2012 +0300
@@ -54,7 +54,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sys/fs/coda/coda_subr.c 234386 2012-04-17 16:28:22Z mckusick $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -365,13 +365,7 @@
 	struct cnode *cp;
 	int count = 0, bad = 0;
 
-	MNT_ILOCK(mp);
-	MNT_VNODE_FOREACH(vp, mp, nvp) {
-		VI_LOCK(vp);
-		if (vp->v_iflag & VI_DOOMED) {
-			VI_UNLOCK(vp);
-			continue;
-		}
+	MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
 		cp = VTOC(vp);
 		count++;
 		if (!(cp->c_flags & C_UNMOUNTING)) {
@@ -381,7 +375,6 @@
 		}
 		VI_UNLOCK(vp);
 	}
-	MNT_IUNLOCK(mp);
 }
 
 void
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/fs/ext2fs/ext2_vfsops.c
--- a/head/sys/fs/ext2fs/ext2_vfsops.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/fs/ext2fs/ext2_vfsops.c	Sat Apr 21 21:38:59 2012 +0300
@@ -33,7 +33,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)ffs_vfsops.c	8.8 (Berkeley) 4/18/94
- * $FreeBSD: head/sys/fs/ext2fs/ext2_vfsops.c 232703 2012-03-08 21:06:05Z pfg $
+ * $FreeBSD: head/sys/fs/ext2fs/ext2_vfsops.c 234386 2012-04-17 16:28:22Z mckusick $
  */
 
 #include <sys/param.h>
@@ -480,19 +480,12 @@
 	}
 
 loop:
-	MNT_ILOCK(mp);
-	MNT_VNODE_FOREACH(vp, mp, mvp) {
-		VI_LOCK(vp);
-		if (vp->v_iflag & VI_DOOMED) {
-			VI_UNLOCK(vp);
-			continue;
-		}
-		MNT_IUNLOCK(mp);
+	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
 		/*
 		 * Step 4: invalidate all cached file data.
 		 */
 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
-			MNT_VNODE_FOREACH_ABORT(mp, mvp);
+			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 			goto loop;
 		}
 		if (vinvalbuf(vp, 0, 0, 0))
@@ -507,7 +500,7 @@
 		if (error) {
 			VOP_UNLOCK(vp, 0);
 			vrele(vp);
-			MNT_VNODE_FOREACH_ABORT(mp, mvp);
+			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 			return (error);
 		}
 		ext2_ei2i((struct ext2fs_dinode *) ((char *)bp->b_data +
@@ -515,9 +508,7 @@
 		brelse(bp);
 		VOP_UNLOCK(vp, 0);
 		vrele(vp);
-		MNT_ILOCK(mp);
 	}
-	MNT_IUNLOCK(mp);
 	return (0);
 }
 
@@ -841,27 +832,24 @@
 	 */
 	MNT_ILOCK(mp);
 loop:
-	MNT_VNODE_FOREACH(vp, mp, mvp) {
-		VI_LOCK(vp);
-		if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED)) {
+	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
+		if (vp->v_type == VNON) {
 			VI_UNLOCK(vp);
 			continue;
 		}
-		MNT_IUNLOCK(mp);
 		ip = VTOI(vp);
 		if ((ip->i_flag &
 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
 		    (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
 		    waitfor == MNT_LAZY)) {
 			VI_UNLOCK(vp);
-			MNT_ILOCK(mp);
 			continue;
 		}
 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
 		if (error) {
 			MNT_ILOCK(mp);
 			if (error == ENOENT) {
-				MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
+				MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 				goto loop;
 			}
 			continue;
@@ -870,9 +858,7 @@
 			allerror = error;
 		VOP_UNLOCK(vp, 0);
 		vrele(vp);
-		MNT_ILOCK(mp);
 	}
-	MNT_IUNLOCK(mp);
 
 	/*
 	 * Force stale file system control information to be flushed.
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/fs/msdosfs/msdosfs_vfsops.c
--- a/head/sys/fs/msdosfs/msdosfs_vfsops.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/fs/msdosfs/msdosfs_vfsops.c	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-/* $FreeBSD: head/sys/fs/msdosfs/msdosfs_vfsops.c 234025 2012-04-08 06:18:18Z mckusick $ */
+/* $FreeBSD: head/sys/fs/msdosfs/msdosfs_vfsops.c 234482 2012-04-20 06:50:44Z mckusick $ */
 /*	$NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $	*/
 
 /*-
@@ -834,7 +834,7 @@
 		vn_printf(vp,
 		    "msdosfs_umount(): just before calling VOP_CLOSE()\n");
 		printf("freef %p, freeb %p, mount %p\n",
-		    TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev,
+		    TAILQ_NEXT(vp, v_actfreelist), vp->v_actfreelist.tqe_prev,
 		    vp->v_mount);
 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
 		    TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
@@ -923,27 +923,22 @@
 	/*
 	 * Write back each (modified) denode.
 	 */
-	MNT_ILOCK(mp);
 loop:
-	MNT_VNODE_FOREACH(vp, mp, nvp) {
-		VI_LOCK(vp);
-		if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED)) {
+	MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
+		if (vp->v_type == VNON) {
 			VI_UNLOCK(vp);
 			continue;
 		}
-		MNT_IUNLOCK(mp);
 		dep = VTODE(vp);
 		if ((dep->de_flag &
 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
 		    (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
 		    waitfor == MNT_LAZY)) {
 			VI_UNLOCK(vp);
-			MNT_ILOCK(mp);
 			continue;
 		}
 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
 		if (error) {
-			MNT_ILOCK(mp);
 			if (error == ENOENT)
 				goto loop;
 			continue;
@@ -953,9 +948,7 @@
 			allerror = error;
 		VOP_UNLOCK(vp, 0);
 		vrele(vp);
-		MNT_ILOCK(mp);
 	}
-	MNT_IUNLOCK(mp);
 
 	/*
 	 * Flush filesystem control info.
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/fs/nfsclient/nfs_clsubs.c
--- a/head/sys/fs/nfsclient/nfs_clsubs.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/fs/nfsclient/nfs_clsubs.c	Sat Apr 21 21:38:59 2012 +0300
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clsubs.c 224083 2011-07-16 08:05:41Z zack $");
+__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clsubs.c 234386 2012-04-17 16:28:22Z mckusick $");
 
 #include "opt_kdtrace.h"
 
@@ -367,17 +367,10 @@
 	struct buf *bp, *nbp;
 	struct bufobj *bo;
 
-	MNT_ILOCK(mp);
-	MNT_VNODE_FOREACH(vp, mp, nvp) {
+	MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
 		bo = &vp->v_bufobj;
-		VI_LOCK(vp);
-		if (vp->v_iflag & VI_DOOMED) {
-			VI_UNLOCK(vp);
-			continue;
-		}
 		vholdl(vp);
 		VI_UNLOCK(vp);
-		MNT_IUNLOCK(mp);
 		BO_LOCK(bo);
 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
 			if (!BUF_ISLOCKED(bp) &&
@@ -387,9 +380,7 @@
 		}
 		BO_UNLOCK(bo);
 		vdrop(vp);
-		MNT_ILOCK(mp);
 	}
-	MNT_IUNLOCK(mp);
 }
 
 /*
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/fs/nfsclient/nfs_clvfsops.c
--- a/head/sys/fs/nfsclient/nfs_clvfsops.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/fs/nfsclient/nfs_clvfsops.c	Sat Apr 21 21:38:59 2012 +0300
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clvfsops.c 231852 2012-02-17 02:39:58Z bz $");
+__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clvfsops.c 234386 2012-04-17 16:28:22Z mckusick $");
 
 
 #include "opt_bootp.h"
@@ -1508,24 +1508,21 @@
 		MNT_IUNLOCK(mp);
 		return (EBADF);
 	}
+	MNT_IUNLOCK(mp);
 
 	/*
 	 * Force stale buffer cache information to be flushed.
 	 */
 loop:
-	MNT_VNODE_FOREACH(vp, mp, mvp) {
-		VI_LOCK(vp);
-		MNT_IUNLOCK(mp);
+	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
 		/* XXX Racy bv_cnt check. */
 		if (NFSVOPISLOCKED(vp) || vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
 		    waitfor == MNT_LAZY) {
 			VI_UNLOCK(vp);
-			MNT_ILOCK(mp);
 			continue;
 		}
 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
-			MNT_ILOCK(mp);
-			MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
+			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 			goto loop;
 		}
 		error = VOP_FSYNC(vp, waitfor, td);
@@ -1533,10 +1530,7 @@
 			allerror = error;
 		NFSVOPUNLOCK(vp, 0);
 		vrele(vp);
-
-		MNT_ILOCK(mp);
 	}
-	MNT_IUNLOCK(mp);
 	return (allerror);
 }
 
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/fs/nfsserver/nfs_nfsdport.c
--- a/head/sys/fs/nfsserver/nfs_nfsdport.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/fs/nfsserver/nfs_nfsdport.c	Sat Apr 21 21:38:59 2012 +0300
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/fs/nfsserver/nfs_nfsdport.c 232467 2012-03-03 16:13:20Z rmacklem $");
+__FBSDID("$FreeBSD: head/sys/fs/nfsserver/nfs_nfsdport.c 234482 2012-04-20 06:50:44Z mckusick $");
 
 #include <sys/capability.h>
 
@@ -2907,12 +2907,14 @@
 	inited = 1;
 	nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
 	TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
+	TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist);
 	nfsv4root_mnt.mnt_export = NULL;
 	TAILQ_INIT(&nfsv4root_opt);
 	TAILQ_INIT(&nfsv4root_newopt);
 	nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
 	nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
 	nfsv4root_mnt.mnt_nvnodelistsize = 0;
+	nfsv4root_mnt.mnt_activevnodelistsize = 0;
 }
 
 /*
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/fs/tmpfs/tmpfs_subr.c
--- a/head/sys/fs/tmpfs/tmpfs_subr.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/fs/tmpfs/tmpfs_subr.c	Sat Apr 21 21:38:59 2012 +0300
@@ -34,7 +34,7 @@
  * Efficient memory file system supporting functions.
  */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/fs/tmpfs/tmpfs_subr.c 234347 2012-04-16 18:10:34Z jh $");
+__FBSDID("$FreeBSD: head/sys/fs/tmpfs/tmpfs_subr.c 234422 2012-04-18 15:22:08Z jh $");
 
 #include <sys/param.h>
 #include <sys/namei.h>
@@ -1080,7 +1080,7 @@
 
 	if ((flags & ~(UF_NODUMP | UF_IMMUTABLE | UF_APPEND | UF_OPAQUE |
 	    UF_NOUNLINK | SF_ARCHIVED | SF_IMMUTABLE | SF_APPEND |
-	    SF_NOUNLINK | SF_SNAPSHOT)) != 0)
+	    SF_NOUNLINK)) != 0)
 		return (EOPNOTSUPP);
 
 	/* Disallow this operation if the file system is mounted read-only. */
@@ -1104,9 +1104,6 @@
 			if (error)
 				return (error);
 		}
-		/* The snapshot flag cannot be toggled. */
-		if ((flags ^ node->tn_flags) & SF_SNAPSHOT)
-			return (EPERM);
 	} else {
 		if (node->tn_flags &
 		    (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/geom/multipath/g_multipath.c
--- a/head/sys/geom/multipath/g_multipath.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/geom/multipath/g_multipath.c	Sat Apr 21 21:38:59 2012 +0300
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/geom/multipath/g_multipath.c 227464 2011-11-12 09:52:27Z mav $");
+__FBSDID("$FreeBSD: head/sys/geom/multipath/g_multipath.c 234415 2012-04-18 09:42:14Z mav $");
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/kernel.h>
@@ -151,20 +151,21 @@
 	if (sc->sc_active == NULL) {
 		printf("GEOM_MULTIPATH: out of providers for %s\n",
 		    sc->sc_name);
-	} else if (!sc->sc_active_active) {
+	} else if (sc->sc_active_active != 1) {
 		printf("GEOM_MULTIPATH: %s is now active path in %s\n",
 		    sc->sc_active->provider->name, sc->sc_name);
 	}
 }
 
 static struct g_consumer *
-g_multipath_choose(struct g_geom *gp)
+g_multipath_choose(struct g_geom *gp, struct bio *bp)
 {
 	struct g_multipath_softc *sc;
 	struct g_consumer *best, *cp;
 
 	sc = gp->softc;
-	if (!sc->sc_active_active)
+	if (sc->sc_active_active == 0 ||
+	    (sc->sc_active_active == 2 && bp->bio_cmd != BIO_READ))
 		return (sc->sc_active);
 	best = NULL;
 	LIST_FOREACH(cp, &gp->consumer, consumer) {
@@ -253,7 +254,7 @@
 		return;
 	}
 	mtx_lock(&sc->sc_mtx);
-	cp = g_multipath_choose(gp);
+	cp = g_multipath_choose(gp, bp);
 	if (cp == NULL) {
 		mtx_unlock(&sc->sc_mtx);
 		g_destroy_bio(cbp);
@@ -323,9 +324,11 @@
 	cnt = (uintptr_t *)&cp->private;
 
 	mtx_lock(&sc->sc_mtx);
-	printf("GEOM_MULTIPATH: Error %d, %s in %s marked FAIL\n",
-	    bp->bio_error, pp->name, sc->sc_name);
-	g_multipath_fault(cp, MP_FAIL);
+	if ((cp->index & MP_FAIL) == 0) {
+		printf("GEOM_MULTIPATH: Error %d, %s in %s marked FAIL\n",
+		    bp->bio_error, pp->name, sc->sc_name);
+		g_multipath_fault(cp, MP_FAIL);
+	}
 	(*cnt)--;
 	if (*cnt == 0 && (cp->index & (MP_LOST | MP_POSTED)) == MP_LOST) {
 		cp->index |= MP_POSTED;
@@ -363,8 +366,10 @@
 			g_multipath_done_error(bp);
 			mtx_lock(&gmtbq_mtx);
 		}
+		if (g_multipath_kt_state != GKT_RUN)
+			break;
 		msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
-		    "gkt:wait", hz / 10);
+		    "gkt:wait", 0);
 	}
 	mtx_unlock(&gmtbq_mtx);
 	wakeup(&g_multipath_kt_state);
@@ -525,7 +530,7 @@
 	    pp->name, sc->sc_name);
 	if (sc->sc_active == NULL) {
 		sc->sc_active = cp;
-		if (!sc->sc_active_active)
+		if (sc->sc_active_active != 1)
 			printf("GEOM_MULTIPATH: %s is now active path in %s\n",
 			    pp->name, sc->sc_name);
 	}
@@ -599,7 +604,7 @@
 	}
 	if (lcp) {
 		sc->sc_active = lcp;
-		if (!sc->sc_active_active)
+		if (sc->sc_active_active != 1)
 			printf("GEOM_MULTIPATH: %s is now active path in %s\n",
 			    lcp->provider->name, sc->sc_name);
 	}
@@ -611,8 +616,7 @@
 {
 	bioq_init(&gmtbq);
 	mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
-	if (kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt") == 0)
-		g_multipath_kt_state = GKT_RUN;
+	kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt");
 }
 
 static void
@@ -879,7 +883,7 @@
 	struct g_geom *gp;
 	const char *mpname, *name;
 	char param[16];
-	int *nargs, i, *active_active;
+	int *nargs, i, *val;
 
 	g_topology_assert();
 
@@ -908,10 +912,13 @@
 	md.md_size = 0;
 	md.md_sectorsize = 0;
 	md.md_uuid[0] = 0;
-	active_active = gctl_get_paraml(req, "active_active",
-	    sizeof(*active_active));
-	md.md_active_active =
-	    (active_active == NULL || *active_active == 0) ? 0 : 1;
+	md.md_active_active = 0;
+	val = gctl_get_paraml(req, "active_active", sizeof(*val));
+	if (val != NULL && *val != 0)
+		md.md_active_active = 1;
+	val = gctl_get_paraml(req, "active_read", sizeof(*val));
+	if (val != NULL && *val != 0)
+		md.md_active_active = 2;
 	gp = g_multipath_create(mp, &md);
 	if (gp == NULL) {
 		gctl_error(req, "GEOM_MULTIPATH: cannot create geom %s/%s\n",
@@ -931,6 +938,67 @@
 }
 
 static void
+g_multipath_ctl_configure(struct gctl_req *req, struct g_class *mp)
+{
+	struct g_multipath_softc *sc;
+	struct g_geom *gp;
+	struct g_consumer *cp;
+	struct g_provider *pp;
+	struct g_multipath_metadata *md;
+	const char *name;
+	int error, *val;
+	void *buf;
+
+	g_topology_assert();
+
+	name = gctl_get_asciiparam(req, "arg0");
+	if (name == NULL) {
+		gctl_error(req, "No 'arg0' argument");
+		return;
+	}
+	gp = g_multipath_find_geom(mp, name);
+	if (gp == NULL) {
+		gctl_error(req, "Device %s is invalid", name);
+		return;
+	}
+	sc = gp->softc;
+	val = gctl_get_paraml(req, "active_active", sizeof(*val));
+	if (val != NULL && *val != 0)
+		sc->sc_active_active = 1;
+	val = gctl_get_paraml(req, "active_read", sizeof(*val));
+	if (val != NULL && *val != 0)
+		sc->sc_active_active = 2;
+	val = gctl_get_paraml(req, "active_passive", sizeof(*val));
+	if (val != NULL && *val != 0)
+		sc->sc_active_active = 0;
+	if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
+		cp = sc->sc_active;
+		pp = cp->provider;
+		error = g_access(cp, 1, 1, 1);
+		if (error != 0) {
+			gctl_error(req, "Can't open %s (%d)", pp->name, error);
+			return;
+		}
+		g_topology_unlock();
+		md = buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
+		strlcpy(md->md_magic, G_MULTIPATH_MAGIC, sizeof(md->md_magic));
+		memcpy(md->md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
+		strlcpy(md->md_name, name, sizeof(md->md_name));
+		md->md_version = G_MULTIPATH_VERSION;
+		md->md_size = pp->mediasize;
+		md->md_sectorsize = pp->sectorsize;
+		md->md_active_active = sc->sc_active_active;
+		error = g_write_data(cp, pp->mediasize - pp->sectorsize,
+		    buf, pp->sectorsize);
+		g_topology_lock();
+		g_access(cp, -1, -1, -1);
+		if (error != 0)
+			gctl_error(req, "Can't update metadata on %s (%d)",
+			    pp->name, error);
+	}
+}
+
+static void
 g_multipath_ctl_fail(struct gctl_req *req, struct g_class *mp, int fail)
 {
 	struct g_multipath_softc *sc;
@@ -964,6 +1032,8 @@
 		    strcmp(cp->provider->name, name) == 0 &&
 		    (cp->index & MP_LOST) == 0) {
 			found = 1;
+			if (!fail == !(cp->index & MP_FAIL))
+				continue;
 			printf("GEOM_MULTIPATH: %s in %s is marked %s.\n",
 				name, sc->sc_name, fail ? "FAIL" : "OK");
 			if (fail) {
@@ -1172,7 +1242,7 @@
 		return;
 	}
 	sc = gp->softc;
-	if (sc->sc_active_active) {
+	if (sc->sc_active_active == 1) {
 		empty = 1;
 		LIST_FOREACH(cp, &gp->consumer, consumer) {
 			if (cp->index & MP_BAD)
@@ -1209,6 +1279,8 @@
 		g_multipath_ctl_add(req, mp);
 	} else if (strcmp(verb, "create") == 0) {
 		g_multipath_ctl_create(req, mp);
+	} else if (strcmp(verb, "configure") == 0) {
+		g_multipath_ctl_configure(req, mp);
 	} else if (strcmp(verb, "stop") == 0) {
 		g_multipath_ctl_stop(req, mp);
 	} else if (strcmp(verb, "destroy") == 0) {
@@ -1245,8 +1317,9 @@
 		    (cp->index & MP_NEW) ? "NEW" :
 		    (cp->index & MP_LOST) ? "LOST" :
 		    (cp->index & MP_FAIL) ? "FAIL" :
-		    (sc->sc_active_active || sc->sc_active == cp) ?
-		     "ACTIVE" : "PASSIVE");
+		    (sc->sc_active_active == 1 || sc->sc_active == cp) ?
+		     "ACTIVE" :
+		     sc->sc_active_active == 2 ? "READ" : "PASSIVE");
 	} else {
 		good = g_multipath_good(gp);
 		sbuf_printf(sb, "%s<State>%s</State>", indent,
@@ -1257,7 +1330,8 @@
 	if (cp == NULL && pp == NULL) {
 		sbuf_printf(sb, "%s<UUID>%s</UUID>", indent, sc->sc_uuid);
 		sbuf_printf(sb, "%s<Mode>Active/%s</Mode>", indent,
-		    sc->sc_active_active ? "Active" : "Passive");
+		    sc->sc_active_active == 2 ? "Read" :
+		    sc->sc_active_active == 1 ? "Active" : "Passive");
 		sbuf_printf(sb, "%s<Type>%s</Type>", indent,
 		    sc->sc_uuid[0] == 0 ? "MANUAL" : "AUTOMATIC");
 	}
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/geom/part/g_part.c
--- a/head/sys/geom/part/g_part.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/geom/part/g_part.c	Sat Apr 21 21:38:59 2012 +0300
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/geom/part/g_part.c 233342 2012-03-23 07:26:17Z ae $");
+__FBSDID("$FreeBSD: head/sys/geom/part/g_part.c 234417 2012-04-18 11:59:03Z marck $");
 
 #include <sys/param.h>
 #include <sys/bio.h>
@@ -103,6 +103,9 @@
 	{ "netbsd-lfs", G_PART_ALIAS_NETBSD_LFS },
 	{ "netbsd-raid", G_PART_ALIAS_NETBSD_RAID },
 	{ "netbsd-swap", G_PART_ALIAS_NETBSD_SWAP },
+	{ "vmware-vmfs", G_PART_ALIAS_VMFS },
+	{ "vmware-vmkdiag", G_PART_ALIAS_VMKDIAG },
+	{ "vmware-reserved", G_PART_ALIAS_VMRESERVED },
 };
 
 SYSCTL_DECL(_kern_geom);
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/geom/part/g_part.h
--- a/head/sys/geom/part/g_part.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/geom/part/g_part.h	Sat Apr 21 21:38:59 2012 +0300
@@ -23,7 +23,7 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * $FreeBSD: head/sys/geom/part/g_part.h 233000 2012-03-15 08:39:10Z ae $
+ * $FreeBSD: head/sys/geom/part/g_part.h 234417 2012-04-18 11:59:03Z marck $
  */
 
 #ifndef _GEOM_PART_H_
@@ -69,6 +69,9 @@
 	G_PART_ALIAS_EBR,		/* A EBR partition entry. */
 	G_PART_ALIAS_MS_FAT32,		/* A Microsoft FAT32 partition entry. */
 	G_PART_ALIAS_BIOS_BOOT,		/* A GRUB 2 boot partition entry. */
+	G_PART_ALIAS_VMFS,		/* A VMware VMFS partition entry */
+	G_PART_ALIAS_VMKDIAG,		/* A VMware vmkDiagnostic partition entry */
+	G_PART_ALIAS_VMRESERVED,	/* A VMware reserved partition entry */
 	/* Keep the following last */
 	G_PART_ALIAS_COUNT
 };
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/geom/part/g_part_gpt.c
--- a/head/sys/geom/part/g_part_gpt.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/geom/part/g_part_gpt.c	Sat Apr 21 21:38:59 2012 +0300
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/geom/part/g_part_gpt.c 226647 2011-10-23 02:51:23Z marcel $");
+__FBSDID("$FreeBSD: head/sys/geom/part/g_part_gpt.c 234417 2012-04-18 11:59:03Z marck $");
 
 #include <sys/param.h>
 #include <sys/bio.h>
@@ -163,6 +163,9 @@
 static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM;
 static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID;
 static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
+static struct uuid gpt_uuid_vmfs = GPT_ENT_TYPE_VMFS;
+static struct uuid gpt_uuid_vmkdiag = GPT_ENT_TYPE_VMKDIAG;
+static struct uuid gpt_uuid_vmreserved = GPT_ENT_TYPE_VMRESERVED;
 static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
 static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED;
 static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA;
@@ -200,6 +203,9 @@
 	{ &gpt_uuid_linux_lvm,		G_PART_ALIAS_LINUX_LVM,		 0 },
 	{ &gpt_uuid_linux_raid,		G_PART_ALIAS_LINUX_RAID,	 0 },
 	{ &gpt_uuid_linux_swap,		G_PART_ALIAS_LINUX_SWAP,	 0 },
+	{ &gpt_uuid_vmfs,		G_PART_ALIAS_VMFS,		 0 },
+	{ &gpt_uuid_vmkdiag,		G_PART_ALIAS_VMKDIAG,		 0 },
+	{ &gpt_uuid_vmreserved,		G_PART_ALIAS_VMRESERVED,	 0 },
 	{ &gpt_uuid_mbr,		G_PART_ALIAS_MBR,		 0 },
 	{ &gpt_uuid_ms_basic_data,	G_PART_ALIAS_MS_BASIC_DATA,	 0x0b },
 	{ &gpt_uuid_ms_ldm_data,	G_PART_ALIAS_MS_LDM_DATA,	 0 },
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/geom/part/g_part_mbr.c
--- a/head/sys/geom/part/g_part_mbr.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/geom/part/g_part_mbr.c	Sat Apr 21 21:38:59 2012 +0300
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/geom/part/g_part_mbr.c 231928 2012-02-20 05:48:57Z ae $");
+__FBSDID("$FreeBSD: head/sys/geom/part/g_part_mbr.c 234417 2012-04-18 11:59:03Z marck $");
 
 #include <sys/param.h>
 #include <sys/bio.h>
@@ -126,6 +126,8 @@
 	{ DOSPTYP_LINLVM,	G_PART_ALIAS_LINUX_LVM },
 	{ DOSPTYP_LINRAID,	G_PART_ALIAS_LINUX_RAID },
 	{ DOSPTYP_PPCBOOT,	G_PART_ALIAS_FREEBSD_BOOT },
+	{ DOSPTYP_VMFS,		G_PART_ALIAS_VMFS },
+	{ DOSPTYP_VMKDIAG,	G_PART_ALIAS_VMKDIAG },
 };
 
 static int
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/geom/raid/g_raid.c
--- a/head/sys/geom/raid/g_raid.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/geom/raid/g_raid.c	Sat Apr 21 21:38:59 2012 +0300
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/geom/raid/g_raid.c 223921 2011-07-11 05:22:31Z ae $");
+__FBSDID("$FreeBSD: head/sys/geom/raid/g_raid.c 234458 2012-04-19 12:30:12Z mav $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -281,6 +281,14 @@
 	case G_RAID_VOLUME_RL_RAID4:
 		return ("RAID4");
 	case G_RAID_VOLUME_RL_RAID5:
+		if (qual == G_RAID_VOLUME_RLQ_R5RA)
+			return ("RAID5RA");
+		if (qual == G_RAID_VOLUME_RLQ_R5RS)
+			return ("RAID5RS");
+		if (qual == G_RAID_VOLUME_RLQ_R5LA)
+			return ("RAID5LA");
+		if (qual == G_RAID_VOLUME_RLQ_R5LS)
+			return ("RAID5LS");
 		return ("RAID5");
 	case G_RAID_VOLUME_RL_RAID6:
 		return ("RAID6");
@@ -313,9 +321,20 @@
 		*level = G_RAID_VOLUME_RL_RAID3;
 	else if (strcasecmp(str, "RAID4") == 0)
 		*level = G_RAID_VOLUME_RL_RAID4;
-	else if (strcasecmp(str, "RAID5") == 0)
+	else if (strcasecmp(str, "RAID5RA") == 0) {
 		*level = G_RAID_VOLUME_RL_RAID5;
-	else if (strcasecmp(str, "RAID6") == 0)
+		*qual = G_RAID_VOLUME_RLQ_R5RA;
+	} else if (strcasecmp(str, "RAID5RS") == 0) {
+		*level = G_RAID_VOLUME_RL_RAID5;
+		*qual = G_RAID_VOLUME_RLQ_R5RS;
+	} else if (strcasecmp(str, "RAID5") == 0 ||
+		   strcasecmp(str, "RAID5LA") == 0) {
+		*level = G_RAID_VOLUME_RL_RAID5;
+		*qual = G_RAID_VOLUME_RLQ_R5LA;
+	} else if (strcasecmp(str, "RAID5LS") == 0) {
+		*level = G_RAID_VOLUME_RL_RAID5;
+		*qual = G_RAID_VOLUME_RLQ_R5LS;
+	} else if (strcasecmp(str, "RAID6") == 0)
 		*level = G_RAID_VOLUME_RL_RAID6;
 	else if (strcasecmp(str, "RAID10") == 0 ||
 		 strcasecmp(str, "RAID1E") == 0)
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/geom/raid/g_raid.h
--- a/head/sys/geom/raid/g_raid.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/geom/raid/g_raid.h	Sat Apr 21 21:38:59 2012 +0300
@@ -23,7 +23,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD$
+ * $FreeBSD: head/sys/geom/raid/g_raid.h 234458 2012-04-19 12:30:12Z mav $
  */
 
 #ifndef	_G_RAID_H_
@@ -227,6 +227,10 @@
 #define G_RAID_VOLUME_RL_UNKNOWN	0xff
 
 #define G_RAID_VOLUME_RLQ_NONE		0x00
+#define G_RAID_VOLUME_RLQ_R5RA		0x00
+#define G_RAID_VOLUME_RLQ_R5RS		0x01
+#define G_RAID_VOLUME_RLQ_R5LA		0x02
+#define G_RAID_VOLUME_RLQ_R5LS		0x03
 #define G_RAID_VOLUME_RLQ_UNKNOWN	0xff
 
 struct g_raid_volume;
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/geom/raid/md_intel.c
--- a/head/sys/geom/raid/md_intel.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/geom/raid/md_intel.c	Sat Apr 21 21:38:59 2012 +0300
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/geom/raid/md_intel.c 229886 2012-01-09 23:01:42Z jimharris $");
+__FBSDID("$FreeBSD: head/sys/geom/raid/md_intel.c 234458 2012-04-19 12:30:12Z mav $");
 
 #include <sys/param.h>
 #include <sys/bio.h>
@@ -682,11 +682,13 @@
 			return (0);
 		if (!force && disks > 6)
 			return (0);
+		if (qual != G_RAID_VOLUME_RLQ_R5LA)
+			return (0);
 		break;
 	default:
 		return (0);
 	}
-	if (qual != G_RAID_VOLUME_RLQ_NONE)
+	if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE)
 		return (0);
 	return (1);
 }
@@ -1029,6 +1031,7 @@
 		mmap = intel_get_map(mvol, 0);
 		vol = g_raid_create_volume(sc, mvol->name, -1);
 		vol->v_md_data = (void *)(intptr_t)i;
+		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
 		if (mmap->type == INTEL_T_RAID0)
 			vol->v_raid_level = G_RAID_VOLUME_RL_RAID0;
 		else if (mmap->type == INTEL_T_RAID1 &&
@@ -1045,11 +1048,11 @@
 				vol->v_raid_level = G_RAID_VOLUME_RL_RAID1;
 			else
 				vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
-		} else if (mmap->type == INTEL_T_RAID5)
+		} else if (mmap->type == INTEL_T_RAID5) {
 			vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
-		else
+			vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LA;
+		} else
 			vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
-		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
 		vol->v_strip_size = (u_int)mmap->strip_sectors * 512; //ZZZ
 		vol->v_disks_count = mmap->total_disks;
 		vol->v_mediasize = (off_t)mvol->total_sectors * 512; //ZZZ
@@ -1485,6 +1488,8 @@
 			gctl_error(req, "No RAID level.");
 			return (-3);
 		}
+		if (strcasecmp(levelname, "RAID5") == 0)
+			levelname = "RAID5LA";
 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
 			return (-4);
@@ -1631,7 +1636,7 @@
 		vol = g_raid_create_volume(sc, volname, -1);
 		vol->v_md_data = (void *)(intptr_t)0;
 		vol->v_raid_level = level;
-		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
+		vol->v_raid_level_qualifier = qual;
 		vol->v_strip_size = strip;
 		vol->v_disks_count = numdisks;
 		if (level == G_RAID_VOLUME_RL_RAID0)
@@ -1658,8 +1663,12 @@
 			if (sd->sd_disk->d_consumer != NULL) {
 				g_raid_change_disk_state(disk,
 				    G_RAID_DISK_S_ACTIVE);
-				g_raid_change_subdisk_state(sd,
-				    G_RAID_SUBDISK_S_ACTIVE);
+				if (level == G_RAID_VOLUME_RL_RAID5)
+					g_raid_change_subdisk_state(sd,
+					    G_RAID_SUBDISK_S_UNINITIALIZED);
+				else
+					g_raid_change_subdisk_state(sd,
+					    G_RAID_SUBDISK_S_ACTIVE);
 				g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
 				    G_RAID_EVENT_SUBDISK);
 			} else {
@@ -1694,6 +1703,8 @@
 			gctl_error(req, "No RAID level.");
 			return (-3);
 		}
+		if (strcasecmp(levelname, "RAID5") == 0)
+			levelname = "RAID5LA";
 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
 			return (-4);
@@ -1818,7 +1829,7 @@
 		vol = g_raid_create_volume(sc, volname, -1);
 		vol->v_md_data = (void *)(intptr_t)i;
 		vol->v_raid_level = level;
-		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
+		vol->v_raid_level_qualifier = qual;
 		vol->v_strip_size = strip;
 		vol->v_disks_count = numdisks;
 		if (level == G_RAID_VOLUME_RL_RAID0)
@@ -1843,8 +1854,12 @@
 			sd->sd_size = size;
 			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
 			if (disk->d_state == G_RAID_DISK_S_ACTIVE) {
-				g_raid_change_subdisk_state(sd,
-				    G_RAID_SUBDISK_S_ACTIVE);
+				if (level == G_RAID_VOLUME_RL_RAID5)
+					g_raid_change_subdisk_state(sd,
+					    G_RAID_SUBDISK_S_UNINITIALIZED);
+				else
+					g_raid_change_subdisk_state(sd,
+					    G_RAID_SUBDISK_S_ACTIVE);
 				g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
 				    G_RAID_EVENT_SUBDISK);
 			}
@@ -2245,6 +2260,9 @@
 			mmap0->status = INTEL_S_FAILURE;
 		else if (vol->v_state == G_RAID_VOLUME_S_DEGRADED)
 			mmap0->status = INTEL_S_DEGRADED;
+		else if (g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_UNINITIALIZED)
+		    == g_raid_nsubdisks(vol, -1))
+			mmap0->status = INTEL_S_UNINITIALIZED;
 		else
 			mmap0->status = INTEL_S_READY;
 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0)
@@ -2288,7 +2306,8 @@
 			    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
 				mmap1->disk_idx[sdi] |= INTEL_DI_RBLD;
 			} else if (sd->sd_state != G_RAID_SUBDISK_S_ACTIVE &&
-			    sd->sd_state != G_RAID_SUBDISK_S_STALE) {
+			    sd->sd_state != G_RAID_SUBDISK_S_STALE &&
+			    sd->sd_state != G_RAID_SUBDISK_S_UNINITIALIZED) {
 				mmap0->disk_idx[sdi] |= INTEL_DI_RBLD;
 				if (mvol->migr_state)
 					mmap1->disk_idx[sdi] |= INTEL_DI_RBLD;
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/geom/raid/md_jmicron.c
--- a/head/sys/geom/raid/md_jmicron.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/geom/raid/md_jmicron.c	Sat Apr 21 21:38:59 2012 +0300
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/geom/raid/md_jmicron.c 220210 2011-03-31 16:19:53Z mav $");
+__FBSDID("$FreeBSD: head/sys/geom/raid/md_jmicron.c 234458 2012-04-19 12:30:12Z mav $");
 
 #include <sys/param.h>
 #include <sys/bio.h>
@@ -400,13 +400,15 @@
 	case G_RAID_VOLUME_RL_RAID5:
 		if (disks < 3)
 			return (0);
+		if (qual != G_RAID_VOLUME_RLQ_R5LA)
+			return (0);
 		if (!force)
 			return (0);
 		break;
 	default:
 		return (0);
 	}
-	if (qual != G_RAID_VOLUME_RLQ_NONE)
+	if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE)
 		return (0);
 	return (1);
 }
@@ -657,6 +659,7 @@
 	vol = g_raid_create_volume(sc, buf, -1);
 	size = ((off_t)meta->disk_sectors_high << 16) + meta->disk_sectors_low;
 	size *= 512; //ZZZ
+	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
 	if (meta->type == JMICRON_T_RAID0) {
 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID0;
 		vol->v_mediasize = size * mdi->mdio_total_disks;
@@ -674,12 +677,12 @@
 		vol->v_mediasize = 0;
 	} else if (meta->type == JMICRON_T_RAID5) {
 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
+		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LA;
 		vol->v_mediasize = size * (mdi->mdio_total_disks - 1);
 	} else {
 		vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
 		vol->v_mediasize = 0;
 	}
-	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
 	vol->v_strip_size = 1024 << meta->stripe_shift; //ZZZ
 	vol->v_disks_count = mdi->mdio_total_disks;
 	vol->v_sectorsize = 512; //ZZZ
@@ -1057,6 +1060,8 @@
 			gctl_error(req, "No RAID level.");
 			return (-3);
 		}
+		if (strcasecmp(levelname, "RAID5") == 0)
+			levelname = "RAID5LA";
 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
 			return (-4);
@@ -1192,7 +1197,7 @@
 		vol = g_raid_create_volume(sc, volname, -1);
 		vol->v_md_data = (void *)(intptr_t)0;
 		vol->v_raid_level = level;
-		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
+		vol->v_raid_level_qualifier = qual;
 		vol->v_strip_size = strip;
 		vol->v_disks_count = numdisks;
 		if (level == G_RAID_VOLUME_RL_RAID0 ||
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/geom/raid/md_nvidia.c
--- a/head/sys/geom/raid/md_nvidia.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/geom/raid/md_nvidia.c	Sat Apr 21 21:38:59 2012 +0300
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/geom/raid/md_nvidia.c 226816 2011-10-26 21:50:10Z mav $");
+__FBSDID("$FreeBSD: head/sys/geom/raid/md_nvidia.c 234458 2012-04-19 12:30:12Z mav $");
 
 #include <sys/param.h>
 #include <sys/bio.h>
@@ -407,11 +407,14 @@
 	case G_RAID_VOLUME_RL_RAID5:
 		if (disks < 3)
 			return (0);
+		if (qual != G_RAID_VOLUME_RLQ_R5LA &&
+		    qual != G_RAID_VOLUME_RLQ_R5LS)
+			return (0);
 		break;
 	default:
 		return (0);
 	}
-	if (qual != G_RAID_VOLUME_RLQ_NONE)
+	if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE)
 		return (0);
 	return (1);
 }
@@ -679,10 +682,11 @@
 		size = 0;
 	} else if (meta->type == NVIDIA_T_RAID5) {
 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
+		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LA;
 		size = vol->v_mediasize / (mdi->mdio_total_disks - 1);
 	} else if (meta->type == NVIDIA_T_RAID5_SYM) {
 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
-//		vol->v_raid_level_qualifier = 0x03;
+		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LS;
 		size = vol->v_mediasize / (mdi->mdio_total_disks - 1);
 	} else {
 		vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
@@ -1059,6 +1063,8 @@
 			gctl_error(req, "No RAID level.");
 			return (-3);
 		}
+		if (strcasecmp(levelname, "RAID5") == 0)
+			levelname = "RAID5LS";
 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
 			return (-4);
@@ -1206,7 +1212,7 @@
 		vol = g_raid_create_volume(sc, volname, -1);
 		vol->v_md_data = (void *)(intptr_t)0;
 		vol->v_raid_level = level;
-		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
+		vol->v_raid_level_qualifier = qual;
 		vol->v_strip_size = strip;
 		vol->v_disks_count = numdisks;
 		vol->v_mediasize = volsize;
@@ -1454,8 +1460,8 @@
 	else if (vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT ||
 	    vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE)
 		meta->type = NVIDIA_T_CONCAT;
-//	else if (vol->v_raid_level_qualifier == 0)
-//		meta->type = NVIDIA_T_RAID5;
+	else if (vol->v_raid_level_qualifier == G_RAID_VOLUME_RLQ_R5LA)
+		meta->type = NVIDIA_T_RAID5;
 	else
 		meta->type = NVIDIA_T_RAID5_SYM;
 	meta->strip_sectors = vol->v_strip_size / vol->v_sectorsize;
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/geom/raid/md_promise.c
--- a/head/sys/geom/raid/md_promise.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/geom/raid/md_promise.c	Sat Apr 21 21:38:59 2012 +0300
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/geom/raid/md_promise.c 226816 2011-10-26 21:50:10Z mav $");
+__FBSDID("$FreeBSD: head/sys/geom/raid/md_promise.c 234458 2012-04-19 12:30:12Z mav $");
 
 #include <sys/param.h>
 #include <sys/bio.h>
@@ -595,11 +595,13 @@
 	case G_RAID_VOLUME_RL_RAID5:
 		if (disks < 3)
 			return (0);
+		if (qual != G_RAID_VOLUME_RLQ_R5LA)
+			return (0);
 		break;
 	default:
 		return (0);
 	}
-	if (qual != G_RAID_VOLUME_RLQ_NONE)
+	if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE)
 		return (0);
 	return (1);
 }
@@ -848,6 +850,7 @@
 	pv = vol->v_md_data;
 	meta = pv->pv_meta;
 
+	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
 	if (meta->type == PROMISE_T_RAID0)
 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID0;
 	else if (meta->type == PROMISE_T_RAID1) {
@@ -857,15 +860,15 @@
 			vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
 	} else if (meta->type == PROMISE_T_RAID3)
 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID3;
-	else if (meta->type == PROMISE_T_RAID5)
+	else if (meta->type == PROMISE_T_RAID5) {
 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
-	else if (meta->type == PROMISE_T_SPAN)
+		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LA;
+	} else if (meta->type == PROMISE_T_SPAN)
 		vol->v_raid_level = G_RAID_VOLUME_RL_CONCAT;
 	else if (meta->type == PROMISE_T_JBOD)
 		vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE;
 	else
 		vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
-	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
 	vol->v_strip_size = 512 << meta->stripe_shift; //ZZZ
 	vol->v_disks_count = meta->total_disks;
 	vol->v_mediasize = (off_t)meta->total_sectors * 512; //ZZZ
@@ -1241,6 +1244,8 @@
 			gctl_error(req, "No RAID level.");
 			return (-3);
 		}
+		if (strcasecmp(levelname, "RAID5") == 0)
+			levelname = "RAID5LA";
 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
 			return (-4);
@@ -1415,7 +1420,7 @@
 		vol = g_raid_create_volume(sc, volname, -1);
 		vol->v_md_data = pv;
 		vol->v_raid_level = level;
-		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
+		vol->v_raid_level_qualifier = qual;
 		vol->v_strip_size = strip;
 		vol->v_disks_count = numdisks;
 		if (level == G_RAID_VOLUME_RL_RAID0 ||
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/geom/raid/md_sii.c
--- a/head/sys/geom/raid/md_sii.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/geom/raid/md_sii.c	Sat Apr 21 21:38:59 2012 +0300
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/geom/raid/md_sii.c 220210 2011-03-31 16:19:53Z mav $");
+__FBSDID("$FreeBSD: head/sys/geom/raid/md_sii.c 234458 2012-04-19 12:30:12Z mav $");
 
 #include <sys/param.h>
 #include <sys/bio.h>
@@ -456,11 +456,13 @@
 	case G_RAID_VOLUME_RL_RAID5:
 		if (disks < 3)
 			return (0);
+		if (qual != G_RAID_VOLUME_RLQ_R5LS)
+			return (0);
 		break;
 	default:
 		return (0);
 	}
-	if (qual != G_RAID_VOLUME_RLQ_NONE)
+	if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE)
 		return (0);
 	return (1);
 }
@@ -719,6 +721,7 @@
 	sii_meta_get_name(meta, buf);
 	vol = g_raid_create_volume(sc, buf, -1);
 	vol->v_mediasize = (off_t)meta->total_sectors * 512;
+	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
 	if (meta->type == SII_T_RAID0) {
 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID0;
 		size = vol->v_mediasize / mdi->mdio_total_disks;
@@ -736,6 +739,7 @@
 		size = 0;
 	} else if (meta->type == SII_T_RAID5) {
 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
+		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LS;
 		size = vol->v_mediasize / (mdi->mdio_total_disks - 1);
 	} else if (meta->type == SII_T_JBOD) {
 		vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE;
@@ -744,7 +748,6 @@
 		vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
 		size = 0;
 	}
-	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
 	vol->v_strip_size = meta->strip_sectors * 512; //ZZZ
 	vol->v_disks_count = mdi->mdio_total_disks;
 	vol->v_sectorsize = 512; //ZZZ
@@ -1144,6 +1147,8 @@
 			gctl_error(req, "No RAID level.");
 			return (-3);
 		}
+		if (strcasecmp(levelname, "RAID5") == 0)
+			levelname = "RAID5LS";
 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
 			return (-4);
@@ -1278,7 +1283,7 @@
 		vol = g_raid_create_volume(sc, volname, -1);
 		vol->v_md_data = (void *)(intptr_t)0;
 		vol->v_raid_level = level;
-		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
+		vol->v_raid_level_qualifier = qual;
 		vol->v_strip_size = strip;
 		vol->v_disks_count = numdisks;
 		if (level == G_RAID_VOLUME_RL_RAID0 ||
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/geom/raid/tr_raid5.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/geom/raid/tr_raid5.c	Sat Apr 21 21:38:59 2012 +0300
@@ -0,0 +1,376 @@
+/*-
+ * Copyright (c) 2012 Alexander Motin <mav at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/sys/geom/raid/tr_raid5.c 234458 2012-04-19 12:30:12Z mav $");
+
+#include <sys/param.h>
+#include <sys/bio.h>
+#include <sys/endian.h>
+#include <sys/kernel.h>
+#include <sys/kobj.h>
+#include <sys/limits.h>
+#include <sys/lock.h>
+#include <sys/malloc.h>
+#include <sys/mutex.h>
+#include <sys/sysctl.h>
+#include <sys/systm.h>
+#include <geom/geom.h>
+#include "geom/raid/g_raid.h"
+#include "g_raid_tr_if.h"
+
+SYSCTL_DECL(_kern_geom_raid);
+
+static MALLOC_DEFINE(M_TR_RAID5, "tr_raid5_data", "GEOM_RAID RAID5 data");
+
+#define TR_RAID5_NONE 0
+#define TR_RAID5_REBUILD 1
+#define TR_RAID5_RESYNC 2
+
+#define TR_RAID5_F_DOING_SOME	0x1
+#define TR_RAID5_F_LOCKED	0x2
+#define TR_RAID5_F_ABORT	0x4
+
+struct g_raid_tr_raid5_object {
+	struct g_raid_tr_object	 trso_base;
+	int			 trso_starting;
+	int			 trso_stopping;
+	int			 trso_type;
+	int			 trso_recover_slabs; /* slabs before rest */
+	int			 trso_fair_io;
+	int			 trso_meta_update;
+	int			 trso_flags;
+	struct g_raid_subdisk	*trso_failed_sd; /* like per volume */
+	void			*trso_buffer;	 /* Buffer space */
+	struct bio		 trso_bio;
+};
+
+static g_raid_tr_taste_t g_raid_tr_taste_raid5;
+static g_raid_tr_event_t g_raid_tr_event_raid5;
+static g_raid_tr_start_t g_raid_tr_start_raid5;
+static g_raid_tr_stop_t g_raid_tr_stop_raid5;
+static g_raid_tr_iostart_t g_raid_tr_iostart_raid5;
+static g_raid_tr_iodone_t g_raid_tr_iodone_raid5;
+static g_raid_tr_kerneldump_t g_raid_tr_kerneldump_raid5;
+static g_raid_tr_locked_t g_raid_tr_locked_raid5;
+static g_raid_tr_free_t g_raid_tr_free_raid5;
+
+static kobj_method_t g_raid_tr_raid5_methods[] = {
+	KOBJMETHOD(g_raid_tr_taste,	g_raid_tr_taste_raid5),
+	KOBJMETHOD(g_raid_tr_event,	g_raid_tr_event_raid5),
+	KOBJMETHOD(g_raid_tr_start,	g_raid_tr_start_raid5),
+	KOBJMETHOD(g_raid_tr_stop,	g_raid_tr_stop_raid5),
+	KOBJMETHOD(g_raid_tr_iostart,	g_raid_tr_iostart_raid5),
+	KOBJMETHOD(g_raid_tr_iodone,	g_raid_tr_iodone_raid5),
+	KOBJMETHOD(g_raid_tr_kerneldump, g_raid_tr_kerneldump_raid5),
+	KOBJMETHOD(g_raid_tr_locked,	g_raid_tr_locked_raid5),
+	KOBJMETHOD(g_raid_tr_free,	g_raid_tr_free_raid5),
+	{ 0, 0 }
+};
+
+static struct g_raid_tr_class g_raid_tr_raid5_class = {
+	"RAID5",
+	g_raid_tr_raid5_methods,
+	sizeof(struct g_raid_tr_raid5_object),
+	.trc_priority = 100
+};
+
+static int
+g_raid_tr_taste_raid5(struct g_raid_tr_object *tr, struct g_raid_volume *vol)
+{
+	struct g_raid_tr_raid5_object *trs;
+	u_int qual;
+
+	trs = (struct g_raid_tr_raid5_object *)tr;
+	qual = tr->tro_volume->v_raid_level_qualifier;
+	if (tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID5 &&
+	    qual >= 0 && qual <= 3) {
+		/* RAID5 */
+	} else
+		return (G_RAID_TR_TASTE_FAIL);
+	trs->trso_starting = 1;
+	return (G_RAID_TR_TASTE_SUCCEED);
+}
+
+static int
+g_raid_tr_update_state_raid5(struct g_raid_volume *vol,
+    struct g_raid_subdisk *sd)
+{
+	struct g_raid_tr_raid5_object *trs;
+	struct g_raid_softc *sc;
+	u_int s;
+	int na, ns, nu;
+
+	sc = vol->v_softc;
+	trs = (struct g_raid_tr_raid5_object *)vol->v_tr;
+	if (trs->trso_stopping &&
+	    (trs->trso_flags & TR_RAID5_F_DOING_SOME) == 0)
+		s = G_RAID_VOLUME_S_STOPPED;
+	else if (trs->trso_starting)
+		s = G_RAID_VOLUME_S_STARTING;
+	else {
+		na = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE);
+		ns = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) +
+		     g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC);
+		nu = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_UNINITIALIZED);
+		if (na == vol->v_disks_count)
+			s = G_RAID_VOLUME_S_OPTIMAL;
+		else if (na + ns == vol->v_disks_count ||
+		    na + ns + nu == vol->v_disks_count /* XXX: Temporary. */)
+			s = G_RAID_VOLUME_S_SUBOPTIMAL;
+		else if (na == vol->v_disks_count - 1 ||
+		    na + ns + nu == vol->v_disks_count)
+			s = G_RAID_VOLUME_S_DEGRADED;
+		else
+			s = G_RAID_VOLUME_S_BROKEN;
+	}
+	if (s != vol->v_state) {
+		g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ?
+		    G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN,
+		    G_RAID_EVENT_VOLUME);
+		g_raid_change_volume_state(vol, s);
+		if (!trs->trso_starting && !trs->trso_stopping)
+			g_raid_write_metadata(sc, vol, NULL, NULL);
+	}
+	return (0);
+}
+
+static int
+g_raid_tr_event_raid5(struct g_raid_tr_object *tr,
+    struct g_raid_subdisk *sd, u_int event)
+{
+
+	g_raid_tr_update_state_raid5(tr->tro_volume, sd);
+	return (0);
+}
+
+static int
+g_raid_tr_start_raid5(struct g_raid_tr_object *tr)
+{
+	struct g_raid_tr_raid5_object *trs;
+	struct g_raid_volume *vol;
+
+	trs = (struct g_raid_tr_raid5_object *)tr;
+	vol = tr->tro_volume;
+	trs->trso_starting = 0;
+	g_raid_tr_update_state_raid5(vol, NULL);
+	return (0);
+}
+
+static int
+g_raid_tr_stop_raid5(struct g_raid_tr_object *tr)
+{
+	struct g_raid_tr_raid5_object *trs;
+	struct g_raid_volume *vol;
+
+	trs = (struct g_raid_tr_raid5_object *)tr;
+	vol = tr->tro_volume;
+	trs->trso_starting = 0;
+	trs->trso_stopping = 1;
+	g_raid_tr_update_state_raid5(vol, NULL);
+	return (0);
+}
+
+static void
+g_raid_tr_iostart_raid5_read(struct g_raid_tr_object *tr, struct bio *bp)
+{
+	struct g_raid_volume *vol;
+	struct g_raid_subdisk *sd;
+	struct bio_queue_head queue;
+	struct bio *cbp;
+	char *addr;
+	off_t offset, start, length, nstripe, remain;
+	int no, pno;
+	u_int strip_size, qual;
+
+	vol = tr->tro_volume;
+	addr = bp->bio_data;
+	strip_size = vol->v_strip_size;
+	qual = tr->tro_volume->v_raid_level_qualifier;
+
+	/* Stripe number. */
+	nstripe = bp->bio_offset / strip_size;
+	/* Start position in stripe. */
+	start = bp->bio_offset % strip_size;
+	/* Parity disk number. */
+	pno = nstripe / (vol->v_disks_count - 1) % vol->v_disks_count;
+	if (qual >= 2)
+		pno = (vol->v_disks_count - 1) - pno;
+	/* Disk number. */
+	no = nstripe % (vol->v_disks_count - 1);
+	if (qual & 1) {
+		no = (pno + no + 1) % vol->v_disks_count;
+	} else if (no >= pno)
+		no++;
+	/* Stripe start position in disk. */
+	offset = (nstripe / (vol->v_disks_count - 1)) * strip_size;
+	/* Length of data to operate. */
+	remain = bp->bio_length;
+
+	bioq_init(&queue);
+	do {
+		length = MIN(strip_size - start, remain);
+		cbp = g_clone_bio(bp);
+		if (cbp == NULL)
+			goto failure;
+		cbp->bio_offset = offset + start;
+		cbp->bio_data = addr;
+		cbp->bio_length = length;
+		cbp->bio_caller1 = &vol->v_subdisks[no];
+		bioq_insert_tail(&queue, cbp);
+		no++;
+		if (qual & 1) {
+			no %= vol->v_disks_count;
+			if (no == pno) {
+				if (qual < 2) {
+					pno = (pno + 1) % vol->v_disks_count;
+					no = (no + 2) % vol->v_disks_count;
+				} else if (pno == 0)
+					pno = vol->v_disks_count - 1;
+				else
+					pno--;
+				offset += strip_size;
+			}
+		} else {
+			if (no == pno)
+				no++;
+			if (no >= vol->v_disks_count) {
+				no %= vol->v_disks_count;
+				if (qual < 2)
+					pno = (pno + 1) % vol->v_disks_count;
+				else if (pno == 0)
+					pno = vol->v_disks_count - 1;
+				else
+					pno--;
+				offset += strip_size;
+			}
+			if (no == pno)
+				no++;
+		}
+		remain -= length;
+		addr += length;
+		start = 0;
+	} while (remain > 0);
+	for (cbp = bioq_first(&queue); cbp != NULL;
+	    cbp = bioq_first(&queue)) {
+		bioq_remove(&queue, cbp);
+		sd = cbp->bio_caller1;
+		cbp->bio_caller1 = NULL;
+		g_raid_subdisk_iostart(sd, cbp);
+	}
+	return;
+failure:
+	for (cbp = bioq_first(&queue); cbp != NULL;
+	    cbp = bioq_first(&queue)) {
+		bioq_remove(&queue, cbp);
+		g_destroy_bio(cbp);
+	}
+	if (bp->bio_error == 0)
+		bp->bio_error = ENOMEM;
+	g_raid_iodone(bp, bp->bio_error);
+}
+
+static void
+g_raid_tr_iostart_raid5(struct g_raid_tr_object *tr, struct bio *bp)
+{
+	struct g_raid_volume *vol;
+	struct g_raid_tr_raid5_object *trs;
+
+	vol = tr->tro_volume;
+	trs = (struct g_raid_tr_raid5_object *)tr;
+	if (vol->v_state < G_RAID_VOLUME_S_SUBOPTIMAL) {
+		g_raid_iodone(bp, EIO);
+		return;
+	}
+	switch (bp->bio_cmd) {
+	case BIO_READ:
+		g_raid_tr_iostart_raid5_read(tr, bp);
+		break;
+	case BIO_WRITE:
+	case BIO_DELETE:
+	case BIO_FLUSH:
+		g_raid_iodone(bp, ENODEV);
+		break;
+	default:
+		KASSERT(1 == 0, ("Invalid command here: %u (volume=%s)",
+		    bp->bio_cmd, vol->v_name));
+		break;
+	}
+}
+
+static void
+g_raid_tr_iodone_raid5(struct g_raid_tr_object *tr,
+    struct g_raid_subdisk *sd, struct bio *bp)
+{
+	struct bio *pbp;
+	int error;
+
+	pbp = bp->bio_parent;
+	pbp->bio_inbed++;
+	error = bp->bio_error;
+	g_destroy_bio(bp);
+	if (pbp->bio_children == pbp->bio_inbed) {
+		pbp->bio_completed = pbp->bio_length;
+		g_raid_iodone(pbp, error);
+	}
+}
+
+static int
+g_raid_tr_kerneldump_raid5(struct g_raid_tr_object *tr,
+    void *virtual, vm_offset_t physical, off_t offset, size_t length)
+{
+
+	return (ENODEV);
+}
+
+static int
+g_raid_tr_locked_raid5(struct g_raid_tr_object *tr, void *argp)
+{
+	struct bio *bp;
+	struct g_raid_subdisk *sd;
+
+	bp = (struct bio *)argp;
+	sd = (struct g_raid_subdisk *)bp->bio_caller1;
+	g_raid_subdisk_iostart(sd, bp);
+
+	return (0);
+}
+
+static int
+g_raid_tr_free_raid5(struct g_raid_tr_object *tr)
+{
+	struct g_raid_tr_raid5_object *trs;
+
+	trs = (struct g_raid_tr_raid5_object *)tr;
+
+	if (trs->trso_buffer != NULL) {
+		free(trs->trso_buffer, M_TR_RAID5);
+		trs->trso_buffer = NULL;
+	}
+	return (0);
+}
+
+G_RAID_TR_DECLARE(g_raid_tr_raid5);
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/i386/conf/GENERIC
--- a/head/sys/i386/conf/GENERIC	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/i386/conf/GENERIC	Sat Apr 21 21:38:59 2012 +0300
@@ -16,7 +16,7 @@
 # If you are in doubt as to the purpose or necessity of a line, check first
 # in NOTES.
 #
-# $FreeBSD: head/sys/i386/conf/GENERIC 233427 2012-03-24 18:08:28Z marius $
+# $FreeBSD: head/sys/i386/conf/GENERIC 234504 2012-04-20 21:37:42Z brooks $
 
 cpu		I486_CPU
 cpu		I586_CPU
@@ -24,6 +24,7 @@
 ident		GENERIC
 
 makeoptions	DEBUG=-g		# Build kernel with gdb(1) debug symbols
+makeoptions	WITH_CTF=1		# Run ctfconvert(1) for DTrace support
 
 options 	SCHED_ULE		# ULE scheduler
 options 	PREEMPTION		# Enable kernel thread preemption
@@ -64,7 +65,7 @@
 options 	CAPABILITY_MODE		# Capsicum capability mode
 options 	CAPABILITIES		# Capsicum capabilities
 options 	MAC			# TrustedBSD MAC Framework
-#options 	KDTRACE_HOOKS		# Kernel DTrace hooks
+options 	KDTRACE_HOOKS		# Kernel DTrace hooks
 options 	INCLUDE_CONFIG_FILE     # Include this file in kernel
 
 # Debugging support.  Always need this:
@@ -74,6 +75,7 @@
 # For full debugger support use this instead:
 options 	DDB			# Support DDB.
 options 	GDB			# Support remote GDB.
+options 	DDB_CTF			# kernel ELF linker loads CTF data
 options 	DEADLKRES		# Enable the deadlock resolver
 options 	INVARIANTS		# Enable calls of extra sanity checking
 options 	INVARIANT_SUPPORT	# Extra sanity checks of internal structures, required by INVARIANTS
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/kern/kern_condvar.c
--- a/head/sys/kern/kern_condvar.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/kern/kern_condvar.c	Sat Apr 21 21:38:59 2012 +0300
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sys/kern/kern_condvar.c 234494 2012-04-20 15:32:36Z jhb $");
 
 #include "opt_ktrace.h"
 
@@ -103,7 +103,7 @@
 	lock_state = 0;
 #ifdef KTRACE
 	if (KTRPOINT(td, KTR_CSW))
-		ktrcsw(1, 0);
+		ktrcsw(1, 0, cv_wmesg(cvp));
 #endif
 	CV_ASSERT(cvp, lock, td);
 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
@@ -140,7 +140,7 @@
 
 #ifdef KTRACE
 	if (KTRPOINT(td, KTR_CSW))
-		ktrcsw(0, 0);
+		ktrcsw(0, 0, cv_wmesg(cvp));
 #endif
 	PICKUP_GIANT();
 	if (lock != &Giant.lock_object) {
@@ -162,7 +162,7 @@
 	td = curthread;
 #ifdef KTRACE
 	if (KTRPOINT(td, KTR_CSW))
-		ktrcsw(1, 0);
+		ktrcsw(1, 0, cv_wmesg(cvp));
 #endif
 	CV_ASSERT(cvp, lock, td);
 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
@@ -197,7 +197,7 @@
 
 #ifdef KTRACE
 	if (KTRPOINT(td, KTR_CSW))
-		ktrcsw(0, 0);
+		ktrcsw(0, 0, cv_wmesg(cvp));
 #endif
 	PICKUP_GIANT();
 }
@@ -220,7 +220,7 @@
 	lock_state = 0;
 #ifdef KTRACE
 	if (KTRPOINT(td, KTR_CSW))
-		ktrcsw(1, 0);
+		ktrcsw(1, 0, cv_wmesg(cvp));
 #endif
 	CV_ASSERT(cvp, lock, td);
 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
@@ -258,7 +258,7 @@
 
 #ifdef KTRACE
 	if (KTRPOINT(td, KTR_CSW))
-		ktrcsw(0, 0);
+		ktrcsw(0, 0, cv_wmesg(cvp));
 #endif
 	PICKUP_GIANT();
 	if (lock != &Giant.lock_object) {
@@ -286,7 +286,7 @@
 	lock_state = 0;
 #ifdef KTRACE
 	if (KTRPOINT(td, KTR_CSW))
-		ktrcsw(1, 0);
+		ktrcsw(1, 0, cv_wmesg(cvp));
 #endif
 	CV_ASSERT(cvp, lock, td);
 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
@@ -324,7 +324,7 @@
 
 #ifdef KTRACE
 	if (KTRPOINT(td, KTR_CSW))
-		ktrcsw(0, 0);
+		ktrcsw(0, 0, cv_wmesg(cvp));
 #endif
 	PICKUP_GIANT();
 	if (lock != &Giant.lock_object) {
@@ -353,7 +353,7 @@
 	lock_state = 0;
 #ifdef KTRACE
 	if (KTRPOINT(td, KTR_CSW))
-		ktrcsw(1, 0);
+		ktrcsw(1, 0, cv_wmesg(cvp));
 #endif
 	CV_ASSERT(cvp, lock, td);
 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
@@ -392,7 +392,7 @@
 
 #ifdef KTRACE
 	if (KTRPOINT(td, KTR_CSW))
-		ktrcsw(0, 0);
+		ktrcsw(0, 0, cv_wmesg(cvp));
 #endif
 	PICKUP_GIANT();
 	if (lock != &Giant.lock_object) {
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/kern/kern_ktrace.c
--- a/head/sys/kern/kern_ktrace.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/kern/kern_ktrace.c	Sat Apr 21 21:38:59 2012 +0300
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/kern_ktrace.c 233925 2012-04-05 17:13:14Z jhb $");
+__FBSDID("$FreeBSD: head/sys/kern/kern_ktrace.c 234494 2012-04-20 15:32:36Z jhb $");
 
 #include "opt_ktrace.h"
 
@@ -733,8 +733,9 @@
 }
 
 void
-ktrcsw(out, user)
+ktrcsw(out, user, wmesg)
 	int out, user;
+	const char *wmesg;
 {
 	struct thread *td = curthread;
 	struct ktr_request *req;
@@ -746,6 +747,10 @@
 	kc = &req->ktr_data.ktr_csw;
 	kc->out = out;
 	kc->user = user;
+	if (wmesg != NULL)
+		strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg));
+	else
+		bzero(kc->wmesg, sizeof(kc->wmesg));
 	ktr_enqueuerequest(td, req);
 	ktrace_exit(td);
 }
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/kern/kern_racct.c
--- a/head/sys/kern/kern_racct.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/kern/kern_racct.c	Sat Apr 21 21:38:59 2012 +0300
@@ -26,11 +26,11 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: head/sys/kern/kern_racct.c 233126 2012-03-18 19:13:32Z jh $
+ * $FreeBSD: head/sys/kern/kern_racct.c 234383 2012-04-17 14:31:02Z trasz $
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/kern_racct.c 233126 2012-03-18 19:13:32Z jh $");
+__FBSDID("$FreeBSD: head/sys/kern/kern_racct.c 234383 2012-04-17 14:31:02Z trasz $");
 
 #include "opt_kdtrace.h"
 
@@ -267,9 +267,6 @@
 	int error;
 #endif
 
-	if (p->p_flag & P_SYSTEM)
-		return (0);
-
 	SDT_PROBE(racct, kernel, rusage, add, p, resource, amount, 0, 0);
 
 	/*
@@ -344,9 +341,6 @@
 racct_add_force(struct proc *p, int resource, uint64_t amount)
 {
 
-	if (p->p_flag & P_SYSTEM)
-		return;
-
 	SDT_PROBE(racct, kernel, rusage, add_force, p, resource, amount, 0, 0);
 
 	/*
@@ -368,9 +362,6 @@
 	int error;
 #endif
 
-	if (p->p_flag & P_SYSTEM)
-		return (0);
-
 	SDT_PROBE(racct, kernel, rusage, set, p, resource, amount, 0, 0);
 
 	/*
@@ -426,9 +417,6 @@
 {
 	int64_t diff;
 
-	if (p->p_flag & P_SYSTEM)
-		return;
-
 	SDT_PROBE(racct, kernel, rusage, set, p, resource, amount, 0, 0);
 
 	/*
@@ -487,9 +475,6 @@
 racct_sub(struct proc *p, int resource, uint64_t amount)
 {
 
-	if (p->p_flag & P_SYSTEM)
-		return;
-
 	SDT_PROBE(racct, kernel, rusage, sub, p, resource, amount, 0, 0);
 
 	/*
@@ -556,12 +541,6 @@
 	 */
 	racct_create(&child->p_racct);
 
-	/*
-	 * No resource accounting for kernel processes.
-	 */
-	if (child->p_flag & P_SYSTEM)
-		return (0);
-
 	PROC_LOCK(parent);
 	PROC_LOCK(child);
 	mtx_lock(&racct_lock);
@@ -723,8 +702,6 @@
 		FOREACH_PROC_IN_SYSTEM(p) {
 			if (p->p_state != PRS_NORMAL)
 				continue;
-			if (p->p_flag & P_SYSTEM)
-				continue;
 
 			microuptime(&wallclock);
 			timevalsub(&wallclock, &p->p_stats->p_start);
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/kern/kern_rctl.c
--- a/head/sys/kern/kern_rctl.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/kern/kern_rctl.c	Sat Apr 21 21:38:59 2012 +0300
@@ -26,11 +26,11 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: head/sys/kern/kern_rctl.c 227293 2011-11-07 06:44:47Z ed $
+ * $FreeBSD: head/sys/kern/kern_rctl.c 234383 2012-04-17 14:31:02Z trasz $
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/kern_rctl.c 227293 2011-11-07 06:44:47Z ed $");
+__FBSDID("$FreeBSD: head/sys/kern/kern_rctl.c 234383 2012-04-17 14:31:02Z trasz $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -73,6 +73,7 @@
 
 /* Default buffer size for rctl_get_rules(2). */
 #define	RCTL_DEFAULT_BUFSIZE	4096
+#define	RCTL_MAX_INBUFLEN	4096
 #define	RCTL_LOG_BUFSIZE	128
 
 /*
@@ -993,11 +994,6 @@
 	case RCTL_SUBJECT_TYPE_PROCESS:
 		p = rule->rr_subject.rs_proc;
 		KASSERT(p != NULL, ("rctl_rule_add: NULL proc"));
-		/*
-		 * No resource limits for system processes.
-		 */
-		if (p->p_flag & P_SYSTEM)
-			return (EPERM);
 
 		rctl_racct_add_rule(p->p_racct, rule);
 		/*
@@ -1035,8 +1031,6 @@
 	 */
 	sx_assert(&allproc_lock, SA_LOCKED);
 	FOREACH_PROC_IN_SYSTEM(p) {
-		if (p->p_flag & P_SYSTEM)
-			continue;
 		cred = p->p_ucred;
 		switch (rule->rr_subject_type) {
 		case RCTL_SUBJECT_TYPE_USER:
@@ -1191,6 +1185,8 @@
 
 	if (inbuflen <= 0)
 		return (EINVAL);
+	if (inbuflen > RCTL_MAX_INBUFLEN)
+		return (E2BIG);
 
 	str = malloc(inbuflen + 1, M_RCTL, M_WAITOK);
 	error = copyinstr(inbufp, str, inbuflen, NULL);
@@ -1281,10 +1277,6 @@
 			error = EINVAL;
 			goto out;
 		}
-		if (p->p_flag & P_SYSTEM) {
-			error = EINVAL;
-			goto out;
-		}
 		outputsbuf = rctl_racct_to_sbuf(p->p_racct, 0);
 		break;
 	case RCTL_SUBJECT_TYPE_USER:
@@ -1716,20 +1708,7 @@
 
 	LIST_INIT(&child->p_racct->r_rule_links);
 
-	/*
-	 * No limits for kernel processes.
-	 */
-	if (child->p_flag & P_SYSTEM)
-		return (0);
-
-	/*
-	 * Nothing to inherit from P_SYSTEM parents.
-	 */
-	if (parent->p_racct == NULL) {
-		KASSERT(parent->p_flag & P_SYSTEM,
-		    ("non-system process without racct; p = %p", parent));
-		return (0);
-	}
+	KASSERT(parent->p_racct != NULL, ("process without racct; p = %p", parent));
 
 	rw_wlock(&rctl_lock);
 
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/kern/kern_synch.c
--- a/head/sys/kern/kern_synch.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/kern/kern_synch.c	Sat Apr 21 21:38:59 2012 +0300
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/kern_synch.c 228424 2011-12-11 21:02:01Z avg $");
+__FBSDID("$FreeBSD: head/sys/kern/kern_synch.c 234494 2012-04-20 15:32:36Z jhb $");
 
 #include "opt_ktrace.h"
 #include "opt_sched.h"
@@ -142,7 +142,7 @@
 	p = td->td_proc;
 #ifdef KTRACE
 	if (KTRPOINT(td, KTR_CSW))
-		ktrcsw(1, 0);
+		ktrcsw(1, 0, wmesg);
 #endif
 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
 	    "Sleeping on \"%s\"", wmesg);
@@ -236,7 +236,7 @@
 	}
 #ifdef KTRACE
 	if (KTRPOINT(td, KTR_CSW))
-		ktrcsw(0, 0);
+		ktrcsw(0, 0, wmesg);
 #endif
 	PICKUP_GIANT();
 	if (lock != NULL && lock != &Giant.lock_object && !(priority & PDROP)) {
@@ -298,7 +298,7 @@
 #ifdef KTRACE
 	if (KTRPOINT(td, KTR_CSW)) {
 		sleepq_release(ident);
-		ktrcsw(1, 0);
+		ktrcsw(1, 0, wmesg);
 		sleepq_lock(ident);
 	}
 #endif
@@ -316,7 +316,7 @@
 	}
 #ifdef KTRACE
 	if (KTRPOINT(td, KTR_CSW))
-		ktrcsw(0, 0);
+		ktrcsw(0, 0, wmesg);
 #endif
 	PICKUP_GIANT();
 	mtx_lock_spin(mtx);
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/kern/kern_thr.c
--- a/head/sys/kern/kern_thr.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/kern/kern_thr.c	Sat Apr 21 21:38:59 2012 +0300
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/kern_thr.c 232700 2012-03-08 19:41:05Z jhb $");
+__FBSDID("$FreeBSD: head/sys/kern/kern_thr.c 234381 2012-04-17 13:44:40Z trasz $");
 
 #include "opt_compat.h"
 #include "opt_posix.h"
@@ -317,13 +317,13 @@
 	rw_wlock(&tidhash_lock);
 
 	PROC_LOCK(p);
-	racct_sub(p, RACCT_NTHR, 1);
 
 	/*
 	 * Shutting down last thread in the proc.  This will actually
 	 * call exit() in the trampoline when it returns.
 	 */
 	if (p->p_numthreads != 1) {
+		racct_sub(p, RACCT_NTHR, 1);
 		LIST_REMOVE(td, td_hash);
 		rw_wunlock(&tidhash_lock);
 		tdsigcleanup(td);
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/kern/subr_acl_nfs4.c
--- a/head/sys/kern/subr_acl_nfs4.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/kern/subr_acl_nfs4.c	Sat Apr 21 21:38:59 2012 +0300
@@ -32,7 +32,7 @@
 
 #ifdef _KERNEL
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/subr_acl_nfs4.c 232936 2012-03-13 20:27:48Z adrian $");
+__FBSDID("$FreeBSD: head/sys/kern/subr_acl_nfs4.c 234385 2012-04-17 14:54:00Z trasz $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -162,6 +162,9 @@
 			return (0);
 	}
 
+	if (access_mask == 0)
+		return (0);
+
 	return (1);
 }
 
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/kern/subr_trap.c
--- a/head/sys/kern/subr_trap.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/kern/subr_trap.c	Sat Apr 21 21:38:59 2012 +0300
@@ -42,7 +42,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/subr_trap.c 233628 2012-03-28 20:58:30Z fabient $");
+__FBSDID("$FreeBSD: head/sys/kern/subr_trap.c 234494 2012-04-20 15:32:36Z jhb $");
 
 #include "opt_capsicum.h"
 #include "opt_hwpmc_hooks.h"
@@ -219,7 +219,7 @@
 	if (flags & TDF_NEEDRESCHED) {
 #ifdef KTRACE
 		if (KTRPOINT(td, KTR_CSW))
-			ktrcsw(1, 1);
+			ktrcsw(1, 1, __func__);
 #endif
 		thread_lock(td);
 		sched_prio(td, td->td_user_pri);
@@ -227,7 +227,7 @@
 		thread_unlock(td);
 #ifdef KTRACE
 		if (KTRPOINT(td, KTR_CSW))
-			ktrcsw(0, 1);
+			ktrcsw(0, 1, __func__);
 #endif
 	}
 
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/kern/vfs_default.c
--- a/head/sys/kern/vfs_default.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/kern/vfs_default.c	Sat Apr 21 21:38:59 2012 +0300
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/vfs_default.c 232317 2012-02-29 21:38:31Z trociny $");
+__FBSDID("$FreeBSD: head/sys/kern/vfs_default.c 234386 2012-04-17 16:28:22Z mckusick $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1114,18 +1114,15 @@
 	/*
 	 * Force stale buffer cache information to be flushed.
 	 */
-	MNT_ILOCK(mp);
 loop:
-	MNT_VNODE_FOREACH(vp, mp, mvp) {
-		/* bv_cnt is an acceptable race here. */
-		if (vp->v_bufobj.bo_dirty.bv_cnt == 0)
+	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
+		if (vp->v_bufobj.bo_dirty.bv_cnt == 0) {
+			VI_UNLOCK(vp);
 			continue;
-		VI_LOCK(vp);
-		MNT_IUNLOCK(mp);
+		}
 		if ((error = vget(vp, lockreq, td)) != 0) {
-			MNT_ILOCK(mp);
 			if (error == ENOENT) {
-				MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
+				MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 				goto loop;
 			}
 			continue;
@@ -1134,9 +1131,7 @@
 		if (error)
 			allerror = error;
 		vput(vp);
-		MNT_ILOCK(mp);
 	}
-	MNT_IUNLOCK(mp);
 	return (allerror);
 }
 
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/kern/vfs_mount.c
--- a/head/sys/kern/vfs_mount.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/kern/vfs_mount.c	Sat Apr 21 21:38:59 2012 +0300
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/vfs_mount.c 233999 2012-04-07 15:27:34Z gleb $");
+__FBSDID("$FreeBSD: head/sys/kern/vfs_mount.c 234482 2012-04-20 06:50:44Z mckusick $");
 
 #include "opt_vfs_allow_nonmpsafe.h"
 
@@ -81,7 +81,6 @@
     "Unprivileged users may mount and unmount file systems");
 
 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
-static MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
 static uma_zone_t mount_zone;
 
 /* List of mounted filesystems. */
@@ -462,6 +461,8 @@
 	    __rangeof(struct mount, mnt_startzero, mnt_endzero));
 	TAILQ_INIT(&mp->mnt_nvnodelist);
 	mp->mnt_nvnodelistsize = 0;
+	TAILQ_INIT(&mp->mnt_activevnodelist);
+	mp->mnt_activevnodelistsize = 0;
 	mp->mnt_ref = 0;
 	(void) vfs_busy(mp, MBF_NOWAIT);
 	mp->mnt_op = vfsp->vfc_vfsops;
@@ -515,6 +516,8 @@
 	}
 	if (mp->mnt_nvnodelistsize != 0)
 		panic("vfs_mount_destroy: nonzero nvnodelistsize");
+	if (mp->mnt_activevnodelistsize != 0)
+		panic("vfs_mount_destroy: nonzero activevnodelistsize");
 	if (mp->mnt_lockref != 0)
 		panic("vfs_mount_destroy: nonzero lock refcount");
 	MNT_IUNLOCK(mp);
@@ -1720,10 +1723,14 @@
 }
 
 /*
- * This is a helper function for filesystems to traverse their
- * vnodes.  See MNT_VNODE_FOREACH() in sys/mount.h
+ * These are helper functions for filesystems to traverse all
+ * their vnodes.  See MNT_VNODE_FOREACH() in sys/mount.h.
+ *
+ * This interface has been deprecated in favor of MNT_VNODE_FOREACH_ALL.
  */
 
+MALLOC_DECLARE(M_VNODE_MARKER);
+
 struct vnode *
 __mnt_vnode_next(struct vnode **mvp, struct mount *mp)
 {
@@ -1812,7 +1819,6 @@
 	MNT_REL(mp);
 }
 
-
 int
 __vfs_statfs(struct mount *mp, struct statfs *sbp)
 {
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/kern/vfs_subr.c
--- a/head/sys/kern/vfs_subr.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/kern/vfs_subr.c	Sat Apr 21 21:38:59 2012 +0300
@@ -39,7 +39,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/vfs_subr.c 234158 2012-04-11 23:01:11Z mckusick $");
+__FBSDID("$FreeBSD: head/sys/kern/vfs_subr.c 234483 2012-04-20 07:00:28Z mckusick $");
 
 #include "opt_ddb.h"
 #include "opt_watchdog.h"
@@ -102,12 +102,10 @@
 		    int slpflag, int slptimeo);
 static void	syncer_shutdown(void *arg, int howto);
 static int	vtryrecycle(struct vnode *vp);
-static void	vbusy(struct vnode *vp);
 static void	v_incr_usecount(struct vnode *);
 static void	v_decr_usecount(struct vnode *);
 static void	v_decr_useonly(struct vnode *);
 static void	v_upgrade_usecount(struct vnode *);
-static void	vfree(struct vnode *);
 static void	vnlru_free(int);
 static void	vgonel(struct vnode *);
 static void	vfs_knllock(void *arg);
@@ -118,8 +116,7 @@
 
 /*
  * Number of vnodes in existence.  Increased whenever getnewvnode()
- * allocates a new vnode, decreased on vdestroy() called on VI_DOOMed
- * vnode.
+ * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode.
  */
 static unsigned long	numvnodes;
 
@@ -778,12 +775,16 @@
 			break;
 		VNASSERT(vp->v_op != NULL, vp,
 		    ("vnlru_free: vnode already reclaimed."));
-		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
+		KASSERT((vp->v_iflag & VI_FREE) != 0,
+		    ("Removing vnode not on freelist"));
+		KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
+		    ("Mangling active vnode"));
+		TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
 		/*
 		 * Don't recycle if we can't get the interlock.
 		 */
 		if (!VI_TRYLOCK(vp)) {
-			TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
+			TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
 			continue;
 		}
 		VNASSERT(VCANRECYCLE(vp), vp,
@@ -878,46 +879,6 @@
  * Routines having to do with the management of the vnode table.
  */
 
-void
-vdestroy(struct vnode *vp)
-{
-	struct bufobj *bo;
-
-	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
-	mtx_lock(&vnode_free_list_mtx);
-	numvnodes--;
-	mtx_unlock(&vnode_free_list_mtx);
-	bo = &vp->v_bufobj;
-	VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
-	    ("cleaned vnode still on the free list."));
-	VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
-	VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
-	VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
-	VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
-	VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
-	VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
-	VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
-	VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
-	VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
-	VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
-	VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
-	VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
-	VI_UNLOCK(vp);
-#ifdef MAC
-	mac_vnode_destroy(vp);
-#endif
-	if (vp->v_pollinfo != NULL)
-		destroy_vpollinfo(vp->v_pollinfo);
-#ifdef INVARIANTS
-	/* XXX Elsewhere we can detect an already freed vnode via NULL v_op. */
-	vp->v_op = NULL;
-#endif
-	lockdestroy(vp->v_vnlock);
-	mtx_destroy(&vp->v_interlock);
-	mtx_destroy(BO_MTX(bo));
-	uma_zfree(vnode_zone, vp);
-}
-
 /*
  * Try to recycle a freed vnode.  We abort if anyone picks up a reference
  * before we actually vgone().  This function must be called with the vnode
@@ -1078,12 +1039,26 @@
 delmntque(struct vnode *vp)
 {
 	struct mount *mp;
+	int active;
 
 	mp = vp->v_mount;
 	if (mp == NULL)
 		return;
 	MNT_ILOCK(mp);
+	VI_LOCK(vp);
+	KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
+	    ("Active vnode list size %d > Vnode list size %d",
+	     mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
+	active = vp->v_iflag & VI_ACTIVE;
+	vp->v_iflag &= ~VI_ACTIVE;
+	if (active) {
+		mtx_lock(&vnode_free_list_mtx);
+		TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
+		mp->mnt_activevnodelistsize--;
+		mtx_unlock(&vnode_free_list_mtx);
+	}
 	vp->v_mount = NULL;
+	VI_UNLOCK(vp);
 	VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
 		("bad mount point vnode list size"));
 	TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
@@ -1123,13 +1098,24 @@
 		ASSERT_VOP_ELOCKED(vp,
 		    "insmntque: mp-safe fs and non-locked vp");
 #endif
+	/*
+	 * We acquire the vnode interlock early to ensure that the
+	 * vnode cannot be recycled by another process releasing a
+	 * holdcnt on it before we get it on both the vnode list
+	 * and the active vnode list. The mount mutex protects only
+	 * manipulation of the vnode list and the vnode freelist
+	 * mutex protects only manipulation of the active vnode list.
+	 * Hence the need to hold the vnode interlock throughout.
+	 */
 	MNT_ILOCK(mp);
+	VI_LOCK(vp);
 	if ((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
 	    ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
 	     mp->mnt_nvnodelistsize == 0)) {
 		locked = VOP_ISLOCKED(vp);
 		if (!locked || (locked == LK_EXCLUSIVE &&
 		     (vp->v_vflag & VV_FORCEINSMQ) == 0)) {
+			VI_UNLOCK(vp);
 			MNT_IUNLOCK(mp);
 			if (dtr != NULL)
 				dtr(vp, dtr_arg);
@@ -1142,6 +1128,14 @@
 	VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
 		("neg mount point vnode list size"));
 	mp->mnt_nvnodelistsize++;
+	KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
+	    ("Activating already active vnode"));
+	vp->v_iflag |= VI_ACTIVE;
+	mtx_lock(&vnode_free_list_mtx);
+	TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
+	mp->mnt_activevnodelistsize++;
+	mtx_unlock(&vnode_free_list_mtx);
+	VI_UNLOCK(vp);
 	MNT_IUNLOCK(mp);
 	return (0);
 }
@@ -2346,19 +2340,41 @@
 	VI_UNLOCK(vp);
 }
 
+/*
+ * Increase the hold count and activate if this is the first reference.
+ */
 void
 vholdl(struct vnode *vp)
 {
+	struct mount *mp;
 
 	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
 	vp->v_holdcnt++;
-	if (VSHOULDBUSY(vp))
-		vbusy(vp);
+	if (!VSHOULDBUSY(vp))
+		return;
+	ASSERT_VI_LOCKED(vp, "vholdl");
+	VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free"));
+	VNASSERT(vp->v_op != NULL, vp, ("vholdl: vnode already reclaimed."));
+	/*
+	 * Remove a vnode from the free list, mark it as in use,
+	 * and put it on the active list.
+	 */
+	mtx_lock(&vnode_free_list_mtx);
+	TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
+	freevnodes--;
+	vp->v_iflag &= ~(VI_FREE|VI_AGE);
+	KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
+	    ("Activating already active vnode"));
+	vp->v_iflag |= VI_ACTIVE;
+	mp = vp->v_mount;
+	TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
+	mp->mnt_activevnodelistsize++;
+	mtx_unlock(&vnode_free_list_mtx);
 }
 
 /*
- * Note that there is one less who cares about this vnode.  vdrop() is the
- * opposite of vhold().
+ * Note that there is one less who cares about this vnode.
+ * vdrop() is the opposite of vhold().
  */
 void
 vdrop(struct vnode *vp)
@@ -2370,28 +2386,93 @@
 
 /*
  * Drop the hold count of the vnode.  If this is the last reference to
- * the vnode we will free it if it has been vgone'd otherwise it is
- * placed on the free list.
+ * the vnode we place it on the free list unless it has been vgone'd
+ * (marked VI_DOOMED) in which case we will free it.
  */
 void
 vdropl(struct vnode *vp)
 {
+	struct bufobj *bo;
+	struct mount *mp;
+	int active;
 
 	ASSERT_VI_LOCKED(vp, "vdropl");
 	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
 	if (vp->v_holdcnt <= 0)
 		panic("vdrop: holdcnt %d", vp->v_holdcnt);
 	vp->v_holdcnt--;
-	if (vp->v_holdcnt == 0) {
-		if (vp->v_iflag & VI_DOOMED) {
-			CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__,
-			    vp);
-			vdestroy(vp);
-			return;
-		} else
-			vfree(vp);
+	if (vp->v_holdcnt > 0) {
+		VI_UNLOCK(vp);
+		return;
 	}
+	if ((vp->v_iflag & VI_DOOMED) == 0) {
+		/*
+		 * Mark a vnode as free: remove it from its active list
+		 * and put it up for recycling on the freelist.
+		 */
+		VNASSERT(vp->v_op != NULL, vp,
+		    ("vdropl: vnode already reclaimed."));
+		VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
+		    ("vnode already free"));
+		VNASSERT(VSHOULDFREE(vp), vp,
+		    ("vdropl: freeing when we shouldn't"));
+		active = vp->v_iflag & VI_ACTIVE;
+		vp->v_iflag &= ~VI_ACTIVE;
+		mp = vp->v_mount;
+		mtx_lock(&vnode_free_list_mtx);
+		if (active) {
+			TAILQ_REMOVE(&mp->mnt_activevnodelist, vp,
+			    v_actfreelist);
+			mp->mnt_activevnodelistsize--;
+		}
+		if (vp->v_iflag & VI_AGE) {
+			TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_actfreelist);
+		} else {
+			TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
+		}
+		freevnodes++;
+		vp->v_iflag &= ~VI_AGE;
+		vp->v_iflag |= VI_FREE;
+		mtx_unlock(&vnode_free_list_mtx);
+		VI_UNLOCK(vp);
+		return;
+	}
+	/*
+	 * The vnode has been marked for destruction, so free it.
+	 */
+	CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
+	mtx_lock(&vnode_free_list_mtx);
+	numvnodes--;
+	mtx_unlock(&vnode_free_list_mtx);
+	bo = &vp->v_bufobj;
+	VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
+	    ("cleaned vnode still on the free list."));
+	VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
+	VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
+	VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
+	VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
+	VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
+	VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
+	VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
+	VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
+	VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
+	VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
+	VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
+	VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
 	VI_UNLOCK(vp);
+#ifdef MAC
+	mac_vnode_destroy(vp);
+#endif
+	if (vp->v_pollinfo != NULL)
+		destroy_vpollinfo(vp->v_pollinfo);
+#ifdef INVARIANTS
+	/* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
+	vp->v_op = NULL;
+#endif
+	lockdestroy(vp->v_vnlock);
+	mtx_destroy(&vp->v_interlock);
+	mtx_destroy(BO_MTX(bo));
+	uma_zfree(vnode_zone, vp);
 }
 
 /*
@@ -2403,6 +2484,7 @@
 void
 vinactive(struct vnode *vp, struct thread *td)
 {
+	struct vm_object *obj;
 
 	ASSERT_VOP_ELOCKED(vp, "vinactive");
 	ASSERT_VI_LOCKED(vp, "vinactive");
@@ -2412,6 +2494,17 @@
 	vp->v_iflag |= VI_DOINGINACT;
 	vp->v_iflag &= ~VI_OWEINACT;
 	VI_UNLOCK(vp);
+	/*
+	 * Before moving off the active list, we must be sure that any
+	 * modified pages are on the vnode's dirty list since these will
+	 * no longer be checked once the vnode is on the inactive list.
+	 */
+	obj = vp->v_object;
+	if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
+		VM_OBJECT_LOCK(obj);
+		vm_object_page_clean(obj, 0, 0, OBJPC_NOSYNC);
+		VM_OBJECT_UNLOCK(obj);
+	}
 	VOP_INACTIVE(vp, td);
 	VI_LOCK(vp);
 	VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
@@ -2467,17 +2560,13 @@
 		}
 		vput(rootvp);
 	}
-	MNT_ILOCK(mp);
 loop:
-	MNT_VNODE_FOREACH(vp, mp, mvp) {
-		VI_LOCK(vp);
+	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
 		vholdl(vp);
-		MNT_IUNLOCK(mp);
 		error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
 		if (error) {
 			vdrop(vp);
-			MNT_ILOCK(mp);
-			MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
+			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 			goto loop;
 		}
 		/*
@@ -2486,7 +2575,6 @@
 		if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
 			VOP_UNLOCK(vp, 0);
 			vdrop(vp);
-			MNT_ILOCK(mp);
 			continue;
 		}
 		/*
@@ -2504,7 +2592,7 @@
 			if (error != 0) {
 				VOP_UNLOCK(vp, 0);
 				vdrop(vp);
-				MNT_VNODE_FOREACH_ABORT(mp, mvp);
+				MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 				return (error);
 			}
 			error = VOP_GETATTR(vp, &vattr, td->td_ucred);
@@ -2515,7 +2603,6 @@
 			    (vp->v_writecount == 0 || vp->v_type != VREG)) {
 				VOP_UNLOCK(vp, 0);
 				vdropl(vp);
-				MNT_ILOCK(mp);
 				continue;
 			}
 		} else
@@ -2540,9 +2627,7 @@
 		}
 		VOP_UNLOCK(vp, 0);
 		vdropl(vp);
-		MNT_ILOCK(mp);
 	}
-	MNT_IUNLOCK(mp);
 	if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
 		/*
 		 * If just the root vnode is busy, and if its refcount
@@ -2993,6 +3078,8 @@
 	db_printf("    mnt_ref = %d\n", mp->mnt_ref);
 	db_printf("    mnt_gen = %d\n", mp->mnt_gen);
 	db_printf("    mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
+	db_printf("    mnt_activevnodelistsize = %d\n",
+	    mp->mnt_activevnodelistsize);
 	db_printf("    mnt_writeopcount = %d\n", mp->mnt_writeopcount);
 	db_printf("    mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
 	db_printf("    mnt_iosize_max = %d\n", mp->mnt_iosize_max);
@@ -3002,10 +3089,18 @@
 	    mp->mnt_secondary_accwrites);
 	db_printf("    mnt_gjprovider = %s\n",
 	    mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
-	db_printf("\n");
-
+
+	db_printf("\n\nList of active vnodes\n");
+	TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
+		if (vp->v_type != VMARKER) {
+			vn_printf(vp, "vnode ");
+			if (db_pager_quit)
+				break;
+		}
+	}
+	db_printf("\n\nList of inactive vnodes\n");
 	TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
-		if (vp->v_type != VMARKER) {
+		if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
 			vn_printf(vp, "vnode ");
 			if (db_pager_quit)
 				break;
@@ -3279,19 +3374,15 @@
 	struct vm_object *obj;
 
 	CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
-	MNT_ILOCK(mp);
-	MNT_VNODE_FOREACH(vp, mp, mvp) {
-		VI_LOCK(vp);
+	MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
 		obj = vp->v_object;
 		if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
 		    (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
-			MNT_IUNLOCK(mp);
 			if (!vget(vp,
 			    LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
 			    curthread)) {
 				if (vp->v_vflag & VV_NOSYNC) {	/* unlinked */
 					vput(vp);
-					MNT_ILOCK(mp);
 					continue;
 				}
 
@@ -3305,55 +3396,9 @@
 				}
 				vput(vp);
 			}
-			MNT_ILOCK(mp);
 		} else
 			VI_UNLOCK(vp);
 	}
-	MNT_IUNLOCK(mp);
-}
-
-/*
- * Mark a vnode as free, putting it up for recycling.
- */
-static void
-vfree(struct vnode *vp)
-{
-
-	ASSERT_VI_LOCKED(vp, "vfree");
-	mtx_lock(&vnode_free_list_mtx);
-	VNASSERT(vp->v_op != NULL, vp, ("vfree: vnode already reclaimed."));
-	VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, ("vnode already free"));
-	VNASSERT(VSHOULDFREE(vp), vp, ("vfree: freeing when we shouldn't"));
-	VNASSERT((vp->v_iflag & VI_DOOMED) == 0, vp,
-	    ("vfree: Freeing doomed vnode"));
-	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
-	if (vp->v_iflag & VI_AGE) {
-		TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
-	} else {
-		TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
-	}
-	freevnodes++;
-	vp->v_iflag &= ~VI_AGE;
-	vp->v_iflag |= VI_FREE;
-	mtx_unlock(&vnode_free_list_mtx);
-}
-
-/*
- * Opposite of vfree() - mark a vnode as in use.
- */
-static void
-vbusy(struct vnode *vp)
-{
-	ASSERT_VI_LOCKED(vp, "vbusy");
-	VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free"));
-	VNASSERT(vp->v_op != NULL, vp, ("vbusy: vnode already reclaimed."));
-	CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
-
-	mtx_lock(&vnode_free_list_mtx);
-	TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
-	freevnodes--;
-	vp->v_iflag &= ~(VI_FREE|VI_AGE);
-	mtx_unlock(&vnode_free_list_mtx);
 }
 
 static void
@@ -4504,3 +4549,187 @@
 
 	return (0);
 }
+
+/*
+ * These are helper functions for filesystems to traverse all
+ * their vnodes.  See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
+ *
+ * This interface replaces MNT_VNODE_FOREACH.
+ */
+
+MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
+
+struct vnode *
+__mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
+{
+	struct vnode *vp;
+
+	if (should_yield())
+		kern_yield(PRI_UNCHANGED);
+	MNT_ILOCK(mp);
+	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
+	vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
+	while (vp != NULL && (vp->v_type == VMARKER ||
+	    (vp->v_iflag & VI_DOOMED) != 0))
+		vp = TAILQ_NEXT(vp, v_nmntvnodes);
+
+	/* Check if we are done */
+	if (vp == NULL) {
+		__mnt_vnode_markerfree_all(mvp, mp);
+		/* MNT_IUNLOCK(mp); -- done in above function */
+		mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
+		return (NULL);
+	}
+	TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
+	TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
+	VI_LOCK(vp);
+	MNT_IUNLOCK(mp);
+	return (vp);
+}
+
+struct vnode *
+__mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
+{
+	struct vnode *vp;
+
+	*mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
+	MNT_ILOCK(mp);
+	MNT_REF(mp);
+	(*mvp)->v_type = VMARKER;
+
+	vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
+	while (vp != NULL && (vp->v_type == VMARKER ||
+	    (vp->v_iflag & VI_DOOMED) != 0))
+		vp = TAILQ_NEXT(vp, v_nmntvnodes);
+
+	/* Check if we are done */
+	if (vp == NULL) {
+		MNT_REL(mp);
+		MNT_IUNLOCK(mp);
+		free(*mvp, M_VNODE_MARKER);
+		*mvp = NULL;
+		return (NULL);
+	}
+	(*mvp)->v_mount = mp;
+	TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
+	VI_LOCK(vp);
+	MNT_IUNLOCK(mp);
+	return (vp);
+}
+
+
+void
+__mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
+{
+
+	if (*mvp == NULL) {
+		MNT_IUNLOCK(mp);
+		return;
+	}
+
+	mtx_assert(MNT_MTX(mp), MA_OWNED);
+
+	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
+	TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
+	MNT_REL(mp);
+	MNT_IUNLOCK(mp);
+	free(*mvp, M_VNODE_MARKER);
+	*mvp = NULL;
+}
+
+/*
+ * These are helper functions for filesystems to traverse their
+ * active vnodes.  See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
+ */
+struct vnode *
+__mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
+{
+	struct vnode *vp, *nvp;
+
+	if (should_yield())
+		kern_yield(PRI_UNCHANGED);
+	MNT_ILOCK(mp);
+	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
+	vp = TAILQ_NEXT(*mvp, v_actfreelist);
+	while (vp != NULL) {
+		VI_LOCK(vp);
+		if (vp->v_mount == mp && vp->v_type != VMARKER &&
+		    (vp->v_iflag & VI_DOOMED) == 0)
+			break;
+		nvp = TAILQ_NEXT(vp, v_actfreelist);
+		VI_UNLOCK(vp);
+		vp = nvp;
+	}
+
+	/* Check if we are done */
+	if (vp == NULL) {
+		__mnt_vnode_markerfree_active(mvp, mp);
+		/* MNT_IUNLOCK(mp); -- done in above function */
+		mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
+		return (NULL);
+	}
+	mtx_lock(&vnode_free_list_mtx);
+	TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
+	TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
+	mtx_unlock(&vnode_free_list_mtx);
+	MNT_IUNLOCK(mp);
+	return (vp);
+}
+
+struct vnode *
+__mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
+{
+	struct vnode *vp, *nvp;
+
+	*mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
+	MNT_ILOCK(mp);
+	MNT_REF(mp);
+	(*mvp)->v_type = VMARKER;
+
+	vp = TAILQ_NEXT(*mvp, v_actfreelist);
+	while (vp != NULL) {
+		VI_LOCK(vp);
+		if (vp->v_mount == mp && vp->v_type != VMARKER &&
+		    (vp->v_iflag & VI_DOOMED) == 0)
+			break;
+		nvp = TAILQ_NEXT(vp, v_actfreelist);
+		VI_UNLOCK(vp);
+		vp = nvp;
+	}
+
+	/* Check if we are done */
+	if (vp == NULL) {
+		MNT_REL(mp);
+		MNT_IUNLOCK(mp);
+		free(*mvp, M_VNODE_MARKER);
+		*mvp = NULL;
+		return (NULL);
+	}
+	(*mvp)->v_mount = mp;
+	mtx_lock(&vnode_free_list_mtx);
+	TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
+	mtx_unlock(&vnode_free_list_mtx);
+	MNT_IUNLOCK(mp);
+	return (vp);
+}
+
+void
+__mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
+{
+
+	if (*mvp == NULL) {
+		MNT_IUNLOCK(mp);
+		return;
+	}
+
+	mtx_assert(MNT_MTX(mp), MA_OWNED);
+
+	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
+	mtx_lock(&vnode_free_list_mtx);
+	TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
+	mtx_unlock(&vnode_free_list_mtx);
+	MNT_REL(mp);
+	MNT_IUNLOCK(mp);
+	free(*mvp, M_VNODE_MARKER);
+	*mvp = NULL;
+}
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/kern/vfs_syscalls.c
--- a/head/sys/kern/vfs_syscalls.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/kern/vfs_syscalls.c	Sat Apr 21 21:38:59 2012 +0300
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/vfs_syscalls.c 232750 2012-03-09 21:31:12Z pho $");
+__FBSDID("$FreeBSD: head/sys/kern/vfs_syscalls.c 234489 2012-04-20 10:08:30Z jh $");
 
 #include "opt_capsicum.h"
 #include "opt_compat.h"
@@ -2744,6 +2744,10 @@
 	struct mount *mp;
 	struct vattr vattr;
 
+	/* We can't support the value matching VNOVAL. */
+	if (flags == VNOVAL)
+		return (EOPNOTSUPP);
+
 	/*
 	 * Prevent non-root users from setting flags on devices.  When
 	 * a device is reused, users can retain ownership of the device
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/mips/atheros/ar71xx_fixup.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/mips/atheros/ar71xx_fixup.c	Sat Apr 21 21:38:59 2012 +0300
@@ -0,0 +1,153 @@
+/*-
+ * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice unmodified, this list of conditions, and the following
+ *    disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/sys/mips/atheros/ar71xx_fixup.c 234485 2012-04-20 08:26:05Z adrian $");
+
+#include "opt_ar71xx.h"
+
+#include <sys/param.h>
+#include <sys/systm.h>
+
+#include <sys/bus.h>
+#include <sys/interrupt.h>
+#include <sys/malloc.h>
+#include <sys/kernel.h>
+#include <sys/module.h>
+#include <sys/rman.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+
+#include <vm/vm.h>
+#include <vm/pmap.h>
+#include <vm/vm_extern.h>
+
+#include <machine/bus.h>
+#include <machine/cpu.h>
+#include <machine/intr_machdep.h>
+#include <machine/pmap.h>
+
+#include <dev/pci/pcivar.h>
+#include <dev/pci/pcireg.h>
+
+#include <dev/pci/pcib_private.h>
+#include "pcib_if.h"
+
+#include <mips/atheros/ar71xxreg.h>
+#include <mips/atheros/ar71xx_pci_bus_space.h>
+
+#include <mips/atheros/ar71xx_cpudef.h>
+
+#include <sys/linker.h>
+#include <sys/firmware.h>
+
+#include <mips/atheros/ar71xx_fixup.h>
+
+/*
+ * Take a copy of the EEPROM contents and squirrel it away in a firmware.
+ * The SPI flash will eventually cease to be memory-mapped, so we need
+ * to take a copy of this before the SPI driver initialises.
+ */
+void
+ar71xx_pci_slot_create_eeprom_firmware(device_t dev, u_int bus, u_int slot,
+    u_int func, long int flash_addr, int size)
+{
+	char buf[64];
+	uint16_t *cal_data = (uint16_t *) MIPS_PHYS_TO_KSEG1(flash_addr);
+	void *eeprom = NULL;
+	const struct firmware *fw = NULL;
+
+	device_printf(dev, "EEPROM firmware: 0x%lx @ %d bytes\n",
+	    flash_addr, size);
+
+	eeprom = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
+	if (! eeprom) {
+		device_printf(dev,
+			    "%s: malloc failed for '%s', aborting EEPROM\n",
+			    __func__, buf);
+			return;
+	}
+
+	memcpy(eeprom, cal_data, size);
+
+	/*
+	 * Generate a flash EEPROM 'firmware' from the given memory
+	 * region.  Since the SPI controller will eventually
+	 * go into port-IO mode instead of memory-mapped IO
+	 * mode, a copy of the EEPROM contents is required.
+	 */
+	snprintf(buf, sizeof(buf), "%s.%d.bus.%d.%d.%d.eeprom_firmware",
+	    device_get_name(dev), device_get_unit(dev), bus, slot, func);
+	fw = firmware_register(buf, eeprom, size, 1, NULL);
+	if (fw == NULL) {
+		device_printf(dev, "%s: firmware_register (%s) failed\n",
+		    __func__, buf);
+		free(eeprom, M_DEVBUF);
+		return;
+	}
+	device_printf(dev, "device EEPROM '%s' registered\n", buf);
+}
+
+#if 0
+static void
+ar71xx_pci_slot_fixup(device_t dev, u_int bus, u_int slot, u_int func)
+{
+	long int flash_addr;
+	char buf[64];
+	int size;
+
+	/*
+	 * Check whether the given slot has a hint to poke.
+	 */
+	if (bootverbose)
+	device_printf(dev, "%s: checking dev %s, %d/%d/%d\n",
+	    __func__, device_get_nameunit(dev), bus, slot, func);
+
+	snprintf(buf, sizeof(buf), "bus.%d.%d.%d.ath_fixup_addr",
+	    bus, slot, func);
+
+	if (resource_long_value(device_get_name(dev), device_get_unit(dev),
+	    buf, &flash_addr) == 0) {
+		snprintf(buf, sizeof(buf), "bus.%d.%d.%d.ath_fixup_size",
+		    bus, slot, func);
+		if (resource_int_value(device_get_name(dev),
+		    device_get_unit(dev), buf, &size) != 0) {
+			device_printf(dev,
+			    "%s: missing hint '%s', aborting EEPROM\n",
+			    __func__, buf);
+			return;
+		}
+
+
+		device_printf(dev, "found EEPROM at 0x%lx on %d.%d.%d\n",
+		    flash_addr, bus, slot, func);
+		ar71xx_pci_fixup(dev, bus, slot, func, flash_addr, size);
+		ar71xx_pci_slot_create_eeprom_firmware(dev, bus, slot, func,
+		    flash_addr, size);
+	}
+}
+#endif /* 0 */
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/mips/atheros/ar71xx_fixup.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/mips/atheros/ar71xx_fixup.h	Sat Apr 21 21:38:59 2012 +0300
@@ -0,0 +1,36 @@
+/*-
+ * Copyright (c) 2012, Adrian Chadd <adrian at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice unmodified, this list of conditions, and the following
+ *    disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: head/sys/mips/atheros/ar71xx_fixup.h 234485 2012-04-20 08:26:05Z adrian $
+ */
+
+#ifndef	__ATHEROS_AR71XX_FIXUP_H__
+#define	__ATHEROS_AR71XX_FIXUP_H__
+
+extern	void ar71xx_pci_slot_create_eeprom_firmware(device_t dev,
+    u_int bus, u_int slot, u_int func, long int flash_addr, int size);
+
+#endif /* __ATHEROS_AR71XX_FIXUP_H__ */
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/mips/atheros/ar71xx_gpio.c
--- a/head/sys/mips/atheros/ar71xx_gpio.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/mips/atheros/ar71xx_gpio.c	Sat Apr 21 21:38:59 2012 +0300
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/mips/atheros/ar71xx_gpio.c 228725 2011-12-20 00:33:56Z adrian $");
+__FBSDID("$FreeBSD: head/sys/mips/atheros/ar71xx_gpio.c 234515 2012-04-20 22:44:00Z adrian $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -329,7 +329,7 @@
 	struct ar71xx_gpio_softc *sc = device_get_softc(dev);
 	int error = 0;
 	int i, j, maxpin;
-	int mask;
+	int mask, pinon;
 	int old = 0;
 
 	KASSERT((device_get_unit(dev) == 0),
@@ -394,6 +394,9 @@
 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
 	    "pinmask", &mask) != 0)
 		mask = 0;
+	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
+	    "pinon", &pinon) != 0)
+		pinon = 0;
 	device_printf(dev, "gpio pinmask=0x%x\n", mask);
 	for (i = 0, j = 0; j < maxpin; j++) {
 		if ((mask & (1 << j)) == 0)
@@ -407,6 +410,11 @@
 		i++;
 	}
 	sc->gpio_npins = i;
+	for (i = 0; i < sc->gpio_npins; i++) {
+		j = sc->gpio_pins[i].gp_pin;
+		if ((pinon & (1 << j)) != 0)
+			ar71xx_gpio_pin_set(dev, j, 1);
+	}
 	device_add_child(dev, "gpiobus", device_get_unit(dev));
 	return (bus_generic_attach(dev));
 }
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/mips/atheros/ar71xx_pci.c
--- a/head/sys/mips/atheros/ar71xx_pci.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/mips/atheros/ar71xx_pci.c	Sat Apr 21 21:38:59 2012 +0300
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/mips/atheros/ar71xx_pci.c 234366 2012-04-17 01:34:49Z adrian $");
+__FBSDID("$FreeBSD: head/sys/mips/atheros/ar71xx_pci.c 234485 2012-04-20 08:26:05Z adrian $");
 
 #include "opt_ar71xx.h"
 
@@ -63,8 +63,7 @@
 #include <mips/atheros/ar71xx_cpudef.h>
 
 #ifdef	AR71XX_ATH_EEPROM
-#include <sys/linker.h>
-#include <sys/firmware.h>
+#include <mips/atheros/ar71xx_fixup.h>
 #endif	/* AR71XX_ATH_EEPROM */
 
 #undef	AR71XX_PCI_DEBUG
@@ -85,8 +84,8 @@
 	struct rman		sc_mem_rman;
 	struct rman		sc_irq_rman;
 
-	struct intr_event	*sc_eventstab[AR71XX_PCI_NIRQS];	
-	mips_intrcnt_t		sc_intr_counter[AR71XX_PCI_NIRQS];	
+	struct intr_event	*sc_eventstab[AR71XX_PCI_NIRQS];
+	mips_intrcnt_t		sc_intr_counter[AR71XX_PCI_NIRQS];
 	struct resource		*sc_irq;
 	void			*sc_ih;
 };
@@ -288,7 +287,7 @@
  */
 static void
 ar71xx_pci_fixup(device_t dev, u_int bus, u_int slot, u_int func,
-    long flash_addr)
+    long flash_addr, int len)
 {
 	uint16_t *cal_data = (uint16_t *) MIPS_PHYS_TO_KSEG1(flash_addr);
 	uint32_t reg, val, bar0;
@@ -328,67 +327,12 @@
 	ar71xx_pci_write_config(dev, bus, slot, func, PCIR_BAR(0), bar0, 4);
 }
 
-/*
- * Take a copy of the EEPROM contents and squirrel it away in a firmware.
- * The SPI flash will eventually cease to be memory-mapped, so we need
- * to take a copy of this before the SPI driver initialises.
- */
-static void
-ar71xx_pci_slot_create_eeprom_firmware(device_t dev, u_int bus, u_int slot,
-    u_int func, long int flash_addr)
-{
-	char buf[64];
-	uint16_t *cal_data = (uint16_t *) MIPS_PHYS_TO_KSEG1(flash_addr);
-	void *eeprom = NULL;
-	const struct firmware *fw = NULL;
-	int len;
-
-	snprintf(buf, sizeof(buf), "bus.%d.%d.%d.ath_fixup_size",
-	    bus, slot, func);
-
-	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
-	    buf, &len) != 0) {
-		device_printf(dev, "%s: missing hint '%s', aborting EEPROM\n",
-		    __func__, buf);
-		return;
-	}
-
-	device_printf(dev, "EEPROM firmware: 0x%lx @ %d bytes\n",
-	    flash_addr, len);
-
-	eeprom = malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
-	if (! eeprom) {
-		device_printf(dev,
-			    "%s: malloc failed for '%s', aborting EEPROM\n",
-			    __func__, buf);
-			return;
-	}
-
-	memcpy(eeprom, cal_data, len);
-
-	/*
-	 * Generate a flash EEPROM 'firmware' from the given memory
-	 * region.  Since the SPI controller will eventually
-	 * go into port-IO mode instead of memory-mapped IO
-	 * mode, a copy of the EEPROM contents is required.
-	 */
-	snprintf(buf, sizeof(buf), "%s.%d.bus.%d.%d.%d.eeprom_firmware",
-	    device_get_name(dev), device_get_unit(dev), bus, slot, func);
-	fw = firmware_register(buf, eeprom, len, 1, NULL);
-	if (fw == NULL) {
-		device_printf(dev, "%s: firmware_register (%s) failed\n",
-		    __func__, buf);
-		free(eeprom, M_DEVBUF);
-		return;
-	}
-	device_printf(dev, "device EEPROM '%s' registered\n", buf);
-}
-
 static void
 ar71xx_pci_slot_fixup(device_t dev, u_int bus, u_int slot, u_int func)
 {
 	long int flash_addr;
-	char buf[32];
+	char buf[64];
+	int size;
 
 	/*
 	 * Check whether the given slot has a hint to poke.
@@ -396,16 +340,28 @@
 	if (bootverbose)
 	device_printf(dev, "%s: checking dev %s, %d/%d/%d\n",
 	    __func__, device_get_nameunit(dev), bus, slot, func);
+
 	snprintf(buf, sizeof(buf), "bus.%d.%d.%d.ath_fixup_addr",
 	    bus, slot, func);
 
 	if (resource_long_value(device_get_name(dev), device_get_unit(dev),
 	    buf, &flash_addr) == 0) {
+		snprintf(buf, sizeof(buf), "bus.%d.%d.%d.ath_fixup_size",
+		    bus, slot, func);
+		if (resource_int_value(device_get_name(dev),
+		    device_get_unit(dev), buf, &size) != 0) {
+			device_printf(dev,
+			    "%s: missing hint '%s', aborting EEPROM\n",
+			    __func__, buf);
+			return;
+		}
+
+
 		device_printf(dev, "found EEPROM at 0x%lx on %d.%d.%d\n",
 		    flash_addr, bus, slot, func);
-		ar71xx_pci_fixup(dev, bus, slot, func, flash_addr);
+		ar71xx_pci_fixup(dev, bus, slot, func, flash_addr, size);
 		ar71xx_pci_slot_create_eeprom_firmware(dev, bus, slot, func,
-		    flash_addr);
+		    flash_addr, size);
 	}
 }
 #endif	/* AR71XX_ATH_EEPROM */
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/mips/atheros/ar724x_pci.c
--- a/head/sys/mips/atheros/ar724x_pci.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/mips/atheros/ar724x_pci.c	Sat Apr 21 21:38:59 2012 +0300
@@ -27,7 +27,9 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/mips/atheros/ar724x_pci.c 229765 2012-01-07 04:13:25Z adrian $");
+__FBSDID("$FreeBSD: head/sys/mips/atheros/ar724x_pci.c 234485 2012-04-20 08:26:05Z adrian $");
+
+#include "opt_ar71xx.h"
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -61,6 +63,10 @@
 
 #include <mips/atheros/ar71xx_cpudef.h>
 
+#ifdef	AR71XX_ATH_EEPROM
+#include <mips/atheros/ar71xx_fixup.h>
+#endif	/* AR71XX_ATH_EEPROM */
+
 #undef	AR724X_PCI_DEBUG
 #ifdef AR724X_PCI_DEBUG
 #define dprintf printf
@@ -243,27 +249,21 @@
 	return (0);
 }
 
+#ifdef	AR71XX_ATH_EEPROM
 #define	AR5416_EEPROM_MAGIC		0xa55a
 
 /*
  * XXX - This should not be here ! And this looks like Atheros (if_ath) only.
  */
 static void
-ar724x_load_eeprom_data(device_t dev)
+ar724x_pci_fixup(device_t dev, long flash_addr, int len)
 {
-	uint32_t	bar0, hint, reg, val;
-	uint16_t	*data = NULL;
+	uint32_t bar0, reg, val;
+	uint16_t *cal_data = (uint16_t *) MIPS_PHYS_TO_KSEG1(flash_addr);
 
-	/* Search for a hint of eeprom data offset */
-	if (resource_int_value(device_get_name(dev),
-	    device_get_unit(dev), "eepromdata", &hint) != 0)
-		return;
-
-	device_printf(dev, "Loading the eeprom fixup data from %#x\n", hint);
-	data = (uint16_t *)MIPS_PHYS_TO_KSEG1(hint);
-
-	if (*data != AR5416_EEPROM_MAGIC) {
-		device_printf(dev, "Invalid calibration data from %#x\n", hint);
+	if (cal_data[0] != AR5416_EEPROM_MAGIC) {
+		device_printf(dev, "%s: Invalid calibration data from 0x%x\n",
+		    __func__, (uintptr_t) flash_addr);
 		return;
 	}
 
@@ -272,19 +272,24 @@
 	/* XXX Access to PCI internal regs AR7240 - 0xffff, 7241,7242 - 0x1000ffff  */
 	ar724x_pci_write_config(dev, 0, 0, 0, PCIR_BAR(0), 0x1000ffff, 4);
 
+	printf("bar0=%08x, new=%08x\n", bar0, 0x1000ffff);
+
 	val = ar724x_pci_read_config(dev, 0, 0, 0, PCIR_COMMAND, 2);
 	val |= (PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN);
 	ar724x_pci_write_config(dev, 0, 0, 0, PCIR_COMMAND, val, 2);
 
 	/* set pointer to first reg address */
-	data += 3;
-	while (*data != 0xffff) {
-		reg = *data++;
-		val = *data++;
-		val |= (*data++) << 16;
+	cal_data += 3;
+	while (*cal_data != 0xffff) {
+		reg = *cal_data++;
+		val = *cal_data++;
+		val |= (*cal_data++) << 16;
+
+		if (bootverbose)
+			printf("    0x%08x=0x%04x\n", reg, val);
 
 		/* Write eeprom fixup data to device memory */
-		printf("\t%08x: %08x\n", reg, val);
+		printf("\t%08x: %08x (Old: %08x)\n", reg, val, ATH_READ_REG(AR71XX_PCI_MEM_BASE + reg));
 		ATH_WRITE_REG(AR71XX_PCI_MEM_BASE + reg, val);
 //		printf("\t%08x: %08x\n", reg, ATH_READ_REG(AR71XX_PCI_MEM_BASE + reg));
 		DELAY(100);
@@ -297,8 +302,50 @@
 	/* Write the saved bar(0) address */
 	ar724x_pci_write_config(dev, 0, 0, 0, PCIR_BAR(0), bar0, 4);
 }
+#undef	AR5416_EEPROM_MAGIC
 
-#undef	AR5416_EEPROM_MAGIC
+/*
+ * XXX This is (mostly) duplicated with ar71xx_pci.c.
+ * It should at some point be fixed.
+ */
+static void
+ar724x_pci_slot_fixup(device_t dev)
+{
+	long int flash_addr;
+	char buf[64];
+	int size;
+
+	/*
+	 * Check whether the given slot has a hint to poke.
+	 */
+	if (bootverbose)
+	device_printf(dev, "%s: checking dev %s, %d/%d/%d\n",
+	    __func__, device_get_nameunit(dev), 0, 0, 0);
+
+	snprintf(buf, sizeof(buf), "bus.%d.%d.%d.ath_fixup_addr",
+	    0, 0, 0);
+
+	if (resource_long_value(device_get_name(dev), device_get_unit(dev),
+	    buf, &flash_addr) == 0) {
+		snprintf(buf, sizeof(buf), "bus.%d.%d.%d.ath_fixup_size",
+		    0, 0, 0);
+		if (resource_int_value(device_get_name(dev),
+		    device_get_unit(dev), buf, &size) != 0) {
+			device_printf(dev,
+			    "%s: missing hint '%s', aborting EEPROM\n",
+			    __func__, buf);
+			return;
+		}
+
+
+		device_printf(dev, "found EEPROM at 0x%lx on %d.%d.%d\n",
+		    flash_addr, 0, 0, 0);
+		ar724x_pci_fixup(dev, flash_addr, size);
+		ar71xx_pci_slot_create_eeprom_firmware(dev, 0, 0, 0,
+		    flash_addr, size);
+	}
+}
+#endif	/* AR71XX_ATH_EEPROM */
 
 static int
 ar724x_pci_probe(device_t dev)
@@ -361,8 +408,9 @@
 	if (ar724x_pci_setup(dev))
 		return (ENXIO);
 
-	/* XXX - Load eeprom fixup data */
-	ar724x_load_eeprom_data(dev);
+#ifdef	AR71XX_ATH_EEPROM
+	ar724x_pci_slot_fixup(dev);
+#endif	/* AR71XX_ATH_EEPROM */
 
 	/* Fixup internal PCI bridge */
 	ar724x_pci_write_config(dev, 0, 0, 0, PCIR_COMMAND,
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/mips/atheros/files.ar71xx
--- a/head/sys/mips/atheros/files.ar71xx	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/mips/atheros/files.ar71xx	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-# $FreeBSD: head/sys/mips/atheros/files.ar71xx 233319 2012-03-22 18:01:23Z gonzo $
+# $FreeBSD: head/sys/mips/atheros/files.ar71xx 234485 2012-04-20 08:26:05Z adrian $
 
 mips/atheros/apb.c		standard
 mips/atheros/ar71xx_gpio.c	optional gpio
@@ -22,5 +22,6 @@
 mips/atheros/ar71xx_chip.c	standard
 mips/atheros/ar724x_chip.c	standard
 mips/atheros/ar91xx_chip.c	standard
+mips/atheros/ar71xx_fixup.c	optional ar71xx_ath_eeprom
 
 dev/hwpmc/hwpmc_mips24k.c	optional hwpmc
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/mips/mips/nexus.c
--- a/head/sys/mips/mips/nexus.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/mips/mips/nexus.c	Sat Apr 21 21:38:59 2012 +0300
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/mips/mips/nexus.c 232768 2012-03-10 06:43:41Z jmallett $");
+__FBSDID("$FreeBSD: head/sys/mips/mips/nexus.c 234525 2012-04-21 04:17:30Z adrian $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -237,11 +237,14 @@
 	device_t child;
 	long	maddr;
 	int	msize;
+	int	order;
 	int	result;
 	int	irq;
 	int	mem_hints_count;
 
-	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
+	if ((resource_int_value(dname, dunit, "order", &order)) != 0)
+		order = 1000;
+	child = BUS_ADD_CHILD(bus, order, dname, dunit);
 	if (child == NULL)
 		return;
 
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/modules/geom/geom_raid/Makefile
--- a/head/sys/modules/geom/geom_raid/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/modules/geom/geom_raid/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-# $FreeBSD$
+# $FreeBSD: head/sys/modules/geom/geom_raid/Makefile 234458 2012-04-19 12:30:12Z mav $
 
 .PATH: ${.CURDIR}/../../../geom/raid
 
@@ -11,7 +11,7 @@
 
 SRCS+=	md_intel.c md_jmicron.c md_nvidia.c md_promise.c md_sii.c
 
-SRCS+=	tr_concat.c tr_raid0.c tr_raid1.c tr_raid1e.c
+SRCS+=	tr_concat.c tr_raid0.c tr_raid1.c tr_raid1e.c tr_raid5.c
 
 MFILES=	kern/bus_if.m kern/device_if.m
 MFILES+= geom/raid/g_raid_md_if.m geom/raid/g_raid_tr_if.m
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/modules/iscsi/initiator/Makefile
--- a/head/sys/modules/iscsi/initiator/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/modules/iscsi/initiator/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,16 +1,14 @@
-# $FreeBSD: head/sys/modules/iscsi/initiator/Makefile 234284 2012-04-14 13:40:39Z bz $
+# $FreeBSD: head/sys/modules/iscsi/initiator/Makefile 234481 2012-04-20 04:40:39Z delphij $
 
-S=      ${.CURDIR}/../../..
-.PATH: $S/dev/iscsi/initiator
-
+.PATH: ${.CURDIR}/../../../dev/iscsi/initiator
 KMOD=iscsi_initiator
 
 SRCS=  iscsi.h iscsivar.h
 SRCS+= iscsi.c isc_cam.c isc_soc.c isc_sm.c isc_subr.c iscsi_subr.c
 SRCS+= opt_cam.h opt_iscsi_initiator.h
 SRCS+= bus_if.h device_if.h
-#CFLAGS+= -DNO_USE_MBUF
-#CFLAGS+= -DISCSI_INITIATOR_DEBUG=2
-CFLAGS+= -I$S
-CFLAGS+= -DINVARIANTS
+
+# Debugging
+# CFLAGS+= -DISCSI_INITIATOR_DEBUG=9
+
 .include <bsd.kmod.mk>
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/modules/mmcsd/Makefile
--- a/head/sys/modules/mmcsd/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/modules/mmcsd/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,8 +1,8 @@
-# $FreeBSD$
+# $FreeBSD: head/sys/modules/mmcsd/Makefile 234524 2012-04-21 01:51:16Z marius $
 
 .PATH: ${.CURDIR}/../../dev/mmc
 
 KMOD=	mmcsd
-SRCS=	mmcsd.c mmcbus_if.h device_if.h bus_if.h
+SRCS=	bus_if.h device_if.h mmcbr_if.h mmcbus_if.h mmcsd.c
 
 .include <bsd.kmod.mk>
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/modules/uart/Makefile
--- a/head/sys/modules/uart/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/modules/uart/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-# $FreeBSD: head/sys/modules/uart/Makefile 221869 2011-05-14 01:53:38Z attilio $
+# $FreeBSD: head/sys/modules/uart/Makefile 234427 2012-04-18 17:44:05Z marcel $
 
 .PATH: ${.CURDIR}/../../dev/uart
 
@@ -7,16 +7,23 @@
 ofw_bus_if=	ofw_bus_if.h
 .endif
 
+.if ${MACHINE} == "i386" || ${MACHINE} == "amd64"
+_uart_cpu=uart_cpu_x86.c
+.else
+_uart_cpu=uart_cpu_${MACHINE}.c
+.endif
+.if exists(${.CURDIR:H:H}/dev/uart/${_uart_cpu})
+uart_cpu_machine= ${_uart_cpu}
+.endif
+
 KMOD=	uart
 SRCS=	uart_bus_acpi.c ${uart_bus_ebus} uart_bus_isa.c uart_bus_pccard.c \
 	uart_bus_pci.c uart_bus_puc.c uart_bus_scc.c \
-	uart_core.c uart_dbg.c \
+	uart_core.c ${uart_cpu_machine} uart_dbg.c \
 	uart_dev_ns8250.c uart_dev_quicc.c uart_dev_sab82532.c \
 	uart_dev_z8530.c \
 	uart_if.c uart_if.h uart_subr.c uart_tty.c
-.if exists(${.CURDIR}/../../dev/uart/uart_cpu_${MACHINE}.c)
-SRCS+=	uart_cpu_${MACHINE}.c
-.endif
+
 SRCS+=	bus_if.h card_if.h device_if.h isa_if.h ${ofw_bus_if} pci_if.h \
 	power_if.h pccarddevs.h serdev_if.h
 
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/net/bridgestp.c
--- a/head/sys/net/bridgestp.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/net/bridgestp.c	Sat Apr 21 21:38:59 2012 +0300
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/net/bridgestp.c 232118 2012-02-24 17:50:36Z thompsa $");
+__FBSDID("$FreeBSD: head/sys/net/bridgestp.c 234488 2012-04-20 10:06:28Z thompsa $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -127,7 +127,7 @@
 static uint32_t	bstp_calc_path_cost(struct bstp_port *);
 static void	bstp_notify_state(void *, int);
 static void	bstp_notify_rtage(void *, int);
-static void	bstp_ifupdstatus(struct bstp_state *, struct bstp_port *);
+static void	bstp_ifupdstatus(void *, int);
 static void	bstp_enable_port(struct bstp_state *, struct bstp_port *);
 static void	bstp_disable_port(struct bstp_state *, struct bstp_port *);
 static void	bstp_tick(void *);
@@ -1677,7 +1677,7 @@
 	if (set) {
 		bp->bp_flags |= BSTP_PORT_AUTOPTP;
 		if (bp->bp_role != BSTP_ROLE_DISABLED)
-			bstp_ifupdstatus(bs, bp);
+			taskqueue_enqueue(taskqueue_swi, &bp->bp_mediatask);
 	} else
 		bp->bp_flags &= ~BSTP_PORT_AUTOPTP;
 	BSTP_UNLOCK(bs);
@@ -1767,85 +1767,93 @@
 }
 
 void
-bstp_linkstate(struct ifnet *ifp, int state)
+bstp_linkstate(struct bstp_port *bp)
 {
-	struct bstp_state *bs;
-	struct bstp_port *bp;
+	struct bstp_state *bs = bp->bp_bs;
 
-	/* search for the stp port */
-	mtx_lock(&bstp_list_mtx);
-	LIST_FOREACH(bs, &bstp_list, bs_list) {
-		BSTP_LOCK(bs);
-		LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
-			if (bp->bp_ifp == ifp) {
-				bstp_ifupdstatus(bs, bp);
-				bstp_update_state(bs, bp);
-				/* it only exists once so return */
-				BSTP_UNLOCK(bs);
-				mtx_unlock(&bstp_list_mtx);
-				return;
-			}
-		}
-		BSTP_UNLOCK(bs);
-	}
-	mtx_unlock(&bstp_list_mtx);
+	if (!bp->bp_active)
+		return;
+
+	bstp_ifupdstatus(bp, 0);
+	BSTP_LOCK(bs);
+	bstp_update_state(bs, bp);
+	BSTP_UNLOCK(bs);
 }
 
 static void
-bstp_ifupdstatus(struct bstp_state *bs, struct bstp_port *bp)
+bstp_ifupdstatus(void *arg, int pending)
 {
+	struct bstp_port *bp = (struct bstp_port *)arg;
+	struct bstp_state *bs = bp->bp_bs;
 	struct ifnet *ifp = bp->bp_ifp;
 	struct ifmediareq ifmr;
-	int error = 0;
+	int error, changed;
 
-	BSTP_LOCK_ASSERT(bs);
+	if (!bp->bp_active)
+		return;
 
 	bzero((char *)&ifmr, sizeof(ifmr));
 	error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
 
+	BSTP_LOCK(bs);
+	changed = 0;
 	if ((error == 0) && (ifp->if_flags & IFF_UP)) {
 		if (ifmr.ifm_status & IFM_ACTIVE) {
 			/* A full-duplex link is assumed to be point to point */
 			if (bp->bp_flags & BSTP_PORT_AUTOPTP) {
-				bp->bp_ptp_link =
-				    ifmr.ifm_active & IFM_FDX ? 1 : 0;
+				int fdx;
+
+				fdx = ifmr.ifm_active & IFM_FDX ? 1 : 0;
+				if (bp->bp_ptp_link ^ fdx) {
+					bp->bp_ptp_link = fdx;
+					changed = 1;
+				}
 			}
 
 			/* Calc the cost if the link was down previously */
 			if (bp->bp_flags & BSTP_PORT_PNDCOST) {
-				bp->bp_path_cost = bstp_calc_path_cost(bp);
+				uint32_t cost;
+
+				cost = bstp_calc_path_cost(bp);
+				if (bp->bp_path_cost != cost) {
+					bp->bp_path_cost = cost;
+					changed = 1;
+				}
 				bp->bp_flags &= ~BSTP_PORT_PNDCOST;
 			}
 
-			if (bp->bp_role == BSTP_ROLE_DISABLED)
+			if (bp->bp_role == BSTP_ROLE_DISABLED) {
 				bstp_enable_port(bs, bp);
+				changed = 1;
+			}
 		} else {
 			if (bp->bp_role != BSTP_ROLE_DISABLED) {
 				bstp_disable_port(bs, bp);
+				changed = 1;
 				if ((bp->bp_flags & BSTP_PORT_ADMEDGE) &&
 				    bp->bp_protover == BSTP_PROTO_RSTP)
 					bp->bp_operedge = 1;
 			}
 		}
-		return;
+	} else if (bp->bp_infois != BSTP_INFO_DISABLED) {
+		bstp_disable_port(bs, bp);
+		changed = 1;
 	}
-
-	if (bp->bp_infois != BSTP_INFO_DISABLED)
-		bstp_disable_port(bs, bp);
+	if (changed)
+		bstp_assign_roles(bs);
+	BSTP_UNLOCK(bs);
 }
 
 static void
 bstp_enable_port(struct bstp_state *bs, struct bstp_port *bp)
 {
 	bp->bp_infois = BSTP_INFO_AGED;
-	bstp_assign_roles(bs);
 }
 
 static void
 bstp_disable_port(struct bstp_state *bs, struct bstp_port *bp)
 {
 	bp->bp_infois = BSTP_INFO_DISABLED;
-	bstp_assign_roles(bs);
 }
 
 static void
@@ -1865,7 +1873,7 @@
 	if (bstp_timer_dectest(&bs->bs_link_timer)) {
 		LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
 			if (!(bp->bp_ifp->if_capabilities & IFCAP_LINKSTATE))
-				bstp_ifupdstatus(bs, bp);
+				taskqueue_enqueue(taskqueue_swi, &bp->bp_mediatask);
 		}
 		bstp_timer_start(&bs->bs_link_timer, BSTP_LINK_TIMER);
 	}
@@ -2075,7 +2083,7 @@
 	LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
 		bp->bp_port_id = (bp->bp_priority << 8) |
 		    (bp->bp_ifp->if_index  & 0xfff);
-		bstp_ifupdstatus(bs, bp);
+		taskqueue_enqueue(taskqueue_swi, &bp->bp_mediatask);
 	}
 
 	bstp_assign_roles(bs);
@@ -2103,10 +2111,8 @@
 	case MOD_LOAD:
 		mtx_init(&bstp_list_mtx, "bridgestp list", NULL, MTX_DEF);
 		LIST_INIT(&bstp_list);
-		bstp_linkstate_p = bstp_linkstate;
 		break;
 	case MOD_UNLOAD:
-		bstp_linkstate_p = NULL;
 		mtx_destroy(&bstp_list_mtx);
 		break;
 	default:
@@ -2198,6 +2204,7 @@
 	bp->bp_priority = BSTP_DEFAULT_PORT_PRIORITY;
 	TASK_INIT(&bp->bp_statetask, 0, bstp_notify_state, bp);
 	TASK_INIT(&bp->bp_rtagetask, 0, bstp_notify_rtage, bp);
+	TASK_INIT(&bp->bp_mediatask, 0, bstp_ifupdstatus, bp);
 
 	/* Init state */
 	bp->bp_infois = BSTP_INFO_DISABLED;
@@ -2261,4 +2268,5 @@
 	KASSERT(bp->bp_active == 0, ("port is still attached"));
 	taskqueue_drain(taskqueue_swi, &bp->bp_statetask);
 	taskqueue_drain(taskqueue_swi, &bp->bp_rtagetask);
+	taskqueue_drain(taskqueue_swi, &bp->bp_mediatask);
 }
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/net/bridgestp.h
--- a/head/sys/net/bridgestp.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/net/bridgestp.h	Sat Apr 21 21:38:59 2012 +0300
@@ -67,7 +67,7 @@
  *
  * OpenBSD: if_bridge.h,v 1.14 2001/03/22 03:48:29 jason Exp
  *
- * $FreeBSD: head/sys/net/bridgestp.h 232014 2012-02-23 00:59:21Z thompsa $
+ * $FreeBSD: head/sys/net/bridgestp.h 234488 2012-04-20 10:06:28Z thompsa $
  */
 
 /*
@@ -326,6 +326,7 @@
 	uint8_t			bp_txcount;
 	struct task		bp_statetask;
 	struct task		bp_rtagetask;
+	struct task		bp_mediatask;
 };
 
 /*
@@ -369,8 +370,6 @@
 
 extern const uint8_t bstp_etheraddr[];
 
-extern	void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
-
 void	bstp_attach(struct bstp_state *, struct bstp_cb_ops *);
 void	bstp_detach(struct bstp_state *);
 void	bstp_init(struct bstp_state *);
@@ -379,7 +378,7 @@
 int	bstp_enable(struct bstp_port *);
 void	bstp_disable(struct bstp_port *);
 void	bstp_destroy(struct bstp_port *);
-void	bstp_linkstate(struct ifnet *, int);
+void	bstp_linkstate(struct bstp_port *);
 int	bstp_set_htime(struct bstp_state *, int);
 int	bstp_set_fdelay(struct bstp_state *, int);
 int	bstp_set_maxage(struct bstp_state *, int);
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/net/if.c
--- a/head/sys/net/if.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/net/if.c	Sat Apr 21 21:38:59 2012 +0300
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)if.c	8.5 (Berkeley) 1/9/95
- * $FreeBSD: head/sys/net/if.c 231229 2012-02-08 22:05:26Z pluknet $
+ * $FreeBSD: head/sys/net/if.c 234487 2012-04-20 09:55:50Z thompsa $
  */
 
 #include "opt_compat.h"
@@ -124,7 +124,7 @@
 static struct sx ifdescr_sx;
 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
 
-void	(*bstp_linkstate_p)(struct ifnet *ifp, int state);
+void	(*bridge_linkstate_p)(struct ifnet *ifp);
 void	(*ng_ether_link_state_p)(struct ifnet *ifp, int state);
 void	(*lagg_linkstate_p)(struct ifnet *ifp, int state);
 /* These are external hooks for CARP. */
@@ -1909,14 +1909,10 @@
 		(*ng_ether_link_state_p)(ifp, link_state);
 	if (ifp->if_carp)
 		(*carp_linkstate_p)(ifp);
-	if (ifp->if_bridge) {
-		KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
-		(*bstp_linkstate_p)(ifp, link_state);
-	}
-	if (ifp->if_lagg) {
-		KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!"));
+	if (ifp->if_bridge)
+		(*bridge_linkstate_p)(ifp);
+	if (ifp->if_lagg)
 		(*lagg_linkstate_p)(ifp, link_state);
-	}
 
 	if (IS_DEFAULT_VNET(curvnet))
 		devctl_notify("IFNET", ifp->if_xname,
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/net/if_bridge.c
--- a/head/sys/net/if_bridge.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/net/if_bridge.c	Sat Apr 21 21:38:59 2012 +0300
@@ -75,7 +75,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/net/if_bridge.c 232315 2012-02-29 20:58:21Z thompsa $");
+__FBSDID("$FreeBSD: head/sys/net/if_bridge.c 234487 2012-04-20 09:55:50Z thompsa $");
 
 #include "opt_inet.h"
 #include "opt_inet6.h"
@@ -333,6 +333,9 @@
 #endif /* INET6 */
 static int	bridge_fragment(struct ifnet *, struct mbuf *,
 		    struct ether_header *, int, struct llc *);
+static void	bridge_linkstate(struct ifnet *ifp);
+
+extern void (*bridge_linkstate_p)(struct ifnet *ifp);
 
 /* The default bridge vlan is 1 (IEEE 802.1Q-2003 Table 9-2) */
 #define	VLANTAGOF(_m)	\
@@ -496,6 +499,7 @@
 		bridge_input_p = bridge_input;
 		bridge_output_p = bridge_output;
 		bridge_dn_p = bridge_dummynet;
+		bridge_linkstate_p = bridge_linkstate;
 		bridge_detach_cookie = EVENTHANDLER_REGISTER(
 		    ifnet_departure_event, bridge_ifdetach, NULL,
 		    EVENTHANDLER_PRI_ANY);
@@ -508,6 +512,7 @@
 		bridge_input_p = NULL;
 		bridge_output_p = NULL;
 		bridge_dn_p = NULL;
+		bridge_linkstate_p = NULL;
 		mtx_destroy(&bridge_list_mtx);
 		break;
 	default:
@@ -988,6 +993,7 @@
 		bridge_set_ifcap(sc, bif, bif->bif_savedcaps);
 	}
 	bstp_destroy(&bif->bif_stp);	/* prepare to free */
+	bridge_linkstate(ifs);
 	BRIDGE_LOCK(sc);
 	free(bif, M_DEVBUF);
 }
@@ -1087,17 +1093,17 @@
 	/* Set interface capabilities to the intersection set of all members */
 	bridge_mutecaps(sc);
 
+	BRIDGE_UNLOCK(sc);
+	/* Update the linkstate for the bridge */
+	bridge_linkstate(ifs);
+	/* Place the interface into promiscuous mode */
 	switch (ifs->if_type) {
-	case IFT_ETHER:
-	case IFT_L2VLAN:
-		/*
-		 * Place the interface into promiscuous mode.
-		 */
-		BRIDGE_UNLOCK(sc);
-		error = ifpromisc(ifs, 1);
-		BRIDGE_LOCK(sc);
-		break;
+		case IFT_ETHER:
+		case IFT_L2VLAN:
+			error = ifpromisc(ifs, 1);
+			break;
 	}
+	BRIDGE_LOCK(sc);
 	if (error)
 		bridge_delete_member(sc, bif, 0);
 out:
@@ -3475,3 +3481,38 @@
 		m_freem(m);
 	return (error);
 }
+
+static void
+bridge_linkstate(struct ifnet *ifp)
+{
+	struct bridge_softc *sc = ifp->if_bridge;
+	struct bridge_iflist *bif, *bif2;
+	int new_link, hasls;
+
+	BRIDGE_LOCK(sc);
+	bif = bridge_lookup_member_if(sc, ifp);
+	if (bif == NULL) {
+		BRIDGE_UNLOCK(sc);
+		return;
+	}
+	new_link = LINK_STATE_DOWN;
+	hasls = 0;
+	/* Our link is considered up if at least one of our ports is active */
+	LIST_FOREACH(bif2, &sc->sc_iflist, bif_next) {
+		if (bif2->bif_ifp->if_capabilities & IFCAP_LINKSTATE)
+			hasls++;
+		if (bif2->bif_ifp->if_link_state == LINK_STATE_UP) {
+			new_link = LINK_STATE_UP;
+			break;
+		}
+	}
+	if (!LIST_EMPTY(&sc->sc_iflist) && !hasls) {
+		/* If no interfaces support link-state then we default to up */
+		new_link = LINK_STATE_UP;
+	}
+	if_link_state_change(sc->sc_ifp, new_link);
+	BRIDGE_UNLOCK(sc);
+
+	bstp_linkstate(&bif->bif_stp);
+}
+
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/netinet/sctp_indata.c
--- a/head/sys/netinet/sctp_indata.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/netinet/sctp_indata.c	Sat Apr 21 21:38:59 2012 +0300
@@ -33,7 +33,7 @@
 /* $KAME: sctp_indata.c,v 1.36 2005/03/06 16:04:17 itojun Exp $	 */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/sctp_indata.c 228907 2011-12-27 10:16:24Z tuexen $");
+__FBSDID("$FreeBSD: head/sys/netinet/sctp_indata.c 234459 2012-04-19 12:43:19Z tuexen $");
 
 #include <netinet/sctp_os.h>
 #include <netinet/sctp_var.h>
@@ -2746,11 +2746,13 @@
 						phd->param_length =
 						    htons(chk_length + sizeof(*phd));
 						SCTP_BUF_LEN(merr) = sizeof(*phd);
-						SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset,
-						    SCTP_SIZE32(chk_length),
-						    M_DONTWAIT);
+						SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset, chk_length, M_DONTWAIT);
 						if (SCTP_BUF_NEXT(merr)) {
-							sctp_queue_op_err(stcb, merr);
+							if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(merr), SCTP_SIZE32(chk_length) - chk_length, NULL)) {
+								sctp_m_freem(merr);
+							} else {
+								sctp_queue_op_err(stcb, merr);
+							}
 						} else {
 							sctp_m_freem(merr);
 						}
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/netinet/sctp_input.c
--- a/head/sys/netinet/sctp_input.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/netinet/sctp_input.c	Sat Apr 21 21:38:59 2012 +0300
@@ -33,7 +33,7 @@
 /* $KAME: sctp_input.c,v 1.27 2005/03/06 16:04:17 itojun Exp $	 */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/sctp_input.c 233660 2012-03-29 13:36:53Z rrs $");
+__FBSDID("$FreeBSD: head/sys/netinet/sctp_input.c 234461 2012-04-19 13:11:17Z tuexen $");
 
 #include <netinet/sctp_os.h>
 #include <netinet/sctp_var.h>
@@ -2463,12 +2463,10 @@
 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 		struct mbuf *mat;
 
-		mat = m_sig;
-		while (mat) {
+		for (mat = m_sig; mat; mat = SCTP_BUF_NEXT(mat)) {
 			if (SCTP_BUF_IS_EXTENDED(mat)) {
 				sctp_log_mb(mat, SCTP_MBUF_SPLIT);
 			}
-			mat = SCTP_BUF_NEXT(mat);
 		}
 	}
 #endif
@@ -5461,23 +5459,24 @@
 					phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
 					phd->param_length = htons(chk_length + sizeof(*phd));
 					SCTP_BUF_LEN(mm) = sizeof(*phd);
-					SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length),
-					    M_DONTWAIT);
+					SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, chk_length, M_DONTWAIT);
 					if (SCTP_BUF_NEXT(mm)) {
+						if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(mm), SCTP_SIZE32(chk_length) - chk_length, NULL)) {
+							sctp_m_freem(mm);
+						} else {
 #ifdef SCTP_MBUF_LOGGING
-						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
-							struct mbuf *mat;
-
-							mat = SCTP_BUF_NEXT(mm);
-							while (mat) {
-								if (SCTP_BUF_IS_EXTENDED(mat)) {
-									sctp_log_mb(mat, SCTP_MBUF_ICOPY);
+							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
+								struct mbuf *mat;
+
+								for (mat = SCTP_BUF_NEXT(mm); mat; mat = SCTP_BUF_NEXT(mat)) {
+									if (SCTP_BUF_IS_EXTENDED(mat)) {
+										sctp_log_mb(mat, SCTP_MBUF_ICOPY);
+									}
 								}
-								mat = SCTP_BUF_NEXT(mat);
 							}
+#endif
+							sctp_queue_op_err(stcb, mm);
 						}
-#endif
-						sctp_queue_op_err(stcb, mm);
 					} else {
 						sctp_m_freem(mm);
 					}
@@ -5805,10 +5804,6 @@
 void
 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
 {
-#ifdef SCTP_MBUF_LOGGING
-	struct mbuf *mat;
-
-#endif
 	struct mbuf *m;
 	int iphlen;
 	uint32_t vrf_id = 0;
@@ -5843,6 +5838,8 @@
 #ifdef SCTP_MBUF_LOGGING
 	/* Log in any input mbufs */
 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
+		struct mbuf *mat;
+
 		for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
 			if (SCTP_BUF_IS_EXTENDED(mat)) {
 				sctp_log_mb(mat, SCTP_MBUF_INPUT);
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/netinet/sctp_output.c
--- a/head/sys/netinet/sctp_output.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/netinet/sctp_output.c	Sat Apr 21 21:38:59 2012 +0300
@@ -33,7 +33,7 @@
 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $	 */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/sctp_output.c 233660 2012-03-29 13:36:53Z rrs $");
+__FBSDID("$FreeBSD: head/sys/netinet/sctp_output.c 234461 2012-04-19 13:11:17Z tuexen $");
 
 #include <netinet/sctp_os.h>
 #include <sys/proc.h>
@@ -3714,12 +3714,10 @@
 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 		struct mbuf *mat;
 
-		mat = copy_init;
-		while (mat) {
+		for (mat = copy_init; mat; mat = SCTP_BUF_NEXT(mat)) {
 			if (SCTP_BUF_IS_EXTENDED(mat)) {
 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 			}
-			mat = SCTP_BUF_NEXT(mat);
 		}
 	}
 #endif
@@ -3734,12 +3732,10 @@
 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 		struct mbuf *mat;
 
-		mat = copy_initack;
-		while (mat) {
+		for (mat = copy_initack; mat; mat = SCTP_BUF_NEXT(mat)) {
 			if (SCTP_BUF_IS_EXTENDED(mat)) {
 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 			}
-			mat = SCTP_BUF_NEXT(mat);
 		}
 	}
 #endif
@@ -6446,12 +6442,10 @@
 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 				struct mbuf *mat;
 
-				mat = appendchain;
-				while (mat) {
+				for (mat = appendchain; mat; mat = SCTP_BUF_NEXT(mat)) {
 					if (SCTP_BUF_IS_EXTENDED(mat)) {
 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 					}
-					mat = SCTP_BUF_NEXT(mat);
 				}
 			}
 #endif
@@ -6547,12 +6541,10 @@
 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 			struct mbuf *mat;
 
-			mat = m;
-			while (mat) {
+			for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
 				if (SCTP_BUF_IS_EXTENDED(mat)) {
 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 				}
-				mat = SCTP_BUF_NEXT(mat);
 			}
 		}
 #endif
@@ -7350,12 +7342,10 @@
 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 			struct mbuf *mat;
 
-			mat = chk->data;
-			while (mat) {
+			for (mat = chk->data; mat; mat = SCTP_BUF_NEXT(mat)) {
 				if (SCTP_BUF_IS_EXTENDED(mat)) {
 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 				}
-				mat = SCTP_BUF_NEXT(mat);
 			}
 		}
 #endif
@@ -8953,12 +8943,10 @@
 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 				struct mbuf *mat;
 
-				mat = cookie;
-				while (mat) {
+				for (mat = cookie; mat; mat = SCTP_BUF_NEXT(mat)) {
 					if (SCTP_BUF_IS_EXTENDED(mat)) {
 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 					}
-					mat = SCTP_BUF_NEXT(mat);
 				}
 			}
 #endif
@@ -9027,12 +9015,10 @@
 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 		struct mbuf *mat;
 
-		mat = outchain;
-		while (mat) {
+		for (mat = outchain; mat; mat = SCTP_BUF_NEXT(mat)) {
 			if (SCTP_BUF_IS_EXTENDED(mat)) {
 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 			}
-			mat = SCTP_BUF_NEXT(mat);
 		}
 	}
 #endif
@@ -9313,12 +9299,10 @@
 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 			struct mbuf *mat;
 
-			mat = m_ack;
-			while (mat) {
+			for (mat = m_ack; mat; mat = SCTP_BUF_NEXT(mat)) {
 				if (SCTP_BUF_IS_EXTENDED(mat)) {
 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 				}
-				mat = SCTP_BUF_NEXT(mat);
 			}
 		}
 #endif
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/netinet/sctp_usrreq.c
--- a/head/sys/netinet/sctp_usrreq.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/netinet/sctp_usrreq.c	Sat Apr 21 21:38:59 2012 +0300
@@ -33,7 +33,7 @@
 /* $KAME: sctp_usrreq.c,v 1.48 2005/03/07 23:26:08 itojun Exp $	 */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/sctp_usrreq.c 233940 2012-04-06 09:03:02Z tuexen $");
+__FBSDID("$FreeBSD: head/sys/netinet/sctp_usrreq.c 234464 2012-04-19 15:30:15Z tuexen $");
 #include <netinet/sctp_os.h>
 #include <sys/proc.h>
 #include <netinet/sctp_pcb.h>
@@ -1616,7 +1616,7 @@
   }
 
 
-#define SCTP_CHECK_AND_CAST(destp, srcp, type, size)  {\
+#define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
 	if (size < sizeof(type)) { \
 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
 		error = EINVAL; \
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/netinet/sctp_var.h
--- a/head/sys/netinet/sctp_var.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/netinet/sctp_var.h	Sat Apr 21 21:38:59 2012 +0300
@@ -33,7 +33,7 @@
 /* $KAME: sctp_var.h,v 1.24 2005/03/06 16:04:19 itojun Exp $	 */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/sctp_var.h 228907 2011-12-27 10:16:24Z tuexen $");
+__FBSDID("$FreeBSD: head/sys/netinet/sctp_var.h 234464 2012-04-19 15:30:15Z tuexen $");
 
 #ifndef _NETINET_SCTP_VAR_H_
 #define _NETINET_SCTP_VAR_H_
@@ -157,7 +157,7 @@
 }
 
 #define sctp_alloc_a_chunk(_stcb, _chk) { \
-	if (TAILQ_EMPTY(&(_stcb)->asoc.free_chunks))  { \
+	if (TAILQ_EMPTY(&(_stcb)->asoc.free_chunks)) { \
 		(_chk) = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_chunk), struct sctp_tmit_chunk); \
 		if ((_chk)) { \
 			SCTP_INCR_CHK_COUNT(); \
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/netinet/sctputil.c
--- a/head/sys/netinet/sctputil.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/netinet/sctputil.c	Sat Apr 21 21:38:59 2012 +0300
@@ -33,7 +33,7 @@
 /* $KAME: sctputil.c,v 1.37 2005/03/07 23:26:09 itojun Exp $	 */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/sctputil.c 233660 2012-03-29 13:36:53Z rrs $");
+__FBSDID("$FreeBSD: head/sys/netinet/sctputil.c 234539 2012-04-21 11:53:24Z tuexen $");
 
 #include <netinet/sctp_os.h>
 #include <netinet/sctp_pcb.h>
@@ -2553,8 +2553,8 @@
 		tmp = sctp_get_mbuf_for_msg(padlen, 0, M_DONTWAIT, 1, MT_DATA);
 		if (tmp == NULL) {
 			/* Out of space GAK! we are in big trouble. */
-			SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
-			return (ENOSPC);
+			SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
+			return (ENOBUFS);
 		}
 		/* setup and insert in middle */
 		SCTP_BUF_LEN(tmp) = padlen;
@@ -3214,7 +3214,7 @@
 	struct sctp_stream_change_event *stradd;
 	int len;
 
-	if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) {
+	if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) {
 		/* event not enabled */
 		return;
 	}
@@ -3275,7 +3275,7 @@
 	struct sctp_assoc_reset_event *strasoc;
 	int len;
 
-	if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) {
+	if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) {
 		/* event not enabled */
 		return;
 	}
@@ -3333,7 +3333,7 @@
 	struct sctp_stream_reset_event *strreset;
 	int len;
 
-	if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) {
+	if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) {
 		/* event not enabled */
 		return;
 	}
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/nfsclient/nfs_subs.c
--- a/head/sys/nfsclient/nfs_subs.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/nfsclient/nfs_subs.c	Sat Apr 21 21:38:59 2012 +0300
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/nfsclient/nfs_subs.c 230394 2012-01-20 20:02:01Z jhb $");
+__FBSDID("$FreeBSD: head/sys/nfsclient/nfs_subs.c 234386 2012-04-17 16:28:22Z mckusick $");
 
 /*
  * These functions support the macros and help fiddle mbuf chains for
@@ -866,16 +866,10 @@
 	struct bufobj *bo;
 
 	MNT_ILOCK(mp);
-	MNT_VNODE_FOREACH(vp, mp, nvp) {
+	MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
 		bo = &vp->v_bufobj;
-		VI_LOCK(vp);
-		if (vp->v_iflag & VI_DOOMED) {
-			VI_UNLOCK(vp);
-			continue;
-		}
 		vholdl(vp);
 		VI_UNLOCK(vp);
-		MNT_IUNLOCK(mp);
 		BO_LOCK(bo);
 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
 			if (!BUF_ISLOCKED(bp) &&
@@ -885,9 +879,7 @@
 		}
 		BO_UNLOCK(bo);
 		vdrop(vp);
-		MNT_ILOCK(mp);
 	}
-	MNT_IUNLOCK(mp);
 }
 
 /*
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/nfsclient/nfs_vfsops.c
--- a/head/sys/nfsclient/nfs_vfsops.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/nfsclient/nfs_vfsops.c	Sat Apr 21 21:38:59 2012 +0300
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/nfsclient/nfs_vfsops.c 231852 2012-02-17 02:39:58Z bz $");
+__FBSDID("$FreeBSD: head/sys/nfsclient/nfs_vfsops.c 234386 2012-04-17 16:28:22Z mckusick $");
 
 
 #include "opt_bootp.h"
@@ -1457,19 +1457,15 @@
 	 * Force stale buffer cache information to be flushed.
 	 */
 loop:
-	MNT_VNODE_FOREACH(vp, mp, mvp) {
-		VI_LOCK(vp);
-		MNT_IUNLOCK(mp);
+	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
 		/* XXX Racy bv_cnt check. */
 		if (VOP_ISLOCKED(vp) || vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
 		    waitfor == MNT_LAZY) {
 			VI_UNLOCK(vp);
-			MNT_ILOCK(mp);
 			continue;
 		}
 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
-			MNT_ILOCK(mp);
-			MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
+			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 			goto loop;
 		}
 		error = VOP_FSYNC(vp, waitfor, td);
@@ -1477,10 +1473,7 @@
 			allerror = error;
 		VOP_UNLOCK(vp, 0);
 		vrele(vp);
-
-		MNT_ILOCK(mp);
 	}
-	MNT_IUNLOCK(mp);
 	return (allerror);
 }
 
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/powerpc/aim/swtch32.S
--- a/head/sys/powerpc/aim/swtch32.S	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/powerpc/aim/swtch32.S	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-/* $FreeBSD: head/sys/powerpc/aim/swtch32.S 223485 2011-06-23 22:21:28Z nwhitehorn $ */
+/* $FreeBSD: head/sys/powerpc/aim/swtch32.S 234517 2012-04-20 23:01:36Z nwhitehorn $ */
 /* $NetBSD: locore.S,v 1.24 2000/05/31 05:09:17 thorpej Exp $ */
 
 /*-
@@ -113,6 +113,7 @@
 	mr	%r3,%r14		/* restore old thread ptr */
 	bl	pmap_deactivate		/* Deactivate the current pmap */
 
+	sync				/* Make sure all of that finished */
 	stw	%r16,TD_LOCK(%r14)	/* ULE:	update old thread's lock */
 
 cpu_switchin:
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/powerpc/aim/swtch64.S
--- a/head/sys/powerpc/aim/swtch64.S	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/powerpc/aim/swtch64.S	Sat Apr 21 21:38:59 2012 +0300
@@ -1,4 +1,4 @@
-/* $FreeBSD: head/sys/powerpc/aim/swtch64.S 231019 2012-02-05 15:59:18Z andreast $ */
+/* $FreeBSD: head/sys/powerpc/aim/swtch64.S 234517 2012-04-20 23:01:36Z nwhitehorn $ */
 /* $NetBSD: locore.S,v 1.24 2000/05/31 05:09:17 thorpej Exp $ */
 
 /*-
@@ -139,6 +139,7 @@
 
 	addi	%r1,%r1,48
 
+	sync				/* Make sure all of that finished */
 	std	%r16,TD_LOCK(%r14)	/* ULE:	update old thread's lock */
 
 cpu_switchin:
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/sys/diskmbr.h
--- a/head/sys/sys/diskmbr.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/sys/diskmbr.h	Sat Apr 21 21:38:59 2012 +0300
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)disklabel.h	8.2 (Berkeley) 7/10/94
- * $FreeBSD: head/sys/sys/diskmbr.h 231367 2012-02-10 09:55:18Z ae $
+ * $FreeBSD: head/sys/sys/diskmbr.h 234417 2012-04-18 11:59:03Z marck $
  */
 
 #ifndef _SYS_DISKMBR_H_
@@ -55,6 +55,8 @@
 #define	DOSPTYP_LINUX	0x83	/* Linux partition */
 #define	DOSPTYP_LINLVM	0x8e	/* Linux LVM partition */
 #define	DOSPTYP_PMBR	0xee	/* GPT Protective MBR */
+#define	DOSPTYP_VMFS	0xfb	/* VMware VMFS partition */
+#define	DOSPTYP_VMKDIAG	0xfc	/* VMware vmkDiagnostic partition */
 #define	DOSPTYP_LINRAID	0xfd	/* Linux raid partition */
 
 struct dos_partition {
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/sys/gpt.h
--- a/head/sys/sys/gpt.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/sys/gpt.h	Sat Apr 21 21:38:59 2012 +0300
@@ -23,7 +23,7 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * $FreeBSD$
+ * $FreeBSD: head/sys/sys/gpt.h 234417 2012-04-18 11:59:03Z marck $
  */
 
 #ifndef _SYS_GPT_H_
@@ -120,6 +120,13 @@
 #define	GPT_ENT_TYPE_LINUX_LVM		\
 	{0xe6d6d379,0xf507,0x44c2,0xa2,0x3c,{0x23,0x8f,0x2a,0x3d,0xf9,0x28}}
 
+#define	GPT_ENT_TYPE_VMFS		\
+	{0xaa31e02a,0x400f,0x11db,0x95,0x90,{0x00,0x0c,0x29,0x11,0xd1,0xb8}}
+#define	GPT_ENT_TYPE_VMKDIAG		\
+	{0x9d275380,0x40ad,0x11db,0xbf,0x97,{0x00,0x0c,0x29,0x11,0xd1,0xb8}}
+#define	GPT_ENT_TYPE_VMRESERVED		\
+	{0x9198effc,0x31c0,0x11db,0x8f,0x78,{0x00,0x0c,0x29,0x11,0xd1,0xb8}}
+
 #define	GPT_ENT_TYPE_APPLE_BOOT		\
 	{0x426F6F74,0x0000,0x11aa,0xaa,0x11,{0x00,0x30,0x65,0x43,0xec,0xac}}
 #define	GPT_ENT_TYPE_APPLE_HFS		\
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/sys/ktrace.h
--- a/head/sys/sys/ktrace.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/sys/ktrace.h	Sat Apr 21 21:38:59 2012 +0300
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)ktrace.h	8.1 (Berkeley) 6/2/93
- * $FreeBSD: head/sys/sys/ktrace.h 233925 2012-04-05 17:13:14Z jhb $
+ * $FreeBSD: head/sys/sys/ktrace.h 234494 2012-04-20 15:32:36Z jhb $
  */
 
 #ifndef _SYS_KTRACE_H_
@@ -135,9 +135,15 @@
  * KTR_CSW - trace context switches
  */
 #define KTR_CSW		6
+struct ktr_csw_old {
+	int	out;	/* 1 if switch out, 0 if switch in */
+	int	user;	/* 1 if usermode (ivcsw), 0 if kernel (vcsw) */
+};
+
 struct ktr_csw {
 	int	out;	/* 1 if switch out, 0 if switch in */
 	int	user;	/* 1 if usermode (ivcsw), 0 if kernel (vcsw) */
+	char	wmesg[8];
 };
 
 /*
@@ -244,7 +250,7 @@
 
 #ifdef	_KERNEL
 void	ktrnamei(char *);
-void	ktrcsw(int, int);
+void	ktrcsw(int, int, const char *);
 void	ktrpsig(int, sig_t, sigset_t *, int);
 void	ktrfault(vm_offset_t, int);
 void	ktrfaultend(int);
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/sys/mount.h
--- a/head/sys/sys/mount.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/sys/mount.h	Sat Apr 21 21:38:59 2012 +0300
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)mount.h	8.21 (Berkeley) 5/20/95
- * $FreeBSD: head/sys/sys/mount.h 234157 2012-04-11 22:43:40Z mckusick $
+ * $FreeBSD: head/sys/sys/mount.h 234482 2012-04-20 06:50:44Z mckusick $
  */
 
 #ifndef _SYS_MOUNT_H_
@@ -164,6 +164,8 @@
 	int		mnt_ref;		/* (i) Reference count */
 	struct vnodelst	mnt_nvnodelist;		/* (i) list of vnodes */
 	int		mnt_nvnodelistsize;	/* (i) # of vnodes */
+	struct vnodelst	mnt_activevnodelist;	/* (i) list of active vnodes */
+	int		mnt_activevnodelistsize;/* (i) # of active vnodes */
 	int		mnt_writeopcount;	/* (i) write syscalls pending */
 	int		mnt_kern_flag;		/* (i) kernel only flags */
 	uint64_t	mnt_flag;		/* (i) flags shared with user */
@@ -187,6 +189,49 @@
 	struct lock	mnt_explock;		/* vfs_export walkers lock */
 };
 
+/*
+ * Definitions for MNT_VNODE_FOREACH_ALL.
+ */
+struct vnode *__mnt_vnode_next_all(struct vnode **mvp, struct mount *mp);
+struct vnode *__mnt_vnode_first_all(struct vnode **mvp, struct mount *mp);
+void          __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp);
+
+#define MNT_VNODE_FOREACH_ALL(vp, mp, mvp) \
+	for (vp = __mnt_vnode_first_all(&(mvp), (mp)); \
+		(vp) != NULL; vp = __mnt_vnode_next_all(&(mvp), (mp)))
+
+#define MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp)				\
+	do {								\
+		MNT_ILOCK(mp);						\
+		__mnt_vnode_markerfree_all(&(mvp), (mp));		\
+		/* MNT_IUNLOCK(mp); -- done in above function */	\
+		mtx_assert(MNT_MTX(mp), MA_NOTOWNED);			\
+	} while (0)
+
+/*
+ * Definitions for MNT_VNODE_FOREACH_ACTIVE.
+ */
+struct vnode *__mnt_vnode_next_active(struct vnode **mvp, struct mount *mp);
+struct vnode *__mnt_vnode_first_active(struct vnode **mvp, struct mount *mp);
+void          __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *);
+
+#define MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) \
+	for (vp = __mnt_vnode_first_active(&(mvp), (mp)); \
+		(vp) != NULL; vp = __mnt_vnode_next_active(&(mvp), (mp)))
+
+#define MNT_VNODE_FOREACH_ACTIVE_ABORT(mp, mvp)				\
+	do {								\
+		MNT_ILOCK(mp);						\
+		__mnt_vnode_markerfree_active(&(mvp), (mp));		\
+		/* MNT_IUNLOCK(mp); -- done in above function */	\
+		mtx_assert(MNT_MTX(mp), MA_NOTOWNED);			\
+	} while (0)
+
+/*
+ * Definitions for MNT_VNODE_FOREACH.
+ *
+ * This interface has been deprecated in favor of MNT_VNODE_FOREACH_ALL.
+ */
 struct vnode *__mnt_vnode_next(struct vnode **mvp, struct mount *mp);
 struct vnode *__mnt_vnode_first(struct vnode **mvp, struct mount *mp);
 void          __mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp);
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/sys/vnode.h
--- a/head/sys/sys/vnode.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/sys/vnode.h	Sat Apr 21 21:38:59 2012 +0300
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)vnode.h	8.7 (Berkeley) 2/4/94
- * $FreeBSD: head/sys/sys/vnode.h 234158 2012-04-11 23:01:11Z mckusick $
+ * $FreeBSD: head/sys/sys/vnode.h 234482 2012-04-20 06:50:44Z mckusick $
  */
 
 #ifndef _SYS_VNODE_H_
@@ -156,7 +156,7 @@
 	/*
 	 * The machinery of being a vnode
 	 */
-	TAILQ_ENTRY(vnode) v_freelist;		/* f vnode freelist */
+	TAILQ_ENTRY(vnode) v_actfreelist;	/* f vnode active/free lists */
 	struct bufobj	v_bufobj;		/* * Buffer cache object */
 
 	/*
@@ -232,6 +232,7 @@
 #define	VI_AGE		0x0040	/* Insert vnode at head of free list */
 #define	VI_DOOMED	0x0080	/* This vnode is being recycled */
 #define	VI_FREE		0x0100	/* This vnode is on the freelist */
+#define	VI_ACTIVE	0x0200	/* This vnode is on the active list */
 #define	VI_DOINGINACT	0x0800	/* VOP_INACTIVE is in progress */
 #define	VI_OWEINACT	0x1000	/* Need to call inactive */
 
@@ -626,7 +627,6 @@
 int	vcount(struct vnode *vp);
 void	vdrop(struct vnode *);
 void	vdropl(struct vnode *);
-void	vdestroy(struct vnode *);
 int	vflush(struct mount *mp, int rootrefs, int flags, struct thread *td);
 int	vget(struct vnode *vp, int lockflag, struct thread *td);
 void	vgone(struct vnode *vp);
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/ufs/ffs/ffs_snapshot.c
--- a/head/sys/ufs/ffs/ffs_snapshot.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/ufs/ffs/ffs_snapshot.c	Sat Apr 21 21:38:59 2012 +0300
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/ufs/ffs/ffs_snapshot.c 234158 2012-04-11 23:01:11Z mckusick $");
+__FBSDID("$FreeBSD: head/sys/ufs/ffs/ffs_snapshot.c 234386 2012-04-17 16:28:22Z mckusick $");
 
 #include "opt_quota.h"
 
@@ -522,17 +522,14 @@
 	    FSMAXSNAP + 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
 	MNT_ILOCK(mp);
 	mp->mnt_kern_flag &= ~MNTK_SUSPENDED;
+	MNT_IUNLOCK(mp);
 loop:
-	MNT_VNODE_FOREACH(xvp, mp, mvp) {
-		VI_LOCK(xvp);
-		MNT_IUNLOCK(mp);
-		if ((xvp->v_iflag & VI_DOOMED) ||
-		    (xvp->v_usecount == 0 &&
+	MNT_VNODE_FOREACH_ALL(xvp, mp, mvp) {
+		if ((xvp->v_usecount == 0 &&
 		     (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) ||
 		    xvp->v_type == VNON ||
 		    IS_SNAPSHOT(VTOI(xvp))) {
 			VI_UNLOCK(xvp);
-			MNT_ILOCK(mp);
 			continue;
 		}
 		/*
@@ -541,13 +538,11 @@
 		 */
 		if (xvp == nd.ni_dvp) {
 			VI_UNLOCK(xvp);
-			MNT_ILOCK(mp);
 			continue;
 		}
 		vholdl(xvp);
 		if (vn_lock(xvp, LK_EXCLUSIVE | LK_INTERLOCK) != 0) {
-			MNT_ILOCK(mp);
-			MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
+			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 			vdrop(xvp);
 			goto loop;
 		}
@@ -557,7 +552,6 @@
 			VI_UNLOCK(xvp);
 			VOP_UNLOCK(xvp, 0);
 			vdrop(xvp);
-			MNT_ILOCK(mp);
 			continue;
 		}
 		VI_UNLOCK(xvp);
@@ -567,14 +561,12 @@
 		    vat.va_nlink > 0) {
 			VOP_UNLOCK(xvp, 0);
 			vdrop(xvp);
-			MNT_ILOCK(mp);
 			continue;
 		}
 		xp = VTOI(xvp);
 		if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) {
 			VOP_UNLOCK(xvp, 0);
 			vdrop(xvp);
-			MNT_ILOCK(mp);
 			continue;
 		}
 		/*
@@ -610,12 +602,10 @@
 			free(copy_fs->fs_csp, M_UFSMNT);
 			free(copy_fs, M_UFSMNT);
 			copy_fs = NULL;
-			MNT_VNODE_FOREACH_ABORT(mp, mvp);
+			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 			goto out1;
 		}
-		MNT_ILOCK(mp);
 	}
-	MNT_IUNLOCK(mp);
 	/*
 	 * Erase the journal file from the snapshot.
 	 */
@@ -2532,31 +2522,26 @@
 
 	td = curthread;
 	(void) vn_start_secondary_write(NULL, &mp, V_WAIT);
-	MNT_ILOCK(mp);
  loop:
-	MNT_VNODE_FOREACH(vp, mp, mvp) {
-		VI_LOCK(vp);
+	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
 		/*
 		 * IN_LAZYACCESS is checked here without holding any
 		 * vnode lock, but this flag is set only while holding
 		 * vnode interlock.
 		 */
-		if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED) != 0 ||
+		if (vp->v_type == VNON ||
 		    ((VTOI(vp)->i_flag & IN_LAZYACCESS) == 0 &&
-			((vp->v_iflag & VI_OWEINACT) == 0 ||
-			vp->v_usecount > 0))) {
+		    ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0))) {
 			VI_UNLOCK(vp);
 			continue;
 		}
-		MNT_IUNLOCK(mp);
 		vholdl(vp);
 		error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
 		if (error != 0) {
 			vdrop(vp);
-			MNT_ILOCK(mp);
 			if (error == ENOENT)
 				continue;	/* vnode recycled */
-			MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
+			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 			goto loop;
 		}
 		ip = VTOI(vp);
@@ -2569,7 +2554,6 @@
 			VI_UNLOCK(vp);
 			VOP_UNLOCK(vp, 0);
 			vdrop(vp);
-			MNT_ILOCK(mp);
 			continue;
 		}
 		vinactive(vp, td);
@@ -2578,9 +2562,7 @@
 		VI_UNLOCK(vp);
 		VOP_UNLOCK(vp, 0);
 		vdrop(vp);
-		MNT_ILOCK(mp);
 	}
-	MNT_IUNLOCK(mp);
 	vn_finished_secondary_write(mp);
 }
 
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/ufs/ffs/ffs_softdep.c
--- a/head/sys/ufs/ffs/ffs_softdep.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/ufs/ffs/ffs_softdep.c	Sat Apr 21 21:38:59 2012 +0300
@@ -40,7 +40,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/ufs/ffs/ffs_softdep.c 233817 2012-04-02 21:58:37Z mckusick $");
+__FBSDID("$FreeBSD: head/sys/ufs/ffs/ffs_softdep.c 234386 2012-04-17 16:28:22Z mckusick $");
 
 #include "opt_ffs.h"
 #include "opt_quota.h"
@@ -12642,29 +12642,21 @@
 	     fs->fs_cstotal.cs_nbfree <= needed) ||
 	    (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
 	     fs->fs_cstotal.cs_nifree <= needed)) {
-		MNT_ILOCK(mp);
-		MNT_VNODE_FOREACH(lvp, mp, mvp) {
-			VI_LOCK(lvp);
+		MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) {
 			if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) {
 				VI_UNLOCK(lvp);
 				continue;
 			}
-			MNT_IUNLOCK(mp);
 			if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT,
-			    curthread)) {
-				MNT_ILOCK(mp);
+			    curthread))
 				continue;
-			}
 			if (lvp->v_vflag & VV_NOSYNC) {	/* unlinked */
 				vput(lvp);
-				MNT_ILOCK(mp);
 				continue;
 			}
 			(void) ffs_syncvnode(lvp, MNT_NOWAIT, 0);
 			vput(lvp);
-			MNT_ILOCK(mp);
-		}
-		MNT_IUNLOCK(mp);
+		}
 		lvp = ump->um_devvp;
 		if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
 			VOP_FSYNC(lvp, MNT_NOWAIT, curthread);
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/ufs/ffs/ffs_vfsops.c
--- a/head/sys/ufs/ffs/ffs_vfsops.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/ufs/ffs/ffs_vfsops.c	Sat Apr 21 21:38:59 2012 +0300
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/ufs/ffs/ffs_vfsops.c 234036 2012-04-08 13:44:55Z trasz $");
+__FBSDID("$FreeBSD: head/sys/ufs/ffs/ffs_vfsops.c 234537 2012-04-21 10:45:46Z trasz $");
 
 #include "opt_quota.h"
 #include "opt_ufs.h"
@@ -699,25 +699,22 @@
 	 * We no longer know anything about clusters per cylinder group.
 	 */
 	if (fs->fs_contigsumsize > 0) {
-		lp = fs->fs_maxcluster;
+		fs->fs_maxcluster = lp = space;
 		for (i = 0; i < fs->fs_ncg; i++)
 			*lp++ = fs->fs_contigsumsize;
+		space = lp;
 	}
+	size = fs->fs_ncg * sizeof(u_int8_t);
+	fs->fs_contigdirs = (u_int8_t *)space;
+	bzero(fs->fs_contigdirs, size);
 
 loop:
-	MNT_ILOCK(mp);
-	MNT_VNODE_FOREACH(vp, mp, mvp) {
-		VI_LOCK(vp);
-		if (vp->v_iflag & VI_DOOMED) {
-			VI_UNLOCK(vp);
-			continue;
-		}
-		MNT_IUNLOCK(mp);
+	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
 		/*
 		 * Step 4: invalidate all cached file data.
 		 */
 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
-			MNT_VNODE_FOREACH_ABORT(mp, mvp);
+			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 			goto loop;
 		}
 		if (vinvalbuf(vp, 0, 0, 0))
@@ -732,7 +729,7 @@
 		if (error) {
 			VOP_UNLOCK(vp, 0);
 			vrele(vp);
-			MNT_VNODE_FOREACH_ABORT(mp, mvp);
+			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 			return (error);
 		}
 		ffs_load_inode(bp, ip, fs, ip->i_number);
@@ -740,9 +737,7 @@
 		brelse(bp);
 		VOP_UNLOCK(vp, 0);
 		vrele(vp);
-		MNT_ILOCK(mp);
 	}
-	MNT_IUNLOCK(mp);
 	return (0);
 }
 
@@ -1441,10 +1436,8 @@
 	td = curthread;
 	if ((mp->mnt_flag & MNT_NOATIME) != 0)
 		goto qupdate;
-	MNT_ILOCK(mp);
-	MNT_VNODE_FOREACH(vp, mp, mvp) {
-		VI_LOCK(vp);
-		if (vp->v_iflag & VI_DOOMED || vp->v_type == VNON) {
+	MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
+		if (vp->v_type == VNON) {
 			VI_UNLOCK(vp);
 			continue;
 		}
@@ -1462,19 +1455,14 @@
 			VI_UNLOCK(vp);
 			continue;
 		}
-		MNT_IUNLOCK(mp);
 		if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
-		    td)) != 0) {
-			MNT_ILOCK(mp);
+		    td)) != 0)
 			continue;
-		}
 		error = ffs_update(vp, 0);
 		if (error != 0)
 			allerror = error;
 		vput(vp);
-		MNT_ILOCK(mp);
 	}
-	MNT_IUNLOCK(mp);
 
 qupdate:
 #ifdef QUOTA
@@ -1538,41 +1526,37 @@
 		lockreq = LK_EXCLUSIVE;
 	}
 	lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
-	MNT_ILOCK(mp);
 loop:
 	/* Grab snapshot of secondary write counts */
+	MNT_ILOCK(mp);
 	secondary_writes = mp->mnt_secondary_writes;
 	secondary_accwrites = mp->mnt_secondary_accwrites;
+	MNT_IUNLOCK(mp);
 
 	/* Grab snapshot of softdep dependency counts */
-	MNT_IUNLOCK(mp);
 	softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
-	MNT_ILOCK(mp);
 
-	MNT_VNODE_FOREACH(vp, mp, mvp) {
+	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
 		/*
 		 * Depend on the vnode interlock to keep things stable enough
 		 * for a quick test.  Since there might be hundreds of
 		 * thousands of vnodes, we cannot afford even a subroutine
 		 * call unless there's a good chance that we have work to do.
 		 */
-		VI_LOCK(vp);
-		if (vp->v_iflag & VI_DOOMED) {
+		if (vp->v_type == VNON) {
 			VI_UNLOCK(vp);
 			continue;
 		}
 		ip = VTOI(vp);
-		if (vp->v_type == VNON || ((ip->i_flag &
+		if ((ip->i_flag &
 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
-		    vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
+		    vp->v_bufobj.bo_dirty.bv_cnt == 0) {
 			VI_UNLOCK(vp);
 			continue;
 		}
-		MNT_IUNLOCK(mp);
 		if ((error = vget(vp, lockreq, td)) != 0) {
-			MNT_ILOCK(mp);
 			if (error == ENOENT || error == ENOLCK) {
-				MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
+				MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 				goto loop;
 			}
 			continue;
@@ -1580,9 +1564,7 @@
 		if ((error = ffs_syncvnode(vp, waitfor, 0)) != 0)
 			allerror = error;
 		vput(vp);
-		MNT_ILOCK(mp);
 	}
-	MNT_IUNLOCK(mp);
 	/*
 	 * Force stale filesystem control information to be flushed.
 	 */
@@ -1590,10 +1572,8 @@
 		if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
 			allerror = error;
 		/* Flushed work items may create new vnodes to clean */
-		if (allerror == 0 && count) {
-			MNT_ILOCK(mp);
+		if (allerror == 0 && count)
 			goto loop;
-		}
 	}
 #ifdef QUOTA
 	qsync(mp);
@@ -1608,18 +1588,18 @@
 		if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
 			allerror = error;
 		VOP_UNLOCK(devvp, 0);
-		if (allerror == 0 && waitfor == MNT_WAIT) {
-			MNT_ILOCK(mp);
+		if (allerror == 0 && waitfor == MNT_WAIT)
 			goto loop;
-		}
 	} else if (suspend != 0) {
 		if (softdep_check_suspend(mp,
 					  devvp,
 					  softdep_deps,
 					  softdep_accdeps,
 					  secondary_writes,
-					  secondary_accwrites) != 0)
+					  secondary_accwrites) != 0) {
+			MNT_IUNLOCK(mp);
 			goto loop;	/* More work needed */
+		}
 		mtx_assert(MNT_MTX(mp), MA_OWNED);
 		mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
 		MNT_IUNLOCK(mp);
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/ufs/ufs/quota.h
--- a/head/sys/ufs/ufs/quota.h	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/ufs/ufs/quota.h	Sat Apr 21 21:38:59 2012 +0300
@@ -30,7 +30,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)quota.h	8.3 (Berkeley) 8/19/94
- * $FreeBSD: head/sys/ufs/ufs/quota.h 222955 2011-06-10 22:19:44Z jeff $
+ * $FreeBSD: head/sys/ufs/ufs/quota.h 234483 2012-04-20 07:00:28Z mckusick $
  */
 
 #ifndef _UFS_UFS_QUOTA_H_
@@ -227,9 +227,10 @@
 void	dqrele(struct vnode *, struct dquot *);
 void	dquninit(void);
 int	getinoquota(struct inode *);
-int	qsync(struct mount *mp);
-int	quotaoff(struct thread *td, struct mount *, int);
-int	quotaon(struct thread *td, struct mount *, int, void *);
+int	qsync(struct mount *);
+int	qsyncvp(struct vnode *);
+int	quotaoff(struct thread *, struct mount *, int);
+int	quotaon(struct thread *, struct mount *, int, void *);
 int	getquota32(struct thread *, struct mount *, u_long, int, void *);
 int	setquota32(struct thread *, struct mount *, u_long, int, void *);
 int	setuse32(struct thread *, struct mount *, u_long, int, void *);
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/ufs/ufs/ufs_inode.c
--- a/head/sys/ufs/ufs/ufs_inode.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/ufs/ufs/ufs_inode.c	Sat Apr 21 21:38:59 2012 +0300
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/ufs/ufs/ufs_inode.c 223769 2011-07-04 20:52:23Z jeff $");
+__FBSDID("$FreeBSD: head/sys/ufs/ufs/ufs_inode.c 234483 2012-04-20 07:00:28Z mckusick $");
 
 #include "opt_quota.h"
 #include "opt_ufs.h"
@@ -88,6 +88,14 @@
 #ifdef UFS_GJOURNAL
 	ufs_gjournal_close(vp);
 #endif
+#ifdef QUOTA
+	/*
+	 * Before moving off the active list, we must be sure that
+	 * any modified quotas have been pushed since these will no
+	 * longer be checked once the vnode is on the inactive list.
+	 */
+	qsyncvp(vp);
+#endif
 	if ((ip->i_effnlink == 0 && DOINGSOFTDEP(vp)) ||
 	    (ip->i_nlink <= 0 && !UFS_RDONLY(ip))) {
 	loop:
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/ufs/ufs/ufs_quota.c
--- a/head/sys/ufs/ufs/ufs_quota.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/ufs/ufs/ufs_quota.c	Sat Apr 21 21:38:59 2012 +0300
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/ufs/ufs/ufs_quota.c 233608 2012-03-28 13:56:18Z kib $");
+__FBSDID("$FreeBSD: head/sys/ufs/ufs/ufs_quota.c 234483 2012-04-20 07:00:28Z mckusick $");
 
 #include "opt_ffs.h"
 
@@ -598,32 +598,25 @@
 	 * adding references to quota file being opened.
 	 * NB: only need to add dquot's for inodes being modified.
 	 */
-	MNT_ILOCK(mp);
 again:
-	MNT_VNODE_FOREACH(vp, mp, mvp) {
-		VI_LOCK(vp);
-		MNT_IUNLOCK(mp);
+	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
-			MNT_ILOCK(mp);
-			MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
+			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 			goto again;
 		}
 		if (vp->v_type == VNON || vp->v_writecount == 0) {
 			VOP_UNLOCK(vp, 0);
 			vrele(vp);
-			MNT_ILOCK(mp);
 			continue;
 		}
 		error = getinoquota(VTOI(vp));
 		VOP_UNLOCK(vp, 0);
 		vrele(vp);
-		MNT_ILOCK(mp);
 		if (error) {
-			MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
+			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 			break;
 		}
 	}
-	MNT_IUNLOCK(mp);
 
         if (error)
 		quotaoff_inchange(td, mp, type);
@@ -669,19 +662,14 @@
 	 * Search vnodes associated with this mount point,
 	 * deleting any references to quota file being closed.
 	 */
-	MNT_ILOCK(mp);
 again:
-	MNT_VNODE_FOREACH(vp, mp, mvp) {
-		VI_LOCK(vp);
-		MNT_IUNLOCK(mp);
+	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
 		if (vp->v_type == VNON) {
 			VI_UNLOCK(vp);
-			MNT_ILOCK(mp);
 			continue;
 		}
 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
-			MNT_ILOCK(mp);
-			MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
+			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 			goto again;
 		}
 		ip = VTOI(vp);
@@ -690,9 +678,7 @@
 		dqrele(vp, dq);
 		VOP_UNLOCK(vp, 0);
 		vrele(vp);
-		MNT_ILOCK(mp);
 	}
-	MNT_IUNLOCK(mp);
 
 	dqflush(qvp);
 	/* Clear um_quotas before closing the quota vnode to prevent
@@ -1057,20 +1043,16 @@
 	 * Search vnodes associated with this mount point,
 	 * synchronizing any modified dquot structures.
 	 */
-	MNT_ILOCK(mp);
 again:
-	MNT_VNODE_FOREACH(vp, mp, mvp) {
-		VI_LOCK(vp);
+	MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
 		if (vp->v_type == VNON) {
 			VI_UNLOCK(vp);
 			continue;
 		}
-		MNT_IUNLOCK(mp);
 		error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td);
 		if (error) {
-			MNT_ILOCK(mp);
 			if (error == ENOENT) {
-				MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
+				MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 				goto again;
 			}
 			continue;
@@ -1081,9 +1063,40 @@
 				dqsync(vp, dq);
 		}
 		vput(vp);
-		MNT_ILOCK(mp);
 	}
-	MNT_IUNLOCK(mp);
+	return (0);
+}
+
+/*
+ * Sync quota file for given vnode to disk.
+ */
+int
+qsyncvp(struct vnode *vp)
+{
+	struct ufsmount *ump = VFSTOUFS(vp->v_mount);
+	struct dquot *dq;
+	int i;
+
+	/*
+	 * Check if the mount point has any quotas.
+	 * If not, simply return.
+	 */
+	UFS_LOCK(ump);
+	for (i = 0; i < MAXQUOTAS; i++)
+		if (ump->um_quotas[i] != NULLVP)
+			break;
+	UFS_UNLOCK(ump);
+	if (i == MAXQUOTAS)
+		return (0);
+	/*
+	 * Search quotas associated with this vnode
+	 * synchronizing any modified dquot structures.
+	 */
+	for (i = 0; i < MAXQUOTAS; i++) {
+		dq = VTOI(vp)->i_dquot[i];
+		if (dq != NODQUOT)
+			dqsync(vp, dq);
+	}
 	return (0);
 }
 
diff -r 18bc588aa6ad -r 455aabca3842 head/sys/ufs/ufs/ufs_vnops.c
--- a/head/sys/ufs/ufs/ufs_vnops.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/sys/ufs/ufs/ufs_vnops.c	Sat Apr 21 21:38:59 2012 +0300
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/ufs/ufs/ufs_vnops.c 234103 2012-04-10 15:59:37Z jh $");
+__FBSDID("$FreeBSD: head/sys/ufs/ufs/ufs_vnops.c 234421 2012-04-18 15:19:00Z jh $");
 
 #include "opt_quota.h"
 #include "opt_suiddir.h"
@@ -573,9 +573,8 @@
 	}
 	/*
 	 * If immutable or append, no one can change any of its attributes
-	 * except the ones already handled (exec atime and, in some cases
-	 * for the superuser, file flags including the immutability flags
-	 * themselves).
+	 * except the ones already handled (in some cases, file flags
+	 * including the immutability flags themselves for the superuser).
 	 */
 	if (ip->i_flags & (IMMUTABLE | APPEND))
 		return (EPERM);
diff -r 18bc588aa6ad -r 455aabca3842 head/usr.bin/calendar/calendars/calendar.freebsd
--- a/head/usr.bin/calendar/calendars/calendar.freebsd	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/usr.bin/calendar/calendars/calendar.freebsd	Sat Apr 21 21:38:59 2012 +0300
@@ -1,7 +1,7 @@
 /*
  * FreeBSD
  *
- * $FreeBSD: head/usr.bin/calendar/calendars/calendar.freebsd 234320 2012-04-15 17:28:47Z madpilot $
+ * $FreeBSD: head/usr.bin/calendar/calendars/calendar.freebsd 234446 2012-04-18 22:56:46Z dteske $
  */
 
 #ifndef _calendar_freebsd_
@@ -181,6 +181,7 @@
 06/04	Justin Gibbs <gibbs at FreeBSD.org> born in San Pedro, California, United States, 1973
 06/04	Jason Evans <jasone at FreeBSD.org> born in Greeley, Colorado, United States, 1973
 06/04	Thomas Moestl <tmm at FreeBSD.org> born in Braunschweig, Niedersachsen, Germany, 1980
+06/04	Devin Teske <dteske at FreeBSD.org> born in Arcadia, California, United States, 1982
 06/04	Zack Kirsch <zack at FreeBSD.org> born in Memphis, Tennessee, United States, 1982
 06/06	Sergei Kolobov <sergei at FreeBSD.org> born in Karpinsk, Russian Federation, 1972
 06/06	Alan Eldridge <alane at FreeBSD.org> died in Denver, Colorado, 2003
diff -r 18bc588aa6ad -r 455aabca3842 head/usr.bin/file/Makefile
--- a/head/usr.bin/file/Makefile	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/usr.bin/file/Makefile	Sat Apr 21 21:38:59 2012 +0300
@@ -1,6 +1,6 @@
-# $FreeBSD$
+# $FreeBSD: head/usr.bin/file/Makefile 234449 2012-04-19 03:20:13Z obrien $
 # Makefile for file(1) cmd.
-# Copyright (c) David E. O'Brien, 2000-2004
+# Copyright (c) David E. O'Brien, 2000-2004, 2009
 # Copyright (c) Ian F. Darwin 86/09/01 - see LEGAL.NOTICE.
 #
 # This software is not subject to any license of the American Telephone
diff -r 18bc588aa6ad -r 455aabca3842 head/usr.bin/kdump/kdump.1
--- a/head/usr.bin/kdump/kdump.1	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/usr.bin/kdump/kdump.1	Sat Apr 21 21:38:59 2012 +0300
@@ -26,9 +26,9 @@
 .\" SUCH DAMAGE.
 .\"
 .\"	@(#)kdump.1	8.1 (Berkeley) 6/6/93
-.\" $FreeBSD: head/usr.bin/kdump/kdump.1 233925 2012-04-05 17:13:14Z jhb $
+.\" $FreeBSD: head/usr.bin/kdump/kdump.1 234494 2012-04-20 15:32:36Z jhb $
 .\"
-.Dd April 5, 2012
+.Dd April 20, 2012
 .Dt KDUMP 1
 .Os
 .Sh NAME
@@ -167,7 +167,7 @@
 .It Li NAMI Ta file name lookup Ta path to file
 .It Li GIO Ta general I/O Ta fd, read/write, number of bytes
 .It Li PSIG Ta signal Ta signal name, handler, mask, code
-.It Li CSW Ta context switch Ta stop/resume user/kernel
+.It Li CSW Ta context switch Ta stop/resume user/kernel wmesg
 .It Li USER Ta data from user process Ta the data
 .It Li STRU Ta various syscalls Ta structure
 .It Li SCTL Ta Xr sysctl 3 requests Ta MIB name
diff -r 18bc588aa6ad -r 455aabca3842 head/usr.bin/kdump/kdump.c
--- a/head/usr.bin/kdump/kdump.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/usr.bin/kdump/kdump.c	Sat Apr 21 21:38:59 2012 +0300
@@ -39,7 +39,7 @@
 #endif
 #endif /* not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/usr.bin/kdump/kdump.c 233925 2012-04-05 17:13:14Z jhb $");
+__FBSDID("$FreeBSD: head/usr.bin/kdump/kdump.c 234494 2012-04-20 15:32:36Z jhb $");
 
 #define _KERNEL
 extern int errno;
@@ -95,6 +95,7 @@
 void ktrgenio(struct ktr_genio *, int);
 void ktrpsig(struct ktr_psig *);
 void ktrcsw(struct ktr_csw *);
+void ktrcsw_old(struct ktr_csw_old *);
 void ktruser_malloc(unsigned char *);
 void ktruser_rtld(int, unsigned char *);
 void ktruser(int, unsigned char *);
@@ -298,7 +299,10 @@
 			ktrpsig((struct ktr_psig *)m);
 			break;
 		case KTR_CSW:
-			ktrcsw((struct ktr_csw *)m);
+			if (ktrlen == sizeof(struct ktr_csw_old))
+				ktrcsw_old((struct ktr_csw_old *)m);
+			else
+				ktrcsw((struct ktr_csw *)m);
 			break;
 		case KTR_USER:
 			ktruser(ktrlen, m);
@@ -1245,12 +1249,19 @@
 }
 
 void
-ktrcsw(struct ktr_csw *cs)
+ktrcsw_old(struct ktr_csw_old *cs)
 {
 	printf("%s %s\n", cs->out ? "stop" : "resume",
 		cs->user ? "user" : "kernel");
 }
 
+void
+ktrcsw(struct ktr_csw *cs)
+{
+	printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
+	    cs->user ? "user" : "kernel", cs->wmesg);
+}
+
 #define	UTRACE_DLOPEN_START		1
 #define	UTRACE_DLOPEN_STOP		2
 #define	UTRACE_DLCLOSE_START		3
diff -r 18bc588aa6ad -r 455aabca3842 head/usr.bin/top/machine.c
--- a/head/usr.bin/top/machine.c	Sat Apr 21 21:30:13 2012 +0300
+++ b/head/usr.bin/top/machine.c	Sat Apr 21 21:38:59 2012 +0300
@@ -20,7 +20,7 @@
  *          Wolfram Schneider <wosch at FreeBSD.org>
  *          Thomas Moestl <tmoestl at gmx.net>
  *
- * $FreeBSD: head/usr.bin/top/machine.c 228992 2011-12-30 11:02:40Z uqs $
+ * $FreeBSD: head/usr.bin/top/machine.c 234416 2012-04-18 10:23:42Z kib $
  */
 
 #include <sys/param.h>
@@ -933,7 +933,7 @@
 		p_tot = rup->ru_inblock + rup->ru_oublock + rup->ru_majflt;
 		s_tot = total_inblock + total_oublock + total_majflt;
 
-		sprintf(fmt, io_Proc_format,
+		snprintf(fmt, sizeof(fmt), io_Proc_format,
 		    pp->ki_pid,
 		    jid_buf,
 		    namelength, namelength, (*get_userid)(pp->ki_ruid),
@@ -961,7 +961,7 @@
 		snprintf(thr_buf, sizeof(thr_buf), "%*d ",
 		    sizeof(thr_buf) - 2, pp->ki_numthreads);
 
-	sprintf(fmt, proc_fmt,
+	snprintf(fmt, sizeof(fmt), proc_fmt,
 	    pp->ki_pid,
 	    jid_buf,
 	    namelength, namelength, (*get_userid)(pp->ki_ruid),


More information about the Zrouter-src-freebsd mailing list