aboutsummaryrefslogtreecommitdiff
path: root/tools/test
diff options
context:
space:
mode:
authorPeter Holm <pho@FreeBSD.org>2021-03-03 12:56:56 +0000
committerPeter Holm <pho@FreeBSD.org>2021-03-03 14:11:40 +0000
commit8a272653d9fbd9fc37691c9aad6a05089b4ecb4d (patch)
tree2463ed22a342700cb7ba2b05c3d5370f75090899 /tools/test
parent3adf72a36b9b151eef57e3d83f71a3a9fbacb78d (diff)
downloadsrc-8a272653d9fbd9fc37691c9aad6a05089b4ecb4d.tar.gz
src-8a272653d9fbd9fc37691c9aad6a05089b4ecb4d.zip
stress2: Initial import
Discussed with: kib
Diffstat (limited to 'tools/test')
-rw-r--r--tools/test/README1
-rw-r--r--tools/test/stress2/Makefile11
-rw-r--r--tools/test/stress2/README20
-rw-r--r--tools/test/stress2/all.cfg8
-rw-r--r--tools/test/stress2/creat.cfg8
-rw-r--r--tools/test/stress2/ddb.conf6
-rw-r--r--tools/test/stress2/default.cfg122
-rw-r--r--tools/test/stress2/df.cfg8
-rw-r--r--tools/test/stress2/disk.cfg8
-rw-r--r--tools/test/stress2/doc/README3
-rw-r--r--tools/test/stress2/doc/linuxforum06.pdfbin0 -> 523790 bytes
-rw-r--r--tools/test/stress2/doc/stress.pdfbin0 -> 39840 bytes
-rw-r--r--tools/test/stress2/include/stress.h64
-rw-r--r--tools/test/stress2/io.cfg9
-rw-r--r--tools/test/stress2/jeff.cfg8
-rw-r--r--tools/test/stress2/lib/Makefile9
-rw-r--r--tools/test/stress2/lib/main.c212
-rw-r--r--tools/test/stress2/lib/options.c268
-rw-r--r--tools/test/stress2/lib/random_int.c56
-rw-r--r--tools/test/stress2/lib/resources.c248
-rw-r--r--tools/test/stress2/link.cfg9
-rw-r--r--tools/test/stress2/load.cfg18
-rw-r--r--tools/test/stress2/lockf.cfg9
-rw-r--r--tools/test/stress2/marcus.cfg24
-rwxr-xr-xtools/test/stress2/misc/1st.sh73
-rwxr-xr-xtools/test/stress2/misc/256m.sh45
-rw-r--r--tools/test/stress2/misc/README4
-rwxr-xr-xtools/test/stress2/misc/advlock.sh245
-rwxr-xr-xtools/test/stress2/misc/aesni.sh38
-rw-r--r--tools/test/stress2/misc/all.debug.inc95
-rw-r--r--tools/test/stress2/misc/all.exclude111
-rw-r--r--tools/test/stress2/misc/all.exclude.pae11
-rwxr-xr-xtools/test/stress2/misc/all.sh255
-rwxr-xr-xtools/test/stress2/misc/altbufferflushes.sh98
-rwxr-xr-xtools/test/stress2/misc/alternativeFlushPath.sh131
-rwxr-xr-xtools/test/stress2/misc/arp.sh57
-rwxr-xr-xtools/test/stress2/misc/aslr.sh46
-rwxr-xr-xtools/test/stress2/misc/audit.sh58
-rwxr-xr-xtools/test/stress2/misc/audit2.sh59
-rwxr-xr-xtools/test/stress2/misc/backingstore.sh81
-rwxr-xr-xtools/test/stress2/misc/backingstore2.sh72
-rwxr-xr-xtools/test/stress2/misc/backingstore3.sh73
-rwxr-xr-xtools/test/stress2/misc/badcode.sh65
-rwxr-xr-xtools/test/stress2/misc/badcode2.sh57
-rwxr-xr-xtools/test/stress2/misc/badcode3.sh57
-rwxr-xr-xtools/test/stress2/misc/bench.sh300
-rwxr-xr-xtools/test/stress2/misc/beneath.sh110
-rwxr-xr-xtools/test/stress2/misc/beneath2.sh101
-rwxr-xr-xtools/test/stress2/misc/beneath3.sh92
-rwxr-xr-xtools/test/stress2/misc/beneath4.sh133
-rwxr-xr-xtools/test/stress2/misc/bio.sh225
-rwxr-xr-xtools/test/stress2/misc/buildkernel.sh63
-rwxr-xr-xtools/test/stress2/misc/buildworld.sh67
-rwxr-xr-xtools/test/stress2/misc/buildworld2.sh57
-rwxr-xr-xtools/test/stress2/misc/buildworld3.sh95
-rwxr-xr-xtools/test/stress2/misc/buildworld4.sh74
-rwxr-xr-xtools/test/stress2/misc/burnin.sh210
-rwxr-xr-xtools/test/stress2/misc/callout_reset_on.sh331
-rwxr-xr-xtools/test/stress2/misc/callout_reset_on2.sh303
-rwxr-xr-xtools/test/stress2/misc/chain.sh77
-rwxr-xr-xtools/test/stress2/misc/churn.sh211
-rwxr-xr-xtools/test/stress2/misc/cleanup.sh105
-rwxr-xr-xtools/test/stress2/misc/cluster.sh269
-rwxr-xr-xtools/test/stress2/misc/cmp.sh187
-rwxr-xr-xtools/test/stress2/misc/collapse.sh153
-rwxr-xr-xtools/test/stress2/misc/compare.sh44
-rwxr-xr-xtools/test/stress2/misc/context.sh130
-rwxr-xr-xtools/test/stress2/misc/context2.sh131
-rwxr-xr-xtools/test/stress2/misc/contigmalloc.sh255
-rwxr-xr-xtools/test/stress2/misc/contigmalloc2.sh223
-rwxr-xr-xtools/test/stress2/misc/contigmalloc3.sh225
-rwxr-xr-xtools/test/stress2/misc/core.sh77
-rwxr-xr-xtools/test/stress2/misc/core2.sh95
-rwxr-xr-xtools/test/stress2/misc/core3.sh171
-rwxr-xr-xtools/test/stress2/misc/core4.sh89
-rwxr-xr-xtools/test/stress2/misc/core5.sh199
-rwxr-xr-xtools/test/stress2/misc/cpuset.sh49
-rwxr-xr-xtools/test/stress2/misc/credleak.sh162
-rwxr-xr-xtools/test/stress2/misc/crossmp.sh89
-rwxr-xr-xtools/test/stress2/misc/crossmp10.sh104
-rwxr-xr-xtools/test/stress2/misc/crossmp11.sh121
-rwxr-xr-xtools/test/stress2/misc/crossmp2.sh86
-rwxr-xr-xtools/test/stress2/misc/crossmp3.sh108
-rwxr-xr-xtools/test/stress2/misc/crossmp4.sh107
-rwxr-xr-xtools/test/stress2/misc/crossmp5.sh95
-rwxr-xr-xtools/test/stress2/misc/crossmp6.sh103
-rwxr-xr-xtools/test/stress2/misc/crossmp7.sh100
-rwxr-xr-xtools/test/stress2/misc/crossmp8.sh125
-rwxr-xr-xtools/test/stress2/misc/crossmp9.sh90
-rwxr-xr-xtools/test/stress2/misc/dangling.sh87
-rwxr-xr-xtools/test/stress2/misc/datagram.sh96
-rwxr-xr-xtools/test/stress2/misc/datagram2.sh104
-rwxr-xr-xtools/test/stress2/misc/datagram3.sh106
-rwxr-xr-xtools/test/stress2/misc/datamove.sh230
-rwxr-xr-xtools/test/stress2/misc/datamove2.sh244
-rwxr-xr-xtools/test/stress2/misc/datamove3.sh241
-rwxr-xr-xtools/test/stress2/misc/datamove4.sh251
-rwxr-xr-xtools/test/stress2/misc/datamove5.sh262
-rwxr-xr-xtools/test/stress2/misc/db.sh178
-rwxr-xr-xtools/test/stress2/misc/dd.sh64
-rwxr-xr-xtools/test/stress2/misc/dev.sh137
-rwxr-xr-xtools/test/stress2/misc/dev2.sh144
-rwxr-xr-xtools/test/stress2/misc/dev3.sh160
-rwxr-xr-xtools/test/stress2/misc/devfd.sh130
-rwxr-xr-xtools/test/stress2/misc/devfs.sh72
-rwxr-xr-xtools/test/stress2/misc/devfs2.sh107
-rwxr-xr-xtools/test/stress2/misc/devfs3.sh45
-rwxr-xr-xtools/test/stress2/misc/devfs4.sh35
-rwxr-xr-xtools/test/stress2/misc/devfs5.sh68
-rwxr-xr-xtools/test/stress2/misc/dfull.sh56
-rwxr-xr-xtools/test/stress2/misc/dtrace.sh68
-rwxr-xr-xtools/test/stress2/misc/dtrace_fault.sh65
-rwxr-xr-xtools/test/stress2/misc/dumpfs.sh73
-rwxr-xr-xtools/test/stress2/misc/dup.sh122
-rwxr-xr-xtools/test/stress2/misc/dup2.sh71
-rwxr-xr-xtools/test/stress2/misc/execi386.sh86
-rwxr-xr-xtools/test/stress2/misc/execve.sh130
-rwxr-xr-xtools/test/stress2/misc/exlock.sh122
-rwxr-xr-xtools/test/stress2/misc/exlock2.sh210
-rwxr-xr-xtools/test/stress2/misc/ext2fs.sh57
-rwxr-xr-xtools/test/stress2/misc/ext2fs2.sh55
-rwxr-xr-xtools/test/stress2/misc/ext2fs3.sh68
-rwxr-xr-xtools/test/stress2/misc/ext3fs.sh61
-rwxr-xr-xtools/test/stress2/misc/ext4fs.sh64
-rwxr-xr-xtools/test/stress2/misc/extattr.sh95
-rwxr-xr-xtools/test/stress2/misc/extattr2.sh94
-rwxr-xr-xtools/test/stress2/misc/extattr3.sh62
-rwxr-xr-xtools/test/stress2/misc/extattr_set_fd.sh79
-rwxr-xr-xtools/test/stress2/misc/extattrctl.sh73
-rwxr-xr-xtools/test/stress2/misc/f_offset.sh160
-rwxr-xr-xtools/test/stress2/misc/fcntl.sh234
-rwxr-xr-xtools/test/stress2/misc/fcntl2.sh188
-rwxr-xr-xtools/test/stress2/misc/fcntl3.sh56
-rwxr-xr-xtools/test/stress2/misc/fdatasync.sh197
-rwxr-xr-xtools/test/stress2/misc/fdatasync2.sh196
-rwxr-xr-xtools/test/stress2/misc/fdescfs.sh74
-rwxr-xr-xtools/test/stress2/misc/fdgrowtable.sh105
-rwxr-xr-xtools/test/stress2/misc/fexecve.sh94
-rwxr-xr-xtools/test/stress2/misc/ffs_blkfree.sh76
-rwxr-xr-xtools/test/stress2/misc/ffs_sync.sh254
-rwxr-xr-xtools/test/stress2/misc/ffs_syncvnode.sh59
-rwxr-xr-xtools/test/stress2/misc/ffs_syncvnode2.sh63
-rwxr-xr-xtools/test/stress2/misc/fifo.sh87
-rwxr-xr-xtools/test/stress2/misc/fifo2.sh206
-rwxr-xr-xtools/test/stress2/misc/fifo3.sh209
-rwxr-xr-xtools/test/stress2/misc/fifo4.sh80
-rwxr-xr-xtools/test/stress2/misc/flock.sh191
-rwxr-xr-xtools/test/stress2/misc/flock_open_close.sh174
-rwxr-xr-xtools/test/stress2/misc/force.sh70
-rwxr-xr-xtools/test/stress2/misc/force2.sh79
-rwxr-xr-xtools/test/stress2/misc/force3.sh65
-rwxr-xr-xtools/test/stress2/misc/force4.sh91
-rwxr-xr-xtools/test/stress2/misc/force5.sh74
-rwxr-xr-xtools/test/stress2/misc/force6.sh79
-rwxr-xr-xtools/test/stress2/misc/fork.sh129
-rwxr-xr-xtools/test/stress2/misc/forkbomb.sh148
-rwxr-xr-xtools/test/stress2/misc/fpclone.sh192
-rwxr-xr-xtools/test/stress2/misc/fpclone2.sh117
-rwxr-xr-xtools/test/stress2/misc/fpu.sh103
-rwxr-xr-xtools/test/stress2/misc/fragments.sh275
-rwxr-xr-xtools/test/stress2/misc/freepages.sh130
-rwxr-xr-xtools/test/stress2/misc/fs.sh91
-rwxr-xr-xtools/test/stress2/misc/fsck.sh149
-rwxr-xr-xtools/test/stress2/misc/fsck2.sh38
-rwxr-xr-xtools/test/stress2/misc/fsck3.sh88
-rwxr-xr-xtools/test/stress2/misc/fsck4.sh67
-rwxr-xr-xtools/test/stress2/misc/fsck5.sh72
-rwxr-xr-xtools/test/stress2/misc/fsck6.sh80
-rwxr-xr-xtools/test/stress2/misc/fsgs.sh135
-rwxr-xr-xtools/test/stress2/misc/fstat.sh178
-rwxr-xr-xtools/test/stress2/misc/fsync.sh91
-rwxr-xr-xtools/test/stress2/misc/fsync2.sh138
-rwxr-xr-xtools/test/stress2/misc/ftruncate.sh189
-rwxr-xr-xtools/test/stress2/misc/ftruncate2.sh205
-rwxr-xr-xtools/test/stress2/misc/fts.sh144
-rwxr-xr-xtools/test/stress2/misc/fts2.sh285
-rwxr-xr-xtools/test/stress2/misc/fts3.sh133
-rwxr-xr-xtools/test/stress2/misc/full.sh67
-rwxr-xr-xtools/test/stress2/misc/fullpath.sh111
-rwxr-xr-xtools/test/stress2/misc/fullpath2.sh214
-rwxr-xr-xtools/test/stress2/misc/fuse.sh57
-rwxr-xr-xtools/test/stress2/misc/fuse2.sh89
-rwxr-xr-xtools/test/stress2/misc/fuse3.sh42
-rwxr-xr-xtools/test/stress2/misc/fuzz.sh180
-rwxr-xr-xtools/test/stress2/misc/gbde.sh55
-rwxr-xr-xtools/test/stress2/misc/geli.sh68
-rwxr-xr-xtools/test/stress2/misc/geomleak.sh52
-rwxr-xr-xtools/test/stress2/misc/geomleak2.sh57
-rwxr-xr-xtools/test/stress2/misc/getrandom.sh142
-rwxr-xr-xtools/test/stress2/misc/getrandom2.sh188
-rwxr-xr-xtools/test/stress2/misc/gjournal.sh76
-rwxr-xr-xtools/test/stress2/misc/gjournal2.sh63
-rwxr-xr-xtools/test/stress2/misc/gjournal3.sh85
-rwxr-xr-xtools/test/stress2/misc/gjournal4.sh65
-rwxr-xr-xtools/test/stress2/misc/gnop.sh77
-rwxr-xr-xtools/test/stress2/misc/gnop10.sh139
-rwxr-xr-xtools/test/stress2/misc/gnop2.sh80
-rwxr-xr-xtools/test/stress2/misc/gnop3.sh73
-rwxr-xr-xtools/test/stress2/misc/gnop4.sh79
-rwxr-xr-xtools/test/stress2/misc/gnop5.sh69
-rwxr-xr-xtools/test/stress2/misc/gnop6.sh68
-rwxr-xr-xtools/test/stress2/misc/gnop7.sh80
-rwxr-xr-xtools/test/stress2/misc/gnop8.sh68
-rwxr-xr-xtools/test/stress2/misc/gnop9.sh139
-rwxr-xr-xtools/test/stress2/misc/gpt.sh51
-rwxr-xr-xtools/test/stress2/misc/graid0.sh71
-rwxr-xr-xtools/test/stress2/misc/graid1.sh80
-rwxr-xr-xtools/test/stress2/misc/graid1_10.sh96
-rwxr-xr-xtools/test/stress2/misc/graid1_2.sh131
-rwxr-xr-xtools/test/stress2/misc/graid1_3.sh93
-rwxr-xr-xtools/test/stress2/misc/graid1_4.sh97
-rwxr-xr-xtools/test/stress2/misc/graid1_5.sh116
-rwxr-xr-xtools/test/stress2/misc/graid1_6.sh106
-rwxr-xr-xtools/test/stress2/misc/graid1_7.sh127
-rwxr-xr-xtools/test/stress2/misc/graid1_8.sh102
-rwxr-xr-xtools/test/stress2/misc/graid1_9.sh114
-rwxr-xr-xtools/test/stress2/misc/graid3.sh71
-rwxr-xr-xtools/test/stress2/misc/growfs.sh63
-rwxr-xr-xtools/test/stress2/misc/holdcnt0.sh261
-rwxr-xr-xtools/test/stress2/misc/holdcnt02.sh238
-rwxr-xr-xtools/test/stress2/misc/holdcnt03.sh237
-rwxr-xr-xtools/test/stress2/misc/holdcnt04.sh248
-rwxr-xr-xtools/test/stress2/misc/holdcnt05.sh249
-rwxr-xr-xtools/test/stress2/misc/ifconfig.sh60
-rwxr-xr-xtools/test/stress2/misc/ifconfig2.sh67
-rwxr-xr-xtools/test/stress2/misc/indir.sh201
-rwxr-xr-xtools/test/stress2/misc/indir_trunc.sh231
-rwxr-xr-xtools/test/stress2/misc/inversion.sh97
-rwxr-xr-xtools/test/stress2/misc/isofs.sh60
-rwxr-xr-xtools/test/stress2/misc/isofs2.sh58
-rwxr-xr-xtools/test/stress2/misc/isofs3.sh66
-rwxr-xr-xtools/test/stress2/misc/jail.sh66
-rwxr-xr-xtools/test/stress2/misc/jail2.sh76
-rwxr-xr-xtools/test/stress2/misc/jail3.sh76
-rwxr-xr-xtools/test/stress2/misc/jail4.sh78
-rwxr-xr-xtools/test/stress2/misc/jexec.sh67
-rwxr-xr-xtools/test/stress2/misc/jumbo.sh221
-rwxr-xr-xtools/test/stress2/misc/kern_umtx_inf_loop.sh148
-rwxr-xr-xtools/test/stress2/misc/kevent.sh174
-rwxr-xr-xtools/test/stress2/misc/kevent10.sh96
-rwxr-xr-xtools/test/stress2/misc/kevent11.sh178
-rwxr-xr-xtools/test/stress2/misc/kevent12.sh233
-rwxr-xr-xtools/test/stress2/misc/kevent13.sh141
-rwxr-xr-xtools/test/stress2/misc/kevent14.sh77
-rwxr-xr-xtools/test/stress2/misc/kevent15.sh161
-rwxr-xr-xtools/test/stress2/misc/kevent2.sh170
-rwxr-xr-xtools/test/stress2/misc/kevent3.sh74
-rwxr-xr-xtools/test/stress2/misc/kevent4.sh174
-rwxr-xr-xtools/test/stress2/misc/kevent5.sh183
-rwxr-xr-xtools/test/stress2/misc/kevent6.sh168
-rwxr-xr-xtools/test/stress2/misc/kevent7.sh281
-rwxr-xr-xtools/test/stress2/misc/kevent8.sh163
-rwxr-xr-xtools/test/stress2/misc/kevent9.sh206
-rwxr-xr-xtools/test/stress2/misc/killpg.sh155
-rwxr-xr-xtools/test/stress2/misc/kinfo.sh162
-rwxr-xr-xtools/test/stress2/misc/kinfo2.sh186
-rwxr-xr-xtools/test/stress2/misc/kinfo3.sh197
-rwxr-xr-xtools/test/stress2/misc/kpti.sh50
-rwxr-xr-xtools/test/stress2/misc/largepage.sh152
-rwxr-xr-xtools/test/stress2/misc/laundry.sh177
-rwxr-xr-xtools/test/stress2/misc/ldt.sh370
-rwxr-xr-xtools/test/stress2/misc/ldt2.sh99
-rwxr-xr-xtools/test/stress2/misc/libMicro.sh53
-rwxr-xr-xtools/test/stress2/misc/linger.sh168
-rwxr-xr-xtools/test/stress2/misc/linger2.sh172
-rwxr-xr-xtools/test/stress2/misc/linger3.sh134
-rwxr-xr-xtools/test/stress2/misc/linger4.sh155
-rwxr-xr-xtools/test/stress2/misc/link.sh193
-rwxr-xr-xtools/test/stress2/misc/link2.sh78
-rwxr-xr-xtools/test/stress2/misc/linux.sh55
-rwxr-xr-xtools/test/stress2/misc/lockd.sh70
-rwxr-xr-xtools/test/stress2/misc/lockf.sh73
-rwxr-xr-xtools/test/stress2/misc/lockf2.sh43
-rwxr-xr-xtools/test/stress2/misc/lockf3.sh163
-rwxr-xr-xtools/test/stress2/misc/lockf4.sh183
-rwxr-xr-xtools/test/stress2/misc/lockf5.sh61
-rwxr-xr-xtools/test/stress2/misc/lookup_shared.sh38
-rwxr-xr-xtools/test/stress2/misc/lstat.sh236
-rwxr-xr-xtools/test/stress2/misc/mac.sh38
-rwxr-xr-xtools/test/stress2/misc/mac_chkexec.sh57
-rwxr-xr-xtools/test/stress2/misc/machipc.sh184
-rwxr-xr-xtools/test/stress2/misc/machipc2.sh292
-rwxr-xr-xtools/test/stress2/misc/marcus.sh55
-rwxr-xr-xtools/test/stress2/misc/marcus2.sh73
-rwxr-xr-xtools/test/stress2/misc/marcus3.sh52
-rwxr-xr-xtools/test/stress2/misc/marcus4.sh86
-rwxr-xr-xtools/test/stress2/misc/marcus5.sh85
-rwxr-xr-xtools/test/stress2/misc/marcus6.sh83
-rwxr-xr-xtools/test/stress2/misc/marcus7.sh90
-rwxr-xr-xtools/test/stress2/misc/maxmemdom.sh52
-rwxr-xr-xtools/test/stress2/misc/maxproc.sh167
-rwxr-xr-xtools/test/stress2/misc/maxvnodes.sh70
-rwxr-xr-xtools/test/stress2/misc/maxvnodes2.sh42
-rwxr-xr-xtools/test/stress2/misc/md.sh58
-rwxr-xr-xtools/test/stress2/misc/md2.sh57
-rwxr-xr-xtools/test/stress2/misc/md3.sh56
-rwxr-xr-xtools/test/stress2/misc/md4.sh43
-rwxr-xr-xtools/test/stress2/misc/md5.sh48
-rwxr-xr-xtools/test/stress2/misc/md6.sh44
-rwxr-xr-xtools/test/stress2/misc/md7.sh39
-rwxr-xr-xtools/test/stress2/misc/md8.sh121
-rwxr-xr-xtools/test/stress2/misc/md9.sh59
-rwxr-xr-xtools/test/stress2/misc/mdconfig.sh39
-rwxr-xr-xtools/test/stress2/misc/mdconfig2.sh53
-rwxr-xr-xtools/test/stress2/misc/mdconfig3.sh46
-rwxr-xr-xtools/test/stress2/misc/memguard.sh65
-rwxr-xr-xtools/test/stress2/misc/memguard2.sh68
-rwxr-xr-xtools/test/stress2/misc/memguard3.sh63
-rwxr-xr-xtools/test/stress2/misc/memguard4.sh61
-rwxr-xr-xtools/test/stress2/misc/midi.sh94
-rwxr-xr-xtools/test/stress2/misc/midi2.sh114
-rwxr-xr-xtools/test/stress2/misc/mincore.sh157
-rwxr-xr-xtools/test/stress2/misc/minherit.sh176
-rwxr-xr-xtools/test/stress2/misc/mkfifo.sh86
-rwxr-xr-xtools/test/stress2/misc/mkfifo2c.sh89
-rwxr-xr-xtools/test/stress2/misc/mkfifo2d.sh81
-rwxr-xr-xtools/test/stress2/misc/mkfifo3.sh139
-rwxr-xr-xtools/test/stress2/misc/mkfifo4.sh186
-rwxr-xr-xtools/test/stress2/misc/mkfifo5.sh214
-rwxr-xr-xtools/test/stress2/misc/mkfifo6.sh183
-rwxr-xr-xtools/test/stress2/misc/mkfifo7.sh203
-rwxr-xr-xtools/test/stress2/misc/mkfifo8.sh200
-rwxr-xr-xtools/test/stress2/misc/mknod.sh163
-rwxr-xr-xtools/test/stress2/misc/mlockall.sh80
-rwxr-xr-xtools/test/stress2/misc/mlockall2.sh165
-rwxr-xr-xtools/test/stress2/misc/mlockall3.sh167
-rwxr-xr-xtools/test/stress2/misc/mlockall4.sh66
-rwxr-xr-xtools/test/stress2/misc/mlockall5.sh162
-rwxr-xr-xtools/test/stress2/misc/mlockall6.sh203
-rwxr-xr-xtools/test/stress2/misc/mlockall7.sh262
-rwxr-xr-xtools/test/stress2/misc/mmap.sh63
-rwxr-xr-xtools/test/stress2/misc/mmap10.sh273
-rwxr-xr-xtools/test/stress2/misc/mmap11.sh301
-rwxr-xr-xtools/test/stress2/misc/mmap12.sh81
-rwxr-xr-xtools/test/stress2/misc/mmap13.sh81
-rwxr-xr-xtools/test/stress2/misc/mmap14.sh252
-rwxr-xr-xtools/test/stress2/misc/mmap15.sh223
-rwxr-xr-xtools/test/stress2/misc/mmap16.sh145
-rwxr-xr-xtools/test/stress2/misc/mmap17.sh86
-rwxr-xr-xtools/test/stress2/misc/mmap18.sh308
-rwxr-xr-xtools/test/stress2/misc/mmap19.sh241
-rwxr-xr-xtools/test/stress2/misc/mmap2.sh177
-rwxr-xr-xtools/test/stress2/misc/mmap20.sh83
-rwxr-xr-xtools/test/stress2/misc/mmap21.sh165
-rwxr-xr-xtools/test/stress2/misc/mmap22.sh130
-rwxr-xr-xtools/test/stress2/misc/mmap23.sh108
-rwxr-xr-xtools/test/stress2/misc/mmap24.sh99
-rwxr-xr-xtools/test/stress2/misc/mmap25.sh120
-rwxr-xr-xtools/test/stress2/misc/mmap26.sh123
-rwxr-xr-xtools/test/stress2/misc/mmap27.sh126
-rwxr-xr-xtools/test/stress2/misc/mmap28.sh131
-rwxr-xr-xtools/test/stress2/misc/mmap29.sh71
-rwxr-xr-xtools/test/stress2/misc/mmap3.sh167
-rwxr-xr-xtools/test/stress2/misc/mmap30.sh77
-rwxr-xr-xtools/test/stress2/misc/mmap31.sh174
-rwxr-xr-xtools/test/stress2/misc/mmap32.sh202
-rwxr-xr-xtools/test/stress2/misc/mmap33.sh121
-rwxr-xr-xtools/test/stress2/misc/mmap34.sh230
-rwxr-xr-xtools/test/stress2/misc/mmap35.sh112
-rwxr-xr-xtools/test/stress2/misc/mmap36.sh80
-rwxr-xr-xtools/test/stress2/misc/mmap37.sh153
-rwxr-xr-xtools/test/stress2/misc/mmap38.sh42
-rwxr-xr-xtools/test/stress2/misc/mmap39.sh131
-rwxr-xr-xtools/test/stress2/misc/mmap4.sh116
-rwxr-xr-xtools/test/stress2/misc/mmap40.sh159
-rwxr-xr-xtools/test/stress2/misc/mmap5.sh129
-rwxr-xr-xtools/test/stress2/misc/mmap6.sh179
-rwxr-xr-xtools/test/stress2/misc/mmap7.sh147
-rwxr-xr-xtools/test/stress2/misc/mmap8.sh89
-rwxr-xr-xtools/test/stress2/misc/mmap9.sh95
-rwxr-xr-xtools/test/stress2/misc/mount.sh82
-rwxr-xr-xtools/test/stress2/misc/mount2.sh73
-rwxr-xr-xtools/test/stress2/misc/mountro.sh67
-rwxr-xr-xtools/test/stress2/misc/mountro2.sh54
-rwxr-xr-xtools/test/stress2/misc/mountro3.sh56
-rwxr-xr-xtools/test/stress2/misc/mountro4.sh100
-rwxr-xr-xtools/test/stress2/misc/mountro5.sh63
-rwxr-xr-xtools/test/stress2/misc/mountro6.sh57
-rwxr-xr-xtools/test/stress2/misc/mountu.sh284
-rwxr-xr-xtools/test/stress2/misc/mprotect.sh75
-rwxr-xr-xtools/test/stress2/misc/mprotect2.sh97
-rwxr-xr-xtools/test/stress2/misc/msdos.sh60
-rwxr-xr-xtools/test/stress2/misc/msdos10.sh89
-rwxr-xr-xtools/test/stress2/misc/msdos2.sh58
-rwxr-xr-xtools/test/stress2/misc/msdos3.sh52
-rwxr-xr-xtools/test/stress2/misc/msdos4.sh76
-rwxr-xr-xtools/test/stress2/misc/msdos5.sh59
-rwxr-xr-xtools/test/stress2/misc/msdos6.sh93
-rwxr-xr-xtools/test/stress2/misc/msdos7.sh57
-rwxr-xr-xtools/test/stress2/misc/msdos8.sh157
-rwxr-xr-xtools/test/stress2/misc/msdos9.sh77
-rwxr-xr-xtools/test/stress2/misc/msetdomain.sh144
-rwxr-xr-xtools/test/stress2/misc/msync.sh201
-rwxr-xr-xtools/test/stress2/misc/msync2.sh131
-rwxr-xr-xtools/test/stress2/misc/multicast.sh118
-rwxr-xr-xtools/test/stress2/misc/multicast2.sh26
-rwxr-xr-xtools/test/stress2/misc/namecache.sh214
-rwxr-xr-xtools/test/stress2/misc/namecache2.sh192
-rwxr-xr-xtools/test/stress2/misc/nanosleep.sh87
-rwxr-xr-xtools/test/stress2/misc/nbufkv.sh156
-rwxr-xr-xtools/test/stress2/misc/newfs.sh94
-rwxr-xr-xtools/test/stress2/misc/newfs2.sh69
-rwxr-xr-xtools/test/stress2/misc/newfs3.sh81
-rwxr-xr-xtools/test/stress2/misc/newfs4.sh128
-rwxr-xr-xtools/test/stress2/misc/newfs5.sh116
-rwxr-xr-xtools/test/stress2/misc/nfs.sh55
-rwxr-xr-xtools/test/stress2/misc/nfs10.sh73
-rwxr-xr-xtools/test/stress2/misc/nfs11.sh67
-rwxr-xr-xtools/test/stress2/misc/nfs12.sh160
-rwxr-xr-xtools/test/stress2/misc/nfs13.sh66
-rwxr-xr-xtools/test/stress2/misc/nfs14.sh74
-rwxr-xr-xtools/test/stress2/misc/nfs15.sh212
-rwxr-xr-xtools/test/stress2/misc/nfs15lockd.sh240
-rwxr-xr-xtools/test/stress2/misc/nfs15lockd2.sh41
-rwxr-xr-xtools/test/stress2/misc/nfs15lockd3.sh250
-rwxr-xr-xtools/test/stress2/misc/nfs16.sh196
-rwxr-xr-xtools/test/stress2/misc/nfs17.sh76
-rwxr-xr-xtools/test/stress2/misc/nfs2.sh69
-rwxr-xr-xtools/test/stress2/misc/nfs3.sh60
-rwxr-xr-xtools/test/stress2/misc/nfs4.sh70
-rwxr-xr-xtools/test/stress2/misc/nfs5.sh68
-rwxr-xr-xtools/test/stress2/misc/nfs6.sh81
-rwxr-xr-xtools/test/stress2/misc/nfs7.sh68
-rwxr-xr-xtools/test/stress2/misc/nfs8.sh66
-rwxr-xr-xtools/test/stress2/misc/nfs9.sh88
-rwxr-xr-xtools/test/stress2/misc/nfs_halfpage.sh99
-rwxr-xr-xtools/test/stress2/misc/nfs_halfpage2.sh103
-rwxr-xr-xtools/test/stress2/misc/nfsdelegation.sh169
-rwxr-xr-xtools/test/stress2/misc/nfsdepth.sh239
-rwxr-xr-xtools/test/stress2/misc/nfsrename.sh224
-rwxr-xr-xtools/test/stress2/misc/nfssillyrename.sh67
-rwxr-xr-xtools/test/stress2/misc/nullfs.sh72
-rwxr-xr-xtools/test/stress2/misc/nullfs10.sh71
-rwxr-xr-xtools/test/stress2/misc/nullfs11.sh89
-rwxr-xr-xtools/test/stress2/misc/nullfs12.sh64
-rwxr-xr-xtools/test/stress2/misc/nullfs13.sh77
-rwxr-xr-xtools/test/stress2/misc/nullfs14.sh58
-rwxr-xr-xtools/test/stress2/misc/nullfs15.sh64
-rwxr-xr-xtools/test/stress2/misc/nullfs16.sh61
-rwxr-xr-xtools/test/stress2/misc/nullfs17.sh84
-rwxr-xr-xtools/test/stress2/misc/nullfs18.sh133
-rwxr-xr-xtools/test/stress2/misc/nullfs19.sh90
-rwxr-xr-xtools/test/stress2/misc/nullfs2.sh54
-rwxr-xr-xtools/test/stress2/misc/nullfs20.sh74
-rwxr-xr-xtools/test/stress2/misc/nullfs21.sh71
-rwxr-xr-xtools/test/stress2/misc/nullfs22.sh247
-rwxr-xr-xtools/test/stress2/misc/nullfs23.sh50
-rwxr-xr-xtools/test/stress2/misc/nullfs24.sh69
-rwxr-xr-xtools/test/stress2/misc/nullfs25.sh90
-rwxr-xr-xtools/test/stress2/misc/nullfs26.sh47
-rwxr-xr-xtools/test/stress2/misc/nullfs27.sh91
-rwxr-xr-xtools/test/stress2/misc/nullfs3.sh56
-rwxr-xr-xtools/test/stress2/misc/nullfs4.sh50
-rwxr-xr-xtools/test/stress2/misc/nullfs5.sh79
-rwxr-xr-xtools/test/stress2/misc/nullfs6.sh50
-rwxr-xr-xtools/test/stress2/misc/nullfs7.sh63
-rwxr-xr-xtools/test/stress2/misc/nullfs8.sh67
-rwxr-xr-xtools/test/stress2/misc/nullfs9.sh82
-rwxr-xr-xtools/test/stress2/misc/numa.sh46
-rwxr-xr-xtools/test/stress2/misc/oom.sh47
-rwxr-xr-xtools/test/stress2/misc/oom2.sh49
-rwxr-xr-xtools/test/stress2/misc/oovm.sh134
-rwxr-xr-xtools/test/stress2/misc/oovm2.sh136
-rwxr-xr-xtools/test/stress2/misc/open.sh84
-rwxr-xr-xtools/test/stress2/misc/openlock.sh94
-rwxr-xr-xtools/test/stress2/misc/overcommit.sh62
-rwxr-xr-xtools/test/stress2/misc/overcommit2.sh65
-rwxr-xr-xtools/test/stress2/misc/overflow3.sh96
-rwxr-xr-xtools/test/stress2/misc/overlap.sh160
-rwxr-xr-xtools/test/stress2/misc/pageout.sh144
-rwxr-xr-xtools/test/stress2/misc/parallelmount.sh68
-rwxr-xr-xtools/test/stress2/misc/parallelmount2.sh84
-rwxr-xr-xtools/test/stress2/misc/pathconf.sh56
-rwxr-xr-xtools/test/stress2/misc/pathconf2.sh37
-rwxr-xr-xtools/test/stress2/misc/pause.sh159
-rwxr-xr-xtools/test/stress2/misc/pcatch.sh154
-rwxr-xr-xtools/test/stress2/misc/pcatch2.sh44
-rwxr-xr-xtools/test/stress2/misc/pdfork.sh58
-rwxr-xr-xtools/test/stress2/misc/perf.sh151
-rwxr-xr-xtools/test/stress2/misc/pfl.sh188
-rwxr-xr-xtools/test/stress2/misc/pfl2.sh104
-rwxr-xr-xtools/test/stress2/misc/pfl3.sh47
-rwxr-xr-xtools/test/stress2/misc/pfl4.sh95
-rwxr-xr-xtools/test/stress2/misc/ping.sh53
-rwxr-xr-xtools/test/stress2/misc/pipe.sh132
-rwxr-xr-xtools/test/stress2/misc/pipe2.sh163
-rwxr-xr-xtools/test/stress2/misc/pipe3.sh148
-rwxr-xr-xtools/test/stress2/misc/pipe_enomem.sh102
-rwxr-xr-xtools/test/stress2/misc/pkru.sh512
-rwxr-xr-xtools/test/stress2/misc/pkru2.sh108
-rwxr-xr-xtools/test/stress2/misc/pmc.sh55
-rwxr-xr-xtools/test/stress2/misc/pmc2.sh51
-rwxr-xr-xtools/test/stress2/misc/pmc3.sh44
-rwxr-xr-xtools/test/stress2/misc/pmc4.sh124
-rwxr-xr-xtools/test/stress2/misc/pmc5.sh138
-rwxr-xr-xtools/test/stress2/misc/pmc6.sh41
-rwxr-xr-xtools/test/stress2/misc/pmc7.sh12
-rwxr-xr-xtools/test/stress2/misc/poll.sh57
-rwxr-xr-xtools/test/stress2/misc/poll2.sh212
-rwxr-xr-xtools/test/stress2/misc/posix_fadvise.sh72
-rwxr-xr-xtools/test/stress2/misc/posix_fadvise2.sh78
-rwxr-xr-xtools/test/stress2/misc/posix_fadvise3.sh120
-rwxr-xr-xtools/test/stress2/misc/posix_openpt.sh79
-rwxr-xr-xtools/test/stress2/misc/posix_openpt2.sh152
-rwxr-xr-xtools/test/stress2/misc/pread.sh190
-rwxr-xr-xtools/test/stress2/misc/proccontrol.sh54
-rwxr-xr-xtools/test/stress2/misc/procfs.sh69
-rwxr-xr-xtools/test/stress2/misc/procfs2.sh37
-rwxr-xr-xtools/test/stress2/misc/procfs3.sh154
-rwxr-xr-xtools/test/stress2/misc/procfs4.sh156
-rwxr-xr-xtools/test/stress2/misc/procfs5.sh84
-rwxr-xr-xtools/test/stress2/misc/procfs6.sh42
-rwxr-xr-xtools/test/stress2/misc/procstat.sh44
-rwxr-xr-xtools/test/stress2/misc/procstat2.sh44
-rwxr-xr-xtools/test/stress2/misc/pthread.sh137
-rwxr-xr-xtools/test/stress2/misc/pthread2.sh296
-rwxr-xr-xtools/test/stress2/misc/pthread3.sh304
-rwxr-xr-xtools/test/stress2/misc/pthread4.sh296
-rwxr-xr-xtools/test/stress2/misc/pthread5.sh121
-rwxr-xr-xtools/test/stress2/misc/pthread6.sh308
-rwxr-xr-xtools/test/stress2/misc/pthread7.sh285
-rwxr-xr-xtools/test/stress2/misc/pthread8.sh137
-rwxr-xr-xtools/test/stress2/misc/pthread9.sh185
-rwxr-xr-xtools/test/stress2/misc/ptrace.sh106
-rwxr-xr-xtools/test/stress2/misc/ptrace10.sh161
-rwxr-xr-xtools/test/stress2/misc/ptrace11.sh124
-rwxr-xr-xtools/test/stress2/misc/ptrace2.sh143
-rwxr-xr-xtools/test/stress2/misc/ptrace3.sh130
-rwxr-xr-xtools/test/stress2/misc/ptrace4.sh58
-rwxr-xr-xtools/test/stress2/misc/ptrace5.sh200
-rwxr-xr-xtools/test/stress2/misc/ptrace6.sh204
-rwxr-xr-xtools/test/stress2/misc/ptrace7.sh238
-rwxr-xr-xtools/test/stress2/misc/ptrace8.sh122
-rwxr-xr-xtools/test/stress2/misc/ptrace9.sh133
-rwxr-xr-xtools/test/stress2/misc/pts.sh154
-rwxr-xr-xtools/test/stress2/misc/pts2.sh79
-rwxr-xr-xtools/test/stress2/misc/pts3.sh102
-rwxr-xr-xtools/test/stress2/misc/pty.sh161
-rwxr-xr-xtools/test/stress2/misc/pty2.sh101
-rwxr-xr-xtools/test/stress2/misc/quota1.sh57
-rwxr-xr-xtools/test/stress2/misc/quota10.sh105
-rwxr-xr-xtools/test/stress2/misc/quota11.sh67
-rwxr-xr-xtools/test/stress2/misc/quota12.sh80
-rwxr-xr-xtools/test/stress2/misc/quota2.sh57
-rwxr-xr-xtools/test/stress2/misc/quota3.sh57
-rwxr-xr-xtools/test/stress2/misc/quota4.sh65
-rwxr-xr-xtools/test/stress2/misc/quota5.sh44
-rwxr-xr-xtools/test/stress2/misc/quota6.sh69
-rwxr-xr-xtools/test/stress2/misc/quota7.sh79
-rwxr-xr-xtools/test/stress2/misc/quota8.sh89
-rwxr-xr-xtools/test/stress2/misc/quota9.sh93
-rwxr-xr-xtools/test/stress2/misc/r335171.sh158
-rwxr-xr-xtools/test/stress2/misc/racct.sh57
-rwxr-xr-xtools/test/stress2/misc/radix.sh271
-rwxr-xr-xtools/test/stress2/misc/random.sh37
-rwxr-xr-xtools/test/stress2/misc/rdgsbase.sh182
-rwxr-xr-xtools/test/stress2/misc/rdwr.sh118
-rwxr-xr-xtools/test/stress2/misc/readdir.sh179
-rwxr-xr-xtools/test/stress2/misc/recursiveflushes.sh75
-rwxr-xr-xtools/test/stress2/misc/rename.sh130
-rwxr-xr-xtools/test/stress2/misc/rename10.sh184
-rwxr-xr-xtools/test/stress2/misc/rename11.sh139
-rwxr-xr-xtools/test/stress2/misc/rename12.sh192
-rwxr-xr-xtools/test/stress2/misc/rename13.sh60
-rwxr-xr-xtools/test/stress2/misc/rename14.sh198
-rwxr-xr-xtools/test/stress2/misc/rename15.sh82
-rwxr-xr-xtools/test/stress2/misc/rename2.sh103
-rwxr-xr-xtools/test/stress2/misc/rename3.sh51
-rwxr-xr-xtools/test/stress2/misc/rename4.sh62
-rwxr-xr-xtools/test/stress2/misc/rename5.sh137
-rwxr-xr-xtools/test/stress2/misc/rename6.sh149
-rwxr-xr-xtools/test/stress2/misc/rename7.sh150
-rwxr-xr-xtools/test/stress2/misc/rename8.sh173
-rwxr-xr-xtools/test/stress2/misc/rename9.sh169
-rwxr-xr-xtools/test/stress2/misc/revoke.sh116
-rwxr-xr-xtools/test/stress2/misc/rot.sh142
-rwxr-xr-xtools/test/stress2/misc/routetbl.sh61
-rwxr-xr-xtools/test/stress2/misc/ruby.sh38
-rwxr-xr-xtools/test/stress2/misc/rw.sh65
-rwxr-xr-xtools/test/stress2/misc/rwlock_ronly.sh82
-rwxr-xr-xtools/test/stress2/misc/sched.sh161
-rwxr-xr-xtools/test/stress2/misc/schedfuzz.sh79
-rwxr-xr-xtools/test/stress2/misc/sctp.sh158
-rwxr-xr-xtools/test/stress2/misc/sctp2.sh169
-rwxr-xr-xtools/test/stress2/misc/sctp3.sh172
-rwxr-xr-xtools/test/stress2/misc/seekdir.sh157
-rwxr-xr-xtools/test/stress2/misc/segnp.sh57
-rwxr-xr-xtools/test/stress2/misc/segregs.sh144
-rwxr-xr-xtools/test/stress2/misc/select.sh168
-rwxr-xr-xtools/test/stress2/misc/select3.sh39
-rwxr-xr-xtools/test/stress2/misc/selfd.sh152
-rwxr-xr-xtools/test/stress2/misc/sem.sh142
-rwxr-xr-xtools/test/stress2/misc/sem_post.sh107
-rwxr-xr-xtools/test/stress2/misc/sem_timedwait.sh161
-rwxr-xr-xtools/test/stress2/misc/sem_wait.sh109
-rwxr-xr-xtools/test/stress2/misc/sendfile.sh213
-rwxr-xr-xtools/test/stress2/misc/sendfile10.sh313
-rwxr-xr-xtools/test/stress2/misc/sendfile11.sh235
-rwxr-xr-xtools/test/stress2/misc/sendfile12.sh241
-rwxr-xr-xtools/test/stress2/misc/sendfile13.sh371
-rwxr-xr-xtools/test/stress2/misc/sendfile14.sh382
-rwxr-xr-xtools/test/stress2/misc/sendfile15.sh218
-rwxr-xr-xtools/test/stress2/misc/sendfile16.sh224
-rwxr-xr-xtools/test/stress2/misc/sendfile17.sh225
-rwxr-xr-xtools/test/stress2/misc/sendfile18.sh125
-rwxr-xr-xtools/test/stress2/misc/sendfile19.sh246
-rwxr-xr-xtools/test/stress2/misc/sendfile2.sh142
-rwxr-xr-xtools/test/stress2/misc/sendfile20.sh247
-rwxr-xr-xtools/test/stress2/misc/sendfile21.sh243
-rwxr-xr-xtools/test/stress2/misc/sendfile22.sh282
-rwxr-xr-xtools/test/stress2/misc/sendfile23.sh207
-rwxr-xr-xtools/test/stress2/misc/sendfile24.sh235
-rwxr-xr-xtools/test/stress2/misc/sendfile25.sh237
-rwxr-xr-xtools/test/stress2/misc/sendfile26.sh308
-rwxr-xr-xtools/test/stress2/misc/sendfile3.sh210
-rwxr-xr-xtools/test/stress2/misc/sendfile4.sh141
-rwxr-xr-xtools/test/stress2/misc/sendfile5.sh165
-rwxr-xr-xtools/test/stress2/misc/sendfile6.sh255
-rwxr-xr-xtools/test/stress2/misc/sendfile7.sh42
-rwxr-xr-xtools/test/stress2/misc/sendfile8.sh267
-rwxr-xr-xtools/test/stress2/misc/sendfile9.sh266
-rwxr-xr-xtools/test/stress2/misc/sendfile_shm.sh200
-rwxr-xr-xtools/test/stress2/misc/sendmsg.sh224
-rwxr-xr-xtools/test/stress2/misc/sendmsg2.sh142
-rwxr-xr-xtools/test/stress2/misc/sethostname.sh36
-rwxr-xr-xtools/test/stress2/misc/setsockopt.sh152
-rwxr-xr-xtools/test/stress2/misc/setsockopt2.sh208
-rwxr-xr-xtools/test/stress2/misc/setuid.sh122
-rwxr-xr-xtools/test/stress2/misc/shm.sh201
-rwxr-xr-xtools/test/stress2/misc/shm2.sh167
-rwxr-xr-xtools/test/stress2/misc/shm_open.sh85
-rwxr-xr-xtools/test/stress2/misc/shm_super.sh87
-rwxr-xr-xtools/test/stress2/misc/sigaltstack.sh128
-rwxr-xr-xtools/test/stress2/misc/sigfastblock.sh141
-rwxr-xr-xtools/test/stress2/misc/sigfastblock2.sh132
-rwxr-xr-xtools/test/stress2/misc/signal.sh247
-rwxr-xr-xtools/test/stress2/misc/signal0.sh102
-rwxr-xr-xtools/test/stress2/misc/sigreturn.sh121
-rwxr-xr-xtools/test/stress2/misc/sigstop.sh98
-rwxr-xr-xtools/test/stress2/misc/sigstop2.sh144
-rwxr-xr-xtools/test/stress2/misc/sigxcpu.sh155
-rwxr-xr-xtools/test/stress2/misc/smrstress.sh56
-rwxr-xr-xtools/test/stress2/misc/smrstress2.sh80
-rwxr-xr-xtools/test/stress2/misc/snap.sh55
-rwxr-xr-xtools/test/stress2/misc/snap10.sh68
-rwxr-xr-xtools/test/stress2/misc/snap11.sh87
-rwxr-xr-xtools/test/stress2/misc/snap12.sh72
-rwxr-xr-xtools/test/stress2/misc/snap2-1.sh60
-rwxr-xr-xtools/test/stress2/misc/snap2.sh60
-rwxr-xr-xtools/test/stress2/misc/snap3.sh64
-rwxr-xr-xtools/test/stress2/misc/snap4.sh57
-rwxr-xr-xtools/test/stress2/misc/snap5-1.sh61
-rwxr-xr-xtools/test/stress2/misc/snap5.sh55
-rwxr-xr-xtools/test/stress2/misc/snap6.sh48
-rwxr-xr-xtools/test/stress2/misc/snap7.sh59
-rwxr-xr-xtools/test/stress2/misc/snap8.sh92
-rwxr-xr-xtools/test/stress2/misc/snap9.sh62
-rwxr-xr-xtools/test/stress2/misc/sndstat.sh138
-rwxr-xr-xtools/test/stress2/misc/socketpair.sh104
-rwxr-xr-xtools/test/stress2/misc/socketpair2.sh273
-rwxr-xr-xtools/test/stress2/misc/socketpair3.sh144
-rwxr-xr-xtools/test/stress2/misc/socketpair4.sh207
-rwxr-xr-xtools/test/stress2/misc/softupdate.sh58
-rwxr-xr-xtools/test/stress2/misc/sort.sh50
-rwxr-xr-xtools/test/stress2/misc/sort2.sh52
-rwxr-xr-xtools/test/stress2/misc/sparse.sh74
-rwxr-xr-xtools/test/stress2/misc/spin.sh124
-rwxr-xr-xtools/test/stress2/misc/split.sh99
-rwxr-xr-xtools/test/stress2/misc/stack_guard_page.sh50
-rwxr-xr-xtools/test/stress2/misc/statfs.sh126
-rwxr-xr-xtools/test/stress2/misc/stealer.sh133
-rwxr-xr-xtools/test/stress2/misc/su.sh54
-rwxr-xr-xtools/test/stress2/misc/suj.sh52
-rwxr-xr-xtools/test/stress2/misc/suj10.sh180
-rwxr-xr-xtools/test/stress2/misc/suj11.sh58
-rwxr-xr-xtools/test/stress2/misc/suj12.sh60
-rwxr-xr-xtools/test/stress2/misc/suj13.sh60
-rwxr-xr-xtools/test/stress2/misc/suj14.sh56
-rwxr-xr-xtools/test/stress2/misc/suj15.sh66
-rwxr-xr-xtools/test/stress2/misc/suj16.sh82
-rwxr-xr-xtools/test/stress2/misc/suj17.sh136
-rwxr-xr-xtools/test/stress2/misc/suj18.sh68
-rwxr-xr-xtools/test/stress2/misc/suj19.sh77
-rwxr-xr-xtools/test/stress2/misc/suj2.sh78
-rwxr-xr-xtools/test/stress2/misc/suj20.sh177
-rwxr-xr-xtools/test/stress2/misc/suj21.sh178
-rwxr-xr-xtools/test/stress2/misc/suj22.sh195
-rwxr-xr-xtools/test/stress2/misc/suj23.sh440
-rwxr-xr-xtools/test/stress2/misc/suj24.sh52
-rwxr-xr-xtools/test/stress2/misc/suj25.sh67
-rwxr-xr-xtools/test/stress2/misc/suj26.sh85
-rwxr-xr-xtools/test/stress2/misc/suj27.sh84
-rwxr-xr-xtools/test/stress2/misc/suj28.sh87
-rwxr-xr-xtools/test/stress2/misc/suj29.sh74
-rwxr-xr-xtools/test/stress2/misc/suj3.sh64
-rwxr-xr-xtools/test/stress2/misc/suj30.sh278
-rwxr-xr-xtools/test/stress2/misc/suj31.sh65
-rwxr-xr-xtools/test/stress2/misc/suj32.sh92
-rwxr-xr-xtools/test/stress2/misc/suj33.sh66
-rwxr-xr-xtools/test/stress2/misc/suj34.sh61
-rwxr-xr-xtools/test/stress2/misc/suj35.sh61
-rwxr-xr-xtools/test/stress2/misc/suj4.sh56
-rwxr-xr-xtools/test/stress2/misc/suj5.sh180
-rwxr-xr-xtools/test/stress2/misc/suj6.sh183
-rwxr-xr-xtools/test/stress2/misc/suj7.sh60
-rwxr-xr-xtools/test/stress2/misc/suj8.sh61
-rwxr-xr-xtools/test/stress2/misc/suj9.sh64
-rwxr-xr-xtools/test/stress2/misc/swap.sh157
-rwxr-xr-xtools/test/stress2/misc/swap2.sh176
-rwxr-xr-xtools/test/stress2/misc/swap3.sh38
-rwxr-xr-xtools/test/stress2/misc/swap4.sh152
-rwxr-xr-xtools/test/stress2/misc/swap5.sh50
-rwxr-xr-xtools/test/stress2/misc/swap6.sh52
-rwxr-xr-xtools/test/stress2/misc/swapoff.sh69
-rwxr-xr-xtools/test/stress2/misc/swapoff2.sh76
-rwxr-xr-xtools/test/stress2/misc/swapoff3.sh144
-rwxr-xr-xtools/test/stress2/misc/swapoff4.sh82
-rwxr-xr-xtools/test/stress2/misc/swapoff5.sh182
-rwxr-xr-xtools/test/stress2/misc/swappedout.sh52
-rwxr-xr-xtools/test/stress2/misc/symlink.sh128
-rwxr-xr-xtools/test/stress2/misc/symlink2.sh121
-rwxr-xr-xtools/test/stress2/misc/symlink3.sh69
-rwxr-xr-xtools/test/stress2/misc/symlink4.sh125
-rwxr-xr-xtools/test/stress2/misc/symlink5.sh137
-rwxr-xr-xtools/test/stress2/misc/syscall4.sh386
-rwxr-xr-xtools/test/stress2/misc/syscall5.sh99
-rwxr-xr-xtools/test/stress2/misc/syscall6.sh46
-rwxr-xr-xtools/test/stress2/misc/sysctl.sh44
-rwxr-xr-xtools/test/stress2/misc/sysctl2.sh43
-rwxr-xr-xtools/test/stress2/misc/sysctl3.sh56
-rwxr-xr-xtools/test/stress2/misc/sysctl4.sh54
-rwxr-xr-xtools/test/stress2/misc/systrace.sh62
-rwxr-xr-xtools/test/stress2/misc/systrace2.sh48
-rwxr-xr-xtools/test/stress2/misc/syzkaller1.sh246
-rwxr-xr-xtools/test/stress2/misc/syzkaller10.sh93
-rwxr-xr-xtools/test/stress2/misc/syzkaller11.sh299
-rwxr-xr-xtools/test/stress2/misc/syzkaller12.sh176
-rwxr-xr-xtools/test/stress2/misc/syzkaller13.sh249
-rwxr-xr-xtools/test/stress2/misc/syzkaller14.sh295
-rwxr-xr-xtools/test/stress2/misc/syzkaller15.sh190
-rwxr-xr-xtools/test/stress2/misc/syzkaller16.sh417
-rwxr-xr-xtools/test/stress2/misc/syzkaller17.sh131
-rwxr-xr-xtools/test/stress2/misc/syzkaller18.sh128
-rwxr-xr-xtools/test/stress2/misc/syzkaller19.sh171
-rwxr-xr-xtools/test/stress2/misc/syzkaller2.sh89
-rwxr-xr-xtools/test/stress2/misc/syzkaller20.sh108
-rwxr-xr-xtools/test/stress2/misc/syzkaller21.sh403
-rwxr-xr-xtools/test/stress2/misc/syzkaller22.sh69
-rwxr-xr-xtools/test/stress2/misc/syzkaller23.sh400
-rwxr-xr-xtools/test/stress2/misc/syzkaller24.sh133
-rwxr-xr-xtools/test/stress2/misc/syzkaller25.sh476
-rwxr-xr-xtools/test/stress2/misc/syzkaller26.sh166
-rwxr-xr-xtools/test/stress2/misc/syzkaller27.sh150
-rwxr-xr-xtools/test/stress2/misc/syzkaller28.sh356
-rwxr-xr-xtools/test/stress2/misc/syzkaller29.sh128
-rwxr-xr-xtools/test/stress2/misc/syzkaller3.sh49
-rwxr-xr-xtools/test/stress2/misc/syzkaller30.sh67
-rwxr-xr-xtools/test/stress2/misc/syzkaller4.sh382
-rwxr-xr-xtools/test/stress2/misc/syzkaller5.sh83
-rwxr-xr-xtools/test/stress2/misc/syzkaller6.sh84
-rwxr-xr-xtools/test/stress2/misc/syzkaller7.sh161
-rwxr-xr-xtools/test/stress2/misc/syzkaller8.sh111
-rwxr-xr-xtools/test/stress2/misc/syzkaller9.sh102
-rwxr-xr-xtools/test/stress2/misc/tar.sh132
-rwxr-xr-xtools/test/stress2/misc/tcp.sh59
-rwxr-xr-xtools/test/stress2/misc/tcp2.sh62
-rwxr-xr-xtools/test/stress2/misc/tcp3.sh288
-rwxr-xr-xtools/test/stress2/misc/tcp4.sh249
-rwxr-xr-xtools/test/stress2/misc/temp.sh167
-rwxr-xr-xtools/test/stress2/misc/thr.sh99
-rwxr-xr-xtools/test/stress2/misc/thr2.sh42
-rwxr-xr-xtools/test/stress2/misc/thr3.sh104
-rwxr-xr-xtools/test/stress2/misc/timeout.sh51
-rwxr-xr-xtools/test/stress2/misc/tmpfs.sh48
-rwxr-xr-xtools/test/stress2/misc/tmpfs10.sh155
-rwxr-xr-xtools/test/stress2/misc/tmpfs11.sh81
-rwxr-xr-xtools/test/stress2/misc/tmpfs12.sh95
-rwxr-xr-xtools/test/stress2/misc/tmpfs13.sh95
-rwxr-xr-xtools/test/stress2/misc/tmpfs14.sh112
-rwxr-xr-xtools/test/stress2/misc/tmpfs15.sh48
-rwxr-xr-xtools/test/stress2/misc/tmpfs16.sh200
-rwxr-xr-xtools/test/stress2/misc/tmpfs17.sh144
-rwxr-xr-xtools/test/stress2/misc/tmpfs18.sh58
-rwxr-xr-xtools/test/stress2/misc/tmpfs19.sh56
-rwxr-xr-xtools/test/stress2/misc/tmpfs2.sh64
-rwxr-xr-xtools/test/stress2/misc/tmpfs20.sh60
-rwxr-xr-xtools/test/stress2/misc/tmpfs21.sh59
-rwxr-xr-xtools/test/stress2/misc/tmpfs22.sh117
-rwxr-xr-xtools/test/stress2/misc/tmpfs23.sh52
-rwxr-xr-xtools/test/stress2/misc/tmpfs3.sh45
-rwxr-xr-xtools/test/stress2/misc/tmpfs4.sh46
-rwxr-xr-xtools/test/stress2/misc/tmpfs5.sh53
-rwxr-xr-xtools/test/stress2/misc/tmpfs6.sh110
-rwxr-xr-xtools/test/stress2/misc/tmpfs7.sh249
-rwxr-xr-xtools/test/stress2/misc/tmpfs8.sh178
-rwxr-xr-xtools/test/stress2/misc/tmpfs9.sh110
-rwxr-xr-xtools/test/stress2/misc/trim.sh72
-rwxr-xr-xtools/test/stress2/misc/trim2.sh59
-rwxr-xr-xtools/test/stress2/misc/trim3.sh71
-rwxr-xr-xtools/test/stress2/misc/trim4.sh68
-rwxr-xr-xtools/test/stress2/misc/trim5.sh49
-rwxr-xr-xtools/test/stress2/misc/trim6.sh57
-rwxr-xr-xtools/test/stress2/misc/trim7.sh62
-rwxr-xr-xtools/test/stress2/misc/trim8.sh62
-rwxr-xr-xtools/test/stress2/misc/truncate.sh99
-rwxr-xr-xtools/test/stress2/misc/truncate2.sh99
-rwxr-xr-xtools/test/stress2/misc/truncate3.sh119
-rwxr-xr-xtools/test/stress2/misc/truncate4.sh54
-rwxr-xr-xtools/test/stress2/misc/truncate5.sh105
-rwxr-xr-xtools/test/stress2/misc/truncate6.sh122
-rwxr-xr-xtools/test/stress2/misc/truncate7.sh124
-rwxr-xr-xtools/test/stress2/misc/truncate8.sh212
-rwxr-xr-xtools/test/stress2/misc/truncate9.sh119
-rwxr-xr-xtools/test/stress2/misc/truss.sh129
-rwxr-xr-xtools/test/stress2/misc/truss3.sh36
-rwxr-xr-xtools/test/stress2/misc/tvnlru.sh251
-rwxr-xr-xtools/test/stress2/misc/udp.sh48
-rwxr-xr-xtools/test/stress2/misc/udp2.sh57
-rwxr-xr-xtools/test/stress2/misc/ufsbench.sh58
-rwxr-xr-xtools/test/stress2/misc/ufssuspend.sh131
-rwxr-xr-xtools/test/stress2/misc/uma_zalloc_arg.sh302
-rwxr-xr-xtools/test/stress2/misc/umount.sh50
-rwxr-xr-xtools/test/stress2/misc/umount2.sh90
-rwxr-xr-xtools/test/stress2/misc/umount3.sh66
-rwxr-xr-xtools/test/stress2/misc/umount4.sh71
-rwxr-xr-xtools/test/stress2/misc/umountf.sh69
-rwxr-xr-xtools/test/stress2/misc/umountf10.sh133
-rwxr-xr-xtools/test/stress2/misc/umountf11.sh50
-rwxr-xr-xtools/test/stress2/misc/umountf12.sh68
-rwxr-xr-xtools/test/stress2/misc/umountf2.sh1273
-rwxr-xr-xtools/test/stress2/misc/umountf3.sh118
-rwxr-xr-xtools/test/stress2/misc/umountf4.sh85
-rwxr-xr-xtools/test/stress2/misc/umountf5.sh69
-rwxr-xr-xtools/test/stress2/misc/umountf6.sh85
-rwxr-xr-xtools/test/stress2/misc/umountf7.sh168
-rwxr-xr-xtools/test/stress2/misc/umountf8.sh42
-rwxr-xr-xtools/test/stress2/misc/umountf9.sh45
-rwxr-xr-xtools/test/stress2/misc/umtx_suspend.sh88
-rwxr-xr-xtools/test/stress2/misc/union.sh65
-rwxr-xr-xtools/test/stress2/misc/unionfs.sh52
-rwxr-xr-xtools/test/stress2/misc/unionfs2.sh46
-rwxr-xr-xtools/test/stress2/misc/unionfs3.sh52
-rwxr-xr-xtools/test/stress2/misc/unix_socket.sh161
-rwxr-xr-xtools/test/stress2/misc/unix_socket_detach.sh124
-rwxr-xr-xtools/test/stress2/misc/unix_socket_detach2.sh157
-rwxr-xr-xtools/test/stress2/misc/unlink.sh69
-rwxr-xr-xtools/test/stress2/misc/vfork.sh131
-rwxr-xr-xtools/test/stress2/misc/vm_fault_dontneed.sh99
-rwxr-xr-xtools/test/stress2/misc/vm_map.sh117
-rwxr-xr-xtools/test/stress2/misc/vm_reserv_populate.sh278
-rwxr-xr-xtools/test/stress2/misc/vmio.sh56
-rwxr-xr-xtools/test/stress2/misc/vmstat.sh47
-rwxr-xr-xtools/test/stress2/misc/vmstat2.sh41
-rwxr-xr-xtools/test/stress2/misc/vmtotal.sh42
-rwxr-xr-xtools/test/stress2/misc/vnodes.sh87
-rwxr-xr-xtools/test/stress2/misc/vunref.sh212
-rwxr-xr-xtools/test/stress2/misc/vunref2.sh72
-rwxr-xr-xtools/test/stress2/misc/watchman.sh43
-rwxr-xr-xtools/test/stress2/misc/wire_no_page.sh104
-rwxr-xr-xtools/test/stress2/misc/write.sh231
-rwxr-xr-xtools/test/stress2/misc/zfs.sh68
-rwxr-xr-xtools/test/stress2/misc/zfs10.sh142
-rwxr-xr-xtools/test/stress2/misc/zfs11.sh77
-rwxr-xr-xtools/test/stress2/misc/zfs2.sh71
-rwxr-xr-xtools/test/stress2/misc/zfs3.sh83
-rwxr-xr-xtools/test/stress2/misc/zfs4.sh91
-rwxr-xr-xtools/test/stress2/misc/zfs5.sh71
-rwxr-xr-xtools/test/stress2/misc/zfs6.sh80
-rwxr-xr-xtools/test/stress2/misc/zfs7.sh72
-rwxr-xr-xtools/test/stress2/misc/zfs8.sh73
-rwxr-xr-xtools/test/stress2/misc/zfs9.sh73
-rwxr-xr-xtools/test/stress2/misc/zz-combo01.sh44
-rwxr-xr-xtools/test/stress2/misc/zz-combo02.sh34
-rwxr-xr-xtools/test/stress2/misc/zz-combo03.sh39
-rwxr-xr-xtools/test/stress2/misc/zz-combo04.sh37
-rwxr-xr-xtools/test/stress2/misc/zzbuildworld.sh54
-rw-r--r--tools/test/stress2/mkdir.cfg9
-rw-r--r--tools/test/stress2/mkfifo.cfg8
-rw-r--r--tools/test/stress2/norw.cfg6
-rw-r--r--tools/test/stress2/noswap.cfg6
-rw-r--r--tools/test/stress2/pty.cfg8
-rwxr-xr-xtools/test/stress2/run.sh84
-rw-r--r--tools/test/stress2/rw.cfg8
-rw-r--r--tools/test/stress2/syscall.cfg11
-rw-r--r--tools/test/stress2/sysctl.cfg12
-rw-r--r--tools/test/stress2/testcases/Makefile28
-rw-r--r--tools/test/stress2/testcases/Makefile.inc6
-rw-r--r--tools/test/stress2/testcases/README9
-rw-r--r--tools/test/stress2/testcases/badcode/Makefile3
-rw-r--r--tools/test/stress2/testcases/badcode/badcode.c133
-rw-r--r--tools/test/stress2/testcases/creat/Makefile3
-rw-r--r--tools/test/stress2/testcases/creat/creat.c136
-rw-r--r--tools/test/stress2/testcases/dirnprename/Makefile3
-rw-r--r--tools/test/stress2/testcases/dirnprename/dirnprename.c172
-rw-r--r--tools/test/stress2/testcases/dirrename/Makefile3
-rw-r--r--tools/test/stress2/testcases/dirrename/dirrename.c144
-rw-r--r--tools/test/stress2/testcases/fts/Makefile3
-rw-r--r--tools/test/stress2/testcases/fts/fts.c102
-rw-r--r--tools/test/stress2/testcases/link/Makefile3
-rw-r--r--tools/test/stress2/testcases/link/link.c144
-rw-r--r--tools/test/stress2/testcases/lockf/Makefile3
-rw-r--r--tools/test/stress2/testcases/lockf/lockf.c184
-rw-r--r--tools/test/stress2/testcases/lockf2/Makefile3
-rw-r--r--tools/test/stress2/testcases/lockf2/lockf2.c130
-rw-r--r--tools/test/stress2/testcases/mkdir/Makefile3
-rw-r--r--tools/test/stress2/testcases/mkdir/mkdir.c135
-rw-r--r--tools/test/stress2/testcases/mkfifo/Makefile4
-rw-r--r--tools/test/stress2/testcases/mkfifo/mkfifo.c183
-rw-r--r--tools/test/stress2/testcases/mmap/Makefile3
-rw-r--r--tools/test/stress2/testcases/mmap/mmap.c148
-rw-r--r--tools/test/stress2/testcases/openat/Makefile3
-rw-r--r--tools/test/stress2/testcases/openat/doat.c688
-rw-r--r--tools/test/stress2/testcases/openat/openat.c187
-rw-r--r--tools/test/stress2/testcases/pty/Makefile4
-rw-r--r--tools/test/stress2/testcases/pty/pty.c111
-rw-r--r--tools/test/stress2/testcases/rename/Makefile3
-rw-r--r--tools/test/stress2/testcases/rename/rename.c134
-rw-r--r--tools/test/stress2/testcases/run/Makefile3
-rw-r--r--tools/test/stress2/testcases/run/run.c117
-rw-r--r--tools/test/stress2/testcases/rw/Makefile3
-rw-r--r--tools/test/stress2/testcases/rw/rw.c180
-rw-r--r--tools/test/stress2/testcases/shm/Makefile4
-rw-r--r--tools/test/stress2/testcases/shm/shm.c182
-rw-r--r--tools/test/stress2/testcases/socket/Makefile3
-rw-r--r--tools/test/stress2/testcases/socket/socket.c118
-rw-r--r--tools/test/stress2/testcases/swap/Makefile3
-rw-r--r--tools/test/stress2/testcases/swap/swap.c166
-rw-r--r--tools/test/stress2/testcases/symlink/Makefile3
-rw-r--r--tools/test/stress2/testcases/symlink/symlink.c138
-rw-r--r--tools/test/stress2/testcases/sysctl/Makefile3
-rw-r--r--tools/test/stress2/testcases/sysctl/sysctl.c63
-rw-r--r--tools/test/stress2/testcases/tcp/Makefile3
-rw-r--r--tools/test/stress2/testcases/tcp/tcp.c188
-rw-r--r--tools/test/stress2/testcases/thr1/Makefile5
-rw-r--r--tools/test/stress2/testcases/thr1/thr1.c75
-rw-r--r--tools/test/stress2/testcases/thr2/Makefile5
-rw-r--r--tools/test/stress2/testcases/thr2/thr2.c93
-rw-r--r--tools/test/stress2/testcases/udp/Makefile3
-rw-r--r--tools/test/stress2/testcases/udp/udp.c112
-rw-r--r--tools/test/stress2/tools/bench.c356
-rw-r--r--tools/test/stress2/tools/calc_mem_use.pl61
-rwxr-xr-xtools/test/stress2/tools/df.sh42
-rwxr-xr-xtools/test/stress2/tools/fail.sh74
-rwxr-xr-xtools/test/stress2/tools/fast.sh59
-rw-r--r--tools/test/stress2/tools/flip.c121
-rwxr-xr-xtools/test/stress2/tools/freeze.sh42
-rwxr-xr-xtools/test/stress2/tools/freeze2.sh43
-rw-r--r--tools/test/stress2/tools/fstool.c236
-rwxr-xr-xtools/test/stress2/tools/iwatch.sh47
-rwxr-xr-xtools/test/stress2/tools/killall.sh50
-rwxr-xr-xtools/test/stress2/tools/kldload.sh71
-rwxr-xr-xtools/test/stress2/tools/leaks.sh112
-rwxr-xr-xtools/test/stress2/tools/leaks2.sh113
-rwxr-xr-xtools/test/stress2/tools/maxvnodes.sh43
-rwxr-xr-xtools/test/stress2/tools/ministat.sh52
-rwxr-xr-xtools/test/stress2/tools/monitor.sh40
-rwxr-xr-xtools/test/stress2/tools/ps.sh50
-rwxr-xr-xtools/test/stress2/tools/ptsleak.sh39
-rwxr-xr-xtools/test/stress2/tools/ptyleak.sh40
-rwxr-xr-xtools/test/stress2/tools/rwatch.sh57
-rwxr-xr-xtools/test/stress2/tools/setup.sh45
-rwxr-xr-xtools/test/stress2/tools/shuffle47
-rwxr-xr-xtools/test/stress2/tools/splitall.sh60
-rw-r--r--tools/test/stress2/tools/swap.c225
-rwxr-xr-xtools/test/stress2/tools/uleak.sh71
-rwxr-xr-xtools/test/stress2/tools/vmstat.sh113
-rw-r--r--tools/test/stress2/udp.cfg9
-rw-r--r--tools/test/stress2/vfs.cfg8
968 files changed, 111344 insertions, 0 deletions
diff --git a/tools/test/README b/tools/test/README
index 2bafa48a9764..5512fd5203ba 100644
--- a/tools/test/README
+++ b/tools/test/README
@@ -21,5 +21,6 @@ ppsapi Test 1 Pulse Per Second (1PPS) input for time control.
pthread_vfork Check that vfork and pthreads work together.
ptrace Verify that ptrace works with syscalls, vfork etc.
sort Tests for the sort command, including a full regression.
+stress2 Kernel stress tool.
testfloat Programs to test floating-point implementations
upsdl Test of mmap functionality.
diff --git a/tools/test/stress2/Makefile b/tools/test/stress2/Makefile
new file mode 100644
index 000000000000..ce98559377b9
--- /dev/null
+++ b/tools/test/stress2/Makefile
@@ -0,0 +1,11 @@
+SUBDIR= lib testcases
+MK_AUTO_OBJ= no
+
+.include <bsd.subdir.mk>
+
+all: setup
+
+setup:
+ @cd ${.CURDIR}; ./tools/setup.sh
+test:
+ cd ${.CURDIR}/misc; pwd; ./all.sh -onc
diff --git a/tools/test/stress2/README b/tools/test/stress2/README
new file mode 100644
index 000000000000..ddc8db817ce2
--- /dev/null
+++ b/tools/test/stress2/README
@@ -0,0 +1,20 @@
+stress2 is a tool for finding problems in the kernel.
+
+It is composed of a large number of regression tests, tests that
+stress various components of the kernel and a few validation tests.
+There are currently some 700 different tests.
+
+The key idea behind stress2 is to randomize as much as possible in
+a test, as a way of achieving better coverage. For example, varying
+VM pressure, varying numbers of threads, varying delays, etc.
+stress2 has found a large number of problems:
+https://people.freebsd.org/~pho/stress/log/
+
+To run the full test suite type "make test" in the stress2 directory.
+
+To run a subset of tests, go to the stress2/misc directory.
+ To run for example all tmpfs tests, type ./all.sh -o $(grep -l tmpfs *.sh)
+ To run fdatasync.sh for one hour, type ./all.sh -m 60 fdatasync.sh"
+ To run dup2.sh three times, type ./all.sh -l 3 dup2.sh
+
+Peter Holm <pho@FreeBSD.org>
diff --git a/tools/test/stress2/all.cfg b/tools/test/stress2/all.cfg
new file mode 100644
index 000000000000..562ed767a110
--- /dev/null
+++ b/tools/test/stress2/all.cfg
@@ -0,0 +1,8 @@
+# Stress Test Suite Configuration: run all test programs
+
+# Default values
+. ./default.cfg
+
+export TESTPROGS=""
+export swapHOG=1
+export swapLOAD=100
diff --git a/tools/test/stress2/creat.cfg b/tools/test/stress2/creat.cfg
new file mode 100644
index 000000000000..dc5931564ffd
--- /dev/null
+++ b/tools/test/stress2/creat.cfg
@@ -0,0 +1,8 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+export TESTPROGS="testcases/creat/creat"
+export creatNODELAY=1
+export creatLOAD=100
diff --git a/tools/test/stress2/ddb.conf b/tools/test/stress2/ddb.conf
new file mode 100644
index 000000000000..d0805f5640b8
--- /dev/null
+++ b/tools/test/stress2/ddb.conf
@@ -0,0 +1,6 @@
+# ddb commands used for stress2 crash reports
+#
+
+script pho1=bt; show allpcpu; show alllocks; show lockedvnods; show allchains; show mount; show bufqueues; show page; show pageq
+script pho=set $lines 20000; run pho1; show freepages; acttrace; ps; allt; dump
+
diff --git a/tools/test/stress2/default.cfg b/tools/test/stress2/default.cfg
new file mode 100644
index 000000000000..43b89dc7e160
--- /dev/null
+++ b/tools/test/stress2/default.cfg
@@ -0,0 +1,122 @@
+# Stress Test Suite Configuration.
+
+# Changes should be added to a `hostname` file in this directory.
+# As a minimum you must updated "testuser".
+
+export SHELL=/bin/sh
+: ${allconfig:=/tmp/stress2.d/`hostname`}
+[ -f "$allconfig" ] && . $allconfig # Source in local configuration
+
+# Default values
+export RUNDIR=${RUNDIR:-/tmp/stressX}
+export RUNTIME=${RUNTIME:-2m}
+export VERBOSE=${VERBOSE:-1}
+export LOAD=${LOAD:-20}
+
+# The INCARNATIONS variable may be set to match the number of CPUs and
+# the amount of RAM in the test box.
+export INCARNATIONS=${INCARNATIONS:-20}
+
+export BLASTHOST=${BLASTHOST:-192.168.1.1} # host with udp discard enabled in inetd.conf
+#export nfs_export=somehost:mount # Used by the NFS tests
+
+# Run all test cases:
+export runRUNTIME=${runRUNTIME:-3d} # Run tests for three days
+export runINCARNATIONS=1
+export runLOAD=100
+
+export swapINCARNATIONS=${swapINCARNATIONS:-$((2 * INCARNATIONS))}
+export swapLOAD=${swapLOAD:-80}
+
+export syscallKILL=1
+export swapKILL=1 # May takes a long time to stop
+
+export rwLOAD=${rwLOAD:-70}
+export mkdirLOAD=${mkdirLOAD:-80}
+export creatLOAD=${creatLOAD:-80}
+
+export symlinkLOAD=${symlinkLOAD:-20}
+
+export tcpKILL=1
+export shmINCARNATIONS=${shmINCARNATIONS:-5}
+
+[ -z "$TESTPROGS" ] && export TESTPROGS="
+testcases/rw/rw
+testcases/swap/swap
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/thr1/thr1
+testcases/udp/udp
+testcases/tcp/tcp
+"
+
+#
+# Defaults for ./misc tests
+#
+
+export diskimage=${diskimage:-/tmp/diskimage} # Location of 1G disk image
+export mntpoint=${mntpoint:-/mnt} # Disk image mount point
+export testuser=${testuser:-TBD} # Name of non root test user
+export mdstart=${mdstart:-10} # Start of free md units
+export part=${part:-a} # Partition to use on a md FS
+export newfs_flags=${newfs_flags:-"-U"} # Default file system flags
+export stress2origin=${stress2origin:-`pwd`}
+export stress2tools=`dirname $stress2origin`/tools
+
+# fsck wrapper
+checkfs () {
+ local s
+
+ fsck -t ufs -fy $1 > /tmp/fsck.log 2>&1
+ if grep -v "IS CLEAN" /tmp/fsck.log | \
+ LANG=C egrep -q "[A-Z][A-Z]"; then
+ echo "fsck -t ufs -fy $1"
+ cat /tmp/fsck.log
+ s=1
+ else
+ rm -f /tmp/fsck.log
+ s=0
+ fi
+ return $s
+}
+
+# Support for pre-build binaries for stress2/misc tests
+# Build modes:
+# 1 Build and copy binary to $STRESS2BIN
+# 2 Do not compile; use binary from $STRESS2BIN
+
+# Examples:
+# BMODE=1 ./all.sh -on `grep -lw mycc *.sh`
+# BMODE=2 STRESS2BIN=/home/pho/stress2/bin.i386.r276368 ./all.sh
+
+CC=${CC:-cc}
+top=`dirname $(pwd)` # cwd for the all.sh script
+STRESS2BIN=${STRESS2BIN:-$top/bin}
+mycc () { # "-o" must be first argument
+ local file
+
+ [ "$1" = "-o" ] && file=`basename $2`
+ if [ "$BMODE" = "1" ]; then
+ $CC $@ || return
+ [ -z "$file" ] && return # "-c"
+ [ -d $STRESS2BIN ] || mkdir $STRESS2BIN
+ [ -x $STRESS2BIN/$file ] &&
+ echo "Overwriting $STRESS2BIN/$file"
+ echo "cp $2 $STRESS2BIN"
+ cp $2 $STRESS2BIN
+ exit 0 # Build but do not run the test
+ elif [ "$BMODE" = "2" ]; then
+ [ -z "$file" ] && return
+ if [ ! -x $STRESS2BIN/$file ]; then
+ echo "$STRESS2BIN/$file not found"
+ exit 1
+ fi
+ echo "Using binary $STRESS2BIN/$file"
+ cp $STRESS2BIN/$file /tmp
+ else
+ $CC $@ # default "build and run" mode
+ fi
+}
+
+id $testuser > /dev/null 2>&1 ||
+ { echo "Non-root \$testuser \"$testuser\" not found."; exit 1; }
diff --git a/tools/test/stress2/df.cfg b/tools/test/stress2/df.cfg
new file mode 100644
index 000000000000..933f52aa5896
--- /dev/null
+++ b/tools/test/stress2/df.cfg
@@ -0,0 +1,8 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+# Only run these two test programs for VFS tests
+
+export TESTPROGS="testcases/creat/creat testcases/rw/rw"
diff --git a/tools/test/stress2/disk.cfg b/tools/test/stress2/disk.cfg
new file mode 100644
index 000000000000..557dcc913202
--- /dev/null
+++ b/tools/test/stress2/disk.cfg
@@ -0,0 +1,8 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+export TESTPROGS="testcases/rw/rw testcases/creat/creat testcases/mkdir/mkdir"
+export rwNODELAY=1
+export rwLOAD=100
diff --git a/tools/test/stress2/doc/README b/tools/test/stress2/doc/README
new file mode 100644
index 000000000000..cd932e4da01f
--- /dev/null
+++ b/tools/test/stress2/doc/README
@@ -0,0 +1,3 @@
+stress.pdf An old (and out of date) document describing the first
+ generation of the test suite.
+linuxforum06.pdf The slides from a presentation at Linux Forum 2006
diff --git a/tools/test/stress2/doc/linuxforum06.pdf b/tools/test/stress2/doc/linuxforum06.pdf
new file mode 100644
index 000000000000..ab9d685b36c3
--- /dev/null
+++ b/tools/test/stress2/doc/linuxforum06.pdf
Binary files differ
diff --git a/tools/test/stress2/doc/stress.pdf b/tools/test/stress2/doc/stress.pdf
new file mode 100644
index 000000000000..8c82924129a7
--- /dev/null
+++ b/tools/test/stress2/doc/stress.pdf
Binary files differ
diff --git a/tools/test/stress2/include/stress.h b/tools/test/stress2/include/stress.h
new file mode 100644
index 000000000000..3ffb49410a20
--- /dev/null
+++ b/tools/test/stress2/include/stress.h
@@ -0,0 +1,64 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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.
+ */
+
+#ifndef _STRESS_H_
+#define _STRESS_H_
+extern int setup(int);
+extern int test(void);
+extern void cleanup(void);
+extern void options(int, char **);
+extern int random_int(int, int);
+/*extern void limits(void);*/
+
+typedef struct {
+ int argc;
+ char **argv;
+ int run_time;
+ int load;
+ char *wd;
+ char *cd;
+ int verbose;
+ int incarnations;
+ int hog;
+ int nodelay;
+ int kill;
+ int64_t kblocks;
+ int64_t inodes;
+} opt_t;
+
+extern opt_t *op;
+
+extern volatile int done_testing;
+extern char *home;
+extern void rmval(void);
+extern void putval(unsigned long);
+extern unsigned long getval(void);
+extern void getdf(int64_t *, int64_t *);
+extern void reservedf(int64_t, int64_t);
+extern void show_status(void);
+extern int64_t swap(void);
+extern unsigned long usermem(void);
+#endif
diff --git a/tools/test/stress2/io.cfg b/tools/test/stress2/io.cfg
new file mode 100644
index 000000000000..38830d6bbd63
--- /dev/null
+++ b/tools/test/stress2/io.cfg
@@ -0,0 +1,9 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+# Only run these three test programs for VFS tests
+
+export TESTPROGS="testcases/swap/swap testcases/creat/creat testcases/mkdir/mkdir testcases/rw/rw"
+export swapLOAD=10
diff --git a/tools/test/stress2/jeff.cfg b/tools/test/stress2/jeff.cfg
new file mode 100644
index 000000000000..66345968d635
--- /dev/null
+++ b/tools/test/stress2/jeff.cfg
@@ -0,0 +1,8 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+# Only run these two test programs for VFS tests
+
+export TESTPROGS="testcases/creat/creat testcases/mkdir/mkdir"
diff --git a/tools/test/stress2/lib/Makefile b/tools/test/stress2/lib/Makefile
new file mode 100644
index 000000000000..b2f3cfb0efee
--- /dev/null
+++ b/tools/test/stress2/lib/Makefile
@@ -0,0 +1,9 @@
+LIB= stress
+SRCS= main.c options.c random_int.c resources.c
+INTERNALLIB= true
+MK_AUTO_OBJ= no
+
+.include "../testcases/Makefile.inc"
+CFLAGS+=-fPIE
+
+.include <bsd.lib.mk>
diff --git a/tools/test/stress2/lib/main.c b/tools/test/stress2/lib/main.c
new file mode 100644
index 000000000000..e7267fff2ed9
--- /dev/null
+++ b/tools/test/stress2/lib/main.c
@@ -0,0 +1,212 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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.
+ *
+ */
+
+/* Main program for all test programs */
+
+#include <sys/wait.h>
+#include <sys/stat.h>
+
+#include <stdio.h>
+#include <signal.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <err.h>
+#include <errno.h>
+
+#include "stress.h"
+
+volatile int done_testing;
+static int cleanupcalled = 0;
+char *home;
+
+static pid_t *r;
+
+static void
+handler(int i __unused)
+{
+ int j;
+
+ done_testing = 1;
+ for (j = 0; j < op->incarnations; j++) {
+ if (op->verbose > 2)
+ printf("handler: kill -HUP %d\n", r[j]);
+ if (r[j] != 0 && kill(r[j], SIGHUP) == -1)
+ if (errno != ESRCH)
+ warn("kill(%d, SIGHUP), %s:%d", r[j], __FILE__, __LINE__);
+ }
+ if (op->kill == 1) {
+ sleep(5);
+ /* test programs may have blocked for the SIGHUP, so try harder */
+ for (j = 0; j < op->incarnations; j++) {
+ if (op->verbose > 2)
+ printf("handler: kill -KILL %d\n", r[j]);
+ if (r[j] != 0)
+ (void) kill(r[j], SIGKILL);
+ }
+ }
+}
+
+static void
+run_test_handler(int i __unused)
+{
+
+ done_testing = 1;
+}
+
+static void
+exit_handler(int i __unused)
+{
+
+ _exit(1);
+}
+
+static void
+callcleanup(void)
+{
+ if (cleanupcalled == 0)
+ cleanup();
+ cleanupcalled = 1;
+}
+
+static void
+run_tests(int i)
+{
+ time_t start;
+ int e;
+
+ signal(SIGHUP, run_test_handler);
+ signal(SIGINT, exit_handler);
+ atexit(callcleanup);
+ setup(i);
+ if ((strcmp(getprogname(), "run") != 0) && (op->nodelay == 0))
+ sleep(random_int(1,10));
+ e = 0;
+ start = time(NULL);
+ while (done_testing == 0 && e == 0 &&
+ (time(NULL) - start) < op->run_time) {
+ e = test();
+ }
+ callcleanup();
+ exit(e);
+}
+
+static void
+run_incarnations(void)
+{
+ int e, i, s;
+
+ e = 0;
+ signal(SIGHUP, handler);
+ for (i = 0; i < op->incarnations && done_testing == 0; i++) {
+ if ((r[i] = fork()) == 0) {
+ run_tests(i);
+ }
+ if (r[i] < 0) {
+ warn("fork(), %s:%d", __FILE__, __LINE__);
+ r[i] = 0;
+ break;
+ }
+ }
+ for (i = 0; i < op->incarnations; i++) {
+ if (r[i] != 0 && waitpid(r[i], &s, 0) == -1)
+ warn("waitpid(%d), %s:%d", r[i], __FILE__, __LINE__);
+ if (s != 0)
+ e = 1;
+ }
+
+ exit(e);
+}
+
+static int
+run_test(void)
+{
+ pid_t p;
+ time_t start;
+ int status = 0;
+
+ if (random_int(1,100) > op->load)
+ return (status);
+
+ show_status();
+
+ start = time(NULL);
+ done_testing = 0;
+ fflush(stdout);
+ rmval();
+ p = fork();
+ if (p == 0)
+ run_incarnations();
+ if (p < 0)
+ err(1, "fork() in %s:%d", __FILE__, __LINE__);
+ while (done_testing != 1 &&
+ (time(NULL) - start) < op->run_time) {
+ sleep(1);
+ if (waitpid(p, &status, WNOHANG) == p)
+ return (status != 0);
+ }
+ if (kill(p, SIGHUP) == -1)
+ warn("kill(%d, SIGHUP), %s:%d", p, __FILE__, __LINE__);
+
+ if (waitpid(p, &status, 0) == -1)
+ err(1, "waitpid(%d), %s:%d", p, __FILE__, __LINE__);
+
+ return (status != 0);
+}
+
+int
+main(int argc, char **argv)
+{
+ struct stat sb;
+ int status = 0;
+
+ options(argc, argv);
+
+ umask(0);
+ if (stat(op->wd, &sb) == -1) {
+ if (mkdir(op->wd, 0770) == -1)
+ if (errno != EEXIST)
+ err(1, "mkdir(%s) %s:%d", op->wd, __FILE__, __LINE__);
+ } else if ((sb.st_mode & S_IRWXU) == 0)
+ errx(1, "No RWX access to %s", op->wd);
+ if (stat(op->cd, &sb) == -1) {
+ if (mkdir(op->cd, 0770) == -1)
+ if (errno != EEXIST)
+ err(1, "mkdir(%s) %s:%d", op->cd, __FILE__, __LINE__);
+ }
+ if ((home = getcwd(NULL, 0)) == NULL)
+ err(1, "getcwd(), %s:%d", __FILE__, __LINE__);
+ if (chdir(op->wd) == -1)
+ err(1, "chdir(%s) %s:%d", op->wd, __FILE__, __LINE__);
+
+ r = (pid_t *)calloc(1, op->incarnations * sizeof(pid_t));
+
+ status = run_test();
+
+ return (status);
+}
diff --git a/tools/test/stress2/lib/options.c b/tools/test/stress2/lib/options.c
new file mode 100644
index 000000000000..a081ca4c6c84
--- /dev/null
+++ b/tools/test/stress2/lib/options.c
@@ -0,0 +1,268 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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 <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sysexits.h>
+#include <string.h>
+#include <time.h>
+#include <err.h>
+
+#include "stress.h"
+
+static opt_t opt;
+opt_t *op;
+
+static char path[64];
+
+static void
+usage(const char *where)
+{
+ const char *help;
+
+ if (where != NULL)
+ printf("Error in \"%s\"\n", where);
+ fprintf(stderr, "Usage: %s [-t | -l | -i | -d | -h | -k | -v]\n", getprogname());
+ help = " t <number><s|m|h|d> : time to run test\n"
+ " l <pct> : load factor 0 - 100%\n"
+ " i <number> : max # of parallel incarnations\n"
+ " d <path> : working directory\n"
+ " h : hog resources\n"
+ " k : terminate with SIGHUP + SIGKILL\n"
+ " n : no startup delay\n"
+ " v : verbose\n";
+ printf("%s", help);
+ exit(EX_USAGE);
+}
+
+static int
+time2sec(const char *string)
+{
+ int r, s = 0;
+ char modifier;
+ r = sscanf(string, "%d%c", &s, &modifier);
+ if (r == 2)
+ switch(modifier) {
+ case 's': break;
+ case 'm': s = s * 60; break;
+ case 'h': s = s * 60 * 60; break;
+ case 'd': s = s * 60 * 60 * 24; break;
+ default:
+ usage("-t");
+ }
+ else
+ usage("-t");
+ return (s);
+}
+
+static char *gete(const char *name)
+{
+ char *cp;
+ char help[128];
+
+ snprintf(help, sizeof(help), "%s%s", getprogname(), name);
+ cp = getenv(help);
+ if (cp == NULL)
+ cp = getenv(name);
+ return (cp);
+}
+
+static void
+environment(void)
+{
+ char *cp;
+
+ if ((cp = gete("INCARNATIONS")) != NULL) {
+ if (sscanf(cp, "%d", &op->incarnations) != 1)
+ usage("INCARNATIONS");
+ }
+ if ((cp = gete("LOAD")) != NULL) {
+ if (sscanf(cp, "%d", &op->load) != 1)
+ usage("LOAD");
+ }
+ if ((cp = gete("RUNTIME")) != NULL) {
+ op->run_time = time2sec(cp);
+ }
+ if ((cp = gete("RUNDIR")) != NULL) {
+ op->wd = cp;
+ }
+ if ((cp = gete("CTRLDIR")) != NULL) {
+ op->cd = cp;
+ }
+ if ((cp = gete("HOG")) != NULL) {
+ op->hog = 1;
+ }
+ if ((cp = gete("KILL")) != NULL) {
+ op->kill = 1;
+ }
+ if ((cp = gete("NODELAY")) != NULL) {
+ op->nodelay = 1;
+ }
+ if ((cp = gete("VERBOSE")) != NULL) {
+ if (sscanf(cp, "%d", &op->verbose) != 1)
+ usage("VERBOSE");
+ }
+ if ((cp = gete("KBLOCKS")) != NULL) {
+ if (sscanf(cp, "%jd", &op->kblocks) != 1)
+ usage("KBLOCKS");
+ }
+ if ((cp = gete("INODES")) != NULL) {
+ if (sscanf(cp, "%jd", &op->inodes) != 1)
+ usage("INODES");
+ }
+}
+
+void
+options(int argc, char **argv)
+{
+ int ch;
+
+ op = &opt;
+
+ op->run_time = 60;
+ op->load = 100;
+ op->wd = strdup("/tmp/stressX");
+ op->cd = strdup("/tmp/stressX.control");
+ op->incarnations = 1;
+ op->hog = 0;
+ op->kill = 0;
+ op->nodelay = 0;
+ op->verbose = 0;
+ op->kblocks = 0;
+ op->inodes = 0;
+
+ environment();
+
+ while ((ch = getopt(argc, argv, "t:l:i:d:hknv")) != -1)
+ switch(ch) {
+ case 't': /* run time */
+ op->run_time = time2sec(optarg);
+ break;
+ case 'l': /* load factor in pct */
+ if (sscanf(optarg, "%d", &op->load) != 1)
+ usage("-l");
+ break;
+ case 'i': /* max incarnations */
+ if (sscanf(optarg, "%d", &op->incarnations) != 1)
+ usage("-i");
+ break;
+ case 'd': /* working directory */
+ op->wd = strdup(optarg);
+ break;
+ case 'h': /* hog flag */
+ op->hog += 1;
+ break;
+ case 'k': /* kill flag */
+ op->kill = 1;
+ break;
+ case 'n': /* no delay flag */
+ op->nodelay = 1;
+ break;
+ case 'v': /* verbose flag */
+ op->verbose += 1;
+ break;
+ default:
+ usage(NULL);
+ }
+ op->argc = argc -= optind;
+ op->argv = argv += optind;
+
+ if (op->incarnations < 1)
+ op->incarnations = 1;
+ if (op->hog == 0)
+ op->incarnations = random_int(1, op->incarnations);
+ if (op->run_time < 15)
+ op->run_time = 15;
+ if (op->load < 0 || op->load > 100)
+ op->load = 100;
+}
+
+void
+show_status(void)
+{
+ char buf[80], pgname[9];
+ int days;
+ time_t t;
+
+ if (op->verbose > 0) {
+ strncpy(pgname, getprogname(), sizeof(pgname));
+ pgname[8] = 0;
+ t = op->run_time;
+ days = t / (60 * 60 * 24);
+ t = t % (60 * 60 * 24);
+ strftime(buf, sizeof(buf), "%T", gmtime(&t));
+ printf("%8s: run time %2d+%s, incarnations %3d, load %3d, "
+ "verbose %d\n",
+ pgname, days, buf, op->incarnations, op->load,
+ op->verbose);
+ fflush(stdout);
+ }
+}
+
+void
+rmval(void)
+{
+ if (snprintf(path, sizeof(path), "%s/%s.conf", op->cd,
+ getprogname()) < 0)
+ err(1, "snprintf path");
+ (void) unlink(path);
+}
+
+void
+putval(unsigned long v)
+{
+ char buf[64];
+
+ rmval();
+ snprintf(buf, sizeof(buf), "%lu", v);
+ if (symlink(buf, path) < 0)
+ err(1, "symlink(%s, %s)", path, buf);
+}
+
+unsigned long
+getval(void)
+{
+ int i, n;
+ unsigned long val;
+ char buf[64];
+
+ if ((n = readlink(path, buf, sizeof(buf) -1)) < 0) {
+ for (i = 0; i < 60; i++) {
+ sleep(1);
+ if ((n = readlink(path, buf, sizeof(buf) -1)) > 0)
+ break;
+ }
+ if (n < 0)
+ err(1, "readlink(%s). %s:%d", path, __FILE__,
+ __LINE__);
+ }
+ buf[n] = '\0';
+ if (sscanf(buf, "%ld", &val) != 1)
+ err(1, "sscanf(%s)", buf);
+ return val;
+}
diff --git a/tools/test/stress2/lib/random_int.c b/tools/test/stress2/lib/random_int.c
new file mode 100644
index 000000000000..2f74523ba83c
--- /dev/null
+++ b/tools/test/stress2/lib/random_int.c
@@ -0,0 +1,56 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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/types.h>
+#include <stdlib.h>
+#include "stress.h"
+
+int
+random_int(int mi, int ma)
+{
+ return (arc4random() % (ma - mi + 1) + mi);
+}
+
+#ifdef TEST
+#include <stdio.h>
+int
+main()
+{
+ int i, j, min, max, r;
+ min = 100;
+ max = 1;
+ for (i = 0; i < 10000; i++) {
+ j += (r = random_int(1,100));
+ if (max < r)
+ max = r;
+ if (min > r)
+ min = r;
+ }
+ printf("Average is %d, min = %d, max = %d\n", j / 10000, min, max);
+ return (0);
+}
+#endif
diff --git a/tools/test/stress2/lib/resources.c b/tools/test/stress2/lib/resources.c
new file mode 100644
index 000000000000..935a085a58de
--- /dev/null
+++ b/tools/test/stress2/lib/resources.c
@@ -0,0 +1,248 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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.
+ *
+ */
+
+/* Get various resource limits for the tests */
+
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/param.h>
+#include <sys/mount.h>
+#include <kvm.h>
+#include <vm/vm_param.h>
+#include <errno.h>
+#include <err.h>
+#include <stdarg.h>
+#include <libutil.h>
+
+#include "stress.h"
+
+static int lockfd;
+static int dffd;
+static int flags;
+static char lockpath[128];
+static char dfpath[128];
+
+static int64_t
+inodes(void)
+{
+ char path[MAXPATHLEN+1];
+ struct statfs buf;
+
+ if (op->inodes != 0)
+ return (op->inodes);
+ if (getcwd(path, sizeof(path)) == NULL)
+ err(1, "getcwd()");
+
+ if (statfs(path, &buf) < 0)
+ err(1, "statfs(%s)", path);
+ if (!strcmp(buf.f_fstypename, "msdosfs"))
+ buf.f_ffree = 9999;
+ flags = buf.f_flags & MNT_VISFLAGMASK;
+ if (op->verbose > 2)
+ printf("Free inodes on %s (%s): %jd\n", path,
+ buf.f_mntonname, buf.f_ffree);
+ return (buf.f_ffree);
+}
+
+static int64_t
+df(void)
+{
+ char path[MAXPATHLEN+1];
+ struct statfs buf;
+
+ if (op->kblocks != 0)
+ return (op->kblocks * (uint64_t)1024);
+
+ if (getcwd(path, sizeof(path)) == NULL)
+ err(1, "getcwd()");
+
+ if (statfs(path, &buf) < 0)
+ err(1, "statfs(%s)", path);
+ if (buf.f_bavail > (int64_t)buf.f_blocks || buf.f_bavail < 0) {
+ warnx("Corrupt statfs(%s). f_bavail = %jd!", path,
+ buf.f_bavail);
+ buf.f_bavail = 100;
+ }
+ if (op->verbose > 2)
+ printf("Free space on %s: %jd Mb\n", path, buf.f_bavail *
+ buf.f_bsize / 1024 / 1024);
+ return (buf.f_bavail * buf.f_bsize);
+}
+
+int64_t
+swap(void)
+{
+ struct xswdev xsw;
+ size_t mibsize, size;
+ int mib[16], n;
+ int64_t sz;
+
+ mibsize = sizeof mib / sizeof mib[0];
+ sz = 0;
+
+ if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
+ err(1, "sysctlnametomib()");
+
+ for (n = 0; ; ++n) {
+ mib[mibsize] = n;
+ size = sizeof xsw;
+ if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1)
+ break;
+ if (xsw.xsw_version != XSWDEV_VERSION)
+ errx(1, "xswdev version mismatch");
+ sz = sz + xsw.xsw_nblks - xsw.xsw_used;
+ }
+ if (errno != ENOENT)
+ err(1, "sysctl()");
+
+ if (op->verbose > 2)
+ printf("Total free swap space %jd Mb\n",
+ sz * getpagesize() / 1024 / 1024);
+
+ return (sz * getpagesize());
+}
+
+unsigned long
+usermem(void)
+{
+ unsigned long mem;
+ size_t nlen = sizeof(mem);
+
+ if (sysctlbyname("hw.usermem", &mem, &nlen, NULL, 0) == -1)
+ err(1, "sysctlbyname() %s:%d", __FILE__, __LINE__);
+
+ if (op->verbose > 2)
+ printf("Total free user memory %lu Mb\n",
+ mem / 1024 / 1024);
+
+ return (mem);
+}
+
+static void
+cleanupdf()
+{
+ unlink(dfpath);
+}
+
+void
+getdf(int64_t *block, int64_t *inode)
+{
+ int i, j;
+ char buf[128];
+
+ snprintf(lockpath, sizeof(lockpath), "%s/lock", op->cd);
+ for (j = 0; j < 2; j++) {
+ for (i = 0; i < 10000; i++) {
+ if ((lockfd = open(lockpath,
+ O_CREAT | O_TRUNC | O_WRONLY |
+ O_EXCL, 0644)) != -1)
+ break;
+ usleep(10000); /* sleep 1/100 sec */
+ if (i > 0 && i % 1000 == 0)
+ fprintf(stderr, "%s is waiting for lock file"
+ " %s\n",
+ getprogname(), lockpath);
+ }
+ if (lockfd != -1)
+ break;
+ fprintf(stderr, "%s. Removing stale %s\n", getprogname(),
+ lockpath);
+ unlink(lockpath);
+ }
+ if (lockfd == -1)
+ errx(1, "%s. Can not create %s\n", getprogname(), lockpath);
+ snprintf(dfpath, sizeof(dfpath), "%s/df", op->cd);
+ if ((dffd = open(dfpath, O_RDWR, 0644)) == -1) {
+ if ((dffd = open(dfpath,
+ O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) {
+ unlink(lockpath);
+ err(1, "creat(%s) %s:%d", dfpath, __FILE__,
+ __LINE__);
+ }
+ atexit(cleanupdf);
+ *block = df();
+ *inode = inodes();
+ snprintf(buf, sizeof(buf), "%jd %jd", *block, *inode);
+
+ if (write(dffd, buf, strlen(buf) + 1) !=
+ (ssize_t)strlen(buf) +1)
+ err(1, "write df. %s:%d", __FILE__, __LINE__);
+ } else {
+ if (read(dffd, buf, sizeof(buf)) < 1) {
+ system("ls -l /tmp/stressX.control");
+ unlink(lockpath);
+ err(1, "read df. %s:%d", __FILE__, __LINE__);
+ }
+ sscanf(buf, "%jd %jd", block, inode);
+ }
+ close(dffd);
+}
+
+void
+reservedf(int64_t blks, int64_t inos)
+{
+ char buf[128];
+ int64_t blocks, inodes;
+
+ if ((dffd = open(dfpath, O_RDWR, 0644)) == -1) {
+ warn("open(%s) %s:%d. %s", dfpath, __FILE__, __LINE__,
+ getprogname());
+ goto err;
+ }
+ if (read(dffd, buf, sizeof(buf)) < 1) {
+ warn("read df. %s:%d", __FILE__, __LINE__);
+ goto err;
+ }
+ sscanf(buf, "%jd %jd", &blocks, &inodes);
+
+ if (op->verbose > 2)
+ printf("%-8s: reservefd(%9jdK, %6jd) out of (%9jdK, %6jd)\n",
+ getprogname(), blks/1024, inos, blocks/1024,
+ inodes);
+ blocks -= blks;
+ inodes -= inos;
+
+ snprintf(buf, sizeof(buf), "%jd %jd", blocks, inodes);
+ if (blocks < 0 || inodes < 0)
+ printf("******************************** %s: %s\n",
+ getprogname(), buf);
+ if (lseek(dffd, 0, 0) == -1)
+ err(1, "lseek. %s:%d", __FILE__, __LINE__);
+ if (write(dffd, buf, strlen(buf) + 1) != (ssize_t)strlen(buf) +1)
+ warn("write df. %s:%d", __FILE__, __LINE__);
+err:
+ close(dffd);
+ close(lockfd);
+ if (unlink(lockpath) == -1)
+ err(1, "unlink(%s)", lockpath);
+}
diff --git a/tools/test/stress2/link.cfg b/tools/test/stress2/link.cfg
new file mode 100644
index 000000000000..e66c5a24031e
--- /dev/null
+++ b/tools/test/stress2/link.cfg
@@ -0,0 +1,9 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+export TESTPROGS="testcases/link/link testcases/swap/swap"
+export linkHOG=1
+export linkLOAD=100
+export linkINCARNATIONS=100
diff --git a/tools/test/stress2/load.cfg b/tools/test/stress2/load.cfg
new file mode 100644
index 000000000000..d4d70161a790
--- /dev/null
+++ b/tools/test/stress2/load.cfg
@@ -0,0 +1,18 @@
+# Just a test of the EXCLUDETESTS feature
+
+# Default values
+. ./default.cfg
+
+###export TESTPROGS="" # Remove comments for test
+###export HOG=1 # Remove comments for test
+
+###export LOAD=100 # Remove comments for test
+###export swapLOAD=100 # Remove comments for test
+###export rwLOAD=100 # Remove comments for test
+###export mkdirLOAD=100 # Remove comments for test
+###export creatLOAD=100 # Remove comments for test
+###export symlinkLOAD=100 # Remove comments for test
+###export swapLOAD=100 # Remove comments for test
+###export linkLOAD=100 # Remove comments for test
+
+EXCLUDETESTS="sysctl|swap|tcp|udp|syscall|mmap|socket|thr|thr1|thr2|shm|badcode"
diff --git a/tools/test/stress2/lockf.cfg b/tools/test/stress2/lockf.cfg
new file mode 100644
index 000000000000..279814a73a65
--- /dev/null
+++ b/tools/test/stress2/lockf.cfg
@@ -0,0 +1,9 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+export TESTPROGS="testcases/lockf2/lockf2 testcases/lockf/lockf testcases/swap/swap"
+export lockf2HOG=1
+export lockf2LOAD=100
+export lockf2INCARNATIONS=100
diff --git a/tools/test/stress2/marcus.cfg b/tools/test/stress2/marcus.cfg
new file mode 100644
index 000000000000..855f1dd4e80b
--- /dev/null
+++ b/tools/test/stress2/marcus.cfg
@@ -0,0 +1,24 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+# Test configuration for the vop_stdvptocnp implementation
+
+export LOAD=80
+export symlinkLOAD=80
+export rwLOAD=80
+export TESTPROGS="
+testcases/lockf2/lockf2
+testcases/symlink/symlink
+testcases/openat/openat
+testcases/rw/rw
+testcases/fts/fts
+testcases/link/link
+testcases/lockf/lockf
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/swap/swap
+testcases/mkfifo/mkfifo
+"
diff --git a/tools/test/stress2/misc/1st.sh b/tools/test/stress2/misc/1st.sh
new file mode 100755
index 000000000000..00602d67e803
--- /dev/null
+++ b/tools/test/stress2/misc/1st.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# stress2 config test:
+# Check that diskimage and RUNDIR has not been clobbered in `hostname`.
+# Variables must be set like this: var=${var:-value}
+
+export diskimage=dummy
+export RUNDIR=dummy
+. ../default.cfg
+
+[ "`sysctl -in debug.vmem_check`" = "1" ] &&
+ echo "debug.vmem_check must be set to 0"
+[ "`sysctl -in debug.vmmap_check`" = "0" ] &&
+ echo "debug.vmmap_check must be set to 1"
+
+if [ "$diskimage" != "dummy" ]; then
+ echo "FATAL: diskimage was overwritten with \"$diskimage\""
+ exit 1
+fi
+if [ "$RUNDIR" != "dummy" ]; then
+ echo "FATAL: RUNDIR was overwritten with \"$RUNDIR\""
+ exit 1
+fi
+if [ "`dirname $mntpoint`" != "/" ]; then
+ echo "FATAL: mntpoint \"$mntpoint\" must be a root directory"
+ exit 1
+fi
+[ -z "`which ruby 2>/dev/null`" ] && echo "Consider installing ruby"
+[ -z "`type mke2fs 2>/dev/null`" ] && echo "Consider installing e2fsprogs"
+[ -z "`type mkisofs 2>/dev/null`" ] && echo "Consider installing cdrtools"
+[ -z "`type mDNSNetMonitor 2>/dev/null`" ] && echo "Consider installing mDNSResponder"
+[ ! -x /usr/local/lib/libmill.so ] && echo "Consider installing libmill"
+
+# Random sanity checks
+
+df -k $(dirname $diskimage) | tail -1 | awk '{print $4}' |
+ grep -Eq '^[0-9]+$' || { echo FATAL; df -k $(dirname $diskimage); }
+
+grep -Eq "^discard" /etc/inetd.conf ||
+ echo "Discard is not enabled in /etc/inetd.conf"
+pgrep -Sq inetd || echo "inetd is not running"
+
+[ `sysctl -n kern.maxvnodes` -le 2000 ] &&
+ echo "FATAL kern.maxvnodes is too small"
+
+exit 0
diff --git a/tools/test/stress2/misc/256m.sh b/tools/test/stress2/misc/256m.sh
new file mode 100755
index 000000000000..79b96010af05
--- /dev/null
+++ b/tools/test/stress2/misc/256m.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Test scenario with 256MB RAM on a single CPU i386.
+
+# "panic: ffs_checkblk: bad block -1" seen:
+# https://people.freebsd.org/~pho/stress/log/256m.txt
+# Fixed by r291743.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+[ `uname -m` = "i386" ] || exit 0
+[ `sysctl -n hw.ncpu` -eq 1 ] || { echo "Single CPU test."; exit 0; }
+[ `sysctl -n hw.physmem` -gt $((256 * 1024 * 1024)) ] &&
+ { echo "RAM must be clamped to 256MB for this test."; exit 0; }
+[ -f /usr/src/sys/i386/conf/GENERIC ] || exit 0
+
+cd /usr/src
+make -j 2 buildkernel KERNCONF=GENERIC
+rm -rf /usr/obj/usr/src/sys/GENERIC
diff --git a/tools/test/stress2/misc/README b/tools/test/stress2/misc/README
new file mode 100644
index 000000000000..7b19252cf219
--- /dev/null
+++ b/tools/test/stress2/misc/README
@@ -0,0 +1,4 @@
+This directory contains various test scenarios. Most are regression
+tests for problems fixed.
+
+The script "all.sh" will run all the test scenarios forever.
diff --git a/tools/test/stress2/misc/advlock.sh b/tools/test/stress2/misc/advlock.sh
new file mode 100755
index 000000000000..16b67c3f3340
--- /dev/null
+++ b/tools/test/stress2/misc/advlock.sh
@@ -0,0 +1,245 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# From r238952's commit log:
+# The first change closes a race where an open() that will block with O_SHLOCK
+# or O_EXLOCK can increase the write count while it waits. If the process
+# holding the current lock on the file then tries to call exec() on the file
+# it has locked, it can fail with ETXTBUSY even though the advisory lock is
+# preventing other threads from successfully completing a writable open().
+#
+# The second change closes a race where a read-only open() with O_SHLOCK or
+# O_EXLOCK may return successfully while the write count is non-zero due to
+# another descriptor that had the advisory lock and was blocking the open()
+# still being in the process of closing. If the process that completed the
+# open() then attempts to call exec() on the file it locked, it can fail with
+# ETXTBUSY even though the other process that held a write lock has closed
+# the file and released the lock.
+
+# https://people.freebsd.org/~pho/stress/log/kostik859.txt
+# https://people.freebsd.org/~pho/stress/log/kostik860.txt
+
+# Fixed by r294204.
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/advlock.c
+mycc -o advlock -Wall -Wextra -O0 -g advlock.c || exit 1
+rm -f advlock.c
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 512m -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+cp /usr/bin/true $mntpoint
+cd $mntpoint
+/tmp/advlock
+r=$?
+cd $odir
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/advlock
+exit $r
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+volatile u_int *share;
+char *cmdline[] = { "./true", NULL };
+const char *tp;
+
+#define SYNC 0
+#define PARALLEL 2
+
+#define RUNTIME (1 * 60)
+
+void
+handler(int i __unused) {
+
+ fprintf(stderr, "ALARM from %s.\n", tp);
+ _exit(1);
+}
+
+void
+slock(void)
+{
+ int fd;
+
+ setproctitle("%s", __func__);
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ tp = __func__;
+ alarm(2);
+ if ((fd = open(cmdline[0], O_RDONLY | O_SHLOCK)) == -1)
+ err(1, "open(%s). %d", cmdline[0], __LINE__);
+ usleep(500);
+ close(fd);
+
+ _exit(0);
+}
+
+void
+elock(void)
+{
+ int fd;
+
+ setproctitle("%s", __func__);
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ tp = __func__;
+ alarm(2);
+ if ((fd = open(cmdline[0], O_WRONLY | O_EXLOCK)) == -1) {
+ if (errno != ETXTBSY)
+ err(1, "open(%s). %d", cmdline[0], __LINE__);
+ } else {
+ usleep(500);
+ close(fd);
+ }
+
+ _exit(0);
+}
+
+void
+stest(void)
+{
+ int fd;
+
+ setproctitle("%s", __func__);
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ tp = __func__;
+ alarm(2);
+ if ((fd = open(cmdline[0], O_RDONLY | O_SHLOCK)) == -1)
+ err(1, "open(%s). %d", cmdline[0], __LINE__);
+
+ if (execve(cmdline[0], cmdline, NULL) == -1)
+ err(1, "execve(%s) @ %d", cmdline[0], __LINE__);
+
+ _exit(0);
+}
+
+void
+etest(void)
+{
+ int fd;
+
+ setproctitle("%s", __func__);
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ tp = __func__;
+ alarm(2);
+ if ((fd = open(cmdline[0], O_RDONLY | O_EXLOCK)) == -1)
+ err(1, "open(%s). %d", cmdline[0], __LINE__);
+
+ if (execve(cmdline[0], cmdline, NULL) == -1)
+ err(1, "execve(%s) @ %d", cmdline[0], __LINE__);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ size_t len;
+ time_t start;
+ int i, n, r, s;
+
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED,
+ -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ signal(SIGALRM, handler);
+ n = r = 0;
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ n++;
+ share[SYNC] = 0;
+ if (fork() == 0)
+ slock();
+ if (fork() == 0)
+ stest();
+
+ for (i = 0; i < PARALLEL; i++) {
+ wait(&s);
+ r += s == 0 ? 0 : 1;
+ }
+ if (r != 0)
+ break;
+
+ share[SYNC] = 0;
+ if (fork() == 0)
+ elock();
+ if (fork() == 0)
+ etest();
+
+ for (i = 0; i < PARALLEL; i++) {
+ wait(&s);
+ r += s == 0 ? 0 : 1;
+ }
+ if (r != 0)
+ break;
+ }
+ if (r != 0)
+ fprintf(stderr, "FAIL @ %d\n", n);
+
+ return (r);
+}
diff --git a/tools/test/stress2/misc/aesni.sh b/tools/test/stress2/misc/aesni.sh
new file mode 100755
index 000000000000..c25694fa002a
--- /dev/null
+++ b/tools/test/stress2/misc/aesni.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Simple AESNI(4) test.
+
+kldstat -v | grep -qw aesni || { kldload aesni.ko; loaded=1; }
+
+../misc/geli.sh
+
+[ $loaded ] && kldunload aesni.ko
+
+exit 0
diff --git a/tools/test/stress2/misc/all.debug.inc b/tools/test/stress2/misc/all.debug.inc
new file mode 100644
index 000000000000..ea0f2d6a5c58
--- /dev/null
+++ b/tools/test/stress2/misc/all.debug.inc
@@ -0,0 +1,95 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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$
+#
+
+# Example debug include file for misc/all.sh
+
+pre_debug() {
+ debug_statfs=`vmstat -m | grep -w statfs | awk '{print $2}'`
+ debug_freework=`vmstat -m | grep -w freework | awk '{print $2}'`
+ debug_newblk=`vmstat -m | grep -w newblk | awk '{print $2}'`
+ debug_pts=`vmstat -m | grep -w pts | awk '{print $2}'`
+ debug_routetbl=`vmstat -m | grep -w routetbl | awk '{print $2}'`
+ debug_mount=`vmstat -m | awk '/ mount/{print $2}'`
+ debug_geom=`vmstat -m | grep -w GEOM | awk '{print $2}'`
+# debug_indirdep=`vmstat -m | grep -w indirdep | awk '{print $2}'`
+}
+
+post_debug() {
+ local debug_new
+
+ debug_new=`vmstat -m | grep -w statfs | awk '{print $2}'`
+ [ $((debug_new - debug_statfs)) -gt 0 ] &&
+ { printf "stress2: statfs leak: %d/%d.\r\n" \
+ $((debug_new - debug_statfs)) $debug_new > $console
+ debug_statfs=$debug_new; }
+
+ debug_new=`vmstat -m | grep -w freework | awk '{print $2}'`
+ [ $((debug_new - debug_freework)) -gt 0 -a $debug_new -gt 100 ] &&
+ { printf "stress2: freework leak: %d/%d.\r\n" \
+ $((debug_new - debug_freework)) $debug_new > $console
+ debug_freework=$debug_new; }
+
+ debug_new=`vmstat -m | grep -w newblk | awk '{print $2}'`
+ [ $((debug_new - debug_newblk)) -gt 0 -a $debug_new -gt 100 ] &&
+ { printf "stress2: newblk leak: %d/%d.\r\n" \
+ $((debug_new - debug_newblk)) $debug_new > $console
+ debug_newblk=$debug_new; }
+
+ debug_new=`vmstat -m | grep -w pts | awk '{print $2}'`
+ [ $((debug_new - debug_pts)) -gt 0 ] &&
+ { printf "stress2: pts leak: %d/%d\r\n" \
+ $((debug_new - debug_pts)) $debug_new > $console
+ debug_pts=$debug_new; }
+
+ debug_new=`vmstat -m | grep -w routetbl | awk '{print $2}'`
+ [ $((debug_new - debug_routetbl)) -gt 0 ] &&
+ { printf "stress2: routetbl leak: %d/%d\r\n" \
+ $((debug_new - debug_routetbl)) $debug_new > $console
+ debug_routetbl=$debug_new; }
+
+ debug_new=`vmstat -m | awk '/ mount/{print $2}'`
+ [ $((debug_new - debug_mount)) -gt 0 ] &&
+ { printf "stress2: mount leak: %d/%d\r\n" \
+ $((debug_new - debug_mount)) $debug_new > $console
+ debug_mount=$debug_new; }
+
+ debug_new=`vmstat -m | awk '/GEOM/{print $2}'`
+ [ $((debug_new - debug_geom)) -gt 0 ] &&
+ { printf "stress2: GEOM leak: %d/%d\r\n" \
+ $((debug_new - debug_geom)) $debug_new > $console
+ debug_geom=$debug_new; }
+
+# debug_new=`vmstat -m | awk '/indirdep/{print $2}'`
+# [ $((debug_new - debug_indirdep)) -gt 100 -a $debug_new -gt 100 ] &&
+# { printf "stress2: indirdep leak: %d/%d\r\n" \
+# $((debug_new - debug_indirdep)) $debug_new > $console
+# debug_indirdep=$debug_new; }
+}
+
+all_debug=1
diff --git a/tools/test/stress2/misc/all.exclude b/tools/test/stress2/misc/all.exclude
new file mode 100644
index 000000000000..f04766c3c2ec
--- /dev/null
+++ b/tools/test/stress2/misc/all.exclude
@@ -0,0 +1,111 @@
+# List of tests not to run, unless the '-a' option is used with run.sh
+# Exclude names must start in column 1
+
+backingstore.sh g_vfs_done():md6a[WRITE(offset=...)]error = 28 20111220
+backingstore2.sh panic: 43 vncache entries remaining 20111220
+backingstore3.sh g_vfs_done():md6a[WRITE(offset=...)]error = 28 20111230
+collapse.sh panic: freeing mapped page 0xfffffe0028ed1d50 20200106
+dd.sh CAM stuck in vmwait 20200116
+devfs4.sh Hang seen 20210210
+fsync.sh panic: Journal overflow 20190208
+fuse.sh Memory corruption seen in log file kostik734.txt 20141114
+fuse2.sh Deadlock seen 20121129
+fuse3.sh Deadlock seen 20141120
+getrandom.sh Known DoS issue 20201107
+getrandom2.sh Known DoS issue 20200302
+gjournal.sh panic: Journal overflow 20190626
+gjournal2.sh panic: Journal overflow 20180125
+gjournal3.sh panic: Bio not on queue 20171225
+gjournal4.sh CAM stuck in vmwait 20180517
+gnop7.sh Waiting for patch commit 20190820
+gnop8.sh Waiting for patch commit 20201214
+gnop9.sh Waiting for patch commit 20201214
+gnop10.sh Waiting for patch commit 20210105
+graid1_8.sh Known issue 20170909
+graid1_9.sh panic: Bad effnlink 20180212
+lockf5.sh Spinning threads seen 20160718
+ifconfig2.sh https://people.freebsd.org/~pho/stress/log/log0051.txt 20210210
+maxvnodes.sh Only supposed to work in single user mode 20190412
+maxvnodes2.sh Only supposed to work in single user mode 20190412
+memguard.sh Waiting for fix commit
+memguard2.sh Waiting for fix commit
+memguard3.sh Waiting for fix commit
+memsetdomain.sh May change policy for random threads to to domainset_fixed 20210104
+mlockall2.sh Unrecoverable OOM killing seen 20190203
+mlockall7.sh Needs further investigation 20210123
+nfs15lockd.sh panic: Assertion td->td_realucred == td->td_ucred failed ... 20210211
+Xnfs15lockd2.sh WiP 20200805
+Xnfs15lockd3.sh WiP 20200805
+newfs4.sh watchdog fired. newbuf 20190225
+nfs10.sh Double fault 20151013
+nfs13.sh mount_nfs hangs in mntref 20191007
+nfs16.sh panic: Failed to register NFS lock locally - error=11 20160608
+oom2.sh Hang in pfault 20180324
+overcommit2.sh CAM stuck in vmwait seen 20200112
+pageout.sh panic: handle_written_filepage: not started 20190218
+pmc4.sh ld: error: /usr/lib/libpmc.so: undefined reference 20210124
+pmc5.sh ld: error: /usr/lib/libpmc.so: undefined reference 20210124
+pmc6.sh ld: error: /usr/lib/libpmc.so: undefined reference 20210124
+quota10.sh people.freebsd.org/~pho/stress/log/quota10-2.txt 20200525
+quota2.sh panic: dqflush: stray dquot 20120221
+quota3.sh panic: softdep_deallocate_dependencies: unrecovered ... 20111222
+quota7.sh panic: dqflush: stray dquot 20120221
+Xrename14.sh mark136.txt 20200525
+sctp.sh panic: Queues are not empty when handling ... i386 20201104
+sctp2.sh panic: soclose: SS_NOFDREF on enter 20200307
+sctp3.sh panic: Queues are not empty when handling SHUTDOWN-COMPLETE 20210211
+sendfile25.sh WiP 20200611
+signal.sh Timing issues. Needs fixing 20171116
+snap8.sh https://people.freebsd.org/~pho/stress/log/log0049.txt 20210216
+snap9.sh panic: handle_written_filepage: not started 20170722
+snap11.sh panic: handle_written_filepage: not started 20200928
+suj13.sh Stuck with suspfs 20210105
+suj31.sh Stuck with suspfs 20210105
+suj34.sh Various hangs and panics (SUJ + NULLFS issue) 20131210
+Xswap4.sh WiP 20171208
+swapoff2.sh swap_pager_force_pagein: read from swap failed 20171223
+swapoff5.sh log0005.txt, known issue 20210111
+systrace.sh WiP 20200227
+systrace2.sh WiP 20200227
+syzkaller11.sh WiP 20200721
+syzkaller15.sh WiP 20200712
+syzkaller16.sh WiP 20200620
+syzkaller17.sh WiP 20200630
+syzkaller19.sh WiP 20200712
+syzkaller25.sh WiP 20201116
+syzkaller28.sh WiP 20201120
+syzkaller29.sh May change policy for random threads to to domainset_fixed 20210104
+syzkaller30.sh May change policy for random threads to to domainset_fixed 20210104
+truss3.sh WiP 20200915
+unionfs.sh insmntque: non-locked vp: xx is not exclusive locked... 20130909
+unionfs2.sh insmntque: mp-safe fs and non-locked vp is not ... 20111219
+unionfs3.sh insmntque: mp-safe fs and non-locked vp is not ... 20111216
+
+# Test not to run for other reasons:
+
+fuzz.sh A know issue
+marcus3.sh OK, but runs for a long time
+statfs.sh Not very interesting
+vunref.sh No problems ever seen
+vunref2.sh No problems ever seen
+
+# Snapshots has been disabled on SU+J
+suj15.sh
+suj16.sh
+suj19.sh
+suj20.sh
+suj21.sh
+suj22.sh
+suj24.sh
+suj25.sh
+suj26.sh
+suj27.sh
+suj28.sh
+
+# Exclude NFS loopback tests
+nfs2.sh panic: wrong diroffset 20140219
+nfs5.sh
+nfs6.sh
+nfs11.sh vmwait deadlock 20151004
+nullfs8.sh
+tmpfs18.sh mntref hang seen 20191019
diff --git a/tools/test/stress2/misc/all.exclude.pae b/tools/test/stress2/misc/all.exclude.pae
new file mode 100644
index 000000000000..281eff644e77
--- /dev/null
+++ b/tools/test/stress2/misc/all.exclude.pae
@@ -0,0 +1,11 @@
+PAE tests
+
+# crossmp7.sh
+# zfs.sh
+# zfs2.sh
+# zfs3.sh
+# zfs4.sh
+# zfs5.sh
+# zfs6.sh
+# zfs7.sh
+# zfs8.sh
diff --git a/tools/test/stress2/misc/all.sh b/tools/test/stress2/misc/all.sh
new file mode 100755
index 000000000000..58bf3a630294
--- /dev/null
+++ b/tools/test/stress2/misc/all.sh
@@ -0,0 +1,255 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2009, 2012-13 Peter Holm <pho@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 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.
+#
+
+# Run all the scripts in stress2/misc.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Log and config files:
+sdir=/tmp/stress2.d
+mkdir -p $sdir
+allconfig=$sdir/`hostname` # config file
+allfaillog=$sdir/fail # Tests that failed
+alllast=$sdir/last # Last test run
+alllist=$sdir/list # -o list
+alllog=$sdir/log # Tests run
+alloutput=$sdir/output # Output from current test
+allexcess=$sdir/excessive # Tests with excessive runtime
+allelapsed=$sdir/elapsed # Test runtime
+alllocal=$sdir/all.exclude # Local exclude list
+loops=0 # Times to run the tests
+# Get kernel config + revision
+rev=`uname -a | awk '{print $7}' | sed 's/://'`
+rev="`uname -a | sed 's#.*/compile/##; s/ .*//'` $rev"
+
+args=`getopt acl:m:no "$@"`
+[ $? -ne 0 ] &&
+ echo "Usage $0 [-a] [-c] [-l <val>] [-m <min.>] [-n] [-o] [<tests>]" &&
+ exit 1
+set -- $args
+for i; do
+ case "$i" in
+ -a) all=1 # Run all tests
+ echo "Note: including known problem tests."
+ shift
+ ;;
+ -c) rm -f $alllast # Clear last know test
+ rm -f $alllist
+ shift
+ ;;
+ -l) loops=$2 # Number of time to run
+ shift; shift
+ ;;
+ -m) minutes=$(($2 * 60)) # Run for minutes
+ shift; shift
+ ;;
+ -n) noshuffle=1 # Do not shuffle the list of tests
+ shift # Resume test after last test
+ ;;
+ -o) loops=1 # Only run once
+ shift
+ ;;
+ --)
+ shift
+ break
+ ;;
+ esac
+done
+
+export allconfig
+if [ ! -f $allconfig ]; then
+ echo "Creating local configuration file: $allconfig."
+ ../tools/setup.sh || exit 1
+fi
+
+. ../default.cfg
+
+# Sanity checks
+minspace=$((1024 * 1024)) # in k
+[ -d `dirname "$diskimage"` ] ||
+ { echo "diskimage dir: $diskimage not found"; exit 1; }
+[ `df -k $(dirname $diskimage) | tail -1 | awk '{print $4}'` -lt \
+ $minspace ] &&
+ echo "Warn: Not enough disk space on `dirname $diskimage` " \
+ "for \$diskimage"
+[ ! -d $(dirname $RUNDIR) ] &&
+ echo "No such \$RUNDIR \"`dirname $RUNDIR`\"" &&
+ exit 1
+[ `sysctl -n hw.physmem` -le $((3 * 1024 * 1024 * 1024)) ] &&
+ echo "Warn: Small RAM size for stress tests `sysctl -n hw.physmem`"
+[ `df -k $(dirname $RUNDIR) | tail -1 | awk '{print $4}'` -lt \
+ $minspace ] &&
+ echo "Warn: Not enough disk space on `dirname $RUNDIR` for \$RUNDIR"
+id $testuser > /dev/null 2>&1 ||
+ { echo "\$testuser \"$testuser\" not found."; exit 1; }
+probe=`dirname $RUNDIR`/probe
+su $testuser -c "touch $probe" > /dev/null 2>&1
+[ -f $probe ] && rm $probe ||
+ { echo "No write access to `dirname $RUNDIR`."; exit 1; }
+[ `swapinfo | wc -l` -eq 1 ] &&
+ echo "Consider adding a swap disk. Many tests rely on this."
+mount | grep -wq $mntpoint &&
+ echo "\$mntpoint ($mntpoint) is already in use" && exit 1
+[ -x ../testcases/run/run ] ||
+ (cd ..; make)
+ping -c 2 -t 2 $BLASTHOST > /dev/null 2>&1 ||
+ { echo "Note: Can not ping \$BLASTHOST: $BLASTHOST"; }
+echo "$loops" | grep -Eq "^[0-9]+$" ||
+ { echo "The -l argument must be a positive number"; exit 1; }
+[ `grep "^[a-zA-Z].*\.sh" $alllocal 2>/dev/null | wc -l` -ne 0 ] &&
+ echo "Using $alllocal"
+
+find `dirname $alllast` -maxdepth 1 -name $alllast -mtime +12h -delete
+touch $alllast $alllog
+chmod 640 $alllast $alllog
+find ../testcases -perm -1 \( -name "*.debug" -o -name "*.full" \) -delete
+tail -2000 $alllog > ${alllog}.new; mv ${alllog}.new $alllog
+touch $allelapsed
+tail -20000 $allelapsed > ${allelapsed}.new; mv ${allelapsed}.new $allelapsed
+
+console=/dev/console
+printf "\r\n" > $console &
+pid=$!
+sleep 1
+kill -0 $pid > /dev/null 2>&1 &&
+{ console=/dev/null; kill -9 $pid; }
+while pgrep -q fsck; do sleep 10; done
+
+status() {
+ local s2 r
+
+ s2=`date +%s`
+ r=$(echo "elapsed $(((s2 - s1) / 86400)) day(s)," \
+ "`date -u -j -f '%s' '+%H:%M.%S' $((s2 - s1))`")
+ printf "`date '+%Y%m%d %T'` all.sh done, $r\n"
+ printf "`date '+%Y%m%d %T'` all.sh done, $r\r\n" > $console
+}
+
+intr() {
+ printf "\nExit all.sh\n"
+ ./cleanup.sh
+ exit 1
+}
+trap status EXIT
+trap intr INT
+
+[ -f all.debug.inc ] && . all.debug.inc
+s1=`date +%s`
+while true; do
+ exclude=`cat all.exclude $alllocal 2>/dev/null | sed '/^#/d' |
+ grep "^[a-zA-Z].*\.sh" | awk '{print $1}'`
+ list=`echo *.sh`
+ [ $# -ne 0 ] && list=$*
+ list=`echo $list |
+ sed "s/[[:<:]]all\.sh[[:>:]]//g;\
+ s/[[:<:]]cleanup\.sh[[:>:]]//g"`
+
+ if [ -n "$noshuffle" -a $# -eq 0 ]; then
+ last=`cat $alllast`
+ if [ -n "$last" ]; then
+ last=`basename $last`
+ l=`cat "$alllist" | sed "s/.*$last//"`
+ [ -z "$l" ] && l=$list # start over
+ list=$l
+ echo "Last test was $last,"\
+ "resuming test at" \
+ "`echo "$list" | awk '{print $1}'`"
+ fi
+ fi
+ [ -n "$noshuffle" ] ||
+ list=`echo $list | tr ' ' '\n' | sort -R |
+ tr '\n' ' '`
+
+ lst=""
+ for i in $list; do
+ [ -z "$all" ] && echo $exclude | grep -qw `basename $i` &&
+ continue
+ lst="$lst $i"
+ done
+ [ -z "$lst" ] && exit
+ echo "$lst" > $alllist
+
+ pgrep -fq vmstat.sh ||
+ daemon ../tools/vmstat.sh > /tmp/stress2.d/vmstat 2>&1
+
+ n1=0
+ n2=`echo $lst | wc -w | sed 's/ //g'`
+ for i in $lst; do
+ i=`basename $i`
+ [ ! -f ./$i ] && { echo "No such file ./$i"; continue; }
+ n1=$((n1 + 1))
+ echo $i > $alllast
+ ./cleanup.sh || exit 1
+ ts=`date '+%Y%m%d %T'`
+ echo "$ts all: $i"
+ printf "$ts all ($n1/$n2): $i\n" >> $alllog
+ printf "$ts all ($n1/$n2): $i\r\n" > $console
+ logger "Starting stress2 test all.sh: $i"
+ [ $all_debug ] && pre_debug
+ [ -f $i ] || loops=1 # break
+ sync; sleep .5; sync; sleep .5
+ grep -E "^USE_TIMEOUT=1" $i && TIMEOUT_ONE=1 ||
+ unset TIMEOUT_ONE
+ start=`date '+%s'`
+ (
+ if [ $USE_TIMEOUT ] || [ $TIMEOUT_ONE ]; then
+ timeout -k 1m 1h ./$i
+ else
+ ./$i
+ fi
+ e=$?
+ [ $e -ne 0 ] &&
+ echo "FAIL $i exit code $e"
+ ) 2>&1 | tee $alloutput
+ ts=`date '+%Y%m%d %T'`
+ grep -qw FAIL $alloutput &&
+ echo "$ts $rev $i" >> $allfaillog &&
+ logger "stress2 test $i failed"
+ grep -qw FATAL $alloutput && exit $e
+ rm -f $alloutput
+ printf "$ts $rev $i $((`date '+%s'` - start))\n" >> \
+ $allelapsed
+ [ -f ../tools/ministat.sh ] &&
+ ../tools/ministat.sh $allelapsed $i
+ [ $((`date '+%s'` - start)) -gt 1980 ] &&
+ printf "$ts *** Excessive run time: %s %d min\r\n" $i, \
+ $(((`date '+%s'` - start) / 60)) |
+ tee $console >> $allexcess
+ while pgrep -q "^swap$"; do
+ echo "swap still running"
+ sleep 2
+ done
+ [ $USE_SWAPOFF ] && { swapoff -a; swapon -a; }
+ [ $all_debug ] && post_debug
+ [ $minutes ] && [ $((`date +%s` - s1)) -ge $minutes ] &&
+ break 2
+ done
+ [ $((loops -= 1)) -eq 0 ] && break
+done
+[ -x ../tools/fail.sh ] && ../tools/fail.sh
+find /tmp . -name "*.core" -mtime -2 -maxdepth 2 -ls 2>/dev/null
diff --git a/tools/test/stress2/misc/altbufferflushes.sh b/tools/test/stress2/misc/altbufferflushes.sh
new file mode 100755
index 000000000000..20b1928d5236
--- /dev/null
+++ b/tools/test/stress2/misc/altbufferflushes.sh
@@ -0,0 +1,98 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Regression test. This script caused this panic:
+
+# panic: lockmgr: locking against myself
+# cpuid = 2
+# KDB: enter: panic
+# [thread pid 2526 tid 100070 ]
+# Stopped at kdb_enter+0x2b: nop
+# db> bt
+# Tracing pid 2526 tid 100070 td 0xc46f8360
+# kdb_enter(c094247f) at kdb_enter+0x2b
+# panic(c09402b6,c46f8360,0,12,c06af5d9,...) at panic+0x14b
+# _lockmgr(d864a748,202122,c479f788,c46f8360,c094b01c,12d) at _lockmgr+0x41a
+# getblk(c479f6b8,5d51940,0,4000,0,...) at getblk+0x13c
+# breadn(c479f6b8,5d51940,0,4000,0,...) at breadn+0x2f
+# bread(c479f6b8,5d51940,0,4000,0,e6d13544,c4743eac,0,c095a185,56d) at bread+0x20
+# ffs_alloccg(c47408c4,104,1754628,0,4000,c4743eac,1,c095a185,4d8) at ffs_alloccg+0x11d
+# ffs_hashalloc(c47408c4,104,1754628,0,4000,...) at ffs_hashalloc+0x45
+# ffs_alloc(c47408c4,3f2cd,0,1754628,0,4000,c42fd400,e6d13674) at ffs_alloc+0x1a5
+# ffs_balloc_ufs2(c4735d70,fcb34000,0,4000,c42fd400,...) at ffs_balloc_ufs2+0x1619
+# ffs_copyonwrite(c479f6b8,d84e3d08) at ffs_copyonwrite+0x3d3
+# ffs_geom_strategy(c479f7c0,d84e3d08) at ffs_geom_strategy+0xbd
+# bufwrite(d84e3d08,4000,d84e3d08,e6d137e4,c070b7a9,...) at bufwrite+0x17a
+# ffs_bufwrite(d84e3d08) at ffs_bufwrite+0x282
+# vfs_bio_awrite(d84e3d08) at vfs_bio_awrite+0x235
+# bdwrite(d864a6e8,c4743eac,0,c095a185,57c,...) at bdwrite+0x237
+# ffs_alloccg(c4b54a50,104,1754628,0,4000,c4743eac,1,c095a185,4d8) at ffs_alloccg+0x1f6
+# ffs_hashalloc(c4b54a50,104,1754628,0,4000,...) at ffs_hashalloc+0x45
+# ffs_alloc(c4b54a50,1b00c,0,1754628,0,4000,c4b22a80,e6d139ac) at ffs_alloc+0x1a5
+# ffs_balloc_ufs2(c4e72158,6c030000,0,4000,c4b22a80,...) at ffs_balloc_ufs2+0x1619
+# ffs_write(e6d13b98) at ffs_write+0x2ac
+# VOP_WRITE_APV(c0a00e80,e6d13b98) at VOP_WRITE_APV+0x132
+# vn_write(c46c65a0,e6d13c60,c4b22a80,0,c46f8360) at vn_write+0x1f6
+# dofilewrite(c46f8360,4,c46c65a0,e6d13c60,ffffffff,...) at dofilewrite+0x77
+# kern_writev(c46f8360,4,e6d13c60,8430000,d0000,...) at kern_writev+0x36
+# write(c46f8360,e6d13d00) at write+0x45
+# syscall(e6d13d38) at syscall+0x256
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+persist () {
+ false
+ while [ $? -ne 0 ]; do
+ $1 > /dev/null 2>&1
+ sleep 1
+ done
+}
+
+diskfree=`df -k /var/tmp | tail -1 | awk '{print $4}'`
+[ $((diskfree / 1024 / 1024)) -lt 5 ] && echo "Not enough disk space" && exit 1
+
+rm -f /var/.snap/stress2 /var/tmp/big.?
+trap "rm -f /var/.snap/stress2 /var/tmp/big.?" EXIT INT
+persist 'mksnap_ffs /var /var/.snap/stress2'
+tresh=`sysctl vfs.dirtybufthresh | awk '{print $NF}'`
+sysctl vfs.dirtybufthresh=10
+
+cd /var/tmp
+for j in `jot 5`; do
+ old=`sysctl vfs.altbufferflushes | awk '{print $NF}'`
+ for i in `jot 4`; do
+ echo "`date '+%T'` Create big.$i"
+ dd if=/dev/zero of=big.$i bs=1m count=4k status=none
+ done
+ sleep 1
+ rm -rf /var/tmp/big.?
+ new=`sysctl vfs.altbufferflushes | awk '{print $NF}'`
+ [ $new -ne $old ] && echo "vfs.altbufferflushes changed from $old to $new."
+done
+sysctl vfs.dirtybufthresh=$tresh
+rm -f /var/.snap/stress2
diff --git a/tools/test/stress2/misc/alternativeFlushPath.sh b/tools/test/stress2/misc/alternativeFlushPath.sh
new file mode 100755
index 000000000000..d122b095902a
--- /dev/null
+++ b/tools/test/stress2/misc/alternativeFlushPath.sh
@@ -0,0 +1,131 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Alternate buffer flush path test (Not verified).
+# Regression test for r169006.
+# Apply this patch to amplify the problem:
+#
+# diff -r1.520 vfs_bio.c
+# 894c894
+# < if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10) {
+# ---
+# > if (bo->bo_dirty.bv_cnt > dirtybufthresh /*+ 10*/) {
+
+. ../default.cfg
+
+odir=`pwd`
+dir=$RUNDIR/alternativeFlushPath
+
+[ -d $dir ] && find $dir -type f | xargs rm
+rm -rf $dir
+mkdir -p $dir
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/alternativeFlushPath.c
+mycc -o /tmp/alternativeFlushPath -Wall -Wextra alternativeFlushPath.c ||
+ exit 1
+rm -f alternativeFlushPath.c
+
+for j in `jot 10`; do
+ /tmp/alternativeFlushPath &
+done
+wait
+sysctl vfs.altbufferflushes
+
+cd $odir
+rm -rf /tmp/alternativeFlushPath $dir
+
+exit
+
+EOF
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <sys/signal.h>
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <err.h>
+
+#define MAXNOFILE 500000 /* To limit runtime */
+
+static volatile sig_atomic_t more;
+
+static void
+handler(int i __unused) {
+ more = 0;
+}
+
+void
+test(void)
+{
+ int i, j;
+ char name[80];
+ pid_t mypid;
+ int *fd;
+ struct rlimit rlp;
+
+ if (getrlimit(RLIMIT_NOFILE, &rlp) == -1)
+ err(1, "getrlimit(RLIMIT_NOFILE)");
+ if (rlp.rlim_cur > MAXNOFILE)
+ rlp.rlim_cur = MAXNOFILE;
+ rlp.rlim_cur /= 10;
+ mypid = getpid();
+ fd = malloc(rlp.rlim_cur * sizeof(int));
+
+ for (i = 0, j = 0; i < rlp.rlim_cur && more == 1; i++, j++) {
+ sprintf(name, "f%05d.%05d", mypid, i);
+ if ((fd[i] = open(name, O_CREAT|O_WRONLY, 0666)) == -1) {
+ warn("open(%s)", name);
+ more = 0;
+ break;
+ }
+ }
+ for (i = 0; i < j; i++) {
+ sprintf(name, "f%05d.%05d", mypid, i);
+ if (unlink(name) == -1)
+ warn("unlink(%s)", name);
+ }
+ for (i = 0; i < j; i++) {
+ if (close(fd[i]) == -1)
+ warn("close(%d)", i);
+ }
+ free(fd);
+}
+
+int
+main()
+{
+ more = 1;
+ signal(SIGALRM, handler);
+ alarm(20 * 60);
+ while (more == 1)
+ test();
+
+ return(0);
+}
diff --git a/tools/test/stress2/misc/arp.sh b/tools/test/stress2/misc/arp.sh
new file mode 100755
index 000000000000..ef66ce351e24
--- /dev/null
+++ b/tools/test/stress2/misc/arp.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# arp(8) seen waiting in "sbwait" (on non HEAD):
+# UID PID PPID CPU PRI NI VSZ RSS MWCHAN STAT TT TIME COMMAND
+# 0 70090 68079 0 20 0 9872 2384 sbwait S+ u0 0:00.32 arp -da
+
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+start=`date +%s`
+for i in `jot 3`; do
+ while [ $((`date +%s` - start)) -lt 120 ]; do arp -da > /dev/null 2>&1; done &
+ pids="$pids $!"
+done
+(cd ../testcases/swap; ./swap -t 2m -i 20 -h -l 100) > /dev/null
+while [ $((`date +%s` - start)) -lt 120 ]; do sleep 1; done
+
+for i in `jot 10`; do
+ n=`pgrep -f arp.sh | wc -l`
+ [ $n -eq 0 ] && break
+ sleep 10
+done
+s=0
+if [ $n -ne 0 ]; then
+ ps -l | grep -v grep | grep arp
+ pgrep arp | xargs procstat -k
+ while pkill arp; do :; done
+ s=1
+fi
+wait
+exit $s
diff --git a/tools/test/stress2/misc/aslr.sh b/tools/test/stress2/misc/aslr.sh
new file mode 100755
index 000000000000..6261fae4546b
--- /dev/null
+++ b/tools/test/stress2/misc/aslr.sh
@@ -0,0 +1,46 @@
+#/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# ELF32/64 address map randomization test.
+
+# "panic: page 0xfffff8103971b840 has queue state" seen in WiP kernel code.
+# https://people.freebsd.org/~pho/stress/log/mark087.txt
+
+old32=`sysctl -ni kern.elf32.aslr.enable`
+old64=`sysctl -ni kern.elf64.aslr.enable`
+
+if [ -n "$old32" ]; then
+ sysctl kern.elf32.aslr.enable=$((1 - old32))
+ ./su.sh
+ sysctl kern.elf32.aslr.enable=$old32
+elif [ -n "$old64" ]; then
+ sysctl kern.elf64.aslr.enable=$((1 - old64))
+ ./su.sh
+ sysctl kern.elf64.aslr.enable=$old64
+fi
diff --git a/tools/test/stress2/misc/audit.sh b/tools/test/stress2/misc/audit.sh
new file mode 100755
index 000000000000..70e1ef8b6977
--- /dev/null
+++ b/tools/test/stress2/misc/audit.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# "Fatal trap 12: page fault while in kernel mode" seen.
+# https://people.freebsd.org/~pho/stress/log/audit.txt
+# Fixed by: r294137
+
+# Test scenario by: kib
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -z "$footshoot" ] && exit 0
+
+[ -f /usr/sbin/auditd -a -f /etc/rc.d/auditd ] || exit 0
+pgrep -q auditd && exit 0
+mount | grep -q /var || exit 0
+
+service auditd onestart
+
+sleep 1
+su $testuser -c date > /dev/null
+
+sleep 1
+umount -f /var
+sleep 1
+mount /var
+
+service auditd onestop
+
+# Some services react badly to /var being unmounted.
+service -R
+exit 0
diff --git a/tools/test/stress2/misc/audit2.sh b/tools/test/stress2/misc/audit2.sh
new file mode 100755
index 000000000000..1435b13d9768
--- /dev/null
+++ b/tools/test/stress2/misc/audit2.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+# No problems seen.
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+[ -f /usr/sbin/auditd -a -f /etc/rc.d/auditd ] || exit 0
+pgrep -q auditd && exit 0 || service auditd onestart
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=5m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg'
+
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ [ $((n += 1)) -gt 300 ] && { echo FAIL; exit 1; }
+done
+checkfs /dev/md${mdstart}$part; s=$?
+mdconfig -d -u $mdstart
+service auditd onestop
+
+exit 0
diff --git a/tools/test/stress2/misc/backingstore.sh b/tools/test/stress2/misc/backingstore.sh
new file mode 100755
index 000000000000..b16ff852acd9
--- /dev/null
+++ b/tools/test/stress2/misc/backingstore.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Test sparse backing store
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+D=$diskimage
+export here=`pwd`
+
+m=$mdstart
+
+mount | grep "$mntpoint" | grep -q md$m && umount ${mntpoint}$m
+mdconfig -l | grep -q md$m && mdconfig -d -u $m
+
+dd if=/dev/zero of=$D$m bs=100m count=1 status=none || exit 1
+
+mdconfig -a -t vnode -f $D$m -u $m
+
+bsdlabel -w md$m auto
+newfs md${m}$part > /dev/null 2>&1
+[ -d ${mntpoint}$m ] || mkdir -p ${mntpoint}$m
+mount $opt /dev/md${m}$part ${mntpoint}$m
+
+n=$m
+m=$((m + 1))
+
+mount | grep "$mntpoint" | grep -q md$m && umount ${mntpoint}$m
+mdconfig -l | grep -q md$m && mdconfig -d -u $m
+
+truncate -s 500M ${mntpoint}$n/diskimage
+mdconfig -a -t vnode -f ${mntpoint}$n/diskimage -u $m
+
+bsdlabel -w md$m auto
+newfs md${m}$part > /dev/null 2>&1
+[ -d ${mntpoint}$m ] || mkdir -p ${mntpoint}$m
+mount $opt /dev/md${m}$part ${mntpoint}$m
+
+export RUNDIR=${mntpoint}$m/stressX
+../testcases/rw/rw -t 5m -i 200 -h -n
+
+while mount | grep -q ${mntpoint}$m; do
+ flag=$([ $((`date '+%s'` % 2)) -eq 0 ] && echo "-f" || echo "")
+ umount $flag ${mntpoint}$m > /dev/null 2>&1
+done
+mdconfig -l | grep -q md$m && mdconfig -d -u $m
+
+m=$((m - 1))
+while mount | grep -q ${mntpoint}$m; do
+ umount $([ $((`date '+%s'` % 2)) -eq 0 ] && \
+ echo "-f" || echo "") ${mntpoint}$m > /dev/null 2>&1
+done
+mdconfig -l | grep -q md$m && mdconfig -d -u $m
+rm -f $D
diff --git a/tools/test/stress2/misc/backingstore2.sh b/tools/test/stress2/misc/backingstore2.sh
new file mode 100755
index 000000000000..48dc6109f719
--- /dev/null
+++ b/tools/test/stress2/misc/backingstore2.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Test unmount of a device that is already gone
+
+# Leaves /mnt6 unmountable and leads to a "panic: 1 vncache entries remaining"
+# during shut down.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+D=$diskimage
+export here=`pwd`
+
+m1=$mdstart
+m2=$((m1 + 1))
+mount | grep "$mntpoint" | grep -q md$m2 && umount ${mntpoint}$m2
+mdconfig -l | grep -q md$m2 && mdconfig -d -u $m2
+mount | grep "$mntpoint" | grep -q md$m1 && umount ${mntpoint}$m1
+mdconfig -l | grep -q md$m1 && mdconfig -d -u $m1
+[ -d ${mntpoint}$m1 ] || mkdir -p ${mntpoint}$m1
+[ -d ${mntpoint}$m2 ] || mkdir -p ${mntpoint}$m2
+
+dd if=/dev/zero of=$D$m bs=100m count=1 status=none || exit 1
+
+mdconfig -a -t vnode -f $D$m1 -u $m1
+
+bsdlabel -w md$m1 auto
+newfs md${m1}$part > /dev/null 2>&1
+mount /dev/md${m1}$part ${mntpoint}$m1
+
+truncate -s 500M ${mntpoint}$m1/diskimage
+mdconfig -a -t vnode -f ${mntpoint}$m1/diskimage -u $m2
+
+bsdlabel -w md$m2 auto
+newfs md${m2}$part > /dev/null 2>&1
+mount /dev/md${m2}$part ${mntpoint}$m2
+
+# Reversed umount sequence:
+umount -f /dev/md${m1}$part
+umount -f /dev/md${m2}$part
+
+mount | grep "$mntpoint" | grep -q md$m2 && umount ${mntpoint}$m2
+mdconfig -l | grep -q md$m2 && mdconfig -d -u $m2
+mount | grep "$mntpoint" | grep -q md$m1 && umount ${mntpoint}$m1
+mdconfig -l | grep -q md$m1 && mdconfig -d -u $m1
diff --git a/tools/test/stress2/misc/backingstore3.sh b/tools/test/stress2/misc/backingstore3.sh
new file mode 100755
index 000000000000..2610983da92e
--- /dev/null
+++ b/tools/test/stress2/misc/backingstore3.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Test unmount of a device that is already gone
+
+# Caused "panic: bundirty: buffer 0xdafaf2c4 still on queue 1"
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+D=$diskimage
+export here=`pwd`
+
+m1=$mdstart
+m2=$((m1 + 1))
+mount | grep "$mntpoint" | grep -q md$m2 && umount ${mntpoint}$m2
+mdconfig -l | grep -q md$m2 && mdconfig -d -u $m2
+mount | grep "$mntpoint" | grep -q md$m1 && umount ${mntpoint}$m1
+mdconfig -l | grep -q md$m1 && mdconfig -d -u $m1
+[ -d ${mntpoint}$m1 ] || mkdir -p ${mntpoint}$m1
+[ -d ${mntpoint}$m2 ] || mkdir -p ${mntpoint}$m2
+
+dd if=/dev=zero of=$D$m bs=25m count=1 status=none || exit 1
+
+mdconfig -a -t vnode -f $D$m1 -u $m1
+
+bsdlabel -w md$m1 auto
+newfs md${m1}$part > /dev/null 2>&1
+mount /dev/md${m1}$part ${mntpoint}$m1
+
+truncate -s 500M ${mntpoint}$m1/diskimage
+mdconfig -a -t vnode -f ${mntpoint}$m1/diskimage -u $m2
+
+bsdlabel -w md$m2 auto
+newfs md${m2}$part > /dev/null 2>&1
+mount /dev/md${m2}$part ${mntpoint}$m2
+
+dd if=/dev/zero of=${mntpoint}$m2/file bs=1m > /dev/null 2>&1
+
+# Reversed umount sequence:
+umount -f /dev/md${m1}$part
+umount -f /dev/md${m2}$part
+
+mount | grep "$mntpoint" | grep -q md$m2 && umount ${mntpoint}$m2
+mdconfig -l | grep -q md$m2 && mdconfig -d -u $m2
+mount | grep "$mntpoint" | grep -q md$m1 && umount ${mntpoint}$m1
+mdconfig -l | grep -q md$m1 && mdconfig -d -u $m1
diff --git a/tools/test/stress2/misc/badcode.sh b/tools/test/stress2/misc/badcode.sh
new file mode 100755
index 000000000000..c869c3a8c13e
--- /dev/null
+++ b/tools/test/stress2/misc/badcode.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Run tests on a 2g swap backed MD with UFS SU fs.
+
+# "panic: SACK scoreboard must not be empty" seen:
+# https://people.freebsd.org/~pho/stress/log/full.txt
+# Fixed by r310547.
+
+. ../default.cfg
+kldstat -v | grep -q pty || kldload pty # ignore any load failure
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export LOAD=80
+export rwLOAD=80
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+
+export TESTPROGS="
+testcases/badcode/badcode
+testcases/swap/swap
+testcases/sysctl/sysctl
+"
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
+
+../tools/killall.sh
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/md${mdstart}$part
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/badcode2.sh b/tools/test/stress2/misc/badcode2.sh
new file mode 100755
index 000000000000..8e47a74a87d1
--- /dev/null
+++ b/tools/test/stress2/misc/badcode2.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Peter Holm
+#
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Freeze seen: https://people.freebsd.org/~pho/stress/log/kostik1241.txt
+# Fixed by: r355474
+
+. ../default.cfg
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+newfs $newfs_flags md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+chmod 777 $mntpoint
+
+export badcodeLOAD=100
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+
+export TESTPROGS="testcases/badcode/badcode testcases/swap/swap"
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
+
+../tools/killall.sh
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+exit
+
diff --git a/tools/test/stress2/misc/badcode3.sh b/tools/test/stress2/misc/badcode3.sh
new file mode 100755
index 000000000000..e0a10d0074a4
--- /dev/null
+++ b/tools/test/stress2/misc/badcode3.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Peter Holm
+#
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# No problems seen.
+
+. ../default.cfg
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+newfs $newfs_flags md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+chmod 777 $mntpoint
+
+here=`pwd`
+cd $mntpoint
+(cd $here/../testcases/swap; ./swap -t 5m -i 20) &
+sleep 2
+while pgrep -q swap; do
+ timeout 1m limits -c 0 $here/../testcases/badcode/badcode -t 1m -i 20
+done
+wait
+cd $here
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+exit
+
diff --git a/tools/test/stress2/misc/bench.sh b/tools/test/stress2/misc/bench.sh
new file mode 100755
index 000000000000..965c9a3d9b99
--- /dev/null
+++ b/tools/test/stress2/misc/bench.sh
@@ -0,0 +1,300 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# "benchmark" for file system use, using no physical disk.
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+export LANG=C
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/bench.c
+mycc -o bench -Wall -Wextra -O0 -g bench.c || exit 1
+rm -f bench.c
+cd $odir
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+log=/tmp/stress2.d/bench.sh.log
+[ -f $log ] && old=`tail -1 $log | awk '{print $2}'`
+tmp=/tmp/bench.sh.tmp
+s=0
+for j in `jot 5`; do
+ newfs -n -b 4096 -f 512 -i 1024 md${mdstart}$part > \
+ /dev/null
+ mount -o async /dev/md${mdstart}$part $mntpoint
+ /usr/bin/time sh -c "(cd $mntpoint; /tmp/bench)" 2>&1 | \
+ awk '{print $1}'
+ [ $? -ne 0 ] && s=1
+ umount $mntpoint
+done | ministat -n | tail -1 | awk '{printf "%.3f %.3f\n",$6,$7}' > $tmp
+r=`cat $tmp`
+echo "`date +%Y%m%d%H%M` $r `uname -a`" >> $log
+tail -5 $log | cut -c 1-92
+rm $tmp
+
+if [ $old ]; then
+ awk -v old=$old -v new=$(echo $r | awk '{print $1}') \
+ 'BEGIN {if ((new - old) * 100 / old > 5) exit 1; else exit 0}'
+ s=$?
+fi
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1
+mdconfig -d -u $mdstart
+rm -rf /tmp/bench
+exit 0
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOOPS 50000
+#define TESTS 6
+#define TIMEOUT 600
+
+static void (*functions[TESTS])();
+
+static void
+t1(void)
+{
+ int fd, i;
+ char file[128];
+
+ alarm(TIMEOUT);
+ for (i = 0; i < LOOPS; i++) {
+ if (i % 1000 == 0)
+ setproctitle("%s @ %d", __func__, i);
+ snprintf(file, sizeof(file), "t1.%06d.%03d", getpid(), i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
+ DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ close(fd);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+ usleep(100);
+ }
+ _exit(0);
+}
+
+static void
+t2(void)
+{
+ int fd, i;
+ char file[128];
+
+ alarm(TIMEOUT);
+ for (i = 0; i < LOOPS; i++) {
+ if (i % 1000 == 0)
+ setproctitle("%s @ %d", __func__, i);
+ snprintf(file, sizeof(file), "t2.%06d.%03d", getpid(), i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
+ DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ close(fd);
+ usleep(100);
+ }
+ for (i = 0; i < LOOPS; i++) {
+ snprintf(file, sizeof(file), "t2.%06d.%03d", getpid(), i);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+ }
+ _exit(0);
+}
+
+static void
+t3(void)
+{
+ int fd, i;
+ char dir[128], file[128];
+
+ alarm(TIMEOUT);
+ snprintf(dir, sizeof(dir), "t3.%06d.dir", getpid());
+ if (mkdir(dir, 700) == -1)
+ err(1, "mkdir(%s)", dir);
+ for (i = 0; i < LOOPS; i++) {
+ if (i % 1000 == 0)
+ setproctitle("%s @ %d", __func__, i);
+ snprintf(file, sizeof(file), "%s/t3.%06d.%03d", dir,
+ getpid(), i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
+ DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ close(fd);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+ usleep(100);
+ }
+ if (rmdir(dir) == -1)
+ err(1, "rmdir(%s)", dir);
+
+ _exit(0);
+}
+
+static void
+t4(void)
+{
+ int fd, i;
+ char file[128], new[128];
+
+ alarm(TIMEOUT);
+ for (i = 0; i < LOOPS / 2; i++) {
+ if (i % 1000 == 0)
+ setproctitle("%s @ %d", __func__, i);
+ snprintf(file, sizeof(file), "t4.%06d.%03d", getpid(), i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
+ DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ close(fd);
+ snprintf(new, sizeof(new), "t4.%06d.%03d.new", getpid(), i);
+ if (rename(file, new) == -1)
+ err(1, "rename(%s, %s)", file, new);
+ if (unlink(new) == -1)
+ err(1, "unlink(%s)", new);
+ usleep(100);
+ }
+ _exit(0);
+}
+
+static void
+t5(void)
+{
+ int fd, i;
+ char buf[512], file[128];
+
+ alarm(TIMEOUT);
+ memset(buf, 0, sizeof(buf));
+ for (i = 0; i < LOOPS; i++) {
+ if (i % 1000 == 0)
+ setproctitle("%s @ %d", __func__, i);
+ snprintf(file, sizeof(file), "t5.%06d.%03d", getpid(), i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
+ DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ if (write(fd, buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write(%s)", file);
+ close(fd);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+ usleep(100);
+ }
+ _exit(0);
+}
+
+static void
+t6(void)
+{
+ int fd, i;
+ char buf[512], file[128];
+
+ alarm(TIMEOUT);
+ memset(buf, 0, sizeof(buf));
+ for (i = 0; i < LOOPS / 2; i++) {
+ if (i % 1000 == 0)
+ setproctitle("%s/write @ %d", __func__, i);
+ snprintf(file, sizeof(file), "t6.%06d.%03d", getpid(), i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
+ DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ if (write(fd, buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write(%s)", file);
+ close(fd);
+ }
+ for (i = 0; i < LOOPS / 2; i++) {
+ if (i % 1000 == 0)
+ setproctitle("%s/read @ %d", __func__, i);
+ snprintf(file, sizeof(file), "t6.%06d.%03d", getpid(), i);
+ if ((fd = open(file, O_RDONLY)) == -1)
+ err(1, "open(%s)", file);
+ if (read(fd, buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write(%s)", file);
+ close(fd);
+ usleep(100);
+ }
+ for (i = 0; i < LOOPS / 2; i++) {
+ snprintf(file, sizeof(file), "t6.%06d.%03d", getpid(), i);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+ }
+ _exit(0);
+}
+
+static int
+test(void)
+{
+ pid_t pids[TESTS];
+ int e, i, status;
+
+ e = 0;
+ for (i = 0; i < TESTS; i++)
+ if ((pids[i] = fork()) == 0)
+ functions[i]();
+ for (i = 0; i < TESTS; i++) {
+ if (waitpid(pids[i], &status, 0) != pids[i])
+ err(1, "waitpid(%d)", pids[i]);
+ e += status != 0;
+ }
+
+ return (e);
+}
+
+int
+main(void)
+{
+ int e;
+
+ functions[0] = &t1;
+ functions[1] = &t2;
+ functions[2] = &t3;
+ functions[3] = &t4;
+ functions[4] = &t5;
+ functions[5] = &t6;
+
+ e = test();
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/beneath.sh b/tools/test/stress2/misc/beneath.sh
new file mode 100755
index 000000000000..6c2f71a7e992
--- /dev/null
+++ b/tools/test/stress2/misc/beneath.sh
@@ -0,0 +1,110 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Konstantin Belousov <kib@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Test of open(2) with the O_BENEATH flag.
+
+# userret: returning with the following locks held:
+# shared lockmgr ufs (ufs) r = 0 (0xfffff804ec0d2a48) locked @
+# kern/vfs_subr.c:2590 seen in WiP code:
+# https://people.freebsd.org/~pho/stress/log/kostik1126.txt
+
+top=/tmp/beneath.d
+mkdir -p $top
+cat > $top/beneath.c <<EOF
+/* $Id: beneath.c,v 1.1 2018/10/13 16:53:02 kostik Exp kostik $ */
+
+#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#ifndef O_BENEATH
+#define O_BENEATH 0x00400000 /* Fail if not under cwd */
+#define AT_BENEATH 0x1000 /* Fail if not under dirfd */
+#endif
+
+int
+main(int argc, char *argv[])
+{
+ struct stat st;
+ char *name;
+ int error, fd, i;
+
+ for (i = 1; i < argc; i++) {
+ name = argv[i];
+ alarm(120);
+ fd = open(name, O_RDONLY | O_BENEATH);
+ if (fd == -1) {
+ fprintf(stderr, "open(\"%s\") failed, error %d %s\n",
+ name, errno, strerror(errno));
+ } else {
+ fprintf(stderr, "open(\"%s\") succeeded\n", name);
+ close(fd);
+ }
+ error = fstatat(AT_FDCWD, name, &st, AT_BENEATH);
+ if (error == -1){
+ fprintf(stderr, "stat(\"%s\") failed, error %d %s\n",
+ name, errno, strerror(errno));
+ } else {
+ fprintf(stderr, "stat(\"%s\") succeeded\n", name);
+ }
+ }
+}
+EOF
+cc -o $top/beneath -Wall -Wextra $top/beneath.c || exit 1
+rm $top/beneath.c
+
+# Test with two directories as arguments:
+cd $top
+mkdir -p a/b
+./beneath a/b
+./beneath $top/a/b
+touch $top/a/c
+./beneath a/c
+./beneath $top/a/c
+./beneath a/d
+./beneath $top/a/d
+
+# CWD is still $top for this test
+top2=/var/tmp/beneath.d
+mkdir -p $top2
+mkdir -p $top2/a/b
+./beneath $top2/a/b > /dev/null 2>&1
+
+touch $top2/a/c
+./beneath $top2/a/c > /dev/null 2>&1
+
+# Other CWDs
+(cd /etc; find . | head -1000 | xargs $top/beneath) > /dev/null 2>&1
+(cd /var; find . | head -1000 | xargs $top/beneath) > /dev/null 2>&1
+
+rm -rf $top $top2
+exit 0
diff --git a/tools/test/stress2/misc/beneath2.sh b/tools/test/stress2/misc/beneath2.sh
new file mode 100755
index 000000000000..93a937c054ff
--- /dev/null
+++ b/tools/test/stress2/misc/beneath2.sh
@@ -0,0 +1,101 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# O_BENEATH test with a relative path, which is a symbolic link pointing
+# to an absolute path.
+
+# "panic: Assertion (ndp->ni_lcf & NI_LCF_LATCH) != 0 failed at
+# ../../../kern/vfs_lookup.c:182" seen. Fixed by r340343.
+
+# Based on scenario by Vladimir Kondratyev <vladimir kondratyev su>
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/beneath2.c
+mycc -o beneath2 -Wall -Wextra -O0 -g beneath2.c || exit 1
+rm -f beneath2.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+set +e
+
+cd $mntpoint
+ln -s /tmp/justalongname symlink
+$dir/beneath2 symlink
+s=$?
+[ -f beneath2.core -a $s -eq 0 ] &&
+ { ls -l beneath2.core; mv beneath2.core $dir; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/beneath2
+exit $s
+
+EOF
+#include <sys/types.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int
+main(int argc, char *argv[])
+{
+ int fd;
+ char *file;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <file>", argv[0]);
+ exit(1);
+ }
+ file = argv[1];
+ if ((fd = open(file, O_RDONLY | O_BENEATH)) != 0 &&
+ errno != ENOTCAPABLE)
+ err(1, "open(%s)", file);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/beneath3.sh b/tools/test/stress2/misc/beneath3.sh
new file mode 100755
index 000000000000..52dc37a7f873
--- /dev/null
+++ b/tools/test/stress2/misc/beneath3.sh
@@ -0,0 +1,92 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# vput: 0xfffff808d79c0278 is not locked but should be
+# KDB: enter: lock violation
+# Test scenario suggestions by kib@ and markj@
+
+# Fixed by r348052.
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/beneath3.c
+mycc -o beneath3 -Wall -Wextra -O0 -g beneath3.c || exit 1
+rm -f beneath3.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+set +e
+
+cd $mntpoint
+(cd /usr; $dir/beneath3)
+s=$?
+[ -f beneath3.core -a $s -eq 0 ] &&
+ { ls -l beneath3.core; mv beneath3.core $dir; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/beneath3
+exit $s
+
+EOF
+#include <sys/types.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+
+int
+main(void)
+{
+ int fd;
+ char file[] = "/..";
+
+ errno = 0;
+ fd = open(file, O_CREAT | O_RDONLY | O_BENEATH);
+ if (fd != -1 || errno != ENOTCAPABLE)
+ err(1, "open(%s) returns %d", file, fd);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/beneath4.sh b/tools/test/stress2/misc/beneath4.sh
new file mode 100755
index 000000000000..d9ee71a8f222
--- /dev/null
+++ b/tools/test/stress2/misc/beneath4.sh
@@ -0,0 +1,133 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Test of:
+# AT_BENEATH 0x1000 /* Fail if not under dirfd */
+# AT_RESOLVE_BENEATH 0x2000 /* As AT_BENEATH, but do not allow
+# resolve to walk out of dirfd even
+
+dir=/tmp/beneath4.dir
+rm -rf $dir
+mkdir -p $dir
+here=`pwd`
+cd $dir
+
+cat > beneath4.c <<EOF
+#include <sys/stat.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+main(int argc, char *argv[])
+{
+ struct stat st;
+ int exp, fd, flag, r;
+ char *cwd, *dir, *obj, *s;
+
+ if (argc != 5) {
+ fprintf(stderr,
+ "Usage: %s <dir> <test obj> <flag> <expected return>\n",
+ argv[0]);
+ return (1);
+ }
+
+ cwd = getwd(NULL);
+ dir = argv[1];
+ obj = argv[2];
+ sscanf(argv[3], "%x", &flag);
+ exp = atoi(argv[4]);
+#if 0
+ if ((flag & (AT_BENEATH | AT_RESOLVE_BENEATH)) == 0) {
+ fprintf(stderr, "Flag must be %#x or %#x\n",
+ AT_BENEATH, AT_RESOLVE_BENEATH);
+ return (1);
+ }
+#endif
+ if ((fd = open(dir, O_DIRECTORY | O_RDONLY)) == -1)
+ err(1, "open(%s)", dir);
+
+ if (fstatat(fd, obj, &st, flag) == -1)
+ r = errno;
+ else
+ r = 0;
+ s = "FAIL";
+ if (r == exp)
+ s = "OK";
+ warn("cwd=%s, top=%s. flag=%0.6x. fstatf(%s) = %2d (expect %2d). %4s",
+ cwd, dir, flag, obj, r, exp, s);
+
+ return (r == exp ? 0 : errno);
+}
+EOF
+cc -o beneath4 -Wall -Wextra -O2 -g beneath4.c || exit 1
+rm beneath4.c
+
+mkdir -p /tmp/beneath4.dir/a/a
+touch /tmp/beneath4.dir/a/f
+ln /tmp/beneath4.dir/a/f /tmp/beneath4.dir/a/c
+ln -s /tmp/beneath4.dir/a/a /tmp/beneath4.dir/a/d
+ln -s /tmp/beneath4.dir/a/b /tmp/beneath4.dir/a/e
+mkfifo /tmp/beneath4.dir/a/fifo
+
+top=$dir/a
+
+cd $here
+s=0
+ls -lR $dir
+echo AT_BENEATH
+$dir/beneath4 $top a 0x1000 0 || s=1
+$dir/beneath4 $top b 0x1000 2 || s=1
+$dir/beneath4 $top c 0x1000 0 || s=1
+$dir/beneath4 $top d 0x1000 0 || s=1
+$dir/beneath4 $top e 0x1000 2 || s=1
+$dir/beneath4 $top fifo 0x1000 0 || s=1
+$dir/beneath4 $top $top/../../beneath4.d/a/a 0x1000 93 || s=1
+$dir/beneath4 $top $top/.. 0x1000 93 || s=1
+$dir/beneath4 $top ../a 0x1000 0 || s=1
+
+printf "\nAT_RESOLVE_BENEATH\n"
+$dir/beneath4 $top a 0x2000 0 || s=1
+$dir/beneath4 $top b 0x2000 2 || s=1
+$dir/beneath4 $top c 0x2000 0 || s=1
+$dir/beneath4 $top d 0x2000 93 || s=1
+$dir/beneath4 $top e 0x2000 93 || s=1
+$dir/beneath4 $top fifo 0x2000 0 || s=1
+$dir/beneath4 $top $top/../../beneath4.d/a/a 0x2000 22 || s=1
+$dir/beneath4 $top $top/.. 0x2000 22 || s=1
+$dir/beneath4 $top ../a 0x2000 93 || s=1
+printf "\nNo flag\n"
+$dir/beneath4 $top ../a 0x0000 0 || s=1
+rm -rf $top
+exit $s
diff --git a/tools/test/stress2/misc/bio.sh b/tools/test/stress2/misc/bio.sh
new file mode 100755
index 000000000000..d344fdb281c3
--- /dev/null
+++ b/tools/test/stress2/misc/bio.sh
@@ -0,0 +1,225 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# mmap() & read()/write() on same file.
+# Test scenario suggestion by: jeff@
+
+# Out of VM deadlock seen:
+# https://people.freebsd.org/~pho/stress/log/jeff114.txt
+
+# panic: deadlkres: possible deadlock detected:
+# https://people.freebsd.org/~pho/stress/log/jeff115.txt
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+ps=`sysctl -n hw.pagesize`
+sed "1,/^EOF/d;s/\$ps/$ps/" < $odir/$0 > $dir/bio.c
+mycc -o bio -Wall -Wextra -O0 -g bio.c || exit 1
+rm -f bio.c
+cd $odir
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs -n md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+(cd $mntpoint; /tmp/bio) &
+pid1=$!
+sleep 5
+(cd ../testcases/swap; ./swap -t 5m -i 20 -k -l 100 -h) &
+pid2=$!
+
+while pgrep -q bio; do
+ sleep 2
+done
+
+while pgrep -q swap; do
+ pkill -9 swap
+done
+wait $pid2
+wait $pid1
+s=$?
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -rf /tmp/bio
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define PS $ps /* From hw.pagesize */
+#define SYN1 0
+#define SYN2 1
+#define INDX 2
+
+#define PAGES (512 * 1024 * 1024 / PS) /* 512MB file size */
+#define PARALLEL 3
+#define RUNTIME (5 * 60)
+#define TIMEOUT (30 * 60)
+
+char buf[PS];
+
+void
+test(int inx)
+{
+ pid_t pid;
+ size_t i, len, slen;
+ time_t start;
+ volatile u_int *share;
+ int fd, r;
+ u_int *ip, val;
+ char file[80];
+
+ slen = PS;
+ if ((share = mmap(NULL, slen, PROT_READ | PROT_WRITE, MAP_ANON |
+ MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ snprintf(file, sizeof(file), "file.%06d", inx);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0640)) < 0)
+ err(1, "%s", file);
+
+ for (i = 0; i < PAGES; i++) {
+ if (write(fd, buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write error");
+ }
+
+ len = PS * PAGES * sizeof(u_int);
+ if ((ip = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
+ fd, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ if ((pid = fork()) == 0) {
+ alarm(2 * RUNTIME);
+ /* mmap read / write access */
+ for (i = 0; i < (size_t)(PAGES * PS); i += PS) {
+ while (share[SYN1] == 0)
+ sched_yield();
+ atomic_add_int(&share[SYN1], -1);
+ if (ip[share[INDX] / sizeof(u_int)] != share[INDX])
+ warn("child expected %d, but got %d\n",
+ ip[share[INDX] / sizeof(u_int)],
+ share[INDX]);
+ share[INDX] += PS;
+ ip[share[INDX] / sizeof(u_int)] = share[INDX];
+ atomic_add_int(&share[SYN2], 1); /* signal parent */
+ if (i % 1000 == 0 && time(NULL) - start > TIMEOUT)
+ errx(1, "Timed out");;
+ }
+ _exit(0);
+ }
+ if (pid == -1)
+ err(1, "fork()");
+ share[INDX] = 0;
+ atomic_add_int(&share[SYN2], 1);
+ alarm(2 * RUNTIME);
+ for (i = 0; i < (size_t)(PAGES * PS); i += PS) {
+ while (share[SYN2] == 0)
+ sched_yield();
+ atomic_add_int(&share[SYN2], -1);
+ if (lseek(fd, share[INDX], SEEK_SET) == -1)
+ err(1, "lseek error");
+ if ((r = read(fd, &val, sizeof(val))) != sizeof(val))
+ err(1, "parent read read %d bytes", r);
+ if (val != share[INDX])
+ warn("parent expected %d, but got %d\n",
+ share[INDX], val);
+ val += PS;
+ if (lseek(fd, val, SEEK_SET) == -1)
+ err(1, "lseek error");
+ if (write(fd, &val, sizeof(val)) != sizeof(val))
+ err(1, "write");
+
+ atomic_add_int(&share[SYN1], 1); /* signal child */
+ if (i % 1000 == 0 && time(NULL) - start > TIMEOUT)
+ errx(1, "Timed out");;
+ }
+ atomic_add_int(&share[SYN2], -1);
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "wait");
+
+ if (munmap(ip, len) == -1)
+ err(1, "unmap()");
+ if (munmap((void *)share, slen) == -1)
+ err(1, "unmap()");
+ close(fd);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ time_t start;
+ int i, s, status;
+
+ start = time(NULL);
+ s = 0;
+ while ((time(NULL) - start) < RUNTIME && s == 0) {
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test(i);
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0)
+ fprintf(stderr, "Child exit status = %d\n",
+ status);
+ s += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (s);
+}
diff --git a/tools/test/stress2/misc/buildkernel.sh b/tools/test/stress2/misc/buildkernel.sh
new file mode 100755
index 000000000000..fe8d530dfab7
--- /dev/null
+++ b/tools/test/stress2/misc/buildkernel.sh
@@ -0,0 +1,63 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Buildkernel test with SU
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+[ -d /usr/src/sys ] || exit 0
+set -e
+mount | grep -q "on $mntpoint " && umount $mntpoint
+mdconfig -a -t swap -s 5g -u $mdstart
+newfs $newfs_flags md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+cd /usr/src
+export MAKEOBJDIRPREFIX=$mntpoint/obj
+export TMPDIR=$mntpoint/tmp
+mkdir $TMPDIR
+chmod 0777 $TMPDIR
+log=$mntpoint/log
+
+p=$((`sysctl -n hw.ncpu`+ 1))
+p=`jot -r 1 1 $p`
+echo "make -j $p buildkernel KERNCONF=GENERIC DESTDIR=$mntpoint" \
+ "TARGET=amd64 TARGET_ARCH=amd64"
+make -j $p buildkernel KERNCONF=GENERIC DESTDIR=$mntpoint TARGET=amd64 \
+ TARGET_ARCH=amd64 > $log 2>&1; s=$?
+[ $s -ne 0 ] && tail -50 $log
+
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/buildworld.sh b/tools/test/stress2/misc/buildworld.sh
new file mode 100755
index 000000000000..ef4927d8dd1b
--- /dev/null
+++ b/tools/test/stress2/misc/buildworld.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# "panic: Invalid page 0xfffff8007f227cf0 on inact queue" seen.
+# https://people.freebsd.org/~pho/stress/log/buildworld.txt
+# Fixed by r289377
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+[ -d /usr/src/sys ] || exit 0
+[ `sysctl -n hw.physmem` -gt $(( 2 * 1024 * 1024 * 1024)) ] &&
+ { echo "RAM must be clamped to 2GB or less for this test."; exit 0; }
+rm -f $diskimage
+[ `df -k $(dirname $diskimage) | tail -1 | awk '{print $4}'` -lt \
+ $((3 * 1024 * 1024)) ] && { echo "Need 3GB on `dirname $diskimage`"; \
+ exit 0; }
+mount | grep -q "on $mntpoint " && umount $mntpoint
+dd if=/dev/zero of=$diskimage bs=1m count=3k status=none
+trap "rm -f $diskimage" EXIT INT
+mdconfig -a -t vnode -f $diskimage -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount -o async /dev/md${mdstart}$part $mntpoint
+
+cd /usr/src
+export MAKEOBJDIRPREFIX=$mntpoint/obj
+export TMPDIR=$mntpoint/tmp
+mkdir $TMPDIR
+chmod 0777 $TMPDIR
+
+p=$((`sysctl -n hw.ncpu`+ 1))
+timeout 20m make -i -j $p buildworld DESTDIR=$mntpoint TARGET=amd64 \
+ TARGET_ARCH=amd64 > /dev/null
+
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/md$mdstart || s=$?
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/buildworld2.sh b/tools/test/stress2/misc/buildworld2.sh
new file mode 100755
index 000000000000..9c1eed97b7ea
--- /dev/null
+++ b/tools/test/stress2/misc/buildworld2.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# tmpfs version of buildworld.sh
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+[ -d /usr/src/sys ] || exit 0
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+[ `sysctl -n hw.physmem` -gt $(( 2 * 1024 * 1024 * 1024)) ] &&
+ { echo "RAM must be clamped to 2GB or less."; exit 0; }
+mount | grep -q "on $mntpoint " && umount $mntpoint
+mount -t tmpfs tmpfs $mntpoint || exit 1
+
+cd /usr/src
+export MAKEOBJDIRPREFIX=$mntpoint/obj
+export TMPDIR=$mntpoint/tmp
+mkdir $TMPDIR
+chmod 0777 $TMPDIR
+
+p=$((`sysctl -n hw.ncpu`+ 1))
+make -j $p buildworld DESTDIR=$mntpoint TARGET=amd64 TARGET_ARCH=amd64 \
+ > /dev/null &
+sleep $((20 * 60))
+kill $!
+wait
+
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
diff --git a/tools/test/stress2/misc/buildworld3.sh b/tools/test/stress2/misc/buildworld3.sh
new file mode 100755
index 000000000000..518c16151bcd
--- /dev/null
+++ b/tools/test/stress2/misc/buildworld3.sh
@@ -0,0 +1,95 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Buildworld / quota test scenario.
+
+# "panic: chkdquot: missing dquot" seen
+# https://people.freebsd.org/~pho/stress/log/kostik1113.txt
+# Fixed in r338798 + r338799
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+[ "`sysctl -in kern.features.ufs_quota`" != "1" ] && exit 0
+[ -d /usr/src/sys ] || exit 0
+mount | grep -q "on $mntpoint " && umount $mntpoint
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+
+export PATH_FSTAB=/tmp/fstab
+trap "rm -f $PATH_FSTAB" EXIT INT
+echo "/dev/md${mdstart}$part $mntpoint ufs rw,userquota 2 2" > $PATH_FSTAB
+mount $mntpoint
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export QK=$(($1 / 2))
+export QI=$(($2 / 2))
+edquota -u -f $mntpoint -e \
+ ${mntpoint}:$((QK - 50)):$QK:$((QI - 50 )):$QI $testuser
+quotaon $mntpoint
+mount | grep $mntpoint
+
+cd /usr/src
+export MAKEOBJDIRPREFIX=$mntpoint/obj
+export TMPDIR=$mntpoint/tmp
+mkdir $TMPDIR $MAKEOBJDIRPREFIX
+chmod 0777 $TMPDIR $MAKEOBJDIRPREFIX
+
+p=$((`sysctl -n hw.ncpu`+ 1))
+su $testuser -c \
+ "make -i -j $p buildworld DESTDIR=$mntpoint TARGET=amd64 \
+ TARGET_ARCH=amd64 > /dev/null" &
+sleep 2
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 1200 ]; do
+ kill -0 $! > /dev/null 2>&1 || break
+ sleep 2
+done
+kill $! > /dev/null 2>&1
+# Let make run 50% of the time so quotaoff runs on an active FS
+[ `jot -r 1 0 1` -eq 1 ] &&
+ pkill -U$testuser make
+wait
+
+while ! quotaoff $mntpoint; do
+ sync
+ sleep 5
+done
+pgrep -q -U$testuser make && pkill -U$testuser make
+export tmp=/tmp/$(basename $0).$$
+quotacheck -v $mntpoint > $tmp 2>&1
+grep -q failed $tmp && { cat $tmp; s=1; }
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/md$mdstart || s=$?
+mdconfig -d -u $mdstart
+rm -f $PATH_FSTAB
+exit $s
diff --git a/tools/test/stress2/misc/buildworld4.sh b/tools/test/stress2/misc/buildworld4.sh
new file mode 100755
index 000000000000..fae22b9a17b9
--- /dev/null
+++ b/tools/test/stress2/misc/buildworld4.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Buildworld test with SUJ
+# "fsync: giving up on dirty (error = 35): tag devfs, type VCHR" seen.
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+[ -d /usr/src/sys ] || exit 0
+rm -f $diskimage
+mount | grep -q "on $mntpoint " && umount $mntpoint
+mdconfig -a -t swap -s 5g -u $mdstart
+bsdlabel -w md$mdstart auto
+[ "$newfs_flags" = "-U" ] && newfs_flags="-j"
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+mount | grep $mntpoint
+
+cd /usr/src
+export MAKEOBJDIRPREFIX=$mntpoint/obj
+export TMPDIR=$mntpoint/tmp
+mkdir $TMPDIR
+chmod 0777 $TMPDIR
+
+p=$((`sysctl -n hw.ncpu`+ 1))
+[ `sysctl -n vm.swap_total` -gt 0 ] && p=$((p * 4))
+p=`jot -r 1 1 $p`
+echo "make -i -j $p buildworld DESTDIR=$mntpoint TARGET=amd64 "\
+ "TARGET_ARCH=amd64"
+make -i -j $p buildworld DESTDIR=$mntpoint TARGET=amd64 TARGET_ARCH=amd64 \
+ > /dev/null &
+sleep 1
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 600 ]; do
+ kill -0 $! > /dev/null 2>&1 || break
+ sleep 30
+done
+kill $! > /dev/null 2>&1
+wait
+
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/md$mdstart; s=$?
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/burnin.sh b/tools/test/stress2/misc/burnin.sh
new file mode 100755
index 000000000000..0b6c206cd69b
--- /dev/null
+++ b/tools/test/stress2/misc/burnin.sh
@@ -0,0 +1,210 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Time creating and deleting a number of files once a minute.
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+export LANG=C
+dir=/tmp
+runtime=1200 # default
+[ $# -eq 1 ] && runtime=$1
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/burnin.c
+mycc -o burnin -Wall -Wextra -O0 -g burnin.c || exit 1
+rm -f burnin.c
+cd $odir
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs -n md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+d=`date '+%Y%m%dT%H%M%S'`
+log=/tmp/burnin.$d.log
+mode=`pgrep -q cron && echo "Multi-user" || echo "Single-user"`
+echo "# `uname -a` $mode mode `hostname`" > $log
+
+/tmp/burnin -r 10 -d $mntpoint > /dev/null 2>&1
+/tmp/burnin -r $runtime -d $mntpoint >> $log
+
+ministat -A -C 2 -w 72 $log | tail -1 | awk '{if ($NF > .1) exit(1)}'
+s=$?
+[ $s -ne 0 ] && ministat -C 2 -w 72 $log
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -rf /tmp/burnin $log
+exit 0
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define DELAY 60
+#define SYNC 0
+
+volatile u_int *share;
+int bufsize, files, parallel, runtime;
+char *buf, *dir;
+
+void
+usage(void)
+{
+ fprintf(stderr, "Usage: %s [-b buf size] [-d directory] [-p parallel] "
+ "[-r runtime]\n",
+ getprogname());
+ _exit(1);
+}
+
+void
+test(void)
+{
+ pid_t pid;
+ int fd, i;
+ char path[MAXPATHLEN + 1];
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != (volatile u_int)parallel)
+ ;
+
+ pid =getpid();
+ for (i = 0; i < files; i++) {
+ snprintf(path, sizeof(path), "%s/f%06d.%06d", dir, pid, i);
+ if ((fd = open(path, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE)) ==
+ -1)
+ err(1, "open(%s)", path);
+ if (write(fd, buf, bufsize) != bufsize)
+ err(1, "write()");
+ if (close(fd) == -1)
+ err(1, "close(%d)", fd);
+ if (unlink(path) == -1)
+ err(1, "unlink(%s)", path);
+ }
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct timeval t1, t2, diff;
+ struct tm *tp;
+ size_t len;
+ time_t start, now;
+ int ch, e, i, *pids, status;
+ char help[80];
+
+ bufsize = 8 * 1024;
+ dir = "/tmp";
+ files = 5000;
+ parallel = 4;
+ runtime = 1 * 60 * 60 * 24;
+
+ while ((ch = getopt(argc, argv, "b:d:f:r:")) != -1)
+ switch(ch) {
+ case 'b': /* bufsize */
+ if (sscanf(optarg, "%d", &bufsize) != 1)
+ usage();
+ break;
+ case 'd': /* dir */
+ dir = optarg;
+ break;
+ case 'f': /* files */
+ if (sscanf(optarg, "%d", &files) != 1)
+ usage();
+ break;
+ case 'p': /* parallel */
+ if (sscanf(optarg, "%d", &parallel) != 1)
+ usage();
+ break;
+ case 'r': /* runtime */
+ if (sscanf(optarg, "%d", &runtime) != 1)
+ usage();
+ break;
+ default:
+ usage();
+ }
+ argc -= optind;
+ argv += optind;
+
+ printf("# Options used: dir=%s, bufsize=%d, files=%d, parallel=%d, "
+ "runtime=%d\n",
+ dir, bufsize, files, parallel, runtime);
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d)", bufsize);
+ if ((pids = malloc(sizeof(pid_t) * parallel)) == NULL)
+ err(1, "malloc(%d)", (int)(sizeof(pid_t) * parallel));
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < runtime && e == 0) {
+ share[SYNC] = 0;
+ gettimeofday(&t1, NULL);
+ for (i = 0; i < parallel; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ for (i = 0; i < parallel; i++) {
+ waitpid(pids[i], &status, 0);
+ e += status == 0 ? 0 : 1;
+ }
+ gettimeofday(&t2, NULL);
+ timersub(&t2, &t1, &diff);
+ now = time(NULL);
+ tp = localtime(&now);
+ strftime(help, sizeof(help), "%Y%m%d%H%M%S", tp);
+ printf("%s %ld.%06ld\n", help, (long)diff.tv_sec,
+ diff.tv_usec);
+ fflush(stdout);
+ if (runtime > DELAY)
+ sleep(DELAY);
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/callout_reset_on.sh b/tools/test/stress2/misc/callout_reset_on.sh
new file mode 100755
index 000000000000..cdffc0dd67f7
--- /dev/null
+++ b/tools/test/stress2/misc/callout_reset_on.sh
@@ -0,0 +1,331 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Scenario based on pr. kern/166340
+# Process under FreeBSD 9.0 hangs in uninterruptable sleep with apparently
+# no syscall (empty wchan).
+
+# http://people.freebsd.org/~pho/stress/log/callout_reset_on.txt
+# Fixed in r243901.
+
+# panic: Bad link elm 0xfffff80012ba8ec8 prev->next != elm
+# https://people.freebsd.org/~pho/stress/log/rrs005.txt
+# Fixed in r278623.
+
+# "ritwait DE 0- 0:00.01 crlogger: writer" seen.
+# https://people.freebsd.org/~pho/stress/log/kostik917.txt
+# Fixed in r302981
+
+. ../default.cfg
+
+rm -f /tmp/crwriter /tmp/crlogger || exit 1
+
+cat > /tmp/crwriter.c <<EOF
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+char *txt[] = {
+ "0 This is a line of text: abcdefghijklmnopqrstuvwxyz",
+ "1 Another line of text: ABCDEFGHIJKLMNOPQRSTUVWXYZ",
+ "2 A different line of text",
+ "3 A very, very different text",
+ "4 A much longer line with a lot of characters in the line",
+ "5 Now this is a quite long line of text, with both upper and lower case letters, and one digit!"
+};
+
+int
+main(void)
+{
+ int i, j, n;
+ char help[256];
+
+ for (i = 0; i < 100000; i++) {
+ j = arc4random() % 6;
+ n = arc4random() % strlen(txt[j]);
+ strncpy(help, txt[j], n);
+ help[n] = 0;
+ printf("%s\n", txt[j]);
+ if ((arc4random() % 1000) == 1)
+ usleep(100000);
+ }
+
+ return (0);
+}
+EOF
+mycc -o /tmp/crwriter -Wall -Wextra -O2 -g /tmp/crwriter.c
+rm -f /tmp/crwriter.c
+
+cat > /tmp/crlogger.c <<EOF
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <signal.h>
+#include <errno.h>
+#include <string.h>
+#include <err.h>
+#include <sys/types.h>
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define BARRIER_CREATE 1
+#define BARRIER_WAIT 2
+#define BARRIER_DELETE 3
+
+void
+barrier(int mode)
+{
+ int fd;
+ char path[128];
+
+ if (mode == BARRIER_CREATE) {
+ snprintf(path, sizeof(path), "barrier.%d", getpid());
+ if ((fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
+ err(1, "%s", path);
+ } else if (mode == BARRIER_WAIT) {
+ snprintf(path, sizeof(path), "barrier.%d", getppid());
+ for(;;) {
+ if (access(path, R_OK) == -1)
+ break;
+ usleep(10000);
+ }
+ } else if (mode == BARRIER_DELETE) {
+ snprintf(path, sizeof(path), "barrier.%d", getpid());
+ if (unlink(path) == -1)
+ err(1, "unlink(%s)", path);
+ } else
+ errx(1, "Bad barrier mode: %d", mode);
+}
+
+pid_t pid;
+int bufsize;
+int port;
+int alarm_exit;
+
+void
+killer(void)
+{
+ setproctitle("killer");
+ alarm(120);
+ barrier(BARRIER_WAIT);
+ for (;;) {
+ if (pid == 0)
+ break;
+ if (kill(pid, SIGUSR1) == -1)
+ break;
+ usleep(1000);
+ }
+ _exit(0);
+}
+
+void
+handler(int s __unused)
+{
+}
+
+void
+ahandler(int s __unused)
+{
+ if (alarm_exit)
+ _exit(0);
+}
+
+/* Read form socket, discard */
+static void
+reader(void) {
+ int tcpsock, msgsock;
+ int on;
+ socklen_t len;
+ struct sockaddr_in inetaddr, inetpeer;
+ int n, *buf;
+
+ setproctitle("reader - init");
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ signal(SIGUSR1, handler);
+ alarm(60);
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+ setproctitle("reader");
+ alarm(0);
+ signal(SIGALRM, ahandler);
+ for (;;) {
+ ualarm(5000, 0);
+ if ((n = recvfrom(msgsock, buf, 4, 0, NULL, NULL)) < 0) {
+ if (errno == EAGAIN)
+ continue;
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ }
+ if (n == 0)
+ break;
+ if (write(msgsock, "OK", 3) != 3)
+ err(1, "write ack. %s:%d", __FILE__, __LINE__);
+
+ }
+ close(msgsock);
+ _exit(0);
+}
+
+/* read from stdin, write to socket */
+static void
+writer(void) {
+ int tcpsock, on;
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ int i, r;
+ char line[1024], ack[80];;
+ pid_t ppid;
+
+ setproctitle("writer - init");
+ ppid = getppid();
+ signal(SIGUSR1, handler);
+ alarm(60);
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ hostent = gethostbyname ("localhost");
+ bzero(&inetaddr, sizeof(inetaddr));
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ setproctitle("writer");
+ barrier(BARRIER_DELETE);
+ alarm(0);
+ while (fgets(line, sizeof(line), stdin) != NULL) {
+ alarm(10);
+ alarm_exit = 1;
+ if (write(tcpsock, line, strlen(line)) < 0)
+ err(1, "socket write(). %s:%d", __FILE__, __LINE__);
+ alarm_exit = 0;
+ ualarm(5000, 0);
+ if (recvfrom(tcpsock, ack, 4, 0, NULL, NULL) < 0) {
+ if (errno == EAGAIN)
+ continue;
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ }
+ }
+ sleep(30);
+ return;
+}
+
+int
+main(int argc, char **argv)
+{
+
+ pid_t kpid;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <port number>\n", argv[0]);
+ port = atoi(argv[1]);
+ bufsize = 128;
+
+ barrier(BARRIER_CREATE);
+ signal(SIGCHLD, SIG_IGN);
+ if ((pid = fork()) == 0)
+ reader();
+
+ if ((kpid = fork()) == 0)
+ killer();
+
+ writer();
+ sleep(1);
+ kill(pid, SIGINT);
+ kill(kpid, SIGINT);
+
+ return (0);
+}
+EOF
+mycc -o /tmp/crlogger -Wall -Wextra -O2 -g /tmp/crlogger.c
+rm -f /tmp/crlogger.c
+
+N=200
+cd /tmp
+start=`date '+%s'`
+for i in `jot 40`; do
+ for j in `jot $N`; do
+ /tmp/crwriter | /tmp/crlogger 1236$j 2>/dev/null &
+ done
+
+ for j in `jot $N`; do
+ wait
+ done
+ [ $((`date '+%s'` - start)) -gt 1200 ] && break
+done
+rm -f /tmp/crwriter /tmp/crlogger ./barrier.*
+exit 0
diff --git a/tools/test/stress2/misc/callout_reset_on2.sh b/tools/test/stress2/misc/callout_reset_on2.sh
new file mode 100755
index 000000000000..270aeb95aa97
--- /dev/null
+++ b/tools/test/stress2/misc/callout_reset_on2.sh
@@ -0,0 +1,303 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Copy of callout_reset_on.sh. Waiting to see if this catches anything.
+
+. ../default.cfg
+
+rm -f /tmp/crwriter2 /tmp/crlogger2 || exit 1
+
+cat > /tmp/crwriter2.c <<EOF
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+char *txt[] = {
+ "0 This is a line of text: abcdefghijklmnopqrstuvwxyz",
+ "1 Another line of text: ABCDEFGHIJKLMNOPQRSTUVWXYZ",
+ "2 A different line of text",
+ "3 A very, very different text",
+ "4 A much longer line with a lot of characters in the line",
+ "5 Now this is a quite long line of text, with both upper and lower case letters, and one digit!"
+};
+
+#define RUNTIME (10 * 60)
+
+int
+main(void)
+{
+ time_t start;
+ int j, n;
+ char help[256];
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ j = arc4random() % 6;
+ n = arc4random() % strlen(txt[j]);
+ strncpy(help, txt[j], n);
+ help[n] = 0;
+ printf("%s\n", txt[j]);
+ if ((arc4random() % 1000) == 1)
+ usleep(100000);
+ }
+
+ return (0);
+}
+EOF
+mycc -o /tmp/crwriter2 -Wall -Wextra -O2 -g /tmp/crwriter2.c
+rm -f /tmp/crwriter2.c
+
+cat > /tmp/crlogger2.c <<EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <unistd.h>
+
+volatile u_int *share;
+
+#define SYNC 0
+
+pid_t pid;
+int bufsize;
+int port;
+int alarm_exit;
+
+void
+killer(void)
+{
+ setproctitle("killer");
+ while (share[SYNC] == 0)
+ ;
+ alarm(120);
+ for (;;) {
+ if (pid == 0)
+ break;
+ if (kill(pid, SIGUSR1) == -1)
+ break;
+ usleep(arc4random() % 2000 + 10);
+ }
+ _exit(0);
+}
+
+void
+handler(int s __unused)
+{
+}
+
+void
+ahandler(int s __unused)
+{
+ if (alarm_exit)
+ _exit(0);
+}
+
+/* Read form socket, discard */
+static void
+reader(void) {
+ int tcpsock, msgsock;
+ int on;
+ socklen_t len;
+ struct sockaddr_in inetaddr, inetpeer;
+ int n, *buf;
+
+ setproctitle("reader - init");
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ signal(SIGUSR1, handler);
+ alarm(60);
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+ setproctitle("reader");
+ alarm(0);
+ signal(SIGALRM, ahandler);
+ for (;;) {
+ ualarm(arc4random() % 5000 + 100, 0);
+ if ((n = recvfrom(msgsock, buf, bufsize, 0, NULL, NULL)) < 0) {
+ if (errno == EAGAIN)
+ continue;
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ }
+ if (n == 0)
+ break;
+ if (write(msgsock, "OK", 3) != 3)
+ err(1, "write ack. %s:%d", __FILE__, __LINE__);
+
+ }
+ close(msgsock);
+ _exit(0);
+}
+
+/* read from stdin, write to socket */
+static void
+writer(void) {
+ int tcpsock, on;
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ int i, r;
+ char line[1024], ack[80];;
+ pid_t ppid;
+
+ setproctitle("writer - init");
+ share[SYNC] = 1;
+ ppid = getppid();
+ signal(SIGUSR1, handler);
+ signal(SIGALRM, ahandler);
+ alarm(60);
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ hostent = gethostbyname ("localhost");
+ bzero(&inetaddr, sizeof(inetaddr));
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ setproctitle("writer");
+ alarm(0);
+ while (fgets(line, sizeof(line), stdin) != NULL) {
+ alarm(10);
+ alarm_exit = 1;
+ if (write(tcpsock, line, strlen(line)) < 0)
+ err(1, "socket write(). %s:%d", __FILE__, __LINE__);
+ alarm_exit = 0;
+ ualarm(arc4random() % 5000 + 1000, 0);
+ if (recvfrom(tcpsock, ack, 4, 0, NULL, NULL) < 0) {
+ if (errno == EAGAIN)
+ continue;
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ }
+ }
+ sleep(30);
+ return;
+}
+
+int
+main(int argc, char **argv)
+{
+
+ pid_t kpid;
+ size_t len;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <port number>\n", argv[0]);
+ _exit(1);
+ }
+ port = atoi(argv[1]);
+ bufsize = 128;
+
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ signal(SIGCHLD, SIG_IGN);
+ if ((pid = fork()) == 0)
+ reader();
+
+ if ((kpid = fork()) == 0)
+ killer();
+
+ writer();
+ sleep(1);
+ kill(pid, SIGINT);
+ kill(kpid, SIGINT);
+
+ return (0);
+}
+EOF
+mycc -o /tmp/crlogger2 -Wall -Wextra -O2 -g /tmp/crlogger2.c
+rm -f /tmp/crlogger2.c
+
+N=50
+cd /tmp
+for j in `jot $N`; do
+ /tmp/crwriter2 | /tmp/crlogger2 1236$j &
+done
+wait
+rm -f /tmp/crwriter2 /tmp/crlogger2
+exit 0
diff --git a/tools/test/stress2/misc/chain.sh b/tools/test/stress2/misc/chain.sh
new file mode 100755
index 000000000000..46d2f32cd7c4
--- /dev/null
+++ b/tools/test/stress2/misc/chain.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Jeremy <peterj@FreeBSD.org>
+#
+# 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 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.
+#
+# Unkillable process in "vm map (user)" seen.
+# https://people.freebsd.org/~pho/stress/log/kostik1070.txt
+# Fixed by: r327468
+
+# OOM killing: https://people.freebsd.org/~pho/stress/log/chain.txt
+
+if [ ! -f /usr/local/include/libmill.h -o \
+ ! -x /usr/local/lib/libmill.so ]; then
+ echo "ports/devel/libmill needed."
+ exit 0
+fi
+
+. ../default.cfg
+
+cat > /tmp/chain.c <<EOF
+#include <stdio.h>
+#include <stdlib.h>
+#include <libmill.h>
+
+coroutine void f(chan left, chan right) {
+ chs(left, int, 1 + chr(right, int));
+}
+
+int
+main(int argc __unused, char **argv)
+{
+ int i, n = argv[1] ? atoi(argv[1]) : 10000;
+ chan leftmost = chmake(int, 0);
+ chan left = NULL;
+ chan right = leftmost;
+
+ alarm(600);
+ for (i = 0; i < n; i++) {
+ left = right;
+ right = chmake(int, 0);
+ go(f(left, right));
+ }
+ chs(right, int, 0);
+ i = chr(leftmost, int);
+ printf("result = %d\n", i);
+ return(0);
+}
+EOF
+
+mycc -o /tmp/chain -I /usr/local/include -L /usr/local/lib -Wall -Wextra \
+ -O2 -g /tmp/chain.c -lmill || exit 1
+limits -c 0 /tmp/chain 1000000
+rm -f /tmp/chain /tmp/chain.c
+exit 0
diff --git a/tools/test/stress2/misc/churn.sh b/tools/test/stress2/misc/churn.sh
new file mode 100755
index 000000000000..6d40381e32bd
--- /dev/null
+++ b/tools/test/stress2/misc/churn.sh
@@ -0,0 +1,211 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# VM test. No problems seen.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/churn.c
+mycc -o churn -Wall -Wextra -O0 -g churn.c -lpthread || exit 1
+rm -f churn.c
+
+/tmp/churn `sysctl -n hw.ncpu` `sysctl -n hw.usermem`
+s=$?
+
+rm -rf /tmp/churn
+exit $?
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <assert.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#ifdef __FreeBSD__
+#include <pthread_np.h>
+#define __NP__
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define ARRAYSIZE (4 * 1024)
+#define RUNTIME (10 * 60)
+#define SYNC 0
+
+volatile u_int *share;
+long mem, parallel;
+int done;
+
+struct {
+ void *addr;
+ long pages;
+ volatile u_int busy;
+} v[ARRAYSIZE];
+
+void *
+test2(void *arg __unused)
+{
+ long i, j, n;
+ volatile char *cp;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+ while (done == 0) {
+ n = 0;
+ for (i = 0; i < ARRAYSIZE; i++) {
+ if (v[i].pages == 0)
+ continue;
+ atomic_add_int(&v[i].busy, 1);
+ if (v[i].busy != 1) {
+ atomic_add_int(&v[i].busy, -1);
+ continue;
+ }
+ cp = v[i].addr;
+ for (j = 0; j < v[i].pages; j++)
+ cp[j * PAGE_SIZE] = 1;
+ atomic_add_int(&v[i].busy, -1);
+ n++;
+ }
+ if (n == 0) {
+ usleep(10000);
+ }
+ }
+
+ return (0);
+}
+
+void
+test(void)
+{
+ pthread_t tp[2];
+ size_t len;
+ time_t start;
+ long i, j, k, size;
+ int r;
+ volatile char *cp;
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != (volatile u_int)parallel)
+ ;
+
+ if ((r = pthread_create(&tp[0], NULL, test2, NULL)) != 0)
+ errc(1, r, "pthread_create");
+ if ((r = pthread_create(&tp[1], NULL, test2, NULL)) != 0)
+ errc(1, r, "pthread_create");
+ size = 0;
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ for (k = 0; k < ARRAYSIZE; k++) {
+ i = arc4random() % ARRAYSIZE;
+ if (v[i].pages != 0)
+ break;
+ }
+ if (v[i].addr != NULL) {
+ atomic_add_int(&v[i].busy, 1);
+ if (v[i].busy != 1) {
+ atomic_add_int(&v[i].busy, -1);
+ continue;
+ }
+ if (munmap(v[i].addr, v[i].pages * PAGE_SIZE) == -1)
+ err(1, "munmap(%p, %ld)", v[i].addr, v[i].pages);
+ v[i].addr = NULL;
+ size -= v[i].pages;
+ v[i].pages = 0;
+ atomic_add_int(&v[i].busy, -1);
+ }
+ if (size < mem) {
+ j = round_page((arc4random() % (mem / 10)) + 1);
+ len = j * PAGE_SIZE;
+ if ((v[i].addr = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED) {
+ v[i].addr = NULL;
+ continue;
+ }
+ atomic_add_int(&v[i].busy, 1);
+ v[i].pages = j;
+ size += j;
+ assert(size > 0);
+ cp = v[i].addr;
+ for (k = 0; k < j * PAGE_SIZE; k += PAGE_SIZE)
+ cp[k] = 1;
+ atomic_add_int(&v[i].busy, -1);
+ }
+ }
+ done = 1;
+ pthread_join(tp[0], NULL);
+ pthread_join(tp[1], NULL);
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ size_t len;
+ int e, i, *pids, status;
+
+ if (argc != 3) {
+ fprintf(stderr, "Usage: %s <memory>\n", argv[0]);
+ _exit(1);
+ }
+
+ parallel = atol(argv[1]);
+ mem = atol(argv[2]) / PAGE_SIZE;
+ mem = mem / parallel;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ pids = malloc(sizeof(void *) * parallel);
+ for (i = 0; i < parallel; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ for (i = 0; i < parallel; i++) {
+ waitpid(pids[i], &status, 0);
+ e += status == 0 ? 0 : 1;
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/cleanup.sh b/tools/test/stress2/misc/cleanup.sh
new file mode 100755
index 000000000000..ac8762d225b5
--- /dev/null
+++ b/tools/test/stress2/misc/cleanup.sh
@@ -0,0 +1,105 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2013 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+bname=`basename $mntpoint`
+mounts=`mount | awk "/$bname/{print \\$3}"`
+nmounts=`sysctl -n hw.ncpu` # Max number of mounts in use
+[ $nmounts -lt 31 ] && nmounts=31 # Arbitrary value
+s=0
+# Unmount the test mount points: /mnt, /mnt10 .. mnt31
+for i in $mounts; do
+ u=`echo $i | sed "s/\/$bname//"`
+ [ -z "$u" ] && u=$mdstart
+ echo "$u" | grep -Eq '^[0-9]+$' || continue
+# [ $u -lt $mdstart -o $u -gt $((mdstart + nmounts)) ] && continue
+ while mount | grep -q "on $i "; do
+ r=`fstat -mf $i 2>/dev/null | awk '$3 ~ /^[0-9]+$/ {print $3}'`
+ if [ -n "$r" ]; then
+ echo "cleanup.sh: kill $r"
+ echo $r | xargs kill; sleep 1
+ fi
+ echo "cleanup.sh: umount -f $i"
+ umount -f $i > /dev/null 2>&1 || s=1
+ [ -z "$r" ] && break
+ done
+done
+
+# Delete the test mount points /mnt10 .. /mnt31
+for i in `ls -d $mntpoint* 2>/dev/null | grep -Ev '^$mntpoint$'`; do
+ u=`echo $i | sed "s/\/$bname//"`
+ echo "$u" | grep -Eq '^[0-9]+$' || continue
+# [ $u -lt $mdstart -o $u -gt $((mdstart + nmounts)) ] && continue
+ if ! mount | grep -q "on $i "; then
+ [ -d $i ] && find $i -delete \
+ 2>/dev/null
+ rm -rf $i > /dev/null 2>&1
+ fi
+done
+
+# Delete the memory disks
+units=`mdconfig -l | sed 's/md//g'`
+for u in $units; do
+ if [ $u -ge $mdstart -a $u -lt $((mdstart + nmounts)) ]; then
+ echo "cleanup.sh: mdconfig -d -u $u"
+ mdconfig -d -u $u || s=1
+ [ -c /dev/md$u ] && sleep .1
+ fi
+done
+
+[ -d "$mntpoint" ] && (cd $mntpoint && find . -delete)
+rm -f /tmp/.snap/stress2* /var/.snap/stress2*
+rm -rf /tmp/stressX.control $RUNDIR
+[ -d `dirname "$diskimage"` ] || mkdir -p `dirname "$diskimage"`
+mkdir -p $RUNDIR
+chmod 0777 $RUNDIR
+
+# Delete $testuser's ipcs
+ipcs | awk "\$5 ~/$testuser/ && \$6 ~/$testuser/ {print \"-\" \$1,\$2}" | \
+ xargs -t ipcrm
+
+# Modules
+#mlist=/tmp/stress2.d/mlist
+#find $mlist -mtime +1 -delete 2>/dev/null
+#[ -f $mlist ] && touch $mlist ||
+# { kldstat | kldstat | awk '/\.ko$/ {print $NF}' > $mlist; \
+# echo "Updating $mlist"; }
+#for m in `kldstat | awk '/\.ko$/ {print $NF}'`; do
+# grep -q $m $mlist && continue
+# echo "pty.ko mqueuefs.ko ioat.ko ums.ko" | grep -q $m && continue
+# echo "cleanup.sh: kldunload $m"
+# kldunload $m
+#done
+# unloading an active dtrace causes a panic
+#kldstat | grep -q dtraceall.ko && kldunload dtraceall.ko
+kldstat | grep -q ext2fs.ko && kldunload ext2fs.ko
+[ $s -ne 0 ] && echo "cleanup.sh: FAIL $s"
+exit $s
diff --git a/tools/test/stress2/misc/cluster.sh b/tools/test/stress2/misc/cluster.sh
new file mode 100755
index 000000000000..bcda95da4755
--- /dev/null
+++ b/tools/test/stress2/misc/cluster.sh
@@ -0,0 +1,269 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Open four (sparse) files for random read and write.
+
+# "panic: softdep_deallocate_dependencies: dangling deps" seen:
+# https://people.freebsd.org/~pho/stress/log/kirk075.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+dir=`dirname $diskimage`
+free=`df -k $dir | tail -1 | awk '{print $4}'`
+[ $((free / 1024 / 1024)) -lt 9 ] && echo "Not enough disk space." && exit
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > cluster.c
+rm -f /tmp/cluster
+mycc -o cluster -Wall -Wextra -g -O2 cluster.c || exit 1
+rm -f cluster.c
+cd $odir
+
+su $testuser -c "/tmp/cluster $dir abc"
+
+rm -f /tmp/cluster
+exit 0
+EOF
+#include <sys/param.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define BSIZE (8 * 1024 * 1024)
+#define MX (8LL * 1024 * 1024 * 1024)
+#define PARALLEL 4
+#define RUNTIME 600
+#define WRLOOPS 1024
+
+int rfd;
+char *buf;
+char *path;
+char *uid;
+char file[MAXPATHLEN + 1];
+
+unsigned long long
+rnd(void) {
+ unsigned long long v;
+
+ read(rfd, &v, sizeof(v));
+ v = v % MX;
+ return (v);
+}
+
+void
+wr(int idx)
+{
+ off_t offset;
+ size_t ln;
+ int fd, i, n;
+
+ snprintf(file, sizeof(file), "%s/f.%s.%06d", path, uid, idx);
+ setproctitle(__func__);
+ if ((fd = open(file, O_RDWR | O_CREAT, 0644)) == -1)
+ err(1, "open(%s)", file);
+ n = arc4random() % WRLOOPS + 1;
+ for (i = 0; i < n; i++) {
+ ln = rnd() % BSIZE + 1;
+ offset = rnd() % (MX - ln);
+ if (lseek(fd, offset, SEEK_SET) == -1)
+ err(1, "lseek in rw 1");
+ while (lockf(fd, F_LOCK, ln) == -1) {
+ if (errno != EDEADLK)
+ err(1, "lockf(%s, F_LOCK)", file);
+ }
+ if (write(fd, buf, ln) < 0)
+ err(1, "write");
+ if (lseek(fd, offset, SEEK_SET) == -1)
+ err(1, "lseek in rw 2");
+ if (lockf(fd, F_ULOCK, ln) == -1)
+ err(1, "lockf(%s, F_ULOCK)", file);
+ }
+ close(fd);
+ _exit(0);
+}
+
+void
+rd(int idx)
+{
+ off_t offset;
+ size_t ln;
+ int fd, i, n;
+
+ snprintf(file, sizeof(file), "%s/f.%s.%06d", path, uid, idx);
+ setproctitle(__func__);
+ for (i = 0; i < 100; i++) {
+ if (access(file, R_OK) == 0)
+ break;
+ usleep(1000);
+ }
+ if ((fd = open(file, O_RDONLY)) == -1)
+ if (errno != ENOENT)
+ err(1, "open(%s)for read", file);
+ n = arc4random() % WRLOOPS + 1;
+ for (i = 0; i < n; i++) {
+ ln = rnd() % BSIZE + 1;
+ offset = rnd() % (MX - ln);
+ if (lseek(fd, offset, SEEK_SET) == -1) {
+ if (errno == EBADF)
+ continue;
+ err(1, "lseek in rd");
+ }
+ if (read(fd, buf, ln) < 0)
+ err(1, "write");
+ }
+ close(fd);
+ _exit(0);
+}
+
+void
+mv(int idx)
+{
+ int i;
+ char file2[MAXPATHLEN + 1];
+
+ snprintf(file, sizeof(file), "%s/f.%s.%06d", path, uid, idx);
+ snprintf(file2, sizeof(file2), "%s/f.%s.%06d.old", path, uid, idx);
+ for (i = 0; i < 100; i++) {
+ if (access(file, R_OK) == 0)
+ break;
+ usleep(1000);
+ }
+ if (rename(file, file2) == -1)
+ if (errno != ENOENT)
+ warn("rename(%s, %s)", file, file2);
+ _exit(0);
+}
+
+void
+tr(int idx)
+{
+ off_t offset;
+ int fd;
+
+ if (arc4random() % 100 < 10) {
+ snprintf(file, sizeof(file), "%s/f.%s.%06d", path, uid, idx);
+ setproctitle(__func__);
+ if ((fd = open(file, O_RDWR | O_CREAT, 0644)) == -1)
+ err(1, "open(%s)for read", file);
+ offset = rnd() % MX;
+ offset = rnd();
+ if (ftruncate(fd, offset) == -1)
+ err(1, "truncate");
+ close(fd);
+ }
+ _exit(0);
+}
+
+void
+rm(int idx)
+{
+ int i;
+ char file2[MAXPATHLEN + 1];
+
+ snprintf(file2, sizeof(file2), "%s/f.%s.%06d.old", path, uid, idx);
+ for (i = 0; i < 100; i++) {
+ if (access(file2, R_OK) == 0)
+ break;
+ usleep(1000);
+ }
+ if (unlink(file2) == -1)
+ if (errno != ENOENT)
+ warn("unlink(%s)", file2);
+ _exit(0);
+}
+
+void
+test2(void (*func)(int nr))
+{
+ time_t start;
+ int i;
+
+ setproctitle(__func__);
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ func(i);
+ }
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+ }
+ _exit(0);
+
+}
+
+void
+test(void (*func)(int nr))
+{
+
+ if (fork() == 0)
+ test2(func);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int i;
+
+ if (argc != 3)
+ errx(1, "Usage: %s <path> <uid>", argv[0]);
+
+ path = argv[1];
+ uid = argv[2];
+
+ if ((rfd = open("/dev/random", O_RDONLY)) == -1)
+ err(1, "open(/dev/random)");
+ setproctitle(__func__);
+ buf = malloc(BSIZE);
+ test(wr);
+ test(rd);
+ test(tr);
+ test(mv);
+ for (i = 0; i < 4; i++)
+ if (wait(NULL) == -1)
+ err(1, "wait");
+
+ for (i = 0; i < PARALLEL; i++) {
+ snprintf(file, sizeof(file), "%s/f.%s.%06d", path, uid, i);
+ unlink(file);
+ snprintf(file, sizeof(file), "%s/f.%s.%06d.old", path, uid, i);
+ unlink(file);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/cmp.sh b/tools/test/stress2/misc/cmp.sh
new file mode 100755
index 000000000000..bd492c8493ca
--- /dev/null
+++ b/tools/test/stress2/misc/cmp.sh
@@ -0,0 +1,187 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Cross mount test of mkdir(2).
+# Page fault seen:
+# http://people.freebsd.org/~pho/stress/log/cmp.txt
+# Fixed by r275347
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > cmp.c
+mycc -o cmp -Wall -Wextra -O2 -g cmp.c || exit 1
+rm -f cmp.c
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+# Don't use SU due to bogus "out of inodes" messages.
+newfs md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+daemon sh -c "(cd $here/../testcases/swap; ./swap -t 5m -i 20 -h -l 100)" \
+ > /dev/null 2>&1
+sleep 1
+su $testuser -c "/tmp/cmp $mntpoint" &
+
+while kill -0 $! 2>/dev/null; do
+ umount -f $mntpoint &&
+ mount /dev/md${mdstart}$part $mntpoint
+ chmod 777 $mntpoint
+ sleep .1
+done
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+[ -d "$mntpoint" ] && (cd $mntpoint && find . -delete)
+
+# tmpfs
+mount -t tmpfs tmpfs $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c "/tmp/cmp $mntpoint" &
+
+while kill -0 $! 2>/dev/null; do
+ umount -f $mntpoint &&
+ mount -t tmpfs tmpfs $mntpoint
+ chmod 777 $mntpoint
+ sleep .1
+done
+pkill -9 swap
+wait
+
+while pkill -9 swap; do
+ :
+done > /dev/null 2>&1
+while mount | grep $mntpoint | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
+[ -d "$mntpoint" ] && (cd $mntpoint && find . -delete)
+rm -f /tmp/cmp
+exit 0
+EOF
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define LOOPS 160
+#define PARALLEL 16
+
+int nbc, nbd;
+char *dir;
+
+void
+tmkdir(void)
+{
+ int i, j;
+ char d[MAXPATHLEN + 1], name[MAXPATHLEN + 1];
+
+ setproctitle(__func__);
+
+ i = 0;
+ snprintf(name, sizeof(name), "%s/d1.%05d", dir, getpid());
+ if (mkdir(name, 0755) == -1) {
+ if (errno != ENAMETOOLONG && errno != ENOENT &&
+ errno != EBUSY && errno != EACCES && errno != EPERM)
+ warn("mkdir(%s)", name);
+ _exit(0);
+ }
+ for (;;) {
+ snprintf(d, sizeof(d), "/%d", i++);
+ strncat(name, d, sizeof(name) - 1);
+ if (mkdir(name, 0755) == -1) {
+ if (errno != ENAMETOOLONG && errno != ENOENT &&
+ errno != EBUSY && errno != EACCES && errno != EPERM)
+ warn("mkdir(%s)", name);
+ i--;
+ break;
+ }
+ nbc++;
+ }
+
+ while (i >= 0) {
+ snprintf(name, sizeof(name), "%s/d1.%05d", dir, getpid());
+ for (j = 0; j < i; j++) {
+ snprintf(d, sizeof(d), "/%d", j);
+ strncat(name, d, sizeof(name) - 1);
+ }
+ if (rmdir(name) == -1) {
+ if (errno != ENOTEMPTY && errno != ENOENT && errno !=
+ EBUSY)
+ warn("rmdir(%s)", name);
+ } else
+ nbd++;
+ i--;
+ }
+#if defined(TEST)
+ if (nbc == 0)
+ fprintf(stderr, "FAIL nbc = %d, nbd = %d\n", nbc, nbd);
+#endif
+ _exit(0);
+}
+
+int
+main(int argc, char **argv)
+{
+ int i, j;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <full path to dir>", argv[0]);
+ exit(1);
+ }
+ dir = argv[1];
+
+ for (j = 0; j < LOOPS; j++) {
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ tmkdir();
+ }
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+ }
+
+ return(0);
+}
diff --git a/tools/test/stress2/misc/collapse.sh b/tools/test/stress2/misc/collapse.sh
new file mode 100755
index 000000000000..fc903d49c6a2
--- /dev/null
+++ b/tools/test/stress2/misc/collapse.sh
@@ -0,0 +1,153 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2020 Jeffrey Roberson <jeff@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 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.
+#
+
+. ../default.cfg
+
+# "panic: freeing mapped page 0xfffffe000aa73910" seen:
+# https://people.freebsd.org/~pho/stress/log/collapse.txt
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > collapse.c
+mycc -o collapse -Wall -Wextra -g -O0 collapse.c || exit 1
+rm -f collapse.c
+cd $odir
+
+daemon sh -c '(cd ../testcases/swap; ./swap -t 20m -i 16 -l 85)' > \
+ /dev/null 2>&1
+sleep 2
+/tmp/collapse
+while pgrep -q swap; do
+ pkill -9 swap
+done
+rm -f /tmp/collapse
+exit 0
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define ADRSPACE (256 * 1024)
+#define DEPTH 6
+#define WIDTH 3
+#define PARALLEL 4
+#define RUNTIME 1200
+#define CHILDTIME 5
+#define STARTTIME 5
+#define TOUCH 16
+
+char *p;
+
+static void
+child(int depth, time_t start)
+{
+ time_t run, delay;
+ int i, shared, off;
+ int len;
+
+ /* Pick a random bit of address space to change inherit on. */
+ for (i = 0; i < ADRSPACE; i += len) {
+ shared = arc4random() & 0x1;
+ len = roundup2(arc4random() % ((ADRSPACE - i) / 4),
+ PAGE_SIZE);
+ if (minherit(p + i, len, shared ? INHERIT_SHARE :
+ INHERIT_COPY) != 0)
+ err(1, "minherit");
+ }
+
+ for (i = 0; depth != 0 && i < WIDTH; i++)
+ if (fork() == 0)
+ child(depth - 1, start);
+
+ /*
+ * Touch all of the memory and exit at a random time to collapse
+ * some portion of the chain.
+ */
+ delay = arc4random() % (CHILDTIME - 1);
+ run = arc4random() % (CHILDTIME - delay);
+ for (;;) {
+ if (time(NULL) >= start + delay)
+ break;
+ usleep(100);
+ }
+ while (time(NULL) - start < run) {
+ off = rounddown2(arc4random() % ADRSPACE, PAGE_SIZE);
+ bzero(p + off, PAGE_SIZE);
+ usleep((run * 1000) / TOUCH);
+ }
+
+ _exit(0);
+}
+
+static void
+work(int depth)
+{
+
+ if ((p = mmap(NULL, ADRSPACE, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANON, -1, 0)) == MAP_FAILED) {
+ if (errno == ENOMEM)
+ return;
+ err(1, "mmap()");
+ }
+ child(depth, time(NULL) + STARTTIME);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ time_t start;
+ int i, n;
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ n = arc4random() % PARALLEL + 1;
+ for (i = 0; i < n; i++) {
+ if ((pids[i] = fork()) == 0)
+ work(DEPTH);
+ }
+
+ sleep(CHILDTIME + STARTTIME + 1);
+
+ for (i = 0; i < n; i++)
+ if (waitpid(pids[i], NULL, 0) != pids[i])
+ err(1, "waitpid(%d)", pids[i]);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/compare.sh b/tools/test/stress2/misc/compare.sh
new file mode 100755
index 000000000000..c983046384b5
--- /dev/null
+++ b/tools/test/stress2/misc/compare.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Compare two disk files.
+
+diskimage=/var/tmp/diskimage
+
+d1=$diskimage
+d2=${diskimage}2
+have=`df -k $(dirname $d1) | tail -1 | awk '{print int($4 / 1024)}'`
+[ $have -lt 1024 ] &&
+ { echo "Not enough disk space on `dirname $d1`"; exit 1; }
+
+dd if=/dev/random of=$d1 bs=1m count=512 status=none || exit 1
+cp $d1 $d2
+cmp $d1 $d2 && s=0 || { echo FAIL; s=1; }
+rm -f $d1 $d2
+exit $s
diff --git a/tools/test/stress2/misc/context.sh b/tools/test/stress2/misc/context.sh
new file mode 100755
index 000000000000..02fc9b8df4c1
--- /dev/null
+++ b/tools/test/stress2/misc/context.sh
@@ -0,0 +1,130 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > context.c
+mycc -o context -Wall -Wextra -O2 -g context.c || exit 1
+rm -f context.c
+[ -d $RUNDIR ] || mkdir -p $RUNDIR
+cd $RUNDIR
+
+daemon sh -c "(cd $here/../testcases/swap; ./swap -t 10m -i 20)" > \
+ /dev/null 2>&1
+for i in `jot 4`; do
+ /tmp/context &
+ pids="$pids $!"
+done
+s=0
+for i in $pids; do
+ wait $i
+ [ $? -ne 0 ] && s=$((s + 1))
+done
+while pgrep -q swap; do
+ pkill -9 swap
+done
+rm -f /tmp/context
+exit $s
+EOF
+/*
+ * Inspired by lmbench-3.0-a9/src/lat_ctx.c
+ * Pass a token thru pipes to CHILDREN+1 processes in a circular list
+ */
+
+#include <sys/types.h>
+
+#include <err.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define CHILDREN 64
+#define RUNTIME 300
+
+int fds[CHILDREN +1 ][2];
+pid_t pid[CHILDREN];
+
+void
+handler(int s __unused)
+{
+ _exit(0);
+}
+
+int
+main(void)
+{
+ time_t start;
+ int i, j;
+ int token;
+
+ for (i = 0; i < CHILDREN + 1; i++) {
+ if (pipe(fds[i]) == -1)
+ err(1, "pipe");
+ }
+
+ signal(SIGHUP, handler);
+ start = time(NULL);
+ for (i = 0; i < CHILDREN; i++) {
+ pid[i] = fork();
+ if (pid[i] == -1) {
+ perror("fork");
+ exit(2);
+ }
+
+ if (pid[i] == 0) { /* child */
+ for (;;) {
+ if (read(fds[i][0], &token, sizeof(token))
+ != sizeof(token))
+ err(1, "read pipe 2");
+ if (write(fds[i+1][1], &token, sizeof(token))
+ != sizeof(token))
+ err(1, "write pipe 1");
+ }
+ }
+
+ } /* parent */
+
+ for (j = 0; time(NULL) - start < RUNTIME; j++) {
+ token = j;
+ if (write(fds[0][1], &token, sizeof(token)) != sizeof(token))
+ err(1, "write pipe 2");
+ if (read(fds[CHILDREN][0], &token, sizeof(token))
+ != sizeof(token))
+ err(1, "read pipe 1");
+ }
+
+ for (i = 0; i < CHILDREN; i++)
+ kill(pid[i], SIGHUP);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/context2.sh b/tools/test/stress2/misc/context2.sh
new file mode 100755
index 000000000000..093e72e9084d
--- /dev/null
+++ b/tools/test/stress2/misc/context2.sh
@@ -0,0 +1,131 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# This problem was seen with WiP kernel code:
+# https://people.freebsd.org/~pho/stress/log/kostik1210.txt
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > context2.c
+mycc -o context2 -Wall -Wextra -O2 context2.c -lpthread || exit 1
+rm -f context2.c
+[ -d $RUNDIR ] || mkdir -p $RUNDIR
+cd $RUNDIR
+
+daemon sh -c "(cd $here/../testcases/swap; ./swap -t 10m -i 20)" > \
+ /dev/null 2>&1
+for i in `jot 4`; do
+ /tmp/context2 &
+done
+wait
+while pgrep -q swap; do
+ pkill -9 swap
+done
+rm -f /tmp/context2
+exit 0
+EOF
+/*
+ * Inspired by lmbench-3.0-a9/src/lat_ctx.c
+ * Pass a token thru pipes to NTHREADS+1 threads in a circular list.
+ */
+
+#include <sys/types.h>
+
+#include <err.h>
+#include <errno.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define NTHREADS 64
+#define RUNTIME 300
+
+pid_t pid[NTHREADS];
+int fds[NTHREADS+1][2];
+
+void *
+thr_routine(void *arg)
+{
+ int i;
+ int token;
+
+ i = (long)arg;
+ for (;;) {
+ if (read(fds[i][0], &token, sizeof(token)) != sizeof(token))
+ err(1, "read pipe 2");
+ token++;
+ if (write(fds[i+1][1], &token, sizeof(token)) != sizeof(token))
+ err(1, "write pipe 1");
+ }
+ return (0);
+}
+
+int
+main(void)
+{
+ pthread_t threads[NTHREADS];
+ time_t start;
+ long arg;
+ int i, r, token;
+
+ for (i = 0; i < NTHREADS + 1; i++) {
+ if (pipe(fds[i]) == -1)
+ err(1, "pipe");
+ }
+
+ for (i = 0; i < NTHREADS; i++) {
+ arg = i;
+ if ((r = pthread_create(&threads[i], NULL, thr_routine,
+ (void *)arg)) != 0)
+ errc(1, r, "pthread_create(): %s\n", strerror(r));
+ }
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ token = 0;
+ if (write(fds[0][1], &token, sizeof(token)) != sizeof(token))
+ err(1, "write pipe 2");
+ if (read(fds[NTHREADS][0], &token, sizeof(token)) !=
+ sizeof(token))
+ err(1, "read pipe 1");
+ }
+
+ for (i = 0; i < NTHREADS; i++)
+ if ((r = pthread_cancel(threads[i])) != 0)
+ errc(1, r, "pthread_cancel(%d)", i);
+ for (i = 0; i < NTHREADS; i++)
+ if ((r = pthread_join(threads[i], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", i);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/contigmalloc.sh b/tools/test/stress2/misc/contigmalloc.sh
new file mode 100755
index 000000000000..92bf6ff40905
--- /dev/null
+++ b/tools/test/stress2/misc/contigmalloc.sh
@@ -0,0 +1,255 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# contigmalloc(9) / contigfree(9) test scenario.
+# malloc() a random number of buffers with random size and then free them.
+
+# A malloc pattern might look like this:
+# contigmalloc(186 pages)
+# contigmalloc(56 pages)
+# contigmalloc(9 pages)
+# contigmalloc(202 pages)
+# contigmalloc(49 pages)
+# contigmalloc(5 pages)
+
+# "panic: vm_reserv_alloc_contig: reserv 0xff... isn't free" seen.
+# http://people.freebsd.org/~pho/stress/log/contigmalloc.txt
+# Fixed by r271351.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -d /usr/src/sys ] || exit 0
+builddir=`sysctl kern.version | grep @ | sed 's/.*://'`
+[ -d "$builddir" ] && export KERNBUILDDIR=$builddir || exit 0
+export SYSDIR=`echo $builddir | sed 's#/sys.*#/sys#'`
+
+. ../default.cfg
+
+odir=`pwd`
+dir=/tmp/contigmalloc
+rm -rf $dir; mkdir -p $dir
+cat > $dir/ctest.c <<EOF
+#include <sys/param.h>
+#include <sys/syscall.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define min(a,b) (((a)<(b))?(a):(b))
+#define CAP (64 * 1024 * 1024) /* Total allocation */
+#define MAXBUF (32 * 1024 * 1024) /* Max buffer size */
+#define N 512 /* Max allocations */
+#define RUNTIME 120
+#define TALLOC 1
+#define TFREE 2
+
+void *p[N];
+long size[N];
+int n;
+
+void
+test(int argc, char *argv[])
+{
+ long mw, s;
+ int i, no, ps, res;
+
+ if (argc == 3) {
+ no = atoi(argv[1]);
+ mw = atol(argv[2]);
+ }
+ if (argc != 3 || no == 0 || mw == 0)
+ errx(1, "Usage: %s <syscall number> <max wired>", argv[0]);
+
+ ps = getpagesize();
+ s = 0;
+ n = arc4random() % N + 1;
+ mw = mw / 100 * 10 * ps; /* Use 10% of vm.max_user_wired */
+ mw = min(mw, CAP);
+ for (i = 0; i < n; i++) {
+ size[i] = round_page((arc4random() % MAXBUF) + 1);
+ if (s + size[i] > mw)
+ continue;
+ res = syscall(no, TALLOC, &p[i], &size[i]);
+ if (res == -1) {
+ warn("contigmalloc(%lu pages) failed at loop %d",
+ size[i] / ps, i);
+ usleep(200000);
+ } else {
+#if defined(TEST)
+ fprintf(stderr, "contigmalloc(%lu pages)\n",
+ size[i] / ps);
+#endif
+ s += size[i];
+ }
+ }
+
+ setproctitle("%ld Mb", s / 1024 / 1024);
+
+ for (i = 0; i < n; i++) {
+ if (p[i] != NULL) {
+ res = syscall(no, TFREE, &p[i], &size[i]);
+#if defined(TEST)
+ fprintf(stderr, "contigfree(%lu pages)\n",
+ size[i] / ps);
+#endif
+ p[i] = NULL;
+ }
+ }
+}
+
+int
+main(int argc, char *argv[])
+{
+ time_t start;
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME)
+ test(argc, argv);
+
+ return (0);
+}
+
+EOF
+mycc -o /tmp/ctest -Wall -Wextra -O0 -g $dir/ctest.c || exit 1
+rm $dir/ctest.c
+
+cd $dir
+cat > Makefile <<EOF
+KMOD= cmalloc
+SRCS= cmalloc.c
+
+.include <bsd.kmod.mk>
+EOF
+
+sed '1,/^EOF2/d' < $odir/$0 > cmalloc.c
+make || exit 1
+kldload $dir/cmalloc.ko || exit 1
+
+cd $odir
+mw=`sysctl -n vm.max_user_wired` || exit 1
+/tmp/ctest `sysctl -n debug.cmalloc_offset` $mw 2>&1 | tail -5
+kldunload $dir/cmalloc.ko
+rm -rf $dir /tmp/ctest
+exit 0
+
+EOF2
+#include <sys/param.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+#include <sys/module.h>
+#include <sys/proc.h>
+#include <sys/sysctl.h>
+#include <sys/sysent.h>
+#include <sys/sysproto.h>
+#include <sys/systm.h>
+
+#define TALLOC 1
+#define TFREE 2
+
+/*
+ * Hook up a syscall for contigmalloc testing.
+ */
+
+struct cmalloc_args {
+ int a_op;
+ void *a_ptr;
+ void *a_size;
+};
+
+static int
+cmalloc(struct thread *td, struct cmalloc_args *uap)
+{
+ void *p;
+ unsigned long size;
+ int error;
+
+ error = copyin(uap->a_size, &size, sizeof(size));
+ if (error != 0) {
+ return (error);
+ }
+ switch (uap->a_op) {
+ case TFREE:
+ error = copyin(uap->a_ptr, &p, sizeof(p));
+ if (error == 0) {
+ if (p != NULL)
+ contigfree(p, size, M_TEMP);
+ }
+ return (error);
+
+ case TALLOC:
+ p = contigmalloc(size, M_TEMP, M_NOWAIT, 0ul, ~0ul, 4096, 0);
+ if (p != NULL) {
+ error = copyout(&p, uap->a_ptr, sizeof(p));
+ return (error);
+ }
+ return (ENOMEM);
+ }
+ return (EINVAL);
+}
+
+/*
+ * The sysent for the new syscall
+ */
+static struct sysent cmalloc_sysent = {
+ .sy_narg = 3, /* sy_narg */
+ .sy_call = (sy_call_t *) cmalloc /* sy_call */
+};
+
+/*
+ * The offset in sysent where the syscall is allocated.
+ */
+static int cmalloc_offset = NO_SYSCALL;
+
+SYSCTL_INT(_debug, OID_AUTO, cmalloc_offset, CTLFLAG_RD, &cmalloc_offset, 0,
+ "cmalloc syscall number");
+
+/*
+ * The function called at load/unload.
+ */
+
+static int
+cmalloc_load(struct module *module, int cmd, void *arg)
+{
+ int error = 0;
+
+ switch (cmd) {
+ case MOD_LOAD :
+ break;
+ case MOD_UNLOAD :
+ break;
+ default :
+ error = EOPNOTSUPP;
+ break;
+ }
+ return (error);
+}
+
+SYSCALL_MODULE(cmalloc_syscall, &cmalloc_offset, &cmalloc_sysent,
+ cmalloc_load, NULL);
diff --git a/tools/test/stress2/misc/contigmalloc2.sh b/tools/test/stress2/misc/contigmalloc2.sh
new file mode 100755
index 000000000000..e3ec5bf3a358
--- /dev/null
+++ b/tools/test/stress2/misc/contigmalloc2.sh
@@ -0,0 +1,223 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# contigmalloc(9) / contigfree(9) test scenario.
+# Regression test for allocations >= 2 GiB.
+# "panic: vm_page_insert_after: mpred doesn't precede pindex" seen.
+# Fixed by r284207.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -d /usr/src/sys ] || exit 0
+builddir=`sysctl kern.version | grep @ | sed 's/.*://'`
+[ -d "$builddir" ] && export KERNBUILDDIR=$builddir || exit 0
+export SYSDIR=`echo $builddir | sed 's#/sys.*#/sys#'`
+
+. ../default.cfg
+
+odir=`pwd`
+dir=/tmp/contigmalloc
+rm -rf $dir; mkdir -p $dir
+cat > $dir/ctest2.c <<EOF
+#include <sys/param.h>
+#include <sys/syscall.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define TALLOC 1
+#define TFREE 2
+
+void *p;
+long size;
+int n;
+
+void
+test(int argc, char *argv[])
+{
+ long mw;
+ int no, ps, res;
+
+ if (argc == 3) {
+ no = atoi(argv[1]);
+ mw = atol(argv[2]);
+ }
+ if (argc != 3 || no == 0 || mw == 0)
+ errx(1, "Usage: %s <syscall number> <max wired>", argv[0]);
+
+ ps = getpagesize();
+ size = mw / 100 * 80 * ps; /* Use 80% of vm.max_user_wired */
+ while (size > 0) {
+ res = syscall(no, TALLOC, &p, &size);
+ if (res == -1) {
+ if (errno != ENOMEM)
+ warn("contigmalloc(%lu pages) failed",
+ size);
+ } else {
+#if defined(TEST)
+ fprintf(stderr, "pre contigmalloc(%lu pages): %lu MiB\n",
+ size, size * ps / 1024 / 1024);
+#endif
+ res = syscall(no, TFREE, &p, &size);
+#if defined(TEST)
+ fprintf(stderr, "contigfree(%lu pages)\n",
+ size);
+#endif
+ }
+ size /= 2;
+ }
+}
+
+int
+main(int argc, char *argv[])
+{
+ test(argc, argv);
+
+ return (0);
+}
+
+EOF
+mycc -o /tmp/ctest2 -Wall -Wextra -O0 -g $dir/ctest2.c || exit 1
+rm $dir/ctest2.c
+
+cd $dir
+cat > Makefile <<EOF
+KMOD= cmalloc2
+SRCS= cmalloc2.c
+
+.include <bsd.kmod.mk>
+EOF
+
+sed '1,/^EOF2/d' < $odir/$0 > cmalloc2.c
+make depend all || exit 1
+kldload $dir/cmalloc2.ko || exit 1
+
+cd $odir
+mw=$((`sysctl -n vm.max_user_wired` - \
+ `sysctl -n vm.stats.vm.v_user_wire_count`)) || exit 1
+/tmp/ctest2 `sysctl -n debug.cmalloc_offset` $mw #2>&1 | tail -5
+kldunload $dir/cmalloc2.ko
+rm -rf $dir /tmp/ctest2
+exit 0
+
+EOF2
+#include <sys/param.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+#include <sys/module.h>
+#include <sys/proc.h>
+#include <sys/sysctl.h>
+#include <sys/sysent.h>
+#include <sys/sysproto.h>
+#include <sys/systm.h>
+
+#define TALLOC 1
+#define TFREE 2
+
+/*
+ * Hook up a syscall for contigmalloc testing.
+ */
+
+struct cmalloc_args {
+ int a_op;
+ void *a_ptr;
+ void *a_size;
+};
+
+static int
+cmalloc(struct thread *td, struct cmalloc_args *uap)
+{
+ void *p;
+ unsigned long size;
+ int error;
+
+ error = copyin(uap->a_size, &size, sizeof(size));
+ if (error != 0) {
+ return (error);
+ }
+ switch (uap->a_op) {
+ case TFREE:
+ error = copyin(uap->a_ptr, &p, sizeof(p));
+ if (error == 0) {
+ if (p != NULL)
+ contigfree(p, size, M_TEMP);
+ }
+ return (error);
+
+ case TALLOC:
+ p = contigmalloc(size, M_TEMP, M_NOWAIT, 0ul, ~0ul, 4096, 0);
+ if (p != NULL) {
+ error = copyout(&p, uap->a_ptr, sizeof(p));
+ return (error);
+ }
+ return (ENOMEM);
+ }
+ return (EINVAL);
+}
+
+/*
+ * The sysent for the new syscall
+ */
+static struct sysent cmalloc_sysent = {
+ .sy_narg = 3, /* sy_narg */
+ .sy_call = (sy_call_t *) cmalloc /* sy_call */
+};
+
+/*
+ * The offset in sysent where the syscall is allocated.
+ */
+static int cmalloc_offset = NO_SYSCALL;
+
+SYSCTL_INT(_debug, OID_AUTO, cmalloc_offset, CTLFLAG_RD, &cmalloc_offset, 0,
+ "cmalloc syscall number");
+
+/*
+ * The function called at load/unload.
+ */
+
+static int
+cmalloc_load(struct module *module, int cmd, void *arg)
+{
+ int error = 0;
+
+ switch (cmd) {
+ case MOD_LOAD :
+ break;
+ case MOD_UNLOAD :
+ break;
+ default :
+ error = EOPNOTSUPP;
+ break;
+ }
+ return (error);
+}
+
+SYSCALL_MODULE(cmalloc_syscall, &cmalloc_offset, &cmalloc_sysent,
+ cmalloc_load, NULL);
diff --git a/tools/test/stress2/misc/contigmalloc3.sh b/tools/test/stress2/misc/contigmalloc3.sh
new file mode 100755
index 000000000000..9497a67f21cf
--- /dev/null
+++ b/tools/test/stress2/misc/contigmalloc3.sh
@@ -0,0 +1,225 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# contigmalloc(9) / contigfree(9) test scenario.
+# Test allocation with 1GB
+
+# "panic: Bad link elm 0x6766fbc next->prev != elm" seen:
+# https://people.freebsd.org/~pho/stress/log/kostik1094.txt
+# Fixed by r331247
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -d /usr/src/sys ] || exit 0
+builddir=`sysctl kern.version | grep @ | sed 's/.*://'`
+[ -d "$builddir" ] && export KERNBUILDDIR=$builddir || exit 0
+export SYSDIR=`echo $builddir | sed 's#/sys.*#/sys#'`
+
+. ../default.cfg
+
+odir=`pwd`
+dir=/tmp/contigmalloc
+rm -rf $dir; mkdir -p $dir
+cat > $dir/ctest.c <<EOF
+#include <sys/param.h>
+#include <sys/syscall.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define MAXBUF (1LL * 1024 * 1024 * 1024) /* Max buffer size */
+#define TALLOC 1
+#define TFREE 2
+
+void
+test(int argc, char *argv[])
+{
+ long mw, size;
+ int i, no, ps, res;
+ char *cp;
+
+ if (argc == 3) {
+ no = atoi(argv[1]);
+ mw = atol(argv[2]);
+ }
+ if (argc != 3 || no == 0 || mw == 0)
+ errx(1, "Usage: %s <syscall number> <max wired>", argv[0]);
+
+ ps = getpagesize();
+ if (mw < MAXBUF / ps) {
+ fprintf(stderr, "max_user_wired too small for this test\n");
+ exit (0);
+ }
+ i = 0;
+ size = round_page(MAXBUF);
+ res = syscall(no, TALLOC, &cp, &size);
+ if (res == -1) {
+ err(1, "contigmalloc(%lu MB) failed",
+ size / 1024 / 1024);
+ } else {
+#if defined(TEST)
+ fprintf(stderr, "contigmalloc(%lu pages) %luMB\n",
+ size / ps, size / 1024 / 1024);
+#endif
+ }
+
+ res = syscall(no, TFREE, &cp, &size);
+#if defined(TEST)
+ fprintf(stderr, "contigfree(%lu pages) %luMB\n",
+ size / ps, size / 1024 / 1024);
+#endif
+}
+
+int
+main(int argc, char *argv[])
+{
+
+ test(argc, argv);
+
+ return (0);
+}
+
+EOF
+mycc -o /tmp/ctest -Wall -Wextra -O0 -g $dir/ctest.c || exit 1
+rm $dir/ctest.c
+
+cd $dir
+cat > Makefile <<EOF
+KMOD= cmalloc
+SRCS= cmalloc.c
+
+.include <bsd.kmod.mk>
+EOF
+
+sed '1,/^EOF2/d' < $odir/$0 > cmalloc.c
+make || exit 1
+kldload $dir/cmalloc.ko || exit 1
+
+cd $odir
+mw=`sysctl -n vm.max_user_wired` || exit 1
+/tmp/ctest `sysctl -n debug.cmalloc_offset` $mw 2>&1 | tail -5
+kldunload $dir/cmalloc.ko
+rm -rf $dir /tmp/ctest
+exit 0
+
+EOF2
+#include <sys/param.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+#include <sys/module.h>
+#include <sys/proc.h>
+#include <sys/sysctl.h>
+#include <sys/sysent.h>
+#include <sys/sysproto.h>
+#include <sys/systm.h>
+
+#define TALLOC 1
+#define TFREE 2
+
+/*
+ * Hook up a syscall for contigmalloc testing.
+ */
+
+struct cmalloc_args {
+ int a_op;
+ void *a_ptr;
+ void *a_size;
+};
+
+static int
+cmalloc(struct thread *td, struct cmalloc_args *uap)
+{
+ void *p;
+ unsigned long size;
+ int error;
+
+ error = copyin(uap->a_size, &size, sizeof(size));
+ if (error != 0) {
+ return (error);
+ }
+ switch (uap->a_op) {
+ case TFREE:
+ error = copyin(uap->a_ptr, &p, sizeof(p));
+ if (error == 0) {
+ if (p != NULL)
+ contigfree(p, size, M_TEMP);
+ }
+ return (error);
+
+ case TALLOC:
+ p = contigmalloc(size, M_TEMP, M_NOWAIT, 0ul, ~0ul, 4096, 0);
+ if (p != NULL) {
+ error = copyout(&p, uap->a_ptr, sizeof(p));
+ return (error);
+ }
+ return (ENOMEM);
+ }
+ return (EINVAL);
+}
+
+/*
+ * The sysent for the new syscall
+ */
+static struct sysent cmalloc_sysent = {
+ .sy_narg = 3, /* sy_narg */
+ .sy_call = (sy_call_t *) cmalloc /* sy_call */
+};
+
+/*
+ * The offset in sysent where the syscall is allocated.
+ */
+static int cmalloc_offset = NO_SYSCALL;
+
+SYSCTL_INT(_debug, OID_AUTO, cmalloc_offset, CTLFLAG_RD, &cmalloc_offset, 0,
+ "cmalloc syscall number");
+
+/*
+ * The function called at load/unload.
+ */
+
+static int
+cmalloc_load(struct module *module, int cmd, void *arg)
+{
+ int error = 0;
+
+ switch (cmd) {
+ case MOD_LOAD :
+ break;
+ case MOD_UNLOAD :
+ break;
+ default :
+ error = EOPNOTSUPP;
+ break;
+ }
+ return (error);
+}
+
+SYSCALL_MODULE(cmalloc_syscall, &cmalloc_offset, &cmalloc_sysent,
+ cmalloc_load, NULL);
diff --git a/tools/test/stress2/misc/core.sh b/tools/test/stress2/misc/core.sh
new file mode 100755
index 000000000000..10d957f1f888
--- /dev/null
+++ b/tools/test/stress2/misc/core.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Test multiple (parallel) core dumps
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+rm -f core
+sed '1,/^EOF/d' < $odir/$0 > core.c
+mycc -o core -Wall core.c
+rm -f core.c
+cd $RUNDIR
+
+for i in `jot 2`; do
+ for j in `jot 4`; do
+ /tmp/core &
+ done
+ for j in `jot 4`; do
+ wait
+ done
+done
+rm -f /tmp/core
+exit
+EOF
+#include <err.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define SIZ 10*1024*1024
+
+int
+main(int argc, char **argv)
+{
+ char *cp;
+
+ if ((cp = malloc(SIZ)) == NULL)
+ err(1, "Could not malloc 10Mb!");
+
+ memset(cp, 1, SIZ);
+
+ sleep(120 - (time(NULL) % 120));
+ raise(SIGSEGV);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/core2.sh b/tools/test/stress2/misc/core2.sh
new file mode 100755
index 000000000000..4f0e0472af95
--- /dev/null
+++ b/tools/test/stress2/misc/core2.sh
@@ -0,0 +1,95 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Test multiple (parallel) core dumps and umount
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > core2.c
+mycc -o core2 -Wall -Wextra -O0 core2.c || exit 1
+rm -f core2.c
+cd $RUNDIR
+
+mount | grep "on $mntpoint " | grep -q md$mdstart && umount $mntpoint
+[ -c /dev/mn$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+touch /tmp/continue
+for i in `jot 64`; do
+ mkdir -p $mntpoint/d$i
+ (cd $mntpoint; /tmp/core2) &
+done
+rm -f /tmp/continue
+
+for i in `jot 60`; do
+ umount $mntpoint 2>/dev/null || sleep 1
+ mount | grep -q "on $mntpoint " || break
+done
+wait
+mount | grep -q "on $mntpoint " &&
+ umount -f $mntpoint
+mdconfig -d -u $mdstart
+rm -f /tmp/core2
+exit
+EOF
+#include <sys/mman.h>
+
+#include <err.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define SIZ 1L * 128 * 1024 * 1024
+
+void *p;
+
+int
+main(void)
+{
+ size_t len;
+
+ len = SIZ;
+ p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
+
+ while (access("/tmp/continue", R_OK) == 0)
+ usleep(1);
+
+ raise(SIGSEGV);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/core3.sh b/tools/test/stress2/misc/core3.sh
new file mode 100755
index 000000000000..68d55468c087
--- /dev/null
+++ b/tools/test/stress2/misc/core3.sh
@@ -0,0 +1,171 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Test multiple (parallel) core dumps and mount / umount.
+# mount(8) stuck in "ufs" or "tmpfs".
+# http://people.freebsd.org/~pho/stress/log/kostik724.txt
+# Fixed by r272535.
+# On i386 pgrep(1) loops. Fixed by r272566.
+
+# "Sleeping on "pmapdi" with the following non-sleepable locks held:"
+# https://people.freebsd.org/~pho/stress/log/kostik883.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > core3.c
+mycc -o core3 -Wall -Wextra -O2 core3.c || exit 1
+rm -f core3.c
+cd $odir
+
+mount | grep -q "on $mntpoint " && umount $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+mkdir $mntpoint/d
+chmod 777 $mntpoint/d
+
+su $testuser -c "/tmp/core3 $mntpoint/d" &
+pid=$!
+sleep 1
+
+while pgrep -q core3; do
+ [ -d $mntpoint/d ] &&
+ umount -f $mntpoint
+done > /dev/null 2>&1 &
+while pgrep -q core3; do
+ [ -d $mntpoint/d ] ||
+ mount /dev/md${mdstart}$part $mntpoint
+done > /dev/null 2>&1
+wait $pid
+status=$?
+mount | grep -q "on $mntpoint " &&
+ umount -f $mntpoint
+mdconfig -d -u $mdstart
+[ $status -ne 0 ] && exit $status
+
+# tmpfs
+mount -o size=1g -t tmpfs tmpfs $mntpoint
+su $testuser -c "/tmp/core3 $mntpoint/d" &
+pid=$!
+sleep 1
+
+while pgrep -q core3; do
+ [ -d $mntpoint/d ] &&
+ umount -f $mntpoint
+done > /dev/null &
+while pgrep -q core3; do
+ if [ ! -d $mntpoint/d ]; then
+ mount -t tmpfs tmpfs $mntpoint
+ mkdir $mntpoint/d
+ fi
+done
+wait $pid
+status=$?
+for i in `jot 5` ; do
+ mount | grep -q "on $mntpoint " || break
+ umount -f $mntpoint
+ sleep 1
+done
+rm -f /tmp/core3
+exit $status
+EOF
+#include <sys/mman.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define PARALLEL 64
+#define SIZ (4 * 1024 * 1024)
+#define TIMEDOUT 22
+
+void *p;
+
+static void
+hand(int i __unused) { /* handler */
+ _exit(TIMEDOUT);
+}
+
+void
+test(char *argv[])
+{
+ size_t len;
+
+ len = SIZ;
+ p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
+
+ /*
+ * This loop caused mount to wait in "ufs".
+ * Adding a usleep(200) would remove the hang.
+ */
+ signal(SIGALRM, hand);
+ alarm(600);
+ while (chdir(argv[1]) == -1)
+ ;
+
+ raise(SIGSEGV);
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ time_t start;
+ int i, s, status;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <path>", argv[0]);
+
+ status = 0;
+ start = time(NULL);
+ while (time(NULL) - start < 600 && status == 0) {
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ test(argv);
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ wait(&s);
+ if (WEXITSTATUS(s) == TIMEDOUT)
+ status = 1;
+ }
+ }
+
+ return (status);
+}
diff --git a/tools/test/stress2/misc/core4.sh b/tools/test/stress2/misc/core4.sh
new file mode 100755
index 000000000000..562160a1a314
--- /dev/null
+++ b/tools/test/stress2/misc/core4.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Test multiple (parallel) core dumps and umount -f
+
+# "panic: vn_finished_write: neg cnt" seen.
+# http://people.freebsd.org/~pho/core4.sh
+# Fixed in r274501
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > core4.c
+mycc -o core4 -Wall -Wextra -O0 -g core4.c || exit 1
+rm -f core4.c
+
+mount | grep -q "$mntpoint" && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+for i in `jot 20`; do
+ mount /dev/md${mdstart}$part $mntpoint
+ chmod 777 $mntpoint
+ su $testuser -c "(cd $mntpoint; /tmp/core4)" &
+ su $testuser -c "(cd $mntpoint; /tmp/core4)" &
+ su $testuser -c "(cd $mntpoint; /tmp/core4)" &
+ sleep .5
+ sleep .`jot -r 1 1 9`
+ umount -f $mntpoint
+ wait
+done 2>/dev/null
+
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/core4
+exit 0
+EOF
+#include <sys/mman.h>
+#include <sys/param.h>
+
+#include <err.h>
+#include <signal.h>
+#include <unistd.h>
+
+#define SIZ (1024L * 1024 * 1024)
+
+int
+main(void)
+{
+ void *p;
+
+ p = mmap(NULL, SIZ, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
+ usleep(1000);
+ raise(SIGSEGV);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/core5.sh b/tools/test/stress2/misc/core5.sh
new file mode 100755
index 000000000000..6dc6ba96b742
--- /dev/null
+++ b/tools/test/stress2/misc/core5.sh
@@ -0,0 +1,199 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# The core file vnode is unreferenced before notification is sent.
+
+# Problem reported by sbruno@
+# http://people.freebsd.org/~pho/stress/log/core5.txt
+# Fixed by r279237.
+
+# 20150714 Slowdown seen with core5 waiting in vlruwk.
+# sysctl vfs.vlru_allow_cache_src=1 used to resolve this.
+# For now change MAXVNODES from 1.000 to 4.000.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > core5.c
+mycc -o core5 -Wall -Wextra -O0 -g core5.c || exit 1
+rm -f core5.c
+
+cat > core5-dumper.c << EOT
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <time.h>
+#include <unistd.h>
+
+int
+main(int argc __unused, char *argv[])
+{
+ time_t start;
+ char core[80];
+
+ snprintf(core, sizeof(core), "%s.core", argv[0]);
+
+ if (unlink(core) == -1)
+ if (errno != ENOENT)
+ warn("unlink(%s)", core);
+
+ start = time(NULL);
+ while (time(NULL) - start < 600) {
+ if (fork() == 0)
+ raise(SIGSEGV);
+ wait(NULL);
+ }
+ if (unlink(core) == -1)
+ if (errno != ENOENT)
+ warn("unlink(%s)", core);
+
+ return (0);
+}
+EOT
+mycc -o core5-dumper -Wall -Wextra -O0 -g core5-dumper.c || exit 1
+rm -f core5-dumper.c
+for i in `jot 10`; do
+ cp core5-dumper core5-dumper$i
+done
+rm -f core5-dumper
+
+mount | grep -q "on $mntpoint " && umount $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t malloc -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+
+newfs -b 4096 -f 512 -i 2048 md${mdstart}$part > /dev/null
+mount -o async /dev/md${mdstart}$part $mntpoint || exit 1
+
+cp /tmp/core5 $mntpoint
+mkdir $mntpoint/dir
+cd $mntpoint
+
+mp2=${mntpoint}2
+[ -d $mp2 ] || mkdir $mp2
+mount | grep -q "on $mp2 " && umount $mp2
+mount -o size=2g -t tmpfs tmpfs $mp2 || exit 1
+for i in `jot 10`; do
+ (cd $mp2; /tmp/core5-dumper$i ) &
+done
+maxvnodes=`sysctl -n kern.maxvnodes`
+trap "sysctl kern.maxvnodes=$maxvnodes > /dev/null" EXIT INT
+$mntpoint/core5 $mntpoint/dir
+wait
+umount $mp2
+
+cd $here
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/core5 /tmp/core5-dumper* /tmp/core5-dumper*.core
+exit 0
+EOF
+#include <sys/wait.h>
+#include <sys/sysctl.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define MAXVNODES 4000
+#define NBFILES 10000
+#define PARALLEL 4
+#define RTIME (10 * 60)
+
+char *path;
+
+void
+test(int n)
+{
+ int fd, i;
+ char file[80];
+
+ usleep(arc4random() % 1000);
+ for (i = 0; i < NBFILES; i++) {
+ snprintf(file, sizeof(file), "%s/f%d.%06d", path, n, i);
+ if ((fd = open(file, O_CREAT, 0644)) == -1) {
+ warn("open(%s)", file);
+ break;
+ }
+ close(fd);
+ }
+ for (i = 0; i < NBFILES; i++) {
+ snprintf(file, sizeof(file), "%s/f%d.%06d", path, n, i);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+ }
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ size_t len;
+ time_t start;
+ unsigned long nv, maxvnodes;
+ int j;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <path>", argv[0]);
+ path = argv[1];
+
+ nv = MAXVNODES;
+ len = sizeof(maxvnodes);
+ if (sysctlbyname("kern.maxvnodes", &maxvnodes, &len, &nv,
+ sizeof(nv)) != 0)
+ err(1, "sysctl kern.maxvnodes 1");
+
+ start = time(NULL);
+ while (time(NULL) - start < RTIME) {
+ for (j = 0; j < PARALLEL; j++)
+ if (fork() == 0)
+ test(j);
+
+ for (j = 0; j < PARALLEL; j++)
+ wait(NULL);
+ }
+
+ if (sysctlbyname("kern.maxvnodes", NULL, NULL, &maxvnodes,
+ sizeof(maxvnodes)) != 0)
+ err(1, "sysctl kern.maxvnodes 2");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/cpuset.sh b/tools/test/stress2/misc/cpuset.sh
new file mode 100755
index 000000000000..ad9f8569f386
--- /dev/null
+++ b/tools/test/stress2/misc/cpuset.sh
@@ -0,0 +1,49 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Deadlock with one really small domain:
+# https://people.freebsd.org/~pho/stress/log/mark074.txt
+
+[ `sysctl -n vm.ndomains` -eq 1 ] && exit 0
+
+#set -e
+# * round-robin:0-4 explicit
+# * round-robin:all explicit root domains
+# * 0-4 implicit root policy
+# * round-robin implicit root domains
+# * all explicit root domains and implicit policy
+
+for p in "round-robin:0" "round-robin:1" "round-robin:0-1" \
+ "round-robin:all" "0-1" round-robin all first-touch prefer:1 \
+ interleave; do
+ echo "`date +%T` cpuset -n $p"
+ cpuset -n $p ../misc/ufsbench.sh
+done
+
+exit 0
diff --git a/tools/test/stress2/misc/credleak.sh b/tools/test/stress2/misc/credleak.sh
new file mode 100755
index 000000000000..da6c0b73f460
--- /dev/null
+++ b/tools/test/stress2/misc/credleak.sh
@@ -0,0 +1,162 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC
+# 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 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.
+#
+
+# Demonstrate that vfs_export() leaks M_CRED when mountd(8) is started:
+# "M_CRED leaked 160".
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+pgrep -q mountd || echo "Note: mountd(8) must run for this test to fail"
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > credleak.c
+mycc -o credleak -Wall -Wextra -O2 -g credleak.c || exit 1
+rm -f credleak.c
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+mount -t tmpfs tmpfs $mntpoint
+chmod 777 $mntpoint
+
+old=`vmstat -m | grep -w cred | awk '{print $2}'`
+su $testuser -c "/tmp/credleak $mntpoint" &
+
+while kill -0 $! 2>/dev/null; do
+ umount -f $mntpoint &&
+ mount -t tmpfs tmpfs $mntpoint
+ chmod 777 $mntpoint
+ sleep .1
+done
+pkill -9 swap
+wait
+
+while pkill -9 swap; do
+ :
+done > /dev/null 2>&1
+while mount | grep $mntpoint | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
+[ -d "$mntpoint" ] && (cd $mntpoint && find . -delete)
+rm -f /tmp/credleak
+
+s=0
+leak=$((`vmstat -m | grep -w cred | awk '{print $2}'` - old))
+[ $leak -gt 10 ] && { echo "M_CRED leaked $leak"; s=1; }
+exit $s
+EOF
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define LOOPS 160
+#define PARALLEL 16
+
+int nbc, nbd;
+char *dir;
+
+void
+tmkdir(void)
+{
+ int i, j;
+ char d[MAXPATHLEN + 1], name[MAXPATHLEN + 1];
+
+ setproctitle(__func__);
+
+ i = 0;
+ snprintf(name, sizeof(name), "%s/d1.%05d", dir, getpid());
+ if (mkdir(name, 0755) == -1) {
+ if (errno != ENAMETOOLONG && errno != ENOENT &&
+ errno != EBUSY && errno != EACCES && errno != EPERM)
+ warn("mkdir(%s)", name);
+ _exit(0);
+ }
+ for (;;) {
+ snprintf(d, sizeof(d), "/%d", i++);
+ strncat(name, d, sizeof(name) - 1);
+ if (mkdir(name, 0755) == -1) {
+ if (errno != ENAMETOOLONG && errno != ENOENT &&
+ errno != EBUSY && errno != EACCES && errno != EPERM)
+ warn("mkdir(%s)", name);
+ i--;
+ break;
+ }
+ nbc++;
+ }
+
+ while (i >= 0) {
+ snprintf(name, sizeof(name), "%s/d1.%05d", dir, getpid());
+ for (j = 0; j < i; j++) {
+ snprintf(d, sizeof(d), "/%d", j);
+ strncat(name, d, sizeof(name) - 1);
+ }
+ if (rmdir(name) == -1) {
+ if (errno != ENOTEMPTY && errno != ENOENT && errno !=
+ EBUSY)
+ warn("rmdir(%s)", name);
+ } else
+ nbd++;
+ i--;
+ }
+#if defined(TEST)
+ if (nbc == 0)
+ fprintf(stderr, "FAIL nbc = %d, nbd = %d\n", nbc, nbd);
+#endif
+ _exit(0);
+}
+
+int
+main(int argc, char **argv)
+{
+ int i, j;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <full path to dir>", argv[0]);
+ exit(1);
+ }
+ dir = argv[1];
+
+ for (j = 0; j < LOOPS; j++) {
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ tmkdir();
+ }
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+ }
+
+ return(0);
+}
diff --git a/tools/test/stress2/misc/crossmp.sh b/tools/test/stress2/misc/crossmp.sh
new file mode 100755
index 000000000000..7d1ccbe3a469
--- /dev/null
+++ b/tools/test/stress2/misc/crossmp.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Parallel mount and umount of file systems
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mounts=15 # Number of parallel scripts
+cont=/tmp/crossmp.continue
+mdstart=$mdstart # Use md unit numbers from this point
+D=$diskimage
+
+if [ $# -eq 0 ]; then
+ touch $cont
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ [ ! -d ${mntpoint}$m ] && mkdir ${mntpoint}$m
+ mount | grep "on ${mntpoint}$m " | grep -q md$m &&
+ umount ${mntpoint}$m
+ mdconfig -l | grep -q md$m && mdconfig -d -u $m
+
+ dd if=/dev/zero of=$D$m bs=1m count=1 status=none
+ mdconfig -a -t vnode -f $D$m -u $m
+ bsdlabel -w md$m auto
+ newfs $newfs_flags md${m}$part > /dev/null 2>&1
+ done
+
+ # start the parallel tests
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ ./$0 $m &
+ ./$0 find &
+ done
+
+ wait
+
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ mdconfig -d -u $m
+ rm -f $D$m
+ done
+ exit 0
+else
+ if [ $1 = find ]; then
+ while [ -r $cont ]; do
+ find ${mntpoint}* -type f > /dev/null 2>&1
+ done
+ else
+
+ # The test: Parallel mount and unmounts
+ for i in `jot 1024`; do
+ m=$1
+ mount /dev/md${m}$part ${mntpoint}$m
+ while mount | grep -q "on ${mntpoint}$m "; do
+ opt=$([ $((`date '+%s'` % 2)) -eq 0 ] &&
+ echo "-f")
+ umount $opt ${mntpoint}$m > /dev/null 2>&1
+ done
+ done
+ rm -f $cont
+ fi
+fi
diff --git a/tools/test/stress2/misc/crossmp10.sh b/tools/test/stress2/misc/crossmp10.sh
new file mode 100755
index 000000000000..30e64a064dac
--- /dev/null
+++ b/tools/test/stress2/misc/crossmp10.sh
@@ -0,0 +1,104 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC Isilon
+# 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 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.
+#
+
+# ext2fs parallel mount & umount test scenario
+# "panic: vm_fault_hold: fault on nofault entry" seen.
+# https://people.freebsd.org/~pho/stress/log/crossmp10.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -z "`which mke2fs`" ] && echo "mke2fs not found" && exit 0
+
+. ../default.cfg
+
+CONT=/tmp/crossmp10.continue
+mounts=4 # Number of parallel scripts
+size=512 # Disk size in MB
+
+if [ $# -eq 0 ]; then
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ [ ! -d ${mntpoint}$m ] &&
+ { mkdir ${mntpoint}$m; chmod 755 ${mntpoint}$m; }
+ mount | grep "${mntpoint}$m " | grep -q md$m && umount ${mntpoint}$m
+ mdconfig -l | grep -q md$m && mdconfig -d -u $m
+
+ mdconfig -a -t swap -s ${size}m -u $m
+ bsdlabel -w md$m auto
+ mke2fs -m 0 /dev/md${m}$part > /dev/null 2>&1
+ done
+
+ # start the parallel tests
+ touch $CONT
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ ./$0 $m &
+ ./$0 find &
+ done
+
+ wait
+
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ while mount | grep -q "on ${mntpoint}$m "; do
+ umount ${mntpoint}$m && break
+ sleep 1
+ done
+ mdconfig -d -u $m
+ done
+ exit 0
+else
+ if [ $1 = find ]; then
+ while [ -f $CONT ]; do
+ find ${mntpoint}* -ls > /dev/null 2>&1
+ sleep .1
+ done
+ else
+ export runRUNTIME=30s
+ # The test: Parallel mount and unmounts
+ start=`date '+%s'`
+ while [ $((`date '+%s'` - start)) -lt 300 ]; do
+ m=$1
+ mount -t ext2fs /dev/md${m}$part ${mntpoint}$m &&
+ chmod 777 ${mntpoint}$m
+ export RUNDIR=${mntpoint}$m/stressX
+ export CTRLDIR=${mntpoint}$m/stressX.control
+ (cd ${mntpoint}$m && find . -delete)
+ su $testuser -c 'cd ..; ./run.sh disk.cfg' > /dev/null 2>&1 &
+
+ sleep 2
+ while mount | grep -q "on ${mntpoint}$m "; do
+ opt=$([ `jot -r 1 0 1` -eq 0 ] && echo "-f")
+ umount $opt ${mntpoint}$m > /dev/null 2>&1
+ [ -f $CONT ] || break 2
+ done
+ wait $!
+ done
+ rm -f $CONT
+ ../tools/killall.sh
+ fi
+fi
diff --git a/tools/test/stress2/misc/crossmp11.sh b/tools/test/stress2/misc/crossmp11.sh
new file mode 100755
index 000000000000..8bc008f6f504
--- /dev/null
+++ b/tools/test/stress2/misc/crossmp11.sh
@@ -0,0 +1,121 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Copy of crossmp3.sh + elements from dfull.sh
+
+# Live lock seen:
+# https://people.freebsd.org/~pho/stress/log/crossmp11.txt
+
+# umount stuck in mnt_ref seen:
+# https://people.freebsd.org/~pho/stress/log/kostik1002.txt
+
+# Fixed by r319518, r319519 and r319539.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+g3=$((3 * 1024 * 1024 * 1024))
+CONT=/tmp/crossmp11.continue
+N=`sysctl -n hw.ncpu`
+[ $N -gt 4 ] && N=4
+usermem=`sysctl -n hw.usermem`
+[ $usermem -gt $g3 ] && usermem=$g3
+[ `sysctl -n vm.swap_total` -eq 0 ] && usermem=$((usermem/100*80))
+size=$((usermem / 1024 / 1024 / N))
+
+mounts=$N # Number of parallel scripts
+
+if [ $# -eq 0 ]; then
+ echo "Expect:
+ /mnt6: write failed, filesystem is full
+ /mnt6: create/symlink failed, no inodes free"
+
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ [ ! -d ${mntpoint}$m ] &&
+ { mkdir ${mntpoint}$m; chmod 755 ${mntpoint}$m; }
+ mount | grep "${mntpoint}$m " | grep -q md$m && umount ${mntpoint}$m
+ mdconfig -l | grep -q md$m && mdconfig -d -u $m
+
+ mdconfig -a -t swap -s ${size}m -u $m
+ bsdlabel -w md$m auto
+ newfs $newfs_flags md${m}$part > /dev/null 2>&1
+ done
+
+ # start the parallel tests
+ touch $CONT
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ ./$0 $m &
+ ./$0 find &
+ done
+
+ wait
+
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ while mount | grep -q "on ${mntpoint}$m "; do
+ umount ${mntpoint}$m && break
+ sleep 1
+ done
+ mdconfig -d -u $m
+ done
+ exit 0
+else
+ if [ $1 = find ]; then
+ while [ -f $CONT ]; do
+ find ${mntpoint}* -type f > /dev/null 2>&1
+ done
+ else
+ m=$1
+ export runRUNTIME=20s
+ # The test: Parallel mount and unmounts
+ for i in `jot 3`; do
+ mount /dev/md${m}$part ${mntpoint}$m &&
+ chmod 777 ${mntpoint}$m
+ export RUNDIR=${mntpoint}$m/stressX
+ export CTRLDIR=${mntpoint}$m/stressX.control
+ (cd ${mntpoint}$m && find . -delete)
+ if [ -z "$KBLOCKS" ]; then
+ r=`df -ik ${mntpoint}$m | tail -1 | awk '{print $4,$7}'`
+ export KBLOCKS=`echo $r | awk '{print $1 * 10}'`
+ export INODES=`echo $r | awk '{print $2 * 10}'`
+ fi
+ su $testuser -c 'cd ..; ./run.sh disk.cfg' > \
+ /dev/null 2>&1
+
+ while mount | grep -q "on ${mntpoint}$m "; do
+ opt=$([ $((`date '+%s'` % 2)) -eq 0 ] && echo "-f")
+ umount $opt ${mntpoint}$m > /dev/null 2>&1
+ [ -f $CONT ] || break 2
+ done
+ done
+ rm -f $CONT
+ fi
+fi
diff --git a/tools/test/stress2/misc/crossmp2.sh b/tools/test/stress2/misc/crossmp2.sh
new file mode 100755
index 000000000000..03cd04e077c1
--- /dev/null
+++ b/tools/test/stress2/misc/crossmp2.sh
@@ -0,0 +1,86 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# NFS test
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+cont=/tmp/crossmp2.continue
+mounts=10 # Number of parallel scripts
+
+if [ $# -eq 0 ]; then
+ [ -z "$nfs_export" ] && exit 0
+ ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+ touch $cont
+ mount -t nfs -o tcp -o nfsv3 -o retrycnt=1 -o intr,soft,timeout=1 \
+ -o rw $nfs_export $mntpoint || exit 0
+ sleep .2
+ umount $mntpoint
+
+ for i in `jot $mounts`; do
+ mp=${mntpoint}$i
+ [ ! -d $mp ] && mkdir $mp
+ mount | grep -q "$mp " && umount $mp
+ done
+
+ # start the parallel tests
+ for i in `jot $mounts`; do
+ ./$0 $i &
+ ./$0 find &
+ done
+ wait
+ exit 0
+else
+ if [ $1 = find ]; then
+ while [ -r $cont ]; do
+ find ${mntpoint}* -maxdepth 1 -type f > \
+ /dev/null 2>&1
+ done
+ else
+
+ # The test: Parallel mount and unmounts
+ for i in `jot 128`; do
+ m=$1
+ mount -t nfs -o tcp -o nfsv3 -o retrycnt=3 \
+ -o intr,soft -o rw $nfs_export ${mntpoint}$m
+ sleep .5
+ opt=`[ $(( m % 2 )) -eq 0 ] && echo -f`
+ n=0
+ while mount | grep -q "${mntpoint}$m "; do
+ umount $opt ${mntpoint}$m > /dev/null 2>&1
+ [ $((n += 1)) -gt 99 ] && umount -f \
+ ${mntpoint}$m > /dev/null 2>&1
+ [ $n -gt 100 ] && exit
+ done
+ done
+ rm -f $cont
+ fi
+fi
diff --git a/tools/test/stress2/misc/crossmp3.sh b/tools/test/stress2/misc/crossmp3.sh
new file mode 100755
index 000000000000..30c9f86840f1
--- /dev/null
+++ b/tools/test/stress2/misc/crossmp3.sh
@@ -0,0 +1,108 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Parallel mount and umount of file systems
+# "panic: Bad link elm 0xfffff8052a20cc00 prev->next != elm" seen:
+# http://people.freebsd.org/~pho/stress/log/crossmp3.txt
+# Fixed in r269853
+
+# panic: softdep_waitidle: work added after flush:
+# http://people.freebsd.org/~pho/stress/log/crossmp3-2.txt, fixed by r273967.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+CONT=/tmp/crossmp3.continue
+if [ $# -eq 0 ]; then
+ N=`sysctl -n hw.ncpu`
+ usermem=`sysctl -n hw.usermem`
+ [ `sysctl -n vm.swap_total` -eq 0 ] && usermem=$((usermem / 2))
+ size=$((usermem / 1024 / 1024 / N))
+ echo "Using $N memory disks of size $size MB."
+
+ mounts=$N # Number of parallel scripts
+
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ [ ! -d ${mntpoint}$m ] &&
+ { mkdir ${mntpoint}$m; chmod 755 ${mntpoint}$m; }
+ mount | grep "${mntpoint}$m " | grep -q md$m &&
+ umount ${mntpoint}$m
+ mdconfig -l | grep -q md$m && mdconfig -d -u $m
+
+ mdconfig -a -t swap -s ${size}m -u $m
+ bsdlabel -w md$m auto
+ newfs $newfs_flags md${m}$part > /dev/null 2>&1
+ done
+
+ # start the parallel tests
+ touch $CONT
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ ./$0 $m &
+ ./$0 find &
+ done
+ wait
+
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ while mount | grep -q "on ${mntpoint}$m "; do
+ umount ${mntpoint}$m && break
+ sleep 1
+ done
+ mdconfig -d -u $m
+ done
+ exit 0
+else
+ if [ $1 = find ]; then
+ while [ -f $CONT ]; do
+ find ${mntpoint}* -type f > /dev/null 2>&1
+ done
+ else
+ export runRUNTIME=20s
+ # The test: Parallel mount and unmounts
+ for i in `jot 3`; do
+ m=$1
+ mount /dev/md${m}$part ${mntpoint}$m &&
+ chmod 777 ${mntpoint}$m
+ export RUNDIR=${mntpoint}$m/stressX
+ export CTRLDIR=${mntpoint}$m/stressX.control
+ (cd ${mntpoint}$m && find . -delete)
+ su $testuser -c 'cd ..; ./run.sh disk.cfg' > \
+ /dev/null 2>&1
+
+ while mount | grep -q "on ${mntpoint}$m "; do
+ opt=$([ $((`date '+%s'` % 2)) -eq 0 ] && echo "-f")
+ umount $opt ${mntpoint}$m > /dev/null 2>&1
+ [ -f $CONT ] || break 2
+ done
+ done
+ rm -f $CONT
+ fi
+fi
diff --git a/tools/test/stress2/misc/crossmp4.sh b/tools/test/stress2/misc/crossmp4.sh
new file mode 100755
index 000000000000..7f24097475f3
--- /dev/null
+++ b/tools/test/stress2/misc/crossmp4.sh
@@ -0,0 +1,107 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Parallel mount and umount of file systems. Nullfs version.
+
+# "panic: Lock (lockmgr) null not locked @ kern/vfs_default.c:523." seen.
+# http://people.freebsd.org/~pho/stress/log/kostik698.txt
+# Fixed by r269708.
+
+# Not fixed: https://people.freebsd.org/~pho/stress/log/kostik798.txt
+# https://people.freebsd.org/~pho/stress/log/kostik856.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+N=`sysctl -n hw.ncpu`
+usermem=`sysctl -n hw.usermem`
+[ `swapinfo | wc -l` -eq 1 ] && usermem=$((usermem/100*80))
+size=$((usermem / 1024 / 1024 - 2))
+
+CONT=/tmp/crossmp4.continue
+mounts=$N # Number of parallel scripts
+
+if [ $# -eq 0 ]; then
+ mount | grep "$mntpoint" | grep -q md && umount $mntpoint
+ mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+ mdconfig -a -t swap -s ${size}m -u $mdstart
+ bsdlabel -w md$mdstart auto
+ newfs $newfs_flags md${mdstart}$part > /dev/null 2>&1
+ mount /dev/md${mdstart}$part $mntpoint
+
+ # start the parallel tests
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ ./$0 $m &
+ ./$0 find &
+ done
+
+ wait
+
+ while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint > /dev/null 2>&1 || sleep 1
+ done
+ mdconfig -d -u $mdstart
+ exit 0
+else
+ touch $CONT
+ if [ $1 = find ]; then
+ while [ -f $CONT ]; do
+ find ${mntpoint}* -type f > /dev/null 2>&1
+ done
+ else
+ m=$1
+ set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+ export KBLOCKS=$(($1 / N))
+ export INODES=$(($2 / N))
+ export runRUNTIME=1m
+ export INCARNATIONS=4
+ # The test: Parallel mount and unmounts
+ for i in `jot 4`; do
+ [ ! -d ${mntpoint}$m ] && mkdir ${mntpoint}$m
+ mount -t nullfs $mntpoint ${mntpoint}$m
+ mkdir -p ${mntpoint}$m/$m
+ chmod 777 ${mntpoint}$m/$m
+ export RUNDIR=${mntpoint}$m/$m/stressX
+ export CTRLDIR=${mntpoint}$m/$m/stressX.control
+ (cd ${mntpoint}$m/$m && find . -delete)
+ su $testuser -c 'cd ..; ./run.sh disk.cfg' > \
+ /dev/null 2>&1 &
+ sleep 30
+
+ while mount | grep -q "on ${mntpoint}$m "; do
+ opt=$([ $((`date '+%s'` % 2)) -eq 0 ] && echo "-f")
+ umount $opt ${mntpoint}$m > /dev/null 2>&1
+ done
+ wait
+ done
+ rm -f $CONT
+ fi
+fi
diff --git a/tools/test/stress2/misc/crossmp5.sh b/tools/test/stress2/misc/crossmp5.sh
new file mode 100755
index 000000000000..9a636c9aae45
--- /dev/null
+++ b/tools/test/stress2/misc/crossmp5.sh
@@ -0,0 +1,95 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Variation of crossmp3.sh
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+N=`sysctl -n hw.ncpu`
+usermem=`sysctl -n hw.usermem`
+[ `swapinfo | wc -l` -eq 1 ] && usermem=$((usermem/100*80))
+size=$((usermem / 1024 / 1024 / N))
+
+mounts=$N # Number of parallel scripts
+
+if [ $# -eq 0 ]; then
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ [ ! -d ${mntpoint}$m ] && mkdir ${mntpoint}$m
+ mount | grep "${mntpoint}$m " | grep -q md$m &&
+ umount ${mntpoint}$m
+ mdconfig -l | grep -q md$m && mdconfig -d -u $m
+
+ mdconfig -a -t swap -s ${size}m -u $m
+ bsdlabel -w md$m auto
+ newfs $newfs_flags md${m}$part > /dev/null 2>&1
+ done
+
+ # start the parallel tests
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ ./$0 $m &
+ ./$0 find &
+ done
+
+ wait
+
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ mdconfig -d -u $m
+ rm -f $D$m
+ done
+ exit 0
+else
+ touch /tmp/crossmp.continue
+ if [ $1 = find ]; then
+ while [ -f /tmp/crossmp.continue ]; do
+ find ${mntpoint}* -type f > /dev/null 2>&1
+ done
+ else
+ # The test: Parallel mount and unmount
+ m=$1
+ for i in `jot 200`; do
+ mount /dev/md${m}$part ${mntpoint}$m
+ chmod 777 ${mntpoint}$m
+ l=`jot -r 1 65535`
+ dd if=/dev/zero of=$mntpoint/$i bs=$l count=100 \
+ status=none
+ rm -f $mntpoint/$i
+
+ while mount | grep -q "on ${mntpoint}$m "; do
+ opt=$([ $((`date '+%s'` % 2)) -eq 0 ] &&
+ echo "-f")
+ umount $opt ${mntpoint}$m > /dev/null 2>&1
+ done
+ done
+ rm -f /tmp/crossmp.continue
+ fi
+fi
diff --git a/tools/test/stress2/misc/crossmp6.sh b/tools/test/stress2/misc/crossmp6.sh
new file mode 100755
index 000000000000..92ca0abcba27
--- /dev/null
+++ b/tools/test/stress2/misc/crossmp6.sh
@@ -0,0 +1,103 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Copy of crossmp2.sh: NFS test, with lockf(1) added.
+# "panic: vinvalbuf: dirty bufs" seen.
+# https://people.freebsd.org/~pho/stress/log/crossmp6.txt
+# Fixed by r283968.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+pgrep -q lockd || { echo "lockd not running."; exit 0; }
+
+CONT=/tmp/crossmp6.continue
+mounts=10 # Number of parallel scripts
+
+if [ $# -eq 0 ]; then
+ [ -z "$nfs_export" ] && exit 0
+ ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+ mount -t nfs -o tcp -o nfsv3 -o retrycnt=1 -o soft,timeout=1 \
+ -o rw $nfs_export $mntpoint || exit 0
+ umount $mntpoint
+
+ for i in `jot $mounts`; do
+ mp=${mntpoint}$i
+ [ ! -d $mp ] && mkdir $mp
+ mount | grep -qw "$mp" && umount $mp
+ done
+
+ # start the parallel tests
+ for i in `jot $mounts`; do
+ ./$0 $i &
+ ./$0 find $i &
+ done
+ wait
+ mount -t nfs -o tcp -o nfsv3 -o retrycnt=1 -o soft,timeout=1 \
+ -o rw $nfs_export $mntpoint || exit 0
+ sleep .5
+ rm -f $mntpoint/$0.*
+ umount $mntpoint
+ rm -f $mntpoint*/$0.*
+ exit 0
+else
+ if [ $1 = find ]; then
+ for i in `jot 128`; do
+ find ${mntpoint}* -maxdepth 1 -type f > \
+ /dev/null 2>&1
+ (lockf -t 10 ${mntpoint}$2/$0.$$.$i sleep 1 &) > \
+ /dev/null 2>&1
+ [ -f $CONT ] || break
+ done
+ wait
+ else
+ # The test: Parallel mount and unmounts
+ start=`date '+%s'`
+ while [ $((`date '+%s'` - start)) -lt 300 ]; do
+ m=$1
+ mount -t nfs -o tcp -o nfsv3 -o retrycnt=3 \
+ -o soft -o rw $nfs_export ${mntpoint}$m
+ sleep .5
+ opt=`[ $(( m % 2 )) -eq 0 ] && echo -f`
+ n=0
+ while mount | grep -qw ${mntpoint}$m; do
+ umount $opt ${mntpoint}$m > /dev/null 2>&1
+ n=$((n + 1))
+ [ $n -gt 99 ] && umount -f ${mntpoint}$m > \
+ /dev/null 2>&1
+ [ $n -gt 100 ] && exit
+ done
+ done
+ rm -f $CONT
+ fi
+fi
diff --git a/tools/test/stress2/misc/crossmp7.sh b/tools/test/stress2/misc/crossmp7.sh
new file mode 100755
index 000000000000..c2c2752f38e7
--- /dev/null
+++ b/tools/test/stress2/misc/crossmp7.sh
@@ -0,0 +1,100 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Parallel mount and umount of zfs file systems.
+
+# Page fault seen:
+# https://people.freebsd.org/~pho/stress/log/avg002.txt
+# Fixed by r309090.
+
+# Page fault seen:
+# https://people.freebsd.org/~pho/stress/log/crossmp7.txt
+# Fixed by r352437.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -m` = "i386" ] && exit 0
+[ `sysctl -n kern.kstack_pages` -lt 4 ] && exit 0
+
+. ../default.cfg
+
+mounts=15 # Number of parallel scripts
+
+if [ $# -eq 0 ]; then
+ kldstat -v | grep -q zfs.ko || { kldload zfs.ko ||
+ exit 0; loaded=1; }
+ zpool list | egrep -q "^stress2_tank" && zpool destroy stress2_tank
+
+ u1=$mdstart
+ u2=$((u1 + 1))
+ u3=$((u2 + 1))
+
+ [ -c /dev/md$u1 ] && mdconfig -d -u $u1
+ [ -c /dev/md$u2 ] && mdconfig -d -u $u2
+ [ -c /dev/md$u3 ] && mdconfig -d -u $u3
+
+ mdconfig -s 512m -u $u1
+ mdconfig -s 512m -u $u2
+ mdconfig -s 512m -u $u3
+
+ zpool create stress2_tank raidz md$u1 md$u2 md$u3
+
+ for i in `jot $mounts`; do
+ zfs create stress2_tank/test$i
+ zfs umount stress2_tank/test$i
+ done
+
+ # start the parallel tests
+ touch /tmp/crossmp7.continue
+ for i in `jot $mounts`; do
+ ./$0 $i &
+ ./$0 find &
+ done
+
+ wait
+
+ zpool destroy stress2_tank
+ [ -n "$loaded" ] && kldunload zfs.ko
+ mdconfig -d -u $u1
+ mdconfig -d -u $u2
+ mdconfig -d -u $u3
+ exit 0
+else
+ if [ $1 = find ]; then
+ while [ -f /tmp/crossmp7.continue ]; do
+ find /stress2_tank -type f > /dev/null 2>&1
+ done
+ else
+ # The test: Parallel mount and unmounts
+ m=$1
+ for i in `jot 1024`; do
+ zfs mount stress2_tank/test$m
+ zfs umount -f stress2_tank/test$m
+ done 2>/dev/null
+ rm -f /tmp/crossmp7.continue
+ fi
+fi
diff --git a/tools/test/stress2/misc/crossmp8.sh b/tools/test/stress2/misc/crossmp8.sh
new file mode 100755
index 000000000000..f8d06d6a4a30
--- /dev/null
+++ b/tools/test/stress2/misc/crossmp8.sh
@@ -0,0 +1,125 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Variation of crossmp3.sh. fifos and sockets added to load.
+# Not really a cross mount point test, but a test of the old
+# non-directory use of the vnode v_un union.
+# mckusick@ suggested using fifos for this test.
+
+# "panic: mtx_lock() of spin mutex @ ../kern/vfs_subr.c:512" seen.
+# https://people.freebsd.org/~pho/stress/log/crossmp8.txt
+# Fixed by r291671.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+CONT=/tmp/crossmp8.continue
+N=`sysctl -n hw.ncpu`
+usermem=`sysctl -n hw.usermem`
+[ `swapinfo | wc -l` -eq 1 ] && usermem=$((usermem/100*80))
+size=$((usermem / 1024 / 1024 / N))
+
+mounts=$N # Number of parallel scripts
+
+if [ $# -eq 0 ]; then
+ oldmx=`sysctl -n kern.maxvnodes`
+ trap "sysctl kern.maxvnodes=$oldmx > /dev/null" EXIT SIGINT
+ sysctl kern.maxvnodes=3072 > /dev/null
+
+ for i in `jot $mounts`; do
+ m=$((i + mdstart - 1))
+ [ ! -d ${mntpoint}$m ] &&
+ { mkdir ${mntpoint}$m; chmod 755 ${mntpoint}$m; }
+ mount | grep "${mntpoint}$m " | grep -q md$m &&
+ umount ${mntpoint}$m
+ mdconfig -l | grep -q md$m && mdconfig -d -u $m
+
+ mdconfig -a -t swap -s ${size}m -u $m
+ bsdlabel -w md$m auto
+ newfs md${m}$part > /dev/null 2>&1
+ done
+
+ # start the parallel tests
+ touch $CONT
+ for i in `jot $mounts`; do
+ m=$((i + mdstart - 1))
+ ./$0 $m &
+ ./$0 find &
+ done
+ sleep 60
+ rm -f $CONT
+ ../tools/killall.sh
+ wait
+
+ for i in `jot $mounts`; do
+ m=$((i + mdstart - 1))
+ while mount | grep -q "on ${mntpoint}$m "; do
+ umount ${mntpoint}$m && break
+ sleep 1
+ done
+ mdconfig -d -u $m
+ done
+ ./cleanup.sh
+ exit 0
+else
+ if [ $1 = find ]; then
+ while [ -f $CONT ]; do
+ find ${mntpoint}* -maxdepth 1 -ls > /dev/null 2>&1
+ sleep .1
+ done
+ else
+ export RUNTIME=15s
+ export runRUNTIME=15s
+ # The test: Parallel mount and unmounts
+ start=`date '+%s'`
+ while [ $((`date '+%s'` - start)) -lt 300 ]; do
+ m=$1
+ mount /dev/md${m}$part ${mntpoint}$m &&
+ chmod 777 ${mntpoint}$m
+ export RUNDIR=${mntpoint}$m/stressX
+ export CTRLDIR=${mntpoint}$m/stressX.control
+ export mkfifoLOAD=80
+ export socketLOAD=80
+ export TP="
+ testcases/mkfifo/mkfifo
+ testcases/mkdir/mkdir
+ "
+ (cd ${mntpoint}$m && find . -delete)
+ su $testuser -c 'cd ..; ./testcases/run/run $TP' > \
+ /dev/null 2>&1
+
+ while mount | grep -q "on ${mntpoint}$m "; do
+ opt=$([ $((`date '+%s'` % 2)) -eq 0 ] && echo "-f")
+ umount $opt ${mntpoint}$m > /dev/null 2>&1
+ [ -f $CONT ] || break 2
+ done
+ done
+ rm -f $CONT
+ fi
+fi
diff --git a/tools/test/stress2/misc/crossmp9.sh b/tools/test/stress2/misc/crossmp9.sh
new file mode 100755
index 000000000000..e55f16b5ede4
--- /dev/null
+++ b/tools/test/stress2/misc/crossmp9.sh
@@ -0,0 +1,90 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC Isilon
+# 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 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.
+#
+
+# Parallel mount and umount of file systems, while using getfsstat(2) via
+# mount(8).
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mounts=15 # Number of parallel scripts
+cont=/tmp/crossmp.continue
+mdstart=$mdstart # Use md unit numbers from this point
+
+if [ $# -eq 0 ]; then
+ touch $cont
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ [ ! -d ${mntpoint}$m ] && mkdir ${mntpoint}$m
+ mount | grep "${mntpoint}$m " | grep -q md$m &&
+ umount ${mntpoint}$m
+ mdconfig -l | grep -q md$m && mdconfig -d -u $m
+
+ mdconfig -a -t swap -s 512m -u $m
+ bsdlabel -w md$m auto
+ newfs $newfs_flags md${m}$part > /dev/null 2>&1
+ done
+
+ # start the parallel tests
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ ./$0 $m &
+ ./$0 find $m &
+ done
+ wait
+
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ mdconfig -d -u $m
+ rm -f $D$m
+ done
+ exit 0
+else
+ if [ $1 = find ]; then
+ m=$2
+ while [ -r $cont ]; do
+ df ${mntpoint}$m
+ mount
+ done > /dev/null 2>&1
+ else
+
+ # The test: Parallel mount and unmounts
+ start=`date '+%s'`
+ while [ $((`date '+%s'` - start)) -lt 300 ] ; do
+ m=$1
+ mount /dev/md${m}$part ${mntpoint}$m
+ while mount | grep -qw ${mntpoint}$m; do
+ opt=$([ $((`date '+%s'` % 2)) -eq 0 ] &&
+ echo "-f")
+ umount $opt ${mntpoint}$m > /dev/null 2>&1
+ done
+ done
+ rm -f $cont
+ fi
+fi
diff --git a/tools/test/stress2/misc/dangling.sh b/tools/test/stress2/misc/dangling.sh
new file mode 100755
index 000000000000..5f3db6db00ec
--- /dev/null
+++ b/tools/test/stress2/misc/dangling.sh
@@ -0,0 +1,87 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "panic: softdep_deallocate_dependencies: dangling deps" seen.
+# "panic: softdep_write_inodeblock: indirect pointer #0 mismatch" seen.
+# http://people.freebsd.org/~pho/stress/log/dangling.txt
+# http://people.freebsd.org/~pho/stress/log/dangling2.txt
+# https://people.freebsd.org/~pho/stress/log/kostik1101.txt
+
+# Test scenario seems optimized for 4 CPUs.
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 4g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint || exit 1
+chmod 777 $mntpoint
+
+export runRUNTIME=4m
+export RUNDIR=$mntpoint/stressX
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export KBLOCKS=$(($1 / 4))
+export INODES=$(($2 / 4))
+
+export symlinkHOG=1
+export rwHOG=1
+export mkdirHOG=1
+
+export LOAD=100
+export symlinkLOAD=100
+export rwLOAD=100
+export mkdirLOAD=100
+export TESTPROGS="
+testcases/symlink/symlink
+testcases/fts/fts
+testcases/mkdir/mkdir
+testcases/rw/rw
+"
+
+for i in `jot 10`; do
+ su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > \
+ /dev/null 2>&1 &
+ sleep 60
+ kill $!
+ ../tools/killall.sh > /dev/null 2>&1
+ ../tools/killall.sh > /dev/null 2>&1
+ wait
+done
+
+s=0
+for i in `jot 6`; do
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && s=1
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/datagram.sh b/tools/test/stress2/misc/datagram.sh
new file mode 100755
index 000000000000..895145a22c84
--- /dev/null
+++ b/tools/test/stress2/misc/datagram.sh
@@ -0,0 +1,96 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# UNIX datagram socket test.
+
+# "panic: mutex unp not owned at ../../../kern/uipc_usrreq.c:879" seen:
+# https://people.freebsd.org/~pho/stress/log/datagram.txt
+# Fixed by r334756.
+
+. ../default.cfg
+
+cd /tmp
+cat > datagram.c <<EOF
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <errno.h>
+#include <netdb.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+char *filename = "/tmp/datagram.socket";
+
+int
+main(void) {
+
+ struct sockaddr_un addr;
+ int bytes, sockfd;
+ char buf[1024];
+
+ unlink(filename);
+ memset(&addr, 0, sizeof(addr));
+ addr.sun_family = AF_UNIX;
+ strncpy(addr.sun_path, filename, 104);
+
+ if ((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
+ err(1, "socket");
+
+ if (bind(sockfd, (struct sockaddr *) &addr,
+ sizeof(addr)) == -1)
+ err(1, "bind");
+
+ if (connect(sockfd, (struct sockaddr *) &addr,
+ sizeof(addr)) == -1)
+ err(1, "connect");
+
+ bytes = read(sockfd, buf, sizeof(buf));
+
+ return (0);
+}
+EOF
+
+mycc -o datagram -Wall -Wextra -O2 -g datagram.c || exit 1
+rm datagram.c
+
+./datagram &
+sleep 1
+kill $!
+wait
+
+rm -f datagram /tmp/datagram.socket
+exit 0
diff --git a/tools/test/stress2/misc/datagram2.sh b/tools/test/stress2/misc/datagram2.sh
new file mode 100755
index 000000000000..66cfd44711c2
--- /dev/null
+++ b/tools/test/stress2/misc/datagram2.sh
@@ -0,0 +1,104 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# UNIX datagram socket test.
+
+# "panic: mutex unp not owned at ../../../kern/uipc_usrreq.c:879" seen.
+# Fixed by r334756.
+
+. ../default.cfg
+
+cd /tmp
+cat > datagram2.c <<EOF
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <errno.h>
+#include <netdb.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+char *filename = "/tmp/datagram2.socket";
+
+int
+main(void) {
+
+ struct message { struct cmsghdr msg_hdr; int fd; } m;
+ struct msghdr mh;
+ struct sockaddr_un addr;
+ ssize_t len;
+ int sockfd;
+
+ unlink(filename);
+ memset(&addr, 0, sizeof(addr));
+ addr.sun_family = AF_UNIX;
+ strncpy(addr.sun_path, filename, 104);
+
+ if ((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
+ err(1, "socket");
+
+ if (bind(sockfd, (struct sockaddr *) &addr,
+ sizeof(addr)) == -1)
+ err(1, "bind");
+
+ if (connect(sockfd, (struct sockaddr *) &addr,
+ sizeof(addr)) == -1)
+ err(1, "connect");
+
+ bzero(&mh, sizeof(mh));
+ bzero(&m, sizeof(m));
+ mh.msg_control = &m;
+ mh.msg_controllen = sizeof(m);
+ m.msg_hdr.cmsg_len = sizeof(m);
+ m.msg_hdr.cmsg_level = SOL_SOCKET;
+ m.msg_hdr.cmsg_type = SCM_RIGHTS;
+ m.fd = sockfd;
+ len = sendmsg(sockfd, &mh, 0);
+ if (len < 0)
+ err(1, "sendmsg");
+
+ return (0);
+}
+EOF
+
+cc -o datagram2 -Wall -Wextra -O2 -g datagram2.c || exit 1
+rm -f datagram2.c datagram2.socket
+
+./datagram2
+
+rm datagram2 datagram2.socket
+exit 0
diff --git a/tools/test/stress2/misc/datagram3.sh b/tools/test/stress2/misc/datagram3.sh
new file mode 100755
index 000000000000..ca54377befcc
--- /dev/null
+++ b/tools/test/stress2/misc/datagram3.sh
@@ -0,0 +1,106 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# UNIX datagram socket test.
+
+# "panic: mutex unp not owned at ../../../kern/uipc_usrreq.c:879" seen.
+# Fixed by r334756.
+
+. ../default.cfg
+
+cd /tmp
+cat > datagram3.c <<EOF
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <errno.h>
+#include <netdb.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+char *filename = "/tmp/datagram3.socket";
+
+int
+main(void) {
+
+ struct message { struct cmsghdr msg_hdr; int fd; } m;
+ struct msghdr mh;
+ struct sockaddr_un addr;
+ ssize_t len;
+ int i, sockfd;
+
+ unlink(filename);
+ memset(&addr, 0, sizeof(addr));
+ addr.sun_family = AF_UNIX;
+ strncpy(addr.sun_path, filename, 104);
+
+ if ((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
+ err(1, "socket");
+
+ if (bind(sockfd, (struct sockaddr *) &addr,
+ sizeof(addr)) == -1)
+ err(1, "bind");
+
+ if (connect(sockfd, (struct sockaddr *) &addr,
+ sizeof(addr)) == -1)
+ err(1, "connect");
+
+ for (i = 0; i < 32; i++) {
+ bzero(&mh, sizeof(mh));
+ bzero(&m, sizeof(m));
+ mh.msg_control = &m;
+ mh.msg_controllen = sizeof(m);
+ m.msg_hdr.cmsg_len = sizeof(m);
+ m.msg_hdr.cmsg_level = SOL_SOCKET;
+ m.msg_hdr.cmsg_type = SCM_RIGHTS;
+ m.fd = sockfd;
+ len = sendmsg(sockfd, &mh, 0);
+ if (len < 0)
+ err(1, "sendmsg");
+ }
+
+ return (0);
+}
+EOF
+
+mycc -o datagram3 -Wall -Wextra -O2 -g datagram3.c || exit 1
+rm -f datagram3.c datagram3.socket
+
+./datagram3
+
+rm datagram3 datagram3.socket
+exit 0
diff --git a/tools/test/stress2/misc/datamove.sh b/tools/test/stress2/misc/datamove.sh
new file mode 100755
index 000000000000..a27a53e5e31b
--- /dev/null
+++ b/tools/test/stress2/misc/datamove.sh
@@ -0,0 +1,230 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# There is a well-known problem in FreeBSD, caused by allowing page faults
+# while doing filesystem data move to or from userspace during read(2) and
+# write(2). The issue is that if the userspace address being read or write
+# from/to is backed by the mapping of the same file we are doing i/o to,
+# we deadlock.
+
+# Test scenario by ups
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > datamove.c
+mycc -o datamove -Wall datamove.c
+rm -f datamove.c
+
+n=5
+old=`sysctl vm.old_msync | awk '{print $NF}'`
+sysctl vm.old_msync=1
+for i in `jot $n`; do
+ mkdir -p /tmp/datamove.dir.$i
+ cd /tmp/datamove.dir.$i
+ /tmp/datamove &
+done
+cd /tmp
+for i in `jot $n`; do
+ wait
+done
+for i in `jot $n`; do
+ rm -rf /tmp/datamove.dir.$i
+done
+sysctl vm.old_msync=$old
+
+rm -rf /tmp/datamove
+exit 0
+EOF
+/*-
+ * Copyright (c) 2006, Stephan Uphoff <ups@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 ``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 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/types.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+
+int prepareFile(char* filename,int* fdp);
+int mapBuffer(char** bufferp,int fd1,int fd2);
+int startIO(int fd,char *buffer);
+
+int pagesize;
+
+#define FILESIZE (32*1024)
+char wbuffer[FILESIZE];
+
+/* Create a FILESIZE sized file - then remove file data from the cache*/
+int prepareFile(char* filename,int* fdp)
+{
+ int fd;
+ int len;
+ int status;
+ void *addr;
+
+ fd = open(filename,O_CREAT | O_TRUNC | O_RDWR,S_IRWXU);
+ if (fd == -1)
+ {
+ perror("Creating file");
+ return fd;
+ }
+
+ len = write(fd,wbuffer,FILESIZE);
+ if (len < 0)
+ {
+ perror("Write failed");
+ return 1;
+ }
+
+ status = fsync(fd);
+ if (status != 0)
+ {
+ perror("fsync failed");
+ return 1;
+ }
+
+ addr = mmap(NULL,FILESIZE, PROT_READ | PROT_WRITE , MAP_SHARED, fd, 0);
+ if (addr == MAP_FAILED)
+ {
+ perror("Mmap failed");
+ return 1;
+ }
+
+ status = msync(addr,FILESIZE,MS_INVALIDATE | MS_SYNC);
+ if (status != 0)
+ {
+ perror("Msync failed");
+ return 1;
+ }
+
+ munmap(addr,FILESIZE);
+
+ *fdp = fd;
+ return 0;
+}
+
+/* mmap a 2 page buffer - first page is from fd1, second page from fd2 */
+int mapBuffer(char** bufferp,int fd1,int fd2)
+{
+ void* addr;
+ char *buffer;
+
+ addr = mmap(NULL,pagesize*2, PROT_READ | PROT_WRITE , MAP_SHARED, fd1, 0);
+ if (addr == MAP_FAILED)
+ {
+ perror("Mmap failed");
+ return 1;
+ }
+
+ buffer = addr;
+ addr = mmap(buffer + pagesize,pagesize, PROT_READ | PROT_WRITE , MAP_FIXED |
+ MAP_SHARED, fd2, 0);
+
+ if (addr == MAP_FAILED)
+ {
+ perror("Mmap2 failed");
+ return 1;
+ }
+ *bufferp = buffer;
+ return 0;
+}
+
+int startIO(int fd,char *buffer)
+{
+ ssize_t len;
+ len = write(fd,buffer,2*pagesize);
+ if (len == -1)
+ {
+ perror("write failed");
+ return 1;
+ }
+ return 0;
+}
+
+int main(int argc,char *argv[],char *envp[])
+{
+
+ int fdA,fdB,fdDelayA,fdDelayB;
+ int status;
+ char *bufferA,*bufferB;
+ pid_t pid;
+
+ pagesize = getpagesize();
+
+ if ((prepareFile("A",&fdA))
+ || (prepareFile("B",&fdB))
+ || (prepareFile("DelayA",&fdDelayA))
+ || (prepareFile("DelayB",&fdDelayB))
+ || (mapBuffer(&bufferA,fdDelayA,fdB))
+ || (mapBuffer(&bufferB,fdDelayB,fdA)))
+ exit(1);
+
+ pid = fork();
+
+ if (pid == 0)
+ {
+ status = startIO(fdA,bufferA);
+ exit(status);
+ }
+
+ if (pid == -1)
+ {
+ exit(1);
+ }
+ status = startIO(fdB,bufferB);
+ exit(status);
+
+}
diff --git a/tools/test/stress2/misc/datamove2.sh b/tools/test/stress2/misc/datamove2.sh
new file mode 100755
index 000000000000..1b4c964a1f12
--- /dev/null
+++ b/tools/test/stress2/misc/datamove2.sh
@@ -0,0 +1,244 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Variation of the datamove.sh scenario by not using "sysctl vm.old_msync=1"
+
+# Deadlock seen:
+# https://people.freebsd.org/~pho/stress/log/numa042.txt
+
+# Test scenario by ups
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > datamove2.c
+mycc -o datamove2 -Wall datamove2.c
+rm -f datamove2.c
+
+for i in `jot 2`; do
+ $here/../testcases/swap/swap -t 5m -i 100 -h &
+ sleep 1
+ /tmp/datamove2 || { echo FAIL; r=1; }
+ while pkill -9 swap; do :; done
+ [ -n "$r" ] && break
+done
+rm -rf /tmp/datamove2
+exit $r
+EOF
+/*-
+ * Copyright (c) 2006, Stephan Uphoff <ups@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 ``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 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 <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+int prepareFile(char *filename, int *fdp);
+int mapBuffer (char **bufferp, int fd1, int fd2);
+int startIO (int fd, char *buffer);
+
+int pagesize;
+
+#define FILESIZE (32*1024)
+char wbuffer [FILESIZE];
+
+/* Create a FILESIZE sized file - then remove file data from the cache */
+int
+prepareFile(char *filename, int *fdp)
+{
+ int fd;
+ int len;
+ int status;
+ void *addr;
+
+ fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
+ if (fd == -1) {
+ perror("Creating file");
+ return fd;
+ }
+ len = write(fd, wbuffer, FILESIZE);
+ if (len < 0) {
+ perror("Write failed");
+ return 1;
+ }
+ status = fsync(fd);
+ if (status != 0) {
+ perror("fsync failed");
+ return 1;
+ }
+ addr = mmap(NULL, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (addr == MAP_FAILED) {
+ perror("Mmap failed");
+ return 1;
+ }
+ status = msync(addr, FILESIZE, MS_INVALIDATE | MS_SYNC);
+ if (status != 0) {
+ perror("Msync failed");
+ return 1;
+ }
+ if (munmap(addr, FILESIZE) == -1) {
+ perror("munmap failed");
+ return 1;
+ }
+
+ *fdp = fd;
+ return 0;
+}
+
+/* mmap a 2 page buffer - first page is from fd1, second page from fd2 */
+int
+mapBuffer(char **bufferp, int fd1, int fd2)
+{
+ void *addr;
+ char *buffer;
+
+ addr = mmap(NULL, pagesize * 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0);
+ if (addr == MAP_FAILED) {
+ perror("Mmap failed");
+ return 1;
+ }
+ buffer = addr;
+ addr = mmap(buffer + pagesize, pagesize, PROT_READ | PROT_WRITE, MAP_FIXED |
+ MAP_SHARED, fd2, 0);
+
+ if (addr == MAP_FAILED) {
+ perror("Mmap2 failed");
+ return 1;
+ }
+ *bufferp = buffer;
+ return 0;
+}
+
+void
+unmapBuffer(char *bufferp)
+{
+ if (munmap(bufferp, pagesize * 2) == -1)
+ err(1, "unmap 1. buffer");
+ /*
+ The following unmaps something random, which could trigger:
+ Program received signal SIGSEGV, Segmentation fault.
+ free (cp=0x28070000) at /usr/src/libexec/rtld-elf/malloc.c:311
+ */
+
+#if 0
+ if (munmap(bufferp + pagesize * 2, pagesize * 2) == -1)
+ err(1, "unmap 2. buffer");
+#endif
+}
+
+int
+startIO(int fd, char *buffer)
+{
+ ssize_t len;
+
+ len = write(fd, buffer, 2 * pagesize);
+ if (len == -1) {
+ perror("write failed");
+ return 1;
+ }
+ return 0;
+}
+
+int
+main(int argc, char *argv[], char *envp[])
+{
+
+ int fdA, fdB, fdDelayA, fdDelayB;
+ int status;
+ int i;
+ char *bufferA, *bufferB;
+ pid_t pid;
+
+ pagesize = getpagesize();
+
+ for (i = 0; i < 1000; i++) {
+ if ((prepareFile("A", &fdA))
+ || (prepareFile("B", &fdB))
+ || (prepareFile("DelayA", &fdDelayA))
+ || (prepareFile("DelayB", &fdDelayB))
+ || (mapBuffer(&bufferA, fdDelayA, fdB))
+ || (mapBuffer(&bufferB, fdDelayB, fdA)))
+ exit(1);
+
+ pid = fork();
+
+ if (pid == 0) {
+ status = startIO(fdA, bufferA);
+ exit(status);
+ }
+ if (pid == -1) {
+ perror("fork");
+ exit(1);
+ }
+ status = startIO(fdB, bufferB);
+ if (wait(&status) == -1)
+ err(1, "wait");
+
+ close(fdA);
+ close(fdB);
+ close(fdDelayA);
+ close(fdDelayB);
+ unmapBuffer(bufferA);
+ unmapBuffer(bufferB);
+ unlink("A");
+ unlink("B");
+ unlink("DelayA");
+ unlink("DelayB");
+ }
+ exit(status);
+
+}
diff --git a/tools/test/stress2/misc/datamove3.sh b/tools/test/stress2/misc/datamove3.sh
new file mode 100755
index 000000000000..47a1cbce4c95
--- /dev/null
+++ b/tools/test/stress2/misc/datamove3.sh
@@ -0,0 +1,241 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Threaded variation of datamove.sh
+
+# Based on a test scenario by ups and suggestions by kib
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > datamove3.c
+mycc -o datamove3 -Wall datamove3.c -lpthread
+rm -f datamove3.c
+
+n=5
+old=`sysctl vm.old_msync | awk '{print $NF}'`
+sysctl vm.old_msync=1
+for i in `jot $n`; do
+ mkdir -p /tmp/datamove3.dir.$i
+ cd /tmp/datamove3.dir.$i
+ /tmp/datamove3 &
+done
+cd /tmp
+for i in `jot $n`; do
+ wait
+done
+for i in `jot $n`; do
+ rm -rf /tmp/datamove3.dir.$i
+done
+sysctl vm.old_msync=$old
+
+rm -rf /tmp/datamove3
+exit 0
+EOF
+/*-
+ * Copyright (c) 2006, Stephan Uphoff <ups@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 ``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 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 <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+struct args {
+ char *bp;
+ int fd1;
+ int fd2;
+} a[2];
+
+int prepareFile(char *, int *);
+void * mapBuffer(void *);
+int startIO(int, char *);
+
+int pagesize;
+
+#define FILESIZE (32*1024)
+char wbuffer [FILESIZE];
+
+/* Create a FILESIZE sized file - then remove file data from the cache */
+int
+prepareFile(char *filename, int *fdp)
+{
+ int fd;
+ int len;
+ int status;
+ void *addr;
+
+ fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
+ if (fd == -1) {
+ perror("Creating file");
+ return fd;
+ }
+ len = write(fd, wbuffer, FILESIZE);
+ if (len < 0) {
+ perror("Write failed");
+ return 1;
+ }
+ status = fsync(fd);
+ if (status != 0) {
+ perror("fsync failed");
+ return 1;
+ }
+ addr = mmap(NULL, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (addr == MAP_FAILED) {
+ perror("Mmap failed");
+ return 1;
+ }
+ status = msync(addr, FILESIZE, MS_INVALIDATE | MS_SYNC);
+ if (status != 0) {
+ perror("Msync failed");
+ return 1;
+ }
+ munmap(addr, FILESIZE);
+
+ *fdp = fd;
+ return 0;
+}
+
+/* mmap a 2 page buffer - first page is from fd1, second page from fd2 */
+void *
+mapBuffer(void *ar)
+{
+ void *addr;
+ char *buffer;
+ int i;
+
+ i = (int )ar;
+ addr = mmap(NULL, pagesize * 2, PROT_READ | PROT_WRITE, MAP_SHARED, a[i].fd1, 0);
+ if (addr == MAP_FAILED) {
+ err(1, "Mmap failed");
+ }
+ buffer = addr;
+ addr = mmap(buffer + pagesize, pagesize, PROT_READ | PROT_WRITE, MAP_FIXED |
+ MAP_SHARED, a[i].fd2, 0);
+
+ if (addr == MAP_FAILED) {
+ err(1, "Mmap2 failed");
+ }
+ a[i].bp = buffer;
+ sleep(1);
+ return (NULL);
+}
+
+int
+startIO(int fd, char *buffer)
+{
+ ssize_t len;
+
+ len = write(fd, buffer, 2 * pagesize);
+ if (len == -1) {
+ warn("startIO(%d, %p): write failed", fd, buffer);
+ return 1;
+ }
+ return 0;
+}
+
+int
+main(int argc, char *argv[], char *envp[])
+{
+
+ int fdA, fdB, fdDelayA, fdDelayB;
+ int r, status;
+ char *bufferA, *bufferB;
+ pid_t pid;
+ pthread_t threads[2];
+
+ pagesize = getpagesize();
+
+ if ((prepareFile("A", &fdA))
+ || (prepareFile("B", &fdB))
+ || (prepareFile("DelayA", &fdDelayA))
+ || (prepareFile("DelayB", &fdDelayB)))
+ exit(1);
+
+ a[0].fd1 = fdDelayA;
+ a[0].fd2 = fdB;
+
+ a[1].fd1 = fdDelayB;
+ a[1].fd2 = fdA;
+
+ if ((r = pthread_create(&threads[0], NULL, mapBuffer, (void *)0)) != 0)
+ errc(1, r, "pthread_create()");
+ if ((r = pthread_create(&threads[1], NULL, mapBuffer, (void *)1)) != 0)
+ errc(1, r, "pthread_create()");
+
+ while (a[0].bp == NULL || a[1].bp == NULL)
+ pthread_yield();
+
+ bufferA = a[0].bp;
+ bufferB = a[1].bp;
+
+ pid = fork();
+
+ if (pid == 0) {
+ status = startIO(fdA, bufferA);
+ exit(status);
+ }
+ if (pid == -1) {
+ exit(1);
+ }
+ status = startIO(fdB, bufferB);
+ exit(status);
+
+}
diff --git a/tools/test/stress2/misc/datamove4.sh b/tools/test/stress2/misc/datamove4.sh
new file mode 100755
index 000000000000..4b4726b8dace
--- /dev/null
+++ b/tools/test/stress2/misc/datamove4.sh
@@ -0,0 +1,251 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Variation of the datamove2.sh, using TMPFS
+# Deadlock seen
+# https://people.freebsd.org/~pho/stress/log/datamove4.txt
+
+# panic: elf32_putnote: Note type 10 changed as we read it (2236 > 2220)...
+# https://people.freebsd.org/~pho/stress/log/datamove4-2.txt
+# Fixed by r288944.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > datamove4.c
+mycc -o datamove4 -Wall -Wextra -O2 -g datamove4.c
+rm -f datamove4.c
+
+mount | grep -q "$mntpoint " && umount $mntpoint
+mount -t tmpfs tmpfs $mntpoint
+chmod 777 $mntpoint
+
+for i in `jot 5`; do
+ su $testuser -c "cd $mntpoint; /tmp/datamove4"
+done
+while mount | grep -q $mntpoint; do
+ umount -f $mntpoint > /dev/null 2>&1
+done
+
+rm -rf /tmp/datamove4
+exit 0
+EOF
+/*-
+ * Copyright (c) 2006, Stephan Uphoff <ups@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 ``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 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 <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+int prepareFile(char *filename, int *fdp);
+int mapBuffer (char **bufferp, int fd1, int fd2);
+int startIO (int fd, char *buffer);
+
+int pagesize;
+
+#define FILESIZE (32*1024)
+char wbuffer [FILESIZE];
+
+/* Create a FILESIZE sized file - then remove file data from the cache */
+int
+prepareFile(char *filename, int *fdp)
+{
+ int fd;
+ int len;
+ int status;
+ void *addr;
+
+ fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
+ if (fd == -1) {
+ perror(filename);
+ return fd;
+ }
+ len = write(fd, wbuffer, FILESIZE);
+ if (len < 0) {
+ perror("Write failed");
+ return 1;
+ }
+ status = fsync(fd);
+ if (status != 0) {
+ perror("fsync failed");
+ return 1;
+ }
+ addr = mmap(NULL, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (addr == MAP_FAILED) {
+ perror("Mmap failed");
+ return 1;
+ }
+ status = msync(addr, FILESIZE, MS_INVALIDATE | MS_SYNC);
+ if (status != 0) {
+ perror("Msync failed");
+ return 1;
+ }
+ if (munmap(addr, FILESIZE) == -1) {
+ perror("munmap failed");
+ return 1;
+ }
+
+ *fdp = fd;
+ return 0;
+}
+
+/* mmap a 2 page buffer - first page is from fd1, second page from fd2 */
+int
+mapBuffer(char **bufferp, int fd1, int fd2)
+{
+ void *addr;
+ char *buffer;
+
+ addr = mmap(NULL, pagesize * 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0);
+ if (addr == MAP_FAILED) {
+ perror("Mmap failed");
+ return 1;
+ }
+ buffer = addr;
+ addr = mmap(buffer + pagesize, pagesize, PROT_READ | PROT_WRITE, MAP_FIXED |
+ MAP_SHARED, fd2, 0);
+
+ if (addr == MAP_FAILED) {
+ perror("Mmap2 failed");
+ return 1;
+ }
+ *bufferp = buffer;
+ return 0;
+}
+
+void
+unmapBuffer(char *bufferp)
+{
+ if (munmap(bufferp, pagesize * 2) == -1)
+ err(1, "unmap 1. buffer");
+ /*
+ The following unmaps something random, which could trigger:
+ Program received signal SIGSEGV, Segmentation fault.
+ free (cp=0x28070000) at /usr/src/libexec/rtld-elf/malloc.c:311
+ */
+
+#if 0
+ if (munmap(bufferp + pagesize * 2, pagesize * 2) == -1)
+ err(1, "unmap 2. buffer");
+#endif
+}
+
+int
+startIO(int fd, char *buffer)
+{
+ ssize_t len;
+
+ len = write(fd, buffer, 2 * pagesize);
+ if (len == -1) {
+ perror("write failed");
+ return 1;
+ }
+ return 0;
+}
+
+int
+main()
+{
+
+ int fdA, fdB, fdDelayA, fdDelayB;
+ int status;
+ int i;
+ char *bufferA, *bufferB;
+ pid_t pid;
+
+ pagesize = getpagesize();
+
+ for (i = 0; i < 1000; i++) {
+ if ((prepareFile("A", &fdA))
+ || (prepareFile("B", &fdB))
+ || (prepareFile("DelayA", &fdDelayA))
+ || (prepareFile("DelayB", &fdDelayB))
+ || (mapBuffer(&bufferA, fdDelayA, fdB))
+ || (mapBuffer(&bufferB, fdDelayB, fdA)))
+ exit(1);
+
+ pid = fork();
+
+ if (pid == 0) {
+ status = startIO(fdA, bufferA);
+ exit(status);
+ }
+ if (pid == -1) {
+ perror("fork");
+ exit(1);
+ }
+ status = startIO(fdB, bufferB);
+ if (wait(&status) == -1)
+ err(1, "wait");
+
+ close(fdA);
+ close(fdB);
+ close(fdDelayA);
+ close(fdDelayB);
+ unmapBuffer(bufferA);
+ unmapBuffer(bufferB);
+ unlink("A");
+ unlink("B");
+ unlink("DelayA");
+ unlink("DelayB");
+ }
+ exit(status);
+
+}
diff --git a/tools/test/stress2/misc/datamove5.sh b/tools/test/stress2/misc/datamove5.sh
new file mode 100755
index 000000000000..5f4a868f03b1
--- /dev/null
+++ b/tools/test/stress2/misc/datamove5.sh
@@ -0,0 +1,262 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Variation of the datamove2.sh, using NULLFS
+# No problems seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > datamove5.c
+mycc -o datamove5 -Wall -Wextra -O2 -g datamove5.c
+rm -f datamove5.c
+
+mp1=$mntpoint
+mp2=${mntpoint}2
+[ -d $mp2 ] || mkdir $mp2
+
+mount | grep -wq $mp2 && umount $mp2
+mount | grep -wq $mp1 && umount $mp1
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mp1
+
+mount -t nullfs $opt $mp1 $mp2
+chmod 777 $mp2
+
+for i in `jot 5`; do
+ su $testuser -c "cd $mp2; /tmp/datamove5"
+done
+
+while mount | grep -wq $mp2; do
+ umount $mp2 || sleep 1
+done
+while mount | grep $mp1 | grep -q /dev/md; do
+ umount $mp1 || sleep 1
+done
+mdconfig -d -u $mdstart
+
+rm -rf /tmp/datamove5
+exit 0
+EOF
+/*-
+ * Copyright (c) 2006, Stephan Uphoff <ups@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 ``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 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 <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+int prepareFile(char *filename, int *fdp);
+int mapBuffer (char **bufferp, int fd1, int fd2);
+int startIO (int fd, char *buffer);
+
+int pagesize;
+
+#define FILESIZE (32*1024)
+char wbuffer [FILESIZE];
+
+/* Create a FILESIZE sized file - then remove file data from the cache */
+int
+prepareFile(char *filename, int *fdp)
+{
+ int fd;
+ int len;
+ int status;
+ void *addr;
+
+ fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
+ if (fd == -1) {
+ perror(filename);
+ return fd;
+ }
+ len = write(fd, wbuffer, FILESIZE);
+ if (len < 0) {
+ perror("Write failed");
+ return 1;
+ }
+ status = fsync(fd);
+ if (status != 0) {
+ perror("fsync failed");
+ return 1;
+ }
+ addr = mmap(NULL, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (addr == MAP_FAILED) {
+ perror("Mmap failed");
+ return 1;
+ }
+ status = msync(addr, FILESIZE, MS_INVALIDATE | MS_SYNC);
+ if (status != 0) {
+ perror("Msync failed");
+ return 1;
+ }
+ if (munmap(addr, FILESIZE) == -1) {
+ perror("munmap failed");
+ return 1;
+ }
+
+ *fdp = fd;
+ return 0;
+}
+
+/* mmap a 2 page buffer - first page is from fd1, second page from fd2 */
+int
+mapBuffer(char **bufferp, int fd1, int fd2)
+{
+ void *addr;
+ char *buffer;
+
+ addr = mmap(NULL, pagesize * 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0);
+ if (addr == MAP_FAILED) {
+ perror("Mmap failed");
+ return 1;
+ }
+ buffer = addr;
+ addr = mmap(buffer + pagesize, pagesize, PROT_READ | PROT_WRITE, MAP_FIXED |
+ MAP_SHARED, fd2, 0);
+
+ if (addr == MAP_FAILED) {
+ perror("Mmap2 failed");
+ return 1;
+ }
+ *bufferp = buffer;
+ return 0;
+}
+
+void
+unmapBuffer(char *bufferp)
+{
+ if (munmap(bufferp, pagesize * 2) == -1)
+ err(1, "unmap 1. buffer");
+ /*
+ The following unmaps something random, which could trigger:
+ Program received signal SIGSEGV, Segmentation fault.
+ free (cp=0x28070000) at /usr/src/libexec/rtld-elf/malloc.c:311
+ */
+
+#if 0
+ if (munmap(bufferp + pagesize * 2, pagesize * 2) == -1)
+ err(1, "unmap 2. buffer");
+#endif
+}
+
+int
+startIO(int fd, char *buffer)
+{
+ ssize_t len;
+
+ len = write(fd, buffer, 2 * pagesize);
+ if (len == -1) {
+ perror("write failed");
+ return 1;
+ }
+ return 0;
+}
+
+int
+main()
+{
+
+ int fdA, fdB, fdDelayA, fdDelayB;
+ int status;
+ int i;
+ char *bufferA, *bufferB;
+ pid_t pid;
+
+ pagesize = getpagesize();
+
+ for (i = 0; i < 1000; i++) {
+ if ((prepareFile("A", &fdA))
+ || (prepareFile("B", &fdB))
+ || (prepareFile("DelayA", &fdDelayA))
+ || (prepareFile("DelayB", &fdDelayB))
+ || (mapBuffer(&bufferA, fdDelayA, fdB))
+ || (mapBuffer(&bufferB, fdDelayB, fdA)))
+ exit(1);
+
+ pid = fork();
+
+ if (pid == 0) {
+ status = startIO(fdA, bufferA);
+ exit(status);
+ }
+ if (pid == -1) {
+ perror("fork");
+ exit(1);
+ }
+ status = startIO(fdB, bufferB);
+ if (wait(&status) == -1)
+ err(1, "wait");
+
+ close(fdA);
+ close(fdB);
+ close(fdDelayA);
+ close(fdDelayB);
+ unmapBuffer(bufferA);
+ unmapBuffer(bufferB);
+ unlink("A");
+ unlink("B");
+ unlink("DelayA");
+ unlink("DelayB");
+ }
+ exit(status);
+
+}
diff --git a/tools/test/stress2/misc/db.sh b/tools/test/stress2/misc/db.sh
new file mode 100755
index 000000000000..580ee71c4c94
--- /dev/null
+++ b/tools/test/stress2/misc/db.sh
@@ -0,0 +1,178 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Demonstrate resource starvation using msync(2).
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/db.c
+mycc -o db -Wall -Wextra -O0 -g db.c -lpthread || exit 1
+rm -f db.c
+cd $odir
+
+dd if=/dev/zero of=$diskimage bs=1m count=10 status=none
+
+/tmp/db $diskimage &
+
+start=`date '+%s'`
+ls -l $diskimage > /dev/null # Will wait for more than 90 seconds
+[ `date '+%s'` -gt $((start + 90)) ] && fail="yes"
+wait
+e=$((`date +%s` - start))
+[ $fail ] &&
+ echo "Time for a ls is $((e / 60)) minutes $((e % 60)) seconds"
+rm -f /tmp/db $diskimage
+#[ $fail ] && exit 1 || exit 0 # Known issue
+exit 0
+
+EOF
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <errno.h>
+#include <pthread.h>
+#ifdef __FreeBSD__
+#include <pthread_np.h>
+#define __NP__
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <time.h>
+#include <unistd.h>
+
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+size_t len;
+void *p;
+int wthreads;
+
+#define BZ 128 /* buffer size */
+#define RUNTIME 180 /* runtime for test */
+#define RTHREADS 64 /* reader threads */
+#define WTHREADS 64 /* writer threads */
+
+void *
+wt(void *arg __unused)
+{
+ time_t start;
+ int64_t pos;
+ void *c;
+ int r;
+ char buf[BZ];
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+ if ((r = pthread_mutex_lock(&mutex)) != 0)
+ errc(1, r, "pthread_mutex_lock");
+ wthreads++;
+ if ((r = pthread_mutex_unlock(&mutex)) != 0)
+ errc(1, r, "pthread_mutex_unlock");
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ pos = arc4random() % (len / BZ);
+ pos = pos * BZ;
+ c = p + pos;
+ bcopy(buf, c, BZ);
+ c = (void *)trunc_page((unsigned long)c);
+ if (msync((void *)c, round_page(BZ), MS_SYNC) == -1)
+ err(1, "msync(%p)", c);
+ usleep(10000 + arc4random() % 1000);
+ }
+
+ if ((r = pthread_mutex_lock(&mutex)) != 0)
+ errc(1, r, "pthread_mutex_lock");
+ wthreads--;
+ if ((r = pthread_mutex_unlock(&mutex)) != 0)
+ errc(1, r, "pthread_mutex_unlock");
+
+ return (NULL);
+}
+
+void *
+rt(void *arg __unused)
+{
+ int64_t pos;
+ char buf[BZ], *c;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+ c = p;
+ do {
+ pos = arc4random() % (len / BZ);
+ pos = pos * BZ;
+ bcopy(&c[pos], buf, BZ);
+ usleep(10000 + arc4random() % 1000);
+ } while (wthreads != 0);
+
+ return (NULL);
+}
+
+int
+main(int argc, char *argv[])
+{
+ pthread_t cp[RTHREADS + WTHREADS];
+ struct stat st;
+ int fd, i, j, rc;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <file>", argv[0]);
+ if ((fd = open(argv[1], O_RDWR)) == -1)
+ err(1, "open %s", argv[1]);
+ if (fstat(fd, &st) == -1)
+ err(1, "stat");
+ len = round_page(st.st_size);
+ p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if ((void *)p == MAP_FAILED)
+ err(1, "mmap");
+
+ i = 0;
+ for (j = 0; j < WTHREADS; j++) {
+ if ((rc = pthread_create(&cp[i++], NULL, wt, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+ }
+ usleep(100);
+ for (j = 0; j < RTHREADS; j++) {
+ if ((rc = pthread_create(&cp[i++], NULL, rt, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+ }
+
+ for (j = 0; j < RTHREADS + WTHREADS; j++)
+ pthread_join(cp[--i], NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/dd.sh b/tools/test/stress2/misc/dd.sh
new file mode 100755
index 000000000000..ba57e7f1d0a2
--- /dev/null
+++ b/tools/test/stress2/misc/dd.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Watchdog fired:
+# https://people.freebsd.org/~pho/stress/log/mark013.txt
+# Fixed by: r327213
+
+# watchdogd: https://people.freebsd.org/~pho/stress/log/kostik1245.txt
+
+. ../default.cfg
+
+outputfile=$RUNDIR/dd.outputfile
+(cd ../testcases/swap; ./swap -t 30m -i 20) > /dev/null 2>&1 &
+trap "rm -f ${outputfile}*" EXIT INT
+N=2
+NCPU=`sysctl -n hw.ncpu`
+s=0
+size=512
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 720 ]; do
+ pids=""
+ for i in `jot $N`; do
+ dd if=/dev/zero of=${outputfile}$i bs=1m count=$size &
+ pids="$pids $!"
+ done > /dev/null 2>&1
+ for pid in $pids; do
+ wait $pid
+ s=$?
+ [ $s -ne 0 ] && break 2
+ done
+ N=$((N * 2))
+ rm -f ${outputfile}*
+ [ $N -gt $((NCPU * 2)) ] && break
+done
+
+while pgrep -q swap; do
+ pkill -9 swap
+done
+exit $s
diff --git a/tools/test/stress2/misc/dev.sh b/tools/test/stress2/misc/dev.sh
new file mode 100755
index 000000000000..1dc094a09e5b
--- /dev/null
+++ b/tools/test/stress2/misc/dev.sh
@@ -0,0 +1,137 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Test parallel read access to /dev.
+
+# "panic: Most recently used by DEVFS1" seen.
+# https://people.freebsd.org/~pho/stress/log/dev.txt
+# Fixed by r293826.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > dev.c
+mycc -o dev -Wall -Wextra -O2 dev.c || exit 1
+rm -f dev.c
+
+echo "Expect: g_access(958): provider ufsid/5c581221fcac7d80 has error 6 set"
+daemon sh -c \
+ "(cd $here/../testcases/swap; ./swap -t 6m -i 20 -k -l 100)" > \
+ /dev/null
+
+/tmp/dev # Note: this runs as root.
+s=$?
+
+while pkill -9 swap; do
+ :
+done
+
+rm -f /tmp/dev
+exit $s
+EOF
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define PARALLEL 4
+#define RUNTIME 300
+
+char path[80];
+
+void
+churn(char *path)
+{
+ FTS *fts;
+ FTSENT *p;
+ time_t start;
+ int fd, ftsoptions;
+ char *args[2];
+
+ ftsoptions = FTS_PHYSICAL;
+ args[0] = path;
+ args[1] = 0;
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ while ((p = fts_read(fts)) != NULL) {
+ if (p->fts_info == FTS_D ||
+ p->fts_info == FTS_DP)
+ continue;
+ if (strstr(p->fts_path, "ttyu") != NULL)
+ continue;
+ if (strstr(p->fts_path, "fcdm") != NULL)
+ continue;
+ if ((fd = open(p->fts_path, O_RDONLY|O_NONBLOCK)) == -1)
+ continue;
+ usleep(arc4random() % 1000);
+ close(fd);
+
+ }
+
+ if (errno != 0 && errno != ENOENT)
+ warn("fts_read");
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int e, i, status;;
+
+ e = 0;
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ churn("/dev");
+
+ for (i = 0; i < PARALLEL; i++) {
+ wait(&status);
+ if (status != 0)
+ e++;
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/dev2.sh b/tools/test/stress2/misc/dev2.sh
new file mode 100755
index 000000000000..5a194974e135
--- /dev/null
+++ b/tools/test/stress2/misc/dev2.sh
@@ -0,0 +1,144 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Test parallel access to /dev. A non-root version.
+
+# "panic: Bad link elm 0xfffff8000b07ed00 prev->next != elm" seen.
+# https://people.freebsd.org/~pho/stress/log/dev2.txt
+
+# Fixed by r294204.
+
+# "panic: Assertion !tty_gone(tp) failed at ../sys/ttydevsw.h:165" seen:
+# https://people.freebsd.org/~pho/stress/log/dev2-2.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+kldstat -v | grep -q pty || { kldload pty || exit 0; }
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > dev2.c
+mycc -o dev2 -Wall -Wextra -O2 dev2.c || exit 1
+rm -f dev2.c
+
+daemon sh -c \
+ "(cd $here/../testcases/swap; ./swap -t 6m -i 20 -k -l 100)" > \
+ /dev/null
+
+su $testuser -c /tmp/dev2
+
+while pkill -9 swap; do
+ :
+done
+
+rm -f /tmp/dev2
+exit
+EOF
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define PARALLEL 4
+#define RUNTIME 300
+
+jmp_buf jbuf;
+char path[80];
+
+void
+handler(int i __unused) {
+ longjmp(jbuf, 1);
+}
+
+void
+churn(char *path)
+{
+ FTS *fts;
+ FTSENT *p;
+ time_t start;
+ int fd, ftsoptions;
+ char *args[2];
+
+ ftsoptions = FTS_PHYSICAL;
+ args[0] = path;
+ args[1] = 0;
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ (void)setjmp(jbuf);
+ ualarm(0, 0);
+ while ((p = fts_read(fts)) != NULL) {
+ if (p->fts_info == FTS_D ||
+ p->fts_info == FTS_DP)
+ continue;
+ ualarm(500000, 0);
+ if ((fd = open(p->fts_path, arc4random() % (O_CLOEXEC << 2))) == -1)
+ continue;
+ ualarm(0, 0);
+ usleep(arc4random() % 1000);
+ close(fd);
+
+ }
+
+ if (errno != 0 && errno != ENOENT)
+ warn("fts_read");
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i;
+
+ signal(SIGALRM, handler);
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ churn("/dev");
+
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/dev3.sh b/tools/test/stress2/misc/dev3.sh
new file mode 100755
index 000000000000..b120266faf83
--- /dev/null
+++ b/tools/test/stress2/misc/dev3.sh
@@ -0,0 +1,160 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# pts memory leak regression test.
+
+# Leaks seen when flags is either O_SHLOCK or O_EXLOCK and /dev/ptmx and
+# /dev/pts/ is being opened.
+# Fixed in r313496.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+kldstat -v | grep -q pty || { kldload pty || exit 0; }
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > dev3.c
+mycc -o dev3 -Wall -Wextra -O2 dev3.c || exit 1
+rm -f dev3.c
+
+#(cd $here/../testcases/swap; ./swap -t 10h -i 20 -l 100) > \
+# /dev/null &
+
+pts=`vmstat -m | grep pts | awk '{print $2}'`
+[ -z "$pts" ] && pts=0
+
+e=0
+n=0
+while true; do
+ su $testuser -c "/tmp/dev3 $n"
+ new=`vmstat -m | grep pts | awk '{print $2}'`
+ if [ $new -gt $pts ]; then
+ leak=$((new - pts))
+ printf "flag %d (0x%x) leaks %d pts, %d allocated.\n" $n $n \
+ $leak $new
+ pts=$new
+ e=1
+ fi
+ [ $n -eq 0 ] && n=1 || n=$((n * 2))
+ [ $n -gt $((0x00200000)) ] && break # O_VERIFY
+done
+while pkill -9 swap; do
+ sleep 1
+done
+wait
+rm -f /tmp/dev3
+exit $e
+EOF
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define PARALLEL 4
+#define RUNTIME 60
+
+jmp_buf jbuf;
+char path[80];
+
+void
+handler(int i __unused) {
+ longjmp(jbuf, 1);
+}
+
+void
+churn(int flag, char *path)
+{
+ FTS *fts;
+ FTSENT *p;
+ time_t start;
+ int fd, ftsoptions;
+ char *args[2];
+
+ ftsoptions = FTS_PHYSICAL;
+ args[0] = path;
+ args[1] = 0;
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ (void)setjmp(jbuf);
+ ualarm(0, 0);
+ while ((p = fts_read(fts)) != NULL) {
+ if (p->fts_info == FTS_D ||
+ p->fts_info == FTS_DP)
+ continue;
+ ualarm(500000, 0);
+ if ((fd = open(p->fts_path, flag)) == -1)
+ continue;
+ ualarm(0, 0);
+ usleep(arc4random() % 1000);
+ close(fd);
+
+ }
+
+ if (errno != 0 && errno != ENOENT)
+ warn("fts_read");
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+ }
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int flag, i;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <flag>\n", argv[0]);
+ exit(1);
+ }
+ flag = atoi(argv[1]);
+ signal(SIGALRM, handler);
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ churn(flag, "/dev");
+
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/devfd.sh b/tools/test/stress2/misc/devfd.sh
new file mode 100755
index 000000000000..1f5eecece0ca
--- /dev/null
+++ b/tools/test/stress2/misc/devfd.sh
@@ -0,0 +1,130 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm
+# 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 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.
+#
+
+# "panic: vn_lock 0xc65b5828: zero hold count" seen.
+
+# Originally found by the iknowthis test suite
+# by Tavis Ormandy <taviso cmpxchg8b com>
+# Fixed by r227952
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > devfd.c
+rm -f /tmp/devfd
+mycc -o devfd -Wall -Wextra -O2 -g devfd.c -lpthread || exit 1
+rm -f devfd.c
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c "(cd $mntpoint; /tmp/devfd)"
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/devfd
+exit
+EOF
+#include <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+int fd[3], fd2[3];
+
+void *
+thr1(void *arg __unused)
+{
+ int i, j;
+ char path[80];
+
+ for (i = 0; i < 100000; i++) {
+ for (j = 0; j < 3; j++) {
+ if (fd[j] != -1)
+ close(fd[j]);
+ sprintf(path, "fx%d", j);
+ fd[j] = open(path, O_RDWR | O_CREAT, 0640);
+ }
+ }
+ return (0);
+}
+
+void *
+thr2(void *arg __unused)
+{
+ int i, j;
+ char path[80];
+
+ for (i = 0; i < 100000; i++) {
+ for (j = 0; j < 3; j++) {
+ if (fd2[j] != -1)
+ close(fd2[j]);
+ sprintf(path, "/dev/fd/%d", j);
+ if ((fd2[j] = open(path, O_RDONLY)) != -1)
+ fchflags(fd2[j], UF_NODUMP);
+ }
+
+ }
+ return (0);
+}
+
+int
+main(void)
+{
+ pthread_t p1, p2;
+ int r;
+
+ close(0);
+ close(1);
+ close(2);
+ if ((r = pthread_create(&p1, NULL, thr1, NULL)) != 0)
+ errc(1, r, "pthread_create");
+ if ((r = pthread_create(&p2, NULL, thr2, NULL)) != 0)
+ errc(1, r, "pthread_create");
+ pthread_join(p1, NULL);
+ pthread_join(p2, NULL);
+
+ return (0);
+}
+
diff --git a/tools/test/stress2/misc/devfs.sh b/tools/test/stress2/misc/devfs.sh
new file mode 100755
index 000000000000..acb351f26e56
--- /dev/null
+++ b/tools/test/stress2/misc/devfs.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Page fault seen:
+# https://people.freebsd.org/~pho/stress/log/devfs.txt
+# Fixed by r326851.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mounts=15 # Number of parallel scripts
+cont=/tmp/devfs.continue
+
+if [ $# -eq 0 ]; then
+ touch $cont
+ for i in `jot $mounts`; do
+ [ ! -d ${mntpoint}$i ] && mkdir ${mntpoint}$i
+ mount | grep -q "on ${mntpoint}$i " && umount ${mntpoint}$i
+ done
+
+ # start the parallel tests
+ for i in `jot $mounts`; do
+ ./$0 $i &
+ ./$0 find &
+ done
+ wait
+else
+ if [ $1 = find ]; then
+ while [ -r $cont ]; do
+ find ${mntpoint}* > /dev/null 2>&1
+ done
+ else
+
+ # The test: Parallel mount and unmounts
+ start=`date '+%s'`
+ while [ `date '+%s'` -lt $((start + 300)) ]; do
+ m=$1
+ mount -t devfs none ${mntpoint}$m
+ opt=`[ $(( m % 2 )) -eq 0 ] && echo -f`
+ while mount | grep -q " ${mntpoint}$m "; do
+ umount $opt ${mntpoint}$m > /dev/null 2>&1
+ done
+ done
+ rm -f $cont
+ fi
+fi
diff --git a/tools/test/stress2/misc/devfs2.sh b/tools/test/stress2/misc/devfs2.sh
new file mode 100755
index 000000000000..47f80a09f7f8
--- /dev/null
+++ b/tools/test/stress2/misc/devfs2.sh
@@ -0,0 +1,107 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Stopped at devfs_open+0x23f: pushl 0x14(%ebx)
+# db> where
+# Tracing pid 46017 tid 100350 td 0xc4c08510
+# devfs_open(e6d06a10) at devfs_open+0x23f
+# VOP_OPEN_APV(c09edda0,e6d06a10) at VOP_OPEN_APV+0x9b
+# vn_open_cred(e6d06b78,e6d06c78,0,c4883900,3,...) at vn_open_cred+0x41e
+# vn_open(e6d06b78,e6d06c78,0,3) at vn_open+0x1e
+# kern_open(c4c08510,8048887,0,1,0,...) at kern_open+0xb7
+# open(c4c08510,e6d06d00) at open+0x18
+# syscall(e6d06d38) at syscall+0x252
+
+# Test scenario by kib@freebsd.org
+
+. ../default.cfg
+
+odir=`pwd`
+dir=/tmp
+
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/devfs2.c
+mycc -o devfs2 -Wall devfs2.c -lthr || exit 1
+rm -f devfs2.c
+
+./devfs2
+
+rm devfs2
+exit 0
+
+EOF
+#include <pthread.h>
+#include <stdio.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <err.h>
+
+void *
+thr1(void *arg)
+{
+ int fd;
+ int i;
+
+ for (i = 0; i < 1024; i++) {
+ if ((fd = open("/dev/zero", O_RDONLY)) == -1)
+ perror("open /dev/zero");
+ close(fd);
+ }
+ return (0);
+}
+
+void *
+thr2(void *arg)
+{
+ int i;
+ for (i = 0; i < 1024; i++)
+ close(3);
+ return (0);
+}
+
+int
+main()
+{
+ pthread_t threads[2];
+ int i;
+ int r;
+
+ if ((r = pthread_create(&threads[0], NULL, thr1, 0)) != 0)
+ err(1, "pthread_create(): %s\n", strerror(r));
+ if ((r = pthread_create(&threads[1], NULL, thr2, 0)) != 0)
+ err(1, "pthread_create(): %s\n", strerror(r));
+
+ for (i = 0; i < 2; i++)
+ if ((r = pthread_join(threads[i], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", i);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/devfs3.sh b/tools/test/stress2/misc/devfs3.sh
new file mode 100755
index 000000000000..4bfce110eade
--- /dev/null
+++ b/tools/test/stress2/misc/devfs3.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Panic seen with two instances of devfs.sh running simultaneously.
+
+# "panic: dev_rel() gave negative count" seen:
+# https://people.freebsd.org/~pho/stress/log/devfs3.txt
+
+# "panic: No vop_unlock(0xfffff80089eb0750, 0xfffffe104944f4c8)" seen:
+# https://people.freebsd.org/~pho/stress/log/kostik898.txt
+
+# Fixed by r301928 + r301929.
+
+./devfs.sh > /dev/null 2>&1 &
+p1=$!
+./devfs.sh > /dev/null 2>&1 &
+p2=$!
+
+wait $p1
+wait $p2
diff --git a/tools/test/stress2/misc/devfs4.sh b/tools/test/stress2/misc/devfs4.sh
new file mode 100755
index 000000000000..eac87374db11
--- /dev/null
+++ b/tools/test/stress2/misc/devfs4.sh
@@ -0,0 +1,35 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Bug 235513 - panic: dead bo 0xfffff8000a838b48
+# https://people.freebsd.org/~pho/stress/log/devfs4.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+umount -f /dev && mount /dev
diff --git a/tools/test/stress2/misc/devfs5.sh b/tools/test/stress2/misc/devfs5.sh
new file mode 100755
index 000000000000..b0b1d98fc83e
--- /dev/null
+++ b/tools/test/stress2/misc/devfs5.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Test scenario for https://reviews.freebsd.org/D20411
+# Add devfs(5) support for VOP_MKDIR(9) and VOP_RMDIR(9)
+
+. ../default.cfg
+
+mkdir /dev/devfs5 2>/dev/null || exit 0
+rmdir /dev/devfs5
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+mount -t devfs null $mntpoint || exit 1
+
+(cd ../testcases/swap; ./swap -t 30m -i 20 -h -l 100) &
+spid=$!
+cd $mntpoint
+N=3000
+for i in `jot 25`; do
+ (
+ mkdir s$i; cd s$i
+ for k in `jot 5`; do
+ for j in `jot $N`; do mkdir d$i.$j; done
+ for j in `jot $N`; do rmdir d$i.$j; done
+ done
+ cd ..; rmdir s$i
+ ) &
+ pids="$pids $!"
+done
+for i in $pids; do
+ wait $i
+done
+while pkill swap; do :; done
+wait spid
+
+cd /
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint && break
+ sleep 1
+done
+exit 0
diff --git a/tools/test/stress2/misc/dfull.sh b/tools/test/stress2/misc/dfull.sh
new file mode 100755
index 000000000000..100784047dbc
--- /dev/null
+++ b/tools/test/stress2/misc/dfull.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# Processes stuck in "ufs" seen.
+# Looping softdep_request_cleanup() seen
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep "$mntpoint" | grep md${mdstart}$part > /dev/null && umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 512m -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+export RUNDIR=$mntpoint/stressX
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export KBLOCKS=$(($1 * 10))
+export INODES=$(($2 * 10))
+export runRUNTIME=20m
+export RUNTIME=1m
+
+(cd ..; ./run.sh disk.cfg > /dev/null 2>&1)
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/dtrace.sh b/tools/test/stress2/misc/dtrace.sh
new file mode 100755
index 000000000000..3521ef3099e5
--- /dev/null
+++ b/tools/test/stress2/misc/dtrace.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Simple dtrace(1) test scenario.
+# https://people.freebsd.org/~pho/stress/log/dtrace.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+dtrace -n 'dtrace:::BEGIN { exit(0); }' > /dev/null 2>&1 || exit 0
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=5m
+export RUNDIR=$mntpoint/stressX
+
+log=/tmp/dtrace.$$
+trap "rm -f $log" EXIT INT
+dtrace -w -n 'syscall::*read:entry,syscall::*write:entry {\
+ @rw[execname,probefunc] = count(); }' > $log 2>&1 &
+pid=$!
+sleep 1
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1
+kill -INT $pid > /dev/null 2>&1
+while pgrep -q dtrace; do
+ pkill dtrace
+ sleep 2
+done
+wait
+tail -5 $log
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+kldstat | grep -q dtraceall &&
+ kldunload dtraceall.ko
+exit 0
diff --git a/tools/test/stress2/misc/dtrace_fault.sh b/tools/test/stress2/misc/dtrace_fault.sh
new file mode 100755
index 000000000000..508cfc48cd59
--- /dev/null
+++ b/tools/test/stress2/misc/dtrace_fault.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Konstantin Belousov <kib@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Regression test for:
+
+# panic: invalid signal
+# KDB: stack backtrace:
+# db_trace_self_wrapper(c161045c,c18b9e7c,0,c15df420,f3de7ad8,...) at db_trace_self_wrapper+0x2a/frame 0xf3de7aa8
+# kdb_backtrace(c160aaf6,5984403e,0,f3de7b74,d,...) at kdb_backtrace+0x2d/frame 0xf3de7b10
+# vpanic(c160b255,f3de7b74,c160b255,f3de7b74,f3de7b74,...) at vpanic+0x133/frame 0xf3de7b44
+# kassert_panic(c160b255,c1612dc1,7d,cc97240c,0,...) at kassert_panic+0xd9/frame 0xf3de7b68
+# trapsignal(cc29ea80,f3de7c30,c0cb259e,2,0,...) at trapsignal+0x246/frame 0xf3de7ba0
+# trap(f3de7ce8) at trap+0x7fe/frame 0xf3de7cdc
+# calltrap() at calltrap+0x6/frame 0xf3de7cdc
+# --- trap 0x20, eip = 0x8048565, esp = 0xbfbfe7d4, ebp = 0xbfbfe7d4 ---
+
+# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=221151
+# Fixed by r321919
+
+. ../default.cfg
+
+uname -a | egrep -q "i386|amd64" || exit 0
+
+cat > /tmp/dtrace_fault.c <<EOF
+int
+main(void)
+{
+
+ __asm __volatile ("int \$0x92");
+}
+EOF
+
+mycc -o /tmp/dtrace_fault /tmp/dtrace_fault.c || exit 1
+rm /tmp/dtrace_fault.c
+
+/tmp/dtrace_fault
+
+rm /tmp/dtrace_fault
+exit 0
diff --git a/tools/test/stress2/misc/dumpfs.sh b/tools/test/stress2/misc/dumpfs.sh
new file mode 100755
index 000000000000..d43d52050e18
--- /dev/null
+++ b/tools/test/stress2/misc/dumpfs.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+export LANG=C
+disk=/tmp/dumpfs.sh.`date +%Y%m%dT%H%M%S`.img
+dump=${disk%.img}.dump
+good=/tmp/dumpfs.sh.good
+md=bc9011de1e3c33a3baf2bf195708417e # 20190902
+md=b86221bacc4651b0a92032f6989302c4 # 20200710
+md=a6f60b37ffca5fb61fc54ccbd00ecbcb # 20210109
+
+s=0
+
+dd if=/dev/zero of=$disk bs=512 count=20480 status=none
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -f $disk -u $mdstart
+newfs -U /dev/md$mdstart > /dev/null || exit 1
+
+mount /dev/md$mdstart $mntpoint || exit 1
+
+here=`pwd`
+cd $mntpoint
+for i in `jot 10`; do
+ echo $i
+done > file1
+cp file1 file2
+cp file2 file3
+mv file1 filea
+rm file2
+
+cd $here
+umount $mntpoint
+dumpfs /dev/md$mdstart | expand > $dump
+r=`sed 's/time *[A-Z].*//;s/id .*//' < $dump | md5`
+if [ $md != $r ]; then
+ echo "$r != $md"
+ s=1
+ [ -f $good ] && diff $good $dump
+else
+ [ ! -f $good ] && mv $dump $good # save good dump
+fi
+mdconfig -d -u $mdstart
+[ $s -eq 0 ] && rm -f $disk $dump
+rm -f $disk $dump # for now
+exit $s
diff --git a/tools/test/stress2/misc/dup.sh b/tools/test/stress2/misc/dup.sh
new file mode 100755
index 000000000000..7223709d09c4
--- /dev/null
+++ b/tools/test/stress2/misc/dup.sh
@@ -0,0 +1,122 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Test scenario for "D20947: Check and avoid overflow when incrementing
+# fp->f_count in fget_unlocked() and fhold()".
+
+# OOM killing seen. Cap files and procs for now.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+rm -f /tmp/dup /tmp/dup.c || exit 1
+sed '1,/^EOF/d' < $odir/$0 > $dir/dup.c
+mycc -o dup -Wall -Wextra dup.c || exit 1
+rm -f dup.c
+cd $odir
+
+/tmp/dup; s=$?
+
+rm -f /tmp/dup
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <fcntl.h>
+#include <err.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+#define N 10240
+#define CAP_FILES 50000
+#define CAP_PROCS 1000
+#define SYNC 0
+
+int
+main(void)
+{
+ struct stat st;
+ pid_t pid[N];
+ size_t len;
+ int fd, fd2, i, j, last;
+
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ if ((fd = open("/dev/zero", O_RDONLY)) == -1)
+ err(1, "open(/dev/zero)");
+ i = 0;
+ for (;;) {
+ if ((fd2 = dup(fd)) == -1) {
+ if (errno == EMFILE)
+ break;
+ err(1, "dup()");
+ }
+ last = fd2;
+ if (++i == CAP_FILES)
+ break;
+ }
+#if defined(DEBUG)
+ fprintf(stderr, "i = %d\n", i);
+#endif
+
+ for (i = 0; i < N; i++) {
+ if ((pid[i] = fork()) == 0) {
+ if (fstat(last, &st) == -1)
+ err(1, "stat(%s)", "/dev/zero");
+ while(share[SYNC] == 0)
+ usleep(100000);
+ _exit(0);
+ }
+ if (pid[i] == -1) {
+ warn("fork()");
+ i--;
+ break;
+ }
+ if (i + 1 == CAP_PROCS)
+ break;
+ }
+ share[SYNC] = 1;
+ for (j = 0; j < i; j++) {
+ if (waitpid(pid[j], NULL, 0) != pid[j])
+ err(1, "waitpid(%d), index %d", pid[j], j);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/dup2.sh b/tools/test/stress2/misc/dup2.sh
new file mode 100755
index 000000000000..2e575d6625f9
--- /dev/null
+++ b/tools/test/stress2/misc/dup2.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Regression test for r234131.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/dup2.c
+mycc -o dup2 -Wall -Wextra dup2.c || exit 1
+rm -f dup2.c
+cd $odir
+
+/tmp/dup2 || { echo FAIL; exit 1; }
+
+rm -f /tmp/dup2
+exit 0
+
+EOF
+#include <err.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ int error, fd2, i;
+
+ error = 0;
+ for (i = 0; i < 10000; i++) {
+ fd2 = arc4random() % 1000000;
+ if (dup2(1, fd2) == -1) {
+ if (errno != EBADF) {
+ warn("dup2(1, %d)", fd2);
+ error = 1;
+ break;
+ }
+ } else
+ close(fd2);
+ }
+
+ return (error);
+}
diff --git a/tools/test/stress2/misc/execi386.sh b/tools/test/stress2/misc/execi386.sh
new file mode 100755
index 000000000000..c59a96765746
--- /dev/null
+++ b/tools/test/stress2/misc/execi386.sh
@@ -0,0 +1,86 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Calling exec*(2) in a thread of a i386 binary on amd64 caused a reboot.
+# Test scenario by: Steven Chamberlain <stev..@pyro.eu.org>
+# Fixed by r266464
+
+[ `uname -p` = "amd64" ] || exit 0
+
+. ../default.cfg
+
+wd=/tmp/execi386.dir
+mkdir -p $wd
+here=`pwd`
+cd $wd
+
+cat > execi386.c <<EOF
+/* https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=743141 */
+#include <unistd.h>
+#include <pthread.h>
+
+void *
+thread_main() {
+ char *cmdline[] = { "./i386", NULL };
+
+ execve(cmdline[0], cmdline, NULL);
+
+ return (NULL);
+}
+
+int
+main() {
+ pthread_t thread;
+
+ pthread_create(&thread, NULL, thread_main, NULL);
+ pthread_join(thread, NULL);
+
+ return (0);
+}
+EOF
+
+mycc -o execi386 -Wall -Wextra -O2 -g execi386.c -lpthread || exit 1
+
+cat > i386.c <<EOF
+#include <stdio.h>
+
+int
+main(void)
+{
+ fprintf(stdout, "Hello, world\n");
+ return (0);
+}
+EOF
+
+mycc -m32 -o i386 -Wall -Wextra -O2 -g i386.c || exit 1
+
+./execi386 > /dev/null || echo FAIL
+
+cd $here
+rm -rf $wd
+exit 0
diff --git a/tools/test/stress2/misc/execve.sh b/tools/test/stress2/misc/execve.sh
new file mode 100755
index 000000000000..7a36ec4ca3e7
--- /dev/null
+++ b/tools/test/stress2/misc/execve.sh
@@ -0,0 +1,130 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Test of execve(2) from a threaded program.
+# "load: 5.40 cmd: bash 24517 [vmmaps] 2.45r 0.00u 0.00s 0% 3448k" seen.
+# Fixed by r282708.
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > execve.c
+mycc -o execve -Wall -Wextra -O2 execve.c -lpthread || exit 1
+rm -f execve.c
+
+daemon sh -c "(cd $here/../testcases/swap; ./swap -t 20m -i 20 -l 100)" > \
+ /dev/null 2>&1
+sleep `jot -r 1 1 9`
+for i in `jot 2`; do
+ /tmp/execve
+done
+while pgrep -q swap; do
+ pkill -9 swap
+done
+
+rm -f /tmp/execve /tmp/execve.core
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define LOOPS 50
+#define PARALLEL 50
+
+volatile int go;
+
+void *
+texecve(void *arg __unused)
+{
+ char *cmdline[] = { "/usr/bin/true", NULL };
+
+ while (go == 0)
+ usleep(100);
+ if (execve(cmdline[0], cmdline, NULL) == -1)
+ err(1, "execve");
+
+ return (NULL);
+}
+
+void
+test(void)
+{
+ pthread_t tid[5];
+ int i, rc;
+
+ go = 0;
+
+ for (i = 0; i < 5; i++) {
+ if ((rc = pthread_create(&tid[i], NULL, texecve, NULL)) != 0)
+ errc(1, rc, "texecve()");
+ }
+
+ usleep(arc4random() % 2000);
+ go = 1;
+
+ for (i = 0; i < 5; i++)
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ _exit(0);
+}
+
+int
+main(void)
+{
+ struct rlimit rl;
+ int i, j;
+
+ rl.rlim_max = rl.rlim_cur = 0;
+ if (setrlimit(RLIMIT_CORE, &rl) == -1)
+ warn("setrlimit");
+
+ for (i = 0; i < LOOPS; i++) {
+ for (j = 0; j < PARALLEL; j++) {
+ if (fork() == 0)
+ test();
+ }
+
+ for (j = 0; j < PARALLEL; j++)
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/exlock.sh b/tools/test/stress2/misc/exlock.sh
new file mode 100755
index 000000000000..6e88f9437177
--- /dev/null
+++ b/tools/test/stress2/misc/exlock.sh
@@ -0,0 +1,122 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Simple O_EXLOCK test scenario.
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > exlock.c
+mycc -o exlock -Wall -Wextra exlock.c || exit 1
+rm -f exlock.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c "cd $mntpoint; /tmp/exlock"
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/exlock
+exit
+EOF
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+
+char buf[128];
+
+#define PARALLEL 3
+
+static void
+tst(char *file, int n)
+{
+ int fd, i;
+
+ for (i = 0; i < (int)sizeof(buf); i++)
+ buf[i] = '0' + n;
+
+ for (i = 0; i < 1024 * 1024; i++) {
+ if ((fd = open(file, O_RDWR | O_CREAT | O_APPEND | O_EXLOCK,
+ 0644)) == -1)
+ err(1, "open(%s)", file);
+ if (write(fd, buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write");
+ close(fd);
+ }
+
+ _exit(0);
+}
+static void
+test(void)
+{
+ int i;
+ char file[80];
+
+ snprintf(file, sizeof(file), "f06%d", getpid());
+
+ for (i = 0; i < 3; i++)
+ if (fork() == 0)
+ tst(file, i);
+ for (i = 0; i < 3; i++)
+ wait(NULL);
+
+ unlink(file);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i;
+
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ test();
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/exlock2.sh b/tools/test/stress2/misc/exlock2.sh
new file mode 100755
index 000000000000..94e3f88f48e3
--- /dev/null
+++ b/tools/test/stress2/misc/exlock2.sh
@@ -0,0 +1,210 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# O_CREAT|O_EXCL|O_EXLOCK atomic implementation test.
+# Lots of input from kib@
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/exlock2.c
+mycc -o exlock2 -Wall -Wextra -O0 -g exlock2.c || exit 1
+rm -f exlock2.c
+cd $odir
+
+$dir/exlock2
+s=$?
+[ -f exlock2.core -a $s -eq 0 ] &&
+ { ls -l exlock2.core; mv exlock2.core $dir; s=1; }
+cd $odir
+
+rm -f $dir/exlock2 /tmp/exlock2.*.file
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdatomic.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static _Atomic(int) *share;
+static int quit;
+static char file[80];
+
+#define RUNTIME (2 * 60)
+#define SYNC 0
+
+static void
+handler(int s __unused)
+{
+ quit = 1;
+}
+
+static void
+test1(void)
+{
+ time_t start;
+ int fd, n;
+
+ signal(SIGHUP, handler);
+ n = 0;
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME && quit == 0) {
+ n++;
+ if ((fd = open(file, O_RDWR|O_CREAT|O_EXCL|O_EXLOCK,
+ DEFFILEMODE)) == -1)
+ err(1, "open(%s) creat", file);
+ unlink(file);
+ if (write(fd, "test", 5) != 5)
+ err(1, "write()");
+ while (share[SYNC] == 1)
+ ; /* wait for test2 to signal "done" */
+ close(fd);
+ }
+#if defined(DEBUG)
+ fprintf(stderr, "%s: n = %d\n", __func__, n);
+#endif
+
+ _exit(0);
+}
+
+static void
+test2(void)
+{
+ struct flock fl;
+ struct stat st;
+ time_t start;
+ int e, fd, n;
+
+ e = 0;
+ fd = 0;
+ n = 0;
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ share[SYNC] = 1;
+ if ((fd = open(file, O_RDWR)) == -1)
+ goto out;
+ n++;
+ memset(&fl, 0, sizeof(fl));
+ fl.l_start = 0;
+ fl.l_len = 0;
+ fl.l_type = F_WRLCK;
+ fl.l_whence = SEEK_SET;
+ if (fcntl(fd, F_SETLK, &fl) < 0) {
+ if (errno != EAGAIN)
+ err(1, "fcntl(F_SETFL)");
+ goto out;
+ }
+ /* test1 must have dropped the lock */
+ fprintf(stderr, "%s got the lock.\n", __func__);
+ if (fstat(fd, &st) == -1)
+ err(1, "stat(%s)", file);
+ /* As test1 has opened the file exclusivly, this
+ should not happen */
+ if (st.st_size == 0)
+ fprintf(stderr, "%s has size 0\n", file);
+ e = 1;
+ break;
+out:
+ if (fd != -1)
+ close(fd);
+ share[SYNC] = 0;
+ usleep(100);
+ }
+#if defined(DEBUG)
+ if (e != 0) {
+ system("ps -Uroot | grep -v grep | grep /tmp/exlock2 | "\
+ "awk '{print $1}' | xargs procstat -f");
+ }
+#endif
+ share[SYNC] = 0;
+
+ _exit(e);
+}
+
+int
+main(void)
+{
+ pid_t pid1, pid2;
+ size_t len;
+ int e, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ snprintf(file, sizeof(file), "/tmp/exlock2.%d.file", getpid());
+ if ((pid1 = fork()) == 0)
+ test1();
+ if (pid1 == -1)
+ err(1, "fork()");
+
+ if ((pid2 = fork()) == 0)
+ test2();
+ if (pid2 == -1)
+ err(1, "fork()");
+
+ if (waitpid(pid2, &status, 0) != pid2)
+ err(1, "waitpid(%d)", pid2);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pid2, WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ kill(pid1, SIGHUP);
+ if (waitpid(pid1, &status, 0) != pid1)
+ err(1, "waitpid(%d)", pid1);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pid1, WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/ext2fs.sh b/tools/test/stress2/misc/ext2fs.sh
new file mode 100755
index 000000000000..f4d33116faed
--- /dev/null
+++ b/tools/test/stress2/misc/ext2fs.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Deadlock seen: http://people.freebsd.org/~pho/stress/log/ext2fs.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# Uses mke2fs from sysutils/e2fsprogs
+[ -z "`type mke2fs 2>/dev/null`" ] &&
+ echo "Skipping test as mke2fs not installed" && exit 0
+
+mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+mke2fs -m 0 /dev/md$mdstart > /dev/null
+
+mount -t ext2fs /dev/md$mdstart $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg'
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/ext2fs2.sh b/tools/test/stress2/misc/ext2fs2.sh
new file mode 100755
index 000000000000..a576c582cc7e
--- /dev/null
+++ b/tools/test/stress2/misc/ext2fs2.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "panic: ext2_dirbad: /mnt: bad dir ino ...: mangled entry" seen.
+# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=191895
+
+. ../default.cfg
+
+# Uses mke2fs from sysutils/e2fsprogs
+[ -x /usr/local/sbin/mke2fs ] || exit 0
+mount | grep "$mntpoint" | grep -q md$mdstart && umount -f $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+mke2fs /dev/md${mdstart}a
+# No panic seen when disabling hashed b-tree lookup for large directories
+# tune2fs -O ^dir_index /dev/md${mdstart}$part
+mount -t ext2fs /dev/md${mdstart}$part $mntpoint
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=10m # Run tests for 10 minutes
+(cd ..; ./run.sh disk.cfg)
+
+while mount | grep "$mntpoint " | grep -q md$mdstart; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/ext2fs3.sh b/tools/test/stress2/misc/ext2fs3.sh
new file mode 100755
index 000000000000..f33b4cc73547
--- /dev/null
+++ b/tools/test/stress2/misc/ext2fs3.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# ext2fs(5) test scenario with a 1k block size
+# "panic: ext2_reallocblks: alloc mismatch" seen.
+# "Fatal trap 12: page fault while in kernel mode" seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -r /usr/src/tools/regression/fsx/fsx.c ] || exit 0
+
+. ../default.cfg
+
+# Uses mke2fs from sysutils/e2fsprogs
+[ -z "`type mke2fs 2>/dev/null`" ] &&
+ echo "Skipping test as mke2fs not installed" && exit 0
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+cc -o fsx -Wall -Wextra -O2 -g /usr/src/tools/regression/fsx/fsx.c || exit 1
+rm -f fsx.c
+cd $odir
+
+mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+mke2fs -m 0 -b 1024 /dev/md$mdstart > /dev/null
+
+mount -t ext2fs /dev/md$mdstart $mntpoint
+chmod 777 $mntpoint
+
+cp /tmp/fsx $mntpoint
+cd $mntpoint
+./fsx -S 2016 -N 2000 ./TEST_FILE
+cd $here
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/ext3fs.sh b/tools/test/stress2/misc/ext3fs.sh
new file mode 100755
index 000000000000..826317857f85
--- /dev/null
+++ b/tools/test/stress2/misc/ext3fs.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Simple ext3 test.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# Uses mke2fs from sysutils/e2fsprogs
+[ -z "`type mke2fs 2>/dev/null`" ] &&
+ echo "Skipping test as mke2fs not installed" && exit 0
+
+mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+mke2fs -t ext3 -m 0 -b 2048 /dev/md$mdstart > /dev/null
+
+mount -t ext2fs /dev/md$mdstart $mntpoint || exit 1
+chmod 777 $mntpoint
+
+export runRUNTIME=5m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' &
+sleep 300
+../tools/killall.sh
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/ext4fs.sh b/tools/test/stress2/misc/ext4fs.sh
new file mode 100755
index 000000000000..610351352501
--- /dev/null
+++ b/tools/test/stress2/misc/ext4fs.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Simple ext4 test.
+
+# "panic: buf_vlist_add: Preallocated nodes insufficient" seen:
+# https://people.freebsd.org/~pho/stress/log/mjguzik036.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# Uses mke2fs from sysutils/e2fsprogs
+[ -z "`type mke2fs 2>/dev/null`" ] &&
+ echo "Skipping test as mke2fs not installed" && exit 0
+
+mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+mke2fs -t ext4 -m 0 -b 2048 /dev/md$mdstart > /dev/null
+
+mount -t ext2fs /dev/md$mdstart $mntpoint || exit 1
+chmod 777 $mntpoint
+
+export runRUNTIME=5m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' &
+sleep 300
+../tools/killall.sh
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/extattr.sh b/tools/test/stress2/misc/extattr.sh
new file mode 100755
index 000000000000..31027eec696b
--- /dev/null
+++ b/tools/test/stress2/misc/extattr.sh
@@ -0,0 +1,95 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Regression test of extattr on a UFS2 FS using ACLs
+# Caused a "Duplicate free" panic.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+[ -z "`which setfacl`" ] && exit 0
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > extattr.c
+mycc -o extattr -Wall extattr.c
+rm -f extattr.c
+cd $odir
+
+mount | grep "$mntpoint" | grep -q md${mdstart}$part && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 20m -u $mdstart
+bsdlabel -w md$mdstart auto
+
+newfs -O 2 md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+mkdir -p $mntpoint/.attribute/system
+cd $mntpoint/.attribute/system
+
+extattrctl initattr -p . 388 posix1e.acl_access
+extattrctl initattr -p . 388 posix1e.acl_default
+cd /
+umount $mntpoint
+tunefs -a enable /dev/md${mdstart}$part
+mount /dev/md${mdstart}$part $mntpoint
+mount | grep md${mdstart}$part
+
+touch $mntpoint/acl-test
+setfacl -b $mntpoint/acl-test
+setfacl -m user:nobody:rw-,group:wheel:rw- $mntpoint/acl-test
+
+for i in `jot 5`; do
+ /tmp/extattr $mntpoint/acl-test &
+done
+for i in `jot 5`; do
+ wait
+done
+
+umount $mntpoint
+mdconfig -d -u $mdstart
+rm -f /tmp/extattr
+exit
+EOF
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <err.h>
+
+int
+main(int argc, char **argv)
+{
+ int i;
+ struct stat sb;
+
+ for (i = 0; i < 100000; i++)
+ if (lstat(argv[1], &sb) == -1)
+ err(1, "lstat(%s)", argv[1]);
+ return (0);
+}
diff --git a/tools/test/stress2/misc/extattr2.sh b/tools/test/stress2/misc/extattr2.sh
new file mode 100755
index 000000000000..313d4ea26447
--- /dev/null
+++ b/tools/test/stress2/misc/extattr2.sh
@@ -0,0 +1,94 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# [Bug 230962] Kernel panic when writing extended attributes with soft updates
+# enabled.
+# "panic: softdep_deallocate_dependencies: dangling deps" seen:
+# https://people.freebsd.org/~pho/stress/log/kostik1121.txt
+# Fixed in r343536.
+# "panic: ffs_truncate3" seen:
+# https://people.freebsd.org/~pho/stress/log/extattr2.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+[ -z "`which setfacl`" ] && exit 0
+
+here=`pwd`
+mount | grep "$mntpoint" | grep -q md${mdstart}$part && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+mkdir -p $mntpoint/.attribute/system
+cd $mntpoint/.attribute/system
+
+extattrctl initattr -p . 388 posix1e.acl_access
+extattrctl initattr -p . 388 posix1e.acl_default
+cd /
+umount $mntpoint
+tunefs -a enable /dev/md${mdstart}$part
+mount /dev/md${mdstart}$part $mntpoint
+mount | grep md${mdstart}$part
+
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+if [ `jot -r 1 0 1` -eq 1 ]; then
+ set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+ export KBLOCKS=$(($1 / 2))
+ export INODES=$(($2 / 2))
+fi
+
+mkdir -p $RUNDIR
+chmod 0777 $RUNDIR
+setfacl -b $RUNDIR
+setfacl -m user:$testuser:rwx,group:$testuser:rwx $RUNDIR
+su $testuser -c "cd $here/..; ./run.sh marcus.cfg" &
+
+sleep 5
+while pgrep -U$testuser -q -f run.sh; do
+ find $RUNDIR | \
+ xargs -P0 -J% setfacl -m user:$testuser:rwx,group:$testuser:rwx %
+done > /dev/null 2>&1
+wait
+
+s=0
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FAIL; fstat -mf $mntpoint; exit 1; }
+done
+checkfs /dev/md${mdstart}$part || s=1
+mdconfig -d -u $mdstart || s=2
+exit $s
diff --git a/tools/test/stress2/misc/extattr3.sh b/tools/test/stress2/misc/extattr3.sh
new file mode 100755
index 000000000000..84b5e1821473
--- /dev/null
+++ b/tools/test/stress2/misc/extattr3.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Bug 230962 Kernel panic when writing extended attributes with soft updates
+# "panic: softdep_disk_write_complete: softdep_bp_to_mp returned NULL with
+# outstanding dependencies" seen.
+
+# Original test scenario by Koro <koro@kzxiv.net>
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+[ -z "`which setfacl`" ] && exit 0
+
+here=`pwd`
+mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 512m -u $mdstart
+newfs $newfs_flags md$mdstart > /dev/null
+tunefs -a enable /dev/md$mdstart
+mount /dev/md$mdstart $mntpoint
+setfacl -d -m u::rwx,g::rx,o::,u:nobody:r $mntpoint
+
+timeout 2s nc -lU $mntpoint/socket
+
+s=0
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FAIL; fstat -mf $mntpoint; exit 1; }
+done
+checkfs /dev/md$mdstart || s=1
+mdconfig -d -u $mdstart || s=2
+exit $s
diff --git a/tools/test/stress2/misc/extattr_set_fd.sh b/tools/test/stress2/misc/extattr_set_fd.sh
new file mode 100755
index 000000000000..22bfa52f4813
--- /dev/null
+++ b/tools/test/stress2/misc/extattr_set_fd.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# "panic: kmem_malloc(2069012480): kmem_map too small" seen.
+# Fixed in r237366.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+[ -z "`which setfacl`" ] && exit 0
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > extattr_set_fd.c
+mycc -o extattr_set_fd -Wall -Wextra -O2 extattr_set_fd.c
+rm -f extattr_set_fd.c
+
+mount | grep -q "$mntpoint" && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+(cd $mntpoint; /tmp/extattr_set_fd)
+
+while mount | grep -q $mntpoint; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/extattr_set_fd
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/extattr.h>
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+char buf[4096];
+
+int
+main(void)
+{
+ int fd;
+
+ if ((fd = open("theFile", O_RDWR | O_CREAT, 0622)) == -1)
+ err(1, "open(%s)", "theFile");
+
+ (void) extattr_set_fd(fd, 1, "test", buf, 0x7b5294a6);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/extattrctl.sh b/tools/test/stress2/misc/extattrctl.sh
new file mode 100755
index 000000000000..dad79023a2fc
--- /dev/null
+++ b/tools/test/stress2/misc/extattrctl.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Regression test of extattrctl and ACLs on UFS1 FS
+# Kernel must be compiled with options UFS_EXTATTR and UFS_EXTATTR_AUTOSTART
+
+# Scenario by rwatson@ from:
+#
+# Newsgroups: lucky.freebsd.current
+# Subject: Re: setfacl requirements?
+# Date: Thu, 5 Dec 2002 15:50:02 +0000 (UTC)
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ "`sysctl -in kern.features.ufs_extattr`" != "1" ] &&
+ { echo "Kernel not build with UFS_EXTATTR"; exit 0; }
+[ -z "`which setfacl`" ] && exit 0
+
+mount | grep "$mntpoint" | grep -q md${mdstart}$part && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 20m -u $mdstart
+bsdlabel -w md$mdstart auto
+
+newfs -O 1 md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+mkdir -p $mntpoint/.attribute/system
+cd $mntpoint/.attribute/system
+
+extattrctl initattr -p . 388 posix1e.acl_access
+extattrctl initattr -p . 388 posix1e.acl_default
+cd /
+umount $mntpoint
+tunefs -a enable /dev/md${mdstart}$part
+mount /dev/md${mdstart}$part $mntpoint
+mount | grep md${mdstart}$part
+
+touch $mntpoint/acl-test
+setfacl -b $mntpoint/acl-test
+setfacl -m user:nobody:rw-,group:wheel:rw- $mntpoint/acl-test
+getfacl $mntpoint/acl-test
+ls -l $mntpoint/acl-test
+
+umount $mntpoint
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/f_offset.sh b/tools/test/stress2/misc/f_offset.sh
new file mode 100755
index 000000000000..746f14bea265
--- /dev/null
+++ b/tools/test/stress2/misc/f_offset.sh
@@ -0,0 +1,160 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Problem seen with atomic assignment of f_offset. Fixed in r238029.
+
+# Test scenario by kib@
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > f_offset.c
+mycc -o f_offset -Wall -Wextra -O2 f_offset.c -lpthread
+rm -f f_offset.c
+
+/tmp/f_offset
+
+rm -f /tmp/f_offset
+exit 0
+EOF
+/*
+ Description by kib:
+To really exercise the race conditions, all the following items must
+be fulfilled simultaneously:
+1. you use 32bit host, i.e. i386
+2. you operate on the file offsets larger than 4GB (but see below)
+3. there are several threads or processes that operate on the same
+ file descriptor simultaneously.
+
+Please note that the normal fork(2) causes file descriptor table
+copy, so only rfork(2) call with RFFDG flag unset causes sharing. Or,
+multi-threading can be used.
+ */
+
+#include <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+int errors, fd;
+char file[128];
+
+#define START 0x100000000ULL
+#define N 1000000
+
+void *
+t1(void *arg __unused)
+{
+ int i;
+ off_t offset;
+
+ offset = START + 2;
+
+ for (i = 0; i < N; i++) {
+ if (lseek(fd, offset, SEEK_SET) == -1)
+ err(1, "lseek error");
+ }
+
+ return (0);
+}
+
+void *
+t2(void *arg __unused)
+{
+ int i;
+ off_t offset;
+
+ offset = 1;
+
+ for (i = 0; i < N; i++) {
+ if (lseek(fd, offset, SEEK_SET) == -1)
+ err(1, "lseek error");
+ }
+ return (0);
+}
+void *
+t3(void *arg __unused)
+{
+ int i;
+ off_t offset;
+
+ offset = 1;
+
+ for (i = 0; i < N; i++) {
+ if ((offset = lseek(fd, 0, SEEK_CUR)) == -1)
+ err(1, "lseek error");
+ if (offset != 1 && offset != START + 2)
+ fprintf(stderr, "FAIL #%d offset = %10jd (0x%09jx)\n",
+ errors++, offset, offset);
+ }
+
+ return (0);
+}
+
+int
+main(void)
+{
+ pthread_t threads[3];
+ int r;
+ int i;
+ off_t offset;
+
+ snprintf(file, sizeof(file), "file.%06d", getpid());
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
+ err(1, "%s", file);
+
+ offset = 1;
+ if (lseek(fd, offset, SEEK_SET) == -1)
+ err(1, "lseek error");
+
+ for (i = 0; i < 20 && errors < 10; i++) {
+ if ((r = pthread_create(&threads[0], NULL, t1, 0)) != 0)
+ errc(1, r, "pthread_create()");
+ if ((r = pthread_create(&threads[1], NULL, t2, 0)) != 0)
+ errc(1, r, "pthread_create()");
+ if ((r = pthread_create(&threads[2], NULL, t3, 0)) != 0)
+ errc(1, r, "pthread_create()");
+
+ if ((r = pthread_join(threads[0], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", 0);
+ if ((r = pthread_join(threads[1], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", 1);
+ if ((r = pthread_join(threads[2], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", 2);
+ }
+ close(fd);
+ if (unlink(file) == -1)
+ err(3, "unlink(%s)", file);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/fcntl.sh b/tools/test/stress2/misc/fcntl.sh
new file mode 100755
index 000000000000..ccbcc45a325f
--- /dev/null
+++ b/tools/test/stress2/misc/fcntl.sh
@@ -0,0 +1,234 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# fcntl(2) locking scenario. No problems seen.
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > fcntl.c
+mycc -o fcntl -Wall -Wextra -O0 -g fcntl.c || exit 1
+rm -f fcntl.c
+
+mkdir -p $RUNDIR
+cd $RUNDIR
+/tmp/fcntl
+status=$?
+
+rm -f /tmp/fcntl
+exit $status
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define PARALLEL 16
+#define N 4096
+
+static volatile sig_atomic_t completed;
+const char name[] = "work";
+int fd;
+
+static void
+ahandler(int s __unused)
+{
+ unlink(name);
+ _exit(1);
+}
+
+static void
+handler(int s __unused)
+{
+ completed++;
+}
+
+void
+add(int n, int increment)
+{
+ struct flock fl;
+ off_t pos;
+ long val, oval;
+ int r;
+
+ pos = n * sizeof(val);
+ memset(&fl, 0, sizeof(fl));
+ fl.l_start = pos;
+ fl.l_len = sizeof(val);
+ fl.l_type = F_WRLCK;
+ fl.l_whence = SEEK_SET;
+
+ while (fcntl(fd, F_SETLK, &fl) < 0) {
+ if (errno != EAGAIN)
+ err(1, "F_SETLK (child)");
+ usleep(100);
+ }
+
+ if (lseek(fd, pos, SEEK_SET) == -1)
+ err(1, "lseek");
+ oval = 999999;
+ while ((r = read(fd, &val, sizeof(val)) != sizeof(val))) {
+ if (r == -1 && errno != EAGAIN)
+ err(1, "read");
+ if (lseek(fd, pos, SEEK_SET) == -1)
+ err(1, "lseek");
+ }
+ oval = val;
+ val = val + increment;
+#if defined(DEBUG)
+ fprintf(stderr, "add(%d, %d) @ pos %ld: %ld = %ld + %d\n",
+ n, increment, (long)pos, val, oval, increment);
+#endif
+ if (lseek(fd, pos, SEEK_SET) == -1)
+ err(1, "lseek");
+ while ((r = write(fd, &val, sizeof(val)) != sizeof(val))) {
+ if (r == -1 && errno != EAGAIN)
+ err(1, "write");
+ if (lseek(fd, pos, SEEK_SET) == -1)
+ err(1, "lseek");
+ }
+
+ fl.l_type = F_UNLCK;
+ if (fcntl(fd, F_SETLK, &fl) < 0)
+ err(1, "F_UNLCK");
+
+}
+
+void
+up(void)
+{
+ int flags, i;
+
+ /* Need to re-open after a fork() */
+ close(fd);
+ if ((fd = open(name, O_RDWR)) == -1)
+ err(1, "open(%s)", name);
+ if ((flags = fcntl(fd, F_GETFL)) == -1)
+ err(1, "fcntl(%d, T_GETFL)", fd);
+ flags |= O_NONBLOCK;
+ if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
+ err(1, "fcntl(%d, T_SETFL, %d)", fd, flags);
+
+ for (i = 0; i < N; i++)
+ add(i, 1);
+
+ kill(getppid(), SIGHUP);
+ while (access("work", R_OK) == 0)
+ usleep(100);
+
+ _exit(0);
+}
+
+void
+down(void)
+{
+ int flags, i;
+
+ close(fd);
+ if ((fd = open(name, O_RDWR)) == -1)
+ err(1, "open(%s)", name);
+ if ((flags = fcntl(fd, F_GETFL)) == -1)
+ err(1, "fcntl(%d, T_GETFL)", fd);
+ flags |= O_NONBLOCK;
+ if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
+ err(1, "fcntl(%d, T_SETFL, %d)", fd, flags);
+
+ for (i = 0; i < N; i++)
+ add(i, -1);
+
+ kill(getppid(), SIGHUP);
+ while (access("work", R_OK) == 0)
+ usleep(100);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int flags, i;
+ long val, sum;
+ off_t len;
+
+ signal(SIGHUP, handler);
+ signal(SIGALRM, ahandler);
+ alarm(300);
+ if ((fd = open(name, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1, "open(%s)", name);
+ len = N * sizeof(val);
+ if (ftruncate(fd, len) == -1)
+ err(1, "ftruncate");
+
+ if ((flags = fcntl(fd, F_GETFL)) == -1)
+ err(1, "fcntl(%d, T_GETFL)", fd);
+ flags |= O_NONBLOCK;
+ if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
+ err(1, "fcntl(%d, T_SETFL, %d)", fd, flags);
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ up();
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ down();
+ }
+
+ while (completed != PARALLEL * 2)
+ usleep(200);
+
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ err(1, "lseek");
+ sum = 0;
+ for (i = 0; i < N; i++) {
+ if (read(fd, &val, sizeof(val)) != sizeof(val))
+ err(1, "Final read");
+ if (val != 0)
+ fprintf(stderr, "index %d: %ld\n", i, val);
+ sum += val;
+ }
+ if (sum != 0)
+ fprintf(stderr, "FAIL\n");
+ unlink(name);
+
+ for (i = 0; i < PARALLEL; i++) {
+ wait(NULL);
+ wait(NULL);
+ }
+
+ close(fd);
+
+ return (sum != 0);
+}
diff --git a/tools/test/stress2/misc/fcntl2.sh b/tools/test/stress2/misc/fcntl2.sh
new file mode 100755
index 000000000000..5f276ae81ccc
--- /dev/null
+++ b/tools/test/stress2/misc/fcntl2.sh
@@ -0,0 +1,188 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# fcntl(2) fuzz
+# "umount: unmount of /mnt failed: Device busy" seen:
+# https://people.freebsd.org/~pho/stress/log/fcntl2.txt
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/fcntl2.c
+mycc -o fcntl2 -Wall -Wextra -O0 -g fcntl2.c || exit 1
+rm -f fcntl2.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+newfs $newfs_flags md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+(cd $odir/../testcases/swap; ./swap -t 3m -i 20) &
+cd $mntpoint
+limits -n 10000 $dir/fcntl2
+s=$?
+[ -f fcntl2.core -a $s -eq 0 ] &&
+ { ls -l fcntl2.core; mv fcntl2.core $dir; s=1; }
+cd $odir
+while pkill swap; do :; done
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/fcntl2
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#define PARALLEL 64
+#define RUNTIME (3 * 60)
+#define SYNC 0
+
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+static u_int32_t r[N];
+
+static unsigned long
+makearg(void)
+{
+ unsigned long val;
+ unsigned int i;
+
+ val = arc4random();
+ i = arc4random() % 100;
+ if (i < 20)
+ val = val & 0xff;
+ if (i >= 20 && i < 40)
+ val = val & 0xffff;
+ if (i >= 40 && i < 60)
+ val = (unsigned long)(r) | (val & 0xffff);
+#if defined(__LP64__)
+ if (i >= 60) {
+ val = (val << 32) | arc4random();
+ if (i > 80)
+ val = val & 0x00007fffffffffffUL;
+ }
+#endif
+
+ return (val);
+}
+static void
+test(void)
+{
+ time_t start;
+ unsigned long arg3;
+ int cmd, fd, i, n, success;
+ char file[80];
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ success = 0;
+ snprintf(file, sizeof(file), "file.%d", getpid());
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE))
+ == -1)
+ err(1, "open(%s)", file);
+ n = arc4random() % 100 + 1;
+ for (i = 0; i < n; i++)
+ if (write(fd, file, sizeof(file)) != sizeof(file))
+ err(1, "write()");
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ cmd = arc4random() % 20;
+ arg3 = makearg();
+ alarm(20);
+ if (fcntl(fd, cmd, arg3) != -1)
+ success++;
+ alarm(0);
+ }
+ close(fd);
+ unlink(file);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int i, status;
+
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ for (i = 0; i < N; i++)
+ r[i] = arc4random();
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ }
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/fcntl3.sh b/tools/test/stress2/misc/fcntl3.sh
new file mode 100755
index 000000000000..8892a9d5670b
--- /dev/null
+++ b/tools/test/stress2/misc/fcntl3.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Mark Johnston <markj@freebsd.org>
+#
+# 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 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.
+#
+
+# Test scenario submitted by Mark Johnston <markj@freebsd.org>
+
+# "Fatal trap 18: integer divide fault while in kernel mode" seen.
+# Reported by syzkaller
+# Fixed by r353010
+
+cat > /tmp/fcntl3.c <<EOF
+#include <err.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+
+ if (fcntl(STDIN_FILENO, F_RDAHEAD) != 0)
+ err(1, "fcntl");
+ return (0);
+}
+EOF
+cc -o /tmp/fcntl3 -Wall -Wextra -O2 /tmp/fcntl3.c || exit 1
+
+echo "Expect: fcntl3: fcntl: Inappropriate ioctl for device"
+/tmp/fcntl3
+
+rm -f /tmp/fcntl3 /tmp/fcntl3.c
+exit 0
diff --git a/tools/test/stress2/misc/fdatasync.sh b/tools/test/stress2/misc/fdatasync.sh
new file mode 100755
index 000000000000..f17e2826ad94
--- /dev/null
+++ b/tools/test/stress2/misc/fdatasync.sh
@@ -0,0 +1,197 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# fdatasync(2) fuzz.
+
+# Deadlock seen:
+# https://people.freebsd.org/~pho/stress/log/fdatasync.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+dir=$RUNDIR
+nfiles=500
+[ `df -i $dir | tail -1 | awk '{print $7}'` -lt $nfiles ] && exit 0
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > fdatasync.c
+rm -f /tmp/fdatasync
+mycc -o fdatasync -Wall -Wextra -O2 -g fdatasync.c -lpthread || exit 1
+rm -f fdatasync.c
+
+mkdir -p $dir && chmod 777 $dir
+
+cd $dir
+jot $nfiles | xargs touch
+jot $nfiles | xargs chmod 666
+cd $odir
+
+(cd /tmp; /tmp/fdatasync $dir)
+e=$?
+
+rm -rf $dir/* /tmp/fdatasync
+exit $e
+EOF
+#include <sys/param.h>
+#include <sys/resource.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <libutil.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+#define RUNTIME 180
+#define THREADS 2
+
+static int fd[900];
+static u_int32_t r[N];
+static char *args[2];
+
+static unsigned long
+makearg(void)
+{
+ unsigned long val;
+
+ val = arc4random();
+#if defined(__LP64__)
+ val = (val << 32) | arc4random();
+ val = val & 0x00007fffffffffffUL;
+#endif
+
+ return(val);
+}
+
+static void *
+test(void *arg __unused)
+{
+ FTS *fts;
+ FTSENT *p;
+ int ftsoptions, i, n;
+
+ ftsoptions = FTS_PHYSICAL;
+
+ for (;;) {
+ for (i = 0; i < N; i++)
+ r[i] = arc4random();
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ i = n = 0;
+ while ((p = fts_read(fts)) != NULL) {
+ if (fd[i] > 0)
+ close(fd[i]);
+ if ((fd[i] = open(p->fts_path, O_RDWR)) == -1)
+ if ((fd[i] = open(p->fts_path, O_WRONLY)) ==
+ -1)
+ continue;
+ if (ftruncate(fd[i], 0) != 0)
+ err(1, "ftruncate");
+ i++;
+ i = i % nitems(fd);
+ }
+
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+ sleep(1);
+ }
+ return(0);
+}
+
+static void *
+calls(void *arg __unused)
+{
+ off_t offset;
+ time_t start;
+ int fd2;
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ fd2 = makearg() % nitems(fd) + 3;
+ offset = makearg();
+ if (lseek(fd2, offset - 1, SEEK_SET) != -1) {
+ if (write(fd2, "x", 1) != 1)
+ if (errno != EBADF && errno != ENOSPC &&
+ errno != E2BIG && errno != ESTALE &&
+ errno != EFBIG)
+ warn("write");
+ } else
+ if (errno != EBADF)
+ warn("lseek");
+ if (fdatasync(fd2) == -1)
+ if (errno != EBADF)
+ warn("x");
+
+ }
+
+ return (0);
+}
+
+int
+main(int argc, char **argv)
+{
+ struct passwd *pw;
+ pthread_t rp, cp[THREADS];
+ int e, i;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <dir>\n", argv[0]);
+ exit(1);
+ }
+ args[0] = argv[1];
+ args[1] = 0;
+
+ if ((pw = getpwnam("nobody")) == NULL)
+ err(1, "failed to resolve nobody");
+ if (setgroups(1, &pw->pw_gid) ||
+ setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
+ seteuid(pw->pw_uid) || setuid(pw->pw_uid))
+ err(1, "Can't drop privileges to \"nobody\"");
+ endpwent();
+
+ if ((e = pthread_create(&rp, NULL, test, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ usleep(1000);
+ for (i = 0; i < THREADS; i++)
+ if ((e = pthread_create(&cp[i], NULL, calls, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ for (i = 0; i < THREADS; i++)
+ pthread_join(cp[i], NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/fdatasync2.sh b/tools/test/stress2/misc/fdatasync2.sh
new file mode 100755
index 000000000000..6011eba53698
--- /dev/null
+++ b/tools/test/stress2/misc/fdatasync2.sh
@@ -0,0 +1,196 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# fdatasync(2) fuzz. Variation of fdatasync.sh.
+# https://people.freebsd.org/~pho/stress/log/fdatasync2.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+dir=$RUNDIR
+nfiles=10000
+[ `df -i $dir | tail -1 | awk '{print $7}'` -lt $nfiles ] && exit 0
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > fdatasync2.c
+rm -f /tmp/fdatasync2
+mycc -o fdatasync2 -Wall -Wextra -O2 -g fdatasync2.c -lpthread || exit 1
+rm -f fdatasync2.c
+
+mkdir -p $dir && chmod 777 $dir
+
+cd $dir
+jot $nfiles | xargs touch
+jot $nfiles | xargs chmod 666
+cd $odir
+
+(cd /tmp; /tmp/fdatasync2 $dir)
+e=$?
+
+rm -rf $dir/* /tmp/fdatasync2
+exit $e
+EOF
+#include <sys/param.h>
+#include <sys/resource.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <libutil.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+#define RUNTIME 180
+#define THREADS 6
+
+static int fd[900];
+static u_int32_t r[N];
+static char *args[2];
+
+static unsigned long
+makearg(void)
+{
+ unsigned long val;
+
+ val = arc4random();
+#if defined(__LP64__)
+ val = (val << 32) | arc4random();
+ val = val & 0x00007fffffffffffUL;
+#endif
+
+ return(val);
+}
+
+static void *
+test(void *arg __unused)
+{
+ FTS *fts;
+ FTSENT *p;
+ int ftsoptions, i, n;
+
+ ftsoptions = FTS_PHYSICAL;
+
+ for (;;) {
+ for (i = 0; i < N; i++)
+ r[i] = arc4random();
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ i = n = 0;
+ while ((p = fts_read(fts)) != NULL) {
+ if (fd[i] > 0)
+ close(fd[i]);
+ if ((fd[i] = open(p->fts_path, O_RDWR)) == -1)
+ if ((fd[i] = open(p->fts_path, O_WRONLY)) ==
+ -1)
+ continue;
+ if (ftruncate(fd[i], 0) != 0)
+ err(1, "ftruncate");
+ i++;
+ i = i % nitems(fd);
+ }
+
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+ sleep(1);
+ }
+ return(0);
+}
+
+static void *
+calls(void *arg __unused)
+{
+ off_t offset;
+ time_t start;
+ int fd2;
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ fd2 = makearg() % nitems(fd) + 3;
+ offset = makearg();
+ if (lseek(fd2, offset - 1, SEEK_SET) != -1) {
+ if (write(fd2, "x", 1) != 1)
+ if (errno != EBADF && errno != ENOSPC &&
+ errno != E2BIG && errno != ESTALE &&
+ errno != EFBIG)
+ warn("write");
+ } else
+ if (errno != EBADF)
+ warn("lseek");
+ if (fdatasync(fd2) == -1)
+ if (errno != EBADF)
+ warn("x");
+
+ }
+
+ return (0);
+}
+
+int
+main(int argc, char **argv)
+{
+ struct passwd *pw;
+ pthread_t rp, cp[THREADS];
+ int e, i, threads;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <dir>\n", argv[0]);
+ exit(1);
+ }
+ args[0] = argv[1];
+ args[1] = 0;
+ threads = arc4random() % (THREADS -1 ) + 2; /* 2 - THREADS */
+
+ if ((pw = getpwnam("nobody")) == NULL)
+ err(1, "failed to resolve nobody");
+ if (setgroups(1, &pw->pw_gid) ||
+ setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
+ seteuid(pw->pw_uid) || setuid(pw->pw_uid))
+ err(1, "Can't drop privileges to \"nobody\"");
+ endpwent();
+
+ if ((e = pthread_create(&rp, NULL, test, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ usleep(1000);
+ for (i = 0; i < threads; i++)
+ if ((e = pthread_create(&cp[i], NULL, calls, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ for (i = 0; i < threads; i++)
+ pthread_join(cp[i], NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/fdescfs.sh b/tools/test/stress2/misc/fdescfs.sh
new file mode 100755
index 000000000000..b0f07e34b285
--- /dev/null
+++ b/tools/test/stress2/misc/fdescfs.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Page fault seen in fdesc_allocvp+0x8f:
+# http://people.freebsd.org/~pho/stress/log/fdescfs-2.txt
+# Fixed by r279401
+# https://people.freebsd.org/~pho/stress/log/fdescfs-3.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mounts=15 # Number of parallel scripts
+cont=/tmp/fdescfs.continue
+
+if [ $# -eq 0 ]; then
+ kldstat -v | grep -q fdescfs || { kldload fdescfs.ko; unload=1; }
+ touch $cont
+ # start the parallel tests
+ for i in `jot $mounts`; do
+ [ -d ${mntpoint}$i ] || mkdir -p ${mntpoint}$i
+ ./$0 $i &
+ ./$0 find &
+ done
+ wait
+ [ $unload ] && kldunload fdescfs.ko
+ exit 0
+else
+ if [ $1 = find ]; then
+ exec 6< /dev/zero
+ exec 7< /dev/zero
+ exec 8< /dev/zero
+ exec 9< /dev/zero
+ while [ -r $cont ]; do
+ ls -l ${mntpoint}* > /dev/null 2>&1
+ done
+ else
+
+ # The test: Parallel mount and unmounts
+ start=`date '+%s'`
+ while [ `date '+%s'` -lt $((start + 300)) ]; do
+ mount -t fdescfs null ${mntpoint}$1
+ while mount | grep -wq ${mntpoint}$1; do
+ umount -f ${mntpoint}$1 > /dev/null 2>&1
+ done
+ done
+ rm -f $cont
+ fi
+fi
diff --git a/tools/test/stress2/misc/fdgrowtable.sh b/tools/test/stress2/misc/fdgrowtable.sh
new file mode 100755
index 000000000000..b75094489412
--- /dev/null
+++ b/tools/test/stress2/misc/fdgrowtable.sh
@@ -0,0 +1,105 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Regression test for r236822.
+# http://people.freebsd.org/~pho/stress/log/fdgrowtable.txt
+# Fixed in r256210.
+
+. ../default.cfg
+
+max=`ulimit -n`
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > fdgrowtable.c
+mycc -o fdgrowtable -Wall -Wextra -O2 fdgrowtable.c || exit 1
+rm -f fdgrowtable.c
+cd $here
+
+su $testuser -c "/tmp/fdgrowtable $max" &
+while kill -0 $! 2>/dev/null; do
+ ../testcases/swap/swap -t 2m -i 40 -h
+done
+wait
+rm -f /tmp/fdgrowtable
+exit
+
+EOF
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define PARALLEL 3
+
+int max;
+
+void test(void)
+{
+ int i;
+
+ for (i = 0; i < max; i++) {
+ if (dup2(1, i + 3) == -1)
+ err(1, "dup2(%d)", i + 3);
+ }
+ _exit(0);
+}
+
+int
+main(int argc, char **argv)
+{
+ time_t start;
+ int i;
+
+ if (argc == 2)
+ max = atoi(argv[1]);
+ else
+ err(1, "Usage: %s <maxfiles>", argv[0]);
+
+ max = (max - 3) / PARALLEL;
+
+ start = time(NULL);
+ while (time(NULL) - start < 600) {
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ test();
+ }
+
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/fexecve.sh b/tools/test/stress2/misc/fexecve.sh
new file mode 100755
index 000000000000..01ff0b723134
--- /dev/null
+++ b/tools/test/stress2/misc/fexecve.sh
@@ -0,0 +1,94 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Reported by syzkaller.
+# "panic: vm_page_free_prep: page 0x61e5968 has unexpected ref_count ." seen
+# Fixed by r352748
+
+# Test scenario by: Mark Johnston <markj@freebsd.org>
+
+cat > /tmp/fexecve.c <<EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+#include <fcntl.h>
+#include <err.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int
+main(int argc __unused, char **argv)
+{
+ char template[PATH_MAX];
+ void *addr;
+ size_t sz;
+ int fd;
+
+ sz = 16 * 4096;
+
+ (void)snprintf(template, sizeof(template), "fexecve.XXXXXX");
+ fd = mkstemp(template);
+ if (fd < 0)
+ err(1, "mkstemp");
+ if (fchmod(fd, 0700) < 0)
+ err(1, "fchmod");
+ if (ftruncate(fd, sz) < 0)
+ err(1, "ftruncate");
+
+ addr = mmap(NULL, sz, PROT_MAX(PROT_READ) | PROT_READ, MAP_SHARED,
+ fd, 0);
+ if (addr == MAP_FAILED)
+ err(1, "mmap");
+
+ if (mlock(addr, sz) != 0)
+ err(1, "mlock");
+
+ if (ftruncate(fd, 0) != 0)
+ err(1, "ftruncate");
+ if (ftruncate(fd, sz) != 0)
+ err(1, "ftruncate");
+
+ (void)close(fd);
+
+ fd = open(template, O_EXEC);
+ if (fd < 0)
+ err(1, "open");
+ fexecve(fd, argv, NULL);
+ err(1, "fexecve");
+
+ return (0);
+}
+EOF
+cc -o /tmp/fexecve -Wall -Wextra -O2 /tmp/fexecve.c || exit 1
+echo "Expect: fexecve: fexecve: Input/output error"
+(cd /tmp; /tmp/fexecve)
+
+rm -f /tmp/fexecve /tmp/fexecve.c /tmp/fexecve.??????
diff --git a/tools/test/stress2/misc/ffs_blkfree.sh b/tools/test/stress2/misc/ffs_blkfree.sh
new file mode 100755
index 000000000000..5ea4828429c3
--- /dev/null
+++ b/tools/test/stress2/misc/ffs_blkfree.sh
@@ -0,0 +1,76 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+D=$diskimage
+DUMP=$RUNDIR/dump
+trap "rm -f $D" EXIT INT
+dd if=/dev/zero of=$D bs=1m count=1024 status=none || exit 1
+
+mount | grep "$mntpoint" | grep md${mdstart}$part > /dev/null &&
+ umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+mount
+
+for i in `jot 10`; do
+ mkdir $mntpoint/d$i
+ for j in `jot 100`; do
+ touch $mntpoint/d$i/f$j
+ done
+done
+
+dump -L0a -f $DUMP /dev/md${mdstart}$part
+
+ls -lf $DUMP
+
+while mount | grep -q $mntpoint; do
+ umount $([ $((`date '+%s'` % 2)) -eq 0 ] &&
+ echo "-f" || echo "") $mntpoint > /dev/null 2>&1
+done
+
+for i in `jot 10`; do
+ newfs $newfs_flags -n md${mdstart}$part > /dev/null
+ mount /dev/md${mdstart}$part $mntpoint
+ (cd $mntpoint; restore -rf $DUMP)
+ rm -rf $mntpoint/*
+ while mount | grep -q $mntpoint; do
+ umount $([ $((`date '+%s'` % 2)) -eq 0 ] &&
+ echo "-f" || echo "") $mntpoint > /dev/null 2>&1
+ done
+done
+
+mdconfig -d -u $mdstart
+rm -f $D $DUMP
diff --git a/tools/test/stress2/misc/ffs_sync.sh b/tools/test/stress2/misc/ffs_sync.sh
new file mode 100755
index 000000000000..aa970839c7b3
--- /dev/null
+++ b/tools/test/stress2/misc/ffs_sync.sh
@@ -0,0 +1,254 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "ffs_fsync: dirty" seen:
+# http://people.freebsd.org/~pho/stress/log/ffs_sync.txt
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > ffs_sync.c
+mycc -o ffs_sync -Wall -Wextra ffs_sync.c || exit 1
+rm -f ffs_sync.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 4g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+for i in `jot 3`; do
+ su $testuser -c "cd $mntpoint; /tmp/ffs_sync" &
+ sleep 60
+ killall -q ffs_sync
+ killall -q ffs_sync
+done
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+rm -f /tmp/ffs_sync
+mdconfig -d -u $mdstart
+exit
+EOF
+#include <sys/param.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <libutil.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define PARALLEL 12
+#define NRW 1000
+#define NFTS 10000
+#define NMKDIR 6000
+#define NSYMLINK 20000
+
+void
+slinktest(void)
+{
+ int i, j;
+ pid_t pid;
+ char file[128];
+
+ setproctitle("slink");
+ pid = getpid();
+ for (j = 0; j < NSYMLINK; j++) {
+ sprintf(file,"p%05d.%05d", pid, j);
+ if (symlink("/tmp/not/there", file) == -1) {
+ if (errno != EINTR)
+ warn("symlink(%s). %s.%d", file, __FILE__, __LINE__);
+ }
+ }
+
+ for (i = --j; i >= 0; i--) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if (unlink(file) == -1)
+ err(3, "unlink(%s)", file);
+ }
+
+ _exit(0);
+}
+
+void
+mktest(void)
+{
+ int i;
+ char path[80];
+
+ setproctitle("mkdir");
+ sprintf(path, "d%06d", getpid());
+ if (mkdir(path, 0770) == -1)
+ err(1, "mkdir(%s)", path);
+ chdir(path);
+
+ sprintf(path, "d");
+ for (i = 0; i < NMKDIR; i++) {
+ if (mkdir(path, 0770) == -1) {
+ warn("mkdir(%s), %s:%d", path, __FILE__, __LINE__);
+ } else
+ chdir(path);
+
+ }
+ for (i = 0; i < NMKDIR; i++) {
+ chdir("..");
+ rmdir(path);
+ }
+ chdir("..");
+
+ _exit(0);
+}
+
+void
+rwtest(void)
+{
+ int fd, i, j;
+ char buf[80], file[80];
+
+ setproctitle("rw");
+ for (i = 0; i < NRW; i++) {
+ sprintf(file, "f%06d.%06d", getpid(), i);
+ if ((fd = open(file, O_CREAT | O_EXCL, 0644)) == -1)
+ err(1, "open(%s)", file);
+ for (j = 0; j < 1024; j++)
+ write(fd, buf, sizeof(buf));
+ lseek(fd, 0, SEEK_SET);
+ for (j = 0; j < 1024; j++)
+ read(fd, buf, sizeof(buf));
+ close(fd);
+ }
+ for (i = 0; i < NRW; i++) {
+ sprintf(file, "f%06d.%06d", getpid(), i);
+ if (unlink(file) == -1)
+ warn("unlink(%s)", file);
+ }
+ _exit(0);
+}
+
+void
+slink(void)
+{
+ int i;
+
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ slinktest();
+}
+
+void
+mk(void)
+{
+ int i;
+
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ mktest();
+}
+
+void
+rw(void)
+{
+ int i;
+
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ rwtest();
+}
+
+void
+ftstest(void)
+{
+ FTS *fts;
+ FTSENT *p;
+ int ftsoptions, i;
+ char *args[2];
+
+ setproctitle("fts");
+ ftsoptions = FTS_PHYSICAL;
+ args[0] = ".";
+ args[1] = 0;
+
+ for (i = 0; i < NFTS; i++) {
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ while ((p = fts_read(fts)) != NULL)
+ ;
+
+ if (errno != 0 && errno != ENOENT)
+ err(1, "fts_read");
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+ }
+
+ _exit(0);
+}
+
+void
+fts(void)
+{
+ int i;
+
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ ftstest();
+}
+
+int
+main(void)
+{
+ int i;
+
+ slink();
+ mk();
+ rw();
+ fts();
+
+ for (i = 0; i < PARALLEL; i++) {
+ wait(NULL);
+ wait(NULL);
+ wait(NULL);
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/ffs_syncvnode.sh b/tools/test/stress2/misc/ffs_syncvnode.sh
new file mode 100755
index 000000000000..faa9856a8bca
--- /dev/null
+++ b/tools/test/stress2/misc/ffs_syncvnode.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# "panic: flush_newblk_deps: Bad newblk 0xc8d2ac00" seen.
+
+# "panic: softdep_deallocate_dependencies: dangling deps" seen with
+# /mnt: out of inodes. 20130627.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=20m
+export RUNDIR=$mntpoint/stressX
+
+find -x / -type f > /dev/null 2>&1 &
+su $testuser -c 'cd ..; ./run.sh disk.cfg'
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/ffs_syncvnode2.sh b/tools/test/stress2/misc/ffs_syncvnode2.sh
new file mode 100755
index 000000000000..5fd9e4cfe843
--- /dev/null
+++ b/tools/test/stress2/misc/ffs_syncvnode2.sh
@@ -0,0 +1,63 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# Lock seen with:
+# 1001 61985 61981 0 52 0 9624 1028 ufs D+ 0 0:22,89 mkdir
+# 1001 61986 61981 0 52 0 9624 1028 ufs D+ 0 0:21,63 mkdir
+# 1001 61987 61981 0 52 0 9624 1028 ufs D+ 0 0:23,39 mkdir
+
+# Fixed in rxxxxxx.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=20m
+export RUNDIR=$mntpoint/stressX
+
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export KBLOCKS=$(($1 * 4))
+export INODES=$(($2 * 4))
+
+su $testuser -c 'cd ..; ./run.sh disk.cfg' 2>/dev/null
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/fifo.sh b/tools/test/stress2/misc/fifo.sh
new file mode 100755
index 000000000000..17881bd9eb00
--- /dev/null
+++ b/tools/test/stress2/misc/fifo.sh
@@ -0,0 +1,87 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Page fault seen.
+# http://people.freebsd.org/~pho/stress/log/kostik652.txt
+# Fixed by r259522.
+
+# https://people.freebsd.org/~pho/stress/log/kostik863.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > fifo.c
+rm -f /tmp/fifo
+mycc -o fifo -Wall -Wextra -O2 -g fifo.c || exit 1
+rm -f fifo.c
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+(cd $mntpoint; /tmp/fifo)
+
+for i in `jot 10`; do
+ mount | grep -q md${mdstart}$part && \
+ umount $mntpoint && mdconfig -d -u $mdstart && break
+ sleep 10
+done
+rm -f /tmp/fifo
+exit
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ int fd;
+
+ if (mkfifo("fifo", 0644) == -1)
+ err(1, "mkfifo");
+
+ fd = open("fifo", O_RDWR | O_SHLOCK | O_EXLOCK);
+ fd = open("fifo", 0x60e9f2, 0xc74c65b1db4be370, 0xb64a34df72368759);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/fifo2.sh b/tools/test/stress2/misc/fifo2.sh
new file mode 100755
index 000000000000..7f269f2368d4
--- /dev/null
+++ b/tools/test/stress2/misc/fifo2.sh
@@ -0,0 +1,206 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Page fault seen
+# http://people.freebsd.org/~pho/stress/log/kostik654.txt
+# Fixed by r259521.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > fifo2.c
+rm -f /tmp/fifo2
+mycc -o fifo2 -Wall -Wextra -O2 -g fifo2.c -lpthread || exit 1
+rm -f fifo2.c
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+mkfifo $mntpoint/f
+chmod 777 $mntpoint/f
+
+sleeptime=12
+st=`date '+%s'`
+while [ $((`date '+%s'` - st)) -lt $((10 * sleeptime)) ]; do
+ (cd $mntpoint; /tmp/fifo2) &
+ start=`date '+%s'`
+ while [ $((`date '+%s'` - start)) -lt $sleeptime ]; do
+ pgrep -q fifo2 || break
+ sleep .5
+ done
+ while pkill -9 fifo2; do :; done
+ wait
+done
+
+for i in `jot 10`; do
+ mount | grep -q md${mdstart}$part && \
+ umount $mntpoint > /dev/null 2>&1 &&
+ mdconfig -d -u $mdstart && break
+ sleep 10
+done
+s=0
+mount | grep -q md${mdstart}$part &&
+ { echo "umount $mntpoint failed"; s=1; }
+rm -f /tmp/fifo2
+exit $s
+EOF
+#include <sys/types.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <libutil.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+u_int32_t r[N];
+
+static void
+hand(int i __unused) { /* handler */
+ _exit(1);
+}
+
+static unsigned long
+makearg(void)
+{
+ unsigned int i;
+ unsigned long val;
+
+ val = arc4random();
+ i = arc4random() % 100;
+ if (i < 20)
+ val = val & 0xff;
+ if (i >= 20 && i < 40)
+ val = val & 0xffff;
+ if (i >= 40 && i < 60)
+ val = (unsigned long)(r) | (val & 0xffff);
+#if defined(__LP64__)
+ if (i >= 60) {
+ val = (val << 32) | arc4random();
+ if (i > 80)
+ val = val & 0x00007fffffffffffUL;
+ }
+#endif
+
+ return(val);
+}
+
+static void *
+calls(void *arg __unused)
+{
+ unsigned long arg1, arg2, arg3, arg4, arg5, arg6, arg7;
+ int i, num;
+
+ for (i = 0;; i++) {
+ arg1 = (unsigned long)(void *)"f";
+ arg2 = makearg();
+ arg3 = makearg();
+ arg4 = makearg();
+ arg5 = makearg();
+ arg6 = makearg();
+ arg7 = makearg();
+
+#if 0
+ fprintf(stderr, "%2d : syscall(%3d, %lx, %lx, %lx, %lx, %lx, %lx, %lx)\n",
+ i, SYS_open, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
+ usleep(100000);
+#endif
+ alarm(1);
+ syscall(SYS_open, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
+ num = 0;
+ }
+
+ return (0);
+}
+
+int
+main(void)
+{
+ struct passwd *pw;
+ struct rlimit limit;
+ pthread_t cp[50];
+ time_t start;
+ int e, j;
+
+ if ((pw = getpwnam("nobody")) == NULL)
+ err(1, "no such user: nobody");
+
+ if (setgroups(1, &pw->pw_gid) ||
+ setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
+ seteuid(pw->pw_uid) || setuid(pw->pw_uid))
+ err(1, "Can't drop privileges to \"nobody\"");
+ endpwent();
+
+ limit.rlim_cur = limit.rlim_max = 1000;
+ if (setrlimit(RLIMIT_NPTS, &limit) < 0)
+ err(1, "setrlimit");
+
+ signal(SIGALRM, hand);
+ signal(SIGILL, hand);
+ signal(SIGFPE, hand);
+ signal(SIGSEGV, hand);
+ signal(SIGBUS, hand);
+ signal(SIGURG, hand);
+ signal(SIGSYS, hand);
+ signal(SIGTRAP, hand);
+
+ start = time(NULL);
+ while ((time(NULL) - start) < 120) {
+ if (fork() == 0) {
+ for (j = 0; j < 1; j++)
+ if ((e = pthread_create(&cp[j], NULL, calls, NULL)) != 0)
+ errc(1, e,"pthread_create");
+
+ for (j = 0; j < 1; j++)
+ pthread_join(cp[j], NULL);
+ _exit(0);
+ }
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/fifo3.sh b/tools/test/stress2/misc/fifo3.sh
new file mode 100755
index 000000000000..04fca483eabf
--- /dev/null
+++ b/tools/test/stress2/misc/fifo3.sh
@@ -0,0 +1,209 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Demonstrate that fts_read(3) will open a fifo for read.
+# Not seen on a pristine FreeBSD.
+
+# $ while ./fifo.sh; do date; done
+# Wed Oct 1 14:07:41 CEST 2014
+# Wed Oct 1 14:09:58 CEST 2014
+# FAIL
+# $ ps -l19547
+# UID PID PPID CPU PRI NI VSZ RSS MWCHAN STAT TT TIME COMMAND
+# 0 19547 19544 0 25 0 12176 3996 fifoor I 0 0:08.19 /tmp/fifo
+# $ gdb /tmp/fifo 19547
+# GNU gdb 6.1.1 [FreeBSD]
+# Copyright 2004 Free Software Foundation, Inc.
+# GDB is free software, covered by the GNU General Public License, and you are
+# welcome to change it and/or distribute copies of it under certain conditions.
+# Type "show copying" to see the conditions.
+# There is absolutely no warranty for GDB. Type "show warranty" for details.
+# This GDB was configured as "amd64-marcel-freebsd"...
+# Attaching to program: /tmp/fifo, process 19547
+# Reading symbols from /lib/libc.so.7...done.
+# Loaded symbols for /lib/libc.so.7
+# Reading symbols from /libexec/ld-elf.so.1...done.
+# Loaded symbols for /libexec/ld-elf.so.1
+# 0x00000008008a9ab8 in enc_openat () from /lib/libc.so.7
+# (gdb) bt
+# #0 0x00000008008a9ab8 in enc_openat () from /lib/libc.so.7
+# #1 0x00000008008a581b in fts_read () from /lib/libc.so.7
+# #2 0x00000008008a4f24 in fts_read () from /lib/libc.so.7
+# #3 0x0000000000400ee9 in test () at /tmp/fifo.c:86
+# #4 0x0000000000400fd8 in main () at /tmp/fifo.c:108
+# (gdb) f 3
+# #3 0x0000000000400ee9 in test () at /tmp/fifo.c:86
+# 86 while ((p = fts_read(fts)) != NULL) {
+# Current language: auto; currently minimal
+# (gdb)
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+cat > /tmp/fifo3.c <<EOF
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fts.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define LOOPS 50
+#define PARALLEL 4
+
+void
+tmkfifo(void)
+{
+ pid_t pid;
+ int i, j;
+ char name[80];
+
+ setproctitle(__func__);
+ pid = getpid();
+ for (j = 0; j < LOOPS; j++) {
+ for (i = 0; i < 1000; i++) {
+ snprintf(name, sizeof(name), "fifo.%d.%06d", pid, i);
+ if (mkfifo(name, 0644) == -1)
+ err(1, "mkfifo(%s)", name);
+ }
+ for (i = 0; i < 1000; i++) {
+ snprintf(name, sizeof(name), "fifo.%d.%06d", pid, i);
+ if (unlink(name) == -1)
+ err(1, "unlink(%s)", name);
+ }
+ }
+ _exit(0);
+}
+
+void
+tmkdir(void)
+{
+ pid_t pid;
+ int i, j;
+ char name[80];
+
+ setproctitle(__func__);
+ pid = getpid();
+ for (j = 0; j < LOOPS; j++) {
+ for (i = 0; i < 1000; i++) {
+ snprintf(name, sizeof(name), "dir.%d.%06d", pid, i);
+ if (mkdir(name, 0644) == -1)
+ err(1, "mkdir(%s)", name);
+ }
+ for (i = 0; i < 1000; i++) {
+ snprintf(name, sizeof(name), "dir.%d.%06d", pid, i);
+ if (rmdir(name) == -1)
+ err(1, "unlink(%s)", name);
+ }
+ }
+ _exit(0);
+}
+
+void
+test(void)
+{
+ FTS *fts;
+ FTSENT *p;
+ int ftsoptions, i;
+ char *args[2];
+
+ if (fork() == 0)
+ tmkfifo();
+ if (fork() == 0)
+ tmkdir();
+
+ ftsoptions = FTS_PHYSICAL;
+ args[0] = ".";
+ args[1] = 0;
+
+ for (i = 0; i < LOOPS; i++) {
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ while ((p = fts_read(fts)) != NULL) {
+#if defined(TEST)
+ fprintf(stdout, "%s\n", p->fts_path);
+#endif
+ }
+
+ if (errno != 0 && errno != ENOENT)
+ err(1, "fts_read");
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+ }
+ wait(NULL);
+ wait(NULL);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i;
+
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ test();
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+
+ return (0);
+}
+EOF
+mycc -o /tmp/fifo3 -Wall -Wextra -O0 -g /tmp/fifo3.c || exit 1
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+(cd $mntpoint; /tmp/fifo3 ) &
+
+while pgrep -q fifo3; do
+ ps -lx | grep -v grep | grep -q fifoor &&
+ { echo FAIL; exit 1; }
+ sleep 2
+done
+
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm /tmp/fifo3 /tmp/fifo3.c
diff --git a/tools/test/stress2/misc/fifo4.sh b/tools/test/stress2/misc/fifo4.sh
new file mode 100755
index 000000000000..7f46f8537dc6
--- /dev/null
+++ b/tools/test/stress2/misc/fifo4.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# tmpfs(5) version of fifo2.sh
+# No problems seen on HEAD.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/fifo2.sh > fifo2.c
+rm -f /tmp/fifo2
+mycc -o fifo2 -Wall -Wextra -O2 -g fifo2.c -lpthread || exit 1
+rm -f fifo2.c
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+
+mount -o size=1g -t tmpfs tmpfs $mntpoint
+chmod 777 $mntpoint
+for i in `jot 5`; do
+ mkfifo $mntpoint/f$i
+ chmod 777 $mntpoint/f$i
+done
+
+daemon sh -c "(cd $odir/../testcases/swap; ./swap -t 10m -i 20 -l 100)" > \
+ /dev/null
+sleeptime=12
+st=`date '+%s'`
+while [ $((`date '+%s'` - st)) -lt $((10 * sleeptime)) ]; do
+ (cd $mntpoint; /tmp/fifo2) &
+ while ! pgrep -q fifo2; do :; done
+ start=`date '+%s'`
+ while [ $((`date '+%s'` - start)) -lt $sleeptime ]; do
+ pgrep -q fifo2 || break
+ sleep .5
+ done
+ while pgrep -q fifo2; do pkill -9 fifo2; done
+ wait
+done
+pkill -9 swap fifo2
+while pgrep -q "swap|fifo2"; do pkill -9 swap fifo2; done
+
+for i in `jot 10`; do
+ mount | grep -q "on $mntpoint " && \
+ umount $mntpoint > /dev/null 2>&1 && break
+ sleep 10
+done
+s=0
+mount | grep -q "on $mntpoint " &&
+ { echo "umount $mntpoint failed"; s=1; }
+rm -f /tmp/fifo2
+exit $s
diff --git a/tools/test/stress2/misc/flock.sh b/tools/test/stress2/misc/flock.sh
new file mode 100755
index 000000000000..451fec4b518a
--- /dev/null
+++ b/tools/test/stress2/misc/flock.sh
@@ -0,0 +1,191 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# flock(2) read (shared) lock test.
+
+# FAIL: Unfair scheduling?
+# share[1] = 359171
+# share[2] = 394437
+# share[3] = 359488
+# share[4] = 394429
+# share[5] = 359441
+# share[6] = 394281
+# share[7] = 359314
+# share[8] = 394615
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/flock.c
+mycc -o flock -Wall -Wextra -O0 -g flock.c || exit 1
+rm -f flock.c
+cd $odir
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+(cd $mntpoint; /tmp/flock)
+e=$?
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -rf /tmp/flock
+exit $e
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+volatile u_int *share;
+int fd;
+
+#define RENDEZVOUS 0
+
+#define CHILDREN 8
+#define LOOPS 8000
+#define PARALLEL 1
+#define RUNTIME (1 * 60)
+
+void
+chld(int id)
+{
+ while (share[RENDEZVOUS] == 0)
+ ;
+
+ while (share[RENDEZVOUS] == 1) {
+ if (flock(fd, LOCK_SH) == -1)
+ err(1, "fcntl @ %d", __LINE__);
+ atomic_add_int(&share[id + 1], 1);
+ if (flock(fd, LOCK_UN) == -1)
+ err(1, "fcntl @ %d", __LINE__);
+ usleep(100);
+ }
+
+ _exit(0);
+}
+
+void
+test(void)
+{
+ int i;
+ char file[80];
+
+ snprintf(file, sizeof(file), "file.%05d", getpid());
+ if ((fd = open(file, O_RDWR | O_CREAT, 0640)) == -1)
+ err(1, "open(%s)", file);
+ if (flock(fd, LOCK_EX) == -1)
+ err(1, "fcntl @ %d", __LINE__);
+
+ for (i = 0; i < CHILDREN; i++) {
+ if (fork() == 0)
+ chld(i);
+ }
+
+ usleep(200);
+ atomic_add_int(&share[RENDEZVOUS], 1); /* start chld */
+ for (i = 0; i < LOOPS; i++) {
+ if (flock(fd, LOCK_UN) == -1)
+ err(1, "fcntl @ %d", __LINE__);
+ if (flock(fd, LOCK_EX) == -1)
+ err(1, "fcntl @ %d", __LINE__);
+ }
+ atomic_add_int(&share[RENDEZVOUS], 1); /* stop chld */
+
+ for (i = 0; i < CHILDREN; i++)
+ wait(NULL);
+
+ close(fd);
+ unlink(file);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ size_t len;
+ time_t start;
+ int i, n, pct;
+
+ len = getpagesize();
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON |
+ MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ share[RENDEZVOUS] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ test();
+ }
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+ }
+ n = 0;
+ for (i = 0; i < CHILDREN; i++)
+ n += share[i + 1];
+ n /= CHILDREN;
+ for (i = 0; i < CHILDREN; i++) {
+ pct = abs((int)share[i + 1] - n) * 100 / n;
+ if (pct > 1) {
+ fprintf(stderr, "Unfair scheduling?\n");
+ for (i = 0; i < CHILDREN; i++) {
+ pct = abs((int)share[i + 1] - n) * 100 / n;
+ fprintf(stderr, "share[%d] = %d\n",
+ i+1, share[i+1]);
+ }
+ break;
+ }
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/flock_open_close.sh b/tools/test/stress2/misc/flock_open_close.sh
new file mode 100755
index 000000000000..173cdcbf2e75
--- /dev/null
+++ b/tools/test/stress2/misc/flock_open_close.sh
@@ -0,0 +1,174 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm
+# 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 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.
+#
+
+# Demonstrate that close() of an flock'd file is not atomic.
+# Fails with "flock_open_close: execv(/mnt/test): Text file busy"
+
+# Test scenario by: jhb
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > flock_open_close.c
+rm -f /tmp/flock_open_close
+mycc -o flock_open_close -Wall -Wextra -O2 -g flock_open_close.c -lpthread || exit 1
+rm -f flock_open_close.c
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+cp /bin/test $mntpoint
+chown $testuser $mntpoint/test
+chmod +w $mntpoint/test
+
+su $testuser -c "/tmp/flock_open_close $mntpoint/test" &
+pid=$!
+while kill -0 $! 2>/dev/null; do
+ mksnap_ffs $mntpoint $mntpoint/.snap/snap
+ sleep 2
+ rm -f $mntpoint/.snap/snap
+ sleep 1
+done
+wait $pid
+s=$?
+
+for i in `jot 10`; do
+ mount | grep -q md${mdstart}$part && \
+ umount $mntpoint && mdconfig -d -u $mdstart && break
+ sleep 2
+done
+if mount | grep -q md${mdstart}$part; then
+ fstat $mntpoint
+ echo "umount $mntpoint failed"
+ exit 1
+fi
+rm -f /tmp/flock_open_close
+exit $s
+EOF
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static void
+usage(void)
+{
+ fprintf(stderr, "Usage: flock_close_race <binary> [args]\n");
+ exit(1);
+}
+
+static void
+child(const char *binary)
+{
+ int fd;
+
+ /* Exit as soon as our parent exits. */
+ while (getppid() != 1) {
+ fd = open(binary, O_RDWR | O_EXLOCK);
+ if (fd < 0) {
+ /*
+ * This may get ETXTBSY since exit() will
+ * close its open fd's (thus releasing the
+ * lock), before it releases the vmspace (and
+ * mapping of the binary).
+ */
+ if (errno == ETXTBSY)
+ continue;
+ err(2, "can't open %s", binary);
+ }
+ close(fd);
+ }
+ exit(0);
+}
+
+static void
+exec_child(char **av)
+{
+ int fd;
+
+ fd = open(av[0], O_RDONLY | O_SHLOCK);
+ execv(av[0], av);
+ /* "flock_open_close: execv(/mnt/test): Text file busy" seen */
+ err(127, "execv(%s)", av[0]);
+}
+
+int
+main(int ac, char **av)
+{
+ struct stat sb;
+ pid_t pid;
+ int e, i, status;
+
+ if (ac < 2)
+ usage();
+ if (stat(av[1], &sb) != 0)
+ err(1, "stat(%s)", av[1]);
+ if (!S_ISREG(sb.st_mode))
+ errx(1, "%s not an executable", av[1]);
+
+ pid = fork();
+ if (pid < 0)
+ err(1, "fork");
+ if (pid == 0)
+ child(av[1]);
+ e = 0;
+ for (i = 0; i < 200000; i++) {
+ pid = fork();
+ if (pid < 0)
+ err(1, "vfork");
+ if (pid == 0)
+ exec_child(av + 1);
+ wait(&status);
+ if (WIFEXITED(status) && WEXITSTATUS(status) == 127) {
+ fprintf(stderr, "FAIL\n");
+ e = 1;
+ break;
+ }
+ if (WIFEXITED(status) && WEXITSTATUS(status) != 1) {
+ /* /bin/test returns 1 */
+ e = 1;
+ break;
+ }
+ }
+ return (e);
+}
diff --git a/tools/test/stress2/misc/force.sh b/tools/test/stress2/misc/force.sh
new file mode 100755
index 000000000000..4c5cd6c255f6
--- /dev/null
+++ b/tools/test/stress2/misc/force.sh
@@ -0,0 +1,70 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+# "mdconfig -o force" test scenario.
+# Run with marcus.cfg on a 1g swap backed MD with UFS SU fs.
+
+# Page fault seen in WiP kernel code:
+# https://people.freebsd.org/~pho/stress/log/kirk113.txt
+
+# Deadlock seen:
+# https://people.freebsd.org/~pho/stress/log/chs002.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+flags=$newfs_flags
+# Disable SUJ tests for now. Known deadlock issue.
+#[ `jot -r 1 0 1` -eq 1 ] && flags="-j"
+echo "newfs $flags md$mdstart"
+newfs $flags md$mdstart > /dev/null 2>&1
+mount /dev/md$mdstart $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=3m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1 &
+
+sleep `jot -r 1 10 40`
+while mdconfig -l | grep -q md$mdstart; do
+ mdconfig -d -u $mdstart -o force || sleep 1
+done
+sleep 1
+../tools/killall.sh
+wait
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ [ $((n += 1)) -gt 300 ] && { echo FAIL; exit 1; }
+done
+exit 0
diff --git a/tools/test/stress2/misc/force2.sh b/tools/test/stress2/misc/force2.sh
new file mode 100755
index 000000000000..35e448afbbba
--- /dev/null
+++ b/tools/test/stress2/misc/force2.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+# "mdconfig -o force" test scenario.
+# Run with marcus.cfg on a 1g swap backed MD with UFS SU fs.
+
+# "panic: softdep_load_inodeblock: nlink we read isn't what we wrote" seen in
+# WiP kernel code: https://people.freebsd.org/~pho/stress/log/kirk130.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+dd if=/dev/zero of=$diskimage bs=1m count=1k status=none || exit 1
+mdconfig -a -t vnode -f $diskimage -u $mdstart
+flags=$newfs_flags
+[ `jot -r 1 0 1` -eq 1 ] && flags="-j"
+echo "newfs $flags md$mdstart"
+newfs $flags md$mdstart > /dev/null 2>&1
+mount /dev/md$mdstart $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=3m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1 &
+
+sleep `jot -r 1 10 40`
+while mdconfig -l | grep md$mdstart; do
+ mdconfig -d -u $mdstart -o force || sleep 1
+done
+sleep 1
+../tools/killall.sh
+wait
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ [ $((n += 1)) -gt 300 ] && { echo FAIL; exit 1; }
+done
+
+mdconfig -a -t vnode -f $diskimage -u $mdstart
+fsck_ffs -Rfy /dev/md$mdstart > /dev/null; s=$?
+if [ $s -eq 0 ]; then
+ mount /dev/md$mdstart $mntpoint
+ ls -lR $mntpoint > /dev/null
+ umount $mntpoint
+fi
+mdconfig -d -u $mdstart
+
+rm -f $diskimage
+exit $s
diff --git a/tools/test/stress2/misc/force3.sh b/tools/test/stress2/misc/force3.sh
new file mode 100755
index 000000000000..4bfb0011cbef
--- /dev/null
+++ b/tools/test/stress2/misc/force3.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "mdconfig -o force" test scenario.
+# Copy of force.sh with an added "umount -f"
+# Spin seen in umount() with WiP kernel code.
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+flags=$newfs_flags
+[ `jot -r 1 0 1` -eq 1 ] && flags="-j"
+echo "newfs $flags md$mdstart"
+newfs $flags md$mdstart > /dev/null 2>&1
+mount /dev/md$mdstart $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=3m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1 &
+
+sleep `jot -r 1 10 40`
+while mdconfig -l | grep -q md$mdstart; do
+ mdconfig -d -u $mdstart -o force || sleep 1
+done
+sleep .`jot -r 1 1 9`
+../tools/killall.sh
+wait
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ [ $((n += 1)) -gt 300 ] && { echo FAIL; exit 1; }
+done
+exit 0
diff --git a/tools/test/stress2/misc/force4.sh b/tools/test/stress2/misc/force4.sh
new file mode 100755
index 000000000000..b1b05dc54efa
--- /dev/null
+++ b/tools/test/stress2/misc/force4.sh
@@ -0,0 +1,91 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "mdconfig -o force" test scenario with nullfs.
+# Looping in kernel with WiP kernel code seen:
+# https://people.freebsd.org/~pho/stress/log/kirk125.txt
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+flags=$newfs_flags
+[ `jot -r 1 0 1` -eq 1 ] && flags="-j"
+echo "newfs $flags md$mdstart"
+newfs $flags md$mdstart > /dev/null 2>&1
+mount /dev/md$mdstart $mntpoint
+chmod 777 $mntpoint
+
+mp2=${mntpoint}2
+mkdir -p $mp2
+mount -t nullfs $mntpoint $mp2 || exit 1
+
+export LOAD=80
+export MAXSWAPPCT=80
+export RUNDIR=$mp2/stressX
+export runRUNTIME=3m
+export rwLOAD=80
+export TESTPROGS='
+testcases/lockf2/lockf2
+testcases/symlink/symlink
+testcases/openat/openat
+testcases/socket/socket
+testcases/rw/rw
+testcases/mmap/mmap
+testcases/fts/fts
+testcases/link/link
+testcases/lockf/lockf
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/swap/swap
+testcases/mkfifo/mkfifo
+testcases/dirnprename/dirnprename
+testcases/dirrename/dirrename
+'
+
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > /dev/null 2>&1 &
+
+sleep `jot -r 1 20 60`
+while mdconfig -l | grep -q md$mdstart; do
+ mdconfig -d -u $mdstart -o force || sleep 1
+done
+sleep 1
+../tools/killall.sh
+wait
+umount $mp2
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ [ $((n += 1)) -gt 300 ] && { echo FAIL; exit 1; }
+done
+exit 0
diff --git a/tools/test/stress2/misc/force5.sh b/tools/test/stress2/misc/force5.sh
new file mode 100755
index 000000000000..c0aa6436fc1f
--- /dev/null
+++ b/tools/test/stress2/misc/force5.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+# "mdconfig -o force" test scenario.
+# Rename focus
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+flags=$newfs_flags
+[ `jot -r 1 0 1` -eq 1 ] && flags="-j"
+echo "newfs $flags md$mdstart"
+newfs $flags md$mdstart > /dev/null 2>&1
+mount /dev/md$mdstart $mntpoint
+chmod 777 $mntpoint
+
+export LOAD=80
+export MAXSWAPPCT=80
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=3m
+export rwLOAD=80
+export TESTPROGS='
+testcases/fts/fts
+testcases/rename/rename
+testcases/swap/swap
+testcases/dirnprename/dirnprename
+testcases/dirrename/dirrename
+'
+
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > /dev/null 2>&1 &
+
+sleep `jot -r 1 20 60`
+while mdconfig -l | grep -q md$mdstart; do
+ mdconfig -d -u $mdstart -o force || sleep 1
+done
+sleep 1
+../tools/killall.sh
+wait
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ [ $((n += 1)) -gt 300 ] && { echo FAIL; exit 1; }
+done
+exit 0
diff --git a/tools/test/stress2/misc/force6.sh b/tools/test/stress2/misc/force6.sh
new file mode 100755
index 000000000000..5c7d508241ec
--- /dev/null
+++ b/tools/test/stress2/misc/force6.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+# "mdconfig -o force" test scenario.
+# Verify file integrity after a fsync(1) followed by a forced umount
+# No problems seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+dd if=/dev/zero of=$diskimage bs=1m count=1k status=none || exit 1
+mdconfig -a -t vnode -f $diskimage -u $mdstart
+flags=$newfs_flags
+[ `jot -r 1 0 1` -eq 1 ] && flags="-j"
+echo "newfs $flags md$mdstart"
+newfs $flags md$mdstart > /dev/null 2>&1
+mount /dev/md$mdstart $mntpoint
+
+file=$mntpoint/file
+dd if=/dev/random of=$file bs=1k count=`jot -r 1 1 1024` status=none
+s1=`cat $mntpoint/file | md5`
+fsync $file
+
+while mdconfig -l | grep -q md$mdstart; do
+ mdconfig -d -u $mdstart -o force || sleep 1
+done
+
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ [ $((n += 1)) -gt 300 ] && { echo FAIL; exit 1; }
+done
+
+mdconfig -a -t vnode -f $diskimage -u $mdstart
+fsck_ffs -Rfy /dev/md$mdstart > /dev/null; s=$?
+if [ $s -eq 0 ]; then
+ mount /dev/md$mdstart $mntpoint
+ [ ! -f $file ] &&
+ { echo "Lost $file"; s=111; }
+ if [ $s -eq 0 ]; then
+ s2=`cat $mntpoint/file | md5`
+ [ "$s1" != "$s2" ] &&
+ { echo "Checksum error"; s=222; ls -l $file; }
+ fi
+ umount $mntpoint
+fi
+mdconfig -d -u $mdstart
+
+rm -f $diskimage
+exit $s
diff --git a/tools/test/stress2/misc/fork.sh b/tools/test/stress2/misc/fork.sh
new file mode 100755
index 000000000000..628ffbd4ce93
--- /dev/null
+++ b/tools/test/stress2/misc/fork.sh
@@ -0,0 +1,129 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# The test program calls fork(2) from a multi-threaded process.
+# Test program stuck in uwrlck seen.
+# Fixed in r266609.
+
+# Note that program erroneously calls exit(3) and not _exit(2).
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > fork.c
+mycc -o fork -Wall -Wextra -O2 -g fork.c -lpthread || exit 1
+
+for i in `jot 100`; do
+ /tmp/fork &
+done
+while ! pgrep -q fork; do
+ sleep .2
+done
+for i in `jot 30`; do
+ pgrep -q fork || break
+ sleep 1
+done
+if pgrep -q fork; then
+ echo FAIL
+ exit 1
+fi
+wait
+
+rm -f /tmp/fork /tmp/fork.c
+exit 0
+EOF
+
+/*
+ * Written by Love Hörnquist Åstrand <lha@NetBSD.org>, March 2003.
+ * Public domain.
+ */
+
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static pid_t parent;
+static int thread_survived = 0;
+
+static void *
+print_pid(void *arg __unused)
+{
+ sleep(3);
+
+ thread_survived = 1;
+ if (parent != getpid()) {
+ exit(1);
+ }
+ return NULL;
+}
+
+int
+main(void)
+{
+ int r;
+ pthread_t p;
+ pid_t fork_pid;
+
+ parent = getpid();
+
+ r = pthread_create(&p, NULL, print_pid, NULL);
+ if (r != 0)
+ errx(1, "r = %d", r);
+
+ fork_pid = fork();
+ if (fork_pid == -1)
+ err(1, "fork");
+
+ if (fork_pid) {
+ int status;
+
+ r = pthread_join(p, NULL);
+ if (r != 0)
+ errx(1, "r = %d", r);
+ if (thread_survived == 0)
+ errx(1, "thread did not survive in parent");
+
+ waitpid(fork_pid, &status, 0);
+ if (WIFEXITED(status) != 1)
+ printf("WIFEXITED(status) = %d\n", WIFEXITED(status));
+ if (WEXITSTATUS(status) != 0)
+ printf("WEXITSTATUS(status) = %d\n", WEXITSTATUS(status));
+ } else {
+ sleep(5);
+ exit(thread_survived ? 1 : 0);
+ }
+}
diff --git a/tools/test/stress2/misc/forkbomb.sh b/tools/test/stress2/misc/forkbomb.sh
new file mode 100755
index 000000000000..9638fe796fee
--- /dev/null
+++ b/tools/test/stress2/misc/forkbomb.sh
@@ -0,0 +1,148 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Fork bomb memory leak test scenario.
+# https://en.wikipedia.org/wiki/Fork_bomb
+
+# OO memory seen:
+# https://people.freebsd.org/~pho/stress/log/forkbomb.txt
+# Fixed by r289026.
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/forkbomb.c
+mycc -o forkbomb -Wall -Wextra -O0 -g forkbomb.c || exit 1
+rm -f forkbomb.c
+cd $odir
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 512m -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+su $testuser -c /tmp/forkbomb
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -rf /tmp/forkbomb
+exit 0
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+volatile u_int *share;
+
+#define R1 1 /* sync start */
+#define R2 2 /* forks */
+#define R3 3 /* exits */
+#define R4 4 /* fork failed */
+
+//#define DEBUG
+#define MXFAIL 100
+#define MAXPROC 40000 /* Arbitrary cap */
+#define PARALLEL 200
+
+void
+test(void)
+{
+ int r;
+
+ alarm(1200);
+ atomic_add_int(&share[R1], 1);
+ while (share[R1] != PARALLEL)
+ ;
+ atomic_add_int(&share[R2], 1);
+
+ for (;;) {
+ if (share[R2] >= MAXPROC || share[R4] > MXFAIL)
+ break;
+ atomic_add_int(&share[R2], 1);
+ if ((r = fork()) == -1) {
+ atomic_add_int(&share[R4], 1);
+ atomic_add_int(&share[R2], -1);
+ break;
+ }
+ }
+
+ atomic_add_int(&share[R3], 1);
+ _exit(0);
+}
+
+int
+main(void)
+{
+ struct sigaction sa;
+ size_t len;
+ int i;
+
+ alarm(1200);
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ sa.sa_handler = SIG_IGN;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction(SIGCHLD, &sa, 0) == -1)
+ err(1, "sigaction");
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ test();
+ }
+
+ while (share[R2] == 0 || share[R3] < share[R2])
+ sleep(1);
+
+#if defined(DEBUG)
+ fprintf(stderr, "MAXPROC: %d. forks: %u / exits: %u / fails: %u\n",
+ MAXPROC, share[R2], share[R3], share[R4]);
+#endif
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/fpclone.sh b/tools/test/stress2/misc/fpclone.sh
new file mode 100755
index 000000000000..3b75c5ec8d36
--- /dev/null
+++ b/tools/test/stress2/misc/fpclone.sh
@@ -0,0 +1,192 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Test scenario by kib@freebsd.org
+
+# Test of patch for Giant trick in cdevsw
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -d /usr/src/sys ] || exit 0
+builddir=`sysctl kern.version | grep @ | sed 's/.*://'`
+[ -d "$builddir" ] && export KERNBUILDDIR=$builddir || exit 0
+export SYSDIR=`echo $builddir | sed 's#/sys.*#/sys#'`
+kldstat -v | grep -q pty || { kldload pty || exit 0; }
+
+. ../default.cfg
+
+odir=`pwd`
+dir=$RUNDIR/fpclone
+[ ! -d $dir ] && mkdir -p $dir
+
+cd $dir
+cat > Makefile <<EOF
+KMOD= fpclone
+SRCS= fpclone.c
+
+.include <bsd.kmod.mk>
+EOF
+
+sed '1,/^EOF2/d' < $odir/$0 > fpclone.c
+make
+kldload $dir/fpclone.ko
+
+cd $odir
+for i in `jot 10`; do
+ dd if=/dev/fpclone bs=1m count=10 > /dev/null 2>&1 &
+done
+
+export runRUNTIME=2m
+cd ..; ./run.sh pty.cfg
+
+for i in `jot 10`; do
+ wait
+done
+kldstat
+dd if=/dev/fpclone bs=1m count=1k > /dev/null 2>&1 &
+kldunload $dir/fpclone.ko
+rm -rf $dir
+exit
+
+EOF2
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/module.h>
+#include <sys/mutex.h>
+#include <sys/conf.h>
+#include <sys/uio.h>
+#include <sys/malloc.h>
+
+static d_open_t fpclone_open;
+static d_close_t fpclone_close;
+static d_read_t fpclone_read;
+
+static struct cdevsw fpclone_cdevsw = {
+ .d_open = fpclone_open,
+ .d_close = fpclone_close,
+ .d_read = fpclone_read,
+ .d_name = "fpclone",
+ .d_version = D_VERSION,
+ .d_flags = D_TRACKCLOSE
+};
+
+MALLOC_DEFINE(M_FPCLONESC, "fpclone memory", "fpclone memory");
+
+struct fpclone_sc
+{
+ int pos;
+};
+
+static struct cdev *fpclone_dev;
+static struct mtx me;
+
+static void
+fpclone_cdevpriv_dtr(void *data)
+{
+ free(data, M_FPCLONESC);
+}
+
+static int
+fpclone_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
+{
+ struct fpclone_sc *sc;
+ int error;
+
+ sc = malloc(sizeof(struct fpclone_sc), M_FPCLONESC,
+ M_WAITOK | M_ZERO);
+ error = devfs_set_cdevpriv(sc, fpclone_cdevpriv_dtr);
+ if (error)
+ fpclone_cdevpriv_dtr(sc);
+ return (error);
+}
+
+static int
+fpclone_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
+{
+
+ devfs_clear_cdevpriv();
+ return (0);
+}
+
+static char rdata[] = "fpclone sample data string\n";
+
+static int
+fpclone_read(struct cdev *dev, struct uio *uio, int ioflag)
+{
+ struct fpclone_sc *sc;
+ int rv, amnt, svpos, error;
+
+ error = devfs_get_cdevpriv((void **)&sc);
+ if (error)
+ return (error);
+
+ rv = 0;
+ while (uio->uio_resid > 0) {
+ svpos = sc->pos;
+ amnt = MIN(uio->uio_resid, sizeof(rdata) - svpos);
+ rv = uiomove(rdata + svpos, amnt, uio);
+ if (rv != 0)
+ break;
+ mtx_lock(&me);
+ sc->pos += amnt;
+ sc->pos %= sizeof(rdata);
+ mtx_unlock(&me);
+ }
+ return (rv);
+}
+
+static int
+fpclone_modevent(module_t mod, int what, void *arg)
+{
+ switch (what) {
+ case MOD_LOAD:
+ mtx_init(&me, "fp_ref", NULL, MTX_DEF);
+ fpclone_dev = make_dev(&fpclone_cdevsw, 0, 0, 0, 0666,
+ "fpclone");
+ return(0);
+
+ case MOD_UNLOAD:
+ destroy_dev(fpclone_dev);
+ mtx_destroy(&me);
+ return (0);
+
+ default:
+ break;
+ }
+
+ return (0);
+}
+
+moduledata_t fpclone_mdata = {
+ "fpclone",
+ fpclone_modevent,
+ NULL
+};
+
+DECLARE_MODULE(fpclone, fpclone_mdata, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
+MODULE_VERSION(fpclone, 1);
diff --git a/tools/test/stress2/misc/fpclone2.sh b/tools/test/stress2/misc/fpclone2.sh
new file mode 100755
index 000000000000..b03f5cced96f
--- /dev/null
+++ b/tools/test/stress2/misc/fpclone2.sh
@@ -0,0 +1,117 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Test scenario by kib@freebsd.org
+
+# Test of patch for Giant trick in cdevsw
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -d /usr/src/sys ] || exit 0
+builddir=`sysctl kern.version | grep @ | sed 's/.*://'`
+[ -d "$builddir" ] && export KERNBUILDDIR=$builddir || exit 0
+export SYSDIR=`echo $builddir | sed 's#/sys.*#/sys#'`
+
+. ../default.cfg
+
+odir=`pwd`
+dir=$RUNDIR/fpclone
+[ ! -d $dir ] && mkdir -p $dir
+
+cd $dir
+cat > Makefile <<EOF
+KMOD= fpclone
+SRCS= fpclone.c
+
+.include <bsd.kmod.mk>
+EOF
+
+sed '1,/^EOF2/d' < $odir/fpclone.sh > fpclone.c
+make
+kldload $dir/fpclone.ko
+
+sed '1,/^EOF2/d' < $odir/$0 > fpclone2.c
+mycc -o /tmp/fpclone2 -Wall fpclone2.c
+rm -f fpclone2.c
+
+cd $odir
+for i in `jot 10`; do
+ /tmp/fpclone2 &
+done
+
+for i in `jot 10`; do
+ wait
+done
+kldstat
+kldunload $dir/fpclone.ko
+rm -rf $dir /tmp/fpclone2
+exit
+
+EOF2
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <err.h>
+
+int
+main(int argc, char **argv)
+{
+ int fd;
+ int i;
+ char buf[80];
+
+ for (i = 0; i < 10000; i++) {
+ if ((fd = open("/dev/fpclone", O_RDONLY)) == -1)
+ err(1, "open(/dev/fpclone");
+ if (read(fd, buf, sizeof(buf)) <= 0)
+ err(1, "read");
+ if (dup2(fd, 10) == -1)
+ err(1, "dup");
+ if (dup2(fd, 11) == -1)
+ err(1, "dup");
+ if (dup2(fd, 12) == -1)
+ err(1, "dup");
+ if (close(fd) == -1)
+ err(1, "close(%d)", fd);
+ if (close(10) == -1)
+ err(1, "close(%d)", 10);
+ if (close(11) == -1)
+ err(1, "close(%d)", 11);
+ if (close(12) == -1)
+ err(1, "close(%d)", 12);
+ }
+ if ((fd = open("/dev/fpclone", O_WRONLY)) == -1)
+ err(1, "open(/dev/fpclone");
+ if (write(fd, "xxx", 3) == -1 && errno != ENODEV)
+ err(1, "write");
+ if (close(fd) == -1)
+ err(1, "close(%d)", fd);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/fpu.sh b/tools/test/stress2/misc/fpu.sh
new file mode 100755
index 000000000000..f4bc53d2f67f
--- /dev/null
+++ b/tools/test/stress2/misc/fpu.sh
@@ -0,0 +1,103 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# Regression test for FPU changes in r208833
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > fpu.c
+mycc -o fpu -Wall -O2 fpu.c
+rm -f fpu.c
+[ -d $RUNDIR ] || mkdir -p $RUNDIR
+cd $RUNDIR
+
+r=`/tmp/fpu`
+[ "$r" = "-0.000000017, 0.000000000, 0.000000000" ] || echo $r
+
+cd $here
+rm -f /tmp/fpu
+
+exit 0
+EOF
+#include <sys/types.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+void
+handler(int i)
+{
+}
+
+void
+test()
+{
+ float val = 0;
+ double lval = 0;
+ long double llval = 0;
+ int i, j;
+
+ for (i = 0; i < 100000; i++) {
+ for (j = 0; j < 100000; j++) {
+ val = val + 0.00001;
+ lval = lval + 0.00001;
+ llval = llval + 0.00001;
+ }
+ for (j = 0; j < 100000; j++) {
+ val = val - 0.00001;
+ lval = lval - 0.00001;
+ llval = llval - 0.00001;
+ }
+ }
+ printf("%.9f, %.9f, %.9Lf\n", val, lval, llval);
+ exit(0);
+
+}
+
+int
+main()
+{
+ pid_t pid;
+ int i;
+
+ signal(SIGHUP, handler);
+
+ if ((pid = fork()) == 0)
+ test();
+
+ for (i = 0; i < 10000; i++)
+ kill(pid, SIGHUP);
+
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/fragments.sh b/tools/test/stress2/misc/fragments.sh
new file mode 100755
index 000000000000..ea1d095081c6
--- /dev/null
+++ b/tools/test/stress2/misc/fragments.sh
@@ -0,0 +1,275 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# Scenario that causes "panic: brelse: free buffer onto another queue???"
+# Idea for scenario by kib@. Fixed in r203818
+
+# When UFS partition is full, then some high load causes
+# panic: brelse: free buffer onto another queue???
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > fragments.c
+rm -f /tmp/fragments
+mycc -o fragments -Wall -Wextra -O2 -g fragments.c
+rm -f fragments.c
+cd $here
+
+mount | grep "$mntpoint" | grep -q md$mdstart && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags -m 0 md${mdstart}$part > /dev/null 2>&1
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+cd $mntpoint
+su $testuser -c "/tmp/fragments"
+cd $here
+
+umount $mntpoint
+mount | grep "$mntpoint" | grep -q md$mdstart && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+rm -f /tmp/fragments
+exit
+EOF
+#include <sys/param.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define LOOPS 600
+#define PARALLEL 8
+
+static pid_t pid;
+static char *buf;
+
+static volatile sig_atomic_t stop;
+
+void
+handler(int i __unused) {
+ stop = 1;
+}
+
+void
+cleanup(int n)
+{
+ int i, j, start;
+ int nb = 0;
+ char file[128];
+ struct statfs sbuf;
+ struct stat sb;
+
+ if (n == -1) {
+ for (i = 0; i < LOOPS; i++) {
+ sprintf(file,"t%05d", i);
+ unlink(file);
+ }
+ return;
+ }
+
+ start = arc4random() % n;
+ for (i = 0; i < LOOPS; i++) {
+ j = (start + i) % LOOPS;
+ sprintf(file,"t%05d", j);
+ if (stat(file, &sb) != 0)
+ continue;
+
+ if (sb.st_size == 0) {
+ unlink(file);
+ continue;
+ }
+ if (truncate(file, 0) == 0) {
+ nb++;
+ continue;
+ }
+ if (nb > 10)
+ break;
+ }
+
+ for (i = 0; i < 10; i++) {
+ if (statfs(".", &sbuf) < 0)
+ err(1, "statfs(%s)", ".");
+
+ if (sbuf.f_bfree > 8)
+ return;
+ }
+
+ for (i = 0; i < LOOPS; i++) {
+ j = (start + i) % LOOPS;
+ sprintf(file,"t%05d", j);
+ if (unlink(file) == 0) {
+ return;
+ }
+ }
+}
+
+void
+fragments(void)
+{
+ int i, len;
+ char file[128];
+ int fd;
+
+ for (i = 0;; i++) {
+ sprintf(file,"d%d/f%05d.%05d", i/1000, pid, i);
+
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0) {
+ if (errno != ENOSPC)
+ warn("open(%s)", file);
+ break;
+ }
+
+ len = 2 * 1024;
+ if (write(fd, buf, len) != len) {
+ }
+
+ close(fd);
+ }
+}
+
+void
+blocks(void)
+{
+ int i, len;
+ char file[128];
+ int fd;
+
+ for (i = 0;; i++) {
+ sprintf(file,"d%d/b%05d.%05d", i/1000, pid, i);
+
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0) {
+ if (errno != ENOSPC)
+ warn("open(%s)", file);
+ break;
+ }
+
+ len = 16 * 1024;
+ if (write(fd, buf, len) != len) {
+ }
+
+ close(fd);
+ }
+}
+
+void
+setup(void)
+{
+ int i;
+ char file[128];
+
+ for (i = 0; i < 300; i++) {
+ sprintf(file,"d%d", i);
+ if (mkdir(file, 0700) == -1)
+ warn("mkdir(%s)", file);
+ }
+
+ blocks();
+ fragments();
+
+ for (i = 0;i < 8; i++) {
+ sprintf(file,"d%d/b%05d.%05d", i/1000, pid, i);
+ unlink(file);
+ }
+ for (i = 0;i < 1; i++) {
+ sprintf(file,"d%d/f%05d.%05d", i/1000, pid, i);
+ unlink(file);
+ }
+
+}
+
+int
+test(void)
+{
+ int i, len, n;
+ char file[128];
+ int fd;
+
+ for (i = 0; i < LOOPS; i++) {
+ sprintf(file,"t%05d", i);
+
+ if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0) {
+ continue;
+ }
+// n = arc4random() % (12 + 1);
+ n = 0;
+ len = (arc4random() % (16 * 1024) + 1) + n * 16;
+ while (len > 0) {
+ if (write(fd, buf, len) == len)
+ break;
+ len = len / 2;
+ usleep(1000);
+ }
+ close(fd);
+ if (len == 0) {
+ cleanup(i);
+ }
+ }
+
+ exit(0);
+
+ return (0);
+}
+
+int
+main()
+{
+ int i, j, status;
+
+ pid = getpid();
+ if ((buf = malloc(12 * 16 * 1024)) == NULL)
+ err(1, "malloc()");
+
+ setup();
+ signal(SIGALRM, handler);
+ alarm(30 * 60);
+ for (j = 0; j < 50 && stop == 0; j++) {
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ test();
+ }
+ for (i = 0; i < PARALLEL; i++)
+ wait(&status);
+ cleanup(-1);
+ }
+ return (0);
+}
diff --git a/tools/test/stress2/misc/freepages.sh b/tools/test/stress2/misc/freepages.sh
new file mode 100755
index 000000000000..4aaea7453b1c
--- /dev/null
+++ b/tools/test/stress2/misc/freepages.sh
@@ -0,0 +1,130 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# Free page shortage test scenario
+# No problems seen
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/freepages.c
+mycc -o freepages -Wall -Wextra -O0 -g freepages.c || exit 1
+rm -f freepages.c
+cd $odir
+
+$dir/freepages `sysctl -n hw.usermem`
+s=$?
+[ -f freepages.core -a $s -eq 0 ] &&
+ { ls -l freepages.core; mv freepages.core /tmp; s=1; }
+
+rm -rf $dir/freepages
+exit $s
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#define PARALLEL 6
+#define RUNTIME (5 * 60)
+#define SYNC 0
+
+static void
+test(char *s)
+{
+ time_t start;
+ size_t i, len;
+ char *cp;
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ len = atol(s) / PARALLEL;
+ len = len / 10 * 8;
+ if ((cp = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap(%zd)", len);
+ for (i = 0; i < len; i += PAGE_SIZE)
+ cp[i] = 1;
+ if (munmap(cp, len) == -1)
+ err(1, "unmap");
+ }
+
+ _exit(0);
+}
+
+int
+main(int argc __unused, char *argv[])
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test(argv[1]);
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/fs.sh b/tools/test/stress2/misc/fs.sh
new file mode 100755
index 000000000000..dde3f39ed3ef
--- /dev/null
+++ b/tools/test/stress2/misc/fs.sh
@@ -0,0 +1,91 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008, 2011 Peter Holm <pho@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 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.
+#
+
+# Run a simple test on different FS variations, with and without disk full.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+flag=/tmp/fs.sh.flag
+
+ftest () { # option, disk full
+ local args="$@"
+ [ $2 -eq 1 ] && df=", disk full" || df=""
+ echo "`date '+%T'` newfs $1 md${mdstart}${part}$df"
+ newfs $1 md${mdstart}$part > /dev/null
+ mount /dev/md${mdstart}$part $mntpoint
+ chmod 777 $mntpoint
+
+ export RUNDIR=$mntpoint/stressX
+ export runRUNTIME=1m
+ disk=$(($2 + 1)) # 1 or 2
+ set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+ export KBLOCKS=$(($1 * disk))
+ export INODES=$(($2 * disk))
+
+ for i in `jot 2`; do
+ rm -rf /tmp/stressX.control $RUNDIR
+ su $testuser -c "(cd ..; ./run.sh disk.cfg)" > \
+ /dev/null 2>&1 &
+ sleep 60
+ ../tools/killall.sh
+ wait
+ done
+
+ for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ if [ $i -eq 6 ]; then
+ touch $flag
+ echo "Test \"$args\" FAIL"
+ fstat -mf $mntpoint
+ umount -f $mntpoint
+ fi
+ done
+ checkfs /dev/md${mdstart}$part || touch $flag
+}
+
+mount | grep "on $mntpoint " | grep -q md${mdstart}$part && umount $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 20m -u $mdstart
+bsdlabel -w md$mdstart auto
+
+ftest "-O 1" 0 # ufs1
+ftest "-O 1" 1 # ufs1, disk full
+ftest "-O 2" 0 # ufs2
+ftest "-O 2" 1 # ufs2, disk full
+ftest "-U" 0 # ufs2 + soft update
+ftest "-U" 1 # ufs2 + soft update, disk full
+ftest "-j" 0 # ufs2 + SU+J
+ftest "-j" 1 # ufs2 + SU+J, disk full
+
+mdconfig -d -u $mdstart
+[ -f $flag ] && s=1 || s=0
+rm -f $flag
+exit $s
diff --git a/tools/test/stress2/misc/fsck.sh b/tools/test/stress2/misc/fsck.sh
new file mode 100755
index 000000000000..a007e722988e
--- /dev/null
+++ b/tools/test/stress2/misc/fsck.sh
@@ -0,0 +1,149 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# fsck_ffs(8) test.
+# "UFS /dev/md11 (/mnt11) cylinder checksum failed" seen.
+# Fixed by r341510.
+
+# 'panic: invalid counts on struct mount' seen:
+# https://people.freebsd.org/~pho/stress/log/fsck-4.txt
+
+[ $DEBUG ] || exit 0 # Still WiP
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+cc -o /tmp/flip -Wall -Wextra -O2 ../tools/flip.c || exit 1
+
+echo 'int sync(void) { return (0); }' > /tmp/fsck_preload.c
+mycc -o /tmp/fsck_preload.so -shared -fpic /tmp/fsck_preload.c || exit 1
+cc -o /tmp/fsck_preload.so -shared -fpic /tmp/fsck_preload.c || exit 1
+rm /tmp/fsck_preload.c
+
+set -e
+u1=$mdstart
+u2=$((mdstart + 1))
+mp1=${mntpoint}$u1
+mp2=${mntpoint}$u2
+mkdir -p $mp1 $mp2
+log=$mp1/fsck.sh.log
+diskimage=$mp1/fsck.sh.diskimage
+backup=/tmp/fsck.sh.diskimage.`date +%Y%m%dT%H%M%S`.gz
+asbs=0
+cleans=0
+reruns=0
+waccess=0
+
+max=$((10 * 1024 * 1024))
+[ "$newfs_flags" = "-j" ] &&
+ max=$((20 * 1024 * 1024))
+
+set +e
+mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1
+[ -c /dev/md$u1 ] && mdconfig -d -u $u1
+mdconfig -a -t swap -s 1g -u $u1
+newfs $newfs_flags /dev/md$u1 > /dev/null
+mount /dev/md$u1 $mp1
+
+[ -c /dev/md$u2 ] && mdconfig -d -u $u2
+dd if=/dev/zero of=$diskimage bs=$max count=1 status=none
+mdconfig -a -t vnode -f $diskimage -u $u2
+backups=`newfs -N $newfs_flags md$u2 | grep -A1 "super-block backups" | \
+ tail -1 | sed 's/,//g'`
+newfs $newfs_flags md$u2 > /dev/null
+mount /dev/md$u2 $mp2
+[ -d /usr/include/sys ] && cp -r /usr/include/sys $mp2
+umount $mp2
+
+chk() {
+ local i
+
+ LD_PRELOAD=/tmp/fsck_preload.so \
+ fsck_ffs -fy $1 > $log 2>&1
+ r=$?
+ if grep -qE "Cannot find file system superblock|Superblock check-hash failed" $log; then
+ for b in $backups; do
+ echo "Using alternate SB $b"
+ asbs=$((asbs + 1))
+ LD_PRELOAD=/tmp/fsck_preload.so \
+ fsck_ffs -b $b -fy $1 > $log 2>&1
+ r=$?
+ grep -qE "Cannot find file system superblock|Superblock check-hash failed" $log ||
+ break
+ done
+ usedasb=1
+ else
+ usedasb=0
+ fi
+ LANG=C egrep -q "[A-Z][A-Z]" $log && clean=0
+ ! grep -Eq "IS CLEAN|MARKED CLEAN" $log; clean=$?
+ ! grep -q RERUN $log; rerun=$?
+ ! grep -q "NO WRITE ACCESS" $log; waccess=$?
+ [ $r -ne 0 -a $clean -eq 1 ] && echo "Exit code $r w/ clean == 1"
+
+}
+
+cd /tmp
+s=0
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 60 ]; do
+ gzip < $diskimage > $backup
+ fsync $backup; sync # ; sleep .2; sync; sleep .2; sync
+ mount /dev/md$u2 $mp2 || { s=101; break; }
+ touch $mp2/`jot -rc 8 a z | tr -d '\n'`
+ umount $mp2
+ /tmp/flip -n 4 $diskimage
+ for i in `jot 3`; do
+ chk /dev/md$u2
+ [ $clean -eq 1 ] && { cleans=$((cleans + 1)); break; }
+ [ $rerun -eq 1 ] && { reruns=$((reruns + 1)); continue; }
+ done
+ [ $r -ne 0 -a $clean -eq 1 ] &&
+ { echo "CLEAN && non zero exit code"; break; }
+ [ $clean -eq 1 ] && continue
+ [ $usedasb -eq 1 ] && { echo "Alt. SB failed"; s=103; }
+ [ $waccess -eq 1 ] && { echo "No write access"; s=555; }
+ break
+done
+[ $DEBUG ] &&
+ echo "$cleans cleans, $reruns reruns, $asbs alternate SBs." && cat $log
+if [ $clean -ne 1 ]; then
+ echo "FS still not clean. Last fsck_ffs exit code was $r."
+ cat $log
+ cp -v $log /tmp || rm $log
+ [ $s -eq 0 ] && s=104
+fi
+mdconfig -d -u $u2 || exit 1
+[ -f fsck_ffs.core ] && ls -l fsck_ffs.core
+
+umount $mp1
+mdconfig -d -u $u1
+rm -f /tmp/fsck_preload.so $backup /tmp/flip
+exit $s
diff --git a/tools/test/stress2/misc/fsck2.sh b/tools/test/stress2/misc/fsck2.sh
new file mode 100755
index 000000000000..313efec490d1
--- /dev/null
+++ b/tools/test/stress2/misc/fsck2.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Test fsck_ffs exit code
+
+unit=55
+[ -c /dev/md$unit ] && exit 0
+
+fsck_ffs /dev/md$unit > /dev/null 2>&1
+s=$?
+[ $s -eq 0 ] && { echo "Bad exit code $s. Expected non zero."; s=1; } || s=0
+exit $s
diff --git a/tools/test/stress2/misc/fsck3.sh b/tools/test/stress2/misc/fsck3.sh
new file mode 100755
index 000000000000..f9bd29017e35
--- /dev/null
+++ b/tools/test/stress2/misc/fsck3.sh
@@ -0,0 +1,88 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# fsck_ffs test.
+# Test scenario broken superblock, use backup SB.
+
+. ../default.cfg
+set -e
+u1=$mdstart
+u2=$((mdstart + 1))
+mp1=${mntpoint}$u1
+mp2=${mntpoint}$u2
+mkdir -p $mp1 $mp2
+log=$mp1/fsck3.sh.log
+diskimage=$mp1/diskimage
+
+max=$((10 * 1024 * 1024))
+[ "$newfs_flags" = "-j" ] &&
+ max=$((20 * 1024 * 1024))
+
+set -e
+mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1
+[ -c /dev/md$u1 ] && mdconfig -d -u $u1
+mdconfig -a -t swap -s 1g -u $u1
+newfs $newfs_flags /dev/md$u1 > /dev/null
+mount /dev/md$u1 $mp1
+
+[ -c /dev/md$u2 ] && mdconfig -d -u $u2
+dd if=/dev/zero of=$diskimage bs=$max count=1 status=none
+mdconfig -a -t vnode -f $diskimage -u $u2
+backups=`newfs -N $newfs_flags md$u2 | grep -A1 "super-block backups" | \
+ tail -1 | sed 's/,//g'`
+newfs $newfs_flags md$u2 > /dev/null
+sblock=`dumpfs md$u2 | grep -m1 "superblock location" | awk '{print $3}'`
+mount /dev/md$u2 $mp2 || s=100
+touch $mp2/file
+umount $mp2
+set +e
+
+s=0
+fsck_ffs -y /dev/md$u2 > $log 2>&1
+r=$?
+for i in $backups; do
+ dd if=/dev/random of=$diskimage oseek=$sblock bs=1 count=8 \
+ conv=notrunc status=none
+ fsck_ffs -y /dev/md$u2 > $log 2>&1
+ r=$?
+ echo "fsck_ffs -b $i -y /dev/md$u2"
+ fsck_ffs -b $i -y /dev/md$u2 > $log 2>&1
+ r=$?
+ mount /dev/md$u2 $mp2 || s=100
+ [ -f $mp2/file ] || { echo "$mp2/file not found"; s=101; }
+ umount $mp2
+done
+
+mdconfig -d -u $u2
+[ -f fsck_ffs.core ] && { ls -l fsck_ffs.core; s=102; }
+
+umount $mp1
+mdconfig -d -u $u1
+rm -f $diskimage
+exit $s
diff --git a/tools/test/stress2/misc/fsck4.sh b/tools/test/stress2/misc/fsck4.sh
new file mode 100755
index 000000000000..3e97f7b54340
--- /dev/null
+++ b/tools/test/stress2/misc/fsck4.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# "panic: ffs_valloc: dup alloc" seen:
+# https://people.freebsd.org/~pho/stress/log/kostik1128.txt
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+md5=c2e2d89745914bf12c5b251c358e1b3f
+size=$(((5368709120 + 13964088) / 1024 + 1))
+zimg=tmp.disk.xz
+log=fsck4.sh.log
+
+[ `df -k $(dirname $diskimage) | tail -1 | awk '{print $4}'` -lt $size ] &&
+{ echo "Not enough disk space."; exit 0; }
+[ -z "`which fetch`" ] && exit 0
+
+cd `dirname $diskimage`
+trap "rm -f $diskimage $zimg" EXIT INT
+fetch -q https://people.freebsd.org/~pho/$zimg || exit 0
+
+m=`md5 < $zimg`
+[ $m != $md5 ] && { echo "md5 diff"; rm $zimg; exit 1; }
+unxz < $zimg > $diskimage
+rm $zimg
+
+mdconfig -a -t vnode -f $diskimage -u $mdstart
+fsck_ffs -fy $diskimage > $log 2>&1
+if grep -q "MARKED CLEAN" $log; then
+ mount /dev/md$mdstart $mntpoint
+ touch $mntpoint/xxxxxxxx # Panics here
+ umount $mntpoint
+ s=0
+else
+ cat $log
+ s=1
+fi
+mdconfig -d -u $mdstart
+
+exit $s
diff --git a/tools/test/stress2/misc/fsck5.sh b/tools/test/stress2/misc/fsck5.sh
new file mode 100755
index 000000000000..35381c8c55e7
--- /dev/null
+++ b/tools/test/stress2/misc/fsck5.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# "INODE 3: FILE SIZE 1073741824 BEYOND END OF ALLOCATED FILE, SIZE SHOULD
+# BE 268435456" seen.
+# Original test scenario by Jamie Landeg-Jones <jamie@catflap.org>
+# Fixed by r346185
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+log1=/tmp/fsck5.sh.1.log
+log2=/tmp/fsck5.sh.2.log
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+newfs $newfs_flags -n md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+cd $mntpoint
+truncate -s 1g test
+ls -lis test > $log1
+sha256 -r test > sha256.out
+cd /
+umount $mntpoint
+
+(
+ fsck -fy /dev/md$mdstart
+ fsck -fy /dev/md$mdstart
+ fsck -fy /dev/md$mdstart
+) > $log2 2>&1
+
+mount /dev/md$mdstart $mntpoint
+cd $mntpoint
+ls -lis test >> $log1
+sha256 -r test > sha256-2.out
+s=0
+cmp sha256.out sha256-2.out ||
+ { cat $log2 $log1 sha256.out sha256-2.out; s=1; }
+cd /
+umount $mntpoint
+mdconfig -d -u $mdstart
+rm -f $log1 $log2
+exit $s
diff --git a/tools/test/stress2/misc/fsck6.sh b/tools/test/stress2/misc/fsck6.sh
new file mode 100755
index 000000000000..e1c5a4f36559
--- /dev/null
+++ b/tools/test/stress2/misc/fsck6.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+# fsck_ffs -p test
+
+# /dev/md10: CANNOT READ BLK: 320
+# /dev/md10: UNEXPECTED SOFT UPDATE INCONSISTENCY; RUN fsck MANUALLY.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+# Check for lingering threads from the last run
+pgrep -x mount && { pgrep -x mount | xargs ps -lp; exit 1; }
+../tools/killall.sh || exit 1
+
+fsck=/sbin/fsck_ffs
+log=/tmp/fsck6.log
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 5g -u $mdstart || exit 1
+md=md$mdstart
+newfs -U /dev/$md > /dev/null 2>&1 || exit 1
+
+gnop create /dev/$md || exit 1
+mount /dev/$md.nop $mntpoint || exit 1
+
+for i in `jot 5`; do
+ cp -a /usr/include $mntpoint/d$i
+done
+(cd $mntpoint; umount $mntpoint)
+ls -lR $mntpoint > /dev/null
+touch $mntpoint/x
+sync; sleep 1; sync; sleep 1; sync
+
+gnop destroy -f /dev/$md.nop
+
+# Wait until forcible unmount, may be up to about 30 seconds,
+# but typically very quick if I/O is in progress
+s=`date +%s`
+n=0
+while mount | grep -q "on $mntpoint "; do
+ [ $n -eq 0 ] && /bin/echo -n "Waiting for $mntpoint to force umount ..."
+ n=$((n + 1))
+ sleep 2
+ if [ $((`date +%s` - s)) -ge 180 ]; then
+ echo "Giving up on waiting for umount of $mntpoint"
+ umount $mntpoint || umount -f $mntpoint
+ break
+ fi
+done
+[ $n -ne 0 ] && echo
+
+$fsck -p /dev/$md; s=$?
+mdconfig -d -u ${md#md}
+rm -f $log
+exit $s
diff --git a/tools/test/stress2/misc/fsgs.sh b/tools/test/stress2/misc/fsgs.sh
new file mode 100755
index 000000000000..ba9e81de392d
--- /dev/null
+++ b/tools/test/stress2/misc/fsgs.sh
@@ -0,0 +1,135 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Regression test for r322799:
+# Ensure that fs/gs bases are stored in pcb before copying the pcb for
+# new process or thread.
+
+# "Exit status is 139" seen.
+
+# Test scenario suggestion by kib@
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/fsgs.c
+mycc -o fsgs -Wall -Wextra -O0 -g fsgs.c || exit 1
+rm -f fsgs.c
+cd $odir
+
+$dir/fsgs
+s=$?
+[ -f $dir/fsgs.core ] &&
+ { file $dir/fsgs.core; s=1; }
+
+rm -rf $dir/fsgs $dir/fsgs.core
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#define PARALLEL 4
+#define RUNTIME (1 * 60)
+#define SYNC 0
+
+static void
+test(void)
+{
+ pid_t pid;
+ int i, status;
+ volatile char *cp;
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ for (i = 0; i < 100; i++) {
+ if ((pid = fork()) == 0) {
+ cp = malloc(2);
+ _exit(0);
+ }
+ if (waitpid(pid, &status, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+ if (status != 0) {
+ fprintf(stderr, "Exit status is %d\n", status);
+ exit(1);
+ }
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/fstat.sh b/tools/test/stress2/misc/fstat.sh
new file mode 100755
index 000000000000..441065f8e769
--- /dev/null
+++ b/tools/test/stress2/misc/fstat.sh
@@ -0,0 +1,178 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Regression test for r368486
+# https://reviews.freebsd.org/D27513
+
+# Problem not seen.
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/fstat.c
+mycc -o fstat -Wall -Wextra -O0 -g fstat.c || exit 1
+rm -f fstat.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+newfs $newfs_flags md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+(cd ../testcases/swap; ./swap -t 10m -i 20 > /dev/null 2>&1) &
+cd $mntpoint
+$dir/fstat
+s=$?
+while pkill swap; do :; done
+wait
+[ -f fstat.core -a $s -eq 0 ] &&
+ { ls -l fstat.core; mv fstat.core $dir; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/fstat
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdatomic.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static _Atomic(int) *share;
+
+#define PARALLEL 5
+#define RUNTIME (2 * 60)
+#define SYNC 0
+#define DONE 1
+
+static void
+test(void)
+{
+ pid_t pid;
+ time_t start;
+ int fd[2], i;
+ char cmd[60];
+
+ (void)atomic_fetch_add(&share[SYNC], 1);
+ while (atomic_load(&share[SYNC]) != PARALLEL)
+ usleep(10);
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ share[DONE] = 0;
+ if ((pid = fork()) == 0) {
+ (void)atomic_fetch_add(&share[DONE], 1);
+ for (i = 0; i < 1000; i++) {
+ if (socketpair(PF_UNIX, SOCK_STREAM, 0, fd) == -1)
+ err(1, "socketpair()");
+ usleep(arc4random() % 1000);
+ close(fd[0]);
+ close(fd[1]);
+ }
+ (void)atomic_fetch_add(&share[DONE], 1);
+ _exit(0);
+ }
+ if (pid == -1)
+ err(1, "fork()");
+ setproctitle("master");
+ while (atomic_load(&share[DONE]) == 0)
+ usleep(10);
+ snprintf(cmd, sizeof(cmd), "fstat -p %d > /dev/null 2>&1",
+ pid);
+ while (atomic_load(&share[DONE]) == 1)
+ system(cmd);
+ system(cmd);
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid()");
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/fsync.sh b/tools/test/stress2/misc/fsync.sh
new file mode 100755
index 000000000000..c8c5a8243ae1
--- /dev/null
+++ b/tools/test/stress2/misc/fsync.sh
@@ -0,0 +1,91 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# vfs_write_suspend() test scenario.
+# The unusual combination of newfs(8) flags: J and j triggers:
+# fsync: giving up on dirty
+# GEOM_JOURNAL: Cannot suspend file system /mnt (error=35). (EAGAIN)
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ $((`sysctl -n hw.usermem` / 1024 / 1024 / 1024)) -le 10 ] &&
+ exit 0
+
+md1=$mdstart
+md2=$((mdstart + 1))
+
+size=5g
+jsize=3g
+
+for u in $md1 $md2; do
+ [ -c /dev/md$u ] && mdconfig -d -u $u
+ mdconfig -a -t swap -s $size -u $u
+done
+
+gmirror load > /dev/null 2>&1 && unload=1
+gmirror label -v -b split -s 2048 data /dev/md$md1 /dev/md$md2 \
+ || exit 1
+[ "`sysctl -in kern.geom.mirror.launch_mirror_before_timeout`" = "0" ] &&
+ sleep $((`sysctl -n kern.geom.mirror.timeout` + 1))
+
+gjournal load > /dev/null 2>&1
+gjournal label -s $jsize /dev/mirror/data > /dev/null ||
+ { gmirror stop data; exit 1; }
+sleep .5
+s=1
+if [ -c /dev/mirror/data.journal ]; then
+ newfs -J -j /dev/mirror/data.journal > /dev/null
+ mount -o async /dev/mirror/data.journal $mntpoint || exit 1
+
+ chmod 777 $mntpoint
+
+ export runRUNTIME=10m
+ export RUNDIR=$mntpoint/stressX
+
+ su $testuser -c 'cd ..; ./run.sh disk.cfg'
+ s=0
+
+ gjournal sync
+ umount $mntpoint
+ while mount | grep $mntpoint | grep -q /mirror/data; do
+ umount $mntpoint || sleep 1
+ done
+else
+ echo "FAIL /dev/mirror/data.journal not found"
+fi
+gjournal stop /dev/mirror/data
+gjournal unload
+gmirror stop data
+[ $unload ] && gmirror unload
+
+for u in $md2 $md1; do
+ mdconfig -d -u $u
+done
+exit $s
diff --git a/tools/test/stress2/misc/fsync2.sh b/tools/test/stress2/misc/fsync2.sh
new file mode 100755
index 000000000000..f2e846c4aa20
--- /dev/null
+++ b/tools/test/stress2/misc/fsync2.sh
@@ -0,0 +1,138 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Deadlock seen: https://people.freebsd.org/~pho/stress/log/mark169.txt
+# Test scenario based on the syzkaller reproducer syzkaller21.sh and comments
+# by markj@.
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/fsync2.c
+mycc -o fsync2 -Wall -Wextra -O0 -g fsync2.c -lpthread || exit 1
+rm -f fsync2.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+newfs $newfs_flags md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+(cd $mntpoint; $dir/fsync2)
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/fsync2
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define RUNTIME 60
+
+static time_t start;
+static volatile int fd;
+
+static void *
+t1(void *data __unused)
+{
+ char cwd[1024];
+
+ if (getcwd(cwd, sizeof(cwd)) == NULL)
+ err(1, "getcwd()");
+ while (time(NULL) - start < RUNTIME) {
+ if (mkdir("./file0", 0740) == -1)
+ err(1, "mkdir(file0)");
+ if (mkdir("./file1", 0740) == -1)
+ err(1, "mkdir(file2)");
+ if (rename("./file1", "./file0/file0") == -1)
+ err(1, "rename()");
+ if ((fd = open("./file0/file0", O_RDONLY)) == -1)
+ err(1, "open()");
+ if (chdir("./file0/file0") == -1)
+ err(1, "chdir()");
+ if (chdir(cwd) == -1)
+ err(1, "chdir(HOME)");
+ close(fd);
+ rmdir("./file0/file0");
+ rmdir("./file0");
+ }
+
+ return (NULL);
+}
+
+static void *
+t2(void *data __unused)
+{
+ while (time(NULL) - start < RUNTIME) {
+ fsync(fd);
+ }
+
+ return (NULL);
+}
+
+int
+main(void)
+{
+ pthread_t tid[2];
+ int r;
+
+ start = time(NULL);
+ if ((r = pthread_create(&tid[0], NULL, t1, NULL)) != 0)
+ errc(1, r, "pthread_create");
+ if ((r = pthread_create(&tid[1], NULL, t2, NULL)) != 0)
+ errc(1, r, "pthread_create");
+
+ if ((r = pthread_join(tid[0], NULL)) != 0)
+ errc(1, r, "pthread_join");
+ if ((r = pthread_join(tid[1], NULL)) != 0)
+ errc(1, r, "pthread_join");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/ftruncate.sh b/tools/test/stress2/misc/ftruncate.sh
new file mode 100755
index 000000000000..ddec85b6745a
--- /dev/null
+++ b/tools/test/stress2/misc/ftruncate.sh
@@ -0,0 +1,189 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# ftruncate(2) fuzz.
+
+# "panic: softdep_deallocate_dependencies: dangling deps" seen in
+# 10.3-STABLE:
+# https://people.freebsd.org/~pho/stress/log/ftruncate.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+dir=$RUNDIR
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > ftruncate.c
+rm -f /tmp/ftruncate
+mycc -o ftruncate -Wall -Wextra -O2 -g ftruncate.c -lpthread || exit 1
+rm -f ftruncate.c
+
+rm -rf $dir
+mkdir -p $dir
+chmod 777 $dir
+
+cd $dir
+jot 500 | xargs touch
+jot 500 | xargs chmod 666
+cd $odir
+
+(cd /tmp; /tmp/ftruncate $dir)
+e=$?
+
+rm -rf $dir
+
+rm -f /tmp/ftruncate
+exit $e
+EOF
+#include <sys/param.h>
+#include <sys/resource.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <libutil.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+#define RUNTIME 180
+#define THREADS 2
+
+static int fd[900];
+static u_int32_t r[N];
+static char *args[2];
+
+static unsigned long
+makearg(void)
+{
+ unsigned long val;
+
+ val = arc4random();
+#if defined(__LP64__)
+ val = (val << 32) | arc4random();
+ val = val & 0x00007fffffffffffUL;
+#endif
+
+ return(val);
+}
+
+static void *
+test(void *arg __unused)
+{
+ FTS *fts;
+ FTSENT *p;
+ int ftsoptions, i, n;
+
+ ftsoptions = FTS_PHYSICAL;
+
+ for (;;) {
+ for (i = 0; i < N; i++)
+ r[i] = arc4random();
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ i = n = 0;
+ while ((p = fts_read(fts)) != NULL) {
+ if (fd[i] > 0)
+ close(fd[i]);
+ if ((fd[i] = open(p->fts_path, O_RDWR)) == -1)
+ if ((fd[i] = open(p->fts_path, O_WRONLY)) == -1)
+ continue;
+ i++;
+ i = i % nitems(fd);
+ }
+
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+ sleep(1);
+ }
+ return(0);
+}
+
+static void *
+calls(void *arg __unused)
+{
+ off_t offset;
+ time_t start;
+ int fd2;
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ fd2 = makearg() % nitems(fd) + 3;
+ offset = makearg();
+ if (ftruncate(fd2, offset) == 0) {
+ if (lseek(fd2, offset - 1, SEEK_SET) != -1)
+ write(fd2, "x", 1);
+ }
+
+ }
+
+ return (0);
+}
+
+int
+main(int argc, char **argv)
+{
+ struct passwd *pw;
+ pthread_t rp, cp[THREADS];
+ int e, i;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <dir>\n", argv[0]);
+ exit(1);
+ }
+ args[0] = argv[1];
+ args[1] = 0;
+
+ if ((pw = getpwnam("nobody")) == NULL)
+ err(1, "failed to resolve nobody");
+ if (setgroups(1, &pw->pw_gid) ||
+ setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
+ seteuid(pw->pw_uid) || setuid(pw->pw_uid))
+ err(1, "Can't drop privileges to \"nobody\"");
+ endpwent();
+
+ if ((e = pthread_create(&rp, NULL, test, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ usleep(1000);
+ for (i = 0; i < THREADS; i++)
+ if ((e = pthread_create(&cp[i], NULL, calls, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ for (i = 0; i < THREADS; i++)
+ pthread_join(cp[i], NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/ftruncate2.sh b/tools/test/stress2/misc/ftruncate2.sh
new file mode 100755
index 000000000000..c8c89e7ea5bd
--- /dev/null
+++ b/tools/test/stress2/misc/ftruncate2.sh
@@ -0,0 +1,205 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# A fuzz test triggered a failed block allocation unwinding problem.
+
+# "panic: ffs_blkfree_cg: freeing free block" seen:
+# https://people.freebsd.org/~pho/stress/log/kostik923.txt
+# Fixed by r304232.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > ftruncate2.c
+rm -f /tmp/ftruncate2
+mycc -o ftruncate2 -Wall -Wextra -O2 -g ftruncate2.c -lpthread || exit 1
+rm -f ftruncate2.c
+
+echo "Expect: \"/mnt: write failed, filesystem is full\""
+mount | grep $mntpoint | grep -q "on $mntpoint " && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs md${mdstart}$part > /dev/null # Non SU panics
+mount /dev/md${mdstart}$part $mntpoint
+
+dir=$mntpoint
+chmod 777 $dir
+
+cd $dir
+jot 500 | xargs touch
+jot 500 | xargs chmod 666
+cd $odir
+
+(cd /tmp; /tmp/ftruncate2 $dir)
+e=$?
+
+rm -rf $dir/*
+
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/ftruncate2
+exit $e
+EOF
+#include <sys/param.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <libutil.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+#define RUNTIME 180
+#define THREADS 2
+
+static int fd[900];
+static u_int32_t r[N];
+static char *args[2];
+
+static unsigned long
+makearg(void)
+{
+ unsigned long val;
+
+ val = arc4random();
+#if defined(__LP64__)
+ val = (val << 32) | arc4random();
+ val = val & 0x00007fffffffffffUL;
+#endif
+
+ return(val);
+}
+
+static void *
+test(void *arg __unused)
+{
+ FTS *fts;
+ FTSENT *p;
+ int ftsoptions, i, n;
+
+ ftsoptions = FTS_PHYSICAL;
+
+ for (;;) {
+ for (i = 0; i < N; i++)
+ r[i] = arc4random();
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ i = n = 0;
+ while ((p = fts_read(fts)) != NULL) {
+ if (fd[i] > 0)
+ close(fd[i]);
+ if ((fd[i] = open(p->fts_path, O_RDWR)) == -1)
+ if ((fd[i] = open(p->fts_path, O_WRONLY)) == -1)
+ continue;
+ if (ftruncate(fd[i], 0) != 0)
+ err(1, "ftruncate");
+ i++;
+ i = i % nitems(fd);
+ }
+
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+ sleep(1);
+ }
+ return(0);
+}
+
+static void *
+calls(void *arg __unused)
+{
+ off_t offset;
+ time_t start;
+ int fd2;
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ fd2 = makearg() % nitems(fd) + 3;
+ offset = makearg();
+ if (lseek(fd2, offset - 1, SEEK_SET) != -1) {
+ if (write(fd2, "x", 1) != 1)
+ if (errno != EBADF && errno != ENOSPC && errno != E2BIG &&
+ errno != ESTALE && errno != EFBIG)
+ warn("write");
+ } else
+ if (errno != EBADF)
+ warn("lseek");
+ if (fsync(fd2) == -1)
+ if (errno != EBADF)
+ warn("x");
+ }
+
+ return (0);
+}
+
+int
+main(int argc, char **argv)
+{
+ struct passwd *pw;
+ pthread_t rp, cp[THREADS];
+ int e, i;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <dir>\n", argv[0]);
+ exit(1);
+ }
+ args[0] = argv[1];
+ args[1] = 0;
+
+ if ((pw = getpwnam("nobody")) == NULL)
+ err(1, "failed to resolve nobody");
+ if (setgroups(1, &pw->pw_gid) ||
+ setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
+ seteuid(pw->pw_uid) || setuid(pw->pw_uid))
+ err(1, "Can't drop privileges to \"nobody\"");
+ endpwent();
+
+ if ((e = pthread_create(&rp, NULL, test, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ usleep(1000);
+ for (i = 0; i < THREADS; i++)
+ if ((e = pthread_create(&cp[i], NULL, calls, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ for (i = 0; i < THREADS; i++)
+ pthread_join(cp[i], NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/fts.sh b/tools/test/stress2/misc/fts.sh
new file mode 100755
index 000000000000..b823517f7c3c
--- /dev/null
+++ b/tools/test/stress2/misc/fts.sh
@@ -0,0 +1,144 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Run with marcus.cfg on a 1g swap backed MD
+# "panic: vm_radix_remove: invalid key found" seen.
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > fts1.c
+mycc -o fts1 -Wall -Wextra fts1.c || exit 1
+rm -f fts1.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=20m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null &
+pid=$!
+while kill -0 $pid 2> /dev/null; do
+ /tmp/fts1 $mntpoint
+ sleep 1
+done
+wait
+
+s=0
+for i in `jot 6`; do
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && s=1
+mdconfig -d -u $mdstart
+rm -f /tmp/fts1
+exit $s
+EOF
+#include <sys/param.h>
+#include <err.h>
+#include <errno.h>
+#include <fts.h>
+#include <libutil.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+test(char *path)
+{
+
+ FTS *fts;
+ FTSENT *p;
+ int ftsoptions;
+ char *args[2];
+
+ ftsoptions = FTS_PHYSICAL;
+ args[0] = path;
+ args[1] = 0;
+
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ while ((p = fts_read(fts)) != NULL) {
+ switch (p->fts_info) {
+ case FTS_F: /* Ignore. */
+ break;
+ case FTS_D: /* Ignore. */
+ break;
+ case FTS_DP:
+ break;
+ case FTS_DC: /* Ignore. */
+ break;
+ case FTS_SL: /* Ignore. */
+ break;
+ case FTS_DNR: /* Warn, continue. */
+ case FTS_ERR:
+ case FTS_NS:
+ case FTS_DEFAULT:
+ break;
+ default:
+ printf("%s: default, %d\n", getprogname(), p->fts_info);
+ break;
+ }
+ }
+
+ if (errno != 0 && errno != ENOENT)
+ err(1, "fts_read");
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+
+ return (0);
+}
+
+int
+main(int argc, char **argv)
+{
+ int i;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <path>", argv[0]);
+ alarm(600);
+ for (i = 0; i < 100; i++)
+ test(argv[1]);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/fts2.sh b/tools/test/stress2/misc/fts2.sh
new file mode 100755
index 000000000000..d155393e66ba
--- /dev/null
+++ b/tools/test/stress2/misc/fts2.sh
@@ -0,0 +1,285 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Show invalid fts_info value:
+
+# FAULT
+# -rw------- 1 root wheel - 4 13 jan 09:25 ./lockf.0.3676
+# fts_path: ./lockf.0.3676
+# fts_info: 13 FTS_SLNONE
+# fts_errno: 0 No error: 0
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+cat > /tmp/fts2.c <<EOF
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOOPS 1
+#define PARALLEL 7
+
+pid_t pid;
+time_t start;
+int fd;
+char file[128];
+
+char *txt[] = {
+ "NULL",
+ "FTS_D",
+ "FTS_DC",
+ "FTS_DEFAULT",
+ "FTS_DNR",
+ "FTS_DOT",
+ "FTS_DP",
+ "FTS_ERR",
+ "FTS_F",
+ "FTS_INIT",
+ "FTS_NS",
+ "FTS_NSOK",
+ "FTS_SL",
+ "FTS_SLNONE",
+ "FTS_W",
+ "15",
+ "16",
+ "17",
+};
+
+int
+get(void) {
+ int sem;
+ if (lockf(fd, F_LOCK, 0) == -1)
+ err(1, "lockf(%s, F_LOCK)", file);
+ if (read(fd, &sem, sizeof(sem)) != sizeof(sem))
+ err(1, "get: read(%d)", fd);
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ err(1, "lseek");
+ if (lockf(fd, F_ULOCK, 0) == -1)
+ err(1, "lockf(%s, F_ULOCK)", file);
+ return (sem);
+}
+
+void
+incr(void) {
+ int sem;
+ if (lockf(fd, F_LOCK, 0) == -1)
+ err(1, "lockf(%s, F_LOCK)", file);
+ if (read(fd, &sem, sizeof(sem)) != sizeof(sem))
+ err(1, "incr: read(%d)", fd);
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ err(1, "lseek");
+ sem++;
+ if (write(fd, &sem, sizeof(sem)) != sizeof(sem))
+ err(1, "incr: read");
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ err(1, "lseek");
+ if (lockf(fd, F_ULOCK, 0) == -1)
+ err(1, "lockf(%s, F_ULOCK)", file);
+}
+
+void
+tlockf(void)
+{
+ int i;
+ int sem = 0;
+
+ usleep(arc4random() % 10000);
+ sprintf(file, "lockf.0.%d", getpid());
+ if ((fd = open(file,O_CREAT | O_TRUNC | O_RDWR, 0600)) == -1)
+ err(1, "creat(%s)", file);
+ if (write(fd, &sem, sizeof(sem)) != sizeof(sem))
+ err(1, "write");
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ err(1, "lseek");
+
+ pid = fork();
+ if (pid == -1) {
+ perror("fork");
+ exit(2);
+ }
+
+ if (pid == 0) { /* child */
+ for (i = 0; i < 100; i++) {
+ while ((get() & 1) == 0)
+ ;
+ incr();
+ }
+ exit(0);
+ } else { /* parent */
+ for (i = 0; i < 100; i++) {
+ while ((get() & 1) == 1)
+ ;
+ incr();
+ }
+ }
+ close(fd);
+ waitpid(pid, &i, 0);
+ unlink(file);
+}
+
+void
+tmkdir(void)
+{
+ pid_t pid;
+ int i, j;
+ char name[80];
+
+ setproctitle(__func__);
+ usleep(arc4random() % 10000);
+ pid = getpid();
+ for (j = 0; j < LOOPS; j++) {
+ for (i = 0; i < 1000; i++) {
+ snprintf(name, sizeof(name), "dir.%d.%06d", pid, i);
+ if (mkdir(name, 0644) == -1)
+ err(1, "mkdir(%s)", name);
+ }
+ for (i = 0; i < 1000; i++) {
+ snprintf(name, sizeof(name), "dir.%d.%06d", pid, i);
+ if (rmdir(name) == -1)
+ err(1, "unlink(%s)", name);
+ }
+ }
+}
+
+void
+tfts(void)
+{
+ FTS *fts;
+ FTSENT *p;
+ int ftsoptions, i;
+ char *args[2];
+ char help[80];
+
+ usleep(arc4random() % 10000);
+ ftsoptions = FTS_LOGICAL;
+ args[0] = ".";
+ args[1] = 0;
+
+ for (i = 0; i < 10; i++) {
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ while ((p = fts_read(fts)) != NULL) {
+ if (p->fts_info == FTS_D || /* preorder directory */
+ p->fts_info == FTS_DNR || /* unreadable directory */
+ p->fts_info == FTS_DOT || /* dot or dot-dot */
+ p->fts_info == FTS_DP || /* postorder directory */
+ p->fts_info == FTS_F || /* regular file */
+ p->fts_info == FTS_NS) /* stat(2) failed */
+ continue;
+ fprintf(stderr, "FAULT\n");
+ sprintf(help, "ls -lo %s", p->fts_path);
+ system(help);
+ fprintf(stderr, "fts_path: %s\n", p->fts_path);
+ fprintf(stderr, "fts_info: %d %s\n", p->fts_info,
+ txt[p->fts_info]);
+ fprintf(stderr, "fts_errno: %d %s\n", p->fts_errno,
+ strerror(p->fts_errno));
+ }
+
+ if (errno != 0 && errno != ENOENT)
+ err(1, "fts_read");
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+ }
+}
+
+void
+test(void)
+{
+
+ start = time(NULL);
+ if (fork() == 0) {
+ while (time(NULL) - start < 60)
+ tmkdir();
+ _exit(0);
+ }
+ if (fork() == 0) {
+ while (time(NULL) - start < 60)
+ tlockf();
+ _exit(0);
+ }
+ if (fork() == 0) {
+ while (time(NULL) - start < 60)
+ tfts();
+ _exit(0);
+ }
+
+ wait(NULL);
+ wait(NULL);
+ wait(NULL);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i;
+
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ test();
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+
+ return (0);
+}
+EOF
+mycc -o /tmp/fts2 -Wall -Wextra -O0 -g /tmp/fts2.c || exit 1
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+(cd $mntpoint; /tmp/fts2)
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm /tmp/fts2 /tmp/fts2.c
diff --git a/tools/test/stress2/misc/fts3.sh b/tools/test/stress2/misc/fts3.sh
new file mode 100755
index 000000000000..4cb75d8bf6e6
--- /dev/null
+++ b/tools/test/stress2/misc/fts3.sh
@@ -0,0 +1,133 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# No problems seen.
+
+. ../default.cfg
+
+[ -d /usr/include ] || exit 0
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > fts3.c
+mycc -o fts3 -Wall -Wextra fts3.c || exit 1
+rm -f fts3.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 3g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 120 ]; do
+ pids=
+ for i in `jot 20`; do
+ cp -r /usr/include $mntpoint/$i &
+ pids="$pids $!"
+ done
+ for p in $pids; do
+ wait $p
+ done
+ (cd $mntpoint && rm -rf *)
+done &
+
+pid=$!
+s=0
+while kill -0 $pid 2> /dev/null; do
+ /tmp/fts3 $mntpoint || { s=1; break; }
+done
+kill $pid > /dev/null 2>&1
+wait $pid
+
+for i in `jot 6`; do
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && s=2
+mdconfig -d -u $mdstart
+rm -f /tmp/fts3
+exit $s
+EOF
+#include <sys/param.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fts.h>
+#include <libutil.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+test(char *path)
+{
+
+ FTS *fts;
+ FTSENT *p;
+ int ftsoptions;
+ char *args[2];
+
+ ftsoptions = FTS_PHYSICAL;
+ args[0] = path;
+ args[1] = 0;
+
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ while ((p = fts_read(fts)) != NULL)
+ ;
+
+ if (errno != 0 && errno != ENOENT)
+ err(1, "fts_read");
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+
+ return (0);
+}
+
+int
+main(int argc, char **argv)
+{
+ int i;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <path>\n", argv[0]);
+ exit(1);
+ }
+ alarm(120);
+ for (i = 0; i < 100; i++)
+ test(argv[1]);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/full.sh b/tools/test/stress2/misc/full.sh
new file mode 100755
index 000000000000..ec99ff88138a
--- /dev/null
+++ b/tools/test/stress2/misc/full.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Run all tests from testcases on a 2g swap backed MD with UFS SU fs.
+
+# "panic: SACK scoreboard must not be empty" seen:
+# https://people.freebsd.org/~pho/stress/log/full.txt
+# Fixed by r310547.
+
+. ../default.cfg
+kldstat -v | grep -q pty || kldload pty # ignore any load failure
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export LOAD=80
+export rwLOAD=80
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+export MAXSWAPPCT=80
+export TESTPROGS=`cd ..; find testcases/ -perm -1 -type f | \
+ egrep -v "/run/"`
+
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
+
+../tools/killall.sh
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+checkfs /dev/md${mdstart}$part; s=$?
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/fullpath.sh b/tools/test/stress2/misc/fullpath.sh
new file mode 100755
index 000000000000..88cee4795009
--- /dev/null
+++ b/tools/test/stress2/misc/fullpath.sh
@@ -0,0 +1,111 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Test scenario by marcus@freebsd.org
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -z "`type perl 2>/dev/null`" ] && exit 0
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > fullpath.c
+mycc -o fullpath -Wall fullpath.c
+rm -f fullpath.c
+cd /proc
+
+for i in `jot 5`; do
+ /tmp/fullpath &
+done
+
+for i in `jot 30`; do
+ for j in `jot 25`; do
+ pid=`perl -e 'print splice(@ARGV,rand(@ARGV),1), " ";' $(echo [0-9]*)`
+# echo $pid
+ procstat -f $pid > /dev/null 2>&1
+ procstat -f $pid > /dev/null 2>&1
+ procstat -f $pid > /dev/null 2>&1
+ procstat -f $pid > /dev/null 2>&1
+ procstat -f $pid > /dev/null 2>&1
+ done
+done
+
+for i in `jot 5`; do
+ wait
+done
+
+rm -f /tmp/fullpath
+exit
+EOF
+
+#include <sys/wait.h>
+#include <err.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <unistd.h>
+
+char buf[4096];
+
+void
+handler(int i) {
+ exit(0);
+}
+
+int
+test(void) {
+ pid_t r;
+ int status;
+
+ for (;;) {
+ r = fork();
+ if (r == 0) {
+ bzero(buf, sizeof(buf));
+ exit(0); /*child dies */
+ }
+ if (r < 0) {
+ perror("fork");
+ exit(2);
+ }
+ wait(&status);
+ }
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ int i;
+
+ i = 0;
+ signal(SIGALRM, handler);
+ alarm(60);
+
+ return test();
+}
diff --git a/tools/test/stress2/misc/fullpath2.sh b/tools/test/stress2/misc/fullpath2.sh
new file mode 100755
index 000000000000..e4024c32f317
--- /dev/null
+++ b/tools/test/stress2/misc/fullpath2.sh
@@ -0,0 +1,214 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC Isilon
+# 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 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.
+#
+
+# fullpath NULL reference problem hunt.
+
+# From the commit log of r308407:
+# vn_fullpath1() checked VV_ROOT and then unreferenced
+# vp->v_mount->mnt_vnodecovered unlocked. This allowed unmount to race.
+# Lock vnode after we noticed the VV_ROOT flag. See comments for
+# explanation why unlocked check for the flag is considered safe.
+
+# 'panic: namei: garbage in ni_resflags: 1':
+# https://people.freebsd.org/~pho/stress/log/fullpath2.txt
+# Fixed by r367130
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+cont=/tmp/fullpath2.continue
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/fullpath2.c
+mycc -o fullpath2 -Wall -Wextra -O2 -g fullpath2.c -lprocstat || exit 1
+rm -f fullpath2.c
+cd $odir
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+gpart create -s GPT md$mdstart > /dev/null || exit 1
+gpart add -t freebsd-ufs md$mdstart > /dev/null || exit 1
+newfs -n $newfs_flags md${mdstart}p1 > /dev/null || exit 1
+mount /dev/md${mdstart}p1 $mntpoint
+touch $mntpoint/marker $cont
+trap "rm -f $cont" EXIT INT
+
+daemon sh -c "(cd $odir/../testcases/swap; ./swap -t 4m -i 10 -l 100)" > \
+ /dev/null 2>&1
+
+for i in `jot $(jot -r 1 2 10)`; do
+ /tmp/fullpath2 $mntpoint &
+ pids="$pids $!"
+done
+
+for i in `jot $(jot -r 1 2 5)`; do
+ while [ -e $cont ]; do find $mntpoint -ls > /dev/null 2>&1; done &
+ pidf="$pidf $!"
+done
+
+umounts=0
+while pgrep -q fullpath2; do
+ for i in `jot 30`; do
+ umount -f $mntpoint && umounts=$((umounts+1)) &&
+ mount /dev/md${mdstart}p1 $mntpoint
+ sleep 2
+ done
+done
+echo "$umounts umounts"
+rm -f $cont
+while mount | grep -q "on $mntpoint "; do
+ umount -f $mntpoint
+done
+for i in $pids; do
+ wait $i
+done
+while pgrep -q swap; do
+ pkill -9 swap
+done
+
+kill $pidp $pidf > /dev/null 2>&1
+wait
+
+rm -f $mntpoint/file.* /tmp/fullpath2 fullpath2.core
+mdconfig -d -u $mdstart
+
+exit 0
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <sys/sysctl.h>
+#include <sys/user.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <libprocstat.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#define NB 1024
+#define RUNTIME 300
+
+/* dtrace -w -n 'fbt::*vn_fullpath1:entry {@rw[execname,probefunc] = count(); }' */
+
+static void
+getfiles(pid_t pid)
+{
+ struct filestat_list *head;
+ struct kinfo_proc *p;
+ struct procstat *prstat;
+ unsigned int cnt;
+
+ if ((prstat = procstat_open_sysctl()) == NULL)
+ err(1, "procstat_open_sysctl");
+
+ if ((p = procstat_getprocs(prstat, KERN_PROC_PID,
+ pid, &cnt)) == NULL)
+ err(1, "procstat_getprocs");
+
+ if ((head = procstat_getfiles(prstat, p, 0)) == NULL)
+ err(1, "procstat_getfiles");
+
+ procstat_freefiles(prstat, head);
+ procstat_freeprocs(prstat, p);
+ procstat_close(prstat);
+}
+
+int
+main(int argc, char *argv[])
+{
+ size_t len;
+ time_t start;
+ int fd[NB], i, n;
+ pid_t pid;
+
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ if ((pid = fork()) == 0) {
+ setproctitle("getfiles");
+ while (share[0] == 0)
+ getfiles(pid);
+ _exit(0);
+ }
+
+ char file[MAXPATHLEN + 1];
+ char marker[MAXPATHLEN + 1];
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <file path>\n", argv[0]);
+ exit(1);
+ }
+
+ memset(fd, 0, sizeof(fd));
+ snprintf(marker, sizeof(marker), "%s/marker", argv[1]);
+ i = n = 0;
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ snprintf(file, sizeof(file), "%s/file.%06d.%02d", argv[1], getpid(), i);
+ if (access(marker, R_OK) == -1)
+ continue;
+ if (fd[i] > 0)
+ close(fd[i]);
+ if ((fd[i] = open(file, O_RDWR | O_CREAT | O_APPEND,
+ DEFFILEMODE)) == -1) {
+ if (errno != ENOENT && errno != EBUSY)
+ warn("open(%s)", file);
+ continue;
+ }
+ n++;
+ write(fd[i], "a", 1);
+ usleep(arc4random() % 400);
+ if (arc4random() % 100 < 10) {
+ close(fd[i]);
+ unlink(file);
+ fd[i] = 0;
+ }
+ i++;
+ i = i % NB;
+ }
+ share[0] = 1;
+
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid");
+ if (n < 100)
+ errx(1, "Short run: %d", n);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/fuse.sh b/tools/test/stress2/misc/fuse.sh
new file mode 100755
index 000000000000..682fdb122845
--- /dev/null
+++ b/tools/test/stress2/misc/fuse.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# General Fuse test scenario
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -z "`type mkntfs 2>/dev/null`" ] && exit 0
+[ -c /dev/fuse ] || kldload fuse.ko
+
+MOUNT=/usr/local/bin/ntfs-3g
+
+mount | grep -q "$mntpoint" && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+mkntfs -Ff /dev/md$mdstart > /dev/null 2>&1 || exit 1
+
+$MOUNT /dev/md$mdstart $mntpoint || exit 1
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=20m
+(cd ..; ./run.sh marcus.cfg)
+rm -rf $RUNDIR
+
+for i in `jot 10`; do
+ umount $mntpoint || sleep 2
+ mount | grep -q $mntpoint || break
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/fuse2.sh b/tools/test/stress2/misc/fuse2.sh
new file mode 100755
index 000000000000..3bd54fa080f5
--- /dev/null
+++ b/tools/test/stress2/misc/fuse2.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Parallel mount and umount test
+# livelock seen
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+[ -z "`type mkntfs 2>/dev/null`" ] && exit 0
+[ -c /dev/fuse ] || kldload fuse.ko
+
+. ../default.cfg
+
+[ -c /dev/fuse ] || kldload fuse.ko
+MOUNT=/usr/local/bin/ntfs-3g
+mounts=15 # Number of parallel scripts
+mdstart=$mdstart # Use md unit numbers from this point
+
+if [ $# -eq 0 ]; then
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ [ ! -d ${mntpoint}$m ] && mkdir ${mntpoint}$m
+ mount | grep "$mntpoint" | grep -q md$m && umount ${mntpoint}$m
+ mdconfig -l | grep -q md$m && mdconfig -d -u $m
+
+ mdconfig -a -t swap -s 1g -u $m
+ mkntfs -Ff /dev/md$m > /dev/null 2>&1 || exit 1
+
+ done
+
+ # start the parallel tests
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ ./$0 $m &
+ ./$0 find &
+ done
+ wait
+
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ mdconfig -d -u $m
+ done
+
+else
+ if [ $1 = find ]; then
+ while mount | grep -q fusefs; do
+ find ${mntpoint}* -type f > /dev/null 2>&1
+ sleep 1
+ done
+ else
+
+ # The test: Parallel mount and unmounts
+ for i in `jot 100`; do
+ m=$1
+ $MOUNT /dev/md$m ${mntpoint}$m || continue
+ cp -r /usr/include/sys ${mntpoint}$m/file.$m 2>/dev/null
+ sleep .5
+ while mount | grep -q ${mntpoint}$m; do
+ umount ${mntpoint}$m > /dev/null 2>&1 ||
+ sleep 1
+ done
+ done
+ fi
+fi
diff --git a/tools/test/stress2/misc/fuse3.sh b/tools/test/stress2/misc/fuse3.sh
new file mode 100755
index 000000000000..d11e9f4140f7
--- /dev/null
+++ b/tools/test/stress2/misc/fuse3.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Demonstrate FUSE memory corruption.
+# http://people.freebsd.org/~pho/stress/log/fuse3.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Start page stealer
+(cd ../testcases/swap; ./swap -t 10m -i 20 -h) &
+sleep 10
+
+while pgrep -q swap; do
+ kldload fuse.ko
+ kldunload fuse.ko
+done
+wait
diff --git a/tools/test/stress2/misc/fuzz.sh b/tools/test/stress2/misc/fuzz.sh
new file mode 100755
index 000000000000..bfe783752280
--- /dev/null
+++ b/tools/test/stress2/misc/fuzz.sh
@@ -0,0 +1,180 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Stress test UFS2 file systems by introducing single bit errors in the FS
+# fsck should fix the FS no matter how damaged, but e.g. this panic has been seen:
+#
+# panic(c0912b65,dfe96000,0,c09e4060,ef48c778,...) at panic+0x14b
+# vm_fault(c1868000,dfe96000,1,0) at vm_fault+0x1e0
+# trap_pfault(ef48c894,0,dfe96000) at trap_pfault+0x137
+# trap(dfe90008,ef480028,c0690028,d0560000,dfe96000,...) at trap+0x341
+# calltrap() at calltrap+0x5
+# --- trap 0xc, eip = 0xc08785a6, esp = 0xef48c8d4, ebp = 0xef48c958 ---
+# generic_bcopy(c81cd570,d0508000,c5ead600,c87b81c0,0,...) at generic_bcopy+0x1a
+# ffs_mount(d0508000,c5ead600,0,c09b0860,c5ecfc3c,...) at ffs_mount+0xa14
+# vfs_domount(c5ead600,cd8c7280,ccb75080,0,...) at vfs_domount+0x687
+# vfs_donmount(c5ead600,0,ef48cc04) at vfs_donmount+0x2ef
+# kernel_mount(c5660960,0,bfbfec86,0,fffffffe,...) at kernel_mount+0x6d
+# ffs_cmount(c5660960,bfbfde50,0,c5ead600,c09b0860,...) at ffs_cmount+0x5d
+# mount(c5ead600,ef48cd04) at mount+0x156
+# syscall(3b,3b,3b,804abcf,bfbfe8e4,...) at syscall+0x22f
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+D=$diskimage
+
+tst() {
+ rm -f $D
+ truncate -s 2M $D
+ mdconfig -a -t vnode -f $D -u $mdstart
+ bsdlabel -w md$mdstart auto
+ newfs -b 8192 -f 1024 $newfs_flags /dev/md${mdstart}$part > /dev/null 2>&1
+ mount /dev/md${mdstart}$part $mntpoint
+ cp /etc/passwd /etc/group /etc/hosts $mntpoint
+ cp -r /usr/include/ufs $mntpoint
+ umount $mntpoint
+
+ for i in `jot 50`; do
+ ./fuzz -n 50 $D
+ if fsck -f -y /dev/md${mdstart}$part 2>&1 | egrep "^[A-Z]" > /dev/null; then
+ if fsck -f -y /dev/md${mdstart}$part 2>&1 | egrep "^[A-Z]" > /dev/null; then
+ if fsck -f -y /dev/md${mdstart}$part 2>&1 | egrep "^[A-Z]" > /dev/null; then
+ echo "fsck is giving up in loop $i!"
+ break
+ fi
+ fi
+ fi
+ sync;sync;sync
+ if mount /dev/md${mdstart}$part $mntpoint; then
+ ls -l $mntpoint > /dev/null
+ find $mntpoint -exec dd if={} of=/dev/null bs=1m count=3 \; > /dev/null 2>&1
+ umount $mntpoint
+ else
+ echo "Giving up at loop $i"
+ break
+ fi
+ done
+ mdconfig -d -u $mdstart
+ rm -f $D
+}
+
+odir=`pwd`
+dir=/tmp
+
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/fuzz.c
+mycc -o fuzz -Wall fuzz.c
+rm -f fuzz.c
+
+for j in `jot 10`; do
+ date '+%T'
+ tst
+done
+rm -f fuzz
+
+exit
+
+EOF
+#include <sys/stat.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static void
+usage(void)
+{
+ fprintf(stderr, "%s {-n <num|-v} <file>\n", getprogname());
+ exit(1);
+}
+
+static long
+random_long(long mi, long ma)
+{
+ return (arc4random() % (ma - mi + 1) + mi);
+}
+
+int
+main(int argc, char **argv)
+{
+ long pos;
+ int ch, fd, i, times = 1, verbose = 0;
+ unsigned char bit, buf, mask, old;
+ struct stat sb;
+
+ while ((ch = getopt(argc, argv, "n:v")) != -1) {
+ switch(ch) {
+ case 'n': /* Bits to alter */
+ if (sscanf(optarg, "%d", &times) != 1)
+ usage();
+ break;
+ case 'v': /* verbose flag */
+ verbose += 1;
+ break;
+ default:
+ usage();
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (argc == 0)
+ usage();
+
+ if ((fd = open(argv[0], O_RDWR)) == -1)
+ err(1, "open(%s)", argv[0]);
+ if (fstat(fd, &sb) == -1)
+ err(1, "stat(%s)", argv[0]);
+
+ for (i = 0; i < times; i++) {
+ pos = random_long(0, sb.st_size - 1);
+ if (lseek(fd, pos, SEEK_SET) == -1)
+ err(1, "fseek(%d, %ld)", fd, pos);
+ if (read(fd, &buf, 1) != 1)
+ err(1, "read(%d)", fd);
+ bit = random_long(0,7);
+ mask = ~(1 << bit);
+ old = buf;
+ buf = (buf & mask) | (~buf & ~mask);
+ if (verbose > 0)
+ printf("Change %2x to %2x at %4ld "
+ "by flipping bit %d\n",
+ old, buf, pos, bit);
+ if (lseek(fd, pos, SEEK_SET) == -1)
+ err(1, "fseek(%d, %ld)", fd, pos);
+ if (write(fd, &buf, 1) != 1)
+ err(1, "write(%d)", fd);
+ }
+ close(fd);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/gbde.sh b/tools/test/stress2/misc/gbde.sh
new file mode 100755
index 000000000000..f47d8e9e15bf
--- /dev/null
+++ b/tools/test/stress2/misc/gbde.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# "panic: bio_driver1 used by the consumer (geom ffs.md5.bde)" seen
+# http://people.freebsd.org/~pho/stress/log/gbde.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+
+gbde init /dev/md$mdstart -P pass-phrase || exit
+gbde attach md$mdstart -p pass-phrase || exit
+
+newfs $newfs_flags /dev/md$mdstart.bde > /dev/null
+mount /dev/md$mdstart.bde $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=20m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg'
+
+while mount | grep $mntpoint | grep -q bde; do
+ umount $mntpoint || sleep 1
+done
+gbde detach md$mdstart
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/geli.sh b/tools/test/stress2/misc/geli.sh
new file mode 100755
index 000000000000..381bd1ea5f8e
--- /dev/null
+++ b/tools/test/stress2/misc/geli.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Simple geli(8) test
+# WIP No problems seen.
+
+. ../default.cfg
+
+kldstat -v | grep -q g_eli ||
+ { geli load && unload=1 || exit 0; }
+set -e
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+
+dd if=/dev/random of=/tmp/geli.key bs=64 count=1 > /dev/null 2>&1
+echo test | geli init -s 4096 -J - -K /tmp/geli.key /dev/md$mdstart > \
+ /dev/null
+echo test | geli attach -j - -k /tmp/geli.key /dev/md$mdstart
+newfs $newfs_flags /dev/md$mdstart.eli > /dev/null
+
+mount /dev/md${mdstart}.eli $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=5m
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1 &
+sleep 300
+../tools/killall.sh
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+set +e
+checkfs /dev/md${mdstart}.eli; s=$?
+geli kill /dev/md$mdstart.eli
+mdconfig -d -u $mdstart
+rm -f /tmp/geli.key
+[ $unload ] && geli unload
+exit $s
diff --git a/tools/test/stress2/misc/geomleak.sh b/tools/test/stress2/misc/geomleak.sh
new file mode 100755
index 000000000000..0961bc58f8bf
--- /dev/null
+++ b/tools/test/stress2/misc/geomleak.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# GEOM leak regression test.
+# The problem was introduced in r328426 and fixed by r329375.
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+set -e
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+old=`vmstat -m | awk '/GEOM/{print $2}'`
+for i in `jot 50`; do
+ mdconfig -a -t swap -s 500m -u $mdstart
+ newfs $newfs_flags /dev/md$mdstart > /dev/null 2>&1
+ mdconfig -d -u $mdstart
+done
+set +e
+new=`vmstat -m | awk '/GEOM/{print $2}'`
+if [ $((new - old)) -gt 5 ]; then
+ s=1
+ echo "InUse changed from $old to $new, leaking $((new - old)) GEOMs"
+else
+ s=0
+fi
+exit $s
diff --git a/tools/test/stress2/misc/geomleak2.sh b/tools/test/stress2/misc/geomleak2.sh
new file mode 100755
index 000000000000..19b6f03d6333
--- /dev/null
+++ b/tools/test/stress2/misc/geomleak2.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+old=`vmstat -m | awk '/GEOM/{print $2}'`
+set -e
+mount | grep -q "on $mntpoint " && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+newfs -U /dev/md$mdstart > /dev/null
+set +e
+start=` date +%s`
+while [ $((`date +%s` - start)) -lt 60 ]; do
+ mount /dev/md$mdstart $mntpoint
+ while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint
+ done
+done
+mdconfig -d -u $mdstart
+
+new=`vmstat -m | awk '/GEOM/{print $2}'`
+if [ $((new - old)) -gt 5 ]; then
+ s=1
+ vmstat -m | sed -n '1p;/GEOM/p'
+else
+ s=0
+fi
+exit $s
diff --git a/tools/test/stress2/misc/getrandom.sh b/tools/test/stress2/misc/getrandom.sh
new file mode 100755
index 000000000000..fe30a688decb
--- /dev/null
+++ b/tools/test/stress2/misc/getrandom.sh
@@ -0,0 +1,142 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# getrandom() non threaded test
+# Reset seen on i386
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -m` = "i386" ] || exit 0
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/getrandom.c
+mycc -o getrandom -Wall -Wextra -O0 -g getrandom.c || exit 1
+rm -f getrandom.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+set +e
+
+(cd $odir/../testcases/swap; ./swap -t 5m -i 40 -l 100) &
+cd $mntpoint
+timeout 5m limits -c 0 $dir/getrandom
+s=0
+while pgrep -q swap; do pkill swap; done
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/getrandom
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/random.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#define PARALLEL 50
+#define RUNTIME (5 * 60)
+#define SYNC 0
+
+static void
+test(void)
+{
+ time_t start;
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ alarm(65);
+ start = time(NULL);
+ while ((time(NULL) - start) < 60)
+ getrandom((void *)arc4random(), arc4random(), arc4random() & 3);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/getrandom2.sh b/tools/test/stress2/misc/getrandom2.sh
new file mode 100755
index 000000000000..941ffb737a6d
--- /dev/null
+++ b/tools/test/stress2/misc/getrandom2.sh
@@ -0,0 +1,188 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+# getrandom(2) DoS scenario.
+
+# panic: pmap_growkernel: no memory to grow kernel
+# cpuid = 8
+# time = 1582102582
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe03e6992450
+# vpanic() at vpanic+0x185/frame 0xfffffe03e69924b0
+# panic() at panic+0x43/frame 0xfffffe03e6992510
+# pmap_growkernel() at pmap_growkernel+0x2d4/frame 0xfffffe03e6992550
+# vm_map_insert() at vm_map_insert+0x296/frame 0xfffffe03e69925f0
+# vm_map_find() at vm_map_find+0x617/frame 0xfffffe03e69926d0
+# kva_import() at kva_import+0x3c/frame 0xfffffe03e6992710
+# vmem_try_fetch() at vmem_try_fetch+0xde/frame 0xfffffe03e6992760
+# vmem_xalloc() at vmem_xalloc+0x4bb/frame 0xfffffe03e69927e0
+# kva_import_domain() at kva_import_domain+0x36/frame 0xfffffe03e6992810
+# vmem_try_fetch() at vmem_try_fetch+0xde/frame 0xfffffe03e6992860
+# vmem_xalloc() at vmem_xalloc+0x4bb/frame 0xfffffe03e69928e0
+# vmem_alloc() at vmem_alloc+0x8a/frame 0xfffffe03e6992930
+# kmem_malloc_domainset() at kmem_malloc_domainset+0x92/frame 0xfffffe03e69929a0
+# malloc() at malloc+0x162/frame 0xfffffe03e69929f0
+# read_random_uio() at read_random_uio+0xa5/frame 0xfffffe03e6992a40
+# sys_getrandom() at sys_getrandom+0x7b/frame 0xfffffe03e6992ac0
+# amd64_syscall() at amd64_syscall+0x183/frame 0xfffffe03e6992bf0
+# fast_syscall_common() at fast_syscall_common+0x101/frame 0xfffffe03e6992bf0
+# --- syscall (563, FreeBSD ELF64, sys_getrandom), rip = 0x80041899a, rsp = 0x7ffffffc3cb8, rbp = 0x7ffffffc3cd0 ---
+# KDB: enter: panic
+# [ thread pid 12095 tid 186584 ]
+# Stopped at kdb_enter+0x37: movq $0,0x1084916(%rip)
+# db> x/s version
+# version: FreeBSD 13.0-CURRENT #0 r358094: Wed Feb 19 06:25:16 CET 2020\012 pho@t2.osted.lan:/usr/src/sys/amd64/compile/PHO\012
+# db>
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/getrandom2.c
+mycc -o getrandom2 -Wall -Wextra -O0 -g getrandom2.c || exit 1
+rm -f getrandom2.c
+cd $odir
+
+cd /tmp
+$dir/getrandom2
+s=$?
+[ -f getrandom2.core -a $s -eq 0 ] &&
+ { ls -l getrandom2.core; s=1; }
+cd $odir
+
+rm -rf $dir/getrandom2
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/random.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdatomic.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static size_t mx;
+static _Atomic(int) *share;
+static int parallel;
+static char *bp;
+
+#define PARALLEL 40000 /* Arbitrary cap */
+#define SYNC 0
+
+static void
+test(void)
+{
+ int i;
+
+ alarm(180);
+ (void)atomic_fetch_add(&share[SYNC], 1);
+ while (atomic_load(&share[SYNC]) != parallel)
+ usleep(200000);
+ for (i = 0; i < 10; i++)
+ getrandom(bp, mx, 0);
+// close(66);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t *pids;
+ struct rlimit rlp;
+ size_t len;
+ size_t f, vsz;
+ u_int pages;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ if (getrlimit(RLIMIT_NPROC, &rlp) < 0)
+ err(1, "getrlimit");
+ parallel = rlp.rlim_cur / 100 * 80;
+ if (parallel > PARALLEL)
+ parallel = PARALLEL;
+ pids = calloc(parallel, sizeof(pid_t));
+
+ vsz = sizeof(pages);
+ if (sysctlbyname("vm.stats.vm.v_free_count", &pages, &vsz, NULL, 0) != 0)
+ err(1, "sysctl(vm.stats.vm.v_free_count)");
+ f = pages;
+ f *= PAGE_SIZE;
+
+ if (getrlimit(RLIMIT_DATA, &rlp) < 0)
+ err(1,"getrlimit");
+ mx = rlp.rlim_cur;
+ if (mx > f / parallel)
+ mx = f / parallel;
+ if ((bp = mmap(NULL, mx, PROT_READ | PROT_WRITE, MAP_ANON, -1,
+ 0)) == MAP_FAILED)
+ err(1, "mmap");
+ for (;;) {
+ if (getrandom(bp, mx, 0) != -1)
+ break;
+ mx = mx / 2;
+ }
+ printf("Max getrandom() buffer size is %zu, %d threads\n", mx,
+ parallel);
+ for (i = 0; i < parallel; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < parallel; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/gjournal.sh b/tools/test/stress2/misc/gjournal.sh
new file mode 100755
index 000000000000..7ccc7a70760e
--- /dev/null
+++ b/tools/test/stress2/misc/gjournal.sh
@@ -0,0 +1,76 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# Deadlock scenario based on kern/154228, fixed in r217880.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+size="2g"
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+[ `swapinfo -k | tail -1 | awk '{print int($4/1024/1024)}'` -lt \
+ ${size%g} ] && exit 0
+m=$((mdstart + 1))
+mp2=${mntpoint}$m
+mount | grep $mp2 | grep -q /dev/md && umount -f $mp2
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+[ -c /dev/md$m ] && mdconfig -d -u $m
+mkdir -p $mp2
+mdconfig -a -t swap -s $size -u $mdstart || exit 1
+
+gjournal load
+gjournal label -s 512m md$mdstart
+sleep .5
+newfs -J /dev/md$mdstart.journal > /dev/null
+mount -o async /dev/md$mdstart.journal $mntpoint
+
+here=`pwd`
+cd $mntpoint
+dd if=/dev/zero of=image bs=1m count=1k status=none
+mdconfig -a -t vnode -f image -u $m
+bsdlabel -w md$m auto
+newfs md${m}$part > /dev/null
+mount /dev/md${m}$part $mp2
+# dd will suspend in wdrain
+echo "Expect \"$mp2: write failed, filesystem is full\""
+dd if=/dev/zero of=$mp2/zero bs=1M > /dev/null 2>&1
+while mount | grep $mp2 | grep -q /dev/md; do
+ umount $mp2 || sleep 1
+done
+mdconfig -d -u $m
+cd $here
+
+gjournal sync
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+gjournal stop md$mdstart
+gjournal unload
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/gjournal2.sh b/tools/test/stress2/misc/gjournal2.sh
new file mode 100755
index 000000000000..48d45dc4fab5
--- /dev/null
+++ b/tools/test/stress2/misc/gjournal2.sh
@@ -0,0 +1,63 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# File system corruption seen
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+size="5g"
+jsize="3g"
+[ $((`sysctl -n hw.usermem` / 1024 / 1024 / 1024)) -le ${size%g} ] && exit 0
+[ `swapinfo -k | tail -1 | awk '{print int($4/1024/1024)}'` -lt \
+ ${size%g} ] && exit 0
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s $size -u $mdstart || exit 1
+
+gjournal load
+gjournal label -s $jsize md$mdstart
+sleep .5
+newfs -J /dev/md$mdstart.journal > /dev/null
+mount -o async /dev/md$mdstart.journal $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1
+
+gjournal sync
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+gjournal stop md$mdstart
+gjournal unload
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/gjournal3.sh b/tools/test/stress2/misc/gjournal3.sh
new file mode 100755
index 000000000000..a2ed7f57098c
--- /dev/null
+++ b/tools/test/stress2/misc/gjournal3.sh
@@ -0,0 +1,85 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# Deadlock seen: http://people.freebsd.org/~pho/stress/log/gjournal3.txt
+# Fixed in r244925
+
+# panic: Bio not on queue
+# https://people.freebsd.org/~pho/stress/log/gjournal3-2.txt
+
+# kib@ wrote:
+# gjournal is good for exposing the suspension problems. The frequency
+# of the suspensions called from the gjournal is not achievable by other
+# methods, so the tests allow to uncover the problems. More, the gjournal
+# only establishes the suspension, without snapshotting, which also
+# makes it easier to see the issues.
+
+# gjournal / ffs snapshot suspension deadlock:
+# https://people.freebsd.org/~pho/stress/log/gjournal3-4.txt
+# Originally reported as kern/164252.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+size="12g"
+jsize="8g"
+[ $((`sysctl -n hw.usermem` / 1024 / 1024 / 1024)) -le ${size%g} ] && exit 0
+[ `swapinfo -k | tail -1 | awk '{print int($4/1024/1024)}'` -lt \
+ ${size%g} ] && exit 0
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/mdmd$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s $size -u $mdstart || exit 1
+
+gjournal load
+gjournal label -s $jsize md$mdstart
+sleep .5
+newfs -J /dev/md$mdstart.journal > /dev/null
+mount -o async /dev/md$mdstart.journal $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ../testcases/rw
+ ./rw -t 2m -i 10 -l 100 > /dev/null 2>&1' &
+while kill -0 $! 2>/dev/null; do
+ mksnap_ffs $mntpoint $mntpoint/.snap/snap
+ sleep .2
+ rm -f $mntpoint/.snap/snap
+done
+wait
+
+gjournal sync
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+gjournal stop md$mdstart
+gjournal unload
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/gjournal4.sh b/tools/test/stress2/misc/gjournal4.sh
new file mode 100755
index 000000000000..80dbcafba904
--- /dev/null
+++ b/tools/test/stress2/misc/gjournal4.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# Experiment with a sparse MD disk.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `sysctl -n vm.swap_total` -eq 0 ] && exit 0
+[ `sysctl -n hw.physmem` -lt $(( 4 * 1024 * 1024 * 1024)) ] && exit 0
+
+. ../default.cfg
+
+size="100g"
+jsize="10g"
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s $size -u $mdstart || exit 1
+
+gjournal load
+gjournal label -s $jsize md$mdstart
+sleep .5
+newfs -J /dev/md$mdstart.journal > /dev/null
+mount -o async /dev/md$mdstart.journal $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+export CTRLDIR=$mntpoint/stressX.control
+export swapINCARNATIONS=5
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1
+
+gjournal sync
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/md$mdstart.journal && s=0 || s=$?
+gjournal stop md$mdstart
+gjournal unload
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/gnop.sh b/tools/test/stress2/misc/gnop.sh
new file mode 100755
index 000000000000..216bcf481ea0
--- /dev/null
+++ b/tools/test/stress2/misc/gnop.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Test with different sector size using gnop(8).
+# Out of VM seen:
+# https://people.freebsd.org/~pho/stress/log/gnop.txt
+
+flag=/tmp/gnop.sh.flag
+test() {
+ . ../default.cfg
+
+ start=`date +%s`
+ set -e
+ mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+ [ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+ mdconfig -a -t swap -s 2g -u $mdstart
+ gnop create -S $1 /dev/md$mdstart
+ newfs $newfs_flags /dev/md$mdstart.nop > /dev/null
+ mount /dev/md$mdstart.nop $mntpoint
+ chmod 777 $mntpoint
+ set +e
+
+ export runRUNTIME=4m
+ export RUNDIR=$mntpoint/stressX
+
+ su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1
+
+ while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ done
+ checkfs /dev/md$mdstart.nop || touch $flag
+ gnop destroy /dev/md$mdstart.nop
+ mdconfig -d -u $mdstart
+ t=`date +%s`
+ echo "Elapsed `date -u -j -f '%s' '+%H:%M' $((t - start))`"
+}
+
+kldstat | grep -q geom_nop || { gnop load 2>/dev/null || exit 0 &&
+ notloaded=1; }
+gnop status || exit 1
+
+for i in 1k 2k 4k 8k; do
+ test $i
+done
+
+[ $notloaded ] && gnop unload
+[ -f $flag ] && s=1 || s=0
+rm -f $flag
+exit $s
diff --git a/tools/test/stress2/misc/gnop10.sh b/tools/test/stress2/misc/gnop10.sh
new file mode 100755
index 000000000000..80ddc2fe1216
--- /dev/null
+++ b/tools/test/stress2/misc/gnop10.sh
@@ -0,0 +1,139 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# fsck test with forced unmount of a SUJ FS.
+# Variation of gnop8.sh by Kirk McKusick <mckusick@mckusick.com>
+
+# Copy of gnop9.sh. Uses SU instead of SUJ.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+# Check for lingering threads from the last run
+pgrep -x mount && { pgrep -x mount | xargs ps -lp; exit 1; }
+../tools/killall.sh || exit 1
+
+fsck=/sbin/fsck_ffs
+fsck_loops=10
+exp=/sbin/fsck_ffs.exp # Experimental version
+log=/tmp/gnop10.log
+[ -f $exp ] && { echo "Using $exp"; fsck=$exp; }
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 5g -u $mdstart || exit 1
+md=md$mdstart
+newfs -U /dev/$md > /dev/null 2>&1 || exit 1
+
+export LOAD=80
+export rwLOAD=80
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+export CTRLDIR=$mntpoint/stressX.control
+export MAXSWAPPCT=80
+export TESTPROGS='
+testcases/lockf2/lockf2
+testcases/symlink/symlink
+testcases/openat/openat
+testcases/socket/socket
+testcases/rw/rw
+testcases/fts/fts
+testcases/link/link
+testcases/lockf/lockf
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/mkfifo/mkfifo
+testcases/dirnprename/dirnprename
+testcases/dirrename/dirrename
+'
+
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export KBLOCKS=$(($1 / 10 * 7))
+export INODES=$(($2 / 10 * 7))
+
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 600 ]; do
+ gnop create /dev/$md || exit 1
+ mount /dev/$md.nop $mntpoint || exit 1
+ mkdir -p $RUNDIR $CTRLDIR
+ chmod 777 $RUNDIR $CTRLDIR
+
+ # start your favorite I/O test here
+ rm -rf /tmp/stressX.control
+ (cd $RUNDIR && find . -delete)
+
+ su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' &
+
+ # after some number of seconds
+ sleep `jot -r 1 30 90`
+ gnop destroy -f /dev/$md.nop
+ ../tools/killall.sh || exit 1
+ wait
+
+ # Wait until forcible unmount, may be up to about 30 seconds,
+ # but typically very quick if I/O is in progress
+ s=`date +%s`
+ n=0
+ while mount | grep -q "on $mntpoint "; do
+ [ $n -eq 0 ] && /bin/echo -n "Waiting for $mntpoint to force umount ..."
+ n=$((n + 1))
+ sleep 2
+ if [ $((`date +%s` - s)) -ge 180 ]; then
+ echo "Giving up on waiting for umount of $mntpoint"
+ umount $mntpoint || umount -f $mntpoint
+ break
+ fi
+ done
+ [ $n -ne 0 ] && echo
+
+ # first fsck will attempt journal recovery
+ $fsck -fy /dev/$md > $log 2>&1
+
+ # The second fsck will do traditional check for any errors
+ # from journal recovery
+
+ # There seems to be a gnop cache issue, which resolves by adding
+ # delays between each fsck run
+ for i in `jot $fsck_loops`; do
+ sleep $((i * i)) # gnop workaround
+ [ $i -ne 1 ] &&
+ echo "`date +%T` $fsck loop #$((i + 1))"
+ $fsck -fy /dev/$md > $log 2>&1
+ grep -Eq "IS CLEAN|MARKED CLEAN" $log && break
+ [ $i -eq $fsck_loops ] &&
+ { echo "$fsck did not mark FS as clean"; exit 1; }
+ [ $i -ne 1 ] && tail -3 $log
+ sync; sleep 5; sync; sleep 5
+ done
+done
+echo "Final $fsck"
+sleep 3; # gnop workaround
+$fsck -fy /dev/$md > $log || { tail -5 $log; exit 1; }
+mdconfig -d -u ${md#md}
+rm -f $log
+exit 0
diff --git a/tools/test/stress2/misc/gnop2.sh b/tools/test/stress2/misc/gnop2.sh
new file mode 100755
index 000000000000..d38754d58456
--- /dev/null
+++ b/tools/test/stress2/misc/gnop2.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "panic: vnode_pager_generic_getpages: sector size 8192 too large" seen
+# with an 8k sector size:
+# https://people.freebsd.org/~pho/stress/log/gnop2.txt
+# Fixed by r307626
+
+. ../default.cfg
+
+kldstat | grep -q geom_nop || { gnop load 2>/dev/null || exit 0 &&
+ notloaded=1; }
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/mmap5.sh > $dir/gnop2.c
+mycc -o gnop2 -Wall -Wextra gnop2.c || exit 1
+rm -f gnop2.c
+cd $odir
+
+test() {
+ . ../default.cfg
+
+ mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+ [ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+ set -e
+ mdconfig -a -t swap -s 2g -u $mdstart
+ gnop create -S $1 /dev/md$mdstart
+ newfs $newfs_flags /dev/md$mdstart.nop > /dev/null
+ mount /dev/md$mdstart.nop $mntpoint
+ chmod 777 $mntpoint
+ set +e
+
+ dd if=/dev/zero of=$mntpoint/file bs=1k count=333 status=none
+ /tmp/gnop2 $mntpoint/file
+
+ while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ done
+ gnop destroy /dev/md$mdstart.nop
+ mdconfig -d -u $mdstart
+}
+
+gnop status || exit 1
+
+for i in 1k 2k 4k 8k; do
+ test $i
+done
+
+[ $notloaded ] && gnop unload
+rm -f /tmp/gnop2
+exit 0
diff --git a/tools/test/stress2/misc/gnop3.sh b/tools/test/stress2/misc/gnop3.sh
new file mode 100755
index 000000000000..3d22e4a74331
--- /dev/null
+++ b/tools/test/stress2/misc/gnop3.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# A 8k sector size test.
+# "panic: run 8 0xfffff80ff3d1e040 invalid" seen during fix development.
+
+# OOM: https://people.freebsd.org/~pho/stress/log/gnop3.txt
+
+. ../default.cfg
+
+# OOM seen with RAM == 32g
+[ $((`sysctl -n hw.usermem` / 1024 / 1024 / 1024)) -lt 32 ] && exit 0
+[ $((`sysctl -n vm.swap_total` / 1024 / 1024 / 1024)) -lt 9 ] && exit 0
+
+kldstat | grep -q geom_nop || { gnop load 2>/dev/null || exit 0 &&
+ notloaded=1; }
+gnop status || exit 1
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+set -e
+mdconfig -a -t swap -s 8g -u $mdstart
+gnop create -S 8k /dev/md$mdstart
+newfs $newfs_flags /dev/md$mdstart.nop > /dev/null
+mount /dev/md$mdstart.nop $mntpoint
+chmod 777 $mntpoint
+set +e
+
+cp -a ../../stress2 $mntpoint
+here=`pwd`
+cd $mntpoint/stress2/misc
+
+export runRUNTIME=20m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg'
+
+cd $here
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+gnop destroy /dev/md$mdstart.nop
+mdconfig -d -u $mdstart
+[ $notloaded ] && gnop unload
+exit 0
diff --git a/tools/test/stress2/misc/gnop4.sh b/tools/test/stress2/misc/gnop4.sh
new file mode 100755
index 000000000000..2e4cc1a5df35
--- /dev/null
+++ b/tools/test/stress2/misc/gnop4.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# A 8k sector size test using buildworld.
+
+# "panic: DI already started" seen:
+# https://people.freebsd.org/~pho/stress/log/kostik1017.txt
+# Fixed by r322175
+
+. ../default.cfg
+
+gigs=9
+[ $((`sysctl -n vm.swap_total` / 1024 / 1024 / 1024)) -lt $gigs ] && exit 0
+[ -d /usr/src/sys ] || exit 0
+
+kldstat | grep -q geom_nop || { gnop load 2>/dev/null || exit 0 &&
+ notloaded=1; }
+gnop status || exit 1
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+set -e
+mdconfig -a -t swap -s ${gigs}g -u $mdstart
+gnop create -S 8k /dev/md$mdstart
+newfs $newfs_flags /dev/md$mdstart.nop > /dev/null
+mount /dev/md$mdstart.nop $mntpoint
+chmod 777 $mntpoint
+set +e
+
+start=`date '+%s'`
+(cd /usr; tar --exclude compile -cf - src) | (cd $mntpoint; tar xf -)
+
+cd $mntpoint/src
+export MAKEOBJDIRPREFIX=$mntpoint/obj
+
+p=$((`sysctl -n hw.ncpu`+ 1))
+make -i -j $p buildworld DESTDIR=$mntpoint TARGET=amd64 TARGET_ARCH=amd64 \
+ > /dev/null &
+e=$((`date '+%s'` - start))
+sleep $((15 * 60 - e))
+kill $!
+wait
+
+cd /
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+gnop destroy /dev/md$mdstart.nop
+mdconfig -d -u $mdstart
+[ $notloaded ] && gnop unload
+exit 0
diff --git a/tools/test/stress2/misc/gnop5.sh b/tools/test/stress2/misc/gnop5.sh
new file mode 100755
index 000000000000..2acd0244d80a
--- /dev/null
+++ b/tools/test/stress2/misc/gnop5.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Test newfs with different sector size.
+# newfs(8) issue fixed by r323157.
+# mount(8) still fails with a sector size > 8k.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+kldstat | grep -q geom_nop || { gnop load 2>/dev/null || exit 0 &&
+ loaded=1; }
+gnop status > /dev/null || exit 1
+
+s=0
+set -e
+# Fails with a sector size > 8k.
+#for i in 1k 2k 4k 8k 16k 32k 64k; do
+for i in 1k 2k 4k 8k; do
+ echo $i
+ mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+ [ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+ mdconfig -a -t swap -s 2g -u $mdstart
+ gnop create -S $i /dev/md$mdstart
+ newfs $newfs_flags /dev/md$mdstart.nop > /dev/null ||
+ { s=1; continue; }
+ mount /dev/md$mdstart.nop $mntpoint ||
+ { gnop destroy /dev/md$mdstart.nop; mdconfig -d -u $mdstart
+ s=1; break; }
+ chmod 777 $mntpoint
+
+ (cd $mntpoint; jot 100 | xargs touch)
+
+ while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ done
+ checkfs /dev/md$mdstart.nop || s=1
+ gnop destroy /dev/md$mdstart.nop
+ mdconfig -d -u $mdstart
+done
+
+[ $loaded ] && gnop unload
+exit $s
diff --git a/tools/test/stress2/misc/gnop6.sh b/tools/test/stress2/misc/gnop6.sh
new file mode 100755
index 000000000000..63301d8fe841
--- /dev/null
+++ b/tools/test/stress2/misc/gnop6.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Read and write delay
+# No problems seen
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+kldstat | grep -q geom_nop || { gnop load 2>/dev/null || exit 0 &&
+ notloaded=1; }
+gnop status || exit 1
+
+. ../default.cfg
+
+delay=`jot -r 1 1 10` # ms delay
+rprob=`jot -r 1 0 100` # read delay in %
+wprob=`jot -r 1 0 100` # write delay in %
+set -e
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart
+gnop create -d $delay -q $rprob -x $wprob /dev/md$mdstart
+newfs $newfs_flags /dev/md$mdstart.nop > /dev/null
+mount /dev/md$mdstart.nop $mntpoint
+chmod 777 $mntpoint
+set +e
+
+export runRUNTIME=5m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/md$mdstart.nop || s=1 && s=0
+gnop destroy /dev/md$mdstart.nop
+mdconfig -d -u $mdstart
+
+[ $notloaded ] && gnop unload
+exit $s
diff --git a/tools/test/stress2/misc/gnop7.sh b/tools/test/stress2/misc/gnop7.sh
new file mode 100755
index 000000000000..3f3118ff0fab
--- /dev/null
+++ b/tools/test/stress2/misc/gnop7.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Failed Disk Test.
+# "panic: witness_warn" seen in WiP kernel code.
+# https://people.freebsd.org/~pho/stress/log/kirk102.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+kldstat | grep -q geom_nop || { gnop load 2>/dev/null || exit 0 &&
+ notloaded=1; }
+gnop status || exit 1
+
+. ../default.cfg
+
+set -e
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart
+gnop create /dev/md$mdstart
+newfs $newfs_flags /dev/md$mdstart.nop > /dev/null
+mount /dev/md$mdstart.nop $mntpoint
+chmod 777 $mntpoint
+set +e
+
+export runRUNTIME=3m
+export RUNDIR=$mntpoint/stressX
+echo "Expect:
+ g_vfs_done():md10.nop[READ(offset=1077805056, length=32768)]error = 5
+ g_vfs_done():md10.nop[WRITE(offset=553680896, length=32768)]error = 5"
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1 &
+sleep `jot -r 1 60 120`
+gnop configure -e 5 -r 100 -w 100 /dev/md$mdstart.nop
+sleep 2
+gnop configure -e 0 -r 0 -w 0 /dev/md$mdstart.nop
+wait
+
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint && break
+ sleep 1
+ [ $((n += 1)) -le 10 ] && continue
+ echo "umount $mntpoint failed"
+ s=1
+ umount -f $mntpoint
+ break
+done
+fsck_ffs -Rfy /dev/md$mdstart.nop || s=2
+gnop destroy /dev/md$mdstart.nop
+mdconfig -d -u $mdstart
+
+[ $notloaded ] && gnop unload
+exit $s
diff --git a/tools/test/stress2/misc/gnop8.sh b/tools/test/stress2/misc/gnop8.sh
new file mode 100755
index 000000000000..6916c14e41f4
--- /dev/null
+++ b/tools/test/stress2/misc/gnop8.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Kirk McKusick <mckusick@mckusick.com>
+#
+# 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 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.
+#
+
+# 'panic: Lock (lockmgr) ufs not locked @ kern/kern_lock.c:1271' seen:
+# https://people.freebsd.org/~pho/stress/log/gnop8.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+fsck=/sbin/fsck_ffs
+exp=/sbin/fsck_ffs.exp # Experimental version
+[ -f $exp ] && { echo "Using $exp"; fsck=$exp; }
+mdconfig -a -t swap -s 5g -u $mdstart || exit 1
+md=md$mdstart
+newfs -j /dev/$md || exit 1
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 120 ]; do
+ gnop create /dev/$md || exit 1
+ mount /dev/$md.nop /mnt || exit 1
+
+ # start your favorite I/O test here
+ cp -rp /[a-l]* /[n-z]* /mnt &
+
+ # after some number of seconds
+ sleep 1
+ gnop destroy -f /dev/$md.nop
+ kill $!
+
+ # wait until forcible unmount, may be up to about 30 seconds,
+ # but typically very quick if I/O is in progress
+ while (a=`mount | egrep /mnt`) do sleep 1; done
+
+ # first fsck will attempt journal recovery
+ $fsck -d -y /dev/$md
+
+ # second fsck will do traditional fsck to check for any errors
+ # from journal recovery
+ $fsck -d -y /dev/$md
+ wait
+done
+mdconfig -d -u ${md#md}
+exit 0
diff --git a/tools/test/stress2/misc/gnop9.sh b/tools/test/stress2/misc/gnop9.sh
new file mode 100755
index 000000000000..0389391398f3
--- /dev/null
+++ b/tools/test/stress2/misc/gnop9.sh
@@ -0,0 +1,139 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# fsck test with forced unmount of a SUJ FS.
+# Variation of gnop8.sh by Kirk McKusick <mckusick@mckusick.com>
+
+# https://people.freebsd.org/~pho/stress/log/gnop9.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+# Check for lingering threads from the last run
+pgrep -x mount && { pgrep -x mount | xargs ps -lp; exit 1; }
+../tools/killall.sh || exit 1
+
+fsck=/sbin/fsck_ffs
+fsck_loops=10
+exp=/sbin/fsck_ffs.exp # Experimental version
+log=/tmp/gnop9.log
+[ -f $exp ] && { echo "Using $exp"; fsck=$exp; }
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 5g -u $mdstart || exit 1
+md=md$mdstart
+newfs -j /dev/$md > /dev/null 2>&1 || exit 1
+
+export LOAD=80
+export rwLOAD=80
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+export CTRLDIR=$mntpoint/stressX.control
+export MAXSWAPPCT=80
+export TESTPROGS='
+testcases/lockf2/lockf2
+testcases/symlink/symlink
+testcases/openat/openat
+testcases/socket/socket
+testcases/rw/rw
+testcases/fts/fts
+testcases/link/link
+testcases/lockf/lockf
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/mkfifo/mkfifo
+testcases/dirnprename/dirnprename
+testcases/dirrename/dirrename
+'
+
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export KBLOCKS=$(($1 / 10 * 7))
+export INODES=$(($2 / 10 * 7))
+
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 600 ]; do
+ gnop create /dev/$md || exit 1
+ mount /dev/$md.nop $mntpoint || exit 1
+ mkdir -p $RUNDIR $CTRLDIR
+ chmod 777 $RUNDIR $CTRLDIR
+
+ # start your favorite I/O test here
+ rm -rf /tmp/stressX.control
+ (cd $RUNDIR && find . -delete)
+
+ su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' &
+
+ # after some number of seconds
+ sleep `jot -r 1 30 90`
+ gnop destroy -f /dev/$md.nop
+ ../tools/killall.sh || exit 1
+ wait
+
+ # Wait until forcible unmount, may be up to about 30 seconds,
+ # but typically very quick if I/O is in progress
+ s=`date +%s`
+ n=0
+ while mount | grep -q "on $mntpoint "; do
+ [ $n -eq 0 ] && /bin/echo -n "Waiting for $mntpoint to force umount ..."
+ n=$((n + 1))
+ sleep 2
+ if [ $((`date +%s` - s)) -ge 180 ]; then
+ echo "Giving up on waiting for umount of $mntpoint"
+ umount $mntpoint || umount -f $mntpoint
+ break
+ fi
+ done
+ [ $n -ne 0 ] && echo
+
+ # first fsck will attempt journal recovery
+ $fsck -fy /dev/$md > $log 2>&1
+
+ # The second fsck will do traditional check for any errors
+ # from journal recovery
+
+ # There seems to be a gnop cache issue, which resolves by adding
+ # delays between each fsck run
+ for i in `jot $fsck_loops`; do
+ sleep $((i * i)) # gnop workaround
+ [ $i -ne 1 ] &&
+ echo "`date +%T` $fsck loop #$((i + 1))"
+ $fsck -fy /dev/$md > $log 2>&1
+ grep -Eq "IS CLEAN|MARKED CLEAN" $log && break
+ [ $i -eq $fsck_loops ] &&
+ { echo "$fsck did not mark FS as clean"; exit 1; }
+ [ $i -ne 1 ] && tail -3 $log
+ sync; sleep 5; sync; sleep 5
+ done
+done
+echo "Final $fsck"
+sleep 3; # gnop workaround
+$fsck -fy /dev/$md > $log || { tail -5 $log; exit 1; }
+mdconfig -d -u ${md#md}
+rm -f $log
+exit 0
diff --git a/tools/test/stress2/misc/gpt.sh b/tools/test/stress2/misc/gpt.sh
new file mode 100755
index 000000000000..51b4ba39afda
--- /dev/null
+++ b/tools/test/stress2/misc/gpt.sh
@@ -0,0 +1,51 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Bug 237269 - panic in glabel (g_label_destroy) stop after resizing GPT
+# partition.
+# "panic: vm_fault_hold: fault on nofault entry, addr: 0 from
+# g_slice_spoiled+0x7"
+# Test scenario by andrew@tao11.riddles.org.uk
+
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+md_unit=$(mdconfig -t swap -s 30MB)
+geom part create -s GPT "$md_unit"
+geom part add -s 10M -t linux-swap -l tst0 "$md_unit"
+geom part resize -i 1 -s 20M "$md_unit"
+
+# at this point "glabel status" shows two gpt/tst0 entries,
+# one of which has no consumer; trying to correct this causes
+# a panic:
+
+glabel stop gpt/tst0
+glabel stop gpt/tst0 # BOOM
+
+mdconfig -d -u $md_unit
+
+exit 0
diff --git a/tools/test/stress2/misc/graid0.sh b/tools/test/stress2/misc/graid0.sh
new file mode 100755
index 000000000000..5887d50f5739
--- /dev/null
+++ b/tools/test/stress2/misc/graid0.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Protection fault seen:
+# https://people.freebsd.org/~pho/stress/log/graid0.txt
+# Fixed by r327721-23
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+md1=$mdstart
+md2=$((mdstart + 1))
+md3=$((mdstart + 2))
+
+size=1g
+[ $((`sysctl -n hw.usermem` / 1024 / 1024 / 1024)) -le 4 ] &&
+ size=512m
+
+for u in $md1 $md2 $md3; do
+ mdconfig -l | grep -q md$u && mdconfig -d -u $u
+ mdconfig -a -t swap -s $size -u $u
+done
+
+gstripe load > /dev/null 2>&1 && unload=1
+gstripe label -v -s 131072 data /dev/md$md1 /dev/md$md2 /dev/md$md3 > \
+ /dev/null || exit 1
+[ -c /dev/stripe/data ] || exit 1
+newfs $newfs_flags /dev/stripe/data > /dev/null
+mount /dev/stripe/data $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg'
+while mount | grep $mntpoint | grep -q /stripe/; do
+ umount $mntpoint || sleep 1
+done
+gstripe stop data && s=0 || s=1
+[ $unload ] && gstripe unload
+
+for u in $md3 $md2 $md1; do
+ mdconfig -d -u $u
+done
+exit $s
diff --git a/tools/test/stress2/misc/graid1.sh b/tools/test/stress2/misc/graid1.sh
new file mode 100755
index 000000000000..eea4aa80b97b
--- /dev/null
+++ b/tools/test/stress2/misc/graid1.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# No problems seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+md1=$mdstart
+md2=$((mdstart + 1))
+md3=$((mdstart + 2))
+
+s=0
+size=1g
+[ $((`sysctl -n hw.usermem` / 1024 / 1024 / 1024)) -le 4 ] &&
+ size=512m
+
+for u in $md1 $md2 $md3; do
+ mdconfig -l | grep -q md$u && mdconfig -d -u $u
+ mdconfig -a -t swap -s $size -u $u
+done
+
+gmirror load > /dev/null 2>&1 && unload=1
+old=`sysctl -n kern.geom.mirror.debug`
+sysctl kern.geom.mirror.debug=-1 | grep -q -- -1 ||
+ sysctl kern.geom.mirror.debug=$old > /dev/null
+gmirror label -v -b split -s 2048 data /dev/md$md1 /dev/md$md2 \
+ /dev/md$md3 > /dev/null || exit 1
+[ "`sysctl -in kern.geom.mirror.launch_mirror_before_timeout`" = "0" ] &&
+ sleep $((`sysctl -n kern.geom.mirror.timeout` + 1))
+[ -c /dev/mirror/data ] || exit 1
+newfs $newfs_flags /dev/mirror/data > /dev/null
+mount /dev/mirror/data $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg'
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+checkfs /dev/mirror/data || s=1
+gmirror stop data || s=2
+gmirror destroy data 2>/dev/null
+[ $unload ] && gmirror unload
+
+for u in $md3 $md2 $md1; do
+ mdconfig -d -u $u || s=3
+done
+exit $s
diff --git a/tools/test/stress2/misc/graid1_10.sh b/tools/test/stress2/misc/graid1_10.sh
new file mode 100755
index 000000000000..51a5e0260914
--- /dev/null
+++ b/tools/test/stress2/misc/graid1_10.sh
@@ -0,0 +1,96 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# Read error handling for synchronization requests
+# Test scenario by Mark Johnston <markj@FreeBSD.org>
+# Fixed by r327779
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+md1=$mdstart
+md2=$((mdstart + 1))
+
+s=0
+size=1g
+[ $((`sysctl -n hw.usermem` / 1024 / 1024 / 1024)) -le 4 ] &&
+ size=512m
+
+for u in $md1 $md2; do
+ mdconfig -l | grep -q md$u && mdconfig -d -u $u
+ mdconfig -a -t swap -s $size -u $u
+done
+
+gmirror load > /dev/null 2>&1 && unload=1
+old=`sysctl -n kern.geom.mirror.debug`
+sysctl kern.geom.mirror.debug=-1 | grep -q -- -1 ||
+ sysctl kern.geom.mirror.debug=$old > /dev/null
+gmirror label -v -b split -s 2048 test /dev/md$md1 /dev/md$md2 \
+ > /dev/null || exit 1
+[ "`sysctl -in kern.geom.mirror.launch_mirror_before_timeout`" = "0" ] &&
+ sleep $((`sysctl -n kern.geom.mirror.timeout` + 1))
+[ -c /dev/mirror/test ] || exit 1
+newfs /dev/mirror/test > /dev/null
+mount /dev/mirror/test $mntpoint
+
+gpid=`pgrep -fS "g_mirror test"`
+s=0
+start=`date +%s`
+roid=debug.fail_point.g_mirror_regular_request_read
+while [ $((`date +%s` - start)) -lt 120 ]; do
+ gmirror forget test md$md2 2>/dev/null
+ gmirror remove test md$md2 2>/dev/null
+ sysctl $roid="1%return(5)[pid $gpid]" > /dev/null
+ gmirror insert test md$md2
+ sleep 10
+ sysctl $roid="off" > /dev/null
+ n=0
+ while gmirror status test | grep -q DEGRADED; do
+ sleep 2
+ [ $((n += 1)) -gt 30 ] && { s=1; break 2; }
+ done
+ sysctl $roid="off" > /dev/null
+done
+[ $s -ne 0 ] && { echo "Timed out"; gmirror status test; }
+
+for i in `jot 12`; do
+ gmirror status test | grep -q SYNCHRONIZING || break
+ sleep 10
+done
+while mount | grep $mntpoint | grep -q /mirror/; do
+ umount $mntpoint || sleep 1
+done
+gmirror stop test || s=2
+gmirror destroy test 2>/dev/null
+[ $unload ] && gmirror unload
+
+for u in $md2 $md1; do
+ mdconfig -d -u $u || s=3
+done
+exit $s
diff --git a/tools/test/stress2/misc/graid1_2.sh b/tools/test/stress2/misc/graid1_2.sh
new file mode 100755
index 000000000000..94d43d1646c5
--- /dev/null
+++ b/tools/test/stress2/misc/graid1_2.sh
@@ -0,0 +1,131 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Test scenario by Mark Johnston <markj@FreeBSD.org>
+# "physwr DL /tmp/graid1_2 /dev/mirror/test" seen.
+# Fixed by r307691.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/graid1_2.c
+mycc -o graid1_2 -Wall -Wextra -O0 -g graid1_2.c || exit 1
+rm -f graid1_2.c
+cd $odir
+
+gmirror load > /dev/null 2>&1 && unload=1
+[ -c /dev/mirror/test ] && { gmirror stop test; gmirror destroy test; }
+old=`sysctl -n kern.geom.mirror.debug`
+sysctl kern.geom.mirror.debug=-1 | grep -q -- -1 ||
+ sysctl kern.geom.mirror.debug=$old > /dev/null
+
+md1=$mdstart
+md2=$((mdstart + 1))
+s=0
+size=$((128 * 1024))
+
+for u in $md1 $md2; do
+ dd if=/dev/zero of=/tmp/graid1_2_di$u bs=$size count=1 status=none
+ [ -c /dev/md$u ] && mdconfig -d -u $u
+ mdconfig -a -t vnode -f /tmp/graid1_2_di$u -u $u
+done
+gmirror label test /dev/md$md1 /dev/md$md2 || exit 1
+[ "`sysctl -in kern.geom.mirror.launch_mirror_before_timeout`" = "0" ] &&
+ sleep $((`sysctl -n kern.geom.mirror.timeout` + 1))
+[ -c /dev/mirror/test ] || exit 1
+
+for i in `jot 150`; do /tmp/graid1_2 /dev/mirror/test; done &
+
+sleep 5
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 300 ]; do
+ gmirror rebuild test /dev/md$md1
+ sleep 2
+ n=0
+ while ps -lx | grep -v grep | grep graid1_2 | grep -q D; do
+ opid=$pid
+ pid=`pgrep graid1_2`
+ [ -z "$pid" -o "$pid" != "$opid" ] && n=0
+ sleep 1
+ n=$((n + 1))
+ if [ $n -gt 180 ]; then
+ echo FAIL
+ ps -lx | grep -v grep | grep graid1_2 | grep D
+ exit 1
+ fi
+ done
+done
+kill $! 2>/dev/null
+pkill graid1_2
+wait
+
+while mount | grep $mntpoint | grep -q /mirror/; do
+ umount $mntpoint || sleep 1
+done
+gmirror stop test || s=2
+[ $unload ] && gmirror unload
+
+for u in $md2 $md1; do
+ mdconfig -d -u $u || s=4
+done
+rm -f /tmp/graid1_2 /tmp/graid1_2_di*
+exit $s
+EOF
+/* Write last sector on disk */
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static char buf[512];
+
+int
+main(int argc __unused, char *argv[])
+{
+ time_t start;
+ int fd;
+
+ if ((fd = open(argv[1], O_RDWR)) == -1)
+ err(1, "open(%s)", argv[1]);
+ start = time(NULL);
+ while (time(NULL) - start < 2) {
+ if (lseek(fd, 254 * sizeof(buf), SEEK_SET) == -1)
+ err(1, "seek");
+ if (write(fd, buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write");
+ }
+ close(fd);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/graid1_3.sh b/tools/test/stress2/misc/graid1_3.sh
new file mode 100755
index 000000000000..088e395b1f2e
--- /dev/null
+++ b/tools/test/stress2/misc/graid1_3.sh
@@ -0,0 +1,93 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Test scenario by Mark Johnston <markj@FreeBSD.org>
+
+# Page fault seen:
+# https://people.freebsd.org/~pho/stress/log/graid1_3.txt
+
+. ../default.cfg
+
+gmirror load > /dev/null 2>&1 && unload=1
+[ -c /dev/mirror/markj-mirror ] &&
+ { gmirror stop markj-mirror; gmirror destroy markj-mirror; }
+old=`sysctl -n kern.geom.mirror.debug`
+sysctl kern.geom.mirror.debug=-1 | grep -q -- -1 ||
+ sysctl kern.geom.mirror.debug=$old > /dev/null
+u1=$mdstart
+u2=$((mdstart + 1))
+size=$((5 * 1024 * 1024))
+for u in $u1 $u2; do
+ dd if=/dev/zero of=/tmp/graid1_2_di$u bs=$size count=1 \
+ status=none
+ [ -c /dev/md$u ] && mdconfig -d -u $u
+ mdconfig -a -t vnode -f /tmp/graid1_2_di$u -u $u
+done
+set -e
+
+(
+gpart create -s GPT md$u1
+gpart create -s GPT md$u2
+gpart add -t freebsd-ufs -s 1M md$u1
+gpart add -t freebsd-ufs -s 1M md$u2
+) > /dev/null
+
+gmirror label markj-mirror md${u1}p1
+set +e
+
+while true; do
+ gmirror label markj-mirror md${u1}p1
+ gmirror destroy markj-mirror
+done 2>/dev/null &
+pid1=$!
+while true; do
+ gmirror insert markj-mirror md${u2}p1
+ gmirror remove markj-mirror md${u2}p1
+done 2>/dev/null &
+pid2=$!
+
+for i in `jot 60`; do
+ gmirror list markj-mirror
+ sleep 1
+done > /dev/null 2>&1
+sleep 60
+
+kill $pid1 $pid2
+wait
+sleep 1
+
+gmirror remove markj-mirror md${u2}p1 > /dev/null 2>&1
+gmirror destroy markj-mirror > /dev/null 2>&1
+
+mdconfig -d -u $u1 || exit 1
+mdconfig -d -u $u2 || exit 1
+rm -f /tmp/graid1_2_di*
+[ $unload ] && gmirror unload
+exit 0
diff --git a/tools/test/stress2/misc/graid1_4.sh b/tools/test/stress2/misc/graid1_4.sh
new file mode 100755
index 000000000000..9f410ef9a645
--- /dev/null
+++ b/tools/test/stress2/misc/graid1_4.sh
@@ -0,0 +1,97 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Non UFS SU gmirror stop -f test.
+
+# Page fault seen:
+# https://people.freebsd.org/~pho/stress/log/graid1_4.txt
+# Fixed in r316867
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+rm -f $diskimage*
+need=1024 # MB
+[ `df -k $(dirname $diskimage) | tail -1 | awk '{print int($4 / 1024)}'` -lt \
+ $need ] && printf "Need %d MB on %s.\n" $need `dirname $diskimage` && exit
+
+gmirror load > /dev/null 2>&1 && unload=1
+[ -c /dev/mirror/test ] && { gmirror stop test; gmirror destroy test; }
+old=`sysctl -n kern.geom.mirror.debug`
+sysctl kern.geom.mirror.debug=-1 | grep -q -- -1 ||
+ sysctl kern.geom.mirror.debug=$old > /dev/null
+
+md1=$mdstart
+md2=$((mdstart + 1))
+
+s=0
+for u in $md1 $md2; do
+ disk="$diskimage.$u"
+ dd if=/dev/zero of=$disk bs=1m count=512 status=none
+ [ -c /dev/md$u ] && mdconfig -d -u $u
+ mdconfig -a -t vnode -f $disk -u $u
+done
+
+gmirror label -v -b split -s 2048 test /dev/md$md1 /dev/md$md2 \
+ > /dev/null || exit 1
+[ "`sysctl -in kern.geom.mirror.launch_mirror_before_timeout`" = "0" ] &&
+ sleep $((`sysctl -n kern.geom.mirror.timeout` + 1))
+[ -c /dev/mirror/test ] || exit 1
+# Soft Updates issues with removal of backing media
+newfs /dev/mirror/test > /dev/null
+mount /dev/mirror/test $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' &
+
+while kill -0 $! > /dev/null 2>&1; do
+ sleep `jot -r 1 1 5`
+ gmirror remove test md$md2
+ sleep `jot -r 1 1 5`
+ gmirror insert test md$md2
+done
+wait
+
+gmirror stop -f test # Note the stop *before* umount
+
+while mount | grep $mntpoint | grep -q /mirror/; do
+ umount $mntpoint || sleep 1
+done
+gmirror stop test || s=1
+gmirror destroy test 2>/dev/null
+[ $unload ] && gmirror unload
+
+for u in $md2 $md1; do
+ mdconfig -d -u $u || s=3
+done
+rm -f $diskimage*
+exit $s
diff --git a/tools/test/stress2/misc/graid1_5.sh b/tools/test/stress2/misc/graid1_5.sh
new file mode 100755
index 000000000000..4200a64150b8
--- /dev/null
+++ b/tools/test/stress2/misc/graid1_5.sh
@@ -0,0 +1,116 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Mirror tests with gnop(8) errors introduced in 2 out of three partitions.
+
+# https://people.freebsd.org/~pho/stress/log/graid1_5-2.txt
+# Fixed by r327698
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+gmirror load > /dev/null 2>&1 && unload=1
+gmirror status > /dev/null || exit 0
+
+gnop load > /dev/null && unload2=1;
+gnop status > /dev/null || exit 0
+
+[ -c /dev/mirror/test ] && { gmirror stop test; gmirror destroy test; }
+old=`sysctl -n kern.geom.mirror.debug`
+sysctl kern.geom.mirror.debug=-1 | grep -q -- -1 ||
+ sysctl kern.geom.mirror.debug=$old > /dev/null
+
+u1=$mdstart
+u2=$((mdstart + 1))
+u3=$((mdstart + 2))
+s=0
+for u in $u1 $u2 $u3; do
+ [ -c /dev/md$u ] && mdconfig -d -u $u
+ mdconfig -a -t swap -s 341m -u $u
+ gpart create -s GPT md$u
+done > /dev/null
+
+set -e
+(
+gpart add -t freebsd-ufs -s 340m md$u1
+gpart add -t freebsd-ufs -s 340m md$u2
+gpart add -t freebsd-ufs -s 340m md$u3
+) > /dev/null
+gnop create md$u2
+gnop create md$u3
+gmirror label test md${u1}p1 md$u2.nopp1 md$u3.nopp1
+[ "`sysctl -in kern.geom.mirror.launch_mirror_before_timeout`" = "0" ] &&
+ sleep $((`sysctl -n kern.geom.mirror.timeout` + 1))
+[ -c /dev/mirror/test ] || exit 1
+
+newfs /dev/mirror/test > /dev/null
+mount /dev/mirror/test $mntpoint
+set +e
+chmod 777 $mntpoint
+
+export runRUNTIME=5m
+export RUNDIR=$mntpoint/stressX
+rm -rf /tmp/stressX.control
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1 &
+pid=$!
+
+gnop configure -r 0 -w 1 md$u2.nop
+gnop configure -r 0 -w 1 md$u3.nop
+while kill -0 $pid > /dev/null 2>&1; do
+ if ! gmirror status test | grep -q md$u2.nopp1; then
+ gmirror forget test
+ gmirror remove test md$u2.nopp1 2>/dev/null
+ gmirror insert test md$u2.nopp1 2>/dev/null
+ fi
+ if ! gmirror status test | grep -q md$u3.nopp1; then
+ gmirror forget test
+ gmirror remove test md$u3.nopp1 2>/dev/null
+ gmirror insert test md$u3.nopp1 2>/dev/null
+ fi
+ sleep 1
+done
+wait
+
+while mount | grep $mntpoint | grep -q /mirror/; do
+ umount $mntpoint || sleep 5
+done
+while gmirror status test | grep -q SYNCHRONIZING; do sleep 10; done
+for i in `jot 10`; do
+ gmirror stop test && break || sleep 30
+done
+[ $i -eq 10 ] && s=1
+gmirror destroy test 2>/dev/null
+[ $unload ] && gmirror unload
+
+for u in $u1 $u2 $u3; do
+ mdconfig -d -u $u || s=3
+done
+[ $unload2 ] && gnop unload
+exit $s
diff --git a/tools/test/stress2/misc/graid1_6.sh b/tools/test/stress2/misc/graid1_6.sh
new file mode 100755
index 000000000000..dc85c51dad04
--- /dev/null
+++ b/tools/test/stress2/misc/graid1_6.sh
@@ -0,0 +1,106 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Variation of graid1_4.sh
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+rm -f $diskimage*
+need=1024 # MB
+[ `df -k $(dirname $diskimage) | tail -1 | awk '{print int($4 / 1024)}'` -lt \
+ $need ] && printf "Need %d MB on %s.\n" $need `dirname $diskimage` && exit
+
+gmirror load > /dev/null 2>&1 && unload=1
+[ -c /dev/mirror/test ] && { gmirror stop test; gmirror destroy test; }
+old=`sysctl -n kern.geom.mirror.debug`
+sysctl kern.geom.mirror.debug=-1 | grep -q -- -1 ||
+ sysctl kern.geom.mirror.debug=$old > /dev/null
+
+md1=$mdstart
+md2=$((mdstart + 1))
+
+s=0
+for u in $md1 $md2; do
+ disk="$diskimage.$u"
+ dd if=/dev/zero of=$disk bs=1m count=512 status=none
+ [ -c /dev/md$u ] && mdconfig -d -u $u
+ mdconfig -a -t vnode -f $disk -u $u
+done
+
+gmirror label -v -b split -s 2048 test /dev/md$md1 /dev/md$md2 \
+ > /dev/null || exit 1
+[ "`sysctl -in kern.geom.mirror.launch_mirror_before_timeout`" = "0" ] &&
+ sleep $((`sysctl -n kern.geom.mirror.timeout` + 1))
+[ -c /dev/mirror/test ] || exit 1
+newfs $newfs_flags /dev/mirror/test > /dev/null
+mount /dev/mirror/test $mntpoint
+chmod 777 $mntpoint
+
+mlog=/tmp/graid1_6.log
+tail -F -n 0 /var/log/messages > $mlog & mpid=$!
+export runRUNTIME=4m
+export RUNDIR=$mntpoint/stressX
+su $testuser -c 'cd ..; ./run.sh marcus.cfg > /dev/null' &
+
+while kill -0 $! > /dev/null 2>&1; do
+ sleep `jot -r 1 1 5`
+ gmirror remove test md$md2
+ sleep `jot -r 1 1 5`
+ gmirror insert test md$md2
+done
+wait $!
+i=0
+while ! gmirror status test | grep -q COMPLETE; do
+ sleep 10
+ if [ $((i += 1)) -gt 20 ]; then
+ echo "FAIL to COMPLETE"
+ graid status test
+ s=1
+ break
+ fi
+done
+
+while mount | grep $mntpoint | grep -q /mirror/; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/mirror/test || s=2
+gmirror stop -f test ||s=3
+gmirror destroy test 2>/dev/null
+[ $unload ] && gmirror unload
+
+for u in $md2 $md1; do
+ mdconfig -d -u $u || s=4
+done
+rm -f $diskimage*
+grep -m 1 "check-hash" $mlog && s=5
+kill $mpid
+wait
+rm -f $mlog
+exit $s
diff --git a/tools/test/stress2/misc/graid1_7.sh b/tools/test/stress2/misc/graid1_7.sh
new file mode 100755
index 000000000000..8280afb97e3b
--- /dev/null
+++ b/tools/test/stress2/misc/graid1_7.sh
@@ -0,0 +1,127 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Component looses it's name:
+# g_dev_taste: make_dev_p() failed
+# (gp->name=gptid/7c598e03-19cb-11e7-b62b-001e6756c168, error=17)
+
+# Deadlock seen:
+# https://people.freebsd.org/~pho/stress/log/graid1_7.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+gmirror load > /dev/null 2>&1 && unload=1
+[ -c /dev/mirror/test ] && { gmirror stop test; gmirror destroy test; }
+old=`sysctl -n kern.geom.mirror.debug`
+sysctl kern.geom.mirror.debug=-1 | grep -q -- -1 ||
+ sysctl kern.geom.mirror.debug=$old > /dev/null
+
+u1=$mdstart
+s=0
+[ -c /dev/md$u1 ] && mdconfig -d -u $u1
+mdconfig -a -t swap -s 1g -u $u1
+
+set -e
+(
+gpart create -s GPT md$u1
+gpart add -t freebsd-ufs -s 341m md$u1
+gpart add -t freebsd-ufs -s 341m md$u1
+gpart add -t freebsd-ufs -s 341m md$u1
+) > /dev/null
+gmirror label test md${u1}p1 md${u1}p2 md${u1}p3
+[ "`sysctl -in kern.geom.mirror.launch_mirror_before_timeout`" = "0" ] &&
+ sleep $((`sysctl -n kern.geom.mirror.timeout` + 1))
+[ -c /dev/mirror/test ] || exit 1
+
+newfs /dev/mirror/test > /dev/null
+mount /dev/mirror/test $mntpoint
+set +e
+chmod 777 $mntpoint
+
+export runRUNTIME=5m
+export RUNDIR=$mntpoint/stressX
+rm -rf /tmp/stressX.control
+
+su $testuser -c 'cd ..; ./run.sh io.cfg' > /dev/null 2>&1 &
+pid=$!
+
+mlog=/tmp/graid1_6.log
+tail -F -n 0 /var/log/messages > $mlog & mpid=$!
+sleep 2
+cont=/tmp/graid1_7.cont
+touch $cont
+for i in `jot 8`; do
+ while [ -f $cont ]; do
+ for u in md${u1}p2 md${u1}p3; do
+ gmirror forget test
+ gmirror remove test $u
+ gmirror insert test $u
+ id=`gmirror status test | grep gptid | awk '{print $1}'`
+ if [ $i -eq 1 -a -n "$id" ]; then
+ echo "FAIL Remove component $id"
+ gmirror remove test $id
+ fi
+ done 2>/dev/null
+ done &
+done
+while kill -0 $pid 2>/dev/null; do sleep 5; done
+rm $cont
+wait $!
+gmirror status test | grep -qw md${u1}p2 || gmirror insert test md${u1}p2
+gmirror status test | grep -qw md${u1}p3 || gmirror insert test md${u1}p3
+i=0
+while ! gmirror status test | grep -q COMPLETE; do
+ sleep 5
+ if [ $((i += 1)) -gt 20 ]; then
+ echo "FAIL to COMPLETE"
+ gmirror status test
+ s=1
+ break
+ fi
+done
+
+while mount | grep $mntpoint | grep -q /mirror/; do
+ umount $mntpoint || sleep 5
+done
+checkfs /dev/mirror/test || s=2
+while gmirror status test | grep -q SYNCHRONIZING; do sleep 10; done
+for i in `jot 10`; do
+ gmirror stop test && break || sleep 30
+done
+[ $i -eq 10 ] && s=3
+gmirror destroy test 2>/dev/null
+[ $unload ] && gmirror unload
+
+mdconfig -d -u $mdstart || s=4
+grep -m 1 "check-hash" $mlog && s=5
+kill $mpid
+wait
+rm -f $mlog
+exit $s
diff --git a/tools/test/stress2/misc/graid1_8.sh b/tools/test/stress2/misc/graid1_8.sh
new file mode 100755
index 000000000000..d130a6152cf2
--- /dev/null
+++ b/tools/test/stress2/misc/graid1_8.sh
@@ -0,0 +1,102 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Mirror test where the third disk is partially wiped:
+# Silent Data Corruption.
+# fsck() will trash your FS in this scenario.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+gmirror load > /dev/null 2>&1 && unload=1
+mount | grep -q "on $mntpoint " && umount $mntpoint
+[ -c /dev/mirror/test ] && { gmirror stop test; gmirror destroy test; }
+old=`sysctl -n kern.geom.mirror.debug`
+sysctl kern.geom.mirror.debug=-1 | grep -q -- -1 ||
+ sysctl kern.geom.mirror.debug=$old > /dev/null
+
+u1=$mdstart
+u2=$((mdstart + 1))
+u3=$((mdstart + 2))
+
+for u in $u1 $u2 $u3; do
+ [ -c /dev/md$u ] && mdconfig -d -u $u
+ dd if=/dev/zero of=$diskimage.$u bs=1m count=100 status=none
+ mdconfig -a -t vnode -f $diskimage.$u -u $u
+done > /dev/null
+
+gmirror label test md$u1 md$u2 md$u3 || exit 1
+[ "`sysctl -in kern.geom.mirror.launch_mirror_before_timeout`" = "0" ] &&
+ sleep 5
+(
+gpart create -s BSD mirror/test
+gpart add -t freebsd-ufs -s 99m mirror/test
+) > /dev/null
+[ -c /dev/mirror/testa ] || exit 1
+
+newfs -n /dev/mirror/testa > /dev/null
+mount /dev/mirror/testa $mntpoint
+jot 10 | xargs -P0 -I% cp /etc/passwd $mntpoint/%
+
+# The test: zap part of the third disk
+dd if=/dev/random of=$diskimage.$u3 bs=1m count=80 conv=notrunc status=none
+umount $mntpoint
+log=/tmp/graid1_8.sh.log
+
+if [ $# -eq 1 ]; then # This will fix the mirror
+ gmirror remove test md$u3
+ gmirror insert test md$u3
+ while gmirror status test | grep -q SYNCHRONIZING; do
+ sleep 2
+ done
+fi
+
+fsck -fy /dev/mirror/testa > $log 2>&1
+grep -q RERUN $log && fsck -fy /dev/mirror/testa > /dev/null 2>&1
+grep -Eq "MODIFIED|BAD" $log &&
+ { s=1; head -5 $log; } ||
+ s=0
+rm $log
+mount /dev/mirror/testa $mntpoint
+[ `ls $mntpoint | wc -l` -lt 10 ] && ls -l $mntpoint
+umount $mntpoint
+
+while gmirror status test | grep -q SYNCHRONIZING; do sleep 10; done
+for i in `jot 10`; do
+ gmirror stop test && break || sleep 30
+done
+[ $i -eq 10 ] && s=1
+gmirror destroy test 2>/dev/null
+[ $unload ] && gmirror unload
+
+for u in $u1 $u2 $u3; do
+ mdconfig -d -u $u || s=3
+ rm $diskimage.$u
+done
+exit $s
diff --git a/tools/test/stress2/misc/graid1_9.sh b/tools/test/stress2/misc/graid1_9.sh
new file mode 100755
index 000000000000..8d8d447b25b0
--- /dev/null
+++ b/tools/test/stress2/misc/graid1_9.sh
@@ -0,0 +1,114 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# Copy of graid1.sh with fail points added.
+# https://people.freebsd.org/~pho/stress/log/mark015.txt
+# Fixed by r327779
+
+# Problem also seen with non SU:
+# https://people.freebsd.org/~pho/stress/log/numa027.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+md1=$mdstart
+md2=$((mdstart + 1))
+md3=$((mdstart + 2))
+
+s=0
+size=1g
+[ $((`sysctl -n hw.usermem` / 1024 / 1024 / 1024)) -le 4 ] &&
+ size=512m
+
+for u in $md1 $md2 $md3; do
+ mdconfig -l | grep -q md$u && mdconfig -d -u $u
+ mdconfig -a -t swap -s $size -u $u
+done
+
+gmirror load > /dev/null 2>&1 && unload=1
+old=`sysctl -n kern.geom.mirror.debug`
+sysctl kern.geom.mirror.debug=-1 | grep -q -- -1 ||
+ sysctl kern.geom.mirror.debug=$old > /dev/null
+gmirror label -v -b split -s 2048 test /dev/md$md1 /dev/md$md2 \
+ /dev/md$md3 > /dev/null || exit 1
+[ "`sysctl -in kern.geom.mirror.launch_mirror_before_timeout`" = "0" ] &&
+ sleep $((`sysctl -n kern.geom.mirror.timeout` + 1))
+[ -c /dev/mirror/test ] || exit 1
+# Do not use SU as it is more intolerant to FS corruption
+newfs /dev/mirror/test > /dev/null
+mount /dev/mirror/test $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+
+woid=debug.fail_point.g_mirror_regular_request_write
+roid=debug.fail_point.g_mirror_regular_request_read
+gpid=`pgrep -fS "g_mirror test"`
+sysctl $woid="0.0005%return(5)[pid $gpid]" > /dev/null
+sysctl $roid="0.0005%return(5)[pid $gpid]" > /dev/null
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null &
+
+while kill -0 $! 2>/dev/null; do
+ if gmirror status test | grep -q DEGRADED; then
+ for i in $md1 $md2 $md3; do
+ if ! gmirror status test | grep -q md$i; then
+ gmirror forget test md$i 2>/dev/null
+ gmirror remove test md$i 2>/dev/null
+ gmirror insert test md$i
+ fi
+ done
+ else
+ sleep 5
+ fi
+done
+wait
+
+sysctl $woid=off > /dev/null
+sysctl $roid=off > /dev/null
+
+for i in `jot 12`; do
+ gmirror status test | grep -q SYNCHRONIZING || break
+ sleep 10
+done
+gmirror status test | grep -q SYNCHRONIZING &&
+ { s=1; gmirror status test; }
+while mount | grep $mntpoint | grep -q /mirror/; do
+ umount $mntpoint || sleep 1
+done
+# The FS is most likely corrupted at this point, so do not run fsck(8).
+gmirror stop test || s=2
+gmirror destroy test 2>/dev/null
+[ $unload ] && gmirror unload
+
+for u in $md3 $md2 $md1; do
+ mdconfig -d -u $u || s=3
+done
+exit $s
diff --git a/tools/test/stress2/misc/graid3.sh b/tools/test/stress2/misc/graid3.sh
new file mode 100755
index 000000000000..f67ac819a47d
--- /dev/null
+++ b/tools/test/stress2/misc/graid3.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Probably unrelated mkfifo() problem seen:
+# http://people.freebsd.org/~pho/stress/log/graid3.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+md1=$mdstart
+md2=$((mdstart + 1))
+md3=$((mdstart + 2))
+
+size=1g
+[ $((`sysctl -n hw.usermem` / 1024 / 1024 / 1024)) -le 4 ] &&
+ size=512m
+
+for u in $md1 $md2 $md3; do
+ mdconfig -l | grep -q md$u && mdconfig -d -u $u
+ mdconfig -a -t swap -s $size -u $u
+done
+
+graid3 load > /dev/null 2>&1 && unload=1
+graid3 label -v -r data md$md1 md$md2 md$md3 > /dev/null || exit 1
+[ -c /dev/raid3/data ] || exit 1
+newfs $newfs_flags /dev/raid3/data > /dev/null
+mount /dev/raid3/data $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=20m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg'
+
+while mount | grep $mntpoint | grep -q raid3; do
+ umount $mntpoint || sleep 1
+done
+
+graid3 stop data && s=0 || s=1
+[ $unload ] && graid3 unload
+
+for u in $md3 $md2 $md1; do
+ mdconfig -d -u $u
+done
+exit $s
diff --git a/tools/test/stress2/misc/growfs.sh b/tools/test/stress2/misc/growfs.sh
new file mode 100755
index 000000000000..5a2ec3f609d3
--- /dev/null
+++ b/tools/test/stress2/misc/growfs.sh
@@ -0,0 +1,63 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# growfs(8) test
+# "checksum failed: cg 52, cgp: 0x0 != bp: 0xe35de2ca" seen.
+# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=222876
+# Fixed by r324499
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+s=0
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 32g -u $mdstart
+/sbin/gpart create -s GPT md$mdstart
+/sbin/gpart add -t freebsd-ufs -s 2g -a 4k md$mdstart
+set +e
+
+newfs $newfs_flags md${mdstart}p1 > /dev/null
+mount /dev/md${mdstart}p1 $mntpoint
+cp -r /usr/include $mntpoint/inc1
+umount $mntpoint
+checkfs /dev/md${mdstart}p1 || { s=1; echo "Initial fsck 1 fail"; }
+
+gpart resize -i 1 -s 31g -a 4k md$mdstart
+growfs -y md${mdstart}p1 > /dev/null
+# This fsck make the checksum error go away
+#checkfs /dev/md${mdstart}p1 || { s=1; echo "fsck after growfs fail"; }
+
+mount /dev/md${mdstart}p1 $mntpoint
+cp -r /usr/include $mntpoint/inc2
+umount $mntpoint
+checkfs /dev/md${mdstart}p1 || { s=1; echo "Final fsck fail"; }
+
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/holdcnt0.sh b/tools/test/stress2/misc/holdcnt0.sh
new file mode 100755
index 000000000000..7a35eb1dbf5f
--- /dev/null
+++ b/tools/test/stress2/misc/holdcnt0.sh
@@ -0,0 +1,261 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# "panic: cluster_wbuild: page 0xc2eebc10 failed shared busing" seen.
+# "panic: vdrop: holdcnt 0" seen.
+# "panic: cleaned vnode isn't" seen.
+# OoVM seen with r285808:
+# https://people.freebsd.org/~pho/stress/log/holdcnt0.txt
+
+# Test scenario suggestion by alc@
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+[ `sysctl -n hw.physmem` -lt $((32 * 1024 * 1024 * 1024)) ] && exit 0
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > holdcnt0.c
+mycc -o holdcnt0 -Wall -Wextra -g holdcnt0.c || exit 1
+rm -f holdcnt0.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 5g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+(cd $mntpoint; /tmp/holdcnt0) &
+pid=$!
+sleep 5
+while kill -0 $! 2> /dev/null; do
+ (cd ../testcases/swap; ./swap -t 1m -i 1) > /dev/null 2>&1
+done
+wait $pid; s=$?
+
+while mount | grep -q md${mdstart}$part; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/holdcnt0
+exit $s
+EOF
+/*
+ A test that causes the page daemon to generate cached pages
+ within a bunch of files and has some consumer that is trying to
+ allocate new pages to the same files.
+*/
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define BUFSIZE (1024 * 1024)
+#define FILES 200
+#define RPARALLEL 8
+#define WPARALLEL 2
+
+static jmp_buf jbuf;
+static off_t maxsize;
+static int ps;
+static char *buf;
+static volatile char val;
+
+static void
+hand(int i __unused) { /* handler */
+
+#if defined(DEBUG)
+ fprintf(stderr, "%d ", i);
+#endif
+ longjmp(jbuf, 1);
+}
+
+static void
+cleanup(void)
+{
+ int i;
+ char file[80];
+
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ unlink(file);
+ }
+}
+
+static void
+init(void)
+{
+ int fd, i;
+ char file[80];
+
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0644)) ==
+ -1)
+ err(1, "open(%s)", file);
+ if (write(fd, buf, BUFSIZE) != BUFSIZE)
+ err(1, "write");
+ close(fd);
+ }
+
+}
+
+static void
+writer(void)
+{
+ struct stat statbuf;
+ time_t start;
+ int fd, i;
+ char file[80];
+
+ setproctitle("writer");
+ start = time(NULL);
+ while (time(NULL) - start < 600) {
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ if ((fd = open(file, O_RDWR | O_APPEND)) == -1) {
+ if (errno != ENOENT)
+ err(1, "open(%s) append", file);
+ goto err;
+ }
+ if (fstat(fd, &statbuf) < 0)
+ err(1, "fstat error");
+ if (statbuf.st_size < maxsize) {
+ if (write(fd, buf, ps) != ps) {
+ warn("writer");
+ goto err;
+ }
+ }
+ close(fd);
+ }
+ }
+err:
+ cleanup();
+
+ _exit(0);
+}
+
+static void
+reader(void)
+{
+ struct stat statbuf;
+ void *p;
+ size_t len;
+ int fd, i, j, n;
+ char file[80];
+
+ setproctitle("reader");
+ signal(SIGSEGV, hand);
+ signal(SIGBUS, hand);
+ fd = 0;
+ for (;;) {
+ (void)setjmp(jbuf);
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ if (fd > 0)
+ close(fd);
+ if ((fd = open(file, O_RDWR)) == -1) {
+ if (errno != ENOENT)
+ warn("reader(%s)", file);
+ _exit(0);
+ }
+ if (fstat(fd, &statbuf) < 0)
+ err(1, "fstat error");
+ if (statbuf.st_size >= maxsize) {
+ if (ftruncate(fd, ps) == -1)
+ err(1, "ftruncate");
+ continue;
+ }
+ len = statbuf.st_size;
+ if ((p = mmap(p, len, PROT_READ, MAP_SHARED, fd, 0))
+ == MAP_FAILED)
+ err(1, "mmap()");
+ close(fd);
+ n = statbuf.st_size / ps;
+ for (j = 0; j < n; j++) {
+ val = *(char *)p;
+ p = p + ps;
+ }
+#if 0
+ if (munmap(p, len) == -1)
+ perror("munmap");
+#endif
+ }
+ }
+ _exit(0);
+}
+int
+main(void)
+{
+ pid_t rpid[RPARALLEL], wpid[WPARALLEL];
+ int e, i, s;
+
+ maxsize = 2LL * 1024 * 1024 * 1024 / FILES;
+ buf = malloc(BUFSIZE);
+ ps = getpagesize();
+
+ init();
+ e = 0;
+ for (i = 0; i < WPARALLEL; i++) {
+ if ((wpid[i] = fork()) == 0)
+ writer();
+ }
+ for (i = 0; i < RPARALLEL; i++) {
+ if ((rpid[i] = fork()) == 0)
+ reader();
+ }
+
+ for (i = 0; i < WPARALLEL; i++) {
+ waitpid(wpid[i], &s, 0);
+ if (e == 0)
+ e = s;
+ }
+ for (i = 0; i < RPARALLEL; i++) {
+ waitpid(rpid[i], &s, 0);
+ if (e == 0)
+ e = s;
+ }
+ free(buf);
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/holdcnt02.sh b/tools/test/stress2/misc/holdcnt02.sh
new file mode 100755
index 000000000000..671d068917f0
--- /dev/null
+++ b/tools/test/stress2/misc/holdcnt02.sh
@@ -0,0 +1,238 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Copy of holdcnt0.sh, but without the memory disk usage.
+
+# Test scenario suggestion by alc@
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+[ `sysctl -n hw.physmem` -lt $((32 * 1024 * 1024 * 1024)) ] && exit 0
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > holdcnt02.c
+mycc -o holdcnt02 -Wall -Wextra -g holdcnt02.c || exit 1
+rm -f holdcnt02.c
+cd $here
+
+trap "rm -f /tmp/holdcnt02 `dirname $diskimage`/f000???" EXIT INT
+(cd `dirname $diskimage`; /tmp/holdcnt02) &
+pid=$!
+sleep 5
+while kill -0 $! 2> /dev/null; do
+ (cd ../testcases/swap; ./swap -t 1m -i 1) > /dev/null 2>&1
+done
+wait $pid
+exit
+EOF
+/*
+ A test that causes the page daemon to generate cached pages
+ within a bunch of files and has some consumer that is trying to
+ allocate new pages to the same files.
+*/
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define BUFSIZE (1024 * 1024)
+#define FILES 200
+#define RPARALLEL 8
+#define WPARALLEL 2
+
+static jmp_buf jbuf;
+static off_t maxsize;
+static int ps;
+static char *buf;
+static volatile char val;
+
+static void
+hand(int i __unused) { /* handler */
+ longjmp(jbuf, 1);
+}
+
+static void
+cleanup(void)
+{
+ int i;
+ char file[80];
+
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ unlink(file);
+ }
+}
+
+static void
+init(void)
+{
+ int fd, i;
+ char file[80];
+
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0644)) ==
+ -1)
+ err(1, "open(%s)", file);
+ if (write(fd, buf, BUFSIZE) != BUFSIZE)
+ err(1, "write");
+ close(fd);
+ }
+
+}
+
+static void
+writer(void)
+{
+ int fd, i;
+ char file[80];
+ time_t start;
+ struct stat statbuf;
+
+ setproctitle("writer");
+ start = time(NULL);
+ while (time(NULL) - start < 600) {
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ if ((fd = open(file, O_RDWR | O_APPEND)) == -1) {
+ if (errno != ENOENT)
+ err(1, "open(%s) append", file);
+ goto err;
+ }
+ if (fstat(fd, &statbuf) < 0)
+ err(1, "fstat error");
+ if (statbuf.st_size < maxsize) {
+ if (write(fd, buf, ps) != ps) {
+ warn("writer");
+ goto err;
+ }
+ }
+ close(fd);
+ }
+ }
+err:
+ cleanup();
+
+ _exit(0);
+}
+
+static void
+reader(void)
+{
+ int fd, i, j, n;
+ void *p;
+ size_t len;
+ char file[80];
+ struct stat statbuf;
+
+ setproctitle("reader");
+ signal(SIGSEGV, hand);
+ signal(SIGBUS, hand);
+ fd = 0;
+ for (;;) {
+ (void)setjmp(jbuf);
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ if (fd > 0)
+ close(fd);
+ if ((fd = open(file, O_RDWR)) == -1) {
+ if (errno != ENOENT)
+ warn("reader(%s)", file);
+ _exit(0);
+ }
+ if (fstat(fd, &statbuf) < 0)
+ err(1, "fstat error");
+ if (statbuf.st_size >= maxsize) {
+ if (ftruncate(fd, ps) == -1)
+ err(1, "ftruncate");
+ continue;
+ }
+ len = statbuf.st_size;
+ if ((p = mmap(p, len, PROT_READ, MAP_SHARED, fd, 0))
+ == MAP_FAILED)
+ err(1, "mmap()");
+ close(fd);
+ n = statbuf.st_size / ps;
+ for (j = 0; j < n; j++) {
+ val = *(char *)p;
+ p = p + ps;
+ }
+#if 0
+ if (munmap(p, len) == -1)
+ perror("munmap");
+#endif
+ }
+ }
+ _exit(0);
+}
+int
+main(void)
+{
+ pid_t rpid[RPARALLEL], wpid[WPARALLEL];
+ int e, i, s;
+
+ maxsize = 2LL * 1024 * 1024 * 1024 / FILES;
+ buf = malloc(BUFSIZE);
+ ps = getpagesize();
+
+ init();
+ e = 0;
+ for (i = 0; i < WPARALLEL; i++) {
+ if ((wpid[i] = fork()) == 0)
+ writer();
+ }
+ for (i = 0; i < RPARALLEL; i++) {
+ if ((rpid[i] = fork()) == 0)
+ reader();
+ }
+
+ for (i = 0; i < WPARALLEL; i++)
+ waitpid(wpid[i], &s, 0);
+ e += s == 0 ? 0 : 1;
+ for (i = 0; i < RPARALLEL; i++)
+ waitpid(rpid[i], &s, 0);
+ e += s == 0 ? 0 : 1;
+
+ free(buf);
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/holdcnt03.sh b/tools/test/stress2/misc/holdcnt03.sh
new file mode 100755
index 000000000000..35437c06e707
--- /dev/null
+++ b/tools/test/stress2/misc/holdcnt03.sh
@@ -0,0 +1,237 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Copy of holdcnt02.sh, but with mmap'ed write.
+# Use 16 GB RAM or less.
+# Deadlock seen:
+# https://people.freebsd.org/~pho/stress/log/alan018.txt
+
+# Test scenario suggestion by alc@
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+[ `sysctl -n hw.physmem` -lt $((32 * 1024 * 1024 * 1024)) ] && exit 0
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > holdcnt03.c
+mycc -o holdcnt03 -Wall -Wextra -g holdcnt03.c || exit 1
+rm -f holdcnt03.c
+cd $here
+
+trap "rm -f /tmp/holdcnt03 `dirname $diskimage`/f000???" EXIT INT
+(cd `dirname $diskimage`; /tmp/holdcnt03) &
+pid=$!
+sleep 5
+while kill -0 $! 2> /dev/null; do
+ (cd ../testcases/swap; ./swap -t 1m -i 1) > /dev/null 2>&1
+done
+wait $pid
+exit
+EOF
+/*
+ A test that causes the page daemon to generate cached pages
+ within a bunch of files and has some consumer that is trying to
+ allocate new pages to the same files.
+*/
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define BUFSIZE (1024 * 1024)
+#define FILES 200
+#define RPARALLEL 8
+#define WPARALLEL 2
+
+static jmp_buf jbuf;
+static off_t maxsize;
+static int ps;
+static char *buf;
+
+static void
+hand(int i __unused) { /* handler */
+ longjmp(jbuf, 1);
+}
+
+static void
+cleanup(void)
+{
+ int i;
+ char file[80];
+
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ unlink(file);
+ }
+}
+
+static void
+init(void)
+{
+ char file[80];
+ int fd, i;
+
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1)
+ err(1, "open(%s)", file);
+ if (write(fd, buf, BUFSIZE) != BUFSIZE)
+ err(1, "write");
+ close(fd);
+ }
+
+}
+
+static void
+writer(void)
+{
+ int fd, i;
+ char file[80];
+ time_t start;
+ struct stat statbuf;
+
+ setproctitle("writer");
+ start = time(NULL);
+ while (time(NULL) - start < 600) {
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ if ((fd = open(file, O_RDWR | O_APPEND)) == -1) {
+ if (errno != ENOENT)
+ err(1, "open(%s) append", file);
+ goto err;
+ }
+ if (fstat(fd, &statbuf) < 0)
+ err(1, "fstat error");
+ if (statbuf.st_size < maxsize) {
+ if (write(fd, buf, ps) != ps) {
+ warn("writer");
+ goto err;
+ }
+ }
+ close(fd);
+ }
+ }
+err:
+ cleanup();
+
+ _exit(0);
+}
+
+static void
+touch(void)
+{
+ int fd, i, j, n;
+ void *p;
+ size_t len;
+ char file[80];
+ struct stat statbuf;
+
+ setproctitle("touch");
+ signal(SIGSEGV, hand);
+ signal(SIGBUS, hand);
+ fd = 0;
+ for (;;) {
+ (void)setjmp(jbuf);
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ if (fd > 0)
+ close(fd);
+ if ((fd = open(file, O_RDWR)) == -1) {
+ if (errno != ENOENT)
+ warn("touch(%s)", file);
+ _exit(0);
+ }
+ if (fstat(fd, &statbuf) < 0)
+ err(1, "fstat error");
+ if (statbuf.st_size >= maxsize) {
+ if (ftruncate(fd, ps) == -1)
+ err(1, "ftruncate");
+ continue;
+ }
+ len = statbuf.st_size;
+ if ((p = mmap(p, len, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED)
+ err(1, "mmap()");
+ close(fd);
+ n = statbuf.st_size / ps;
+ for (j = 0; j < n; j++) {
+ *(char *)p = 1;
+ p = p + ps;
+ }
+ if (munmap(p, len) == -1)
+ perror("munmap");
+ }
+ }
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t rpid[RPARALLEL], wpid[WPARALLEL];
+ int e, i, s;
+
+ maxsize = 2LL * 1024 * 1024 * 1024 / FILES;
+ buf = malloc(BUFSIZE);
+ ps = getpagesize();
+
+ init();
+ e = 0;
+ for (i = 0; i < WPARALLEL; i++) {
+ if ((wpid[i] = fork()) == 0)
+ writer();
+ }
+ for (i = 0; i < RPARALLEL; i++) {
+ if ((rpid[i] = fork()) == 0)
+ touch();
+ }
+
+ for (i = 0; i < WPARALLEL; i++)
+ waitpid(wpid[i], &s, 0);
+ e += s == 0 ? 0 : 1;
+ for (i = 0; i < RPARALLEL; i++)
+ waitpid(rpid[i], &s, 0);
+ e += s == 0 ? 0 : 1;
+
+ free(buf);
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/holdcnt04.sh b/tools/test/stress2/misc/holdcnt04.sh
new file mode 100755
index 000000000000..52aff0e3816d
--- /dev/null
+++ b/tools/test/stress2/misc/holdcnt04.sh
@@ -0,0 +1,248 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Lite version of holdcnt0.sh
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `sysctl -n vm.swap_total` -ne 0 ] && exit 0
+[ `sysctl -n hw.physmem` -lt 5250000000 ] && exit 0
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > holdcnt04.c
+mycc -o holdcnt04 -Wall -Wextra -g holdcnt04.c || exit 1
+rm -f holdcnt04.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+(cd $mntpoint; /tmp/holdcnt04) &
+pid=$!
+sleep 5
+while kill -0 $! 2> /dev/null; do
+ (cd ../testcases/swap; ./swap -t 1m -i 1) > /dev/null 2>&1
+done
+wait $pid; s=$?
+
+while mount | grep -q md${mdstart}$part; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/holdcnt04
+exit $s
+EOF
+/*
+ A test that causes the page daemon to generate cached pages
+ within a bunch of files and has some consumer that is trying to
+ allocate new pages to the same files.
+*/
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define BUFSIZE (1024 * 1024)
+#define FILES 20
+#define RPARALLEL 8
+#define WPARALLEL 2
+
+static jmp_buf jbuf;
+static off_t maxsize;
+static int ps;
+static char *buf;
+static volatile char val;
+
+static void
+hand(int i __unused) { /* handler */
+ longjmp(jbuf, 1);
+}
+
+static void
+cleanup(void)
+{
+ int i;
+ char file[80];
+
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ unlink(file);
+ }
+}
+
+static void
+init(void)
+{
+ int fd, i;
+ char file[80];
+
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0644)) ==
+ -1)
+ err(1, "open(%s)", file);
+ if (write(fd, buf, BUFSIZE) != BUFSIZE)
+ err(1, "write");
+ close(fd);
+ }
+
+}
+
+static void
+writer(void)
+{
+ struct stat statbuf;
+ time_t start;
+ int fd, i;
+ char file[80];
+
+ setproctitle("writer");
+ start = time(NULL);
+ while (time(NULL) - start < 600) {
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ if ((fd = open(file, O_RDWR | O_APPEND)) == -1) {
+ if (errno != ENOENT)
+ err(1, "open(%s) append", file);
+ goto err;
+ }
+ if (fstat(fd, &statbuf) < 0)
+ err(1, "fstat error");
+ if (statbuf.st_size < maxsize) {
+ if (write(fd, buf, ps) != ps) {
+ warn("writer");
+ goto err;
+ }
+ }
+ close(fd);
+ }
+ }
+err:
+ cleanup();
+
+ _exit(0);
+}
+
+static void
+reader(void)
+{
+ struct stat statbuf;
+ void *p;
+ size_t len;
+ int fd, i, j, n;
+ char file[80];
+
+ setproctitle("reader");
+ signal(SIGSEGV, hand);
+ signal(SIGBUS, hand);
+ fd = 0;
+ for (;;) {
+ (void)setjmp(jbuf);
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ if (fd > 0)
+ close(fd);
+ if ((fd = open(file, O_RDWR)) == -1) {
+ if (errno != ENOENT)
+ warn("reader(%s)", file);
+ _exit(0);
+ }
+ if (fstat(fd, &statbuf) < 0)
+ err(1, "fstat error");
+ if (statbuf.st_size >= maxsize) {
+ if (ftruncate(fd, ps) == -1)
+ err(1, "ftruncate");
+ continue;
+ }
+ len = statbuf.st_size;
+ if ((p = mmap(p, len, PROT_READ, MAP_SHARED, fd, 0))
+ == MAP_FAILED)
+ err(1, "mmap()");
+ close(fd);
+ n = statbuf.st_size / ps;
+ for (j = 0; j < n; j++) {
+ val = *(char *)p;
+ p = p + ps;
+ }
+#if 0
+ if (munmap(p, len) == -1)
+ perror("munmap");
+#endif
+ }
+ }
+ _exit(0);
+}
+int
+main(void)
+{
+ pid_t rpid[RPARALLEL], wpid[WPARALLEL];
+ int e, i, s;
+
+ maxsize = 1LL * 1024 * 1024 * 1024 / FILES;
+ buf = malloc(BUFSIZE);
+ ps = getpagesize();
+
+ init();
+ e = 0;
+ for (i = 0; i < WPARALLEL; i++) {
+ if ((wpid[i] = fork()) == 0)
+ writer();
+ }
+ for (i = 0; i < RPARALLEL; i++) {
+ if ((rpid[i] = fork()) == 0)
+ reader();
+ }
+
+ for (i = 0; i < WPARALLEL; i++)
+ waitpid(wpid[i], &s, 0);
+ e += s == 0 ? 0 : 1;
+ for (i = 0; i < RPARALLEL; i++)
+ waitpid(rpid[i], &s, 0);
+ e += s == 0 ? 0 : 1;
+
+ free(buf);
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/holdcnt05.sh b/tools/test/stress2/misc/holdcnt05.sh
new file mode 100755
index 000000000000..efafa984bdd8
--- /dev/null
+++ b/tools/test/stress2/misc/holdcnt05.sh
@@ -0,0 +1,249 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Tmpfs version of holdcnt0.sh
+# Test scenario suggestion by alc@
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+[ `sysctl -n hw.physmem` -lt $((32 * 1024 * 1024 * 1024)) ] && exit 0
+
+. ../default.cfg
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > holdcnt05.c
+cc -o holdcnt05 -Wall -Wextra -g holdcnt05.c || exit 1
+rm -f holdcnt05.c
+cd $here
+
+mount | grep $mntpoint && umount -f $mntpoint
+mount -t tmpfs null $mntpoint
+
+cd $mntpoint
+/tmp/holdcnt05
+s=$?
+
+cd /
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+rm -f /tmp/holdcnt05
+[ $s -ne 0 ] && echo "Exit status $s"
+exit $s
+EOF
+/*
+ A test that causes the page daemon to generate cached pages
+ within a bunch of files and has some consumer that is trying to
+ allocate new pages to the same files.
+*/
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define BUFSIZE (1024 * 1024)
+#define FILES 200
+#define RPARALLEL 8
+#define WPARALLEL 2
+
+static jmp_buf jbuf;
+static off_t maxsize;
+static int ps;
+static char *buf;
+static volatile char val;
+
+static void
+hand(int i __unused) { /* handler */
+
+#if defined(DEBUG)
+ fprintf(stderr, "%d ", i);
+#endif
+ longjmp(jbuf, 1);
+}
+
+static void
+cleanup(void)
+{
+ int i;
+ char file[80];
+
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ unlink(file);
+ }
+}
+
+static void
+init(void)
+{
+ int fd, i;
+ char file[80];
+
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0644)) ==
+ -1)
+ err(1, "open(%s)", file);
+ if (write(fd, buf, BUFSIZE) != BUFSIZE)
+ err(1, "write");
+ close(fd);
+ }
+
+}
+
+static void
+writer(void)
+{
+ struct stat statbuf;
+ time_t start;
+ int fd, i;
+ char file[80];
+
+ setproctitle("writer");
+ start = time(NULL);
+ while (time(NULL) - start < 600) {
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ if ((fd = open(file, O_RDWR | O_APPEND)) == -1) {
+ if (errno != ENOENT)
+ err(1, "open(%s) append", file);
+ goto err;
+ }
+ if (fstat(fd, &statbuf) < 0)
+ err(1, "fstat error");
+ if (statbuf.st_size < maxsize) {
+ if (write(fd, buf, ps) != ps) {
+ warn("writer");
+ goto err;
+ }
+ }
+ close(fd);
+ }
+ }
+err:
+ cleanup();
+
+ _exit(0);
+}
+
+static void
+reader(void)
+{
+ struct stat statbuf;
+ void *p;
+ size_t len;
+ int fd, i, j, n;
+ char file[80];
+
+ setproctitle("reader");
+ signal(SIGSEGV, hand);
+ signal(SIGBUS, hand);
+ fd = 0;
+ for (;;) {
+ (void)setjmp(jbuf);
+ for (i = 0; i < FILES; i++) {
+ snprintf(file, sizeof(file), "f%06d", i);
+ if (fd > 0)
+ close(fd);
+ if ((fd = open(file, O_RDWR)) == -1) {
+ if (errno != ENOENT)
+ warn("reader(%s)", file);
+ _exit(0);
+ }
+ if (fstat(fd, &statbuf) < 0)
+ err(1, "fstat error");
+ if (statbuf.st_size >= maxsize) {
+ if (ftruncate(fd, ps) == -1)
+ err(1, "ftruncate");
+ continue;
+ }
+ len = statbuf.st_size;
+ if ((p = mmap(p, len, PROT_READ, MAP_SHARED, fd, 0))
+ == MAP_FAILED)
+ err(1, "mmap()");
+ close(fd);
+ n = statbuf.st_size / ps;
+ for (j = 0; j < n; j++) {
+ val = *(char *)p;
+ p = p + ps;
+ }
+#if 0
+ if (munmap(p, len) == -1)
+ perror("munmap");
+#endif
+ }
+ }
+ _exit(0);
+}
+int
+main(void)
+{
+ pid_t rpid[RPARALLEL], wpid[WPARALLEL];
+ int e, i, s;
+
+ maxsize = 2LL * 1024 * 1024 * 1024 / FILES;
+ buf = malloc(BUFSIZE);
+ ps = getpagesize();
+
+ init();
+ e = 0;
+ for (i = 0; i < WPARALLEL; i++) {
+ if ((wpid[i] = fork()) == 0)
+ writer();
+ }
+ for (i = 0; i < RPARALLEL; i++) {
+ if ((rpid[i] = fork()) == 0)
+ reader();
+ }
+
+ for (i = 0; i < WPARALLEL; i++) {
+ waitpid(wpid[i], &s, 0);
+ if (e == 0)
+ e = s;
+ }
+ for (i = 0; i < RPARALLEL; i++) {
+ waitpid(rpid[i], &s, 0);
+ if (e == 0)
+ e = s;
+ }
+ free(buf);
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/ifconfig.sh b/tools/test/stress2/misc/ifconfig.sh
new file mode 100755
index 000000000000..6359711a0445
--- /dev/null
+++ b/tools/test/stress2/misc/ifconfig.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Test scenario from D17599 "Fix for double free when deleting entries from
+# epoch managed lists"
+# by Hans Petter Selasky <hselasky@freebsd.org>
+
+# "panic: starting DAD on non-tentative address 0xfffff8010c311000" seen.
+# https://people.freebsd.org/~pho/stress/log/epoch.txt
+
+# Fatal trap 9: general protection fault while in kernel mode
+# https://people.freebsd.org/~pho/stress/log/epoch-2.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+if=`ifconfig | grep -w mtu | grep -v RUNNING | sed 's/:.*//' | head -1`
+[ -z "$if" ] &&
+ if=`ifconfig | \
+ awk '/^[a-z].*: / {gsub(":", ""); ifn = $1}; /no car/{print ifn; exit}'`
+
+[ -z "$if" ] && exit 0
+echo "Using $if for test."
+ifconfig $if | grep -q RUNNING && running=1
+
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 300 ]; do
+ for i in `jot 255`; do
+ (ifconfig $if.$i create
+ ifconfig $if.$i inet 224.0.0.$i
+ ifconfig $if.$i destroy) > /dev/null 2>&1 &
+ done
+ wait
+done
+[ $running ] || ifconfig $if down
+
+exit 0
diff --git a/tools/test/stress2/misc/ifconfig2.sh b/tools/test/stress2/misc/ifconfig2.sh
new file mode 100755
index 000000000000..61abbab3dd54
--- /dev/null
+++ b/tools/test/stress2/misc/ifconfig2.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+# Test scenario from D17599 "Fix for double free when deleting entries from
+# epoch managed lists"
+# by Hans Petter Selasky <hselasky@freebsd.org>
+
+# Page fault in nd6_dad_timer+0x6b seen:
+# https://people.freebsd.org/~pho/stress/log/ifconfig2.txt
+# https://people.freebsd.org/~pho/stress/log/log0051.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+if=`ifconfig | grep -w mtu | grep -v RUNNING | sed 's/:.*//' | head -1`
+[ -z "$if" ] &&
+ if=`ifconfig | \
+ awk '/^[a-z].*: / {gsub(":", ""); ifn = $1}; /no car/{print ifn; exit}'`
+
+[ -z "$if" ] && exit 0
+echo "Using $if for test."
+ifconfig $if | grep -q RUNNING && running=1
+
+sync=/tmp/`basename $0`.sync
+rm -f $sync
+for i in `jot 5`; do
+ (
+ while [ ! -f $sync ]; do
+ sleep .1
+ done
+ while [ -f $sync ]; do
+ ifconfig $if.$i create
+ ifconfig $if.$i inet 224.0.0.$i
+ ifconfig $if.$i destroy
+ done
+ ) > /dev/null 2>&1 &
+done
+touch $sync
+sleep 120
+rm -f $sync
+wait
+[ $running ] || ifconfig $if down
+
+exit 0
diff --git a/tools/test/stress2/misc/indir.sh b/tools/test/stress2/misc/indir.sh
new file mode 100755
index 000000000000..d2d28cd650ab
--- /dev/null
+++ b/tools/test/stress2/misc/indir.sh
@@ -0,0 +1,201 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Hunt for "panic: ufsdirhash_dirtrunc: bad offset"
+# by making the directory inode grow into the indirect blocks.
+
+# Truncate directories larger than 12 * block_size bytes
+# default is 12 * 32768 = 393216, 384k
+
+# No problems seen.
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/indir.c
+sed -i '' -e "s#MNTPOINT#$mntpoint#g" $dir/indir.c
+mycc -o indir -Wall -Wextra -O0 -g indir.c || exit 1
+rm -f indir.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+newfs -j -n -b 16384 -f 2048 -i 2048 md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+ifree=`df -i $mntpoint | tail -1 | awk '{print $7 - 50}'`
+set +e
+
+if [ $dtrace ]; then
+ dtrace -w -n '*ufsdirhash_dirtrunc:entry {@rw[execname,probefunc] = \
+ count(); }' &
+ dpid=$!
+ sleep 2
+fi
+
+/tmp/indir $mntpoint $ifree
+
+if [ $dtrace ]; then
+ kill -s TERM $dpid
+ wait $dpid
+fi
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/indir
+if [ $dtrace ]; then
+ while pgrep -q dtrace; do sleep 1; done
+ kldstat | grep -q dtraceall &&
+ kldunload dtraceall.ko
+fi
+exit 0
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdatomic.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static long files;
+static char *path;
+
+#define PARALLEL 64
+#define RUNTIME (10 * 60)
+
+static void
+test(int idx)
+{
+ long j;
+ int fd;
+ pid_t pid;
+ char dir[128], file[128], new[128];
+
+ sleep(1);
+
+ snprintf(dir, sizeof(dir), "%s/d%d", path, idx);
+ if (mkdir(dir, 0755) == -1) {
+ if (errno != EEXIST)
+ err(1, "mkdir(%s)", dir);
+ }
+ if (chdir(dir) == -1)
+ err(1, "chdir(%s)", dir);
+
+ pid = getpid();
+ for (j = 0; j < files; j++) {
+ sprintf(file,"p%05d.%05ld", pid, j);
+ if ((fd = creat(file, 0660)) == -1)
+ err(1, "creat(%s). %s:%d", file, __FILE__, __LINE__);
+ if (fd != -1 && close(fd) == -1)
+ err(2, "close(%ld)", j);
+ }
+
+ for (j = 0; j < files; j++) {
+ sprintf(file,"p%05d.%05ld", pid, j);
+ sprintf(new,"p%05d.%05ld.old", pid, j);
+ if (rename(file, new) == -1)
+ err(1, "rename(%s, %s)", file, new);
+ }
+
+ for (j = 0; j < files; j++) {
+ sprintf(file,"p%05d.%05ld", pid, j);
+ sprintf(new,"p%05d.%05ld.old", pid, j);
+ if (rename(new, file) == -1)
+ err(1, "rename(%s, %s)", new, file);
+ }
+
+ for (j = 0; j < files; j++) {
+ sprintf(file,"p%05d.%05ld", pid, j);
+ if (unlink(file) == -1)
+ warn("unlink(%s)", file);
+
+ }
+ if (chdir("..") == -1)
+ err(1, "chdir ..");
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ pid_t pids[PARALLEL];
+ time_t start;
+ int e, i, n, status;
+
+ if (argc != 3) {
+ fprintf(stderr, "Usage %s <path> <files>\n", argv[0]);
+ exit(1);
+ }
+ path = argv[1];
+ files = atol(argv[2]) / PARALLEL;
+ fprintf(stderr, "Using %ld inodes per dir. %d threads. %ld inodes in total\n",
+ files, PARALLEL, files * PARALLEL);
+ e = n = 0;
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ fprintf(stderr, "Loop #%d\n", ++n);
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test(i);
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+// system("(cd MNTPOINT; umount MNTPOINT) > /dev/null 2>&1");
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/indir_trunc.sh b/tools/test/stress2/misc/indir_trunc.sh
new file mode 100755
index 000000000000..64c11f3570ee
--- /dev/null
+++ b/tools/test/stress2/misc/indir_trunc.sh
@@ -0,0 +1,231 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# No problems seen.
+# Test scenario inspired by:
+# syzbot+6532e9aab8911f58beeb@syzkaller.appspotmail.com
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -m` = "i386" ] && exit 0 # OOM killing
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/indir_trunc.c
+mycc -o indir_trunc -Wall -Wextra -O0 -g indir_trunc.c -lpthread || exit 1
+rm -f indir_trunc.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 40g -u $mdstart
+newfs $newfs_flags -n md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+###(cd ../testcases/swap; ./swap -t 5m -i 20) &
+cd $mntpoint
+$dir/indir_trunc
+s=$?
+while pkill swap; do :; done
+wait
+[ -f indir_trunc.core -a $s -eq 0 ] &&
+ { ls -l indir_trunc.core; mv indir_trunc.core $dir; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/indir_trunc
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#if defined(__LP64__)
+#define MASK 0x7ffffffffffffULL
+#else
+#define MASK 0xffffffff
+#endif
+#define PARALLEL 64
+#define RUNTIME (5 * 60)
+#define SYNC 0
+
+static int fd;
+
+static void *
+t1(void *data __unused)
+{
+ off_t offset;
+ time_t start;
+ char *cmdline[] = { "/usr/bin/true", NULL };
+
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ offset = arc4random();
+ offset = (offset << 32) | arc4random();
+ offset &= MASK;
+ if (lseek(fd, offset, SEEK_SET) == -1)
+ err(1, "lseek(%jd)", offset);
+ if (write(fd, "a", 1) != 1)
+ err(1, "write");
+ usleep(10);
+ if (arc4random() % 1000 < 2) {
+ if (execve(cmdline[0], cmdline, NULL) == -1)
+ err(1, "execve");
+ }
+ }
+
+ return (NULL);
+}
+static void *
+t2(void *data __unused)
+{
+ off_t offset, old;
+ time_t start;
+ void *p;
+ char *c;
+
+ old = 0;
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ if (old != 0)
+ munmap(p, old);
+ offset = arc4random();
+ offset = (offset << 32) | arc4random();
+ offset &= MASK;
+ if (ftruncate(fd, offset) == -1)
+ err(1, "ftruncate(%jd)", offset);
+ write(fd, "b", 1);
+ p = mmap(NULL, offset, PROT_READ | PROT_WRITE, MAP_SHARED,
+ fd, 0);
+ if (p == MAP_FAILED)
+ old = 0;
+ else {
+ old = offset;
+ c = p;
+ c[offset / 2] = 1;
+ if (offset > 0)
+ c[offset - 1] = 2;
+ }
+ usleep(10);
+ }
+ return (NULL);
+}
+
+static void
+test(void)
+{
+ pthread_t tid[2];
+ int r;
+ char file[80];
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ snprintf(file, sizeof(file), "file.%d", getpid());
+ if ((fd = open(file, O_RDWR | O_CREAT, DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+
+ if ((r = pthread_create(&tid[0], NULL, t1, NULL)) != 0)
+ errc(1, r, "pthread_create");
+ if ((r = pthread_create(&tid[1], NULL, t2, NULL)) != 0)
+ errc(1, r, "pthread_create");
+
+ if ((r = pthread_join(tid[0], NULL)) != 0)
+ errc(1, r, "pthread_join");
+ if ((r = pthread_join(tid[1], NULL)) != 0)
+ errc(1, r, "pthread_join");
+ close(fd);
+ unlink(file);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/inversion.sh b/tools/test/stress2/misc/inversion.sh
new file mode 100755
index 000000000000..759549af0233
--- /dev/null
+++ b/tools/test/stress2/misc/inversion.sh
@@ -0,0 +1,97 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Provokes a deadlock by lower priority process holding a lock and
+# never beeing run
+
+. ../default.cfg
+
+dir=$RUNDIR
+N=5 # Number of CPUs + 1
+M=25 # Number of lower priority jobs
+
+odir=`pwd`
+mkdir -p $dir || exit 1
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/inversion.c
+mycc -o inversion -Wall inversion.c
+rm -f inversion.c
+
+mp=`df $dir | tail -1 | awk '{print $NF}'`
+mp=`mount | grep "on $mp "`
+if echo $mp | grep -wq nfs; then
+ pgrep -q lockd || { echo "lockd not running"; exit 0; }
+fi
+
+for i in `jot $N`; do
+ ./inversion 600 &
+done
+
+while pgrep inversion > /dev/null; do
+ (
+ for i in `jot $M`; do
+ nice -n 20 lockf -s -t 0 .lock pwd > /dev/null &
+ done
+ for i in `jot $M`; do
+ wait
+ done
+ )
+done
+
+for i in `jot $N`; do
+ wait
+done
+rm -f inversion
+exit
+
+EOF
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+void
+handler(int i)
+{
+ exit(0);
+}
+
+int
+main(int argc, char **argv)
+{
+
+ int t;
+ if (argc == 2)
+ t = atoi(argv[1]);
+ else
+ t = 60;
+ signal(SIGALRM, handler);
+ alarm(t);
+ for (;;)
+ ;
+ return (0);
+}
diff --git a/tools/test/stress2/misc/isofs.sh b/tools/test/stress2/misc/isofs.sh
new file mode 100755
index 000000000000..5f5c26e00b4c
--- /dev/null
+++ b/tools/test/stress2/misc/isofs.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must not be root!" && exit 1
+
+[ -z "`type mkisofs 2>/dev/null`" ] && echo "mkisofs not found" && exit 0
+
+. ../default.cfg
+
+D=`dirname $diskimage`/dir
+I=`dirname $diskimage`/dir.iso
+export here=`pwd`
+cd /tmp
+
+mycc -o fstool $here/../tools/fstool.c
+
+rm -rf $D $I
+mkdir $D
+
+(cd $D; /tmp/fstool -n 10 -l -f 512)
+
+mkisofs -o $I -r $D > /dev/null 2>&1
+
+mdconfig -a -t vnode -f $I -u $mdstart
+mount -t cd9660 /dev/md$mdstart $mntpoint
+
+for i in `jot 64`; do
+ find /$mntpoint -type f > /dev/null 2>&1 &
+done
+wait
+
+umount $mntpoint
+mdconfig -d -u $mdstart
+
+rm -rf $D $I fstool
diff --git a/tools/test/stress2/misc/isofs2.sh b/tools/test/stress2/misc/isofs2.sh
new file mode 100755
index 000000000000..ad03e7e410b0
--- /dev/null
+++ b/tools/test/stress2/misc/isofs2.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Run program from isofs file system
+# "panic: userret: returning with the following locks held:"
+# https://people.freebsd.org/~pho/stress/log/isofs2.txt
+# Fixed by: r292772.
+
+[ `id -u ` -ne 0 ] && echo "Must not be root!" && exit 1
+
+[ -z "`type mkisofs 2>/dev/null`" ] && echo "mkisofs not found" && exit 0
+
+. ../default.cfg
+
+D=`dirname $diskimage`/dir
+I=`dirname $diskimage`/dir.iso
+here=`pwd`
+cd /tmp
+rm -rf $D $I
+mkdir $D
+cp `which date` $D
+mkisofs -o $I -r $D > /dev/null 2>&1
+mdconfig -a -t vnode -f $I -u $mdstart
+mount -t cd9660 /dev/md$mdstart $mntpoint
+
+cd $mntpoint
+./date > /dev/null
+cd $here
+
+umount $mntpoint
+mdconfig -d -u $mdstart
+
+rm -rf $D $I
diff --git a/tools/test/stress2/misc/isofs3.sh b/tools/test/stress2/misc/isofs3.sh
new file mode 100755
index 000000000000..476c729568ac
--- /dev/null
+++ b/tools/test/stress2/misc/isofs3.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC
+# 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 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.
+#
+
+# Simple isofs / union test scenario
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -z "`which mkisofs`" ] && echo "mkisofs not found" && exit 0
+
+. ../default.cfg
+
+D=`dirname $diskimage`/dir
+I=`dirname $diskimage`/dir.iso
+
+rm -rf $D $I
+mkdir -p $D
+cp -r ../../stress2 $D 2>/dev/null
+
+mkisofs -o $I -r $D > /dev/null 2>&1
+
+mount | grep -q /dev/md${mdstart}$part && umount -f /dev/md${mdstart}$part
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t vnode -f $I -u $mdstart || exit 1
+mount -t cd9660 /dev/md$mdstart $mntpoint || exit 1
+
+m2=$((mdstart + 1))
+mdconfig -s 1g -u $m2
+bsdlabel -w md$m2 auto
+newfs $newfs_flags md${m2}$part > /dev/null
+
+mount -o union /dev/md${m2}$part $mntpoint || exit 1
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=5m
+(cd $mntpoint/stress2; ./run.sh marcus.cfg) > /dev/null
+
+umount $mntpoint
+mdconfig -d -u $m2
+umount $mntpoint
+mdconfig -d -u $mdstart
+rm -rf $D $I
+exit 0
diff --git a/tools/test/stress2/misc/jail.sh b/tools/test/stress2/misc/jail.sh
new file mode 100755
index 000000000000..2c77033d10a2
--- /dev/null
+++ b/tools/test/stress2/misc/jail.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Regression test for problem found with the syscall.sh test
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > jail.c
+mycc -o jail -Wall jail.c
+rm -f jail.c
+cd $RUNDIR
+/tmp/jail
+rm -f /tmp/jail
+exit
+EOF
+#include <sys/param.h>
+#include <sys/jail.h>
+#include <err.h>
+
+int
+main()
+{
+ struct jail j;
+
+ j.version = 2;
+ j.path = (char *)0xa000000;
+ j.hostname = (char *)1;
+ j.jailname = (char *)0;
+ j.ip4s = 0;
+ j.ip6s = 0;
+ j.ip4 = (struct in_addr *)0x58000000;
+ j.ip6 = (struct in6_addr *)0x33000001;
+
+ if (jail(&j) == -1)
+ err(1, "jail()");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/jail2.sh b/tools/test/stress2/misc/jail2.sh
new file mode 100755
index 000000000000..7247eacb73b6
--- /dev/null
+++ b/tools/test/stress2/misc/jail2.sh
@@ -0,0 +1,76 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Regression test for problem found with the syscall.sh test
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > jail2.c
+mycc -o jail2 -Wall jail2.c
+rm -f jail2.c
+cd $odir
+/tmp/jail2
+rm -f /tmp/jail2
+exit
+EOF
+#include <sys/param.h>
+#include <sys/jail.h>
+#include <err.h>
+
+int
+main()
+{
+ struct jail j;
+
+ /*
+ version = 0x2,
+ path = 0x0,
+ hostname = 0x0,
+ jailname = 0x50000000 <Address 0x50000000 out of bounds>,
+ ip4s = 0xf7000004,
+ ip6s = 0x1,
+ ip4 = 0x0,
+ ip6 = 0x0
+ */
+ j.version = 2;
+ j.path = 0;
+ j.hostname = 0;
+ j.jailname = (char *)0x50000000;
+ j.ip4s = 0xf7000004;
+ j.ip6s = 1;
+ j.ip4 = 0;
+ j.ip6 = 0;
+
+ if (jail(&j) == -1)
+ err(1, "jail()");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/jail3.sh b/tools/test/stress2/misc/jail3.sh
new file mode 100755
index 000000000000..97bc078e8e89
--- /dev/null
+++ b/tools/test/stress2/misc/jail3.sh
@@ -0,0 +1,76 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Regression test for problem found with the syscall.sh test
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > jail3.c
+mycc -o jail3 -Wall jail3.c
+rm -f jail3.c
+cd $odir
+/tmp/jail3
+rm -f /tmp/jail3
+exit
+EOF
+#include <sys/param.h>
+#include <sys/jail.h>
+#include <err.h>
+
+int
+main()
+{
+ struct jail j;
+
+ /*
+ version = 0x0,
+ path = 0x2809cd61 <Address 0x2809cd61 out of bounds>,
+ hostname = 0x2809b650 <Address 0x2809b650 out of bounds>,
+ jailname = 0x0,
+ ip4s = 0x1,
+ ip6s = 0x0,
+ ip4 = 0x0,
+ ip6 = 0x0
+ */
+ j.version = 0;
+ j.path = (char *)0x2809cd61;
+ j.hostname = (char *)0x2809b650;
+ j.jailname = 0;
+ j.ip4s = 1;
+ j.ip6s = 0;
+ j.ip4 = 0;
+ j.ip6 = 0;
+
+ if (jail(&j) == -1)
+ err(1, "jail()");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/jail4.sh b/tools/test/stress2/misc/jail4.sh
new file mode 100755
index 000000000000..c641c957997d
--- /dev/null
+++ b/tools/test/stress2/misc/jail4.sh
@@ -0,0 +1,78 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Regression test for problem found with the syscall.sh test
+
+# "panic: kern_jail: too many iovecs (28)" seen.
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > jail4.c
+mycc -o jail4 -Wall jail4.c
+rm -f jail4.c
+cd $odir
+/tmp/jail4
+rm -f /tmp/jail4
+exit
+EOF
+#include <sys/param.h>
+#include <sys/jail.h>
+#include <err.h>
+
+int
+main()
+{
+ struct jail j;
+
+ /*
+ version = 0x2,
+ path = 0x28190cb1 <Address 0x28190cb1 out of bounds>,
+ hostname = 0x28167b90 <Address 0x28167b90 out of bounds>,
+ jailname = 0x28198700 <Address 0x28198700 out of bounds>,
+ ip4s = 0x0,
+ ip6s = 0x0,
+ ip4 = 0x0,
+ ip6 = 0x0}
+ */
+ j.version = 2;
+ j.path = (char *)0x28190cb1;
+ j.hostname = (char *)0x28167b90;
+ j.jailname = (char *)0x28198700;
+ j.ip4s = 0;
+ j.ip6s = 0;
+ j.ip4 = 0;
+ j.ip6 = 0;
+
+ if (jail(&j) == -1)
+ err(1, "jail()");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/jexec.sh b/tools/test/stress2/misc/jexec.sh
new file mode 100755
index 000000000000..c5c2ab605e08
--- /dev/null
+++ b/tools/test/stress2/misc/jexec.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+# Bug 238032 - "jexec pr_ref leak" by bz@
+# "foo DYING" seen.
+# Fixed by r358676
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+mount | grep "$mntpoint" | grep -q nfs && umount $mntpoint
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mntpoint
+
+here=`pwd`
+cd $mntpoint
+(cd $here/../testcases/swap; ./swap -t 2m -i 20) &
+sleep 10
+jail -c name=foo persist
+start=`date +%s`
+while [ $((`date +%s`- start)) -lt 120 ]; do
+ jexec foo <<-EOF
+ sleep .2
+EOF
+done > /dev/null 2>&1
+jail -r foo
+jls -dv | grep foo && s=1 || s=0
+
+cd $here
+for i in `jot 5`; do
+ umount $mntpoint && break
+ sleep 2
+done
+wait
+
+# Break into kgdb and type "show prison"
+# Check ref count growth
+
+exit $s
diff --git a/tools/test/stress2/misc/jumbo.sh b/tools/test/stress2/misc/jumbo.sh
new file mode 100755
index 000000000000..8dcc5aa7aca3
--- /dev/null
+++ b/tools/test/stress2/misc/jumbo.sh
@@ -0,0 +1,221 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Stress the jumbo mbuf allocation
+# vmstat -z | sed -n '1p;/jumbo/p'
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > jumbo.c
+mycc -o jumbo -Wall -Wextra -O2 jumbo.c || exit 1
+rm -f jumbo.c
+
+log=/tmp/mbuf.log
+nb=80
+host=localhost
+[ $# -eq 1 ] && host=$1
+(
+ for i in `jot $nb 0`; do
+ /tmp/jumbo $((12345 + $i)) &
+ done
+ sleep 30
+ for i in `jot $nb 0`; do
+ /tmp/jumbo $host $((12345 + $i)) &
+ done
+ for i in `jot $nb 0`; do
+ wait
+ done
+) > $log 2>&1
+
+rm -f /tmp/jumbo
+grep -q FAIL $log && { cat $log 1>&2; exit 1; }
+rm -f $log
+exit 0
+EOF
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define MX (1024 * 1024)
+#define RETRIES 5
+#define TIMEOUT 1200
+
+char *host;
+int loops, port, server;
+
+void
+ahandler(int s __unused)
+{
+ if (server)
+ fprintf(stderr, "FAIL Server timed out after %d loops.\n",
+ loops);
+ else
+ fprintf(stderr, "FAIL Client timed out after %d loops.\n",
+ loops);
+ _exit(0);
+}
+
+static void
+reader(void) {
+ socklen_t len;
+ struct sockaddr_in inetaddr, inetpeer;
+ int *buf, i, n, msgsock, on, tcpsock;
+
+ setproctitle("reader - init");
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ signal(SIGALRM, ahandler);
+ alarm(TIMEOUT);
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ if ((buf = malloc(MX)) == NULL)
+ err(1, "malloc(%d), %s:%d", MX, __FILE__, __LINE__);
+ setproctitle("reader");
+ for (i = 4096; i < MX; i += 1024) {
+ alarm(TIMEOUT);
+ if ((n = recvfrom(msgsock, buf, i, MSG_WAITALL, NULL,
+ NULL)) < 0) {
+ if (errno == EAGAIN)
+ continue;
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ }
+ if (n == 0)
+ break;
+
+ loops++;
+ }
+ close(msgsock);
+ _exit(0);
+}
+
+static void
+writer(void) {
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ int i, on, r, tcpsock;
+ char *line;
+
+ setproctitle("writer - init");
+ signal(SIGALRM, ahandler);
+ alarm(TIMEOUT);
+ on = 1;
+ for (i = 0; i < RETRIES; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ if ((hostent = gethostbyname (host)) == NULL)
+ err(1, "gethostbyname(%s)", host);
+ bzero((char *) &inetaddr, sizeof(inetaddr));
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(%s, %d), %s:%d", host, port, __FILE__,
+ __LINE__);
+
+ setproctitle("writer");
+ if ((line = malloc(MX)) == NULL)
+ err(1, "malloc(%d), %s:%d", MX, __FILE__, __LINE__);
+ alarm(TIMEOUT);
+ for (i = sysconf(_SC_PAGESIZE); i < MX; i += 1024) {
+ if (write(tcpsock, line, i) < 0)
+ err(1, "socket write(). %s:%d", __FILE__, __LINE__);
+ loops++;
+ }
+ close(tcpsock);
+
+ return;
+}
+
+int
+main(int argc, char **argv)
+{
+
+ if (argc == 2) {
+ server = 1;
+ port = atoi(argv[1]);
+ reader();
+ } else if (argc == 3) {
+ host = argv[1];
+ port = atoi(argv[2]);
+ writer();
+ } else
+ errx(1, "Usage: %s {<host>} <port number>\n", argv[0]);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/kern_umtx_inf_loop.sh b/tools/test/stress2/misc/kern_umtx_inf_loop.sh
new file mode 100755
index 000000000000..b8088814d7b2
--- /dev/null
+++ b/tools/test/stress2/misc/kern_umtx_inf_loop.sh
@@ -0,0 +1,148 @@
+#!/bin/sh
+
+# The program is a test case by Eric which demonstrates the
+# bug, unkillable spinning thread, owning a spinlock.
+
+# "panic: spin lock held too long" seen.
+# Fixed in r277970.
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > kern_umtx_inf_loop.c
+mycc -o kern_umtx_inf_loop -Wall -Wextra -O0 -g kern_umtx_inf_loop.c \
+ -lpthread || exit 1
+rm -f kern_umtx_inf_loop.c
+
+/tmp/kern_umtx_inf_loop
+
+rm -f /tmp/kern_umtx_inf_loop
+exit 0
+EOF
+/*-
+ * Copyright (c) 2015 Eric van Gyzen
+ * 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 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>
+#include <sys/types.h>
+
+#include <machine/cpufunc.h>
+#include <machine/cpu.h>
+
+#include <err.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+pthread_mutex_t the_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+pthread_t contender;
+
+static void *
+contender_func(void *arg)
+{
+ int error;
+
+ (void) arg;
+
+ error = pthread_mutex_lock(&the_mutex);
+ if (error) errc(1, error, "pthread_mutex_lock contender");
+
+ fprintf(stderr, "contender lock succeeded\n");
+
+ error = pthread_mutex_unlock(&the_mutex);
+ if (error) errc(1, error, "pthread_mutex_unlock contender");
+
+ fprintf(stderr, "contender unlock succeeded; exiting\n");
+
+ return (NULL);
+}
+
+static void *
+signaler_func(void *arg __unused)
+{
+ int error;
+
+ // Wait for the main thread to sleep.
+ usleep(100000);
+
+ error = pthread_kill(contender, SIGHUP);
+ if (error) errc(1, error, "pthread_kill");
+
+ // Wait for the contender to lock umtx_lock
+ // in umtx_repropagate_priority.
+ usleep(100000);
+
+ error = pthread_mutex_lock(&the_mutex);
+ if (error) errc(1, error, "pthread_mutex_lock signaler");
+
+ return (NULL);
+}
+
+int
+main(void)
+{
+ int error;
+
+ pthread_mutexattr_t mattr;
+
+ error = pthread_mutexattr_init(&mattr);
+ if (error) errc(1, error, "pthread_mutexattr_init");
+
+ error = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
+ if (error) errc(1, error, "pthread_mutexattr_setprotocol");
+
+ error = pthread_mutex_init(&the_mutex, &mattr);
+ if (error) errc(1, error, "pthread_mutex_init");
+
+ error = pthread_mutexattr_destroy(&mattr);
+ if (error) errc(1, error, "pthread_mutexattr_destroy");
+
+ //error = pthread_mutex_lock(&the_mutex);
+ //if (error) errc(1, error, "pthread_mutex_lock");
+
+ // Hack lock.
+ *(int *)the_mutex = pthread_getthreadid_np();
+
+ error = pthread_create(&contender, NULL, contender_func, NULL);
+ if (error) errc(1, error, "pthread_create");
+
+ // Wait for the contender to sleep.
+ usleep(100000);
+
+ pthread_t signaler;
+ error = pthread_create(&signaler, NULL, signaler_func, NULL);
+ if (error) errc(1, error, "pthread_create");
+
+ error = pthread_mutex_lock(&the_mutex);
+ if (error) errc(1, error, "pthread_mutex_lock recurse");
+
+ return (0);
+}
+
diff --git a/tools/test/stress2/misc/kevent.sh b/tools/test/stress2/misc/kevent.sh
new file mode 100755
index 000000000000..ebcaca639849
--- /dev/null
+++ b/tools/test/stress2/misc/kevent.sh
@@ -0,0 +1,174 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# panic: KN_INFLUX set when not suppose to be
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > kevent.c
+mycc -o kevent -Wall kevent.c -pthread || exit 1
+rm -f kevent.c
+[ -d "$RUNDIR" ] || mkdir -p $RUNDIR
+cd $RUNDIR
+
+for i in `jot 10`; do
+ for j in `jot 12`; do
+ /tmp/kevent > /dev/null 2>&1 &
+ done
+ wait
+done
+rm -f /tmp/kevent
+exit
+EOF
+#include <sys/types.h>
+#include <sys/event.h>
+
+#include <err.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
+static int waiting;
+
+static int fd1[2];
+static int fd2[2];
+static int fd3[2];
+
+#define RUNTIME 12
+
+static void *
+thr1(void *arg)
+{
+ struct kevent ev[3];
+ int kq, n, r;
+
+ if ((kq = kqueue()) < 0)
+ err(1, "kqueue(). %s:%d", __FILE__, __LINE__);
+
+ n = 0;
+ EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
+ EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
+ n++;
+ EV_SET(&ev[n], fd2[1], EVFILT_WRITE,
+ EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
+ n++;
+ EV_SET(&ev[n], fd3[1], EVFILT_WRITE,
+ EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
+ n++;
+
+ if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
+ err(1, "kevent(). %s:%d", __FILE__, __LINE__);
+
+ if ((r = pthread_mutex_lock(&mutex)) != 0)
+ errc(1, r, "pthread_mutex_lock");
+ waiting = 0;
+ if ((r = pthread_cond_signal(&cond)) != 0)
+ errc(1, r, "pthread_cond_signal");
+ if ((r = pthread_mutex_unlock(&mutex)) != 0)
+ errc(1, r, "pthread_mutex_unlock");
+
+ n = 0;
+ EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
+ EV_DELETE, 0, 0, 0);
+ n++;
+ if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
+ warn("kevent(). %s:%d", __FILE__, __LINE__);
+ close(kq);
+
+ return (0);
+}
+
+static void *
+thr2(void *arg)
+{
+ int r;
+
+ if ((r = pthread_mutex_lock(&mutex)) != 0)
+ errc(1, r, "pthread_mutex_lock");
+ while (waiting == 1) {
+ if ((r = pthread_cond_wait(&cond, &mutex)) != 0)
+ errc(1, r, "pthread_cond_wait");
+ }
+ if ((r = pthread_mutex_unlock(&mutex)) != 0)
+ errc(1, r, "pthread_mutex_unlock");
+ close(fd1[0]);
+ close(fd1[1]);
+ close(fd2[0]);
+ close(fd2[1]);
+ close(fd3[0]);
+ close(fd3[1]);
+ return (0);
+}
+
+int
+main(int argc, char **argv)
+{
+ pthread_t threads[2];
+ time_t start;
+ int r;
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ waiting = 1;
+ if (pipe(fd1) == -1)
+ err(1, "pipe()");
+ if (pipe(fd2) == -1)
+ err(1, "pipe()");
+ if (pipe(fd3) == -1)
+ err(1, "pipe()");
+
+ if ((r = pthread_mutex_init(&mutex, 0)) != 0)
+ errc(1, r, "pthread_mutex_init");
+ if ((r = pthread_cond_init(&cond, NULL)) != 0)
+ errc(1, r, "pthread_cond_init");
+
+ if ((r = pthread_create(&threads[0], NULL, thr1, 0)) != 0)
+ errc(1, r, "pthread_create()");
+ if ((r = pthread_create(&threads[1], NULL, thr2, 0)) != 0)
+ errc(1, r, "pthread_create()");
+
+ if ((r = pthread_join(threads[0], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", 0);
+ if ((r = pthread_join(threads[1], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", 1);
+ if ((r = pthread_mutex_destroy(&mutex)) != 0)
+ errc(1, r, "pthread_mutex_destroy");
+ if ((r = pthread_cond_destroy(&cond)) != 0)
+ errc(1, r, "pthread_cond_destroy)");
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/kevent10.sh b/tools/test/stress2/misc/kevent10.sh
new file mode 100755
index 000000000000..d746897cddcf
--- /dev/null
+++ b/tools/test/stress2/misc/kevent10.sh
@@ -0,0 +1,96 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Regression test for
+# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=217435
+# by Tim Newsham <tim newsham nccgroup trust>
+
+# panic: Assertion size > 0 failed at ../../../kern/subr_vmem.c:1082
+# cpuid = 2
+# time = 1501182301
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe0173117650
+# vpanic() at vpanic+0x19c/frame 0xfffffe01731176d0
+# kassert_panic() at kassert_panic+0x126/frame 0xfffffe0173117740
+# vmem_alloc() at vmem_alloc+0x11b/frame 0xfffffe0173117780
+# kmem_malloc() at kmem_malloc+0x33/frame 0xfffffe01731177b0
+# uma_large_malloc() at uma_large_malloc+0x48/frame 0xfffffe01731177f0
+# malloc() at malloc+0xe3/frame 0xfffffe0173117840
+# ktrgenio() at ktrgenio+0x60/frame 0xfffffe0173117880
+# sys_kevent() at sys_kevent+0x12f/frame 0xfffffe0173117930
+# amd64_syscall() at amd64_syscall+0x7d2/frame 0xfffffe0173117ab0
+
+# Fixed by r315155.
+
+. ../default.cfg
+
+cat > /tmp/kevent10.c <<EOF
+#include <sys/param.h>
+#include <sys/event.h>
+#include <sys/time.h>
+#include <sys/uio.h>
+#include <sys/ktrace.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ struct kevent changes;
+ struct kevent events;
+ char *fn = "/tmp/kevent10.trace";
+
+ if (open(fn, O_RDWR | O_CREAT, 0666) == -1)
+ err(1, "%s", fn);
+ if (ktrace(fn, KTRFLAG_DESCEND | KTROP_SET, KTRFAC_GENIO, 0) == -1)
+ err(1, "ktrace");
+ memset(&changes, 0, sizeof(struct kevent));
+ memset(&events, 0, sizeof(struct kevent));
+ if (kevent(0, &changes, -1, &events, 1, 0) == -1)
+ if (errno != EBADF)
+ err(1, "kevent");
+
+ return (0);
+}
+EOF
+
+mycc -o /tmp/kevent10 -Wall -Wextra -O2 /tmp/kevent10.c || exit 1
+rm /tmp/kevent10.c
+
+/tmp/kevent10
+s=$?
+
+rm /tmp/kevent10 /tmp/kevent10.trace
+exit $s
diff --git a/tools/test/stress2/misc/kevent11.sh b/tools/test/stress2/misc/kevent11.sh
new file mode 100755
index 000000000000..f3a319f2ecff
--- /dev/null
+++ b/tools/test/stress2/misc/kevent11.sh
@@ -0,0 +1,178 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Variation of kevent5.sh
+# OOM seen: https://people.freebsd.org/~pho/stress/log/mark018.txt
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/kevent11.c
+mycc -o kevent11 -Wall -Wextra -O0 -g kevent11.c || exit 1
+rm -f kevent11.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+set +e
+
+(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -h -l 100 > /dev/null) &
+cd $mntpoint
+$dir/kevent11
+s=$?
+[ -f kevent11.core -a $s -eq 0 ] &&
+ { ls -l kevent11.core; mv kevent11.core /tmp; s=1; }
+cd $odir
+while pkill -9 swap; do :; done
+wait
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1
+mdconfig -d -u $mdstart
+rm -rf $dir/kevent11
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/event.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+static char *file = "file";
+
+#define PARALLEL 256
+#define RUNTIME (3 * 60)
+#define SYNC 0
+
+static void
+test(void)
+{
+ struct kevent ev[2];
+ struct timespec ts;
+ int kq, fd, n;
+
+ if ((fd = open(file, O_RDONLY, 0)) == -1)
+ err(1, "open(%s). %s:%d", file, __func__, __LINE__);
+
+ if ((kq = kqueue()) < 0)
+ err(1, "kqueue()");
+
+ n = 0;
+ EV_SET(&ev[n], fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
+ NOTE_DELETE, 0, 0);
+ n++;
+
+ if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
+ err(1, "kevent()");
+
+ setproctitle("presync");
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ usleep(10000);
+ setproctitle("postsync");
+
+ memset(&ev, 0, sizeof(ev));
+ n = kevent(kq, NULL, 0, ev, 1, &ts);
+ close(fd);
+ close(kq);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, fd, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ if ((fd = open(file, O_CREAT | O_TRUNC | O_RDWR, 0660)) ==
+ -1)
+ err(1, "open(%s)", file);
+ close(fd);
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ while (share[SYNC] != PARALLEL)
+ usleep(10000);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s). %s:%d\n", file,
+ __FILE__, __LINE__);
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/kevent12.sh b/tools/test/stress2/misc/kevent12.sh
new file mode 100755
index 000000000000..3ae0085f1e5d
--- /dev/null
+++ b/tools/test/stress2/misc/kevent12.sh
@@ -0,0 +1,233 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Bug 228858 - panic when delivering knote to a process who has opened a
+# kqueue() is dying
+# Page fault seen: https://people.freebsd.org/~pho/stress/log/mark052.txt
+# Fixed by r340897.
+
+# Test scenario based on analysis by siddharthtuli gmail com
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > kevent12.c
+mycc -o kevent12 -Wall -Wextra -O2 -g kevent12.c || exit 1
+rm -f kevent12.c
+cd $odir
+
+(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -l 100 > /dev/null 2>&1) &
+pid=$!
+/tmp/kevent12
+while pgrep -q swap; do
+ pkill -9 swap
+done
+wait $pid
+
+rm -f /tmp/kevent12
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define PARALLEL 64
+#define RUNTIME (5 * 60)
+
+static pid_t parent;
+static int kq;
+
+static void
+hand(int i __unused) { /* handler */
+ kill(parent, SIGINT);
+ _exit(1);
+}
+
+static void
+init_kq()
+{
+ kq = kqueue();
+ if (kq == -1)
+ err(1, "kqueue");
+}
+
+static void
+add_watch(pid_t pid)
+{
+ struct kevent kev;
+ char msg[64];
+
+ bzero(&kev, sizeof(kev));
+ kev.ident = pid;
+ kev.flags = EV_ADD | EV_ENABLE;
+ kev.filter = EVFILT_PROC;
+ kev.fflags = NOTE_EXIT | NOTE_EXEC | NOTE_TRACK | NOTE_TRACKERR;
+
+ for (;;) {
+ int res = kevent(kq, &kev, 1, NULL, 0, NULL);
+ if (res == -1) {
+ if (errno == EINTR)
+ continue;
+ if (errno == ESRCH)
+ break;
+
+ snprintf(msg, sizeof(msg),
+ "kevent - add watch for pid %u", pid);
+ err(1, "%s", msg);
+ }
+ else
+ break;
+ }
+}
+
+static void
+polling()
+{
+ struct kevent kev[10];
+#if defined(DEBUG)
+ pid_t pid;
+ int i;
+#endif
+
+ for (;;) {
+ bzero(&kev, sizeof(kev));
+ if (arc4random() % 100 < 10) {
+ signal(SIGALRM, hand);
+ ualarm(10000, 0);
+ }
+ int res = kevent(kq, NULL, 0, kev,
+ sizeof(kev) / sizeof(kev[0]), NULL);
+ if (res == -1) {
+ if (errno == EINTR)
+ continue;
+
+ if (errno == ESRCH)
+ continue;
+
+ err(1, "kevent");
+ }
+
+#if defined(DEBUG)
+ for (i = 0; i < res; i++) {
+ pid = kev[i].ident;
+ if (kev[i].fflags & NOTE_CHILD) {
+ add_watch(pid);
+ printf("%u - new process, parent %u\n", pid,
+ (unsigned int)kev[i].data);
+ }
+ if (kev[i].fflags & NOTE_FORK) {
+ printf("%u forked\n", pid);
+ }
+ if (kev[i].fflags & NOTE_EXEC) {
+ printf("%u called exec\n", pid);
+ }
+ if (kev[i].fflags & NOTE_EXIT) {
+ printf("%u exited\n", pid);
+ if (parent == pid)
+ return;
+ }
+ if (kev[i].fflags & NOTE_TRACK) {
+ printf("%u forked - track\n", pid);
+ }
+ if (kev[i].fflags & NOTE_TRACKERR) {
+ fprintf(stderr, "%u - track error\n", pid);
+ }
+ }
+#endif
+ }
+}
+
+void
+churn(void)
+{
+ pid_t pid;
+ time_t start;
+ char *cmdline[] = { "/usr/bin/true", NULL };
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ if ((pid = fork()) == -1)
+ err(1, "fork");
+ if (pid == 0) {
+ if (execve(cmdline[0], cmdline, NULL) == -1)
+ err(1, "execve");
+ }
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid(%d):%d", pid, __LINE__);
+ }
+
+ _exit(0);
+}
+
+int
+test(void)
+{
+ if ((parent = fork()) == 0)
+ churn();
+
+ init_kq();
+ add_watch(parent);
+ polling();
+ if (waitpid(parent, NULL, 0) != parent)
+ err(1, "waitpid(%d):%d", parent, __LINE__);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ time_t start;
+ int i;
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ for (i = 0; i < PARALLEL; i++)
+ if (waitpid(pids[i], NULL, 0) != pids[i])
+ err(1, "waitpid(%d):%d", pids[i], __LINE__);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/kevent13.sh b/tools/test/stress2/misc/kevent13.sh
new file mode 100755
index 000000000000..82a7a10b983c
--- /dev/null
+++ b/tools/test/stress2/misc/kevent13.sh
@@ -0,0 +1,141 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Test based on scenario by Babcia Padlina
+
+# "panic: mutex pipe mutex not owned at sys_pipe.c:1769" seen:
+# https://people.freebsd.org/~pho/stress/log/kevent13.txt
+# Fixed by r235640.
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > kevent13.c
+cc -o kevent13 -Wall kevent13.c -lpthread
+rm -f kevent13.c
+
+[ -d "$RUNDIR" ] || mkdir -p $RUNDIR
+cd $RUNDIR
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 300 ]; do
+ for i in `jot 10`; do
+ /tmp/kevent13 &
+ /tmp/kevent13 &
+ wait
+ done
+done
+
+rm -f /tmp/kevent13
+exit 0
+EOF
+/*
+ * 29.08.2009, babcia padlina
+ */
+
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/param.h>
+#include <sys/timespec.h>
+#include <unistd.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define LOOPS 200000
+
+struct kevent kev[2], ke[2];
+struct timespec timeout;
+volatile int go;
+int fd[2], kq;
+
+void
+do_thread(void) {
+ int i;
+
+ go = 1;
+ for (i = 0; i < LOOPS; i++) {
+ if (pipe(fd) < 0)
+ err(1, "pipe");
+ memset(&kev, 0, sizeof(kev));
+ EV_SET(&kev[0], fd[0], EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, NULL);
+ EV_SET(&kev[1], fd[1], EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, NULL);
+
+ if (kevent(kq, kev, 2, ke, 2, &timeout) < 0)
+ err(1, "kevent");
+
+ close(fd[0]);
+ close(fd[1]);
+ }
+}
+
+void
+do_thread2(void) {
+ int i;
+
+ while (go == 0)
+ usleep(10);
+ for (i = 0; i < LOOPS; i++) {
+ if (arc4random() % 2 == 0) {
+ close(fd[0]);
+ close(fd[1]);
+ } else {
+ close(fd[1]);
+ close(fd[0]);
+ }
+ }
+}
+
+int
+main(void) {
+ pthread_t pth, pth2;
+ int r;
+
+ if ((kq = kqueue()) < 0)
+ err(1, "kqueue");
+
+ if ((r = pthread_create(&pth, NULL, (void *)do_thread, NULL)) != 0)
+ errc(1, r, "pthread_create");
+ if ((r = pthread_create(&pth2, NULL, (void *)do_thread2, NULL)) != 0)
+ errc(1, r, "pthread_create");
+
+ timeout.tv_sec = 0;
+ timeout.tv_nsec = 1;
+
+ if ((r = pthread_join(pth, NULL)) != 0)
+ errc(1, r, "pthread_join");
+ if ((r = pthread_join(pth2, NULL)) != 0)
+ errc(1, r, "pthread_join");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/kevent14.sh b/tools/test/stress2/misc/kevent14.sh
new file mode 100755
index 000000000000..2531b15caecf
--- /dev/null
+++ b/tools/test/stress2/misc/kevent14.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# "panic: Bad tailq NEXT(0xfffff815a589e238->tqh_last) != NULL" seen.
+# https://people.freebsd.org/~pho/stress/log/kevent14.txt
+# Fixed by r340734.
+
+# Test scenario by: Mark Johnston <markj@freebsd.org>
+
+cat > /tmp/kevent14.c <<EOF
+#include <sys/event.h>
+
+#include <err.h>
+#include <time.h>
+#include <stdlib.h>
+
+int
+main(void)
+{
+ struct kevent ev[2];
+ time_t start;
+ int kq, ret;
+
+ kq = kqueue();
+ if (kq < 0)
+ err(1, "kqueue");
+
+ EV_SET(&ev[0], 42, EVFILT_TIMER, EV_ADD | EV_ENABLE | EV_ONESHOT, 0,
+ 0, 0);
+
+ start = time(NULL);
+ while (time(NULL) - start < 120) {
+ ret = kevent(kq, &ev[0], 1, &ev[1], 1, NULL);
+ if (ret < 0)
+ err(1, "kevent");
+ if (ret == 0)
+ errx(1, "no events");
+ if (ret == 1 && (ev[1].flags & EV_ERROR) != 0)
+ errc(1, ev[1].data, "kevent");
+ }
+
+ return (0);
+}
+EOF
+cc -o /tmp/kevent14 -Wall -Wextra -g -O2 /tmp/kevent14.c
+rm /tmp/kevent14.c
+
+/tmp/kevent14
+
+rm -f /tmp/kevent14
+exit 0
diff --git a/tools/test/stress2/misc/kevent15.sh b/tools/test/stress2/misc/kevent15.sh
new file mode 100755
index 000000000000..52925365c865
--- /dev/null
+++ b/tools/test/stress2/misc/kevent15.sh
@@ -0,0 +1,161 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# No problems seen.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/kevent15.c
+mycc -o kevent15 -Wall -Wextra -O0 -g kevent15.c || exit 1
+rm -f kevent15.c
+cd $odir
+
+(cd ../testcases/swap; ./swap -t 3m -i 20 -l 80) &
+sleep 2
+cd $dir
+timeout 5m ./kevent15
+s=$?
+while pkill swap; do sleep .1; done
+wait
+[ -f kevent15.core -a $s -eq 0 ] &&
+ { ls -l kevent15.core; s=1; }
+cd $odir
+
+rm -rf $dir/kevent15
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/event.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdatomic.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static _Atomic(int) *share;
+
+#define PARALLEL 800
+#define RUNTIME (3 * 60)
+#define SYNC 0
+#define ACT 1
+
+static void
+handler(int i __unused) {
+ _exit(0);
+}
+
+static void
+test(void)
+{
+ struct kevent ev[2];
+ struct timespec ts;
+ time_t start;
+ int kq, ret;
+
+ signal(SIGUSR1, handler);
+ (void)atomic_fetch_add(&share[SYNC], 1);
+ while (atomic_load(&share[SYNC]) != PARALLEL)
+ usleep(1);
+ (void)atomic_fetch_add(&share[ACT], 1);
+
+ if ((kq = kqueue()) < 0)
+ err(1, "kqueue");
+
+ EV_SET(&ev[0], 42, EVFILT_TIMER, EV_ADD | EV_ENABLE | EV_ONESHOT, 0,
+ 0, 0);
+
+ start = time(NULL);
+ while (time(NULL) - start < 30) {
+ ts.tv_sec = 0;
+ ts.tv_nsec = arc4random() % 2000;
+ if ((ret = kevent(kq, &ev[0], 1, &ev[1], 1, &ts)) == -1)
+ err(1, "kevent");
+ if (ret == 1 && (ev[1].flags & EV_ERROR) != 0)
+ errc(1, ev[1].data, "kevent");
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int i, status;
+
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ share[ACT] = share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ while (share[ACT] != PARALLEL)
+ usleep(1);
+ usleep(arc4random() % 50000);
+ for (i = 0; i < PARALLEL; i++) {
+ if (kill(pids[i], SIGUSR1) == -1)
+ err(1, "kill");
+ }
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status) && WTERMSIG(status) != 2)
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ }
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/kevent2.sh b/tools/test/stress2/misc/kevent2.sh
new file mode 100755
index 000000000000..b2ae2a97e422
--- /dev/null
+++ b/tools/test/stress2/misc/kevent2.sh
@@ -0,0 +1,170 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > kevent2.c
+mycc -o kevent2 -Wall kevent2.c -pthread || exit 1
+rm -f kevent2.c
+cd $RUNDIR
+
+for i in `jot 10`; do
+ for j in `jot 12`; do
+ /tmp/kevent2 > /dev/null 2>&1 &
+ done
+ wait
+done
+rm -f /tmp/kevent2
+exit
+EOF
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/time.h>
+
+#include <err.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
+static int waiting;
+
+static int fd1[2];
+static int fd2[2];
+static int fd3[2];
+
+static void *
+thr1(void *arg)
+{
+ struct kevent ev[3];
+ struct timespec ts;
+ int kq, n, r;
+
+ if ((kq = kqueue()) < 0)
+ err(1, "kqueue(). %s:%d", __FILE__, __LINE__);
+
+ n = 0;
+ EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
+ EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
+ n++;
+ EV_SET(&ev[n], fd2[1], EVFILT_WRITE,
+ EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
+ n++;
+ EV_SET(&ev[n], fd3[1], EVFILT_WRITE,
+ EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
+ n++;
+
+ if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
+ err(1, "kevent(). %s:%d", __FILE__, __LINE__);
+
+ if ((r = pthread_mutex_lock(&mutex)) != 0)
+ errc(1, r, "pthread_mutex_lock");
+ waiting = 0;
+ if ((r = pthread_cond_signal(&cond)) != 0)
+ errc(1, r, "pthread_cond_signal");
+ if ((r = pthread_mutex_unlock(&mutex)) != 0)
+ errc(1, r, "pthread_mutex_unlock");
+
+ n = 0;
+ EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
+ EV_DELETE, 0, 0, 0);
+ n++;
+ ts.tv_sec = 0;
+ ts.tv_nsec = 0;
+ if (kevent(kq, ev, n, NULL, 0, &ts) < 0)
+ err(1, "kevent(). %s:%d", __FILE__, __LINE__);
+ close(kq);
+
+ close(fd1[1]);
+ close(fd2[1]);
+ close(fd3[1]);
+ return (0);
+}
+
+static void *
+thr2(void *arg)
+{
+ int r;
+
+ if ((r = pthread_mutex_lock(&mutex)) != 0)
+ errc(1, r, "pthread_mutex_lock");
+ while (waiting == 1) {
+ if ((r = pthread_cond_wait(&cond, &mutex)) != 0)
+ errc(1, r, "pthread_cond_wait");
+ }
+ if ((r = pthread_mutex_unlock(&mutex)) != 0)
+ errc(1, r, "pthread_mutex_unlock");
+ close(fd1[0]);
+ close(fd2[0]);
+ close(fd3[0]);
+ return (0);
+}
+
+int
+main(int argc, char **argv)
+{
+ pthread_t threads[2];
+ int i, r;
+
+ for (i = 0; i < 1000; i++) {
+ waiting = 1;
+ if (pipe(fd1) == -1)
+ err(1, "pipe()");
+ if (pipe(fd2) == -1)
+ err(1, "pipe()");
+ if (pipe(fd3) == -1)
+ err(1, "pipe()");
+
+ if ((r = pthread_mutex_init(&mutex, 0)) != 0)
+ errc(1, r, "pthread_mutex_init");
+ if ((r = pthread_cond_init(&cond, NULL)) != 0)
+ errc(1, r, "pthread_cond_init");
+
+ if ((r = pthread_create(&threads[0], NULL, thr1, 0)) != 0)
+ errc(1, r, "pthread_create()");
+ if ((r = pthread_create(&threads[1], NULL, thr2, 0)) != 0)
+ errc(1, r, "pthread_create()");
+
+ if ((r = pthread_join(threads[0], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", 0);
+ if ((r = pthread_join(threads[1], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", 1);
+ if ((r = pthread_mutex_destroy(&mutex)) != 0)
+ errc(1, r, "pthread_mutex_destroy");
+ if ((r = pthread_cond_destroy(&cond)) != 0)
+ errc(1, r, "pthread_condattr_destroy");
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/kevent3.sh b/tools/test/stress2/misc/kevent3.sh
new file mode 100755
index 000000000000..0ee9706a27b1
--- /dev/null
+++ b/tools/test/stress2/misc/kevent3.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > kevent3.c
+mycc -o kevent3 -Wall kevent3.c -pthread || exit 1
+rm -f kevent3.c
+cd $RUNDIR
+
+for i in `jot 64`; do
+ for j in `jot 12`; do
+ /tmp/kevent3 > /dev/null 2>&1 &
+ done
+ for j in `jot 12`; do
+ wait
+ done
+done
+rm -f /tmp/kevent3
+exit 0
+EOF
+/*
+ * Obtained from:
+ * http://projects.info-pull.com/mokb/MOKB-24-11-2006.html
+ */
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/time.h>
+
+#include <stdio.h>
+#include <unistd.h>
+
+int main(void)
+{
+ struct kevent ke;
+ int kq;
+
+ kq = kqueue();
+ EV_SET(&ke, getpid(), EVFILT_PROC, EV_ADD,
+ NOTE_EXIT|NOTE_EXEC|NOTE_TRACK, 0, NULL);
+ kevent(kq, &ke, 1, NULL, 0, NULL);
+ if (fork() != 0)
+ kevent(kq, NULL, 0, &ke, 1, NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/kevent4.sh b/tools/test/stress2/misc/kevent4.sh
new file mode 100755
index 000000000000..87f421322ed5
--- /dev/null
+++ b/tools/test/stress2/misc/kevent4.sh
@@ -0,0 +1,174 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Test scenario by kib@
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > kevent4.c
+mycc -o kevent4 -Wall kevent4.c -pthread || exit 1
+rm -f kevent4.c
+
+cd $odir
+export runRUNTIME=3m
+(cd ..; ./run.sh > /dev/null 2>&1) &
+rpid=$!
+
+(cd $RUNDIR; /tmp/kevent4 $rpid > /dev/null) &
+
+sleep 120
+kill $rpid
+../tools/killall.sh > /dev/null 2>&1
+kill $!
+wait
+rm -f /tmp/kevent4
+
+exit
+EOF
+#include <sys/types.h>
+#include <sys/event.h>
+
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#ifndef true
+# define true 1
+#endif
+
+static int kq;
+
+static void
+err(const char *msg, int err_no)
+{
+ fprintf(stderr, "%s: %s\n", msg, strerror(err_no));
+ exit(1);
+}
+
+static void
+init_kq()
+{
+ kq = kqueue();
+ if (kq == -1)
+ err("kqueue", errno);
+}
+
+static void
+add_watch(pid_t pid)
+{
+ struct kevent kev;
+
+ bzero(&kev, sizeof(kev));
+ kev.ident = pid;
+ kev.flags = EV_ADD | EV_ENABLE;
+ kev.filter = EVFILT_PROC;
+ kev.fflags = NOTE_EXIT | NOTE_FORK | NOTE_EXEC | NOTE_TRACK;
+
+ while (true) {
+ int res = kevent(kq, &kev, 1, NULL, 0, NULL);
+ if (res == -1) {
+ if (errno == EINTR)
+ continue;
+ if (errno == ESRCH)
+ break;
+
+ int err_no = errno;
+ char msg[64];
+ snprintf(msg, sizeof(msg),
+ "kevent - add watch for pid %u", pid);
+ err(msg, err_no);
+ }
+ else
+ break;
+ }
+}
+
+static void polling()
+{
+ struct kevent kev[10];
+ pid_t pid;
+ int i;
+
+ while (true) {
+ bzero(&kev, sizeof(kev));
+ int res = kevent(kq, NULL, 0, kev,
+ sizeof(kev) / sizeof(kev[0]), NULL);
+ if (res == -1) {
+ if (errno == EINTR)
+ continue;
+
+ if (errno == ESRCH)
+ continue;
+
+ err("kevent", errno);
+ }
+
+ for (i = 0; i < res; i++) {
+ pid = kev[i].ident;
+ if (kev[i].fflags & NOTE_CHILD) {
+ add_watch(pid);
+ printf("%u - new process, parent %u\n", pid,
+ (unsigned int)kev[i].data);
+ }
+ if (kev[i].fflags & NOTE_FORK) {
+ printf("%u forked\n", pid);
+ }
+ if (kev[i].fflags & NOTE_EXEC) {
+ printf("%u called exec\n", pid);
+ }
+ if (kev[i].fflags & NOTE_EXIT) {
+ printf("%u exited\n", pid);
+ }
+ if (kev[i].fflags & NOTE_TRACK) {
+ printf("%u forked - track\n", pid);
+ }
+ if (kev[i].fflags & NOTE_TRACKERR) {
+ fprintf(stderr, "%u - track error\n", pid);
+ }
+ }
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ if (argc != 2) {
+ fprintf(stderr, "pid ?\n");
+ return (2);
+ }
+ pid_t parent = atoi(argv[1]);
+
+ init_kq();
+ add_watch(parent);
+ polling();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/kevent5.sh b/tools/test/stress2/misc/kevent5.sh
new file mode 100755
index 000000000000..35a9e6e78e72
--- /dev/null
+++ b/tools/test/stress2/misc/kevent5.sh
@@ -0,0 +1,183 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Test EVFILT_VNODE. Found page fault in knlist_add+0x39
+# Test scenario by kib@
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > kevent5.c
+mycc -o kevent5 -Wall -Wextra -O2 -g kevent5.c || exit 1
+rm -f kevent5.c
+
+[ -d $RUNDIR ] || mkdir -p $RUNDIR
+cd $RUNDIR
+/tmp/kevent5 kevent5.xxx kevent5.yyy
+s=$?
+
+rm -f /tmp/kevent5 kevent.xxx kevent.yyy
+
+exit $s
+EOF
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/event.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+volatile u_int *share;
+
+static char *file1, *file2;
+
+#define N 1000
+#define SYNC 0
+
+static void
+test(void) {
+
+ struct kevent ev[2];
+ struct timespec ts;
+ int fd, kq, n;
+
+ if ((fd = open(file1, O_RDONLY, 0)) == -1)
+ err(1, "open(%s)(2)", file1);
+ atomic_add_int(&share[SYNC], 1);
+
+ if ((kq = kqueue()) < 0)
+ err(1, "kqueue()");
+
+ n = 0;
+ EV_SET(&ev[n], fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
+ NOTE_DELETE, 0, 0);
+ n++;
+
+ ts.tv_sec = 5;
+ ts.tv_nsec = 0;
+
+ if (kevent(kq, ev, n, NULL, 0, &ts) < 0)
+ err(1, "kevent()");
+
+ memset(&ev, 0, sizeof(ev));
+ n = kevent(kq, NULL, 0, ev, 1, NULL);
+ close(fd);
+ close(kq);
+
+/* Once the rendezvous file is gone create a new kevent */
+
+ if ((fd = open(file2, O_RDONLY, 0)) == -1)
+ err(1, "open(%s)(2)", file2);
+
+ if ((kq = kqueue()) < 0)
+ err(1, "kqueue()");
+
+ n = 0;
+ EV_SET(&ev[n], fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
+ NOTE_DELETE, 0, 0);
+ n++;
+
+ if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
+ err(1, "kevent()");
+
+ memset(&ev, 0, sizeof(ev));
+ n = kevent(kq, NULL, 0, ev, 1, &ts);
+ close(fd);
+ close(kq);
+}
+
+int
+main(int argc, char **argv) {
+ size_t len;
+ int e, fd, i, j, status;
+
+ if (argc != 3) {
+ fprintf(stderr, "Usage: %s <rendezvous file> <tail file>\n",
+ argv[0]);
+ return (1);
+ }
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ e = 0;
+ file1 = argv[1];
+ file2 = argv[2];
+
+ for (j = 0; j < 100 && e == 0; j++) {
+ if ((fd = open(file1, O_CREAT | O_TRUNC | O_RDWR, 0660)) ==
+ -1)
+ err(1, "open(%s)", file1);
+ close(fd);
+ if ((fd = open(file2, O_CREAT | O_TRUNC | O_RDWR, 0660)) ==
+ -1)
+ err(1, "open(%s)", file2);
+ close(fd);
+
+ share[SYNC] = 0;
+ for (i = 0; i < N; i++) {
+ if (fork() == 0) {
+ test();
+ return (0);
+ }
+ }
+
+ while (share[SYNC] != N)
+ usleep(200);
+
+ sleep(1);
+ if (unlink(file1) == -1)
+ err(1, "unlink(%s). %s:%d\n", file1, __FILE__,
+ __LINE__);
+ sleep(2);
+ if (unlink(file2) == -1)
+ err(1, "unlink(%s). %s:%d\n", file2, __FILE__,
+ __LINE__);
+
+ for (i = 0; i < N; i++) {
+ if (wait(&status) == -1)
+ err(1, "wait(), %s:%d", __FILE__, __LINE__);
+ if (status != 0)
+ e = 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/kevent6.sh b/tools/test/stress2/misc/kevent6.sh
new file mode 100755
index 000000000000..528bde5e78a1
--- /dev/null
+++ b/tools/test/stress2/misc/kevent6.sh
@@ -0,0 +1,168 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# "Sleeping thread (tid 101094, pid 13104) owns a non-sleepable lock" seen.
+# Fixed in r255798.
+
+# panic: Memory modified after free:
+# https://people.freebsd.org/~pho/stress/log/alan011.txt
+# https://people.freebsd.org/~pho/stress/log/kevent6.txt
+# Fixed by: r286921
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > kevent6.c
+mycc -o kevent6 -Wall -Wextra -O2 -g kevent6.c -lpthread || exit 1
+rm -f kevent6.c
+cd $odir
+
+mount | grep "on $mntpoint " | grep -q md$mdstart && umount -f $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c "(cd $mntpoint; /tmp/kevent6)"
+
+while mount | grep -q $mntpoint; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+
+mount -o size=1g -t tmpfs tmpfs $mntpoint
+chmod 777 $mntpoint
+su $testuser -c "(cd $mntpoint; /tmp/kevent6)"
+while mount | grep -q $mntpoint; do
+ umount $mntpoint || sleep 1
+done
+rm -f /tmp/kevent6
+
+exit
+EOF
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define LOOPS 500000
+#define PARALLEL 8
+
+static int fd;
+static char path[80];
+
+static void *
+spin(void *arg __unused)
+{
+ int i;
+
+ for (i= 0;; i++) {
+ snprintf(path, sizeof(path), "file.%06d.%d", getpid(), i);
+ if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0622)) ==
+ -1)
+ err(1, "creat()");
+ close(fd);
+ usleep(10000);
+ fd = 0;
+ unlink(path);
+ }
+ return (NULL);
+}
+
+static void *
+test(void *arg __unused)
+{
+ int kq;
+ int i, n;
+ struct kevent ev;
+ struct timespec ts;
+
+ for (i = 0; i < LOOPS; i++) {
+ if ((kq = kqueue()) < 0)
+ err(1, "kqueue()");
+
+ n = 0;
+ memset(&ev, 0, sizeof(ev));
+ EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
+ NOTE_DELETE|NOTE_RENAME|NOTE_EXTEND, 0, 0);
+ n++;
+
+ if (kevent(kq, &ev, n, NULL, 0, NULL) < 0) {
+ close(kq);
+ continue;
+ }
+
+ memset(&ev, 0, sizeof(ev));
+ ts.tv_sec = 0;
+ ts.tv_nsec = 1000000;
+ if ((n = kevent(kq, NULL, 0, &ev, 1, &ts)) == -1)
+ err(1, "kevent()");
+
+ close(kq);
+ }
+ return (NULL);
+}
+
+int
+main(void)
+{
+ pthread_t cp[PARALLEL], sp;
+ int e, i;
+
+ if ((e = pthread_create(&sp, NULL, spin, NULL)) != 0)
+ errc(1, e, "pthread_create");
+
+ for (i = 0; i < PARALLEL; i++) {
+ if ((e = pthread_create(&cp[i], NULL, test, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ }
+
+ for (i = 0; i < PARALLEL; i++)
+ pthread_join(cp[i], NULL);
+
+ close(fd);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/kevent7.sh b/tools/test/stress2/misc/kevent7.sh
new file mode 100755
index 000000000000..9383918ea134
--- /dev/null
+++ b/tools/test/stress2/misc/kevent7.sh
@@ -0,0 +1,281 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Threaded syscall(2) fuzz test inspired by the iknowthis test suite
+# by Tavis Ormandy <taviso cmpxchg8b com>
+
+# kevent(2) with random arguments.
+# Spinning threads seen.
+# Fixed in r255877.
+
+# "panic: softclock_call_cc: act 0xfffff801219a0840 0" seen:
+# https://people.freebsd.org/~pho/stress/log/kevent7.txt
+# Fixed by r315289
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+ulimit -t 200
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > kevent7.c
+rm -f /tmp/kevent7
+mycc -o kevent7 -Wall -Wextra -O2 -g kevent7.c -lpthread || exit 1
+rm -f kevent7.c
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+for i in `jot 5`; do
+ (cd $mntpoint; /tmp/kevent7 $* < /dev/null) &
+ sleep 60
+ while pgrep -q kevent7; do
+ pkill -9 kevent7
+ sleep 1
+ done
+done
+
+for i in `jot 5`; do
+ mount | grep -q md${mdstart}$part && \
+ umount $mntpoint && mdconfig -d -u $mdstart && break
+ sleep 10
+done
+if mount | grep -q md${mdstart}$part; then
+ fstat $mntpoint
+ echo "umount $mntpoint failed"
+ exit 1
+fi
+rm -f /tmp/kevent7
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <libutil.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <signal.h>
+#include <sys/socket.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define THREADS 50
+
+int fd[900], fds[2], socketpr[2];
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+static u_int32_t r[N];
+static int syscallno;
+
+static void
+hand(int i __unused) { /* handler */
+ _exit(1);
+}
+
+static unsigned long
+makearg(void)
+{
+ unsigned int i;
+ unsigned long val;
+
+ val = arc4random();
+ i = arc4random() % 100;
+ if (i < 20)
+ val = val & 0xff;
+ if (i >= 20 && i < 40)
+ val = val & 0xffff;
+ if (i >= 40 && i < 60)
+ val = (unsigned long)(r) | (val & 0xffff);
+#if defined(__LP64__)
+ if (i >= 60) {
+ val = (val << 32) | arc4random();
+ if (i > 80)
+ val = val & 0x00007fffffffffffUL;
+ }
+#endif
+
+ return(val);
+}
+
+static void *
+test(void *arg __unused)
+{
+
+ FTS *fts;
+ FTSENT *p;
+ int ftsoptions;
+ int i;
+ char *args[5];
+
+ ftsoptions = FTS_PHYSICAL;
+ args[0] = "/dev";
+ args[1] = "/proc";
+ args[2] = "/usr/compat/linux/proc";
+ args[3] = ".";
+ args[4] = 0;
+
+ for (;;) {
+ for (i = 0; i < N; i++)
+ r[i] = arc4random();
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ i = 0;
+ while ((p = fts_read(fts)) != NULL) {
+ if (fd[i] > 0)
+ close(fd[i]);
+ if ((fd[i] = open(p->fts_path, O_RDWR)) == -1)
+ if ((fd[i] = open(p->fts_path, O_WRONLY)) ==
+ -1)
+ if ((fd[i] = open(p->fts_path,
+ O_RDONLY)) == -1)
+ continue;
+ i++;
+ i = i % nitems(fd);
+ }
+
+ if (fts_close(fts) == -1)
+ if (errno != ENOTDIR)
+ warn("fts_close()");
+ if (pipe(fds) == -1)
+ err(1, "pipe()");
+ if (socketpair(PF_UNIX, SOCK_SEQPACKET, 0, socketpr) == -1)
+ err(1, "socketpair()");
+ sleep(1);
+ close(socketpr[0]);
+ close(socketpr[1]);
+ close(fds[0]);
+ close(fds[1]);
+ }
+ return(0);
+}
+
+static void *
+calls(void *arg __unused)
+{
+ unsigned long arg1, arg2, arg3, arg4, arg5, arg6, arg7;
+ int i, kq, num;
+
+ if ((kq = kqueue()) < 0)
+ err(1, "kqueue()");
+ for (i = 0; i < 1000; i++) {
+ if (i == 0)
+ usleep(1000);
+ num = syscallno;
+ arg1 = makearg();
+ arg2 = makearg();
+ arg3 = makearg();
+ arg4 = makearg();
+ arg5 = makearg();
+ arg6 = makearg();
+ arg7 = makearg();
+
+#if 0
+ fprintf(stderr, "%2d : syscall(%3d, %lx, %lx, %lx, %lx, %lx,"
+ " %lx, %lx)\n",
+ i, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
+#endif
+ alarm(1);
+ syscall(num, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
+ num = 0;
+ }
+ close(kq);
+
+ return (0);
+}
+
+int
+main(void)
+{
+ struct passwd *pw;
+ time_t start;
+ pthread_t rp, cp[THREADS];
+ int e, j, n;
+
+ if ((pw = getpwnam("nobody")) == NULL)
+ err(1, "no such user: nobody");
+
+ if (setgroups(1, &pw->pw_gid) ||
+ setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
+ seteuid(pw->pw_uid) || setuid(pw->pw_uid))
+ err(1, "Can't drop privileges to \"nobody\"");
+ endpwent();
+
+ signal(SIGALRM, hand);
+ signal(SIGILL, hand);
+ signal(SIGFPE, hand);
+ signal(SIGSEGV, hand);
+ signal(SIGBUS, hand);
+ signal(SIGURG, hand);
+ signal(SIGSYS, hand);
+ signal(SIGTRAP, hand);
+
+ syscallno = SYS_kevent;
+
+ n = 0;
+ start = time(NULL);
+ while (time(NULL) - start < 120) {
+ if (fork() == 0) {
+ if ((e = pthread_create(&rp, NULL, test, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ usleep(1000);
+ for (j = 0; j < THREADS; j++) {
+ if ((e = pthread_create(&cp[j], NULL, calls,
+ NULL)) != 0)
+ errc(1, e, "pthread_create");
+ }
+ for (j = 0; j < THREADS; j++)
+ pthread_join(cp[j], NULL);
+ if ((e = pthread_kill(rp, SIGINT)) != 0)
+ errc(1, e, "pthread_kill");
+ _exit(0);
+ }
+ wait(NULL);
+ if (n++ > 5000)
+ break;
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/kevent8.sh b/tools/test/stress2/misc/kevent8.sh
new file mode 100755
index 000000000000..66007eb5a2b1
--- /dev/null
+++ b/tools/test/stress2/misc/kevent8.sh
@@ -0,0 +1,163 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Out of VM seen due to missing close of kqueue handle.
+# Fixed in 256849
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+ulimit -k 5000 || { echo FAIL; exit 1; }
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > kevent8.c
+mycc -o kevent8 -Wall -Wextra -O2 -g kevent8.c -lpthread || exit 1
+rm -f kevent8.c
+cd $odir
+
+mount | grep "on $mntpoint " | grep -q md$mdstart && umount -f $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c "(cd $mntpoint; /tmp/kevent8)" &
+for i in `jot 99`; do
+ sleep 1
+ kill -0 $! 2>/dev/null || break
+done
+umount -f $mntpoint
+pkill kevent8
+wait
+
+while mount | grep -q $mntpoint; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/kevent8
+
+exit
+EOF
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define PARALLEL 64
+
+static int fd;
+static char path[80];
+
+static void *
+spin(void *arg __unused)
+{
+ int i;
+ setproctitle("spin");
+
+ for (i= 0;; i++) {
+ snprintf(path, sizeof(path), "file.%06d.%d", getpid(), i);
+ if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0622)) ==
+ -1)
+ err(1, "creat()");
+ close(fd);
+ fd = 0;
+ unlink(path);
+ }
+ return (NULL);
+}
+
+static void *
+test(void *arg __unused)
+{
+ int kq;
+ int i, n;
+ struct kevent ev;
+ struct timespec ts;
+
+ for (i = 0; i < 500000; i++) {
+ if ((kq = kqueue()) < 0)
+ if (errno != ENOMEM)
+ err(1, "kqueue()");
+
+ n = 0;
+ memset(&ev, 0, sizeof(ev));
+ EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
+ NOTE_DELETE|NOTE_RENAME|NOTE_EXTEND, 0, 0);
+ n++;
+
+ if (kevent(kq, &ev, n, NULL, 0, NULL) < 0)
+ continue; /* Note: missing close(kq)! */
+
+ memset(&ev, 0, sizeof(ev));
+ ts.tv_sec = 0;
+ ts.tv_nsec = 1000000;
+ if ((n = kevent(kq, NULL, 0, &ev, 1, &ts)) == -1)
+ err(1, "kevent()");
+
+ close(kq);
+ }
+ return (NULL);
+}
+
+int
+main(void)
+{
+ pthread_t cp[PARALLEL], sp;
+ int e, i;
+
+ if ((e = pthread_create(&sp, NULL, spin, NULL)) != 0)
+ errc(1, e, "pthread_create");
+
+ for (i = 0; i < PARALLEL; i++) {
+ if ((e = pthread_create(&cp[i], NULL, test, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ }
+
+ for (i = 0; i < PARALLEL; i++)
+ pthread_join(cp[i], NULL);
+
+ close(fd);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/kevent9.sh b/tools/test/stress2/misc/kevent9.sh
new file mode 100755
index 000000000000..e7b88a06ec1b
--- /dev/null
+++ b/tools/test/stress2/misc/kevent9.sh
@@ -0,0 +1,206 @@
+#!/bin/sh
+
+# Test scenario by Eric Badger <eric badgerio us>
+
+# userret: returning with the following locks held:
+# exclusive sleep mutex process lock (process lock) r = 0 (0xcb714758)
+# locked @ kern/kern_event.c:2125
+# panic: witness_warn
+
+# https://people.freebsd.org/~pho/stress/log/kevent9.txt
+# Fixed in r302235.
+
+. ../default.cfg
+
+[ `sysctl -n hw.ncpu` -ne 4 ] && echo "For best results use hw.ncpu == 4"
+
+cd /tmp
+cat > /tmp/kevent9-1.c <<EOF
+#include <sys/event.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define NUM_PROCS 4000
+
+void *procmaker(void *arg __unused)
+{
+ pthread_set_name_np(pthread_self(), "procmaker");
+ for (int i = 0; i < NUM_PROCS; ++i)
+ {
+ struct timespec ts;
+ ts.tv_sec = 0;
+ ts.tv_nsec = 50000;
+ nanosleep(&ts, NULL);
+ switch(fork())
+ {
+ case -1:
+ err(1, "fork");
+ break;
+ case 0:
+ execl("./kevent9-2", "kevent9-2", NULL);
+ _exit(127);
+ break;
+ default:
+ break;
+ }
+ }
+ printf("done forking\n");
+ return NULL;
+}
+
+void *reaper(void *arg __unused)
+{
+ pthread_set_name_np(pthread_self(), "reaper");
+ int counter = 0;
+ while (counter < NUM_PROCS)
+ {
+ int status;
+ if (wait(&status) > 0)
+ {
+ ++counter;
+ }
+ }
+ printf("Reaped %d\n", counter);
+ return NULL;
+}
+
+int main()
+{
+ pthread_set_name_np(pthread_self(), "main");
+
+ int kqfd = kqueue();
+ if (kqfd == -1)
+ {
+ err(1, "kqueue()");
+ }
+
+ struct kevent change;
+ memset(&change, 0, sizeof(change));
+ change.ident = getpid();
+ change.filter = EVFILT_PROC;
+ change.flags = EV_ADD | EV_ENABLE;
+ change.fflags = NOTE_EXIT | NOTE_EXEC | NOTE_FORK | NOTE_TRACK;
+
+ if (kevent(kqfd, &change, 1, NULL, 0, NULL) == -1)
+ {
+ err(1, "kevent change");
+ }
+
+ pthread_t t;
+ pthread_create(&t, NULL, procmaker, NULL);
+ pthread_create(&t, NULL, reaper, NULL);
+
+ int numexecs = 0;
+ int numexits = 0;
+ int numforks = 0;
+ int nummults = 0;
+ int numchlds = 0;
+ int numterrs = 0;
+
+ while (1)
+ {
+ struct kevent event;
+ struct timespec to;
+ to.tv_sec = 1;
+ to.tv_nsec = 0;
+ int ret = kevent(kqfd, NULL, 0, &event, 1, &to);
+ if (ret == -1)
+ {
+ err(1, "kevent event");
+ }
+ else if (ret == 0)
+ {
+ printf("numexecs: %d numexits: %d numforks: %d numchlds: %d numterrs: %d nummults: %d\n",
+ numexecs, numexits, numforks, numchlds, numterrs, nummults);
+
+ // Sometimes we miss a NOTE_EXIT. If it hasn't arrived by the timeout, bail out since
+ // it will probably never arrive.
+ break;
+ /*
+ if (numexits == NUM_PROCS)
+ {
+ break;
+ }
+ else
+ {
+ continue;
+ }
+ */
+ }
+
+ int numflags = 0;
+ if (event.fflags & NOTE_EXEC)
+ {
+ ++numflags;
+ ++numexecs;
+ }
+ if (event.fflags & NOTE_EXIT)
+ {
+ ++numflags;
+ ++numexits;
+ }
+ if (event.fflags & NOTE_FORK)
+ {
+ ++numflags;
+ ++numforks;
+ }
+ if (event.fflags & NOTE_CHILD)
+ {
+ ++numflags;
+ ++numchlds;
+ }
+ if (event.fflags & NOTE_TRACKERR)
+ {
+ ++numflags;
+ ++numterrs;
+ }
+ if (numflags > 1)
+ {
+ ++nummults;
+ }
+
+ /*
+ struct timespec ts;
+ ts.tv_sec = 0;
+ ts.tv_nsec = 50000;
+ nanosleep(&ts, NULL);
+ */
+ }
+ return 0;
+}
+EOF
+
+cat > /tmp/kevent9-2.c <<EOF
+#include <time.h>
+
+int main()
+{
+ struct timespec ts;
+ ts.tv_sec = 0;
+ ts.tv_nsec = 1000;
+ nanosleep(&ts, NULL);
+ return 0;
+}
+EOF
+
+mycc -o kevent9-1 -Wall -Wextra -O2 -g kevent9-1.c -lpthread || exit 1
+mycc -o kevent9-2 -Wall -Wextra -O2 -g kevent9-2.c || exit 1
+rm kevent9-1.c kevent9-2.c
+
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 300 ]; do
+ ./kevent9-1 > /dev/null
+done
+rm kevent9-1 kevent9-2
+exit 0
diff --git a/tools/test/stress2/misc/killpg.sh b/tools/test/stress2/misc/killpg.sh
new file mode 100755
index 000000000000..c6af55a3d593
--- /dev/null
+++ b/tools/test/stress2/misc/killpg.sh
@@ -0,0 +1,155 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Regression test for r241859:
+# Return EPERM if processes were found but they were unable to be signaled.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > killpg.c
+mycc -o killpg -Wall -Wextra killpg.c || exit 1
+rm -f killpg.c
+
+/tmp/killpg
+
+rm -f /tmp/killpg
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pwd.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static pid_t gid, pid;
+
+#define LOOPS 100
+
+static void
+hand(int i __unused) { /* handler */
+ _exit(0);
+}
+
+static void
+nlooper(void)
+{
+ int i;
+
+ setproctitle("nlooper");
+ setpgrp(0, getpid());
+ for (i = 0; i < LOOPS; i++) {
+ if ((pid = fork()) == 0) {
+ signal(SIGINFO, hand);
+ if (arc4random() % 100 < 10)
+ usleep(arc4random() % 100);
+ _exit(0);
+ }
+ wait(NULL);
+ }
+ _exit(0);
+}
+
+static void
+looper(void)
+{
+ int i;
+
+ setproctitle("looper");
+ setpgrp(0, getpid());
+ for (i = 0; i < LOOPS; i++) {
+ if ((pid = fork()) == 0) {
+ signal(SIGINFO, hand);
+ if (arc4random() % 100 < 10)
+ usleep(arc4random() % 100);
+ _exit(0);
+ }
+ wait(NULL);
+ }
+ unlink("cont");
+ _exit(0);
+}
+
+static void
+killer(void)
+{
+ struct passwd *pw;
+
+ setproctitle("killer");
+ if ((pw = getpwnam("nobody")) == NULL)
+ err(1, "no such user: nobody");
+
+ if (setgroups(1, &pw->pw_gid) ||
+ setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
+ seteuid(pw->pw_uid) || setuid(pw->pw_uid))
+ err(1, "Can't drop privileges to \"nobody\"");
+ endpwent();
+
+ if ((gid = fork()) == 0)
+ nlooper(); /* nobody looper */
+
+ sleep(1);
+ while (access("cont", R_OK) == 0) {
+ if (killpg(gid, SIGINFO) == -1) {
+ if (errno == EPERM)
+ continue;
+ warn("FAIL killpg");
+ }
+ }
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int fd;
+
+ if ((fd = open("cont", O_RDWR | O_CREAT, 0666)) == -1)
+ err(1, "creat");
+ close(fd);
+ signal(SIGINFO, SIG_IGN);
+
+ if ((gid = fork()) == 0)
+ looper();
+ if (fork() == 0)
+ killer();
+
+ wait(NULL);
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/kinfo.sh b/tools/test/stress2/misc/kinfo.sh
new file mode 100755
index 000000000000..8cd5bdf0fb54
--- /dev/null
+++ b/tools/test/stress2/misc/kinfo.sh
@@ -0,0 +1,162 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Test scenario by marcus@freebsd.org
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > kinfo.c
+mycc -o kinfo -Wall kinfo.c -lutil || exit 1
+rm -f kinfo.c
+
+mount | grep -q procfs || mount -t procfs procfs /proc
+for i in `jot 20`; do
+ for j in `jot 5`; do
+ /tmp/kinfo &
+ done
+
+ for j in `jot 5`; do
+ wait
+ done
+done
+
+rm -f /tmp/kinfo
+exit
+EOF
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/sysctl.h>
+#include <sys/param.h>
+#include <sys/user.h>
+#include <sys/signal.h>
+#include <fcntl.h>
+#include <err.h>
+#include <strings.h>
+#include <sys/wait.h>
+#include <libutil.h>
+
+char buf[8096];
+
+void
+handler(int i) {
+ exit(0);
+}
+
+/* Stir /dev/proc */
+int
+churning(void) {
+ pid_t r;
+ int fd, status;
+
+ for (;;) {
+ r = fork();
+ if (r == 0) {
+ if ((fd = open("/proc/curproc/mem", O_RDONLY)) == -1)
+ err(1, "open(/proc/curproc/mem)");
+ bzero(buf, sizeof(buf));
+ exit(0);
+ }
+ if (r < 0) {
+ perror("fork");
+ exit(2);
+ }
+ wait(&status);
+ }
+}
+
+/* Get files for each proc */
+void
+list(void)
+{
+ struct kinfo_file *freep;
+ struct kinfo_vmentry *freep_vm;
+ long i;
+ int cnt, name[4];
+ struct kinfo_proc *kipp;
+ size_t len;
+
+ name[0] = CTL_KERN;
+ name[1] = KERN_PROC;
+ name[2] = KERN_PROC_PROC;
+
+ len = 0;
+ if (sysctl(name, 3, NULL, &len, NULL, 0) < 0)
+ err(-1, "sysctl: kern.proc.all");
+
+ kipp = malloc(len);
+ if (kipp == NULL)
+ err(1, "malloc");
+
+ if (sysctl(name, 3, kipp, &len, NULL, 0) < 0) {
+ free(kipp);
+// warn("sysctl: kern.proc.all");
+ return;
+ }
+
+ for (i = 0; i < len / sizeof(*kipp); i++) {
+
+ /* The test starts here */
+ freep = kinfo_getfile(kipp[i].ki_pid, &cnt);
+ free(freep);
+
+ freep_vm = kinfo_getvmmap(kipp[i].ki_pid, &cnt);
+ free(freep_vm);
+ /* End test */
+ }
+ free(kipp);
+}
+
+int
+main(int argc, char **argv)
+{
+ pid_t r;
+ signal(SIGALRM, handler);
+ alarm(60);
+
+ if ((r = fork()) == 0) {
+ alarm(60);
+ for (;;)
+ churning();
+ }
+ if (r < 0) {
+ perror("fork");
+ exit(2);
+ }
+
+ for (;;)
+ list();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/kinfo2.sh b/tools/test/stress2/misc/kinfo2.sh
new file mode 100755
index 000000000000..307763d88144
--- /dev/null
+++ b/tools/test/stress2/misc/kinfo2.sh
@@ -0,0 +1,186 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Test scenario by marcus@freebsd.org
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > kinfo2.c
+mycc -o kinfo2 -Wall -Wextra kinfo2.c -lutil || exit 1
+rm -f kinfo2.c
+
+mount | grep -q procfs || mount -t procfs procfs /proc
+s=0
+for i in `jot 15`; do
+ pids=""
+ for j in `jot 5`; do
+ /tmp/kinfo2 &
+ pids="$pids $!"
+ done
+ for p in $pids; do
+ wait $p
+ [ $? -ne 0 ] && s=1
+ done
+done
+
+rm -f /tmp/kinfo2
+exit $s
+EOF
+
+#include <sys/types.h>
+#include <sys/signal.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <dirent.h>
+#include <err.h>
+#include <fcntl.h>
+#include <libutil.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <unistd.h>
+
+static char buf[8096];
+
+static void
+handler(int i __unused) {
+ _exit(0);
+}
+
+/* Stir /dev/proc */
+static void
+churning(void) {
+ pid_t r;
+ int fd, status;
+
+ for (;;) {
+ r = fork();
+ if (r == 0) {
+ if ((fd = open("/proc/curproc/mem", O_RDONLY)) == -1)
+ err(1, "open(/proc/curproc/mem)");
+ bzero(buf, sizeof(buf));
+ _exit(0);
+ }
+ if (r < 0) {
+ perror("fork");
+ exit(2);
+ }
+ wait(&status);
+ }
+}
+
+/* Get files for each proc */
+void
+list(void)
+{
+ struct dirent *dp;
+ struct kinfo_file *freep;
+ struct kinfo_vmentry *freep_vm;
+ struct stat sb;
+ pid_t pid;
+ off_t base;
+ long l;
+ int cnt, fd, n;
+ int space = sizeof(buf);
+ char *bp = buf;
+ char *dummy;
+
+ if ((fd = open("/proc", O_RDONLY)) == -1)
+ err(1, "open(%s)", "/proc");
+
+ if (fstat(fd, &sb) == -1)
+ err(1, "fstat()");
+ do {
+ if ((n = getdirentries(fd, bp, space, &base)) == -1)
+ err(1, "getdirentries");
+ space = space - n;
+ if (space < sb.st_blksize)
+ break;
+ bp = bp + n;
+ } while (n != 0);
+ close(fd);
+
+ bp = buf;
+ dp = (struct dirent *)bp;
+ for (;;) {
+#if defined(DEBUG)
+ printf("name: %-10s, inode %7ju, type %2d, namelen %d, "
+ "d_reclen %d\n",
+ dp->d_name, (uintmax_t)dp->d_fileno, dp->d_type,
+ dp->d_namlen, dp->d_reclen); fflush(stdout);
+#endif
+
+ if (dp->d_type == DT_DIR &&
+ (dp->d_name[0] >= '0' && dp->d_name[0] <= '9')) {
+ l = strtol(dp->d_name, &dummy, 10);
+ pid = l;
+
+ /* The tests start here */
+ freep = kinfo_getfile(pid, &cnt);
+ free(freep);
+
+ freep_vm = kinfo_getvmmap(pid, &cnt);
+ free(freep_vm);
+ /* End test */
+ }
+
+ bp = bp + dp->d_reclen;
+ dp = (struct dirent *)bp;
+ if (dp->d_reclen <= 0)
+ break;
+ }
+}
+
+int
+main(void)
+{
+ pid_t r;
+
+ signal(SIGALRM, handler);
+ alarm(60);
+
+ if ((r = fork()) == 0) {
+ alarm(60);
+ for (;;)
+ churning();
+ }
+ if (r < 0) {
+ perror("fork");
+ exit(2);
+ }
+
+ for (;;)
+ list();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/kinfo3.sh b/tools/test/stress2/misc/kinfo3.sh
new file mode 100755
index 000000000000..9f4d00dbf2fb
--- /dev/null
+++ b/tools/test/stress2/misc/kinfo3.sh
@@ -0,0 +1,197 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Test scenario by marcus@freebsd.org and kib@freebsd.org
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > kinfo3.c
+mycc -o kinfo3 -Wall -Wextra kinfo3.c -lutil -pthread || exit 1
+rm -f kinfo3.c
+
+s=0
+mount | grep -q procfs || mount -t procfs procfs /proc
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 1200 ]; do
+ pids=""
+ for i in `jot 5`; do
+ timeout 5m /tmp/kinfo3 &
+ pids="$pids $!"
+ done
+ for pid in $pids; do
+ wait $pid
+ r=$?
+ [ $r -ne 0 ] && { s=1; echo "Exit code $r"; break; }
+ done
+done
+
+rm -f /tmp/kinfo3
+exit $s
+EOF
+
+#include <sys/types.h>
+#include <sys/signal.h>
+#include <sys/sysctl.h>
+#include <sys/user.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <libutil.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+
+static char buf[8096];
+static volatile sig_atomic_t more;
+
+static void
+handler(int i __unused) {
+
+ more = 0;
+}
+
+static void *
+thr(void *arg __unused)
+{
+ int fd;
+
+ if ((fd = open("/proc/curproc/mem", O_RDONLY)) == -1)
+ err(1, "open(/proc/curproc/mem)");
+ close(fd);
+ return (0);
+}
+
+/* Stir /dev/proc */
+static int
+churning(void) {
+ pid_t r;
+ pthread_t threads[5];
+ int i, status;;
+
+ while(more) {
+ r = fork();
+ if (r == 0) {
+ for (i = 0; i < 5; i++) {
+ if ((r = pthread_create(&threads[i], NULL, thr, 0)) != 0)
+ errc(1, r, "pthread_create()");
+ }
+ for (i = 0; i < 5; i++) {
+ if ((r = pthread_join(threads[i], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", i);
+ }
+
+ bzero(buf, sizeof(buf));
+ _exit(0);
+ }
+ if (r < 0) {
+ perror("fork");
+ exit(2);
+ }
+ wait(&status);
+ }
+ _exit(0);
+}
+
+/* Get files for each proc */
+static void
+list(void)
+{
+ struct kinfo_proc *kipp;
+ struct kinfo_vmentry *freep_vm;
+ struct kinfo_file *freep, *kif;
+ size_t len;
+ long i, j;
+ int cnt, name[4];
+
+ name[0] = CTL_KERN;
+ name[1] = KERN_PROC;
+ name[2] = KERN_PROC_PROC;
+
+ len = 0;
+ if (sysctl(name, 3, NULL, &len, NULL, 0) < 0)
+ err(-1, "sysctl: kern.proc.all");
+
+ kipp = malloc(len);
+ if (kipp == NULL)
+ err(1, "malloc");
+
+ if (sysctl(name, 3, kipp, &len, NULL, 0) < 0) {
+ free(kipp);
+// warn("sysctl: kern.proc.all");
+ return;
+ }
+
+ for (i = 0; i < (long)(len / sizeof(*kipp)); i++) {
+
+ /* The test starts here */
+ freep = kinfo_getfile(kipp[i].ki_pid, &cnt);
+ for (j = 0; j < cnt && freep; j++) {
+ kif = &freep[j];
+// printf("%d : %s\n", kif->kf_fd, kif->kf_path);
+ }
+ free(freep);
+
+ freep_vm = kinfo_getvmmap(kipp[i].ki_pid, &cnt);
+ free(freep_vm);
+ /* End test */
+ }
+ free(kipp);
+}
+
+int
+main(void)
+{
+ pid_t r;
+
+ signal(SIGALRM, handler);
+ alarm(30);
+
+ more = 1;
+ if ((r = fork()) == 0) {
+ alarm(30);
+ while(more)
+ churning();
+ }
+ if (r < 0) {
+ perror("fork");
+ exit(2);
+ }
+
+ while(more)
+ list();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/kpti.sh b/tools/test/stress2/misc/kpti.sh
new file mode 100755
index 000000000000..c5ca254a48d6
--- /dev/null
+++ b/tools/test/stress2/misc/kpti.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Simple test of kpti (Kernel page-table isolation) enable / disable
+
+# Page fault seen: https://people.freebsd.org/~pho/stress/log/dougm061.txt
+# Fixed by r354132
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+proccontrol -m kpti -q 2> /dev/null || exit 0
+[ `sysctl -n vm.pmap.pti` -eq 1 ] && echo "Page Table Isolation enabled" ||
+ echo "Page Table Isolation disabled"
+
+prog=date
+[ $# -eq 1 ] && prog=$1
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 120 ]; do
+ for i in `jot 50`; do
+ proccontrol -m kpti -s disable $prog &
+ proccontrol -m kpti -s enable $prog &
+ done > /dev/null
+ wait
+done
+exit 0
diff --git a/tools/test/stress2/misc/largepage.sh b/tools/test/stress2/misc/largepage.sh
new file mode 100755
index 000000000000..3c8e745ff3ab
--- /dev/null
+++ b/tools/test/stress2/misc/largepage.sh
@@ -0,0 +1,152 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Test scenario for of non-transparent superpages.
+
+# No problems seen.
+
+. ../default.cfg
+[ `uname -p` = "i386" ] && exit 0
+[ -z "`sysctl -i vm.largepages`" ] && exit 0
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > largepage.c
+mycc -o largepage -Wall -Wextra -g -O0 largepage.c || exit 1
+rm -f largepage.c
+cd $odir
+
+/tmp/largepage
+s=$?
+
+for path in `posixshmcontrol ls | grep largepage | awk '{print $NF}'`; do
+ echo "posixshmcontrol rm $path"
+ posixshmcontrol rm $path
+done
+rm -f ./largepage /tmp/largepage.0* /tmp/largepage
+exit 0
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static size_t ps[MAXPAGESIZES];
+static int fd;
+
+#define PARALLEL 4
+#define RUNTIME 60
+
+static void
+work(int idx)
+{
+ size_t len;
+ int i,r;
+ char *p;
+ volatile char val;
+ char path[PATH_MAX];
+
+ len = ps[idx];
+ sprintf(path, "/tmp/largepage.%06d", getpid());
+ if ((fd = shm_create_largepage(path, O_CREAT | O_RDWR, idx,
+ SHM_LARGEPAGE_ALLOC_DEFAULT, 0600)) == -1)
+ err(1,"shm_create_largepage(%zu)", len);
+
+ for (i = 0; i < 100; i++) {
+ r = ftruncate(fd, len);
+ if (r == 0)
+ break;
+ usleep(200000);
+ }
+ if (r == -1) {
+ shm_unlink(path);
+ err(1, "ftruncate(%zu)", len);
+ }
+ close(fd);
+ if ((fd = shm_open(path, O_RDWR, 0)) == -1)
+ err(1, "shm_open()");
+
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
+ MAP_FAILED) {
+ if (errno == ENOMEM)
+ return;
+ err(1, "mmap()");
+ }
+
+ val = p[arc4random() % len];
+
+ close(fd);
+ if (munmap(p, len) == -1)
+ err(1, "munmap(%p)", p);
+ if (shm_unlink(path) == -1)
+ err(1, "shm_unlink()");
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ time_t start;
+ int e, i, n, nps, status;
+
+ nps = getpagesizes(ps, MAXPAGESIZES);
+ e = 0;
+ n = 1;
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME && e == 0) {
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ work(n);
+ }
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) != pids[i])
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) e = 1;
+ }
+ n++;
+ n = n % nps;
+ if (n == 0) /* skip 4k pages */
+ n = 1;
+ }
+ close(fd);
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/laundry.sh b/tools/test/stress2/misc/laundry.sh
new file mode 100755
index 000000000000..5d368432ac3c
--- /dev/null
+++ b/tools/test/stress2/misc/laundry.sh
@@ -0,0 +1,177 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# vm.stats.vm.v_laundry_count test. WiP. No problems seen.
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/laundry.c
+mycc -o laundry -Wall -Wextra -O0 -g laundry.c || exit 1
+rm -f laundry.c
+cd $odir
+
+size=`sysctl -n hw.usermem`
+swaptotal=`sysctl -n vm.swap_total`
+[ $swaptotal -eq 0 ] && exit 0
+[ $size -gt $swaptotal ] && size=$swaptotal
+size=$((size / 10 * 8))
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+set +e
+
+cd $mntpoint
+$dir/laundry $size
+s=$?
+[ -f laundry.core -a $s -eq 0 ] &&
+ { ls -l laundry.core; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1
+mdconfig -d -u $mdstart
+rm -rf $dir/laundry
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+size_t size;
+
+#define PARALLEL 4
+#define RUNTIME (5 * 60)
+#define SYNC 0
+
+static void
+test(void)
+{
+ size_t i, sz;
+ time_t start;
+ int n;
+ char *cp;
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ sz = size;
+ if ((cp = mmap(0, sz, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0)) ==
+ (caddr_t) - 1)
+ err(1, "mmap size %zd", sz);
+
+ n = 0;
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ n++;
+ n = n & 0xff;
+ for (i = 0; i < sz; i += PAGE_SIZE)
+ cp[i] = n;
+ usleep(100);
+ }
+ munmap(cp, sz);
+
+ _exit(0);
+}
+
+int
+main(int argc __unused, char *argv[])
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, status;
+ u_int i;
+
+ sscanf(argv[1], "%zd", &size);
+ size /= PARALLEL;
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFEXITED(status)) {
+ printf("exited, status=%d\n",
+ WEXITSTATUS(status));
+ } else if (WIFSIGNALED(status)) {
+ printf("killed by signal %d\n",
+ WTERMSIG(status));
+ } else if (WIFSTOPPED(status)) {
+ printf("stopped by signal %d\n",
+ WSTOPSIG(status));
+ } else if (WIFCONTINUED(status)) {
+ printf("continued\n");
+ }
+ fprintf(stderr, "pid %d exit code %d\n",
+ pids[i], status);
+ }
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
+
diff --git a/tools/test/stress2/misc/ldt.sh b/tools/test/stress2/misc/ldt.sh
new file mode 100755
index 000000000000..21c7055d76fe
--- /dev/null
+++ b/tools/test/stress2/misc/ldt.sh
@@ -0,0 +1,370 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Test the amd64 implementation of:
+# 1. Per-process private ldt and corresponding i386 arch syscalls.
+# 2. Per-process private io permission bitmap and corresponding
+# i386 arch syscalls.
+# 3. Sigcontext
+
+# The tests must be compiled on i386 and run on amd64
+
+# All tests by kib@
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+cd /tmp
+if [ "`uname -p`" = "i386" ]; then
+ cat > ldt.c <<EOF
+/* \$Id: ldt.c,v 1.8 2008/11/01 21:14:59 kostik Exp kostik \$ */
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <machine/segments.h>
+#include <machine/sysarch.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+char stack[64 * 1024];
+
+char a[1];
+
+int
+s2ds(int sel)
+{
+
+ return (LSEL(sel, SEL_UPL));
+}
+
+unsigned char
+readbyte(int sel, int offset)
+{
+ unsigned char res;
+
+ __asm__ volatile(
+ "\tpushl %%es\n"
+ "\tmovl %1,%%es\n"
+ "\tmovb %%es:(%2),%0\n"
+ "\tpopl %%es\n"
+ : "=r"(res) : "r"(s2ds(sel)), "r"(offset));
+
+ return (res);
+}
+
+void
+writebyte(int sel, int offset, unsigned char val)
+{
+
+ __asm__ volatile(
+ "\tpushl %%es\n"
+ "\tmovl %0,%%es\n"
+ "\tmovb %2,%%es:(%1)\n"
+ "\tpopl %%es\n"
+ : : "r"(s2ds(sel)), "r"(offset), "r"(val) : "memory");
+}
+
+int
+alloc_sel(char *base, size_t len, int type, int p)
+{
+ int sel;
+ union descriptor descs[1], descsk[1];
+ uintptr_t pb;
+
+ memset(descs, 0, sizeof(descs));
+ if (len > PAGE_SIZE) {
+ len = roundup(len, PAGE_SIZE);
+ len /= PAGE_SIZE;
+ descs[0].sd.sd_lolimit = len & 0xffff;
+ descs[0].sd.sd_hilimit = (len >> 16) & 0xf;
+ descs[0].sd.sd_gran = 1;
+ } else {
+ descs[0].sd.sd_lolimit = len;
+ descs[0].sd.sd_hilimit = 0;
+ descs[0].sd.sd_gran = 0;
+ }
+ pb = (uintptr_t)base;
+ descs[0].sd.sd_lobase = pb & 0xffffff;
+ descs[0].sd.sd_hibase = (pb >> 24) & 0xff;
+ descs[0].sd.sd_type = type;
+ descs[0].sd.sd_dpl = SEL_UPL;
+ descs[0].sd.sd_p = p;
+ descs[0].sd.sd_def32 = 1;
+
+ if ((sel = i386_set_ldt(LDT_AUTO_ALLOC, descs, 1)) == -1)
+ fprintf(stderr, "i386_set_ldt: %s\n", strerror(errno));
+ else if (i386_get_ldt(sel, descsk, 1) == -1) {
+ fprintf(stderr, "i386_get_ldt: %s\n", strerror(errno));
+ sel = -1;
+ } else if (memcmp(descs, descsk, sizeof(descs)) != 0) {
+ fprintf(stderr, "descs != descsk\n");
+ sel = -1;
+ } else
+ fprintf(stderr, "selector %d\n", sel);
+
+ return (sel);
+}
+
+int
+test1(int tnum, int sel)
+{
+ unsigned char ar;
+
+ writebyte(sel, 0, '1');
+ ar = readbyte(sel, 0);
+ if (ar == '1')
+ fprintf(stderr, "test %d.1 ok\n", tnum);
+ else
+ fprintf(stderr, "test%d.1 failed, ar %x\n", tnum, ar);
+ writebyte(sel, 0, '2');
+ ar = readbyte(sel, 0);
+ if (ar == '2')
+ fprintf(stderr, "test %d.2 ok\n", tnum);
+ else
+ fprintf(stderr, "test%d.2 failed, ar %x\n", tnum, ar);
+ return (sel);
+}
+
+int
+test2_func(void *arg)
+{
+ int *sel;
+
+ sel = arg;
+ test1(2, *sel);
+ rfork(0);
+ test1(3, *sel);
+ return (0);
+}
+
+void
+test2(int sel)
+{
+ pid_t r;
+ int status;
+
+ r = rfork_thread(RFPROC | RFMEM, stack + sizeof(stack),
+ test2_func, &sel);
+ if (r == -1) {
+ fprintf(stderr, "rfork(RFPROC): %s\n", strerror(errno));
+ return;
+ } else {
+ waitpid(r, &status, 0);
+ if (WIFSIGNALED(status)) {
+ fprintf(stderr, "test2: child terminated by %s\n",
+ strsignal(WTERMSIG(status)));
+ }
+ }
+}
+
+int
+main(int argc, char *argv[])
+{
+ int sel;
+
+ sel = alloc_sel(a, 1, SDT_MEMRWA, 1);
+ if (sel == -1)
+ return (1);
+
+ test1(1, sel);
+ test2(sel);
+ return (0);
+}
+EOF
+ cc -o ldt_static_i386 -Wall -static ldt.c
+ rm ldt.c
+
+ cat > ioperm.c <<EOF
+/* \$Id: ioperm.c,v 1.3 2008/11/02 15:43:33 kostik Exp \$ */
+
+#include <machine/sysarch.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static const unsigned int port_num = 0x130;
+
+unsigned char
+inb(unsigned int port)
+{
+ unsigned char data;
+
+ __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
+ return (data);
+}
+
+void
+sigbus_handler(int signo)
+{
+
+ fprintf(stderr, "Got SIGBUS\n");
+ exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct sigaction sa;
+ unsigned int length1;
+ int enable1;
+
+ if (i386_get_ioperm(port_num, &length1, &enable1) == -1) {
+ fprintf(stderr, "get 1: %s\n", strerror(errno));
+ return (1);
+ }
+ if (length1 != 0 && enable1 != 0) {
+ fprintf(stderr, "enable1: enabled\n");
+ return (1);
+ }
+ if (i386_set_ioperm(port_num, 1, 1) == -1) {
+ fprintf(stderr, "set 1: %s\n", strerror(errno));
+ return (1);
+ }
+ inb(port_num);
+ if (i386_set_ioperm(port_num, 1, 0) == -1) {
+ fprintf(stderr, "set 2: %s\n", strerror(errno));
+ return (1);
+ }
+ if (i386_get_ioperm(port_num, &length1, &enable1) == -1) {
+ fprintf(stderr, "get 1: %s\n", strerror(errno));
+ return (1);
+ }
+ if (enable1 != 0) {
+ fprintf(stderr, "enable2: enabled\n");
+ return (1);
+ }
+ fprintf(stderr, "And now we should get SIGBUS\n");
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_handler = sigbus_handler;
+ if (sigaction(SIGBUS, &sa, NULL) == -1) {
+ fprintf(stderr, "sigaction(SIGBUS): %s\n", strerror(errno));
+ return (1);
+ }
+ inb(port_num);
+
+ return (0);
+}
+EOF
+ cc -o ioperm_static_i386 -Wall -static ioperm.c
+ rm ioperm.c
+
+ cat > fault.c <<EOF
+/* \$Id: fault.c,v 1.5 2008/10/28 17:39:16 kostik Exp \$ */
+
+#include <sys/cdefs.h>
+#include <sys/types.h>
+#include <sys/ucontext.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+extern char *fault_instr;
+int run;
+
+void
+sigsegv_sigaction(int signo, siginfo_t *si, void *c)
+{
+ ucontext_t *uc;
+ mcontext_t *mc;
+
+ uc = c;
+ mc = &uc->uc_mcontext;
+ printf("SIGSEGV run %d err %x ds %x ss %x es %x fs %x gs %x\n",
+ run, mc->mc_err, mc->mc_ds, mc->mc_ss, mc->mc_es, mc->mc_fs,
+ mc->mc_gs);
+ switch (run) {
+ case 0:
+ mc->mc_ds = 0x1111;
+ break;
+ case 1:
+ mc->mc_es = 0x1111;
+ break;
+ case 2:
+ mc->mc_fs = 0x1111;
+ break;
+ case 3:
+ mc->mc_gs = 0x1111;
+ break;
+ case 4:
+ mc->mc_ss = 0x1111;
+ break;
+ case 5:
+ _exit(11);
+ }
+ run++;
+}
+
+void
+fault(void)
+{
+
+ __asm__ volatile(".globl\tfault_instr;fault_instr:\ttestl\t\$0,0\n");
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct sigaction sa;
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_sigaction = sigsegv_sigaction;
+ sa.sa_flags = SA_SIGINFO;
+ if (sigaction(SIGSEGV, &sa, NULL) == -1) {
+ fprintf(stderr, "sigaction: %s\n", strerror(errno));
+ return (1);
+ }
+ if (sigaction(SIGBUS, &sa, NULL) == -1) {
+ fprintf(stderr, "sigaction: %s\n", strerror(errno));
+ return (1);
+ }
+
+ fault();
+
+ return (0);
+}
+EOF
+ cc -o fault_static_i386 -Wall -static fault.c
+ rm fault.c
+fi
+
+if [ "`uname -p`" = "amd64" ]; then
+ [ -x ldt_static_i386 ] && ./ldt_static_i386
+ [ -x ioperm_static_i386 ] && ./ioperm_static_i386
+ [ -x fault_static_i386 ] && { ./fault_static_i386; rm fault_static_i386.core; }
+fi
+exit 0
diff --git a/tools/test/stress2/misc/ldt2.sh b/tools/test/stress2/misc/ldt2.sh
new file mode 100755
index 000000000000..31ee4725bd52
--- /dev/null
+++ b/tools/test/stress2/misc/ldt2.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Test the implementation of i386_get_ldt() and i386_set_ldt() for 32 bit
+# processes on amd64 by running wine and mplayer with a 32 bit codec.
+
+# This is not a test script, but more of a howto document.
+
+[ `uname -p` != "amd64" ] && echo "Must run on amd64" && exit
+
+exit 0
+
+This are notes of how to perform the test.
+
+First of all you will need a i386 jail on amd64. This could be build like
+this:
+
+cat > jailbuild.sh <<EOF
+#!/bin/sh
+
+jail=/var/tmp/jail2 # Change this
+
+mount | grep $jail | grep -q devfs && umount $jail/dev
+
+here=`pwd`
+chflags -R 0 $jail
+rm -rf $jail
+mkdir -p $jail
+
+cd /var/tmp/deviant2 # You will need to change this!
+make -j4 TARGET=i386 TARGET_ARCH=i386 DESTDIR=$jail world
+make TARGET=i386 TARGET_ARCH=i386 DESTDIR=$jail distribution
+
+mount -t devfs devfs $jail/dev
+
+cp /etc/fstab /etc/hosts /etc/resolv.conf $jail/etc
+cp /boot/kernel/kernel $jail/boot/kernel
+EOF
+
+Before changing to the jail you may need these files:
+- Fetch the Firefox i386 installer. (http://www.mozilla.com)
+- Fetch a clip for a win32 codec:
+ ftp http://www.jhepple.com/support/SampleMovies/Real_Media.rm
+
+and place these in for examle $jail/root
+
+Chroot to /var/tmp/jail /bin/sh
+
+1) Install wine. For example by "UNAME_m=i386 pkg_add -r wine"
+3) Run wine on the Firefox installer.
+
+The mplayer test:
+
+It would seem that the default build of mplayer does not contain
+the i386 codec, so you have to build mplayer your self with option
+"Enable win32 codec set on the IA32 arch".
+Remember to set the environment variable UNAME_m to "i386".
+
+cat > mplayer.sh <<EOF
+#!/bin/sh
+
+export DISPLAY=<your display host goes here>:0
+while true;do
+ pos=100
+ for i in `jot 5`; do
+ mplayer -vc rv40win -geometry $pos:$pos /root/samples/Real_Media.rm < \
+ /dev/null > /dev/null 2>&1 &
+ pos=$((pos + 50))
+ done
+ for i in `jot 5`; do
+ wait
+ done
+done
+EOF
diff --git a/tools/test/stress2/misc/libMicro.sh b/tools/test/stress2/misc/libMicro.sh
new file mode 100755
index 000000000000..dc60dfd1c304
--- /dev/null
+++ b/tools/test/stress2/misc/libMicro.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Test using the OpenSolaris libmicro benchmark
+# Has shown page fault with the cascade_lockf test
+
+if [ $# -eq 0 ]; then
+ . ../default.cfg
+
+ [ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+ [ -x /usr/local/bin/libmicro-bench ] ||
+ { echo "ports/benchmarks/libmicro is not installed"; exit 0; }
+
+ [ `id -un` = $testuser ] &&
+ { echo "\$testuser is identical to current id"; exit 1; }
+
+ rm -f /tmp/libmicro.log
+ trap "rm -rf /var/tmp/libmicro.[0-9]*" 0
+ su $testuser -c "$0 x"
+ echo ""
+else
+ /usr/local/bin/libmicro-bench > /tmp/libmicro.log &
+ # Temp. work-around for hanging "c_lockf_10" test.
+ sleep 60
+ kill 0
+ wait
+fi
diff --git a/tools/test/stress2/misc/linger.sh b/tools/test/stress2/misc/linger.sh
new file mode 100755
index 000000000000..fee2524ffd3b
--- /dev/null
+++ b/tools/test/stress2/misc/linger.sh
@@ -0,0 +1,168 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Demonstrate premature "out of inodes" problem with SU
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > linger.c
+mycc -o linger -Wall -O2 linger.c
+rm -f linger.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+[ $# -eq 1 ] && opt="$1"
+[ $# -eq 0 ] && opt=$newfs_flags # No argument == default flag
+echo "newfs $opt md${mdstart}$part"
+newfs $opt md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+if ! su $testuser -c "cd $mntpoint; /tmp/linger $size"; then
+ min=2
+ [ -r $mntpoint/.sujournal ] && min=3
+ r=`df -hi $mntpoint | head -1`
+ echo " $r"
+ for i in `jot 10`; do
+ r=`df -hi $mntpoint | tail -1`
+ echo "`date '+%T'` $r"
+ [ `echo $r | awk '{print $6}'` = $min ] && break
+ sleep 10
+ done
+ ls -lR $mntpoint
+fi
+
+while mount | grep "$mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/linger
+exit
+EOF
+#include <sys/mount.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define PARALLEL 10
+#define TIMEOUT 1200
+static int size = 6552; /* 10 free inodes */
+
+static int
+test(void)
+{
+ int fd, i, j;
+ pid_t pid;
+ char file[128];
+
+ for (;;) {
+ if (access("rendezvous", R_OK) == 0)
+ break;
+ sched_yield();
+ }
+ pid = getpid();
+ for (j = 0; j < size; j++) {
+ sprintf(file,"p%05d.%05d", pid, j);
+ if ((fd = creat(file, 0660)) == -1) {
+ if (errno != EINTR) {
+ warn("creat(%s). %s:%d", file, __FILE__, __LINE__);
+ unlink("continue");
+ break;
+ }
+ }
+ if (fd != -1 && close(fd) == -1)
+ err(2, "close(%d)", j);
+
+ }
+ sleep(3);
+
+ for (i = --j; i >= 0; i--) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if (unlink(file) == -1)
+ warn("unlink(%s)", file);
+
+ }
+ return (0);
+}
+
+int
+main(void)
+{
+ time_t start;
+ int error = 0, fd, i, j, status;
+
+ umask(0);
+ if ((fd = open("continue", O_CREAT, 0644)) == -1)
+ err(1, "open()");
+ close(fd);
+ start = time(NULL);
+ for (i = 0; i < 100; i++) {
+ for (j = 0; j < PARALLEL; j++) {
+ if (fork() == 0) {
+ test();
+ exit(0);
+ }
+ }
+
+ if ((fd = open("rendezvous", O_CREAT, 0644)) == -1)
+ err(1, "open()");
+ close(fd);
+
+ for (j = 0; j < PARALLEL; j++) {
+ wait(&status);
+ error += status;
+ }
+
+ unlink("rendezvous");
+ if (access("continue", R_OK) == -1)
+ break;
+ if (time(NULL) - start > TIMEOUT) {
+ fprintf(stderr, "FAIL Timeout\n");
+ break;
+ }
+ }
+ unlink("continue");
+
+ return (error != 0);
+}
diff --git a/tools/test/stress2/misc/linger2.sh b/tools/test/stress2/misc/linger2.sh
new file mode 100755
index 000000000000..e5dad8d7e21d
--- /dev/null
+++ b/tools/test/stress2/misc/linger2.sh
@@ -0,0 +1,172 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Variation of linger.sh, with emphasis on blocks.
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > linger2.c
+mycc -o linger2 -Wall -O2 linger2.c
+rm -f linger2.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+[ $# -eq 1 ] && opt="$1"
+[ $# -eq 0 ] && opt=$newfs_flags # No argument == default flag
+newfs $opt -n md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+set `df -i $mntpoint | tail -1 | awk '{print $3, $6}'`
+
+min=24
+[ -r $mntpoint/.sujournal ] && { size=88; min=8232; }
+if ! su $testuser -c "cd $mntpoint; /tmp/linger2 $size 2>/dev/null"; then
+ r=`df -i $mntpoint | head -1`
+ echo " $r"
+ for i in `jot 12`; do
+ r=`df -ik $mntpoint | tail -1`
+ [ "$r" != "$old" ] && echo "`date '+%T'` $r"
+ old=$r
+ [ `echo $r | awk '{print $3}'` -le $min ] && break
+ sleep 10
+ done
+fi
+
+while mount | grep "$mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/linger2
+exit
+EOF
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define PARALLEL 10
+static int size = 89;
+
+int
+test(void)
+{
+ int error = 0, fd, i, j;
+ pid_t pid;
+ char file[128];
+ char *buf;
+
+ for (;;) {
+ if (access("rendezvous", R_OK) == 0)
+ break;
+ sched_yield();
+ }
+ pid = getpid();
+ buf = malloc(1024 * 1024);
+ for (j = 0; j < size; j++) {
+ sprintf(file,"p%05d.%05d", pid, j);
+ if ((fd = creat(file, 0660)) == -1) {
+ if (errno != EINTR) {
+ warn("creat(%s). %s:%d", file, __FILE__, __LINE__);
+ unlink("continue");
+ error = 1;
+ break;
+ }
+ }
+ if (write(fd, buf, 1024 * 1024) != 1024 * 1024) {
+ warn("write()");
+ unlink("continue");
+ error = 1;
+ break;
+ }
+ if (fd != -1 && close(fd) == -1)
+ err(2, "close(%d)", j);
+
+ }
+ sleep(3);
+
+ if (error == 0)
+ j--;
+ for (i = j; i >= 0; i--) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if (unlink(file) == -1)
+ warn("unlink(%s)", file);
+
+ }
+ return (error);
+}
+
+int
+main(int argc, char **argv)
+{
+ int error = 0, fd, i, j, status;
+
+ if (argc == 2)
+ size = atoi(argv[1]);
+
+ umask(0);
+ if ((fd = open("continue", O_CREAT, 0644)) == -1)
+ err(1, "open()");
+ close(fd);
+ for (i = 0; i < 200; i++) {
+ for (j = 0; j < PARALLEL; j++) {
+ if (fork() == 0)
+ exit(test());
+ }
+
+ if ((fd = open("rendezvous", O_CREAT, 0644)) == -1)
+ err(1, "open()");
+ close(fd);
+
+ for (j = 0; j < PARALLEL; j++) {
+ wait(&status);
+ error += status;
+ }
+
+ unlink("rendezvous");
+ if (access("continue", R_OK) == -1) {
+ break;
+ }
+ }
+ unlink("continue");
+
+ return (error != 0);
+}
diff --git a/tools/test/stress2/misc/linger3.sh b/tools/test/stress2/misc/linger3.sh
new file mode 100755
index 000000000000..c888d992138a
--- /dev/null
+++ b/tools/test/stress2/misc/linger3.sh
@@ -0,0 +1,134 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# Test for SU problem with false EMLINK
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > linger3.c
+mycc -o linger3 -Wall -Wextra -O2 linger3.c
+rm -f linger3.c
+
+mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+[ $# -eq 1 ] && opt="$1"
+[ $# -eq 0 ] && opt=$newfs_flags # No argument == default flag
+echo "newfs $opt md${mdstart}$part"
+newfs $opt md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+cd $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c "/tmp/linger3"
+
+cd $here
+
+while mount | grep -q $mntpoint; do
+ umount $mntpoint 2> /dev/null || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/linger3
+exit 0
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define PARALLEL 4
+#define ND ((32767 / PARALLEL) - 2)
+
+static pid_t p;
+
+void
+setup(int loop)
+{
+ int d;
+ char name[128];
+
+ for (d = 0; d < ND; d++) {
+ snprintf(name, sizeof(name), "d%06d.%03d", p, d);
+ if (mkdir(name, 00700) == -1)
+ err(1, "mkdir(%s) @ loop #%d", name, loop);
+ }
+}
+
+void
+prune(int loop)
+{
+ int d;
+ char name[128];
+
+ for (d = 0; d < ND; d++) {
+ snprintf(name, sizeof(name), "d%06d.%03d", p, d);
+ if (rmdir(name) == -1)
+ err(1, "rmdir(%s) @ loop #%d", name, loop);
+ }
+}
+
+int
+main()
+{
+ int i, j;
+ int linger = 0;
+
+ if (getenv("LINGER"))
+ linger = atoi(getenv("LINGER"));
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0) {
+ p = getpid();
+ for (j = 0; j < 10; j++) {
+ setup(j);
+ prune(j);
+ if (linger != 0)
+ sleep(linger);
+ }
+ _exit(0);
+ }
+ }
+
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/linger4.sh b/tools/test/stress2/misc/linger4.sh
new file mode 100755
index 000000000000..1578575fa387
--- /dev/null
+++ b/tools/test/stress2/misc/linger4.sh
@@ -0,0 +1,155 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# Test for SU problem with "out of inodes" for CREAT
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > linger4.c
+mycc -o linger4 -Wall -Wextra -O2 linger4.c
+rm -f linger4.c
+
+mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+[ $# -eq 1 ] && opt="$1"
+[ $# -eq 0 ] && opt="$newfs_flags -n" # No argument == default flag
+echo "newfs $opt md${mdstart}$part"
+newfs $opt md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+cd $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c "/tmp/linger4" ||
+ { ls -la $mntpoint; df -i $mntpoint; }
+
+cd $here
+
+while mount | grep -q $mntpoint; do
+ umount $mntpoint 2> /dev/null || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/linger4
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOOPS 300
+#define NUMBER 80000 / PARALLEL /* Number of files to use. Max is 131068 */
+#define PARALLEL 4
+#define TIMEOUT (20 * 60)
+
+void
+Creat(int loopno)
+{
+ int e, fd, i, j;
+ char file[128];
+ char path[128];
+ pid_t pid;
+
+ e = 0;
+ pid = getpid();
+ sprintf(path, "f%06d", pid);
+ if (mkdir(path, 0770) == -1)
+ warn("mkdir(%s), %s:%d, loop #%d", path, __FILE__, __LINE__, loopno);
+ chdir(path);
+
+ for (j = 0; j < NUMBER; j++) {
+ sprintf(file,"p%05d.%05d", pid, j);
+ if ((fd = creat(file, 0660)) == -1) {
+ if (errno != EINTR) {
+ warn("creat(%s). %s:%d, loop #%d", file, __FILE__, __LINE__, loopno);
+ e = 1;
+ break;
+ }
+ }
+ if (fd != -1 && close(fd) == -1)
+ err(2, "close(%d)", j);
+
+ }
+
+ for (i = --j; i >= 0; i--) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if (unlink(file) == -1)
+ err(3, "unlink(%s)", file);
+
+ }
+ chdir ("..");
+ if (rmdir(path) == -1)
+ err(1, "rmdir(%s), %s:%d, loop #%d", path, __FILE__, __LINE__, loopno);
+
+ _exit(e);
+}
+
+int
+main()
+{
+ time_t start;
+ int e, i, j, status;
+
+ e = 0;
+ start = time(NULL);
+ for (j = 0; j < LOOPS; j++) {
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ Creat(j);
+ }
+
+ for (i = 0; i < PARALLEL; i++) {
+ wait(&status);
+ e += WEXITSTATUS(status);
+ }
+ if (e != 0)
+ break;
+// sleep(60); /* No problems if this is included */
+ if (time(NULL) - start > TIMEOUT) {
+ fprintf(stderr, "Timeout.\n");
+ e++;
+ break;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/link.sh b/tools/test/stress2/misc/link.sh
new file mode 100755
index 000000000000..64f76ca5f3b0
--- /dev/null
+++ b/tools/test/stress2/misc/link.sh
@@ -0,0 +1,193 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Deadlock seen for file systems which suspend writes on unmount, such as
+# UFS and tmpfs.
+# http://people.freebsd.org/~pho/stress/log/link.txt
+# Fixed by r272130
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > link.c
+mycc -o link -Wall -Wextra -O2 -g link.c || exit 1
+rm -f link.c
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+daemon sh -c "(cd $here/../testcases/swap; ./swap -t 5m -i 20 -h -l 100)" \
+ > /dev/null 2>&1
+/tmp/link $mntpoint > /dev/null 2>&1 &
+
+for i in `jot 100`; do
+ umount -f $mntpoint &&
+ mount /dev/md${mdstart}$part $mntpoint
+ sleep .1
+done
+pkill -9 link
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+
+# tmpfs
+mount -t tmpfs tmpfs $mntpoint
+
+/tmp/link $mntpoint > /dev/null 2>&1 &
+
+for i in `jot 100`; do
+ umount -f $mntpoint &&
+ mount -t tmpfs tmpfs $mntpoint
+ sleep .1
+done
+pkill -9 link swap
+wait
+
+while pkill -9 swap; do
+ :
+done > /dev/null 2>&1
+while mount | grep $mntpoint | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
+[ -d "$mntpoint" ] && (cd $mntpoint && find . -delete)
+rm -f /tmp/link
+exit 0
+EOF
+#include <sys/param.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <time.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define PARALLEL 16
+#define RUNTIME 300
+
+time_t start;
+char *dir;
+
+void
+trename(void)
+{
+ int fd;
+ char name1[MAXPATHLEN + 1];
+ char name2[MAXPATHLEN + 1];
+
+ setproctitle(__func__);
+ snprintf(name1, sizeof(name1), "%s/r1.%05d", dir, getpid());
+ snprintf(name2, sizeof(name2), "%s/r2.%05d", dir, getpid());
+ if ((fd = open(name1, O_RDWR | O_CREAT, 0644)) == -1)
+ err(1, "open(%s)", name1);
+ close(fd);
+
+ while (time(NULL) - start < RUNTIME) {
+ if (rename(name1, name2) == -1) {
+ if (errno == ENOENT) {
+ if ((fd = open(name1, O_RDWR | O_CREAT, 0644)) == -1)
+ err(1, "open(%s)", name1);
+ close(fd);
+ continue;
+ } else
+ warn("link(%s, %s)", name1, name2);
+ }
+ if (rename(name2, name1) == -1)
+ warn("link(%s, %s)", name2, name1);
+ }
+ unlink(name1);
+ unlink(name2);
+ _exit(0);
+}
+
+void
+tlink(void)
+{
+ int fd;
+ char name1[MAXPATHLEN + 1];
+ char name2[MAXPATHLEN + 1];
+
+ setproctitle(__func__);
+ snprintf(name1, sizeof(name1), "%s/f1.%05d", dir, getpid());
+ snprintf(name2, sizeof(name2), "%s/f2.%05d", dir, getpid());
+ if ((fd = open(name1, O_RDWR | O_CREAT, 0644)) == -1)
+ err(1, "open(%s)", name1);
+ close(fd);
+
+ while (time(NULL) - start < RUNTIME) {
+ unlink(name2);
+ if (link(name1, name2) == -1) {
+ if (errno == ENOENT) {
+ if ((fd = open(name1, O_RDWR | O_CREAT, 0644)) == -1)
+ err(1, "open(%s)", name1);
+ close(fd);
+ continue;
+ } else
+ warn("link(%s, %s)", name1, name2);
+ }
+ }
+ unlink(name1);
+ unlink(name2);
+ _exit(0);
+}
+
+int
+main(int argc, char **argv)
+{
+ int i, type;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <full path to dir>", argv[0]);
+ dir = argv[1];
+ type = arc4random() % 2; /* test either link() or rename() */
+
+ start = time(NULL);
+ for (i = 0; i < PARALLEL; i++) {
+ if (type == 0 && fork() == 0)
+ tlink();
+ if (type == 1 && fork() == 0)
+ trename();
+ }
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+
+ return(0);
+}
diff --git a/tools/test/stress2/misc/link2.sh b/tools/test/stress2/misc/link2.sh
new file mode 100755
index 000000000000..94116c0dc081
--- /dev/null
+++ b/tools/test/stress2/misc/link2.sh
@@ -0,0 +1,78 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Copy of link.sh with leak detection added
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/link.sh > link.c
+mycc -o link -Wall -Wextra -O2 -g link.c || exit 1
+rm -f link.c
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+daemon sh -c "(cd $here/../testcases/swap; ./swap -t 5m -i 20 -h -l 100)" \
+ > /dev/null 2>&1
+/tmp/link $mntpoint > /dev/null 2>&1 &
+
+m1=`vmstat -m | awk '/ mount/ {print $2}'`
+for i in `jot 100`; do
+ umount -f $mntpoint &&
+ mount /dev/md${mdstart}$part $mntpoint
+ sleep .1
+done
+m2=`vmstat -m | awk '/ mount/ {print $2}'`
+pkill -9 link
+while pgrep -q swap; do
+ pkill -9 swap
+done
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+
+[ -d "$mntpoint" ] && (cd $mntpoint && find . -delete)
+rm -f /tmp/link
+leak=$((m2 - m1))
+[ $leak -gt 2 ] &&
+ { echo "Leaked $leak InUse \"mount\""; s=1; vmstat -m | \
+ sed -n '1p;/ mount/p'; } ||
+ s=0
+exit $s
diff --git a/tools/test/stress2/misc/linux.sh b/tools/test/stress2/misc/linux.sh
new file mode 100755
index 000000000000..1cbbfb8b27a1
--- /dev/null
+++ b/tools/test/stress2/misc/linux.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Bug 230144 - Linux emulator does not work on Ryzen / Epic processors
+# No problems seen.
+
+[ -x /compat/linux/bin/date ] || exit 0
+kldstat | grep -q linux.ko && exit 0
+
+set -e
+kldload linux.ko
+mount -t linprocfs linprocfs /compat/linux/proc
+mount -t linsysfs linsysfs /compat/linux/sys
+mount -t tmpfs -o rw,mode=1777 tmpfs /compat/linux/dev/shm
+[ `uname -m` = amd64 ] && kldload linux64.ko
+set +e
+[ -x /compat/linux/bin/bash ] &&
+ /compat/linux/bin/bash -c "/compat/linux/bin/date"
+
+[ `uname -m` = amd64 ] && kldunload linux64.ko
+umount /compat/linux/dev/shm
+kldstat | grep -q tmpfs.ko && kldunload tmpfs.ko
+umount /compat/linux/sys
+kldunload linsysfs.ko
+umount /compat/linux/proc
+kldunload linprocfs.ko
+kldunload linux.ko
+
+exit 0
diff --git a/tools/test/stress2/misc/lockd.sh b/tools/test/stress2/misc/lockd.sh
new file mode 100755
index 000000000000..01866e39ef74
--- /dev/null
+++ b/tools/test/stress2/misc/lockd.sh
@@ -0,0 +1,70 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Demonstrate rpc memory leak when remote lockd & statd are not running.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+pgrep -q lockd || { echo "lockd not running."; exit 0; }
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+mount -t nfs -o tcp -o nfsv3 -o retrycnt=1 -o soft,timeout=1 \
+ -o rw $nfs_export $mntpoint || exit 0
+
+lockf -t 10 $mntpoint/$$.lock sleep 2 > /tmp/$$.log 2>&1
+if grep -q "No locks available" /tmp/$$.log; then
+ echo "Is lockd running on the remote host?"
+ rm /tmp/$$.log
+fi
+
+rpc1=`vmstat -m | grep rpc | tail -1 | awk '{print $2}'`
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 60 ]; do
+ for i in `jot 10`; do
+ rm -f $mntpoint/$0.$$.$i
+ lockf -t 10 $mntpoint/$0.$$.$i sleep 2 &
+ done
+ wait
+done
+rm -f $mntpoint/$0.$$*
+
+s=0
+for i in `jot 6`; do
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && s=1
+rpc2=`vmstat -m | grep rpc | tail -1 | awk '{print $2}'`
+if [ $((rpc2 - rpc1)) -gt 2 ]; then
+ echo "rpc memory leak. Was $rpc1, is $rpc2"
+ s=2
+fi
+exit $s
diff --git a/tools/test/stress2/misc/lockf.sh b/tools/test/stress2/misc/lockf.sh
new file mode 100755
index 000000000000..347bd063bfe1
--- /dev/null
+++ b/tools/test/stress2/misc/lockf.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Page fault seen:
+# http://people.freebsd.org/~pho/stress/log/lockf.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+pgrep -q lockd || { echo "lockd not running"; exit 0; }
+trap "rm -f /tmp/$0.$$.*" EXIT INT
+mount | grep "$mntpoint" | grep nfs > /dev/null && umount $mntpoint
+
+mount -t nfs -o tcp -o retrycnt=3 -o intr -o soft \
+ -o rw $nfs_export $mntpoint
+sleep .5
+lockf -t 10 $mntpoint/$$.lock sleep 2 || fail=1
+umount $mntpoint
+[ $fail ] && exit 1
+
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 300 ] ; do
+ mount -t nfs -o tcp -o retrycnt=3 -o intr -o soft \
+ -o rw $nfs_export $mntpoint || break
+ sleep 1
+
+ for j in `jot 50`; do
+ lockf -t 10 $mntpoint/$0.$$.$j sleep 3 &
+ done
+
+ while mount | grep -q $mntpoint; do
+ umount -f $mntpoint > /dev/null 2>&1
+ done
+ wait
+done 2>/dev/null
+mount -t nfs -o tcp -o retrycnt=3 -o intr -o soft \
+ -o rw $nfs_export $mntpoint || break
+sleep .5
+rm $mntpoint/$0.*
+while mount | grep -q $mntpoint; do
+ umount -f $mntpoint > /dev/null 2>&1
+done
+exit 0
diff --git a/tools/test/stress2/misc/lockf2.sh b/tools/test/stress2/misc/lockf2.sh
new file mode 100755
index 000000000000..6d1501d581b0
--- /dev/null
+++ b/tools/test/stress2/misc/lockf2.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+for i in `jot 100`; do
+ for j in `jot 50`; do
+ file=/tmp/lockf2.$$.$j
+ touch $file
+ lockf -t 10 $file sleep 3 &
+ done
+
+ for j in `jot 50`; do
+ rm -f /tmp/lockf2.$$.$j
+ done
+
+ for j in `jot 50`; do
+ wait
+ done
+done
diff --git a/tools/test/stress2/misc/lockf3.sh b/tools/test/stress2/misc/lockf3.sh
new file mode 100755
index 000000000000..4493fa214a33
--- /dev/null
+++ b/tools/test/stress2/misc/lockf3.sh
@@ -0,0 +1,163 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > lockf3.c
+mycc -o lockf3 -Wall -Wextra -O2 lockf3.c || exit 1
+rm -f lockf3.c
+
+mount | grep -q "$mntpoint" && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 40m -u $mdstart
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+cd $mntpoint
+for i in `jot 3`; do
+ $here/../testcases/swap/swap -t 10m -i 200 > /dev/null &
+ /tmp/lockf3 || break
+ pkill swap
+ wait
+done
+while pgrep -q swap; do
+ pkill swap
+done
+pkill lockf3
+wait
+cd $here
+
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+rm -rf /tmp/lockf3
+exit 0
+EOF
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define LOOPS 10000
+#define PIDS 100
+
+void
+handler(int s __unused)
+{
+}
+
+void
+int_handler(int s __unused)
+{
+ _exit(0);
+}
+
+void
+ahandler(int s __unused)
+{
+ fprintf(stderr, "FAIL timed out\n");
+ _exit(1);
+}
+
+int
+main(void)
+{
+ int fd, i, j;
+ char name[128];
+ pid_t pid[PIDS];
+ struct sigaction sa;
+ int status;
+
+ signal(SIGALRM, ahandler);
+ alarm(15 * 60);
+ for (i = 0; i < PIDS; i++) {
+ sprintf(name, "lock.%06d", getpid());
+ if ((fd = open(name, O_CREAT | O_TRUNC | O_RDWR, 0640)) == -1)
+ err(1, "open(%s)", name);
+ if (lockf(fd, F_LOCK, 0) == -1)
+ err(1, "flock 1");
+
+ if ((pid[i] = fork()) == -1)
+ err(1, "fork");
+
+ if (pid[i] == 0) {
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_handler = handler;
+ if (sigaction(SIGHUP, &sa, NULL) == -1)
+ err(1, "signal");
+ sa.sa_handler = int_handler;
+ if (sigaction(SIGINT, &sa, NULL) == -1)
+ err(1, "signal");
+
+ for (;;) {
+ if (lockf(fd, F_LOCK, 0) == -1)
+ if (errno != EINTR)
+ warn("lockf");
+ }
+ _exit(0);
+ }
+ unlink(name);
+ }
+
+ usleep(10000);
+
+ for (i = 0; i < LOOPS; i++) {
+ for (j = 0; j < PIDS; j++) {
+ if (kill(pid[j], SIGHUP) == -1) {
+ warn("kill(%d), i = %d, j = %d", pid[j], i, j);
+ pid[j] = 0;
+ }
+ }
+ }
+ for (j = 0; j < PIDS; j++) {
+ if (kill(pid[j], SIGINT) == -1) {
+ warn("kill(%d), i = %d, j = %d", pid[j], i, j);
+ pid[j] = 0;
+ }
+ }
+
+ for (j = 0; j < PIDS; j++) {
+ if (waitpid(pid[j], &status, 0) == -1)
+ err(1, "waitpid(%d)", pid[j]);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/lockf4.sh b/tools/test/stress2/misc/lockf4.sh
new file mode 100755
index 000000000000..690bad033cf4
--- /dev/null
+++ b/tools/test/stress2/misc/lockf4.sh
@@ -0,0 +1,183 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# lockf(3) test scenario.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > lockf4.c
+rm -f /tmp/lockf4
+mycc -o lockf4 -Wall -Wextra -g -O2 lockf4.c || exit 1
+rm -f lockf4.c
+cd $odir
+
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c "(cd $mntpoint; /tmp/lockf4)" &
+su $testuser -c "(cd $mntpoint; /tmp/lockf4)" &
+su $testuser -c "(cd $mntpoint; /tmp/lockf4)" &
+wait
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/lockf4
+exit 0
+EOF
+#include <fcntl.h>
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define N (5 * 1024 * 1024) /* 40 MB */
+int fd;
+int idx[N];
+char file[] = "lockf4.file";
+
+#define TIMEOUT 600
+
+int64_t
+add(int ix, int val)
+{
+ int64_t v;
+ off_t offset;
+ time_t start;
+ int r;
+
+ offset = ix * sizeof(v);
+ if (lseek(fd, offset, SEEK_SET) == -1)
+ err(1, "lseek");
+ start = time(NULL);
+ while (lockf(fd, F_LOCK, sizeof(v)) == -1) {
+ if (errno != EDEADLK)
+ err(1, "lockf(%s, F_LOCK)", file);
+ if (time(NULL) - start > TIMEOUT)
+ errx(1, "lockf timedout");
+ }
+ v = 0;
+ r = read(fd, &v, sizeof(v));
+ if (r == 0)
+ v = 0;
+ else
+ if (r == -1)
+ err(1, "read");
+ v += val;
+
+ if (lseek(fd, offset, SEEK_SET) == -1)
+ err(1, "lseek");
+ if (write(fd, &v, sizeof(v)) < 0)
+ err(1, "write");
+ if (lseek(fd, offset, SEEK_SET) == -1)
+ err(1, "lseek");
+ if (lockf(fd, F_ULOCK, sizeof(v)) == -1)
+ err(1, "lockf(%s, F_ULOCK)", file);
+
+ return (v);
+}
+
+int
+check(void)
+{
+ int64_t v;
+ int i;
+
+ setproctitle("check");
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ err(1, "lseek");
+ for (i = 1; i < N; i++) {
+ if (read(fd, &v, sizeof(v)) < 0)
+ err(1, "read");
+ if (v != 0)
+ return (1);
+ }
+
+ return (0);
+}
+
+int
+main(void)
+{
+ int64_t r;
+ int e, i, j, t;
+ char help[80];
+
+ for (i = 1; i < N; i++)
+ idx[i] = i;
+
+ for (i = 1; i < N; i++) {
+ j = arc4random() % (N - 1) + 1;
+ t = idx[i];
+ idx[i] = idx[j];
+ idx[j] = t;
+ }
+
+ if ((fd = open(file, O_RDWR | O_CREAT, 0644)) == -1)
+ err(1, "open(%s)", file);
+
+ add(0, 1);
+ for (i = 1; i < N; i++) {
+ if (i % 500 == 0) {
+ snprintf(help, sizeof(help), "add %d%%", i * 100 / N);
+ setproctitle("%s", help);
+ }
+ add(idx[i], 1);
+ }
+ for (i = 1; i < N; i++) {
+ if (i % 500 == 0) {
+ snprintf(help, sizeof(help), "sub %d%%", i * 100 / N);
+ setproctitle("%s", help);
+ }
+ add(idx[i], -1);
+ }
+
+ e = 0;
+ if ((r = add(0, -1)) == 0) {
+ e = check();
+ if (e == 0)
+ unlink(file);
+ else
+ fprintf(stderr, "FAIL\n");
+ }
+
+ close(fd);
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/lockf5.sh b/tools/test/stress2/misc/lockf5.sh
new file mode 100755
index 000000000000..5b5dbad1015c
--- /dev/null
+++ b/tools/test/stress2/misc/lockf5.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# "Fatal trap 9: general protection fault while in kernel mode" seen:
+# https://people.freebsd.org/~pho/stress/log/lockf5.txt
+
+# Test scenario by: ngie@FreeBSD.org.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+pgrep -q lockd || { echo "lockd not running."; exit 1; }
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+mount -t nfs $nfs_export $mntpoint || exit 1
+sleep .5
+rm -rf $mntpoint/$0
+mkdir -p $mntpoint/$0
+
+for i in `jot 100`; do
+ (cd $mntpoint/$0; lockf -t 10 f$i sleep 5) > /dev/null 2>&1 &
+done
+sleep 3
+
+umount -f $mntpoint
+wait
+
+mount -t nfs $nfs_export $mntpoint || exit 1
+sleep .5
+rm -rf $mntpoint/$0
+umount $mntpoint
+exit 0
diff --git a/tools/test/stress2/misc/lookup_shared.sh b/tools/test/stress2/misc/lookup_shared.sh
new file mode 100755
index 000000000000..935811bee719
--- /dev/null
+++ b/tools/test/stress2/misc/lookup_shared.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -z "`sysctl -in vfs.lookup_shared`" ] && exit 0
+
+saved=`sysctl vfs.lookup_shared | awk '{print $NF}'`
+trap "sysctl vfs.lookup_shared=$saved" EXIT INT
+
+export runRUNTIME=10m # Run tests for 10 minutes
+sysctl vfs.lookup_shared=0
+(cd ..; ./run.sh disk.cfg)
+exit 0
diff --git a/tools/test/stress2/misc/lstat.sh b/tools/test/stress2/misc/lstat.sh
new file mode 100755
index 000000000000..f89886a4556e
--- /dev/null
+++ b/tools/test/stress2/misc/lstat.sh
@@ -0,0 +1,236 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Hunt for;
+# Bug 204764 - Filesystem deadlock, process in vodead state
+# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=204764
+# No problem seen.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/lstat.c
+mycc -o lstat -Wall -Wextra -O0 -g lstat.c || exit 1
+rm -f lstat.c
+cd $odir
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs -n -b 4096 -f 512 -i 1024 md${mdstart}$part > /dev/null
+mount -o async /dev/md${mdstart}$part $mntpoint || exit 1
+
+path=$mntpoint/a/b/c
+mkdir -p $path
+
+$dir/lstat $path
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/lstat $wdir/lstat.tmp.*
+exit
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *dirs, *share;
+static char *arg;
+
+#define R1 0
+#define R2 1
+
+#define MXDIRS 3000
+#define PARALLEL 2
+#define RUNTIME 600
+#define SLPTIME 400
+
+static void
+tfts(int idx)
+{
+ FTS *fts;
+ FTSENT *p;
+ struct stat sb;
+ int ftsoptions;
+ char *args[2];
+
+ if (idx != 0)
+ _exit(0);
+
+ ftsoptions = FTS_PHYSICAL;
+ args[0] = arg,
+ args[1] = 0;
+
+ setproctitle("fts");
+ while (share[R2] == 0) {
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ while ((p = fts_read(fts)) != NULL) {
+ lstat(fts->fts_path, &sb);
+ if (share[R2] == 1)
+ break;
+ }
+
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+ }
+
+ _exit(0);
+}
+
+static int
+test(int idx)
+{
+ struct stat sb;
+ pid_t fpid, pd, pid;
+ size_t len;
+ int i, n, r;
+ char dir[128], path[128];
+
+ atomic_add_int(&share[R1], 1);
+ while (share[R1] != PARALLEL)
+ ;
+
+ len = PAGE_SIZE;
+ if ((dirs = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ if ((fpid = fork()) == 0)
+ tfts(idx);
+
+ pid = getpid();
+ snprintf(dir, sizeof(dir), "%s/dir.%d", arg, pid);
+ if (mkdir(dir, 0777) == -1)
+ err(1, "mkdir(%s)", dir);
+ if (chdir(dir) == -1)
+ err(1, "chdir(%s)", dir);
+ if ((pd = fork()) == 0) {
+ setproctitle("mkdir");
+ i = 0;
+ while (share[R2] == 0) {
+ snprintf(path, sizeof(path), "%s/d.%d.%d", arg, pid,
+ i);
+ n = 0;
+ while (dirs[0] > MXDIRS && share[R2] == 0)
+ usleep(SLPTIME);
+ while ((r = mkdir(path, 0777)) == -1) {
+ if (errno != EMLINK)
+ err(1, "mkdir(%s) @ %d", path,
+ __LINE__);
+ usleep(SLPTIME);
+ if (share[2] == 1)
+ break;
+ }
+ if (r == 0) {
+ atomic_add_int(&dirs[0], 1);
+ i++;
+ }
+ }
+
+ _exit(0);
+ }
+
+ i = 0;
+ setproctitle("rmdir");
+ while (dirs[0] > 0 || share[R2] == 0) {
+ n = 0;
+ if (dirs[0] < MXDIRS / 2)
+ usleep(SLPTIME);
+ snprintf(path, sizeof(path), "%s/d.%d.%d", arg, pid, i);
+ while (lstat(path, &sb) == -1 && share[R2] == 0) {
+ usleep(SLPTIME);
+ }
+ if (rmdir(path) == -1) {
+ if (errno != ENOENT)
+ err(1, "rmdir(%s)", path);
+ } else {
+ atomic_add_int(&dirs[0], -1);
+ i++;
+ }
+ }
+ waitpid(pd, NULL, 0);
+ waitpid(fpid, NULL, 0);
+
+ chdir("..");
+ if ((rmdir(dir)) == -1)
+ err(1, "unlink(%s)", dir);
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ size_t len;
+ int e, i, pids[PARALLEL], status;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <path>", argv[0]);
+ arg = argv[1];
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ share[R1] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test(i);
+ }
+ sleep(RUNTIME);
+ share[R2] = 1;
+ for (i = 0; i < PARALLEL; i++) {
+ waitpid(pids[i], &status, 0);
+ e += status == 0 ? 0 : 1;
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/mac.sh b/tools/test/stress2/misc/mac.sh
new file mode 100755
index 000000000000..48fc8a323f0e
--- /dev/null
+++ b/tools/test/stress2/misc/mac.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+if sysctl security.mac.test 2>&1 | grep -q unknown; then
+ echo "Kernel must be configured with MAC and MAC_TEST!"
+ exit 0
+fi
+
+./crossmp.sh
+
+sysctl security.mac.test
diff --git a/tools/test/stress2/misc/mac_chkexec.sh b/tools/test/stress2/misc/mac_chkexec.sh
new file mode 100755
index 000000000000..d3e961b02200
--- /dev/null
+++ b/tools/test/stress2/misc/mac_chkexec.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Regression test for panic in second ls
+
+exit 0 # Not part of the kernel
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+sysctl -a | ! grep -q security.mac.chkexec && echo "chkexec.ko must be loaded" && exit 1
+
+mount | grep "$mntpoint" | grep -q md${mdstart}$part && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 12m -u $mdstart
+bsdlabel -w md$mdstart auto
+
+newfs md${mdstart}$part > /dev/null
+tunefs -l enable /dev/md${mdstart}$part
+mount /dev/md${mdstart}$part $mntpoint
+
+cp /bin/ls $mntpoint
+setfmac chkexec/md5:`md5 -q $mntpoint/ls` $mntpoint/ls
+sysctl security.mac.chkexec.enforce=1
+$mntpoint/ls $mntpoint/ls
+setfmac chkexec/none $mntpoint/ls
+$mntpoint/ls $mntpoint/ls
+sysctl security.mac.chkexec.enforce=0
+
+umount $mntpoint
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/machipc.sh b/tools/test/stress2/misc/machipc.sh
new file mode 100755
index 000000000000..09294d0d0295
--- /dev/null
+++ b/tools/test/stress2/misc/machipc.sh
@@ -0,0 +1,184 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Threaded Mach IPC test scenario
+# https://people.freebsd.org/~pho/stress/log/kip014.txt
+
+ps -p1 | grep -q launchd || exit 0
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > machipc.c
+cc -o machipc -Wall -Wextra -O2 -g machipc.c -lmach -lpthread || exit 1
+rm machipc.c
+cd $odir
+
+(cd ../testcases/swap; ./swap -t 20m -i 20 -h -v) > /dev/null 2>&1 &
+sleep 5
+/tmp/machipc
+pkill swap
+wait
+rm -f /tmp/machipc
+exit 0
+EOF
+#include <sys/types.h>
+
+#include <mach/mach.h>
+
+#include <err.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+typedef struct {
+ unsigned int msgt_name : 8,
+ msgt_size : 8,
+ msgt_number : 12,
+ msgt_inline : 1,
+ msgt_longform : 1,
+ msgt_deallocate : 1,
+ msgt_unused : 1;
+} mach_msg_type_t;
+
+struct integer_message {
+ mach_msg_header_t head;
+ mach_msg_type_t type;
+
+ int inline_integer;
+};
+
+struct message_recv
+{
+ mach_msg_header_t head;
+ mach_msg_type_t type;
+ int inline_integer;
+ mach_msg_trailer_t trailer;
+};
+
+mach_port_t bootstrap_port;
+
+#define MACH_MSG_TYPE_INTEGER_32 2
+#define N 100000000
+
+static void
+error(int exitcode, int macherr, const char *funcname)
+{
+ printf("%s failed with %x\n", funcname, macherr);
+ exit(exitcode);
+}
+
+void *
+client(void *arg)
+{
+ mach_port_t port = *(mach_port_t *) arg;
+ struct message_recv message = {};
+ int err, i;
+
+ message.head.msgh_local_port = port;
+ message.head.msgh_size = sizeof(message);
+
+ for (i = 0; i < N; i++) {
+ /* Receive a message */
+ err = mach_msg(&message.head, /* The header */
+ MACH_RCV_MSG, /* Flags */
+ 0, /* Send size */
+ sizeof(message), /* Max receive size */
+ port, /* Receive port */
+ MACH_MSG_TIMEOUT_NONE, /* No timeout */
+ MACH_PORT_NULL); /* No notification */
+ if (err)
+ error(1, err, "server mach_msg MACH_RCV_MSG");
+ if (message.inline_integer != i)
+ errx(1, "FAIL message.inline_integer = %d, i = %d",
+ message.inline_integer, i);
+ }
+
+ return(0);
+}
+
+int
+main(void)
+{
+ pthread_t ptd;
+ mach_port_t port;
+ struct integer_message message;
+ int err, i;
+
+ /* Allocate a port */
+ err = mach_port_allocate(mach_task_self(),
+ MACH_PORT_RIGHT_RECEIVE, &port);
+ if (err)
+ error(1, err, "mach_port_allocate");
+
+ err = mach_port_insert_right(mach_task_self(),
+ port, port, MACH_MSG_TYPE_MAKE_SEND);
+
+ if (err)
+ error(10, err, "mach_port_insert_right");
+
+ if ((err = pthread_create(&ptd, NULL, client, &port)) != 0) {
+ errc(1, err, "pthread_create failed");
+ }
+
+ /* Fill the header fields : */
+ message.head.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND);
+ message.head.msgh_size = sizeof( struct integer_message );
+ message.head.msgh_local_port = MACH_PORT_NULL;
+ message.head.msgh_remote_port = port;
+ message.head.msgh_id = 0; /* Message id */
+ message.head.msgh_size = sizeof(message); /* Message size */
+
+ /* Set the message type */
+ message.type.msgt_name = MACH_MSG_TYPE_INTEGER_32;
+ message.type.msgt_size = sizeof(message);
+ message.type.msgt_number = 1;
+ message.type.msgt_inline = TRUE;
+ message.type.msgt_longform = FALSE;
+ message.type.msgt_deallocate = FALSE;
+
+ for (i = 0; i < N; i++) {
+
+ message.inline_integer = i;
+
+ /* Send the message */
+ err = mach_msg(&message.head, /* The header */
+ MACH_SEND_MSG, /* Flags */
+ sizeof(message), /* Send size */
+ 0, /* Max receive Size */
+ port, /* Send port */
+ MACH_MSG_TIMEOUT_NONE, /* No timeout */
+ MACH_PORT_NULL); /* No notification */
+ if (err)
+ error(3, err, "client mach_msg");
+ }
+ pthread_join(ptd, NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/machipc2.sh b/tools/test/stress2/misc/machipc2.sh
new file mode 100755
index 000000000000..bfa4e288495a
--- /dev/null
+++ b/tools/test/stress2/misc/machipc2.sh
@@ -0,0 +1,292 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Non threaded Mach IPC test scenario
+# https://people.freebsd.org/~pho/stress/log/kip018.txt
+
+ps -p1 | grep -q launchd || exit 0
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > machipc2.c
+# Test fails without the -lpthread. Need to investigate why.
+cc -o machipc2 -Wall -Wextra -O2 -g machipc2.c -lmach -lpthread || exit 1
+rm machipc2.c
+cd $odir
+
+(cd ../testcases/swap; ./swap -t 5m -i 20 -h -v) &
+sleep 5
+/tmp/machipc2
+pkill swap
+rm -f /tmp/machipc2
+exit 0
+EOF
+/*
+ Inspired by: Michael Weber: http://www.foldr.org/~michaelw/log/2009/03/13/
+ */
+
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <mach/mach.h>
+#include <servers/bootstrap.h>
+
+#include <err.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#define MACH_MSG_TYPE_INTEGER_32 2
+#define N 200000000
+
+typedef struct {
+ unsigned int msgt_name : 8,
+ msgt_size : 8,
+ msgt_number : 12,
+ msgt_inline : 1,
+ msgt_longform : 1,
+ msgt_deallocate : 1,
+ msgt_unused : 1;
+} mach_msg_type_t;
+
+struct integer_message {
+ mach_msg_header_t head;
+ mach_msg_type_t type;
+
+ int inline_integer;
+};
+
+struct message_recv
+{
+ mach_msg_header_t head;
+ mach_msg_type_t type;
+ int inline_integer;
+ mach_msg_trailer_t trailer;
+};
+
+static task_t child_task = MACH_PORT_NULL;
+
+mach_port_t bootstrap_port;
+
+#define CHECK_MACH_ERROR(err, s) \
+ do { \
+ if (err != KERN_SUCCESS) { \
+ fprintf(stderr, "%s: %s", s, mach_error_string(err)); \
+ exit(1); \
+ } \
+ } while (0)
+
+static int
+setup_recv_port (mach_port_t *recv_port)
+{
+ kern_return_t kerr;
+ mach_port_t port = MACH_PORT_NULL;
+ kerr = mach_port_allocate (mach_task_self (),
+ MACH_PORT_RIGHT_RECEIVE, &port);
+ CHECK_MACH_ERROR (kerr, "mach_port_allocate failed:");
+
+ kerr = mach_port_insert_right (mach_task_self (),
+ port, port, MACH_MSG_TYPE_MAKE_SEND);
+ CHECK_MACH_ERROR (kerr, "mach_port_insert_right failed:");
+
+ *recv_port = port;
+
+ return (0);
+}
+
+static int
+send_port (mach_port_t remote_port, mach_port_t port)
+{
+ kern_return_t kerr;
+
+ struct {
+ mach_msg_header_t header;
+ mach_msg_body_t body;
+ mach_msg_port_descriptor_t task_port;
+ } msg;
+
+ msg.header.msgh_remote_port = remote_port;
+ msg.header.msgh_local_port = MACH_PORT_NULL;
+ msg.header.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND, 0) |
+ MACH_MSGH_BITS_COMPLEX;
+ msg.header.msgh_size = sizeof msg;
+
+ msg.body.msgh_descriptor_count = 1;
+ msg.task_port.name = port;
+ msg.task_port.disposition = MACH_MSG_TYPE_COPY_SEND;
+ msg.task_port.type = MACH_MSG_PORT_DESCRIPTOR;
+
+ kerr = mach_msg_send (&msg.header);
+ CHECK_MACH_ERROR(kerr, "mach_msg_send failed:");
+
+ return (0);
+}
+
+static int
+recv_port(mach_port_t recv_port, mach_port_t *port)
+{
+ kern_return_t kerr;
+ struct {
+ mach_msg_header_t header;
+ mach_msg_body_t body;
+ mach_msg_port_descriptor_t task_port;
+ mach_msg_trailer_t trailer;
+ } msg;
+
+ kerr = mach_msg(&msg.header, MACH_RCV_MSG,
+ 0, sizeof msg, recv_port,
+ MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
+ CHECK_MACH_ERROR(kerr, "mach_msg failed:");
+
+ *port = msg.task_port.name;
+ return (0);
+}
+
+void
+writeint(mach_port_t port)
+{
+ struct integer_message message;
+ int kerr, i;
+
+ /* Fill the header fields : */
+ message.head.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND);
+ message.head.msgh_size = sizeof( struct integer_message );
+ message.head.msgh_local_port = MACH_PORT_NULL;
+ message.head.msgh_remote_port = port;
+ message.head.msgh_id = 0; /* Message id */
+ message.head.msgh_size = sizeof(message); /* Message size */
+
+ /* Set the message type */
+ message.type.msgt_name = MACH_MSG_TYPE_INTEGER_32;
+ message.type.msgt_size = sizeof(message);
+ message.type.msgt_number = 1;
+ message.type.msgt_inline = TRUE;
+ message.type.msgt_longform = FALSE;
+ message.type.msgt_deallocate = FALSE;
+
+ for (i = 0; i < N; i++) {
+ message.inline_integer = i;
+
+ /* Send the message */
+ kerr = mach_msg(&message.head, /* The header */
+ MACH_SEND_MSG, /* Flags */
+ sizeof(message), /* Send size */
+ 0, /* Max receive Size */
+ port, /* Send port */
+ MACH_MSG_TIMEOUT_NONE, /* No timeout */
+ MACH_PORT_NULL); /* No notification */
+ if (kerr)
+ errx(1, "client mach_msg: %s", mach_error_string(kerr));
+ }
+}
+
+void
+readint(mach_port_t port)
+{
+ struct message_recv rmessage = {};
+ int kerr, i;
+
+ rmessage.head.msgh_local_port = port;
+ rmessage.head.msgh_size = sizeof(rmessage);
+
+ for (i = 0; i < N; i++) {
+ /* Receive a message */
+ kerr = mach_msg(&rmessage.head, /* The header */
+ MACH_RCV_MSG, /* Flags */
+ 0, /* Send size */
+ sizeof(rmessage), /* Max receive size */
+ port, /* Receive port */
+ MACH_MSG_TIMEOUT_NONE, /* No timeout */
+ MACH_PORT_NULL); /* No notification */
+ if (kerr)
+ errx(1, "client mach_msg MACH_RCV_MSG: %s", mach_error_string(kerr));
+ if (rmessage.inline_integer != i)
+ errx(1, "FAIL message.inline_integer = %d, i = %d",
+ rmessage.inline_integer, i);
+ }
+}
+
+void
+sampling_fork(void)
+{
+ pid_t pid;
+ kern_return_t kerr;
+ mach_port_t parent_recv_port = MACH_PORT_NULL;
+ mach_port_t child_recv_port = MACH_PORT_NULL;
+
+ if (setup_recv_port(&parent_recv_port) != 0)
+ return;
+ kerr = task_set_bootstrap_port(mach_task_self(), parent_recv_port);
+ CHECK_MACH_ERROR(kerr, "task_set_bootstrap_port failed:");
+
+ if ((pid = fork()) == -1)
+ err(1, "fork");
+
+ if (pid == 0) {
+ kerr = task_get_bootstrap_port(mach_task_self(), &parent_recv_port);
+ CHECK_MACH_ERROR(kerr, "task_get_bootstrap_port failed:");
+ if (setup_recv_port(&child_recv_port) != 0)
+ return;
+ if (send_port(parent_recv_port, mach_task_self()) != 0)
+ return;
+ if (send_port(parent_recv_port, child_recv_port) != 0)
+ return;
+ if (recv_port(child_recv_port, &bootstrap_port) != 0)
+ return;
+ kerr = task_set_bootstrap_port(mach_task_self(), bootstrap_port);
+ CHECK_MACH_ERROR(kerr, "task_set_bootstrap_port failed:");
+
+ readint(child_recv_port);
+
+ _exit(0);
+ }
+
+ /* parent */
+ kerr = task_set_bootstrap_port(mach_task_self(), bootstrap_port);
+ CHECK_MACH_ERROR(kerr, "task_set_bootstrap_port failed:");
+ if (recv_port(parent_recv_port, &child_task) != 0)
+ return;
+ if (recv_port(parent_recv_port, &child_recv_port) != 0)
+ return;
+ if (send_port(child_recv_port, bootstrap_port) != 0)
+ return;
+ kerr = mach_port_deallocate(mach_task_self(), parent_recv_port);
+ CHECK_MACH_ERROR(kerr, "mach_port_deallocate failed:");
+
+ writeint(child_recv_port);
+}
+
+int
+main(void)
+{
+ sampling_fork();
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/marcus.sh b/tools/test/stress2/misc/marcus.sh
new file mode 100755
index 000000000000..727b1ad56c17
--- /dev/null
+++ b/tools/test/stress2/misc/marcus.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Run with marcus.cfg on a 1g swap backed MD with UFS SU fs.
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=20m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg'
+
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ [ $((n += 1)) -gt 300 ] && { echo FAIL; exit 1; }
+done
+checkfs /dev/md${mdstart}$part; s=$?
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/marcus2.sh b/tools/test/stress2/misc/marcus2.sh
new file mode 100755
index 000000000000..1e58c52ddfde
--- /dev/null
+++ b/tools/test/stress2/misc/marcus2.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Run with marcus.cfg on a 2g swap backed MD with UFS SU fs.
+# Copy of marcus.sh, but with maximum load.
+
+# "panic: vm_radix_remove: invalid key found" seen.
+# watchdog fired: https://people.freebsd.org/~pho/stress/log/marcus2.txt
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=20m
+export RUNDIR=$mntpoint/stressX
+
+n=`find ../testcases -perm -1 -type f | wc -l`
+m=`su $testuser -c "limits | awk '/maxprocesses/ {print \\$NF}'"`
+export INCARNATIONS=$((m / n))
+export swapINCARNATIONS=$INCARNATIONS
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' &
+
+sleep 10
+start=`date '+%s'`
+while pgrep -q run; do
+ [ $((`date '+%s'` - start)) -gt 1500 ] &&
+ ../tools/killall.sh
+ sleep 10
+done
+wait
+
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ [ $((n += 1)) -gt 300 ] && { echo FAIL; exit 1; }
+done
+checkfs /dev/md${mdstart}$part; s=$?
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/marcus3.sh b/tools/test/stress2/misc/marcus3.sh
new file mode 100755
index 000000000000..f870ebe89115
--- /dev/null
+++ b/tools/test/stress2/misc/marcus3.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Watchdog fired:
+# https://people.freebsd.org/~pho/stress/log/kostik853.txt
+# but runs for a long time without watchdogd and deadlkres.
+
+. ../default.cfg
+
+dev=$(df -h `dirname $RUNDIR` | tail -1 | awk '{print $1}')
+mount | grep $dev | grep -q journaled && exit 0
+size=$((`sysctl -n hw.physmem` / 1024 / 1024))
+[ $size -gt $((4 * 1024)) ] &&
+ echo "RAM should be capped to 4GB for this test."
+[ "`sysctl -n debug.deadlkres.sleepfreq 2>/dev/null`" = "3" ] &&
+ { echo "deadlkres must be disabled for this test."; exit 0; }
+
+n=`find ../testcases -perm -1 -type f | wc -l`
+m=`su $testuser -c "limits | grep maxprocesses | awk '{print \\$NF}'"`
+export runRUNTIME=15m
+export INCARNATIONS=$((m / n))
+export swapINCARNATIONS=$INCARNATIONS
+
+timeout 16m su $testuser -c 'cd ..; ./run.sh marcus.cfg'
+exit 0
diff --git a/tools/test/stress2/misc/marcus4.sh b/tools/test/stress2/misc/marcus4.sh
new file mode 100755
index 000000000000..60f0e4c87605
--- /dev/null
+++ b/tools/test/stress2/misc/marcus4.sh
@@ -0,0 +1,86 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Copy of marcus3.sh, but without the VM (page stealer) pressure.
+# Deadlock and "panic: smp_targeted_tlb_shootdown: interrupts disabled"
+# https://people.freebsd.org/~pho/stress/log/marcus4.txt
+
+# "panic: spin lock held too long" seen.
+# Fixed in r313472.
+
+. ../default.cfg
+
+pgrep -q watchdogd && { service watchdogd stop > /dev/null && restart=1; }
+dev=$(df -h `dirname $RUNDIR` | tail -1 | awk '{print $1}')
+mount | grep $dev | grep -q journaled && exit 0
+size=$((`sysctl -n hw.physmem` / 1024 / 1024))
+[ $size -gt $((4 * 1024)) ] &&
+ { echo "RAM should be capped to 4GB for this test."; }
+[ "`sysctl -n debug.deadlkres.sleepfreq 2>/dev/null`" = "3" ] &&
+ { echo "deadlkres must be disabled for this test."; exit 0; }
+
+n=`find ../testcases -perm -1 -type f | wc -l`
+m=`su $testuser -c "limits | grep maxprocesses | awk '{print \\$NF}'"`
+m=$((m / 2))
+
+export INCARNATIONS=$((m / n))
+export runRUNTIME=15m
+export LOAD=80
+export symlinkLOAD=80
+export rwLOAD=80
+export TESTPROGS="
+testcases/lockf2/lockf2
+testcases/symlink/symlink
+testcases/openat/openat
+testcases/rw/rw
+testcases/fts/fts
+testcases/link/link
+testcases/lockf/lockf
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/mkfifo/mkfifo
+"
+
+start=`date +%s`
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' &
+
+sleep $((16 * 60))
+../tools/killall.sh; s=$?
+wait
+./cleanup.sh
+elapsed=$((`date +%s` - start))
+if [ $elapsed -gt $((30 * 60)) ]; then
+ echo "Runtime is $elapsed seconds"
+ s=100
+fi
+
+[ $restart ] && service watchdogd start > /dev/null
+exit $s
diff --git a/tools/test/stress2/misc/marcus5.sh b/tools/test/stress2/misc/marcus5.sh
new file mode 100755
index 000000000000..97c2465ba49c
--- /dev/null
+++ b/tools/test/stress2/misc/marcus5.sh
@@ -0,0 +1,85 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+
+# Copy of marcus4.sh, but with a md(4) disk.
+# "panic: userret: Returning with SU cleanup request not handled" seen:
+# https://people.freebsd.org/~pho/stress/log/marcus5.txt
+# Fixed by r292541.
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 5g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+size=$((`sysctl -n hw.physmem` / 1024 / 1024))
+[ $size -gt $((4 * 1024)) ] &&
+ echo "RAM should be capped to 4GB for this test."
+[ "`sysctl -n debug.deadlkres.sleepfreq 2>/dev/null`" = "3" ] &&
+ { echo "deadlkres must be disabled for this test."; exit 0; }
+
+n=`find ../testcases -perm -1 -type f | wc -l`
+m=`su $testuser -c "limits | awk '/maxprocesses/ {print \\$NF}'"`
+
+export RUNDIR=$mntpoint/stressX
+export INCARNATIONS=$((m / n))
+export runRUNTIME=15m
+export LOAD=80
+export symlinkLOAD=80
+export rwLOAD=80
+export TESTPROGS="
+testcases/lockf2/lockf2
+testcases/symlink/symlink
+testcases/openat/openat
+testcases/rw/rw
+testcases/fts/fts
+testcases/link/link
+testcases/lockf/lockf
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/mkfifo/mkfifo
+"
+
+timeout 20m su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
+
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ [ $((n += 1)) -gt 300 ] && { echo FAIL; exit 1; }
+done
+checkfs /dev/md${mdstart}$part; s=$!
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/marcus6.sh b/tools/test/stress2/misc/marcus6.sh
new file mode 100755
index 000000000000..076a51e1e4a4
--- /dev/null
+++ b/tools/test/stress2/misc/marcus6.sh
@@ -0,0 +1,83 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+
+# Variation of marcus5.sh
+# "panic: Assertion pgrp->pg_jobc > 0 failed at kern_proc.c:740" seen.
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 5g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+size=$((`sysctl -n hw.physmem` / 1024 / 1024))
+[ "`sysctl -n debug.deadlkres.sleepfreq 2>/dev/null`" = "3" ] &&
+ { echo "deadlkres must be disabled for this test."; exit 0; }
+
+m=`su $testuser -c "limits | awk '/maxprocesses/ {print \\$NF}'"`
+[ $m -gt 100000 ] && m=100000
+
+rm -rf /tmp/stressX.control
+export RUNDIR=$mntpoint/stressX
+export INCARNATIONS=$((m / 11))
+export runRUNTIME=3m
+export LOAD=80
+export symlinkLOAD=80
+export rwLOAD=80
+export TESTPROGS="
+testcases/lockf2/lockf2
+testcases/symlink/symlink
+testcases/openat/openat
+testcases/rw/rw
+testcases/fts/fts
+testcases/link/link
+testcases/lockf/lockf
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/mkfifo/mkfifo
+"
+
+timeout -s SIGINT -k 2m 1m su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
+
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ [ $((n += 1)) -gt 60 ] && { echo "Timed out"; exit 1; }
+done
+checkfs /dev/md${mdstart}$part; s=$!
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/marcus7.sh b/tools/test/stress2/misc/marcus7.sh
new file mode 100755
index 000000000000..94800c5c5f60
--- /dev/null
+++ b/tools/test/stress2/misc/marcus7.sh
@@ -0,0 +1,90 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Variation of marcus5.sh with more rename tests.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+
+# Copy of marcus4.sh, but with a md(4) disk.
+# "panic: userret: Returning with SU cleanup request not handled" seen:
+# https://people.freebsd.org/~pho/stress/log/marcus5.txt
+# Fixed by r292541.
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 5g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+size=$((`sysctl -n hw.physmem` / 1024 / 1024))
+[ $size -gt $((4 * 1024)) ] &&
+ echo "RAM should be capped to 4GB for this test."
+[ "`sysctl -n debug.deadlkres.sleepfreq 2>/dev/null`" = "3" ] &&
+ { echo "deadlkres must be disabled for this test."; exit 0; }
+
+n=`find ../testcases -perm -1 -type f | wc -l`
+m=`su $testuser -c "limits | awk '/maxprocesses/ {print \\$NF}'"`
+
+export RUNDIR=$mntpoint/stressX
+export INCARNATIONS=$((m / n))
+export runRUNTIME=10m
+export LOAD=80
+export symlinkLOAD=80
+export rwLOAD=80
+export TESTPROGS="
+testcases/lockf2/lockf2
+testcases/symlink/symlink
+testcases/openat/openat
+testcases/rw/rw
+testcases/fts/fts
+testcases/link/link
+testcases/lockf/lockf
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/mkfifo/mkfifo
+testcases/dirnprename/dirnprename
+testcases/dirrename/dirrename
+"
+
+timeout 12m su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
+
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ [ $((n += 1)) -gt 300 ] && { echo FAIL; exit 1; }
+done
+checkfs /dev/md${mdstart}$part; s=$!
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/maxmemdom.sh b/tools/test/stress2/misc/maxmemdom.sh
new file mode 100755
index 000000000000..661de87410f2
--- /dev/null
+++ b/tools/test/stress2/misc/maxmemdom.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Demonstrate that "options MAXMEMDOM" is broken. (NUMA test)
+# panic: vm_page_alloc: missing page
+# https://people.freebsd.org/~pho/stress/log/maxmemdom.txt
+# Fixed in r293640.
+
+. ../default.cfg
+
+[ `sysctl -n vm.ndomains` -eq 1 ] && exit 0
+size=$((`sysctl -n hw.physmem` / 1024 / 1024))
+need=$((size * 2))
+d1=$diskimage.1
+d2=$diskimage.2
+rm -f $d1 $d2
+[ `df -k $(dirname $diskimage) | tail -1 | awk '{print int($4 / 1024)}'` -lt \
+ $need ] && printf "Need %d MB on %s.\n" $need `dirname $diskimage` && exit
+timeout 30m sh -ce "
+ dd if=/dev/zero of=$d1 bs=1m count=$size status=none
+ cp $d1 $d2
+"
+s=$?
+[ $s -eq 124 ] && echo "Timed out"
+
+rm -f $d1 $d2
+exit $s
diff --git a/tools/test/stress2/misc/maxproc.sh b/tools/test/stress2/misc/maxproc.sh
new file mode 100755
index 000000000000..c425c307e06b
--- /dev/null
+++ b/tools/test/stress2/misc/maxproc.sh
@@ -0,0 +1,167 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Test that a non root user can at most have maxproc - 10 processes.
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > maxproc.c
+mycc -o maxproc -Wall -Wextra maxproc.c -lkvm || exit 1
+rm -f maxproc.c
+[ `sysctl -n kern.maxproc` -gt 37028 ] && exit 0 # Excessive run time
+cd $here
+
+/tmp/maxproc
+
+rm -f /tmp/maxproc
+exit
+EOF
+#include <sys/wait.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <kvm.h>
+#include <limits.h>
+#include <pwd.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+enum {
+ NL_NPROCS,
+ NL_MAXPROC,
+ NL_MARKER
+};
+
+static struct {
+ int order;
+ const char *name;
+} namelist[] = {
+ { NL_NPROCS, "_nprocs" },
+ { NL_MAXPROC, "_maxproc" },
+ { NL_MARKER, "" },
+};
+
+#define NNAMES (sizeof(namelist) / sizeof(*namelist))
+#define MULTIUSERFUZZ 5
+
+static struct nlist nl[NNAMES];
+
+static void
+t2(void)
+{
+ pid_t p;
+
+ for (;;) {
+ if ((p = fork()) == 0) {
+ sleep(2);
+ _exit(0);
+ }
+ if (p == -1)
+ break;
+ }
+}
+
+static void
+t1(int priv)
+{
+ pid_t p;
+ struct passwd *pw;
+
+ if ((p = fork()) == 0) {
+ if ((pw = getpwnam("nobody")) == NULL)
+ err(1, "no such user: nobody");
+
+ if (priv == 0) {
+ if (setgroups(1, &pw->pw_gid) ||
+ setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
+ seteuid(pw->pw_uid) || setuid(pw->pw_uid))
+ err(1, "Can't drop privileges to \"nobody\"");
+ }
+ endpwent();
+
+ t2();
+ _exit(0);
+ }
+ waitpid(p, NULL, 0);
+}
+
+int
+getprocs(void)
+{
+ kvm_t *kd;
+ int i, nprocs, maxproc;
+ char buf[_POSIX2_LINE_MAX];
+ char *nlistf, *memf;
+
+ nlistf = memf = NULL;
+ for (i = 0; i < (int)NNAMES; i++)
+ nl[namelist[i].order].n_name = strdup(namelist[i].name);
+
+ if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
+ errx(1, "kvm_openfile(%s, %s): %s", nlistf, memf, buf);
+ if (kvm_nlist(kd, nl) == -1)
+ errx(1, "kvm_nlist: %s", kvm_geterr(kd));
+ if (kvm_read(kd, nl[NL_NPROCS].n_value, &nprocs,
+ sizeof(nprocs)) != sizeof(nprocs))
+ errx(1, "kvm_read(): %s", kvm_geterr(kd));
+ if (kvm_read(kd, nl[NL_MAXPROC].n_value, &maxproc,
+ sizeof(maxproc)) != sizeof(maxproc))
+ errx(1, "kvm_read(): %s", kvm_geterr(kd));
+ kvm_close(kd);
+
+ return (maxproc - nprocs - 1);
+}
+
+int
+main(void)
+{
+ int i, n;
+
+ alarm(1200);
+ n = getprocs();
+ for (i = 0; i < n / 10 * 8; i++) {
+ if (fork() == 0) {
+ sleep(2);
+ _exit(0);
+ }
+ }
+
+ t1(0);
+
+ n = getprocs();
+ if (n < 10 - MULTIUSERFUZZ)
+ errx(1, "FAIL: nprocs = %d\n", n);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/maxvnodes.sh b/tools/test/stress2/misc/maxvnodes.sh
new file mode 100755
index 000000000000..b962310cbb0e
--- /dev/null
+++ b/tools/test/stress2/misc/maxvnodes.sh
@@ -0,0 +1,70 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Test dynamic kern.maxvnodes implementation.
+
+# "panic: vm_fault_hold: fault on nofault entry, addr: 0xfffffe00b1b3c000"
+# seen: https://people.freebsd.org/~pho/stress/log/kostik1175.txt
+
+. ../default.cfg
+
+kldstat | grep -q tmpfs.ko || notloaded=1
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mount -o size=2g -t tmpfs tmpfs $mntpoint || exit 1
+chmod 777 $mntpoint
+
+oldmx=`sysctl -n kern.maxvnodes`
+trap "sysctl kern.maxvnodes=$oldmx > /dev/null" EXIT SIGINT
+
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+export TESTPROGS="
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/swap/swap
+"
+export creatINCARNATIONS=50
+export creatLOAD=100
+
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > /dev/null 2>&1 &
+
+min=1000
+max=$((oldmx * 4))
+while kill -0 $! 2>/dev/null; do
+ sysctl kern.maxvnodes=`jot -r 1 $min $max` > /dev/null
+ sleep `jot -r 1 1 3`
+done
+wait
+
+while mount | grep $mntpoint | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
+[ $notloaded ] && kldunload tmpfs.ko
+exit 0
diff --git a/tools/test/stress2/misc/maxvnodes2.sh b/tools/test/stress2/misc/maxvnodes2.sh
new file mode 100755
index 000000000000..798eff1bffd6
--- /dev/null
+++ b/tools/test/stress2/misc/maxvnodes2.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC Isilon
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "panic: Bad link elm 0xfffff8015ea20000 prev->next != elm" seen:
+# https://people.freebsd.org/~pho/stress/log/mjguzik005.txt
+# Fixed by r309067.
+
+oldmx=`sysctl -n kern.maxvnodes`
+trap "sysctl kern.maxvnodes=$oldmx > /dev/null" EXIT SIGINT
+
+sysctl kern.maxvnodes=2000
+
+timeout 10m ../misc/msdos6.sh
+
+exit
diff --git a/tools/test/stress2/misc/md.sh b/tools/test/stress2/misc/md.sh
new file mode 100755
index 000000000000..47e491e2b841
--- /dev/null
+++ b/tools/test/stress2/misc/md.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Caused panic: ffs_truncate3
+# The problem is caused by a full FS with Soft-updates disabled.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep "$mntpoint" | grep md${mdstart}$part > /dev/null && umount $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2m -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+export RUNDIR=$mntpoint/stressX
+export KBLOCKS=30000 # Exaggerate disk capacity
+export INODES=8000
+
+for i in `jot 20`; do
+ (cd ../testcases/rw;./rw -t 2m -i 20 > /dev/null 2>&1)
+done
+
+while mount | grep -q $mntpoint; do
+ umount $([ $((`date '+%s'` % 2)) -eq 0 ] &&
+ echo "-f" || echo "") $mntpoint > /dev/null 2>&1
+done
+
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/md2.sh b/tools/test/stress2/misc/md2.sh
new file mode 100755
index 000000000000..939d7e46db39
--- /dev/null
+++ b/tools/test/stress2/misc/md2.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2011 Peter Holm <pho@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 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.
+#
+
+# panic: ufs_dirbad: /mnt: bad dir ino 32899 at offset 16896: mangled entry
+
+# "panic: ffs_read: type 0" seen:
+# https://people.freebsd.org/~pho/stress/log/kostik969.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep "on $mntpoint " | grep -q md$mdstart && umount $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t malloc -s 256m -u $mdstart
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=10m
+for i in 1 2; do
+ echo "newfs -O$i -i1024 /dev/md$mdstart"
+ newfs -O$i -i1024 /dev/md$mdstart > /dev/null 2>&1
+ mount /dev/md$mdstart $mntpoint
+
+ (cd ..; ./run.sh)
+
+ while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint
+ done
+done
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/md3.sh b/tools/test/stress2/misc/md3.sh
new file mode 100755
index 000000000000..626d578e54ae
--- /dev/null
+++ b/tools/test/stress2/misc/md3.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009-2011 Peter Holm <pho@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 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.
+#
+
+# Test scenario for deadlock fixed in r200447
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q "$mntpoint" && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1400m -u $mdstart
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+# Stop FS "out of inodes" problem by only using 70%
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export KBLOCKS=$(($1 / 10 * 7))
+export INODES=$(($2 / 10 * 7))
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=10m
+
+(cd ..; ./run.sh marcus.cfg)
+
+umount $mntpoint
+mount | grep -q "$mntpoint" && umount -f $mntpoint
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/md4.sh b/tools/test/stress2/misc/md4.sh
new file mode 100755
index 000000000000..484e531e3f6c
--- /dev/null
+++ b/tools/test/stress2/misc/md4.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# Demonstrate data corruption on the swap-backed md.
+# Test scenario by Nigel Williams <njwilliams swin edu au>.
+# Fixed in r250966.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+status=0
+MD_DEV=`mdconfig -an -t swap -s 1m -x 63 -y 16`
+fdisk -I md$MD_DEV > /dev/null 2>&1
+bsdlabel -w -B md${MD_DEV}s1 || exit 1
+dd if=/dev/md$MD_DEV of=/dev/null bs=64k status=none
+bsdlabel md${MD_DEV}s1 > /dev/null ||
+ { echo FAIL; status=1; }
+mdconfig -d -u $MD_DEV
+exit $status
diff --git a/tools/test/stress2/misc/md5.sh b/tools/test/stress2/misc/md5.sh
new file mode 100755
index 000000000000..a6655db395b5
--- /dev/null
+++ b/tools/test/stress2/misc/md5.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# "dd: /dev/md5: Input/output error" seen.
+# kib@: kern_physio() detects EOF due to incorrect calculation
+# of bio bio_resid after the bio_length was clipped by the 'excess' code
+# in g_io_check()
+# Test scenario by Stefan Hegnauer <stefan hegnauer gmx ch>
+# Fixed in r259200
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+log=/tmp/md5.log
+dd if=/dev/zero of=$diskimage bs=1k count=5k status=none
+mdconfig -f $diskimage -u md$mdstart || exit 1
+newfs $newfs_flags /dev/md$mdstart > /dev/null
+dd if=/dev/md$mdstart of=/dev/null > $log 2>&1 && s=0 || s=1
+[ $s -eq 1 ] && cat $log
+mdconfig -d -u $mdstart
+rm -f $diskimage $log
+exit $s
diff --git a/tools/test/stress2/misc/md6.sh b/tools/test/stress2/misc/md6.sh
new file mode 100755
index 000000000000..f24f0d921e8b
--- /dev/null
+++ b/tools/test/stress2/misc/md6.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Regression test for mmap problem introduced by r271635
+# where mdconfig -l fails due to
+# mmap(0,0x1000,PROT_READ,MAP_FILE,0x4,0)
+# returning 0.
+# Fixed by r271721.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 10m -u $mdstart || exit 1
+[ -c /dev/md$mdstart ] || echo "FAIL"
+mdconfig -l -v > /dev/null || echo "FAIL of mdconfig -l"
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/md7.sh b/tools/test/stress2/misc/md7.sh
new file mode 100755
index 000000000000..f15377607a97
--- /dev/null
+++ b/tools/test/stress2/misc/md7.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Deadlock seen: The softdep code requesting workqueue cleanups while
+# owning vnode locks:
+# https://people.freebsd.org/~pho/stress/log/kostik812.txt
+# Fixed by: r283600
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `swapinfo | wc -l` -eq 1 ] && exit 0 # Covered by md3.sh
+
+swapoff -a
+./md3.sh
+swapon -a
diff --git a/tools/test/stress2/misc/md8.sh b/tools/test/stress2/misc/md8.sh
new file mode 100755
index 000000000000..e073271dac15
--- /dev/null
+++ b/tools/test/stress2/misc/md8.sh
@@ -0,0 +1,121 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Test of unmapped unaligned i/o over the vnode-backed md(4) volume.
+# "panic: vm_fault: fault on nofault entry, addr: fffffe07f302c000" seen.
+# https://people.freebsd.org/~pho/stress/log/md8.txt
+# Fixed in r292128.
+
+# Test scenario by kib@
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -d /usr/src/sys ] || exit 0
+
+. ../default.cfg
+
+rm -f $diskimage
+dir=`dirname $diskimage`
+free=`df -k $dir | tail -1 | awk '{print $4}'`
+[ $((free / 1024)) -lt 50 ] && echo "Not enough disk space." && exit
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > md8.c
+rm -f /tmp/md8
+mycc -o md8 -Wall -Wextra -g -O2 md8.c || exit 1
+rm -f md8.c
+
+cd $odir
+trap "rm -f $diskimage" EXIT INT
+dd if=/dev/zero of=$diskimage bs=1m count=50 status=none
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t vnode -f $diskimage -u $mdstart
+
+n=`sysctl -n hw.ncpu`
+n=$((n + 1))
+(cd /usr/src; make -j $n buildkernel > /dev/null 2>&1) &
+sleep 1
+/tmp/md8 /dev/md$mdstart
+kill $!
+wait
+
+mdconfig -d -u $mdstart
+rm -rf /tmp/md8
+exit 0
+EOF
+#include <sys/param.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define LOOPS 2000
+
+void
+test(char *path)
+{
+ int fd;
+ char data[MAXPHYS + 512] __aligned(PAGE_SIZE);
+
+ if ((fd = open(path, O_RDONLY)) == -1)
+ err(1, "open(%s)", path);
+ if (read(fd, data + 512, MAXPHYS) != MAXPHYS)
+ err(1, "read");
+ close(fd);
+
+ if ((fd = open(path, O_WRONLY)) == -1)
+ err(1, "open(%s)", path);
+ if (write(fd, data + 512, MAXPHYS) != MAXPHYS)
+ err(1, "write");
+ close(fd);
+
+ if ((fd = open(path, O_RDONLY)) == -1)
+ err(1, "open(%s)", path);
+ if (read(fd, data + 512, MAXPHYS) != MAXPHYS)
+ err(1, "read");
+ close(fd);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int i;
+ char *path;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <path>", argv[0]);
+
+ path = argv[1];
+
+ for (i = 0; i < LOOPS; i++)
+ test(path);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/md9.sh b/tools/test/stress2/misc/md9.sh
new file mode 100755
index 000000000000..a232ade6963f
--- /dev/null
+++ b/tools/test/stress2/misc/md9.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# memory disk with vnode on a tmpfs file system triggers:
+# "g_handleattr(GEOM::ident): md10 bio_length 24 len 31 -> EFAULT"
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+set -e
+mp2=${mntpoint}2
+mkdir -p $mp2
+mount | grep "on $mp2 " | grep -q /dev/md && umount -f $mp2
+mount -t tmpfs tmpfs $mp2
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+diskimage=$mp2/diskimage
+dd if=/dev/zero of=$diskimage bs=1m count=2k status=none
+mdconfig -a -t vnode -f $diskimage -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -U /dev/md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+for i in `jot 10`; do
+ umount $mntpoint && break
+ sleep .5
+done
+mount | grep "on $mntpoint " && { echo FATAL; exit 1; }
+mdconfig -d -u $mdstart
+umount $mp2
+tail -5 /var/log/messages | grep g_handleattr && s=1 || s=0
+
+exit $s
diff --git a/tools/test/stress2/misc/mdconfig.sh b/tools/test/stress2/misc/mdconfig.sh
new file mode 100755
index 000000000000..ecfe188a102d
--- /dev/null
+++ b/tools/test/stress2/misc/mdconfig.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC Isilon
+# 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 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.
+#
+
+. ../default.cfg
+
+# Regression test for:
+# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=214721
+# https://people.freebsd.org/~pho/stress/log/mdconfig.txt
+# Fixed by: r311964.
+
+mdconfig -at swap -S 65536 -s 20M -u $mdstart
+mdconfig -d -u $mdstart
+
+exit 0
diff --git a/tools/test/stress2/misc/mdconfig2.sh b/tools/test/stress2/misc/mdconfig2.sh
new file mode 100755
index 000000000000..13c13dc55111
--- /dev/null
+++ b/tools/test/stress2/misc/mdconfig2.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# /dev/md* fails to be created.
+# "kernel: g_dev_taste: make_dev_p() failed (gp->name=md10, error=17)" seen.
+# The cause is that the device node is removed asynchronously.
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -l | grep -q md$mdstart &&
+ { echo FAIL; ls -l /dev/md$mdstart; mdconfig -lv; exit 1; }
+
+s=0
+start=`date +%s`
+workaround=${workaround:-0}
+while [ $((`date +%s` - start)) -lt 60 ]; do
+ mdconfig -a -t swap -s 100m -u $mdstart || { s=2; break; }
+ [ -c /dev/md$mdstart ] ||
+ { echo FAIL; ls -l /dev/md$mdstart; mdconfig -lv; exit 3; }
+ mdconfig -d -u $mdstart || { s=4; break; }
+ while [ $workaround -eq 1 -a -c /dev/md$mdstart ]; do
+ echo "Note: Waiting for removal of /dev/md$mdstart"
+ sleep .1
+ done
+done
+exit $s
diff --git a/tools/test/stress2/misc/mdconfig3.sh b/tools/test/stress2/misc/mdconfig3.sh
new file mode 100755
index 000000000000..7fbdcbc1a5ff
--- /dev/null
+++ b/tools/test/stress2/misc/mdconfig3.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# mdconfig(x) option force test.
+# Fixed by r345758
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+rm -df $diskimage
+dd if=/dev/zero of=$diskimage bs=1m count=5 status=none
+mdconfig -a -t vnode -f $diskimage -u $mdstart
+dd if=/dev/random of=/dev/md$mdstart bs=1m count=5 status=none
+md1=`md5 < /dev/md$mdstart`
+mdconfig -d -u $mdstart -o force
+
+md2=`md5 < $diskimage`
+rm -d $diskimage
+
+[ "$md1" != "$md2" ] && exit 1 || exit 0
diff --git a/tools/test/stress2/misc/memguard.sh b/tools/test/stress2/misc/memguard.sh
new file mode 100755
index 000000000000..9b2a3a964d1b
--- /dev/null
+++ b/tools/test/stress2/misc/memguard.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# MEMGUARD(9) test scenario: Page fault seen in softdep_setup_inomapdep().
+
+. ../default.cfg
+
+sysctl vm | grep -q memguard || { echo "MEMGUARD(9) not enabled"; exit 0; }
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 20m -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=5m
+export RUNDIR=$mntpoint/stressX
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export KBLOCKS=$(($1 * 2))
+export INODES=$(($2 * 2))
+
+sysctl vm.memguard.options=3 > /dev/null
+sysctl vm.memguard.desc=inodedep
+
+su $testuser -c 'cd ..; ./run.sh disk.cfg' > /dev/null
+
+sysctl vm.memguard.desc="" > /dev/null
+sysctl vm.memguard.options=1 > /dev/null
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/memguard2.sh b/tools/test/stress2/misc/memguard2.sh
new file mode 100755
index 000000000000..4c4619ef89e3
--- /dev/null
+++ b/tools/test/stress2/misc/memguard2.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# MEMGUARD(9) test scenario
+# http://people.freebsd.org/~pho/stress/log/memguard4.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+sysctl vm | grep -q memguard || { echo "MEMGUARD(9) not enabled"; exit 1; }
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=30s
+export RUNDIR=$mntpoint/stressX
+[ -d $RUNDIR ] || mkdir $RUNDIR
+chmod 777 $RUNDIR
+
+sysctl vm.memguard.options=3 > /dev/null
+{ vmstat -m; echo dummy; } | sed '1d;s/ [0-9].*//;s/^ *//' | \
+ sed 's/ *$//' | tr '\n' ' ' | ../tools/shuffle | \
+ tr ' ' '\n' | head -10 | while read type; do
+ sysctl vm.memguard.desc="$type"
+ su $testuser -c 'cd ..; ./run.sh disk.cfg' > /dev/null
+ find /tmp/stressX.control/* $mntpoint/stressX/* -delete 2>/dev/null
+done
+sysctl vm.memguard.desc="" > /dev/null
+sysctl vm.memguard.options=1 > /dev/null
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/memguard3.sh b/tools/test/stress2/misc/memguard3.sh
new file mode 100755
index 000000000000..8a7705c71537
--- /dev/null
+++ b/tools/test/stress2/misc/memguard3.sh
@@ -0,0 +1,63 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# MEMGUARD(9) test scenario using "frequency".
+# "panic: __rw_wlock_hard: recursing but non-recursive rw kmem vm object"
+# seen. http://people.freebsd.org/~pho/stress/log/memguard5.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+sysctl vm | grep -q memguard || { echo "MEMGUARD(9) not enabled"; exit 1; }
+
+sysctl vm.memguard.options=3
+sysctl vm.memguard.frequency=1000
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 5g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=5m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg'
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/md${mdstart}$part; s=$?
+mdconfig -d -u $mdstart
+
+sysctl vm.memguard.frequency=0 > /dev/null
+sysctl vm.memguard.options=1 > /dev/null
+exit $s
diff --git a/tools/test/stress2/misc/memguard4.sh b/tools/test/stress2/misc/memguard4.sh
new file mode 100755
index 000000000000..323183caf96d
--- /dev/null
+++ b/tools/test/stress2/misc/memguard4.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Test without using memguard.options
+
+. ../default.cfg
+
+sysctl vm | grep -q memguard || { echo "MEMGUARD(9) not enabled"; exit 0; }
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=5m
+export RUNDIR=$mntpoint/stressX
+
+sysctl vm.memguard.desc=inodedep
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1
+
+sysctl vm.memguard.desc="" > /dev/null
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/midi.sh b/tools/test/stress2/misc/midi.sh
new file mode 100755
index 000000000000..47594734b4e0
--- /dev/null
+++ b/tools/test/stress2/misc/midi.sh
@@ -0,0 +1,94 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Mark Johnston <markj@freebsd.org>
+#
+# 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 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.
+#
+
+# 'panic: vm_fault_hold: fault on nofault entry, addr: 0x33522000' seen.
+# Fixed by 351262
+
+cat > /tmp/midi.c <<EOF
+#include <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdatomic.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define NTHREADS 16
+
+static _Atomic(int) threads;
+static int fd;
+
+static void *
+t(void *data __unused)
+{
+ char buf[4096];
+ ssize_t n;
+ off_t off;
+
+ (void)atomic_fetch_add(&threads, 1);
+ while (atomic_load(&threads) != NTHREADS)
+ ;
+
+ for (;;) {
+ arc4random_buf(&off, sizeof(off));
+ if ((n = pread(fd, buf, sizeof(buf), off)) >= 0)
+ write(STDOUT_FILENO, buf, n);
+ }
+
+ return (NULL);
+}
+
+int
+main(void)
+{
+ pthread_t tid[NTHREADS];
+ int error, i;
+
+ fd = open("/dev/midistat", O_RDONLY);
+ if (fd < 0)
+ err(1, "open");
+
+ for (i = 0; i < NTHREADS; i++)
+ if ((error = pthread_create(&tid[i], NULL, t, NULL)) != 0)
+ errc(1, error, "pthread_create");
+ for (i = 0; i < NTHREADS; i++)
+ if ((error = pthread_join(tid[i], NULL)) != 0)
+ errc(1, error, "pthread_join");
+
+ return (0);
+}
+EOF
+cc -o /tmp/midi -Wall -Wextra -O2 /tmp/midi.c -lpthread
+
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 120 ]; do
+ timeout 10 /tmp/midi | strings | head -20
+done
+
+rm -f /tmp/midi /tmp/midi.c /tmp/midi.core
+exit 0
diff --git a/tools/test/stress2/misc/midi2.sh b/tools/test/stress2/misc/midi2.sh
new file mode 100755
index 000000000000..91f15aa3cc3b
--- /dev/null
+++ b/tools/test/stress2/misc/midi2.sh
@@ -0,0 +1,114 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Test threaded access to /dev/midistat.
+
+# "panic: vm_fault_hold: fault on nofault entry, addr: 0x8b352000" seen.
+# https://people.freebsd.org/~pho/stress/log/mark089.txt
+# Fixed by 351262
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/midi2.c
+mycc -o midi2 -Wall -Wextra -O0 -g midi2.c -lpthread || exit 1
+
+$dir/midi2
+s=$?
+cat /dev/midistat > /dev/null || s=1
+
+rm -rf midi2 midi2.c midi2.core
+exit $s
+
+EOF
+#include <sys/param.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int share;
+static int fd;
+static char buf[0xbc59], buf2[0x40eddf];
+
+#define THREADS 4
+#define RUNTIME (1 * 60)
+#define SYNC 0
+
+static void *
+t1(void *data __unused)
+{
+
+ atomic_add_int(&share, 1);
+ while (share != THREADS)
+ ;
+
+ pread(fd, buf, 0xbc59, 0x27dbb298LL);
+ pread(fd, buf, 0xbc59, 0x104e35c6d22eLL);
+ pread(fd, buf2, 0x40eddf, 0x405d1df88cbf41LL);
+
+ return (NULL);
+}
+
+int
+main(void)
+{
+ pthread_t tid[THREADS];
+ time_t start;
+ int i, rc;
+
+ if ((fd = open("/dev/midistat", O_RDONLY)) == -1)
+ err(1, "open(/dev/midistat)");
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ share = 0;
+ for (i = 0; i < THREADS; i++) {
+ if ((rc = pthread_create(&tid[i], NULL, t1, NULL)) !=
+ 0)
+ errc(1, rc, "pthread_create");
+ }
+
+ for (i = 0; i < THREADS; i++) {
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join");
+ }
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mincore.sh b/tools/test/stress2/misc/mincore.sh
new file mode 100755
index 000000000000..85c09cc0ceaf
--- /dev/null
+++ b/tools/test/stress2/misc/mincore.sh
@@ -0,0 +1,157 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Hunt for https://people.freebsd.org/~pho/stress/log/log0018.txt
+
+# Test scenario idea by kib@
+# Scenario should be the following (without paging load, so that pages stay
+# resident):
+# - create some large file, say 2G
+# - map it and touch every page to ensure that they all allocated
+# - unmap, and map again, so while the pages are resident, they are not
+# installed into page table
+# Do mincore(2) on the mapped region.
+#
+
+# The problem was not reproduced.
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+[ `sysctl -n vm.swap_total` -eq 0 ] && exit 0
+path=`dirname $diskimage`
+[ `df -k $path | tail -1 | awk '{print int($4 / 1024 / 1024)}'` -lt 4 ] && exit 0
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mincore.c
+mycc -o mincore -Wall -Wextra -O0 -g mincore.c || exit 1
+rm -f mincore.c
+cd $odir
+
+(cd ../testcases/swap; ./swap -t 10m -i 20 -l 100) &
+for i in `jot 20`; do
+ [ `swapinfo | tail -1 | awk '{gsub("%", ""); print $5}'` -gt 0 ] && break
+ sleep 5
+ pgrep -q swap || break
+done
+echo "`date +%T` Go"
+
+$dir/mincore $path; s=$?
+
+while pkill swap; do :; done
+wait
+rm -rf $dir/mincore
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <fcntl.h>
+#include <err.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define PARALLEL 2
+#define RUNTIME 300
+#define SIZE (2LL * 1024 * 1024 * 1024)
+
+static void
+test(char *dir)
+{
+ size_t i, len;
+ int fd;
+ char file[128], *p, *vec;
+
+ len = SIZE;
+ snprintf(file, sizeof(file), "%s/mincore.%d.file", dir, getpid());
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE))
+ == -1)
+ err(1, "open(%s)", file);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+ if (ftruncate(fd, len) == -1)
+ err(1, "ftruncete()");
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
+ MAP_FAILED)
+ err(1, "mmap()");
+ for (i = 0; i < len; i += PAGE_SIZE)
+ p[i] = 1;
+ if (munmap(p, len) == -1)
+ err(1, "munmap()");
+ if ((vec = malloc(len / PAGE_SIZE)) == NULL)
+ err(1, "malloc");
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
+ MAP_FAILED)
+ err(1, "mmap()");
+ if (mincore(p, len, vec) == -1)
+ err(1, "mincore()");
+ if (munmap(p, len) == -1)
+ err(1, "munmap()");
+ close(fd);
+
+ _exit(0);
+}
+
+int
+main(int argc __unused, char *argv[])
+{
+ pid_t pids[PARALLEL];
+ time_t start;
+ int i;
+
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test(argv[1]);
+ }
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], NULL, WNOHANG) == pids[i]) {
+ if ((pids[i] = fork()) == 0)
+ test(argv[1]);
+ }
+ }
+ }
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], NULL, 0) != pids[i])
+ err(1, "waitpid");
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/minherit.sh b/tools/test/stress2/misc/minherit.sh
new file mode 100755
index 000000000000..facb018a0337
--- /dev/null
+++ b/tools/test/stress2/misc/minherit.sh
@@ -0,0 +1,176 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Peter Holm
+#
+# 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 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.
+#
+
+# minherit() test scenario inspired by Jeff's collapse.sh test.
+# No problems seen.
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/minherit.c
+mycc -o minherit -Wall -Wextra -O0 -g minherit.c || exit 1
+rm -f minherit.c
+cd $odir
+
+(cd $odir/../testcases/swap; ./swap -t 5m -i 10 -l 50) &
+pid=$!
+cd /tmp
+$dir/minherit
+s=$?
+while pkill swap; do :; done
+wait $oid
+[ -f minherit.core -a $s -eq 0 ] &&
+ { ls -l minherit.core; mv minherit.core $dir; s=1; }
+cd $odir
+
+rm -rf $dir/minherit
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdatomic.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static _Atomic(int) *share;
+
+#define ADRSPACE (1024 * 1024)
+#define CHILDREN 200
+#define PARALLEL 10
+#define RUNTIME (5 * 60)
+#define SYNC 0
+
+static void
+test(void)
+{
+ pid_t pids[CHILDREN];
+ off_t len;
+ void *p;
+ int i, ix, j, n, shared;
+ char *cp;
+
+ (void)atomic_fetch_add(&share[SYNC], 1);
+ while (atomic_load(&share[SYNC]) != PARALLEL)
+ ;
+
+ if ((p = mmap(NULL, ADRSPACE, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANON, -1, 0)) == MAP_FAILED) {
+ if (errno == ENOMEM)
+ return;
+ err(1, "mmap()");
+ }
+
+ /* Pick a random bit of address space to change inherit on. */
+ for (i = 0; i < ADRSPACE; i += len) {
+ shared = arc4random() & 0x1;
+ len = roundup2(arc4random() % ((ADRSPACE - i) / 4),
+ PAGE_SIZE);
+ if (minherit(p + i, len, shared ? INHERIT_SHARE :
+ INHERIT_COPY) != 0)
+ err(1, "minherit");
+ }
+
+ n = arc4random() % CHILDREN + 1;
+ for (i = 0; i < n; i++) {
+ pids[i] = fork();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ if (pids[i] == 0) {
+ usleep(arc4random() % 100);
+ for (j = 0; j < 10; j++) {
+ cp = p;
+ for (ix = 0; ix < ADRSPACE; ix += PAGE_SIZE) {
+ cp[ix] = 1;
+ if (arc4random() % 100 < 5)
+ usleep(arc4random() % 50);
+ }
+ }
+ _exit(0);
+ }
+ }
+ for (i = 0; i < n; i++) {
+ if (waitpid(pids[i], NULL, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ e = status;
+ break;
+ }
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/mkfifo.sh b/tools/test/stress2/misc/mkfifo.sh
new file mode 100755
index 000000000000..a0f012da5baa
--- /dev/null
+++ b/tools/test/stress2/misc/mkfifo.sh
@@ -0,0 +1,86 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Hunt for "panic: ufsdirhash_newblk: bad offset"
+
+# but page fault seen in scheduler() due to a _thread_lock_flags() call on
+# an inactive td.
+
+# Fault seen in "softdep_disk_io_initiation+0x41":
+# https://people.freebsd.org/~pho/stress/log/mkfifo.txt
+
+# Run with mkfifo.cfg on a 2g swap backed MD
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+[ "$newfs_flags" = "-U" ] && opt="-j"
+newfs $opt md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=20m
+export RUNDIR=$mntpoint/stressX
+
+export TESTPROGS="
+testcases/creat/creat
+testcases/fts/fts
+testcases/link/link
+testcases/lockf/lockf
+testcases/mkdir/mkdir
+testcases/mkfifo/mkfifo
+testcases/openat/openat
+testcases/rename/rename
+testcases/rw/rw
+testcases/swap/swap
+"
+export creatLOAD=100
+export ftsLOAD=100
+export linkLOAD=100
+export lockfLOAD=100
+export mkdirLOAD=100
+export mkfifoLOAD=100
+export openatLOAD=100
+export renameLOAD=100
+export rwLOAD=100
+export swapLOAD=100
+
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/mkfifo2c.sh b/tools/test/stress2/misc/mkfifo2c.sh
new file mode 100755
index 000000000000..6d83a5aca4a2
--- /dev/null
+++ b/tools/test/stress2/misc/mkfifo2c.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "bad offset" panic after up 10:57 on leopard3
+
+# Run with mkfifo.cfg on a 2g swap backed MD
+# Problem seen with and without +j
+
+# "panic: ufsdirhash_newblk: bad offset" seen:
+# https://people.freebsd.org/~pho/stress/log/mkfifo2c.txt
+# https://people.freebsd.org/~pho/stress/log/mkfifo2c-2.txt
+# https://people.freebsd.org/~pho/stress/log/kostik932.txt
+# Fixed by r305601.
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+[ $# -eq 1 ] && opt="$1"
+[ $# -eq 0 ] && opt="-j"
+newfs $opt md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=20m
+export RUNDIR=$mntpoint/stressX
+
+export TESTPROGS="
+testcases/fts/fts
+testcases/link/link
+testcases/mkfifo/mkfifo
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/swap/swap
+"
+
+export ftsLOAD=100
+export linkLOAD=100
+export mkdirLOAD=100
+export mkfifoLOAD=100
+export renameLOAD=100
+export swapLOAD=100
+
+export renameINCARNATIONS=4
+export swapINCARNATIONS=4
+export linkINCARNATIONS=12
+export mkdirINCARNATIONS=20
+export mkfifoINCARNATIONS=22
+export ftsINCARNATIONS=2
+
+export HOG=1
+
+su $testuser -c 'cd ..; timeout 15m ./testcases/run/run $TESTPROGS'
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/mkfifo2d.sh b/tools/test/stress2/misc/mkfifo2d.sh
new file mode 100755
index 000000000000..74d5875d090f
--- /dev/null
+++ b/tools/test/stress2/misc/mkfifo2d.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Copy of mkfifo2c.sh, but with a SU file system and async mount
+# "panic: ufsdirhash_newblk: bad offset" seen from openat()
+# https://people.freebsd.org/~pho/stress/log/mkfifo2d.txt
+# Fixed by r305601.
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount -o async /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=5m
+export RUNDIR=$mntpoint/stressX
+
+export TESTPROGS="
+testcases/fts/fts
+testcases/link/link
+testcases/mkfifo/mkfifo
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/swap/swap
+"
+
+export ftsLOAD=100
+export linkLOAD=100
+export mkdirLOAD=100
+export mkfifoLOAD=100
+export renameLOAD=100
+export swapLOAD=100
+
+export renameINCARNATIONS=4
+export swapINCARNATIONS=4
+export linkINCARNATIONS=12
+export mkdirINCARNATIONS=20
+export mkfifoINCARNATIONS=22
+export ftsINCARNATIONS=2
+
+export HOG=1
+
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/mkfifo3.sh b/tools/test/stress2/misc/mkfifo3.sh
new file mode 100755
index 000000000000..1f205dbfa98b
--- /dev/null
+++ b/tools/test/stress2/misc/mkfifo3.sh
@@ -0,0 +1,139 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+. ../default.cfg
+
+# Regression test for
+# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=203162
+
+# FAIL
+# 1001 82102 907 0 20 0 6484 2480 wait S+ 0 0:00,01 /bin/sh ./mkfifo3.sh
+# 1001 82113 82102 0 52 0 3832 1604 fifoow I+ 0 0:03,91 /tmp/mkfifo3
+
+# Fixed in r288044.
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mkfifo3.c
+mycc -o mkfifo3 -Wall -Wextra -O0 -g mkfifo3.c || exit 1
+rm -f mkfifo3.c
+cd $odir
+
+fifo=/tmp/mkfifo3.fifo
+trap "rm -f $fifo /tmp/mkfifo3" EXIT INT
+
+/tmp/mkfifo3 &
+
+for i in `jot 12`; do
+ pgrep -q mkfifo3 || break
+ sleep 10
+done
+s=0
+if pgrep -q mkfifo3; then
+ s=1
+ pgrep mkfifo3 | xargs ps -lp
+ pkill mkfifo3
+fi
+wait
+
+exit $s
+EOF
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define ATIME 1
+#define LOOPS 100000
+
+char file[] = "/tmp/mkfifo3.fifo";
+
+static void
+hand(int i __unused) { /* handler */
+}
+
+int
+main(void)
+{
+ pid_t pid;
+ struct sigaction sa;
+ int fd, i, status;
+
+ sa.sa_handler = hand;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction(SIGALRM, &sa, NULL) == -1)
+ err(1, "sigaction");
+
+ if (unlink(file) == -1)
+ if (errno != ENOENT)
+ err(1, "unlink(%s)", file);
+ if (mkfifo(file, 0640) == -1)
+ err(1, "mkfifo(%s)", file);
+
+ for (i = 0; i < LOOPS; i++) {
+ if ((pid = fork()) == 0) {
+ ualarm(ATIME, 0);
+ do {
+ if((fd = open(file, O_RDONLY)) == -1)
+ if (errno != EINTR)
+ err(1, "open(%s, O_RDONLY @ %d)",
+ file, i);
+ } while (fd == -1);
+ if (close(fd) == -1)
+ err(1, "close() in child");
+ alarm(0);
+ _exit(0);
+ }
+ ualarm(ATIME, 0);
+ do {
+ if ((fd = open(file, O_WRONLY)) == -1)
+ if (errno != EINTR)
+ err(1, "open(%s, O_WRONLY @ %d)",
+ file, i);
+ } while (fd == -1);
+ if (close(fd) == -1)
+ err(1, "close() in parent");
+ alarm(0);
+ if (waitpid(pid, &status, 0) == -1)
+ err(1, "wait");
+ }
+
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mkfifo4.sh b/tools/test/stress2/misc/mkfifo4.sh
new file mode 100755
index 000000000000..0e560ffe7b4e
--- /dev/null
+++ b/tools/test/stress2/misc/mkfifo4.sh
@@ -0,0 +1,186 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+. ../default.cfg
+
+# "Assertion vap->va_type == VDIR failed" seen on non HEAD.
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mkfifo4.c
+mycc -o mkfifo4 -Wall -Wextra -O0 -g mkfifo4.c || exit 1
+rm -f mkfifo4.c
+cd $odir
+
+fifo=/tmp/mkfifo4.fifo
+trap "rm -f $fifo /tmp/mkfifo4" EXIT INT
+
+export runRUNTIME=5m
+(cd ..; ./run.sh disk.cfg) > /dev/null 2>&1 &
+sleep .2
+
+while pgrep -fq run.sh; do
+ timeout 300 /tmp/mkfifo4 | grep -v Done
+ [ $? -eq 124 ] &&
+ { echo "Timedout"; exit 1; }
+done
+wait
+
+exit $s
+EOF
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define ATIME 1
+#define MXRETRY 100
+#define LOOPS 1000000
+
+volatile int sigs;
+char file[] = "/tmp/mkfifo4.fifo";
+
+static void
+hand(int i __unused) { /* handler */
+ sigs++;
+}
+
+int
+main(void)
+{
+ pid_t pid, hpid;
+ struct sigaction sa;
+ int e, fd, fd2, i, status;
+ int failures, r, retries, w;
+ char c;
+
+ sa.sa_handler = hand;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction(SIGALRM, &sa, NULL) == -1)
+ err(1, "sigaction");
+
+ sa.sa_handler = SIG_IGN;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction(SIGPIPE, &sa, NULL) == -1)
+ err(1, "sigaction");
+
+ unlink(file);
+ if (mkfifo(file, 0640) == -1)
+ err(1, "mkfifo(%s)", file);
+ if ((hpid = fork()) == 0) {
+ if ((fd2 = open(file, O_WRONLY | O_APPEND)) == -1)
+ err(1, "hold open of fifo");
+ for (;;)
+ pause();
+ _exit(0);
+ }
+
+ if ((pid = fork()) == 0) {
+ setproctitle("child");
+ r = 0;
+ for (i = 0; i < LOOPS; i++) {
+ failures = 0;
+restart:
+ do {
+ if ((fd = open(file, O_RDONLY |
+ O_NONBLOCK)) == -1)
+ if (errno != EINTR) /* on OS X */
+ err(1, "open(%s, O_RDONLY)",
+ file);
+ } while (fd == -1);
+ retries = 0;
+ do {
+ if ((e = read(fd, &c, 1)) == -1) {
+ if (errno != EINTR &&
+ errno != EAGAIN)
+ err(1, "read(%d, ...)", fd);
+ } else if (retries++ > MXRETRY) {
+ close(fd);
+ usleep(1000);
+ fprintf(stderr,
+ "Re-open for read @ %d\n", i);
+ if (failures++ > 100)
+ errx(1,
+ "FAIL: Failure to read");
+ goto restart;
+ }
+ } while (e <= 0);
+ r++;
+ ualarm(ATIME, 0);
+ if (close(fd) == -1)
+ err(1, "close() in child");
+ alarm(0);
+ }
+ fprintf(stdout, "Done child. %d reads, %d signals.\n", r,
+ sigs);
+ fflush(stdout);
+ _exit(0);
+ }
+ setproctitle("parent");
+ w = 0;
+ for (i = 0; i < LOOPS; i++) {
+ do {
+ if ((fd = open(file, O_WRONLY | O_APPEND |
+ O_NONBLOCK)) == -1)
+ if (errno != EINTR && errno != ENXIO)
+ err(1, "open(%s, O_WRONLY)", file);
+ } while (fd == -1);
+ do {
+ if ((e = write(fd, "a", 1)) != 1)
+ if (errno != EPIPE && errno != EAGAIN)
+ err(1, "write(%d, ...)", fd);
+ } while (e == -1);
+ w++;
+ ualarm(ATIME, 0);
+ if (close(fd) == -1)
+ err(1, "close() in parent");
+ alarm(0);
+ }
+ fprintf(stdout, "Done parent. %d writes, %d signals.\n", w, sigs);
+
+ if (waitpid(pid, &status, 0) == -1)
+ err(1, "wait");
+ if (kill(hpid, SIGHUP) == -1)
+ err(1, "kill %d", hpid);
+ if (waitpid(hpid, NULL, 0) == -1)
+ err(1, "wait");
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+
+ return (WEXITSTATUS(status));
+}
diff --git a/tools/test/stress2/misc/mkfifo5.sh b/tools/test/stress2/misc/mkfifo5.sh
new file mode 100755
index 000000000000..c0355cef2db0
--- /dev/null
+++ b/tools/test/stress2/misc/mkfifo5.sh
@@ -0,0 +1,214 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# mkfifo(2), select(2) with tmpfs(5) scenario.
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mkfifo5.c
+mycc -o mkfifo5 -Wall -Wextra -O0 -g mkfifo5.c || exit 1
+rm -f mkfifo5.c
+cd $odir
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+mount -o size=1g -t tmpfs tmpfs $mntpoint
+
+fifo=$mntpoint/fifo.file
+cd $mntpoint
+/tmp/mkfifo5 $fifo
+s=$?
+cd $odir
+
+while mount | grep "on $mntpoint " | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
+rm -rf /tmp/mkfifo5
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static char *path;
+
+#define PARALLEL 1
+
+static void
+reader(void)
+{
+ fd_set rset;
+ struct timeval timeout;
+ int fd, n, r;
+ char ch;
+
+ do {
+ if((fd = open(path, O_RDONLY)) == -1)
+ if (errno != EINTR)
+ err(1, "open(%s, O_RDONLY)",
+ path);
+ if (fd == -1)
+ warn("open(%s) ro", path);
+ } while (fd == -1);
+
+ /* Read one character */
+ FD_ZERO(&rset);
+ FD_SET(fd, &rset);
+ if ((n = select(fd + 1, &rset, NULL, NULL, NULL)) < 0)
+ if (errno != EINTR)
+ err(1, "select()");
+ if (n == 1 && FD_ISSET(fd, &rset)) {
+ r = read(fd, &ch, 1);
+ if (r == -1)
+ err(1, "read");
+ if (r == 0)
+ fprintf(stderr, "read(1): EOF\n");
+ }
+
+ /* timeout */
+ ch = 'z';
+ FD_ZERO(&rset);
+ FD_SET(fd, &rset);
+ timeout.tv_sec = 1;
+ timeout.tv_usec = 0;
+ if ((n = select(fd + 1, &rset, NULL, NULL, &timeout)) < 0)
+ if (errno != EINTR)
+ err(1, "select()");
+ if (n != 1)
+ fprintf(stderr, "FAIL Expected n == 0, got %d\n", n);
+ if (n == 1 && FD_ISSET(fd, &rset)) {
+ r = read(fd, &ch, 1);
+ if (r == -1)
+ err(1, "read");
+ if (r == 0)
+ fprintf(stderr, "read(2): EOF\n");
+ }
+
+ /* timeout */
+ ch = 'z';
+ FD_ZERO(&rset);
+ FD_SET(fd, &rset);
+ timeout.tv_sec = 1;
+ timeout.tv_usec = 0;
+ if ((n = select(fd + 1, &rset, NULL, NULL, &timeout)) < 0)
+ if (errno != EINTR)
+ err(1, "select()");
+ if (n != 1)
+ fprintf(stderr, "FAIL Expected n == 0, got %d\n", n);
+ if (n == 1 && FD_ISSET(fd, &rset)) {
+ r = read(fd, &ch, 1);
+ if (r == -1)
+ err(1, "read");
+ if (r != 0)
+ fprintf(stderr, "read(3): %c\n", ch);
+ }
+
+ if (close(fd) == -1)
+ err(1, "close() in child");
+ _exit(n == 1 ? 0 : 1);
+}
+
+static void
+writer(void)
+{
+ int fd;
+
+ do {
+ if ((fd = open(path, O_WRONLY)) == -1)
+ if (errno != EINTR)
+ err(1, "open(%s, O_WRONLY)",
+ path);
+ if (fd == -1)
+ warn("open(%s) wr", path);
+ } while (fd == -1);
+ if (write(fd, "a", 1) != 1)
+ err(1, "write one");
+ if (write(fd, "b", 1) != 1)
+ err(1, "write one");
+ if (close(fd) == -1)
+ warn("close() in parent");
+}
+
+static void
+test(void)
+{
+ pid_t pid;
+ int status;
+
+ if ((pid = fork()) == 0)
+ reader();
+ writer();
+
+ if (waitpid(pid, &status, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+
+ _exit(status != 0);
+}
+
+int
+main(int argc __unused, char *argv[])
+{
+ int e, i, pids[PARALLEL], status;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <fifo file>\n", argv[0]);
+ exit(1);
+ }
+ path = argv[1];
+ e = 0;
+
+ unlink(path);
+ if (mkfifo(path, 0640) == -1)
+ err(1, "mkfifo(%s)", path);
+
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ e += status == 0 ? 0 : 1;
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/mkfifo6.sh b/tools/test/stress2/misc/mkfifo6.sh
new file mode 100755
index 000000000000..5647bf9493d6
--- /dev/null
+++ b/tools/test/stress2/misc/mkfifo6.sh
@@ -0,0 +1,183 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# mkfifo(2), select(2) with tmpfs(5) scenario.
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mkfifo6.c
+mycc -o mkfifo6 -Wall -Wextra -O0 -g mkfifo6.c || exit 1
+rm -f mkfifo6.c
+cd $odir
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+mount -o size=1g -t tmpfs tmpfs $mntpoint
+
+fifo=$mntpoint/fifo.file
+cd $mntpoint
+/tmp/mkfifo6 $fifo
+s=$?
+cd $odir
+
+while mount | grep "on $mntpoint " | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
+rm -rf /tmp/mkfifo6
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static char *path;
+
+#define PARALLEL 1
+
+static void
+reader(void)
+{
+ fd_set rset;
+ struct timeval timeout;
+ int fd, n, r;
+ char ch;
+
+ do {
+ if((fd = open(path, O_RDONLY)) == -1)
+ if (errno != EINTR)
+ err(1, "open(%s, O_RDONLY)",
+ path);
+ if (fd == -1)
+ warn("open(%s) ro", path);
+ } while (fd == -1);
+
+ /* timeout */
+ ch = 'z';
+ FD_ZERO(&rset);
+ FD_SET(fd, &rset);
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 100000;
+ if ((n = select(fd + 1, &rset, NULL, NULL, &timeout)) < 0)
+ if (errno != EINTR)
+ err(1, "select()");
+ if (n != 0)
+ fprintf(stderr, "FAIL Expected n == 0, got %d\n", n);
+ if (n == 1 && FD_ISSET(fd, &rset)) {
+ r = read(fd, &ch, 1);
+ if (r == -1)
+ err(1, "read");
+ if (r != 0)
+ fprintf(stderr, "read(2): %c\n", ch);
+ else
+ fprintf(stderr, "read(2): EOF\n");
+ }
+
+ sleep(3);
+ if (close(fd) == -1)
+ err(1, "close() in child");
+ _exit(n);
+}
+
+static void
+writer(void)
+{
+ int fd;
+
+ do {
+ if ((fd = open(path, O_WRONLY)) == -1)
+ if (errno != EINTR)
+ err(1, "open(%s, O_WRONLY)",
+ path);
+ if (fd == -1)
+ warn("open(%s) wr", path);
+ } while (fd == -1);
+ sleep(2);
+ if (write(fd, "a", 1) != 1)
+ err(1, "write one");
+ if (close(fd) == -1)
+ warn("close() in parent");
+}
+
+static void
+test(void)
+{
+ pid_t pid;
+ int status;
+
+ if ((pid = fork()) == 0)
+ reader();
+ writer();
+
+ if (waitpid(pid, &status, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+
+ _exit(status != 0);
+}
+
+int
+main(int argc __unused, char *argv[])
+{
+ int e, i, pids[PARALLEL], status;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <fifo file>\n", argv[0]);
+ exit(1);
+ }
+ path = argv[1];
+ e = 0;
+
+ unlink(path);
+ if (mkfifo(path, 0640) == -1)
+ err(1, "mkfifo(%s)", path);
+
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ e += status == 0 ? 0 : 1;
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/mkfifo7.sh b/tools/test/stress2/misc/mkfifo7.sh
new file mode 100755
index 000000000000..b17e84536bed
--- /dev/null
+++ b/tools/test/stress2/misc/mkfifo7.sh
@@ -0,0 +1,203 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# mkfifo(2), poll(2) with tmpfs(5) scenario.
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mkfifo7.c
+mycc -o mkfifo7 -Wall -Wextra -O0 -g mkfifo7.c || exit 1
+rm -f mkfifo7.c
+cd $odir
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+mount -o size=1g -t tmpfs tmpfs $mntpoint
+
+fifo=$mntpoint/fifo.file
+cd $mntpoint
+/tmp/mkfifo7 $fifo
+s=$?
+cd $odir
+
+while mount | grep "on $mntpoint " | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
+rm -rf /tmp/mkfifo7
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static char *path;
+
+#define PARALLEL 1
+
+static void
+reader(void)
+{
+ struct pollfd pfd;
+ int fd, n, r;
+ char ch;
+
+ do {
+ if((fd = open(path, O_RDONLY)) == -1)
+ if (errno != EINTR)
+ err(1, "open(%s, O_RDONLY)",
+ path);
+ if (fd == -1)
+ warn("open(%s) ro", path);
+ } while (fd == -1);
+
+ /* Read one character */
+ pfd.fd = fd;
+ pfd.events = POLLIN;
+ if ((n = poll(&pfd, 1, 0)) == -1)
+ err(1, "poll()");
+ if (n == 1) {
+ r = read(fd, &ch, 1);
+ if (r == -1)
+ err(1, "read");
+ if (r != 0)
+ fprintf(stderr, "read(1): %c\n", ch);
+ else
+ fprintf(stderr, "read(1): EOF\n");
+ }
+
+ /* timeout */
+ ch = 'z';
+ if ((n = poll(&pfd, 1, 7000)) == -1)
+ err(1, "poll()");
+ if (n != 1)
+ fprintf(stderr, "FAIL Expected n == 1, got %d\n", n);
+ if (n == 1) {
+ r = read(fd, &ch, 1);
+ if (r == -1)
+ err(1, "read");
+ if (r == 0)
+ fprintf(stderr, "read(2): EOF\n");
+ }
+
+ /* timeout */
+ ch = 'z';
+ if ((n = poll(&pfd, 1, 7000)) == -1)
+ err(1, "poll()");
+ if (n != 1)
+ fprintf(stderr, "FAIL Expected n == 1, got %d\n", n);
+ if (n == 1) {
+ r = read(fd, &ch, 1);
+ if (r == -1)
+ err(1, "read");
+ if (r != 0)
+ fprintf(stderr, "read(3): %c\n", ch);
+ }
+
+ if (close(fd) == -1)
+ err(1, "close() in child");
+ _exit(n == 1 ? 0 : 1);
+}
+
+static void
+writer(void)
+{
+ int fd;
+
+ do {
+ if ((fd = open(path, O_WRONLY)) == -1)
+ if (errno != EINTR)
+ err(1, "open(%s, O_WRONLY)",
+ path);
+ if (fd == -1)
+ warn("open(%s) wr", path);
+ } while (fd == -1);
+ if (write(fd, "a", 1) != 1)
+ err(1, "write one");
+ if (close(fd) == -1)
+ warn("close() in parent");
+}
+
+static void
+test(void)
+{
+ pid_t pid;
+ int status;
+
+ if ((pid = fork()) == 0)
+ reader();
+ writer();
+
+ if (waitpid(pid, &status, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+
+ _exit(status != 0);
+}
+
+int
+main(int argc __unused, char *argv[])
+{
+ int e, i, pids[PARALLEL], status;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <fifo file>\n", argv[0]);
+ exit(1);
+ }
+ path = argv[1];
+ e = 0;
+
+ unlink(path);
+ if (mkfifo(path, 0640) == -1)
+ err(1, "mkfifo(%s)", path);
+
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ e += status == 0 ? 0 : 1;
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/mkfifo8.sh b/tools/test/stress2/misc/mkfifo8.sh
new file mode 100755
index 000000000000..6227070f016e
--- /dev/null
+++ b/tools/test/stress2/misc/mkfifo8.sh
@@ -0,0 +1,200 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Regression test for https://reviews.freebsd.org/D20784
+# "Fix mutual exclusion in pipe_direct_write()"
+# https://people.freebsd.org/~pho/stress/log/mkfifo8.txt
+
+# Reported by syzbot+21811cc0a89b2a87a9e7@syzkaller.appspotmail.com
+# Test scenario suggestion by markj@
+# Fixed by r349546
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mkfifo8.c
+mycc -o mkfifo8 -Wall -Wextra -O0 -g mkfifo8.c || exit 1
+rm -f mkfifo8.c
+cd $odir
+
+set -e
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+newfs $newfs_flags /dev/md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+cd $mntpoint
+$dir/mkfifo8; s=$?
+cd $odir
+
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -rf /tmp/mkfifo8
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define ACT 1
+#define NPROCS 64
+#define PARALLEL 4
+#define RUNTIME 60
+#define SIZ 8192
+#define SYNC 0
+
+static volatile u_int *share;
+static int fd;
+static char cp[SIZ];
+
+static void
+tw(void)
+{
+ int r;
+
+ atomic_add_int(&share[ACT], 1);
+ r = write(fd, cp, SIZ);
+ if (r == -1)
+ warn("write");
+
+ _exit(0);
+}
+
+static void
+tr(void)
+{
+ int i, r;
+ char cp[SIZ];
+
+ while (share[ACT] < NPROCS / 2)
+ usleep(10);
+ for (i = 0; i < NPROCS; i++) {
+ r = read(fd, cp, SIZ);
+ if (r == -1)
+ warn("read");
+ }
+
+ _exit(0);
+}
+
+static void
+test(void)
+{
+ pid_t pid[NPROCS + 1];
+ int i;
+ char file[80];
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ snprintf(file, sizeof(file), "fifo.%d", getpid());
+ if (mkfifo(file, DEFFILEMODE) == -1)
+ err(1, "mkfifo(%s)", file);
+ if ((fd = open(file, O_RDWR)) == -1)
+ err(1, "open(%s)", file);
+
+ for (i = 0; i < NPROCS; i++) {
+ if ((pid[i] = fork()) == 0)
+ tw();
+ }
+ if ((pid[NPROCS] = fork()) == 0)
+ tr();
+
+ for (i = 0; i < NPROCS + 1; i++) {
+ if (waitpid(pid[i], NULL, 0) != pid[i])
+ err(1, "waitpid");
+ }
+ close(fd);
+ unlink(file);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ for (i = 0; i < SIZ; i += PAGE_SIZE)
+ cp[i] = 1;
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ share[ACT] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/mknod.sh b/tools/test/stress2/misc/mknod.sh
new file mode 100755
index 000000000000..d2b9aa617577
--- /dev/null
+++ b/tools/test/stress2/misc/mknod.sh
@@ -0,0 +1,163 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# mknod(2) regression test
+# "panic: ffs_write: type 0xca2b02d0 8 (0,3)" seen.
+# Reported by: Dmitry Vyukov <dvyukov@google.com>
+# Fixed by r324853
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mknod.c
+mycc -o mknod -Wall -Wextra -O0 -g mknod.c || exit 1
+rm -f mknod.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+set +e
+
+cd $mntpoint
+$dir/mknod $mntpoint
+s=$?
+[ -f mknod.core -a $s -eq 0 ] &&
+ { ls -l mknod.core; mv mknod.core /tmp; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1
+mdconfig -d -u $mdstart
+rm -rf $dir/mknod
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+static char *mp;
+
+#define PARALLEL 4
+#define RUNTIME (1 * 60)
+#define SYNC 0
+
+static void
+test(void)
+{
+ dev_t dev;
+ mode_t mode;
+ time_t start;
+ int fd, n, r;
+ char path[128];
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+ n = 0;
+ snprintf(path, sizeof(path), "%s/node.%06d.%d", mp, getpid(), n);
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ dev = makedev(arc4random(), arc4random());
+ mode = arc4random() % 0x10000;
+ r = mknod(path, mode, dev);
+ if (r == 0) {
+ if ((fd = open(path, O_RDWR)) != -1) {
+ write(fd, "x", 1);
+ close(fd);
+ }
+ unlink(path);
+ n++;
+ snprintf(path, sizeof(path), "%s/node.%06d.%d", mp,
+ getpid(), n);
+ }
+ }
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ int e, i, status;
+
+ if (argc != 2)
+ return (1);
+ mp = argv[1];
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/mlockall.sh b/tools/test/stress2/misc/mlockall.sh
new file mode 100755
index 000000000000..6171725d8b3d
--- /dev/null
+++ b/tools/test/stress2/misc/mlockall.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# "panic: swap_reserved < decr" seen. Fixed in r195329
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > mlockall.c
+mycc -o mlockall -Wall mlockall.c
+rm -f mlockall.c
+
+for i in `jot 10`; do
+ /tmp/mlockall &
+ sleep 1
+ ps -x | grep /tmp/mlockall | grep -v grep | awk '{print $1}' | \
+ while read pid; do
+ kill -2 $pid
+ kill -9 $pid
+ done
+done
+
+rm -f /tmp/mlockall
+exit
+EOF
+#include <err.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+void
+child(void)
+{
+ if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
+ err(1, "mlockall(MCL_CURRENT | MCL_FUTURE)");
+ fork();
+ sleep(60);
+}
+
+int
+main(int argc, char **argv)
+{
+ int status;
+
+ if (fork() == 0)
+ child();
+ wait(&status);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mlockall2.sh b/tools/test/stress2/misc/mlockall2.sh
new file mode 100755
index 000000000000..dbeaf729342b
--- /dev/null
+++ b/tools/test/stress2/misc/mlockall2.sh
@@ -0,0 +1,165 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# core dumps seen in watchdogd after mlockall() was added.
+# This scenario demonstrates the problem. Fixed in r242012.
+
+# "panic: freeing mapped page 0xfffff8181b38a5e8" seen:
+# https://people.freebsd.org/~pho/stress/log/mark030.txt
+
+mem=`sysctl -n hw.usermem`
+[ `sysctl -n vm.swap_total` -eq 0 ] && mem=$((mem / 100 * 60))
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > mlockall2.c
+mycc -o mlockall2 -Wall -Wextra -O2 -g mlockall2.c || exit 1
+rm -f mlockall2.c
+cd $here
+
+rm -f mlockall2.core
+/tmp/mlockall2 $mem &
+while kill -0 $! 2>/dev/null; do
+ [ -r mlockall2.core ] && kill $! && break
+ sleep 10
+done
+[ -r mlockall2.core ] && s=1 || s=0
+pkill mlockall2
+wait
+rm -f /tmp/mlockall2 mlockall2.core
+exit $s
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/rtprio.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOAD 40
+#define N 90000
+#define PARALLEL 5
+#define RUNTIME 600
+
+static long size;
+
+static void
+swap(void)
+{
+ long i;
+ int page;
+ volatile char *c;
+
+ setproctitle("swap");
+ c = malloc(size);
+ while (c == NULL) {
+ size -= 1024 * 1024;
+ c = malloc(size);
+ }
+ page = getpagesize();
+ for (;;) {
+ i = 0;
+ while (i < size) {
+ c[i] = 0;
+ i += page;
+ }
+ }
+}
+
+static void
+test(void)
+{
+ pid_t p;
+ int i, status;
+
+ setproctitle("test");
+ for (i = 0; i < N; i++) {
+ if ((p = fork()) == 0) {
+ _exit(0);
+ }
+ if (p > 0)
+ waitpid(p, &status, 0);
+ if (status != 0)
+ break;
+ }
+ _exit(0);
+}
+
+int
+main(int argc __unused, char **argv)
+{
+ time_t start;
+ struct rtprio rtp;
+ pid_t pids[LOAD], pids2[PARALLEL];
+ int i;
+
+ size = atol(argv[1]) / LOAD * 1.5;
+ for (i = 0; i < LOAD; i++)
+ if ((pids[i] = fork()) == 0)
+ swap();
+ sleep(10);
+
+ rtp.type = RTP_PRIO_REALTIME;
+ rtp.prio = 0;
+ if (rtprio(RTP_SET, 0, &rtp) == -1)
+ err(1, "rtprio");
+
+ if (madvise(0, 0, MADV_PROTECT) != 0)
+ err(1, "madvise failed");
+ if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0)
+ err(1, "mlockall failed");
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids2[i] = fork()) == 0)
+ test();
+ }
+
+ for (i = 0; i < PARALLEL; i++)
+ if (waitpid(pids2[i], NULL, 0) != pids2[i])
+ err(1, "waitpid(%d) (2)", pids2[i]);
+ if (access("mlockall2.core", R_OK) == 0)
+ break;
+ }
+ for (i = 0; i < LOAD; i++)
+ kill(pids[i], SIGKILL);
+ for (i = 0; i < LOAD; i++)
+ if (waitpid(pids[i], NULL, 0) != pids[i])
+ err(1, "waitpid(%d)", pids[i]);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mlockall3.sh b/tools/test/stress2/misc/mlockall3.sh
new file mode 100755
index 000000000000..0ff1e24eaa7f
--- /dev/null
+++ b/tools/test/stress2/misc/mlockall3.sh
@@ -0,0 +1,167 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# mlockall(2) test scenario.
+# "panic: vm_page_unwire: page xxx's wire count is zero" seen.
+# http://people.freebsd.org/~pho/stress/log/mlockall.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mlockall3.c
+mycc -o mlockall3 -Wall -Wextra mlockall3.c -lpthread || exit 1
+rm -f mlockall3.c
+cd $odir
+
+/tmp/mlockall3
+
+killall mlockall3 > /dev/null 2>&1
+rm -f /tmp/mlockall3
+exit
+
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <signal.h>
+#include <sys/mman.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+u_int32_t r[N];
+
+static void
+hand(int i __unused) { /* handler */
+ _exit(1);
+}
+
+unsigned long
+makearg(void)
+{
+ unsigned int i;
+ unsigned long val;
+
+ val = arc4random();
+ i = arc4random() % 100;
+ if (i < 20)
+ val = val & 0xff;
+ if (i >= 20 && i < 40)
+ val = val & 0xffff;
+ if (i >= 40 && i < 60)
+ val = (unsigned long)(r) | (val & 0xffff);
+#if defined(__LP64__)
+ if (i >= 60) {
+ val = (val << 32) | arc4random();
+ if (i > 80)
+ val = val & 0x00007fffffffffffUL;
+ }
+#endif
+
+ return(val);
+}
+
+void *
+calls(void *arg __unused)
+{
+ int i;
+ unsigned long arg1, arg2, arg3;
+
+ usleep(1000);
+ for (i = 0; i < 500; i++) {
+ arg1 = makearg();
+ arg2 = makearg();
+ arg3 = makearg();
+
+ alarm(1);
+ syscall(SYS_mlockall, arg1, arg2, arg3);
+ }
+
+ return (0);
+}
+
+int
+main(void)
+{
+ struct passwd *pw;
+ pid_t pid;
+ pthread_t cp[50];
+ int e, i, j;
+
+ if ((pw = getpwnam("nobody")) == NULL)
+ err(1, "no such user: nobody");
+
+ if (setgroups(1, &pw->pw_gid) ||
+ setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
+ seteuid(pw->pw_uid) || setuid(pw->pw_uid))
+ err(1, "Can't drop privileges to \"nobody\"");
+ endpwent();
+
+ signal(SIGALRM, hand);
+ signal(SIGILL, hand);
+ signal(SIGFPE, hand);
+ signal(SIGSEGV, hand);
+ signal(SIGBUS, hand);
+ signal(SIGURG, hand);
+ signal(SIGSYS, hand);
+ signal(SIGTRAP, hand);
+
+ alarm(180);
+ for (i = 0; i < 8000; i++) {
+ if ((pid = fork()) == 0) {
+ for (j = 0; j < N; j++)
+ r[j] = arc4random();
+ for (j = 0; j < 50; j++)
+ if ((e = pthread_create(&cp[j], NULL, calls, NULL)) != 0)
+ errc(1, e, "pthread_create");
+
+ for (j = 0; j < 50; j++)
+ pthread_join(cp[j], NULL);
+ _exit(0);
+ }
+ if (pid == -1)
+ err(1, "fork()");
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mlockall4.sh b/tools/test/stress2/misc/mlockall4.sh
new file mode 100755
index 000000000000..778256fa8976
--- /dev/null
+++ b/tools/test/stress2/misc/mlockall4.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# mlockall(2) / nullfs(5) scenario causes:
+# http://people.freebsd.org/~pho/stress/log/kostik619.txt
+# kern/182661, fixed in r256211.
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > mlockall4.c
+mycc -o mlockall4 -Wall -Wextra mlockall4.c || exit 1
+rm -f mlockall4.c
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+mount -t nullfs /tmp $mntpoint
+$mntpoint/mlockall4 &
+sleep 2
+umount -f $mntpoint
+
+wait
+rm -f /tmp/mlockall4
+exit
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
+ err(1, "mlockall(MCL_CURRENT | MCL_FUTURE)");
+ sleep(5);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mlockall5.sh b/tools/test/stress2/misc/mlockall5.sh
new file mode 100755
index 000000000000..847909b478c2
--- /dev/null
+++ b/tools/test/stress2/misc/mlockall5.sh
@@ -0,0 +1,162 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Regression test for r287591:
+
+# From the commit log:
+# Remove a check which caused spurious SIGSEGV on usermode access to the
+# mapped address without valid pte installed, when parallel wiring of
+# the entry happen. The entry must be copy on write. If entry is COW
+# but was already copied, and parallel wiring set
+# MAP_ENTRY_IN_TRANSITION, vm_fault() would sleep waiting for the
+# MAP_ENTRY_IN_TRANSITION flag to clear. After that, the fault handler
+# is restarted and vm_map_lookup() or vm_map_lookup_locked() trip over
+# the check. Note that this is race, if the address is accessed after
+# the wiring is done, the entry does not fault at all.
+
+# Test scenario by kib@.
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mlockall5.c
+mycc -o mlockall5 -Wall -Wextra -O0 -g mlockall5.c -lpthread || exit 1
+rm -f mlockall5.c
+cd $odir
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 512m -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags -n md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint || exit 1
+
+(cd $mntpoint; /tmp/mlockall5 || echo FAIL)
+
+n=0
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ n=$((n + 1))
+ [ $n -gt 10 ] && { echo FAIL; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf /tmp/mlockall5
+exit
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+size_t clen;
+volatile u_int share;
+int ps;
+char *c;
+
+void *
+touch(void *arg __unused)
+{
+
+ int i;
+
+ while (share == 0)
+ ;
+ for (i = 0; i < (int)clen; i += ps)
+ c[i] = 1;
+
+ return (NULL);
+}
+
+void *
+ml(void *arg __unused)
+{
+ while (share == 0)
+ ;
+ if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
+ err(1, "mlock");
+
+ return (NULL);
+}
+
+int
+test(void)
+{
+ pthread_t tid[2];
+ int i, rc, status;
+
+ if (fork() == 0) {
+ alarm(60);
+ rc = pthread_create(&tid[0], NULL, touch, NULL);
+ if (rc != 0)
+ errc(1, rc, "pthread_create()");
+ rc = pthread_create(&tid[1], NULL, ml, NULL);
+ if (rc != 0)
+ errc(1, rc, "pthread_create()");
+ share = 1;
+ for (i = 0; i < (int)nitems(tid); i++) {
+ rc = pthread_join(tid[i], NULL);
+ if (rc != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ }
+ _exit(0);
+ }
+
+ if (wait(&status) == -1)
+ err(1, "wait");
+
+ return (WTERMSIG(status));
+}
+
+int
+main(void)
+{
+ int s;
+
+ ps = getpagesize();
+ clen = 32 * 1024;
+ if ((c = mmap(NULL, clen, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED,
+ -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ s = test();
+
+ return (s);
+}
diff --git a/tools/test/stress2/misc/mlockall6.sh b/tools/test/stress2/misc/mlockall6.sh
new file mode 100755
index 000000000000..a755350f673d
--- /dev/null
+++ b/tools/test/stress2/misc/mlockall6.sh
@@ -0,0 +1,203 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# "panic: Lock (rw) vm object not locked @ vm/vm_page.c:1013" seen:
+# https://people.freebsd.org/~pho/stress/log/mlockall6-2.txt
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mlockall6.c
+mycc -o mlockall6 -Wall -Wextra -O0 -g mlockall6.c || exit 1
+rm -f mlockall6.c
+cd $odir
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 512m -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags -n md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint || exit 1
+
+daemon sh -c "(cd $odir/../testcases/swap; ./swap -t 20m -i 20 -l 100)" \
+ > /dev/null 2>&1
+sleep 2
+
+(cd $mntpoint; /tmp/mlockall6 || echo FAIL)
+
+while pgrep -q swap; do
+ pkill -9 swap
+done
+
+n=0
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ n=$((n + 1))
+ [ $n -gt 10 ] && { echo FAIL; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf /tmp/mlockall6
+exit 0
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOOPS 2
+#define PARALLEL 8
+#define R0 0
+#define R1 1
+#define R2 2
+#define RUNTIME (10 * 60)
+
+static volatile u_int *share;
+static int ps;
+static char c[32 * 1024 * 1024];
+
+static void
+touch(void)
+{
+ int i;
+
+ for (i = 0; i < (int)sizeof(c); i += ps)
+ c[i] = 1;
+}
+
+static void
+test2(void)
+{
+ pid_t pid;
+ volatile u_int *share2;
+ size_t len;
+ int i, status;
+
+ len = ps;
+ if ((share2 = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ touch();
+ usleep(arc4random() % 100000);
+ alarm(600);
+ if ((pid = fork()) == 0) {
+ alarm(600);
+ while (share2[R1] == 0) /* Wait for parent */
+ ;
+ atomic_add_int(&share2[R1], 1);
+ if (arc4random() % 100 < 50)
+ usleep(arc4random() % 1000);
+ if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
+ err(1, "mlock");
+ touch();
+ atomic_add_int(&share2[R2], 1);
+ _exit(0);
+ }
+ atomic_add_int(&share2[R1], 1);
+ while (share2[R1] != 2) /* Wait for child */
+ ;
+
+ for (i = 0; i < 100000 && share2[R2] == 0; i++)
+ touch(); /* while child is running */
+
+ if (waitpid(pid, &status, 0) == -1)
+ err(1, "wait");
+
+ if (status != 0)
+ fprintf(stderr, "Got signal %d\n", WTERMSIG(status));
+ _exit(WTERMSIG(status));
+}
+
+static void
+test(void)
+{
+ pid_t pid;
+ int i, s, status;
+
+ while (share[R0] == 0)
+ ;
+ s = 0;
+ for (i = 0; i < LOOPS; i++) {
+ if ((pid = fork()) == 0)
+ test2();
+ waitpid(pid, &status, 0);
+ s = (s == 0) ? status : s;
+ }
+ _exit(s);
+}
+
+int
+main(void)
+{
+ pid_t pid;
+ size_t len;
+ time_t start;
+ int i, s, status;
+
+ ps = getpagesize();
+ len = ps;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ start = time(NULL);
+ s = 0;
+ while (s == 0 && (time(NULL) - start) < RUNTIME) {
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pid = fork()) == 0)
+ test();
+ }
+ atomic_add_int(&share[R0], 1); /* Start test() runs */
+ for (i = 0; i < PARALLEL; i++) {
+ waitpid(pid, &status, 0);
+ if (status != 0) {
+ fprintf(stderr, "FAIL: status = %d\n",
+ status);
+ }
+ s = (s == 0) ? status : s;
+ }
+ atomic_add_int(&share[R0], -1);
+ }
+
+ return (s);
+}
diff --git a/tools/test/stress2/misc/mlockall7.sh b/tools/test/stress2/misc/mlockall7.sh
new file mode 100755
index 000000000000..987e312f19e7
--- /dev/null
+++ b/tools/test/stress2/misc/mlockall7.sh
@@ -0,0 +1,262 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Variation of mmap18.sh.
+# "panic: vm_page_unwire: page 0xfffff81038d721f0's wire count is zero" seen:
+# https://people.freebsd.org/~pho/stress/log/mlockall7.txt
+# Fixed by r328880
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > mlockall7.c
+mycc -o mlockall7 -Wall -Wextra -O2 mlockall7.c -lpthread || exit 1
+rm -f mlockall7.c
+
+/tmp/mlockall7 `[ $# -eq 0 ] && echo 1 || echo $1` || s=1
+
+sleep 2
+rm -f /tmp/mlockall7 /tmp/mlockall7.core
+exit $s
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <pwd.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define N 4096
+#define PARALLEL 50
+#define RUNTIME 180
+
+static u_int32_t r[N];
+static void *p;
+
+static unsigned long
+makearg(void)
+{
+ unsigned long val;
+ unsigned int i;
+
+ val = arc4random();
+ i = arc4random() % 100;
+ if (i < 20)
+ val = val & 0xff;
+ if (i >= 20 && i < 40)
+ val = val & 0xffff;
+ if (i >= 40 && i < 60)
+ val = (unsigned long)(r) | (val & 0xffff);
+#if defined(__LP64__)
+ if (i >= 60) {
+ val = (val << 32) | arc4random();
+ if (i > 80)
+ val = val & 0x00007fffffffffffUL;
+ }
+#endif
+
+ return (val);
+}
+
+static void *
+tmmap(void *arg __unused)
+{
+ size_t len;
+ time_t start;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ len = 128LL * 1024 * 1024;
+
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON,
+ -1, 0)) != MAP_FAILED) {
+ usleep(100);
+ munmap(p, len);
+ }
+ }
+
+ return (NULL);
+}
+
+static void *
+tmlockall(void *arg __unused)
+{
+ time_t start;
+ int flags;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ flags = makearg() & 0xff;
+ mlockall(flags);
+ usleep(100);
+ munlockall();
+ usleep(1000);
+ }
+
+ return (NULL);
+}
+
+static void
+test(void)
+{
+ pthread_t tid[2];
+ time_t start;
+ int i, rc;
+
+ if ((rc = pthread_create(&tid[0], NULL, tmmap, NULL)) != 0)
+ errc(1, rc, "tmmap()");
+ if ((rc = pthread_create(&tid[1], NULL, tmlockall, NULL)) != 0)
+ errc(1, rc, "tmlock()");
+
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ if (fork() == 0) {
+ usleep(10000);
+ _exit(0);
+ }
+ wait(NULL);
+ }
+
+ for (i = 0; i < 2; i++)
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join");
+ _exit(0);
+}
+
+int
+testing(unsigned long maxl)
+{
+ struct passwd *pw;
+ struct rlimit rl;
+ rlim_t maxlock;
+ time_t start;
+ int i;
+
+ maxlock = maxl;
+ if ((pw = getpwnam("nobody")) == NULL)
+ err(1, "failed to resolve nobody");
+ if (setgroups(1, &pw->pw_gid) ||
+ setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
+ seteuid(pw->pw_uid) || setuid(pw->pw_uid))
+ err(1, "Can't drop privileges to \"nobody\"");
+ endpwent();
+
+ rl.rlim_max = rl.rlim_cur = 0;
+ if (setrlimit(RLIMIT_CORE, &rl) == -1)
+ warn("setrlimit");
+
+ if (getrlimit(RLIMIT_MEMLOCK, &rl) == -1)
+ warn("getrlimit");
+ if (maxlock <= 0)
+ errx(1, "Argument is %jd", maxlock);
+ maxlock = (maxlock / 10 * 8) / PARALLEL * PAGE_SIZE;
+ if (maxlock < rl.rlim_cur) {
+ rl.rlim_max = rl.rlim_cur = maxlock;
+ if (setrlimit(RLIMIT_MEMLOCK, &rl) == -1)
+ warn("setrlimit");
+ }
+
+ for (i = 0; i < N; i++)
+ r[i] = arc4random();
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ test();
+ }
+
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+ }
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ pid_t pid;
+ size_t len;
+ int i, loops, s, status;
+ unsigned long max_wired;
+ unsigned int wire_count, wire_count_old;
+
+ s = 0;
+ if (argc != 2)
+ errx(1, "Usage: %s <loops>", argv[0]);
+ loops = atoi(argv[1]);
+
+ len = sizeof(max_wired);
+ if (sysctlbyname("vm.max_user_wired", &max_wired, &len, NULL, 0) != 0)
+ err(1, "vm.max_user_wired");
+
+ len = sizeof(wire_count);
+ if (sysctlbyname("vm.stats.vm.v_user_wire_count", &wire_count, &len,
+ NULL, 0) != 0)
+ err(1, "vm.stats.vm.v_user_wire_count");
+
+ for (i = 0; i < loops; i++) {
+ wire_count_old = wire_count;
+
+ if ((pid = fork()) == 0)
+ testing(max_wired);
+ if (waitpid(pid, &status, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+ if (status != 0)
+ errx(1, "Exit status %d from pid %d\n", status, pid);
+
+ len = sizeof(wire_count);
+ if (sysctlbyname("vm.stats.vm.v_user_wire_count", &wire_count, &len,
+ NULL, 0) != 0)
+ err(1, "vm.stats.vm.v_user_wire_count");
+ fprintf(stderr, "vm.stats.vm.v_user_wire_count was %d, is %d. %d\n",
+ wire_count_old, wire_count, wire_count - wire_count_old);
+ }
+
+ return (s);
+}
diff --git a/tools/test/stress2/misc/mmap.sh b/tools/test/stress2/misc/mmap.sh
new file mode 100755
index 000000000000..95561365a53d
--- /dev/null
+++ b/tools/test/stress2/misc/mmap.sh
@@ -0,0 +1,63 @@
+#!/bin/sh
+
+# Test scenario by Michiel Boland <michiel@boland.org>
+
+# panic: pmap_remove_all: page 0xc491f000 is fictitious
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > mmap.c
+mycc -o mmap -Wall mmap.c
+rm -f mmap.c
+
+./mmap
+rm -f ./mmap
+exit
+
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+
+static const off_t map_address = 0xa0000;
+static const size_t map_size = 0x1000;
+
+static int testit(int fd)
+{
+ void *p;
+ int rv;
+
+ p = mmap(NULL, 2 * map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
+ map_address);
+ if (p == MAP_FAILED) {
+ perror("mmap");
+ return -1;
+ }
+ rv = *(char *) p;
+ if (munmap(p, map_size) == -1) {
+ perror("munmap");
+ return -1;
+ }
+ return rv;
+}
+
+int main(void)
+{
+ int fd, rv;
+
+ fd = open("/dev/mem", O_RDWR);
+ if (fd == -1) {
+ perror("open");
+ return 1;
+ }
+ rv = testit(fd);
+ close(fd);
+ return rv;
+}
+
diff --git a/tools/test/stress2/misc/mmap10.sh b/tools/test/stress2/misc/mmap10.sh
new file mode 100755
index 000000000000..a29768c36691
--- /dev/null
+++ b/tools/test/stress2/misc/mmap10.sh
@@ -0,0 +1,273 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# "panic: vm_page_dirty: page is invalid!" seen.
+# http://people.freebsd.org/~pho/stress/log/mmap10.txt
+# No problems seen after r271681.
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > mmap10.c
+mycc -o mmap10 -Wall -Wextra -O2 -g mmap10.c -lpthread || exit 1
+rm -f mmap10.c
+
+daemon sh -c "(cd $here/../testcases/swap; ./swap -t 2m -i 20 -k)"
+sleep `jot -r 1 0 9`
+for i in `jot 2`; do
+ /tmp/mmap10 &
+done
+sleep 300
+while pgrep -q mmap10; do
+ pkill -9 mmap10
+ sleep 2
+done
+wait
+killall -q swap
+
+rm -f /tmp/mmap10 /tmp/mmap10.core
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define LOOPS 2
+#define MMSIZE (192 * 1024 * 1024)
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+#define PARALLEL 50
+
+void *p;
+u_int32_t r[N];
+
+unsigned long
+makearg(void)
+{
+ unsigned long val;
+ unsigned int i;
+
+ val = arc4random();
+ i = arc4random() % 100;
+ if (i < 20)
+ val = val & 0xff;
+ if (i >= 20 && i < 40)
+ val = val & 0xffff;
+ if (i >= 40 && i < 60)
+ val = (unsigned long)(r) | (val & 0xffff);
+#if defined(__LP64__)
+ if (i >= 60) {
+ val = (val << 32) | arc4random();
+ if (i > 80)
+ val = val & 0x00007fffffffffffUL;
+ }
+#endif
+
+ return(val);
+}
+
+void *
+makeptr(void)
+{
+ unsigned long val;
+
+ if (p != MAP_FAILED && p != NULL)
+ val = (unsigned long)p + arc4random();
+ else
+ val = makearg();
+ val = trunc_page(val);
+
+ return ((void *)val);
+}
+
+void *
+tmmap(void *arg __unused)
+{
+ size_t len;
+ int i, j, fd;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ len = MMSIZE;
+
+ for (i = 0, j = 0; i < 100; i++) {
+ if ((fd = open("/dev/zero", O_RDWR)) == -1)
+ err(1,"open()");
+
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE,
+ fd, 0)) != MAP_FAILED) {
+ usleep(100);
+ munmap(p, len);
+ j++;
+ }
+
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON,
+ -1, 0)) != MAP_FAILED) {
+ usleep(100);
+ munmap(p, len);
+ j++;
+ }
+ close(fd);
+ }
+ if (j == 0)
+ fprintf(stderr, "FAIL: all mmap(2) calls failed.\n");
+
+ return (NULL);
+}
+
+void *
+tmlock(void *arg __unused)
+{
+ size_t len;
+ int i, n;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ n = 0;
+ for (i = 0; i < 200; i++) {
+ len = trunc_page(makearg());
+ if (mlock(makeptr(), len) == 0)
+ n++;
+ len = trunc_page(makearg());
+ if (arc4random() % 100 < 50)
+ if (munlock(makeptr(), len) == 0)
+ n++;
+ }
+ if (n < 10)
+ fprintf(stderr, "Note: tmlock() only succeeded %d times.\n",
+ n);
+
+ return (NULL);
+}
+
+void *
+tmprotect(void *arg __unused)
+{
+ size_t len;
+ void *addr;
+ int i, n, prot;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ n = 0;
+ for (i = 0; i < 200; i++) {
+ addr = makeptr();
+ len = trunc_page(makearg());
+ prot = makearg();
+ if (mprotect(addr, len, prot) == 0)
+ n++;
+ usleep(1000);
+ }
+ if (n < 10)
+ fprintf(stderr, "Note: tmprotect() only succeeded %d times.\n",
+ n);
+
+ return (NULL);
+}
+
+void *
+tmlockall(void *arg __unused)
+{
+ int flags, i, n;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ n = 0;
+ for (i = 0; i < 200; i++) {
+ flags = makearg();
+ if (mlockall(flags) == 0)
+ n++;
+ usleep(100);
+ munlockall();
+ usleep(1000);
+ }
+ if (n < 10)
+ fprintf(stderr, "Note: tmlockall() only succeeded %d times.\n",
+ n);
+
+ return (NULL);
+}
+
+void
+test(void)
+{
+ pthread_t tid[4];
+ int i, rc;
+
+ if ((rc = pthread_create(&tid[0], NULL, tmmap, NULL)) != 0)
+ errc(1, rc, "tmmap()");
+ if ((rc = pthread_create(&tid[1], NULL, tmlock, NULL)) != 0)
+ errc(1, rc, "tmlock()");
+ if ((rc = pthread_create(&tid[2], NULL, tmprotect, NULL)) != 0)
+ errc(1, rc, "tmprotect()");
+ if ((rc = pthread_create(&tid[3], NULL, tmlockall, NULL)) != 0)
+ errc(1, rc, "tmlockall()");
+
+ for (i = 0; i < 100; i++) {
+ if (fork() == 0) {
+ usleep(10000);
+ _exit(0);
+ }
+ wait(NULL);
+ }
+
+ for (i = 0; i < 4; i++)
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i, j;
+
+ for (i = 0; i < N; i++)
+ r[i] = arc4random();
+
+ for (i = 0; i < LOOPS; i++) {
+ for (j = 0; j < PARALLEL; j++) {
+ if (fork() == 0)
+ test();
+ }
+
+ for (j = 0; j < PARALLEL; j++)
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap11.sh b/tools/test/stress2/misc/mmap11.sh
new file mode 100755
index 000000000000..5d2fba6d98b7
--- /dev/null
+++ b/tools/test/stress2/misc/mmap11.sh
@@ -0,0 +1,301 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Copy of mmap10.sh with msync() added.
+# "panic: vm_map_protect: inaccessible wired map entry" seen:
+# http://people.freebsd.org/~pho/stress/log/mmap11.txt
+# No problems seen after r271681.
+
+# http://people.freebsd.org/~pho/stress/log/kostik730.txt, Fixed in r273784
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > mmap11.c
+mycc -o mmap11 -Wall -Wextra -O2 -g mmap11.c -lpthread || exit 1
+rm -f mmap11.c
+
+daemon sh -c "(cd $here/../testcases/swap; ./swap -t 2m -i 20 -k)"
+sleep `jot -r 1 0 9`
+for i in `jot 2`; do
+ /tmp/mmap11 &
+done
+sleep 300
+while pgrep -q mmap11; do
+ pkill -9 mmap11
+ sleep 2
+done
+wait
+killall -q swap
+
+rm -f /tmp/mmap11 /tmp/mmap11.core
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define LOOPS 2
+#define MMSIZE (192 * 1024 * 1024)
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+#define PARALLEL 50
+
+void *p;
+u_int32_t r[N];
+
+unsigned long
+makearg(void)
+{
+ unsigned long val;
+ unsigned int i;
+
+ val = arc4random();
+ i = arc4random() % 100;
+ if (i < 20)
+ val = val & 0xff;
+ if (i >= 20 && i < 40)
+ val = val & 0xffff;
+ if (i >= 40 && i < 60)
+ val = (unsigned long)(r) | (val & 0xffff);
+#if defined(__LP64__)
+ if (i >= 60) {
+ val = (val << 32) | arc4random();
+ if (i > 80)
+ val = val & 0x00007fffffffffffUL;
+ }
+#endif
+
+ return(val);
+}
+
+void *
+makeptr(void)
+{
+ unsigned long val;
+
+ if (p != MAP_FAILED && p != NULL)
+ val = (unsigned long)p + arc4random();
+ else
+ val = makearg();
+ val = trunc_page(val);
+
+ return ((void *)val);
+}
+
+void *
+tmmap(void *arg __unused)
+{
+ size_t len;
+ int i, j, fd;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ len = MMSIZE;
+
+ for (i = 0, j = 0; i < 100; i++) {
+ if ((fd = open("/dev/zero", O_RDWR)) == -1)
+ err(1,"open()");
+
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE,
+ fd, 0)) != MAP_FAILED) {
+ usleep(100);
+ munmap(p, len);
+ j++;
+ }
+
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON,
+ -1, 0)) != MAP_FAILED) {
+ usleep(100);
+ munmap(p, len);
+ j++;
+ }
+ close(fd);
+ }
+ if (j == 0)
+ fprintf(stderr, "FAIL: all mmap(2) calls failed.\n");
+
+ return (NULL);
+}
+
+void *
+tmlock(void *arg __unused)
+{
+ size_t len;
+ int i, n;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ n = 0;
+ for (i = 0; i < 200; i++) {
+ len = trunc_page(makearg());
+ if (mlock(makeptr(), len) == 0)
+ n++;
+ len = trunc_page(makearg());
+ if (arc4random() % 100 < 50)
+ if (munlock(makeptr(), len) == 0)
+ n++;
+ }
+ if (n < 10)
+ fprintf(stderr, "Note: tmlock() only succeeded %d times.\n",
+ n);
+
+ return (NULL);
+}
+
+void *
+tmprotect(void *arg __unused)
+{
+ size_t len;
+ void *addr;
+ int i, n, prot;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ n = 0;
+ for (i = 0; i < 200; i++) {
+ addr = makeptr();
+ len = trunc_page(makearg());
+ prot = makearg();
+ if (mprotect(addr, len, prot) == 0)
+ n++;
+ usleep(1000);
+ }
+ if (n < 10)
+ fprintf(stderr, "Note: tmprotect() only succeeded %d times.\n",
+ n);
+ return (NULL);
+}
+
+void *
+tmlockall(void *arg __unused)
+{
+ int flags, i, n;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ n = 0;
+ for (i = 0; i < 200; i++) {
+ flags = makearg();
+ if (mlockall(flags) == 0)
+ n++;
+ usleep(100);
+ munlockall();
+ usleep(1000);
+ }
+ if (n < 10)
+ fprintf(stderr, "Note: tmlockall() only succeeded %d times.\n",
+ n);
+
+ return (NULL);
+}
+
+void *
+tmsync(void *arg __unused)
+{
+ size_t len;
+ void *addr;
+ int flags, i, n;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ n = 0;
+ for (i = 0; i < 200; i++) {
+ addr = makeptr();
+ len = trunc_page(makearg());
+ flags = makearg();
+ if (msync(addr, len, flags) == 0)
+ n++;
+ usleep(2000);
+ }
+ if (n < 10)
+ fprintf(stderr, "Note: tmsync() only succeeded %d times.\n",
+ n);
+
+ return (NULL);
+}
+
+void
+test(void)
+{
+ pthread_t tid[5];
+ int i, rc;
+
+ if ((rc = pthread_create(&tid[0], NULL, tmmap, NULL)) != 0)
+ errc(1, rc, "tmmap()");
+ if ((rc = pthread_create(&tid[1], NULL, tmlock, NULL)) != 0)
+ errc(1, rc, "tmlock()");
+ if ((rc = pthread_create(&tid[2], NULL, tmprotect, NULL)) != 0)
+ errc(1, rc, "tmprotect()");
+ if ((rc = pthread_create(&tid[3], NULL, tmlockall, NULL)) != 0)
+ errc(1, rc, "tmlockall()");
+ if ((rc = pthread_create(&tid[4], NULL, tmsync, NULL)) != 0)
+ errc(1, rc, "tmlockall()");
+
+ for (i = 0; i < 100; i++) {
+ if (fork() == 0) {
+ usleep(10000);
+ _exit(0);
+ }
+ wait(NULL);
+ }
+
+ for (i = 0; i < 5; i++)
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i, j;
+
+ for (i = 0; i < N; i++)
+ r[i] = arc4random();
+
+ for (i = 0; i < LOOPS; i++) {
+ for (j = 0; j < PARALLEL; j++) {
+ if (fork() == 0)
+ test();
+ }
+
+ for (j = 0; j < PARALLEL; j++)
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap12.sh b/tools/test/stress2/misc/mmap12.sh
new file mode 100755
index 000000000000..917c23a6df8b
--- /dev/null
+++ b/tools/test/stress2/misc/mmap12.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# "panic: vm_map_protect: inaccessible wired map entry" seen:
+# http://people.freebsd.org/~pho/stress/log/mmap11.txt
+# Fixed in r266780.
+
+# Test scenario by: Mark Johnston markj@
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap12.c
+mycc -o mmap12 -O2 -Wall -Wextra mmap12.c || exit 1
+rm -f mmap12.c
+cd $odir
+
+/tmp/mmap12
+
+rm -f /tmp/mmap12
+exit
+
+EOF
+#include <sys/mman.h>
+
+#include <err.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ void *addr;
+ size_t sz = 1;
+
+/*
+ * This is the minimum amount of C code ot takes to panic the kernel.
+ * This is as submitted and thus not a complete and correct test program.
+ */
+ addr = mmap(NULL, sz, PROT_READ, MAP_ANON, -1, 0);
+ if (addr == NULL)
+ err(1, "mmap");
+
+ if (mlock(addr, sz) != 0)
+ err(1, "mlock");
+ if (mprotect(addr, sz, PROT_NONE) != 0)
+ err(1, "mprotect 1");
+ if (mprotect(addr, sz, PROT_WRITE) != 0)
+ err(1, "mprotect 2");
+ if (munmap(addr, sz) != 0)
+ err(1, "munmap");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap13.sh b/tools/test/stress2/misc/mmap13.sh
new file mode 100755
index 000000000000..993e1bc2185c
--- /dev/null
+++ b/tools/test/stress2/misc/mmap13.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Leak of "vm.stats.vm.v_user_wire_count" seen.
+# This test must run in single user mode for accurate leak reporting.
+# Test scenario by: Mark Johnston markj@
+# Fixed in r269134.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap13.c
+mycc -o mmap13 -O2 -Wall -Wextra mmap13.c || exit 1
+rm -f mmap13.c
+cd $odir
+
+# Both the 5000 and 500 are empirical values.
+# Combined they demonstrate the leak in a consistent way.
+
+v1=`sysctl -n vm.stats.vm.v_user_wire_count`
+for i in `jot 5000`; do
+ /tmp/mmap13
+done 2>&1 | tail -5
+v2=`sysctl -n vm.stats.vm.v_user_wire_count`
+[ $v2 -gt $((v1 + 500)) ] &&
+ echo "vm.stats.vm.v_user_wire_count changed from $v1 to $v2."
+
+rm -f /tmp/mmap13
+exit 0
+
+EOF
+#include <sys/mman.h>
+
+#include <err.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ void *addr;
+ size_t sz;
+
+ sz = getpagesize();
+ addr = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
+ if (addr == NULL)
+ err(1, "mmap");
+ if (mlock(addr, sz) != 0)
+ err(1, "mlock");
+ if (mprotect(addr, sz, PROT_NONE) != 0)
+ err(1, "mprotect");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap14.sh b/tools/test/stress2/misc/mmap14.sh
new file mode 100755
index 000000000000..e5a59890189b
--- /dev/null
+++ b/tools/test/stress2/misc/mmap14.sh
@@ -0,0 +1,252 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Simplified version of mmap10.sh with focus on core dumps
+# Deadlock seen:
+# http://people.freebsd.org/~pho/stress/log/kostik673.txt
+# No issues seen with r272060.
+
+# panic: vm_reserv_populate: reserv 0xfffff807cbd46300 is already promoted
+# http://people.freebsd.org/~pho/stress/log/kostik764.txt
+# Fixed by r280238
+
+# panic: vm_reserv_break: reserv 0xfffff807cbe9af00 is full
+# https://people.freebsd.org/~pho/stress/log/alan006.txt
+
+# panic: vm_page_dirty: page is invalid!
+# https://people.freebsd.org/~pho/stress/log/kostik818.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > mmap14.c
+mycc -o mmap14 -Wall -Wextra -O2 -g mmap14.c -lpthread || exit 1
+rm -f mmap14.c
+
+daemon sh -c "(cd $here/../testcases/swap; ./swap -t 2m -i 20 -k -h)"
+sleep `jot -r 1 1 10`
+wire=$((`sysctl -n vm.max_user_wired` - \
+ `sysctl -n vm.stats.vm.v_user_wire_count`))
+for i in `jot 2`; do
+ /tmp/mmap14 $wire
+done
+while pgrep -q swap; do
+ pkill -9 swap
+done
+
+rm -f /tmp/mmap14 /tmp/mmap14.core
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOOPS 2
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+#define PARALLEL 50
+
+static void *p;
+static u_int32_t r[N];
+
+static unsigned long
+makearg(void)
+{
+ unsigned long val;
+ unsigned int i;
+
+ val = arc4random();
+ i = arc4random() % 100;
+ if (i < 20)
+ val = val & 0xff;
+ if (i >= 20 && i < 40)
+ val = val & 0xffff;
+ if (i >= 40 && i < 60)
+ val = (unsigned long)(r) | (val & 0xffff);
+#if defined(__LP64__)
+ if (i >= 60) {
+ val = (val << 32) | arc4random();
+ if (i > 80)
+ val = val & 0x00007fffffffffffUL;
+ }
+#endif
+
+ return(val);
+}
+
+static void *
+makeptr(void)
+{
+ unsigned long val;
+
+ if (p != MAP_FAILED && p != NULL)
+ val = (unsigned long)p + arc4random();
+ else
+ val = makearg();
+ val = trunc_page(val);
+
+ return ((void *)val);
+}
+
+static void *
+tmmap(void *arg __unused)
+{
+ size_t len;
+ int i, fd;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ len = 1LL * 1024 * 1024 * 1024;
+
+ for (i = 0; i < 100; i++) {
+ if ((fd = open("/dev/zero", O_RDWR)) == -1)
+ err(1,"open()");
+
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,
+ 0)) != MAP_FAILED) {
+ usleep(100);
+ munmap(p, len);
+ }
+
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0)) !=
+ MAP_FAILED) {
+ usleep(100);
+ munmap(p, len);
+ }
+ close(fd);
+ }
+
+ return (NULL);
+}
+
+static void *
+tmlock(void *arg __unused)
+{
+ size_t len;
+ int i, n;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ n = 0;
+ for (i = 0; i < 200; i++) {
+ len = trunc_page(makearg());
+ if (mlock(makeptr(), len) == 0)
+ n++;
+ len = trunc_page(makearg());
+ if (arc4random() % 100 < 50)
+ if (munlock(makeptr(), len) == 0)
+ n++;
+ }
+ if (n < 10)
+ fprintf(stderr, "Note: tmlock() only succeeded %d times.\n",
+ n);
+
+ return (NULL);
+}
+
+static void
+test(void)
+{
+ pthread_t tid[4];
+ int i, rc;
+
+ if ((rc = pthread_create(&tid[0], NULL, tmmap, NULL)) != 0)
+ errc(1, rc, "tmmap()");
+ if ((rc = pthread_create(&tid[1], NULL, tmlock, NULL)) != 0)
+ errc(1, rc, "tmlock()");
+
+ for (i = 0; i < 100; i++) {
+ if (fork() == 0) {
+ usleep(10000);
+ _exit(0);
+ }
+ wait(NULL);
+ }
+
+ raise(SIGSEGV);
+
+ for (i = 0; i < 2; i++)
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct rlimit rl;
+ rlim_t maxlock;
+ int i, j;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage:%s <max pages to lock.>\n", argv[0]);
+ exit(1);
+ }
+ if (getrlimit(RLIMIT_MEMLOCK, &rl) == -1)
+ warn("getrlimit");
+ maxlock = atol(argv[1]);
+ if (maxlock == 0)
+ errx(1, "Argument is zero");
+ maxlock = (maxlock / 10 * 8) / PARALLEL * PAGE_SIZE;
+ if (maxlock < rl.rlim_cur) {
+ rl.rlim_max = rl.rlim_cur = maxlock;
+ if (setrlimit(RLIMIT_MEMLOCK, &rl) == -1)
+ warn("setrlimit");
+ }
+
+ for (i = 0; i < N; i++)
+ r[i] = arc4random();
+
+ for (i = 0; i < LOOPS; i++) {
+ for (j = 0; j < PARALLEL; j++) {
+ if (fork() == 0)
+ test();
+ }
+
+ for (j = 0; j < PARALLEL; j++)
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap15.sh b/tools/test/stress2/misc/mmap15.sh
new file mode 100755
index 000000000000..a34e8436255b
--- /dev/null
+++ b/tools/test/stress2/misc/mmap15.sh
@@ -0,0 +1,223 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Missing wakeup in the bufobj_wwait().
+
+# Snapshot of WiP work.
+# http://people.freebsd.org/~pho/stress/log/mmap15.txt
+# Not fixed
+
+# panic: invalid size
+# http://people.freebsd.org/~pho/stress/log/kostik738.txt
+# Fixed in r274878
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > mmap15.c
+mycc -o mmap15 -Wall -Wextra -O2 -g mmap15.c -lpthread || exit 1
+rm -f mmap15.c
+
+for i in `jot 2`; do
+ su $testuser -c /tmp/mmap15 &
+done
+sleep 300
+while pgrep -q mmap15; do
+ pkill -9 mmap15
+ sleep 2
+done
+wait
+
+rm -f /tmp/mmap15 /tmp/mmap15.core
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOOPS 2
+#define MMSIZE (256 * 1024)
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+#define PARALLEL 50
+
+void *p;
+u_int32_t r[N];
+
+unsigned long
+makearg(void)
+{
+ unsigned long val;
+ unsigned int i;
+
+ val = arc4random();
+ i = arc4random() % 100;
+ if (i < 20)
+ val = val & 0xff;
+ if (i >= 20 && i < 40)
+ val = val & 0xffff;
+ if (i >= 40 && i < 60)
+ val = (unsigned long)(r) | (val & 0xffff);
+#if defined(__LP64__)
+ if (i >= 60) {
+ val = (val << 32) | arc4random();
+ if (i > 80)
+ val = val & 0x00007fffffffffffUL;
+ }
+#endif
+
+ return(val);
+}
+
+void *
+makeptr(void)
+{
+ unsigned long val;
+
+ if (p != MAP_FAILED && p != NULL)
+ val = (unsigned long)p + arc4random();
+ else
+ val = makearg();
+ val = trunc_page(val);
+
+ return ((void *)val);
+}
+
+void *
+tmmap(void *arg __unused)
+{
+ size_t len;
+ int i, j, fd;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ len = MMSIZE;
+
+ if ((fd = open("/dev/zero", O_RDWR)) == -1)
+ err(1,"open()");
+ for (i = 0, j = 0; i < 100; i++) {
+ p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
+ if (p != MAP_FAILED)
+ j++;
+ p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
+ if (p != MAP_FAILED)
+ j++;
+ }
+ if (j == 0)
+ fprintf(stderr, "FAIL: all mmap(2) calls failed.\n");
+ close(fd);
+
+ return (NULL);
+}
+
+void *
+tmlock(void *arg __unused)
+{
+ size_t len;
+ int i, n;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ n = 0;
+ for (i = 0; i < 200; i++) {
+ len = trunc_page(makearg());
+ if (mlock(makeptr(), len) == 0)
+ n++;
+ len = trunc_page(makearg());
+ if (arc4random() % 100 < 50)
+ if (munlock(makeptr(), len) == 0)
+ n++;
+ }
+ if (n < 10)
+ fprintf(stderr, "Note: tmlock() only succeeded %d times.\n",
+ n);
+
+ return (NULL);
+}
+
+void
+test(void)
+{
+ pthread_t tid[2];
+ int i, rc;
+
+ if ((rc = pthread_create(&tid[0], NULL, tmmap, NULL)) != 0)
+ errc(1, rc, "tmmap()");
+ if ((rc = pthread_create(&tid[1], NULL, tmlock, NULL)) != 0)
+ errc(1, rc, "tmlock()");
+
+ for (i = 0; i < 100; i++) {
+ if (fork() == 0) {
+ usleep(10000);
+ _exit(0);
+ }
+ wait(NULL);
+ }
+
+ raise(SIGSEGV);
+
+ for (i = 0; i < 2; i++)
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i, j;
+
+ for (i = 0; i < N; i++)
+ r[i] = arc4random();
+
+ for (i = 0; i < LOOPS; i++) {
+ for (j = 0; j < PARALLEL; j++) {
+ if (fork() == 0)
+ test();
+ }
+
+ for (j = 0; j < PARALLEL; j++)
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap16.sh b/tools/test/stress2/misc/mmap16.sh
new file mode 100755
index 000000000000..15a310505edd
--- /dev/null
+++ b/tools/test/stress2/misc/mmap16.sh
@@ -0,0 +1,145 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Test scenario by kib@
+
+[ `uname -m` = "i386" ] || exit 0
+
+. ../default.cfg
+
+grep -q MAP_GUARD /usr/include/sys/mman.h 2>/dev/null || exit 0
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > mmap16.c
+mycc -o mmap16 -Wall -Wextra -O2 -g mmap16.c -lpthread || exit 1
+rm -f mmap16.c /tmp/mmap16.core
+
+echo "Expect:
+ mmap16: mprotect: Permission denied"
+/tmp/mmap16 > /dev/null
+s=$?
+
+rm -f /tmp/mmap16 /tmp/mmap16.core
+exit $s
+EOF
+/* $Id: map_hole.c,v 1.6 2014/06/16 05:52:03 kostik Exp kostik $ */
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/resource.h>
+
+#include <err.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ucontext.h>
+#include <unistd.h>
+
+static void
+sighandler(int signo, siginfo_t *info, void *uap1)
+{
+ static char scratch;
+ ucontext_t *uap;
+
+ uap = uap1;
+ printf("SIG%s(%d) at %p (%%eax %p)\n",
+ signo < sys_nsig ? sys_signame[signo] : "SOME", signo,
+ info->si_addr, (void *)(uintptr_t)uap->uc_mcontext.mc_eax);
+ uap->uc_mcontext.mc_eax = (uintptr_t)&scratch;
+}
+
+static void
+access_addr(char *addr)
+{
+ char r;
+
+ r = '1';
+ printf("accessing %p\n", addr);
+ __asm __volatile("movb %0,(%%eax)" : : "i"(r), "a"(addr) : "memory");
+ printf("done\n");
+}
+
+static int pagesz;
+
+static void
+test_access(char *addr)
+{
+ struct rusage ru;
+ long majflt, minflt;
+
+ if (getrusage(RUSAGE_THREAD, &ru) == -1)
+ err(1, "getrusage");
+ majflt = ru.ru_majflt;
+ minflt = ru.ru_minflt;
+ access_addr(addr);
+ if (mprotect(addr, pagesz, PROT_READ | PROT_WRITE) == -1)
+ warn("mprotect");
+ access_addr(addr);
+ if (getrusage(RUSAGE_THREAD, &ru) == -1)
+ err(1, "getrusage");
+ majflt = ru.ru_majflt - majflt;
+ minflt = ru.ru_minflt - minflt;
+ printf("majflt %ld minflt %ld\n", majflt, minflt);
+}
+
+int
+main(void)
+{
+ struct sigaction sa;
+ char *addr;
+ char cmd[128];
+
+ bzero(&sa, sizeof(sa));
+ sa.sa_sigaction = sighandler;
+ sa.sa_flags = SA_SIGINFO;
+ if (sigaction(SIGSEGV, &sa, NULL) == -1)
+ err(1, "sigaction");
+ pagesz = getpagesize();
+
+ printf("MAP_GUARD\n");
+ addr = mmap(NULL, pagesz, PROT_NONE, MAP_GUARD, -1, 0);
+ if (addr == (char *)MAP_FAILED)
+ err(1, "FAIL: mmap(MAP_GUARD)");
+ test_access(addr);
+
+ printf("PROT_NONE wire\n");
+ addr = mmap(NULL, pagesz, PROT_NONE, MAP_ANON, -1, 0);
+ if (addr == (char *)MAP_FAILED)
+ err(1, "mmap(PROT_NONE)");
+ if (mlock(addr, pagesz) == -1)
+ if (errno != ENOMEM)
+ err(1, "mlock");
+ test_access(addr);
+
+ snprintf(cmd, sizeof(cmd), "procstat -v %d", getpid());
+ system(cmd);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap17.sh b/tools/test/stress2/misc/mmap17.sh
new file mode 100755
index 000000000000..bc67bb2e5a9e
--- /dev/null
+++ b/tools/test/stress2/misc/mmap17.sh
@@ -0,0 +1,86 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Test scenario by kib@
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > mmap17.c
+mycc -o mmap17 -Wall -Wextra -O2 -g mmap17.c -lpthread || exit 1
+rm -f mmap17.c /tmp/mmap17.core
+rm -f /tmp/mmap17.core
+
+{ /tmp/mmap17 > /dev/null; } 2>&1 | grep -v worked
+
+rm -f /tmp/mmap17 /tmp/mmap17.core
+exit 0
+EOF
+/* $Id: map_excl.c,v 1.2 2014/06/16 06:02:52 kostik Exp kostik $ */
+
+#include <sys/mman.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#ifndef MAP_EXCL
+#define MAP_EXCL 0x00004000 /* for MAP_FIXED, fail if address is used */
+#endif
+
+int
+main(void)
+{
+ char *addr, *addr1;
+ int pagesz;
+ char cmd[128];
+
+ pagesz = getpagesize();
+ addr = mmap(NULL, pagesz, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
+ if (addr == (char *)MAP_FAILED)
+ err(1, "mmap 1");
+ printf("addr %p\n", addr);
+
+ addr -= pagesz;
+ addr1 = mmap(addr, 3 * pagesz, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_FIXED | MAP_EXCL, -1, 0);
+ if (addr1 == MAP_FAILED)
+ warn("EXCL worked");
+ else
+ fprintf(stderr, "EXCL failed\n");
+
+ addr1 = mmap(addr, 3 * pagesz, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_FIXED, -1, 0);
+ printf("addr1 %p\n", addr);
+
+ snprintf(cmd, sizeof(cmd), "procstat -v %d", getpid());
+ system(cmd);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap18.sh b/tools/test/stress2/misc/mmap18.sh
new file mode 100755
index 000000000000..065b5bb7df6c
--- /dev/null
+++ b/tools/test/stress2/misc/mmap18.sh
@@ -0,0 +1,308 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Copy of mmap10.sh with core dump disabled.
+# http://people.freebsd.org/~pho/stress/log/kostik711.txt
+
+# panic: vm_fault_copy_entry: main object missing page
+# http://people.freebsd.org/~pho/stress/log/mmap18.txt
+# Fixed by: r316689
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > mmap18.c
+mycc -o mmap18 -Wall -Wextra -O2 mmap18.c -lpthread || exit 1
+rm -f mmap18.c
+
+s=0
+wire=$((`sysctl -n vm.max_user_wired` - \
+ `sysctl -n vm.stats.vm.v_user_wire_count`))
+/tmp/mmap18 $wire &
+start=`date +%s`
+while true; do
+ e=$((`date +%s` - start))
+ pgrep -q mmap18 || break
+ if [ $e -gt 900 ]; then
+ pgrep mmap18 | xargs ps -lHp
+ pkill mmap18
+ break;
+ fi
+ sleep 10
+done
+wait $!; s=$?
+
+rm -f /tmp/mmap18 /tmp/mmap18.core
+exit $s
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOOPS 50
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+#define PARALLEL 50
+
+static u_int32_t r[N];
+static void *p;
+
+static unsigned long
+makearg(void)
+{
+ unsigned long val;
+ unsigned int i;
+
+ val = arc4random();
+ i = arc4random() % 100;
+ if (i < 20)
+ val = val & 0xff;
+ if (i >= 20 && i < 40)
+ val = val & 0xffff;
+ if (i >= 40 && i < 60)
+ val = (unsigned long)(r) | (val & 0xffff);
+#if defined(__LP64__)
+ if (i >= 60) {
+ val = (val << 32) | arc4random();
+ if (i > 80)
+ val = val & 0x00007fffffffffffUL;
+ }
+#endif
+
+ return (val);
+}
+
+static void *
+makeptr(void)
+{
+ unsigned long val;
+
+ if (p != MAP_FAILED && p != NULL)
+ val = (unsigned long)p + arc4random();
+ else
+ val = makearg();
+ val = trunc_page(val);
+
+ return ((void *)val);
+}
+
+static void *
+tmmap(void *arg __unused)
+{
+ size_t len;
+ int i, fd;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ len = 1LL * 1024 * 1024 * 1024;
+
+ for (i = 0; i < 100; i++) {
+ if ((fd = open("/dev/zero", O_RDWR)) == -1)
+ err(1,"open()");
+
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE,
+ fd, 0)) != MAP_FAILED) {
+ usleep(100);
+ munmap(p, len);
+ }
+
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON,
+ -1, 0)) != MAP_FAILED) {
+ usleep(100);
+ munmap(p, len);
+ }
+ close(fd);
+ }
+
+ return (NULL);
+}
+
+static void *
+tmlock(void *arg __unused)
+{
+ int i, n;
+ size_t len;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ n = 0;
+ for (i = 0; i < 200; i++) {
+ len = trunc_page(makearg());
+ if (mlock(makeptr(), len) == 0)
+ n++;
+ len = trunc_page(makearg());
+ if (arc4random() % 100 < 50)
+ if (munlock(makeptr(), len) == 0)
+ n++;
+ }
+#if defined(DEBUG)
+ if (n < 10)
+ fprintf(stderr, "Note: tmlock() only succeeded %d "
+ "times.\n", n);
+#endif
+
+ return (NULL);
+}
+
+static void *
+tmprotect(void *arg __unused)
+{
+ void *addr;
+ size_t len;
+ int i, n, prot;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ n = 0;
+ for (i = 0; i < 200; i++) {
+ addr = makeptr();
+ len = trunc_page(makearg());
+ prot = makearg();
+ if (mprotect(addr, len, prot) == 0)
+ n++;
+ usleep(1000);
+ }
+#if defined(DEBUG)
+ if (n < 10)
+ fprintf(stderr, "Note: tmprotect() only succeeded %d "
+ "times.\n", n);
+#endif
+
+ return (NULL);
+}
+
+static void *
+tmlockall(void *arg __unused)
+{
+ int flags, i, n;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ n = 0;
+ for (i = 0; i < 200; i++) {
+ flags = makearg();
+ if (mlockall(flags) == 0)
+ n++;
+ usleep(100);
+ munlockall();
+ usleep(1000);
+ }
+#if defined(DEBUG)
+ if (n < 10)
+ fprintf(stderr, "Note: tmlockall() only succeeded %d "
+ "times.\n", n);
+#endif
+
+ return (NULL);
+}
+
+static void
+test(void)
+{
+ pthread_t tid[4];
+ int i, rc;
+
+ if ((rc = pthread_create(&tid[0], NULL, tmmap, NULL)) != 0)
+ errc(1, rc, "tmmap()");
+ if ((rc = pthread_create(&tid[1], NULL, tmlock, NULL)) != 0)
+ errc(1, rc, "tmlock()");
+ if ((rc = pthread_create(&tid[2], NULL, tmprotect, NULL)) != 0)
+ errc(1, rc, "tmprotect()");
+ if ((rc = pthread_create(&tid[3], NULL, tmlockall, NULL)) != 0)
+ errc(1, rc, "tmlockall()");
+
+ for (i = 0; i < 100; i++) {
+ if (fork() == 0) {
+ usleep(10000);
+ _exit(0);
+ }
+ wait(NULL);
+ }
+
+ for (i = 0; i < 4; i++)
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct rlimit rl;
+ rlim_t maxlock;
+ int i, j;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage:%s <max pages to lock.>\n", argv[0]);
+ exit(1);
+ }
+ rl.rlim_max = rl.rlim_cur = 0;
+ if (setrlimit(RLIMIT_CORE, &rl) == -1)
+ warn("setrlimit");
+
+ if (getrlimit(RLIMIT_MEMLOCK, &rl) == -1)
+ warn("getrlimit");
+ maxlock = atol(argv[1]);
+ if (maxlock <= 0)
+ errx(1, "Bad argument %jd", maxlock);
+ maxlock = (maxlock / 10 * 8) / PARALLEL * PAGE_SIZE;
+ if (maxlock < rl.rlim_cur) {
+ rl.rlim_max = rl.rlim_cur = maxlock;
+ if (setrlimit(RLIMIT_MEMLOCK, &rl) == -1)
+ warn("setrlimit");
+ }
+
+ for (i = 0; i < N; i++)
+ r[i] = arc4random();
+
+ for (i = 0; i < LOOPS; i++) {
+ for (j = 0; j < PARALLEL; j++) {
+ if (fork() == 0)
+ test();
+ }
+
+ for (j = 0; j < PARALLEL; j++)
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap19.sh b/tools/test/stress2/misc/mmap19.sh
new file mode 100755
index 000000000000..6a646563499a
--- /dev/null
+++ b/tools/test/stress2/misc/mmap19.sh
@@ -0,0 +1,241 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# panic: pmap active 0xfffff801d22cdae8" seen.
+# Variation of mmap18.sh.
+# http://people.freebsd.org/~pho/stress/log/kostik712.txt
+# Fixed by r271000.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > mmap19.c
+mycc -o mmap19 -Wall -Wextra -O2 mmap19.c -lpthread || exit 1
+rm -f mmap19.c
+
+daemon sh -c "(cd $here/../testcases/swap; ./swap -t 2m -i 20 -k)"
+rnd=`od -An -N1 -t u1 /dev/random | sed 's/ //g'`
+sleep $((rnd % 10))
+s=0
+for i in `jot 2`; do
+ /tmp/mmap19 || s=$?
+done
+while pgrep -q swap; do
+ pkill -9 swap
+done
+
+rm -f /tmp/mmap19 /tmp/mmap19.core
+exit $s
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOOPS 50
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+#define PARALLEL 50
+
+u_int32_t r[N];
+void *p;
+
+unsigned long
+makearg(void)
+{
+ unsigned long val;
+ unsigned int i;
+
+ val = arc4random();
+ i = arc4random() % 100;
+ if (i < 20)
+ val = val & 0xff;
+ if (i >= 20 && i < 40)
+ val = val & 0xffff;
+ if (i >= 40 && i < 60)
+ val = (unsigned long)(r) | (val & 0xffff);
+#if defined(__LP64__)
+ if (i >= 60) {
+ val = (val << 32) | arc4random();
+ if (i > 80)
+ val = val & 0x00007fffffffffffUL;
+ }
+#endif
+
+ return(val);
+}
+
+void *
+makeptr(void)
+{
+ unsigned long val;
+
+ if (p != MAP_FAILED && p != NULL)
+ val = (unsigned long)p + arc4random();
+ else
+ val = makearg();
+ val = trunc_page(val);
+
+ return ((void *)val);
+}
+
+void *
+tmmap(void *arg __unused)
+{
+ size_t len;
+ int i, fd;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ len = 1LL * 1024 * 1024 * 1024;
+
+ for (i = 0; i < 100; i++) {
+ if ((fd = open("/dev/zero", O_RDWR)) == -1)
+ err(1,"open()");
+
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE,
+ fd, 0)) != MAP_FAILED) {
+ usleep(100);
+ munmap(p, len);
+ }
+
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1,
+ 0)) != MAP_FAILED) {
+ usleep(100);
+ munmap(p, len);
+ }
+ close(fd);
+ }
+
+ return (NULL);
+}
+
+void *
+tmprotect(void *arg __unused)
+{
+ void *addr;
+ size_t len;
+ int i, n, prot;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ n = 0;
+ for (i = 0; i < 200; i++) {
+ addr = makeptr();
+ len = trunc_page(makearg());
+ prot = makearg();
+ if (mprotect(addr, len, prot) == 0)
+ n++;
+ usleep(1000);
+ }
+ if (n < 10)
+ fprintf(stderr, "Note: tmprotect() only succeeded %d times.\n",
+ n);
+
+ return (NULL);
+}
+
+void
+test(void)
+{
+ pthread_t tid[2];
+ int i, rc;
+
+ if ((rc = pthread_create(&tid[0], NULL, tmmap, NULL)) != 0)
+ errc(1, rc, "tmmap()");
+ if ((rc = pthread_create(&tid[1], NULL, tmprotect, NULL)) != 0)
+ errc(1, rc, "tmprotect()");
+
+ for (i = 0; i < 100; i++) {
+ if (fork() == 0) {
+ usleep(10000);
+ _exit(0);
+ }
+ wait(NULL);
+ }
+
+ alarm(120);
+ for (i = 0; i < 2; i++)
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ time_t start;
+ struct rlimit rl;
+ int e, i, j, status;
+
+ rl.rlim_max = rl.rlim_cur = 0;
+ if (setrlimit(RLIMIT_CORE, &rl) == -1)
+ warn("setrlimit");
+
+ for (i = 0; i < N; i++)
+ r[i] = arc4random();
+
+ e = 0;
+ start = time(NULL);
+ for (i = 0; i < LOOPS && e == 0; i++) {
+ for (j = 0; j < PARALLEL; j++) {
+ if ((pids[j] = fork()) == 0)
+ test();
+ }
+
+ for (j = 0; j < PARALLEL; j++) {
+ if (waitpid(pids[j], &status, 0) != pids[j])
+ err(1, "waitpid(%d)", pids[j]);
+ if (status != 0 && e == 0 && status != SIGSEGV && status != EFAULT)
+ e = status;
+ }
+ if (time(NULL) - start > 1200) {
+ fprintf(stderr, "Timed out.");
+ break;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/mmap2.sh b/tools/test/stress2/misc/mmap2.sh
new file mode 100755
index 000000000000..1595f7afacce
--- /dev/null
+++ b/tools/test/stress2/misc/mmap2.sh
@@ -0,0 +1,177 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Stress mmap by having at most 100 threads mapping random areas within
+# a 100 Mb range.
+
+# Test scenario by kib@
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > mmap2.c
+mycc -o mmap2 -Wall -g mmap2.c -lpthread
+rm -f mmap2.c
+
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 600 ]; do
+ ./mmap2
+done
+rm -f ./mmap2*
+exit
+
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <sys/mman.h>
+#include <sys/param.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#define THREADS 100
+#define STARTADDR 0x50000000U
+#define ADRSPACE 0x06400000U /* 100 Mb */
+
+static void
+work(int nr)
+{
+ int fd, m;
+ void *p;
+ size_t left, len;
+ char path[128];
+
+ p = (void *)STARTADDR + trunc_page(arc4random() % ADRSPACE);
+ left = ADRSPACE - (size_t)p + STARTADDR;
+ len = trunc_page(arc4random() % left) + PAGE_SIZE;
+ fd = -1;
+
+ if (arc4random() % 100 < 90)
+ sprintf(path, "/tmp/mmap.%06d.%04d", getpid(), nr);
+ else
+ sprintf(path, "/dev/zero");
+ if (arc4random() % 2 == 0) {
+ if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0622)) == -1)
+ err(1,"open()");
+ if (ftruncate(fd, len) == -1)
+ err(1, "ftruncate");
+ if (arc4random() % 2 == 0) {
+ if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
+ MAP_FAILED) {
+ if (errno == ENOMEM)
+ return;
+ err(1, "mmap()");
+ }
+ } else {
+ if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) ==
+ MAP_FAILED) {
+ if (errno == ENOMEM)
+ return;
+ err(1, "mmap()");
+ }
+ }
+ if (fd > 0 && strcmp(path, "/dev/zero"))
+ if (unlink(path) == -1)
+ err(1, "unlink(%s)", path);
+ } else {
+ if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0)) == MAP_FAILED) {
+ if (errno == ENOMEM)
+ return;
+ err(1, "mmap()");
+ }
+ strcpy(path, "anon");
+ }
+#if 0
+ printf("nr = %d, %-14s, start = %p, end = %p, len = 0x%08x, (%5d pages)\n",
+ nr, path, p, p + len, len, len>>PAGE_SHIFT);
+#endif
+
+ *(int *)p = 1;
+
+ if (arc4random() % 2 == 0) {
+ m = arc4random() % 10;
+ if (madvise(p, len, m) == -1)
+ warn("madvise(%p, %zd, %d)", p, len, m);
+ }
+ if (arc4random() %2 == 0)
+ if (mprotect(p, trunc_page(arc4random() % len), PROT_READ) == -1 )
+ err(1, "mprotect failed with error:");
+ if (arc4random() % 2 == 0) {
+ if (arc4random() %2 == 0) {
+ if (msync(p, 0, MS_SYNC) == -1)
+ err(1, "msync(%p)", p);
+ } else {
+ if (msync(p, 0, MS_INVALIDATE) == -1)
+ err(1, "msync(%p)", p);
+ }
+ }
+ if (munmap(p, len) == -1)
+ err(1, "munmap(%p)", p);
+ close(fd);
+}
+
+void *
+thr(void *arg)
+{
+ int i;
+
+ for (i = 0; i < 512; i++) {
+ work(*(int *)arg);
+ }
+ return (0);
+}
+
+int
+main(int argc, char **argv)
+{
+ pthread_t threads[THREADS];
+ int nr[THREADS];
+ int i, n, r;
+
+// printf("Address start 0x%x, address end 0x%x, pages %d\n",
+// STARTADDR, STARTADDR + ADRSPACE, ADRSPACE>>PAGE_SHIFT);
+ n = arc4random() % THREADS + 1;
+ for (i = 0; i < n; i++) {
+ nr[i] = i;
+ if ((r = pthread_create(&threads[i], NULL, thr, (void *)&nr[i])) != 0)
+ errc(1, r, "pthread_create()");
+ }
+
+ for (i = 0; i < n; i++) {
+ if ((r = pthread_join(threads[i], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", i);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap20.sh b/tools/test/stress2/misc/mmap20.sh
new file mode 100755
index 000000000000..da1a55e4b2b0
--- /dev/null
+++ b/tools/test/stress2/misc/mmap20.sh
@@ -0,0 +1,83 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# "panic: pmap_unwire: pte 0x672d405 is missing PG_W" seen.
+# http://people.freebsd.org/~pho/stress/log/mmap20.txt
+
+# Test scenario by: Mark Johnston markj@
+
+# Fixed by r272036
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap20.c
+mycc -o mmap20 -O2 -Wall -Wextra mmap20.c || exit 1
+rm -f mmap20.c
+cd $odir
+
+/tmp/mmap20
+s=$?
+
+rm -f /tmp/mmap20
+exit $s
+
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+
+#include <err.h>
+#include <stdlib.h>
+
+int
+main(void)
+{
+ char *ptr;
+ size_t sz;
+
+ sz = 4096;
+ ptr = mmap(NULL, sz, PROT_READ, MAP_ANON, -1, 0);
+ if (ptr == NULL)
+ err(1, "mmap");
+
+ if (mlock(ptr, sz) != 0)
+ err(1, "mlock");
+
+ if (mprotect(ptr, sz, PROT_EXEC) != 0)
+ err(1, "mprotect");
+
+ if (madvise(ptr, sz, MADV_WILLNEED) != 0)
+ err(1, "madvise");
+
+ if (munlock(ptr, sz) != 0)
+ err(1, "munlock");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap21.sh b/tools/test/stress2/misc/mmap21.sh
new file mode 100755
index 000000000000..4b5079160fce
--- /dev/null
+++ b/tools/test/stress2/misc/mmap21.sh
@@ -0,0 +1,165 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# panic: vm_reserv_populate: reserv 0xfffff807cbd3c400 is already promoted
+# http://people.freebsd.org/~pho/stress/log/mmap21.txt
+# Fixed by r280238
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > mmap21.c
+mycc -o mmap21 -Wall -Wextra -O2 -g mmap21.c -lpthread || exit 1
+rm -f mmap21.c
+
+su $testuser -c /tmp/mmap21
+
+rm -f /tmp/mmap21 /tmp/mmap21.core
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define LOOPS 1
+#define NMAPS 50
+#define PARALLEL 2
+
+void *p;
+
+static void *
+tmmap(void *arg __unused)
+{
+ size_t len;
+ int i;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ len = 1LL * 128 * 1024 * 1024;
+
+ for (i = 0; i < NMAPS; i++)
+ p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
+
+ return (NULL);
+}
+
+static void *
+tmlock(void *arg __unused)
+{
+ size_t len;
+ int i, n;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ n = 0;
+ for (i = 0; i < 200; i++) {
+ len = trunc_page(arc4random());
+ if (mlock(p, len) == 0)
+ n++;
+ len = trunc_page(arc4random());
+ if (arc4random() % 100 < 50)
+ if (munlock(p, len) == 0)
+ n++;
+ }
+ if (n < 10)
+ fprintf(stderr, "Note: tmlock() only succeeded %d times.\n",
+ n);
+
+ return (NULL);
+}
+
+static void
+test(void)
+{
+ pid_t pid;
+ pthread_t tid[2];
+ int i, rc;
+
+ if ((rc = pthread_create(&tid[0], NULL, tmmap, NULL)) != 0)
+ errc(1, rc, "tmmap()");
+ if ((rc = pthread_create(&tid[1], NULL, tmlock, NULL)) != 0)
+ errc(1, rc, "tmlock()");
+
+ for (i = 0; i < 100; i++) {
+ if ((pid = fork()) == 0) {
+ usleep(10000);
+ _exit(0);
+ }
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+ }
+
+ raise(SIGSEGV);
+
+ for (i = 0; i < 2; i++)
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ _exit(0);
+}
+
+int
+main(void)
+{
+
+ pid_t pids[PARALLEL];
+ time_t start;
+ int e, i, j, status;
+
+ start = time(NULL);
+ for (i = 0; i < LOOPS; i++) {
+ for (j = 0; j < PARALLEL; j++) {
+ if ((pids[j] = fork()) == 0)
+ test();
+ }
+
+ e = 0;
+ for (j = 0; j < PARALLEL; j++) {
+ if (waitpid(pids[j], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[j]);
+ e += status == 0 ? 0 : 1;
+ }
+ if (time(NULL) - start > 1200) {
+ fprintf(stderr, "Timed out.");
+ break;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/mmap22.sh b/tools/test/stress2/misc/mmap22.sh
new file mode 100755
index 000000000000..4b730100d7dc
--- /dev/null
+++ b/tools/test/stress2/misc/mmap22.sh
@@ -0,0 +1,130 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Core dump wedge
+# Trimmed down version of mmap15.sh
+# http://people.freebsd.org/~pho/stress/log/mmap15-2.txt
+# Fixed in r272534 + r272535.
+
+# Page fault seen: http://people.freebsd.org/~pho/stress/log/kostik733.txt
+# Fixed in r274474.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > mmap22.c
+mycc -o mmap22 -Wall -Wextra -O2 -g mmap22.c -lpthread || exit 1
+rm -f mmap22.c
+
+su $testuser -c /tmp/mmap22 &
+
+sleep 300
+while pgrep -q mmap22; do
+ pkill -9 mmap22
+ sleep 2
+done
+wait
+
+rm -f /tmp/mmap22 /tmp/mmap22.core
+exit 0
+EOF
+#include <sys/mman.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define MMAPS 25
+#define PARALLEL 4
+#define SIZ (64 * 1024 * 1024)
+
+void *
+tmmap(void *arg __unused)
+{
+ size_t len;
+ void *p;
+ int i;
+
+ len = SIZ;
+ for (i = 0; i < MMAPS; i++)
+ p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
+ fsync(fileno(stdout));
+
+ return (NULL);
+}
+
+void
+test(void)
+{
+ pthread_t tid;
+ int i, rc;
+
+ if ((rc = pthread_create(&tid, NULL, tmmap, NULL)) != 0)
+ errc(1, rc, "test()");
+
+ for (i = 0; i < 100; i++) {
+ if (fork() == 0) {
+ usleep(10000);
+ _exit(0);
+ }
+ wait(NULL);
+ }
+
+ raise(SIGSEGV);
+
+ if ((rc = pthread_join(tid, NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i;
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ test();
+ }
+
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap23.sh b/tools/test/stress2/misc/mmap23.sh
new file mode 100755
index 000000000000..6a4a04eecab1
--- /dev/null
+++ b/tools/test/stress2/misc/mmap23.sh
@@ -0,0 +1,108 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# "panic: vm_object_unwire: missing page" seen.
+# https://people.freebsd.org/~pho/stress/log/mmap23.txt
+# Test scenario by kib@.
+# Fixed by r285878.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap23.c
+mycc -o mmap23 -Wall -Wextra mmap23.c || exit 1
+rm -f mmap23.c
+cd $odir
+
+cp /tmp/mmap23 /tmp/mmap23.inputfile
+daemon sh -c '(cd ../testcases/swap; ./swap -t 1m -i 2)' > \
+ /dev/null 2>&1
+
+/tmp/mmap23 /tmp/mmap23.inputfile &
+sleep .2
+dd if=/dev/zero of=/tmp/mmap23.inputfile bs=1k count=1 status=none
+
+while pgrep -q swap; do
+ pkill -9 swap
+done
+
+rm -f /tmp/mmap23 /tmp/mmap23.inputfile
+exit
+
+EOF
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+const char *file;
+
+void
+test(void)
+{
+ struct stat st;
+ char *p;
+ size_t len;
+ int error, fd;
+
+ if ((fd = open(file, O_RDWR)) == -1)
+ err(1, "open %s", file);
+ if ((error = fstat(fd, &st)) == -1)
+ err(1, "stat(%s)", file);
+ len = round_page(st.st_size);
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ p[arc4random() % len] = 1;
+ if ((error = mlock(p, len)) == -1)
+ err(1, "mlock");
+ sleep(2);
+ if (munmap(p, len) == -1)
+ err(1, "unmap()");
+ close(fd);
+}
+
+int
+main(int argc, char *argv[])
+{
+ if (argc != 2)
+ errx(1, "Usage: %s <file>", argv[0]);
+ file = argv[1];
+
+ test();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap24.sh b/tools/test/stress2/misc/mmap24.sh
new file mode 100755
index 000000000000..4a25a0e3520f
--- /dev/null
+++ b/tools/test/stress2/misc/mmap24.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# "panic: vm_page_unwire: page 0xc36cafbc's wire count is zero" seen.
+# https://people.freebsd.org/~pho/stress/log/mmap24.txt
+# Test scenario by trasz@.
+# Fixed by r285878.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap24.c
+mycc -o mmap24 -Wall -Wextra mmap24.c || exit 1
+rm -f mmap24.c
+cd $odir
+
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+cp /tmp/mmap24 $mntpoint
+(cd $mntpoint; ./mmap24) &
+sleep .2
+umount -f $mntpoint
+
+kill $!
+wait
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+
+rm -f /tmp/mmap24
+exit
+
+EOF
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <sys/mman.h>
+#include <err.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ int error;
+
+ error = mlockall(MCL_CURRENT | MCL_FUTURE);
+ if (error != 0)
+ err(1, "mlockall");
+
+ for (;;) {
+ sleep(1);
+ }
+}
diff --git a/tools/test/stress2/misc/mmap25.sh b/tools/test/stress2/misc/mmap25.sh
new file mode 100755
index 000000000000..caad84d9b549
--- /dev/null
+++ b/tools/test/stress2/misc/mmap25.sh
@@ -0,0 +1,120 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# "panic: vm_page_unwire: page 0xc36cce48's wire count is zero" seen.
+# Fixed by r285878.
+
+# Test scenario by kib@:
+# 1. We mapped and mlocked the file.
+# 2. The file is truncated
+# 3. The file is extended again to cover the whole mapped area.
+# 4. The program accesses the mapping past the point of truncation.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap25.c
+mycc -o mmap25 -Wall -Wextra mmap25.c || exit 1
+rm -f mmap25.c
+cd $odir
+
+cp /tmp/mmap25 /tmp/mmap25.inputfile
+daemon sh -c '(cd ../testcases/swap; ./swap -t 1m -i 2)' > \
+ /dev/null 2>&1
+sleep 1
+
+/tmp/mmap25 /tmp/mmap25.inputfile
+
+while pgrep -q swap; do
+ pkill -9 swap
+done
+rm -f /tmp/mmap25 /tmp/mmap25.inputfile mmap25.core
+exit
+
+EOF
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+const char *file;
+volatile char c;
+
+void
+test(void)
+{
+ struct stat st;
+ char *p;
+ size_t len;
+ off_t pos;
+ int error, fd;
+
+ if ((fd = open(file, O_RDWR)) == -1)
+ err(1, "open %s", file);
+ if ((error = fstat(fd, &st)) == -1)
+ err(1, "stat(%s)", file);
+ len = round_page(st.st_size);
+ pos = len - getpagesize();
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0))
+ == MAP_FAILED)
+ err(1, "mmap");
+ if ((error = mlock(p, len)) == -1)
+ err(1, "mlock");
+
+ if (ftruncate(fd, pos) == -1)
+ err(1, "ftruncate 1");
+ if (ftruncate(fd, len) == -1)
+ err(1, "ftruncate 2");
+
+ c = p[pos]; /* Program received signal SIGSEGV, Segmentation fault. */
+
+ if (munmap(p, len) == -1)
+ err(1, "unmap()");
+ close(fd);
+}
+
+int
+main(int argc, char *argv[])
+{
+ if (argc != 2)
+ errx(1, "Usage: %s <file>", argv[0]);
+ file = argv[1];
+
+ test();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap26.sh b/tools/test/stress2/misc/mmap26.sh
new file mode 100755
index 000000000000..19479b92461f
--- /dev/null
+++ b/tools/test/stress2/misc/mmap26.sh
@@ -0,0 +1,123 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# "panic: vm_page_unwire: page 0xc36cce48's wire count is zero" seen.
+# https://people.freebsd.org/~pho/stress/log/kostik820.txt
+# Fixed by r285878.
+
+# Variation of mmap25.sh:
+# Access one byte past the end of the file, which is not wrong, but
+# out of specified behaviour.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap26.c
+mycc -o mmap26 -Wall -Wextra mmap26.c || exit 1
+rm -f mmap26.c
+cd $odir
+
+cp /tmp/mmap26 /tmp/mmap26.inputfile
+daemon sh -c '(cd ../testcases/swap; ./swap -t 1m -i 2)' > \
+ /dev/null 2>&1
+sleep 1
+
+(cd /tmp; /tmp/mmap26 /tmp/mmap26.inputfile)
+
+while pkill -9 swap; do :; done
+rm -f /tmp/mmap26 /tmp/mmap26.inputfile /tmp/mmap26.core
+exit 0
+
+EOF
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+const char *file;
+volatile char c;
+
+void
+test(void)
+{
+ struct stat st;
+ char *p;
+ size_t len;
+ int error, fd;
+
+ if ((fd = open(file, O_RDWR)) == -1)
+ err(1, "open %s", file);
+ if ((error = fstat(fd, &st)) == -1)
+ err(1, "stat(%s)", file);
+ len = round_page(st.st_size);
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0))
+ == MAP_FAILED)
+ err(1, "mmap");
+ if ((error = mlock(p, len)) == -1)
+ err(1, "mlock");
+
+ if (ftruncate(fd, (off_t)0) == -1)
+ err(1, "ftruncate 1");
+ if (ftruncate(fd, len) == -1)
+ err(1, "ftruncate 2");
+
+ p[len - 1] = 1;
+
+ /* one byte past EOF */
+ if (round_page((unsigned long)&p[len]) ==
+ round_page((unsigned long)&p[len - 1])) {
+ fprintf(stderr, "Expect: Segmentation fault (core dumped)\n");
+ c = p[len];
+ }
+
+ if (munmap(p, len) == -1)
+ err(1, "unmap()");
+ close(fd);
+}
+
+int
+main(int argc, char *argv[])
+{
+ if (argc != 2)
+ errx(1, "Usage: %s <file>", argv[0]);
+ file = argv[1];
+
+ test();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap27.sh b/tools/test/stress2/misc/mmap27.sh
new file mode 100755
index 000000000000..7f8d4e94583a
--- /dev/null
+++ b/tools/test/stress2/misc/mmap27.sh
@@ -0,0 +1,126 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# 'WARNING: A device driver has set "memattr" inconsistently.' seen on
+# console.
+# https://people.freebsd.org/~pho/stress/log/mmap27.txt
+# Fixed by r298891.
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > mmap27.c
+mycc -o mmap27 -Wall -Wextra -g -O0 mmap27.c || exit 1
+rm -f mmap27.c
+cd $odir
+
+daemon sh -c '(cd ../testcases/swap; ./swap -t 2m -i 20 -l 100)' > /dev/null 2>&1
+sleep 2
+/tmp/mmap27
+while pgrep -q swap; do
+ pkill -9 swap
+done
+rm -f ./mmap27 /tmp/mmap27.0* /tmp/mmap27
+exit 0
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+int fd;
+
+#define ADRSPACE (256 * 1024 * 1024 )
+#define PARALLEL 64
+#define RUNTIME 120
+#define STARTADDR 0x50000000U
+
+static void
+work(void)
+{
+ size_t left, len;
+ int i;
+ char *p;
+ volatile char val;
+
+ if ((fd = open("/dev/mem", O_RDWR)) == -1)
+ err(1,"open()");
+
+ p = (void *)STARTADDR + trunc_page(arc4random() % ADRSPACE);
+ left = ADRSPACE - (size_t)p + STARTADDR;
+ len = trunc_page(arc4random() % left) + PAGE_SIZE;
+
+ if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
+ MAP_FAILED) {
+ if (errno == ENOMEM)
+ return;
+ err(1, "mmap()");
+ }
+
+ for (i = 0; i < 100; i++)
+ val = p[arc4random() % len];
+
+ if (munmap(p, len) == -1)
+ err(1, "munmap(%p)", p);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ time_t start;
+ int i, n;
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ n = arc4random() % PARALLEL + 1;
+ for (i = 0; i < n; i++) {
+ if ((pids[i] = fork()) == 0)
+ work();
+ }
+
+ for (i = 0; i < n; i++)
+ if (waitpid(pids[i], NULL, 0) != pids[i])
+ err(1, "waitpid(%d)", pids[i]);
+ }
+ close(fd);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap28.sh b/tools/test/stress2/misc/mmap28.sh
new file mode 100755
index 000000000000..fd64202e3814
--- /dev/null
+++ b/tools/test/stress2/misc/mmap28.sh
@@ -0,0 +1,131 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# 'panic: vnode_pager_generic_getpages: page 0xc3350b0c offset beyond vp
+# 0xcc187000 size' seen.
+# https://people.freebsd.org/~pho/stress/log/mmap28.txt
+# This was introduced by r292373.
+#
+# A page fault is seen on a non INVARIANTS kernel w/ r292373,
+# whereas this test runs as expected on r292372.
+# https://people.freebsd.org/~pho/stress/log/mmap28-2.txt
+# https://people.freebsd.org/~pho/stress/log/mmap28-3.txt
+# To repeat, run this test with "sysctl vfs.ffs.use_buf_pager=0".
+# Fixed by r307626
+
+# Test scenario refinement by kib@
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > mmap28.c
+mycc -o mmap28 -Wall -Wextra -g -O0 mmap28.c || exit 1
+rm -f mmap28.c
+cd $odir
+
+(cd /tmp; ./mmap28)
+
+rm -f /tmp/mmap28 /tmp/mmap28.0* /tmp/mmap28.core
+exit 0
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define ADRSPACE (256 * 1024 * 1024 )
+#define STARTADDR 0x50000000U
+
+static void
+work(void)
+{
+ size_t indx, left, len;
+ int fd, rfd;
+ int i;
+ char *p;
+ char path[128];
+ volatile char val;
+
+ if ((rfd = open("/dev/random", O_RDONLY)) == -1)
+ err(1, "open(/dev/random)");
+
+ snprintf(path, sizeof(path), "/tmp/mmap28.%06d", 0);
+ if ((fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1,"open(%s)", path);
+
+ p = (void *)STARTADDR + trunc_page(arc4random() % ADRSPACE);
+ left = ADRSPACE - (size_t)p + STARTADDR;
+ len = trunc_page(arc4random() % left) + PAGE_SIZE;
+
+ if (ftruncate(fd, len) == -1)
+ err(1, "ftruncate");
+
+ if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
+ MAP_FAILED) {
+ if (errno == ENOMEM)
+ return;
+ err(1, "mmap()");
+ }
+
+ /*
+ Truncating the mapped file triggers a panic when accessed beyond
+ EOF.
+ */
+ if (ftruncate(fd, len / 2) == -1)
+ err(1, "ftruncate(%s)", path);
+
+ for (i = 0; i < 1000; i++) {
+ if (read(rfd, &indx, sizeof(indx)) != sizeof(indx))
+ err(1, "read(random)");
+ val = p[indx % len];
+ }
+ close(rfd);
+
+ if (munmap(p, len) == -1)
+ err(1, "munmap(%p)", p);
+ close(fd);
+ unlink(path);
+}
+
+int
+main(void)
+{
+
+ work();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap29.sh b/tools/test/stress2/misc/mmap29.sh
new file mode 100755
index 000000000000..e6aacc15e48e
--- /dev/null
+++ b/tools/test/stress2/misc/mmap29.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+# Test scenario by: David Cross <dcrosstech@gmail.com>
+
+# "panic: softdep_deallocate_dependencies: dangling deps" seen.
+# https://people.freebsd.org/~pho/stress/log/mmap29.txt
+# Fixed by: r302567.
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+[ -z "`which timeout`" ] && exit 0
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs -U md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+mkdir $mntpoint/mmap29
+cd /tmp
+cat > mmap29.c <<EOFHERE
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(int argc, char **argv)
+{
+ int fd;
+ unsigned char *memrange;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <file>\n", argv[0]);
+ exit(1);
+ }
+ unlink(argv[1]);
+ if ((fd = open(argv[1], O_RDWR | O_CREAT, DEFFILEMODE)) == -1)
+ err(1, "open(%s)", argv[1]);
+ lseek(fd, 0xbfff, SEEK_SET);
+ write(fd, "\0", 1);
+ if ((memrange = mmap(0, 0x2b6000, PROT_READ | PROT_WRITE, MAP_SHARED |
+ MAP_HASSEMAPHORE | MAP_NOSYNC, fd, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ memrange[0] = 5;
+ munmap(memrange, 0x2b6000);
+ close(fd);
+
+ return (0);
+}
+EOFHERE
+
+cc -o mmap29 -Wall -Wextra -O0 -g mmap29.c || exit 1
+rm mmap29.c
+./mmap29 $mntpoint/mmap29/mmap291
+old=`sysctl -n kern.maxvnodes`
+trap "sysctl kern.maxvnodes=$old" EXIT INT
+sysctl kern.maxvnodes=2000
+timeout 60 find / -xdev -print >/dev/null
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm mmap29
+exit 0
diff --git a/tools/test/stress2/misc/mmap3.sh b/tools/test/stress2/misc/mmap3.sh
new file mode 100755
index 000000000000..bc5905dd920e
--- /dev/null
+++ b/tools/test/stress2/misc/mmap3.sh
@@ -0,0 +1,167 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Variation of mmap2.sh with focus on random arguments for mprotect()
+# https://people.freebsd.org/~pho/stress/log/kostik209.txt
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > mmap3.c
+mycc -o mmap3 -Wall mmap3.c -lpthread || exit 1
+rm -f mmap3.c
+
+start=`date '+%s'`
+while [ `date '+%s'` -lt $((start + 5 * 60)) ]; do
+ ./mmap3
+done
+echo "Expect Segmentation faults"
+start=`date '+%s'`
+while [ `date '+%s'` -lt $((start + 5 * 60)) ]; do
+ ./mmap3 random
+done
+rm -f mmap3 mmap3.core /tmp/mmap.0*
+exit
+
+EOF
+/*
+ Stress mmap by having max 100 threads mapping random areas within
+ a 100 Mb range.
+ */
+#include <sys/types.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <sys/mman.h>
+#include <sys/param.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#define THREADS 100
+#define STARTADDR 0x50000000U
+#define ADRSPACE 0x06400000U /* 100 Mb */
+
+static int ra;
+
+void
+trash(void *p)
+{
+ unsigned long v;
+
+ mprotect(p, 0x570e3d38, 0x2c8fd54f);
+ if (ra) {
+ v = arc4random();
+#if defined(__LP64__)
+ v = v << 32 | arc4random();
+#endif
+ madvise((void *)v, arc4random(), arc4random());
+ mprotect((void *)v, arc4random(), arc4random());
+ msync((void *)v, arc4random(), arc4random());
+ }
+
+}
+
+void
+work(int nr)
+{
+ int fd, m;
+ void *p;
+ size_t len;
+ char path[128];
+
+ p = (void *)STARTADDR;
+ len = ADRSPACE;
+
+ sprintf(path, "/tmp/mmap.%06d.%04d", getpid(), nr);
+ if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0622)) == -1)
+ err(1,"open()");
+ if (ftruncate(fd, len) == -1)
+ err(1, "ftruncate");
+ if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
+ MAP_FAILED) {
+ if (errno == ENOMEM)
+ return;
+ err(1, "mmap()");
+ }
+ if (unlink(path) == -1)
+ err(1, "unlink(%s)", path);
+
+ trash(p);
+
+ m = arc4random() % 10;
+ if (madvise(p, len, m) == -1)
+ warn("madvise(%p, %zd, %d)", p, len, m);
+ if (mprotect(p, trunc_page(arc4random() % len), PROT_READ) == -1 )
+ err(1, "mprotect failed with error:");
+ if (msync(p, 0, MS_SYNC) == -1)
+ err(1, "msync(%p)", p);
+ if (munmap(p, len) == -1)
+ err(1, "munmap(%p)", p);
+ close(fd);
+}
+
+void *
+thr(void *arg)
+{
+ int i;
+
+ for (i = 0; i < 512; i++) {
+ work(*(int *)arg);
+ }
+ return (0);
+}
+
+int
+main(int argc, char **argv)
+{
+ pthread_t threads[THREADS];
+ int nr[THREADS];
+ int i, n, r;
+
+ n = arc4random() % 14 + 5;
+ ra = argc != 1;
+// printf("Address start 0x%x, address end 0x%x, pages %d, n %d\n",
+// STARTADDR, STARTADDR + ADRSPACE, ADRSPACE>>PAGE_SHIFT, n);
+ for (i = 0; i < n; i++) {
+ nr[i] = i;
+ if ((r = pthread_create(&threads[i], NULL, thr, (void *)&nr[i])) != 0)
+ errc(1, r, "pthread_create()");
+ }
+
+ for (i = 0; i < n; i++) {
+ if ((r = pthread_join(threads[i], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", i);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap30.sh b/tools/test/stress2/misc/mmap30.sh
new file mode 100755
index 000000000000..3f1b8cccbc54
--- /dev/null
+++ b/tools/test/stress2/misc/mmap30.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# r320344: allow mprotect(2) over the guards to succeed regardless of
+# the requested protection.
+
+. ../default.cfg
+
+grep -q MAP_GUARD /usr/include/sys/mman.h 2>/dev/null || exit 0
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > mmap30.c
+mycc -o mmap30 -Wall -Wextra -O2 -g mmap30.c || exit 1
+rm -f mmap30.c /tmp/mmap30.core
+
+/tmp/mmap30 > /dev/null
+s=$?
+
+rm -f /tmp/mmap30 /tmp/mmap30.core
+exit $s
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ int pagesz;
+ void *addr;
+
+ pagesz = getpagesize();
+ addr = mmap(NULL, pagesz, PROT_NONE, MAP_GUARD, -1, 0);
+ if (addr == (char *)MAP_FAILED)
+ err(1, "FAIL: mmap(MAP_GUARD)");
+
+ if (mprotect(addr, pagesz, PROT_READ | PROT_WRITE) == -1)
+ err(1, "mprotect(RW)");
+
+ if (mprotect(addr, pagesz, PROT_NONE) == -1)
+ err(1, "mprotect(RW)");
+
+ if (munmap(addr, pagesz) == -1)
+ err(1, "munmap");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap31.sh b/tools/test/stress2/misc/mmap31.sh
new file mode 100755
index 000000000000..8dd239579fb2
--- /dev/null
+++ b/tools/test/stress2/misc/mmap31.sh
@@ -0,0 +1,174 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Test scenario for 2MB page promotions.
+# No problems seen.
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ "`uname -p`" = "amd64" ] || exit 0
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap31.c
+mycc -o mmap31 -Wall -Wextra -O0 -g mmap31.c || exit 1
+rm -f mmap31.c
+cd $odir
+
+(cd ../testcases/swap; ./swap -t 10m -i 20) > /dev/null &
+
+/tmp/mmap31
+s=$?
+kill $! 2>/dev/null
+while pkill -9 swap; do :; done
+wait
+
+rm -rf /tmp/mmap31
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <time.h>
+#include <unistd.h>
+
+#define SEGS 512
+static size_t l[SEGS];
+static char *c[SEGS];
+static volatile char r;
+static volatile u_int *share;
+
+#define PARALLEL 8
+#define RUNTIME (2 * 60)
+#define SYNC 0
+
+static void
+handler(int s __unused)
+{
+ _exit(0);
+}
+
+static void
+touch(void)
+{
+ time_t t;
+ int idx;
+ char *c1;
+
+ t = time(NULL);
+ while (time(NULL) - t < 5) {
+ idx = arc4random() % SEGS;
+ c1 = c[idx];
+ idx = arc4random() % l[idx];
+ r += c1[idx];
+ c1[idx] += 1;
+ }
+}
+
+static void
+test(void)
+{
+ pid_t pid;
+ int i, mode, status;
+
+ r = 0;
+ for (i = 0; i < SEGS; i++) {
+ l[i] = (arc4random() % 512 + 1) * PAGE_SIZE;
+ mode = PROT_READ | PROT_WRITE;
+ if ((c[i] = (char *)mmap(NULL, l[i], mode,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ }
+
+ if ((pid = fork()) == 0) {
+ signal(SIGHUP, handler);
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+ touch();
+ pause();
+ _exit(0);
+ }
+
+ while (share[SYNC] != PARALLEL)
+ ;
+ touch();
+
+ kill(pid, SIGHUP);
+ if (waitpid(pid, &status, 0) != pid)
+ err(1, "waitpid");
+ if (status != 0)
+ errx(1, "child status = %d", status);
+
+ _exit(status);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/mmap32.sh b/tools/test/stress2/misc/mmap32.sh
new file mode 100755
index 000000000000..a775b756084e
--- /dev/null
+++ b/tools/test/stress2/misc/mmap32.sh
@@ -0,0 +1,202 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Bug 223732 - mmap(2) causes unkillable denial of service with specific
+# flags
+# Test scenario inspired by: Arto Pekkanen <aksyom@gmail.com>
+
+# Fixed by r326098.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap32.c
+mycc -o mmap32 -Wall -Wextra -O0 -g mmap32.c || exit 1
+rm -f mmap32.c
+
+$dir/mmap32
+s=$?
+[ -f mmap32.core -a $s -eq 0 ] &&
+ { ls -l mmap32.core; mv mmap32.core /tmp; s=1; }
+
+rm -rf $dir/mmap32
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/resource.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <unistd.h>
+
+#define N 4096
+static uint32_t r[N];
+
+static unsigned long
+makearg(void)
+{
+ unsigned int i;
+ unsigned long val;
+
+ val = arc4random();
+ i = arc4random() % 100;
+ if (i < 20)
+ val = val & 0xff;
+ if (i >= 20 && i < 40)
+ val = val & 0xffff;
+ if (i >= 40 && i < 60)
+ val = (unsigned long)(r) | (val & 0xffff);
+#if defined(__LP64__)
+ if (i >= 60) {
+ val = (val << 32) | arc4random();
+ if (i > 80)
+ val = val & 0x00007fffffffffffUL;
+ }
+#endif
+
+ return(val);
+}
+
+static void
+fuzz(int arg, void *addr, size_t len, int prot, int flags, int fd,
+ off_t offset)
+{
+ time_t start;
+ void *vp;
+ int n;
+
+ setproctitle("arg%d", arg);
+ n = 0;
+ start = time(NULL);
+ while (time(NULL) - start < 10) {
+ switch (arg) {
+ case 1:
+ addr = (void *)makearg();
+ break;
+ case 2:
+ len = makearg();
+ break;
+ case 3:
+ prot = makearg();
+ break;
+ case 4:
+ flags = makearg();
+ break;
+ case 5:
+ fd = makearg();
+ break;
+ case 6:
+ offset = makearg() & 0xffff;
+ break;
+ case 34:
+ prot = makearg();
+ flags = makearg();
+ break;
+ default:
+ errx(1, "Bad argument %d to %s", arg, __func__);
+ }
+ vp = mmap(addr, len, prot, flags, fd, offset);
+ if (vp != MAP_FAILED) {
+ munmap(vp, len);
+ n++;
+ }
+ }
+#if defined(DEBUG)
+ if (n == 0 && arg != 5)
+ fprintf(stderr, "%s(%d) failed\n", __func__, arg);
+#endif
+ exit(0);
+}
+
+int
+main(void)
+{
+ off_t offset;
+ pid_t pid;
+ size_t len;
+ struct rlimit rl;
+ time_t start;
+ void *addr, *vp;
+ int e, flags, fd, i, prot, status;
+
+ e = 0;
+
+ rl.rlim_max = rl.rlim_cur = 0;
+ if (setrlimit(RLIMIT_CORE, &rl) == -1)
+ warn("setrlimit");
+ addr = 0;
+ len = PAGE_SIZE;
+ prot = PROT_READ | PROT_WRITE;
+ flags = MAP_ANON | MAP_SHARED;
+ fd = -1;
+ offset = 0;
+ vp = mmap(addr, len, prot, flags, fd, offset);
+ if (vp == MAP_FAILED)
+ err(1, "initail mmap");
+ munmap(vp, len);
+
+ start = time(NULL);
+ while (time(NULL) - start < 120) {
+ for (i = 0; i < N; i++)
+ r[i] = arc4random();
+ for (i = 0; i < 6; i++) {
+ if ((pid = fork()) == 0)
+ fuzz(i + 1, addr, len, prot, flags, fd,
+ offset);
+ if (waitpid(pid, &status, 0) != pid)
+ err(1, "waitpid %d", pid);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pid, WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+ if ((pid = fork()) == 0)
+ fuzz(34, addr, len, prot, flags, fd, offset);
+ if (waitpid(pid, &status, 0) != pid)
+ err(1, "waitpid %d", pid);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pid, WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/mmap33.sh b/tools/test/stress2/misc/mmap33.sh
new file mode 100755
index 000000000000..70ac054a9934
--- /dev/null
+++ b/tools/test/stress2/misc/mmap33.sh
@@ -0,0 +1,121 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+# ftruncate+mmap+fsync fails for small maps
+# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=225586
+
+# Original test scenario by tris_vern@hotmail.com
+
+# Fixed in r328773:
+# On pageout, in vnode generic pager, for partially dirty page, only
+# clear dirty bits for completely invalid blocks.
+
+. ../default.cfg
+
+cat > /tmp/mmap33.c <<EOF
+#include <sys/mman.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+main (int argc, char *argv[])
+{
+ size_t i, size1, size2;
+ int fd;
+ char *data;
+ char *filename;
+ char pattern = 0x01;
+
+ if (argc != 4) {
+ fprintf(stderr, "Usage: %s filename size1 size2\n", argv[0]);
+ exit(1);
+ }
+
+ filename = argv[1];
+ size1 = atoi(argv[2]);
+ size2 = atoi(argv[3]);
+
+ fd = open(filename, O_RDWR | O_TRUNC | O_CREAT, 0644);
+ for (i = 0; i < size1; i++)
+ write(fd, &pattern, 1);
+ close(fd);
+
+ fd = open(filename, O_RDWR, 0644);
+ if (fd == -1)
+ err(1, "open(%s)", filename);
+ if (ftruncate(fd, size2) == -1)
+ err(1, "ftruncate()");
+ data = mmap(NULL, size2, PROT_READ|PROT_WRITE,MAP_SHARED, fd, 0);
+ if (data == MAP_FAILED)
+ err(1, "mmap()");
+ memset(data, 0xFF, size2);
+
+ if (munmap(data, size2) == -1)
+ err(1, "munmap");
+ close(fd);
+
+ return (0);
+}
+EOF
+cc -o /tmp/mmap33 -Wall -Wextra -O2 -g /tmp/mmap33.c || exit 1
+rm /tmp/mmap33.c
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags -n md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+set +e
+
+file=file
+odir=`pwd`
+cd $mntpoint
+/tmp/mmap33 $file 1024 511
+s=$?
+sum1=`md5 < $mntpoint/$file`
+[ -f mmap33.core -a $s -eq 0 ] &&
+ { ls -l mmap33.core; mv mmap33.core /tmp; s=1; }
+cd $odir
+umount $mntpoint
+mount /dev/md${mdstart}$part $mntpoint
+# This fails for truncate size < 512
+sum2=`md5 < $mntpoint/$file`
+[ $sum1 = $sum2 ] ||
+ { s=2; echo "md5 fingerprint differs."; }
+umount $mntpoint
+
+mdconfig -d -u $mdstart
+rm /tmp/mmap33
+exit $s
diff --git a/tools/test/stress2/misc/mmap34.sh b/tools/test/stress2/misc/mmap34.sh
new file mode 100755
index 000000000000..028d23b4f71f
--- /dev/null
+++ b/tools/test/stress2/misc/mmap34.sh
@@ -0,0 +1,230 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# "panic: caller failed to provide space -546873344 at address 0x20a00000"
+# seen.
+# https://people.freebsd.org/~pho/stress/log/indir_trunc.txt
+# Fixed by r348968
+
+# Test scenario inspired by:
+# syzbot+6532e9aab8911f58beeb@syzkaller.appspotmail.com
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap34.c
+mycc -o mmap34 -Wall -Wextra -O0 -g mmap34.c -lpthread || exit 1
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 4g -u $mdstart
+newfs $newfs_flags -n md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+(cd ../testcases/swap; ./swap -t 5m -i 20) &
+cd $mntpoint
+$dir/mmap34
+s=$?
+while pkill swap; do :; done
+wait
+[ -f mmap34.core ] &&
+ { ls -l mmap34.core; mv mmap34.core $dir; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -f $dir/mmap34
+[ $s -eq 0 ] && rm -f mmap34.c
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#if defined(__LP64__)
+#define MASK 0x7ffffffffffffULL
+#else
+#define MASK 0xffffffffULL
+#endif
+#define PARALLEL 64
+#define RUNTIME (5 * 60)
+#define SYNC 0
+
+static int fd;
+
+static void *
+t1(void *data __unused)
+{
+ off_t offset;
+ time_t start;
+
+ start = time(NULL);
+ while (time(NULL) - start < 30) {
+ offset = arc4random();
+ offset = (offset << 32) | arc4random();
+ offset &= MASK;
+ if (lseek(fd, offset, SEEK_SET) == -1)
+ err(1, "lseek(%jd)", offset);
+ if (write(fd, "a", 1) != 1)
+ err(1, "write");
+ if (fsync(fd) == -1)
+ err(1, "fsync");
+ usleep(100);
+ }
+ return (NULL);
+}
+static void *
+t2(void *data __unused)
+{
+ off_t offset, old;
+ time_t start;
+ void *p;
+ char *c;
+
+ old = 0;
+ start = time(NULL);
+ while (time(NULL) - start < 30) {
+ if (old != 0)
+ munmap(p, old);
+ offset = arc4random();
+ offset = (offset << 32) | arc4random();
+ offset &= MASK;
+ if (ftruncate(fd, offset) == -1)
+ err(1, "ftruncate(%jd)", offset);
+ write(fd, "b", 1);
+ p = mmap(NULL, offset, PROT_READ | PROT_WRITE, MAP_SHARED,
+ fd, 0);
+ if (p == MAP_FAILED)
+ old = 0;
+ else {
+ old = offset;
+ c = p;
+ c[offset / 2] = 1;
+ if (offset > 0)
+ c[offset - 1] = 2;
+ }
+ usleep(20000);
+ }
+ return (NULL);
+}
+
+static void
+test(void)
+{
+ pthread_t tid[2];
+ int r;
+ char file[80];
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ snprintf(file, sizeof(file), "file.%d", getpid());
+ if ((fd = open(file, O_RDWR | O_CREAT, DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+
+ if ((r = pthread_create(&tid[0], NULL, t1, NULL)) != 0)
+ errc(1, r, "pthread_create");
+ if ((r = pthread_create(&tid[1], NULL, t2, NULL)) != 0)
+ errc(1, r, "pthread_create");
+
+ if ((r = pthread_join(tid[0], NULL)) != 0)
+ errc(1, r, "pthread_join");
+ if ((r = pthread_join(tid[1], NULL)) != 0)
+ errc(1, r, "pthread_join");
+ close(fd);
+ unlink(file);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/mmap35.sh b/tools/test/stress2/misc/mmap35.sh
new file mode 100755
index 000000000000..6e76434e77ec
--- /dev/null
+++ b/tools/test/stress2/misc/mmap35.sh
@@ -0,0 +1,112 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# mmap(2) should fail with "Invalid argument" on i386
+# Fixed by r348843
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+[ "`uname -p`" = "i386" ] || exit 0
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap35.c
+mycc -o mmap35 -Wall -Wextra -O0 -g mmap35.c || exit 1
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 4g -u $mdstart
+newfs $newfs_flags -n md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+cd $mntpoint
+echo "Expect: mmap35: mmap: Cannot allocate memory"
+$dir/mmap35
+s=$?
+[ -f mmap35.core ] &&
+ { ls -l mmap35.core; mv mmap35.core $dir; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -f $dir/mmap35
+[ $s -eq 0 ] && rm -f mmap35.c
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define SIZ 0xfffff0fcULL
+
+int
+main(void)
+{
+ off_t offset;
+ int fd;
+ char *c, file[80];
+
+ snprintf(file, sizeof(file), "file.%d", getpid());
+ if ((fd = open(file, O_RDWR | O_CREAT, DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+
+ offset = SIZ;
+ if (ftruncate(fd, offset) == -1)
+ err(1, "ftruncate(%jd)", offset);
+ write(fd, "b", 1);
+
+ c = mmap(NULL, offset, PROT_READ | PROT_WRITE, MAP_SHARED,
+ fd, 0);
+ if (c == MAP_FAILED)
+ warn("mmap");
+ else
+ c[offset / 2] = 1;
+
+ unlink(file);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap36.sh b/tools/test/stress2/misc/mmap36.sh
new file mode 100755
index 000000000000..2c38fd26890b
--- /dev/null
+++ b/tools/test/stress2/misc/mmap36.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# mmap(2) should fail with "Invalid argument" and not core dump
+# Fixed by
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap36.c
+mycc -o mmap36 -Wall -Wextra -O0 -g mmap36.c || exit 1
+
+echo "Expect: mmap36: mmap: Cannot allocate memory"
+$dir/mmap36
+s=$?
+[ -f mmap36.core ] &&
+ { ls -l mmap36.core; mv mmap36.core $dir; s=1; }
+cd $odir
+[ $s -eq 0 ] && rm -f mmap36.c mmap36
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ off_t offset;
+ char *c;
+
+ offset = SIZE_MAX - 5;
+
+ c = mmap(NULL, offset, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON,
+ -1, 0);
+ if (c == MAP_FAILED)
+ warn("mmap");
+ else
+ c[offset / 2] = 1;
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap37.sh b/tools/test/stress2/misc/mmap37.sh
new file mode 100755
index 000000000000..f5e25a282927
--- /dev/null
+++ b/tools/test/stress2/misc/mmap37.sh
@@ -0,0 +1,153 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Regression test for https://reviews.freebsd.org/D20800
+# "Use a consistent snapshot of the fd's rights in fget_mmap()"
+# https://people.freebsd.org/~pho/stress/log/mmap37.txt
+
+# Reported by syzbot+ae359438769fda1840f8@syzkaller.appspotmail.com
+# Test scenario suggestion by markj@
+# Fixed by r349547
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > mmap37.c
+mycc -o mmap37 -Wall -Wextra -O0 mmap37.c -lpthread || exit 1
+rm -f mmap37.c
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+newfs $newfs_flags md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+cd $mntpoint
+/tmp/mmap37
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/mmap37
+exit
+
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/param.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <unistd.h>
+
+#define THREADS 2
+#define SIZ 0x06400000U /* 100 Mb */
+
+static volatile int go;
+static char path[128];
+
+void *
+thr(void *arg)
+{
+ size_t len;
+ void *p;
+ int fd;
+
+ fd = 0;
+ len = SIZ;
+ if (*(int *)arg == 0) {
+ while (go == 1) {
+ if ((fd = open(path, 2)) == -1)
+ err(1,"open()");
+ p = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_SHARED, fd, 0);
+ munmap(p, len);
+ close(fd);
+ usleep(10);
+ }
+ } else {
+ while (go == 1) {
+ close(fd);
+ usleep(10);
+ }
+ }
+
+ return (0);
+}
+
+int
+main(void)
+{
+ pthread_t threads[THREADS];
+ size_t len;
+ int nr[THREADS];
+ int i, fd, r;
+
+ sprintf(path, "mmap37.%06d", getpid());
+ if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0622)) == -1)
+ err(1,"open()");
+ len = SIZ;
+ if (ftruncate(fd, len) == -1)
+ err(1, "ftruncate");
+ close(fd);
+
+ go = 1;
+ for (i = 0; i < THREADS; i++) {
+ nr[i] = i;
+ if ((r = pthread_create(&threads[i], NULL, thr,
+ (void *)&nr[i])) != 0)
+ errc(1, r, "pthread_create()");
+ }
+
+ sleep(30);
+ go = 0;
+
+ for (i = 0; i < THREADS; i++) {
+ if ((r = pthread_join(threads[i], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", i);
+ }
+ if (unlink(path) == -1)
+ err(1, "unlink(%s)", path);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap38.sh b/tools/test/stress2/misc/mmap38.sh
new file mode 100755
index 000000000000..b4cf60afe25b
--- /dev/null
+++ b/tools/test/stress2/misc/mmap38.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+# mmap(2) fuzz test.
+# No problems seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+num=`awk '/SYS_mmap/ {print $NF}' < /usr/include/sys/syscall.h`
+[ -z "$num" ] && exit 0
+start=`date +%s`
+
+cd ../misc
+while [ $((`date +%s` - start)) -lt 300 ]; do
+ ./syscall4.sh $num
+done
+exit 0
diff --git a/tools/test/stress2/misc/mmap39.sh b/tools/test/stress2/misc/mmap39.sh
new file mode 100755
index 000000000000..15aa8ec718a3
--- /dev/null
+++ b/tools/test/stress2/misc/mmap39.sh
@@ -0,0 +1,131 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Attempt to reproduce
+# "panic: pmap_release: pmap 0xfffffe012fb61b08 resident count 1 != 0"
+# No problems seen.
+
+# Test scenario suggestion by kib@
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -p` != "amd64" ] && exit 0
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap39.c
+mycc -o mmap39 -Wall -Wextra -O0 -g mmap39.c -static || exit 1
+rm -f mmap39.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+newfs $newfs_flags md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+for i in `jot 128`; do
+ $dir/mmap39 &
+ pids="$pids $!"
+done
+s=0
+for pid in $pids; do
+ wait $pid
+ r=$?
+ [ $s -ne 0 ] && s=$r
+done
+
+[ -f mmap39.core -a $s -eq 0 ] &&
+ { ls -l mmap39.core; mv mmap39.core $dir; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/mmap39
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <time.h>
+#include <unistd.h>
+
+void
+test(void)
+{
+ size_t i, len;
+ void *p;
+
+ p = (void *)0x8000000000; /* 512G */
+ len = 0x200000; /* 2M */
+ if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0)) ==
+ MAP_FAILED)
+ err(1, "mmap");
+
+ if (p != (void *)0x8000000000)
+ errx(1, "mmap address fail");
+
+ for (i = 0; i < len; i += PAGE_SIZE)
+ *(char *)(p + i) = 1;
+
+ if (munmap(p, len) == -1)
+ err(1, "munmap()");
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ time_t start;
+ pid_t pid;
+
+ sleep(5);
+ start = time(NULL);
+ while (time(NULL) - start < 120) {
+ if ((pid = fork()) == 0)
+ test();
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid");
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap4.sh b/tools/test/stress2/misc/mmap4.sh
new file mode 100755
index 000000000000..8c84401fb960
--- /dev/null
+++ b/tools/test/stress2/misc/mmap4.sh
@@ -0,0 +1,116 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# Test overcommit of the file system capacity
+# Causes panic: 1 vncache entries remaining
+# Fixed in r202529
+
+# Scenario by kib@
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > mmap4.c
+mycc -o mmap4 -Wall -O2 mmap4.c
+rm -f mmap4.c
+
+mount | grep -q "$mntpoint" && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 40m -u $mdstart
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+/tmp/mmap4 /$mntpoint/file
+
+cd $here
+rm -f /tmp/mmap4
+
+while mount | grep -q $mntpoint; do
+ sync;sync;sync
+ sleep 1
+ umount $mntpoint
+done
+
+mdconfig -d -u $mdstart
+exit 0
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <sys/param.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#define STARTADDR 0x0U
+#define ADRSPACE 0x06400000U /* 100 Mb */
+
+int
+main(int argc, char **argv)
+{
+ int fd, ps;
+ void *p;
+ size_t len;
+ volatile char *c;
+ char *path;
+
+ p = (void *)STARTADDR;
+ len = ADRSPACE;
+
+ path = argv[1];
+ if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0622)) == -1)
+ err(1,"open()");
+ if (ftruncate(fd, len) == -1)
+ err(1, "ftruncate");
+ if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
+ MAP_FAILED) {
+ if (errno == ENOMEM)
+ return (1);
+ err(1, "mmap(1)");
+ }
+
+ c = p;
+ ps = getpagesize();
+ for (c = p; (void *)c < p + len; c += ps) {
+ *c = 1;
+ }
+
+ close(fd);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap40.sh b/tools/test/stress2/misc/mmap40.sh
new file mode 100755
index 000000000000..4bf60fc8f44d
--- /dev/null
+++ b/tools/test/stress2/misc/mmap40.sh
@@ -0,0 +1,159 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Attempt to reproduce "vm_page_assert_xbusied: page XXXX not exclusive busy"
+# No problems seen.
+
+# Test scenario idea by markj@
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+[ `sysctl -n vm.swap_total` -eq 0 ] && exit 0
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap40.c
+mycc -o mmap40 -Wall -Wextra -O0 -g mmap40.c || exit 1
+rm -f mmap40.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+newfs $newfs_flags md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+u1=`swapinfo | tail -1 | awk '{print $3}'`
+(nice $odir/../testcases/swap/swap -t 10m -i 30 -h -l 100) &
+while [ $((`swapinfo | tail -1 | awk '{print $3}'` - $u1)) -le 100 ]; do
+ sleep 1
+done
+
+$dir/mmap40
+s=0
+while pkill swap; do :; done
+wait
+[ -f mmap40.core -a $s -eq 0 ] &&
+ { ls -l mmap40.core; mv mmap40.core $dir; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/mmap40
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define PARALLEL 512
+#define RUNTIME 300
+
+void
+test(void)
+{
+ pid_t pid;
+ size_t i, len;
+ time_t start;
+ void *p;
+ char *vec;
+
+ len = 1024 * 1024;
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0)) ==
+ MAP_FAILED)
+ err(1, "mmap");
+ memset(p, 0, len); /* dirty the memory */
+ if ((vec = malloc(len / PAGE_SIZE)) == NULL)
+ err(1, "malloc");
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ usleep(arc4random() % 1000);
+ if (mincore(p, len, vec) == -1)
+ err(1, "mincore");
+ for (i = 0; i < len / PAGE_SIZE; i++) {
+ if ((vec[i] & MINCORE_MODIFIED) == 0) {
+ _exit(0);
+ }
+ }
+ if ((pid = fork()) == 0) {
+ _exit(0);
+ }
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid)");
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ time_t start;
+ int i;
+
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], NULL, WNOHANG) == pids[i]) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ }
+ }
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], NULL, 0) != pids[i])
+ err(1, "waitpid");
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap5.sh b/tools/test/stress2/misc/mmap5.sh
new file mode 100755
index 000000000000..e6dee6d551de
--- /dev/null
+++ b/tools/test/stress2/misc/mmap5.sh
@@ -0,0 +1,129 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Test scenario inspired by alc@
+# "panic: vm_page_dirty: page is invalid!" seen.
+# Fixed in r255396.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap5.c
+mycc -o mmap5 -Wall -Wextra mmap5.c || exit 1
+rm -f mmap5.c
+cd $odir
+
+cp /tmp/mmap5 /tmp/mmap5.inputfile
+(cd ../testcases/swap; ./swap -t 1m -i 2) &
+cp /tmp/mmap5 /tmp/mmap5.inputfile
+/tmp/mmap5 /tmp/mmap5.inputfile
+while killall -9 swap; do
+ sleep .1
+done > /dev/null 2>&1
+wait
+rm -f /tmp/mmap5 /tmp/mmap5.inputfile
+exit
+
+EOF
+#include <err.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+const char *file;
+
+void
+test2(void)
+{
+ struct stat st;
+ char *p;
+ size_t len;
+ int error, fd;
+
+ if ((fd = open(file, O_RDWR)) == -1)
+ err(1, "open %s", file);
+ if ((error = fstat(fd, &st)) == -1)
+ err(1, "stat(%s)", file);
+ len = round_page(st.st_size);
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ p[arc4random() % len] = 1;
+ if (arc4random() % 100 < 50)
+ if ((error = mlock(p, len)) == -1)
+ err(1, "mlock");
+ p[arc4random() % len] = 1;
+ if (arc4random() % 100 < 50)
+ if ((error = msync(p, len, MS_SYNC | MS_INVALIDATE)) == -1)
+ if (errno != EBUSY)
+ err(1, "msync");
+ if (munmap(p, len) == -1)
+ err(1, "unmap()");
+ close(fd);
+ _exit(0);
+
+}
+
+void
+test(void)
+{
+ int i;
+
+ for (i = 0; i < 3; i++)
+ if (fork() == 0)
+ test2();
+ for (i = 0; i < 3; i++)
+ wait(NULL);
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int i;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <file>", argv[0]);
+ file = argv[1];
+
+ for (i = 0; i < 30000; i++) {
+ if (fork() == 0)
+ test();
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap6.sh b/tools/test/stress2/misc/mmap6.sh
new file mode 100755
index 000000000000..e9efbbea5f8a
--- /dev/null
+++ b/tools/test/stress2/misc/mmap6.sh
@@ -0,0 +1,179 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Test scenario inspired by alc@
+# "panic: vm_page_dirty: page is invalid!" seen.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/wire_no_page.c
+mycc -o mmap6 -Wall -Wextra wire_no_page.c || exit 1
+rm -f wire_no_page.c
+cd $odir
+
+cp /tmp/mmap6 /tmp/mmap6.inputfile
+(cd ../testcases/swap; ./swap -t 5m -i 2) &
+cp /tmp/mmap6 /tmp/mmap6.inputfile
+/tmp/mmap6 /tmp/mmap6.inputfile
+while killall -9 swap; do
+ sleep .1
+done > /dev/null 2>&1
+wait
+rm -f /tmp/mmap6 /tmp/mmap6.inputfile
+exit 0
+
+EOF
+#include <sys/param.h>
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define RUNTIME 300
+
+const char *file;
+char c;
+
+void
+rd(void)
+{
+ struct stat st;
+ char *p1, *p2;
+ size_t len;
+ int error, fd;
+
+ if ((fd = open(file, O_RDONLY)) == -1)
+ err(1, "open %s", file);
+ if ((error = fstat(fd, &st)) == -1)
+ err(1, "stat(%s)", file);
+ len = round_page(st.st_size);
+ if ((p1 = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ if ((p2 = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ c = p1[arc4random() % len];
+ c = p2[arc4random() % len];
+ if (arc4random() % 100 < 50)
+ if ((error = mlock(p1, len)) == -1)
+ err(1, "mlock");
+ c = p1[arc4random() % len];
+ if (munmap(p2, len) == -1)
+ err(1, "unmap()");
+ if (munmap(p1, len) == -1)
+ err(1, "unmap()");
+ close(fd);
+
+}
+void
+wr(void)
+{
+ struct stat st;
+ char *p1, *p2;
+ size_t len;
+ int error, fd;
+
+ if ((fd = open(file, O_RDWR)) == -1)
+ err(1, "open %s", file);
+ if ((error = fstat(fd, &st)) == -1)
+ err(1, "stat(%s)", file);
+ len = round_page(st.st_size);
+ if ((p1 = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
+ MAP_FAILED)
+ err(1, "mmap");
+ if ((p2 = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
+ MAP_FAILED)
+ err(1, "mmap");
+ p1[arc4random() % len] = 1;
+ p2[arc4random() % len] = 1;
+ if (arc4random() % 100 < 50)
+ if ((error = mlock(p1, len)) == -1)
+ err(1, "mlock");
+ p1[arc4random() % len] = 1;
+ if (arc4random() % 100 < 50)
+ if ((error = msync(p1, len, MS_SYNC | MS_INVALIDATE)) == -1)
+ if (errno != EBUSY)
+ err(1, "msync");
+ if (munmap(p2, len) == -1)
+ err(1, "unmap()");
+ if (munmap(p1, len) == -1)
+ err(1, "unmap()");
+ close(fd);
+
+}
+
+void
+test2(void)
+{
+ if (arc4random() % 100 < 30)
+ rd();
+ else
+ wr();
+ _exit(0);
+}
+
+void
+test(void)
+{
+ int i;
+
+ for (i = 0; i < 3; i++)
+ if (fork() == 0)
+ test2();
+ for (i = 0; i < 3; i++)
+ wait(NULL);
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ time_t start;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <file>", argv[0]);
+ file = argv[1];
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ if (fork() == 0)
+ test();
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap7.sh b/tools/test/stress2/misc/mmap7.sh
new file mode 100755
index 000000000000..7d467c715203
--- /dev/null
+++ b/tools/test/stress2/misc/mmap7.sh
@@ -0,0 +1,147 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Test scenario inspired by alc@
+# Threaded version in order to "use the same pmap", as pointed out by kib@
+
+# https://people.freebsd.org/~pho/stress/log/kostik601.txt
+# Fixed by r255396
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/wire_no_page.c
+mycc -o mmap7 -Wall -Wextra wire_no_page.c -lpthread || exit 1
+rm -f wire_no_page.c
+cd $odir
+
+(cd ../testcases/swap; ./swap -t 1m -i 2) &
+sleep 1
+cp /tmp/mmap7 /tmp/mmap7.inputfile
+/tmp/mmap7 /tmp/mmap7.inputfile
+while pkill -9 swap; do :; done
+wait
+rm -f /tmp/mmap7 /tmp/mmap7.inputfile
+exit
+
+EOF
+#include <err.h>
+#include <errno.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+char *p1, *p2, *p3;
+char c;
+const char *file;
+int fd;
+size_t len;
+struct stat st;
+
+void *
+test2(void *arg __unused)
+{
+ int error, i;
+
+ p1[arc4random() % len] = 1;
+ p2[arc4random() % len] = 1;
+
+ if (arc4random() % 100 < 30)
+ i = p3[arc4random() % len];
+
+ if (arc4random() % 100 < 50)
+ if ((error = mlock(p1, len)) == -1)
+ err(1, "mlock");
+ if (arc4random() % 100 < 50)
+ if ((error = msync(p1, len, MS_SYNC | MS_INVALIDATE)) == -1)
+ if (errno != EBUSY)
+ err(1, "msync");
+ return (0);
+}
+
+void
+test(void)
+{
+ pthread_t cp[3];
+ int e, error, i;
+
+ if ((fd = open(file, O_RDWR)) == -1)
+ err(1, "open %s", file);
+ if ((error = fstat(fd, &st)) == -1)
+ err(1, "stat(%s)", file);
+ len = round_page(st.st_size);
+ if ((p1 = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ if ((p2 = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ if ((p3 = mmap(NULL, len, PROT_READ , MAP_SHARED, fd, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ for (i = 0; i < 3; i++)
+ if ((e = pthread_create(&cp[i], NULL, test2, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ for (i = 0; i < 3; i++)
+ pthread_join(cp[i], NULL);
+
+ if (munmap(p3, len) == -1)
+ err(1, "unmap()");
+ if (munmap(p2, len) == -1)
+ err(1, "unmap()");
+ if (munmap(p1, len) == -1)
+ err(1, "unmap()");
+ close(fd);
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int i;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <file>", argv[0]);
+ file = argv[1];
+
+ for (i = 0; i < 30000; i++) {
+ if (fork() == 0)
+ test();
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap8.sh b/tools/test/stress2/misc/mmap8.sh
new file mode 100755
index 000000000000..e8fd631ed1e6
--- /dev/null
+++ b/tools/test/stress2/misc/mmap8.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# "panic: vm_fault_copy_wired: page missing" seen:
+# http://people.freebsd.org/~pho/stress/log/mmap8.txt
+# Fixed in r265002.
+
+# Test scenario by: Mark Johnston markj@
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap8.c
+# At one point during the fix development, only the thread version would panic
+mycc -o mmap8 -Wall -Wextra mmap8.c || exit 1
+mycc -o mmap8p -Wall -Wextra mmap8.c -pthread || exit 1
+rm -f mmap8.c
+cd $odir
+
+/tmp/mmap8p
+/tmp/mmap8
+
+rm -f /tmp/mmap8 /tmp/mmap8p
+exit
+
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+
+#include <err.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ size_t sz = 1;
+ char *addr;
+
+/*
+ * This is the minimum amount of C code it takes to panic the kernel.
+ * This is as submitted and thus not a complete and correct test program.
+ */
+ addr = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
+ if (addr == NULL)
+ err(1, "mmap");
+ memset(addr, 0, sz);
+
+ if (fork() != 0) {
+ if (mprotect(addr, sz, PROT_READ))
+ err(1, "mprotect");
+ if (mlock(addr, sz))
+ err(1, "mlock");
+ if (mprotect(addr, sz, PROT_WRITE | PROT_READ))
+ err(1, "mprotect");
+ if (munmap(addr, sz))
+ err(1, "munmap");
+ } else
+ sleep(1);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mmap9.sh b/tools/test/stress2/misc/mmap9.sh
new file mode 100755
index 000000000000..8608df01cb0c
--- /dev/null
+++ b/tools/test/stress2/misc/mmap9.sh
@@ -0,0 +1,95 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# "panic: vm_map_protect: object 0xca6869c0 overcharged" seen:
+# http://people.freebsd.org/~pho/stress/log/mmap9.txt
+# Fixed in r265843
+
+# Test scenario by: Mark Johnston markj@
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/mmap9.c
+# At one point during the fix development, only the thread version would panic
+mycc -o mmap9 -O2 -Wall -Wextra mmap9.c || exit 1
+mycc -o mmap9p -O2 -Wall -Wextra mmap9.c -lpthread || exit 1
+rm -f mmap9.c
+cd $odir
+
+/tmp/mmap9
+/tmp/mmap9p
+
+rm -f /tmp/mmap9 /tmp/mmap9p
+exit
+
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+
+#include <err.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ size_t sz = 1;
+ char *addr;
+
+/*
+ * This is the minimum amount of C code it takes to panic the kernel.
+ * This is as submitted and thus not a complete and correct test program.
+ */
+ addr = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_ANON,
+ -1, 0);
+ if (addr == NULL)
+ err(1, "mmap");
+ memset(addr, 0, sz);
+
+ if (fork() != 0) {
+ if (mlock(addr, sz))
+ err(1, "mlock");
+ if (mprotect(addr, sz, PROT_READ))
+ err(1, "mprotect");
+ if (mprotect(addr, sz, PROT_READ | PROT_WRITE))
+ err(1, "mprotect");
+ if (mprotect(addr, sz, PROT_READ))
+ err(1, "mprotect");
+ if (mprotect(addr, sz, PROT_READ | PROT_WRITE))
+ err(1, "mprotect");
+ } else
+ /*
+ * Ensure that shadow objects aren't collapsed by process exit.
+ */
+ sleep(1);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mount.sh b/tools/test/stress2/misc/mount.sh
new file mode 100755
index 000000000000..4b020ca919bd
--- /dev/null
+++ b/tools/test/stress2/misc/mount.sh
@@ -0,0 +1,82 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Mount regression test
+
+# panic: vm_fault: fault on nofault entry, addr: deadc000
+# cpuid = 1
+# KDB: enter: panic
+# [thread pid 69453 tid 100388 ]
+# Stopped at kdb_enter+0x2b: nop
+# db> where
+# Tracing pid 69453 tid 100388 td 0xc4b5c1b0
+# kdb_enter(c091d9db) at kdb_enter+0x2b
+# panic(c0938fa0,deadc000,e6b44834,c06c650e,c0a57d20,...) at panic+0x14b
+# vm_fault(c1869000,deadc000,1,0) at vm_fault+0x1e0
+# trap_pfault(e6b4499c,0,deadc112) at trap_pfault+0x137
+# trap(8,c0910028,28,deadc0de,deadc0de,...) at trap+0x3f5
+# calltrap() at calltrap+0x5
+# --- trap 0xc, eip = 0xc0667def, esp = 0xe6b449dc, ebp = 0xe6b44a00 ---
+# g_io_request(c66d6bdc,c4a1d840,d7c99940,d7c99940,e6b44a34,...) at g_io_request+0x5f
+# g_vfs_strategy(c40624c4,d7c99940,d7c99940,0,c4e16dec,...) at g_vfs_strategy+0x49
+# ffs_geom_strategy(c40624c4,d7c99940,4ba0,0,c09dad00,...) at ffs_geom_strategy+0x141
+# ufs_strategy(e6b44a7c) at ufs_strategy+0xb5
+# VOP_STRATEGY_APV(c09da7c0,e6b44a7c) at VOP_STRATEGY_APV+0x95
+# bufstrategy(c50d2be0,d7c99940) at bufstrategy+0x55
+# breadn(c50d2b2c,0,0,1000,0,...) at breadn+0xf7
+# bread(c50d2b2c,0,0,1000,0,...) at bread+0x20
+# ffs_read(e6b44bb0) at ffs_read+0x23f
+# VOP_READ_APV(c09da7c0,e6b44bb0) at VOP_READ_APV+0x7e
+# ufs_readdir(e6b44c38) at ufs_readdir+0xd1
+# VOP_READDIR_APV(c09da7c0,e6b44c38) at VOP_READDIR_APV+0x7e
+# getdirentries(c4b5c1b0,e6b44d04) at getdirentries+0x13f
+# syscall(3b,3b,3b,8240160,1,...) at syscall+0x256
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q "$mntpoint" && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+
+# The test:
+
+echo "Expect: mount: /dev/md5a: Device busy"
+mount -r /dev/md${mdstart}$part $mntpoint
+mount -r /dev/md${mdstart}$part $mntpoint
+umount $mntpoint
+
+ls -lR $mntpoint > /dev/null # panic
+
+# End of test
+mount | grep -q "$mntpoint" && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/mount2.sh b/tools/test/stress2/misc/mount2.sh
new file mode 100755
index 000000000000..d4b30bafe711
--- /dev/null
+++ b/tools/test/stress2/misc/mount2.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Stress test by performing parallel calls to mount and umount. Alternate
+# between forced and non-forced unmounts.
+
+# "kernel: g_dev_taste: make_dev_p() failed (gp->name=md10, error=17)" seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mounts=15 # Number of parallel scripts
+D=$diskimage
+
+for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ [ ! -d ${mntpoint}$m ] && mkdir ${mntpoint}$m
+ mount | grep "$mntpoint" | grep -q md$m &&
+ umount ${mntpoint}$m
+ mdconfig -l | grep -q md$m && mdconfig -d -u $m
+
+ dd if=/dev/zero of=$D$m bs=1m count=1 status=none
+ mdconfig -a -t vnode -f $D$m -u $m || { rm -f $D$m; exit 1; }
+ bsdlabel -w md$m auto
+ newfs md${m}$part > /dev/null
+done
+
+# start the parallel tests
+for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ start=`date +%s`
+ while [ $((`date +%s` - start)) -lt 300 ]; do
+ opt=`[ $(( m % 2 )) -eq 0 ] && echo -f`
+ mount /dev/md${m}$part ${mntpoint}$m
+ while mount | grep -q ${mntpoint}$m; do
+ umount $opt ${mntpoint}$m > /dev/null 2>&1
+ done
+ done &
+done
+wait
+
+for i in `jot $mounts`; do
+ m=$((i + mdstart - 1))
+ mdconfig -d -u $m
+ rm -f $D$m
+done
+exit 0
diff --git a/tools/test/stress2/misc/mountro.sh b/tools/test/stress2/misc/mountro.sh
new file mode 100755
index 000000000000..38f6c6e5bd10
--- /dev/null
+++ b/tools/test/stress2/misc/mountro.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2011 Peter Holm <pho@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 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.
+#
+
+# Page fault seen: https://people.freebsd.org/~pho/stress/log/mountro.txt
+# Fixed by r346031
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+D=$diskimage
+dd if=/dev/zero of=$D bs=1m count=128 status=none || exit 1
+
+mount | grep "$mntpoint " | grep -q /md && umount -f $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart || { rm -f $D; exit 1; }
+
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null 2>&1
+mount /dev/md${mdstart}$part $mntpoint
+
+mkdir $mntpoint/stressX
+chmod 777 $mntpoint/stressX
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=4m
+(cd ..; ./run.sh disk.cfg > /dev/null 2>&1) &
+sleep 10
+
+for i in `jot 10`; do
+ mount -u -o ro $mntpoint
+ sleep 3
+ mount -u -o rw $mntpoint
+ sleep 3
+done > /dev/null 2>&1
+
+umount -f $mntpoint
+mdconfig -d -u $mdstart
+rm -f $D
+pkill run.sh
+wait
diff --git a/tools/test/stress2/misc/mountro2.sh b/tools/test/stress2/misc/mountro2.sh
new file mode 100755
index 000000000000..a57c6776fb03
--- /dev/null
+++ b/tools/test/stress2/misc/mountro2.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Test scenario by Matthew D. Fuller <fullermd@over-yonder.net>
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+D=$diskimage
+dd if=/dev/zero of=$D bs=1m count=20 status=none || exit
+
+mount | grep "$mntpoint" | grep -q /md && umount -f $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart || { rm -f $D; exit 1; }
+
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null 2>&1
+mount /dev/md${mdstart}$part $mntpoint
+
+mtree -deU -f /etc/mtree/BSD.usr.dist -p $mntpoint/ >> /dev/null
+sync ; sync ; sync
+
+rm -rf $mntpoint/*
+mount -u -o ro $mntpoint
+
+umount -f $mntpoint > /dev/null 2>&1
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/mountro3.sh b/tools/test/stress2/misc/mountro3.sh
new file mode 100755
index 000000000000..e1749dfdc391
--- /dev/null
+++ b/tools/test/stress2/misc/mountro3.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Test scenario by barbara <barbara.xxx1975@libero.it>
+# kern/121809: unable to umount
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+D=$diskimage
+dd if=/dev/zero of=$D bs=1m count=64 status=none || exit 1
+
+mount | grep "$mntpoint" | grep md${mdstart}$part > /dev/null && umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart || { rm -f $D; exit 1; }
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null 2>&1
+
+mount /dev/md${mdstart}$part $mntpoint
+touch $mntpoint/file
+umount $mntpoint
+
+mount /dev/md${mdstart}$part $mntpoint
+rm $mntpoint/file
+mount -u -o ro $mntpoint # Should fail with "Device busy"
+
+umount $mntpoint
+mdconfig -d -u $mdstart
+rm -f $D
diff --git a/tools/test/stress2/misc/mountro4.sh b/tools/test/stress2/misc/mountro4.sh
new file mode 100755
index 000000000000..b6ff922d5ff9
--- /dev/null
+++ b/tools/test/stress2/misc/mountro4.sh
@@ -0,0 +1,100 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Check that the time stamps are not updated for a RO mount.
+
+. ../default.cfg
+
+f1=$mntpoint/f1
+f2=$mntpoint/f2
+s=0
+
+# ufs
+mount | grep -q "on $mntpoint " && umount $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+newfs $newfs_flags /dev/md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint || exit 1
+touch $f1
+mount -u -o ro $mntpoint
+touch $f2 2>/dev/null && { echo "ufs: ro failed"; s=1; }
+d1=`stat -f '%a %m %c' $f1`
+sleep 1
+cat $f1 > /dev/null
+d2=`stat -f '%a %m %c' $f1`
+if [ "$d1" != "$d2" ]; then
+ echo "ufs: Access time was updated. $d1 != $d2"
+ s=1
+fi
+mount -u -o rw $mntpoint
+touch $f2 2>/dev/null || { echo "ufs: rw failed"; s=1; }
+umount $mntpoint
+mdconfig -d -u $mdstart
+
+# tmpfs
+mount -o size=100m -t tmpfs null $mntpoint || exit 1
+touch $f1
+mount -u -o ro $mntpoint
+touch $f2 2>/dev/null && { echo "tmpfs: ro failed"; s=1; }
+d1=`stat -f '%a %m %c' $f1`
+sleep 1
+cat $f1 > /dev/null
+d2=`stat -f '%a %m %c' $f1`
+if [ "$d1" != "$d2" ]; then
+ echo "tmpfs: Access time was updated. $d1 != $d2"
+ s=1
+fi
+mount -u -o rw $mntpoint
+touch $f2 2>/dev/null || { echo "tmpfs: rw failed"; s=1; }
+umount $mntpoint
+
+# msdosfs
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs_msdos /dev/md${mdstart}$part > /dev/null
+mount -t msdosfs /dev/md${mdstart}$part $mntpoint || exit 1
+
+touch $f1
+mount -u -o ro $mntpoint
+touch $f2 2>/dev/null && { echo "msdosfs: ro failed"; s=1; }
+d1=`stat -f '%a %m %c' $f1`
+sleep 1
+cat $f1 > /dev/null
+d2=`stat -f '%a %m %c' $f1`
+if [ "$d1" != "$d2" ]; then
+ echo "msdosfs: Access time was updated. $d1 != $d2"
+ s=1
+fi
+mount -u -o rw $mntpoint
+touch $f2 2>/dev/null || { echo "msdosfs: rw failed"; s=1; }
+umount $mntpoint
+mdconfig -d -u $mdstart
+
+exit $s
diff --git a/tools/test/stress2/misc/mountro5.sh b/tools/test/stress2/misc/mountro5.sh
new file mode 100755
index 000000000000..67663df54145
--- /dev/null
+++ b/tools/test/stress2/misc/mountro5.sh
@@ -0,0 +1,63 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Toggle a tmpfs file system rw / ro.
+# No problems seen.
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+mount | grep "on $mntpoint " | grep -q /md && umount -f $mntpoint
+mount -t tmpfs null $mntpoint
+
+mkdir $mntpoint/stressX
+chmod 777 $mntpoint/stressX
+
+export RUNDIR=$mntpoint/stressX
+export CTRLDIR=$mntpoint/stressX.control
+export runRUNTIME=4m
+export TESTPROGS="testcases/swap/swap testcases/creat/creat \
+ testcases/mkdir/mkdir testcases/rw/rw"
+(cd ..; ./testcases/run/run $TESTPROGS > /dev/null 2>&1) &
+sleep 10
+
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 60 ]; do
+ mount -fu -o ro $mntpoint
+ sleep .5
+ mount -u -o rw $mntpoint
+ sleep .5
+done
+
+../tools/killall.sh
+wait
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint
+done
diff --git a/tools/test/stress2/misc/mountro6.sh b/tools/test/stress2/misc/mountro6.sh
new file mode 100755
index 000000000000..398cbeed4fea
--- /dev/null
+++ b/tools/test/stress2/misc/mountro6.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Test rw->ro remount when there is a text vnode mapping.
+# Fixed by: r347968
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 512m -u $mdstart
+newfs $newfs_flags md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+cd $mntpoint
+cp /bin/sleep .
+./sleep 20 &
+cd /
+sleep .1
+mount -u -o ro $mntpoint || s=1
+wait
+for i in `jot 5`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 5
+done
+mount | grep -q "on $mntpoint " && { umount -f $mntpoint; s=2; }
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/mountu.sh b/tools/test/stress2/misc/mountu.sh
new file mode 100755
index 000000000000..1c1b619463cd
--- /dev/null
+++ b/tools/test/stress2/misc/mountu.sh
@@ -0,0 +1,284 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2012 Peter Holm <pho@FreeBSD.org>
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Change mount point from rw to ro with a file mapped rw
+# Currently fails for NFS
+
+# Page fault seen:
+# https://people.freebsd.org/~pho/stress/log/mountu.txt
+# Fixed by: r285039.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > mountu.c
+mycc -o mountu -Wall -Wextra -O2 mountu.c || exit 1
+rm -f mountu.c
+
+pstat() {
+ local pid
+ pid=`ps ax | grep -v grep | grep /tmp/mountu | awk '{print $1}'`
+ [ -n "$pid" ] && procstat -v $pid
+}
+
+ck() {
+ if mount | grep $mntpoint | grep -q "read-only"; then
+ if pstat $!| awk "\$2 == \"$map\"" | grep -q " rw-"; then
+ echo
+ echo "$1 difference"
+ mount | grep $mntpoint
+ printf "RW mount mapping and RO mount mapping:\n%s\n" "$r"
+ pstat $! | awk "\$2 == \"$map\""
+ status=$((status + 1))
+ fi
+ else
+ echo "$1 mount point RO did not succeed"
+ mount | grep $mntpoint
+ status=$((status + 1))
+ fi
+}
+
+status=0
+file=$mntpoint/mountu.sh.file
+mapfile=/tmp/mountu.sh.map
+mount | grep -q "$mntpoint " && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 100m -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+# ufs
+exec 5>$mapfile
+/tmp/mountu UFS $file &
+pid=$!
+sleep 1
+map=`cat $mapfile`; rm $mapfile; exec 5>&-
+
+r=`pstat $! | awk "\\$2 == \"$map\""`
+mount -u -o ro $mntpoint 2>/dev/null || mount -fu -o ro $mntpoint
+ck UFS
+mount -u -o rw $mntpoint
+rm -f $file
+wait $pid
+s=$?
+[ $s -ne 139 ] && { echo "UFS exit status is $s"; status=1; }
+while mount | grep -q "$mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+
+# nfs
+if ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1; then
+ mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export \
+ $mntpoint
+ sleep .2
+ rm -f $file
+ /tmp/mountu NFS $file &
+ pid=$!
+ sleep 1
+
+ r=`pstat $! | awk "\\$2 == \"$map\""`
+ mount -u -o ro $mntpoint 2>/dev/null ||
+ mount -fu -o ro $mntpoint 2>/dev/null
+ ck NFS
+ wait $pid
+ s=$?
+ [ $s -ne 139 ] && { echo "NFS exit status is $s"; status=1; }
+
+ mount -u -o rw $mntpoint 2>/dev/null
+ sleep .2
+ [ -f $file ] && rm -f $file
+ umount $mntpoint || umount $mntpoint
+fi
+
+# msdos
+if [ -x /sbin/mount_msdosfs ]; then
+ mdconfig -a -t swap -s 100m -u $mdstart
+ bsdlabel -w md$mdstart auto
+ newfs_msdos -F 16 -b 8192 /dev/md${mdstart}$part > /dev/null 2>&1
+ mount_msdosfs -m 777 /dev/md${mdstart}$part $mntpoint
+ /tmp/mountu MSDOS $file &
+ pid=$!
+
+ sleep 1
+ r=`pstat $! | awk "\\$2 == \"$map\""`
+ mount -u -o ro $mntpoint 2>/dev/null || mount -fu -o ro $mntpoint
+ ck MSDOS
+ wait $pid
+ s=$?
+ [ $s -ne 139 ] && { echo "MSDOS exit status is $s"; status=1; }
+ mount -u -o rw $mntpoint
+ rm -f $file
+
+ while mount | grep -q "$mntpoint "; do
+ umount $mntpoint || sleep 1
+ done
+ mdconfig -d -u $mdstart
+fi
+
+# tmpfs
+mount -t tmpfs null $mntpoint
+chmod 777 $mntpoint
+
+/tmp/mountu TMPFS $file &
+pid=$!
+
+sleep 1
+r=`pstat $! | awk "\\$2 == \"$map\""`
+mount -u -o ro $mntpoint 2>/dev/null || mount -fu -o ro $mntpoint
+ck TMPFS
+sleep 1
+mount -u -o rw $mntpoint
+rm -f $file
+wait $pid
+s=$?
+[ $s -ne 139 ] && { echo "TMPFS exit status is $s"; status=1; }
+while mount | grep -q "$mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+
+rm -f /tmp/mountu
+exit 0
+EOF
+/* kib@ noted:
+ UFS/NFS/msdosfs reclaim vnode on rw->ro forced remount, and
+ change the type of the underying object to OBJT_DEAD, but leave
+ the pages on the object queue and installed in the page tables.
+ Applications can read/write already mapped pages, but cannot
+ page in new pages, cannot observe possible further modifications
+ to already mapped pages (if ro->rw remount happen later), and
+ their updates to pages are not flushed to file.
+
+ It is impossible to mimic this behaviour for tmpfs.
+ */
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/ucontext.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pwd.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define STARTADDR 0x0U
+#define ADRSPACE 0x0640000U
+
+static void
+sighandler(int signo, siginfo_t *si, void *uc1)
+{
+ ucontext_t *uc;
+
+ uc = uc1;
+ printf("SIG%s at %p, addr %p\n", sys_signame[signo], si->si_addr,
+#if defined(__i386__)
+ (void *)uc->uc_mcontext.mc_eip);
+#else
+ (void *)uc->uc_mcontext.mc_rip);
+#endif
+ exit(1);
+}
+
+int
+main(int argc __unused, char **argv)
+{
+ struct passwd *pw;
+ struct sigaction sa;
+ void *p;
+ size_t len;
+ int fd;
+ char *name, *path;
+ volatile char *c;
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_sigaction = sighandler;
+ sa.sa_flags = SA_SIGINFO;
+ if (sigaction(SIGSEGV, &sa, NULL) == -1)
+ err(1, "sigaction(SIGSEGV)");
+ if (sigaction(SIGBUS, &sa, NULL) == -1)
+ err(1, "sigaction(SIGBUS)");
+
+ if ((pw = getpwnam("nobody")) == NULL)
+ err(1, "no such user: nobody");
+
+ if (setgroups(1, &pw->pw_gid) ||
+ setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
+ seteuid(pw->pw_uid) || setuid(pw->pw_uid))
+ err(1, "Can't drop privileges to \"nobody\"");
+ endpwent();
+
+ p = (void *)STARTADDR;
+ len = ADRSPACE;
+
+ name = argv[1];
+ path = argv[2];
+ if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0622)) == -1)
+ err(1,"open(%s)", path);
+ if (ftruncate(fd, len) == -1)
+ err(1, "ftruncate");
+ if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
+ MAP_FAILED) {
+ if (errno == ENOMEM) {
+ warn("mmap()");
+ return (1);
+ }
+ err(1, "mmap(1)");
+ }
+ dprintf(5, "%p\n", p);
+
+ for (c = p; (void *)c < p + len; c += PAGE_SIZE) {
+ *c = 1;
+ }
+
+ close(fd);
+ sleep(5);
+ fprintf(stderr, "%s: Late read start.\n", name);
+ for (c = p; (void *)c < p + len; c += PAGE_SIZE) {
+ *c;
+ }
+ fprintf(stderr, "%s: Late read complete.\n", name);
+
+ fprintf(stderr, "%s: Late write start.\n", name);
+ for (c = p; (void *)c < p + len; c += PAGE_SIZE) {
+ *c = 1;
+ }
+ fprintf(stderr, "%s: Late write complete.\n", name);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/mprotect.sh b/tools/test/stress2/misc/mprotect.sh
new file mode 100755
index 000000000000..9fde68de56ec
--- /dev/null
+++ b/tools/test/stress2/misc/mprotect.sh
@@ -0,0 +1,75 @@
+#!/bin/sh
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Mark Johnston <markj@freebsd.org>
+#
+# 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 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.
+#
+
+# "panic: pmap_demote_pde: page table page for a wired mapping
+# is missing" seen.
+
+# Fixed by r345382
+
+. ../default.cfg
+
+cd /tmp
+cat > mprotect.c <<EOF
+#include <sys/mman.h>
+
+#include <err.h>
+#include <stdlib.h>
+
+int
+main(void)
+{
+ char *addr, c;
+ size_t i, len;
+
+ len = 2 * 1024 * 1024;
+ addr = mmap(NULL, 2 * 1024 * 1024, PROT_READ,
+ MAP_ANON | MAP_ALIGNED_SUPER, -1, 0);
+ if (addr == MAP_FAILED)
+ err(1, "mmap");
+ if (mlock(addr, len) != 0) /* hopefully this gets a superpage */
+ err(1, "mlock");
+ if (mprotect(addr, len, PROT_NONE) != 0)
+ err(1, "mprotect");
+ if (mprotect(addr, len, PROT_READ) != 0)
+ err(1, "mprotect");
+ for (i = 0; i < len; i++) /* preemptive superpage mapping */
+ c = *(volatile char *)(addr + i);
+ if (mprotect(addr, 4096, PROT_NONE) != 0) /* trigger demotion */
+ err(1, "mprotect");
+ if (munlock(addr, len) != 0)
+ err(1, "munlock");
+
+ return (0);
+}
+EOF
+mycc -o mprotect -Wall -Wextra -O2 mprotect.c || exit 1
+
+./mprotect; s=$?
+
+rm mprotect.c mprotect
+exit $s
diff --git a/tools/test/stress2/misc/mprotect2.sh b/tools/test/stress2/misc/mprotect2.sh
new file mode 100755
index 000000000000..c5cdc44a0f39
--- /dev/null
+++ b/tools/test/stress2/misc/mprotect2.sh
@@ -0,0 +1,97 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Test scenario suggestion by alc@
+
+. ../default.cfg
+
+cd /tmp
+cat > mprotect2.c <<EOF
+/*
+ For example, write a program that mmap(MAP_ANON)'s a gigabyte of
+ PROT_WRITE virtual address space, iterates over that space removing
+ PROT_WRITE on every other page, and then measure the time to perform a
+ single mprotect() restoring PROT_WRITE to the entire gigabyte of virtual
+ address space.
+ */
+
+#include <sys/param.h>
+#include <sys/mman.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define SIZ 0x40000000LL /* 1GB */
+
+static void
+test(void)
+{
+ size_t i, len;
+ char *cp;
+
+ len = SIZ;
+ if ((cp = mmap(NULL, len, PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0))
+ == MAP_FAILED)
+ err(1, "mmap");
+
+ for (i = 0; i < SIZ; i += PAGE_SIZE * 2) {
+ if (mprotect(cp + i, PAGE_SIZE, 0) != 0)
+ err(1, "mprotect(). %d", __LINE__);
+ }
+ if (mprotect(cp, SIZ, PROT_WRITE) != 0)
+ err(1, "mprotect(). %d", __LINE__);
+
+ if (munmap(cp, SIZ) == -1)
+ err(1, "munmap()");
+}
+
+int
+main(void)
+{
+ int i;
+
+ /* Slow run with debug.vmmap_check=1 */
+ alarm(120);
+
+ for (i = 0; i < 64; i++)
+ test();
+
+ return (0);
+}
+EOF
+mycc -o mprotect2 -Wall -Wextra -O2 mprotect2.c || exit 1
+
+./mprotect2; s=$?
+
+rm mprotect2.c mprotect2
+exit $s
diff --git a/tools/test/stress2/misc/msdos.sh b/tools/test/stress2/misc/msdos.sh
new file mode 100755
index 000000000000..2324404bfd16
--- /dev/null
+++ b/tools/test/stress2/misc/msdos.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2010 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# LOR seen. Fixed by r204467.
+
+. ../default.cfg
+
+[ -x /sbin/mount_msdosfs ] || exit
+log=/tmp/msdos.sh.log
+mount | grep "$mntpoint" | grep -q md$mdstart && umount -f $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs_msdos /dev/md${mdstart}$part > /dev/null
+mount -t msdosfs /dev/md${mdstart}$part $mntpoint
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=10m # Run tests for 10 minutes
+(cd ..; ./run.sh disk.cfg)
+
+while mount | grep "$mntpoint" | grep -q md$mdstart; do
+ umount $mntpoint || sleep 1
+done
+fsck -t msdosfs -y /dev/md${mdstart}$part > $log 2>&1
+s=0
+if egrep -q "BAD|INCONSISTENCY|MODIFIED" $log; then
+ cat $log
+ rm $log
+ s=1
+fi
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/msdos10.sh b/tools/test/stress2/misc/msdos10.sh
new file mode 100755
index 000000000000..09613a859082
--- /dev/null
+++ b/tools/test/stress2/misc/msdos10.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# fsx test of with msdosfs and a 1k block size
+# "panic: Assertion ma[i]->dirty == VM_PAGE_BITS_ALL failed" seen.
+# Fixed by r324794
+
+# Original test scenario by fsu@freebsd.org
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+[ -r /usr/src/tools/regression/fsx/fsx.c ] || exit 0
+
+[ -x /sbin/mount_msdosfs ] || exit 0
+dir=/tmp
+odir=`pwd`
+cd $dir
+cc -o fsx -Wall -Wextra -O2 -g /usr/src/tools/regression/fsx/fsx.c || exit 1
+rm -f fsx.c
+cd $odir
+log=/tmp/fsx.sh.log
+mount | grep "$mntpoint" | grep -q md$mdstart && umount -f $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+set -e
+mdconfig -a -t swap -s 4g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs_msdos -b 1024 /dev/md${mdstart}$part > /dev/null
+mount -t msdosfs /dev/md${mdstart}$part $mntpoint
+set +e
+
+cp /tmp/fsx $mntpoint
+
+cd $mntpoint
+
+NUM_OPS=2000
+SEED=2016
+(
+./fsx -S ${SEED} -N ${NUM_OPS} ./TEST_FILE0 &
+./fsx -S ${SEED} -l 5234123 -o 5156343 -N ${NUM_OPS} ./TEST_FILE1 &
+./fsx -S ${SEED} -l 2311244 -o 2311200 -N ${NUM_OPS} ./TEST_FILE2 &
+./fsx -S ${SEED} -l 8773121 -o 863672 -N ${NUM_OPS} ./TEST_FILE3 &
+./fsx -S ${SEED} -l 234521 -o 234521 -N ${NUM_OPS} ./TEST_FILE4 &
+./fsx -S ${SEED} -l 454321 -o 33 -N ${NUM_OPS} ./TEST_FILE5 &
+./fsx -S ${SEED} -l 7234125 -o 7876728 -N ${NUM_OPS} ./TEST_FILE6 &
+wait
+) > /dev/null
+cd /
+
+while mount | grep "$mntpoint" | grep -q md$mdstart; do
+ umount $mntpoint || sleep 1
+done
+fsck -t msdosfs -y /dev/md${mdstart}$part > $log 2>&1
+if egrep -q "BAD|INCONSISTENCY|MODIFIED" $log; then
+ cat $log
+ s=1
+
+ mount -t msdosfs /dev/md${mdstart}$part $mntpoint || exit 1
+ ls -lR $mntpoint
+ umount $mntpoint
+fi
+mdconfig -d -u $mdstart
+rm /tmp/fsx $log
+exit $s
diff --git a/tools/test/stress2/misc/msdos2.sh b/tools/test/stress2/misc/msdos2.sh
new file mode 100755
index 000000000000..838585595ec3
--- /dev/null
+++ b/tools/test/stress2/misc/msdos2.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# panic: __lockmgr_args: recursing on non recursive lockmgr devfs @ ../../../kern/vfs_subr.c:2204
+# Scenario by kib@
+
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -x /sbin/mount_msdosfs ] || exit
+mount | grep "$mntpoint" | grep -q md$mdstart && umount -f $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs_msdos /dev/md${mdstart}$part > /dev/null
+mount -t msdosfs /dev/md${mdstart}$part $mntpoint
+
+u=$((mdstart + 1))
+mdconfig -l | grep -q $u && mdconfig -d -u $u
+mdconfig -a -t swap -s 1g -u $u
+bsdlabel -w md$u auto
+newfs_msdos /dev/md${u}$part > /dev/null
+mount -u /dev/md${u}$part $mntpoint > /dev/null 2>&1 # panic
+
+ls $mntpoint > /dev/null
+
+while mount | grep "$mntpoint" | grep -q md$mdstart; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+mdconfig -d -u $u
diff --git a/tools/test/stress2/misc/msdos3.sh b/tools/test/stress2/misc/msdos3.sh
new file mode 100755
index 000000000000..e251f7bf7ee7
--- /dev/null
+++ b/tools/test/stress2/misc/msdos3.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# Page fault seen
+# Scenario by kib@
+
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -x /sbin/mount_msdosfs ] || exit
+mount | grep "$mntpoint" | grep -q md$mdstart && umount -f $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs_msdos /dev/md${mdstart}$part > /dev/null
+
+mount -t msdosfs /dev/md${mdstart}$part $mntpoint
+mount -t msdosfs /dev/md${mdstart}$part $mntpoint || echo OK
+
+ls $mntpoint > /dev/null
+
+while mount | grep "$mntpoint" | grep -q md$mdstart; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/msdos4.sh b/tools/test/stress2/misc/msdos4.sh
new file mode 100755
index 000000000000..807c3b5206eb
--- /dev/null
+++ b/tools/test/stress2/misc/msdos4.sh
@@ -0,0 +1,76 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "panic: leaf should be empty" seen
+
+# "panic: Assertion sq->sq_wchan != NULL" seen:
+# https://people.freebsd.org/~pho/stress/log/msdos4.txt
+
+. ../default.cfg
+
+[ -x /sbin/mount_msdosfs ] || exit
+log=/tmp/msdos4.sh.log
+mount | grep "$mntpoint" | grep -q md$mdstart && umount -f $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs_msdos /dev/md${mdstart}$part > /dev/null
+mount -t msdosfs /dev/md${mdstart}$part $mntpoint || exit 1
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=10m
+export TESTPROGS='
+testcases/lockf2/lockf2
+testcases/openat/openat
+testcases/rw/rw
+testcases/fts/fts
+testcases/lockf/lockf
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/swap/swap
+'
+
+(cd ..; ./testcases/run/run $TESTPROGS)
+
+while mount | grep "$mntpoint" | grep -q md$mdstart; do
+ umount $mntpoint || sleep 1
+done
+fsck -t msdosfs -y /dev/md${mdstart}$part > $log 2>&1
+s=0
+if egrep -q "BAD|INCONSISTENCY|MODIFIED" $log; then
+ cat $log
+ rm $log
+ s=1
+fi
+mdconfig -d -u $mdstart
+s=0 # Ignore for now
+exit $s
diff --git a/tools/test/stress2/misc/msdos5.sh b/tools/test/stress2/misc/msdos5.sh
new file mode 100755
index 000000000000..815eeca6fd4c
--- /dev/null
+++ b/tools/test/stress2/misc/msdos5.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# "panic: Freeing unused sector 79510 22 ff800000" seen.
+# http://people.freebsd.org/~pho/stress/log/msdos5.txt
+# FS corruption seen: http://people.freebsd.org/~pho/stress/log/msdos5-2.txt
+# Fixed by r333693.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -x /sbin/mount_msdosfs ] || exit
+
+. ../default.cfg
+
+mycc -o /tmp/fstool -Wall -Wextra -O2 ../tools/fstool.c || exit 1
+
+cd /tmp
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 3g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs_msdos -F 32 -b 8192 /dev/md${mdstart}$part > /dev/null 2>&1
+mount_msdosfs -m 777 /dev/md${mdstart}$part $mntpoint
+
+for i in `jot 5`; do
+ (mkdir $mntpoint/test$i; cd $mntpoint/test$i; /tmp/fstool -l -f 400 -n 200 -s ${i}k)
+done
+
+sleep 1
+rm -rf $mntpoint/* || echo FAIL
+
+while mount | grep -q "$mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+rm -f /tmp/fstool
diff --git a/tools/test/stress2/misc/msdos6.sh b/tools/test/stress2/misc/msdos6.sh
new file mode 100755
index 000000000000..475989b3fa1c
--- /dev/null
+++ b/tools/test/stress2/misc/msdos6.sh
@@ -0,0 +1,93 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC Isilon
+# 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 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.
+#
+
+# Parallel mount and umount of file systems
+
+# "panic: userret: Returning with 1 locks held" seen:
+# https://people.freebsd.org/~pho/stress/log/mark165.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mounts=15 # Number of parallel scripts
+cont=/tmp/msdos6.continue
+mdstart=$mdstart # Use md unit numbers from this point
+
+if [ $# -eq 0 ]; then
+ touch $cont
+ mycc -o /tmp/fstool -Wall -Wextra -O2 ../tools/fstool.c || exit 1
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ [ ! -d ${mntpoint}$m ] && mkdir ${mntpoint}$m
+ mount | grep "$mntpoint" | grep -q md$m && umount ${mntpoint}$m
+ mdconfig -l | grep -q md$m && mdconfig -d -u $m
+
+ mdconfig -a -t swap -s 1g -u $m
+ bsdlabel -w md$m auto
+ newfs_msdos -F 32 -b 8192 /dev/md${m}$part > /dev/null 2>&1
+ mount -t msdosfs /dev/md${m}$part ${mntpoint}$m
+ (mkdir ${mntpoint}$m/test$i; cd ${mntpoint}$m/test$i; /tmp/fstool -l -f 100 -n 100 -s ${i}k)
+ umount ${mntpoint}$m > /dev/null 2>&1
+ done
+
+ # start the parallel tests
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ ./$0 $m &
+ ./$0 find &
+ done
+
+ wait
+
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ mdconfig -d -u $m
+ done
+ rm -f /tmp/fstool
+
+else
+ if [ $1 = find ]; then
+ while [ -r $cont ]; do
+ find ${mntpoint}* -type f > /dev/null 2>&1
+ done
+ else
+
+ # The test: Parallel mount and unmounts
+ start=`date '+%s'`
+ while [ $((`date '+%s'` - start)) -lt 300 ]; do
+ m=$1
+ mount -t msdosfs /dev/md${m}$part ${mntpoint}$m
+ while mount | grep -qw $mntpoint$m; do
+ opt=$([ $((`date '+%s'` % 2)) -eq 0 ] && echo "-f")
+ umount $opt ${mntpoint}$m > /dev/null 2>&1
+ done
+ done
+ rm -f $cont
+ fi
+fi
diff --git a/tools/test/stress2/misc/msdos7.sh b/tools/test/stress2/misc/msdos7.sh
new file mode 100755
index 000000000000..7bebe3703848
--- /dev/null
+++ b/tools/test/stress2/misc/msdos7.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC Isilon
+# 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 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.
+#
+
+# Run misc/datamove.sh on a msdos fs.
+# Thread waiting on "msdosfs".
+# https://people.freebsd.org/~pho/stress/log/kostik958.txt
+# Fixed by r308025.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q "on $mntpoint " && umount $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs_msdos -F 32 -b 8192 /dev/md${mdstart}$part > /dev/null || exit 1
+mount -t msdosfs /dev/md${mdstart}$part $mntpoint
+
+here=`pwd`
+cd /tmp
+sed "1,/^EOF/d" < $here/datamove.sh > msdos7.c
+mycc -o msdos7 -Wall msdos7.c
+rm -f msdos7.c
+
+(cd $mntpoint; /tmp/msdos7)
+
+umount $mntpoint
+mdconfig -d -u $mdstart
+rm -f /tmp/msdos7
+exit 0
diff --git a/tools/test/stress2/misc/msdos8.sh b/tools/test/stress2/misc/msdos8.sh
new file mode 100755
index 000000000000..58f0390e195d
--- /dev/null
+++ b/tools/test/stress2/misc/msdos8.sh
@@ -0,0 +1,157 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# msdosfs rename scenario
+# "Invalid long filename entry" seen from fsck
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+[ -x /sbin/mount_msdosfs ] || exit 0
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/msdos8.c
+cc -o msdos8 -Wall -Wextra -O0 -g msdos8.c || exit 1
+rm -f msdos8.c
+cd $odir
+log=/tmp/msdos8.sh.log
+mount | grep "$mntpoint" | grep -q md$mdstart && umount -f $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs_msdos /dev/md${mdstart}$part #> /dev/null
+mount -t msdosfs /dev/md${mdstart}$part $mntpoint || exit 1
+
+(cd $mntpoint; /tmp/msdos8)
+s=$?
+
+while mount | grep "$mntpoint" | grep -q md$mdstart; do
+ umount $mntpoint || sleep 1
+done
+fsck -t msdosfs -y /dev/md${mdstart}$part > $log 2>&1
+if egrep -q "BAD|INCONSISTENCY|MODIFIED" $log; then
+ cat $log
+ s=1
+
+ mount -t msdosfs /dev/md${mdstart}$part $mntpoint || exit 1
+ ls -lR $mntpoint
+ umount $mntpoint
+fi
+mdconfig -d -u $mdstart
+rm /tmp/msdos8 $log
+s=0 # Ignore for now
+exit $s
+EOF
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+# define PARALLEL 10
+
+static unsigned long size;
+
+static void
+test(void)
+{
+ pid_t pid;
+ int fd, i, j;
+ char file1[128], file2[128];
+
+ pid = getpid();
+ for (i = 0; i < (int)size; i++) {
+ sprintf(file1,"p%05d.%05d", pid, i);
+ if ((fd = open(file1, O_RDONLY|O_CREAT, 0660)) == -1)
+ err(1, "openat(%s), %s:%d", file1, __FILE__,
+ __LINE__);
+ close(fd);
+ }
+ for (j = 0; j < 100; j++) {
+ for (i = 0; i < (int)size; i++) {
+ sprintf(file1,"p%05d.%05d", pid, i);
+ sprintf(file2,"p%05d.%05d.togo", pid, i);
+ if (rename(file1, file2) == -1)
+ err(1, "rename(%s, %s). %s:%d", file1,
+ file2, __FILE__, __LINE__);
+ }
+ for (i = 0; i < (int)size; i++) {
+ sprintf(file1,"p%05d.%05d", pid, i);
+ sprintf(file2,"p%05d.%05d.togo", pid, i);
+ if (rename(file2, file1) == -1)
+ err(1, "rename(%s, %s). %s:%d", file2,
+ file1, __FILE__, __LINE__);
+ }
+ }
+
+ for (i = 0; i < (int)size; i++) {
+ sprintf(file1,"p%05d.%05d", pid, i);
+ if (unlink(file1) == -1)
+ err(1, "unlink(%s), %s:%d", file1, __FILE__,
+ __LINE__);
+ }
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ size = 5;
+ start = time(NULL);
+ while ((time(NULL) - start) < 60 && e == 0) {
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (WIFSIGNALED(status))
+ fprintf(stderr, "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/msdos9.sh b/tools/test/stress2/misc/msdos9.sh
new file mode 100755
index 000000000000..cafabc546aa9
--- /dev/null
+++ b/tools/test/stress2/misc/msdos9.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# fsx test of with msdosfs and a 1k block size
+# "panic: Assertion ma[i]->dirty == VM_PAGE_BITS_ALL failed" seen.
+# Fixed by r324794
+
+# Original test scenario by fsu@freebsd.org
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+[ -r /usr/src/tools/regression/fsx/fsx.c ] || exit 0
+
+[ -x /sbin/mount_msdosfs ] || exit 0
+dir=/tmp
+odir=`pwd`
+cd $dir
+cc -o fsx -Wall -Wextra -O2 -g /usr/src/tools/regression/fsx/fsx.c || exit 1
+rm -f fsx.c
+cd $odir
+log=/tmp/fsx.sh.log
+mount | grep "$mntpoint" | grep -q md$mdstart && umount -f $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+set -e
+mdconfig -a -t swap -s 4g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs_msdos -b 1024 /dev/md${mdstart}$part > /dev/null
+mount -t msdosfs /dev/md${mdstart}$part $mntpoint
+set +e
+
+cp /tmp/fsx $mntpoint
+
+cd $mntpoint
+./fsx -S 2016 -N 2000 ./TEST_FILE > /dev/null
+cd /
+
+while mount | grep "$mntpoint" | grep -q md$mdstart; do
+ umount $mntpoint || sleep 1
+done
+fsck -t msdosfs -y /dev/md${mdstart}$part > $log 2>&1
+if egrep -q "BAD|INCONSISTENCY|MODIFIED" $log; then
+ cat $log
+ s=1
+
+ mount -t msdosfs /dev/md${mdstart}$part $mntpoint || exit 1
+ ls -lR $mntpoint
+ umount $mntpoint
+fi
+mdconfig -d -u $mdstart
+rm /tmp/fsx $log
+exit $s
diff --git a/tools/test/stress2/misc/msetdomain.sh b/tools/test/stress2/misc/msetdomain.sh
new file mode 100755
index 000000000000..53e792be52e5
--- /dev/null
+++ b/tools/test/stress2/misc/msetdomain.sh
@@ -0,0 +1,144 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# msetdomain(2) fuzz test.
+# No problems seen.
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+nm /usr/lib/libc.a | grep -q __sys_msetdomain || exit 0
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/msetdomain.c
+mycc -o msetdomain -Wall -Wextra -O0 -g msetdomain.c || exit 1
+rm -f msetdomain.c
+cd $odir
+
+$dir/msetdomain
+s=$?
+[ -f msetdomain.core -a $s -eq 0 ] &&
+ { ls -l msetdomain.core; mv msetdomain.core /tmp; s=1; }
+rm -rf $dir/msetdomain
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/resource.h>
+#include <sys/cpuset.h>
+#include <sys/domainset.h>
+
+#include <err.h>
+#include <stdlib.h>
+#include <time.h>
+
+/*
+struct msetdomain_args {
+ void *addr;
+ size_t size;
+ size_t domainsetsize;
+ domainset_t *mask;
+ int policy;
+ int flags;
+*/
+
+static long
+random_long(long mi, long ma)
+{
+ return (arc4random() % (ma - mi + 1) + mi);
+}
+
+void
+flip(void *ap, size_t len)
+{
+ unsigned char *cp;
+ int byte;
+ unsigned char bit, buf, mask, old;
+
+ cp = (unsigned char *)ap;
+ byte = random_long(0, len);
+ bit = random_long(0,7);
+ mask = ~(1 << bit);
+ buf = cp[byte];
+ old = cp[byte];
+ buf = (buf & mask) | (~buf & ~mask);
+ cp[byte] = buf;
+}
+
+int
+main(void)
+{
+ size_t len;
+ time_t start;
+ void *share;
+ domainset_t rootmask;
+ int flags, policy;
+
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ if (cpuset_getdomain(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
+ sizeof(rootmask), &rootmask, &policy) != 0)
+ err(EXIT_FAILURE, "getdomain");
+
+ flags = 0;
+ flip(&flags, sizeof(flags));
+ msetdomain(share, len, sizeof(rootmask), &rootmask, policy,
+ flags);
+ }
+
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ if (cpuset_getdomain(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
+ sizeof(rootmask), &rootmask, &policy) != 0)
+ err(EXIT_FAILURE, "getdomain");
+
+ flip(&policy, sizeof(policy));
+ msetdomain(share, len, sizeof(rootmask), &rootmask, policy,
+ flags);
+ }
+
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ if (cpuset_getdomain(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
+ sizeof(rootmask), &rootmask, &policy) != 0)
+ err(EXIT_FAILURE, "getdomain");
+
+ flip(&rootmask, sizeof(rootmask));
+ msetdomain(share, len, sizeof(rootmask), &rootmask, policy,
+ flags);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/msync.sh b/tools/test/stress2/misc/msync.sh
new file mode 100755
index 000000000000..326c7e723774
--- /dev/null
+++ b/tools/test/stress2/misc/msync.sh
@@ -0,0 +1,201 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# msync(2) / mlockall(2) test scenario.
+# "panic: vm_fault_copy_wired: page missing" seen.
+# http://people.freebsd.org/~pho/stress/log/msync.txt
+# Fixed in r253189.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/msync.c
+mycc -o msync -Wall -Wextra msync.c -lpthread || exit 1
+rm -f msync.c
+cd $odir
+
+/tmp/msync &
+sleep 180
+while pkill -9 msync; do :; done
+wait
+rm -f /tmp/msync
+exit
+
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <signal.h>
+#include <sys/mman.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+int syscallno = SYS_msync;
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+u_int32_t r[N];
+
+static void
+hand(int i __unused) { /* handler */
+ _exit(1);
+}
+
+unsigned long
+makearg(void)
+{
+ unsigned int i;
+ unsigned long val;
+
+ val = arc4random();
+ i = arc4random() % 100;
+ if (i < 20)
+ val = val & 0xff;
+ if (i >= 20 && i < 40)
+ val = val & 0xffff;
+ if (i >= 40 && i < 60)
+ val = (unsigned long)(r) | (val & 0xffff);
+#if defined(__LP64__)
+ if (i >= 60) {
+ val = (val << 32) | arc4random();
+ if (i > 80)
+ val = val & 0x00007fffffffffffUL;
+ }
+#endif
+
+ return(val);
+}
+
+void *
+calls(void *arg __unused)
+{
+ int i, num;
+ unsigned long arg1, arg2, arg3;
+
+ usleep(1000);
+ num = syscallno;
+ for (i = 0; i < 500; i++) {
+ arg1 = makearg();
+ arg2 = makearg();
+#if 0
+ arg3 = makearg();
+ arg3 = arg3 & ~MS_INVALIDATE; /* No problem seen */
+#else
+ arg3 = MS_INVALIDATE; /* panic */
+#endif
+
+#if 0
+ fprintf(stderr, "%2d : syscall(%3d, 0x%lx, 0x%lx, 0x%lx)\n",
+ i, num, arg1, arg2, arg3);
+ usleep(50000);
+#endif
+ alarm(1);
+ syscall(num, arg1, arg2, arg3);
+ num = 0;
+ }
+
+ return (0);
+}
+void
+wd(void)
+{
+ int i;
+
+ if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0)
+ err(1, "mlockall failed");
+
+ for (i = 0; i < 800; i++) {
+ if (fork() == 0) {
+ usleep(20000);
+ _exit(0);
+ }
+ wait(NULL);
+ usleep(100000);
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ struct passwd *pw;
+ pthread_t cp[50];
+ int e, i, j;
+
+ if (fork() == 0)
+ wd();
+
+ if ((pw = getpwnam("nobody")) == NULL)
+ err(1, "no such user: nobody");
+
+ if (setgroups(1, &pw->pw_gid) ||
+ setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
+ seteuid(pw->pw_uid) || setuid(pw->pw_uid))
+ err(1, "Can't drop privileges to \"nobody\"");
+ endpwent();
+
+ signal(SIGALRM, hand);
+ signal(SIGILL, hand);
+ signal(SIGFPE, hand);
+ signal(SIGSEGV, hand);
+ signal(SIGBUS, hand);
+ signal(SIGURG, hand);
+ signal(SIGSYS, hand);
+ signal(SIGTRAP, hand);
+
+ alarm(180);
+ for (i = 0; i < 8000; i++) {
+ if (fork() == 0) {
+ for (j = 0; j < N; j++)
+ r[j] = arc4random();
+ for (j = 0; j < 50; j++)
+ if ((e = pthread_create(&cp[j], NULL, calls, NULL)) != 0)
+ errc(1, e, "pthread_create");
+
+ for (j = 0; j < 50; j++)
+ pthread_join(cp[j], NULL);
+ _exit(0);
+ }
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/msync2.sh b/tools/test/stress2/misc/msync2.sh
new file mode 100755
index 000000000000..c1a9e4f0e372
--- /dev/null
+++ b/tools/test/stress2/misc/msync2.sh
@@ -0,0 +1,131 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# "panic: vm_pageout_flush: partially invalid page xx index 0/1" seen.
+# Fixed in r255566.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/msync2.c
+mycc -o msync2 -Wall -Wextra msync2.c || exit 1
+rm -f msync2.c
+cd $odir
+
+dd if=/dev/zero bs=$((4096 + 1)) of=/tmp/msync2.inputfile count=1 \
+ status=none
+/tmp/msync2 /tmp/msync2.inputfile
+rm -f /tmp/msync2 /tmp/msync2.inputfile
+exit
+
+EOF
+#include <sys/param.h>
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define RUNTIME 400
+
+const char *file;
+char c;
+
+void
+wr(void)
+{
+ struct stat st;
+ char *p1;
+ size_t len;
+ int error, fd;
+
+ if ((fd = open(file, O_RDWR)) == -1)
+ err(1, "open %s", file);
+ if ((error = fstat(fd, &st)) == -1)
+ err(1, "stat(%s)", file);
+ len = round_page(st.st_size);
+ if ((p1 = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
+ MAP_FAILED)
+ err(1, "mmap");
+// p1[len - 1] = 1; /* No panic with this */
+ p1[arc4random() % len] = 1; /* Need this for the panic */
+
+ if ((error = msync(p1, len, MS_SYNC | MS_INVALIDATE)) == -1)
+ if (errno != EBUSY)
+ err(1, "msync");
+
+ if (munmap(p1, len) == -1)
+ err(1, "unmap()");
+ close(fd);
+
+ _exit(0);
+}
+
+void
+test(void)
+{
+#if 1
+ int i;
+
+ for (i = 0; i < 3; i++)
+ if (fork() == 0)
+ wr();
+ for (i = 0; i < 3; i++)
+ wait(NULL);
+#else
+ wr(); /* No problem here */
+#endif
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ time_t start;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <file>", argv[0]);
+ file = argv[1];
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ if (fork() == 0)
+ test();
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/multicast.sh b/tools/test/stress2/misc/multicast.sh
new file mode 100755
index 000000000000..d95a55cef918
--- /dev/null
+++ b/tools/test/stress2/misc/multicast.sh
@@ -0,0 +1,118 @@
+#!/bin/sh
+
+# Multicast test example by Mark Claypool, claypool at cs.wpi.edu
+# https://web.cs.wpi.edu/~claypool/courses/4514-B99/samples/multicast.c
+
+# Kernel page fault seen with WiP branch:
+# https://people.freebsd.org/~pho/stress/log/kip036.txt
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/multicast.c
+mycc -o multicast -Wall -Wextra -O0 -g multicast.c || exit 1
+rm -f multicast.c
+cd $odir
+
+cd $dir
+(
+ timeout -k 1s 20s ./multicast &
+ sleep 1
+ timeout -k 1s 25s ./multicast 1
+) > /dev/null
+wait
+
+rm -f $dir/multicast
+exit $s
+EOF
+/*
+multicast.c
+
+The following program sends or receives multicast packets. If invoked
+with one argument, it sends a packet containing the current time to an
+arbitrarily chosen multicast group and UDP port. If invoked with no
+arguments, it receives and prints these packets. Start it as a sender on
+just one host and as a receiver on all the other hosts
+
+*/
+
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <string.h>
+#include <time.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define EXAMPLE_PORT 6000
+#define EXAMPLE_GROUP "239.0.0.1"
+
+int
+main(int argc, char *argv[] __unused)
+{
+ struct ip_mreq mreq;
+ struct sockaddr_in addr;
+ socklen_t addrlen;
+ int cnt, sock;
+ char message[50];
+
+ /* set up socket */
+ sock = socket(AF_INET, SOCK_DGRAM, 0);
+ if (sock < 0) {
+ perror("socket");
+ exit(1);
+ }
+ bzero((char *)&addr, sizeof(addr));
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = htonl(INADDR_ANY);
+ addr.sin_port = htons(EXAMPLE_PORT);
+ addrlen = sizeof(addr);
+
+ if (argc > 1) {
+ /* send */
+ addr.sin_addr.s_addr = inet_addr(EXAMPLE_GROUP);
+ while (1) {
+ time_t t = time(0);
+ sprintf(message, "time is %-24.24s", ctime(&t));
+ printf("sending: %s\n", message);
+ cnt = sendto(sock, message, sizeof(message), 0,
+ (struct sockaddr *) &addr, addrlen);
+ if (cnt < 0) {
+ perror("sendto");
+ exit(1);
+ }
+ sleep(5);
+ }
+ } else {
+
+ /* receive */
+ if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+ perror("bind");
+ exit(1);
+ }
+ mreq.imr_multiaddr.s_addr = inet_addr(EXAMPLE_GROUP);
+ mreq.imr_interface.s_addr = htonl(INADDR_ANY);
+ if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
+ &mreq, sizeof(mreq)) < 0) {
+ perror("setsockopt mreq");
+ exit(1);
+ }
+ while (1) {
+ cnt = recvfrom(sock, message, sizeof(message), 0,
+ (struct sockaddr *) &addr, &addrlen);
+ if (cnt < 0) {
+ perror("recvfrom");
+ exit(1);
+ } else if (cnt == 0) {
+ break;
+ }
+ printf("%s: message = \"%s\"\n", inet_ntoa(addr.sin_addr), message);
+ }
+ }
+}
diff --git a/tools/test/stress2/misc/multicast2.sh b/tools/test/stress2/misc/multicast2.sh
new file mode 100755
index 000000000000..bd95cd0bd9e6
--- /dev/null
+++ b/tools/test/stress2/misc/multicast2.sh
@@ -0,0 +1,26 @@
+#!/bin/sh
+
+# D19886 Fix numerous refcount bugs in multicast
+
+# Page fault in in6_pcbpurgeif0+0xc8 seen.
+# https://people.freebsd.org/~pho/stress/log/mmacy035.txt
+# Test scenario by mmacy
+
+# Fixed by r349507
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -x /usr/local/bin/mDNSResponderPosix ] ||
+ { echo "mDNSResponder not installed"; exit 0; }
+
+(cd ../testcases/swap; ./swap -t 2m -i 50 -v -h -l 100) &
+sleep 2
+
+service mdnsd onestart
+ifconfig vtnet0 delete 2>/dev/null
+ifconfig epair create
+ifconfig epair0a 0/24 up
+ifconfig epair0a destroy
+service mdnsd onestop
+
+while pkill swap; do :; done
+wait
diff --git a/tools/test/stress2/misc/namecache.sh b/tools/test/stress2/misc/namecache.sh
new file mode 100755
index 000000000000..6ca790b297f8
--- /dev/null
+++ b/tools/test/stress2/misc/namecache.sh
@@ -0,0 +1,214 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Test case for vfs.lookup_shared=1 that shows possible name cache
+# inconsistency:
+
+# $ ls -l /tmp/file.05015?
+# ls: /tmp/file.050150: No such file or directory
+# $ fsdb -r /dev/ad4s1e
+# ** /dev/ad4s1e (NO WRITE)
+# Examining file system `/dev/ad4s1e'
+# Last Mounted on /tmp
+# current inode: directory
+# I=2 MODE=41777 SIZE=5120
+# BTIME=May 7 05:54:47 2006 [0 nsec]
+# MTIME=Apr 2 11:27:36 2009 [0 nsec]
+# CTIME=Apr 2 11:27:36 2009 [0 nsec]
+# ATIME=Apr 2 12:00:30 2009 [0 nsec]
+# OWNER=root GRP=wheel LINKCNT=35 FLAGS=0 BLKCNT=c GEN=65f71df4
+# fsdb (inum: 2)> lookup file.050150
+# component `file.050150': current inode: regular file
+# I=198 MODE=100600 SIZE=0
+# BTIME=Apr 2 11:24:33 2009 [0 nsec]
+# MTIME=Apr 2 11:24:33 2009 [0 nsec]
+# CTIME=Apr 2 11:24:33 2009 [0 nsec]
+# ATIME=Apr 2 11:24:33 2009 [0 nsec]
+# OWNER=pho GRP=wheel LINKCNT=1 FLAGS=0 BLKCNT=0 GEN=1deaab3a
+# fsdb (inum: 198)> quit
+# $
+
+# Consistency is restored by a umount + mount of the FS
+
+# Observations:
+# No problems seen with vfs.lookup_shared=0.
+# Does not fail in a "private" subdirectory
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > namecache.c
+mycc -o namecache -Wall namecache.c
+rm -f namecache.c
+
+#dir=/tmp/namecache.dir # No problems seen
+dir=/tmp
+[ -d $dir ] || mkdir -p $dir
+cd $dir
+
+start=`date '+%s'`
+for i in `jot 30`; do
+ for j in `jot 10`; do
+ /tmp/namecache &
+ done
+
+ for j in `jot 10`; do
+ wait
+ done
+ [ $((`date '+%s'` - start)) -gt 1200 ] && break
+done
+
+if ls -l $dir/file.0* 2>&1 | egrep "file.0[0-9]" | grep -q "No such file"; then
+ echo FAIL
+ echo "ls -l $dir/file.0*"
+ ls -l $dir/file.0*
+fi
+
+rm -f /tmp/namecache # /$dir/file.0*
+exit
+EOF
+/* Test scenario for possible name cache problem */
+
+#include <sys/types.h>
+#include <dirent.h>
+#include <err.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+static char path[MAXPATHLEN+1];
+static char buf[64 * 1024];
+
+void
+pm(void)
+{
+ int fd, n;
+ int space = sizeof(buf);
+ struct stat statb;
+ off_t base;
+ struct dirent *dp;
+ char *bp = buf;
+
+ if ((fd = open(".", O_RDONLY)) == -1)
+ err(1, "open(%s)", ".");
+
+ do {
+ if ((n = getdirentries(fd, bp, space, &base)) == -1)
+ err(1, "getdirentries");
+ space = space - n;
+ bp = bp + n;
+ } while (n != 0);
+ close(fd);
+
+ bp = buf;
+ dp = (struct dirent *)bp;
+ for (;;) {
+ if (strcmp(path, dp->d_name) == 0) {
+
+ if (stat(dp->d_name, &statb) == -1) {
+ warn("stat(%s)", dp->d_name);
+ printf("name: %-10s, inode %7ju, "
+ "type %2d, namelen %d, d_reclen %d\n",
+ dp->d_name, (uintmax_t)dp->d_fileno, dp->d_type,
+ dp->d_namlen, dp->d_reclen);
+ fflush(stdout);
+ } else {
+ printf("stat(%s) succeeded!\n", path);
+ fflush(stdout);
+ }
+
+ }
+ bp = bp + dp->d_reclen;
+ dp = (struct dirent *)bp;
+ if (dp->d_reclen <= 0)
+ break;
+ }
+}
+
+static void
+reader(void) {
+ int fd;
+
+ if ((fd = open(path, O_RDWR, 0600)) < 0) {
+ warn("open(%s). %s:%d", path, __FILE__, __LINE__);
+ pm();
+ exit(1);
+ }
+ close(fd);
+ return;
+}
+
+static void
+writer(void) {
+ int fd;
+
+ if ((fd = open(path, O_RDWR, 0600)) < 0) {
+ warn("open(%s). %s:%d", path, __FILE__, __LINE__);
+ pm();
+ exit(1);
+ }
+ close(fd);
+ return;
+}
+
+int
+main(int argc, char **argv)
+{
+ pid_t pid;
+ int fd, i, status;
+
+ for (i = 0; i < 10000; i++) {
+ if (sprintf(path, "file.0%d", getpid()) < 0)
+ err(1, "sprintf()");
+ if ((fd = open(path, O_CREAT | O_RDWR, 0600)) == -1)
+ err(1, "open(%s)", path);
+ close(fd);
+
+ if ((pid = fork()) == 0) {
+ writer();
+ exit(EXIT_SUCCESS);
+
+ } else if (pid > 0) {
+ reader();
+ if (waitpid(pid, &status, 0) == -1)
+ warn("waitpid(%d)", pid);
+ } else
+ err(1, "fork(), %s:%d", __FILE__, __LINE__);
+
+ if (unlink(path) == -1)
+ err(1, "unlink(%s). %s:%d", path, __FILE__, __LINE__);
+ }
+ return (0);
+}
diff --git a/tools/test/stress2/misc/namecache2.sh b/tools/test/stress2/misc/namecache2.sh
new file mode 100755
index 000000000000..a5dd4319fc66
--- /dev/null
+++ b/tools/test/stress2/misc/namecache2.sh
@@ -0,0 +1,192 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm
+# 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 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.
+#
+
+# UFS cache inconsistancy for rename(2) demonstrated
+# Fails with:
+# ls -ali /mnt
+# ls: tfa1022: No such file or directory
+# Fixed by r248422
+
+# Test scenario obtained from Rick Miller <vmiller at hostileadmin com>
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# This threaded test is designed for MP.
+[ `sysctl hw.ncpu | sed 's/.* //'` -eq 1 ] && exit 0
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > namecache2.c
+rm -f /tmp/namecache2
+mycc -o namecache2 -Wall -Wextra -g -O2 namecache2.c -lpthread || exit 1
+rm -f namecache2.c
+cd $odir
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+(cd $mntpoint; /tmp/namecache2)
+
+f=`(cd $mntpoint; echo *)`
+if [ "$f" != '*' ]; then
+ echo FAIL
+ echo "echo $mntpoint/*"
+ echo $mntpoint/*
+ echo ""
+ echo "ls -ali $mntpoint"
+ ls -ali $mntpoint
+ echo ""
+ echo "fsdb -r /dev/md${mdstart}$part"
+ fsdb -r /dev/md${mdstart}$part <<-EF
+ ls
+ quit
+ EF
+fi
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/namecache2
+exit 0
+EOF
+/*
+ * NOTE: This must be run with the current working directory on a local UFS
+ * disk partition, to demonstrate a FreeBSD namecache bug. I have never seen
+ * this bug happen with an NFS partition.
+ */
+
+#include <pthread.h>
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <time.h>
+
+int stopping = false;
+char *pFilename = 0;
+
+static void *
+statThread(void *arg __unused)
+{
+
+ struct stat statData;
+ int rc;
+
+ for (;;) {
+ while (pFilename == 0) {
+ if (stopping)
+ return 0;
+ }
+
+ rc = stat(pFilename, &statData);
+ if (rc < 0 && errno != ENOENT) {
+ printf(" statThread stat() on %s failed with errno %d\n",
+ pFilename, errno);
+ return 0;
+ }
+ }
+
+ return 0;
+}
+
+int
+main(void)
+{
+ char filename1 [20], filename2[20], filename3[20];
+ pthread_t threadId;
+ struct stat statData;
+ int result, fd;
+ unsigned int number;
+ struct timespec period;
+ time_t start;
+
+ sprintf(filename1, "tfa0");
+ fd = open(filename1, O_CREAT, S_IRWXU);
+ if (fd < 0) {
+ printf("open(O_CREAT) on %s failed with errno %d\n", filename1, errno);
+ return 0;
+ }
+ if (close(fd) < 0) {
+ printf("close() on %s failed with errno %d\n", filename1, errno);
+ return 0;
+ }
+ result = pthread_create(&threadId, NULL, statThread, NULL);
+ if (result < 0)
+ errc(1, result, "pthread_create()");
+
+ start = time(NULL);
+ for (number = 0; number < 0x001FFFFF; number += 2) {
+ sprintf(filename1, "tfa%u", number);
+ sprintf(filename2, "tfa%u", number + 1);
+ sprintf(filename3, "tfa%u", number + 2);
+ if (rename(filename1, filename2) < 0) {
+ printf(" rename1() from %s to %s failed with errno %d\n",
+ filename1, filename2, errno);
+ return 0;
+ }
+ pFilename = filename3;
+
+ if (rename(filename2, filename3) < 0) {
+ printf(" rename2() from %s to %s failed with errno %d\n",
+ filename2, filename3, errno);
+ return 0;
+ }
+ pFilename = 0;
+ period.tv_sec = 0;
+ period.tv_nsec = 500;
+ nanosleep(&period, 0);
+
+ if (stat(filename3, &statData) < 0) {
+ printf("stat(%s) failed with errno %d\n", filename3, errno);
+ stopping = true;
+ period.tv_sec = 0;
+ period.tv_nsec = 500;
+ nanosleep(&period, 0);
+ return 0;
+ }
+ if (time(NULL) - start > 1200) {
+ fprintf(stderr, "Test timed out.\n");
+ break;
+ }
+ }
+ unlink(filename3);
+
+ return 0;
+}
diff --git a/tools/test/stress2/misc/nanosleep.sh b/tools/test/stress2/misc/nanosleep.sh
new file mode 100755
index 000000000000..79e2d505fb47
--- /dev/null
+++ b/tools/test/stress2/misc/nanosleep.sh
@@ -0,0 +1,87 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# A simplistic regression test for r200510:
+
+[ `sysctl -n kern.hz` -lt 1000 ] && exit 0
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > nanosleep.c
+mycc -o nanosleep -Wall -Wextra nanosleep.c || exit 1
+rm -f nanosleep.c
+
+/tmp/nanosleep && s=0 || s=$?
+
+rm -f /tmp/nanosleep
+exit $s
+EOF
+#include <sys/limits.h>
+#include <sys/time.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <strings.h>
+#include <time.h>
+
+#define N 20000
+
+int
+main(void)
+{
+ struct timespec rmt, rqt;
+ struct timespec finish, start, res;
+ long m;
+ int i;
+
+ m = LONG_MAX;
+ for (i = 0; i < 100; i++) {
+ rqt.tv_sec = 0;
+ rqt.tv_nsec = N;
+ clock_gettime(CLOCK_REALTIME_PRECISE, &start);
+ if (nanosleep(&rqt, &rmt) == -1)
+ err(1, "nanosleep");
+ clock_gettime(CLOCK_REALTIME_PRECISE, &finish);
+ timespecsub(&finish, &start, &res);
+ if (res.tv_nsec < N)
+ errx(1, "Short sleep: %ld", res.tv_nsec);
+// fprintf(stderr, "asked for %f, got %f\n", (double)N / 1e9,
+// (double)res.tv_nsec / 1e9);
+ if (m > res.tv_nsec)
+ m = res.tv_nsec;
+ }
+ if (m > 2 * N) {
+ fprintf(stderr, "nanosleep(%fs). Best value is %fs.\n",
+ (double)N / 1e9, (double)m / 1e9);
+ errx(1, "FAIL");
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/nbufkv.sh b/tools/test/stress2/misc/nbufkv.sh
new file mode 100755
index 000000000000..db2c45feb496
--- /dev/null
+++ b/tools/test/stress2/misc/nbufkv.sh
@@ -0,0 +1,156 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Test scenario with a 20G files on two UFS2 FSs with 64k/64k
+# Test program will hang (deadlock) in "nbufkv"
+
+# Test scenario by John-Mark Gurney <jmg at funkthat dot com>
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+dir=`dirname $diskimage`
+d1=$dir/diskimage1
+d2=$dir/diskimage2
+rm -f $d1 $d2
+
+size=20 # G
+avail=$((`sysctl -n hw.physmem` / 1024 / 1024 / 1024))
+[ $((size * 2)) -gt $avail ] && size=$((avail / 2))
+[ `df -k $dir | tail -1 | awk '{print $4}'` -lt \
+ $((size * 2 * 1024 * 1024)) ] &&
+ echo "Not enough disk space on $dir." && exit 0
+
+odir=`pwd`
+
+USE_TIMEOUT=1
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > nbufkv.c
+mycc -o nbufkv -Wall nbufkv.c
+rm -f nbufkv.c
+cd $odir
+
+u1=$mdstart
+u2=$((u1 + 1))
+mp1=$mntpoint
+mp2=${mntpoint}2
+[ -d $mp1 ] || mkdir $mp1
+[ -d $mp2 ] || mkdir $mp2
+dd if=/dev/zero of=$d1 bs=1m count=${size}k status=none || exit 1
+cp $d1 $d2 || exit 1
+
+mount | grep -q /dev/md${u2}$part && umount -f /dev/md${u2}$part
+mount | grep -q /dev/md${u1}$part && umount -f /dev/md${u1}$part
+[ -c /dev/md$u2 ] && mdconfig -d -u $u2
+[ -c /dev/md$u1 ] && mdconfig -d -u $u1
+
+mdconfig -a -t vnode -f $d1 -u $u1 || exit 1
+bsdlabel -w md$u1 auto
+newfs -b 65536 -f 65536 -O2 md${u1}$part > /dev/null
+
+mdconfig -a -t vnode -f $d2 -u $u2 || exit 1
+bsdlabel -w md$u2 auto
+newfs -b 65536 -f 65536 -O2 md${u2}$part > /dev/null
+
+mount /dev/md${u1}$part $mp1
+mount /dev/md${u2}$part $mp2
+
+/tmp/nbufkv $mp1 &
+/tmp/nbufkv $mp2 &
+wait
+
+umount /dev/md${u2}$part
+umount /dev/md${u1}$part
+
+mount | grep -q /dev/md${u2}$part && umount -f /dev/md${u2}$part
+mount | grep -q /dev/md${u1}$part && umount -f /dev/md${u1}$part
+
+mdconfig -d -u $u2
+mdconfig -d -u $u1
+
+rm -rf $d1 $d2 /tmp/nbufkv
+exit
+EOF
+#include <sys/param.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+void
+handler(int i) {
+ fprintf(stderr, "FAIL. Timerout.\n");
+ _exit(0);
+}
+
+void
+work(int fd, size_t n)
+{
+ int i;
+
+ for (i = 0; i < 128 * 1024; i++) {
+ n = n - PAGE_SIZE;
+ if (lseek(fd, n , SEEK_SET) == -1)
+ err(1, "lseek()");
+ if (write(fd, "1", 1) != 1)
+ err(1, "write()");
+ }
+
+}
+
+int
+main(int argc, char **argv)
+{
+
+ int fd;
+ off_t len;
+ char path[128];
+
+ len = 20;
+ len = len * 1024 * 1024 * 1024;
+
+ sprintf(path, "%s/nbufkv.%06d", argv[1], getpid());
+ if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0640)) == -1)
+ err(1,"open(%s)", path);
+ if (ftruncate(fd, len) == -1)
+ err(1, "ftruncate");
+
+ signal(SIGALRM, handler);
+ alarm(1200);
+ work(fd, len);
+
+ close(fd);
+ if (unlink(path) == -1)
+ err(1, "unlink(%s)", path);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/newfs.sh b/tools/test/stress2/misc/newfs.sh
new file mode 100755
index 000000000000..8ef7b9eae31e
--- /dev/null
+++ b/tools/test/stress2/misc/newfs.sh
@@ -0,0 +1,94 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2011 Peter Holm <pho@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 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.
+#
+
+# A few UFS1 newfs combinations are known to cause fsck(8) to fail.
+# Ignore these for now. Will be fixed:
+# newfs -O1 -b 65536 -f 8192
+# newfs -O1 -b 65536 -f 16384
+# newfs -O1 -b 65536 -f 32768
+# newfs -O1 -b 65536 -f 65536
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep "$mntpoint" | grep md${mdstart}$part > /dev/null &&
+ umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+
+echo "Expect warnings from SU and SU+J."
+log=/tmp/newfs.sh.log
+s=0
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=10s
+export RUNTIME=$runRUNTIME
+export CTRLDIR=$mntpoint/stressX.control
+start=`date '+%s'`
+for opt in -O1 -O2 -U -j; do
+ echo "Testing newfs with option $opt."
+ blocksize=4096
+ while [ $blocksize -le 65536 ]; do
+ for i in 8 4 2 1; do
+ fragsize=$((blocksize / i))
+ newfs $opt -b $blocksize -f $fragsize \
+ md${mdstart}$part > /dev/null 2>&1 || continue
+ mount /dev/md${mdstart}$part $mntpoint
+ chmod 777 $mntpoint
+ rm -rf /tmp/stressX.control
+ su $testuser -c \
+ "(cd ..; ./run.sh disk.cfg > /dev/null 2>&1)" &
+ sleep 10
+ ../tools/killall.sh
+ wait
+ while mount | grep "$mntpoint" | \
+ grep -q md${mdstart}$part; do
+ umount $mntpoint > /dev/null 2>&1 || sleep 1
+ done
+ checkfs /dev/md${mdstart}$part > $log 2>&1 || {
+ cmd="newfs $opt -b $blocksize -f $fragsize"
+# if ! grep -q -- "$cmd" $0; then
+ s=1
+ echo "$cmd"
+ cat $log
+# fi
+ }
+ done
+ blocksize=$((blocksize * 2))
+ done
+ if [ $((`date '+%s'` - start)) -gt 1200 ]; then
+ echo "Timed out"
+ s=1
+ break
+ fi
+done
+mdconfig -d -u $mdstart
+rm -f $log
+exit $s
diff --git a/tools/test/stress2/misc/newfs2.sh b/tools/test/stress2/misc/newfs2.sh
new file mode 100755
index 000000000000..ac891e7926fb
--- /dev/null
+++ b/tools/test/stress2/misc/newfs2.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2011 Peter Holm <pho@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 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.
+#
+
+# phk has seen freezes with this newfs option: "-b 32768 -f 4096 -O2"
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+size=$((32 * 1024 * 1024))
+
+mount | grep "$mntpoint" | grep -q md${mdstart}$part && umount $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+s=0
+start=`date '+%s'`
+while [ $size -le $((900 * 1024 * 1024)) ]; do
+ mb=$((size / 1024 / 1024))
+ rm -f $diskimage
+ dd if=/dev/zero of=$diskimage bs=1m count=$mb status=none
+ mdconfig -a -t vnode -f $diskimage -u $mdstart ||
+ { rm $diskimage; exit 1; }
+ bsdlabel -w md$mdstart auto
+ newfs -b 32768 -f 4096 -O2 md${mdstart}$part > /dev/null 2>&1
+ mount /dev/md${mdstart}$part $mntpoint
+ export RUNDIR=$mntpoint/stressX
+ export runRUNTIME=30s
+ export RUNTIME=$runRUNTIME
+ export CTRLDIR=$mntpoint/stressX.control
+ (cd ..; ./run.sh disk.cfg) > /dev/null
+ while mount | grep "$mntpoint" | grep -q md${mdstart}$part; do
+ umount $mntpoint > /dev/null 2>&1
+ done
+ checkfs md${mdstart}$part || s=1
+ mdconfig -d -u $mdstart
+ size=$((size + 32 * 1024 * 1024))
+ if [ $((`date '+%s'` - start)) -gt 1200 ]; then
+ echo "Timed out"
+ s=1
+ break
+ fi
+done
+rm -f $diskimage
+exit $s
diff --git a/tools/test/stress2/misc/newfs3.sh b/tools/test/stress2/misc/newfs3.sh
new file mode 100755
index 000000000000..f6dd84e801bc
--- /dev/null
+++ b/tools/test/stress2/misc/newfs3.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2011 Peter Holm <pho@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 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.
+#
+
+# phk has seen freezes with this newfs option: "-b 32768 -f 4096 -O2"
+#
+# Deadlocks seen with this test and:
+# newfs -b 4096 -f 4096 -O2 md0c on a 128 Mb FS
+# newfs -b 4096 -f 1024 -O2 md0c on a 64 Mb FS
+# 20070505 newfs -b 4096 -f 4096 -O2 md0c on a 32 Mb FS: panic: lockmgr: locking against myself
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+size=$((32 * 1024 * 1024))
+opt="-O2" # newfs option. Eg. -U
+
+mount | grep "$mntpoint" | grep -q md${mdstart}$part && umount $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+s=0
+while [ $size -le $((128 * 1024 * 1024)) ]; do
+ mb=$((size / 1024 / 1024))
+ rm -f $diskimage
+ dd if=/dev/zero of=$diskimage bs=1m count=$mb status=none
+ mdconfig -a -t vnode -f $diskimage -u $mdstart ||
+ { rm -f $diskimage; exit 1; }
+ bsdlabel -w md$mdstart auto
+ blocksize=4096
+ while [ $blocksize -le 65536 ]; do
+ for i in 1 2 4 8; do
+ fragsize=$((blocksize / i))
+ newfs -b $blocksize -f $fragsize $opt md${mdstart}$part > \
+ /dev/null 2>&1
+ mount /dev/md${mdstart}$part $mntpoint
+ export RUNDIR=$mntpoint/stressX
+ export runRUNTIME=15s
+ export RUNTIME=$runRUNTIME
+ export CTRLDIR=$mntpoint/stressX.control
+ (cd ..; ./run.sh disk.cfg) > /dev/null 2>&1 &
+ sleep 15
+ ../tools/killall.sh
+ wait
+ while mount | grep "$mntpoint" | \
+ grep -q md${mdstart}$part; do
+ umount $mntpoint > /dev/null 2>&1
+ done
+ checkfs /dev/md${mdstart}$part || s=1
+ done
+ blocksize=$((blocksize * 2))
+ done
+ mdconfig -d -u $mdstart
+ size=$((size + 32 * 1024 * 1024))
+done
+rm -f $diskimage
+exit $s
diff --git a/tools/test/stress2/misc/newfs4.sh b/tools/test/stress2/misc/newfs4.sh
new file mode 100755
index 000000000000..46d563a49460
--- /dev/null
+++ b/tools/test/stress2/misc/newfs4.sh
@@ -0,0 +1,128 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2013 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# Deadlock problems. Test scenario by Lev Serebryakov <lev@freebsd.org>
+# newfs -O2 -U -b 65536
+# The io programs will get stuck in nbufkv wait state.
+
+# Threads stuck in newbuf:
+# https://people.freebsd.org/~pho/stress/log/newfs4-2.txt
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > newfs4.c
+mycc -o newfs4 -Wall -Wextra newfs4.c || exit 1
+rm -f newfs4.c
+cd $odir
+
+mount | grep "$mntpoint" | grep -q md${mdstart}$part && umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+size=9 # Gb
+[ `df -k $(dirname $diskimage) | tail -1 | \
+ awk '{print $4}'` -lt $((size * 1024 * 1024)) ] && \
+ echo "Not enough disk space on `dirname $diskimage`." && exit 1
+trap "rm -f $diskimage" EXIT INT
+dd if=/dev/zero of=$diskimage bs=1m count=$((size * 1024)) status=none ||
+ exit 1
+
+blocksize="-b 65536"
+opt="-O2 -U"
+mdconfig -a -t vnode -f $diskimage -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $blocksize $opt md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+cd $mntpoint
+truncate -s 2g f1
+truncate -s 2g f2
+truncate -s 2g f3
+truncate -s 2g f4
+/tmp/newfs4 f1 &
+/tmp/newfs4 f2 &
+/tmp/newfs4 f3 &
+/tmp/newfs4 f4 &
+wait
+
+while mount | grep "$mntpoint" | grep -q md${mdstart}$part; do
+ umount -f $mntpoint || sleep 1
+done
+checkfs /dev/md${mdstart}$part; s=$?
+
+mdconfig -d -u $mdstart
+rm -f /tmp/newfs4
+exit $s
+
+EOF
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+/* Perform random IO operations on a file */
+
+int
+main(int argc, char **argv)
+{
+ struct stat sb;
+ off_t bp, maxb;
+ long i;
+ int fd;
+ char buf[256];
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage %s: file\n", argv[0]);
+ return (1);
+ }
+ if ((fd = open(argv[1], O_RDWR)) == -1)
+ err(1, "open(%s)", argv[1]);
+ if (fstat(fd, &sb) == -1)
+ err(1, "fstatf(stdin)");
+ maxb = sb.st_size - sizeof(buf);
+
+ for (i = 0; i < 10000; i++) {
+ bp = arc4random();
+ bp = (bp << 31 | arc4random()) % maxb;
+
+ if (lseek(fd, bp, 0) == -1)
+ err(1, "lseek()");
+ if (write(fd, buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write()");
+ }
+ close(fd);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/newfs5.sh b/tools/test/stress2/misc/newfs5.sh
new file mode 100755
index 000000000000..f41da12e28dd
--- /dev/null
+++ b/tools/test/stress2/misc/newfs5.sh
@@ -0,0 +1,116 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# Variation of newfs4.sh, using a swap backed MD disk
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > newfs5.c
+mycc -o newfs5 -Wall -Wextra newfs5.c
+rm -f newfs5.c
+cd $odir
+
+mount | grep "$mntpoint" | grep md${mdstart}$part > /dev/null && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+blocksize="-b 65536"
+opt="-O2 -U"
+size=9 # Gb
+mdconfig -a -t swap -s ${size}g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $blocksize $opt md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+cd $mntpoint
+truncate -s 2g f1
+truncate -s 2g f2
+truncate -s 2g f3
+truncate -s 2g f4
+/tmp/newfs5 f1 &
+/tmp/newfs5 f2 &
+/tmp/newfs5 f3 &
+/tmp/newfs5 f4 &
+wait
+
+while mount | grep "$mntpoint" | grep -q md${mdstart}$part; do
+ umount -f $mntpoint || sleep 1
+done
+checkfs /dev/md${mdstart}$part; s=$?
+
+mdconfig -d -u $mdstart
+rm -f $diskimage
+rm -f /tmp/newfs5
+exit $s
+
+EOF
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+/* Perform random IO operations on a file */
+
+int
+main(int argc, char **argv)
+{
+ struct stat sb;
+ char buf[256];
+ off_t bp, maxb;
+ int fd;
+ long i;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage %s: file\n", argv[0]);
+ return (1);
+ }
+ if ((fd = open(argv[1], O_RDWR)) == -1)
+ err(1, "open(%s)", argv[1]);
+ if (fstat(fd, &sb) == -1)
+ err(1, "fstatf(stdin)");
+ maxb = sb.st_size - sizeof(buf);
+
+ for (i = 0; i < 10000; i++) {
+ bp = arc4random();
+ bp = (bp << 31 | arc4random()) % maxb;
+
+ if (lseek(fd, bp, 0) == -1)
+ err(1, "lseek()");
+ if (write(fd, buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write()");
+ }
+ close(fd);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/nfs.sh b/tools/test/stress2/misc/nfs.sh
new file mode 100755
index 000000000000..a38745ce1cc3
--- /dev/null
+++ b/tools/test/stress2/misc/nfs.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2013 Peter Holm <pho@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 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.
+#
+
+# No problems seen with this test
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+mount | grep "$mntpoint" | grep -q nfs && umount $mntpoint
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mntpoint
+
+export RUNDIR=$mntpoint/nfs.`jot -rc 8 a z | tr -d '\n'`/stressX
+export runRUNTIME=10m
+
+mkdir -p $RUNDIR
+chmod 777 $RUNDIR
+su $testuser -c "(cd ..; ./run.sh disk.cfg)" 2>/dev/null
+su $testuser -c "rm -rf $RUNDIR 2>/dev/null"
+rm -rf $RUNDIR
+
+umount $mntpoint
+while mount | grep "$mntpoint " | grep -q nfs; do
+ umount -f $mntpoint
+done
+exit 0
diff --git a/tools/test/stress2/misc/nfs10.sh b/tools/test/stress2/misc/nfs10.sh
new file mode 100755
index 000000000000..15f449548f45
--- /dev/null
+++ b/tools/test/stress2/misc/nfs10.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# nfsv4 test scenario
+# Deadlock seen: http://people.freebsd.org/~pho/stress/log/nfs10.txt
+# Lock violation: http://people.freebsd.org/~pho/stress/log/nfs10-2.txt
+# Double fault: http://people.freebsd.org/~pho/stress/log/nfs10-3.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+grep -q $mntpoint /etc/exports ||
+ { echo "$mntpoint missing from /etc/exports"; exit 0; }
+
+m2=${mntpoint}2
+[ -d $m2 ] || mkdir $m2
+mount | grep "on $m2 " | grep -q nfs && umount $m2
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+mount -t nfs -o nfsv4 -o rw,retrycnt=3 127.0.0.1:$mntpoint $m2
+
+export RUNDIR=$m2/stressX
+export runRUNTIME=10m # Run tests for 10 minutes
+
+su $testuser -c "(cd ..; ./run.sh marcus.cfg)" &
+sleep 300
+umount $m2 2>/dev/null # Test umount of active FS while here
+wait
+
+while mount | grep "on $m2 " | grep -q nfs; do
+ umount $m2
+done
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/nfs11.sh b/tools/test/stress2/misc/nfs11.sh
new file mode 100755
index 000000000000..e132ad9f894e
--- /dev/null
+++ b/tools/test/stress2/misc/nfs11.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# nfsv4 test scenario
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+grep -q $mntpoint /etc/exports ||
+ { echo "$mntpoint missing from /etc/exports"; exit 0; }
+
+m2=${mntpoint}2
+[ -d $m2 ] || mkdir $m2
+mount | grep "on $m2 " | grep -q nfs && umount $m2
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+mount -t nfs -o nfsv4 -o rw,retrycnt=3 127.0.0.1:$mntpoint $m2
+
+export RUNDIR=$m2/stressX
+export runRUNTIME=10m # Run tests for 10 minutes
+
+su $testuser -c "(cd ..; ./run.sh marcus.cfg)"
+
+while mount | grep "on $m2 " | grep -q nfs; do
+ umount $m2
+done
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/nfs12.sh b/tools/test/stress2/misc/nfs12.sh
new file mode 100755
index 000000000000..e1b6e2c9279e
--- /dev/null
+++ b/tools/test/stress2/misc/nfs12.sh
@@ -0,0 +1,160 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# "umount -f" test scenario (distill of nfs4.sh)
+# "panic: vputx: missed vn_close" seen.
+# Fixed in r248815
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > nfs12.c
+mycc -o nfs12 -Wall -Wextra -O2 -g nfs12.c
+rm -f nfs12.c
+cd $here
+
+mount | grep "on $mntpoint " | grep nfs > /dev/null && umount $mntpoint
+version="-o nfsv3" # The default
+[ $# -eq 1 ] && [ "$1" -eq 4 ] && version="-o nfsv4"
+for i in `jot 10`; do
+ mount -t nfs $version -o tcp -o retrycnt=3 -o intr,soft -o rw \
+ $nfs_export $mntpoint
+ sleep 2
+
+ if [ $i -eq 10 ]; then
+ rm -f $mntpoint/nfs12.p*
+ else
+ (cd $mntpoint; /tmp/nfs12 > /dev/null 2>&1) &
+ sleep 2
+ fi
+
+ while mount | grep "on $mntpoint " | grep -q nfs; do
+ umount -f $mntpoint
+ done
+ kill -9 $! > /dev/null 2>/dev/null && kill $!
+ wait
+done
+
+rm -f /tmp/nfs12
+exit
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define INPUTFILE "/bin/date"
+#define PARALLEL 5
+
+static int
+tmmap(void)
+{
+ struct stat statbuf;
+ pid_t pid;
+ char *src, *dst;
+ int i;
+ int fdin, fdout;
+ char file[128];
+
+ pid = getpid();
+ setproctitle("mmap");
+ for (i = 0; i < 50000; i++) {
+ sprintf(file,"nfs12.p%05d.%05d", pid, i);
+
+ if ((fdin = open(INPUTFILE, O_RDONLY)) < 0)
+ err(1, INPUTFILE);
+
+ if ((fdout = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
+ err(1, "%s", file);
+
+ if (fstat(fdin, &statbuf) < 0)
+ err(1, "fstat error");
+
+ if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1)
+ err(1, "lseek error");
+
+ /* write a dummy byte at the last location */
+ if (write(fdout, "", 1) != 1)
+ err(1, "write error");
+
+ if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0)) ==
+ (caddr_t) - 1)
+ err(1, "mmap error for input");
+
+ if ((dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE,
+ MAP_SHARED, fdout, 0)) == (caddr_t) - 1)
+ err(1, "mmap error for output");
+
+ memcpy(dst, src, statbuf.st_size);
+
+ if (munmap(src, statbuf.st_size) == -1)
+ err(1, "munmap");
+ close(fdin);
+
+ if (munmap(dst, statbuf.st_size) == -1)
+ err(1, "munmap");
+ close(fdout);
+
+ if (unlink(file) == -1)
+ err(3, "unlink(%s)", file);
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i;
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ tmmap();
+ }
+
+ for (i = 0; i < PARALLEL; i++) {
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/nfs13.sh b/tools/test/stress2/misc/nfs13.sh
new file mode 100755
index 000000000000..fb65b8a64044
--- /dev/null
+++ b/tools/test/stress2/misc/nfs13.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# nfs loopback mount of a tmpfs file system.
+# "[tcp] 127.0.0.1:/mnt: Permission denied" seen.
+
+# " mount_nfs hangs in mntref" seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+grep -q $mntpoint /etc/exports 2>/dev/null ||
+ { echo "$mntpoint missing from /etc/exports"; exit 0; }
+
+s=0
+m2=${mntpoint}2
+[ -d $m2 ] || mkdir $m2
+mount | grep "on $m2 " | grep -q nfs && umount $m2
+mount | grep "on $mntpoint " | grep -q tmpfs && umount -f $mntpoint
+
+mount -o size=1g -t tmpfs tmpfs $mntpoint || s=1
+chmod 777 $mntpoint
+
+[ $s -eq 0 ] &&
+ mount -t nfs -o tcp -o rw,retrycnt=1 127.0.0.1:$mntpoint $m2 || s=2
+
+export RUNDIR=$m2/stressX
+export runRUNTIME=10m # Run tests for 10 minutes
+
+[ $s -eq 0 ] &&
+ su $testuser -c "(cd ..; ./run.sh marcus.cfg)"
+
+while mount | grep "on $m2 " | grep -q nfs; do
+ umount $m2
+done
+
+while mount | grep "on $mntpoint " | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
+exit $s
diff --git a/tools/test/stress2/misc/nfs14.sh b/tools/test/stress2/misc/nfs14.sh
new file mode 100755
index 000000000000..c3b4152c5af4
--- /dev/null
+++ b/tools/test/stress2/misc/nfs14.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Bring the network down and up again during NFS tests.
+
+# "panic: re_txeof: freeing NULL mbufs!" seen:
+# https://people.freebsd.org/~pho/stress/log/nfs14.txt
+
+# This is a very disruptive test, so be aware!
+[ -z "$footshoot" ] && exit 0
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+if=`ifconfig -lu | awk '{print $1}'`
+[ -z "$if" ] && exit 0
+
+[ ! -d $mntpoint ] && mkdir $mntpoint
+mount | grep "on $mntpoint " | grep -q nfs && umount $mntpoint
+mount -t nfs -o tcp -o retrycnt=3 -o soft \
+ -o rw $nfs_export $mntpoint
+
+sleep .5
+export RUNDIR=$mntpoint/nfs14.`jot -rc 8 a z | tr -d '\n'`/stressX
+rm -rf $RUNDIR
+mkdir -p $RUNDIR
+chmod 777 $RUNDIR
+export runRUNTIME=3m
+rm -rf /tmp/stressX.control/*
+
+su $testuser -c '(cd ..; ./run.sh rw.cfg) > /dev/null 2>&1' &
+
+sleep `jot -r 1 5 15`.`jot -r 1 1 9`
+downtime=`jot -r 1 100 150`
+echo "Testing with $downtime seconds downtime."
+ifconfig $if down; sleep $downtime; ifconfig $if up
+
+wait
+rm -rf $RUNDIR
+while mount | grep -q $mntpoint; do
+ umount -f $mntpoint > /dev/null 2>&1
+done
+../tools/killall.sh
+exit 0
diff --git a/tools/test/stress2/misc/nfs15.sh b/tools/test/stress2/misc/nfs15.sh
new file mode 100755
index 000000000000..33546aba8d4b
--- /dev/null
+++ b/tools/test/stress2/misc/nfs15.sh
@@ -0,0 +1,212 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Test effort to reproduce reported: "observed a hang in a multi-threaded
+# process that had hit an assertion failure and was attempting to dump core".
+# Problem never seen.
+
+# Most interesting load are:
+# - coredumping of the multithreaded program with the current dir on NFS,
+# which also accesses NFS files;
+# - advisory locking tests on NFS files, while e.g. sending SIGSTOP/SIGCONT
+# to the test programs.
+
+# See also pthread9.sh
+
+# https://people.freebsd.org/~pho/stress/log/kostik897.txt
+# Fixed in r302013.
+
+# "panic: mutex sleepq chain not owned at subr_sleepqueue.c:1009" seen:
+# https://people.freebsd.org/~pho/stress/log/kostik914.txt
+# Fixed in r302328.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+USE_TIMEOUT=1
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > nfs15.c
+mycc -o nfs15 -Wall -Wextra -O2 -g nfs15.c -lpthread || exit 1
+rm -f nfs15.c
+cd $here
+
+mount | grep "on $mntpoint " | grep nfs > /dev/null && umount $mntpoint
+
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw -o nolockd \
+ $nfs_export $mntpoint || exit 1
+sleep 2
+wd=$mntpoint/nfs15.dir
+rm -rf $wd
+mkdir $wd
+
+(cd $wd; /tmp/nfs15)
+rm -rf $wd
+
+while mount | grep "on $mntpoint " | grep -q nfs; do
+ umount $mntpoint || sleep 1
+done
+
+rm -f /tmp/nfs15
+exit 0
+EOF
+#include <sys/stat.h>
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define PARALLEL 4
+#define RUNTIME 300
+#define SYNC 0
+
+volatile u_int *share;
+
+static void *
+t1(void *data __unused)
+{
+ atomic_add_int(&share[SYNC], 1);
+ usleep(arc4random() % 8000);
+ raise(SIGABRT);
+
+ return (NULL);
+}
+
+static void *
+t2(void *data __unused)
+{
+ int fd, i, r;
+ char file[80];
+
+ for (i = 0; i < 100; i++) {
+ atomic_add_int(&share[SYNC], 1);
+ snprintf(file, sizeof(file), "file.%06d", i);
+ if ((fd = open(file, O_WRONLY | O_CREAT | O_APPEND,
+ DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ do {
+ r = lockf(fd, F_LOCK, 0);
+ } while (r == -1 && errno == EDEADLK);
+ if (r == -1)
+ err(1, "lockf(%s, F_LOCK)", file);
+ write(fd, "x", 1);
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ err(1, "lseek");
+ if (lockf(fd, F_ULOCK, 0) == -1)
+ err(1, "lockf(%s, F_ULOCK)", file);
+ close(fd);
+ }
+
+ return (NULL);
+}
+
+int
+test(void)
+{
+ pthread_t tid[3];
+ int i, rc;
+
+ for (i = 0; i < 10; i++) {
+ if ((rc = pthread_create(&tid[0], NULL, t2, NULL)) == -1)
+ errc(1, rc, "pthread_create");
+ if ((rc = pthread_create(&tid[1], NULL, t2, NULL)) == -1)
+ errc(1, rc, "pthread_create");
+ if ((rc = pthread_create(&tid[2], NULL, t1, NULL)) == -1)
+ errc(1, rc, "pthread_create");
+
+ if ((rc = pthread_join(tid[0], NULL)) == -1)
+ errc(1, rc, "pthread_join");
+ if ((rc = pthread_join(tid[1], NULL)) == -1)
+ errc(1, rc, "pthread_join");
+ if ((rc = pthread_join(tid[2], NULL)) == -1)
+ errc(1, rc, "pthread_join");
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int i, status;
+
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+
+ for(;;) {
+ if (share[SYNC] > 0)
+ atomic_add_int(&share[SYNC], -1);
+ for (i = 0; i < PARALLEL; i++)
+ kill(pids[i], SIGSTOP);
+ usleep(100 + arc4random() % 1000);
+ for (i = 0; i < PARALLEL; i++)
+ kill(pids[i], SIGCONT);
+ usleep(100 + arc4random() % 400);
+ if (share[SYNC] == 0) { /* If all procs are done */
+ usleep(500);
+ if (share[SYNC] == 0)
+ break;
+ }
+ }
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) != pids[i])
+ err(1, "waitpid");
+ }
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/nfs15lockd.sh b/tools/test/stress2/misc/nfs15lockd.sh
new file mode 100755
index 000000000000..8e744b967b18
--- /dev/null
+++ b/tools/test/stress2/misc/nfs15lockd.sh
@@ -0,0 +1,240 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC Isilon
+# 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 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.
+#
+
+# Demonstrate "wrong handling for suspend".
+# https://www.mail-archive.com/freebsd-current@freebsd.org/msg166333.html
+
+# panic: Failed to register NFS lock locally - error=11
+# https://people.freebsd.org/~pho/stress/log/kostik897.txt
+# Fixed in r302013.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+pgrep -q lockd || { echo "lockd not running."; exit 1; }
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > nfs15lockd.c
+mycc -o nfs15lockd -Wall -Wextra -O2 -g nfs15lockd.c -lpthread || exit 1
+rm -f nfs15lockd.c
+cd $here
+
+mount | grep "on $mntpoint " | grep nfs > /dev/null && umount $mntpoint
+
+[ $# -ne 0 ] &&
+ # Problem only seen with lockd
+ { echo "Not using lockd"; debug="-o nolockd"; }
+mount -t nfs -o tcp -o retrycnt=3 -o soft -o rw $debug \
+ $nfs_export $mntpoint
+sleep 2
+
+s=0
+lockf -t 10 $mntpoint/$$.lock sleep 2 > /tmp/$$.log 2>&1
+if grep -q "No locks available" /tmp/$$.log; then
+ echo "Is lockd running on the remote host?"
+ rm /tmp/$$.log
+ s=1
+fi
+
+wd=$mntpoint/nfs15lockd-`jot -rc 8 a z | tr -d '\n'`.dir
+rm -rf $wd
+mkdir $wd
+
+echo "Expect: nfs15lockd exited on signal 6 (core dumped)"
+(cd $wd; /tmp/nfs15lockd) &
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 600 ]; do
+ pgrep -q nfs15lockd || break
+ sleep 2
+done
+if pgrep -q nfs15lockd; then
+ s=2
+ echo "Thread suspension issue:"
+ ps -lx | grep -v grep | grep nfs15lockd | grep "T+" | \
+ awk '{print $2}' | while read pid; do
+ ps -lp$pid
+ procstat -k $pid
+ kill -9 $pid
+ done
+ pkill nfs15lockd
+fi
+wait
+rm -rf $wd
+
+n=0
+while mount | grep "on $mntpoint " | grep -q nfs; do
+ umount $mntpoint && break
+ n=$((n + 1))
+ if [ $n -gt 60 ]; then
+ fstat -mf $mntpoint
+ s=3
+ break
+ fi
+ sleep 2
+done 2>&1 | awk '!seen[$0]++'
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+
+rm -f /tmp/nfs15lockd nfs15lockd.core
+exit $s
+EOF
+#include <sys/stat.h>
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define PARALLEL 4
+#define RUNTIME 300
+#define SYNC 0
+
+volatile u_int *share;
+
+static void *
+t1(void *data __unused)
+{
+ atomic_add_int(&share[SYNC], 1);
+ usleep(arc4random() % 8000);
+ raise(SIGABRT);
+
+ return (NULL);
+}
+
+static void *
+t2(void *data __unused)
+{
+ int fd, i, r;
+ char file[80];
+
+ for (i = 0; i < 100; i++) {
+ atomic_add_int(&share[SYNC], 1);
+ snprintf(file, sizeof(file), "file.%06d", i);
+ if ((fd = open(file, O_WRONLY | O_CREAT | O_APPEND,
+ DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ do {
+ r = lockf(fd, F_LOCK, 0);
+ } while (r == -1 && (errno == EDEADLK || errno == EINTR));
+ if (r == -1)
+ err(1, "lockf(%s, F_LOCK)", file);
+ write(fd, "x", 1);
+ usleep(arc4random() % 1000);
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ err(1, "lseek");
+ if (lockf(fd, F_ULOCK, 0) == -1)
+ err(1, "lockf(%s, F_ULOCK)", file);
+ close(fd);
+ }
+
+ return (NULL);
+}
+
+int
+test(void)
+{
+ pthread_t tid[3];
+ int i, rc;
+
+ for (i = 0; i < 10; i++) {
+ if ((rc = pthread_create(&tid[0], NULL, t2, NULL)) == -1)
+ errc(1, rc, "pthread_create");
+ if ((rc = pthread_create(&tid[1], NULL, t2, NULL)) == -1)
+ errc(1, rc, "pthread_create");
+ if ((rc = pthread_create(&tid[2], NULL, t1, NULL)) == -1)
+ errc(1, rc, "pthread_create");
+
+ if ((rc = pthread_join(tid[0], NULL)) == -1)
+ errc(1, rc, "pthread_join");
+ if ((rc = pthread_join(tid[1], NULL)) == -1)
+ errc(1, rc, "pthread_join");
+ if ((rc = pthread_join(tid[2], NULL)) == -1)
+ errc(1, rc, "pthread_join");
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int i, status;
+
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+
+ for(;;) {
+ if (share[SYNC] > 0)
+ atomic_add_int(&share[SYNC], -1);
+ for (i = 0; i < PARALLEL; i++)
+ kill(pids[i], SIGSTOP);
+ usleep(1000);
+ for (i = 0; i < PARALLEL; i++)
+ kill(pids[i], SIGCONT);
+ usleep(100 + arc4random() % 400);
+ if (share[SYNC] == 0) { /* If all procs are done */
+ usleep(500);
+ if (share[SYNC] == 0)
+ break;
+ }
+ }
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) != pids[i])
+ err(1, "waitpid");
+ }
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/nfs15lockd2.sh b/tools/test/stress2/misc/nfs15lockd2.sh
new file mode 100755
index 000000000000..dcbef1946ce3
--- /dev/null
+++ b/tools/test/stress2/misc/nfs15lockd2.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Page fault seen:
+# https://people.freebsd.org/~pho/stress/log/kostik907.txt
+# Fixed by: r302019 + r302020.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+./nfs15lockd.sh &
+sleep 10
+mount | grep "on $mntpoint " | grep -q nfs && umount -f $mntpoint
+wait
+exit $?
diff --git a/tools/test/stress2/misc/nfs15lockd3.sh b/tools/test/stress2/misc/nfs15lockd3.sh
new file mode 100755
index 000000000000..fe43efa64a7c
--- /dev/null
+++ b/tools/test/stress2/misc/nfs15lockd3.sh
@@ -0,0 +1,250 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC Isilon
+# 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 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.
+#
+
+# Demonstrate "wrong handling for suspend".
+# https://www.mail-archive.com/freebsd-current@freebsd.org/msg166333.html
+
+# Variation of nfs15lockd.sh
+
+# Threads marked as stopped, but can not be killed.
+# Fixed by r302215.
+
+# Also seen and not fixed:
+# $ fstat -mf /mnt
+# USER CMD PID FD MOUNT INUM MODE SZ|DV R/W
+# $ umount /mnt
+# umount: unmount of /mnt failed: Device busy
+# $
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+pgrep -q lockd || { echo "lockd not running."; exit 1; }
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > nfs15lockd3.c
+mycc -o nfs15lockd3 -Wall -Wextra -O2 -g nfs15lockd3.c -lpthread || exit 1
+rm -f nfs15lockd3.c
+cd $here
+
+mount | grep "on $mntpoint " | grep nfs > /dev/null && umount $mntpoint
+
+[ $# -ne 0 ] &&
+ # Problem only seen with lockd
+ { echo "Not using lockd"; debug="-o nolockd"; }
+mount -t nfs -o tcp -o retrycnt=3 -o soft -o rw $debug \
+ $nfs_export $mntpoint
+sleep 2
+
+s=0
+lockf -t 10 $mntpoint/$$.lock sleep 2 > /tmp/$$.log 2>&1
+if grep -q "No locks available" /tmp/$$.log; then
+ echo "Is lockd running on the remote host?"
+ rm /tmp/$$.log
+ s=1
+fi
+
+wd=$mntpoint/nfs15lockd3-`jot -rc 8 a z | tr -d '\n'`.dir
+rm -rf $wd
+mkdir $wd
+
+(cd $wd; /tmp/nfs15lockd3) &
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 600 ]; do
+ pgrep -q nfs15lockd || break
+ sleep 2
+done
+if pgrep -q nfs15lockd; then
+ s=2
+ echo "Thread suspension issue:"
+ ps -lx | grep -v grep | grep nfs15lockd | grep "T+" | \
+ awk '{print $2}' | while read pid; do
+ ps -lp$pid
+ procstat -k $pid
+ kill -9 $pid
+ done
+ pkill nfs15lockd
+fi
+wait
+rm -rf $wd
+
+n=0
+while mount | grep "on $mntpoint " | grep -q nfs; do
+ umount $mntpoint && break
+ n=$((n + 1))
+ if [ $n -gt 60 ]; then
+ fstat -mf $mntpoint
+ s=3
+ break
+ fi
+ sleep 2
+done
+
+rm -f /tmp/nfs15lockd3 nfs15lockd3.core file.0?????
+exit $s
+EOF
+#include <sys/stat.h>
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define PARALLEL 4
+#define RUNTIME 300
+#define SYNC 0
+
+volatile u_int *share;
+
+static void *
+t1(void *data __unused)
+{
+ atomic_add_int(&share[SYNC], 1);
+ usleep(arc4random() % 8000);
+ raise(SIGABRT);
+
+ return (NULL);
+}
+
+static void *
+t2(void *data __unused)
+{
+ int fd, i, r;
+ char file[80];
+
+ for (i = 0; i < 10; i++) {
+ atomic_add_int(&share[SYNC], 1);
+ snprintf(file, sizeof(file), "file.%06d", i);
+ if ((fd = open(file, O_WRONLY | O_CREAT | O_APPEND,
+ DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ do {
+ r = lockf(fd, F_LOCK, 0);
+ } while (r == -1 && (errno == EDEADLK || errno == EINTR));
+ if (r == -1)
+ err(1, "lockf(%s, F_LOCK)", file);
+ write(fd, "x", 1);
+ usleep(arc4random() % 1000);
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ err(1, "lseek");
+ if (lockf(fd, F_ULOCK, 0) == -1)
+ err(1, "lockf(%s, F_ULOCK)", file);
+ close(fd);
+ }
+
+ return (NULL);
+}
+
+int
+test(void)
+{
+ pthread_t tid[3];
+ int i, rc;
+
+ for (i = 0; i < 5; i++) {
+ if ((rc = pthread_create(&tid[0], NULL, t2, NULL)) == -1)
+ errc(1, rc, "pthread_create");
+ if ((rc = pthread_create(&tid[1], NULL, t2, NULL)) == -1)
+ errc(1, rc, "pthread_create");
+ if ((rc = pthread_create(&tid[2], NULL, t1, NULL)) == -1)
+ errc(1, rc, "pthread_create");
+
+ if ((rc = pthread_join(tid[0], NULL)) == -1)
+ errc(1, rc, "pthread_join");
+ if ((rc = pthread_join(tid[1], NULL)) == -1)
+ errc(1, rc, "pthread_join");
+ if ((rc = pthread_join(tid[2], NULL)) == -1)
+ errc(1, rc, "pthread_join");
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int i, n, status;
+
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ n = 0;
+ while (time(NULL) - start < RUNTIME) {
+ n++;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+
+ for(;;) {
+ if (share[SYNC] > 0)
+ atomic_add_int(&share[SYNC], -1);
+ for (i = 0; i < PARALLEL; i++)
+ kill(pids[i], SIGSTOP);
+ usleep(1000);
+ for (i = 0; i < PARALLEL; i++)
+ kill(pids[i], SIGCONT);
+ usleep(100 + arc4random() % 400);
+ if (share[SYNC] == 0) { /* If all procs are done */
+ usleep(500);
+ if (share[SYNC] == 0)
+ break;
+ }
+ }
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) != pids[i])
+ err(1, "waitpid");
+ }
+ if (n > 2)
+ break;
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/nfs16.sh b/tools/test/stress2/misc/nfs16.sh
new file mode 100755
index 000000000000..b8bc0d1237cf
--- /dev/null
+++ b/tools/test/stress2/misc/nfs16.sh
@@ -0,0 +1,196 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Variation of nfs15.sh, using lockd(8).
+# "panic: Failed to register NFS lock locally - error=11" seen:
+# https://people.freebsd.org/~pho/stress/log/kostik897.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > nfs16.c
+mycc -o nfs16 -Wall -Wextra -O2 -g nfs16.c -lpthread || exit 1
+rm -f nfs16.c
+cd $here
+
+mount | grep "on $mntpoint " | grep nfs > /dev/null && umount $mntpoint
+
+mount -t nfs -o tcp -o retrycnt=3 -o soft -o rw \
+ $nfs_export $mntpoint
+sleep 2
+wd=$mntpoint/nfs16.dir
+rm -rf $wd
+mkdir $wd
+
+(cd $wd; /tmp/nfs16)
+rm -rf $wd
+
+while mount | grep "on $mntpoint " | grep -q nfs; do
+ umount $mntpoint || sleep 1
+done
+
+rm -f /tmp/nfs16
+exit 0
+EOF
+#include <sys/stat.h>
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define PARALLEL 4
+#define RUNTIME 300
+#define SYNC 0
+
+volatile u_int *share;
+
+static void *
+t1(void *data __unused)
+{
+ atomic_add_int(&share[SYNC], 1);
+ usleep(arc4random() % 8000);
+ raise(SIGABRT);
+
+ return (NULL);
+}
+
+static void *
+t2(void *data __unused)
+{
+ int fd, i, r;
+ char file[80];
+
+ for (i = 0; i < 100; i++) {
+ atomic_add_int(&share[SYNC], 1);
+ snprintf(file, sizeof(file), "file.%06d", i);
+ if ((fd = open(file, O_WRONLY | O_CREAT | O_APPEND, DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ do {
+ r = lockf(fd, F_LOCK, 0);
+ } while (r == -1 && errno == EDEADLK);
+ if (r == -1)
+ err(1, "lockf(%s, F_LOCK)", file);
+ write(fd, "x", 1);
+ usleep(arc4random() % 1000);
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ err(1, "lseek");
+ if (lockf(fd, F_ULOCK, 0) == -1)
+ err(1, "lockf(%s, F_ULOCK)", file);
+ close(fd);
+ }
+
+ return (NULL);
+}
+
+int
+test(void)
+{
+ pthread_t tid[3];
+ int i, rc;
+
+ for (i = 0; i < 10; i++) {
+ if ((rc = pthread_create(&tid[0], NULL, t2, NULL)) == -1)
+ errc(1, rc, "pthread_create");
+ if ((rc = pthread_create(&tid[1], NULL, t2, NULL)) == -1)
+ errc(1, rc, "pthread_create");
+ if ((rc = pthread_create(&tid[2], NULL, t1, NULL)) == -1)
+ errc(1, rc, "pthread_create");
+
+ if ((rc = pthread_join(tid[0], NULL)) == -1)
+ errc(1, rc, "pthread_join");
+ if ((rc = pthread_join(tid[1], NULL)) == -1)
+ errc(1, rc, "pthread_join");
+ if ((rc = pthread_join(tid[2], NULL)) == -1)
+ errc(1, rc, "pthread_join");
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int i, status;
+
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+
+ for(;;) {
+ if (share[SYNC] > 0)
+ atomic_add_int(&share[SYNC], -1);
+ for (i = 0; i < PARALLEL; i++)
+ kill(pids[i], SIGSTOP);
+ usleep(1000);
+ for (i = 0; i < PARALLEL; i++)
+ kill(pids[i], SIGCONT);
+ usleep(100 + arc4random() % 400);
+ if (share[SYNC] == 0) { /* If all procs are done */
+ usleep(500);
+ if (share[SYNC] == 0)
+ break;
+ }
+ }
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) != pids[i])
+ err(1, "waitpid");
+ }
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/nfs17.sh b/tools/test/stress2/misc/nfs17.sh
new file mode 100755
index 000000000000..2a012e476e55
--- /dev/null
+++ b/tools/test/stress2/misc/nfs17.sh
@@ -0,0 +1,76 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Copy tests to a NFS FS and run from there.
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+mount | grep "on $mntpoint " | grep -q nfs && umount $mntpoint
+mount -t nfs -o tcp -o retrycnt=3 -o soft -o rw $nfs_export $mntpoint
+grep -q $mntpoint /etc/exports ||
+ { echo "$mntpoint missing from /etc/exports"; exit 0; }
+
+chmod 777 $mntpoint
+
+rm -rf $mntpoint/nfs17; mkdir -p $mntpoint/nfs17
+chmod 0777 $mntpoint/nfs17
+cp -r ../../stress2 $mntpoint/nfs17
+
+log=/tmp/nfs17.log
+export CTRLDIR=$mntpoint/nfs17/stressX.control
+export LOAD=80
+export MAXSWAPPCT=80
+export RUNDIR=$mntpoint/nfs17/stressX
+export runRUNTIME=5m
+export rwLOAD=80
+export TESTPROGS=`cd ..; find testcases/ -perm -1 -type f | \
+ egrep -v "/run/|lockf|dirnprename"`
+
+here=`pwd`
+cd $mntpoint/nfs17/stress2/misc || exit 1
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' 2>&1 | tee $log
+cd $here
+rm -rf $mntpoint/nfs17/stress2
+
+for i in `jot 3`; do
+ umount $mntpoint && break
+ sleep 10
+done
+mount | grep -q "on $mntpoint " && { s=1; umount -f $mntpoint; }
+sed < /tmp/nfs17.log | sed '/Loop/d;/run time/d'
+s=0
+[ `sed < /tmp/nfs17.log | sed '/Loop/d;/run time/d' | wc -l` -ne 0 ] &&
+ s=2
+[ $s -ne 0 ] && echo "Exit value is $s"
+exit $s
diff --git a/tools/test/stress2/misc/nfs2.sh b/tools/test/stress2/misc/nfs2.sh
new file mode 100755
index 000000000000..9d470c18e8f0
--- /dev/null
+++ b/tools/test/stress2/misc/nfs2.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2013 Peter Holm <pho@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 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.
+#
+
+# Test msdos over nfs. "panic: wrong diroffset" seen.
+# This needs to be in /etc/exports: /mnt -maproot=root 127.0.0.1
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+grep -q $mntpoint /etc/exports ||
+ { echo "$mntpoint missing from /etc/exports"; exit 0; }
+[ -x /sbin/mount_msdosfs ] || exit
+
+D=$diskimage
+dd if=/dev/zero of=$D bs=1m count=128 status=none || exit
+
+mount | grep "${mntpoint}2" | grep nfs > /dev/null && umount -f ${mntpoint}2
+mount | grep "$mntpoint" | grep /md > /dev/null && umount -f $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+
+bsdlabel -w md$mdstart auto
+newfs_msdos -F 16 -b 8192 /dev/md${mdstart}$part > /dev/null
+mount -t msdosfs -o rw /dev/md${mdstart}$part $mntpoint
+
+mkdir $mntpoint/stressX
+chmod 777 $mntpoint/stressX
+
+[ ! -d ${mntpoint}2 ] && mkdir ${mntpoint}2
+chmod 777 ${mntpoint}2
+
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw \
+ 127.0.0.1:$mntpoint ${mntpoint}2
+
+export INODES=9999 # No inodes on a msdos fs
+export RUNDIR=${mntpoint}2/stressX
+export runRUNTIME=10m # Run tests for 10 minutes
+(cd ..; ./run.sh disk.cfg)
+
+umount -f ${mntpoint}2 > /dev/null 2>&1
+umount -f $mntpoint > /dev/null 2>&1
+mdconfig -d -u $mdstart
+rm -f $D
diff --git a/tools/test/stress2/misc/nfs3.sh b/tools/test/stress2/misc/nfs3.sh
new file mode 100755
index 000000000000..69ed0275b41d
--- /dev/null
+++ b/tools/test/stress2/misc/nfs3.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2013 Peter Holm <pho@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 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.
+#
+
+# panic: neg mount point vnode list size
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+[ ! -d $mntpoint ] && mkdir $mntpoint
+mount | grep "$mntpoint" | grep nfs > /dev/null && umount $mntpoint
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mntpoint
+sleep 1
+rm -rf $mntpoint/stressX/*
+rm -rf /tmp/stressX.control
+
+export RUNDIR=$mntpoint/nfs3.`jot -rc 8 a z | tr -d '\n'`/stressX
+mkdir -p $RUNDIR
+export runRUNTIME=1m
+rm -rf /tmp/stressX.control/*
+
+su $testuser -c "(cd ..; ./run.sh io.cfg > /dev/null 2>&1)" &
+sleep 50
+
+while mount | grep -q "on $mntpoint "; do
+ umount -f $mntpoint || sleep 1
+done
+kill -9 $!
+../tools/killall.sh
+wait
+exit 0
diff --git a/tools/test/stress2/misc/nfs4.sh b/tools/test/stress2/misc/nfs4.sh
new file mode 100755
index 000000000000..1d986c161154
--- /dev/null
+++ b/tools/test/stress2/misc/nfs4.sh
@@ -0,0 +1,70 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2013 Peter Holm <pho@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 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.
+#
+
+# panic: vm_fault: fault on nofault entry, from vfs_stdcheckexp+0x74
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+[ ! -d $mntpoint ] && mkdir $mntpoint
+mount | grep "$mntpoint" | grep nfs > /dev/null && umount $mntpoint
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mntpoint
+rm -rf $mntpoint/stressX/*
+rm -rf /tmp/stressX.control
+
+export RUNDIR=$mntpoint/nfs4.`jot -rc 8 a z | tr -d '\n'`/stressX
+mkdir -p $RUNDIR
+chmod 777 $RUNDIR
+export runRUNTIME=3m
+rm -rf /tmp/stressX.control/*
+
+su $testuser -c '(cd ..; ./run.sh all.cfg) > /dev/null 2>&1' &
+sleep 60
+
+while mount | grep -q $mntpoint; do
+ umount -f $mntpoint > /dev/null 2>&1
+ sleep 1
+done
+kill -9 $!
+../tools/killall.sh
+wait
+
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mntpoint
+sleep .2
+su $testuser -c "find $RUNDIR -delete 2>/dev/null"
+find $RUNDIR -delete
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1 || exit 0
diff --git a/tools/test/stress2/misc/nfs5.sh b/tools/test/stress2/misc/nfs5.sh
new file mode 100755
index 000000000000..1f311ef47baf
--- /dev/null
+++ b/tools/test/stress2/misc/nfs5.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2013 Peter Holm <pho@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 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.
+#
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+D=$diskimage
+dd if=dev=zero of=$D bs=1m count=128 status=none || exit
+
+mount | grep "${mntpoint}2" | grep nfs > /dev/null && umount -f ${mntpoint}2
+mount | grep "$mntpoint" | grep /md > /dev/null && umount -f $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+mkdir $mntpoint/stressX
+chmod 777 $mntpoint/stressX
+
+[ ! -d ${mntpoint}2 ] && mkdir ${mntpoint}2
+chmod 777 ${mntpoint}2
+
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw 127.0.0.1:$mntpoint \
+ ${mntpoint}2
+
+export RUNDIR=${mntpoint}2/stressX
+export runRUNTIME=4m
+su $testuser -c "(cd ..; ./run.sh disk.cfg > /dev/null 2>&1)" &
+sleep 60
+
+umount -f $mntpoint > /dev/null 2>&1
+umount -f ${mntpoint}2 > /dev/null 2>&1
+
+mdconfig -d -u $mdstart
+rm -f $D
+kill $!
+../tools/killall.sh
+wait
+exit 0
diff --git a/tools/test/stress2/misc/nfs6.sh b/tools/test/stress2/misc/nfs6.sh
new file mode 100755
index 000000000000..98b2a9d40945
--- /dev/null
+++ b/tools/test/stress2/misc/nfs6.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2013 Peter Holm <pho@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 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.
+#
+
+# panic: vn_finished_write: neg cnt
+# http://people.freebsd.org/~pho/stress/log/kostik500.txt
+# http://people.freebsd.org/~pho/stress/log/nfs6.txt
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+grep -q $mntpoint /etc/exports ||
+ { echo "$mntpoint missing from /etc/exports"; exit 0; }
+
+D=$diskimage
+dd if=/dev/zero of=$D bs=1m count=128 status=none || exit
+
+mount | grep "${mntpoint}2" | grep nfs > /dev/null && umount -f ${mntpoint}2
+mount | grep "$mntpoint" | grep /md > /dev/null && umount -f $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+mkdir $mntpoint/stressX
+chmod 777 $mntpoint/stressX
+
+[ ! -d ${mntpoint}2 ] && mkdir ${mntpoint}2
+chmod 777 ${mntpoint}2
+
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw 127.0.0.1:$mntpoint \
+ ${mntpoint}2
+
+export RUNDIR=${mntpoint}2/stressX
+export runRUNTIME=4m
+su $testuser -c "(cd ..; ./run.sh disk.cfg > /dev/null 2>&1)" &
+sleep 60
+
+for i in `jot 10`; do
+ umount -f $mntpoint > /dev/null 2>&1
+ sleep 1
+ mount /dev/md${mdstart}$part $mntpoint
+ sleep 1
+done
+
+umount -f $mntpoint > /dev/null 2>&1
+umount -f ${mntpoint}2 > /dev/null 2>&1
+
+mdconfig -d -u $mdstart
+rm -f $D
+kill $!
+../tools/killall.sh
+wait
+exit 0
diff --git a/tools/test/stress2/misc/nfs7.sh b/tools/test/stress2/misc/nfs7.sh
new file mode 100755
index 000000000000..8fad71000f73
--- /dev/null
+++ b/tools/test/stress2/misc/nfs7.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009-2013 Peter Holm <pho@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 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.
+#
+
+# NFS test excluding lockd
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+[ ! -d $mntpoint ] && mkdir $mntpoint
+mount | grep "$mntpoint" | grep nfs > /dev/null && umount $mntpoint
+mount -t nfs -o nfsv3,tcp,nolockd -o retrycnt=3 -o intr,soft -o rw \
+ $nfs_export $mntpoint
+rm -rf /tmp/stressX.control
+
+export RUNDIR=$mntpoint/nfs7.`jot -rc 8 a z | tr -d '\n'`/stressX
+USE_TIMEOUT=1
+rm -rf $RUNDIR
+mkdir -p $RUNDIR
+chmod 777 $RUNDIR
+export runRUNTIME=10m
+rm -rf /tmp/stressX.control/*
+
+su $testuser -c "(cd ..; ./run.sh marcus.cfg) > /dev/null 2>&1"
+
+umount $mntpoint
+while mount | grep -q $mntpoint; do
+ umount -f $mntpoint > /dev/null 2>&1
+done
+
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mntpoint
+sleep .2
+su $testuser -c "find $RUNDIR -delete 2>/dev/null"
+find $RUNDIR -delete
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1 || exit 0
diff --git a/tools/test/stress2/misc/nfs8.sh b/tools/test/stress2/misc/nfs8.sh
new file mode 100755
index 000000000000..010b3b1ae8e0
--- /dev/null
+++ b/tools/test/stress2/misc/nfs8.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Test scenario for a lock cascade problem with sending SIGSTOP to processes
+# accessing a NFS intr mount.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+[ ! -d $mntpoint ] && mkdir $mntpoint
+mount | grep "$mntpoint" | grep nfs > /dev/null && umount $mntpoint
+mount -t nfs -o nfsv3,tcp,nolockd -o retrycnt=3 -o intr,soft -o rw \
+ $nfs_export $mntpoint
+sleep .2
+
+wdir=$mntpoint/nfs8.sh.dir
+mkdir -p $wdir
+jot 1000 | xargs -I% touch $wdir/%
+for i in `jot 10`; do
+ find $mntpoint > /dev/null 2>&1 &
+ sleep 0.1
+ kill -s STOP $!
+ pids="$pids $!"
+done
+rm -rf $wdir
+
+umount $mntpoint 2>&1 | grep -v "Device busy"
+while mount | grep -q $mntpoint; do
+ umount -f $mntpoint > /dev/null 2>&1
+done
+
+kill -s CONT $pids
+for pid in $pids; do
+ wait $pid
+done
+exit 0
diff --git a/tools/test/stress2/misc/nfs9.sh b/tools/test/stress2/misc/nfs9.sh
new file mode 100755
index 000000000000..b970ab8e564c
--- /dev/null
+++ b/tools/test/stress2/misc/nfs9.sh
@@ -0,0 +1,88 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2013 Peter Holm <pho@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 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.
+#
+
+# Copy of nfs4.sh, where it was discovered that a missing killall.sh script
+# turned up quite a few new panics. For example:
+
+# vfs_mount_destroy: nonzero writeopcount
+# Lock nfs not locked @ kern/vfs_default.c:462
+# Assertion x == LK_SHARERS_LOCK(1) failed at kern/kern_lock.c:236
+
+# "panic: SACK scoreboard must not be empty" seen:
+# https://people.freebsd.org/~pho/stress/log/nfs9-2.txt
+
+# Hang seen:
+# $ ps -lp52442
+# UID PID PPID CPU PRI NI VSZ RSS MWCHAN STAT TT TIME COMMAND
+# 0 52442 52120 0 20 0 11336 2868 nfsreq S 0 0:01.48 rm -rf /mnt/nfs9.dktfkpuf/stressX
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+[ ! -d $mntpoint ] && mkdir $mntpoint
+export RUNDIR=$mntpoint/nfs9.`jot -rc 8 a z | tr -d '\n'`/stressX
+USE_TIMEOUT=1
+s=0
+start=`date +%s`
+for i in `jot 10`; do
+ mount | grep "on $mntpoint " | grep -q nfs && umount $mntpoint
+ mount -t nfs -o tcp -o retrycnt=3 -o intr -o soft \
+ -o rw $nfs_export $mntpoint
+ sleep .5
+
+ rm -rf $RUNDIR 2> /dev/null
+ mkdir -p $RUNDIR || exit 1
+ chmod -R 777 $RUNDIR 2> /dev/null
+ export runRUNTIME=3m
+ rm -rf /tmp/stressX.control/*
+
+ if [ $i -eq 10 -o $((`date +%s` - start)) -ge 600 ]; then
+ ../tools/killall.sh
+ su $testuser -c "rm -rf $RUNDIR" 2> /dev/null
+ rm -rf `dirname $RUNDIR` || s=1
+ done=1
+ else
+ su $testuser -c '(cd ..; ./run.sh all.cfg) > /dev/null \
+ 2>&1' &
+ sleep 60
+ fi
+
+ while mount | grep -q $mntpoint; do
+ umount -f $mntpoint > /dev/null 2>&1
+ done
+ ../tools/killall.sh || break
+ kill -9 $! 2>/dev/null
+ wait
+ [ $done ] && break
+done
+exit $s
diff --git a/tools/test/stress2/misc/nfs_halfpage.sh b/tools/test/stress2/misc/nfs_halfpage.sh
new file mode 100755
index 000000000000..b665e84d7ddf
--- /dev/null
+++ b/tools/test/stress2/misc/nfs_halfpage.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2017 Konstantin Belousov <kib@FreeBSD.org>
+#
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+# Regression test for:
+# Mark pages after EOF as clean after pageout.
+# https://reviews.freebsd.org/D11697
+# Committed as r321580 + r321581.
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/nfs_halfpage.c
+mycc -o nfs_halfpage -Wall -Wextra -O0 -g nfs_halfpage.c || exit 1
+rm -f nfs_halfpage.c
+cd $odir
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+mount | grep "$mntpoint" | grep -q nfs && umount $mntpoint
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mntpoint
+
+file=$mntpoint/nfs_halfpage.file
+/tmp/nfs_halfpage $file
+
+echo "Reboot now to trigger syncing disks loop."
+sleep 60
+
+rm $file
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && s=1 || s=0
+rm -f /tmp/nfs_halfpage
+
+exit $s
+EOF
+/* $Id: nfs_halfpage.c,v 1.2 2017/07/23 09:36:23 kostik Exp kostik $ */
+
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <err.h>
+#include <unistd.h>
+
+int
+main(int argc __unused, char *argv[])
+{
+ char *m;
+ int error, fd, pgsz, sz;
+
+ pgsz = getpagesize();
+ fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, 0666);
+ if (fd == -1)
+ err(1, "open %s", argv[1]);
+ sz = pgsz / 4;
+ error = lseek(fd, sz, SEEK_SET);
+ if (error == -1)
+ err(1, "lseek");
+ error = write(fd, "a", 1);
+ if (error == -1)
+ err(1, "write");
+ else if (error != 1)
+ errx(1, "short write");
+ m = mmap(NULL, sz + 1, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (m == MAP_FAILED)
+ err(1, "mmap");
+ m[0] = 'x';
+}
diff --git a/tools/test/stress2/misc/nfs_halfpage2.sh b/tools/test/stress2/misc/nfs_halfpage2.sh
new file mode 100755
index 000000000000..15895da2da21
--- /dev/null
+++ b/tools/test/stress2/misc/nfs_halfpage2.sh
@@ -0,0 +1,103 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2017 Konstantin Belousov <kib@FreeBSD.org>
+#
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+# Variation of nfs_halfpage.sh with a 2GB file.
+# Mark pages after EOF as clean after pageout.
+# https://reviews.freebsd.org/D11697
+# Committed as r321580 + r321581.
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/nfs_halfpage.c
+mycc -o nfs_halfpage -Wall -Wextra -O0 -g nfs_halfpage.c || exit 1
+rm -f nfs_halfpage.c
+cd $odir
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+mount | grep "$mntpoint" | grep -q nfs && umount $mntpoint
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mntpoint
+
+file=$mntpoint/nfs_halfpage.file
+/tmp/nfs_halfpage $file
+
+ls -l $file
+echo "Reboot now to trigger syncing disks loop."
+sleep 60
+
+rm $file
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1
+rm -f /tmp/nfs_halfpage
+
+exit 0
+EOF
+/* $Id: nfs_halfpage.c,v 1.2 2017/07/23 09:36:23 kostik Exp kostik $ */
+
+#include <sys/types.h>
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <err.h>
+#include <unistd.h>
+
+int
+main(int argc __unused, char *argv[])
+{
+ off_t sz;
+ char *m;
+ int error, fd, pgsz;
+
+ pgsz = getpagesize();
+ fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, 0666);
+ if (fd == -1)
+ err(1, "open %s", argv[1]);
+ sz = pgsz / 4;
+ sz += 2LL * 1024 * 1024 * 1024;
+ error = lseek(fd, sz, SEEK_SET);
+ if (error == -1)
+ err(1, "lseek");
+ error = write(fd, "a", 1);
+ if (error == -1)
+ err(1, "write");
+ else if (error != 1)
+ errx(1, "short write");
+ m = mmap(NULL, sz + 1, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (m == MAP_FAILED)
+ err(1, "mmap");
+ m[sz - 1] = 'x';
+}
diff --git a/tools/test/stress2/misc/nfsdelegation.sh b/tools/test/stress2/misc/nfsdelegation.sh
new file mode 100755
index 000000000000..b84b63e96658
--- /dev/null
+++ b/tools/test/stress2/misc/nfsdelegation.sh
@@ -0,0 +1,169 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Test nfsv4 delegations. Scenario suggestion by kib.
+# "(nfsdelegation), uid 0, was killed: text file modification" seen.
+# Fixed by r316745
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > nfsdelegation.c
+mycc -o nfsdelegation -Wall -Wextra -O0 nfsdelegation.c || exit 1
+rm -f nfsdelegation.c
+
+[ `sysctl -n sysctl vfs.timestamp_precision` -ne 3 ] &&
+ echo "vfs.timestamp_precision must be set to 3"
+[ "`sysctl -ni vfs.nfsd.issue_delegations`" != "1" ] &&
+ { echo "vfs.nfsd.issue_delegations is not enabled"; exit 0; }
+pgrep -q nfscbd || { echo "nfscbd is not running"; exit 0; }
+
+mount | grep "$mntpoint" | grep -q nfs && umount $mntpoint
+opt="-o nocto"
+opt="$opt -o nolockd -o nfsv4"
+mount $opt $nfs_export $mntpoint || exit 1
+sleep .2
+
+wdir=$mntpoint/nfsdelegation.`jot -rc 8 a z | tr -d '\n'`/nfsdelegation
+mkdir -p $wdir || exit 1
+
+delegs=0
+s=0
+(cd $wdir; /tmp/nfsdelegation) &
+while kill -0 $! 2>/dev/null; do
+ r=`nfsstat -ec | grep -A1 Delegs | tail -1 | awk '{print $5}'`
+ [ $r -gt $delegs ] && { delegs=$r; break; }
+done
+wait
+[ $delegs -eq 0 ] && { echo "No delegations detected"; s=2; }
+
+rm -rf $wdir
+umount $mntpoint
+while mount | grep "$mntpoint " | grep -q nfs; do
+ umount -f $mntpoint
+done
+tail -3 /var/log/messages | grep -m1 nfsdelegation: && s=2
+rm -f /tmp/nfsdelegation
+exit $s
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define LOOPS 100
+#define INPUTFILE "/bin/sleep"
+#define PARALLEL 3
+
+static volatile u_int *share;
+
+static int
+tmmap(int idx)
+{
+ struct stat statbuf;
+ pid_t epid, pid;
+ int i;
+ int fdout;
+ char *cmdline[3], *dst, file[128], help[80];
+
+ pid = getpid();
+ cmdline[1] = ".01";
+ cmdline[2] = 0;
+ for (i = 0; i < LOOPS; i++) {
+ sprintf(file,"nfsdelegation.p%05d.%05d", pid, i);
+ cmdline[0] = file;
+
+ snprintf(help, sizeof(help), "cp %s %s; chmod 777 %s", INPUTFILE, file, file);
+ system(help);
+ share[idx] = 0;
+ if ((epid = fork()) == 0) {
+ alarm(60);
+ while (share[idx] == 0)
+ usleep(100);
+ if (execve(cmdline[0], cmdline, NULL) == -1)
+ err(1, "execve");
+ }
+
+ if ((fdout = open(file, O_RDWR)) < 0)
+ err(1, "open(%s)", file);
+ if (fstat(fdout, &statbuf) < 0)
+ err(1, "fstat error");
+
+ if ((dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE |
+ MAP_PRIVATE, MAP_SHARED, fdout, 0)) == (caddr_t) - 1)
+ err(1, "mmap error for output");
+
+ dst[statbuf.st_size] = 1;
+
+ close(fdout);
+ if (munmap(dst, statbuf.st_size) == -1)
+ err(1, "munmap");
+ share[idx] = 1;
+ if (waitpid(epid, NULL, 0) != epid)
+ err(1, "waitpid(%d)", epid);
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ size_t len;
+ int i;
+
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ tmmap(i);
+ }
+
+ for (i = 0; i < PARALLEL; i++) {
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/nfsdepth.sh b/tools/test/stress2/misc/nfsdepth.sh
new file mode 100755
index 000000000000..1abe56e24a80
--- /dev/null
+++ b/tools/test/stress2/misc/nfsdepth.sh
@@ -0,0 +1,239 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# NFS test with deep directories.
+
+# Only issue seen is:
+# nfsdepth: mkdir(d93) l35: Permission denied
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > nfsdepth.c
+mycc -o nfsdepth -Wall -Wextra -g nfsdepth.c || exit 1
+rm -f nfsdepth.c
+cd $odir
+
+mount | grep "$mntpoint" | grep nfs > /dev/null && umount $mntpoint
+mount -t nfs -o tcp -o rw -o soft $nfs_export $mntpoint
+
+work=$mntpoint/nfsdepth.`jot -rc 8 a z | tr -d '\n'`.dir
+mkdir -p $work
+chmod 777 $work
+
+su $testuser -c "cd $work; /tmp/nfsdepth"
+s=$?
+if [ $s -eq 0 ]; then
+ su $testuser -c "rm -rf $work 2>/dev/null"
+ rm -rf $work
+else
+ find $work -ls
+fi
+
+umount $mntpoint > /dev/null 2>&1
+while mount | grep "$mntpoint" | grep -q nfs; do
+ umount $mntpoint > /dev/null 2>&1
+done
+
+rm -f /tmp/nfsdepth
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static unsigned long actual, size;
+volatile int done_testing;
+int fail;
+
+#define DEPTH 200
+#define PARALLEL 8
+#define RUNTIME 180
+
+void
+handler(int s __unused)
+{
+ done_testing = 1;
+}
+
+void
+mkDir(char *path, int level) {
+ int n, r;
+ char newPath[MAXPATHLEN + 1];
+
+ n = 0;
+ do {
+ r = mkdir(path, 0770);
+ if (r == -1 && errno == EACCES && n < 10) {
+ warn("mkdir(%s) l%d", path, __LINE__);
+ n++;
+ errno = EAGAIN;
+ usleep(10000);
+ }
+ if (r == -1 && errno == EINTR)
+ (void)rmdir(path);
+ } while (r == -1 && (errno == EINTR || errno == EAGAIN));
+
+ if (r == -1) {
+ warn("mkdir(%s), pid %d, l%d", path, getpid(), __LINE__);
+ fail++;
+ } else {
+ actual++;
+ do {
+ r = chdir (path);
+ if (r == -1 && errno == EACCES && n < 10) {
+ warn("chdir(%s) .%d", path, __LINE__);
+ n++;
+ errno = EAGAIN;
+ usleep(10000);
+ }
+ } while (r == -1 && (errno == EINTR || errno == EAGAIN));
+ if (r == -1)
+ err(1, "chdir(%s), pid %d, l%d", path, getpid(), __LINE__);
+ }
+
+ if (done_testing == 0 && fail == 0 && level < (int)size) {
+ sprintf(newPath,"d%d", level + 1);
+ mkDir(newPath, level + 1);
+ }
+}
+
+void
+rmDir(char *path, int level) {
+ int n, r;
+ char newPath[MAXPATHLEN + 1];
+
+ if (level == 0)
+ return;
+
+ if (level < (int)actual) {
+ sprintf(newPath,"d%d", level+1);
+ rmDir(newPath, level+1);
+ }
+ n = 0;
+ do {
+ r = chdir ("..");
+ if (r == -1 && errno == EACCES && n < 10) {
+ warn("chdir(%s) l%d", path, __LINE__);
+ n++;
+ errno = EAGAIN;
+ usleep(10000);
+ }
+ } while (r == -1 && (errno == EINTR || errno == EAGAIN));
+ if (r == -1)
+ err(1, "chdir(%s), pid %d, l%d", "..", getpid(), __LINE__);
+ n = 0;
+ do {
+ r = rmdir(path);
+ if (r == -1 && errno == EACCES && n < 10) {
+ warn("rmdir(%s) l%d", path, __LINE__);
+ n++;
+ errno = EAGAIN;
+ usleep(10000);
+ }
+ } while (r == -1 && (errno == EINTR || errno == EAGAIN));
+ if (r == -1)
+ err(1, "rmdir(%s), pid %d, l%d", path, getpid(), __LINE__);
+}
+
+int
+test2(void)
+{
+ char path[MAXPATHLEN + 1];
+
+ fail = actual = 0;
+ umask(0);
+ sprintf(path,"p%05d.d%d", getpid(), 1);
+ mkDir(path, 1);
+ rmDir(path, 1);
+
+ _exit (fail);
+}
+
+int
+test(void)
+{
+ pid_t pid;
+ time_t start;
+ int status;
+
+ size = (arc4random() % DEPTH) + 1;
+
+ signal(SIGHUP, handler);
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME && fail == 0) {
+ if ((pid = fork()) == 0) {
+ done_testing = 0;
+ test2();
+ }
+
+ status = 0;
+ while (wait4(pid, &status, WNOHANG, NULL) != pid) {
+ if (kill(pid, SIGHUP) == -1)
+ err(1, "kill(%d)", pid);
+ usleep(100000 + (arc4random() % 10000));
+ }
+ if (status != 0)
+ fail++;
+ }
+
+ _exit (status != 0);
+}
+
+int
+main(void)
+{
+ int e, i, pids[PARALLEL], status;
+
+ e = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ waitpid(pids[i], &status, 0);
+ e += status == 0 ? 0 : 1;
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/nfsrename.sh b/tools/test/stress2/misc/nfsrename.sh
new file mode 100755
index 000000000000..b6513fba1553
--- /dev/null
+++ b/tools/test/stress2/misc/nfsrename.sh
@@ -0,0 +1,224 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2013 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# Test scenario by jhb@
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > nfsrename.c
+mycc -o nfsrename -Wall nfsrename.c
+rm -f nfsrename.c
+cd $odir
+
+mount | grep "$mntpoint" | grep nfs > /dev/null && umount $mntpoint
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mntpoint
+
+for i in `jot 10`; do
+ /tmp/nfsrename $mntpoint/nfsrename.$i > /dev/null 2>&1 &
+ pids="$pids $!"
+done
+s=0
+for i in $pids; do
+ wait $i
+ [ $? -ne 0 ] && s=1
+done
+pkill nfsrename
+rm -f $mntpoint/nfsrename.*
+
+umount $mntpoint > /dev/null 2>&1
+while mount | grep "$mntpoint" | grep -q nfs; do
+ umount -f $mntpoint > /dev/null 2>&1
+done
+
+rm -f /tmp/nfsrename
+exit $s
+
+EOF
+/*
+ * Try to expose races with doing renames over NFS that require silly
+ * renames. This results in 2 different RENAME RPCs leaving a race
+ * window where the file may not exist. It also appears that FreeBSD
+ * with shared lookups in NFS can get confused and possibly reference
+ * the sillyrenamed file in lookup but the file is deleted by the time
+ * open gets to it.
+ */
+
+#include <err.h>
+#include <libgen.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static char *filename;
+static char *dir;
+
+#define RUNTIME 720
+
+static void
+usage(void)
+{
+
+ fprintf(stderr, "nfsrename: [-n children] file\n");
+ exit(1);
+}
+
+static void
+read_file(void)
+{
+ FILE *fp;
+ char buffer[4096];
+
+ fp = fopen(filename, "r");
+ if (fp == NULL) {
+ return;
+ }
+ while (!feof(fp)) {
+ if (fread(buffer, sizeof(buffer), 1, fp) < sizeof(buffer))
+ break;
+ }
+ if (ferror(fp))
+ warnx("fread encountered an error");
+ fclose(fp);
+}
+
+static void
+write_file(void)
+{
+ FILE *fp;
+ char path[1024];
+ int fd;
+
+ snprintf(path, sizeof(path), "%s/nfsrename.XXXXXX", dir);
+ fd = mkstemp(path);
+ if (fd < 0) {
+ warn("mkstemp");
+ return;
+ }
+
+ fp = fdopen(fd, "w");
+ if (fp == NULL) {
+ warn("fdopen:writer");
+ close(fd);
+ unlink(path);
+ }
+
+ fprintf(fp, "blah blah blah garbage %ld\n", random());
+ fclose(fp);
+ if (rename(path, filename) < 0) {
+ warn("rename");
+ unlink(path);
+ }
+}
+
+static void
+random_sleep(int base, int slop)
+{
+ long val;
+
+ val = random() % slop;
+ usleep(base + val);
+}
+
+static void
+child(void)
+{
+ time_t start;
+
+ start = time(NULL);
+ for (;;) {
+ random_sleep(500, 50);
+ read_file();
+ if (time(NULL) - start > RUNTIME)
+ errx(1, "Timed out");
+ }
+ exit(0);
+}
+
+int
+main(int ac, char **av)
+{
+ time_t start;
+ long i, nchild;
+ char *cp;
+ int ch;
+
+ nchild = 1;
+ while ((ch = getopt(ac, av, "n:")) != -1) {
+ switch (ch) {
+ case 'n':
+ nchild = strtol(optarg, &cp, 0);
+ if (*cp != '\0')
+ errx(1, "Invalid count %s", optarg);
+ break;
+ case '?':
+ default:
+ usage();
+ }
+ }
+ ac -= optind;
+ av += optind;
+
+ if (ac == 0)
+ errx(1, "Missing filename");
+ else if (ac > 1)
+ errx(1, "Extra arguments");
+
+ filename = av[0];
+ dir = dirname(filename);
+ srandomdev();
+ write_file();
+
+ for (i = 0; i < nchild; i++) {
+ switch (fork()) {
+ case 0:
+ child();
+ case -1:
+ err(1, "fork");
+ }
+ }
+
+ start = time(NULL);
+ for (i = 0; i < 10000; i++) {
+ random_sleep(1500, 1000);
+ write_file();
+ if (time(NULL) - start > RUNTIME)
+ errx(1, "Timed out");
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/nfssillyrename.sh b/tools/test/stress2/misc/nfssillyrename.sh
new file mode 100755
index 000000000000..8cb63c920533
--- /dev/null
+++ b/tools/test/stress2/misc/nfssillyrename.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# "rm: src/: Directory not empty" seen.
+
+# See also nfsrename.sh
+
+# Test scenario by bdrewery@
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+mount | grep "$mntpoint" | grep -q nfs && umount $mntpoint
+mount -t nfs -o tcp -o retrycnt=3 -o soft -o rw $nfs_export $mntpoint ||
+ exit 1
+
+mp=$mntpoint/sillyrename
+rm -rf $mp
+mkdir -p $mp
+
+status=0
+cd $mp
+mkdir src dst
+touch src/foo
+ln src/foo dst/foo
+tail -F dst/foo &
+rm -rf src/ || status=1
+
+cd /
+kill $!
+wait
+rm -rf $mp
+umount $mntpoint
+while mount | grep "$mntpoint " | grep -q nfs; do
+ umount -f $mntpoint
+done
+exit $status
diff --git a/tools/test/stress2/misc/nullfs.sh b/tools/test/stress2/misc/nullfs.sh
new file mode 100755
index 000000000000..1424a90eed44
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Stress test by performing parallel calls to mount and umount. Alternate
+# between forced and non-forced unmounts.
+
+# https://people.freebsd.org/~pho/stress/log/kostik169.txt
+# https://people.freebsd.org/~pho/stress/log/kostik487.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mounts=15 # Number of parallel scripts
+: ${nullfs_srcdir:=/tmp}
+: ${nullfs_dstdir:=$mntpoint}
+
+if [ $# -eq 0 ]; then
+ for i in `jot $mounts`; do
+ [ ! -d ${nullfs_dstdir}$i ] && mkdir ${nullfs_dstdir}$i
+ mount | grep -q " ${nullfs_dstdir}$i " &&
+ umount ${nullfs_dstdir}$i
+ done
+
+ # start the parallel tests
+ for i in `jot $mounts`; do
+ ./$0 $i &
+ done
+ wait
+
+ for i in `jot $mounts`; do
+ umount ${nullfs_dstdir}$i > /dev/null 2>&1
+ done
+ exit 0
+else
+ # The test: Parallel mount and unmounts
+ start=`date '+%s'`
+ while [ `date '+%s'` -lt $((start + 300)) ]; do
+ m=$1
+ mount_nullfs $nullfs_srcdir ${nullfs_dstdir}$m > \
+ /dev/null 2>&1
+ opt=`[ $(( m % 2 )) -eq 0 ] && echo -f`
+ while mount | grep -q ${nullfs_dstdir}$m; do
+ umount $opt ${nullfs_dstdir}$m > /dev/null 2>&1
+ done
+ done
+fi
diff --git a/tools/test/stress2/misc/nullfs10.sh b/tools/test/stress2/misc/nullfs10.sh
new file mode 100755
index 000000000000..4444bcca2913
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs10.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Regression test:
+
+# There is an issue, namely, when the lower file is already
+# opened r/w, but its nullfs alias is executed. This situation obviously
+# shall result in ETXTBUSY, but it currently does not.
+
+# Test scenario by kib@
+
+. ../default.cfg
+
+mnt2=${mntpoint}2
+mount | grep -q $mnt2 && umount $mnt2
+
+[ -d $mnt2 ] || mkdir $mnt2
+mount | grep $mnt2 | grep -q /dev/md && umount -f $mnt2
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+mount -t nullfs $mntpoint $mnt2
+
+cp /bin/ls $mntpoint
+chmod +w $mntpoint/ls
+sleep 2 >> $mntpoint/ls &
+sleep .5
+# This line should cause a "/mnt2/ls: Text file busy"
+$mnt2/ls -l /bin/ls $mntpoint $mnt2 && echo FAIL || echo OK
+kill $!
+wait
+
+while mount | grep -q "$mnt2 "; do
+ umount $mnt2 || sleep 1
+done
+
+while mount | grep -q "$mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/nullfs11.sh b/tools/test/stress2/misc/nullfs11.sh
new file mode 100755
index 000000000000..9d02abd3b3ca
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs11.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# nullfs cache / nocache benchmark
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+export LANG=C
+CACHE=nullfs11-cache.log
+NOCACHE=nullfs11-nocache.log
+mp1=$mntpoint
+mp2=${mntpoint}2
+[ -d $mp2 ] || mkdir $mp2
+rm -f $CACHE $NOCACHE
+
+mycc -o /tmp/fstool ../tools/fstool.c
+
+test() {
+ opt=$1
+ mount | grep -wq $mp2 && umount $mp2
+ mount | grep -wq $mp1 && umount $mp1
+ mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+ mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+ bsdlabel -w md$mdstart auto
+ newfs $newfs_flags md${mdstart}$part > /dev/null
+ mount /dev/md${mdstart}$part $mp1
+
+ mount -t nullfs $opt $mp1 $mp2
+
+ i=1
+ (mkdir $mp2/test$i; cd $mp2/test$i; /tmp/fstool -l -f 50 -n 500 -s 4k)
+ rm -rf $mp2/test$i
+ sync
+ sleep 5
+
+ parallel=4
+ for j in `jot 5`; do
+ s=`date '+%s'`
+ for i in `jot $parallel`; do
+ (mkdir $mp2/test$i; cd $mp2/test$i; \
+ /tmp/fstool -l -f 50 -n 500 -s $((i * 4))k) &
+ done
+ for i in `jot $parallel`; do
+ wait
+ done
+ rm -rf $mp2/*
+ echo $((`date '+%s'` - $s))
+ done
+ while mount | grep -wq $mp2; do
+ umount $mp2 || sleep 1
+ done
+ while mount | grep $mp1 | grep -q /dev/md; do
+ umount $mp1 || sleep 1
+ done
+ mdconfig -d -u $mdstart
+}
+
+test "-o nocache" > $NOCACHE
+test "-o cache" > $CACHE
+
+ministat -s -w 60 $NOCACHE $CACHE
+rm -f /tmp/fstool $CACHE $NOCACHE
diff --git a/tools/test/stress2/misc/nullfs12.sh b/tools/test/stress2/misc/nullfs12.sh
new file mode 100755
index 000000000000..12671bb695c1
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs12.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "umount -f" test with files open for writing
+# "panic: vputx: missed vn_close" seen.
+# Scenario by kib@. Fixed in r245262.
+
+. ../default.cfg
+
+mnt2=${mntpoint}2
+mount | grep -q $mnt2 && umount $mnt2
+
+[ -d $mnt2 ] || mkdir $mnt2
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+mount -t nullfs $mntpoint $mnt2
+
+(sleep 5 > $mnt2/log) &
+sleep 1
+umount -f $mnt2
+
+kill $!
+wait
+
+while mount | grep -q "$mnt2 "; do
+ umount $mnt2 || sleep 1
+done
+
+while mount | grep -q "$mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/nullfs13.sh b/tools/test/stress2/misc/nullfs13.sh
new file mode 100755
index 000000000000..5c51621a8c94
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs13.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# kern/178238 "nullfs don't release i-nodes on unlink"
+# See also nullfs16.sh
+# Fixed by: r292961.
+
+. ../default.cfg
+
+nullfsmp=${mntpoint}2
+mount | grep -q "$nullfsmp " && umount $nullfsmp
+
+[ -d $nullfsmp ] || mkdir $nullfsmp
+mount | grep "$mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+mount -t nullfs $mntpoint $nullfsmp
+
+(cd $nullfsmp; jot 20 | xargs touch)
+rm $nullfsmp/*
+
+(cd $nullfsmp; jot 20 | xargs touch)
+rm $mntpoint/*
+
+(cd $mntpoint; jot 20 | xargs touch)
+rm $mntpoint/*
+
+(cd $mntpoint; jot 20 | xargs touch)
+rm $nullfsmp/*
+
+ino=`df -i $nullfsmp | tail -1 | awk '{print $6}'`
+if [ $ino -ne 2 ]; then
+ echo FAIL
+ df -i $mntpoint $nullfsmp
+ echo "ls -la $mntpoint $nullfsmp"
+ ls -la $mntpoint $nullfsmp
+fi
+
+while mount | grep -q "$nullfsmp "; do
+ umount $nullfsmp || sleep 1
+done
+
+while mount | grep -q "$mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/nullfs14.sh b/tools/test/stress2/misc/nullfs14.sh
new file mode 100755
index 000000000000..96bcb470dc40
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs14.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Page fault seen in fifo_close()
+
+. ../default.cfg
+
+nullfsmp=${mntpoint}2
+mount | grep -q "$nullfsmp " && umount $nullfsmp
+
+[ -d $nullfsmp ] || mkdir $nullfsmp
+mount | grep "$mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+#chmod 777 $mntpoint
+mount -t nullfs $mntpoint $nullfsmp
+
+mkfifo $nullfsmp/fifo
+rm $mntpoint/fifo # Triggers page fault
+
+while mount | grep -q "$nullfsmp "; do
+ umount $nullfsmp || sleep 1
+done
+
+while mount | grep -q "$mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/nullfs15.sh b/tools/test/stress2/misc/nullfs15.sh
new file mode 100755
index 000000000000..fcaca4af8cb9
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs15.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# panic: lockmgr still held. Fixed in r250852.
+# Test scenario by antoine@
+
+nullfsmp=${mntpoint}2
+mount | grep -q "$nullfsmp " && umount $nullfsmp
+
+[ -d $nullfsmp ] || mkdir $nullfsmp
+mount | grep "$mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+mount -t nullfs $mntpoint $nullfsmp
+
+mkdir $mntpoint/.new_packages $nullfsmp/new_packages
+mount -t nullfs $mntpoint/.new_packages $nullfsmp/new_packages
+dd if=/dev/zero of=$nullfsmp/new_packages/bar count=20000 status=none
+mv $mntpoint/.new_packages/bar /tmp/
+rm -rf $mntpoint/.new_packages
+umount -f $nullfsmp/new_packages
+
+while mount | grep -q "$nullfsmp "; do
+ umount $nullfsmp || sleep 1
+done
+
+while mount | grep -q "$mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/bar
+exit 0
diff --git a/tools/test/stress2/misc/nullfs16.sh b/tools/test/stress2/misc/nullfs16.sh
new file mode 100755
index 000000000000..32f6d006bda6
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs16.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# [Bug 178238] [nullfs] nullfs don't release i-nodes on unlink.
+# Test scenaro by: noah.bergbauer@tum.de
+# Fixed by: r292961
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+mount -o size=1g -t tmpfs tmpfs $mntpoint
+
+mp2=${mntpoint}2
+[ -d $mp2 ] || mkdir -p $mp2
+mount | grep -q "on $mp2 " && umount -f $mp2
+mount -t nullfs $mntpoint $mp2
+
+# Expect second dd to fail
+for i in `jot 2`; do
+ dd if=/dev/zero of=$mp2/file bs=1m count=1023 \
+ status=none
+ rm -f $mp2/file
+done
+if [ `df -i $mp2 | tail -1 | awk '{print $5}'` = "100%" ]; then
+ echo FAIL
+ s=1
+ ls -al $mntpoint $mp2
+ df -i | egrep "${mntpoint}$|${mp2}$"
+fi
+
+umount $mp2
+umount $mntpoint
+
+exit $s
diff --git a/tools/test/stress2/misc/nullfs17.sh b/tools/test/stress2/misc/nullfs17.sh
new file mode 100755
index 000000000000..121225f67c00
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs17.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Variation of nullfs.sh
+
+# "panic: LK_RETRY set with incompatible flags (0x202400) or
+# an error occured (11)" seen.
+# https://people.freebsd.org/~pho/stress/log/matt001.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mounts=15 # Number of parallel scripts
+: ${nullfs_srcdir:=/tmp}
+: ${nullfs_dstdir:=$mntpoint}
+CONT=/tmp/nullfs17.continue
+
+if [ $# -eq 0 ]; then
+ for i in `jot $mounts`; do
+ [ ! -d ${nullfs_dstdir}$i ] && mkdir ${nullfs_dstdir}$i
+ mount | grep -q " ${nullfs_dstdir}$i " &&
+ umount ${nullfs_dstdir}$i
+ done
+
+ # start the parallel tests
+ for i in `jot $mounts`; do
+ ./$0 $i &
+ ./$0 find $i &
+ done
+ wait
+
+ for i in `jot $mounts`; do
+ umount ${nullfs_dstdir}$i > /dev/null 2>&1
+ done
+ exit 0
+else
+ if [ $1 = find ]; then
+ while [ -f $CONT ]; do
+ find ${nullfs_dstdir}$2 -type f -maxdepth 2 -ls > \
+ /dev/null 2>&1
+ done
+ else
+ # The test: Parallel mount and unmounts
+ touch $CONT
+ start=`date '+%s'`
+ while [ `date '+%s'` -lt $((start + 300)) ]; do
+ m=$1
+ mount_nullfs $nullfs_srcdir ${nullfs_dstdir}$m > \
+ /dev/null 2>&1
+ opt=`[ $(( m % 2 )) -eq 0 ] && echo -f`
+ while mount | grep -q ${nullfs_dstdir}$m; do
+ umount $opt ${nullfs_dstdir}$m > \
+ /dev/null 2>&1
+ done
+ done
+ rm -f $CONT
+ fi
+fi
diff --git a/tools/test/stress2/misc/nullfs18.sh b/tools/test/stress2/misc/nullfs18.sh
new file mode 100755
index 000000000000..1f3320bff1e6
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs18.sh
@@ -0,0 +1,133 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Demonstate nullfs(5) inode leak.
+# Fixed by r295717.
+
+. ../default.cfg
+
+N=3
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs -n md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export KBLOCKS=$(($1 / N))
+export INODES=$(($2 / N))
+
+export runRUNTIME=2m
+export LOAD=80
+export symlinkLOAD=80
+export rwLOAD=80
+export TESTPROGS="
+testcases/rw/rw
+testcases/creat/creat
+testcases/mkdir/mkdir
+"
+
+for i in `jot $N 1`; do
+ eval mp$i=${mntpoint}$i
+done
+
+for i in `jot $N 1`; do
+ eval mp=\$mp$i
+ [ -d $mp ] || mkdir -p $mp
+ mount | grep $mp | grep -q nullfs && umount -f $mp
+ msrc=$mntpoint/d$i
+ mkdir -p $msrc
+ chmod 777 $msrc
+ mount -t nullfs $msrc $mp
+ chmod 777 $mp
+ export RUNDIR=$mp/stressX
+ export CTRLDIR=$mp/stressX.control
+ mkdir $RUNDIR $CTRLDIR
+ chmod 777 $RUNDIR $CTRLDIR
+ su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > \
+ /dev/null 2>&1 &
+ mps="$mps $mp"
+done
+
+(cd ../testcases/swap; ./swap -t 10m -i 20 > /dev/null 2>&1) &
+sleep 1
+while pgrep -q run; do
+ find $mps -ls > /dev/null 2>&1
+done
+while pgrep -q swap; do
+ pkill -9 swap
+done
+wait
+
+(cd $mntpoint; find . -delete)
+sync; sleep 1; sync; sleep 1; sync
+inodes=`df -i $mntpoint | tail -1 | awk '{print $6}'`
+if [ $inodes -ne 4 ]; then
+ echo "FAIL 1"
+ e=1
+ mount | sed -n "1p;/${mntpoint#/}/p"
+ echo
+ df -ik | sed -n "1p;/${mntpoint#/}/p"
+ printf "\nfind ${mntpoint}* -ls\n"
+ find ${mntpoint}* -ls
+
+ for i in `jot $N 1`; do
+ eval mp=\$mp$i
+ echo "umount $mp"
+ mount | grep $mp | grep -q nullfs && umount $mp
+ done
+
+ echo
+ df -ik | sed -n "1p;/${mntpoint#/}/p"
+else
+ for i in `jot $N 1`; do
+ eval mp=\$mp$i
+ mount | grep $mp | grep -q nullfs && umount $mp
+ done
+ inodes=`df -i $mntpoint | tail -1 | awk '{print $6}'`
+ if [ $inodes -ne 1 ]; then
+ echo "FAIL 2"
+ e=2
+ mount | sed -n "1p;/${mntpoint#/}/p"
+ echo
+ df -ik | sed -n "1p;/${mntpoint#/}/p"
+ fi
+fi
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+exit $e
diff --git a/tools/test/stress2/misc/nullfs19.sh b/tools/test/stress2/misc/nullfs19.sh
new file mode 100755
index 000000000000..fd5f16f4b780
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs19.sh
@@ -0,0 +1,90 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Simplified version of nullfs18.sh
+
+. ../default.cfg
+
+N=3
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs -n $newfs_flags md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export KBLOCKS=$(($1 / N))
+export INODES=$(($2 / N))
+
+export runRUNTIME=2m
+export LOAD=80
+export TESTPROGS="
+testcases/creat/creat
+testcases/link/link
+testcases/mkdir/mkdir
+testcases/fts/fts
+"
+
+for i in `jot $N 1`; do
+ eval mp$i=${mntpoint}$i
+done
+
+for i in `jot $N 1`; do
+ eval mp=\$mp$i
+ [ -d $mp ] || mkdir -p $mp
+ mount | grep $mp | grep -q nullfs && umount -f $mp
+ msrc=$mntpoint/d$i
+ mkdir -p $msrc
+ chmod 777 $msrc
+ mount -t nullfs $msrc $mp
+ chmod 777 $mp
+ export RUNDIR=$mp/stressX
+ export CTRLDIR=$mp/stressX.control
+ mkdir $RUNDIR $CTRLDIR
+ chmod 777 $RUNDIR $CTRLDIR
+ su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > \
+ /dev/null 2>&1 &
+done
+wait
+
+for i in `jot $N 1`; do
+ eval mp=\$mp$i
+ mount | grep $mp | grep -q nullfs && umount $mp
+done
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/nullfs2.sh b/tools/test/stress2/misc/nullfs2.sh
new file mode 100755
index 000000000000..96cf57407f9c
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs2.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Simple nullfs test scenario.
+
+# Deadlock seen:
+# https://people.freebsd.org/~pho/stress/log/nullfs2-2.txt
+# suj34.sh seems to trigger the same problem.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# NULLFS(5) and SUJ has known issues.
+mount | grep "on `df $RUNDIR | sed '1d;s/.* //'` " | \
+ grep -q "journaled soft-updates" &&
+ { echo "Skipping test due to SUJ."; exit 0; }
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+
+mount -t nullfs $RUNDIR $mntpoint
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=10m
+(cd ..; ./run.sh marcus.cfg)
+
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
diff --git a/tools/test/stress2/misc/nullfs20.sh b/tools/test/stress2/misc/nullfs20.sh
new file mode 100755
index 000000000000..78c5295bae32
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs20.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# VOP_LOOKUP: 0xfffff8014d6fe000 is not locked but should be
+# https://people.freebsd.org/~pho/stress/log/nullfs20.txt
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs -n -b 4096 -f 512 -i 1024 md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+mp2=$mntpoint$((mdstart + 1))
+[ -d $mp2 ] || mkdir -p $mp2
+mount | grep -wq $mp2 && umount $mp2
+mount -t nullfs $mntpoint $mp2
+
+export runRUNTIME=10m
+export RUNDIR=$mp2/stressX
+
+export LOAD=100
+export ftsLOAD=100
+export TESTPROGS="
+testcases/fts/fts
+testcases/swap/swap
+testcases/link/link
+"
+
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
+
+while mount | grep $mp2 | grep -q nullfs; do
+ umount $mp2 || sleep 1
+done
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ n=$((n + 1))
+ [ $n -gt 30 ] && { echo FAIL; exit 1; }
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/nullfs21.sh b/tools/test/stress2/misc/nullfs21.sh
new file mode 100755
index 000000000000..209ed65d1d3f
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs21.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# mv <nullfs src> <origin src> removes the file. This is OK on FreeBSD.
+# This is handled on Linux with "mount --bind".
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 512m -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs -n -b 4096 -f 512 -i 1024 md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+
+mp2=${mntpoint}2
+[ -d $mp2 ] || mkdir -p $mp2
+mount | grep -wq $mp2 && umount $mp2
+mount -t nullfs $mntpoint $mp2
+
+touch $mntpoint/file
+
+# mv's rename(2) fails as this is a "cross mount" ->
+# rm dst; cp src dst; rm src
+
+mv $mp2/file $mountpoint/file
+[ -f $mntpoint/file ] ||
+ { ls -ali $mntpoint $mp2; status=0; }
+# { echo FAIL; ls -ali $mntpoint $mp2; status=1; }
+
+while mount | grep $mp2 | grep -q nullfs; do
+ umount $mp2 || sleep 1
+done
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ n=$((n + 1))
+ [ $n -gt 30 ] && { echo FAIL; status=2; }
+done
+mdconfig -d -u $mdstart
+exit $status
diff --git a/tools/test/stress2/misc/nullfs22.sh b/tools/test/stress2/misc/nullfs22.sh
new file mode 100755
index 000000000000..f7652d71bf40
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs22.sh
@@ -0,0 +1,247 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# fcntl(2) locking scenario, using UFS and a nullfs mount.
+# No problems seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > nullfs22.c
+mycc -o nullfs22 -Wall -Wextra -O0 -g nullfs22.c || exit 1
+rm -f nullfs22.c
+
+mp2=${mntpoint}2
+[ -d $mp2 ] || mkdir -p $mp2
+mount | grep -q "on $mp2 " && umount $mp2
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 512m -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs -n md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+mount -t nullfs $mntpoint $mp2
+
+/tmp/nullfs22 $mntpoint $mp2
+status=$?
+
+while mount | grep -q "on $mp2 "; do
+ umount $mp2
+done
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/nullfs22
+exit $status
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define LOOPS 1024
+#define N (512)
+#define PARALLEL 4
+
+#define DONE 1
+#define SYNC 0
+
+int fd;
+volatile u_int *share;
+char name1[80], name2[80];
+
+static void
+ahandler(int s __unused)
+{
+ fprintf(stderr, "In alarm handler\n");
+ unlink(name1);
+ _exit(1);
+}
+
+void
+add(int n, int increment)
+{
+ struct flock fl;
+ off_t pos;
+ long val, oval;
+ int r;
+
+ pos = n * sizeof(val);
+ memset(&fl, 0, sizeof(fl));
+ fl.l_start = pos;
+ fl.l_len = sizeof(val);
+ fl.l_type = F_WRLCK;
+ fl.l_whence = SEEK_SET;
+
+ while (fcntl(fd, F_SETLKW, &fl) < 0) {
+ if (errno != EAGAIN)
+ err(1, "F_SETLKW (child)");
+ usleep(100);
+ }
+
+ if (lseek(fd, pos, SEEK_SET) == -1)
+ err(1, "lseek");
+ oval = 999999;
+ while ((r = read(fd, &val, sizeof(val)) != sizeof(val))) {
+ if (r == -1 && errno != EAGAIN)
+ err(1, "read");
+ if (lseek(fd, pos, SEEK_SET) == -1)
+ err(1, "lseek");
+ }
+ oval = val;
+ val = val + increment;
+#if defined(DEBUG)
+ fprintf(stderr, "add(%d, %d) @ pos %ld: %ld = %ld + %d\n",
+ n, increment, (long)pos, val, oval, increment);
+#endif
+ if (lseek(fd, pos, SEEK_SET) == -1)
+ err(1, "lseek");
+ while ((r = write(fd, &val, sizeof(val)) != sizeof(val))) {
+ if (r == -1 && errno != EAGAIN)
+ err(1, "write");
+ if (lseek(fd, pos, SEEK_SET) == -1)
+ err(1, "lseek");
+ }
+
+ fl.l_type = F_UNLCK;
+ if (fcntl(fd, F_SETLK, &fl) < 0)
+ err(1, "F_UNLCK");
+
+}
+
+void
+count(int val)
+{
+ int i, j;
+ char help[80], *name;
+
+ if (val == 1)
+ name = name1;
+ else
+ name = name2;
+ snprintf(help, sizeof(help), "%s %d %s", __func__, val, name);
+ setproctitle("%s", help);
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != 2 * PARALLEL)
+ ;
+
+ /* Need to re-open after a fork() */
+ close(fd);
+ if ((fd = open(name, O_RDWR)) == -1)
+ err(1, "open(%s)", name);
+
+ for (i = 0; i < LOOPS; i++) {
+ for (j = 0; j < N; j++)
+ add(j, val);
+ }
+
+ atomic_add_int(&share[DONE], 1);
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ off_t len;
+ size_t mlen;
+ long val, sum;
+ int i, s, stat;
+
+ if (argc != 3) {
+ fprintf(stderr, "Usage: %s <dir1> <dir2>\n", argv[0]);
+ exit(1);
+ }
+
+ mlen = PAGE_SIZE;
+ if ((share = mmap(NULL, mlen, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ snprintf(name1, sizeof(name1), "%s/work", argv[1]);
+ snprintf(name2, sizeof(name2), "%s/work", argv[2]);
+ signal(SIGALRM, ahandler);
+ alarm(300);
+ if ((fd = open(name1, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1, "open(%s)", name1);
+ len = N * sizeof(val);
+ if (ftruncate(fd, len) == -1)
+ err(1, "ftruncate");
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ count(1);
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ count(-1);
+ }
+
+ while (share[DONE] != 2 * PARALLEL)
+ usleep(10000);
+
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ err(1, "lseek");
+ sum = 0;
+ for (i = 0; i < N; i++) {
+ if (read(fd, &val, sizeof(val)) != sizeof(val))
+ err(1, "Final read");
+ if (val != 0)
+ fprintf(stderr, "index %d: %ld\n", i, val);
+ sum += val;
+ }
+ if (sum != 0)
+ fprintf(stderr, "FAIL\n");
+ unlink(name1);
+
+ s = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ wait(&stat);
+ s += WEXITSTATUS(stat);
+ wait(&stat);
+ s += WEXITSTATUS(stat);
+ }
+
+ close(fd);
+
+ return (sum != 0 || s != 0);
+}
diff --git a/tools/test/stress2/misc/nullfs23.sh b/tools/test/stress2/misc/nullfs23.sh
new file mode 100755
index 000000000000..3e91c04d91b4
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs23.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# "panic: Lock (lockmgr) nullfs not locked @ kern/kern_lock.c:620." seen:
+# https://people.freebsd.org/~pho/stress/log/nullfs23.txt
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+mp2=/media
+mount | grep "$mp2" | grep -q nfs && umount $mp2
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mp2
+
+export nullfs_srcdir=$mp2
+../misc/nullfs17.sh
+
+umount $mp2
+while mount | grep "$mp2 " | grep -q nfs; do
+ umount -f $mp2
+done
+exit 0
diff --git a/tools/test/stress2/misc/nullfs24.sh b/tools/test/stress2/misc/nullfs24.sh
new file mode 100755
index 000000000000..d7741c52a73a
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs24.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# Variation of nullfs.sh
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mounts=15 # Number of parallel scripts
+: ${nullfs_srcdir:=/tmp}
+: ${nullfs_dstdir:=$mntpoint}
+runtime=300
+
+for i in `jot $mounts`; do
+ [ ! -d ${nullfs_dstdir}$i ] && mkdir ${nullfs_dstdir}$i
+ mount | grep -q " ${nullfs_dstdir}$i " &&
+ umount ${nullfs_dstdir}$i
+done
+
+for i in `jot $mounts`; do
+ start=`date '+%s'`
+ while [ `date '+%s'` -lt $((start + $runtime)) ]; do
+ find ${nullfs_dstdir}* -type f -maxdepth 2 -ls > \
+ /dev/null 2>&1
+ done &
+done
+
+(cd ../testcases/swap; ./swap -t ${runtime}s -i 20) &
+for i in `jot $mounts`; do
+ # The test: Parallel mount and unmounts
+ start=`date '+%s'`
+ while [ `date '+%s'` -lt $((start + $runtime)) ]; do
+ mount_nullfs $nullfs_srcdir ${nullfs_dstdir}$i > \
+ /dev/null 2>&1
+ opt=$([ `jot -r 1 0 1` -eq 0 ] && echo "-f")
+ while mount | grep -q ${nullfs_dstdir}$i; do
+ umount $opt ${nullfs_dstdir}$i > \
+ /dev/null 2>&1
+ done
+ done &
+done
+wait
+exit 0
diff --git a/tools/test/stress2/misc/nullfs25.sh b/tools/test/stress2/misc/nullfs25.sh
new file mode 100755
index 000000000000..5588567ead22
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs25.sh
@@ -0,0 +1,90 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Variation of nullfs17.sh WiP
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+mounts=4 # Number of parallel scripts
+: ${nullfs_srcdir:=$mntpoint}
+: ${nullfs_dstdir:=$mntpoint}
+CONT=/tmp/nullfs25.continue
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+(cd $mntpoint; jot 500 | xargs touch)
+(cd ../testcases/swap; ./swap -t 5m -i 20 > /dev/null) &
+
+for i in `jot $mounts $mdstart`; do
+ [ ! -d ${nullfs_dstdir}$i ] && mkdir ${nullfs_dstdir}$i
+ mount | grep -q " ${nullfs_dstdir}$i " &&
+ umount ${nullfs_dstdir}$i
+done
+
+# Start the parallel tests
+touch $CONT
+for i in `jot $mounts $mdstart`; do
+ while [ -f $CONT ]; do
+ find ${nullfs_dstdir}$i -type f -maxdepth 2 -ls > \
+ /dev/null 2>&1
+ done &
+ # The test: Parallel mount and unmounts
+ start=`date +%s`
+ (
+ while [ $((`date +%s` - start)) -lt 300 ]; do
+ mount_nullfs $nullfs_srcdir ${nullfs_dstdir}$i > \
+ /dev/null 2>&1
+ opt=$([ `jot -r 1 0 1` -eq 0 ] && echo "-f")
+ while mount | grep -q ${nullfs_dstdir}$i; do
+ umount $opt ${nullfs_dstdir}$i > \
+ /dev/null 2>&1
+ done
+ done
+ rm -f $CONT
+ ) &
+done
+while [ -f $CONT ] ; do sleep 1; done
+while pgrep -q swap; do pkill swap; done
+wait
+
+for i in `jot $mounts`; do
+ umount ${nullfs_dstdir}$i > /dev/null 2>&1
+done
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ [ $((n += 1)) -gt 300 ] && { echo FAIL; exit 1; }
+done
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/nullfs26.sh b/tools/test/stress2/misc/nullfs26.sh
new file mode 100755
index 000000000000..720021e9e199
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs26.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+. ../default.cfg
+
+# nullfs + tmpfs scenario. No problems seen.
+
+mp1=${mntpoint}$mdstart
+mp2=${mntpoint}$((mdstart + 1))
+mkdir -p $mp1 $mp2
+mount -o size=5g -t tmpfs null $mp1 || exit 1
+mount -t nullfs $mp1 $mp2 || exit 1
+
+export runRUNTIME=10m
+export RUNDIR=$mp2/stressX
+mkdir -p $RUNDIR; chmod 0777 $RUNDIR
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg'
+
+umount $mp2
+umount $mp1
+exit 0
diff --git a/tools/test/stress2/misc/nullfs27.sh b/tools/test/stress2/misc/nullfs27.sh
new file mode 100755
index 000000000000..02894fba5efb
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs27.sh
@@ -0,0 +1,91 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Run all the rename(2) test scenarios with nullfs.
+# Used in connection with WiP work on nullfs.
+
+. ../default.cfg
+
+set -e
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+mp2=${mntpoint}2
+[ -d $mp2 ] || mkdir -p $mp2
+mount | grep -wq $mp2 && umount $mp2
+mount -t nullfs $mntpoint $mp2
+chmod 777 $mp2
+set +e
+
+export LOAD=80
+export MAXSWAPPCT=80
+export RUNDIR=$mp2/stressX
+export runRUNTIME=10m
+export rwLOAD=80
+export TESTPROGS='
+testcases/lockf2/lockf2
+testcases/symlink/symlink
+testcases/openat/openat
+testcases/socket/socket
+testcases/rw/rw
+testcases/mmap/mmap
+testcases/fts/fts
+testcases/link/link
+testcases/lockf/lockf
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/swap/swap
+testcases/mkfifo/mkfifo
+testcases/dirnprename/dirnprename
+testcases/dirrename/dirrename
+'
+
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
+
+while mount | grep $mp2 | grep -q nullfs; do
+ umount $mp2 || sleep 1
+done
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ n=$((n + 1))
+ [ $n -gt 30 ] && { echo FAIL; status=2; }
+done
+mdconfig -d -u $mdstart
+exit $status
diff --git a/tools/test/stress2/misc/nullfs3.sh b/tools/test/stress2/misc/nullfs3.sh
new file mode 100755
index 000000000000..36a606ce7b7e
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs3.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Test scenario by "Paul B. Mahol" <onemda gmail com>
+
+# Caused: lock violation
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# NULLFS(5) and SUJ has known issues.
+mount | grep "on `df $RUNDIR | sed '1d;s/.* //'` " | \
+ grep -q "journaled soft-updates" &&
+ { echo "Skipping test due to SUJ."; exit 0; }
+
+[ -d $RUNDIR/stressX ] || mkdir -p $RUNDIR/stressX
+
+mp=$mntpoint
+mount | grep -q $mp && umount -f $mp
+
+mount -t nullfs `dirname $RUNDIR` $mp
+
+cd $mp/stressX
+whereis something > /dev/null
+cd /
+
+umount $mp
+
+mount | grep -q $mp && umount -f $mp
+exit 0
diff --git a/tools/test/stress2/misc/nullfs4.sh b/tools/test/stress2/misc/nullfs4.sh
new file mode 100755
index 000000000000..ac328196f56a
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs4.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Test scenario by Anatoli Klassen <dev 26th net>
+
+# kern/94269: [nullfs] procfs shows wrong data if executable is running from
+# nullfs
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q /proc || mount -t procfs procfs /proc
+mount -t nullfs /bin $mntpoint
+
+r1=`/bin/ls -l /proc/curproc/file`
+r2=`$mntpoint/ls -l /proc/curproc/file | sed "s#$mntpoint#/bin#"`
+if [ "$r1" != "$r2" ]; then
+ echo "/bin/ls -l /proc/curproc/file"
+ echo $r1
+ echo "$mntpoint/ls -l /proc/curproc/file"
+ echo $r2
+fi
+
+umount $mntpoint
diff --git a/tools/test/stress2/misc/nullfs5.sh b/tools/test/stress2/misc/nullfs5.sh
new file mode 100755
index 000000000000..fa73b8179817
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs5.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Composite test: nullfs2.sh + kinfo.sh
+
+# Kernel page fault with the following non-sleepable locks held from
+# nullfs/null_vnops.c:531
+
+# Fatal trap 12: page fault while in kernel mode
+# https://people.freebsd.org/~pho/stress/log/jeff106.txt
+
+# panic: vholdl: inactive held vnode:
+# https://people.freebsd.org/~pho/stress/log/kostik815.txt
+
+# umount busy seen:
+# https://people.freebsd.org/~pho/stress/log/kostik893.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# NULLFS(5) and SUJ has known issues.
+mount | grep "on `df $RUNDIR | sed '1d;s/.* //'` " | \
+ grep -q "journaled soft-updates" &&
+ { echo "Skipping test due to SUJ."; exit 0; }
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d;s/60/600/' < $odir/kinfo.sh > kinfo.c
+mycc -o kinfo -Wall -g kinfo.c -lutil
+rm -f kinfo.c
+cd $odir
+
+mount | grep -q procfs || mount -t procfs procfs /proc
+
+for j in `jot 5`; do
+ /tmp/kinfo &
+done
+
+mount | grep -q $mntpoint && umount -f $mntpoint
+
+mount -t nullfs `dirname $RUNDIR` $mntpoint
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=10m
+#(cd ..; ./run.sh marcus.cfg)
+(cd ..; timeout -k 15m 12m ./run.sh marcus.cfg)
+
+umount $mntpoint 2>&1 | grep -v busy
+
+mount | grep -q $mntpoint && umount -f $mntpoint
+
+wait
+rm -f /tmp/kinfo
diff --git a/tools/test/stress2/misc/nullfs6.sh b/tools/test/stress2/misc/nullfs6.sh
new file mode 100755
index 000000000000..628e2fe2bfd7
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs6.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# Lock violation panic regression test
+# Test scenario by Mikolaj Golub <to my trociny gmail com>
+# Fixed in r208773
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+nullfs_srcdir=${nullfs_srcdir:-/tmp}
+mount | grep nullfs | grep -q $nullfs_srcdir/1 && umount $nullfs_srcdir/1
+
+rm -rf $nullfs_srcdir/1 $nullfs_srcdir/2
+mkdir $nullfs_srcdir/1 $nullfs_srcdir/2
+touch $nullfs_srcdir/1/test.file
+
+mount -t nullfs $nullfs_srcdir/1 $nullfs_srcdir/2
+
+cp $nullfs_srcdir/1/test.file $nullfs_srcdir/2/test.file # scenario by kib
+mv $nullfs_srcdir/1/test.file $nullfs_srcdir/2/ # panics with lock violation
+
+umount $nullfs_srcdir/1
+rm -rf $nullfs_srcdir/1 $nullfs_srcdir/2
diff --git a/tools/test/stress2/misc/nullfs7.sh b/tools/test/stress2/misc/nullfs7.sh
new file mode 100755
index 000000000000..329258f5af4d
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs7.sh
@@ -0,0 +1,63 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# "panic: Lock ufs not locked @ ../../../kern/vfs_default.c:508" seen.
+# Based on scenario by Subbsd <subbsd gmail com>
+# Fixed in r226681
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# NULLFS(5) and SUJ has known issues.
+mount | grep "on `df $RUNDIR | sed '1d;s/.* //'` " | \
+ grep -q "journaled soft-updates" &&
+ { echo "Skipping test due to SUJ."; exit 0; }
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+[ -d $RUNDIR ] || mkdir -p $RUNDIR
+for i in `jot 50`; do
+ DST="$mntpoint/$i"
+ [ -d "$DST" ] || mkdir $DST
+ mount -oro -t nullfs $RUNDIR $DST
+ mount -orw -t nullfs /bin $DST
+done
+mount | grep nullfs | awk '{print $3}' | xargs umount
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ [ $((n += 1)) -gt 100 ] && exit 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/nullfs8.sh b/tools/test/stress2/misc/nullfs8.sh
new file mode 100755
index 000000000000..c0c19fb48bdb
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs8.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Based on scenario by rea@, kern/164261. Different panic.
+# insmntque: mp-safe fs and non-locked vp: 0xcb413984 is not exclusive
+# locked but should be
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+nullfs_srcdir=${nullfs_srcdir:-/tmp}
+opt="-o nfsv3,rw,udp,rdirplus,noauto,retrycnt=3"
+grep -q $mntpoint /etc/exports ||
+ { echo "$mntpoint missing from /etc/exports"; exit 0; }
+
+mount | grep -wq $mntpoint && umount $mntpoint
+mount -t nullfs $nullfs_srcdir $mntpoint
+
+mntpoint2=${mntpoint}2
+mntpoint3=${mntpoint}3
+for m in $mntpoint2 $mntpoint3; do
+ [ -d $m ] || mkdir $m
+ mount | grep -wq $m && umount $m
+ mount -t nfs $opt 127.0.0.1:$mntpoint $m
+done
+
+for i in `jot 50` ; do
+ su $testuser -c "cp -r /usr/include $mntpoint2/nullfs8-2 2>/dev/null" &
+ su $testuser -c "cp -r /usr/include $mntpoint3/nullfs8-2 2>/dev/null" &
+ wait
+ su $testuser -c "find $mntpoint2 > /dev/null 2>&1" &
+ su $testuser -c "find $mntpoint3 > /dev/null 2>&1" &
+ wait
+ rm -rf $nullfs_srcdir/nullfs8-2
+done
+
+for m in $mntpoint3 $mntpoint2 $mntpoint; do
+ while mount | grep -wq $m; do
+ umount $m || sleep 1
+ done
+done
diff --git a/tools/test/stress2/misc/nullfs9.sh b/tools/test/stress2/misc/nullfs9.sh
new file mode 100755
index 000000000000..e74cde947924
--- /dev/null
+++ b/tools/test/stress2/misc/nullfs9.sh
@@ -0,0 +1,82 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# nullfs + tmpfs regression test:
+# ETXTBSY problem of executable
+# Fails with "./nullfs9.sh: cannot create /mnt2/mp/true: Text file busy"
+
+. ../default.cfg
+
+mnt2=${mntpoint}2
+mount | grep -q $mnt2/mp && umount $mnt2/mp
+
+[ -d $mnt2 ] || mkdir $mnt2
+mount | grep $mnt2 | grep -q /dev/md && umount -f $mnt2
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mnt2
+chmod 777 $mnt2
+
+mount | grep $mntpoint | grep -q tmpfs && umount -f $mntpoint
+mount -t tmpfs tmpfs $mntpoint
+chmod 777 $mntpoint
+
+mkdir $mnt2/mp
+mount -t nullfs $mntpoint $mnt2/mp
+
+# OK
+cp /usr/bin/true $mnt2/mp/true
+$mntpoint/true
+if ! > $mntpoint/true ; then
+ echo FAIL 1
+ mount | egrep "tmpfs|nullfs|$mntpoint |$mnt2 "
+fi
+rm -f $mntpoint/true
+
+# Fails
+cp /usr/bin/true $mnt2/mp/true
+$mnt2/mp/true
+if ! > $mnt2/mp/true; then
+ echo FAIL 2
+ mount | egrep "tmpfs|nullfs|$mntpoint |$mnt2 "
+fi
+rm -f $mnt2/mp/true
+
+umount $mnt2/mp
+while mount | grep -q "$mnt2 "; do
+ umount $mnt2 || sleep 1
+done
+mdconfig -d -u $mdstart
+
+while mount | grep -q "$mntpoint "; do
+ umount $mntpoint || sleep 1
+done
diff --git a/tools/test/stress2/misc/numa.sh b/tools/test/stress2/misc/numa.sh
new file mode 100755
index 000000000000..048eb1201714
--- /dev/null
+++ b/tools/test/stress2/misc/numa.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Test scenario: Last memory domain significantly smaller than the others.
+# https://people.freebsd.org/~pho/stress/log/numa.txt
+# Test scenario description by markj@
+
+[ `sysctl -n vm.ndomains` -eq 1 ] && exit 0
+
+doms=`sysctl -n vm.ndomains`
+phys=`sysctl -n hw.physmem` # clamped size
+real=`sysctl -n hw.realmem` # HW size
+
+goal=$((real - (real / doms) + 2 * 1024 * 1024 * 1024))
+mb=$((goal / 1024 / 1024))
+[ $phys -gt $goal ] &&
+ { echo "hw.physmem must be set to ${mb}M for this test."; exit 0; }
+
+../misc/sort.sh
+exit
diff --git a/tools/test/stress2/misc/oom.sh b/tools/test/stress2/misc/oom.sh
new file mode 100755
index 000000000000..d64ff9e88182
--- /dev/null
+++ b/tools/test/stress2/misc/oom.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Test OOM killing.
+
+# https://people.freebsd.org/~pho/stress/log/kostik847.txt
+# Fixed by r290915 and r290917
+
+# Expect:
+# kernel: pid 5654 (sort), uid 0, was killed: out of swap space
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `sysctl -n hw.physmem` -gt $(( 1 * 1024 * 1024 * 1024)) ] &&
+ echo "RAM should be capped to 1GB for this test."
+[ `sysctl -n hw.physmem` -gt $(( 8 * 1024 * 1024 * 1024)) ] && exit 0
+[ `sysctl -n vm.swap_total` -gt 0 ] && { swapoff -a; off=1; }
+
+for i in `jot 4`; do
+ sort < /dev/zero &
+done
+wait
+[ -n "$off" ] && swapon -a
diff --git a/tools/test/stress2/misc/oom2.sh b/tools/test/stress2/misc/oom2.sh
new file mode 100755
index 000000000000..a588c1d6cbde
--- /dev/null
+++ b/tools/test/stress2/misc/oom2.sh
@@ -0,0 +1,49 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# Test OOM killing.
+
+# sort stuck in "pfault" seen.
+# https://people.freebsd.org/~pho/stress/log/oom2.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `sysctl -n vm.swap_total` -gt 0 ] && { swapoff -a; off=1; }
+
+[ `sysctl -n hw.physmem` -gt $((32 * 1024 * 1024 * 1024)) ] &&
+ echo "RAM should be capped to no more than 32GB"
+n=`sysctl -n hw.ncpu`
+n=`jot $n`
+start=`date +%s`
+while [ $((`date +%s` - $start)) -lt 300 ]; do
+ for i in $n; do
+ sort < /dev/zero > /dev/null 2>&1 &
+ done
+ wait
+done
+[ $off ] && swapon -a
+exit 0
diff --git a/tools/test/stress2/misc/oovm.sh b/tools/test/stress2/misc/oovm.sh
new file mode 100755
index 000000000000..3eb4418c50d6
--- /dev/null
+++ b/tools/test/stress2/misc/oovm.sh
@@ -0,0 +1,134 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Out of VM deadlock seen. Introduced by r285808.
+# https://people.freebsd.org/~pho/stress/log/oovm.txt
+# https://people.freebsd.org/~pho/stress/log/oovm-2.txt
+
+# Fixed by r290047 and <alc's PQ_LAUNDRY patch>
+
+# Test scenario suggestion by alc@
+
+. ../default.cfg
+
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+maxsize=$((2 * 1024)) # Limit size due to runtime reasons
+size=$((`sysctl -n hw.physmem` / 1024 / 1024))
+[ $size -gt $((4 * 1024)) ] &&
+ echo "RAM should be capped to 4GB for this test."
+[ $size -gt $maxsize ] && size=$maxsize
+need=$((size * 2))
+d1=$diskimage.1
+d2=$diskimage.2
+rm -f $d1 $d2
+[ `df -k $(dirname $diskimage) | tail -1 | awk '{print int($4 / 1024)}'` -lt \
+ $need ] && printf "Need %d MB on %s.\n" $need `dirname $diskimage` && exit
+dd if=/dev/zero of=$d1 bs=1m count=$size status=none
+cp $d1 $d2 || exit
+trap "rm -f $d1 $d2" EXIT INT
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/oovm.c
+mycc -o oovm -Wall -Wextra -g oovm.c || exit 1
+rm -f oovm.c
+cd $odir
+
+(cd /tmp; /tmp/oovm $d1) &
+(cd /tmp; /tmp/oovm $d2) &
+wait
+
+rm -f /tmp/oovm /tmp/oovm.core
+exit
+
+EOF
+#include <sys/param.h>
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+const char *file;
+
+#define RUNTIME 600
+
+void
+test(void)
+{
+ struct stat st;
+ size_t i, olen, len;
+ time_t start;
+ int error, fd, ps;
+ char *p;
+
+ ps = getpagesize();
+ if ((fd = open(file, O_RDWR)) == -1)
+ err(1, "open(%s)", file);
+ if ((error = fstat(fd, &st)) == -1)
+ err(1, "stat(%s)", file);
+ len = olen = round_page(st.st_size);
+ do {
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
+ fd, 0)) == MAP_FAILED) {
+ if (errno == ENOMEM)
+ len -= ps;
+ else
+ err(1, "mmap");
+ }
+ } while (p == MAP_FAILED);
+
+ start = time(NULL);
+ /* Touch all pages of the file. */
+ for (i = 0; i < len; i += ps)
+ p[i] = 1;
+ while (time(NULL) - start < RUNTIME)
+ p[arc4random() % len] = 1;
+
+ if (munmap(p, len) == -1)
+ err(1, "unmap()");
+ close(fd);
+}
+
+int
+main(int argc, char *argv[])
+{
+ if (argc != 2)
+ errx(1, "Usage: %s <file>", argv[0]);
+ file = argv[1];
+
+ test();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/oovm2.sh b/tools/test/stress2/misc/oovm2.sh
new file mode 100755
index 000000000000..b65194ad792b
--- /dev/null
+++ b/tools/test/stress2/misc/oovm2.sh
@@ -0,0 +1,136 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Out of VM deadlock seen. Introduced by r285808. Variation of oovm.sh
+# https://people.freebsd.org/~pho/stress/log/oovm2.txt
+
+# Fixed by r290047 and <alc's PQ_LAUNDRY patch>
+
+# Test scenario suggestion by alc@
+
+. ../default.cfg
+
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+maxsize=$((2 * 1024)) # Limit size due to runtime reasons
+size=$((`sysctl -n hw.physmem` / 1024 / 1024))
+[ $size -gt $maxsize ] && size=$maxsize
+d1=$diskimage.1
+d2=$diskimage.2
+d3=$diskimage.3
+d4=$diskimage.4
+rm -f $d1 $d2 $d3 $d4
+[ `df -k $(dirname $diskimage) | tail -1 | awk '{print int($4 / 1024)}'` -lt \
+ $size ] && printf "Need %d MB on %s.\n" $size `dirname $diskimage` && exit
+dd if=/dev/zero of=$d1 bs=1m count=$((size / 4)) status=none
+cp $d1 $d2 || exit
+cp $d1 $d3 || exit
+cp $d1 $d4 || exit
+trap "rm -f $d1 $d2 $d3 $d4" EXIT INT
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/oovm2.c
+mycc -o oovm2 -Wall -Wextra -g oovm2.c || exit 1
+rm -f oovm2.c
+cd $odir
+
+(cd /tmp; /tmp/oovm2 $d1) &
+(cd /tmp; /tmp/oovm2 $d2) &
+(cd /tmp; /tmp/oovm2 $d3) &
+(cd /tmp; /tmp/oovm2 $d4) &
+wait
+
+rm -f /tmp/oovm2 /tmp/oovm2.core
+exit
+
+EOF
+#include <sys/param.h>
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+const char *file;
+
+#define RUNTIME 600
+
+void
+test(void)
+{
+ struct stat st;
+ size_t i, olen, len;
+ time_t start;
+ int error, fd, ps;
+ char *p;
+
+ ps = getpagesize();
+ if ((fd = open(file, O_RDWR)) == -1)
+ err(1, "open(%s)", file);
+ if ((error = fstat(fd, &st)) == -1)
+ err(1, "stat(%s)", file);
+ len = olen = round_page(st.st_size);
+ do {
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
+ fd, 0)) == MAP_FAILED) {
+ if (errno == ENOMEM)
+ len -= ps;
+ else
+ err(1, "mmap");
+ }
+ } while (p == MAP_FAILED);
+
+ start = time(NULL);
+ /* Touch all pages of the file. */
+ for (i = 0; i < len; i += ps)
+ p[i] = 1;
+ while (time(NULL) - start < RUNTIME)
+ p[arc4random() % len] = 1;
+
+ if (munmap(p, len) == -1)
+ err(1, "unmap()");
+ close(fd);
+}
+
+int
+main(int argc, char *argv[])
+{
+ if (argc != 2)
+ errx(1, "Usage: %s <file>", argv[0]);
+ file = argv[1];
+
+ test();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/open.sh b/tools/test/stress2/misc/open.sh
new file mode 100755
index 000000000000..e17df339842e
--- /dev/null
+++ b/tools/test/stress2/misc/open.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Regression test for:
+# Bug 202892 open with O_CREAT | O_DIRECTORY when path references a symlink.
+# Fixed by r287599.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/open.c
+mycc -o open -Wall -Wextra -O0 -g open.c || exit 1
+rm -f open.c
+
+wdir=/tmp/open.$$
+rm -rf $wdir
+mkdir -p $wdir
+cd $wdir
+status=0
+/tmp/open || { echo FAIL; status=$?; }
+[ -f broken -o -f broken2 ] && { ls -l; echo FAIL; status=1; }
+cd $odir
+
+rm -rf /tmp/open $wdir
+exit $status
+
+EOF
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ int fd;
+
+ /* Setup. */
+ if (unlink("broken") <= 0 && errno != ENOENT)
+ err(1, "unlink(broken)");
+ if (unlink("target") <= 0 && errno != ENOENT)
+ err(1, "unlink(target)");
+ if (symlink("target", "broken") < 0)
+ err(1, "symlink(target, broken)");
+
+ /* Test. */
+ fd = open("broken", O_CREAT | O_DIRECTORY, 0600);
+ if (fd >= 0)
+ errx(1, "open(broken, O_CREAT | O_DIRECTORY) - no error");
+
+ fd = open("broken2", O_CREAT | O_DIRECTORY | O_EXCL, 0600);
+ if (fd != -1)
+ errx(1, "open() O_CREAT | O_DIRECTORY | O_EXCL");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/openlock.sh b/tools/test/stress2/misc/openlock.sh
new file mode 100755
index 000000000000..71263b79c02f
--- /dev/null
+++ b/tools/test/stress2/misc/openlock.sh
@@ -0,0 +1,94 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Check O_EXLOCK behaviour on different file types
+# Regression test for r313549.
+
+# Test scenario by kib@
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+kldstat -v | grep -q pty || { kldload pty || exit 0; }
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/openlock.c
+mycc -o openlock -Wall -Wextra -O0 -g openlock.c || exit 1
+rm -f openlock.c
+
+e=0
+echo "expect: \"error 45 Operation not supported\""
+/tmp/openlock /dev/ptmx
+s=$?
+[ $s -eq 45 ] || { echo "Expected error 45, got $s."; e=1; }
+/tmp/openlock /bin/ls
+s=$?
+[ $s -eq 0 ] || { echo "Expected error 0, got $s."; e=$((e + 2)); }
+mkfifo openlock.fifo
+s=$?
+sleep 1 > openlock.fifo &
+/tmp/openlock openlock.fifo
+s=$?
+[ $s -eq 45 ] || { echo "Expected error 45, got $s."; e=$((e + 4)); }
+wait
+
+rm -f /tmp/openlock openlock.fifo
+exit $e
+EOF
+
+/* $Id: openlock.c,v 1.1 2017/02/10 14:07:24 kostik Exp kostik $ */
+
+#include <sys/fcntl.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+main(int argc, char *argv[])
+{
+ int e, fd, i;
+
+ for (i = 1; i < argc; i++) {
+ fd = open(argv[i], O_RDONLY | O_EXLOCK);
+ if (fd == -1) {
+ printf("%s error %d %s\n", argv[i], errno,
+ strerror(errno));
+ e = errno;
+ } else {
+ printf("%s success\n", argv[i]);
+ close(fd);
+ e = 0;
+ }
+ }
+ return (e);
+}
+
diff --git a/tools/test/stress2/misc/overcommit.sh b/tools/test/stress2/misc/overcommit.sh
new file mode 100755
index 000000000000..40f60a32f4af
--- /dev/null
+++ b/tools/test/stress2/misc/overcommit.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Test vm.overcommit
+
+# Setting bit 0 of the vm.overcommit sysctl causes the virtual memory
+# system to return failure to the process when allocation of memory
+# causes vm.swap_reserved to exceed vm.swap_total.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+old=`sysctl -n vm.overcommit`
+[ $old -eq 1 ] && exit
+
+sysctl vm.overcommit=1
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg'
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+
+sysctl vm.overcommit=$old
diff --git a/tools/test/stress2/misc/overcommit2.sh b/tools/test/stress2/misc/overcommit2.sh
new file mode 100755
index 000000000000..aed26689b933
--- /dev/null
+++ b/tools/test/stress2/misc/overcommit2.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Test vm.overcommit. Variation of overcommit.sh
+# Use a swap backed MD disk with the size of 1.2 * hw.usermem.
+# Deadlock seen: https://people.freebsd.org/~pho/stress/log/alan007.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+
+. ../default.cfg
+
+old=`sysctl -n vm.overcommit`
+[ $old -eq 1 ] && exit
+
+size=$((`sysctl -n hw.usermem` / 1024 / 1024)) # in MB
+size=$((size + size / 100 * 20)) # 120% of hw.usermem
+sysctl vm.overcommit=1
+trap "sysctl vm.overcommit=$old" EXIT INT
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s ${size}m -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+echo "Expect:
+ /mnt: write failed, filesystem is full
+ dd: /mnt/big.1: No space left on device"
+
+for i in `jot 10`; do
+ dd if=/dev/zero of=$mntpoint/big.$i bs=1m status=none
+done
+wait
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/overflow3.sh b/tools/test/stress2/misc/overflow3.sh
new file mode 100755
index 000000000000..a5a29721a30b
--- /dev/null
+++ b/tools/test/stress2/misc/overflow3.sh
@@ -0,0 +1,96 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Mark Johnston <markj@FreeBSD.org>
+#
+# 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 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.
+#
+# Leaking fp references when truncating SCM_RIGHTS control messages
+# Fixed in r343784
+
+. ../default.cfg
+
+cd /tmp
+cat > overflow3.c <<EOF
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <err.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ struct iovec iov;
+ struct msghdr hdr, rhdr;
+ struct cmsghdr *chdr;
+ int nfds, sv[2];
+ char ch;
+
+ if (socketpair(PF_LOCAL, SOCK_STREAM, 0, sv) != 0)
+ err(1, "socketpair");
+
+ nfds = 253;
+
+ memset(&hdr, 0, sizeof(hdr));
+ ch = 'a';
+ iov.iov_base = &ch;
+ iov.iov_len = 1;
+ hdr.msg_iov = &iov;
+ hdr.msg_iovlen = 1;
+ hdr.msg_control = calloc(1, CMSG_SPACE(nfds * sizeof(int)));
+ hdr.msg_controllen = CMSG_SPACE(nfds * sizeof(int));
+
+ chdr = (struct cmsghdr *)hdr.msg_control;
+ chdr->cmsg_len = CMSG_LEN(nfds * sizeof(int));
+ chdr->cmsg_level = SOL_SOCKET;
+ chdr->cmsg_type = SCM_RIGHTS;
+
+ memset(&rhdr, 0, sizeof(rhdr));
+ rhdr.msg_iov = &iov;
+ rhdr.msg_iovlen = 1;
+ rhdr.msg_control = calloc(1, CMSG_SPACE(0));
+ rhdr.msg_controllen = CMSG_SPACE(0);
+
+ for (;;) {
+ if (sendmsg(sv[0], &hdr, 0) != 1)
+ err(1, "sendmsg");
+ if (recvmsg(sv[1], &rhdr, 0) != 1)
+ err(1, "recvmsg");
+ if ((rhdr.msg_flags & MSG_CTRUNC) == 0)
+ errx(1, "MSG_CTRUNC not set");
+ }
+
+ return (0);
+}
+EOF
+mycc -o overflow3 -Wall -Wextra -O2 overflow3.c || exit 1
+rm overflow3.c
+
+timeout 2m ./overflow3
+
+rm overflow3
+exit
diff --git a/tools/test/stress2/misc/overlap.sh b/tools/test/stress2/misc/overlap.sh
new file mode 100755
index 000000000000..f7ac00116559
--- /dev/null
+++ b/tools/test/stress2/misc/overlap.sh
@@ -0,0 +1,160 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Test that fails on various non FreeBSD file systems.
+# 3.13.0-74-generic #118-Ubuntu SMP: Mismatch @ count 214, 0 != 123
+# OK on OS X (Darwin Kernel Version 15.5.0)
+
+# Scanario by: Michael Ubell ubell mindspring com.
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+sed '1,/^EOF/d' < $0 > /tmp/overlap.c
+mycc -o /tmp/overlap -Wall -Wextra -O2 /tmp/overlap.c -lpthread || exit 1
+rm -f /tmp/overlap.c
+
+size="1g"
+mdconfig -a -t swap -s $size -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs -U md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+(cd $mntpoint; /tmp/overlap)
+s=$?
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/overlap
+exit $s
+EOF
+#include <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/uio.h>
+#include <unistd.h>
+
+char file[128];
+int bsiz, siz;
+
+void
+handler(int s __unused)
+{
+ _exit(0);
+}
+
+void *
+writer(void *arg __unused) {
+ int fdes, count;
+ ssize_t nwrite;
+ int *buf;
+
+ if ((fdes = open(file, O_RDWR|O_CREAT, 0664)) == -1)
+ err(1, "open(%s)", file);
+
+ count = 0;
+ buf = malloc(bsiz * sizeof(int));
+ buf[0] = buf[bsiz - 1] = 0;
+ while ((nwrite = pwrite(fdes, buf, siz, 0)) != -1) {
+ if (nwrite < siz)
+ err(1, "pwrite @ count %d, nwrite %zd", count, nwrite);
+ buf[0] = ++buf[bsiz - 1];
+ count++;
+ }
+
+ err(1, "pwrite()");
+}
+
+void *
+reader(void *arg __unused) {
+ int fdes, count;
+ ssize_t nread;
+ int *buf;
+
+ if ((fdes = open(file, O_RDWR|O_CREAT, 0664)) == -1)
+ err(1, "open(%s)", file);
+ count = 0;
+
+ buf = malloc(bsiz * sizeof(int));
+ while ((nread = pread(fdes, buf, siz, 0)) == 0)
+ continue;
+
+ do {
+ if (nread < siz)
+ err(1, "pread @ count %d, nread %zd", count, nread);
+ if (buf[0] != buf[bsiz - 1]) {
+ printf("Mismatch @ count %d, %d != %d\n",
+ count, buf[0], buf[bsiz - 1]);
+ abort();
+ }
+ count++;
+ } while ((nread = pread(fdes, buf, siz, 0)) != -1);
+
+ err(1, "pread()");
+}
+
+int
+main(int argc, char **argv) {
+ pthread_t rp, wp;
+ int ret;
+ void *exitstatus;
+
+ snprintf(file, sizeof(file), "test.%0d5", getpid());
+ siz = 65536;
+ if (argc == 2)
+ siz = atoi(argv[1]);
+
+ bsiz = siz / sizeof(int);
+
+ signal(SIGALRM, handler);
+ alarm(300);
+
+ if ((ret = pthread_create(&wp, NULL, writer, NULL)) != 0)
+ errc(1, ret, "pthread_create");
+ if ((ret = pthread_create(&rp, NULL, reader, NULL)) != 0)
+ errc(1, ret, "pthread_create");
+
+ pthread_join(rp, &exitstatus);
+ pthread_join(wp, &exitstatus);
+
+ unlink(file);
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pageout.sh b/tools/test/stress2/misc/pageout.sh
new file mode 100755
index 000000000000..5497a1135ff4
--- /dev/null
+++ b/tools/test/stress2/misc/pageout.sh
@@ -0,0 +1,144 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Trigger the two EDEADLK in vm/vm_pageout.c
+# OOVM deadlock seen
+# https://people.freebsd.org/~pho/stress/log/pageout.txt
+
+# "panic: handle_written_filepage: not started" seen:
+# https://people.freebsd.org/~pho/stress/log/pageout-2.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/pageout.c
+mycc -o pageout -Wall -Wextra -g pageout.c || exit 1
+rm -f pageout.c
+cd $odir
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+f1=$mntpoint/f1
+dd if=/dev/zero of=$f1 bs=1m count=1k status=none
+
+daemon sh -c "(cd ../testcases/swap; ./swap -t 5m -i 20 -l 100 -h)" > /dev/null
+(cd /tmp; /tmp/pageout $f1) &
+sleep .2
+while kill -0 $! 2> /dev/null; do
+ mksnap_ffs $mntpoint $mntpoint/.snap/stress2 &&
+ rm -f $mntpoint/.snap/stress2
+done
+while pgrep -q swap; do
+ pkill swap
+done
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/pageout /tmp/pageout.core
+exit
+
+EOF
+#include <sys/param.h>
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+const char *file;
+
+#define RUNTIME 600
+
+void
+test(void)
+{
+ struct stat st;
+ size_t i, len;
+ time_t start;
+ int error, fd, ps;
+ char *p;
+
+ ps = getpagesize();
+ if ((fd = open(file, O_RDWR)) == -1)
+ err(1, "open(%s)", file);
+ if ((error = fstat(fd, &st)) == -1)
+ err(1, "stat(%s)", file);
+ len = round_page(st.st_size);
+ do {
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
+ fd, 0)) == MAP_FAILED) {
+ if (errno == ENOMEM)
+ len -= ps;
+ else
+ err(1, "mmap");
+ }
+ } while (p == MAP_FAILED);
+
+ start = time(NULL);
+ /* Touch all pages of the file. */
+ for (i = 0; i < len; i += ps)
+ p[i] = 1;
+ while (time(NULL) - start < RUNTIME)
+ p[arc4random() % len] = 1;
+
+ if (munmap(p, len) == -1)
+ err(1, "unmap()");
+ close(fd);
+}
+
+int
+main(int argc, char *argv[])
+{
+ if (argc != 2)
+ errx(1, "Usage: %s <file>", argv[0]);
+ file = argv[1];
+
+ test();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/parallelmount.sh b/tools/test/stress2/misc/parallelmount.sh
new file mode 100755
index 000000000000..5292efbed97a
--- /dev/null
+++ b/tools/test/stress2/misc/parallelmount.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Parallel mount and unmount of the same mount point
+# http://people.freebsd.org/~pho/stress/log/parallelumount3.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+parallel=40
+
+if [ $# -eq 0 ]; then
+ [ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+ mdconfig -a -t swap -s 10m -u $mdstart || exit 1
+ bsdlabel -w md$mdstart auto
+ newfs $newfs_flags md${mdstart}$part > /dev/null
+
+ # start the parallel tests
+ for i in `jot $parallel`; do
+ ./$0 $i &
+ done
+
+ while kill -0 $! 2> /dev/null; do
+ for i in `jot 100`; do
+ find $mntpoint > /dev/null 2>&1
+ done
+ done
+ wait
+
+ while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ done
+ mdconfig -d -u $mdstart
+ exit 0
+else
+ start=`date '+%s'`
+ while [ $((`date '+%s'` - start)) -lt 600 ]; do
+ mount /dev/md${mdstart}$part $mntpoint
+ umount $mntpoint
+ mount
+ done > /dev/null 2>&1
+fi
diff --git a/tools/test/stress2/misc/parallelmount2.sh b/tools/test/stress2/misc/parallelmount2.sh
new file mode 100755
index 000000000000..454a7b4b01ed
--- /dev/null
+++ b/tools/test/stress2/misc/parallelmount2.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC
+# 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 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.
+#
+
+# Quota version of parallelmount.sh
+# "umount busy seen:
+# https://people.freebsd.org/~pho/stress/log/parallelmount2.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -z "$DEBUG" ] && exit 0 # Waiting for fix
+[ "`sysctl -in kern.features.ufs_quota`" != "1" ] && exit 0
+
+. ../default.cfg
+
+parallel=40
+
+if [ $# -eq 0 ]; then
+ [ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+ mdconfig -a -t swap -s 10m -u $mdstart || exit 1
+ bsdlabel -w md$mdstart auto
+ newfs $newfs_flags md${mdstart}$part > /dev/null
+ export PATH_FSTAB=/var/tmp/fstab.$$
+ echo "/dev/md${mdstart}$part $mntpoint ufs rw,userquota 2 2" > $PATH_FSTAB
+ mount /dev/md${mdstart}$part $mntpoint
+ set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+ export QK=$(($1 / 10 * 8))
+ export QI=$(($2 / 10 * 8))
+ edquota -u -f $mntpoint -e $mntpoint:$((QK - 50)):$QK:$((QI - 50 )):$QI $testuser
+ quotaon $mntpoint
+ umount $mntpoint
+
+ # start the parallel tests
+ for i in `jot $parallel`; do
+ ./$0 $i &
+ done
+
+ while kill -0 $! 2> /dev/null; do
+ for i in `jot 100`; do
+ find $mntpoint > /dev/null 2>&1
+ done
+ done
+ wait
+ rm -f $PATH_FSTAB
+
+ for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ done
+ [ $i -eq 6 ] && exit 1
+ mdconfig -d -u $mdstart
+ exit 0
+else
+ start=`date '+%s'`
+ while [ $((`date '+%s'` - start)) -lt 600 ]; do
+ mount /dev/md${mdstart}$part $mntpoint
+ quotaon $mntpoint
+ umount $mntpoint
+ mount
+ done > /dev/null 2>&1
+fi
diff --git a/tools/test/stress2/misc/pathconf.sh b/tools/test/stress2/misc/pathconf.sh
new file mode 100755
index 000000000000..5de6b4e3728d
--- /dev/null
+++ b/tools/test/stress2/misc/pathconf.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# "panic: _PC_ASYNC_IO should not get here" seen:
+# https://people.freebsd.org/~pho/stress/log/pathconf.txt
+# Fixed by r320900
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+mp=${mntpoint}2
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+syscall=`grep lpathconf /usr/include/sys/syscall.h 2>/dev/null`
+[ -z "$syscall" ] && exit 0
+syscall=`echo $syscall | sed 's/.*[\t ]//'`
+
+mkdir -p $mp/nfs || exit 1
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mp/nfs
+chmod a+rw $mp/nfs
+
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 600 ]; do
+ ../misc/syscall4.sh $syscall
+done
+
+umount $mp/nfs
+rm -rf $mp
+exit 0
diff --git a/tools/test/stress2/misc/pathconf2.sh b/tools/test/stress2/misc/pathconf2.sh
new file mode 100755
index 000000000000..962f26e1aa53
--- /dev/null
+++ b/tools/test/stress2/misc/pathconf2.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# "panic: _PC_ASYNC_IO should not get here" seen:
+# https://people.freebsd.org/~pho/stress/log/pathconf2.txt
+# Fixed by r320900
+
+# Test scenario by: Ngie Cooper <ngie@FreeBSD.org>
+
+getconf _POSIX_ASYNC_IO /usr/src/ > /dev/null
+
+exit
diff --git a/tools/test/stress2/misc/pause.sh b/tools/test/stress2/misc/pause.sh
new file mode 100755
index 000000000000..e786dd6cce0c
--- /dev/null
+++ b/tools/test/stress2/misc/pause.sh
@@ -0,0 +1,159 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Hunt for lost wakeup problem.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/pause.c
+mycc -o pause -Wall -Wextra -O0 -g pause.c || exit 1
+rm -f pause.c
+
+pkill pause
+$dir/pause &
+pid=$!
+start=`date +%s`
+while pgrep -q pause; do
+ sleep .5
+ [ $((`date +%s` - $start)) -gt 1200 ] &&
+ { echo "Timed out"; pgrep pause | xargs ps -lp; exit 1; }
+done
+wait $pid
+s=$?
+
+cd $odir
+rm -rf $dir/pause
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#define PARALLEL 400
+#define RUNTIME (5 * 60)
+#define SYNC 0
+
+void
+hand(int i __unused) {
+}
+
+static void
+test(int idx)
+{
+ pid_t pid;
+ time_t start;
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ usleep(1);
+ share[idx] = 0;
+
+ if ((pid = fork()) == 0) {
+ share[idx] = 1;
+ for (;;)
+ pause();
+ _exit(0);
+ }
+ while (share[idx] == 0)
+ usleep(10);
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ usleep(arc4random() % 100);
+ if (kill(pid, SIGHUP) == -1)
+ err(1, "kill(%d)", pid);
+ }
+ kill(pid, SIGTERM);
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ struct sigaction sa;
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ sa.sa_handler = hand;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction(SIGHUP, &sa, NULL) == -1)
+ err(1, "sigaction");
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test(i + 1);
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/pcatch.sh b/tools/test/stress2/misc/pcatch.sh
new file mode 100755
index 000000000000..f1db7de253f2
--- /dev/null
+++ b/tools/test/stress2/misc/pcatch.sh
@@ -0,0 +1,154 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# The issue makes it possible for applications to get wrong EINTR
+# or ERESTART (the later is not directly visible) when doing write
+# to the file on suspended UFS volume. I.e. the thread is sleeping
+# when fs is suspended, and process is signaled.
+
+# Test scenario mostly by kib.
+# Fixed in r275744.
+
+# Deadlock seen:
+# https://people.freebsd.org/~pho/stress/log/pcatch.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -z "$DEBUG" ] && exit 0 # Waiting for fix
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pcatch.c
+mycc -o pcatch -Wall -Wextra -O0 -g pcatch.c || exit 1
+rm -f pcatch.c
+cd $here
+
+mount | grep -q "$mntpoint" && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 120 ]; do
+ /tmp/pcatch $mntpoint
+done
+
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/pcatch
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/mount.h>
+#include <sys/wait.h>
+
+#include <ufs/ffs/fs.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static void
+hand_sigaction(int signo __unused, siginfo_t *si __unused, void *c __unused)
+{
+}
+
+static void
+suspend(char *path)
+{
+ struct statfs s;
+ int fd, error;
+
+ if (fork() == 0) {
+ if ((error = statfs(path, &s)) != 0)
+ err(1, "statfs %s", path);
+ fd = open("/dev/ufssuspend", O_RDWR);
+ if ((error = ioctl(fd, UFSSUSPEND, &s.f_fsid)) != 0)
+ err(1, "UFSSUSPEND");
+ sleep(1);
+ if ((error = ioctl(fd, UFSRESUME, &s.f_fsid)) != 0)
+ err(1, "UFSRESUME");
+ _exit(0);
+ }
+}
+
+static void
+test(char *mp)
+{
+ pid_t pid;
+ struct sigaction sa;
+ int fd;
+ char buf[80], file[80];
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_sigaction = hand_sigaction;
+ if (sigaction(SIGUSR1, &sa, NULL) == -1)
+ err(1, "sigaction");
+
+ snprintf(file, sizeof(file), "%s/file", mp);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1, "open(%s). %s:%d", file, __FILE__, __LINE__);
+
+ suspend(mp);
+
+ if ((pid = fork()) == 0) {
+ if (write(fd, buf, sizeof(buf)) == -1)
+ warn("FAIL: write");
+ _exit(0);
+ }
+ usleep(10000);
+ if (kill(pid, SIGUSR1) == -1)
+ err(1, "kill");
+ wait(NULL);
+ wait(NULL);
+ close(fd);
+}
+
+int
+main(int argc, char *argv[])
+{
+
+ if (argc != 2)
+ errx(1, "Usage: %s <UFS mount point>", argv[0]);
+
+ test(argv[1]);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pcatch2.sh b/tools/test/stress2/misc/pcatch2.sh
new file mode 100755
index 000000000000..1c36406fec6f
--- /dev/null
+++ b/tools/test/stress2/misc/pcatch2.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC
+# 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 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.
+#
+
+# Process stuck in "suspfs":
+# https://people.freebsd.org/~pho/stress/log/kostik944.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -z "$DEBUG" ] && exit 0 # Waiting for fix
+
+for i in `jot 10`; do
+ ./pcatch.sh &
+ sleep `jot -r 1 1 5`
+ kill -INT $!
+ ./cleanup.sh
+ pgrep -q pcatch || break
+ wait
+done
+pgrep -q pcatch && s=1 || s=0
+exit $s
diff --git a/tools/test/stress2/misc/pdfork.sh b/tools/test/stress2/misc/pdfork.sh
new file mode 100755
index 000000000000..6c258a83521f
--- /dev/null
+++ b/tools/test/stress2/misc/pdfork.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+# truss / pdfork regression test.
+# Test scenario by: Ryan Stone rstone@, slightly mangled by pho@
+
+# Interruptable hang seen:
+# $ ps -lp992
+# UID PID PPID CPU PRI NI VSZ RSS MWCHAN STAT TT TIME COMMAND
+# 1001 992 991 0 27 0 4168 1908 - TX+ 0 0:00.00 /tmp/pdfork -p
+# $
+
+cat > /tmp/pdfork.c <<EOF
+#include <sys/types.h>
+#include <sys/procdesc.h>
+#include <sys/resource.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <err.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+main(int argc, char **argv)
+{
+ pid_t pid;
+ int fd;
+
+ if (argc > 1 && strcmp(argv[1], "-p") == 0) {
+ pid = pdfork(&fd, 0);
+ } else {
+ pid = fork();
+ }
+
+ if (pid == 0) {
+ sleep(1);
+ exit(0);
+ } else if (pid < 0) {
+ err(1, "fork() failed");
+ } else {
+ int status = 0;
+ if (argc > 1 && strcmp(argv[1], "-p") != 0) {
+ int error = wait4(pid, &status, WEXITED, NULL);
+ if (error < 0)
+ err(1, "wait4 failed");
+ }
+ exit(status);
+ }
+}
+EOF
+cc -o /tmp/pdfork -Wall -Wextra -O2 /tmp/pdfork.c || exit 1
+
+timeout 20s truss -f /tmp/pdfork 2> /dev/null; s1=$?
+timeout 20s truss -f /tmp/pdfork -p 2> /dev/null; s2=$?
+
+rm -f /tmp/pdfork /tmp/pdfork.c
+return $((s1 + s2))
diff --git a/tools/test/stress2/misc/perf.sh b/tools/test/stress2/misc/perf.sh
new file mode 100755
index 000000000000..89e9dd8d371c
--- /dev/null
+++ b/tools/test/stress2/misc/perf.sh
@@ -0,0 +1,151 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# O_CREAT / unlink() timing test with different FFS options.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -m` = "i386" ] && exit 0 # very long runtime
+
+. ../default.cfg
+[ $# -eq 0 ] && half=1 # SU and SUJ workaround
+first=1
+export LANG=en_US.ISO8859-1
+odir=`pwd`
+dir=$mntpoint
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > perf.c
+mycc -o perf -Wall -Wextra perf.c || exit 1
+rm -f perf.c
+cd $odir
+
+mount | grep -q "on $mntpoint " && umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart
+
+tst() {
+ local i j k s
+
+ s=0
+ cd $dir
+ inodes=`df -ik $mntpoint | tail -1 | \
+ awk '{printf "%d\n", $7}'`
+# SU and SUJ tests fail with ENOSPC
+ [ $half ] &&
+ i=$((inodes / 4)) ||
+ i=$(((inodes - 500) / 2))
+ [ $first -eq 1 ] &&
+ printf "Using %'\''d inodes out of a total of %'\''d.\n" \
+ $((i * 2)) $inodes
+ first=0
+
+ for k in `jot 3`; do
+ pids=""
+ for j in `jot 2`; do
+ /tmp/perf $i &
+ pids="$pids $!"
+ done
+ for pid in $pids; do
+ wait $pid; r=$?
+ [ $r -ne 0 ] && s=$r
+ done
+ done
+ cd $odir
+ return $s
+}
+
+s=0
+for i in "" "-U" "-j"; do
+ newfs $i /dev/md$mdstart > /dev/null 2>&1
+ mount /dev/md$mdstart $mntpoint
+
+ t1=`date +%s`
+ tst; r=$?
+ t2=$((`date +%s` - t1))
+
+ umount -f $mntpoint
+ t2=$((`date +%s` - t1))
+ [ $t2 -eq 0 ] && t2=1
+ [ -z "$base" ] && base=$t2
+ pct=$(((t2 - base) * 100 / base))
+ printf '%3d seconds elapsed for newfs option "%2s" (%+4d%%)\n' \
+ $t2 "$i" $pct
+ [ $pct -gt 10 ] && s=111
+ [ $s -eq 0 -a $r -ne 0 ] && s=$r
+done
+rm -f /tmp/perf
+mdconfig -d -u $mdstart
+exit $s
+EOF
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(int argc __unused, char **argv)
+{
+ pid_t pid;
+ int64_t size;
+ int e, fd, i, j;
+ char file[128];
+
+ size = atol(argv[1]);
+
+ e = 0;
+ pid = getpid();
+ for (j = 0; j < size; j++) {
+ sprintf(file,"p%05d.%05d", pid, j);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
+ DEFFILEMODE)) == -1) {
+ e = errno;
+ if (errno != EINTR) {
+ warn("open(%s)", file);
+ printf("break out at %d, errno %d\n", j,
+ errno);
+ break;
+ }
+ }
+ close(fd);
+ }
+
+ for (i = --j; i >= 0; i--) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if (unlink(file) == -1)
+ err(3, "unlink(%s)", file);
+
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/pfl.sh b/tools/test/stress2/misc/pfl.sh
new file mode 100755
index 000000000000..6d590b5cf225
--- /dev/null
+++ b/tools/test/stress2/misc/pfl.sh
@@ -0,0 +1,188 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Test scenario for the change of a global SU lock to a per filesystem lock.
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pfl.c
+mycc -o pfl -Wall -Wextra pfl.c || exit 1
+rm -f pfl.c
+cd $here
+
+md1=$mdstart
+md2=$((mdstart + 1))
+mp1=${mntpoint}$md1
+mp2=${mntpoint}$md2
+mkdir -p $mp1 $mp2
+
+usermem=`sysctl -n hw.usermem`
+[ `swapinfo | wc -l` -eq 1 ] && usermem=$((usermem/100*80))
+size=$((2 * 1024 * 1024 * 1024)) # Ideal disk size is 2G
+[ $((size * 2)) -gt $usermem ] && size=$((usermem / 2))
+size=$((size / 1024 / 1024))
+
+opt=$([ $((`date '+%s'` % 2)) -eq 0 ] && echo "-j" || echo "-U")
+[ "$newfs_flags" = "-U" ] || opt=""
+mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1
+[ -c /dev/md$md1 ] && mdconfig -d -u $md1
+mdconfig -a -t swap -s ${size}m -u $md1
+bsdlabel -w md$md1 auto
+newfs $opt md${md1}$part > /dev/null
+mount /dev/md${md1}$part $mp1
+chmod 777 $mp1
+
+mount | grep "on $mp2 " | grep -q /dev/md && umount -f $mp2
+[ -c /dev/md$md2 ] && mdconfig -d -u $md2
+mdconfig -a -t swap -s ${size}m -u $md2
+bsdlabel -w md$md2 auto
+newfs $opt md${md2}$part > /dev/null
+mount /dev/md${md2}$part $mp2
+chmod 777 $mp2
+
+su $testuser -c "cd $mp1; /tmp/pfl" &
+pids=$!
+su $testuser -c "cd $mp2; /tmp/pfl" &
+pids="$pids $!"
+sleep .5
+s=0
+start=`date '+%s'`
+while pgrep -q pfl; do
+ if [ $((`date '+%s'`- start)) -gt 900 ]; then
+ s=1
+ echo "$0 timed out."
+ pkill -9 pfl
+ fi
+ sleep 10
+done
+for p in $pids; do
+ wait $p
+ [ $? -ne 0 ] && s=2
+done
+
+while mount | grep "$mp2 " | grep -q /dev/md; do
+ umount $mp2 || sleep 1
+done
+mdconfig -d -u $md2
+while mount | grep "$mp1 " | grep -q /dev/md; do
+ umount $mp1 || sleep 1
+done
+rm -f /tmp/pfl
+mdconfig -d -u $md1
+exit $s
+
+EOF
+#include <sys/mount.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define PARALLEL 10
+
+static void
+test(void)
+{
+ pid_t pid;
+ int fd, i, j;
+ char file[128];
+
+ pid = getpid();
+ sprintf(file,"d%05d", pid);
+ if (mkdir(file, 0740) == -1)
+ err(1, "mkdir(%s)", file);
+ chdir(file);
+ for (j = 0; j < 10000; j++) {
+ sprintf(file,"p%05d.%05d", pid, j);
+ if ((fd = open(file, O_CREAT | O_TRUNC | O_WRONLY, 0644)) ==
+ -1) {
+ if (errno != EINTR) {
+ warn("mkdir(%s). %s:%d", file, __FILE__,
+ __LINE__);
+ unlink("continue");
+ break;
+ }
+ }
+ if (arc4random() % 100 < 10)
+ if (write(fd, "1", 1) != 1)
+ err(1, "write()");
+ close(fd);
+
+ }
+ sleep(3);
+
+ for (i = --j; i >= 0; i--) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if (unlink(file) == -1)
+ err(3, "unlink(%s)", file);
+
+ }
+ chdir("..");
+ sprintf(file,"d%05d", pid);
+ if (rmdir(file) == -1)
+ err(3, "unlink(%s)", file);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ int e, fd, j, k, s;
+
+ umask(0);
+ if ((fd = open("continue", O_CREAT, 0644)) == -1)
+ err(1, "open()");
+ close(fd);
+ e = 0;
+ for (j = 0; j < PARALLEL; j++) {
+ if ((pids[j] = fork()) == 0) {
+ for (k = 0; k < 40; k++)
+ test();
+ _exit(0);
+ }
+ }
+
+ for (j = 0; j < PARALLEL; j++) {
+ if (waitpid(pids[j], &s, 0) == -1)
+ err(1, "waitpid(%d)", pids[j]);
+ e += s == 0 ? 0 : 1;
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/pfl2.sh b/tools/test/stress2/misc/pfl2.sh
new file mode 100755
index 000000000000..9b6497294242
--- /dev/null
+++ b/tools/test/stress2/misc/pfl2.sh
@@ -0,0 +1,104 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Test scenario for the change of a global SU lock to a per filesystem lock.
+# "panic: softdep_write_inodeblock: indirect pointer #0 mismatch ..." seen.
+# http://people.freebsd.org/~pho/stress/log/kirk063.txt
+
+# https://people.freebsd.org/~pho/stress/log/kirk080.txt
+
+. ../default.cfg
+
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+
+md1=$mdstart
+md2=$((mdstart + 1))
+mp1=${mntpoint}$md1
+mp2=${mntpoint}$md2
+mkdir -p $mp1 $mp2
+
+usermem=`sysctl -n hw.usermem`
+size=$((2 * 1024 * 1024 * 1024)) # Ideal disk size is 2G
+[ $((size * 2)) -gt $usermem ] && size=$((usermem / 2))
+size=$((size / 1024 / 1024))
+
+opt=$([ $((`date '+%s'` % 2)) -eq 0 ] && echo "-j" || echo "-U")
+[ "$newfs_flags" = "-U" ] || opt=""
+mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1
+mdconfig -l | grep -q md$md1 && mdconfig -d -u $md1
+mdconfig -a -t swap -s ${size}m -u $md1
+bsdlabel -w md$md1 auto
+newfs $opt md${md1}$part > /dev/null
+mount /dev/md${md1}$part $mp1
+chmod 777 $mp1
+
+mount | grep "on $mp2 " | grep -q /dev/md && umount -f $mp2
+mdconfig -l | grep -q md$md2 && mdconfig -d -u $md2
+mdconfig -a -t swap -s ${size}m -u $md2
+bsdlabel -w md$md2 auto
+newfs $opt md${md2}$part > /dev/null
+mount /dev/md${md2}$part $mp2
+chmod 777 $mp2
+
+export runRUNTIME=10m
+export RUNDIR=$mp1/stressX
+export CTRLDIR=$mp1/stressX.control
+export LOAD=80
+export symlinkLOAD=80
+export rwLOAD=80
+export TESTPROGS="
+testcases/lockf2/lockf2
+testcases/symlink/symlink
+testcases/openat/openat
+testcases/rw/rw
+testcases/fts/fts
+testcases/link/link
+testcases/lockf/lockf
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/mkfifo/mkfifo
+"
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > /dev/null 2>&1 &
+
+export TESTPROGS="$TESTPROGS testcases/swap/swap"
+export RUNDIR=$mp2/stressX
+export CTRLDIR=$mp2/stressX.control
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > /dev/null 2>&1 &
+wait
+
+while mount | grep "$mp2 " | grep -q /dev/md; do
+ umount $mp2 || sleep 1
+done
+mdconfig -d -u $md2
+while mount | grep "$mp1 " | grep -q /dev/md; do
+ umount $mp1 || sleep 1
+done
+mdconfig -d -u $md1
diff --git a/tools/test/stress2/misc/pfl3.sh b/tools/test/stress2/misc/pfl3.sh
new file mode 100755
index 000000000000..8c2bb2396e9d
--- /dev/null
+++ b/tools/test/stress2/misc/pfl3.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Start pfl.sh and "umount -f" the two mount points in use.
+# "panic: handle_written_inodeblock: live inodedep" seen.
+# http://people.freebsd.org/~pho/stress/log/pfl3.txt
+
+# Not seen on FreeBSD 14.0-CURRENT #0 main-n244671-b3c6fe663bb
+
+. ../default.cfg
+
+./pfl.sh > /dev/null 2>&1 &
+sleep `jot -r 1 10 60`
+while mount | grep -q "$mntpoint"; do
+ umount -f ${mntpoint}$mdstart & pid=$!
+ umount -f ${mntpoint}$((mdstart + 1))
+ wait $pid
+done
+wait
+exit 0
diff --git a/tools/test/stress2/misc/pfl4.sh b/tools/test/stress2/misc/pfl4.sh
new file mode 100755
index 000000000000..8128fa1f585c
--- /dev/null
+++ b/tools/test/stress2/misc/pfl4.sh
@@ -0,0 +1,95 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# "panic: invalid queue 255" seen:
+# https://people.freebsd.org/~pho/stress/log/pfl4.txt
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ $((`sysctl -n hw.usermem` / 1024 / 1024 / 1024)) -le 8 ] && exit 0
+
+mounts=4
+newfs_flags=""
+
+export runRUNTIME=10m
+export LOAD=80
+export symlinkLOAD=80
+export rwLOAD=80
+export TESTPROGS="
+testcases/lockf2/lockf2
+testcases/symlink/symlink
+testcases/openat/openat
+testcases/rw/rw
+testcases/fts/fts
+testcases/link/link
+testcases/lockf/lockf
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/mkfifo/mkfifo
+"
+
+prefix=$mntpoint
+start=$mdstart
+for i in `jot $mounts $start`; do
+ mdstart=$i
+ mntpoint=${prefix}$i
+ [ -d $mntpoint ] || mkdir -p $mntpoint
+
+ mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+ mdconfig -a -t swap -s 2g -u $mdstart
+ bsdlabel -w md$mdstart auto
+ newfs $newfs_flags md${mdstart}$part > /dev/null
+ mount /dev/md${mdstart}$part $mntpoint
+ chmod 777 $mntpoint
+
+ export RUNDIR=$mntpoint/stressX
+ export CTRLDIR=$mntpoint/stressX.control
+ set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+ export KBLOCKS=$(($1 / 1))
+ export INODES=$(($2 / 1))
+ su $testuser -c 'sleep 2; cd ..; ./testcases/run/run $TESTPROGS' > \
+ /dev/null 2>&1 &
+done
+su $testuser -c "sleep 2; cd ..; ./testcases/swap/swap -t 10m -i 20" &
+
+wait
+
+s=0
+for i in `jot $mounts $start`; do
+ mdstart=$i
+ mntpoint=${prefix}$i
+ n=0
+ while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint && mdconfig -d -u $mdstart || sleep 1
+ n=$((n += 1))
+ [ $n -gt 60 ] && exit 1
+ done
+ checkfs /dev/md${mdstart}$part || s=$?
+done
+exit $s
diff --git a/tools/test/stress2/misc/ping.sh b/tools/test/stress2/misc/ping.sh
new file mode 100755
index 000000000000..2594c6d5e43c
--- /dev/null
+++ b/tools/test/stress2/misc/ping.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+# Regression test for r351461. Handle missing time stamp from SO_TIMESTAMP.
+# Fixed by r352229
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+[ -z "$nfs_export" ] && exit 0
+ip=`echo $nfs_export | sed 's/:.*//'`
+ping -c 2 $ip > /dev/null 2>&1 || exit 0
+
+log=/tmp/ping.log
+tail -F -n 0 /var/log/messages > $log &
+sleep .5
+pid=$!
+for i in `jot 10`; do
+ ping -c 1 $ip &
+ pids="$pids $!"
+done > /dev/null
+for i in $pids; do wait $i; done
+kill $pid
+wait
+grep "(ping)" $log && s=1 || s=0
+rm -f $log
+exit $s
diff --git a/tools/test/stress2/misc/pipe.sh b/tools/test/stress2/misc/pipe.sh
new file mode 100755
index 000000000000..3a27a3dd7d88
--- /dev/null
+++ b/tools/test/stress2/misc/pipe.sh
@@ -0,0 +1,132 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Stress vm object collapse.
+
+# "panic: backing_object 0xfffff800a018f420 was somehow re-referenced during
+# collapse!" seen with uma_zalloc_arg fail point enabled.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/pipe.c
+mycc -o pipe -Wall -Wextra -O0 -g pipe.c || exit 1
+rm -f pipe.c
+cd $odir
+
+daemon sh -c '(cd ../testcases/swap; ./swap -t 10m -i 20)' > /dev/null 2>&1
+sleep 1
+e=0
+export e
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 300 ]; do
+ for i in `jot $(sysctl -n hw.ncpu)`; do
+ /tmp/pipe &
+ pids="$pids $!"
+ done
+ for i in $pids; do
+ wait $i
+ [ $? -ne 0 ] && e=$((e + 1))
+ done
+ pids=""
+ [ $e -ne 0 ] && break
+done
+while pgrep -q swap; do
+ pkill -9 swap
+done
+rm -rf /tmp/pipe pipe.core
+exit $e
+
+EOF
+#include <sys/wait.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <time.h>
+#include <unistd.h>
+
+#define PIPES 64
+#define RUNTIME 300
+
+int
+test(void)
+{
+ int c, e, status;
+ int fds[PIPES][2];
+ int i;
+
+ for (i = 0; i < PIPES; i++) {
+ if (pipe(fds[i]) == -1)
+ err(1, "pipe");
+ }
+ c = e = 0;
+ if (write(fds[0][1], &c, sizeof(c)) != sizeof(c))
+ err(1, "pipe write");
+ for (i = 0; i < PIPES; i++) {
+ if (fork() == 0) {
+ close(fds[i][1]);
+ if (read(fds[i][0], &c, sizeof(c)) != sizeof(c))
+ err(1, "pipe read");
+#if defined(DEBUG)
+ fprintf(stderr, "pid %d: i = %d: read %d\n", getpid(),
+ i, c);
+#endif
+ c++;
+ if (i != PIPES - 1)
+ if (write(fds[i + 1][1], &c, sizeof(c)) !=
+ sizeof(c))
+ err(1, "pipe write");
+
+ _exit(0);
+ }
+ close(fds[i][0]);
+ close(fds[i][1]);
+ }
+ for (i = 0; i < PIPES; i++) {
+ wait(&status);
+ e += status == 0 ? 0 : 1;
+ }
+
+ return (e);
+}
+
+int
+main(void)
+{
+ time_t start;
+ int e;
+
+ e = 0;
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME && e == 0)
+ e = test();
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/pipe2.sh b/tools/test/stress2/misc/pipe2.sh
new file mode 100755
index 000000000000..b20318c45a98
--- /dev/null
+++ b/tools/test/stress2/misc/pipe2.sh
@@ -0,0 +1,163 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# pipe(2) test
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/pipe2.c
+mycc -o pipe2 -Wall -Wextra -O0 -g pipe2.c || exit 1
+rm -f pipe2.c
+
+daemon sh -c "(cd $odir/../testcases/swap; ./swap -t 10m -i 20)" > \
+ /dev/null 2>&1
+sleep 10
+
+su $testuser -c /tmp/pipe2
+s=$?
+
+while pgrep -q swap; do
+ pkill -9 swap
+done
+
+rm -rf /tmp/pipe2
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+volatile u_int *share, *share2;
+
+#define R1 1 /* sync start */
+#define R2 2 /* forks */
+
+#define PIPES 128
+#define PARALLEL 32
+
+static void
+hand(int i __unused) { /* handler */
+ fprintf(stderr, "Timed out\n");
+ _exit(1);
+}
+
+void
+test(void)
+{
+ size_t len;
+ int fds[2], r;
+ int token;
+
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ if (pipe(fds) == -1)
+ err(1, "pipe");
+ token = 0;
+ write(fds[1], &token, sizeof(token));
+ for (;;) {
+ if (share[R2] >= PIPES)
+ break;
+ if ((r = fork()) == 0) {
+ atomic_add_int(&share[R2], 1);
+ if (read(fds[0], &token, sizeof(token)) != sizeof(token))
+ err(1, "read");
+ close(fds[0]);
+ if (pipe(fds) == -1)
+ err(1, "pipe");
+ token++;
+ if (write(fds[1], &token, sizeof(token)) != sizeof(token))
+ err(1, "write");
+ }
+ if (r == -1)
+ err(1, "fork()");
+ if (r != 0)
+ _exit(0);
+ }
+
+ if (share[R2] == PIPES) {
+#if defined(DEBUG)
+ if (read(fds[0], &token, sizeof(token)) != sizeof(token))
+ err(1, "final read");
+ fprintf(stderr, "FINAL read %d from %d\n", token, fds[0]);
+#endif
+ atomic_add_int(&share2[R1], 1);
+ }
+ _exit(0);
+}
+
+int
+main(void)
+{
+ struct sigaction sa;
+ size_t len;
+ int i;
+
+ len = PAGE_SIZE;
+ if ((share2 = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ sa.sa_handler = SIG_IGN;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction(SIGCHLD, &sa, 0) == -1)
+ err(1, "sigaction");
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ test();
+ }
+
+ signal(SIGALRM, hand);
+ alarm(300);
+ while (share2[R1] != PARALLEL) {
+ sleep(1);
+#if defined(DEBUG)
+ fprintf(stderr, "share2 = %d\n", share2[R1]);
+#endif
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pipe3.sh b/tools/test/stress2/misc/pipe3.sh
new file mode 100755
index 000000000000..6208e1a52f90
--- /dev/null
+++ b/tools/test/stress2/misc/pipe3.sh
@@ -0,0 +1,148 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Peter Holm
+#
+# 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 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.
+#
+
+# pipe(2) tests.
+# "panic: vm_page_dequeue: queued unlocked page 0xfffffe0019a73518" seen.
+
+# Reported by syzkaller
+# Fixed by r354400
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/pipe3.c
+mycc -o pipe3 -Wall -Wextra -O0 -g pipe3.c || exit 1
+rm -f pipe3.c
+cd $odir
+
+(cd ../testcases/swap; ./swap -t 5m -i 20 -l 100) &
+sleep 1
+for i in `jot 25`; do
+ /tmp/pipe3 &
+done
+while pkill -0 pipe3; do sleep 2; done
+while pkill -9 swap; do sleep 1; done
+wait
+rm -rf /tmp/pipe3
+exit 0
+
+EOF
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define PARALLEL 64
+#define RUNTIME (5 * 60)
+
+static void
+handler(int i __unused) {
+ _exit(0);
+}
+
+void
+test(void)
+{
+ int fds[2], r;
+ char c;
+
+ if (pipe(fds) == -1)
+ err(1, "pipe");
+
+ if (fork() == 0) {
+ signal(SIGALRM, handler);
+ ualarm(1 + arc4random() % 10000, 0);
+ close(fds[1]);
+ if ((r = write(fds[0], &c, sizeof(c))) != sizeof(c))
+ if (r == -1)
+ err(1, "pipe write");
+ _exit(0);
+ }
+ signal(SIGALRM, handler);
+ ualarm(1 + arc4random() % 10000, 0);
+ close(fds[0]);
+ if ((r = read(fds[1], &c, sizeof(c))) != sizeof(c))
+ if (r == -1)
+ err(1, "pipe read");
+ wait(NULL);
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL], rpid;
+ time_t start;
+ int i, running, status;
+ bool done;
+
+ for (i = 0; i < PARALLEL; i++)
+ pids[i] = 0;
+ running = 0;
+ start = time(NULL);
+ for (;;) {
+ done = (time(NULL) - start) >= RUNTIME;
+ for (i = 0; i < PARALLEL; i++) {
+ if (pids[i] == 0 && !done) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ running++;
+ }
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (pids[i] != 0) {
+ if ((rpid = waitpid(pids[i], &status,
+ WNOHANG)) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (rpid == 0)
+ continue;
+ if (rpid != pids[i])
+ err(1, "waitpid(%d)", pids[i]);
+ running --;
+ pids[i] = 0;
+ break;
+ }
+ }
+ if (running == 0 && done)
+ break;
+ usleep(100);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pipe_enomem.sh b/tools/test/stress2/misc/pipe_enomem.sh
new file mode 100755
index 000000000000..92558b164336
--- /dev/null
+++ b/tools/test/stress2/misc/pipe_enomem.sh
@@ -0,0 +1,102 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Konstantin Belousov
+#
+# 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 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.
+#
+
+# Pipe test scenario from https://reviews.freebsd.org/D23993
+# https://gist.github.com/kostikbel/b2844258b7fba6e8ce3ccd8ef9422e5a
+
+. ../default.cfg
+
+cd /tmp
+cat > pipe_enomem.c <<EOF
+/* $Id: pipe_enomem.c,v 1.3 2020/03/07 21:02:04 kostik Exp kostik $ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+struct pipepair {
+ int pp[2];
+};
+
+static struct pipepair *p;
+
+int
+main(void)
+{
+ int error, pp[2];
+ size_t i, k, nsz, sz;
+ char x;
+
+ sz = 1024;
+ p = calloc(sz, sizeof(struct pipepair));
+ if (p == NULL) {
+ fprintf(stderr, "calloc: %s\n", strerror(errno));
+ exit(1);
+ }
+
+ for (i = 0;; i++) {
+ if (pipe(pp) == -1) {
+ printf("created %zd pipes. syscall error %s\n",
+ i, strerror(errno));
+ break;
+ }
+ if (i >= sz) {
+ nsz = sz * 2;
+ p = reallocf(p, nsz * sizeof(struct pipepair));
+ if (p == NULL) {
+ fprintf(stderr, "reallocf: %s\n",
+ strerror(errno));
+ exit(1);
+ }
+ memset(p + sz, 0, (nsz - sz) * sizeof(struct pipepair));
+ sz = nsz;
+ }
+ p[i].pp[0]= pp[0];
+ p[i].pp[1]= pp[1];
+ }
+
+ x = 'a';
+ for (k = 0; k < i; k++) {
+ error = write(p[k].pp[1], &x, 1);
+ if (error == -1)
+ printf("pipe %zd fds %d %d error %s\n",
+ k, p[k].pp[0], p[k].pp[1], strerror(errno));
+ else if (error == 0)
+ printf("pipe %zd fds %d %d EOF\n",
+ k, p[k].pp[0], p[k].pp[1]);
+ }
+}
+EOF
+
+mycc -o pipe_enomem -Wall -Wextra -O2 pipe_enomem.c || exit 1
+./pipe_enomem 2>&1 | head -5
+rm -f pipe_enomem.c pipe_enomem
+exit 0
diff --git a/tools/test/stress2/misc/pkru.sh b/tools/test/stress2/misc/pkru.sh
new file mode 100755
index 000000000000..9ac0a5000daf
--- /dev/null
+++ b/tools/test/stress2/misc/pkru.sh
@@ -0,0 +1,512 @@
+#/bin/sh
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Konstantin Belousov <kib@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Test scenario for Intel userspace protection keys feature on Skylake Xeons
+
+grep -qw PKU /var/run/dmesg.boot || exit 0
+cd /tmp
+cat > /tmp/pkru_exec.c <<EOF
+/* $Id: pkru_exec.c,v 1.4 2019/01/12 04:55:57 kostik Exp kostik $ */
+/*
+ * cc -Wall -Wextra -g -O -o pkru_fork64 pkru_fork.c
+ * Run it with LD_BIND_NOW=1.
+ */
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <machine/sysarch.h>
+#include <err.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#ifdef TEST_COMPILE
+int x86_pkru_get_perm(unsigned int keyidx, int *access, int *modify);
+int x86_pkru_set_perm(unsigned int keyidx, int access, int modify);
+int x86_pkru_protect_range(void *addr, unsigned long len, unsigned int keyidx,
+ int flag);
+int x86_pkru_unprotect_range(void *addr, unsigned long len);
+uint32_t rdpkru(void);
+void wrpkru(uint32_t);
+#define AMD64_PKRU_PERSIST 0x0001
+#endif
+
+extern char **environ;
+
+#define OPKEY 1
+
+int
+main(void)
+{
+ char *args[3] = {
+ "/bin/date",
+ NULL
+ };
+ struct rlimit rl;
+
+ if (getrlimit(RLIMIT_STACK, &rl) != 0)
+ err(1, "getrlimit RLIMIT_STACK");
+ if (x86_pkru_protect_range(0, 0x800000000000 - rl.rlim_max, OPKEY,
+ AMD64_PKRU_PERSIST) != 0)
+ err(1, "x86_pkru_protect_range");
+ if (x86_pkru_set_perm(1, 1, 0) != 0)
+ err(1, "x86_pkru_set_perm");
+ execve("/bin/date", args, environ);
+}
+EOF
+cc -Wall -Wextra -g -O -o pkru_exec64 pkru_exec.c || exit 1
+cc -Wall -Wextra -g -O -o pkru_exec32 pkru_exec.c -m32 || exit 1
+rm pkru_exec.c
+echo "Expect: Segmentation fault (core dumped)"
+LD_BIND_NOW=1 ./pkru_exec64
+LD_BIND_NOW=1 ./pkru_exec32
+rm -f pkru_exec64 pkru_exec32 pkru_exec64.core pkru_exec32.core
+
+cat > /tmp/pkru_fork.c <<EOF
+/* $Id: pkru_fork.c,v 1.2 2019/01/12 03:39:42 kostik Exp kostik $ */
+/* cc -Wall -Wextra -g -O -o pkru_fork64 pkru_fork.c */
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+#include <machine/cpufunc.h>
+#include <machine/sysarch.h>
+#include <x86/fpu.h>
+#include <err.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#ifdef TEST_COMPILE
+int x86_pkru_get_perm(unsigned int keyidx, int *access, int *modify);
+int x86_pkru_set_perm(unsigned int keyidx, int access, int modify);
+int x86_pkru_protect_range(void *addr, unsigned long len, unsigned int keyidx,
+ int flag);
+int x86_pkru_unprotect_range(void *addr, unsigned long len);
+uint32_t rdpkru(void);
+void wrpkru(uint32_t);
+#endif
+
+static volatile char *mapping;
+
+#define OPKEY 1
+
+int
+main(void)
+{
+ int error;
+ pid_t child;
+
+ mapping = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANON, -1, 0);
+ if (mapping == MAP_FAILED)
+ err(1, "mmap");
+ error = x86_pkru_protect_range((void *)mapping, getpagesize(),
+ OPKEY, 0);
+ if (error != 0)
+ err(1, "x86_pkru_protect_range");
+ error = x86_pkru_set_perm(OPKEY, 0, 0);
+ if (error != 0)
+ err(1, "x86_pkru_set_perm");
+ child = fork();
+ if (child == -1)
+ err(1, "fork");
+ if (child == 0) {
+ *mapping = 0;
+ printf("Still alive, pkru did not worked after fork");
+ }
+ waitpid(child, NULL, 0);
+}
+EOF
+cc -Wall -Wextra -g -O -o pkru_fork64 pkru_fork.c || exit 1
+cc -Wall -Wextra -g -O -o pkru_fork32 -m32 pkru_fork.c || exit 1
+rm pkru_fork.c
+./pkru_fork64
+./pkru_fork32
+rm -f pkru_fork64 pkru_fork64.core pkru_fork32 pkru_fork32.core
+
+cat > /tmp/pkru_perm.c <<EOF
+/* $Id: pkru_perm.c,v 1.6 2019/01/12 04:43:20 kostik Exp kostik $ */
+/* cc -Wall -Wextra -g -O -o pkru_fork64 pkru_fork.c */
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <machine/cpufunc.h>
+#include <machine/sysarch.h>
+#include <x86/fpu.h>
+#include <err.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#ifdef TEST_COMPILE
+int x86_pkru_get_perm(unsigned int keyidx, int *access, int *modify);
+int x86_pkru_set_perm(unsigned int keyidx, int access, int modify);
+int x86_pkru_protect_range(void *addr, unsigned long len, unsigned int keyidx,
+ int flag);
+int x86_pkru_unprotect_range(void *addr, unsigned long len);
+uint32_t rdpkru(void);
+void wrpkru(uint32_t);
+#define AMD64_PKRU_PERSIST 0x0001
+#endif
+
+static void
+sighandler(int signo __unused, siginfo_t *si __unused, void *uc1 __unused)
+{
+
+ exit(0);
+}
+
+static volatile char *mapping;
+
+#define OPKEY 1
+
+int
+main(void)
+{
+ struct sigaction sa;
+ char *mapping1;
+ int error;
+
+ error = x86_pkru_set_perm(OPKEY, 0, 0);
+ if (error != 0)
+ err(1, "x86_pkru_set_perm");
+ mapping = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANON, -1, 0);
+ if (mapping == MAP_FAILED)
+ err(1, "mmap");
+ error = x86_pkru_protect_range((void *)mapping, getpagesize(),
+ OPKEY, 0);
+ if (error != 0)
+ err(1, "x86_pkru_protect_range");
+ error = munmap((void *)mapping, getpagesize());
+ if (error != 0)
+ err(1, "munmap");
+ mapping1 = mmap((void *)mapping, getpagesize(), PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_EXCL, -1, 0);
+ if (mapping1 == MAP_FAILED)
+ err(1, "mmap 2");
+ *mapping = 0;
+ error = x86_pkru_protect_range((void *)mapping, getpagesize(),
+ OPKEY, AMD64_PKRU_PERSIST);
+ mapping1 = mmap((void *)mapping, getpagesize(), PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
+ if (mapping1 == MAP_FAILED)
+ err(1, "mmap 3");
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_sigaction = sighandler;
+ sa.sa_flags = SA_SIGINFO;
+ if (sigaction(SIGSEGV, &sa, NULL) == -1)
+ err(1, "sigaction");
+ *mapping = 0;
+ printf("Still alive, pkru persist did not worked");
+ exit(1);
+}
+EOF
+cc -Wall -Wextra -g -O -o pkru_perm64 pkru_perm.c || exit 1
+cc -Wall -Wextra -g -O -o pkru_perm32 -m32 pkru_perm.c || exit 1
+rm pkru_perm.c
+./pkru_perm64
+./pkru_perm32
+rm -f pkru_perm64 pkru_perm32
+
+cat > /tmp/pkru.c <<EOF
+/* $Id: pkru.c,v 1.27 2019/01/10 12:06:31 kostik Exp $ */
+/* cc -Wall -Wextra -g -O -o pkru64 pkru.c -lpthread */
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <machine/cpufunc.h>
+#include <machine/sysarch.h>
+#include <x86/fpu.h>
+#include <err.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#ifdef TEST_COMPILE
+int x86_pkru_get_perm(unsigned int keyidx, int *access, int *modify);
+int x86_pkru_set_perm(unsigned int keyidx, int access, int modify);
+int x86_pkru_protect_range(void *addr, unsigned long len, unsigned int keyidx,
+ int flag);
+int x86_pkru_unprotect_range(void *addr, unsigned long len);
+uint32_t rdpkru(void);
+void wrpkru(uint32_t);
+#endif
+
+static char *mut_region;
+static size_t mut_region_len;
+static unsigned *mut_region_keys;
+static pthread_t bga_thr;
+static int signal_seen;
+static siginfo_t si_seen;
+static ucontext_t *uc_seen;
+static u_int rpku_offset;
+
+static void
+handler(int i __unused) {
+ _exit(0);
+}
+
+static void
+report_sig(int signo, siginfo_t *si, ucontext_t *uc)
+{
+
+ printf("signal %d %s", signo, strsignal(signo));
+ printf(" si_code %d si_status %d si_addr %p", si->si_code,
+ si->si_status, si->si_addr);
+ printf(" mc_err %#jx", (uintmax_t)uc->uc_mcontext.mc_err);
+ if (uc->uc_mcontext.mc_xfpustate != 0 &&
+ (unsigned long)uc->uc_mcontext.mc_xfpustate_len >=
+ rpku_offset) {
+ printf(" pkru 0x%08x", *(uint32_t *)(
+ uc->uc_mcontext.mc_xfpustate + rpku_offset));
+ }
+ printf("\n");
+}
+
+static void
+sighandler(int signo, siginfo_t *si, void *u)
+{
+ ucontext_t *uc;
+ pthread_t thr;
+ size_t len;
+ uint32_t *pkrup;
+
+ uc = u;
+ thr = pthread_self();
+ if (thr == bga_thr) {
+ printf("Fault from background access thread\n");
+ report_sig(signo, si, uc);
+ exit(1);
+ }
+ signal_seen = signo;
+ si_seen = *si;
+
+ len = sizeof(ucontext_t);
+ if (uc->uc_mcontext.mc_xfpustate != 0)
+ len += uc->uc_mcontext.mc_xfpustate_len;
+ uc_seen = malloc(len);
+ if (uc_seen == NULL)
+ err(1, "malloc(%d)", (int)len);
+ memcpy(uc_seen, uc, sizeof(*uc));
+#if 0
+printf("signal %d xpfustate %p len %ld rpkuo %u\n", signo, (void *)uc->uc_mcontext.mc_xfpustate, uc->uc_mcontext.mc_xfpustate_len, rpku_offset);
+#endif
+ if (uc->uc_mcontext.mc_xfpustate != 0) {
+ uc_seen->uc_mcontext.mc_xfpustate = (uintptr_t)uc_seen +
+ sizeof(*uc);
+ memcpy((void *)uc_seen->uc_mcontext.mc_xfpustate,
+ (void *)uc->uc_mcontext.mc_xfpustate,
+ uc->uc_mcontext.mc_xfpustate_len);
+
+ if ((unsigned long)uc->uc_mcontext.mc_xfpustate_len >=
+ rpku_offset + sizeof(uint32_t)) {
+ pkrup = (uint32_t *)(rpku_offset +
+ (char *)uc->uc_mcontext.mc_xfpustate);
+#if 0
+printf("signal %d *pkru %08x\n", signo, *pkrup);
+#endif
+ *pkrup = 0;
+ }
+ }
+}
+
+static void *
+bg_access_thread_fn(void *arg __unused)
+{
+ char *c, x;
+
+ pthread_set_name_np(pthread_self(), "bgaccess");
+ for (x = 0, c = mut_region;;) {
+ *c = x;
+ if (++c >= mut_region + mut_region_len) {
+ c = mut_region;
+ x++;
+ }
+ }
+ return (NULL);
+}
+
+static void
+clear_signal_report(void)
+{
+
+ signal_seen = 0;
+ free(uc_seen);
+ uc_seen = NULL;
+}
+
+static void
+check_signal(unsigned key, int check_access, int check_modify)
+{
+
+ if (signal_seen == 0) {
+ printf("Did not get signal, key %d check_access %d "
+ "check_modify %d\n", key, check_access, check_modify);
+ printf("pkru 0x%08x\n", rdpkru());
+ exit(1);
+ }
+}
+
+static void
+check_no_signal(void)
+{
+
+ if (signal_seen != 0) {
+ printf("pkru 0x%08x\n", rdpkru());
+ printf("Got signal\n");
+ report_sig(signal_seen, &si_seen, uc_seen);
+ exit(1);
+ }
+}
+
+static void
+check(char *p, unsigned key, int check_access, int check_modify)
+{
+ int access, error, modify, orig_access, orig_modify;
+
+ error = x86_pkru_get_perm(key, &orig_access, &orig_modify);
+ if (error != 0)
+ err(1, "x86_pkru_get_perm");
+ access = check_access ? 0 : 1;
+ modify = check_modify ? 0 : 1;
+ error = x86_pkru_set_perm(key, access, modify);
+ if (error != 0)
+ err(1, "x86_pkru_set_perm access");
+ clear_signal_report();
+ if (check_modify)
+ *(volatile char *)p = 1;
+ else if (check_access)
+ *(volatile char *)p;
+ if (key == mut_region_keys[(p - mut_region) / getpagesize()])
+ check_signal(key, check_access, check_modify);
+ else
+ check_no_signal();
+ error = x86_pkru_set_perm(key, orig_access, orig_modify);
+ if (error != 0)
+ err(1, "x86_pkru_set_perm access restore");
+ clear_signal_report();
+ if (check_modify)
+ *(volatile char *)p = 1;
+ else if (check_access)
+ *(volatile char *)p;
+ check_no_signal();
+}
+
+static void
+mutate_perms(void)
+{
+ unsigned key;
+ char *p;
+
+ for (p = mut_region;;) {
+ for (key = 1; key < 0x10; key++) {
+ check(p, key, 1, 0);
+ check(p, key, 0, 1);
+ check(p, key, 1, 1);
+ }
+ p += getpagesize();
+ if (p >= mut_region + mut_region_len)
+ p = mut_region;
+ }
+}
+
+int
+main(void)
+{
+ struct sigaction sa;
+ char *p;
+ unsigned i;
+ u_int regs[4];
+ int error;
+
+ cpuid_count(0xd, 0x9, regs);
+ rpku_offset = regs[1];
+ if (rpku_offset != 0)
+#if defined(__i386__)
+ rpku_offset -= sizeof(union savefpu);
+#else
+ rpku_offset -= sizeof(struct savefpu);
+#endif
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_sigaction = sighandler;
+ sa.sa_flags = SA_SIGINFO;
+ if (sigaction(SIGSEGV, &sa, NULL) == -1)
+ err(1, "sigaction SIGSEGV");
+ if (sigaction(SIGBUS, &sa, NULL) == -1)
+ err(1, "sigaction SIGBUS");
+
+ mut_region_len = getpagesize() * 100;
+ mut_region_keys = calloc(mut_region_len, sizeof(unsigned));
+ if (mut_region_keys == NULL)
+ err(1, "calloc keys");
+ mut_region = mmap(NULL, mut_region_len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0);
+ if (mut_region == MAP_FAILED)
+ err(1, "mmap");
+ for (i = 1, p = mut_region; p < mut_region + mut_region_len;
+ p += getpagesize()) {
+ error = x86_pkru_protect_range(p, getpagesize(), i, 0);
+ if (error != 0)
+ err(1, "x86_pkru_protect_range key %d", i);
+ mut_region_keys[(p - mut_region) / getpagesize()] = i;
+ if (++i > 0xf)
+ i = 1;
+ }
+
+ signal(SIGALRM, handler);
+ alarm(5);
+ error = pthread_create(&bga_thr, NULL, bg_access_thread_fn, NULL);
+ if (error != 0)
+ errc(1, error, "pthread create background access thread");
+
+ mutate_perms();
+}
+EOF
+cc -Wall -Wextra -g -O -o pkru64 pkru.c -lpthread || exit 1
+cc -Wall -Wextra -g -O -o pkru32 -m32 pkru.c -lpthread || exit 1
+rm pkru.c
+./pkru64
+./pkru32
+rm -f pkru64 pkru32
+
+exit
diff --git a/tools/test/stress2/misc/pkru2.sh b/tools/test/stress2/misc/pkru2.sh
new file mode 100755
index 000000000000..cf9fac187793
--- /dev/null
+++ b/tools/test/stress2/misc/pkru2.sh
@@ -0,0 +1,108 @@
+#/bin/sh
+
+# Test scenario for Intel userspace protection keys feature on Skylake Xeons
+# Based on tests by kib@
+
+grep -qw PKU /var/run/dmesg.boot || exit 0
+cd /tmp
+
+cat > /tmp/pkru2a.c <<EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+#include <machine/sysarch.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile char *mapping;
+
+#define OPKEY 1
+
+int
+main(void)
+{
+ time_t start;
+ int error;
+
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ mapping = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANON, -1, 0);
+ if (mapping == MAP_FAILED)
+ err(1, "mmap");
+ error = x86_pkru_protect_range((void *)mapping,
+ getpagesize(), OPKEY, 0);
+ error = x86_pkru_protect_range((void *)mapping,
+ getpagesize() * 64, OPKEY, 0);
+ if (error != 0)
+ err(1, "x86_pkru_protect_range");
+ error = x86_pkru_set_perm(OPKEY, 0, 0);
+ if (error != 0)
+ err(1, "x86_pkru_set_perm");
+ if (munmap((void *)mapping, getpagesize()) == -1)
+ err(1, "munmap()");
+ }
+ return (0);
+}
+EOF
+cc -Wall -Wextra -g -O -o pkru2a64 pkru2a.c || exit 1
+cc -Wall -Wextra -g -O -o pkru2a32 -m32 pkru2a.c || exit 1
+rm pkru2a.c
+./pkru2a64
+./pkru2a32
+rm -f pkru2a64 pkru2a32
+
+cat > /tmp/pkru2b.c <<EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+#include <machine/sysarch.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile char *mapping;
+
+#define OPKEY 1
+
+int
+main(void)
+{
+ time_t start;
+ int error;
+
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ mapping = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANON, -1, 0);
+ if (mapping == MAP_FAILED)
+ err(1, "mmap");
+ error = x86_pkru_protect_range((void *)mapping,
+ getpagesize() * 64, OPKEY, 0);
+ if (error != 0)
+ err(1, "x86_pkru_protect_range");
+ error = x86_pkru_set_perm(OPKEY, 0, 0);
+ if (error != 0)
+ err(1, "x86_pkru_set_perm");
+ if (munmap((void *)mapping, getpagesize()) == -1)
+ err(1, "munmap()");
+ }
+ return (0);
+}
+EOF
+
+cc -Wall -Wextra -g -O -o pkru2b64 pkru2b.c || exit 1
+cc -Wall -Wextra -g -O -o pkru2b32 -m32 pkru2b.c || exit 1
+rm pkru2b.c
+./pkru2b64
+./pkru2b32
+rm -f pkru2b64 pkru2b32
+
+exit
diff --git a/tools/test/stress2/misc/pmc.sh b/tools/test/stress2/misc/pmc.sh
new file mode 100755
index 000000000000..b36cfa89c779
--- /dev/null
+++ b/tools/test/stress2/misc/pmc.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2011 Peter Holm
+# 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 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.
+#
+
+# Simple pmc test
+
+# "panic: [pmc,4950] pm=0x2ddfe880 runcount 0" seen.
+# https://people.freebsd.org/~pho/stress/log/pmc.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+kldstat -v | grep -q hwpmc || { kldload hwpmc; loaded=1; }
+
+event=`pmcstat -L 2>/dev/null | tr -d '\t' | sort -R | head -1`
+if [ -n "$event" ]; then
+ for i in `jot 2`; do
+ pmcstat -P $event -O /tmp/sample.out.$i find -x /var \
+ -name not.there &
+ done
+ sleep 1
+
+ export runRUNTIME=5m
+ pgrep -q pmcstat &&
+ (cd ..; ./run.sh vfs.cfg)
+ wait
+fi
+[ $loaded ] && kldunload hwpmc
+rm -f /tmp/sample.out.*
+exit 0
diff --git a/tools/test/stress2/misc/pmc2.sh b/tools/test/stress2/misc/pmc2.sh
new file mode 100755
index 000000000000..1e773e1448f8
--- /dev/null
+++ b/tools/test/stress2/misc/pmc2.sh
@@ -0,0 +1,51 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Page fault seen:
+# https://people.freebsd.org/~pho/stress/log/pmc2.txt
+
+# https://people.freebsd.org/~pho/stress/log/mmacy004.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+kldstat -v | grep -q hwpmc || { kldload hwpmc; loaded=1; }
+
+event=`pmcstat -L 2>/dev/null | tr -d '\t' | sort -R | head -1`
+if [ -n "$event" ]; then
+ ../misc/churn.sh &
+ pid=$!
+ while ! pgrep -q churn; do sleep .5; done
+ pmcstat -P $event -t churn -O /tmp/pmc2.log
+ wait $pid
+ rm /tmp/pmc2.log
+fi
+[ $loaded ] && kldunload hwpmc
+
+exit 0
diff --git a/tools/test/stress2/misc/pmc3.sh b/tools/test/stress2/misc/pmc3.sh
new file mode 100755
index 000000000000..cdfee000d352
--- /dev/null
+++ b/tools/test/stress2/misc/pmc3.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# syscall fuzz of hwpmc.ko
+
+# Page fault seen:
+# https://people.freebsd.org/~pho/stress/log/hwpmc3-1.txt
+
+kldstat | grep -q hwpmc || { kldload hwpmc.ko; loaded=1; }
+
+num=`kldstat -d -m hwpmc | awk -F ',' '/hwpmc/ {print $2}'`
+[ $num ] || exit 1
+
+for i in `jot 2`; do
+ noswap=1 sleeptime=18 ../misc/syscall4.sh $num
+done
+
+[ $loaded ] && kldunload hwpmc
+exit 0
diff --git a/tools/test/stress2/misc/pmc4.sh b/tools/test/stress2/misc/pmc4.sh
new file mode 100755
index 000000000000..949e97073750
--- /dev/null
+++ b/tools/test/stress2/misc/pmc4.sh
@@ -0,0 +1,124 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# pmc fuzz test
+
+. ../default.cfg
+
+kldstat -v | grep -q hwpmc || { kldload hwpmc; loaded=1; }
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/pmc4.c
+mycc -o pmc4 -Wall -Wextra -O0 -g pmc4.c -lpmc -lpthread || exit 0
+rm -f pmc4.c
+
+for i in `jot 100`; do
+ ./pmc4
+done > /dev/null 2>&1
+
+rm -rf pmc4 pmc4.core
+[ $loaded ] && kldunload hwpmc
+exit 0
+
+EOF
+#include <sys/param.h>
+#include <sys/event.h>
+
+#include <err.h>
+#include <pmc.h>
+#include <pmclog.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static int fd1[2];
+static int kq;
+#define THREADS 20
+
+static void *
+test(void *arg __unused)
+{
+
+ void *rfd;
+ char *cmdline[] = { "/usr/bin/true", NULL };
+
+ if ((rfd = pmclog_open(kq)) == NULL)
+ err(1, "pmclog_open(%d)", kq);
+ if (pmc_configure_logfile(kq) < 0)
+ err(1, "ERROR: Cannot configure log file");
+ sleep(1);
+ usleep(arc4random() % 20000);
+ if (execve(cmdline[0], cmdline, NULL) == -1)
+ err(1, "execve");
+
+ return (0);
+}
+
+int
+main(void)
+{
+ struct kevent ev[3];
+ pthread_t tid[THREADS];
+ int i, n, rc;
+
+ if (pmc_init() == -1)
+ err(1, "pmc_init");
+
+ if (pipe(fd1) == -1)
+ err(1, "pipe()");
+
+ if ((kq = kqueue()) < 0)
+ err(1, "kqueue(). %s:%d", __FILE__, __LINE__);
+
+ n = 0;
+ EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
+ EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
+ n++;
+
+ if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
+ err(1, "kevent(). %s:%d", __FILE__, __LINE__);
+ n = 0;
+ EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
+ EV_DELETE, 0, 0, 0);
+ n++;
+ if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
+ warn("kevent(). %s:%d", __FILE__, __LINE__);
+
+ for (i = 0; i < THREADS; i++) {
+ if ((rc = pthread_create(&tid[i], NULL, test, NULL)) != 0)
+ errc(1, rc, "test()");
+ }
+ for (i = 0; i < THREADS; i++) {
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pmc5.sh b/tools/test/stress2/misc/pmc5.sh
new file mode 100755
index 000000000000..c7305aa4d470
--- /dev/null
+++ b/tools/test/stress2/misc/pmc5.sh
@@ -0,0 +1,138 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# pmc fuzz test
+
+. ../default.cfg
+
+kldstat -v | grep -q hwpmc || { kldload hwpmc; loaded=1; }
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/pmc5.c
+mycc -o pmc5 -Wall -Wextra -O0 -g pmc5.c -lpmc -lpthread || exit 1
+rm -f pmc5.c
+
+for i in `jot 100`; do
+ ./pmc5
+done > /dev/null 2>&1
+
+rm -rf pmc5 pmc5.core
+[ $loaded ] && kldunload hwpmc
+exit 0
+
+EOF
+#include <sys/param.h>
+#include <sys/event.h>
+#include <sys/mman.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <pmc.h>
+#include <pmclog.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+static int fd1[2];
+static int kq;
+#define THREADS 20
+#define SYNC 0
+
+void *
+test(void *arg __unused)
+{
+
+ void *rfd;
+ char *cmdline[] = { "/usr/bin/true", NULL };
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != THREADS)
+ ;
+ if ((rfd = pmclog_open(kq)) == NULL)
+ err(1, "pmclog_open(%d)", kq);
+// if (pmc_configure_logfile(fd1[1]) < 0)
+ if (pmc_configure_logfile(kq) < 0)
+ err(1, "ERROR: Cannot configure log file");
+ sleep(1);
+ usleep(arc4random() % 20000);
+ if (execve(cmdline[0], cmdline, NULL) == -1)
+ err(1, "execve");
+
+ return (0);
+}
+
+int
+main(void)
+{
+ struct kevent ev[3];
+ pthread_t tid[THREADS];
+ size_t len;
+ int i, n, rc;
+
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ if (pmc_init() == -1)
+ err(1, "pmc_init");
+
+ if (pipe(fd1) == -1)
+ err(1, "pipe()");
+
+ if ((kq = kqueue()) < 0)
+ err(1, "kqueue(). %s:%d", __FILE__, __LINE__);
+
+ n = 0;
+ EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
+ EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
+ n++;
+
+ if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
+ err(1, "kevent(). %s:%d", __FILE__, __LINE__);
+ n = 0;
+ EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
+ EV_DELETE, 0, 0, 0);
+ n++;
+ if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
+ warn("kevent(). %s:%d", __FILE__, __LINE__);
+
+ for (i = 0; i < THREADS; i++) {
+ if ((rc = pthread_create(&tid[i], NULL, test, NULL)) != 0)
+ errc(1, rc, "test()");
+ }
+ for (i = 0; i < THREADS; i++) {
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pmc6.sh b/tools/test/stress2/misc/pmc6.sh
new file mode 100755
index 000000000000..4cbdf278125f
--- /dev/null
+++ b/tools/test/stress2/misc/pmc6.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Test scenario from:
+# [Bug 230985] pmcstat triggers assertion "pmcval outside of expected range"
+# by markj@FreeBSD.org
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+kldstat -v | grep -q hwpmc || { kldload hwpmc; loaded=1; }
+
+event=inst_retired.any
+pmcstat -L 2>/dev/null | grep -q $event || exit 0
+pmcstat -P $event -- find -x / -name foo > /dev/null 2>&1
+
+[ $loaded ] && kldunload hwpmc
diff --git a/tools/test/stress2/misc/pmc7.sh b/tools/test/stress2/misc/pmc7.sh
new file mode 100755
index 000000000000..6a4726b24203
--- /dev/null
+++ b/tools/test/stress2/misc/pmc7.sh
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+# From https://reviews.freebsd.org/D17011
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+cd /tmp
+kldstat -v | grep -q hwpmc || { kldload hwpmc; loaded=1; }
+pmcstat -S unhalted_core_cycles -O ppid.pmcstat sleep 10
+pmcstat -R ppid.pmcstat -z100 -G ppid.stacks
+[ $loaded ] && kldunload hwpmc
+rm -f ppid.pmcstat ppid.stacks
diff --git a/tools/test/stress2/misc/poll.sh b/tools/test/stress2/misc/poll.sh
new file mode 100755
index 000000000000..c345ffae9817
--- /dev/null
+++ b/tools/test/stress2/misc/poll.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+# A pipe_poll() regression test.
+# Python test scenario by Alexander Motin <mav@FreeBSD.org>
+# https://reviews.freebsd.org/D21333
+
+# Hang seen:
+# $ procstat -k 19529
+# PID TID COMM KSTACK
+# 19529 101381 python3.7 - ... _sleep kqueue_kevent kern_kevent_fp kern_kevent kern_kevent_generic sys_kevent amd64_syscall fast_syscall_common
+# 19529 101630 python3.7 - ... _sleep pipe_read dofileread kern_readv sys_read amd64_syscall fast_syscall_common
+# 19529 101631 python3.7 - ... _sleep umtxq_sleep do_sem2_wait __umtx_op_sem2_wait amd64_syscall fast_syscall_common
+# $
+
+# Fixed by r351348
+
+[ -z "`type python3 2>/dev/null`" ] && exit 0
+cat > /tmp/poll.py <<EOF
+#!/usr/local/bin/python3
+
+import concurrent.futures
+import asyncio
+
+procpool = concurrent.futures.ProcessPoolExecutor(
+ max_workers=1,
+)
+
+def x():
+ return ['x'] * 10241
+
+async def say():
+ for i in range(100000):
+ await asyncio.get_event_loop().run_in_executor(procpool, x)
+ print(i)
+
+loop = asyncio.get_event_loop()
+loop.run_until_complete(say())
+loop.close()
+EOF
+chmod +x /tmp/poll.py
+
+log=/tmp/poll.log
+start=`date +%s`
+cpuset -l 0 /tmp/poll.py > $log &
+pid=$!
+sleep 60
+s1=`wc -l < $log`
+sleep 60
+s2=`wc -l < $log`
+while pgrep -qf poll.py; do pkill -f poll.py; done
+wait $pid
+[ $s2 -gt $s1 ] && s=0 || s=1
+
+rm -f /tmp/poll.py $log
+exit $s
+
+dtrace -wn '*::pipe_poll:entry {@rw[execname,probefunc] = count(); }'
diff --git a/tools/test/stress2/misc/poll2.sh b/tools/test/stress2/misc/poll2.sh
new file mode 100755
index 000000000000..13db2d0d22d3
--- /dev/null
+++ b/tools/test/stress2/misc/poll2.sh
@@ -0,0 +1,212 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Test of pipe_poll()
+# https://reviews.freebsd.org/D21333
+
+# No problems seen.
+
+# markj@ write:
+# A simplified reproducible might be tricky to come up with. I think this
+# would do it:
+#
+# - Thread W writes 8KB (PIPE_MINDIRECT) of data to a pipe at a time.
+# - Thread P poll()s the pipe for POLLIN.
+# - Thread R reads 8KB of data from the pipe at a time.
+#
+# Thread P uses non-blocking poll() (timeout == 0). When thread P does
+# not see POLLIN, it signals the reader and the writer and continues
+# polling in a loop. When thread P sees POLLIN it signals the reader and
+# sleeps until the reader returns and wakes it up. After threads R and W
+# finish their respective system calls, they always wait for another
+# signal from P before doing anything.
+#
+# Basically, if all three threads are executing their respective system
+# calls, and the reader has drained the writer's data and awoken the
+# writer, there is a window where poll() will return POLLIN even though
+# all data has been read. If the reader then attempts to read() from the
+# pipe again, it will block and the application appears to be hung.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+rm -f $dir/poll2.c || exit 1
+sed '1,/^EOF/d' < $odir/$0 > $dir/poll2.c
+mycc -o poll2 -Wall -Wextra -O0 -g poll2.c -lpthread || exit 1
+
+cpuset -l 0 $dir/poll2
+s=$?
+pkill swap
+wait
+
+rm -rf poll2 poll2.c poll2.core
+exit $s
+
+EOF
+#include <sys/param.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile int done, frd, fwr, fpl;
+static int fds[2];
+static char b1[8192], b2[8192];
+
+#define RUNTIME (2 * 60)
+#define LOOP 400000
+
+static void *
+wr(void *data __unused)
+{
+ int i;
+
+ for (i = 0; i < LOOP; i++) {
+ pthread_set_name_np(pthread_self(), "wr-idle");
+ while (fwr == 0)
+ usleep(5);
+ pthread_set_name_np(pthread_self(), "wr-act");
+ fpl = 1;
+ if (write(fds[1], b1, sizeof(b1)) != sizeof(b1))
+ err(1, "write");
+ fpl = 1;
+ fwr = 0;
+ }
+
+ return (NULL);
+}
+
+static void *
+rd(void *data __unused)
+{
+ int i;
+
+ for (i = 0; i < LOOP; i++) {
+ fpl = 1;
+ pthread_set_name_np(pthread_self(), "rd-idle");
+ while (frd == 0)
+ usleep(5);
+ pthread_set_name_np(pthread_self(), "rd-act");
+ if (read(fds[0], b2, sizeof(b2)) != sizeof(b2))
+ err(1, "read");
+ frd = 0;
+ fpl = 1;
+ }
+ done = 1;
+
+ return (NULL);
+}
+
+static void *
+pl(void *data __unused)
+{
+ struct pollfd pfd;
+ int i, r;
+
+ pfd.fd = fds[0];
+ pfd.events = POLLIN;
+ for (i = 0; done == 0; i++) {
+ pfd.fd = fds[0];
+ pfd.events = POLLIN;
+ pthread_set_name_np(pthread_self(), "pl-idle");
+ pthread_set_name_np(pthread_self(), "pl-act");
+ while (fpl == 0)
+ usleep(5);
+again:
+ if ((r = poll(&pfd, 1, 0)) == -1)
+ err(1, "poll");
+ if (done == 1)
+ return (NULL);
+ if (r == 0) {
+ frd = fwr = 1;
+ goto again;
+ } else {
+ fpl = 0;
+ frd = fwr = 1;
+ }
+ }
+
+ return (NULL);
+}
+
+void
+test(void)
+{
+ pthread_t tid[3];
+ int rc;
+
+ if (pipe(fds) == -1)
+ err(1, "pipe");
+ done = 0;
+ fpl = 0;
+ frd = 0;
+ fwr = 0;
+ if ((rc = pthread_create(&tid[0], NULL, rd, NULL)) != 0)
+ errc(1, rc, "pthread_create");
+ if ((rc = pthread_create(&tid[1], NULL, wr, NULL)) != 0)
+ errc(1, rc, "pthread_create");
+ if ((rc = pthread_create(&tid[2], NULL, pl, NULL)) != 0)
+ errc(1, rc, "pthread_create");
+
+ frd = 1;
+ fwr = 1;
+
+ if ((rc = pthread_join(tid[0], NULL)) != 0)
+ errc(1, rc, "pthread_join");
+ if ((rc = pthread_join(tid[1], NULL)) != 0)
+ errc(1, rc, "pthread_join");
+ if ((rc = pthread_join(tid[2], NULL)) != 0)
+ errc(1, rc, "pthread_join");
+
+ close(fds[0]);
+ close(fds[1]);
+}
+
+int
+main(void)
+{
+ time_t start;
+
+ alarm(600);
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ test();
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/posix_fadvise.sh b/tools/test/stress2/misc/posix_fadvise.sh
new file mode 100755
index 000000000000..df0995570783
--- /dev/null
+++ b/tools/test/stress2/misc/posix_fadvise.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Memory leak. Fixed in r232702.
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > posix_fadvise.c
+mycc -o posix_fadvise -Wall -Wextra -O2 -g posix_fadvise.c
+
+n1=`vmstat -m | grep fadvise | awk '{print $2 + 0}'`
+/tmp/posix_fadvise
+n2=`vmstat -m | grep fadvise | awk '{print $2 + 0}'`
+if [ $((n2 - n1)) -gt 10 ]; then
+ echo FAIL
+ vmstat -m | sed -n '1p;/fadvise/p'
+fi
+
+rm -f /tmp/posix_fadvise posix_fadvise.c
+exit
+EOF
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ off_t len, offset;
+ int advise, fd, i;
+
+ for (i = 0; i < 500; i++) {
+ if ((fd = open("posix_fadvise.c", O_RDONLY)) == -1)
+ err(1, "open()");
+ offset = arc4random();
+ len = arc4random();
+ advise = arc4random() % 6;
+ if (posix_fadvise(fd, offset, len, advise) == -1)
+ warn("posix_fadvise");
+ close(fd);
+ }
+ return (0);
+}
diff --git a/tools/test/stress2/misc/posix_fadvise2.sh b/tools/test/stress2/misc/posix_fadvise2.sh
new file mode 100755
index 000000000000..de9562e6c57b
--- /dev/null
+++ b/tools/test/stress2/misc/posix_fadvise2.sh
@@ -0,0 +1,78 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Looping thread seen:
+# https://people.freebsd.org/~pho/stress/log/kostik850.txt
+# Fixed by r292326.
+
+. ../default.cfg
+[ -f /usr/libexec/sendmail/sendmail ] || exit 0
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > posix_fadvise2.c
+mycc -o posix_fadvise2 -Wall -Wextra -O2 posix_fadvise2.c || exit 1
+rm -f posix_fadvise2.c
+
+/tmp/posix_fadvise2
+
+rm -f /tmp/posix_fadvise2 /tmp/posix_fadvise2
+exit
+EOF
+#include <sys/param.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+char *file[2] = {
+ "/usr/libexec/sendmail/sendmail",
+ "/tmp/posix_fadvise2"};
+
+int
+main(void)
+{
+ int fd, i, r;
+
+ for (i = 0; i < 2; i++) {
+ fprintf(stderr, "Testing with %s.\n", file[i]);
+ if ((fd = open(file[i], O_RDONLY)) == -1)
+ err(1, "open(%s)", file[i]);
+
+/* Arguments from syscall4.sh test as seen in kostik850.txt */
+ if ((r = posix_fadvise(fd, 0x1e9cda7a9ada8319,
+ 0x1e9d1deee0401abd, POSIX_FADV_DONTNEED)) != 0)
+ errc(1, r, "posix_fadvise(%s)", file[i]);
+
+ close(fd);
+ }
+
+ return(0);
+}
diff --git a/tools/test/stress2/misc/posix_fadvise3.sh b/tools/test/stress2/misc/posix_fadvise3.sh
new file mode 100755
index 000000000000..c9dc5895d8ea
--- /dev/null
+++ b/tools/test/stress2/misc/posix_fadvise3.sh
@@ -0,0 +1,120 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Looping thread seen:
+# https://people.freebsd.org/~pho/stress/log/kostik855.txt
+# Fixed by r293197.
+
+# Again on i386:
+# https://people.freebsd.org/~pho/stress/log/kostik867.txt
+# Fixed by r295716.
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > posix_fadvise3.c
+mycc -o posix_fadvise3 -Wall -Wextra -O2 -g posix_fadvise3.c
+
+data=/tmp/posix_fadvise3.data
+dd if=/dev/zero of=$data bs=1m count=64 status=none
+/tmp/posix_fadvise3
+
+rm $data
+truncate -s 64m $data
+/tmp/posix_fadvise3
+
+rm -f /tmp/posix_fadvise3 posix_fadvise3.c $data
+exit
+EOF
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define LOOPS 10000
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+
+u_int32_t r[N];
+
+unsigned long
+makearg(void)
+{
+ unsigned int i;
+ unsigned long val;
+
+ val = arc4random();
+ i = arc4random() % 100;
+ if (i < 20)
+ val = val & 0xff;
+ if (i >= 20 && i < 40)
+ val = val & 0xffff;
+ if (i >= 40 && i < 60)
+ val = (unsigned long)(r) | (val & 0xffff);
+#if defined(__LP64__)
+ if (i >= 60) {
+ val = (val << 32) | arc4random();
+ if (i > 80)
+ val = val & 0x00007fffffffffffUL;
+ }
+#endif
+
+ return(val);
+}
+
+int
+main(void)
+{
+ off_t len, offset;
+ int advise, fd, i, j;
+
+ if ((fd = open("/tmp/posix_fadvise3.data", O_RDONLY)) == -1)
+ err(1, "open()");
+ offset = 0;
+ len = 0x7fffffffffffffff;
+ advise = 4;
+ if (posix_fadvise(fd, offset, len, advise) == -1)
+ warn("posix_fadvise");
+ close(fd);
+
+ for (i = 0; i < LOOPS; i++) {
+ for (j = 0; j < N; j++)
+ r[j] = arc4random();
+ if ((fd = open("/tmp/posix_fadvise3.data", O_RDONLY)) == -1)
+ err(1, "open()");
+ offset = makearg();
+ len = makearg();
+ advise = arc4random() % 6;
+ if (posix_fadvise(fd, offset, len, advise) == -1)
+ warn("posix_fadvise");
+ close(fd);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/posix_openpt.sh b/tools/test/stress2/misc/posix_openpt.sh
new file mode 100755
index 000000000000..8457b8c4fe55
--- /dev/null
+++ b/tools/test/stress2/misc/posix_openpt.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# "panic: Assertion ttyinq_getsize(&tp->t_inq) == 0 failed" seen.
+# https://people.freebsd.org/~pho/stress/log/posix_openpt.txt
+# Test scenario by brde@
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > posix_openpt.c
+mycc -o posix_openpt -Wall -Wextra -O2 posix_openpt.c || exit 1
+rm -f posix_openpt.c
+
+for i in `jot 10`; do
+ /tmp/posix_openpt &
+done
+
+me=`tty`
+stty -f /dev/ptmx 300 2>/dev/null
+for i in /dev/pts/*; do
+ [ $i = $me ] && continue
+ stty -f $i 300 2>/dev/null
+done
+wait
+rm -f /tmp/posix_openpt
+
+exit
+EOF
+#include <err.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ int masterfd, slavefd;
+ char *slave;
+
+ if ((masterfd = posix_openpt(O_RDWR | O_NOCTTY)) == -1)
+ err(1, "posix_openpt");
+ if ((slave = ptsname (masterfd)) == NULL)
+ err(1, "ptsname");
+ if ((slavefd = open(slave, O_RDWR|O_NOCTTY)) == -1)
+ err(1, "open(%s)", slave);
+
+ sleep(arc4random() % 60 + 1);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/posix_openpt2.sh b/tools/test/stress2/misc/posix_openpt2.sh
new file mode 100755
index 000000000000..2d3427c26ea1
--- /dev/null
+++ b/tools/test/stress2/misc/posix_openpt2.sh
@@ -0,0 +1,152 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# "panic: Assertion !tty_gone(tp) failed at ttydevsw.h:153" seen.
+# https://people.freebsd.org/~pho/stress/log/posix_openpt2.txt
+# Fixed by r312077
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > posix_openpt2.c
+mycc -o posix_openpt2 -Wall -Wextra -O2 posix_openpt2.c -lutil || exit 1
+rm -f posix_openpt2.c
+
+/tmp/posix_openpt2 &
+
+while kill -0 $! 2>/dev/null; do
+ $here/../testcases/swap/swap -t 2m -i 20
+done
+wait
+
+rm -f /tmp/posix_openpt2
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <libutil.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <termios.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOOPS 10
+#define PARALLEL 2
+#define RUNTIME 300
+
+void
+churn(char *path)
+{
+ FTS *fts;
+ FTSENT *p;
+ time_t start;
+ int fd, ftsoptions;
+ char *args[2];
+
+ ftsoptions = FTS_PHYSICAL;
+ args[0] = path;
+ args[1] = 0;
+
+ setproctitle("churn");
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME / LOOPS) {
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ while ((p = fts_read(fts)) != NULL) {
+ if (p->fts_info == FTS_D ||
+ p->fts_info == FTS_DP)
+ continue;
+ if ((fd = open(p->fts_path, O_RDONLY)) > 0)
+ close(fd);
+
+ }
+
+ if (errno != 0 && errno != ENOENT)
+ err(1, "fts_read");
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+ }
+
+ _exit(0);
+}
+
+void
+pty(void)
+{
+ time_t start;
+ int masterfd, slavefd;
+ char *slave;
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME / LOOPS) {
+ if ((masterfd = posix_openpt(O_RDWR | O_NOCTTY)) == -1)
+ err(1, "posix_openpt");
+ if ((slave = ptsname (masterfd)) == NULL)
+ err(1, "ptsname");
+ if ((slavefd = open(slave, O_RDWR|O_NOCTTY)) == -1)
+ err(1, "open(%s)", slave);
+ usleep(arc4random() % 10000);
+ close(slavefd);
+ close(masterfd);
+ }
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i, j;
+
+ for (j = 0; j < LOOPS; j++) {
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ pty();
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ churn("/dev/pts");
+ }
+ for (i = 0; i < 2 * PARALLEL; i++)
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pread.sh b/tools/test/stress2/misc/pread.sh
new file mode 100755
index 000000000000..fecdc18675c9
--- /dev/null
+++ b/tools/test/stress2/misc/pread.sh
@@ -0,0 +1,190 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# pread(2) fuzzing inspired by the iknowthis test suite
+# by Tavis Ormandy <taviso cmpxchg8b com>
+
+# Fixed in r227527.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pread.c
+mycc -o pread -Wall -Wextra pread.c
+rm -f pread.c
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mount -t tmpfs tmpfs $mntpoint
+cp -a /usr/include $mntpoint
+echo "Testing tmpfs(5)"
+/tmp/pread $mntpoint
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+
+echo "Testing fdescfs(5)"
+mount -t fdescfs null /dev/fd
+for i in `jot 100`; do
+ /tmp/pread /dev/fd
+done
+
+while mount | grep -q "on /dev/fd "; do
+ umount /dev/fd || sleep 1
+done
+
+echo "Testing procfs(5)"
+mount -t procfs procfs $mntpoint
+/tmp/pread $mntpoint
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+cp -a /usr/include $mntpoint
+echo "Testing FFS"
+/tmp/pread $mntpoint
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+
+mount -t nullfs /bin $mntpoint
+echo "Testing nullfs(5)"
+/tmp/pread $mntpoint
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+
+echo "Testing procfs(5)"
+mount -t procfs procfs $mntpoint
+/tmp/pread $mntpoint
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+
+echo "Testing devfs(8)"
+mount -t devfs devfs $mntpoint
+/tmp/pread $mntpoint
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+
+rm -f /tmp/pread
+exit 0
+EOF
+#include <sys/types.h>
+#include <strings.h>
+#include <dirent.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <pwd.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/uio.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+static void
+hand(int i __unused) { /* handler */
+ _exit(1);
+}
+
+int
+test(char *path)
+{
+
+ FTS *fts;
+ FTSENT *p;
+ int ftsoptions;
+ char *args[2];
+ int buf[64], fd;
+
+ signal(SIGSEGV, hand);
+ signal(SIGABRT, hand);
+ ftsoptions = FTS_PHYSICAL;
+ args[0] = path;
+ args[1] = 0;
+
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ while ((p = fts_read(fts)) != NULL) {
+ if ((fd = open(p->fts_path, O_RDONLY)) == -1) {
+ if (errno != EACCES && errno != ENXIO)
+ warn("open(%s)", p->fts_path);
+ continue;
+ }
+ alarm(1);
+ pread(fd, (void *)0xdeadc0de, 0x7ffffff, 0xffffffff);
+ pread(fd, buf, 0x7ffffff, 0xffffffff);
+ pread(fd, buf, sizeof(buf), 0xffffffff);
+ pread(fd, buf, sizeof(buf), 0);
+ close(fd);
+ }
+ fts_close(fts);
+
+ exit(0);
+}
+
+int
+main(int argc __unused, char **argv)
+{
+ int i;
+ struct passwd *pw;
+
+ if ((pw = getpwnam("nobody")) == NULL)
+ err(1, "no such user: nobody");
+
+ if (setgroups(1, &pw->pw_gid) ||
+ setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
+ seteuid(pw->pw_uid) || setuid(pw->pw_uid))
+ err(1, "Can't drop privileges to \"nobody\"");
+ endpwent();
+
+ if (daemon(0, 0) == -1)
+ err(1, "daemon()");
+
+ for (i = 0; i < 10; i++) {
+ if (fork() == 0)
+ test(argv[1]);
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/proccontrol.sh b/tools/test/stress2/misc/proccontrol.sh
new file mode 100755
index 000000000000..52f7cc824372
--- /dev/null
+++ b/tools/test/stress2/misc/proccontrol.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# A brif test of proccontrol(1). No problems seen.
+
+[ -x /usr/bin/proccontrol ] || exit 0
+
+sleep 60 & pid=$!
+proccontrol -m aslr -s disable sleep 1
+proccontrol -m aslr -s enable sleep 1
+proccontrol -m aslr sleep 1
+proccontrol -m aslr -s disable -p $pid
+proccontrol -m aslr -s enable -p $pid
+proccontrol -m aslr -q -p $pid
+
+proccontrol -m trace -s disable sleep 1
+proccontrol -m trace -s enable sleep 1
+proccontrol -m trace sleep 1
+proccontrol -m trace -q -p $pid
+
+proccontrol -m trapcap -s disable sleep 1
+proccontrol -m trapcap -s enable sleep 1
+proccontrol -m trapcap sleep 1
+proccontrol -m trapcap -s disable -p $pid
+proccontrol -m trapcap -s enable -p $pid
+proccontrol -m trapcap -q -p $pid
+kill $pid
+wait
diff --git a/tools/test/stress2/misc/procfs.sh b/tools/test/stress2/misc/procfs.sh
new file mode 100755
index 000000000000..6b445b0d7e48
--- /dev/null
+++ b/tools/test/stress2/misc/procfs.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mounts=15 # Number of parallel scripts
+mdstart=$mdstart # Use md unit numbers from this point
+
+if [ $# -eq 0 ]; then
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ [ ! -d ${mntpoint}$m ] && mkdir ${mntpoint}$m
+ mount | grep "$mntpoint" | grep -q md$m && umount ${mntpoint}$m
+ done
+
+ # start the parallel tests
+ touch /tmp/$0
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ ./$0 $m &
+ ./$0 find $m > /dev/null 2>&1 &
+ done
+ wait
+else
+ if [ $1 = find ]; then
+ while [ -r /tmp/$0 ]; do
+ ls -lR ${mntpoint}*
+ done
+ else
+
+ # The test: Parallel mount and unmounts
+ for i in `jot 128`; do
+ m=$1
+ mount -t procfs proc ${mntpoint}$m
+ while mount | grep -qw $mntpoint$m; do
+ opt=$([ $((`date '+%s'` % 2)) -eq 0 ] && echo "-f")
+ umount $opt ${mntpoint}$m > /dev/null 2>&1
+ done
+ done
+ rm -f /tmp/$0
+ fi
+fi
diff --git a/tools/test/stress2/misc/procfs2.sh b/tools/test/stress2/misc/procfs2.sh
new file mode 100755
index 000000000000..dd0f1074f9b2
--- /dev/null
+++ b/tools/test/stress2/misc/procfs2.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# panic: not suspended thread 0xc674c870
+# Found by scrashme and fixed in r216120
+
+mount | grep -q "/proc " || { mount -t procfs procfs /proc || exit 1; }
+for i in `find /proc ! -type d`; do
+ dd if=$i of=/dev/null > /dev/null 2>&1
+ dd if=/dev/random of=$i > /dev/null 2>&1
+done
+exit 0
diff --git a/tools/test/stress2/misc/procfs3.sh b/tools/test/stress2/misc/procfs3.sh
new file mode 100755
index 000000000000..c9c4820adbff
--- /dev/null
+++ b/tools/test/stress2/misc/procfs3.sh
@@ -0,0 +1,154 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# procfs(5) test scenario.
+# "panic: wchan 0xc10a4f68 has no wmesg" seen
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q "/proc " || { mount -t procfs procfs /proc || exit 1; }
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > procfs3.c
+mycc -o procfs3 -Wall -Wextra -O2 procfs3.c || exit 1
+rm -f procfs3.c
+cd $here
+
+su $testuser -c /tmp/procfs3
+
+rm -f /tmp/procfs3
+exit 0
+EOF
+#include <sys/param.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <libutil.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define PARALLEL 10
+
+void
+handler(int i __unused)
+{
+}
+
+int
+test(void)
+{
+
+ FTS *fts;
+ FTSENT *p;
+ int ftsoptions;
+ char *args[2];
+ int fd, i;
+ char buf[1629];
+
+ ftsoptions = FTS_PHYSICAL;
+ args[0] = "/proc";
+ args[1] = 0;
+
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ while ((p = fts_read(fts)) != NULL) {
+ switch (p->fts_info) {
+ case FTS_F: /* Ignore. */
+ break;
+ case FTS_D: /* Ignore. */
+ continue;
+ case FTS_DP:
+ continue;
+ case FTS_DC: /* Ignore. */
+ continue;
+ case FTS_SL: /* Ignore. */
+ continue;
+ case FTS_DNR:
+ continue;
+ case FTS_NS:
+ continue;
+ case FTS_ERR:
+ case FTS_DEFAULT:
+ warnx("%s: %s. fts_info = %d", p->fts_path, strerror(p->fts_errno),
+ p->fts_info);
+ continue;
+ default:
+ printf("%s: default, %d\n", getprogname(), p->fts_info);
+ break;
+ }
+
+ if ((fd = open(p->fts_path, O_RDONLY)) == -1)
+ continue;
+ signal(SIGALRM, handler);
+ alarm(1);
+
+ for (i = 0; i < 2; i++) {
+ read(fd, buf, 1629);
+ }
+ close(fd);
+ }
+
+ if (errno != 0 && errno != ENOENT)
+ err(1, "fts_read");
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+
+ return (0);
+}
+
+int
+main(void)
+{
+ int i, j;
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0) {
+ for (j = 0; j < 50; j++) {
+ test();
+ }
+ _exit(0);
+ }
+ }
+
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/procfs4.sh b/tools/test/stress2/misc/procfs4.sh
new file mode 100755
index 000000000000..ffa812a7f73d
--- /dev/null
+++ b/tools/test/stress2/misc/procfs4.sh
@@ -0,0 +1,156 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Test scenario idea by kib@
+
+# "panic: double fault" seen due to recursion
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q procfs || mount -t procfs procfs /proc
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > procfs4.c
+mycc -o procfs4 -Wall -Wextra -O2 procfs4.c || exit 1
+rm -f procfs4.c
+cd $here
+
+su $testuser -c /tmp/procfs4
+e=$?
+
+rm -f /tmp/procfs4
+exit $e
+EOF
+#include <sys/param.h>
+#include <sys/ioctl.h>
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOOPS 1000
+#define MAXRUN 1200
+#define PARALLEL 10
+
+char *files[] = {
+ "cmdline",
+ "ctl",
+ "dbregs",
+ "etype",
+ "file",
+ "fpregs",
+ "map",
+ "mem",
+ "note",
+ "notepg",
+ "osrel",
+ "regs",
+ "rlimit",
+ "status"
+};
+
+void
+test(void)
+{
+ pid_t p;
+ int fd, i, j, n, opens;
+ char path[128];
+
+ for (i = 0; i < 64; i++) {
+ if ((p = fork()) == 0) {
+ setproctitle("Sleeper");
+ usleep(20000);
+ usleep(arc4random() % 200);
+ for (j = 0; j < 10000; j++)
+ getpid();
+ _exit(0);
+ }
+ opens = 0;
+ setproctitle("load");
+ for (j = 0; j < 14; j++) {
+ snprintf(path, sizeof(path), "/proc/%d/%s", p, files[j]);
+ if ((fd = open(path, O_RDWR)) == -1)
+ if ((fd = open(path, O_RDONLY)) == -1)
+ continue;
+
+ ioctl(fd, FIONREAD, &n);
+ if (ioctl(fd, FIONBIO, &n) != -1)
+ opens++;
+
+ close(fd);
+ }
+ kill(p, SIGHUP);
+#if 0
+ if (opens < 1)
+ fprintf(stderr, "Warn %d open(s) for pid %d\n", opens, getpid());
+#endif
+ }
+
+ for (i = 0; i < 64; i++)
+ wait(NULL);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ time_t start;
+ int e, i, j;
+
+ e = 0;
+ start = time(NULL);
+ for (i = 0; i < LOOPS; i++) {
+ for (j = 0; j < PARALLEL; j++) {
+ if (fork() == 0)
+ test();
+ }
+
+ for (j = 0; j < PARALLEL; j++)
+ wait(NULL);
+ usleep(10000);
+ if (time(NULL) - start > MAXRUN) {
+ fprintf(stderr, "FAIL Timeout\n");
+ e = 1;
+ break;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/procfs5.sh b/tools/test/stress2/misc/procfs5.sh
new file mode 100755
index 000000000000..70cd3214db02
--- /dev/null
+++ b/tools/test/stress2/misc/procfs5.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Issue involving signed overflow.
+# Test scenario based on panic seen in
+# http://people.freebsd.org/~pho/stress/log/kostik640.txt
+# Fixed in r258365, r258397.
+
+# Scenario by kib@
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q "/proc " || mount -t procfs procfs /proc
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > procfs_lr.c
+mycc -o procfs_lr -Wall -Wextra -O2 procfs_lr.c || exit 1
+rm -f procfs_lr.c
+
+/tmp/procfs_lr 2>/dev/null
+
+rm -f /tmp/procfs_lr
+
+exit 0
+EOF
+/* $Id: procfs_lr.c,v 1.1 2013/11/16 07:06:46 kostik Exp kostik $ */
+
+#include <sys/types.h>
+#include <sys/fcntl.h>
+#include <err.h>
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ static const char name[] = "/proc/curproc/map";
+ const off_t uio_offset = 0x6f51f3a1185bced9;
+#if defined(__LP64__)
+ const size_t uio_resid = 0x4c330b10965a61af;
+#else
+ const size_t uio_resid = 0x965a61af;
+#endif
+ char buf[1];
+ int error, fd;
+
+ fd = open(name, O_RDONLY);
+ if (fd == -1)
+ err(1, "open");
+ error = pread(fd, buf, uio_resid, uio_offset);
+ if (error == -1)
+ fprintf(stderr, "pread: %s\n", strerror(errno));
+ return (0);
+}
diff --git a/tools/test/stress2/misc/procfs6.sh b/tools/test/stress2/misc/procfs6.sh
new file mode 100755
index 000000000000..b4cc4c1eb4c0
--- /dev/null
+++ b/tools/test/stress2/misc/procfs6.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Regression test for problem introduced by r351741 and fixed by 351815.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+if ! mount | grep -q /proc; then
+ mount -t procfs null /proc || exit 1
+ mounted=1
+fi
+[ `ls /proc | wc -l` -eq 0 ] && { ls -l /proc; exit 1; }
+[ `ls /proc/$$ | wc -l` -eq 0 ] && { ls -l /proc; exit 1; }
+
+[ $mounted ] && umount /proc
+exit 0
diff --git a/tools/test/stress2/misc/procstat.sh b/tools/test/stress2/misc/procstat.sh
new file mode 100755
index 000000000000..91a846ba91ee
--- /dev/null
+++ b/tools/test/stress2/misc/procstat.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# procstat -k test scenario
+# Page fault seen in stack_save_td()
+# https://people.freebsd.org/~pho/stress/log/procstat.txt
+
+[ `uname -m` = "i386" ] || exit 0 # XXX Known issue on i386 XXX
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+./fifo4.sh &
+sleep .1
+while pgrep -fq fifo4.sh; do
+ pgrep fifo2 | xargs procstat -k > /dev/null 2>&1
+done
+echo done
+wait
+exit 0
diff --git a/tools/test/stress2/misc/procstat2.sh b/tools/test/stress2/misc/procstat2.sh
new file mode 100755
index 000000000000..712f53c11b3b
--- /dev/null
+++ b/tools/test/stress2/misc/procstat2.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+# This test scenario would trigger an NMI.
+# https://people.freebsd.org/~pho/stress/log/mjguzik022.txt
+
+# Test scenario idea by Mark Johnston <markj@freebsd.org>
+# Fixed by r357334
+
+../misc/marcus.sh > /dev/null 2>&1 &
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 300 ]; do
+ procstat -kka > /dev/null 2>&1
+done
+../tools/killall.sh
+wait
+
+exit 0
diff --git a/tools/test/stress2/misc/pthread.sh b/tools/test/stress2/misc/pthread.sh
new file mode 100755
index 000000000000..9bb45a11465d
--- /dev/null
+++ b/tools/test/stress2/misc/pthread.sh
@@ -0,0 +1,137 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# panic: spin lock held too long
+
+# Test program and scenario by Peter Wemm <peter@FreeBSD.org>
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > pth.c
+mycc -o pth -Wall pth.c -pthread
+rm -f pth.c
+cd $odir
+
+for i in `jot 2000`; do
+ /tmp/pth 2>/dev/null
+done
+
+rm -f /tmp/pth
+exit
+EOF
+#include <pthread.h>
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdio.h>
+
+static pthread_t worker1_thr;
+static pthread_t worker2_thr;
+
+static pthread_mutex_t worker_mtx;
+static pthread_cond_t worker_go;
+static pthread_cond_t worker_done;
+
+struct workitem {
+ struct workitem *next;
+ int a, b;
+};
+
+struct workitem *head;
+
+static void *
+worker(void *arg)
+{
+ struct workitem *w;
+
+ pthread_detach(pthread_self());
+ fprintf(stderr, "WORKER: started %p\n", arg); fflush(stderr);
+
+ for (;;) {
+ pthread_mutex_lock(&worker_mtx);
+ while (head == NULL) {
+ pthread_cond_wait(&worker_go, &worker_mtx);
+ }
+ w = head;
+ head = w->next;
+ pthread_mutex_unlock(&worker_mtx);
+
+ fprintf(stderr, "WORKER(%p): got work a=%d b=%d\n", arg, w->a, w->b); fflush(stderr);
+ free(w);
+ pthread_cond_signal(&worker_done);
+ }
+}
+
+void
+work_add(int a, int b)
+{
+ struct workitem *w;
+ int dowake = 0;
+
+ w = calloc(sizeof(*w), 1);
+ w->a = a;
+ w->b = b;
+ pthread_mutex_lock(&worker_mtx);
+ if (head == 0)
+ dowake = 1;
+ w->next = head;
+ head = w;
+ pthread_mutex_unlock(&worker_mtx);
+ if (dowake)
+ pthread_cond_signal(&worker_go);
+}
+
+int
+main()
+{
+ pthread_mutex_init(&worker_mtx, NULL);
+ pthread_cond_init(&worker_go, NULL);
+ pthread_cond_init(&worker_done, NULL);
+
+ fprintf(stderr, "pthread create\n"); fflush(stderr);
+ pthread_create(&worker1_thr, NULL, worker, (void *)1);
+ pthread_create(&worker2_thr, NULL, worker, (void *)2);
+
+ work_add(10, 15);
+ work_add(11, 22);
+ work_add(314, 159);
+
+ pthread_mutex_lock(&worker_mtx);
+ while (head != NULL) {
+ pthread_cond_wait(&worker_done, &worker_mtx);
+ }
+ pthread_mutex_unlock(&worker_mtx);
+
+ fprintf(stderr, "job complete\n"); fflush(stderr);
+ exit(0);
+}
diff --git a/tools/test/stress2/misc/pthread2.sh b/tools/test/stress2/misc/pthread2.sh
new file mode 100755
index 000000000000..08236647fcad
--- /dev/null
+++ b/tools/test/stress2/misc/pthread2.sh
@@ -0,0 +1,296 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Threaded producer-consumer test.
+
+. ../default.cfg
+
+export LANG=C
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pthread2.c
+mycc -o pthread2 -Wall -Wextra -O2 -g pthread2.c -lpthread || exit 1
+rm -f pthread2.c /tmp/pthread2.core
+
+log=/tmp/pthread2.`date '+%Y%m%d-%H%M'`
+for i in `jot 5`; do
+ [ $i -eq 1 ] && echo "# `uname -v`"
+ time sh -c '
+ for i in `jot 8`; do
+ /tmp/pthread2 &
+ done
+ wait
+ '
+done > $log 2>&1
+rm -f /tmp/pthread2
+
+if [ -n "$bench" ]; then
+ pair=`ls /tmp/pthread2* | egrep "pthread2\.[0-9]{8}-" | sort |
+ tail -2 | tr '\n' ' '`
+ ministat -w 72 $pair
+else
+ rm -f $log
+fi
+
+# __thr_umutex_lock() may call abort(3) under VM pressure.
+[ -r /tmp/pthread2.core ] && echo FAIL
+exit 0
+EOF
+/*
+ * Threaded producer-consumer test.
+ * Loosly based on work by
+ * Andrey Zonov (c) 2012
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/queue.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#ifdef __FreeBSD__
+#include <pthread_np.h>
+#define __NP__
+#endif
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOCK(x) plock(&x.mtx)
+#define UNLOCK(x) punlock(&x.mtx)
+#define SIGNAL(x) psig(&x.wait)
+#define WAIT(x) pwait(&x.wait, &x.mtx)
+
+long ncreate, nrename, nunlink;
+int bench, max;
+char *dirname1;
+char *dirname2;
+
+struct file {
+ char *name;
+ STAILQ_ENTRY(file) next;
+};
+
+struct files {
+ pthread_mutex_t mtx;
+ pthread_cond_t wait;
+ STAILQ_HEAD(, file) list;
+};
+
+static struct files newfiles;
+static struct files renamedfiles;
+
+#define MAXQ 100000 /* Max create queue length */
+#define MESSAGES 10000000;
+
+static void
+hand(int i __unused) { /* handler */
+ fprintf(stderr, "max = %d, ncreate = %ld, nrename = %ld, nunlink = %ld\n",
+ max, ncreate, nrename, nunlink);
+}
+
+static void
+ahand(int i __unused) { /* handler */
+ fprintf(stderr, "FAIL\n");
+ hand(0);
+ _exit(0);
+}
+
+void
+plock(pthread_mutex_t *l)
+{
+ int rc;
+
+ if ((rc = pthread_mutex_lock(l)) != 0)
+ errc(1, rc, "pthread_mutex_lock");
+}
+
+void
+punlock(pthread_mutex_t *l)
+{
+ int rc;
+
+ if ((rc = pthread_mutex_unlock(l)) != 0)
+ errc(1, rc, "pthread_mutex_unlock");
+}
+
+void
+psig(pthread_cond_t *c)
+{
+ int rc;
+
+ if ((rc = pthread_cond_signal(c)) != 0)
+ errc(1, rc, "pthread_cond_signal");
+}
+
+void
+pwait(pthread_cond_t *c, pthread_mutex_t *l)
+{
+ int rc;
+
+ if ((rc = pthread_cond_wait(c, l)) != 0)
+ errc(1, rc, "pthread_cond_wait");
+}
+
+void *
+loop_create(void *arg __unused)
+{
+ int i;
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ for (i = 0; i < max; i++) {
+ file = malloc(sizeof(*file));
+ asprintf(&file->name, "%s/filename_too-long:%d", dirname1, i);
+ LOCK(newfiles);
+ STAILQ_INSERT_TAIL(&newfiles.list, file, next);
+ ncreate++;
+ UNLOCK(newfiles);
+ SIGNAL(newfiles);
+ if (ncreate - nrename > MAXQ)
+ usleep(400);
+ }
+ return (NULL);
+}
+
+void *
+loop_rename(void *arg __unused)
+{
+ char *filename, *newname;
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ while (nrename < max) {
+ LOCK(newfiles);
+ while (STAILQ_EMPTY(&newfiles.list)) {
+ WAIT(newfiles);
+ }
+ file = STAILQ_FIRST(&newfiles.list);
+ STAILQ_REMOVE_HEAD(&newfiles.list, next);
+ UNLOCK(newfiles);
+ filename = strrchr(file->name, '/');
+ asprintf(&newname, "%s/%s", dirname2, filename);
+ nrename++;
+ free(file->name);
+ file->name = newname;
+ LOCK(renamedfiles);
+ STAILQ_INSERT_TAIL(&renamedfiles.list, file, next);
+ UNLOCK(renamedfiles);
+ SIGNAL(renamedfiles);
+ }
+ return (NULL);
+}
+
+void *
+loop_unlink(void *arg __unused)
+{
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ while (nunlink < max) {
+ LOCK(renamedfiles);
+ while (STAILQ_EMPTY(&renamedfiles.list)) {
+ WAIT(renamedfiles);
+ }
+ file = STAILQ_FIRST(&renamedfiles.list);
+ STAILQ_REMOVE_HEAD(&renamedfiles.list, next);
+ nunlink++;
+ UNLOCK(renamedfiles);
+ free(file->name);
+ free(file);
+ }
+ return (NULL);
+}
+
+int
+main(void)
+{
+ int i;
+ int rc;
+ pthread_t tid[3];
+
+ bench = getenv("bench") != NULL;
+ asprintf(&dirname1, "%s.1", "f1");
+ asprintf(&dirname2, "%s.2", "f2");
+ max = MESSAGES;
+
+ STAILQ_INIT(&newfiles.list);
+ STAILQ_INIT(&renamedfiles.list);
+
+ if ((rc = pthread_mutex_init(&newfiles.mtx, NULL)) != 0)
+ errc(1, rc, "pthread_mutex_init()");
+ if ((rc = pthread_cond_init(&newfiles.wait, NULL)) != 0)
+ errc(1, rc, "pthread_cond_init()");
+ if ((rc = pthread_mutex_init(&renamedfiles.mtx, NULL)) != 0)
+ errc(1, rc, "pthread_mutex_init()");
+ if ((rc = pthread_cond_init(&renamedfiles.wait, NULL)) != 0)
+ errc(1, rc, "pthread_cond_init()");
+
+ signal(SIGINFO, hand);
+ signal(SIGALRM, ahand);
+ alarm(300);
+ if ((rc = pthread_create(&tid[0], NULL, loop_create, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+ if ((rc = pthread_create(&tid[1], NULL, loop_rename, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+ if ((rc = pthread_create(&tid[2], NULL, loop_unlink, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+
+ for (i = 0; i < 3; i++) {
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ }
+
+ if ((rc = pthread_mutex_destroy(&newfiles.mtx)) != 0)
+ errc(1, rc, "pthread_mutex_destroy(newfiles)");
+ if ((rc = pthread_cond_destroy(&newfiles.wait)) != 0)
+ errc(1, rc, "pthread_cond_destroy(newfiles)");
+ if ((rc = pthread_mutex_destroy(&renamedfiles.mtx)) != 0)
+ errc(1, rc, "pthread_mutex_destroy(renamedfiles)");
+ if ((rc = pthread_cond_destroy(&renamedfiles.wait)) != 0)
+ errc(1, rc, "pthread_cond_destroy(renamedfiles)");
+ free(dirname1);
+ free(dirname2);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pthread3.sh b/tools/test/stress2/misc/pthread3.sh
new file mode 100755
index 000000000000..71ec35141ef4
--- /dev/null
+++ b/tools/test/stress2/misc/pthread3.sh
@@ -0,0 +1,304 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# _exit(2) test scenario with focus on shared channel tear down.
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pthread3.c
+mycc -o pthread3 -Wall -Wextra -O2 -g -gdwarf-2 pthread3.c -lpthread || exit 1
+rm -f pthread3.c
+
+for i in `jot 8`; do
+ /tmp/pthread3 &
+ pids="$pids $!"
+done
+s=0
+for i in $pids; do
+ wait $i
+ e=$?
+ [ $e -ne 0 ] && s=$e
+done
+
+rm -f /tmp/pthread3
+
+exit $s
+EOF
+/*
+ * Threaded producer-consumer test.
+ * Loosly based on work by
+ * Andrey Zonov (c) 2012
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/queue.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#ifdef __FreeBSD__
+#include <pthread_np.h>
+#define __NP__
+#endif
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#define LOCK(x) plock(&x.mtx)
+#define UNLOCK(x) punlock(&x.mtx)
+#define SIGNAL(x) psig(&x.wait)
+#define WAIT(x) pwait(&x.wait, &x.mtx)
+
+long ncreate, nrename, nunlink;
+int max;
+char *dirname1;
+char *dirname2;
+
+struct file {
+ char *name;
+ STAILQ_ENTRY(file) next;
+};
+
+struct files {
+ pthread_mutex_t mtx;
+ pthread_cond_t wait;
+ STAILQ_HEAD(, file) list;
+};
+
+static struct files newfiles;
+static struct files renamedfiles;
+
+static void
+hand(int i __unused) { /* handler */
+ fprintf(stderr, "max = %d, ncreate = %ld, nrename = %ld, nunlink = %ld\n",
+ max, ncreate, nrename, nunlink);
+}
+
+static void
+ahand(int i __unused) { /* handler */
+ fprintf(stderr, "FAIL\n");
+ hand(0);
+ _exit(0);
+}
+
+void
+plock(pthread_mutex_t *l)
+{
+ int rc;
+
+ if ((rc = pthread_mutex_lock(l)) != 0)
+ errc(1, rc, "pthread_mutex_lock");
+}
+
+void
+punlock(pthread_mutex_t *l)
+{
+ int rc;
+
+ if ((rc = pthread_mutex_unlock(l)) != 0)
+ errc(1, rc, "pthread_mutex_unlock");
+}
+
+void
+psig(pthread_cond_t *c)
+{
+ int rc;
+
+ if ((rc = pthread_cond_signal(c)) != 0)
+ errc(1, rc, "pthread_cond_signal");
+}
+
+void
+pwait(pthread_cond_t *c, pthread_mutex_t *l)
+{
+ int rc;
+
+ if ((rc = pthread_cond_wait(c, l)) != 0)
+ errc(1, rc, "pthread_cond_wait");
+}
+
+void *
+loop_create(void *arg __unused)
+{
+ int i, j;
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ for (i = 0; i < max; i++) {
+ file = malloc(sizeof(*file));
+ asprintf(&file->name, "%s/filename_too-long:%d", dirname1, i);
+ LOCK(newfiles);
+ STAILQ_INSERT_TAIL(&newfiles.list, file, next);
+ ncreate++;
+ UNLOCK(newfiles);
+ SIGNAL(newfiles);
+ if ((i > 0) && (i % 100000 == 0))
+ for (j = 0; j < 10 && ncreate != nrename; j++)
+ usleep(400);
+ }
+ return (NULL);
+}
+
+void *
+loop_rename(void *arg __unused)
+{
+ char *filename, *newname;
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ while (nrename < max) {
+ LOCK(newfiles);
+ while (STAILQ_EMPTY(&newfiles.list)) {
+ WAIT(newfiles);
+ }
+ file = STAILQ_FIRST(&newfiles.list);
+ STAILQ_REMOVE_HEAD(&newfiles.list, next);
+ UNLOCK(newfiles);
+ filename = strrchr(file->name, '/');
+ asprintf(&newname, "%s/%s", dirname2, filename);
+ nrename++;
+ free(file->name);
+ file->name = newname;
+ LOCK(renamedfiles);
+ STAILQ_INSERT_TAIL(&renamedfiles.list, file, next);
+ UNLOCK(renamedfiles);
+ SIGNAL(renamedfiles);
+ }
+ return (NULL);
+}
+
+void *
+loop_unlink(void *arg __unused)
+{
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ while (nunlink < max) {
+ LOCK(renamedfiles);
+ while (STAILQ_EMPTY(&renamedfiles.list)) {
+ WAIT(renamedfiles);
+ }
+ file = STAILQ_FIRST(&renamedfiles.list);
+ STAILQ_REMOVE_HEAD(&renamedfiles.list, next);
+ nunlink++;
+ UNLOCK(renamedfiles);
+ free(file->name);
+ free(file);
+ if (arc4random() % 100 == 1)
+ if (arc4random() % 100 == 1)
+ if (arc4random() % 100 < 10)
+ _exit(0);
+ }
+ return (NULL);
+}
+
+void
+test(void)
+{
+ int i;
+ int rc;
+ pthread_t tid[3];
+
+ asprintf(&dirname1, "%s.1", "f1");
+ asprintf(&dirname2, "%s.2", "f2");
+// max = 15000000;
+ max = 50000;
+
+ STAILQ_INIT(&newfiles.list);
+ STAILQ_INIT(&renamedfiles.list);
+
+ if ((rc = pthread_mutex_init(&newfiles.mtx, NULL)) != 0)
+ errc(1, rc, "pthread_mutex_init()");
+ if ((rc = pthread_cond_init(&newfiles.wait, NULL)) != 0)
+ errc(1, rc, "pthread_cond_init()");
+ if ((rc = pthread_mutex_init(&renamedfiles.mtx, NULL)) != 0)
+ errc(1, rc, "pthread_mutex_init()");
+ if ((rc = pthread_cond_init(&renamedfiles.wait, NULL)) != 0)
+ errc(1, rc, "pthread_cond_init()");
+
+ signal(SIGINFO, hand);
+ signal(SIGALRM, ahand);
+ alarm(300);
+ if ((rc = pthread_create(&tid[0], NULL, loop_create, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+ if ((rc = pthread_create(&tid[1], NULL, loop_rename, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+ if ((rc = pthread_create(&tid[2], NULL, loop_unlink, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+
+ for (i = 0; i < 3; i++) {
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ }
+
+ if ((rc = pthread_mutex_destroy(&newfiles.mtx)) != 0)
+ errc(1, rc, "pthread_mutex_destroy(newfiles)");
+ if ((rc = pthread_cond_destroy(&newfiles.wait)) != 0)
+ errc(1, rc, "pthread_cond_destroy(newfiles)");
+ if ((rc = pthread_mutex_destroy(&renamedfiles.mtx)) != 0)
+ errc(1, rc, "pthread_mutex_destroy(renamedfiles)");
+ if ((rc = pthread_cond_destroy(&renamedfiles.wait)) != 0)
+ errc(1, rc, "pthread_cond_destroy(renamedfiles)");
+ free(dirname1);
+ free(dirname2);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i;
+
+ alarm(1200);
+ for (i = 0; i < 1000; i++) {
+ if (fork() == 0)
+ test();
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pthread4.sh b/tools/test/stress2/misc/pthread4.sh
new file mode 100755
index 000000000000..8733ac6e2cf0
--- /dev/null
+++ b/tools/test/stress2/misc/pthread4.sh
@@ -0,0 +1,296 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP version of pthread2.sh
+
+. ../default.cfg
+
+export LANG=C
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pthread4.c
+mycc -o pthread4 -Wall -Wextra -O2 -g -gdwarf-2 pthread4.c -lpthread || exit 1
+rm -f pthread4.c /tmp/pthread4.core
+
+log=/tmp/pthread4.`date '+%Y%m%d-%H%M'`
+for i in `jot 5`; do
+ [ $i -eq 1 ] && echo "# `uname -v`"
+ time sh -c '
+ for i in `jot 8`; do
+ /tmp/pthread4 &
+ done
+ wait
+ '
+done > $log 2>&1
+rm -f /tmp/pthread4
+
+if [ -n "$bench" ]; then
+ pair=`ls /tmp/pthread4* | egrep "pthread4\.[0-9]{8}-" | sort |
+ tail -2 | tr '\n' ' '`
+ ministat -w 72 $pair
+else
+ rm -f $log
+fi
+
+exit 0
+EOF
+/*
+ * Threaded producer-consumer test.
+ * Loosly based on work by
+ * Andrey Zonov (c) 2012
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/queue.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#ifdef __FreeBSD__
+#include <pthread_np.h>
+#define __NP__
+#endif
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOCK(x) plock(&x.mtx)
+#define UNLOCK(x) punlock(&x.mtx)
+#define SIGNAL(x) psig(&x.wait)
+#define WAIT(x) pwait(&x.wait, &x.mtx)
+
+long ncreate, nrename, nunlink;
+int bench, max;
+char *dirname1;
+char *dirname2;
+
+struct file {
+ char *name;
+ STAILQ_ENTRY(file) next;
+};
+
+struct files {
+ pthread_mutex_t mtx;
+ pthread_cond_t wait;
+ STAILQ_HEAD(, file) list;
+};
+
+static struct files newfiles;
+static struct files renamedfiles;
+
+static void
+hand(int i __unused) { /* handler */
+ fprintf(stderr, "max = %d, ncreate = %ld, nrename = %ld, nunlink = %ld\n",
+ max, ncreate, nrename, nunlink);
+}
+
+static void
+ahand(int i __unused) { /* handler */
+ fprintf(stderr, "FAIL\n");
+ hand(0);
+ _exit(0);
+}
+
+void
+plock(pthread_mutex_t *l)
+{
+ int rc;
+
+ if ((rc = pthread_mutex_lock(l)) != 0)
+ errc(1, rc, "pthread_mutex_lock");
+}
+
+void
+punlock(pthread_mutex_t *l)
+{
+ int rc;
+
+ if ((rc = pthread_mutex_unlock(l)) != 0)
+ errc(1, rc, "pthread_mutex_unlock");
+}
+
+void
+psig(pthread_cond_t *c)
+{
+ int rc;
+
+ if ((rc = pthread_cond_signal(c)) != 0)
+ errc(1, rc, "pthread_cond_signal");
+}
+
+void
+pwait(pthread_cond_t *c, pthread_mutex_t *l)
+{
+ int rc;
+
+ if ((rc = pthread_cond_wait(c, l)) != 0)
+ errc(1, rc, "pthread_cond_wait");
+}
+
+void *
+loop_create(void *arg __unused)
+{
+ int i, j;
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ for (i = 0; i < max; i++) {
+ file = malloc(sizeof(*file));
+ asprintf(&file->name, "%s/filename_too-long:%d", dirname1, i);
+ LOCK(newfiles);
+ STAILQ_INSERT_TAIL(&newfiles.list, file, next);
+ ncreate++;
+ UNLOCK(newfiles);
+ SIGNAL(newfiles);
+ if ((bench == 0) && (i > 0) && (i % 100000 == 0))
+ for (j = 0; j < 10 && ncreate != nrename; j++)
+ usleep(400);
+ }
+ return (NULL);
+}
+
+void *
+loop_rename(void *arg __unused)
+{
+ char *filename, *newname;
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ while (nrename < max) {
+ LOCK(newfiles);
+ while (STAILQ_EMPTY(&newfiles.list)) {
+ WAIT(newfiles);
+ }
+ file = STAILQ_FIRST(&newfiles.list);
+ STAILQ_REMOVE_HEAD(&newfiles.list, next);
+ UNLOCK(newfiles);
+ filename = strrchr(file->name, '/');
+ asprintf(&newname, "%s/%s", dirname2, filename);
+ nrename++;
+ free(file->name);
+ file->name = newname;
+ LOCK(renamedfiles);
+ STAILQ_INSERT_TAIL(&renamedfiles.list, file, next);
+ UNLOCK(renamedfiles);
+ SIGNAL(renamedfiles);
+ }
+ return (NULL);
+}
+
+void *
+loop_unlink(void *arg __unused)
+{
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ while (nunlink < max) {
+ LOCK(renamedfiles);
+ while (STAILQ_EMPTY(&renamedfiles.list)) {
+ WAIT(renamedfiles);
+ }
+ file = STAILQ_FIRST(&renamedfiles.list);
+ STAILQ_REMOVE_HEAD(&renamedfiles.list, next);
+ nunlink++;
+ UNLOCK(renamedfiles);
+ free(file->name);
+ free(file);
+ }
+ return (NULL);
+}
+
+int
+main(void)
+{
+ int i;
+ int rc;
+ pthread_t tid[3];
+ pthread_mutexattr_t attr, *pattr = NULL;
+
+ bench = getenv("bench") != NULL;
+ asprintf(&dirname1, "%s.1", "f1");
+ asprintf(&dirname2, "%s.2", "f2");
+ max = 15000000;
+
+ STAILQ_INIT(&newfiles.list);
+ STAILQ_INIT(&renamedfiles.list);
+
+ pthread_mutexattr_init (&attr);
+ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
+ pattr = &attr;
+ if ((rc = pthread_mutex_init(&newfiles.mtx, pattr)) != 0)
+ errc(1, rc, "pthread_mutex_init()");
+ if ((rc = pthread_cond_init(&newfiles.wait, NULL)) != 0)
+ errc(1, rc, "pthread_cond_init()");
+ if ((rc = pthread_mutex_init(&renamedfiles.mtx, NULL)) != 0)
+ errc(1, rc, "pthread_mutex_init()");
+ if ((rc = pthread_cond_init(&renamedfiles.wait, NULL)) != 0)
+ errc(1, rc, "pthread_cond_init()");
+
+ signal(SIGINFO, hand);
+ signal(SIGALRM, ahand);
+ alarm(300);
+ if ((rc = pthread_create(&tid[0], NULL, loop_create, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+ if ((rc = pthread_create(&tid[1], NULL, loop_rename, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+ if ((rc = pthread_create(&tid[2], NULL, loop_unlink, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+
+ for (i = 0; i < 3; i++) {
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ }
+
+ if ((rc = pthread_mutex_destroy(&newfiles.mtx)) != 0)
+ errc(1, rc, "pthread_mutex_destroy(newfiles)");
+ if ((rc = pthread_cond_destroy(&newfiles.wait)) != 0)
+ errc(1, rc, "pthread_cond_destroy(newfiles)");
+ if ((rc = pthread_mutex_destroy(&renamedfiles.mtx)) != 0)
+ errc(1, rc, "pthread_mutex_destroy(renamedfiles)");
+ if ((rc = pthread_cond_destroy(&renamedfiles.wait)) != 0)
+ errc(1, rc, "pthread_cond_destroy(renamedfiles)");
+ free(dirname1);
+ free(dirname2);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pthread5.sh b/tools/test/stress2/misc/pthread5.sh
new file mode 100755
index 000000000000..02093ae48ed5
--- /dev/null
+++ b/tools/test/stress2/misc/pthread5.sh
@@ -0,0 +1,121 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Stress shchan allocations.
+
+. ../default.cfg
+[ `swapinfo | wc -l` -eq 1 ] && exit 0 # kstack allocation failed
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pthread5.c
+mycc -o pthread5 -Wall -Wextra -O2 pthread5.c -lpthread || exit 1
+rm -f pthread5.c
+
+/tmp/pthread5
+
+rm -f /tmp/pthread5
+exit 0
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <errno.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/event.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define ITER 2
+#define PARALLEL 1000
+#define THREADS 100
+
+pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void *
+nicethreads(void *data __unused)
+{
+ struct timespec ts;
+ struct timeval tp;
+
+ pthread_mutex_lock(&mutex);
+ gettimeofday(&tp, NULL);
+
+ ts.tv_sec = tp.tv_sec;
+ ts.tv_nsec = tp.tv_usec * 1000;
+ ts.tv_sec += 30;
+
+ pthread_cond_timedwait(&cond, &mutex, &ts);
+ pthread_mutex_unlock(&mutex);
+
+ return (NULL);
+}
+
+int
+test(void)
+{
+ int num_thread = THREADS;
+ pthread_t tid[num_thread];
+ int i, iter, rc;
+
+ for (iter = 0; iter < ITER; iter++) {
+ for (i = 0; i < num_thread; i++) {
+ if ((rc = pthread_create(&tid[i], NULL, nicethreads,
+ NULL)) != 0)
+ errc(1, rc, "pthread_create");
+ }
+ usleep(20000);
+ for (i = 0; i < num_thread; i++) {
+ rc = pthread_mutex_lock(&mutex);
+ rc = pthread_cond_signal(&cond);
+ rc = pthread_mutex_unlock(&mutex);
+ }
+ for (i = 0; i < num_thread; i++)
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join");
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i;
+
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ test();
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pthread6.sh b/tools/test/stress2/misc/pthread6.sh
new file mode 100755
index 000000000000..052dd2c5f678
--- /dev/null
+++ b/tools/test/stress2/misc/pthread6.sh
@@ -0,0 +1,308 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# "panic: pmap active 0xfffff80128cd84b8" seen:
+# http://people.freebsd.org/~pho/stress/log/attilio101.txt
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pthread6.c
+mycc -o pthread6 -Wall -Wextra -O2 -g -gdwarf-2 pthread6.c -lpthread || exit 1
+rm -f pthread6.c /tmp/pthread6.core
+
+daemon sh -c "(cd $here/../testcases/swap; ./swap -t 2m -i 20 -k)"
+sleep `jot -r 1 1 9`
+echo "Expect SIGABRT"
+for i in `jot 50`; do
+ /tmp/pthread6
+done
+while pgrep -q swap; do
+ pkill swap
+ sleep 1
+done
+
+rm -f /tmp/pthread6 /tmp/pthread6.core
+exit 0
+EOF
+/*
+ * Threaded producer-consumer test.
+ * Loosly based on work by
+ * Andrey Zonov (c) 2012
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/queue.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#ifdef __FreeBSD__
+#include <pthread_np.h>
+#define __NP__
+#endif
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOCK(x) plock(&x.mtx)
+#define UNLOCK(x) punlock(&x.mtx)
+#define SIGNAL(x) psig(&x.wait)
+#define WAIT(x) pwait(&x.wait, &x.mtx)
+
+#define PARALLEL 4
+#define LOOPS 2
+
+long ncreate, nrename, nunlink;
+int bench, max;
+char *dirname1;
+char *dirname2;
+
+struct file {
+ char *name;
+ STAILQ_ENTRY(file) next;
+};
+
+struct files {
+ pthread_mutex_t mtx;
+ pthread_cond_t wait;
+ STAILQ_HEAD(, file) list;
+};
+
+static struct files newfiles;
+static struct files renamedfiles;
+
+static void
+hand(int i __unused) { /* handler */
+}
+
+static void
+ahand(int i __unused) { /* handler */
+ abort();
+}
+
+void
+plock(pthread_mutex_t *l)
+{
+ int rc;
+
+ if ((rc = pthread_mutex_lock(l)) != 0)
+ errc(1, rc, "pthread_mutex_lock");
+}
+
+void
+punlock(pthread_mutex_t *l)
+{
+ int rc;
+
+ if ((rc = pthread_mutex_unlock(l)) != 0)
+ errc(1, rc, "pthread_mutex_unlock");
+}
+
+void
+psig(pthread_cond_t *c)
+{
+ int rc;
+
+ if ((rc = pthread_cond_signal(c)) != 0)
+ errc(1, rc, "pthread_cond_signal");
+}
+
+void
+pwait(pthread_cond_t *c, pthread_mutex_t *l)
+{
+ int rc;
+
+ if ((rc = pthread_cond_wait(c, l)) != 0)
+ errc(1, rc, "pthread_cond_wait");
+}
+
+void *
+loop_create(void *arg __unused)
+{
+ int i, j;
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ for (i = 0; i < max; i++) {
+ file = malloc(sizeof(*file));
+ asprintf(&file->name, "%s/filename_too-long:%d", dirname1, i);
+ LOCK(newfiles);
+ STAILQ_INSERT_TAIL(&newfiles.list, file, next);
+ ncreate++;
+ UNLOCK(newfiles);
+ SIGNAL(newfiles);
+ if ((bench == 0) && (i > 0) && (i % 100000 == 0))
+ for (j = 0; j < 10 && ncreate != nrename; j++)
+ usleep(400);
+ }
+ return (NULL);
+}
+
+void *
+loop_rename(void *arg __unused)
+{
+ char *filename, *newname;
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ while (nrename < max) {
+ LOCK(newfiles);
+ while (STAILQ_EMPTY(&newfiles.list)) {
+ WAIT(newfiles);
+ }
+ file = STAILQ_FIRST(&newfiles.list);
+ STAILQ_REMOVE_HEAD(&newfiles.list, next);
+ UNLOCK(newfiles);
+ filename = strrchr(file->name, '/');
+ asprintf(&newname, "%s/%s", dirname2, filename);
+ nrename++;
+ free(file->name);
+ file->name = newname;
+ LOCK(renamedfiles);
+ STAILQ_INSERT_TAIL(&renamedfiles.list, file, next);
+ UNLOCK(renamedfiles);
+ SIGNAL(renamedfiles);
+ }
+ return (NULL);
+}
+
+void *
+loop_unlink(void *arg __unused)
+{
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ while (nunlink < max) {
+ LOCK(renamedfiles);
+ while (STAILQ_EMPTY(&renamedfiles.list)) {
+ WAIT(renamedfiles);
+ }
+ file = STAILQ_FIRST(&renamedfiles.list);
+ STAILQ_REMOVE_HEAD(&renamedfiles.list, next);
+ nunlink++;
+ UNLOCK(renamedfiles);
+ free(file->name);
+ free(file);
+ }
+ return (NULL);
+}
+
+void
+test(void)
+{
+ int i;
+ int rc;
+ pthread_t tid[3];
+ pthread_mutexattr_t attr, *pattr = NULL;
+
+ bench = getenv("bench") != NULL;
+ bench = 1;
+ asprintf(&dirname1, "%s.1", "f1");
+ asprintf(&dirname2, "%s.2", "f2");
+ max = 15000000;
+
+ STAILQ_INIT(&newfiles.list);
+ STAILQ_INIT(&renamedfiles.list);
+
+ pthread_mutexattr_init (&attr);
+ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
+ pattr = &attr;
+ if ((rc = pthread_mutex_init(&newfiles.mtx, pattr)) != 0)
+ errc(1, rc, "pthread_mutex_init()");
+ if ((rc = pthread_cond_init(&newfiles.wait, NULL)) != 0)
+ errc(1, rc, "pthread_cond_init()");
+ if ((rc = pthread_mutex_init(&renamedfiles.mtx, NULL)) != 0)
+ errc(1, rc, "pthread_mutex_init()");
+ if ((rc = pthread_cond_init(&renamedfiles.wait, NULL)) != 0)
+ errc(1, rc, "pthread_cond_init()");
+
+ signal(SIGINFO, hand);
+ signal(SIGALRM, ahand);
+ if ((rc = pthread_create(&tid[0], NULL, loop_create, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+ if ((rc = pthread_create(&tid[1], NULL, loop_rename, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+ if ((rc = pthread_create(&tid[2], NULL, loop_unlink, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+
+ usleep(1000);
+ ualarm(arc4random() % 100000, 0);
+ for (i = 0; i < 3; i++) {
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ }
+
+ if ((rc = pthread_mutex_destroy(&newfiles.mtx)) != 0)
+ errc(1, rc, "pthread_mutex_destroy(newfiles)");
+ if ((rc = pthread_cond_destroy(&newfiles.wait)) != 0)
+ errc(1, rc, "pthread_cond_destroy(newfiles)");
+ if ((rc = pthread_mutex_destroy(&renamedfiles.mtx)) != 0)
+ errc(1, rc, "pthread_mutex_destroy(renamedfiles)");
+ if ((rc = pthread_cond_destroy(&renamedfiles.wait)) != 0)
+ errc(1, rc, "pthread_cond_destroy(renamedfiles)");
+ free(dirname1);
+ free(dirname2);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i, j;
+
+ for (i = 0; i < LOOPS; i++) {
+ for (j = 0; j < PARALLEL; j++) {
+ if (fork() == 0)
+ test();
+ }
+
+ for (j = 0; j < PARALLEL; j++)
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pthread7.sh b/tools/test/stress2/misc/pthread7.sh
new file mode 100755
index 000000000000..41190190bd22
--- /dev/null
+++ b/tools/test/stress2/misc/pthread7.sh
@@ -0,0 +1,285 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# PTHREAD_PRIO_INHERIT version of pthread2.sh
+
+. ../default.cfg
+
+export LANG=C
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pthread7.c
+mycc -o pthread7 -Wall -Wextra -O2 -g pthread7.c -lpthread || exit 1
+rm -f pthread7.c
+
+for i in `jot 5`; do
+ for i in `jot 8`; do
+ /tmp/pthread7 &
+ done
+ wait
+done
+rm -f /tmp/pthread7
+exit 0
+EOF
+/*
+ * Threaded producer-consumer test.
+ * Loosly based on work by
+ * Andrey Zonov (c) 2012
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/queue.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#ifdef __FreeBSD__
+#include <pthread_np.h>
+#define __NP__
+#endif
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOCK(x) plock(&x.mtx)
+#define UNLOCK(x) punlock(&x.mtx)
+#define SIGNAL(x) psig(&x.wait)
+#define WAIT(x) pwait(&x.wait, &x.mtx)
+
+long ncreate, nrename, nunlink;
+int max;
+char *dirname1;
+char *dirname2;
+
+struct file {
+ char *name;
+ STAILQ_ENTRY(file) next;
+};
+
+struct files {
+ pthread_mutex_t mtx;
+ pthread_cond_t wait;
+ STAILQ_HEAD(, file) list;
+};
+
+static struct files newfiles;
+static struct files renamedfiles;
+
+#define MAXQ 100000 /* Max create queue length */
+#define MESSAGES 10000000;
+
+static void
+hand(int i __unused) { /* handler */
+ fprintf(stderr, "max = %d, ncreate = %ld, nrename = %ld, nunlink = %ld\n",
+ max, ncreate, nrename, nunlink);
+}
+
+static void
+ahand(int i __unused) { /* handler */
+ fprintf(stderr, "FAIL\n");
+ hand(0);
+ _exit(0);
+}
+
+void
+plock(pthread_mutex_t *l)
+{
+ int rc;
+
+ if ((rc = pthread_mutex_lock(l)) != 0)
+ errc(1, rc, "pthread_mutex_lock");
+}
+
+void
+punlock(pthread_mutex_t *l)
+{
+ int rc;
+
+ if ((rc = pthread_mutex_unlock(l)) != 0)
+ errc(1, rc, "pthread_mutex_unlock");
+}
+
+void
+psig(pthread_cond_t *c)
+{
+ int rc;
+
+ if ((rc = pthread_cond_signal(c)) != 0)
+ errc(1, rc, "pthread_cond_signal");
+}
+
+void
+pwait(pthread_cond_t *c, pthread_mutex_t *l)
+{
+ int rc;
+
+ if ((rc = pthread_cond_wait(c, l)) != 0)
+ errc(1, rc, "pthread_cond_wait");
+}
+
+void *
+loop_create(void *arg __unused)
+{
+ int i;
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ for (i = 0; i < max; i++) {
+ file = malloc(sizeof(*file));
+ asprintf(&file->name, "%s/filename_too-long:%d", dirname1, i);
+ LOCK(newfiles);
+ STAILQ_INSERT_TAIL(&newfiles.list, file, next);
+ ncreate++;
+ UNLOCK(newfiles);
+ SIGNAL(newfiles);
+ if (ncreate - nrename > MAXQ)
+ usleep(400);
+ }
+ return (NULL);
+}
+
+void *
+loop_rename(void *arg __unused)
+{
+ char *filename, *newname;
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ while (nrename < max) {
+ LOCK(newfiles);
+ while (STAILQ_EMPTY(&newfiles.list)) {
+ WAIT(newfiles);
+ }
+ file = STAILQ_FIRST(&newfiles.list);
+ STAILQ_REMOVE_HEAD(&newfiles.list, next);
+ UNLOCK(newfiles);
+ filename = strrchr(file->name, '/');
+ asprintf(&newname, "%s/%s", dirname2, filename);
+ nrename++;
+ free(file->name);
+ file->name = newname;
+ LOCK(renamedfiles);
+ STAILQ_INSERT_TAIL(&renamedfiles.list, file, next);
+ UNLOCK(renamedfiles);
+ SIGNAL(renamedfiles);
+ }
+ return (NULL);
+}
+
+void *
+loop_unlink(void *arg __unused)
+{
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ while (nunlink < max) {
+ LOCK(renamedfiles);
+ while (STAILQ_EMPTY(&renamedfiles.list)) {
+ WAIT(renamedfiles);
+ }
+ file = STAILQ_FIRST(&renamedfiles.list);
+ STAILQ_REMOVE_HEAD(&renamedfiles.list, next);
+ nunlink++;
+ UNLOCK(renamedfiles);
+ free(file->name);
+ free(file);
+ }
+ return (NULL);
+}
+
+int
+main(void)
+{
+ int i;
+ int rc;
+ pthread_t tid[3];
+ pthread_mutexattr_t attr, *pattr = NULL;
+
+ asprintf(&dirname1, "%s.1", "f1");
+ asprintf(&dirname2, "%s.2", "f2");
+ max = MESSAGES;
+
+ STAILQ_INIT(&newfiles.list);
+ STAILQ_INIT(&renamedfiles.list);
+
+ pthread_mutexattr_init (&attr);
+ if ((rc = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT)) != 0)
+ errc(1, rc, "PTHREAD_PRIO_INHERIT");
+ pattr = &attr;
+ if ((rc = pthread_mutex_init(&newfiles.mtx, pattr)) != 0)
+ errc(1, rc, "pthread_mutex_init()");
+ if ((rc = pthread_cond_init(&newfiles.wait, NULL)) != 0)
+ errc(1, rc, "pthread_cond_init()");
+ if ((rc = pthread_mutex_init(&renamedfiles.mtx, NULL)) != 0)
+ errc(1, rc, "pthread_mutex_init()");
+ if ((rc = pthread_cond_init(&renamedfiles.wait, NULL)) != 0)
+ errc(1, rc, "pthread_cond_init()");
+
+ signal(SIGINFO, hand);
+ signal(SIGALRM, ahand);
+ alarm(300);
+ if ((rc = pthread_create(&tid[0], NULL, loop_create, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+ if ((rc = pthread_create(&tid[1], NULL, loop_rename, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+ if ((rc = pthread_create(&tid[2], NULL, loop_unlink, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+
+ for (i = 0; i < 3; i++) {
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ }
+
+ if ((rc = pthread_mutex_destroy(&newfiles.mtx)) != 0)
+ errc(1, rc, "pthread_mutex_destroy(newfiles)");
+ if ((rc = pthread_cond_destroy(&newfiles.wait)) != 0)
+ errc(1, rc, "pthread_cond_destroy(newfiles)");
+ if ((rc = pthread_mutex_destroy(&renamedfiles.mtx)) != 0)
+ errc(1, rc, "pthread_mutex_destroy(renamedfiles)");
+ if ((rc = pthread_cond_destroy(&renamedfiles.wait)) != 0)
+ errc(1, rc, "pthread_cond_destroy(renamedfiles)");
+ free(dirname1);
+ free(dirname2);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pthread8.sh b/tools/test/stress2/misc/pthread8.sh
new file mode 100755
index 000000000000..d6461daa0f46
--- /dev/null
+++ b/tools/test/stress2/misc/pthread8.sh
@@ -0,0 +1,137 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# PTHREAD_PRIO_INHERIT test scenario
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pthread8.c
+mycc -o pthread8 -Wall -Wextra -O0 -g pthread8.c -lpthread || exit 1
+rm -f pthread8.c /tmp/pthread8.core
+
+/tmp/pthread8
+
+rm -f /tmp/pthread8
+exit 0
+EOF
+/* $Id: pi.c,v 1.2 2015/01/31 11:36:07 kostik Exp kostik $ */
+
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <err.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+struct runner_arg {
+ pthread_mutex_t *locks;
+ u_int lock_cnt;
+};
+
+int stop;
+
+void *
+runner(void *arg)
+{
+ struct runner_arg *ra;
+ pthread_mutex_t *l;
+ u_int i;
+ int error;
+
+ ra = arg;
+ while (stop == 0) {
+ for (i = 0; i < ra->lock_cnt; i++) {
+ l = &ra->locks[i];
+ error = pthread_mutex_lock(l);
+ if (error != 0)
+ errc(1, error, "pthread_mutex_lock");
+ pthread_yield();
+ }
+ for (i = 0; i < ra->lock_cnt; i++) {
+ l = &ra->locks[i];
+ error = pthread_mutex_unlock(l);
+ if (error != 0)
+ errc(1, error, "pthread_mutex_lock");
+ pthread_yield();
+ }
+ }
+ return (NULL);
+}
+
+int
+main(void)
+{
+ struct runner_arg ra;
+ time_t start;
+ pthread_t *threads;
+ pthread_mutexattr_t mattr;
+ u_int i, ncpus;
+ int error;
+ size_t ncpus_len;
+
+ ncpus_len = sizeof(ncpus);
+ error = sysctlbyname("hw.ncpu", &ncpus, &ncpus_len, NULL, 0);
+ if (error != 0)
+ err(1, "sysctl hw.ncpus");
+ threads = calloc(ncpus, sizeof(pthread_t));
+ if (threads == NULL)
+ err(1, "calloc threads");
+
+ ra.lock_cnt = 100;
+ ra.locks = calloc(ra.lock_cnt, sizeof(pthread_mutex_t));
+ if (ra.locks == NULL)
+ err(1, "calloc locks");
+ error = pthread_mutexattr_init(&mattr);
+ if (error != 0)
+ errc(1, error, "pthread_mutexattr_init");
+ error = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
+ if (error != 0)
+ errc(1, error, "pthread_mutexattr_setprotocol PRIO_INHERIT");
+ for (i = 0; i < ra.lock_cnt; i++) {
+ error = pthread_mutex_init(&ra.locks[i], &mattr);
+ if (error != 0)
+ errc(1, error, "pthread_mutex_init");
+ }
+
+ for (i = 0; i < ncpus; i++) {
+ error = pthread_create(&threads[i], NULL, runner, &ra);
+ if (error != 0)
+ errc(1, error, "pthread_create");
+ }
+ start = time(NULL);
+ while (time(NULL) - start < 180)
+ sleep(1);
+ stop = 1;
+ for (i = 0; i < ncpus; i++)
+ pthread_join(threads[i], NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pthread9.sh b/tools/test/stress2/misc/pthread9.sh
new file mode 100755
index 000000000000..a5aaeeab9af0
--- /dev/null
+++ b/tools/test/stress2/misc/pthread9.sh
@@ -0,0 +1,185 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Thread suspend deadlock seen:
+# https://people.freebsd.org/~pho/stress/log/pthread9.txt
+
+# Test scenario by Conrad Meyer.
+# Fixed by r283320.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pthread9.c
+mycc -o pthread9 -Wall -Wextra -O2 pthread9.c -lpthread || exit 1
+rm -f pthread9.c
+
+status=0
+if ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1; then
+ mount -t nfs -o nfsv3,tcp,nolockd,retrycnt=3,intr $nfs_export \
+ $mntpoint || exit 1
+ sleep .5
+ echo "Expect core dumps"
+ (cd $mntpoint; /tmp/pthread9) &
+ sleep 200
+ if pgrep -q pthread9; then
+ echo FAIL
+ procstat -k `pgrep pthread9 | grep -v $!`
+ status=1
+ fi
+ rm -f $mntpoint/pthread9.core
+ umount -f $mntpoint
+ wait
+fi
+
+rm -f /tmp/pthread9 /tmp/pthread9.core
+exit $status
+EOF
+#include <sys/types.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#ifdef __FreeBSD__
+#include <pthread_np.h>
+#define __NP__
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define LOOPS 50
+#define RUNTIME 180
+
+volatile u_int go;
+int fd;
+char file[] = "pthread9.file";
+
+static void *
+t1(void *data __unused)
+{
+ int i;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+ while (go == 0)
+ pthread_yield();
+
+ atomic_add_int(&go, 1);
+ for (i = 0; i < 100; i++)
+ if (ftruncate(fd, 0) == -1)
+ err(1, "truncate");
+
+ return (NULL);
+}
+
+static void *
+t2(void *data __unused)
+{
+ int i;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+ while (go == 0)
+ pthread_yield();
+
+ atomic_add_int(&go, 1);
+ for (i = 0; i < 100; i++)
+ if (ftruncate(fd, 0) == -1)
+ err(1, "truncate");
+
+ return (NULL);
+}
+
+static void *
+t3(void *data __unused)
+{
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+ while (go != 3)
+ pthread_yield();
+ abort();
+
+ return (NULL);
+}
+
+int
+test(void)
+{
+ pthread_t tid[3];
+ int i, rc;
+
+ go = 0;
+ if ((rc = pthread_create(&tid[0], NULL, t1, NULL)) != 0)
+ errc(1, rc, "pthread_create");
+ if ((rc = pthread_create(&tid[1], NULL, t2, NULL)) != 0)
+ errc(1, rc, "pthread_create");
+ if ((rc = pthread_create(&tid[2], NULL, t3, NULL)) != 0)
+ errc(1, rc, "pthread_create");
+ usleep(200);
+ atomic_add_int(&go, 1);
+
+ for (i = 0; i < 3; i++) {
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join");
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ time_t start;
+
+ if ((fd = open(file, O_RDWR | O_CREAT, 0644)) == -1)
+ err(1, "open(%s)", file);
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ if (fork() == 0)
+ test();
+ wait(NULL);
+ }
+ close(fd);
+ unlink(file);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/ptrace.sh b/tools/test/stress2/misc/ptrace.sh
new file mode 100755
index 000000000000..7e747378624e
--- /dev/null
+++ b/tools/test/stress2/misc/ptrace.sh
@@ -0,0 +1,106 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# wait4(2) / ptrace(2) regression test.
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > ptrace.c
+mycc -o ptrace -Wall -Wextra -g ptrace.c || exit 1
+rm -f ptrace.c
+cd $here
+
+/tmp/ptrace
+
+rm -f /tmp/ptrace
+exit
+
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <strings.h>
+#include <sys/ptrace.h>
+#include <sys/wait.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+
+void
+test(void)
+{
+ pid_t pid, rpid;
+ struct rusage ru;
+ int status;
+
+ if ((pid = fork()) == 0) {
+ usleep(2000);
+ _exit(64);
+ }
+ if (pid == -1)
+ err(1, "fork()");
+ if (ptrace(PT_ATTACH, pid, NULL, 0) == -1)
+ err(1, "ptrace(%d) attach", pid);
+ if (wait(NULL) == -1)
+ err(1, "wait");
+ bzero(&ru, sizeof(ru));
+ usleep(2000);
+ if ((rpid = wait4(-1, &status, WNOHANG, &ru)) == -1) {
+ if (errno == ECHILD)
+ warn("FAIL");
+ else
+ err(1, "wait4");
+ }
+ if (rpid == 0) {
+// fprintf(stderr, "No rusage info.\n");
+ if (ptrace(PT_DETACH, pid, NULL, 0) == -1)
+ err(1, "ptrace(%d) detach", pid);
+ if (wait(&status) == -1)
+ err(1, "wait");
+ } else {
+ fprintf(stderr, "FAIL Got unexpected rusage.\n");
+ if (ru.ru_utime.tv_sec != 0)
+ fprintf(stderr, "FAIL tv_sec\n");
+ }
+ if (status != 0x4000)
+ fprintf(stderr, "FAIL Child exit status 0x%x\n", status);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ test();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/ptrace10.sh b/tools/test/stress2/misc/ptrace10.sh
new file mode 100755
index 000000000000..c2dec736a240
--- /dev/null
+++ b/tools/test/stress2/misc/ptrace10.sh
@@ -0,0 +1,161 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Mark Johnston <markj@FreeBSD.org>
+#
+# 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 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.
+#
+
+# ptrace(2) test scenario by Mark Johnston
+# https://people.freebsd.org/~markj/ptrace_stop_mt.c
+# Fixed by r303423.
+
+# stopped on signal 17 after detach
+# UID PID PPID CPU PRI NI VSZ RSS MWCHAN STAT TT TIME COMMAND
+# 1001 47125 62778 0 52 0 6568 2456 wait S+ 2 0:00.01 /bin/sh ./ptrace10.sh
+# 1001 47146 47125 0 23 0 6108 1928 nanslp S+ 2 0:00.00 ./ptrace10
+# 1001 47148 47146 0 24 0 6240 1932 - T+ 2 0:00.00 ./ptrace10
+# 1001 47148 47146 0 24 0 6240 1932 - T+ 2 0:00.00 ./ptrace10
+
+. ../default.cfg
+
+cd /tmp
+cat > ptrace10.c <<EOF
+#include <sys/types.h>
+#include <sys/ptrace.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static void
+sighup(int sig __unused)
+{
+}
+
+static void
+sleep_forever(void)
+{
+
+ while (1)
+ sleep(1);
+}
+
+static void *
+thread(void *arg __unused)
+{
+
+ sleep_forever();
+ return (NULL);
+}
+
+int
+main(void)
+{
+ struct sigaction act;
+ sigset_t set;
+ pthread_t t;
+ pid_t pid, ret;
+ int e, try, limit, r, status;
+
+ e = 0;
+ pid = fork();
+ if (pid < 0)
+ err(1, "fork");
+ if (pid == 0) {
+ act.sa_handler = sighup;
+ act.sa_flags = 0;
+ sigemptyset(&act.sa_mask);
+ if (sigaction(SIGHUP, &act, NULL) != 0)
+ err(1, "sigaction");
+
+ r = pthread_create(&t, NULL, thread, NULL);
+ if (r != 0)
+ errc(1, r, "pthread_create");
+
+ /* Force SIGHUP to be delivered to the new thread. */
+ sigemptyset(&set);
+ sigaddset(&set, SIGHUP);
+ r = pthread_sigmask(SIG_BLOCK, &set, NULL);
+ if (r != 0)
+ errc(1, r, "pthread_sigmask");
+
+ sleep_forever();
+ } else {
+ sleep(1); /* give the child a chance to set itself up */
+
+ limit = 100;
+ for (try = 1; try <= limit; try++) {
+ if (kill(pid, SIGHUP) != 0)
+ err(1, "kill(SIGHUP)");
+ if (ptrace(PT_ATTACH, pid, NULL, 0) != 0)
+ err(1, "ptrace(PT_ATTACH)");
+ if (waitpid(pid, &status, WUNTRACED) != pid)
+ err(1, "waitpid 1");
+ if (!WIFSTOPPED(status))
+ errx(1, "unexpected status %d after PT_ATTACH",
+ status);
+ if (ptrace(PT_DETACH, pid, NULL, 0) != 0)
+ err(1, "ptrace(PT_DETACH)");
+
+ sleep(1);
+ ret = waitpid(pid, &status, WUNTRACED | WNOHANG);
+ if (ret < 0)
+ err(1, "waitpid");
+ if (ret == 0)
+ continue;
+ if (!WIFSTOPPED(status))
+ errx(1, "unexpected status %d after PT_DETACH",
+ status);
+ printf("stopped on signal %d after detach\n",
+ WSTOPSIG(status));
+ e = 1;
+ break;
+ }
+ }
+ kill(pid, SIGINT);
+
+ return (e);
+}
+EOF
+
+mycc -o ptrace10 -Wall -Wextra -O2 -g ptrace10.c -lpthread || exit 1
+rm ptrace10.c
+
+./ptrace10
+s=$?
+if [ $s -ne 0 ]; then
+ ps -lxH | grep -v grep | egrep "UID|ptrace10"
+ while pgrep -q ptrace10; do
+ pkill -9 ptrace10
+ done
+fi
+wait
+
+rm -f ptrace10
+exit $s
diff --git a/tools/test/stress2/misc/ptrace11.sh b/tools/test/stress2/misc/ptrace11.sh
new file mode 100755
index 000000000000..9fbd44481624
--- /dev/null
+++ b/tools/test/stress2/misc/ptrace11.sh
@@ -0,0 +1,124 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# markj@ wrote:
+# I found a kernel bug that caused init to consume 100% CPU if the
+# following steps occurred:
+# - process A forks and creates process B
+# - process C ptrace attaches to process B
+# - process A exits
+# - process C detaches from process B
+# - process B exits
+#
+# Process B gets reparented to init, and the bug causes wait() to return
+# an error each time init attempts to reap process B.
+
+. ../default.cfg
+cd /tmp
+cat > ptrace11.c <<EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/ptrace.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+#define SYNC 0
+
+int
+main(void)
+{
+ pid_t pida, pidb;
+ size_t len;
+ int status;
+
+ setproctitle("A");
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ pida = fork();
+ if (pida < 0)
+ err(1, "fork");
+ if (pida == 0) {
+ setproctitle("B");
+ while (share[SYNC] != 2)
+ usleep(10);
+ _exit(0);
+ } else {
+ sleep(1);
+ pidb = fork();
+ if (pidb < 0)
+ err(1, "fork");
+ if (pidb == 0) {
+ setproctitle("C");
+ if (ptrace(PT_ATTACH, pida, NULL, 0) != 0)
+ err(1, "ptrace(PT_ATTACH)");
+ wait4(pida, &status, 0, NULL);
+ share[SYNC] = 1; /* A to exit */
+ usleep(1000);
+ if (ptrace(PT_DETACH, pida, NULL, 0) != 0)
+ err(1, "ptrace(PT_DETACH)");
+ share[SYNC] = 2; /* B to exit */
+ _exit(0);
+ } else {
+ while (share[SYNC] != 1)
+ usleep(10);
+ _exit(0);
+ }
+ }
+
+ return (0);
+}
+EOF
+
+mycc -o ptrace11 -Wall -Wextra -O2 -g ptrace11.c || exit 1
+rm ptrace11.c
+old=`ps auxwwl | grep -v grep | grep "<defunct>" | wc -l`
+
+./ptrace11
+
+new=`ps auxwwl | grep -v grep | grep "<defunct>" | wc -l`
+if [ $old -ne $new ]; then
+ ps auxwwl | sed -n '1p;/<defunct>/p'
+ echo FAIL
+ s=1
+fi
+
+rm -f ptrace11
+exit $s
diff --git a/tools/test/stress2/misc/ptrace2.sh b/tools/test/stress2/misc/ptrace2.sh
new file mode 100755
index 000000000000..7e689f981fc8
--- /dev/null
+++ b/tools/test/stress2/misc/ptrace2.sh
@@ -0,0 +1,143 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Regression test for kern/142757, race condition in traced process signal
+# handling. Fixed in r202692.
+
+# Test scenario by Tijl Coosemans, tijl@
+
+. ../default.cfg
+
+cd /tmp
+
+cat > race1.c <<EOF
+#include <sys/types.h>
+#include <sys/ptrace.h>
+#include <sys/wait.h>
+#include <signal.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ pid_t pid;
+ int i, status;
+
+ alarm(120);
+ /* fork dummy child process */
+ pid = fork();
+ if (pid == 0) {
+ /* child does nothing */
+ for (;;) {
+ sleep(1);
+ }
+ } else {
+ /* parent */
+ sleep(1);
+ for (i = 0; i < 100000; i++) {
+ /* loop: attach, wait, detach */
+ printf("attach ");
+ fflush(stdout);
+ ptrace(PT_ATTACH, pid, (caddr_t) 0, 0);
+
+ printf("wait ");
+ fflush(stdout);
+ wait4(pid, &status, 0, NULL);
+
+ printf("detach ");
+ fflush(stdout);
+ ptrace(PT_DETACH, pid, (caddr_t) 1, 0);
+
+ printf("ok\n");
+ fflush(stdout);
+ }
+ }
+ kill(pid, SIGINT);
+
+ return (0);
+}
+EOF
+
+cat > race2.c <<EOF
+#include <sys/types.h>
+#include <sys/ptrace.h>
+#include <sys/wait.h>
+#include <signal.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ pid_t pid;
+ int i, status;
+
+ alarm(120);
+ /* fork dummy child process */
+ pid = fork();
+ if (pid == 0) {
+ /* child does nothing */
+ for (;;) {
+ sleep(1);
+ }
+ } else {
+ /* parent */
+ sleep(1);
+ ptrace(PT_ATTACH, pid, (caddr_t) 0, 0);
+ wait4(pid, &status, 0, NULL);
+ for (i = 0; i < 100000; i++) {
+ /* loop: continue, kill, wait */
+ printf("continue ");
+ fflush(stdout);
+ ptrace(PT_CONTINUE, pid, (caddr_t) 1, 0);
+
+ printf("kill ");
+ fflush(stdout);
+ kill(pid, SIGINT);
+
+ printf("wait ");
+ fflush(stdout);
+ wait4(pid, &status, 0, NULL);
+
+ printf("ok\n");
+ fflush(stdout);
+ }
+ }
+
+ return (0);
+}
+EOF
+
+mycc -o race1 -Wall -Wextra race1.c
+mycc -o race2 -Wall -Wextra race2.c
+
+./race1 > /dev/null || echo "FAIL #1"
+./race2 > /dev/null || echo "FAIL #2"
+
+rm -f race1.c race1 race2.c race2
diff --git a/tools/test/stress2/misc/ptrace3.sh b/tools/test/stress2/misc/ptrace3.sh
new file mode 100755
index 000000000000..9b8cd4c39670
--- /dev/null
+++ b/tools/test/stress2/misc/ptrace3.sh
@@ -0,0 +1,130 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# A regression test for a bug introduced in r269656.
+# Page fault in proc_realparent+0x70 seen.
+# Fixed in r270024.
+
+# Test scenario by Mark Johnston markj@
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > ptrace3.c
+mycc -o ptrace3 -Wall -Wextra -g ptrace3.c || exit 1
+rm -f ptrace3.c
+cd $here
+
+/tmp/ptrace3
+
+rm -f /tmp/ptrace3
+exit
+EOF
+#include <sys/types.h>
+#include <sys/ptrace.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <signal.h>
+#include <unistd.h>
+
+/*
+ * A regression test for a bug introduced in r269656. The test process p creates
+ * child processes c1 and c2 which do nothing but sleep; a third child process
+ * (c3) uses ptrace(2) to reparent c1 and c2 from p to c3. Then c3 detaches from
+ * c1 and c2, causing a crash when c1 and c2 are removed from p's now-corrupt
+ * orphan list.
+ */
+
+pid_t
+sleeper()
+{
+ pid_t p;
+
+ p = fork();
+ if (p == -1)
+ err(1, "fork");
+ else if (p == 0)
+ while (1)
+ sleep(1000000);
+ return (p);
+}
+
+void
+attach(pid_t p)
+{
+ int status;
+
+ if (ptrace(PT_ATTACH, p, 0, 0) == -1)
+ err(1, "ptrace");
+
+ if (waitpid(p, &status, 0) == -1)
+ err(1, "waitpid");
+ else if (!WIFSTOPPED(status))
+ errx(1, "failed to stop child");
+}
+
+void
+detach(pid_t p)
+{
+
+ if (ptrace(PT_DETACH, p, 0, 0) == -1)
+ err(1, "ptrace");
+}
+
+int
+main(void)
+{
+ int status;
+ pid_t c1, c2, p;
+
+ c1 = sleeper();
+ c2 = sleeper();
+
+ p = fork();
+ if (p == -1) {
+ err(1, "fork");
+ } else if (p == 0) {
+ attach(c1);
+ attach(c2);
+ detach(c1);
+ detach(c2);
+ } else {
+ if (waitpid(p, &status, 0) == -1)
+ err(1, "waitpid");
+ if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
+ errx(1, "child exited abnormally");
+ }
+
+ /* Clean up. */
+ (void)kill(c1, SIGKILL);
+ (void)kill(c2, SIGKILL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/ptrace4.sh b/tools/test/stress2/misc/ptrace4.sh
new file mode 100755
index 000000000000..88d9893c165a
--- /dev/null
+++ b/tools/test/stress2/misc/ptrace4.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# ptrace(2) test scenario
+
+# Test program can not be killed:
+# https://people.freebsd.org/~pho/stress/log/kostik836.txt
+# Fixed by r289660.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+export sleeptime=24
+./syscall4.sh 26 &
+
+sleep 240
+l=0
+while ps -Unobody | grep -v grep | grep -q syscall4; do
+ l=$((l + 1))
+ if [ $l -gt 10 ]; then
+ ps -HlUnobody | grep syscall4
+ echo FAIL
+ break
+ fi
+ sleep 10
+done
+kill $! 2>/dev/null
+pkill -9 swap syscall4
+while pgrep -q swap; do
+ pkill -9 swap
+done
+wait
diff --git a/tools/test/stress2/misc/ptrace5.sh b/tools/test/stress2/misc/ptrace5.sh
new file mode 100755
index 000000000000..5ff80cd68ca9
--- /dev/null
+++ b/tools/test/stress2/misc/ptrace5.sh
@@ -0,0 +1,200 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# It seems to be possible for ptrace(PT_ATTACH) to race with the delivery
+# of a signal to the same process.
+
+# "panic: Assertion TD_IS_SLEEPING(td) failed at subr_sleepqueue.c:958".
+# https://people.freebsd.org/~pho/stress/log/ptrace5.txt
+# https://people.freebsd.org/~pho/stress/log/ptrace5-2.txt
+# Fixed by r303426.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/ptrace5.c
+mycc -o ptrace5 -Wall -Wextra -O0 -g ptrace5.c -pthread || exit 1
+rm -f ptrace5.c
+cd $odir
+
+/tmp/ptrace5
+s=$?
+
+rm -rf /tmp/ptrace5
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/ptrace.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static pid_t pid;
+static pthread_barrier_t barr;
+static int cont;
+
+#define PARALLEL 4
+#define RUNTIME (5 * 60)
+
+static void
+ahandler(int i __unused)
+{
+ system("ps -Hl");
+ fprintf(stderr, "SIGALRM pid %d\n", pid);
+ exit(1);
+}
+
+static void
+handler(int i __unused)
+{
+}
+
+static void *
+t1(void *data __unused)
+{
+ int status;
+
+ while (cont == 1) {
+ if (ptrace(PT_ATTACH, pid, 0, 0) == -1)
+ err(1, "ptrace(%d)", pid);
+
+ if (waitpid(pid, &status, 0) == -1)
+ err(1, "waitpid");
+ else if (!WIFSTOPPED(status))
+ errx(1, "failed to stop child");
+ if (ptrace(PT_DETACH, pid, 0, 0) == -1)
+ err(1, "ptrace");
+ usleep(arc4random() % 200);
+ }
+
+ return (NULL);
+}
+
+static void *
+t2(void *data __unused)
+{
+ while (cont == 1) {
+ if (kill(pid, SIGHUP) == -1)
+ err(1, "kill");
+ usleep(arc4random() % 200);
+ }
+
+ return (NULL);
+}
+
+static void
+test(void)
+{
+ pthread_t tid[2];
+ struct sigaction sa;
+ int r, status;
+
+ r = pthread_barrier_wait(&barr);
+ if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
+ errc(1, r, "pthread_barrier_wait");
+
+ sa.sa_handler = ahandler;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction(SIGALRM, &sa, NULL) == -1)
+ err(1, "sigaction");
+ alarm(2 * RUNTIME);
+ sa.sa_handler = handler;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction(SIGHUP, &sa, NULL) == -1)
+ err(1, "sigaction");
+
+ if ((pid = fork()) == 0) {
+ for(;;)
+ usleep(20);
+
+ _exit(0);
+ }
+
+ cont = 1;
+ if ((r = pthread_create(&tid[0], NULL, t1, NULL)) == -1)
+ errc(1, r, "pthread_create");
+ if ((r = pthread_create(&tid[1], NULL, t2, NULL)) == -1)
+ errc(1, r, "pthread_create");
+
+ sleep(RUNTIME);
+
+ cont = 0;
+ if ((r = pthread_join(tid[0], NULL)) == -1)
+ errc(1, r, "pthread_join");
+ if ((r = pthread_join(tid[1], NULL)) == -1)
+ errc(1, r, "pthread_join");
+ if (kill(pid, SIGKILL) != 0)
+ err(1, "kill(%d)", pid);
+ waitpid(pid, &status, 0);
+
+ exit(0);
+}
+
+int
+main(void)
+{
+ pthread_barrierattr_t attr;
+ int e, i, pids[PARALLEL], r, status;
+
+ if ((r = pthread_barrierattr_init(&attr)) != 0)
+ errc(1, r, "pthread_barrierattr_init");
+ if ((r = pthread_barrierattr_setpshared(&attr,
+ PTHREAD_PROCESS_SHARED)) != 0)
+ errc(1, r, "pthread_barrierattr_setpshared");
+ if ((r = pthread_barrier_init(&barr, &attr, PARALLEL)) != 0)
+ errc(1, r, "pthread_barrier_init");
+
+ e = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ waitpid(pids[i], &status, 0);
+ e += status == 0 ? 0 : 1;
+ }
+
+ if ((r = pthread_barrier_destroy(&barr)) > 0)
+ errc(1, r, "pthread_barrier_destroy");
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/ptrace6.sh b/tools/test/stress2/misc/ptrace6.sh
new file mode 100755
index 000000000000..872c1c0d202c
--- /dev/null
+++ b/tools/test/stress2/misc/ptrace6.sh
@@ -0,0 +1,204 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# panic: Assertion TD_IS_SLEEPING(td) failed at subr_sleepqueue.c:958.
+# Fixed by r303426.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/ptrace6.c
+mycc -o ptrace6 -Wall -Wextra -O0 -g ptrace6.c -pthread || exit 1
+rm -f ptrace6.c
+cd $odir
+
+/tmp/ptrace6
+s=$?
+
+while pgrep -q swap; do
+ pkill -9 swap
+done
+rm -rf /tmp/ptrace6
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/ptrace.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static pid_t pid;
+static pthread_barrier_t barr;
+static int cont;
+static int state;
+
+#define PARALLEL 4
+#define RUNTIME (4 * 60)
+
+static void
+ahandler(int i __unused)
+{
+ system("ps -Hl");
+ fprintf(stderr, "SIGALRM state %d, pid %d\n", state, pid);
+ exit(1);
+}
+
+static void
+handler(int i __unused)
+{
+}
+
+static void *
+t1(void *data __unused)
+{
+ int status;
+
+ while (cont == 1) {
+ state = 1;
+ if (ptrace(PT_ATTACH, pid, 0, 0) == -1)
+ err(1, "ptrace(%d)", pid);
+
+ state = 2;
+ if (waitpid(pid, &status, 0) == -1)
+ err(1, "waitpid");
+ else if (!WIFSTOPPED(status))
+ errx(1, "failed to stop child");
+ state = 3;
+ if (ptrace(PT_DETACH, pid, 0, 0) == -1)
+ err(1, "ptrace");
+ state = 4;
+ usleep(arc4random() % 200);
+ }
+
+ return (NULL);
+}
+
+static void *
+t2(void *data __unused)
+{
+ while (cont == 1) {
+ if (kill(pid, SIGHUP) == -1)
+ err(1, "kill");
+ usleep(arc4random() % 200);
+ }
+
+ return (NULL);
+}
+
+static void
+test(void)
+{
+ pthread_t tid[2];
+ struct sigaction sa;
+ int r;
+
+ r = pthread_barrier_wait(&barr);
+ if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
+ errc(1, r, "pthread_barrier_wait");
+
+ sa.sa_handler = ahandler;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction(SIGALRM, &sa, NULL) == -1)
+ err(1, "sigaction");
+ alarm(RUNTIME + 60);
+
+ sa.sa_handler = handler;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction(SIGHUP, &sa, NULL) == -1)
+ err(1, "sigaction");
+
+ if ((pid = fork()) == 0) {
+ for(;;)
+ usleep(arc4random() % 1000);
+
+ _exit(0);
+ }
+
+ cont = 1;
+ if ((r = pthread_create(&tid[0], NULL, t1, NULL)) == -1)
+ errc(1, r, "pthread_create");
+ if ((r = pthread_create(&tid[1], NULL, t2, NULL)) == -1)
+ errc(1, r, "pthread_create");
+
+ sleep(RUNTIME);
+
+ cont = 0;
+ if ((r = pthread_join(tid[0], NULL)) == -1)
+ errc(1, r, "pthread_join");
+ if ((r = pthread_join(tid[1], NULL)) == -1)
+ errc(1, r, "pthread_join");
+ if (kill(pid, SIGKILL) != 0)
+ err(1, "kill(%d)", pid);
+ waitpid(pid, NULL, 0);
+
+ exit(0);
+}
+
+int
+main(void)
+{
+ pthread_barrierattr_t attr;
+ int e, i, pids[PARALLEL], r, status;
+
+ if ((r = pthread_barrierattr_init(&attr)) != 0)
+ errc(1, r, "pthread_barrierattr_init");
+ if ((r = pthread_barrierattr_setpshared(&attr,
+ PTHREAD_PROCESS_SHARED)) != 0)
+ errc(1, r, "pthread_barrierattr_setpshared");
+ if ((r = pthread_barrier_init(&barr, &attr, PARALLEL)) != 0)
+ errc(1, r, "pthread_barrier_init");
+
+ e = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ waitpid(pids[i], &status, 0);
+ e += status == 0 ? 0 : 1;
+ }
+
+ if ((r = pthread_barrier_destroy(&barr)) > 0)
+ errc(1, r, "pthread_barrier_destroy");
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/ptrace7.sh b/tools/test/stress2/misc/ptrace7.sh
new file mode 100755
index 000000000000..c6c810b60d01
--- /dev/null
+++ b/tools/test/stress2/misc/ptrace7.sh
@@ -0,0 +1,238 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# It seems to be possible for ptrace(PT_ATTACH) to race with the delivery
+# of a signal to the same process.
+
+# $ while ./pt.sh; do date; done
+# 15. juli 2016 kl. 05.59.05 CEST
+# 15. juli 2016 kl. 06.03.07 CEST
+# UID PID PPID CPU PRI NI VSZ RSS MWCHAN STAT TT TIME COMMAND
+# 1001 863 862 0 44 0 13880 5268 wait Is 0 0:00,13 -bash (bash)
+# 1001 21053 863 0 52 0 13188 3088 wait I+ 0 0:00,01 /bin/sh ./pt.sh
+# 1001 21096 21053 0 52 0 10544 2308 wait I+ 0 0:00,00 /tmp/pt
+# 1001 21103 21096 0 20 0 12852 2456 wait S+ 0 0:00,00 pt: main (pt)
+# 1001 21103 21096 0 35 0 12852 2456 wait I+ 0 0:55,30 pt: main (pt)
+# 1001 21104 21096 0 72 0 0 0 - Z+ 0 0:00,00 <defunct>
+# 1001 21105 21096 0 72 0 0 0 - Z+ 0 0:00,00 <defunct>
+# 1001 21116 21103 0 103 0 10544 2336 - RX+ 0 4:22,41 pt: spinner (pt)
+# 1001 37711 21103 0 20 0 21200 2960 - R+ 0 0:00,00 ps -Hl
+# 1001 890 879 0 20 0 22184 6196 select Ss+ 1 1:21,86 top -s 1
+# 1001 85222 85221 0 21 0 13880 5276 ttyin Is+ 2 0:00,23 -bash (bash)
+# SIGALRM state 2, pid 21116
+# $
+
+# Fixed by r303423.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/ptrace7.c
+mycc -o ptrace7 -Wall -Wextra -O0 -g ptrace7.c -pthread || exit 1
+rm -f ptrace7.c
+cd $odir
+
+/tmp/ptrace7
+s=$?
+
+while pgrep -q swap; do
+ pkill -9 swap
+done
+rm -rf /tmp/ptrace7
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/ptrace.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#ifdef __FreeBSD__
+#include <pthread_np.h>
+#define __NP__
+#endif
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static pid_t pid;
+static pthread_barrier_t barr;
+static int cont;
+static int state;
+
+#define PARALLEL 8
+#define RUNTIME (4 * 60)
+
+static void
+ahandler(int i __unused)
+{
+ system("ps -Hl");
+ fprintf(stderr, "SIGALRM state %d, pid %d\n", state, pid);
+ exit(1);
+}
+
+static void
+handler(int i __unused)
+{
+}
+
+static void *
+t1(void *data __unused)
+{
+ int status;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+ while (cont == 1) {
+ state = 1;
+ if (ptrace(PT_ATTACH, pid, 0, 0) == -1)
+ err(1, "ptrace(%d)", pid);
+
+ state = 2;
+ if (waitpid(pid, &status, 0) == -1)
+ err(1, "waitpid");
+ else if (!WIFSTOPPED(status))
+ errx(1, "failed to stop child");
+ state = 3;
+ if (ptrace(PT_DETACH, pid, 0, 0) == -1)
+ err(1, "ptrace");
+ state = 4;
+ usleep(arc4random() % 100 + 50);
+ }
+
+ return (NULL);
+}
+
+static void *
+t2(void *data __unused)
+{
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+ while (cont == 1) {
+ if (kill(pid, SIGHUP) == -1)
+ err(1, "kill");
+ usleep(arc4random() % 100 + 50);
+ }
+
+ return (NULL);
+}
+
+static void
+test(void)
+{
+ pthread_t tid[2];
+ struct sigaction sa;
+ int r, status;
+
+ r = pthread_barrier_wait(&barr);
+ if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
+ errc(1, r, "pthread_barrier_wait");
+
+ sa.sa_handler = ahandler;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction(SIGALRM, &sa, NULL) == -1)
+ err(1, "sigaction");
+ alarm(RUNTIME + 60);
+
+ sa.sa_handler = handler;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction(SIGHUP, &sa, NULL) == -1)
+ err(1, "sigaction");
+
+ setproctitle("%s", "spinner");
+ if ((pid = fork()) == 0) {
+ for(;;)
+ getuid();
+
+ _exit(0);
+ }
+
+ cont = 1;
+ if ((r = pthread_create(&tid[0], NULL, t1, NULL)) == -1)
+ errc(1, r, "pthread_create");
+ if ((r = pthread_create(&tid[1], NULL, t2, NULL)) == -1)
+ errc(1, r, "pthread_create");
+
+ setproctitle("%s", "main");
+ sleep(RUNTIME);
+
+ cont = 0;
+ if ((r = pthread_join(tid[0], NULL)) == -1)
+ errc(1, r, "pthread_join");
+ if ((r = pthread_join(tid[1], NULL)) == -1)
+ errc(1, r, "pthread_join");
+ if (kill(pid, SIGKILL) != 0)
+ err(1, "kill(%d)", pid);
+ if (waitpid(pid, &status, 0) == -1)
+ err(1, "waitpid(%d)", pid);
+
+ exit(0);
+}
+
+int
+main(void)
+{
+ pthread_barrierattr_t attr;
+ int e, i, pids[PARALLEL], r, status;
+
+ if ((r = pthread_barrierattr_init(&attr)) != 0)
+ errc(1, r, "pthread_barrierattr_init");
+ if ((r = pthread_barrierattr_setpshared(&attr,
+ PTHREAD_PROCESS_SHARED)) != 0)
+ errc(1, r, "pthread_barrierattr_setpshared");
+ if ((r = pthread_barrier_init(&barr, &attr, PARALLEL)) != 0)
+ errc(1, r, "pthread_barrier_init");
+
+ e = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid%d)", pids[i]);
+ e += status == 0 ? 0 : 1;
+ }
+
+ if ((r = pthread_barrier_destroy(&barr)) > 0)
+ errc(1, r, "pthread_barrier_destroy");
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/ptrace8.sh b/tools/test/stress2/misc/ptrace8.sh
new file mode 100755
index 000000000000..2021c47905b4
--- /dev/null
+++ b/tools/test/stress2/misc/ptrace8.sh
@@ -0,0 +1,122 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# A regression test for r302919.
+# Triggered a witness message:
+#
+# vmspace_free() called with the following non-sleepable locks held:
+# shared rw vm object (vm object) r = 0 locked @ kern/sys_process.c:432
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/ptrace8.c
+mycc -o ptrace8 -Wall -Wextra -O0 -g ptrace8.c || exit 1
+rm -f ptrace8.c
+cd $odir
+
+/tmp/ptrace8
+s=$?
+
+rm -rf /tmp/ptrace8
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/ptrace.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+volatile u_int *share;
+
+#define SYNC 0
+
+int
+main(void)
+{
+ struct ptrace_vm_entry ent;
+ size_t len;
+ int pid, r, status;
+ char path[MAXPATHLEN + 1];
+
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ if ((pid = fork()) == 0) {
+ while (share[SYNC] == 0)
+ sleep(1);
+
+ _exit(0);
+ }
+
+ if (ptrace(PT_ATTACH, pid, 0, 0) == -1)
+ err(1, "ptrace");
+
+ if (waitpid(pid, &status, 0) == -1)
+ err(1, "waitpid");
+ else if (!WIFSTOPPED(status))
+ errx(1, "failed to stop child");
+
+ ent.pve_entry = 0;
+ ent.pve_path = path;
+ ent.pve_pathlen = sizeof(path);
+ do {
+ r = ptrace(PT_VM_ENTRY, pid, (caddr_t)&ent, 0);
+#if defined(DEBUG)
+ if (r == 0)
+ fprintf(stderr, "path = %s 0x%lx - 0x%lx\n",
+ ent.pve_path, ent.pve_start, ent.pve_end);
+#endif
+ } while (r == 0);
+ if (r == -1 && errno != ENOENT)
+ err(1, "ptrace(PT_VM_ENTRY)");
+
+ share[SYNC] = 1;
+ if (ptrace(PT_DETACH, pid, 0, 0) == -1)
+ err(1, "ptrace");
+
+ if (waitpid(pid, &status, 0) == -1)
+ err(1, "waitpid(%d)", pid);
+
+ return (status != 0);
+}
diff --git a/tools/test/stress2/misc/ptrace9.sh b/tools/test/stress2/misc/ptrace9.sh
new file mode 100755
index 000000000000..ac1329fd1a2a
--- /dev/null
+++ b/tools/test/stress2/misc/ptrace9.sh
@@ -0,0 +1,133 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2016 Mark Johnston <markj@FreeBSD.org>
+#
+# 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 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.
+#
+
+# ptrace(2) test scenario by Mark Johnston
+# https://people.freebsd.org/~markj/ptrace_stop.c
+# Fixed by r303423.
+
+. ../default.cfg
+
+cd /tmp
+cat > ptrace9.c <<EOF
+#include <sys/types.h>
+#include <sys/ptrace.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <signal.h>
+#include <stdio.h>
+#include <unistd.h>
+
+static void
+sigalrm(int sig __unused)
+{
+ _exit(0);
+}
+
+static void
+sighup(int sig __unused)
+{
+}
+
+int
+main(void)
+{
+ struct sigaction act;
+ pid_t pid;
+ int e, status;
+
+ signal(SIGALRM, sigalrm);
+ e = 1;
+ pid = fork();
+ if (pid < 0)
+ err(1, "fork");
+ if (pid == 0) {
+ act.sa_handler = sighup;
+ act.sa_flags = 0;
+ sigemptyset(&act.sa_mask);
+ if (sigaction(SIGHUP, &act, NULL) != 0)
+ err(1, "sigaction");
+ alarm(5);
+ while (1) {
+ sleep(1);
+ }
+ } else {
+ alarm(5);
+ sleep(1); /* give the child a chance to call sigaction */
+
+ if (kill(pid, SIGSTOP) != 0)
+ err(1, "kill(SIGSTOP)");
+
+ printf("waiting for child to stop...\n");
+ if (waitpid(pid, &status, WUNTRACED) != pid)
+ err(1, "waitpid");
+ if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
+ errx(1, "unexpected status %d after SIGSTOP", status);
+
+ if (kill(pid, SIGHUP) != 0)
+ err(1, "kill(SIGHUP)");
+
+ if (ptrace(PT_ATTACH, pid, NULL, 0) != 0)
+ err(1, "ptrace(PT_ATTACH)");
+ if (waitpid(pid, &status, WUNTRACED) != pid)
+ err(1, "waitpid");
+ if (!WIFSTOPPED(status))
+ errx(1, "unexpected status %d after PT_ATTACH", status);
+ printf("stopping signal is %d\n", WSTOPSIG(status));
+ if (ptrace(PT_DETACH, pid, NULL, 0) != 0)
+ err(1, "ptrace(PT_DETACH)");
+
+ /* if ptrace works as expected, we'll block here */
+ printf("waiting on child...\n"); fflush(stdout);
+ if (waitpid(pid, &status, WUNTRACED) != pid)
+ err(1, "waitpid");
+ if (!WIFSTOPPED(status))
+ errx(1, "unexpected status %d after PT_DETACH", status);
+ printf("child is stopped after detach (sig %d)\n",
+ WSTOPSIG(status)); fflush(stdout);
+ e = 1;
+ }
+
+ return (e);
+}
+EOF
+
+mycc -o ptrace9 -Wall -Wextra -O2 -g ptrace9.c || exit 1
+rm ptrace9.c
+
+echo "Expect:
+ waiting for child to stop...
+ stopping signal is 17
+ waiting on child..."
+./ptrace9
+s=$?
+
+pkill -9 ptrace9
+rm -f ptrace9
+exit $s
diff --git a/tools/test/stress2/misc/pts.sh b/tools/test/stress2/misc/pts.sh
new file mode 100755
index 000000000000..f994c1f89bc6
--- /dev/null
+++ b/tools/test/stress2/misc/pts.sh
@@ -0,0 +1,154 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Page fault in ttydev_open+0x2d seen. Fixed in r237219.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pts.c
+mycc -o pts -Wall -Wextra -O2 pts.c -lutil || exit 1
+rm -f pts.c
+
+/tmp/pts &
+pid=$!
+
+while kill -0 $! 2>/dev/null; do
+ $here/../testcases/swap/swap -t 2m -i 20 > /dev/null
+done
+wait $pid
+status=$?
+
+rm -f /tmp/pts
+exit $status
+EOF
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/param.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <libutil.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <termios.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOOPS 10
+#define RUNTIME 60
+
+void
+churn(char *path)
+{
+ FTS *fts;
+ FTSENT *p;
+ time_t start;
+ int fd, ftsoptions;
+ char *args[2];
+
+ ftsoptions = FTS_PHYSICAL;
+ args[0] = path;
+ args[1] = 0;
+
+ setproctitle("churn");
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ while ((p = fts_read(fts)) != NULL) {
+ if (p->fts_info == FTS_D ||
+ p->fts_info == FTS_DP)
+ continue;
+ if ((fd = open(p->fts_path, O_RDONLY)) > 0)
+ close(fd);
+
+ }
+
+ if (errno != 0 && errno != ENOENT)
+ err(1, "fts_read");
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+ }
+
+ _exit(0);
+}
+
+void
+pty(void)
+{
+ time_t start;
+ int master, slave;
+ char slname[1025];
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ if (openpty(&master, &slave, slname, NULL, NULL) == -1)
+ err(1, "openpty");
+ usleep(arc4random() % 10000);
+ if (close(master) == -1)
+ err(1, "close(master)");
+ if (close(slave) == -1)
+ err(1, "close(%s)", slname);
+ }
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i, j, s, status;
+
+ status = 0;
+ for (j = 0; j < LOOPS && status == 0; j++) {
+ for (i = 0; i < 2; i++) {
+ if (fork() == 0)
+ pty();
+ }
+ for (i = 0; i < 2; i++) {
+ if (fork() == 0)
+ churn("/dev/pts");
+ }
+ for (i = 0; i < 4; i++) {
+ wait(&s);
+ if (s != 0)
+ status = 1;
+ }
+ }
+
+ return (status);
+}
diff --git a/tools/test/stress2/misc/pts2.sh b/tools/test/stress2/misc/pts2.sh
new file mode 100755
index 000000000000..84f7bbd78255
--- /dev/null
+++ b/tools/test/stress2/misc/pts2.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# pts leak seen.
+# Fixed in r313496.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+kldstat -v | grep -q pty || { kldload pty || exit 0; }
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pts2.c
+mycc -o pts2 -Wall -Wextra -O2 pts2.c || exit 1
+rm -f pts2.c
+
+pts=`vmstat -m | grep pts | awk '{print $2}'`
+for i in `jot 10`; do
+ /tmp/pts2
+done
+new=`vmstat -m | grep pts | awk '{print $2}'`
+s=0
+[ $((new - pts)) -gt 1 ] && { s=1; echo "Leaked $((new - pts)) pts."; }
+
+rm -f /tmp/pts2
+exit $s
+EOF
+#include <sys/ioctl.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+const char *master = "/dev/ptmx";
+int
+main(void)
+{
+ int fd, fd2, slave;
+ char sl[80];
+
+ if ((fd = open(master, O_RDONLY)) == -1)
+ err(1, "open(%s)", master);
+
+ if (ioctl(fd, TIOCGPTN, &slave) == -1)
+ err(1, "ioctl");
+
+ snprintf(sl, sizeof(sl), "/dev/pts/%d", slave);
+
+ if ((fd2 = open(sl, O_RDONLY | O_EXLOCK)) == -1)
+ err(1, "open(%s)", sl);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pts3.sh b/tools/test/stress2/misc/pts3.sh
new file mode 100755
index 000000000000..b398e92916af
--- /dev/null
+++ b/tools/test/stress2/misc/pts3.sh
@@ -0,0 +1,102 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# "panic: Most recently used by tty" seen.
+# Reported by syzbot+c9b6206303bf47bac87e@syzkaller.appspotmail.com
+# Fixed by r349733
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+kldstat -v | grep -q pty || { kldload pty || exit 0; }
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pts3.c
+mycc -o pts3 -Wall -Wextra -O2 pts3.c || exit 1
+rm -f pts3.c
+
+/tmp/pts3; s=$?
+
+rm -f /tmp/pts3
+exit $s
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+#define SYNC 0
+
+int
+main(void)
+{
+ pid_t p, pid;
+ size_t len;
+ time_t start;
+ int fd;
+ char path[128];
+
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ p = getpid();
+ sprintf(path, "/dev/ptmx");
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ share[SYNC] = 0;
+ if ((fd = open(path, O_RDWR)) == -1)
+ err(1,"open()");
+ if ((pid = fork()) == 0) {
+ while (share[SYNC] == 0)
+ ;
+ _exit(0);
+ }
+ share[SYNC] = 1;
+ if (fcntl(fd, F_SETOWN, p) == -1)
+ warn("fcntl()");
+ close(fd);
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid()");
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pty.sh b/tools/test/stress2/misc/pty.sh
new file mode 100755
index 000000000000..a2b8ecbcc574
--- /dev/null
+++ b/tools/test/stress2/misc/pty.sh
@@ -0,0 +1,161 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# pty(4) test scenario.
+
+# "panic: make_dev_credv: bad si_name (error=17, si_name=ptysn)" seen.
+# https://people.freebsd.org/~pho/stress/log/pty.txt
+
+# /dev/pty[l-sL-S][0-9a-v] Pseudo-terminal master devices.
+# /dev/tty[l-sL-S][0-9a-v] Pseudo-terminal slave devices.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+kldstat -v | grep -q pty || { kldload pty || exit 0; }
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pty.c
+mycc -o pty -Wall -Wextra -O2 pty.c || exit 1
+rm -f pty.c
+
+su $testuser -c /tmp/pty
+
+rm -f /tmp/pty
+exit
+EOF
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <unistd.h>
+
+#define PARALLEL 4
+#define RUNTIME 300
+
+char path[80];
+
+void
+churn(char *path)
+{
+
+ FTS *fts;
+ FTSENT *p;
+ time_t start;
+ int fd, ftsoptions;
+ char *args[2];
+
+ ftsoptions = FTS_PHYSICAL;
+ args[0] = path;
+ args[1] = 0;
+
+ setproctitle("churn");
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ while ((p = fts_read(fts)) != NULL) {
+ if (p->fts_info == FTS_D ||
+ p->fts_info == FTS_DP)
+ continue;
+ if ((fd = open(p->fts_path, O_RDWR)) == -1)
+ if ((fd = open(p->fts_path, O_WRONLY)) == -1)
+ if ((fd = open(p->fts_path, O_RDONLY)) == -1)
+ continue;
+ usleep(arc4random() % 1000);
+ close(fd);
+
+ }
+
+ if (errno != 0 && errno != ENOENT)
+ err(1, "fts_read");
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+ }
+
+ _exit(0);
+}
+
+void
+pty(void)
+{
+ int fd[512], i, j, n;
+ char c1, c2;
+
+ n = 0;
+ c1 = 'l';
+ for (i = 0; i < 16; i++) {
+ c2 = '0';
+ for (j = 0; j < 32; j++) {
+ snprintf(path, sizeof(path), "/dev/pty%c%c", c1, c2);
+ fd[n++] = open(path, O_RDWR);
+ if (c2 == '9')
+ c2 = 'a';
+ else
+ c2++;
+ }
+ if (c1 == 's')
+ c1 = 'L';
+ else
+ c1++;
+ }
+
+ for (i = 0; i < n; i++)
+ if (fd[i] != -1)
+ close(fd[i]);
+}
+
+int
+main(void)
+{
+ int i;
+
+ time_t start;
+
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ churn("/dev");
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME)
+ pty();
+
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/pty2.sh b/tools/test/stress2/misc/pty2.sh
new file mode 100755
index 000000000000..b7cf346c994f
--- /dev/null
+++ b/tools/test/stress2/misc/pty2.sh
@@ -0,0 +1,101 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# pty(4) test scenario.
+
+# "panic: make_dev_sv: bad si_name (error=17, si_name=ptyp0)" seen.
+# Fixed by r293825.
+
+# Based on test scenario by Bruce Evans.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+kldstat -v | grep -q pty || { kldload pty || exit 0; }
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pty2.c
+mycc -o pty2 -Wall -Wextra -O2 pty2.c || exit 1
+rm -f pty2.c
+
+/tmp/pty2
+
+rm -f /tmp/pty2
+exit
+EOF
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define PARALLEL 3
+#define RUNTIME 180
+
+#define IN "/dev/ptyp0"
+
+void
+test(void)
+{
+ time_t start;
+ int fd;
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ if ((fd = open(IN, O_RDONLY)) == -1) {
+ if (errno != EBUSY && errno != ENXIO && errno != ENOENT)
+ err(1, "open(%s)", IN);
+ } else
+ close(fd);
+
+ }
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i;
+
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ test();
+
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+
+ return(0);
+}
+
diff --git a/tools/test/stress2/misc/quota1.sh b/tools/test/stress2/misc/quota1.sh
new file mode 100755
index 000000000000..38e0fcd9c351
--- /dev/null
+++ b/tools/test/stress2/misc/quota1.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Causes this: panic: mutex Giant not owned at ../../../kern/vfs_subr.c:1968
+# with a kernel compiled with "options QUOTA".
+# This is not really a quota test.
+
+. ../default.cfg
+
+D=$diskimage
+trap "rm -f $D" 0
+dd if=/dev/zero of=$D bs=1m count=128 status=none || exit 1
+
+mount | grep "$mntpoint" | grep md${mdstart}$part > /dev/null &&
+ umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=10m # Run tests for 10 minutes
+(cd ..; ./run.sh disk.cfg)
+while mount | grep -q $mntpoint; do
+ umount $mntpoint > /dev/null 2>&1 || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f $D
+exit 0
diff --git a/tools/test/stress2/misc/quota10.sh b/tools/test/stress2/misc/quota10.sh
new file mode 100755
index 000000000000..1452f68538cf
--- /dev/null
+++ b/tools/test/stress2/misc/quota10.sh
@@ -0,0 +1,105 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008, 2011 Peter Holm <pho@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 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.
+#
+
+# Hunt for deadlock that could occur running umount and quota at the same time
+# "panic: dqsync: file" seen:
+# https://people.freebsd.org/~pho/stress/log/quota10.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ "`sysctl -in kern.features.ufs_quota`" != "1" ] && exit 0
+
+. ../default.cfg
+
+mounts=15 # Number of parallel scripts
+mdstart=$mdstart # Use md unit numbers from this point
+D=$diskimage
+export PATH_FSTAB=/tmp/fstab
+
+if [ $# -eq 0 ]; then
+ rm -f $PATH_FSTAB
+ for i in `jot $mounts`; do
+ m=$((i + mdstart - 1))
+ [ ! -d ${mntpoint}$m ] && mkdir ${mntpoint}$m
+ mount | grep "$mntpoint" | grep -q md$m &&
+ umount ${mntpoint}$m
+ [ -c /dev/md$m ] && mdconfig -d -u $m
+
+ dd if=/dev/zero of=$D$m bs=1m count=1 status=none
+ mdconfig -a -t vnode -f $D$m -u $m
+ bsdlabel -w md$m auto
+ newfs md${m}$part > /dev/null 2>&1
+ echo "/dev/md${m}$part ${mntpoint}$m ufs rw,userquota 2 2" \
+ >> $PATH_FSTAB
+ mount ${mntpoint}$m
+ edquota -u -f ${mntpoint}$m -e \
+ ${mntpoint}$m:100000:110000:15000:16000 root
+ umount ${mntpoint}$m
+ done
+ sync;sync;sync
+
+ # start the parallel tests
+ touch /tmp/$0
+ for i in `jot $mounts`; do
+ m=$((i + mdstart - 1))
+ ./$0 $m &
+ ./$0 find $m &
+ done
+ wait
+
+ for i in `jot $mounts`; do
+ m=$((i + mdstart - 1))
+ mdconfig -d -u $m
+ rm -f $D$m
+ done
+ rm -f $PATH_FSTAB
+else
+ if [ $1 = find ]; then
+ while [ -r /tmp/$0 ]; do
+ (
+ quotaon ${mntpoint}$2
+ quotaoff ${mntpoint}$2
+ ) 2>&1 | egrep -v "No such file or directory"
+ done
+ else
+
+ # The test: Parallel mount and unmounts
+ start=`date '+%s'`
+ while [ $((`date '+%s'` - start)) -lt 1200 ]; do
+ m=$1
+ opt=`[ $(( m % 2 )) -eq 0 ] && echo -f`
+ mount $opt /dev/md${m}$part ${mntpoint}$m
+ while mount | grep -qw $mntpoint$m; do
+ opt=$([ $((`date '+%s'` % 2)) -eq 0 ] &&
+ echo "-f")
+ umount $opt ${mntpoint}$m > /dev/null 2>&1
+ done
+ done
+ rm -f /tmp/$0
+ fi
+fi
+exit 0
diff --git a/tools/test/stress2/misc/quota11.sh b/tools/test/stress2/misc/quota11.sh
new file mode 100755
index 000000000000..bf08f9e15db4
--- /dev/null
+++ b/tools/test/stress2/misc/quota11.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# "panic: share->excl" seen.
+# http://people.freebsd.org/~pho/stress/log/kostik715.txt
+# Fixed in r270795 and r270797
+
+# Test scenario by Hiroki Sato <hrs FreeBSD org>
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ "`sysctl -in kern.features.ufs_quota`" != "1" ] && exit 0
+
+. ../default.cfg
+
+mount | grep "on $mntpoint " | grep -q md${mdstart}$part && umount $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+export PATH_FSTAB=/tmp/fstab
+trap "rm -f $PATH_FSTAB" 0
+echo "/dev/md${mdstart}$part $mntpoint ufs rw,userquota 2 2" > $PATH_FSTAB
+mount $mntpoint
+edquota -u -f $mntpoint -e $mntpoint:1000:2000:100:200 root
+quotaon $mntpoint
+
+while true; do repquota -av > /dev/null; done &
+
+dd if=/dev/random of=$mntpoint/foo.data bs=512 count=1024x1024 2>&1 |
+ egrep -v "trans|record"
+kill $!
+wait
+
+n=0
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ n=$((n + 1))
+ [ $n -gt 60 ] && exit 1
+done
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/quota12.sh b/tools/test/stress2/misc/quota12.sh
new file mode 100755
index 000000000000..78a5e0c0cd81
--- /dev/null
+++ b/tools/test/stress2/misc/quota12.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Run marcus.cfg on a quota enabled FS
+
+# "Inability to allocate buffers" seen with WiP kernel code:
+# https://people.freebsd.org/~pho/stress/log/kostik1218.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ "`sysctl -in kern.features.ufs_quota`" != "1" ] && exit 0
+
+. ../default.cfg
+
+mount | grep "on $mntpoint " | grep -q md${mdstart}$part && umount $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+export PATH_FSTAB=/tmp/fstab
+trap "rm -f $PATH_FSTAB" 0
+echo "/dev/md${mdstart}$part $mntpoint ufs rw,userquota 2 2" > $PATH_FSTAB
+mount $mntpoint
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export KBLOCKS=$1
+export INODES=$2
+s=0
+
+export QK=$((KBLOCKS / 1))
+export QI=$((INODES / 1))
+edquota -u -f $mntpoint -e \
+ ${mntpoint}:$((QK - 50)):$QK:$((QI - 50 )):$QI $testuser > \
+ /dev/null 2>&1
+quotaon $mntpoint
+
+chmod 777 $mntpoint
+export runRUNTIME=15m
+export RUNDIR=$mntpoint/stressX
+su $testuser -c 'cd ..; ./run.sh marcus.cfg > /dev/null 2>&1'
+
+while ! quotaoff $mntpoint; do
+ sync
+ sleep 1
+done
+quotacheck $mntpoint
+
+n=0
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ n=$((n + 1))
+ [ $n -gt 60 ] && exit 1
+done
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/quota2.sh b/tools/test/stress2/misc/quota2.sh
new file mode 100755
index 000000000000..9deb97726d60
--- /dev/null
+++ b/tools/test/stress2/misc/quota2.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ "`sysctl -in kern.features.ufs_quota`" != "1" ] && exit 0
+
+. ../default.cfg
+
+D=$diskimage
+export PATH_FSTAB=/tmp/fstab
+trap "rm -f $D $PATH_FSTAB" 0
+dd if=/dev/zero of=$D bs=1m count=128 status=none || exit 1
+
+mount | grep "$mntpoint" | grep md${mdstart}$part > /dev/null && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+echo "/dev/md${mdstart}$part $mntpoint ufs rw,userquota 2 2" > $PATH_FSTAB
+mount $mntpoint
+edquota -u -f $mntpoint -e $mntpoint:100000:110000:15000:16000 root
+quotacheck $mntpoint
+quotaon $mntpoint
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=10m # Run tests for 10 minutes
+(cd ..; ./run.sh disk.cfg) 2>/dev/null
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/quota3.sh b/tools/test/stress2/misc/quota3.sh
new file mode 100755
index 000000000000..de4c60ec2810
--- /dev/null
+++ b/tools/test/stress2/misc/quota3.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ "`sysctl -in kern.features.ufs_quota`" != "1" ] && exit 0
+
+. ../default.cfg
+
+D=$diskimage
+export PATH_FSTAB=/tmp/fstab
+trap "rm -f $D $PATH_FSTAB" 0
+dd if=/dev/zero of=$D bs=1m count=1k status=none || exit 1
+
+mount | grep "$mntpoint" | grep -q md${mdstart}$part && umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+echo "/dev/md${mdstart}$part $mntpoint ufs rw,userquota 2 2" > $PATH_FSTAB
+mount $mntpoint
+edquota -u -f $mntpoint -e $mntpoint:850000:900000:130000:140000 root
+quotacheck $mntpoint
+quotaon $mntpoint
+mksnap_ffs $mntpoint $mntpoint/.snap/stress2
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=10m # Run tests for 10 minutes
+(cd ..; ./run.sh disk.cfg)
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/quota4.sh b/tools/test/stress2/misc/quota4.sh
new file mode 100755
index 000000000000..3eeae263cac2
--- /dev/null
+++ b/tools/test/stress2/misc/quota4.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ "`sysctl -in kern.features.ufs_quota`" != "1" ] && exit 0
+
+# Has shown a deadlock after 7 hours of testing
+# https://people.freebsd.org/~pho/stress/log/quota4.txt
+
+. ../default.cfg
+
+D=$diskimage
+trap "rm -f $D" 0
+dd if=/dev/zero of=$D bs=1m count=1k status=none || exit 1
+
+mount | grep "$mntpoint" | grep md${mdstart}$part > /dev/null && umount \
+ $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+echo "/dev/md${mdstart}$part $mntpoint ufs rw,userquota 2 2" >> \
+ /etc/fstab
+mount $mntpoint
+edquota -u -f $mntpoint -e ${mntpoint}:850000:900000:130000:140000 root \
+ > /dev/null 2>&1
+quotaon $mntpoint
+sed -i -e "/md${mdstart}$part/d" /etc/fstab # clean up before any panics
+export RUNDIR=$mntpoint/stressX
+../testcases/rw/rw -t 2m -i 200 -h -n 2>/dev/null &
+sleep 60
+false
+while mount | grep -q $mntpoint; do
+ umount $([ $((`date '+%s'` % 2)) -eq 0 ] && echo "-f" || echo "") \
+ $mntpoint > /dev/null 2>&1
+done
+mdconfig -d -u $mdstart
+rm -f $D
+exit 0
diff --git a/tools/test/stress2/misc/quota5.sh b/tools/test/stress2/misc/quota5.sh
new file mode 100755
index 000000000000..050ea4a822d9
--- /dev/null
+++ b/tools/test/stress2/misc/quota5.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ "`sysctl -in kern.features.ufs_quota`" != "1" ] && exit 0
+
+mount | grep -q "on /tmp (ufs," || exit 0
+if ! grep /tmp /etc/fstab | grep -q quota ; then
+ echo "Note: /tmp must have quota enabled for this test."
+ exit 0
+fi
+edquota -u -f /tmp -e /tmp:1500000:1400000:200000:180000 $testuser
+edquota -g -f /tmp -e /tmp:1500000:1400000:200000:180000 $testuser
+quotaon /tmp
+
+su $testuser -c "export runRUNTIME=60m; cd ../testcases/mkdir; \
+ ./mkdir -t 30m -i 200 -v -v"
+
+quotaoff /tmp
diff --git a/tools/test/stress2/misc/quota6.sh b/tools/test/stress2/misc/quota6.sh
new file mode 100755
index 000000000000..9cb3f5187f65
--- /dev/null
+++ b/tools/test/stress2/misc/quota6.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ "`sysctl -in kern.features.ufs_quota`" != "1" ] && exit 0
+
+. ../default.cfg
+
+# Deadlock in umount(1) while out of disk space
+
+D=$diskimage
+truncate -s 250M $D
+
+mount | grep $mntpoint | grep -q md${mdstart}$part && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+export PATH_FSTAB=/tmp/fstab
+echo "/dev/md${mdstart}$part $mntpoint ufs rw,userquota 2 2" > \
+ $PATH_FSTAB
+mount $mntpoint
+edquota -u -f $mntpoint -e $mntpoint:850000:900000:130000:140000 root > \
+ /dev/null 2>&1
+quotaon $mntpoint
+export RUNDIR=$mntpoint/stressX
+timeout 12m ../testcases/rw/rw -t 10m -i 200 -h -n &
+pid=$!
+for i in `jot 5`; do
+ echo "`date '+%T'` mksnap_ffs $mntpoint $mntpoint/.snap/snap$i"
+ mksnap_ffs $mntpoint $mntpoint/.snap/snap$i
+done
+for i in `jot 5`; do
+ rm -f $mntpoint/.snap/snap1
+done
+kill $pid
+wait
+while mount | grep -q $mntpoint; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f $D $PATH_FSTAB
+exit 0
diff --git a/tools/test/stress2/misc/quota7.sh b/tools/test/stress2/misc/quota7.sh
new file mode 100755
index 000000000000..f098fceb0939
--- /dev/null
+++ b/tools/test/stress2/misc/quota7.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Quota / snapshot test scenario by Kris@
+# Causes spin in ffs_sync or panic in panic: vfs_allocate_syncvnode: insmntque failed
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ "`sysctl -in kern.features.ufs_quota`" != "1" ] && exit 0
+
+. ../default.cfg
+
+D=$diskimage
+trap "rm -f $D" 0
+dd if=/dev/zero of=$D bs=1m count=1k status=none
+
+mount | grep $mntpoint | grep -q md$mdstart && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+export PATH_FSTAB=/tmp/fstab
+echo "/dev/md${mdstart}$part $mntpoint ufs rw,userquota 2 2" > $PATH_FSTAB
+mount $mntpoint
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export KBLOCKS=$(($1 / 21))
+export INODES=$(($2 / 21))
+export HOG=1
+export INCARNATIONS=40
+
+export QK=$((KBLOCKS / 2))
+export QI=$((INODES / 2))
+edquota -u -f $mntpoint -e $mntpoint:$((QK - 50)):$QK:$((QI - 50 )):$QI $testuser
+quotaon $mntpoint
+export RUNDIR=$mntpoint/stressX
+mkdir $mntpoint/stressX
+chmod 777 $mntpoint/stressX
+rm -rf /tmp/stressX.control/*
+su $testuser -c "(cd ..;runRUNTIME=20m ./run.sh disk.cfg)"& # panic: vfs_allocate_syncvnode: insmntque failed
+for i in `jot 20`; do
+ echo "`date '+%T'` mksnap_ffs $mntpoint $mntpoint/.snap/snap$i"
+ mksnap_ffs $mntpoint $mntpoint/.snap/snap$i
+ sleep 1
+done
+i=$(($(date '+%S') % 20 + 1))
+echo "rm -f $mntpoint/.snap/snap$i"
+rm -f $mntpoint/.snap/snap$i
+wait
+
+while mount | grep -q $mntpoint; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f $D $PATH_FSTAB
diff --git a/tools/test/stress2/misc/quota8.sh b/tools/test/stress2/misc/quota8.sh
new file mode 100755
index 000000000000..a11418aba5d9
--- /dev/null
+++ b/tools/test/stress2/misc/quota8.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Quota / snapshot test scenario by Kris@
+# Causes spin in ffs_sync or panic in panic: vfs_allocate_syncvnode:
+# insmntque failed
+
+# "Fatal double fault" seen when compiling selected files
+# with "-O0" on i386:
+# https://people.freebsd.org/~pho/stress/log/quota8.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ "`sysctl -in kern.features.ufs_quota`" != "1" ] && exit 0
+
+. ../default.cfg
+
+D=$diskimage
+trap "rm -f $D" 0
+dd if=/dev/zero of=$D bs=1m count=1k status=none || exit 1
+
+mount | grep "$mntpoint" | grep -q md$mdstart && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+echo "/dev/md${mdstart}$part $mntpoint ufs rw,userquota 2 2" >> \
+ /etc/fstab
+mount $mntpoint
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export KBLOCKS=$(($1 / 21))
+export INODES=$(($2 / 21))
+export HOG=1
+export INCARNATIONS=40
+
+export QK=$((KBLOCKS / 2))
+export QI=$((INODES / 2))
+edquota -u -f $mntpoint -e ${mntpoint}:$((QK - 50)):$QK:$((QI - 50 )):$QI \
+$testuser
+quotaon $mntpoint
+sed -i -e "/md${mdstart}$part/d" /etc/fstab
+export RUNDIR=$mntpoint/stressX
+mkdir $mntpoint/stressX
+chmod 777 $mntpoint/stressX
+su $testuser -c 'sh -c "(cd ..;runRUNTIME=20m ./run.sh disk.cfg > \
+ /dev/null 2>&1)"&'
+for i in `jot 20`; do
+ echo "`date '+%T'` mksnap_ffs $mntpoint $mntpoint/.snap/snap$i"
+ mksnap_ffs $mntpoint $mntpoint/.snap/snap$i
+ sleep 1
+done
+# Remove random snapshot file
+i=$((`date +%S` % 20 + 1))
+echo "rm -f $mntpoint/.snap/snap$i"
+rm -f $mntpoint/.snap/snap$i
+wait
+
+su $testuser -c 'sh -c "../tools/killall.sh"'
+while mount | grep -q $mntpoint; do
+ umount $([ $((`date '+%s'` % 2)) -eq 0 ] && echo "-f" || echo "") \
+ $mntpoint > /dev/null 2>&1
+done
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/quota9.sh b/tools/test/stress2/misc/quota9.sh
new file mode 100755
index 000000000000..01681bce8a01
--- /dev/null
+++ b/tools/test/stress2/misc/quota9.sh
@@ -0,0 +1,93 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm
+# 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 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.
+#
+
+# Test if quotacheck reports actual usage.
+
+. ../default.cfg
+
+export tmp=/tmp/$(basename $0).$$
+export D=$diskimage
+
+qc() {
+ local s
+ umount $1
+ s=0
+ quotacheck -v $1 > $tmp 2>&1
+ grep -q fixed $tmp && { cat $tmp; s=1; }
+ mount $1
+ return $s
+}
+
+trap "rm -f $D $tmp" 0
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ "`sysctl -in kern.features.ufs_quota`" != "1" ] && exit 0
+
+dd if=/dev/zero of=$D bs=1m count=50 status=none || exit 1
+
+mount | grep "$mntpoint" | grep -q md$mdstart &&
+ umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+export PATH_FSTAB=/tmp/fstab
+echo "/dev/md${mdstart}$part $mntpoint ufs rw,userquota 2 2" \
+ > $PATH_FSTAB
+mount $mntpoint
+mkdir $mntpoint/stressX
+chown $testuser $mntpoint/stressX
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export KBLOCKS=$1
+export INODES=$2
+
+export QK=$((KBLOCKS / 2))
+export QI=$((INODES / 2))
+edquota -u -f $mntpoint -e \
+ ${mntpoint}:$((QK - 50)):$QK:$((QI - 50 )):$QI $testuser > \
+ /dev/null 2>&1
+quotaon $mntpoint
+
+qc $mntpoint
+
+su $testuser -c '
+ for i in `jot 20`; do
+ dd if=/dev/zero of=$mntpoint/stressX/d$i bs=1m count=1 \
+ status=none
+ done
+ '
+
+qc $mntpoint; s=$?
+
+while mount | grep -q $mntpoint; do
+ umount $([ $((`date '+%s'` % 2)) -eq 0 ] &&
+ echo "-f" || echo "") $mntpoint > /dev/null 2>&1
+done
+mdconfig -d -u $mdstart
+rm -f $D $PATH_FSTAB
+exit $s
diff --git a/tools/test/stress2/misc/r335171.sh b/tools/test/stress2/misc/r335171.sh
new file mode 100755
index 000000000000..2d2e9ad72fee
--- /dev/null
+++ b/tools/test/stress2/misc/r335171.sh
@@ -0,0 +1,158 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Handle the race between fork/vm_object_split() and faults.
+#
+# If fault started before vmspace_fork() locked the map, and then during
+# fork, vm_map_copy_entry()->vm_object_split() is executed, it is
+# possible that the fault instantiate the page into the original object
+# when the page was already copied into the new object (see
+# vm_map_split() for the orig/new objects terminology). This can happen
+# if split found a busy page (e.g. from the fault) and slept dropping
+# the objects lock, which allows the swap pager to instantiate
+# read-behind pages for the fault. Then the restart of the scan can see
+# a page in the scanned range, where it was already copied to the upper
+# object.
+
+# No problems seen.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/template.c
+mycc -o template -Wall -Wextra -O0 -g template.c || exit 1
+rm -f template.c
+export MAXSWAPPCT=101
+(cd $odir/../testcases/swap; ./swap -t 5m -i 30 -l 100 -h) > /dev/null &
+$dir/template > /dev/null
+s=$?
+[ -f template.core -a $s -eq 0 ] &&
+ { ls -l template.core; mv template.core $dir; s=1; }
+wait
+
+rm -rf $dir/template
+exit $s
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+static volatile char *cp;
+
+#define PARALLEL 16
+#define RUNTIME (5 * 60)
+#define SYNC 0
+
+static void
+test(void)
+{
+ size_t len;
+ pid_t pid;
+ int i;
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ usleep(200);
+ len = 1280 * PAGE_SIZE;
+ if ((cp = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_PRIVATE, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ for (i = 0; i < (int)(len / sizeof(char)); i += PAGE_SIZE)
+ cp[i] = 1;
+ usleep(arc4random() % 500000);
+ if ((pid = fork()) == 0) {
+ usleep(arc4random() % 1000);
+ for (i = 0; i < (int)(len); i += PAGE_SIZE)
+ cp[i] = 2;
+ fprintf(stdout, ".\n"); fflush(stdout);
+ _exit(0);
+ }
+ if (pid == -1)
+ err(1, "fork()");
+ for (i = 0; i < (int)(len / sizeof(char)); i += PAGE_SIZE)
+ cp[i] = 3;
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/racct.sh b/tools/test/stress2/misc/racct.sh
new file mode 100755
index 000000000000..8249d7acf388
--- /dev/null
+++ b/tools/test/stress2/misc/racct.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# Regression test inspired by Andriy Gapon for Bug 222027 - panic on
+# non-zero RACCT destroy.
+
+# "panic: destroying non-empty racct ..." seen.
+# https://people.freebsd.org/~pho/stress/log/racct.txt
+# "Page fault in slab_free_item()" seen:
+# https://people.freebsd.org/~pho/stress/log/racct-2.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ "`sysctl -in kern.racct.enable`" != "1" ] &&
+ echo "Note: kern.racct.enable is disabled"
+pgrep -Sq accounting || { service accounting onestart && started=1; }
+(cd ../testcases/swap; ./swap -t 2m -i 5 -v -l 100) > /dev/null &
+sleep .5
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 120 ]; do
+ pids=""
+ for i in `jot 5`; do
+ for i in `jot 500`; do
+ exec su -c xuser -m root -c ':' &
+ done &
+ pids="$pids $!"
+ done
+ wait $pids
+ while [ `pgrep su | wc -l` -gt 100 ]; do sleep 1; done
+done
+wait
+[ $started ] && service accounting onestop
+exit 0
diff --git a/tools/test/stress2/misc/radix.sh b/tools/test/stress2/misc/radix.sh
new file mode 100755
index 000000000000..4bca761c08b9
--- /dev/null
+++ b/tools/test/stress2/misc/radix.sh
@@ -0,0 +1,271 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Consume VM radix nodes
+
+# "panic: default pager with handle" seen with WiP kernel code.
+# https://people.freebsd.org/~pho/stress/log/kostik1243.txt
+
+[ `sysctl vm.swap_total | sed 's/.* //'` -eq 0 ] && exit 0
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/radix.c
+mycc -o radix -Wall -Wextra radix.c || exit 1
+rm -f radix.c
+cd $odir
+
+set -e
+parallel=1
+usermem=`sysctl hw.usermem | sed 's/.* //'`
+pagesize=`pagesize`
+start=`date +%s`
+while true; do
+ /tmp/radix $parallel > /tmp/radix.log 2>&1
+ used=`awk '{print $4}' < /tmp/radix.log`
+ [ -z "$used" ] && break
+ [ $((`date +%s` - start)) -gt 300 ] && break
+ [ $used -gt $((usermem / pagesize)) ] && break
+ [ $parallel -eq 1 ] &&
+ parallel=$((usermem / pagesize / used))
+ parallel=$((parallel + 1))
+ echo "`date +%T` parallel=$parallel" # XXX
+done
+cat /tmp/radix.log
+
+rm -f /tmp/radix #/tmp/radix.log
+exit
+
+EOF
+/*
+ On Wed, 17 Apr 2013 18:57:00 -0500 alc wrote:
+
+ Suppose that I write a program for i386 that creates giant VM objects,
+ perhaps, using shm_open() + ftruncate(), and touches pages 0, 1, 8, 9,
+ 64, 65, 72, 73, 512, 513, 520, 521, 576, 577, 584, 585, 4096, 4097,
+ 4104, 4105, ... in each of the VM objects. (The sequence would be
+ different on amd64.) I could work around the 32-bit address space
+ limitation by mmap(2)ing and munmap(2)ing windows onto a VM object.
+ Each of the VM objects would have only one less interior node in the
+ radix tree than pages. If I create enough of these VM objects, then I
+ can consume all of the available pages and an almost equal number of
+ interior nodes. (Maybe it's worth writing this program so that some
+ experiments could be done?)
+*/
+
+#include <sys/param.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#ifdef __LP64__
+#define WIDTH 4
+#else
+#define WIDTH 3
+#endif
+#define N (int)howmany(sizeof(uint64_t) * NBBY, WIDTH)
+
+typedef uint64_t state_t[N];
+
+static uint64_t pgs;
+static int fds[2];
+static int parallel;
+static volatile sig_atomic_t s1;
+static int ps;
+
+static void
+init(state_t state)
+{
+ int i;
+
+ for (i = 0; i < N; i++)
+ state[i] = 0;
+}
+
+static uint64_t
+generator(state_t state)
+{
+ uint64_t value;
+ int i;
+
+ value = 0;
+ for (i = 0; i < N; i++)
+ value += state[i] << (i * WIDTH);
+ for (i = 0; i < N; i++)
+ if (state[i] == 0)
+ break;
+ if (i < N)
+ state[i]++;
+ for (i--; i >= 0; i--)
+ state[i]--;
+ return (value);
+}
+
+static int
+wr(int fd, off_t pno)
+{
+ off_t len, offset;
+ void *p;
+
+ offset = pno * ps;
+ len = ps;
+ p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_NOSYNC,
+ fd, offset);
+ if (p == MAP_FAILED) {
+ if (errno == ENOMEM)
+ return (1);
+ err(1, "mmap(len 0x%jx, offset 0x%jx). %s:%d", len, offset,
+ __FILE__, __LINE__);
+ }
+ *(char *)p = 1;
+ pgs++;
+
+ return (0);
+}
+
+static void
+handler(int s __unused)
+{
+ s1++;
+}
+
+static void
+ihandler(int s __unused)
+{
+ _exit(0);
+}
+
+static int
+radix(void)
+{
+ FILE *f;
+ int r;
+
+ if ((f = popen("vmstat -z | grep RADIX | awk -F',' '{print $3}'", "r")) == NULL)
+ err(1, "popen");
+ fscanf(f, "%d", &r);
+ pclose(f);
+
+ return (r);
+}
+
+static void
+test(void)
+{
+ state_t state;
+ off_t offset;
+ int fd;
+
+ signal(SIGHUP, ihandler);
+ for (;;) {
+ if (access("rendezvous", R_OK) == 0)
+ break;
+ usleep(2000);
+ }
+
+ if ((fd = open("/dev/zero", O_RDWR)) == -1)
+ err(1, "open()");
+
+ init(state);
+ offset = generator(state);
+ do {
+ if (wr(fd, offset) != 0)
+ break;
+ offset = generator(state);
+ } while (offset != 0);
+
+ if (write(fds[1], &pgs, sizeof(pgs)) != sizeof(pgs))
+ err(1, "ewrite pipe");
+ kill(getppid(), SIGHUP);
+ for (;;)
+ sleep(1);
+ close(fd);
+
+ _exit(0);
+}
+
+int
+main(int argc, char **argv)
+{
+ uint64_t pages;
+ pid_t *pids;
+ int i, r1, r2, rfd;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <number of parallel processes>.", argv[0]);
+ parallel = atoi(argv[1]);
+
+ ps = getpagesize();
+ signal(SIGHUP, handler);
+ unlink("rendezvous");
+ pids = malloc(parallel * sizeof(pid_t));
+ if (pipe(fds) == -1)
+ err(1, "pipe");
+ r1 = radix();
+ for (i = 0; i < parallel; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ if ((rfd = open("rendezvous", O_CREAT, 0644)) == -1)
+ err(1, "open()");
+ close(rfd);
+ alarm(60);
+ while (s1 != parallel)
+ usleep(10000);
+ alarm(0);
+ r2 = radix();
+ pages = 0;
+ for (i = 0; i < parallel; i++) {
+ kill(pids[i], SIGHUP);
+ if (read(fds[0], &pgs, sizeof(pgs)) != sizeof(pgs))
+ err(1, "read pipe");
+ pages += pgs;
+ }
+ fprintf(stderr, "A total of %jd pages (%.1f MB) touched, %d"
+ " RADIX nodes used, p/r = %.1f, parallel = %d.\n",
+ pages, pages * ps / 1024. / 1024, r2 - r1,
+ pages / (r2 - r1 + 0.), parallel);
+
+ for (i = 0; i < parallel; i++) {
+ wait(NULL);
+ }
+ unlink("rendezvous");
+ return (0);
+}
diff --git a/tools/test/stress2/misc/random.sh b/tools/test/stress2/misc/random.sh
new file mode 100755
index 000000000000..04a0075d0c2c
--- /dev/null
+++ b/tools/test/stress2/misc/random.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# A regression test for short AES mode reads
+# https://people.freebsd.org/~pho/stress/log/fsck-2.txt
+# Fixed by r349176
+
+for i in `jot 1024 1`; do
+ dd if=/dev/random of=/dev/null bs=$i count=1 status=none
+done
+exit 0
diff --git a/tools/test/stress2/misc/rdgsbase.sh b/tools/test/stress2/misc/rdgsbase.sh
new file mode 100755
index 000000000000..66a44592d419
--- /dev/null
+++ b/tools/test/stress2/misc/rdgsbase.sh
@@ -0,0 +1,182 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2017 Konstantin Belousov <kib@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Test scenario for "D12023: Make WRFSBASE and WRGSBASE functional."
+
+[ `uname -m` = "amd64" ] || exit 0
+
+. ../default.cfg
+
+cat > /tmp/rdgsbase.c <<EOF
+/*
+Below is the updated version of the test program. It is probably most
+useful to run it in parallel with the normal stress jobs. Please note
+that the program starts two CPU-intensive threads per core, so the
+whole test load would be run 4x slower.
+
+Also, it is very useful to try to run hwpmc measurements in parallel, at
+least for some time. E.g.,
+ pmcstat -S instructions -T
+in a terminal in parallel with other activities is enough. I do not need
+any numbers from hwpmc, just the fact that the driver causes the series of
+NMI to adjust the load for testing.
+*/
+
+/* $Id: rdgsbase.c,v 1.9 2017/08/13 16:22:01 kostik Exp kostik $ */
+
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <err.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <machine/sysarch.h>
+#include <machine/specialreg.h>
+#include <machine/cpufunc.h>
+
+void
+hand(int i __unused) { /* handler */
+ _exit(0);
+}
+
+static void *
+rungs(void *arg __unused)
+{
+ volatile char x[1024];
+ unsigned i;
+ uint64_t y, oldbase;
+
+ oldbase = rdgsbase();
+ for (i = 0;;) {
+ wrgsbase((uintptr_t)&x[i]);
+ if (rdgsbase() != (uintptr_t)&x[i]) {
+ wrgsbase(oldbase);
+ printf("bug1 %lx %lx\n", rdgsbase(), (uintptr_t)&x[i]);
+ exit(1);
+ }
+ sysarch(AMD64_GET_GSBASE, &y);
+ if (y != (uintptr_t)&x[i]) {
+ wrgsbase(oldbase);
+ printf("bug2 %lx %lx\n", y, (uintptr_t)&x[i]);
+ exit(1);
+ }
+ i++;
+ if (i >= nitems(x))
+ i = 0;
+ }
+ return (NULL);
+}
+
+static void *
+runfs(void *arg __unused)
+{
+ volatile char x[1024];
+ unsigned i;
+ uint64_t y, oldbase;
+
+ oldbase = rdfsbase();
+ for (i = 0;;) {
+ wrfsbase((uintptr_t)&x[i]);
+ if (rdfsbase() != (uintptr_t)&x[i]) {
+ wrfsbase(oldbase);
+ printf("bug3 %lx %lx\n", rdfsbase(), (uintptr_t)&x[i]);
+ exit(1);
+ }
+ sysarch(AMD64_GET_FSBASE, &y);
+ if (y != (uintptr_t)&x[i]) {
+ wrfsbase(oldbase);
+ printf("bug4 %lx %lx\n", y, (uintptr_t)&x[i]);
+ exit(1);
+ }
+ i++;
+ if (i > nitems(x))
+ i = 0;
+ }
+ return (NULL);
+}
+
+static void
+start(int nthreads)
+{
+ pthread_t thrs[nthreads * 2];
+ int error, i;
+
+ for (i = 0; i < nthreads; i++) {
+ error = pthread_create(&thrs[i], NULL, rungs, NULL);
+ if (error != 0)
+ errc(1, error, "pthread_create");
+ }
+ for (; i < 2 * nthreads; i++) {
+ error = pthread_create(&thrs[i], NULL, runfs, NULL);
+ if (error != 0)
+ errc(1, error, "pthread_create");
+ }
+}
+
+int
+main(void)
+{
+ static const int mib[2] = {CTL_HW, HW_NCPU};
+ int error, nthreads;
+ u_int p[4];
+ size_t len;
+
+ do_cpuid(0, p);
+ if (p[0] < 0x7) {
+ fprintf(stderr, "CPU does not support extended functions\n");
+ return (1);
+ }
+ cpuid_count(0x7, 0x0, p);
+ if ((p[1] & CPUID_STDEXT_FSGSBASE) == 0) {
+ fprintf(stderr, "CPU does not support RDGSBASE\n");
+ return (0);
+ }
+
+ len = sizeof(nthreads);
+ error = sysctl(mib, nitems(mib), &nthreads, &len, NULL, 0);
+ if (error == -1)
+ err(1, "sysctl hw.ncpu");
+ signal(SIGALRM, hand);
+ alarm(10);
+ start(nthreads);
+ for (;;)
+ pause();
+}
+EOF
+
+mycc -o /tmp/rdgsbase /tmp/rdgsbase.c -lpthread || exit 1
+rm /tmp/rdgsbase.c
+
+(cd /tmp; /tmp/rdgsbase)
+s=$?
+
+rm -f /tmp/rdgsbase /tmp/rdgsbase.core
+exit $s
diff --git a/tools/test/stress2/misc/rdwr.sh b/tools/test/stress2/misc/rdwr.sh
new file mode 100755
index 000000000000..c77c955dc210
--- /dev/null
+++ b/tools/test/stress2/misc/rdwr.sh
@@ -0,0 +1,118 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Test with read/write length of INT_MAX (i386) or INT_MAX+1 (amd64)
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > rdwr.c
+mycc -o rdwr -Wall rdwr.c || exit
+rm -f rdwr.c
+
+oldclamp=`sysctl debug.devfs_iosize_max_clamp 2>/dev/null |
+ awk '{print $NF}'`
+if [ `uname -m` = amd64 ]; then
+ [ "$oldclamp" = "1" ] && sysctl debug.devfs_iosize_max_clamp=0
+fi
+for j in `jot 10`; do
+ /tmp/rdwr || { echo FAIL; break; }
+done
+if [ `uname -m` = amd64 ]; then
+ [ "$oldclamp" = "1" ] && sysctl debug.devfs_iosize_max_clamp=1
+fi
+
+rm -f /tmp/rdwr
+exit
+EOF
+#include <sys/types.h>
+#include <sys/limits.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+#include <err.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+int
+main(int argc, char **argv)
+{
+ int fd1, fd2;
+ size_t len;
+ void *p;
+ struct iovec iov;
+
+ alarm(120);
+ if ((fd1 = open("/dev/null", O_RDWR, 0)) == -1)
+ err(1, "open /dev/null");
+
+ if ((fd2 = open("/dev/zero", O_RDWR)) == -1)
+ err(1, "open /dev/zero");
+
+ if (sizeof(size_t) == sizeof(int32_t))
+ len = (size_t)INT_MAX; /* i386 */
+ else
+ len = (size_t)INT_MAX + 1; /* amd64 */
+
+ if ((p = mmap(0, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd2, 0)) ==
+ MAP_FAILED)
+ err(1, "mmap");
+
+ if (read(fd2, p, len) != len)
+ err(1, "read");
+
+ if (write(fd1, p, len) != len)
+ err(1, "write");
+
+ if (pread(fd2, p, len, 0) != len)
+ err(1, "pread");
+
+ if (pwrite(fd1, p, len, 0) != len)
+ err(1, "pwrite");
+
+ iov.iov_base = p;
+ iov.iov_len = len;
+ if (readv(fd2, &iov, 1) != len)
+ err(1, "readv");
+
+ if (writev(fd1, &iov, 1) != len)
+ err(1, "writev");
+
+ if (preadv(fd2, &iov, 1, 0) != len)
+ err(1, "preadv");
+
+ if (pwritev(fd1, &iov, 1, 0) != len)
+ err(1, "pwritev");
+
+ close(fd1);
+ close(fd2);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/readdir.sh b/tools/test/stress2/misc/readdir.sh
new file mode 100755
index 000000000000..16523982d859
--- /dev/null
+++ b/tools/test/stress2/misc/readdir.sh
@@ -0,0 +1,179 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# readdir(3) fuzzing inspired by the iknowthis test suite
+# by Tavis Ormandy <taviso cmpxchg8b com>
+
+# "panic: kmem_malloc(1328054272): kmem_map too small" seen
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > readdir.c
+mycc -o readdir -Wall -Wextra readdir.c || exit 1
+rm -f readdir.c
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mount -t tmpfs tmpfs $mntpoint
+echo "Testing tmpfs(5)"
+cp -a /usr/include $mntpoint
+/tmp/readdir $mntpoint
+umount $mntpoint
+
+echo "Testing fdescfs(5)"
+kldstat -v | grep -q fdescfs || { kldload fdescfs.ko; loaded=1; }
+mount -t fdescfs null /dev/fd
+/tmp/readdir /dev/fd
+umount /dev/fd
+[ $unload ] && kldunload fdescfs.ko
+
+echo "Testing procfs(5)"
+mount -t procfs procfs $mntpoint
+/tmp/readdir $mntpoint
+umount $mntpoint
+
+if ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1; then
+ echo "Testing nfs"
+ mount -t nfs -o nfsv3,tcp,nolockd,retrycnt=3,soft,timeout=1 \
+ $nfs_export $mntpoint
+ /tmp/readdir $mntpoint
+ umount $mntpoint
+fi
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+cp -a /usr/include $mntpoint
+echo "Testing UFS"
+/tmp/readdir $mntpoint
+umount $mntpoint
+mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+cp -a /usr/include $mntpoint
+echo "Testing FFS"
+/tmp/readdir $mntpoint
+umount $mntpoint
+mdconfig -d -u $mdstart
+
+mount -t nullfs /bin $mntpoint
+echo "Testing nullfs(5)"
+/tmp/readdir $mntpoint
+umount $mntpoint
+
+rm -f /tmp/readdir
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/uio.h>
+#include <sys/wait.h>
+
+#include <dirent.h>
+#include <err.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <time.h>
+#include <unistd.h>
+
+#define RUNTIME 120
+
+/* copy from /usr/src/lib/libc/gen/gen-private.h */
+struct _telldir; /* see telldir.h */
+struct pthread_mutex;
+
+/*
+ * Structure describing an open directory.
+ *
+ * NOTE. Change structure layout with care, at least dd_fd field has to
+ * remain unchanged to guarantee backward compatibility.
+ */
+struct _dirdesc {
+ int dd_fd; /* file descriptor associated with directory */
+ long dd_loc; /* offset in current buffer */
+ long dd_size; /* amount of data returned by getdirentries */
+ char *dd_buf; /* data buffer */
+ int dd_len; /* size of data buffer */
+ long dd_seek; /* magic cookie returned by getdirentries */
+ long dd_rewind; /* magic cookie for rewinding */
+ int dd_flags; /* flags for readdir */
+ struct pthread_mutex *dd_lock; /* lock */
+ struct _telldir *dd_td; /* telldir position recording */
+};
+/* End copy */
+
+static void
+hand(int i __unused) { /* handler */
+ _exit(1);
+}
+
+static void
+test(char *path)
+{
+ DIR *dirp, fuzz;
+ int i;
+
+ signal(SIGSEGV, hand);
+ alarm(300);
+ for (i = 0; i < 2000; i++) {
+ if ((dirp = opendir(path)) == NULL)
+ break;
+ bcopy(dirp, &fuzz, sizeof(fuzz));
+ fuzz.dd_len = arc4random();
+ readdir(&fuzz);
+ closedir(dirp);
+ }
+
+ _exit(0);
+}
+
+int
+main(int argc __unused, char **argv)
+{
+ time_t start;
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ if (fork() == 0)
+ test(argv[1]);
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/recursiveflushes.sh b/tools/test/stress2/misc/recursiveflushes.sh
new file mode 100755
index 000000000000..1da843a2f1d1
--- /dev/null
+++ b/tools/test/stress2/misc/recursiveflushes.sh
@@ -0,0 +1,75 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Test recursive flushes in bdwrite().
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+snap() {
+ for i in `jot 5`; do
+ mksnap_ffs $1 $2
+ [ $? -eq 0 ] && break
+ done
+}
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 4g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+old=`sysctl vfs.recursiveflushes | awk '{print $NF}'`
+cd $mntpoint
+rm -f $mntpoint/.snap/stress2.*
+snap $mntpoint $mntpoint/.snap/stress2.1
+snap $mntpoint $mntpoint/.snap/stress2.2
+snap $mntpoint $mntpoint/.snap/stress2.3
+snap $mntpoint $mntpoint/.snap/stress2.4
+snap $mntpoint $mntpoint/.snap/stress2.5
+
+for i in `jot 32`; do
+ # Create 32 Mb files
+ dd if=/dev/zero of=big.$i bs=16k count=2048 status=none
+done
+wait
+for i in `jot 32`; do
+ rm -f big.$i
+done
+
+rm -f $mntpoint/.snap/stress2.*
+new=`sysctl vfs.recursiveflushes | awk '{print $NF}'`
+[ $old != $new ] && echo "vfs.recursiveflushes changed from $old to $new"
+
+cd /
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/rename.sh b/tools/test/stress2/misc/rename.sh
new file mode 100755
index 000000000000..93a1c9338ae4
--- /dev/null
+++ b/tools/test/stress2/misc/rename.sh
@@ -0,0 +1,130 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Test race between ISDOTDOT lookups and directory removal/rename
+
+# With lookup_shared=1 rename() will fail from time to time with ENOENT and
+# the following stat() will succed.
+
+# Test scenario by tegge
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > rename.c
+mycc -o rename -Wall rename.c || exit 1
+rm -f rename.c
+
+rm -rf /tmp/rename.dir.*
+for i in `jot 10`; do
+ for j in `jot 10`; do
+ /tmp/rename &
+ done
+ for j in `jot 10`; do
+ wait
+ done
+done
+rm -rf /tmp/rename.dir.* /tmp/rename
+exit 0
+EOF
+#include <err.h>
+#include <fcntl.h>
+#include <libgen.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+#define RUNTIME 30
+
+static char dir1[128];
+static char dir2[128];
+
+int
+main(int argc, char **argv)
+{
+ time_t start;
+ int status;
+ struct stat sb;
+ pid_t p;
+
+ sprintf(dir1, "rename.dir.%d", getpid());
+ sprintf(dir2, "rename.dir.2.%d", getpid());
+ if (mkdir(dir1, 0700) == -1)
+ err(1, "mkdir(%s)", dir1);
+
+ if (chdir(dir1) == -1)
+ err(1, "chdir(%s)", dir1);
+ if ((p = fork()) == -1)
+ err(1, "fork()");
+ if (p == 0) {
+ setproctitle("child");
+ if (chdir("..") == -1)
+ err(1, "chdir(%s)", "..");
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ if (rename(dir1, dir2) == -1) {
+ warn("rename(%s, %s)", dir1, dir2);
+ stat(dir1, &sb);
+ if (stat(dir1, &sb) == -1)
+ err(1, "stat(%s)", dir1);
+ else
+ errx(1, "stat(%s) succeeded!", dir1);
+ }
+ if (rename(dir2, dir1) == -1) {
+ warn("rename(%s, %s)", dir2, dir1);
+ stat(dir2, &sb);
+ if (stat(dir2, &sb) == -1)
+ err(1, "stat(%s)", dir2);
+ else
+ errx(1, "stat(%s) succeeded!", dir2);
+ }
+ }
+ _exit(0);
+ } else {
+ setproctitle("parent");
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ if (stat("..", &sb) == -1)
+ err(1, "stat(..)");
+ }
+ }
+ if (waitpid(p, &status, 0) == -1)
+ err(1, "waitpid()");
+ if (chdir("..") == -1)
+ err(1, "chdir(%s)", "..");
+ if (rmdir(dir1) == -1)
+ err(1, "rmdir(%s)", dir1);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/rename10.sh b/tools/test/stress2/misc/rename10.sh
new file mode 100755
index 000000000000..5cbf39f3d3d2
--- /dev/null
+++ b/tools/test/stress2/misc/rename10.sh
@@ -0,0 +1,184 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Regression test for rename(2) problem with missing reference release of
+# a busy "to" vnode, resulting in a leak.
+# Fixed in r253998.
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > rename10.c
+mycc -o rename10 -Wall -Wextra -g -O2 rename10.c || exit 1
+rm -f rename10.c
+cd $here
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 4g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+avail=`df -k $mntpoint | tail -1 | awk '{print $4}'`
+
+cd $mntpoint
+/tmp/rename10; s=$?
+cd $here
+
+for i in `jot 3`; do
+ sync
+ sleep 2
+done
+
+if [ `df -k $mntpoint | tail -1 | awk '{print $4}'` -lt $avail ]; then
+ echo FAIL
+ ls -ial $mntpoint
+ df -i $mntpoint
+fi
+
+n=0
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ n=$((n + 1))
+ [ $n -gt 5 ] && { umount -f $mntpoint; break; }
+done
+
+checkfs /dev/md${mdstart}$part || s=$?
+rm -f /tmp/rename10
+mdconfig -d -u $mdstart
+exit $s
+EOF
+#include <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define PARALLEL 4
+#define SIZE (1 * 1024 * 1024)
+
+static char *logfile = "logfile";
+static char *oldfiles[] = {
+ "logfile.0", "logfile.1", "logfile.2", "logfile.3", "logfile.4"
+};
+
+void *
+logger(void)
+{
+ int fd;
+ char * cp;
+
+ setproctitle("logger");
+ cp = calloc(1, SIZE);
+ for(;;) {
+ if ((fd = open(logfile, O_RDWR | O_APPEND)) != -1) {
+ if (write(fd, cp, SIZE) != SIZE)
+ err(1, "write()");
+ close(fd);
+ }
+ usleep(1);
+ }
+
+ _exit(0);
+}
+
+void *
+spin(void)
+{
+ int fd, i;
+
+ setproctitle("spin");
+ for(;;) {
+ for (i = 0; i < 5; i++) {
+ if ((fd = open(oldfiles[i], O_RDWR | O_APPEND)) != -1)
+ close(fd);
+ }
+ usleep(1);
+ }
+ _exit(0);
+}
+
+void
+renamer()
+{
+ int fd, i;
+ time_t start;
+
+ setproctitle("renamer");
+ start = time(NULL);
+ i = 0;
+ while (time(NULL) - start < 60) {
+ if ((fd = open(logfile, O_RDWR | O_CREAT | O_EXCL, 0644)) == -1)
+ err(1, "creat(%s)", logfile);
+ close(fd);
+ if (rename(logfile, oldfiles[i]) == -1)
+ err(1, "rename(%s, %s)", logfile, oldfiles[i]);
+ i = (i + 1) % 5;
+ }
+ for (i = 0; i < 5; i++) {
+ unlink(oldfiles[i]);
+ }
+ unlink(logfile);
+
+}
+
+int
+main() {
+ pid_t pids[PARALLEL], spids[PARALLEL];
+ int i;
+
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ logger();
+ if ((spids[i] = fork()) == 0)
+ spin();
+ }
+
+ renamer();
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (kill(pids[i], SIGINT) == -1)
+ err(1, "kill(%d)", pids[i]);
+ if (kill(spids[i], SIGINT) == -1)
+ err(1, "kill(%d)", spids[i]);
+ }
+ for (i = 0; i < PARALLEL * 2; i++)
+ wait(NULL);
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/rename11.sh b/tools/test/stress2/misc/rename11.sh
new file mode 100755
index 000000000000..19aaa16f92ea
--- /dev/null
+++ b/tools/test/stress2/misc/rename11.sh
@@ -0,0 +1,139 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# SU problem with "mkdir(d.008740.000987): Too many links" seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > rename11.c
+mycc -o rename11 -Wall -Wextra rename11.c || exit 1
+rm -f rename11.c
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+[ $# -eq 1 ] && newfs_flags=$1 # Problem only seen with SU
+echo newfs $newfs_flags md${mdstart}$part
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+mkdir $mntpoint/dir
+(
+ cd $mntpoint/dir
+ /tmp/rename11 || echo FAIL
+)
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+
+checkfs /dev/md${mdstart}$part; s=$?
+mdconfig -d -u $mdstart
+rm -rf /tmp/rename11
+exit $s
+EOF
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <err.h>
+#include <fcntl.h>
+#include <libgen.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define LOOPS 10
+#define PARALLEL 3
+#define ND 5000
+
+static void
+check(char *name)
+{
+ struct stat sb;
+
+ if (stat(name, &sb) == -1)
+ warn("stat(%s)", name);
+ else
+ warnx("stat(%s) succeeded!", name);
+}
+
+static void
+dirs(void)
+{
+ pid_t pid;
+ int i, j;
+ char name[80], to[80];
+
+ pid = getpid();
+
+ for (j = 0; j < LOOPS; j++) {
+ for (i = 0; i < ND; i++) {
+ snprintf(name, sizeof(name), "d.%06d.%06d", pid, i);
+ if (mkdir(name, 0700) == -1)
+ err(1, "mkdir(%s)", name);
+ snprintf(to , sizeof(to ), "d2.%06d.%06d", pid, i);
+ if (rename(name, to) == -1)
+ warn("rename(%s, %s)", name, to);
+ if (rename(to, name) == -1)
+ warn("rename(%s, %s)", to, name);
+ if (rmdir(name) == -1) {
+ check(name);
+ err(1, "rmdir(%s)", name);
+ }
+ }
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i, r, s;
+
+ r = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ dirs();
+ }
+
+ for (i = 0; i < PARALLEL; i++) {
+ wait(&s);
+ r += WEXITSTATUS(s);
+ }
+
+ return (r);
+}
diff --git a/tools/test/stress2/misc/rename12.sh b/tools/test/stress2/misc/rename12.sh
new file mode 100755
index 000000000000..3000e59b70d3
--- /dev/null
+++ b/tools/test/stress2/misc/rename12.sh
@@ -0,0 +1,192 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# No problems seen with SU. Panics with SU+J, just like suj30.sh
+
+# Triggers "known LOR in SU code" when crossmp8.sh is run first:
+# https://people.freebsd.org/~pho/stress/log/rename12.txt.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > rename12.c
+mycc -o rename12 -Wall -Wextra rename12.c || exit 1
+rm -f rename12.c
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 4g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+inodes=`df -i $mntpoint | tail -1 | awk '{print $7}'`
+loops=4
+parallel=12
+timeout=1200
+start=`date '+%s'`
+for i in `jot $loops`; do
+ for j in `jot $parallel`; do
+ mkdir -p $mntpoint/d$j/dir1
+ mkdir -p $mntpoint/d$j/dir2
+ (cd $mntpoint/d$j; /tmp/rename12 $((inodes/parallel)) ) &
+ done
+ wait
+ for j in `jot $parallel`; do
+ rmdir $mntpoint/d$j/dir1
+ rmdir $mntpoint/d$j/dir2
+ done
+ [ $((`date '+%s'` - start)) -lt $timeout ] && break
+done
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+
+checkfs /dev/md${mdstart}$part; s=$?
+mdconfig -d -u $mdstart
+rm -rf /tmp/rename12
+exit $s
+EOF
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <libgen.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+pid_t crpid;
+long n;
+int mvpipe[2], rmpipe[2];
+
+void
+cr(void)
+{
+ pid_t pid;
+ int i;
+ char name[80];
+
+ setproctitle("cr");
+ pid = getpid();
+ usleep(arc4random() & 1000);
+ for (i = 0; i < n; i++) {
+ snprintf(name, sizeof(name), "dir1/d.%06d.%06d", pid, i);
+ if (mkdir(name, 0700) == -1)
+ err(1, "mkdir(%s). %s:%d", name, __FILE__, __LINE__);
+ if (write(mvpipe[1], &i, sizeof(i)) != sizeof(i))
+ err(1, "write mvpipe");
+ }
+
+ _exit(0);
+}
+
+void
+mv(void)
+{
+ pid_t pid;
+ int i;
+ char name[80], to[80];
+
+ setproctitle("mv");
+ pid = crpid;
+ i = 0;
+ while (i != n - 1 &&
+ read(mvpipe[0], &i, sizeof(i)) == sizeof(i)) {
+ snprintf(name, sizeof(name), "dir1/d.%06d.%06d", pid, i);
+ snprintf(to , sizeof(to ), "dir2/d.%06d.%06d", pid, i);
+ if (rename(name, to) == -1)
+ warn("rename(%s, %s)", name, to);
+ if (write(rmpipe[1], &i, sizeof(i)) != sizeof(i))
+ err(1, "write rmpipe");
+ }
+ _exit(0);
+}
+
+void
+rm(void)
+{
+ pid_t pid;
+ int i;
+ char to[80];
+
+ setproctitle("rm");
+ pid = crpid;
+ i = 0;
+ while (i != n - 1 &&
+ read(rmpipe[0], &i, sizeof(i)) == sizeof(i)) {
+ snprintf(to, sizeof(to ), "dir2/d.%06d.%06d", pid, i);
+ if (rmdir(to) == -1)
+ warn("rmdir(%s)", to);
+ }
+ _exit(0);
+}
+
+int
+main(int argc, char **argv)
+{
+ int r, s;
+
+ if (argc != 2)
+ errx(1, "Usage %s <num inodes>", argv[0]);
+ n = atol(argv[1]);
+ if (n > 32765) {
+ n = 32765 - 1;
+ }
+
+ if (pipe(mvpipe) == -1)
+ err(1, "pipe()");
+ if (pipe(rmpipe) == -1)
+ err(1, "pipe()");
+
+ r = 0;
+ if ((crpid = fork()) == 0)
+ cr();
+ if (fork() == 0)
+ mv();
+ if (fork() == 0)
+ rm();
+
+ wait(&s);
+ r += WEXITSTATUS(s);
+ wait(&s);
+ r += WEXITSTATUS(s);
+ wait(&s);
+ r += WEXITSTATUS(s);
+
+ return (r);
+}
diff --git a/tools/test/stress2/misc/rename13.sh b/tools/test/stress2/misc/rename13.sh
new file mode 100755
index 000000000000..9234d4454751
--- /dev/null
+++ b/tools/test/stress2/misc/rename13.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Regression test for crossmp scenario:
+# Bug 216380 - mv /[dir == mountpoint] causes kernel panic
+# "panic: No vop_rename(0xfffff80033049000, 0xfffffe104d1e48a8)" seen.
+# Fixed by r312645.
+
+# Test scenario by: fnacl@protonmail.com
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 512m -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+s=0
+to=/tmp/rename13.dir
+mkdir $to
+mv $mntpoint $to 2>/dev/null && s=1
+
+for i in `jot 10`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint || sleep 1
+done
+mount | grep -q "on $mntpoint " && { s=2; umount -f $mntpoint; }
+mdconfig -d -u $mdstart
+rm -rf $to
+exit $s
diff --git a/tools/test/stress2/misc/rename14.sh b/tools/test/stress2/misc/rename14.sh
new file mode 100755
index 000000000000..83612a2632df
--- /dev/null
+++ b/tools/test/stress2/misc/rename14.sh
@@ -0,0 +1,198 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# "panic: handle_workitem_remove: DIRCHG and worklist not empty." seen:
+# https://people.freebsd.org/~pho/stress/log/rename14.txt
+# Fixed by r356714
+
+# Based on a syzkaller scenario reported by tuexen@freebsd.org
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed -e '1,/^EOF/d' < $odir/$0 > $dir/rename14.c
+mycc -o rename14 -Wall -Wextra -O0 -g rename14.c -lpthread || exit 1
+rm -f rename14.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+[ `jot -r 1 0 1` -eq 1 ] && opt="-U -n" || opt="-j -n"
+newfs -j -n md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+s=0
+(cd /mnt; /tmp/rename14)
+[ -f rename14.core -a $s -eq 0 ] &&
+ { ls -l rename14.core; mv rename14.core $dir; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+checkfs /dev/md$mdstart || s=2
+mdconfig -d -u $mdstart
+rm -rf $dir/rename14
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static sig_atomic_t done_testing;
+static volatile u_int *share;
+
+#define PARALLEL 10
+#define RUNTIME (3 * 60)
+#define SYNC 0
+
+static void *
+t1(void *data __unused)
+{
+ char n1[80];
+
+ snprintf(n1, sizeof(n1), "file1.%d", getpid());
+ while (done_testing == 0) {
+ rmdir(n1);
+ usleep(50000);
+ }
+
+ return (NULL);
+}
+
+static void *
+t2(void *data __unused)
+{
+ time_t start;
+ char n0[80], n00[80], n1[80];
+
+ snprintf(n0, sizeof(n0), "file0.%d", getpid());
+ snprintf(n00, sizeof(n00), "file0.%d/file0", getpid());
+ snprintf(n1, sizeof(n1), "file1.%d", getpid());
+ if (mkdir(n0, 0) == -1)
+ err(1, "mkdir(%s)", n0);
+
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ if (mkdir(n00, 0) == -1)
+ err(1, "mkdir(%s)", n00);
+ if (rename(n00, n1) == -1)
+ err(1, "rename(i%s, %s)", n00, n1);
+ }
+ if (rmdir(n0) == -1)
+ err(1, "rmdir(%s)", n0);
+ done_testing = 1;
+
+ return (NULL);
+}
+
+static void
+test(void)
+{
+ pthread_t tid[2];
+ int r;
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ done_testing = 0;
+ if ((r = pthread_create(&tid[0], NULL, t1, NULL)) != 0)
+ errc(1, r, "pthread_create");
+ if ((r = pthread_create(&tid[1], NULL, t2, NULL)) != 0)
+ errc(1, r, "pthread_create");
+
+ if ((r = pthread_join(tid[0], NULL)) != 0)
+ errc(1, r, "pthread_join");
+ if ((r = pthread_join(tid[1], NULL)) != 0)
+ errc(1, r, "pthread_join");
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/rename15.sh b/tools/test/stress2/misc/rename15.sh
new file mode 100755
index 000000000000..aa884e045010
--- /dev/null
+++ b/tools/test/stress2/misc/rename15.sh
@@ -0,0 +1,82 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "panic: flush_newblk_dep: Bad newblk 0xfffff8015fd9e800" seen:
+# https://people.freebsd.org/~pho/stress/log/rename15.txt
+
+. ../default.cfg
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+fl=$newfs_flags
+fl='-j'
+[ $fl = "-U" -a `jot -r 1 0 1` -eq 1 ] && fl="-j"
+echo "newfs $fl md$mdstart"
+newfs $fl md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+chmod 777 $mntpoint
+
+export LOAD=80
+export MAXSWAPPCT=80
+export RUNDIR=$mntpoint/stressX
+export dirnprenameLOAD=100
+export dirrenameLOAD=100
+export renameLOAD=100
+export runRUNTIME=10m
+export rwLOAD=80
+export TESTPROGS='
+testcases/lockf2/lockf2
+testcases/symlink/symlink
+testcases/openat/openat
+testcases/rw/rw
+testcases/fts/fts
+testcases/link/link
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/swap/swap
+testcases/dirnprename/dirnprename
+testcases/dirrename/dirrename
+'
+
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
+
+../tools/killall.sh
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+checkfs /dev/md$mdstart; s=$?
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/rename2.sh b/tools/test/stress2/misc/rename2.sh
new file mode 100755
index 000000000000..2c9781602d2c
--- /dev/null
+++ b/tools/test/stress2/misc/rename2.sh
@@ -0,0 +1,103 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# With lookup_shared=1 rename() will fail from time to time with ENOENT and
+# the following stat() will succeed. (Variation of rename.sh)
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > rename2.c
+mycc -o rename2 -Wall rename2.c || exit 1
+rm -f rename2.c
+cd $here
+
+rm -rf /tmp/rename.dir.*
+start=`date +%s`
+while [ $((`date +%s` - start -lt 300 ]; do
+ for j in `jot 10`; do
+ /tmp/rename2 &
+ done
+ wait
+done
+rm -rf /tmp/rename.dir.* /tmp/rename2
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static char dir1[128];
+static char dir2[128];
+
+int
+main(int argc, char **argv)
+{
+ struct stat sb;
+ time_t start;
+
+ sprintf(dir1, "/tmp/rename.dir.%d", getpid());
+ sprintf(dir2, "/tmp/rename.dir.2.%d", getpid());
+ if (mkdir(dir1, 0700) == -1)
+ err(1, "mkdir(%s)", dir1);
+
+ if (chdir(dir1) == -1)
+ err(1, "chdir(%s)", dir1);
+ if (chdir("..") == -1)
+ err(1, "chdir(%s)", "..");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < 120) {
+ if (rename(dir1, dir2) == -1) {
+ warn("rename(%s, %s)", dir1, dir2);
+ if (stat(dir1, &sb) == -1)
+ err(1, "stat(%s)", dir1);
+ else
+ errx(1, "stat(%s) succeeded!", dir1);
+ }
+ if (rename(dir2, dir1) == -1) {
+ warn("rename(%s, %s)", dir2, dir1);
+ if (stat(dir2, &sb) == -1)
+ err(1, "stat(%s)", dir2);
+ else
+ errx(1, "stat(%s) succeeded!", dir2);
+ }
+ }
+
+ if (rmdir(dir1) == -1)
+ err(1, "rmdir(%s)", dir1);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/rename3.sh b/tools/test/stress2/misc/rename3.sh
new file mode 100755
index 000000000000..c7ce91f0d359
--- /dev/null
+++ b/tools/test/stress2/misc/rename3.sh
@@ -0,0 +1,51 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Test vulnerability to transient failures when a directory closer
+# to the root directory is renamed
+
+# Deadlocks seen:
+# https://people.freebsd.org/~pho/stress/log/kostik447.txt
+# https://people.freebsd.org/~pho/stress/log/kirk058.txt
+
+# Test scenario by Tor Egge
+
+root=/tmp
+for i in `jot 10000`; do
+ rm -rf $root/a
+ mkdir -p $root/a/b/c/d/e/f/g
+ mkdir -p $root/a/b/c/d/e/f/z
+ cd $root/a/b/c/d/e/f
+ ( mv $root/a/b/c $root/a/c ) &
+ if ! mv z g/z; then
+ echo "FAILURE at loop $i"
+ break
+ fi
+ wait
+done
+rm -rf $root/a
diff --git a/tools/test/stress2/misc/rename4.sh b/tools/test/stress2/misc/rename4.sh
new file mode 100755
index 000000000000..1a8b1e0046e8
--- /dev/null
+++ b/tools/test/stress2/misc/rename4.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# lookup() does not return error when lookup of path ending on "/." is done
+# for RENAME operation.
+
+# Tets scenario by Jim Meyering <jim meyering net>
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > rename4.c
+mycc -o rename4 -Wall rename4.c
+rm -f rename4.c
+cd $RUNDIR
+
+rm -rf 1 2
+mkdir 1 2
+
+/tmp/rename4 1 2/.
+
+rm -rf 1 2 /tmp/rename4
+exit
+EOF
+#include <stdio.h>
+#include <err.h>
+
+int
+main(int argc, char **argv)
+{
+ if (rename(argv[1], argv[2]) == -1)
+ err(1, "rename(%s, %s)", argv[1], argv[2]);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/rename5.sh b/tools/test/stress2/misc/rename5.sh
new file mode 100755
index 000000000000..89779b79ec32
--- /dev/null
+++ b/tools/test/stress2/misc/rename5.sh
@@ -0,0 +1,137 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# Rename scenario from kern/156545 by Konstantin,
+# konstantin malov kaspersky com
+
+# On 8.2-STABLE fsck reports
+# "xxx IS AN EXTRANEOUS HARD LINK TO DIRECTORY yyy"
+# Fixed by r220986
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q "$mntpoint" && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > rename5.c
+mycc -o rename5 -Wall -Wextra -O2 rename5.c
+rm -f rename5.c
+
+cd $mntpoint
+
+/tmp/rename5
+
+cd $here
+rm -f /tmp/rename5
+
+while mount | grep -q md${mdstart}$part; do
+ umount $mntpoint || sleep 1
+done
+
+checkfs /dev/md${mdstart}$part; s=$?
+
+mdconfig -d -u $mdstart
+exit $s
+EOF
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define N 1000
+#define RUNTIME (5 * 60)
+
+void
+test(void)
+{
+ pid_t pid;
+ int i;
+ char from[128], to[128];
+
+ pid = getpid();
+ for (i = 0; i < N; i++) {
+ snprintf(from, sizeof(from), "src/dir.%06d", i);
+ snprintf(to , sizeof(to), "src/dir.%06d.%06d", i, pid);
+ (void)rename(from, to);
+ }
+ _exit(0);
+}
+
+int
+main()
+{
+ time_t start;
+ int fd, i;
+ char dir[128], file[128];
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ if (mkdir("src", 0700) == -1)
+ err(1, "mkdir(src)");
+
+ for (i = 0; i < N; i++) {
+ snprintf(dir, sizeof(dir), "src/dir.%06d", i);
+ if (mkdir(dir, 0700) == -1)
+ err(1, "mkdir(%s)", dir);
+ snprintf(file, sizeof(file), "%s/meta", dir);
+ if ((fd = open(file, O_RDWR | O_CREAT, 0600)) < 0)
+ err(1, "open(%s)", file);
+ close(fd);
+ snprintf(file, sizeof(file), "%s/data", dir);
+ if ((fd = open(file, O_RDWR | O_CREAT, 0600)) < 0)
+ err(1, "open(%s)", file);
+ close(fd);
+ }
+
+ for (i = 0; i < 2; i++) {
+ if (fork() == 0)
+ test();
+ }
+ for (i = 0; i < 2; i++)
+ wait(NULL);
+
+ system("rm -rf src > /dev/null 2>&1");
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/rename6.sh b/tools/test/stress2/misc/rename6.sh
new file mode 100755
index 000000000000..6c27c5601d2a
--- /dev/null
+++ b/tools/test/stress2/misc/rename6.sh
@@ -0,0 +1,149 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Demonstrate rename(2) cache problem, where the original name lingers in the VFS cache.
+
+# Original test scenario by Anton Yuzhaninov <citrin citrin ru>
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > rename6.c
+mycc -o rename6 -Wall -Wextra -O2 rename6.c
+rm -f rename6.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c "cd $mntpoint; /tmp/rename6"
+
+while mount | grep -q md${mdstart}$part; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/rename6
+exit
+EOF
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+pid_t spid;
+const char *logfile = "test.log";
+
+void
+cleanup()
+{
+ if (kill(spid, SIGINT) == -1 && errno != ESRCH)
+ err(1, "kill(%d)", spid);
+}
+
+static void
+Stat()
+{
+ struct stat sb;
+ int i;
+
+ setproctitle("Stat");
+ for (;;) {
+ for (i = 0; i < 1000; i++) {
+ stat(logfile, &sb);
+ }
+ usleep(1000);
+ }
+}
+
+int
+main(void)
+{
+ struct stat sb1, sb2;
+ int fd, i;
+ char new[128];
+
+ if ((spid = fork()) == 0)
+ Stat();
+
+ setproctitle("main");
+ atexit(cleanup);
+ for (i = 0; i < 20000; i++) {
+ sprintf(new, "test.log.%05d", i);
+ if ((fd = open(logfile, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1)
+ err(1, "creat(%s)", logfile);
+ close(fd);
+#if 1
+ if (rename(logfile, new) == -1)
+ warn("rename(%s, %s)", logfile, new);
+#else
+ /* No cache problem is seen */
+ if (link(logfile, new) == -1)
+ err(1, "link(%s, %s)", logfile, new);
+ if (unlink(logfile) == -1)
+ err(1, "unlink(%s)", logfile);
+#endif
+ /*
+ * stat() for logfile and new will be identical sometimes,
+ * but only when Stat() is running.
+ */
+ if (stat(logfile, &sb1) == 0 && stat(new, &sb2) == 0 &&
+ bcmp(&sb1, &sb2, sizeof(sb1)) == 0) {
+ fprintf(stderr, "At loop #%d\n", i);
+ fprintf(stderr, "%-15s: ino = %ju, nlink = %ju,"
+ " size = %jd\n", logfile, (uintmax_t)sb1.st_ino,
+ (uintmax_t)sb1.st_nlink, sb1.st_blocks);
+ fprintf(stderr, "%-15s: ino = %ju, nlink = %ju, "
+ "size = %jd\n", new , (uintmax_t)sb2.st_ino,
+ (uintmax_t)sb2.st_nlink, sb2.st_blocks);
+ }
+ unlink(new);
+ }
+
+ kill(spid, SIGINT);
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/rename7.sh b/tools/test/stress2/misc/rename7.sh
new file mode 100755
index 000000000000..89edcd17e827
--- /dev/null
+++ b/tools/test/stress2/misc/rename7.sh
@@ -0,0 +1,150 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# After a few runs this will happen:
+# $ umount /mnt
+# umount: unmount of /mnt failed: Device busy
+# $ umount -f /mnt
+# $
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > rename7.c
+mycc -o rename7 -Wall -Wextra -O2 rename7.c || exit
+rm -f rename7.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c "cd $mntpoint; /tmp/rename7 || echo FAIL"
+
+for i in `jot 10`; do
+ mount | grep -q md${mdstart}$part && \
+ umount $mntpoint && mdconfig -d -u $mdstart && break
+done
+if mount | grep -q md${mdstart}$part; then
+ echo "Test failed"
+ exit 1
+fi
+rm -f /tmp/rename7
+exit 0
+EOF
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+const char *logfile = "test.log";
+pid_t wpid, spid;
+
+void
+r1(void)
+{
+ int i;
+ struct stat sb1, sb2;
+
+ for (i = 0; i < 800000; i++) {
+ rename(logfile, "r1");
+ if (stat("r1", &sb1) == 0 && stat("r2", &sb2) == 0 &&
+ bcmp(&sb1, &sb2, sizeof(sb1)) == 0) {
+ fprintf(stderr, "r1 and r2 are identical after rename(%s, \"r1\")\n", logfile);
+ system("ls -ail");
+ _exit(1);
+ }
+ }
+ _exit(0);
+}
+
+void
+r2(void)
+{
+ int i;
+ struct stat sb1, sb2;
+
+// _exit(0); /* No problems with only r1 running */
+ for (i = 0; i < 800000; i++) {
+ rename(logfile, "r2");
+ if (stat("r1", &sb1) == 0 && stat("r2", &sb2) == 0 &&
+ bcmp(&sb1, &sb2, sizeof(sb1)) == 0) {
+ usleep(10000);
+ fprintf(stderr, "r1 and r2 are identical after rename(%s, \"r2\")\n", logfile);
+ system("ls -ail");
+ _exit(1);
+ }
+ }
+ _exit(0);
+}
+int
+main(void)
+{
+ pid_t wpid, spid;
+ int e, fd, i, status;
+
+ if ((wpid = fork()) == 0)
+ r1();
+ if ((spid = fork()) == 0)
+ r2();
+
+ setproctitle("main");
+ e = 0;
+
+ for (i = 0; i < 800000; i++) {
+ if ((fd = open(logfile, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1)
+ warn("creat(%s)", logfile);
+ close(fd);
+ }
+
+ kill(wpid, SIGINT);
+ kill(spid, SIGINT);
+ wait(&status);
+ e += WEXITSTATUS(status);
+ wait(&status);
+ e += WEXITSTATUS(status);
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/rename8.sh b/tools/test/stress2/misc/rename8.sh
new file mode 100755
index 000000000000..e0019cb353fa
--- /dev/null
+++ b/tools/test/stress2/misc/rename8.sh
@@ -0,0 +1,173 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Cache inconsistency seen on "to" file for rename(2).
+
+# Scenario by jhb@
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > rename8.c
+mycc -o rename8 -Wall -Wextra -O2 rename8.c
+rm -f rename8.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c "cd $mntpoint; mkdir r; /tmp/rename8 r"
+ls -li $mntpoint/r | egrep -v "^total"
+
+for i in `jot 10`; do
+ mount | grep -q md${mdstart}$part && \
+ umount $mntpoint && mdconfig -d -u $mdstart && break
+ sleep 1
+done
+if mount | grep -q md${mdstart}$part; then
+ fuser $mntpoint
+ echo "umount $mntpoint failed"
+ exit 1
+fi
+
+mdconfig -l | grep -q md$mdstart &&
+ mdconfig -d -u $mdstart
+rm -f /tmp/rename8
+exit
+EOF
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+static char *always, *file1, *file2;
+static ino_t always_ino;
+
+static void
+usage(void)
+{
+ fprintf(stderr, "Usage: rename_race <dir>\n");
+ exit(1);
+}
+
+static void
+child(void)
+{
+ struct stat sb;
+
+ /* Exit as soon as our parent exits. */
+ while (getppid() != 1) {
+ stat(file1, &sb);
+ }
+ exit(0);
+}
+
+static void
+create_file(const char *path)
+{
+ int fd;
+
+ fd = open(path, O_CREAT, 0666);
+ if (fd < 0)
+ err(1, "open(%s)", path);
+ close(fd);
+}
+
+int
+main(int ac, char **av)
+{
+ struct stat sb, sb2;
+ pid_t pid;
+ int i, r;
+
+ if (ac != 2)
+ usage();
+ if (stat(av[1], &sb) != 0)
+ err(1, "stat(%s)", av[1]);
+ if (!S_ISDIR(sb.st_mode))
+ errx(1, "%s not a directory", av[1]);
+
+ asprintf(&always, "%s/file.always", av[1]);
+ asprintf(&file1, "%s/file1", av[1]);
+ asprintf(&file2, "%s/file2", av[1]);
+
+ create_file(always);
+ if (stat(always, &sb) != 0)
+ err(1, "stat(%s)", always);
+ always_ino = sb.st_ino;
+
+ pid = fork();
+ if (pid < 0)
+ err(1, "fork");
+ if (pid == 0)
+ child();
+ r = 0;
+ for (i = 0; i < 100000; i++) {
+ if (unlink(file1) < 0 && errno != ENOENT)
+ err(1, "unlink(%s)", file1);
+ if (link(always, file1) < 0)
+ err(1, "link(%s, %s)", always, file1);
+ create_file(file2);
+ if (stat(file2, &sb2) < 0)
+ err(1, "stat(%s)", file2);
+ if (rename(file2, file1) < 0)
+ err(1, "rename(%s, %s)", file2, file1);
+ if (stat(file1, &sb) < 0)
+ err(1, "stat(%s)", file1);
+ if (sb.st_ino != sb2.st_ino ||
+ sb.st_ino == always_ino) {
+ printf("FAIL. Bad stat: always: %ju file1: %ju (should be %ju)\n",
+ (uintmax_t)always_ino, (uintmax_t)sb.st_ino,
+ (uintmax_t)sb2.st_ino);
+ r = EXIT_FAILURE;
+ break;
+ }
+ }
+ kill(pid, SIGINT);
+ wait(NULL);
+ if (r == 0) {
+ unlink(always);
+ unlink(file1);
+ unlink(file2);
+ }
+ return (r);
+}
diff --git a/tools/test/stress2/misc/rename9.sh b/tools/test/stress2/misc/rename9.sh
new file mode 100755
index 000000000000..e169c64af5ea
--- /dev/null
+++ b/tools/test/stress2/misc/rename9.sh
@@ -0,0 +1,169 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Variation of rename6.sh. Cache problem of "to" file name seen.
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > rename9.c
+mycc -o rename9 -Wall -Wextra -O2 rename9.c
+rm -f rename9.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+rm -rf $mntpoint/.snap
+chmod 777 $mntpoint
+
+(while true; do ls -lRi $mntpoint > /dev/null 2>&1; done) &
+su $testuser -c "cd $mntpoint; /tmp/rename9"
+kill $! > /dev/null 2>&1
+wait
+ls -ilR $mntpoint | egrep -v "^total "
+
+while mount | grep -q md${mdstart}$part; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/rename9
+exit
+EOF
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+pid_t spid;
+char *fromFile = "fromFile.log";
+char toFile[128];
+
+void
+cleanup()
+{
+ kill(spid, SIGINT);
+}
+
+static void
+statFrom()
+{
+ struct stat sb;
+
+ setproctitle("Stat");
+ for (;;) {
+ stat(fromFile, &sb);
+ }
+}
+
+int
+main(void)
+{
+ struct stat fb, tb, fa, ta;
+ int fd, i;
+
+ if ((spid = fork()) == 0)
+ statFrom();
+
+ setproctitle("main");
+ atexit(cleanup);
+ for (i = 0;i < 100000; i++) {
+ bzero(&fb, sizeof(fb));
+ bzero(&tb, sizeof(tb));
+ bzero(&fa, sizeof(fa));
+ bzero(&ta, sizeof(ta));
+
+ if ((fd = open(fromFile, O_RDWR | O_CREAT | O_TRUNC, 0644))
+ == -1)
+ err(1, "creat(%s)", fromFile);
+ close(fd);
+
+ sprintf(toFile, "toFile.log.%05d", i);
+ if ((fd = open(toFile, O_RDWR | O_CREAT | O_TRUNC, 0644))
+ == -1)
+ err(1, "creat(%s)", toFile);
+ write(fd, "xxx", 3);
+ close(fd);
+
+ stat(fromFile, &fb);
+ stat(toFile, &tb);
+ if (rename(fromFile, toFile) == -1)
+ warn("rename(%s, %s)", fromFile, toFile);
+ stat(fromFile, &fa);
+ if (stat(toFile, &ta) == -1)
+ err(1, "stat(%s)", toFile);
+
+ if (tb.st_ino == ta.st_ino) {
+ fprintf(stderr, "FAIL: old and new \"To\" inode "
+ "number is identical\n");
+ fprintf(stderr, "stat() before the rename():\n");
+ fprintf(stderr,
+ "%-16s: ino = %4ju, nlink = %ju, size = %jd\n",
+ fromFile, (uintmax_t)fb.st_ino, (uintmax_t)fb.st_nlink,
+ fb.st_blocks);
+ fprintf(stderr,
+ "%-16s: ino = %4ju, nlink = %ju, size = %jd\n",
+ toFile, (uintmax_t)tb.st_ino, (uintmax_t)tb.st_nlink,
+ tb.st_blocks);
+ fprintf(stderr, "\nstat() after the rename():\n");
+ if (fa.st_ino != 0)
+ fprintf(stderr,
+ "%-16s: ino = %4ju, nlink = %ju, size = "
+ "%jd\n", fromFile, (uintmax_t)fa.st_ino,
+ (uintmax_t)fa.st_nlink, fa.st_blocks);
+ fprintf(stderr,
+ "%-16s: ino = %4ju, nlink = %ju, size = %jd\n",
+ toFile, (uintmax_t)ta.st_ino, (uintmax_t)ta.st_nlink,
+ ta.st_blocks);
+ kill(spid, SIGINT);
+ exit(1);
+ }
+ unlink(toFile);
+ }
+
+ kill(spid, SIGINT);
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/revoke.sh b/tools/test/stress2/misc/revoke.sh
new file mode 100755
index 000000000000..453cbc1001a5
--- /dev/null
+++ b/tools/test/stress2/misc/revoke.sh
@@ -0,0 +1,116 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Regression test. Causes panic on 6.1
+
+. ../default.cfg
+
+odir=`pwd`
+dir=/tmp
+
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/revoke.c
+mycc -o revoke -Wall revoke.c || exit 1
+rm -f revoke.c
+
+n=100 # Number of times to test
+for i in `jot $n`; do
+ ./revoke /dev/ttyv9 > /dev/null 2>&1 &
+ ./revoke /dev/ttyva > /dev/null 2>&1 &
+ ./revoke /dev/ttyvb > /dev/null 2>&1 &
+ ./revoke /dev/ttyvc > /dev/null 2>&1 &
+ wait
+done
+
+rm -f revoke
+
+exit
+EOF
+/* By Martin Blapp, <mb@imp.ch> <mbr@FreeBSD.org> */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <err.h>
+#include <sys/ioctl.h>
+#include <sys/wait.h>
+#include <string.h>
+
+/*#define TTY "/dev/ttyv9"*/ /* should be totally unused */
+#define CTTY "/dev/tty"
+
+int
+main(int argc, char **argv)
+{
+ int ttyfd;
+ pid_t pid;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s /dev/ttyv?\n", argv[0]);
+ return 1;
+ }
+
+ /* Get rid of my ctty. */
+ printf("Parent starting: pid %d\n", getpid());
+ pid = fork();
+ if (pid < 0) {
+ err(1, "fork");
+ exit(1);
+ } else if (pid > 0) {
+ int status;
+ /* parent */
+ waitpid(pid, &status, 0);
+ exit(0);
+ }
+ /* child */
+ printf("Child: pid %d\n", getpid());
+
+ if (setsid() < 0) {
+ err(1, "setsid");
+ exit(1);
+ }
+ ttyfd = open(argv[1], O_RDWR);
+ if (ttyfd < 0) {
+ err(1, "open(%s)", argv[1]);
+ exit(1);
+ }
+ if (ioctl(ttyfd, TIOCSCTTY) < 0) {
+ err(1, "ioctl(TIOCSCTTY)");
+ exit(1);
+ }
+ if (revoke(argv[1]) < 0) {
+ err(1, "revoke(%s)", argv[1]);
+ exit(1);
+ }
+ if (open(CTTY, O_RDWR) < 0) {
+ err(1, "open(%s)", CTTY);
+ exit(1);
+ }
+ return 0;
+}
diff --git a/tools/test/stress2/misc/rot.sh b/tools/test/stress2/misc/rot.sh
new file mode 100755
index 000000000000..b658f35aad1b
--- /dev/null
+++ b/tools/test/stress2/misc/rot.sh
@@ -0,0 +1,142 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Unmapped I/O test scenario:
+# http://people.freebsd.org/~pho/stress/log/kostik515.txt
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > rot.c
+mycc -o rot -Wall -Wextra -O2 -g rot.c || exit 1
+rm -f rot.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+(cd $mntpoint; /tmp/rot)
+(cd `dirname $diskimage`; /tmp/rot)
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/rot
+exit 0
+EOF
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+#define N 10240 /* 40 Mb */
+#define PARALLEL 20
+#define RUNTIME (60 * 15)
+
+int
+test(void)
+{
+ int fd, i, j, s;
+ unsigned char *buf;
+ char path[128];
+
+ s = getpagesize();
+
+ sprintf(path,"%s.%05d", getprogname(), getpid());
+ if ((fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
+ err(1, "open(%s)", path);
+ if (lseek(fd, s * N - 1, SEEK_SET) == -1)
+ err(1, "lseek error");
+
+ /* write a dummy byte at the last location */
+ if (write(fd, "", 1) != 1)
+ err(1, "write error");
+
+ for (i = 0; i < N; i++) {
+ if ((buf = mmap(0, s, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
+ i * s)) == MAP_FAILED)
+ err(1, "write map");
+ for (j = 0; j < s; j++)
+ buf[j] = i % 256;
+ }
+ if (munmap(buf, s) == -1)
+ err(1, "unmap (write)");
+ close(fd);
+
+ if ((fd = open(path, O_RDONLY)) < 0)
+ err(1, "open(%s)", path);
+ for (i = 0; i < N; i++) {
+ if ((buf = mmap(0, s, PROT_READ, MAP_SHARED, fd, i * s)) ==
+ MAP_FAILED)
+ err(1, "write map");
+ for (j = 0; j < s; j++)
+ if (buf[j] != i % 256)
+ fprintf(stderr, "read %d, expected %d at %d\n",
+ buf[j], i % 256, i);
+ }
+ if (munmap(buf, s) == -1)
+ err(1, "unmap (read)");
+ close(fd);
+ unlink(path);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ time_t start;
+ int i;
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ test();
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+ }
+
+ return(0);
+}
diff --git a/tools/test/stress2/misc/routetbl.sh b/tools/test/stress2/misc/routetbl.sh
new file mode 100755
index 000000000000..0a2b936f15e4
--- /dev/null
+++ b/tools/test/stress2/misc/routetbl.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Demonstrate memory leak of routetbl allegedly caused by use free() in
+# vfs_free_addrlist_af(), instead of rn_detachhead().
+# "routetbl grew 3868" seen on amd64.
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+pgrep -q mountd || exit 0
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 128m -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+
+routetbl=`vmstat -m | grep routetbl | awk '{print $2}'`
+s=0
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 60 ]; do
+ mount /dev/md${mdstart}$part $mntpoint &&
+ umount $mntpoint
+done
+routetbl=$((`vmstat -m | grep routetbl | awk '{print $2}'` - routetbl))
+[ $routetbl -gt 0 ] &&
+ { echo "routetbl grew $routetbl"; s=1; }
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1
+mdconfig -d -u $mdstart
+rm -rf /tmp/template
+exit $s
diff --git a/tools/test/stress2/misc/ruby.sh b/tools/test/stress2/misc/ruby.sh
new file mode 100755
index 000000000000..2ed1b023d0ec
--- /dev/null
+++ b/tools/test/stress2/misc/ruby.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# "small MAP_STACK requests" scenario.
+
+# "<main>: warning: pthread_create failed for timer: Resource temporarily
+# unavailable, scheduling broken" seen.
+# Fixed by r320339.
+
+[ -x "`which ruby`" ] || exit 0
+
+ruby -v 2>&1 | tee $log | grep warning && s=1 || s=0
+exit $s
diff --git a/tools/test/stress2/misc/rw.sh b/tools/test/stress2/misc/rw.sh
new file mode 100755
index 000000000000..2cba9587c925
--- /dev/null
+++ b/tools/test/stress2/misc/rw.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Simple read / write test scenario with VM pressure.
+
+# Out of VM seen:
+# 100135 D vmwait 0xffffffff81d1a348 [CAM taskq]
+# https://people.freebsd.org/~pho/stress/log/rw.txt
+
+. ../default.cfg
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+
+export rwLOAD=100
+export swapLOAD=100
+export TESTPROGS="
+testcases/rw/rw
+testcases/swap/swap
+"
+
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/rwlock_ronly.sh b/tools/test/stress2/misc/rwlock_ronly.sh
new file mode 100755
index 000000000000..4a9b761ad98e
--- /dev/null
+++ b/tools/test/stress2/misc/rwlock_ronly.sh
@@ -0,0 +1,82 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# Test scenario by kib@
+# Demonstrate process looping in kernel mode.
+# Fixed in r251684.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/rwlock_ronly.c
+mycc -o rwlock_ronly -Wall -Wextra rwlock_ronly.c || exit 1
+rm -f rwlock_ronly.c
+cd $odir
+
+/tmp/rwlock_ronly || echo OK
+
+rm -f /tmp/rwlock_ronly
+exit
+
+EOF
+/* $Id: rwlock_ronly.c,v 1.2 2013/06/10 04:44:08 kostik Exp kostik $ */
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <errno.h>
+#include <sys/umtx.h>
+#include <err.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ char *p;
+ struct urwlock *rw;
+ int error;
+
+ p = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANON, -1, 0);
+ if (p == (char *)MAP_FAILED)
+ err(1, "mmap");
+
+ rw = (struct urwlock *)p;
+ rw->rw_state = URWLOCK_READ_WAITERS;
+
+ error = mprotect(p, getpagesize(), PROT_READ);
+ if (error == -1)
+ err(1, "mprotect");
+
+ error = _umtx_op(p, UMTX_OP_RW_RDLOCK, 0, NULL, NULL);
+ if (error != 0)
+ err(1, "rdlock");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sched.sh b/tools/test/stress2/misc/sched.sh
new file mode 100755
index 000000000000..f91055b3c289
--- /dev/null
+++ b/tools/test/stress2/misc/sched.sh
@@ -0,0 +1,161 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Show scheduler fairness for ULE vs. 4BSD.
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > sched.c
+mycc -o sched -Wall -Wextra -O0 sched.c || exit 1
+rm -f sched.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+cpus=`sysctl hw.ncpu | sed 's/.*: //'`
+uname -v
+(cd $mntpoint; /tmp/sched $((cpus + 1))) > /dev/null 2>&1 &
+sleep 30
+export LANG=C
+top -U nobody -d 1 | grep nobody | awk '{print $11}' | sed 's/%//' |
+ ministat -A -w 73 | tail -1 | awk '{if ($NF > 1.0) exit 1}' ||
+{ echo Broken; top -U nobody -d 1 | grep nobody; }
+killall sched
+wait
+
+for i in `jot 3`; do
+ echo "run #$i"
+ (cd $mntpoint; /tmp/sched $((cpus + 1)))
+done
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+rm -f /tmp/sched
+mdconfig -d -u $mdstart
+exit
+EOF
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <libutil.h>
+#include <pwd.h>
+#include <sched.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define N 100 * 1024 * 1024
+
+double r;
+int parallel;
+
+void
+work(void)
+{
+ struct passwd *pw;
+ struct timespec start, finish;
+ double d1, d2;
+ int i, j;
+ volatile char *cp;
+
+ while (access("rendezvous", R_OK) != 0)
+ usleep(1);
+
+ if ((pw = getpwnam("nobody")) == NULL)
+ err(1, "no such user: nobody");
+ if (setgroups(1, &pw->pw_gid) ||
+ setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
+ seteuid(pw->pw_uid) || setuid(pw->pw_uid))
+ err(1, "Can't drop privileges to \"nobody\"");
+ endpwent();
+
+ d1 = d2 = 0;
+ cp = malloc(N);
+ clock_gettime(CLOCK_REALTIME_PRECISE, &start);
+ for (i = 0; i < 1; i++) {
+ for (j = 0; j < INT_MAX; j++) {
+ d1 = d1 + 1.0 / j;
+ d2 = d1 + 0.8 / j;
+ if (j % 1000 == 0) {
+ cp[arc4random() % N] = j % 255;
+ }
+ }
+ }
+ r = d1 + d2;
+ clock_gettime(CLOCK_REALTIME_PRECISE, &finish);
+ timespecsub(&finish, &start, &finish);
+#if defined(DEBUG)
+ fprintf(stderr, "Elapsed time for pid %d: %.4f\n", getpid(),
+ finish.tv_sec + (double)finish.tv_nsec / 1e9);
+#endif
+
+ _exit(0);
+}
+
+int
+main(int argc, char **argv)
+{
+ int fd, i;
+
+ if (argc == 2)
+ parallel = atoi(argv[1]);
+ else
+ errx(1, "Usage: %s <cpus>", argv[0]);
+
+ for (i = 0; i < parallel; i++) {
+ if (fork() == 0)
+ work();
+ }
+ if ((fd = open("rendezvous", O_CREAT, 0644)) == -1)
+ err(1, "open()");
+ close(fd);
+ for (i = 0; i < parallel; i++)
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/schedfuzz.sh b/tools/test/stress2/misc/schedfuzz.sh
new file mode 100755
index 000000000000..c1a7918cc4fb
--- /dev/null
+++ b/tools/test/stress2/misc/schedfuzz.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Sched fuzz test scenario.
+
+# "panic: sleeping thread" seen:
+# https://people.freebsd.org/~pho/stress/log/schedfuzz.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+sysctl kern.sched_fuzz.prof_on > /dev/null 2>&1 || exit 0
+
+: ${pct1:=0.01}
+: ${pct2:=0.1}
+export pct1 pct2
+
+seton() {
+ sysctl kern.sched_fuzz.sched_fuzz_scheduler="${pct1}%return(1)"
+ sysctl kern.sched_fuzz.sched_fuzz_sx_slock_delay="${pct1}%schedfuzz(82)"
+ sysctl kern.sched_fuzz.sched_fuzz_sx_xlock_delay="${pct1}%schedfuzz(82)"
+ sysctl kern.sched_fuzz.sched_fuzz_sx_slock_sleep="${pct1}%schedfuzz(82)"
+ sysctl kern.sched_fuzz.sched_fuzz_sx_xlock_sleep="${pct1}%schedfuzz(82)"
+ sysctl kern.sched_fuzz.sched_fuzz_mtx_spin_lock="${pct1}%schedfuzz(44)"
+ sysctl kern.sched_fuzz.sched_fuzz_mtx_spin_unlock="${pct1}%schedfuzz(44)"
+ sysctl kern.sched_fuzz.sched_fuzz_malloc="${pct1}%schedfuzz(62)"
+ sysctl kern.sched_fuzz.sched_fuzz_cv_wait="${pct2}%schedfuzz(100)"
+ sysctl kern.sched_fuzz.sched_fuzz_cv_timed_wait="${pct2}%schedfuzz(100)"
+ sysctl kern.sched_fuzz.sched_fuzz_cv_timed_wait_sig="${pct2}%schedfuzz(100)"
+ sysctl kern.sched_fuzz.sched_fuzz_mtx_lock="${pct1}%schedfuzz(22)"
+ sysctl kern.sched_fuzz.sched_fuzz_mtx_unlock="${pct1}%schedfuzz(22)"
+ sysctl kern.sched_fuzz.sched_fuzz_sleepq="${pct1}%schedfuzz(150)"
+
+ sysctl kern.sched_fuzz.prof_on=1
+}
+
+setoff() {
+ sysctl kern.sched_fuzz.prof_on=0
+}
+
+if [ $# -eq 1 ]; then
+ case "$1" in
+ on) seton
+ ;;
+ off) setoff
+ ;;
+ *) echo "Usage: $0 [on|off]"
+ exit 1
+ ;;
+ esac
+else
+ seton
+ ./procfs.sh
+ setoff
+fi
diff --git a/tools/test/stress2/misc/sctp.sh b/tools/test/stress2/misc/sctp.sh
new file mode 100755
index 000000000000..7e28deb5b300
--- /dev/null
+++ b/tools/test/stress2/misc/sctp.sh
@@ -0,0 +1,158 @@
+#!/bin/sh
+
+# Based on https://gist.github.com/zonque/7d03568eab14a2bb57cb by
+# Daniel Mack github@zonque.org
+
+# "panic: general protection fault" seen:
+# https://people.freebsd.org/~pho/stress/log/sctp.txt
+# Fixed by r350626
+
+kldstat -v | grep -q sctp || kldload sctp.ko
+cat > /tmp/sctp.c <<EOF
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <libgen.h>
+#include <netinet/in.h>
+#include <netinet/sctp.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static int my_port_num;
+
+static void
+die(const char *s)
+{
+ perror(s);
+ exit(1);
+}
+
+ static void
+server(void)
+{
+ struct sctp_sndrcvinfo sndrcvinfo;
+ struct sockaddr_in servaddr = {
+ .sin_family = AF_INET,
+ .sin_addr.s_addr = htonl(INADDR_ANY),
+ .sin_port = htons(my_port_num),
+ };
+ struct sctp_initmsg initmsg = {
+ .sinit_num_ostreams = 5,
+ .sinit_max_instreams = 5,
+ .sinit_max_attempts = 4,
+ };
+ int listen_fd, conn_fd, flags, ret, in;
+
+ listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
+ if (listen_fd < 0)
+ die("socket");
+
+ ret = bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));
+ if (ret < 0)
+ die("bind");
+
+ ret = setsockopt(listen_fd, IPPROTO_SCTP, SCTP_INITMSG, &initmsg,
+ sizeof(initmsg));
+ if (ret < 0)
+ die("setsockopt");
+
+ ret = listen(listen_fd, initmsg.sinit_max_instreams);
+ if (ret < 0)
+ die("listen");
+
+ for (;;) {
+ char buffer[1024];
+
+ printf("Waiting for connection\n");
+ fflush(stdout);
+
+ conn_fd = accept(listen_fd, (struct sockaddr *) NULL, NULL);
+ if(conn_fd < 0)
+ die("accept()");
+
+ printf("New client connected\n");
+ fflush(stdout);
+
+ /* Note that flags is uninitialized here */
+ in = sctp_recvmsg(conn_fd, buffer, sizeof(buffer), NULL, 0,
+ &sndrcvinfo, &flags);
+ if (in > 0) {
+ printf("Received data: %s\n", buffer);
+ fflush(stdout);
+ }
+
+ close(conn_fd);
+ }
+}
+
+static void
+client(void)
+{
+ struct sockaddr_in servaddr = {
+ .sin_family = AF_INET,
+ .sin_port = htons(my_port_num),
+ .sin_addr.s_addr = inet_addr("127.0.0.1"),
+ };
+ int conn_fd, ret;
+ const char *msg = "Hello, Server!";
+
+ conn_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
+ if (conn_fd < 0)
+ die("socket()");
+
+ ret = connect(conn_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));
+ if (ret < 0)
+ die("connect()");
+
+ ret = sctp_sendmsg(conn_fd, (void *) msg, strlen(msg) + 1, NULL, 0, 0, 0, 0, 0, 0 );
+ if (ret < 0)
+ die("sctp_sendmsg");
+
+ close(conn_fd);
+}
+
+int
+main(int argc __unused, char *argv[])
+{
+
+ my_port_num = atoi(argv[1]);
+ if (strstr(basename(argv[0]), "server"))
+ server();
+ else
+ client();
+
+ return (0);
+}
+EOF
+
+cc -o /tmp/server -Wall -Wextra -O2 /tmp/sctp.c || exit
+ln -sf /tmp/server /tmp/client
+
+parallel=100
+for i in `jot $parallel 62324`; do
+ /tmp/server $i > /dev/null &
+done
+(cd ../testcases/swap; ./swap -t 1m -i 20 -l 100) &
+sleep 2
+
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 60 ]; do
+ pids=
+ for i in `jot 50`; do
+ for j in `jot $parallel 62324`; do
+ /tmp/client $j &
+ pids="$pids $!"
+ done
+ done
+ for i in $pids; do
+ wait $i
+ done
+done
+pkill server
+wait
+while pkill swap; do :; done
+wait
+rm -f /tmp/sctp.c /tmp/server /tmp/client
+exit 0
diff --git a/tools/test/stress2/misc/sctp2.sh b/tools/test/stress2/misc/sctp2.sh
new file mode 100755
index 000000000000..45727e03c206
--- /dev/null
+++ b/tools/test/stress2/misc/sctp2.sh
@@ -0,0 +1,169 @@
+#!/bin/sh
+
+# Based on https://gist.github.com/zonque/7d03568eab14a2bb57cb by
+# Daniel Mack github@zonque.org
+
+# Modified version of sctp.sh by Michael Tuexen <tuexen@freebsd.org>:
+# * Use loopback as the address of the server on both side initialized using
+# htonl(INADDR_LOOPBACK).
+# * Negotiate only 1 stream in both directions since only one stream is used.
+# * Don't use initmsg.sinit_max_instreams as an argument in listen(), which
+# does not make sense.
+# Use an arbitrary positive integer, 5 in this case.
+# * Initialize flags before calling sctp_recvmsg().
+
+# "panic: Don't own TCB lock" seen:
+# https://people.freebsd.org/~pho/stress/log/sctp2.txt
+
+# "panic: soclose: SS_NOFDREF on enter" seen:
+# https://people.freebsd.org/~pho/stress/log/sctp2-2.txt
+
+kldstat -v | grep -q sctp || kldload sctp.ko
+cat > /tmp/sctp2.c <<EOF
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <libgen.h>
+#include <netinet/in.h>
+#include <netinet/sctp.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static int my_port_num;
+
+static void
+die(const char *s)
+{
+ perror(s);
+ exit(1);
+}
+
+static void
+server(void)
+{
+ struct sctp_sndrcvinfo sndrcvinfo;
+ struct sockaddr_in servaddr = {
+ .sin_family = AF_INET,
+ .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
+ .sin_port = htons(my_port_num),
+ };
+ struct sctp_initmsg initmsg = {
+ .sinit_num_ostreams = 1,
+ .sinit_max_instreams = 1,
+ .sinit_max_attempts = 4,
+ };
+ int listen_fd, conn_fd, flags, ret, in;
+
+ listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
+ if (listen_fd < 0)
+ die("socket");
+
+ ret = bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));
+ if (ret < 0)
+ die("bind");
+
+ ret = setsockopt(listen_fd, IPPROTO_SCTP, SCTP_INITMSG, &initmsg,
+ sizeof(initmsg));
+ if (ret < 0)
+ die("setsockopt");
+
+ ret = listen(listen_fd, 5);
+ if (ret < 0)
+ die("listen");
+
+ for (;;) {
+ char buffer[1024];
+
+ printf("Waiting for connection\n");
+ fflush(stdout);
+
+ conn_fd = accept(listen_fd, (struct sockaddr *) NULL, NULL);
+ if(conn_fd < 0)
+ die("accept()");
+
+ printf("New client connected\n");
+ fflush(stdout);
+
+ flags = 0;
+ in = sctp_recvmsg(conn_fd, buffer, sizeof(buffer), NULL, 0,
+ &sndrcvinfo, &flags);
+ if (in > 0) {
+ printf("Received data: %s\n", buffer);
+ fflush(stdout);
+ }
+
+ close(conn_fd);
+ }
+}
+
+static void
+client(void)
+{
+ struct sockaddr_in servaddr = {
+ .sin_family = AF_INET,
+ .sin_port = htons(my_port_num),
+ .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
+ };
+ int conn_fd, ret;
+ const char *msg = "Hello, Server!";
+
+ conn_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
+ if (conn_fd < 0)
+ die("socket()");
+
+ ret = connect(conn_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));
+ if (ret < 0)
+ die("connect()");
+
+ ret = sctp_sendmsg(conn_fd, (void *) msg, strlen(msg) + 1, NULL, 0, 0, 0, 0, 0, 0 );
+ if (ret < 0)
+ die("sctp_sendmsg");
+
+ close(conn_fd);
+}
+
+int
+main(int argc __unused, char *argv[])
+{
+
+ my_port_num = atoi(argv[1]);
+ if (strstr(basename(argv[0]), "server"))
+ server();
+ else
+ client();
+
+ return (0);
+}
+EOF
+
+cc -o /tmp/server -Wall -Wextra -O2 /tmp/sctp2.c || exit
+ln -sf /tmp/server /tmp/client
+
+parallel=100
+for i in `jot $parallel 62324`; do
+ /tmp/server $i > /dev/null &
+done
+(cd ../testcases/swap; ./swap -t 1m -i 20 -l 100) &
+sleep 2
+
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 60 ]; do
+ pids=
+ for i in `jot 50`; do
+ for j in `jot $parallel 62324`; do
+ /tmp/client $j &
+ pids="$pids $!"
+ done
+ done
+ for i in $pids; do
+ wait $i
+ done
+done
+pkill server
+wait
+while pkill swap; do :; done
+wait
+rm -f /tmp/sctp2.c /tmp/server /tmp/client
+exit 0
diff --git a/tools/test/stress2/misc/sctp3.sh b/tools/test/stress2/misc/sctp3.sh
new file mode 100755
index 000000000000..74017bb5194f
--- /dev/null
+++ b/tools/test/stress2/misc/sctp3.sh
@@ -0,0 +1,172 @@
+#!/bin/sh
+
+# Based on https://gist.github.com/zonque/7d03568eab14a2bb57cb by
+# Daniel Mack github@zonque.org
+
+# Modified version of sctp.sh by Michael Tuexen <tuexen@freebsd.org>:
+# Basically it is the first test without calling sctp_recvmsg() on
+# the server side and the required cleanups to avoid unused variables.
+# This program triggers pretty quickly the "Queues are not empty when
+# handling SHUTDOWN-COMPLETE" panic. This happened "by accident" with
+# the original sctp.sh, if the flags argument contained the value
+# MSG_DONTWAIT and sctp_recvmsg() returned -1 indicating EAGAIN. This
+# way no successful sctp_recvmsg() call happened.
+
+# "panic: Queues are not empty when handling SHUTDOWN-COMPLETE" seen.
+
+kldstat -v | grep -q sctp || kldload sctp.ko
+cat > /tmp/sctp3.c <<EOF
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <libgen.h>
+#include <netinet/in.h>
+#include <netinet/sctp.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static int my_port_num;
+
+static void
+die(const char *s)
+{
+ perror(s);
+ exit(1);
+}
+
+static void
+server(void)
+{
+#if 0
+ struct sctp_sndrcvinfo sndrcvinfo;
+#endif
+ struct sockaddr_in servaddr = {
+ .sin_family = AF_INET,
+ .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
+ .sin_port = htons(my_port_num),
+ };
+ struct sctp_initmsg initmsg = {
+ .sinit_num_ostreams = 1,
+ .sinit_max_instreams = 1,
+ .sinit_max_attempts = 4,
+ };
+ int listen_fd, conn_fd, ret;
+#if 0
+ int flags, in;
+#endif
+
+ listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
+ if (listen_fd < 0)
+ die("socket");
+
+ ret = bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));
+ if (ret < 0)
+ die("bind");
+
+ ret = setsockopt(listen_fd, IPPROTO_SCTP, SCTP_INITMSG, &initmsg,
+ sizeof(initmsg));
+ if (ret < 0)
+ die("setsockopt");
+
+ ret = listen(listen_fd, 5);
+ if (ret < 0)
+ die("listen");
+
+ for (;;) {
+#if 0
+ char buffer[1024];
+#endif
+ printf("Waiting for connection\n");
+ fflush(stdout);
+
+ conn_fd = accept(listen_fd, (struct sockaddr *) NULL, NULL);
+ if(conn_fd < 0)
+ die("accept()");
+
+ printf("New client connected\n");
+ fflush(stdout);
+
+#if 0
+ flags = 0;
+ in = sctp_recvmsg(conn_fd, buffer, sizeof(buffer), NULL, 0,
+ &sndrcvinfo, &flags);
+ if (in > 0) {
+ printf("Received data: %s\n", buffer);
+ fflush(stdout);
+ }
+#endif
+ close(conn_fd);
+ }
+}
+
+static void
+client(void)
+{
+ struct sockaddr_in servaddr = {
+ .sin_family = AF_INET,
+ .sin_port = htons(my_port_num),
+ .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
+ };
+ int conn_fd, ret;
+ const char *msg = "Hello, Server!";
+
+ conn_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
+ if (conn_fd < 0)
+ die("socket()");
+
+ ret = connect(conn_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));
+ if (ret < 0)
+ die("connect()");
+
+ ret = sctp_sendmsg(conn_fd, (void *) msg, strlen(msg) + 1, NULL, 0, 0, 0, 0, 0, 0 );
+ if (ret < 0)
+ die("sctp_sendmsg");
+
+ close(conn_fd);
+}
+
+int
+main(int argc __unused, char *argv[])
+{
+
+ my_port_num = atoi(argv[1]);
+ if (strstr(basename(argv[0]), "server"))
+ server();
+ else
+ client();
+
+ return (0);
+}
+EOF
+
+cc -o /tmp/server -Wall -Wextra -O2 /tmp/sctp3.c || exit
+ln -sf /tmp/server /tmp/client
+
+parallel=100
+for i in `jot $parallel 62324`; do
+ /tmp/server $i > /dev/null &
+done
+(cd ../testcases/swap; ./swap -t 1m -i 20 -l 100) &
+sleep 2
+
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 60 ]; do
+ pids=
+ for i in `jot 50`; do
+ for j in `jot $parallel 62324`; do
+ /tmp/client $j &
+ pids="$pids $!"
+ done
+ done
+ for i in $pids; do
+ wait $i
+ done
+done
+pkill server
+wait
+while pkill swap; do :; done
+wait
+rm -f /tmp/sctp3.c /tmp/server /tmp/client
+exit 0
diff --git a/tools/test/stress2/misc/seekdir.sh b/tools/test/stress2/misc/seekdir.sh
new file mode 100755
index 000000000000..a6f3a3a6d19f
--- /dev/null
+++ b/tools/test/stress2/misc/seekdir.sh
@@ -0,0 +1,157 @@
+#!/bin/sh
+
+# A regression test for seekdir/telldir
+# submitted by julian@freebsd.org
+# https://reviews.freebsd.org/D2410.
+# Fixed by r282485
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > seekdir.c
+rm -f /tmp/seekdir
+mycc -o seekdir -O2 seekdir.c || exit 1
+rm -f seekdir.c
+cd $odir
+
+mount | grep -q "$mntpoint " && umount -f $mntpoint
+mount -o size=1g -t tmpfs tmpfs $mntpoint
+
+cd $mntpoint
+mkdir test2
+/tmp/seekdir > /dev/null
+[ `echo $mntpoint/test2/* | wc -w` -eq 1 ] ||
+ { echo FAIL; status=1; }
+cd $odir
+
+while mount | grep $mntpoint | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
+rm -f /tmp/seekdir
+exit $status
+EOF
+#include <stdio.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sysexits.h>
+#include <err.h>
+
+#define CHUNKSIZE 5
+#define TOTALFILES 40
+
+static void
+SeekDir(DIR *dirp, long loc)
+{
+ printf("Seeking back to location %ld\n", loc);
+ seekdir(dirp, loc);
+}
+
+static long
+TellDir(DIR *dirp)
+{
+ long loc;
+
+ loc = telldir(dirp);
+ printf("telldir assigned location %ld\n", loc);
+ return (loc);
+}
+
+int
+main(int argc, char *argv[])
+{
+ DIR *dirp;
+ int i;
+ int j;
+ long offset = 0, prev_offset = 0;
+ char *files[100];
+ char filename[100];
+ int fd;
+ struct dirent *dp = NULL;
+
+ if (chdir("./test2") != 0) {
+ err(EX_OSERR, "chdir");
+ }
+
+ /*****************************************************/
+ /* Put a set of sample files in the target directory */
+ /*****************************************************/
+
+ for (i=1; i < TOTALFILES ; i++)
+ {
+ sprintf(filename, "file-%d", i);
+ fd = open(filename, O_CREAT, 0666);
+ if (fd == -1) {
+ err(EX_OSERR, "open");
+ }
+ close(fd);
+ }
+ dirp = opendir(".");
+ offset = TellDir(dirp);
+ for (i = 0; i < 20; i++)
+ files[i] = malloc(20);
+
+ /*******************************************************/
+ /* enumerate and delete small sets of files, one group */
+ /* at a time. */
+ /*******************************************************/
+ do {
+
+ /*****************************************/
+ /* Read in up to CHUNKSIZE file names */
+ /* i will be the number of files we hold */
+ /*****************************************/
+ for (i = 0; i < CHUNKSIZE; i++) {
+ if ((dp = readdir(dirp)) != NULL) {
+ strcpy(files[i], dp->d_name);
+
+ printf("readdir (%ld) returned file %s\n",
+ offset, files[i]);
+
+ prev_offset = offset;
+ offset = TellDir(dirp);
+
+ } else {
+ printf("readdir returned null\n");
+ break;
+ }
+ }
+
+/****************************************************************/
+ /* Simuate the last entry not fitting into our (samba's) buffer */
+ /* If we read someting in on the last slot, push it back */
+ /* Pretend it didn't fit. This is approximately what SAMBA does.*/
+/****************************************************************/
+ if (dp != NULL) {
+ /* Step back */
+ SeekDir(dirp, prev_offset);
+ offset = TellDir(dirp);
+ i--;
+ printf("file %s returned\n", files[i]);
+ }
+
+ /*****************************************/
+ /* i is the number of names we have left.*/
+ /* Delete them. */
+ /*****************************************/
+ for (j = 0; j < i; j++) {
+ if (*files[j] == '.') {
+ printf ("skipping %s\n", files[j]);
+ } else {
+ printf("Unlinking file %s\n", files[j]);
+ if (unlink(files[j]) != 0) {
+ err(EX_OSERR, "unlink");
+ }
+ }
+ }
+ } while (dp != NULL);
+
+ closedir(dirp);
+ //chdir("..");
+
+}
diff --git a/tools/test/stress2/misc/segnp.sh b/tools/test/stress2/misc/segnp.sh
new file mode 100755
index 000000000000..3dbe91e04550
--- /dev/null
+++ b/tools/test/stress2/misc/segnp.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2017 Konstantin Belousov <kib@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Trigger a SIGSEGV/SIGBUS _not_ caused by an access to the unmapped page.
+
+uname -a | egrep -q "i386|amd64" || exit 0
+. ../default.cfg
+
+cat > /tmp/segnp.c <<EOF
+/* $Id: segnp.c,v 1.2 2017/08/12 10:23:28 kostik Exp kostik $ */
+
+#include <stdio.h>
+
+int
+main(void)
+{
+
+ __asm __volatile ("movw %w0,%%ds\n" : : "q" (0x1117) : "memory");
+ printf("Huh ?\n");
+}
+EOF
+mycc -o /tmp/segnp -Wall -Wextra -O2 /tmp/segnp.c || exit 1
+rm /tmp/segnp.c
+
+echo "expect: Bus error (core dumped)"
+(cd /tmp; /tmp/segnp)
+s=$?
+[ $s -ne 138 ] && { echo "Expected 138, got $s", exit 1; }
+
+rm /tmp/segnp /tmp/segnp.core
+exit 0
diff --git a/tools/test/stress2/misc/segregs.sh b/tools/test/stress2/misc/segregs.sh
new file mode 100755
index 000000000000..0ecc3565546a
--- /dev/null
+++ b/tools/test/stress2/misc/segregs.sh
@@ -0,0 +1,144 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2017 Konstantin Belousov <kib@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Maxime Villard <max@m00nbsd.net> spotted this problem:
+# Issue with segment registers on freebsd-i386
+
+# Fixed in r323722
+
+[ `uname -m` = "i386" ] || exit 0
+
+. ../default.cfg
+
+cat > /tmp/mvillard_nest.c <<EOF
+/* $Id: mvillard_nest.c,v 1.2 2017/09/15 14:33:30 kostik Exp kostik $ */
+
+#include <sys/types.h>
+#include <sys/syscall.h>
+#include <sys/ucontext.h>
+#include <machine/atomic.h>
+#include <machine/segments.h>
+#include <machine/sysarch.h>
+#include <err.h>
+#include <pthread.h>
+#include <signal.h>
+#include <time.h>
+#include <string.h>
+#include <unistd.h>
+
+static volatile u_int b, s;
+
+static void *
+dealloc_ldt(void *arg __unused)
+{
+ u_int sl;
+
+ for (;;) {
+ while (atomic_load_acq_int(&b) == 0)
+ ;
+ sl = s;
+ s = 0;
+ if (sl != 0)
+ i386_set_ldt(sl, NULL, 1);
+ atomic_store_rel_int(&b, 0);
+ }
+ return (NULL);
+}
+
+static void
+func(void)
+{
+ union descriptor desc;
+ u_int sel, sl;
+
+ bzero(&desc, sizeof(desc));
+ desc.sd.sd_type = SDT_MEMRWA;
+ desc.sd.sd_dpl = SEL_UPL;
+ desc.sd.sd_p = 1;
+ desc.sd.sd_def32 = 1;
+ desc.sd.sd_gran = 1;
+ desc.sd.sd_lolimit = 0xffff;
+ desc.sd.sd_hilimit = 0xf;
+ sl = i386_set_ldt(LDT_AUTO_ALLOC, &desc, 1);
+ if ((int)sl == -1)
+ err(1, "i386_set_ldt");
+ sel = LSEL(sl, SEL_UPL);
+ s = sl;
+ __asm volatile("movw\t%w0,%%es" : : "r" (sel));
+ atomic_store_rel_int(&b, 1);
+ while (atomic_load_acq_int(&b) != 0)
+ ;
+ getpid();
+}
+
+static void
+sigsegv_handler(int signo __unused, siginfo_t *si __unused, void *rctx)
+{
+ ucontext_t *uc;
+
+ uc = rctx;
+ uc->uc_mcontext.mc_es = uc->uc_mcontext.mc_ds;
+}
+
+int
+main(void)
+{
+ pthread_t thr;
+ time_t start;
+ struct sigaction sa;
+ int error;
+
+ bzero(&sa, sizeof(sa));
+ sa.sa_sigaction = sigsegv_handler;
+ sa.sa_flags = SA_SIGINFO;
+ error = sigaction(SIGSEGV, &sa, NULL);
+ if (error != 0)
+ err(1, "sigaction SIGSEGV");
+ error = sigaction(SIGBUS, &sa, NULL);
+ if (error != 0)
+ err(1, "sigaction SIGBUS");
+
+ error = pthread_create(&thr, NULL, dealloc_ldt, NULL);
+ if (error != 0)
+ errc(1, error, "pthread_create");
+
+ start = time(NULL);
+ while (time(NULL) - start < 120)
+ func();
+}
+EOF
+
+mycc -o /tmp/mvillard_nest -Wall -Wextra -O2 -g /tmp/mvillard_nest.c \
+ -l pthread || exit 1
+rm /tmp/mvillard_nest.c
+
+/tmp/mvillard_nest; s=$?
+
+rm /tmp/mvillard_nest
+exit $s
diff --git a/tools/test/stress2/misc/select.sh b/tools/test/stress2/misc/select.sh
new file mode 100755
index 000000000000..92652b0d41aa
--- /dev/null
+++ b/tools/test/stress2/misc/select.sh
@@ -0,0 +1,168 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# The combination of ualarm() firing before and after the select(2) timeout
+# triggers select() to return EINTR a number of times.
+# Problem only seen on i386.
+
+# Test scenario suggestion by kib@
+
+# "FAIL n = 2389" seen on r302369, no debug build.
+# Fixed by: r302573.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/select.c
+mycc -o select -Wall -Wextra -O0 -g select.c -lpthread || exit 1
+rm -f select.c
+cd $odir
+
+/tmp/select
+s=$?
+
+rm -f /tmp/select
+exit $s
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static pthread_barrier_t barr;
+static sig_atomic_t alarms;
+static int lines;
+
+#define LINES 128000
+#define N 2000 /* also seen fail with N = 20.000 */
+#define PARALLEL 16 /* Fails seen with 1 - 16 */
+#define RUNTIME (10 * 60)
+
+static void
+handler(int i __unused) {
+ alarms++;
+}
+
+static void
+test(void)
+{
+ struct timeval tv;
+ int i, n, r, s;
+
+ r = pthread_barrier_wait(&barr);
+ if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
+ errc(1, r, "pthread_barrier_wait");
+
+ signal(SIGALRM, handler);
+ s = 0;
+ for (i = 0; i < lines; i++) {
+ alarms = 0;
+ if (arc4random() % 100 < 50)
+ ualarm(N / 2, 0);
+ else
+ ualarm(N * 2, 0);
+ tv.tv_sec = 0;
+ tv.tv_usec = N;
+ n = 0;
+ do {
+ r = select(1, NULL, NULL, NULL, &tv);
+ n++;
+ } while (r == -1 && errno == EINTR);
+ if (r == -1)
+ err(1, "select");
+ ualarm(0, 0);
+ if (n > 2) {
+ fprintf(stderr, "FAIL n = %d, tv = %ld.%06ld\n",
+ n, (long)tv.tv_sec, tv.tv_usec);
+ s = 1;
+ break;
+ }
+ if (alarms > 1) {
+ fprintf(stderr, "FAIL alarms = %d\n", (int)alarms);
+ s = 2;
+ break;
+ }
+
+ }
+
+ exit(s);
+}
+
+int
+main(void)
+{
+ pthread_barrierattr_t attr;
+ time_t start;
+ int e, i, j, pids[PARALLEL], r, status;
+
+ lines = LINES / PARALLEL;
+ if (lines == 0)
+ lines = 1;
+ e = 0;
+ if ((r = pthread_barrierattr_init(&attr)) != 0)
+ errc(1, r, "pthread_barrierattr_init");
+ if ((r = pthread_barrierattr_setpshared(&attr,
+ PTHREAD_PROCESS_SHARED)) != 0)
+ errc(1, r, "pthread_barrierattr_setpshared");
+ if ((r = pthread_barrier_init(&barr, &attr, PARALLEL)) != 0)
+ errc(1, r, "pthread_barrier_init");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ waitpid(pids[i], &status, 0);
+ e += status == 0 ? 0 : 1;
+ if (status != 0) {
+ for (j = i + 1; j < PARALLEL; j++)
+ kill(pids[j], SIGINT);
+ }
+ }
+ }
+
+ if ((r = pthread_barrier_destroy(&barr)) > 0)
+ errc(1, r, "pthread_barrier_destroy");
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/select3.sh b/tools/test/stress2/misc/select3.sh
new file mode 100755
index 000000000000..fde3a5c5430a
--- /dev/null
+++ b/tools/test/stress2/misc/select3.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Check if M_SELECT leaks: nselect > threads
+
+m=`vmstat -m | grep -w select | awk '{print $2}'`
+threads=`vmstat -z | sed 's/,/ /g' | grep THREAD | awk '{print $4}'`
+free=`vmstat -z | sed 's/,/ /g' | grep THREAD | awk '{print $5}'`
+[ $m -le $((threads + free)) ] && exit 0
+
+echo FAIL
+vmstat -m | sed -n '1p; /select/p'
+vmstat -z | sed -n '1p;/THREAD/p'
+exit 1
diff --git a/tools/test/stress2/misc/selfd.sh b/tools/test/stress2/misc/selfd.sh
new file mode 100755
index 000000000000..5af48f0ec34c
--- /dev/null
+++ b/tools/test/stress2/misc/selfd.sh
@@ -0,0 +1,152 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# selfd regression test attempt for r285310. Not reproduced.
+# Watchdog fired seen: https://people.freebsd.org/~pho/stress/log/selfd.txt
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/selfd.c
+mycc -o selfd -Wall -Wextra -O0 -g selfd.c -lpthread || exit 1
+rm -f selfd.c
+cd $odir
+
+rm -rf /tmp/stressX.control
+daemon sh -c "(cd ../testcases/swap; ./swap -t 10m -i 20 -l 100)" > \
+ /dev/null 2>&1
+sleep 2
+
+/tmp/selfd
+s=$?
+
+while pgrep -q swap; do
+ pkill -9 swap
+done
+
+rm -rf /tmp/selfd
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/select.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static pthread_barrier_t barr;
+
+#define PARALLEL 16
+#define PIPES 32
+#define RUNTIME (5 * 60)
+
+static void
+handler(int s __unused)
+{
+}
+
+static void
+test(void)
+{
+ fd_set rset, tmpl;
+ struct sigaction sa;
+ struct timeval timeout;
+ time_t start;
+ int fds[PIPES][2], i, n, r;
+
+ r = pthread_barrier_wait(&barr);
+ if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
+ errc(1, r, "pthread_barrier_wait");
+
+ FD_ZERO(&tmpl);
+ for (i = 0; i < PIPES; i++) {
+ if (pipe(fds[i]) == -1)
+ err(1, "pipe()");
+ FD_SET(fds[i][0], &tmpl);
+ }
+ sa.sa_handler = handler;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction(SIGALRM, &sa, NULL) == -1)
+ err(1, "sigaction");
+ start = time(NULL);
+ while ((time(NULL) - start) < 10) {
+ rset = tmpl;
+ timeout.tv_sec = 0;
+ timeout.tv_usec = arc4random() % 10000 + 100;
+ n = arc4random() % PIPES;
+ ualarm(arc4random() % 10000 + 100, 0);
+ write(fds[n][1], "a", 1);
+ if ((n = select(PIPES, &rset, NULL, NULL, &timeout)) < 0)
+ if (errno != EINTR)
+ err(1, "select()");
+ ualarm(0, 0);
+ }
+
+ _exit(0);
+
+}
+
+int
+main(void)
+{
+ pthread_barrierattr_t attr;
+ time_t start;
+ int i, r;
+
+ if ((r = pthread_barrierattr_init(&attr)) != 0)
+ errc(1, r, "pthread_barrierattr_init");
+ if ((r = pthread_barrierattr_setpshared(&attr,
+ PTHREAD_PROCESS_SHARED)) != 0)
+ errc(1, r, "pthread_barrierattr_setpshared");
+ if ((r = pthread_barrier_init(&barr, &attr, PARALLEL)) != 0)
+ errc(1, r, "pthread_barrier_init");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ test();
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sem.sh b/tools/test/stress2/misc/sem.sh
new file mode 100755
index 000000000000..9b229ab662f5
--- /dev/null
+++ b/tools/test/stress2/misc/sem.sh
@@ -0,0 +1,142 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Regression test for panic in semexit_myhook
+# Test scenario by kib@
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > sem.c
+mycc -o sem -Wall sem.c
+rm -f sem.c
+
+cd $RUNDIR/..
+for i in `jot 5`; do
+ /tmp/sem&
+done
+for i in `jot 5`; do
+ wait
+done
+
+rm -f /tmp/sem
+
+exit
+EOF
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/ipc.h>
+#include <sys/msg.h>
+#include <sys/shm.h>
+#include <sys/sem.h>
+#include <signal.h>
+#include <sys/wait.h>
+#include <sched.h>
+#include <errno.h>
+#include <err.h>
+
+int semid = -1;
+key_t semkey;
+struct sembuf sop[2];
+
+size_t pgsize;
+pid_t pid;
+
+void
+random_work(void)
+{
+ int i, n;
+
+ n = (arc4random() % 5000) + 200;
+ for (i = 0; i < n; i++)
+ (void) getpid();
+}
+
+int
+main()
+{
+ int i, j, seed, status;
+
+ seed = getpid();
+ semkey = ftok("/", seed);
+
+ for (i = 0; i < 5000; i++) {
+
+ pid = fork();
+ if (pid == -1) {
+ perror("fork");
+ exit(2);
+ }
+
+ if (pid == 0) { /* child */
+ j = 0;
+ /* get sem */
+ do {
+ sched_yield();
+ semid = semget(semkey, 0, 0);
+ } while (semid == -1 && j++ < 10000);
+ if (semid == -1)
+ exit(1);
+
+ /*
+ * Attempt to acquire the semaphore.
+ */
+ sop[0].sem_num = 0;
+ sop[0].sem_op = -1;
+ sop[0].sem_flg = SEM_UNDO;
+
+ semop(semid, sop, 1);
+ random_work();
+ _exit(0);
+
+ } else { /* parent */
+ /* create sem */
+ if ((semid = semget(semkey, 1, IPC_CREAT | 010640)) == -1)
+ err(1, "semget (%s:%d)", __FILE__, __LINE__);
+ usleep(2000);
+
+ sop[0].sem_num = 0;
+ sop[0].sem_op = 1;
+ sop[0].sem_flg = 0;
+ if (semop(semid, sop, 1) == -1)
+ warn("init: semop (%s:%d)", __FILE__, __LINE__);
+
+ random_work();
+ if (semctl(semid, 0, IPC_RMID, 0) == -1 && errno != EINVAL)
+ warn("shmctl IPC_RMID (%s:%d)", __FILE__, __LINE__);
+
+ }
+ waitpid(pid, &status, 0);
+ }
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sem_post.sh b/tools/test/stress2/misc/sem_post.sh
new file mode 100755
index 000000000000..2f1d7c4c004f
--- /dev/null
+++ b/tools/test/stress2/misc/sem_post.sh
@@ -0,0 +1,107 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# No problems seen.
+
+cat > /tmp/sem_post.c <<EOF
+#include <err.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static sem_t semaphore;
+
+static void *
+threadfunc(void *data __unused) {
+
+ for (;;) {
+ usleep(arc4random() % 100);
+ if (sem_wait(&semaphore) == -1)
+ err(1, "sem_wait");
+ usleep(arc4random() % 100);
+ if (arc4random() % 100 < 3)
+ sleep(1);
+ if (sem_post(&semaphore) == -1)
+ err(1, "sem_post");
+ }
+
+ return (NULL);
+}
+
+int
+main(void) {
+ pthread_t mythread;
+ time_t start;
+ int r;
+
+ // initialize semaphore and set value to 1
+ if (sem_init(&semaphore, 0, 1) == -1)
+ err(1, "sem_init");
+
+ r = pthread_create(&mythread, NULL, threadfunc, NULL);
+ if (r != 0)
+ errc(1, r, "pthread_create");
+
+ usleep(50);
+ alarm(300);
+ start = time(NULL);
+ while (time(NULL) - start < 120) {
+ usleep(arc4random() % 100);
+ if (sem_wait(&semaphore) == -1)
+ err(1, "sem_wait");
+ usleep(arc4random() % 100);
+ if (arc4random() % 100 < 3)
+ sleep(1);
+ if (sem_post(&semaphore) == -1)
+ err(1, "sem_post");
+ }
+ if ((r = pthread_kill(mythread, SIGINT)) != 0)
+ errc(1, r, "pthread_kill");
+ if ((r = pthread_join(mythread, NULL)) != 0)
+ errc(1, r, "pthread_join");
+
+ return (0);
+}
+EOF
+cc -o /tmp/sem_post -Wall -Wextra -O2 /tmp/sem_post.c -lpthread || exit 1
+(cd ../testcases/swap; ./swap -t 3m -i 30 -h -l 100) &
+sleep 20
+for i in `jot 128`; do
+ /tmp/sem_post > /dev/null &
+done
+while pgrep -q sem_post; do sleep .5; done
+while pkill swap; do :; done
+wait
+rm -f /tmp/sem_post /tmp/sem_post.c
+exit 0
diff --git a/tools/test/stress2/misc/sem_timedwait.sh b/tools/test/stress2/misc/sem_timedwait.sh
new file mode 100755
index 000000000000..33e5c1933f37
--- /dev/null
+++ b/tools/test/stress2/misc/sem_timedwait.sh
@@ -0,0 +1,161 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# No problems seen.
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sem_timedwait.c
+mycc -o sem_timedwait -Wall -Wextra -O0 -g sem_timedwait.c || exit 1
+rm -f sem_timedwait.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+newfs $newfs_flags md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -h -l 100) > /dev/null &
+cd $mntpoint
+nice $dir/sem_timedwait
+s=$?
+[ -f sem_timedwait.core -a $s -eq 0 ] &&
+ { ls -l sem_timedwait.core; mv sem_timedwait.core $dir; s=1; }
+cd $odir
+
+while pkill swap; do :; done
+wait
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/sem_timedwait
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <semaphore.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#define PARALLEL 64
+#define RUNTIME (5 * 60)
+#define SYNC 0
+
+static void
+test(void)
+{
+ struct timespec tm;
+ sem_t sem;
+ int i;
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ sem_init(&sem, 0, 0);
+ i = 0;
+ do {
+ clock_gettime(CLOCK_REALTIME, &tm);
+ tm.tv_nsec += 1000;
+ if (tm.tv_nsec >= 1000000000L) {
+ tm.tv_nsec -= 1000000000L;
+ tm.tv_sec++;
+ }
+
+ if (++i == 1000)
+ sem_post(&sem);
+ else
+ usleep(10000);
+ } while (sem_timedwait(&sem, &tm) == -1);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/sem_wait.sh b/tools/test/stress2/misc/sem_wait.sh
new file mode 100755
index 000000000000..6d8bc49c9199
--- /dev/null
+++ b/tools/test/stress2/misc/sem_wait.sh
@@ -0,0 +1,109 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# "panic: vm_page_free_prep: freeing mapped page 0x657936c" seen.
+# https://people.freebsd.org/~pho/stress/log/sem_wait.txt
+# Fixed by r350005
+
+. ../default.cfg
+
+cat > /tmp/sem_wait.c <<EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static sem_t *semaphore;
+
+static void *
+test(void) {
+
+ setproctitle("%s", __func__);
+ alarm(300);
+ for (;;) {
+ if (sem_wait(semaphore) == -1)
+ err(1, "sem_wait");
+ if (sem_post(semaphore) == -1)
+ err(1, "sem_post");
+ }
+}
+
+int
+main(void) {
+ pid_t pid;
+ size_t len;
+ time_t start;
+
+ setproctitle("%s", __func__);
+ alarm(300);
+ len = PAGE_SIZE;
+ if ((semaphore = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ // initialize semaphore and set value to 1
+ if (sem_init(semaphore, 1, 1) == -1)
+ err(1, "sem_init");
+
+ if ((pid = fork()) == 0)
+ test();
+ if (pid == -1)
+ err(1, "fork()");
+
+ usleep(50);
+ alarm(300);
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ if (sem_wait(semaphore) == -1)
+ err(1, "sem_wait");
+ if (sem_post(semaphore) == -1)
+ err(1, "sem_post");
+ }
+ kill(pid, SIGHUP);
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid()");
+
+ return (0);
+}
+EOF
+mycc -o /tmp/sem_wait -Wall -Wextra -O2 /tmp/sem_wait.c || exit 1
+timeout 6m /tmp/sem_wait; s=$?
+[ $s -eq 124 ] && echo "Timed out"
+rm -f /tmp/sem_wait /tmp/sem_wait.c
+exit $s
diff --git a/tools/test/stress2/misc/sendfile.sh b/tools/test/stress2/misc/sendfile.sh
new file mode 100755
index 000000000000..408564bef0e2
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile.sh
@@ -0,0 +1,213 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Test scenario for sendfile livelock seen on 7.2-STABLE for non SMP
+
+# Scenario by kib@
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > sendfile.c
+mycc -o sendfile -Wall sendfile.c -pthread
+rm -f sendfile.c
+[ -d "$RUNDIR" ] || mkdir -p $RUNDIR
+cd $RUNDIR
+
+in=inputFile
+out=outputFile
+
+for i in 1 2 3 4 8 16 1k 2k 3k 4k 5k 1m 2m 3m 4m 5m ; do
+ rm -f $in $out
+ dd if=/dev/random of=$in bs=$i count=1 status=none
+ /tmp/sendfile $in $out 12345
+ cmp $in $out || { echo FAIL; ls -l $in $out; }
+ rm -f $in $out
+done
+rm -f /tmp/sendfile
+exit
+EOF
+#include <err.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+int port;
+char *inputFile;
+char *outputFile;
+int bufsize = 4096;
+
+static void
+reader(void) {
+ int tcpsock, msgsock;
+ int on;
+ socklen_t len;
+ struct sockaddr_in inetaddr, inetpeer;
+ int n, t, *buf, fd;
+
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ t = 0;
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+
+ if ((fd = open(outputFile, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1, "open(%s)", outputFile);
+
+ for (;;) {
+ if ((n = read(msgsock, buf, bufsize)) < 0)
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ t += n;
+ if (n == 0)
+ break;
+
+ if ((write(fd, buf, n)) != n)
+ err(1, "write");
+ }
+ close(msgsock);
+ close(fd);
+ return;
+}
+
+static void
+writer(void) {
+ int tcpsock, on;
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ struct stat statb;
+ int i, r, fd;
+ off_t off = 0;
+#if 1
+ size_t size;
+#endif
+
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+#if 1 /* livelock trigger */
+ size = getpagesize() -4;
+ if (setsockopt(tcpsock, SOL_SOCKET, SO_SNDBUF, (void *)&size,
+ sizeof(size)) < 0)
+ err(1, "setsockopt(SO_SNDBUF), %s:%d",
+ __FILE__, __LINE__);
+#endif
+
+ hostent = gethostbyname ("localhost");
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if (stat(inputFile, &statb) != 0)
+ err(1, "stat(%s)", inputFile);
+
+ if ((fd = open(inputFile, O_RDONLY)) == -1)
+ err(1, "open(%s)", inputFile);
+
+ if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1)
+ err(1, "sendfile");
+
+ return;
+}
+
+int
+main(int argc, char **argv)
+{
+ pid_t pid;
+
+ if (argc != 4) {
+ fprintf(stderr, "Usage: %s <inputFile outputFile portNumber\n",
+ argv[0]);
+ return (1);
+ }
+ inputFile = argv[1];
+ outputFile = argv[2];
+ port = atoi(argv[3]);
+
+ if ((pid = fork()) == 0) {
+ writer();
+ exit(EXIT_SUCCESS);
+ } else if (pid > 0) {
+ reader();
+ kill(pid, SIGINT);
+ } else
+ err(1, "fork(), %s:%d", __FILE__, __LINE__);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sendfile10.sh b/tools/test/stress2/misc/sendfile10.sh
new file mode 100755
index 000000000000..0bfc4a83da71
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile10.sh
@@ -0,0 +1,313 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Copy of sendfile8.sh with size validation added.
+
+# No problems seen (after r315910).
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile10.c
+mycc -o sendfile10 -Wall -Wextra -O0 -g sendfile10.c || exit 1
+rm -f sendfile10.c
+cd $odir
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags -n md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+cd $mntpoint
+dd if=/dev/random of=template bs=1m count=50 status=none
+/tmp/sendfile10 template in out 76543
+s=$?
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1
+mdconfig -d -u $mdstart
+rm -rf /tmp/sendfile10
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile off_t *share;
+static int port;
+static char *input, *output;
+
+#define BUFSIZE 4096
+#define MX (100 * 1024 * 1024)
+#define OSZ 1
+#define PARALLEL 1
+#define RUNTIME (2 * 60)
+#define SZ 0
+
+static void
+mess(void)
+{
+ off_t length;
+ int fd;
+
+ if ((fd = open(input, O_RDWR)) == -1)
+ err(1, "open(%s)", input);
+ length = arc4random() % MX;
+ if (ftruncate(fd, length) == -1)
+ err(1, "truncate(%jd)", length);
+ share[SZ] = length;
+ close(fd);
+}
+
+static void
+reader(void) {
+ off_t t;
+ int tcpsock, msgsock;
+ int on;
+ socklen_t len;
+ struct sockaddr_in inetaddr, inetpeer;
+ int n, *buf, fd;
+
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ len = sizeof(inetpeer);
+ alarm(10);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+ alarm(0);
+
+ t = 0;
+ if ((buf = malloc(BUFSIZE)) == NULL)
+ err(1, "malloc(%d), %s:%d", BUFSIZE, __FILE__, __LINE__);
+
+ if ((fd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1, "open(%s)", output);
+
+ for (;;) {
+ if ((n = read(msgsock, buf, BUFSIZE)) < 0)
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ t += n;
+ if (n == 0) break;
+
+ if ((write(fd, buf, n)) != n)
+ err(1, "write");
+ }
+ close(msgsock);
+ close(fd);
+#if 0
+ if (t != share[SZ] && t != share[OSZ]) {
+ fprintf(stderr, "1) Send size %lu, original size %lu, "
+ "receive size %lu\n",
+ (unsigned long)share[SZ],
+ (unsigned long)share[OSZ],
+ (unsigned long)t);
+ exit(1);
+ }
+#endif
+ return;
+}
+
+static void
+writer(void) {
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ struct stat statb;
+ off_t off = 0;
+ size_t size;
+ int i, r, fd;
+ int tcpsock, on;
+
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ size = getpagesize() -4;
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_SNDBUF, (void *)&size, sizeof(size)) < 0)
+ err(1, "setsockopt(SO_SNDBUF), %s:%d", __FILE__,
+ __LINE__);
+
+ hostent = gethostbyname ("localhost");
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if ((fd = open(input, O_RDONLY)) == -1)
+ err(1, "open(%s)", input);
+
+ if (fstat(fd, &statb) != 0)
+ err(1, "stat(%s)", input);
+ share[SZ] = statb.st_size;
+ if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1)
+ err(1, "sendfile");
+ close(fd);
+ close(tcpsock);
+
+ return;
+}
+
+static void
+test(void)
+{
+ pid_t pid;
+
+ if ((pid = fork()) == 0) {
+ writer();
+ _exit(0);
+ }
+ reader();
+ kill(pid, SIGINT);
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct stat statin, statorig, statout;
+ size_t len;
+ time_t start;
+ int e, i, pids[PARALLEL], status;
+ char help[80], *template;
+
+ if (argc != 5) {
+ fprintf(stderr, "Usage: %s <template> <input file> "
+ "<output file> <port>\n", argv[0]);
+ exit(1);
+ }
+ template = argv[1];
+ input = argv[2];
+ output = argv[3];
+ port = atoi(argv[4]);
+ snprintf(help, sizeof(help), "cp %s %s", template, input);
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ system(help);
+ if (stat(input, &statin) == -1)
+ err(1, "stat(%s)", input);
+ share[OSZ] = statin.st_size;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ usleep(arc4random() % 10000);
+ mess();
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ e += status == 0 ? 0 : 1;
+ }
+ if (stat(template, &statorig) == -1)
+ err(1, "stat(%s)", input);
+ if (stat(input, &statin) == -1)
+ err(1, "stat(%s)", input);
+ if (stat(output, &statout) == -1)
+ err(1, "stat(%s)", output);
+ if (statout.st_size >= MX) {
+ fprintf(stderr, "Send size %lu, original size %lu, "
+ "receive size %lu\n",
+ (unsigned long)statin.st_size,
+ (unsigned long)statorig.st_size,
+ (unsigned long)statout.st_size);
+ system("ls -l | grep -v total");
+ exit(1);
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/sendfile11.sh b/tools/test/stress2/misc/sendfile11.sh
new file mode 100755
index 000000000000..439416f063f0
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile11.sh
@@ -0,0 +1,235 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# sendfile(2) && block size > page size:
+# panic: vnode_pager_generic_getpages: sector size 8192 too large
+# https://people.freebsd.org/~pho/stress/log/sendfile11.txt
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > sendfile11.c
+mycc -o sendfile11 -Wall sendfile11.c -pthread || exit 1
+rm -f sendfile11.c
+
+set -e
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+
+dd if=/dev/random of=/tmp/geli.key bs=64 count=1 status=none
+echo test | geli init -s 8192 -J - -K /tmp/geli.key /dev/md$mdstart > /dev/null
+echo test | geli attach -j - -k /tmp/geli.key /dev/md$mdstart
+newfs $newfs_flags /dev/md$mdstart.eli > /dev/null
+
+mount /dev/md${mdstart}.eli $mntpoint
+chmod 777 $mntpoint
+set +e
+
+cd $mntpoint
+dd if=/dev/zero of=file bs=1m count=512 status=none
+(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -h -l 100) &
+sleep 5
+/tmp/sendfile11 file output 12345; s=$?
+cd $odir
+while pkill swap; do
+ sleep 1
+done
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/md${mdstart}.eli || s=$?
+geli kill /dev/md$mdstart.eli
+mdconfig -d -u $mdstart
+rm -f /tmp/geli.key /tmp/sendfile11
+exit $s
+EOF
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define BUFSIZE 8192
+
+static int port;
+static char *inputFile;
+static char *outputFile;
+
+static void
+reader(void) {
+ struct sockaddr_in inetaddr, inetpeer;
+ socklen_t len;
+ int tcpsock, msgsock;
+ int *buf, fd, n, on, t;
+
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ t = 0;
+ if ((buf = malloc(BUFSIZE)) == NULL)
+ err(1, "malloc(%d), %s:%d", BUFSIZE, __FILE__, __LINE__);
+
+ if ((fd = open(outputFile, O_RDWR | O_CREAT | O_TRUNC, 0640)) ==
+ -1)
+ err(1, "open(%s)", outputFile);
+
+ for (;;) {
+ if ((n = read(msgsock, buf, BUFSIZE)) < 0)
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ t += n;
+ if (n == 0) break;
+
+ if ((write(fd, buf, n)) != n)
+ err(1, "write");
+ }
+ close(msgsock);
+ close(fd);
+ return;
+}
+
+static void
+writer(void) {
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ struct stat statb;
+ off_t off = 0;
+ size_t size;
+ int i, fd, on, r, tcpsock;
+
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ size = getpagesize() -4;
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_SNDBUF, (void *)&size, sizeof(size)) < 0)
+ err(1, "setsockopt(SO_SNDBUF), %s:%d", __FILE__,
+ __LINE__);
+
+ hostent = gethostbyname ("localhost");
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if (stat(inputFile, &statb) != 0)
+ err(1, "stat(%s)", inputFile);
+
+ if ((fd = open(inputFile, O_RDONLY)) == -1)
+ err(1, "open(%s)", inputFile);
+
+ if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1)
+ err(1, "sendfile");
+
+ return;
+}
+
+int
+main(int argc, char **argv)
+{
+ pid_t pid;
+ int e, s;
+
+ if (argc != 4) {
+ fprintf(stderr,
+ "Usage: %s <inputFile outputFile portNumber\n", argv[0]);
+ return (1);
+ }
+ e = 0;
+ inputFile = argv[1];
+ outputFile = argv[2];
+ port = atoi(argv[3]);
+
+ if ((pid = fork()) == 0) {
+ writer();
+ exit(EXIT_SUCCESS);
+ } else if (pid > 0) {
+ reader();
+ kill(pid, SIGINT);
+ waitpid(pid, &s, 0);
+ if (s != 0)
+ e = 1;
+ } else
+ err(1, "fork(), %s:%d", __FILE__, __LINE__);
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/sendfile12.sh b/tools/test/stress2/misc/sendfile12.sh
new file mode 100755
index 000000000000..05f30c9a292c
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile12.sh
@@ -0,0 +1,241 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Test scenario suggestion by alc@
+
+# sendfile(2) using SF_NOCACHE and a mmap'ed file with touched pages.
+# "panic: vm_page_free_toq: freeing mapped page 0xc49e53bc" seen:
+# https://people.freebsd.org/~pho/stress/log/sendfile12.txt
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > sendfile12.c
+mycc -o sendfile12 -Wall sendfile12.c -pthread || exit 1
+rm -f sendfile12.c
+
+set -e
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+newfs $newfs_flags /dev/md$mdstart > /dev/null
+
+mount /dev/md${mdstart} $mntpoint
+chmod 777 $mntpoint
+set +e
+
+cd $mntpoint
+dd if=/dev/zero of=file bs=1m count=512 status=none
+(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -h -l 100) > /dev/null &
+sleep 5
+/tmp/sendfile12 file output 12345; s=$?
+cd $odir
+while pkill swap; do
+ sleep 1
+done
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/md${mdstart} || s=$?
+mdconfig -d -u $mdstart
+rm -f /tmp/sendfile12
+exit $s
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define BUFSIZE 8192
+
+static int port;
+static char *inputFile;
+static char *outputFile;
+
+static void
+reader(void) {
+ struct sockaddr_in inetaddr, inetpeer;
+ socklen_t len;
+ int tcpsock, msgsock;
+ int *buf, fd, n, on, t;
+
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ t = 0;
+ if ((buf = malloc(BUFSIZE)) == NULL)
+ err(1, "malloc(%d), %s:%d", BUFSIZE, __FILE__, __LINE__);
+
+ if ((fd = open(outputFile, O_RDWR | O_CREAT | O_TRUNC, 0640)) ==
+ -1)
+ err(1, "open(%s)", outputFile);
+
+ for (;;) {
+ if ((n = read(msgsock, buf, BUFSIZE)) < 0)
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ t += n;
+ if (n == 0) break;
+
+ if ((write(fd, buf, n)) != n)
+ err(1, "write");
+ }
+ close(msgsock);
+ close(fd);
+ return;
+}
+
+static void
+writer(void) {
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ struct stat statb;
+ off_t off = 0;
+ size_t size;
+ int i, fd, on, r, tcpsock;
+ char *cp;
+
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ size = getpagesize() -4;
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_SNDBUF, (void *)&size, sizeof(size)) < 0)
+ err(1, "setsockopt(SO_SNDBUF), %s:%d", __FILE__,
+ __LINE__);
+
+ hostent = gethostbyname ("localhost");
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if (stat(inputFile, &statb) != 0)
+ err(1, "stat(%s)", inputFile);
+
+ if ((fd = open(inputFile, O_RDWR)) == -1)
+ err(1, "open(%s)", inputFile);
+
+ if ((cp = mmap(NULL, statb.st_size, PROT_READ | PROT_WRITE,
+ MAP_SHARED, fd, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ cp[0] = 1;
+
+ if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, SF_NOCACHE)
+ == -1)
+ err(1, "sendfile");
+
+ return;
+}
+
+int
+main(int argc, char **argv)
+{
+ pid_t pid;
+ int e, s;
+
+ if (argc != 4) {
+ fprintf(stderr,
+ "Usage: %s <inputFile outputFile portNumber\n", argv[0]);
+ return (1);
+ }
+ e = 0;
+ inputFile = argv[1];
+ outputFile = argv[2];
+ port = atoi(argv[3]);
+
+ if ((pid = fork()) == 0) {
+ writer();
+ exit(EXIT_SUCCESS);
+ } else if (pid > 0) {
+ reader();
+ kill(pid, SIGINT);
+ waitpid(pid, &s, 0);
+ if (s != 0)
+ e = 1;
+ } else
+ err(1, "fork(), %s:%d", __FILE__, __LINE__);
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/sendfile13.sh b/tools/test/stress2/misc/sendfile13.sh
new file mode 100755
index 000000000000..9a7b0ad40003
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile13.sh
@@ -0,0 +1,371 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# Move files between two FSs using sendfile(2).
+# CG checksum errors reported.
+
+# Unrelated gpart page fault seen:
+# https://people.freebsd.org/~pho/stress/log/sendfile13.txt
+# Fixed by r329262
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile13.c
+mycc -o sendfile13 -Wall -Wextra -O0 -g sendfile13.c || exit 1
+rm -f sendfile13.c
+cd $odir
+
+set -e
+size="$((`sysctl -n hw.usermem` / 2 / 1024 / 1024 / 1024))"
+size="$((size * 7 / 10))g"
+[ "$size" = "0g" ] && exit 0
+[ "$newfs_flags" = "-U" ] || exit 0
+newfs_flags="-j"
+
+mp1=$mntpoint
+mkdir -p $mp1
+md1=$mdstart
+mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1
+[ -c /dev/md$md1 ] && mdconfig -d -u $md1
+mdconfig -a -t swap -s $size -u $md1
+bsdlabel -w md$md1 auto
+newfs $newfs_flags -n md${md1}$part > /dev/null 2>&1
+mount /dev/md${md1}$part $mp1
+
+md2=$((mdstart + 1))
+mp2=${mntpoint}$md2
+mkdir -p $mp2
+mount | grep "on $mp2 " | grep -q /dev/md && umount -f $mp2
+[ -c /dev/md$md2 ] && mdconfig -d -u $md2
+mdconfig -a -t swap -s $size -u $md2
+bsdlabel -w md$md2 auto
+newfs $newfs_flags -n md${md2}$part > /dev/null 2>&1
+mount /dev/md${md2}$part $mp2
+set +e
+
+free=`df $mp1 | tail -1 | awk '{print $4}'`
+$dir/sendfile13 5432 $mp1 $mp2 $((free / 2)) &
+p1=$!
+$dir/sendfile13 5433 $mp2 $mp1 $((free / 2)) &
+p2=$!
+cd $odir
+s=0
+wait $p1; code=$?
+[ $code -ne 0 ] && { s=$code; echo "$p1 exit status $code"; }
+wait $p2; code=$?
+[ $code -ne 0 ] && { s=$code; echo "$p2 exit status $code"; }
+[ -f sendfile13.core -a $s -eq 0 ] &&
+ { ls -l sendfile13.core; mv sendfile13.core /tmp; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mp1 " || break
+ umount $mp1 && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mp1; exit 1; }
+done
+for i in `jot 6`; do
+ mount | grep -q "on $mp2 " || break
+ umount $mp2 && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mp2; exit 1; }
+done
+checkfs /dev/md${md1}$part || s=1
+checkfs /dev/md${md2}$part || s=1
+mdconfig -d -u $md1 || s=1
+mdconfig -d -u $md2 || s=1
+
+for i in `jot 6`; do
+ mount | grep -q "on $mp2 " || break
+ umount $mp2 && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mp2; exit 1; }
+done
+checkfs /dev/md${md2}$part || s=1
+
+rm -rf $dir/sendfile13
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define BUFSIZE 8192
+#define MAXTHREADS 32
+
+static int files, port;
+static char *fromdir, *todir;
+
+static void
+create(char *path, size_t size)
+{
+ size_t s;
+ int fd, i, ifd;
+ char *cp, file[128], help[128];
+
+ i = 0;
+ while (size > 0) {
+ do {
+ s =arc4random() % size + 1;
+ } while (s > 1024 * 1024 * 1024);
+ size -= s;
+ sprintf(file, "%s/f%06d.%06d", path, getpid(), i++);
+ if ((ifd = open("/dev/zero", O_RDONLY)) == -1)
+ err(1, "open(/dev/zero)");
+ if ((cp = mmap(0, s, PROT_READ, MAP_SHARED, ifd, 0)) ==
+ (caddr_t) - 1)
+ err(1, "mmap error for input");
+ if ((fd = open(file, O_WRONLY | O_CREAT, 0640)) == -1)
+ err(1, "create(%s)", file);
+ if (write(fd, cp, s) != (ssize_t)s)
+ err(1, "write(%s)", file);
+ munmap(cp, s);
+ close(fd);
+ close(ifd);
+ files++;
+ }
+ snprintf(help, sizeof(help),
+ "umount %s 2>&1 | grep -v 'Device busy'", path);
+ system(help);
+#if defined(DEBUG)
+ fprintf(stderr, "%d files created\n", files);
+#endif
+}
+
+static void
+server(void)
+{
+ pid_t pid;
+ struct sigaction sa;
+ struct sockaddr_in inetaddr, inetpeer;
+ socklen_t len;
+ int tcpsock, msgsock;
+ int *buf, fd, idx, n, on, t;
+ char ofile[128], nfile[128];
+
+ sa.sa_handler = SIG_IGN;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction(SIGCHLD, &sa, 0) == -1)
+ err(1, "sigaction");
+
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ idx = 0;
+ len = sizeof(inetpeer);
+ for (;;) {
+ alarm(120);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ if ((pid = fork()) == 0) {
+ alarm(120);
+ t = 0;
+ if ((buf = malloc(BUFSIZE)) == NULL)
+ err(1, "malloc(%d), %s:%d", BUFSIZE,
+ __FILE__, __LINE__);
+
+ sprintf(ofile, "%s/g%06d.%06d", todir, getpid(),
+ idx);
+ sprintf(nfile, "%s/n%06d.%06d", todir, getpid(),
+ idx);
+ if ((fd = open(ofile, O_RDWR | O_CREAT | O_TRUNC,
+ 0640)) == -1)
+ err(1, "open(%s)", ofile);
+
+ for (;;) {
+ if ((n = read(msgsock, buf, BUFSIZE)) < 0)
+ err(1, "read(), %s:%d", __FILE__,
+ __LINE__);
+ t += n;
+ if (n == 0) break;
+
+ if ((write(fd, buf, n)) != n)
+ err(1, "write n=%d", n);
+ }
+ close(msgsock);
+ close(fd);
+ if (rename(ofile, nfile) != 0)
+ err(1, "rename(%s, %s)", ofile, nfile);
+ _exit(0);
+ }
+ close(msgsock);
+ if (++idx == files)
+ break;
+ }
+
+ _exit(0);
+}
+
+static void
+writer(char *inputFile) {
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ struct stat statb;
+ off_t off = 0;
+ size_t size;
+ int i, fd, on, r, tcpsock;
+
+ alarm(120);
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ size = getpagesize();
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_SNDBUF, (void *)&size, sizeof(size)) < 0)
+ err(1, "setsockopt(SO_SNDBUF), %s:%d", __FILE__,
+ __LINE__);
+
+ hostent = gethostbyname ("localhost");
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if (stat(inputFile, &statb) != 0)
+ err(1, "stat(%s)", inputFile);
+
+ if ((fd = open(inputFile, O_RDWR)) == -1)
+ err(1, "open(%s)", inputFile);
+
+ if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off,
+ SF_NOCACHE) == -1)
+ warn("sendfile()");
+ close(fd);
+
+ return;
+}
+
+static void
+move(int num)
+{
+ char ifile[128];
+
+ sprintf(ifile, "%s/f%06d.%06d", fromdir, getpid(), num);
+ writer(ifile);
+
+ if (unlink(ifile) != 0)
+ err(1, "unlink(%s)", ifile);
+}
+
+int
+main(int argc, char *argv[])
+{
+ pid_t spid;
+ size_t size;
+ int i, status;
+
+ if (argc != 5) {
+ fprintf(stderr,
+ "Usage %s <port> <from dir> <to dir> <size in k>",
+ argv[0]);
+ exit(1);
+ }
+ port = atoi(argv[1]);
+ fromdir = argv[2];
+ if (chdir(fromdir) == -1)
+ err(1, "chdir(%s)", fromdir);
+ todir = argv[3];
+ sscanf(argv[4], "%zd", &size);
+ size = size * 1024;
+ create(fromdir, size);
+
+ if ((spid = fork()) == 0)
+ server();
+
+ for (i = 0; i < files; i++) {
+ move(i);
+ sleep(10);
+ }
+ if (waitpid(spid, &status, 0) != spid)
+ err(1, "waitpid");
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ spid, WTERMSIG(status));
+ }
+
+ return (status == 0 ? 0 : 1);
+}
diff --git a/tools/test/stress2/misc/sendfile14.sh b/tools/test/stress2/misc/sendfile14.sh
new file mode 100755
index 000000000000..00fc8cd01b99
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile14.sh
@@ -0,0 +1,382 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# Regression test scenario attempt for r328914:
+# "Occasional cylinder-group check-hash errors were being reported on
+# systems running with a heavy filesystem load."
+
+# Assert seen in WiP code:
+# https://people.freebsd.org/~pho/stress/log/mmacy016.txt
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile14.c
+mycc -o sendfile14 -Wall -Wextra -O0 -g sendfile14.c -lpthread || exit 1
+rm -f sendfile14.c
+cd $odir
+
+set -e
+size="$((`sysctl -n hw.usermem` / 2 / 1024 / 1024 / 1024))"
+size="$((size * 8 / 10))g"
+[ "$size" = "0g" ] && exit 0
+[ "$newfs_flags" = "-U" ] || exit 0
+newfs_flags="-j"
+
+mp1=$mntpoint
+mkdir -p $mp1
+md1=$mdstart
+mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1
+[ -c /dev/md$md1 ] && mdconfig -d -u $md1
+mdconfig -a -t swap -s $size -u $md1
+bsdlabel -w md$md1 auto
+newfs $newfs_flags -n md${md1}$part > /dev/null 2>&1
+mount /dev/md${md1}$part $mp1
+
+md2=$((mdstart + 1))
+mp2=${mntpoint}$md2
+mkdir -p $mp2
+mount | grep "on $mp2 " | grep -q /dev/md && umount -f $mp2
+[ -c /dev/md$md2 ] && mdconfig -d -u $md2
+mdconfig -a -t swap -s $size -u $md2
+bsdlabel -w md$md2 auto
+newfs $newfs_flags -n md${md2}$part > /dev/null 2>&1
+mount /dev/md${md2}$part $mp2
+set +e
+
+free=`df $mp1 | tail -1 | awk '{print $4}'`
+$dir/sendfile14 5432 $mp1 $mp2 $((free / 2)) &
+$dir/sendfile14 5433 $mp2 $mp1 $((free / 2)) &
+cd $odir
+wait
+s=0
+[ -f sendfile14.core -a $s -eq 0 ] &&
+ { ls -l sendfile14.core; mv sendfile14.core /tmp; s=1; }
+pkill sendfile14
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mp1 " || break
+ umount $mp1 && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mp1; exit 1; }
+done
+for i in `jot 6`; do
+ mount | grep -q "on $mp2 " || break
+ umount $mp2 && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mp2; exit 1; }
+done
+checkfs /dev/md${md1}$part || s=1
+checkfs /dev/md${md2}$part || s=1
+mdconfig -d -u $md1 || s=1
+mdconfig -d -u $md2 || s=1
+
+rm -rf $dir/sendfile14
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define BUFSIZE 8192
+#define MAXTHREADS 5
+
+static volatile int active;
+static volatile u_int *share;
+static int files, port;
+static char *fromdir, *todir;
+
+void
+create(char *path, size_t size)
+{
+ size_t s;
+ int fd, i, ifd;
+ char *cp, file[128], help[128];
+
+ setproctitle("%s", __func__);
+ i = 0;
+ while (size > 0) {
+ do {
+ s =arc4random() % size + 1;
+ } while (s > 1024 * 1024 * 1024);
+ size -= s;
+ sprintf(file, "%s/f%06d.%06d", path, getpid(), i++);
+ if ((ifd = open("/dev/zero", O_RDONLY)) == -1)
+ err(1, "open(/dev/zero)");
+ if ((cp = mmap(0, s, PROT_READ, MAP_SHARED, ifd, 0)) ==
+ (caddr_t) - 1)
+ err(1, "mmap error for input");
+ if ((fd = open(file, O_WRONLY | O_CREAT, 0640)) == -1)
+ err(1, "create(%s)", file);
+ if (write(fd, cp, s) != (ssize_t)s)
+ err(1, "write(%s)", file);
+ munmap(cp, s);
+ close(fd);
+ close(ifd);
+ files++;
+ }
+ snprintf(help, sizeof(help),
+ "umount %s 2>&1 | grep -v 'Device busy'", path);
+ system(help);
+#if defined(DEBUG)
+ fprintf(stderr, "%d files created\n", files);
+#endif
+}
+
+void
+server(void)
+{
+ pid_t pid[100];
+ struct sigaction sa;
+ struct sockaddr_in inetaddr, inetpeer;
+ socklen_t len;
+ int tcpsock, msgsock;
+ int *buf, fd, idx, n, on, t;
+ char ofile[128], nfile[128];
+
+ setproctitle("%s", __func__);
+ sa.sa_handler = SIG_IGN;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction(SIGCHLD, &sa, 0) == -1)
+ err(1, "sigaction");
+
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ idx = 0;
+ len = sizeof(inetpeer);
+ for (;;) {
+ alarm(600);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ if ((pid[idx] = fork()) == 0) {
+ t = 0;
+ if ((buf = malloc(BUFSIZE)) == NULL)
+ err(1, "malloc(%d), %s:%d", BUFSIZE,
+ __FILE__, __LINE__);
+
+ sprintf(ofile, "%s/g%06d.%06d", todir, getpid(), idx);
+ sprintf(nfile, "%s/n%06d.%06d", todir, getpid(), idx);
+ if ((fd = open(ofile, O_RDWR | O_CREAT | O_TRUNC,
+ 0640)) == -1)
+ err(1, "open(%s)", ofile);
+
+ for (;;) {
+ if ((n = read(msgsock, buf, BUFSIZE)) < 0)
+ err(1, "read(), %s:%d", __FILE__,
+ __LINE__);
+ t += n;
+ if (n == 0) break;
+
+ if ((write(fd, buf, n)) != n)
+ err(1, "write");
+ }
+ close(msgsock);
+ close(fd);
+ if (rename(ofile, nfile) != 0)
+ err(1, "rename(%s, %s)", ofile, nfile);
+ _exit(0);
+ }
+ close(msgsock);
+ if (++idx == files)
+ break;
+ if (idx == nitems(pid))
+ errx(1, "pid overflow");
+ }
+ for (n = 0; n < idx; n++)
+ if (waitpid(pid[n], NULL, 0) != pid[n])
+ err(1, "waitpid(%d)", pid[n]);
+
+ _exit(0);
+}
+
+static void
+writer(char *inputFile) {
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ struct stat statb;
+ off_t off = 0;
+ size_t size;
+ int i, fd, on, r, tcpsock;
+
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ size = getpagesize();
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_SNDBUF, (void *)&size, sizeof(size)) < 0)
+ err(1, "setsockopt(SO_SNDBUF), %s:%d", __FILE__,
+ __LINE__);
+
+ hostent = gethostbyname ("localhost");
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if (stat(inputFile, &statb) != 0)
+ err(1, "stat(%s)", inputFile);
+
+ if ((fd = open(inputFile, O_RDWR)) == -1)
+ err(1, "open(%s)", inputFile);
+
+ if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off,
+ SF_NOCACHE) == -1)
+ err(1, "sendfile");
+ close(fd);
+
+ return;
+}
+
+void *
+move(void *arg)
+{
+ int num;
+ char ifile[128];
+
+ setproctitle("%s", __func__);
+ while (active >= MAXTHREADS)
+ usleep(100000);
+ active++;
+ num = (int)(long)arg;
+
+ sprintf(ifile, "%s/f%06d.%06d", fromdir, getpid(), num);
+ writer(ifile);
+
+ if (unlink(ifile) != 0)
+ err(1, "unlink(%s)", ifile);
+ active--;
+
+ return (NULL);
+}
+
+int
+main(int argc, char *argv[])
+{
+ pid_t spid;
+ pthread_t *cp;
+ size_t len, size;
+ void *vp;
+ int e, i;
+
+ setproctitle("%s", __func__);
+ if (argc != 5) {
+ fprintf(stderr,
+ "Usage %s <port> <from dir> <to dir> <size in k>",
+ argv[0]);
+ exit(1);
+ }
+ port = atoi(argv[1]);
+ fromdir = argv[2];
+ if (chdir(fromdir) == -1)
+ err(1, "chdir(%s)", fromdir);
+ todir = argv[3];
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ sscanf(argv[4], "%zd", &size);
+ size = size * 1024;
+ create(fromdir, size);
+
+ if ((spid = fork()) == 0)
+ server();
+
+ cp = malloc(files * sizeof(pthread_t));
+ for (i = 0; i < files; i++) {
+ vp = (void *)(long)i;
+ if (pthread_create(&cp[i], NULL, move, vp) != 0)
+ perror("pthread_create");
+ }
+ for (i = 0; i < files; i++) {
+ pthread_join(cp[i], NULL);
+ }
+ if (waitpid(spid, NULL, 0) != spid)
+ err(1, "waitpid");
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/sendfile15.sh b/tools/test/stress2/misc/sendfile15.sh
new file mode 100755
index 000000000000..373c49977157
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile15.sh
@@ -0,0 +1,218 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# sendfile() over socket pairs test.
+
+# Page fault in unp_dispose+0x90 seen:
+# https://people.freebsd.org/~pho/stress/log/sendfile15.txt
+
+# "panic: bad pte va 800657000 pte 0" seen:
+# https://people.freebsd.org/~pho/stress/log/sendfile15-2.txt
+
+# "panic: vm_page_free_prep 0xfffff817e0efac10 PG_ZERO 87 ...":
+# https://people.freebsd.org/~pho/stress/log/sendfile15-2.txt
+# Note that r334783 seems to alleviate the problem.
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile15.c
+mycc -o sendfile15 -Wall -Wextra -O0 -g sendfile15.c || exit 1
+rm -f sendfile15.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+set +e
+
+cd $mntpoint
+dd if=/dev/random of=input bs=4k count=1 status=none
+(cd $odir/../testcases/swap; ./swap -t 5m -i 20 > /dev/null) &
+$dir/sendfile15
+s=$?
+while pgrep -q swap; do
+ pkill swap
+done
+wait
+[ -f sendfile15.core -a $s -eq 0 ] &&
+ { ls -l sendfile15.core; mv sendfile15.core $dir; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/sendfile15
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#define PARALLEL 128
+#define RUNTIME (5 * 60)
+#define SYNC 0
+
+static void
+test(void)
+{
+ struct stat st;
+ off_t written, pos;
+ int child, error, from, n, status, sv[2];
+ char *buf;
+ const char *from_name;
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ from_name = "input";
+
+ if ((from = open(from_name, O_RDONLY)) == -1)
+ err(1, "open read %s", from_name);
+
+ if ((error = fstat(from, &st)) == -1)
+ err(1, "stat %s", from_name);
+
+ if ((error = socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) == -1)
+ err(1, "socketpair");
+
+ child = fork();
+ if (child == -1)
+ err(1, "fork");
+ else if (child != 0) {
+ setproctitle("parent");
+ close(sv[1]);
+ pos = 0;
+ for (;;) {
+ error = sendfile(from, sv[0], pos, st.st_size - pos,
+ NULL, &written, 0);
+ if (error == -1) {
+ if (errno != EAGAIN)
+ err(1, "sendfile");
+ }
+ pos += written;
+ if (pos == st.st_size)
+ break;
+ }
+ close(sv[0]);
+ if (waitpid(child, &status, 0) != child)
+ err(1, "waitpid(%d)", child);
+ } else {
+ setproctitle("child");
+ close(sv[0]);
+ buf = malloc(st.st_size);
+ if (buf == NULL)
+ err(1, "malloc %jd", st.st_size);
+ pos = 0;
+ for (;;) {
+ written = st.st_size - pos;
+ n = read(sv[1], buf + pos, written);
+ if (n == -1)
+ err(1, "read");
+ else if (n == 0)
+ errx(1, "Short read");
+ pos += n;
+ if (pos == st.st_size)
+ break;
+ }
+ close(sv[1]);
+ _exit(0);
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/sendfile16.sh b/tools/test/stress2/misc/sendfile16.sh
new file mode 100755
index 000000000000..2374bd00fd46
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile16.sh
@@ -0,0 +1,224 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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.
+#
+
+# sendfile() test.
+# Copy of sendfile15.sh
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile16.c
+mycc -o sendfile16 -Wall -Wextra -O0 -g sendfile16.c || exit 1
+rm -f sendfile16.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+set +e
+
+cd $mntpoint
+dd if=/dev/random of=input bs=4k count=1 status=none
+export MAXSWAPPCT=85
+(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -l 100 > /dev/null) &
+sleep 5
+$dir/sendfile16
+s=$?
+while pgrep -q swap; do
+ pkill swap
+done
+wait
+[ -f sendfile16.core -a $s -eq 0 ] &&
+ { ls -l sendfile16.core; mv sendfile16.core $dir; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/sendfile16
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#define PARALLEL 128
+#define RUNTIME (5 * 60)
+#define SYNC 0
+#define SYNC2 1
+#define TIMEOUT 180
+
+static void
+test(void)
+{
+ struct stat st;
+ off_t written, pos;
+ const char *from_name;
+ int sv[2];
+ int child, error, from, n, status;
+ char *buf;
+
+ alarm(TIMEOUT);
+ setproctitle("spinup");
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ usleep(100);
+ alarm(0);
+
+ from_name = "input";
+
+ if ((from = open(from_name, O_RDONLY)) == -1)
+ err(1, "open read %s", from_name);
+
+ if ((error = fstat(from, &st)) == -1)
+ err(1, "stat %s", from_name);
+
+ if ((error = socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) == -1)
+ err(1, "socketpair");
+
+ child = fork();
+ if (child == -1)
+ err(1, "fork");
+ else if (child != 0) {
+ setproctitle("parent");
+ close(sv[1]);
+ pos = 0;
+ for (;;) {
+ error = sendfile(from, sv[0], pos, st.st_size - pos,
+ NULL, &written, 0);
+ if (error == -1) {
+ if (errno != EAGAIN)
+ err(1, "sendfile");
+ }
+ pos += written;
+ if (pos == st.st_size)
+ break;
+ }
+ close(sv[0]);
+ if (waitpid(child, &status, 0) != child)
+ err(1, "waitpid(%d)", child);
+ } else {
+ setproctitle("child");
+ close(sv[0]);
+ buf = malloc(st.st_size);
+ if (buf == NULL)
+ err(1, "malloc %jd", st.st_size);
+ pos = 0;
+ for (;;) {
+ written = st.st_size - pos;
+ n = read(sv[1], buf + pos, written);
+ if (n == -1)
+ err(1, "read");
+ else if (n == 0)
+ errx(1, "Short read");
+ pos += n;
+ if (pos == st.st_size)
+ break;
+ }
+ close(sv[1]);
+ _exit(0);
+ }
+ setproctitle("spindown");
+ n = 0;
+ atomic_add_int(&share[SYNC2], 1);
+ alarm(TIMEOUT);
+ while (share[SYNC2] != PARALLEL)
+ usleep(100000);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+ share[SYNC] = 0;
+ share[SYNC2] = 0;
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/sendfile17.sh b/tools/test/stress2/misc/sendfile17.sh
new file mode 100755
index 000000000000..8580778d5ae8
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile17.sh
@@ -0,0 +1,225 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# sendfile() over socket pairs test. Variation of sendfile15.sh with
+# ualarm(2) added.
+
+# Page fault seen: https://people.freebsd.org/~pho/stress/log/dougm029.txt
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile17.c
+mycc -o sendfile17 -Wall -Wextra -O0 -g sendfile17.c || exit 1
+rm -f sendfile17.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 3g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+set +e
+
+cd $mntpoint
+dd if=/dev/random of=input bs=4k count=1 status=none
+(cd $odir/../testcases/swap; ./swap -t 5m -i 20 > /dev/null) &
+$dir/sendfile17
+s=$?
+while pgrep -q swap; do
+ pkill swap
+done
+wait
+[ -f sendfile17.core -a $s -eq 0 ] &&
+ { ls -l sendfile17.core; mv sendfile17.core $dir; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/sendfile17
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#define PARALLEL 32
+#define RUNTIME (5 * 60)
+#define SYNC 0
+
+static void
+handler(int i __unused) {
+ _exit(0);
+}
+
+static void
+test(void)
+{
+ struct stat st;
+ off_t written, pos;
+ int child, error, from, n, status, sv[2];
+ char *buf;
+ const char *from_name;
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ usleep(100);
+
+ from_name = "input";
+
+ if ((from = open(from_name, O_RDONLY)) == -1)
+ err(1, "open read %s", from_name);
+
+ if ((error = fstat(from, &st)) == -1)
+ err(1, "stat %s", from_name);
+
+ if ((error = socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) == -1)
+ err(1, "socketpair");
+
+ signal(SIGALRM, handler);
+ child = fork();
+ if (child == -1)
+ err(1, "fork");
+ else if (child != 0) {
+ setproctitle("parent");
+ close(sv[1]);
+ pos = 0;
+ for (;;) {
+ if (arc4random() % 100 < 20)
+ ualarm(arc4random() % 10000, 0);
+ error = sendfile(from, sv[0], pos, st.st_size - pos,
+ NULL, &written, 0);
+ if (error == -1) {
+ if (errno != EAGAIN && errno != EPIPE &&
+ errno != ENOTCONN)
+ err(1, "sendfile");
+ _exit(0);;
+ }
+ ualarm(0, 0);
+ pos += written;
+ if (pos == st.st_size)
+ break;
+ }
+ close(sv[0]);
+ if (waitpid(child, &status, 0) != child)
+ err(1, "waitpid(%d)", child);
+ } else {
+ setproctitle("child");
+ close(sv[0]);
+ buf = malloc(st.st_size);
+ if (buf == NULL)
+ err(1, "malloc %jd", st.st_size);
+ pos = 0;
+ for (;;) {
+ written = st.st_size - pos;
+ if (arc4random() % 100 < 20)
+ ualarm(arc4random() % 10000, 0);
+ n = read(sv[1], buf + pos, written);
+ ualarm(0, 0);
+ if (n == -1 && errno != ENOTCONN)
+ err(1, "read");
+ else if (n == 0)
+ _exit(0);
+ pos += n;
+ if (pos == st.st_size)
+ break;
+ }
+ close(sv[1]);
+ _exit(0);
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/sendfile18.sh b/tools/test/stress2/misc/sendfile18.sh
new file mode 100755
index 000000000000..3b13292932f1
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile18.sh
@@ -0,0 +1,125 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Mark Johnston <markj@FreeBSD.org>
+#
+# 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 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.
+#
+
+# "Fatal trap 9: general protection fault while in kernel mode" seen:
+# https://people.freebsd.org/~pho/stress/log/sendfile15-4.txt
+
+# Mark wrote:
+# It took me some time but I managed to get a repro. Here is what I did:
+# - Create a malloc-backed md(4) device with a UFS filesystem. Configure
+# a gnop provider with -q 100 -d 10, i.e., always delay reads by 10ms.
+# - Write a small file (e.g., 4KB) to the filesystem.
+# - In a loop, unmount the filesystem, mount it, and run the test program.
+# The test program:
+# - creates a PF_LOCAL socket pair <s1, s2>,
+# - opens the input file,
+# - sends the file over s1,
+# - closes s1,
+# - sleeps for a second before exiting (and closing s2)
+#
+# Using a separate filesystem ensures that the input file is not cached
+# when the test program runs, so sendfile will perform an async getpages
+# and place the "future" mbufs in s2's receive buffer. The gnop delay
+# ensures that the I/O request will not be completed before s1 is closed,
+# and because s1 is closed uipc_ready() will free the promised mbufs and
+# return ECONNRESET. Because of the sleep, s2's receive buffer will not
+# be scanned until after the uipc_ready() call.
+
+# Fixed by r359778
+
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+kldstat | grep -q geom_nop || { gnop load 2>/dev/null || exit 0 &&
+ notloaded=1; }
+gnop status || exit 1
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile18.c
+mycc -o sendfile18 -Wall -Wextra -O0 -g sendfile18.c || exit 1
+rm -f sendfile18.c
+cd $odir
+
+set -e
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart
+gnop create /dev/md$mdstart
+newfs $newfs_flags /dev/md$mdstart.nop > /dev/null
+mount /dev/md$mdstart.nop $mntpoint
+chmod 777 $mntpoint
+gnop configure -q 100 -d 10 /dev/md$mdstart.nop
+set +e
+
+dd if=/dev/zero of=$mntpoint/file bs=4k count=1 status=none
+
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 5 ]; do
+ umount $mntpoint
+ mount /dev/md$mdstart.nop $mntpoint
+ /tmp/sendfile18 $mntpoint/file
+done
+umount $mntpoint
+
+gnop destroy /dev/md$mdstart.nop
+mdconfig -d -u $mdstart
+rm $dir/sendfile18
+[ $notloaded ] && gnop unload
+
+exit 0
+EOF
+#include <sys/socket.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(int argc __unused, char **argv)
+{
+ int fd, sd[2];
+
+ if (socketpair(PF_LOCAL, SOCK_STREAM, 0, sd) != 0)
+ err(1, "socketpair");
+
+ fd = open(argv[1], O_RDONLY | O_DIRECT);
+ if (fd < 0)
+ err(1, "open");
+
+ if (sendfile(fd, sd[0], 0, 4096, NULL, NULL, 0) != 0)
+ err(1, "sendfile");
+ close(sd[0]);
+ sleep(1);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sendfile19.sh b/tools/test/stress2/misc/sendfile19.sh
new file mode 100755
index 000000000000..0254a495d5cf
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile19.sh
@@ -0,0 +1,246 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# "panic: Memory modified after free" seen:
+# https://people.freebsd.org/~pho/stress/log/sendfile19.txt
+# Broken by r358995-r359002
+# Fixed by r359778
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > sendfile19.c
+mycc -o sendfile19 -Wall sendfile19.c -pthread || exit 1
+rm -f sendfile19.c
+
+(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -l 100) &
+sleep 5
+mount | grep "$mntpoint" | grep -q nfs && umount $mntpoint
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mntpoint
+
+cd $mntpoint
+parallel=1000
+for i in `jot $parallel`; do
+ size=`jot -r 1 1 $((2 * 1024 * 1024))`
+ dd if=/dev/zero of=input.$i bs=$size count=1 status=none
+done
+cd $odir
+while mount | grep "$mntpoint " | grep -q nfs; do
+ umount -f $mntpoint
+done
+sleep 1
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mntpoint
+spid=$!
+cd $mntpoint
+pids=
+for i in `jot $parallel`; do
+ /tmp/sendfile19 input.$i output.$i 1234$i &
+ pids="$pids $!"
+done
+for p in $pids; do
+ wait $p
+done
+for i in `jot $parallel`; do
+ rm -f input.$i output.$i
+done
+while pkill swap; do :; done
+wait $spid
+
+cd $odir
+umount $mntpoint
+while mount | grep "$mntpoint " | grep -q nfs; do
+ umount -f $mntpoint
+done
+rm -f /tmp/sendfile19
+exit 0
+EOF
+/* Slightly modified scenario from sendfile.sh */
+#include <err.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+int port;
+char *inputFile;
+char *outputFile;
+int bufsize = 4096;
+
+static void
+reader(void) {
+ int tcpsock, msgsock;
+ int on;
+ socklen_t len;
+ struct sockaddr_in inetaddr, inetpeer;
+ int n, t, *buf, fd;
+
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ t = 0;
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+
+ if ((fd = open(outputFile, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1, "open(%s)", outputFile);
+
+ for (;;) {
+ if ((n = read(msgsock, buf, bufsize)) < 0)
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ t += n;
+ if (n == 0)
+ break;
+
+ if ((write(fd, buf, n)) != n)
+ err(1, "write");
+ }
+ close(msgsock);
+ close(fd);
+ return;
+}
+
+static void
+writer(void) {
+ int tcpsock, on;
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ struct stat statb;
+ int i, r, fd;
+ off_t off = 0;
+#if 1
+ size_t size;
+#endif
+
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+#if 1 /* livelock trigger */
+ size = getpagesize() -4;
+ if (setsockopt(tcpsock, SOL_SOCKET, SO_SNDBUF, (void *)&size,
+ sizeof(size)) < 0)
+ err(1, "setsockopt(SO_SNDBUF), %s:%d",
+ __FILE__, __LINE__);
+#endif
+
+ hostent = gethostbyname ("localhost");
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if (stat(inputFile, &statb) != 0)
+ err(1, "stat(%s)", inputFile);
+
+ if ((fd = open(inputFile, O_RDONLY)) == -1)
+ err(1, "open(%s)", inputFile);
+
+ if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, SF_NOCACHE) == -1)
+ err(1, "sendfile");
+
+ return;
+}
+
+int
+main(int argc, char **argv)
+{
+ pid_t pid;
+
+ if (argc != 4) {
+ fprintf(stderr, "Usage: %s <inputFile outputFile portNumber\n",
+ argv[0]);
+ return (1);
+ }
+ inputFile = argv[1];
+ outputFile = argv[2];
+ port = atoi(argv[3]);
+
+ if ((pid = fork()) == 0) {
+ writer();
+ exit(EXIT_SUCCESS);
+ } else if (pid > 0) {
+ reader();
+ kill(pid, SIGINT);
+ } else
+ err(1, "fork(), %s:%d", __FILE__, __LINE__);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sendfile2.sh b/tools/test/stress2/misc/sendfile2.sh
new file mode 100755
index 000000000000..42520c54d708
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile2.sh
@@ -0,0 +1,142 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# Test scenario for sendfile corruption of read only input file
+
+# Scenario by Ming Fu <Ming Fu watchguard com>
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > sendfile2.c
+mycc -o sendfile2 -Wall sendfile2.c
+rm -f sendfile2.c
+[ -d "$RUNDIR" ] || mkdir -p $RUNDIR
+cd $RUNDIR
+
+dd if=/dev/random of=large bs=1m count=3 status=none
+md1=`md5 large`
+
+nc -l 7000 > lf &
+sleep 0.1
+/tmp/sendfile2
+kill $! 2>/dev/null
+wait
+
+md2=`md5 large`
+[ "$md1" != "$md2" ] && printf "%s\n%s\n" "$md1" "$md2"
+
+rm -f /tmp/sendfile2 large lf
+exit
+EOF
+#include <sys/types.h>
+#include <arpa/inet.h>
+#include <err.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/uio.h>
+#include <unistd.h>
+
+int
+main () {
+ int s, f;
+ struct sockaddr_in addr;
+ struct hostent *hostent;
+ int flags;
+ char str[32]="\r\n800\r\n";
+ char *p = str;
+ struct stat sb;
+ int n;
+ fd_set wset;
+ int64_t size;
+ off_t sbytes;
+ off_t sent = 0;
+ int chunk;
+
+ alarm(120);
+ s = socket(AF_INET, SOCK_STREAM, 0);
+ bzero(&addr, sizeof(addr));
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(7000);
+ hostent = gethostbyname ("localhost");
+ memcpy (&addr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ n = connect(s, (struct sockaddr *)&addr, sizeof (addr));
+ if (n < 0)
+ warn ("fail to connect");
+ flags = fcntl(s, F_GETFL);
+ flags |= O_NONBLOCK;
+ fcntl(s, F_SETFL);
+
+ f = open("large", O_RDONLY);
+ if (f < 0)
+ warn("fail to open file");
+ n = fstat(f, &sb);
+ if (n < 0)
+ warn("fstat failed");
+
+ size = sb.st_size;
+ chunk = 0;
+ while (size > 0) {
+ FD_ZERO(&wset);
+ FD_SET(s, &wset);
+ n = select(f+1, NULL, &wset, NULL, NULL);
+ if (n < 0)
+ continue;
+ if (chunk > 0) {
+ sbytes = 0;
+ n = sendfile(f, s, sent, chunk, NULL, &sbytes, 0);
+ if (n < 0)
+ continue;
+ chunk -= sbytes;
+ size -= sbytes;
+ sent += sbytes;
+ continue;
+ }
+ if (size > 2048)
+ chunk = 2048;
+ else
+ chunk = size;
+ n = sprintf(str, "\r\n%x\r\n", 2048);
+ p = str;
+ write(s, p, n);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sendfile20.sh b/tools/test/stress2/misc/sendfile20.sh
new file mode 100755
index 000000000000..88c0a344a926
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile20.sh
@@ -0,0 +1,247 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# "panic: vm_page_assert_xbusied: page not exclusive busy" seen:
+# https://people.freebsd.org/~pho/stress/log/sendfile20.txt
+# Fixed by r359778
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > sendfile20.c
+mycc -o sendfile20 -Wall sendfile20.c -pthread || exit 1
+rm -f sendfile20.c
+
+(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -l 100) &
+sleep 5
+mount | grep "$mntpoint" | grep -q nfs && umount $mntpoint
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mntpoint
+
+cd $mntpoint
+parallel=2000
+for i in `jot $parallel`; do
+ size=`jot -r 1 1 $((1024 * 1024))`
+ dd if=/dev/zero of=input.$i bs=$size count=1 status=none
+done
+cd $odir
+while mount | grep "$mntpoint " | grep -q nfs; do
+ umount -f $mntpoint
+done
+sleep 1
+mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mntpoint
+spid=$!
+cd $mntpoint
+pids=
+for i in `jot $parallel`; do
+ /tmp/sendfile20 input.$i output.$i 1234$i &
+ pids="$pids $!"
+done
+sleep 30
+while pkill sendfile20; do :; done
+for p in $pids; do
+ wait $p
+done
+for i in `jot $parallel`; do
+# cmp input.$i output.$i || { echo FAIL; ls -l input$i output$i; }
+ rm -f input.$i output.$i
+done
+while pkill swap; do :; done
+wait $spid
+
+cd $odir
+umount $mntpoint
+while mount | grep "$mntpoint " | grep -q nfs; do
+ umount -f $mntpoint
+done
+rm -f /tmp/sendfile20
+exit 0
+EOF
+#include <err.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+int port;
+char *inputFile;
+char *outputFile;
+int bufsize = 4096;
+
+static void
+reader(void) {
+ int tcpsock, msgsock;
+ int on;
+ socklen_t len;
+ struct sockaddr_in inetaddr, inetpeer;
+ int n, t, *buf, fd;
+
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ t = 0;
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+
+ if ((fd = open(outputFile, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1, "open(%s)", outputFile);
+
+ for (;;) {
+ if ((n = read(msgsock, buf, bufsize)) < 0)
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ t += n;
+ if (n == 0)
+ break;
+
+ if ((write(fd, buf, n)) != n)
+ err(1, "write");
+ }
+ close(msgsock);
+ close(fd);
+ return;
+}
+
+static void
+writer(void) {
+ int tcpsock, on;
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ struct stat statb;
+ int i, r, fd;
+ off_t off = 0;
+#if 1
+ size_t size;
+#endif
+
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+#if 1 /* livelock trigger */
+ size = getpagesize() -4;
+ if (setsockopt(tcpsock, SOL_SOCKET, SO_SNDBUF, (void *)&size,
+ sizeof(size)) < 0)
+ err(1, "setsockopt(SO_SNDBUF), %s:%d",
+ __FILE__, __LINE__);
+#endif
+
+ hostent = gethostbyname ("localhost");
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if (stat(inputFile, &statb) != 0)
+ err(1, "stat(%s)", inputFile);
+
+ if ((fd = open(inputFile, O_RDONLY)) == -1)
+ err(1, "open(%s)", inputFile);
+
+ if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, SF_NOCACHE) == -1)
+ err(1, "sendfile");
+
+ return;
+}
+
+int
+main(int argc, char **argv)
+{
+ pid_t pid;
+
+ if (argc != 4) {
+ fprintf(stderr, "Usage: %s <inputFile outputFile portNumber\n",
+ argv[0]);
+ return (1);
+ }
+ inputFile = argv[1];
+ outputFile = argv[2];
+ port = atoi(argv[3]);
+
+ if ((pid = fork()) == 0) {
+ writer();
+ exit(EXIT_SUCCESS);
+ } else if (pid > 0) {
+ reader();
+ kill(pid, SIGINT);
+ } else
+ err(1, "fork(), %s:%d", __FILE__, __LINE__);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sendfile21.sh b/tools/test/stress2/misc/sendfile21.sh
new file mode 100755
index 000000000000..4387d167dfc2
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile21.sh
@@ -0,0 +1,243 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Konstantin Belousov <kib@FreeBSD.org> wrote:
+# 1. create a suffuciently large new file, e.g. of size 100 * PAGE_SIZE
+# 2. fill the file with some data, so no holes exist
+# 3. fsync it
+# 4. mmap the file
+# 5. for each odd-numbered page in the file, do msync(MS_INVALIDATE),
+# but keep even-numbered pages intact
+# 6. unmap the file
+# 7. sendfile the file to a receiver (similar to existing tests in stress2)
+# 8. compare received data with the content of file
+#
+# Without the fixes from r359767 and r359818, the kernel should panic or
+# supply incorrect data to the sendfile peer.
+
+# "Fatal trap 9: general protection fault while in kernel mode
+# (unp_dispose+0x99)" seen.
+# https://people.freebsd.org/~pho/stress/log/sendfile21.txt
+# Not seen on r359843
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile21.c
+mycc -o sendfile21 -Wall -Wextra -O0 -g sendfile21.c || exit 1
+rm -f sendfile21.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags -n md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+cd $mntpoint
+dd if=/dev/random of=input bs=4k count=100 status=none
+
+(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -l 100 > /dev/null) &
+sleep 30
+n=1
+start=` date +%s`
+while [ $((` date +%s` - start)) -lt 180 ]; do
+ rm -f output
+# The umount is needed to trigger the short read error
+ umount $mntpoint 2>/dev/null # busy umount
+ $dir/sendfile21
+ s=$?
+ cmp -s input output || break
+ [ `stat -f '%z' input` -ne ` stat -f '%z' output` ] && break
+ n=$((n + 1))
+done
+while pgrep -q swap; do
+ pkill swap
+done
+cmp -s input output || { echo "Loop #$n"; ls -l; s=1; }
+wait
+[ -f sendfile21.core -a $s -eq 0 ] &&
+ { ls -l sendfile21.core; mv sendfile21.core $dir; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/sendfile21
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define DEBUG 1
+
+static void
+test(void)
+{
+ struct stat st;
+ off_t i, j, k, rd, written, pos;
+ pid_t pid;
+ int error, from, n, status, sv[2], to;
+ char buf[4086], *cp;
+ const char *from_name, *to_name;
+
+ from_name = "input";
+ to_name = "output";
+
+ if ((error = socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) == -1)
+ err(1, "socketpair");
+
+ if ((from = open(from_name, O_RDONLY)) == -1)
+ err(1, "open read %s", from_name);
+
+ if ((error = fstat(from, &st)) == -1)
+ err(1, "stat %s", from_name);
+
+ pid = fork();
+ if (pid == -1)
+ err(1, "fork");
+ else if (pid != 0) {
+ setproctitle("parent");
+ close(sv[1]);
+
+ if ((cp = mmap(NULL, st.st_size, PROT_READ,
+ MAP_PRIVATE, from, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ if (fsync(from) == -1)
+ err(1, "fsync()");
+
+ for (i = 0; i < st.st_size; i += PAGE_SIZE)
+ k = cp[i]; /* touch all pages */
+
+ for (i = 0, j = 0; i < st.st_size; i += PAGE_SIZE, j++) {
+ if (j % 2 == 1) {
+ if (msync(cp + i, PAGE_SIZE, MS_INVALIDATE)
+ == -1)
+ err(1, "msync(), j = %d", (int)j);
+ }
+ }
+ if (munmap(cp, st.st_size) == -1)
+ err(1, "munmap()");
+
+ pos = 0;
+ for (;;) {
+ error = sendfile(from, sv[0], pos, st.st_size - pos,
+ NULL, &written, 0);
+ if (error == -1)
+ err(1, "sendfile");
+ if (written != st.st_size)
+ fprintf(stderr, "sendfile sent %d bytes\n",
+ (int)written);
+ pos += written;
+ if (pos == st.st_size)
+ break;
+ }
+ close(sv[0]);
+ if (pos != st.st_size)
+ fprintf(stderr, "%d written, expected %d\n",
+ (int) pos, (int)st.st_size);
+ if (waitpid(pid, &status, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+ _exit(WEXITSTATUS(status));
+ } else {
+ setproctitle("child");
+ close(from);
+ close(sv[0]);
+
+ if ((to = open(to_name, O_RDWR | O_CREAT, DEFFILEMODE)) ==
+ -1)
+ err(1, "open write %s", to_name);
+
+ rd = 0;
+ for (;;) {
+ n = read(sv[1], buf, sizeof(buf));
+ if (n == -1)
+ err(1, "read");
+ else if (n == 0)
+ break;
+ rd += n;
+ if (write(to, buf, n) != n)
+ err(1, "write()");
+ }
+ close(to);
+ close(sv[1]);
+ if (rd != st.st_size)
+ fprintf(stderr, "Short read %d, expected %d\n",
+ (int)rd, (int)st.st_size);
+ _exit(0);
+ }
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pid;
+ int e, status;
+
+ e = 0;
+ if ((pid = fork()) == 0)
+ test();
+ if (pid == -1)
+ err(1, "fork()");
+ if (waitpid(pid, &status, 0) == -1)
+ err(1, "waitpid(%d)", pid);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pid, WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/sendfile22.sh b/tools/test/stress2/misc/sendfile22.sh
new file mode 100755
index 000000000000..ea3a564e1561
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile22.sh
@@ -0,0 +1,282 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Variation of sendfile21.sh: without the use of socketpair().
+# No problems seen.
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile22.c
+mycc -o sendfile22 -Wall -Wextra -O0 -g sendfile22.c || exit 1
+rm -f sendfile22.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs_flags=""
+newfs $newfs_flags -n md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+cd $mntpoint
+dd if=/dev/random of=input bs=4k count=100 status=none
+
+(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -l 100 > /dev/null) &
+sleep 30
+n=1
+start=` date +%s`
+while [ $((` date +%s` - start)) -lt 180 ]; do
+ rm -f output
+ umount $mntpoint 2>/dev/null # busy umount
+ $dir/sendfile22
+ s=$?
+ cmp -s input output || break
+ [ `stat -f '%z' input` -ne ` stat -f '%z' output` ] && break
+ n=$((n + 1))
+done
+while pgrep -q swap; do
+ pkill swap
+done
+cmp -s input output || { echo "Loop #$n"; ls -l; s=1; }
+wait
+[ -f sendfile22.core -a $s -eq 0 ] &&
+ { ls -l sendfile22.core; mv sendfile22.core $dir; s=1; }
+cd $odir
+
+for i in `jot 18`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 18 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/sendfile22
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+
+static void
+test(void)
+{
+ struct stat st;
+ struct sockaddr_in inetaddr, inetpeer;
+ struct hostent *hostent;
+ socklen_t len;
+ off_t i, j, rd, written, pos;
+ pid_t pid;
+ int error, from, i1, msgsock, n, on, port, r, status, tcpsock, to;
+ char buf[4086], *cp;
+ const char *from_name, *to_name;
+
+ from_name = "input";
+ to_name = "output";
+ port = 12345;
+
+ if ((from = open(from_name, O_RDONLY)) == -1)
+ err(1, "open read %s", from_name);
+
+ if ((error = fstat(from, &st)) == -1)
+ err(1, "stat %s", from_name);
+
+ pid = fork();
+ if (pid == -1)
+ err(1, "fork");
+ else if (pid != 0) {
+ setproctitle("parent");
+
+ alarm(300);
+ on = 1;
+ for (i1 = 1; i1 < 5; i1++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on,
+ sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__,
+ __LINE__);
+
+ hostent = gethostbyname ("localhost");
+ bzero((char *) &inetaddr, sizeof(inetaddr));
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if ((cp = mmap(NULL, st.st_size, PROT_READ,
+ MAP_PRIVATE, from, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ if (fsync(from) == -1)
+ err(1, "fsync()");
+
+ for (i = 0, j = 0; i < st.st_size; i += PAGE_SIZE, j++) {
+ (void)cp[i];
+ if (j % 2 == 1) {
+ if (msync(cp + i, PAGE_SIZE, MS_INVALIDATE)
+ == -1)
+ err(1, "msync(), j = %d", (int)j);
+ }
+ }
+ if (munmap(cp, st.st_size) == -1)
+ err(1, "munmap()");
+
+ pos = 0;
+ for (;;) {
+ error = sendfile(from, tcpsock, pos, st.st_size -
+ pos, NULL, &written, 0);
+ if (error == -1)
+ err(1, "sendfile");
+ if (written != st.st_size)
+ fprintf(stderr, "sendfile sent %d bytes\n",
+ (int)written);
+ pos += written;
+ if (pos == st.st_size)
+ break;
+ }
+ if (pos != st.st_size)
+ fprintf(stderr, "%d written, expected %d\n",
+ (int)pos, (int)st.st_size);
+ close(tcpsock);
+ if (waitpid(pid, &status, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+ } else {
+ setproctitle("child");
+ close(from);
+ alarm(300);
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ if ((to = open(to_name, O_RDWR | O_CREAT, DEFFILEMODE)) ==
+ -1)
+ err(1, "open write %s", to_name);
+
+ rd = 0;
+ for (;;) {
+ n = read(msgsock, buf, sizeof(buf));
+ if (n == -1)
+ err(1, "read");
+ else if (n == 0)
+ break;
+ rd += n;
+ if (write(to, buf, n) != n)
+ err(1, "write()");
+ }
+ close(to);
+ if (rd != st.st_size)
+ fprintf(stderr, "Short read %d, expected %d\n",
+ (int)rd, (int)st.st_size);
+ _exit(0);
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pid;
+ int e, status;
+
+ e = 0;
+ if ((pid = fork()) == 0)
+ test();
+ if (pid == -1)
+ err(1, "fork()");
+ if (waitpid(pid, &status, 0) == -1)
+ err(1, "waitpid(%d)", pid);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pid, WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/sendfile23.sh b/tools/test/stress2/misc/sendfile23.sh
new file mode 100755
index 000000000000..c5b77a685f1c
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile23.sh
@@ -0,0 +1,207 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Variation of sendfile21.sh. Short read seen.
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile23.c
+mycc -o sendfile23 -Wall -Wextra -O0 -g sendfile23.c || exit 1
+rm -f sendfile23.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags -n md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+cd $mntpoint
+dd if=/dev/random of=input bs=4k count=100 status=none
+
+(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -l 100 > /dev/null) &
+sleep 30
+n=1
+start=` date +%s`
+while [ $((` date +%s` - start)) -lt 180 ]; do
+ rm -f output
+# The umount is needed to trigger the short read error
+ umount $mntpoint 2>/dev/null # busy umount
+ $dir/sendfile23
+ s=$?
+ cmp -s input output || break
+ [ `stat -f '%z' input` -ne ` stat -f '%z' output` ] && break
+ n=$((n + 1))
+done
+while pgrep -q swap; do
+ pkill swap
+done
+cmp -s input output || { echo "Loop #$n"; ls -l; s=1; }
+wait
+[ -f sendfile23.core -a $s -eq 0 ] &&
+ { ls -l sendfile23.core; mv sendfile23.core $dir; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/sendfile23
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define DEBUG 1
+
+static void
+test(void)
+{
+ struct stat st;
+ off_t rd, written, pos;
+ pid_t pid;
+ int error, from, n, status, sv[2], to;
+ char buf[4086];
+ const char *from_name, *to_name;
+
+ from_name = "input";
+ to_name = "output";
+
+ if ((error = socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) == -1)
+ err(1, "socketpair");
+
+ if ((from = open(from_name, O_RDONLY)) == -1)
+ err(1, "open read %s", from_name);
+
+ if ((error = fstat(from, &st)) == -1)
+ err(1, "stat %s", from_name);
+
+ pid = fork();
+ if (pid == -1)
+ err(1, "fork");
+ else if (pid != 0) {
+ setproctitle("parent");
+ close(sv[1]);
+
+ pos = 0;
+ for (;;) {
+ error = sendfile(from, sv[0], pos, st.st_size - pos,
+ NULL, &written, 0);
+ if (error == -1)
+ err(1, "sendfile");
+ if (written != st.st_size)
+ fprintf(stderr, "sendfile sent %d bytes\n",
+ (int)written);
+ pos += written;
+ if (pos == st.st_size)
+ break;
+ }
+ close(sv[0]);
+ if (pos != st.st_size)
+ fprintf(stderr, "%d written, expected %d\n",
+ (int) pos, (int)st.st_size);
+ if (waitpid(pid, &status, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+ _exit(WEXITSTATUS(status));
+ } else {
+ setproctitle("child");
+ close(from);
+ close(sv[0]);
+
+ if ((to = open(to_name, O_RDWR | O_CREAT, DEFFILEMODE)) ==
+ -1)
+ err(1, "open write %s", to_name);
+
+ rd = 0;
+ for (;;) {
+ n = read(sv[1], buf, sizeof(buf));
+ if (n == -1)
+ err(1, "read");
+ else if (n == 0)
+ break;
+ rd += n;
+ if (write(to, buf, n) != n)
+ err(1, "write()");
+ }
+ close(to);
+ close(sv[1]);
+ if (rd != st.st_size)
+ fprintf(stderr, "Short read %d, expected %d\n",
+ (int)rd, (int)st.st_size);
+ _exit(0);
+ }
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pid;
+ int e, status;
+
+ e = 0;
+ if ((pid = fork()) == 0)
+ test();
+ if (pid == -1)
+ err(1, "fork()");
+ if (waitpid(pid, &status, 0) == -1)
+ err(1, "waitpid(%d)", pid);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pid, WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/sendfile24.sh b/tools/test/stress2/misc/sendfile24.sh
new file mode 100755
index 000000000000..e61a01a436c5
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile24.sh
@@ -0,0 +1,235 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# sendfile(2) test with disk read errors
+
+# 0 2986 2924 0 52 0 4392 2132 vmopar D+ 0 0:00.00 umount /mnt
+# seen.
+# Fixed by r361852
+
+# Test scenario suggestion by chs@
+
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+kldstat | grep -q geom_nop || { gnop load 2>/dev/null || exit 0 &&
+ notloaded=1; }
+gnop status || exit 1
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile24.c
+mycc -o sendfile24 -Wall -Wextra -O0 -g sendfile24.c || exit 1
+cd $odir
+
+set -e
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart
+gnop create /dev/md$mdstart
+newfs $newfs_flags /dev/md$mdstart.nop > /dev/null
+mount /dev/md$mdstart.nop $mntpoint
+chmod 777 $mntpoint
+set +e
+
+dd if=/dev/zero of=$mntpoint/file bs=4k count=1 status=none
+gnop configure -e 5 -r 1 /dev/md$mdstart.nop
+
+start=`date +%s`
+echo 'Expect:
+ sendfile24: sendfile: sendfile24: read(), sendfile24.c:61: Broken pipe
+ Connection reset by peer'
+while [ $((`date +%s` - start)) -lt 10 ]; do
+ (cd $mntpoint; umount $mntpoint) > /dev/null 2>&1
+ /tmp/sendfile24 $mntpoint/file /dev/null 12345
+done
+umount $mntpoint
+
+gnop destroy /dev/md$mdstart.nop
+mdconfig -d -u $mdstart
+[ $notloaded ] && gnop unload
+rm -f /tmp/sendfile24 /tmp/sendfile24.c
+
+exit 0
+EOF
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int port;
+char *inputFile;
+char *outputFile;
+int bufsize = 4096;
+
+static void
+reader(void) {
+ int tcpsock, msgsock;
+ int on;
+ socklen_t len;
+ struct sockaddr_in inetaddr, inetpeer;
+ int n, t, *buf, fd;
+
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ t = 0;
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+
+ if ((fd = open(outputFile, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1, "open(%s)", outputFile);
+
+ for (;;) {
+ if ((n = read(msgsock, buf, bufsize)) < 0)
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ t += n;
+ if (n == 0)
+ break;
+
+ if ((write(fd, buf, n)) != n)
+ err(1, "write");
+ }
+ close(msgsock);
+ close(fd);
+ return;
+}
+
+static void
+writer(void) {
+ int tcpsock, on;
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ struct stat statb;
+ off_t off = 0;
+ size_t size;
+ int i, r, fd;
+
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ size = getpagesize() -4;
+ if (setsockopt(tcpsock, SOL_SOCKET, SO_SNDBUF, (void *)&size,
+ sizeof(size)) < 0)
+ err(1, "setsockopt(SO_SNDBUF), %s:%d",
+ __FILE__, __LINE__);
+
+ hostent = gethostbyname ("localhost");
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if (stat(inputFile, &statb) != 0)
+ err(1, "stat(%s)", inputFile);
+
+ if ((fd = open(inputFile, O_RDONLY)) == -1)
+ err(1, "open(%s)", inputFile);
+
+ if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1)
+ err(1, "sendfile");
+
+ return;
+}
+
+int
+main(int argc, char **argv)
+{
+ pid_t pid;
+
+ if (argc != 4) {
+ fprintf(stderr, "Usage: %s <inputFile outputFile portNumber\n",
+ argv[0]);
+ return (1);
+ }
+ inputFile = argv[1];
+ outputFile = argv[2];
+ port = atoi(argv[3]);
+
+ if ((pid = fork()) == 0) {
+ writer();
+ exit(EXIT_SUCCESS);
+ } else if (pid > 0) {
+ reader();
+ kill(pid, SIGINT);
+ } else
+ err(1, "fork(), %s:%d", __FILE__, __LINE__);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sendfile25.sh b/tools/test/stress2/misc/sendfile25.sh
new file mode 100755
index 000000000000..dba0378d547a
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile25.sh
@@ -0,0 +1,237 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# sendfile(2) test with disk read errors
+# "panic: vm_page_readahead_finish: 0xfffffe000d88d758 is invalid" seen:
+# https://people.freebsd.org/~pho/stress/log/sendfile25.txt
+
+# Test scenario suggestion by chs@
+# Fixed by
+
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+kldstat | grep -q geom_nop || { gnop load 2>/dev/null || exit 0 &&
+ notloaded=1; }
+gnop status || exit 1
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile25.c
+mycc -o sendfile25 -Wall -Wextra -O0 -g sendfile25.c || exit 1
+cd $odir
+
+set -e
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart
+gnop create /dev/md$mdstart
+newfs $newfs_flags -n -b $((32 * 1024)) /dev/md$mdstart.nop > /dev/null
+mount /dev/md$mdstart.nop $mntpoint
+chmod 777 $mntpoint
+set +e
+
+dd if=/dev/zero of=$mntpoint/file bs=416k count=1 status=none
+start=`date +%s`
+echo 'Expect:
+ sendfile25: sendfile: sendfile25: read(), sendfile25.c:61: Broken pipe
+ Connection reset by peer'
+while [ $((`date +%s` - start)) -lt 10 ]; do
+ umount $mntpoint
+ mount /dev/md$mdstart.nop $mntpoint
+ ls -l $mntpoint > /dev/null
+ gnop configure -e 5 -r 1 /dev/md$mdstart.nop
+ /tmp/sendfile25 $mntpoint/file /dev/null 12345
+ gnop configure -e 5 -r 0 /dev/md$mdstart.nop
+done
+umount $mntpoint
+
+gnop destroy /dev/md$mdstart.nop
+mdconfig -d -u $mdstart
+[ $notloaded ] && gnop unload
+rm -f /tmp/sendfile25 /tmp/sendfile25.c
+
+exit 0
+EOF
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int port;
+char *inputFile;
+char *outputFile;
+int bufsize = 4096;
+
+static void
+reader(void) {
+ int tcpsock, msgsock;
+ int on;
+ socklen_t len;
+ struct sockaddr_in inetaddr, inetpeer;
+ int n, t, *buf, fd;
+
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ t = 0;
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+
+ if ((fd = open(outputFile, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1, "open(%s)", outputFile);
+
+ for (;;) {
+ if ((n = read(msgsock, buf, bufsize)) < 0)
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ t += n;
+ if (n == 0)
+ break;
+
+ if ((write(fd, buf, n)) != n)
+ err(1, "write");
+ }
+ close(msgsock);
+ close(fd);
+ return;
+}
+
+static void
+writer(void) {
+ int tcpsock, on;
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ struct stat statb;
+ off_t off = 0;
+ size_t size;
+ int i, r, fd;
+
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ size = getpagesize() -4;
+ if (setsockopt(tcpsock, SOL_SOCKET, SO_SNDBUF, (void *)&size,
+ sizeof(size)) < 0)
+ err(1, "setsockopt(SO_SNDBUF), %s:%d",
+ __FILE__, __LINE__);
+
+ hostent = gethostbyname ("localhost");
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if (stat(inputFile, &statb) != 0)
+ err(1, "stat(%s)", inputFile);
+
+ if ((fd = open(inputFile, O_RDONLY)) == -1)
+ err(1, "open(%s)", inputFile);
+
+ off = 12 * 32 * 1024;
+ if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1)
+ err(1, "sendfile");
+
+ return;
+}
+
+int
+main(int argc, char **argv)
+{
+ pid_t pid;
+
+ if (argc != 4) {
+ fprintf(stderr, "Usage: %s <inputFile outputFile portNumber\n",
+ argv[0]);
+ return (1);
+ }
+ inputFile = argv[1];
+ outputFile = argv[2];
+ port = atoi(argv[3]);
+
+ if ((pid = fork()) == 0) {
+ writer();
+ exit(EXIT_SUCCESS);
+ } else if (pid > 0) {
+ reader();
+ kill(pid, SIGINT);
+ } else
+ err(1, "fork(), %s:%d", __FILE__, __LINE__);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sendfile26.sh b/tools/test/stress2/misc/sendfile26.sh
new file mode 100755
index 000000000000..da9aa52d5f3c
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile26.sh
@@ -0,0 +1,308 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Jean-Sébastien Pédron <dumbbell@FreeBSD.org>
+#
+# 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 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.
+#
+
+# "Written file doesn't match memory buffer" seen on main-n244961-e6bb49f12ca
+# https://reviews.freebsd.org/D28811
+
+. ../default.cfg
+kldstat -v | grep -q zfs.ko || { kldload zfs.ko ||
+ exit 0; loaded=1; }
+
+cat > /tmp/write_vs_sendfile.c <<EOF
+#include <sys/errno.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/uio.h>
+#include <netinet/in.h>
+
+#include <assert.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+#include <unistd.h>
+
+#define FILENAME "myfile.bin"
+#define MAX_SIZE 50 * 1000 * 1000
+
+int tcp_port;
+int chunk_size;
+
+static void *
+sender_start(void *buffer __unused)
+{
+ int fd, sock, ret;
+ struct sockaddr_in sa = { 0 };
+ off_t cursor;
+
+ printf("Sender: opening connection to TCP port %d\n", tcp_port);
+ sock = socket(AF_INET, SOCK_STREAM, 0);
+ if (sock < 0) {
+ perror("Sender: failed to create socket");
+ return (NULL);
+ }
+
+ sa.sin_family = AF_INET;
+ sa.sin_port = htons(tcp_port);
+ sa.sin_addr.s_addr = htonl((((((127 << 8) | 0) << 8) | 0) << 8) | 1);
+
+ ret = connect(sock, (struct sockaddr *)&sa, sizeof(sa));
+ if (ret < 0) {
+ perror("Sender: failed to connect to localhost");
+ close(sock);
+ return (NULL);
+ }
+
+ printf("Sender: opening %s\n", FILENAME);
+ fd = open(FILENAME, O_CREAT|O_RDONLY, 0644);
+ if (fd < 0) {
+ perror("Sender: failed to open file");
+ close(sock);
+ return (NULL);
+ }
+
+ printf("Sender: starting sendfile(2) loop\n");
+ cursor = 0;
+ do {
+ size_t to_send = chunk_size;
+ off_t sbytes = 0;
+
+ do {
+#if defined(__FreeBSD__)
+ ret = sendfile(fd, sock, cursor, to_send,
+ NULL, &sbytes, 0);
+ if (ret == 0) {
+ cursor += sbytes;
+ to_send -= sbytes;
+ }
+#elif defined(__APPLE__)
+ sbytes = to_send;
+ ret = sendfile(fd, sock, cursor, &sbytes, NULL, 0);
+ if (ret < 0 && (errno == EAGAIN || errno == EINTR)) {
+ ret = 0;
+ }
+ if (ret == 0) {
+ ret = 0;
+ cursor += sbytes;
+ to_send -= sbytes;
+ }
+#else
+#error Not implemented
+#endif
+ } while (ret == 0 && to_send > 0);
+ } while (cursor < MAX_SIZE);
+
+ printf("Sender: closing socket\n");
+ close(fd);
+ printf("Sender: closing %s\n", FILENAME);
+ close(sock);
+
+ return (NULL);
+}
+
+static void *
+writer_start(void *buffer)
+{
+ int fd, cursor, ret;
+
+ printf("Writer: opening %s\n", FILENAME);
+ fd = open(FILENAME, O_CREAT|O_RDWR|O_TRUNC|O_DIRECT, 0644);
+ if (fd < 0) {
+ perror("Writer: failed to open file");
+ return (NULL);
+ }
+
+ /* We sleep one second to give a head start to the sendfile(2) thread
+ * above. */
+ sleep(1);
+
+ printf(
+ "Writer: writing chunks of %u bytes to a max of %u bytes\n",
+ chunk_size, MAX_SIZE);
+ cursor = 0;
+ do {
+ ret = write(fd, buffer, chunk_size);
+ if (ret < 0) {
+ perror("Writer: failed to write file");
+ break;
+ }
+ assert(ret == chunk_size);
+
+ cursor += ret;
+ } while (cursor < MAX_SIZE);
+
+ printf("Writer: closing %s\n", FILENAME);
+ close(fd);
+
+ return (NULL);
+}
+
+int
+check_file(void *buffer, int flags)
+{
+ int fd, ret, cursor;
+ void *read_buffer;
+
+ printf("Writer: opening %s\n", FILENAME);
+ fd = open(FILENAME, O_RDONLY | flags | O_DIRECT);
+ if (fd < 0) {
+ perror("Checker: failed to open file");
+ return (1);
+ }
+
+ read_buffer = malloc(chunk_size);
+ if (buffer == NULL) {
+ perror("Checker: failed to allocate buffer");
+ close(fd);
+ return (1);
+ }
+
+ cursor = 0;
+ do {
+ ret = read(fd, read_buffer, chunk_size);
+ if (ret < 0) {
+ perror("Checker: failed to read file");
+ close(fd);
+ free(read_buffer);
+ return (1);
+ }
+ assert(ret == chunk_size);
+
+ cursor += ret;
+
+ ret = memcmp(buffer, read_buffer, chunk_size);
+ } while (ret == 0 && cursor < MAX_SIZE);
+
+ return (ret);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int ret;
+ void *buffer;
+ pthread_t sender;
+ pthread_t writer;
+
+ /* The sender thread will connect to the TCP port on the local host.
+ * The user is responsible for starting an instance of netcat like
+ * this:
+ *
+ * nc -k -l 8080
+ */
+ tcp_port = argc >= 2 ? atoi(argv[1]) : 8080;
+ chunk_size = argc >= 3 ? atoi(argv[2]) : 32128;
+
+ /* We initialize a buffer and fill it with 0xff bytes. The buffer is
+ * written many times to a file by the writer thread (see below). */
+ buffer = malloc(chunk_size);
+ if (buffer == NULL) {
+ perror("Main: failed to allocate buffer");
+ return (1);
+ }
+
+ memset(buffer, 255, chunk_size);
+
+ unlink(FILENAME);
+
+ /* The sender thread is responsible for sending the file written by the
+ * writer thread to a local TCP port. The goal is always try to send
+ * data which is not written to the file yet. */
+ ret = pthread_create(&sender, NULL, &sender_start, buffer);
+ if (ret != 0) {
+ free(buffer);
+ return (2);
+ }
+
+ /* The writer thread is responsible for writing the allocated buffer to
+ * the file until it reaches a size of 50 MB. */
+ ret = pthread_create(&writer, NULL, &writer_start, buffer);
+ if (ret != 0) {
+ pthread_cancel(sender);
+ pthread_join(sender, NULL);
+ free(buffer);
+ return (2);
+ }
+
+ pthread_join(writer, NULL);
+ pthread_cancel(sender);
+ pthread_join(sender, NULL);
+
+ /* Now that both threads terminated, we check the content of the
+ * written file. The bug on ZFS on FreeBSD is that some portions of the
+ * file contains zeros instead of the expected 0xff bytes. */
+ ret = check_file(buffer, 0);
+ free(buffer);
+
+ if (ret != 0) {
+ fprintf(stderr, "\033[1;31mWritten file doesn't match memory buffer\033[0m\n");
+ }
+
+ return (ret);
+}
+EOF
+
+mycc -o /tmp/write_vs_sendfile -Wall -Wextra -O2 /tmp/write_vs_sendfile.c -lpthread || exit 1
+rm /tmp/write_vs_sendfile.c
+
+u1=$mdstart
+u2=$((u1 + 1))
+
+mdconfig -l | grep -q md$u1 && mdconfig -d -u $u1
+mdconfig -l | grep -q md$u2 && mdconfig -d -u $u2
+
+mdconfig -s 2g -u $u1
+mdconfig -s 2g -u $u2
+
+zpool list | egrep -q "^stress2_tank" && zpool destroy stress2_tank
+[ -d /stress2_tank ] && rm -rf /stress2_tank
+zpool create stress2_tank raidz md$u1 md$u2
+zfs create stress2_tank/test
+
+here=`pwd`
+cd /stress2_tank/test
+nc -k -l 8080 >/dev/null &
+sleep .5
+/tmp/write_vs_sendfile; s=$?
+kill $!
+wait
+cd $here
+
+zfs umount stress2_tank/test
+zfs destroy -r stress2_tank
+zpool destroy stress2_tank
+
+mdconfig -d -u $u1
+mdconfig -d -u $u2
+[ -n "$loaded" ] && kldunload zfs.ko
+rm /tmp/write_vs_sendfile
+exit $s
diff --git a/tools/test/stress2/misc/sendfile3.sh b/tools/test/stress2/misc/sendfile3.sh
new file mode 100755
index 000000000000..f46d02d906d8
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile3.sh
@@ -0,0 +1,210 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# Test scenario for sendfile deadlock (processes stuck in sfbufa)
+
+# Variation of sendfile.sh
+# kern.ipc.nsfbufs should be low for this test
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > sendfile3.c
+mycc -o sendfile3 -Wall -Wextra sendfile3.c
+rm -f sendfile3.c
+[ -d "$RUNDIR" ] || mkdir -p $RUNDIR
+cd $RUNDIR
+
+in=inputFile
+out=outputFile
+parallel=20
+
+for i in 50m 100m; do
+ rm -f $in
+ dd if=/dev/random of=$in bs=$i count=1 status=none
+ for j in `jot $parallel`; do
+ rm -f ${out}$j
+ /tmp/sendfile3 $in ${out}$j 1234$j &
+ done
+ wait
+ for j in `jot $parallel`; do
+ rm -f ${out}$j
+ done
+done
+rm -f $in /tmp/sendfile3
+exit
+EOF
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int port;
+char *inputFile;
+char *outputFile;
+int bufsize = 4096;
+
+static void
+reader(void) {
+ int tcpsock, msgsock;
+ int on;
+ socklen_t len;
+ struct sockaddr_in inetaddr, inetpeer;
+ int n, t, *buf, fd;
+
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ t = 0;
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+
+ if ((fd = open(outputFile, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1, "open(%s)", outputFile);
+
+ for (;;) {
+ if ((n = read(msgsock, buf, bufsize)) < 0)
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ t += n;
+ if (n == 0) break;
+
+ if ((write(fd, buf, n)) != n)
+ err(1, "write");
+ }
+ close(msgsock);
+ close(fd);
+ return;
+}
+
+static void
+writer(void) {
+ int tcpsock, on;
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ struct stat statb;
+ int i, r, fd;
+ off_t off = 0;
+
+ on = 1;
+ for (i = 0; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ bzero((char *) &inetaddr, sizeof(inetaddr));
+ hostent = gethostbyname ("localhost");
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if (stat(inputFile, &statb) != 0)
+ err(1, "stat(%s)", inputFile);
+
+ if ((fd = open(inputFile, O_RDONLY)) == -1)
+ err(1, "open(%s)", inputFile);
+
+ if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1)
+ err(1, "sendfile");
+}
+
+int
+main(int argc, char **argv)
+{
+ pid_t pid;
+
+ if (argc != 4) {
+ fprintf(stderr, "Usage: %s <inputFile outputFile portNumber\n", argv[0]);
+ return (1);
+ }
+ inputFile = argv[1];
+ outputFile = argv[2];
+ port = atoi(argv[3]);
+
+ if ((pid = fork()) == 0) {
+ writer();
+ exit(EXIT_SUCCESS);
+ } else if (pid > 0) {
+ reader();
+ kill(pid, SIGINT);
+ waitpid(pid, NULL, 0);
+ } else
+ err(1, "fork(), %s:%d", __FILE__, __LINE__);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sendfile4.sh b/tools/test/stress2/misc/sendfile4.sh
new file mode 100755
index 000000000000..b327c8bfb6e2
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile4.sh
@@ -0,0 +1,141 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# sendfile(2) test by kib@
+# Both processes will sit in the sbwait state forever:
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > sendfile4.c
+mycc -o sendfile4 -Wall -Wextra -O2 sendfile4.c || exit
+rm -f sendfile4.c
+cd $here
+
+dd if=/dev/zero of=$diskimage bs=1m count=6 status=none
+/tmp/sendfile4 $diskimage || echo FAIL
+
+rm -f /tmp/sendfile4 $diskimage
+exit
+EOF
+#include <sys/types.h>
+#include <sys/fcntl.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
+#include <sys/wait.h>
+#include <err.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static void
+handler(int i __unused) {
+ _exit(1);
+}
+
+int
+main(int argc, char *argv[])
+{
+ const char *from_name;
+ char *buf;
+ int sv[2];
+ struct stat st;
+ off_t written, pos;
+ int child, error, from, status;
+
+ if (argc != 2)
+ errx(1, "Usage: %s from", argv[0]);
+ from_name = argv[1];
+
+ from = open(from_name, O_RDONLY);
+ if (from == -1)
+ err(1, "open read %s", from_name);
+
+ error = fstat(from, &st);
+ if (error == -1)
+ err(1, "stat %s", from_name);
+
+ error = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
+ if (error == -1)
+ err(1, "socketpair");
+
+ signal(SIGALRM, handler);
+ alarm(120);
+ child = fork();
+ if (child == -1)
+ err(1, "fork");
+ else if (child != 0) {
+ close(sv[1]);
+ pos = 0;
+ for (;;) {
+ error = sendfile(from, sv[0], pos, st.st_size - pos,
+ NULL, &written, 0);
+ if (error == -1) {
+ if (errno != EAGAIN)
+ err(1, "sendfile");
+ }
+ pos += written;
+ if (pos == st.st_size)
+ break;
+ }
+ close(sv[0]);
+ waitpid(child, &status, 0);
+ } else {
+ close(sv[0]);
+ buf = malloc(st.st_size);
+ if (buf == NULL)
+ err(1, "malloc %jd", st.st_size);
+ pos = 0;
+ for (;;) {
+ written = 413;
+ if (written > st.st_size - pos)
+ written = st.st_size - pos;
+#if 0
+ written = st.st_size - pos;
+ if (written > 1000)
+ written = 1000;
+ written = arc4random_uniform(written) + 1;
+#endif
+ error = read(sv[1], buf + pos, written);
+ if (error == -1)
+ err(1, "read");
+ else if (error == 0)
+ errx(1, "short read");
+ pos += error;
+ if (pos == st.st_size)
+ break;
+ }
+ close(sv[1]);
+ _exit(0);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sendfile5.sh b/tools/test/stress2/misc/sendfile5.sh
new file mode 100755
index 000000000000..5bae03441779
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile5.sh
@@ -0,0 +1,165 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# sendfile(2) test by kib@
+# Deadlock seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+file=`basename $diskimage`
+dir=`dirname $diskimage`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > sendfile5.c
+mycc -o sendfile5 -Wall -Wextra -O2 sendfile5.c
+rm -f sendfile5.c
+need=1024
+[ `df -k $(dirname $diskimage) | tail -1 | awk '{print int($4 / 1024)}'` \
+ -lt $need ] &&
+ printf "Need %d MB on %s.\n" $need `dirname $diskimage` && exit 0
+dd if=/dev/zero of=$diskimage bs=1m count=$need status=none
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+kldstat | grep -q tmpfs.ko || loaded=1
+mount -t tmpfs tmpfs $mntpoint
+echo "Testing tmpfs(5)"
+cp $diskimage $mntpoint
+/tmp/sendfile5 $mntpoint/$file
+umount $mntpoint
+[ $loaded ] && kldunload tmpfs.ko
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+echo "Testing FFS"
+cp $diskimage $mntpoint
+/tmp/sendfile5 $mntpoint/$file
+umount $mntpoint
+mdconfig -d -u $mdstart
+
+mount -t nullfs $dir $mntpoint
+echo "Testing nullfs(5)"
+/tmp/sendfile5 $mntpoint/$file
+umount $mntpoint
+
+rm -f /tmp/sendfile5 $diskimage
+exit
+EOF
+#include <sys/types.h>
+#include <sys/fcntl.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
+#include <sys/wait.h>
+#include <err.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(int argc, char *argv[])
+{
+ const char *from_name;
+ char *buf;
+ int sv[2];
+ struct stat st;
+ off_t written, pos;
+ int child, error, from, status;
+
+ if (argc != 2)
+ errx(1, "Usage: %s from", argv[0]);
+ from_name = argv[1];
+
+ from = open(from_name, O_RDONLY);
+ if (from == -1)
+ err(1, "open read %s", from_name);
+
+ error = fstat(from, &st);
+ if (error == -1)
+ err(1, "stat %s", from_name);
+
+ error = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
+ if (error == -1)
+ err(1, "socketpair");
+
+ child = fork();
+ if (child == -1)
+ err(1, "fork");
+ else if (child != 0) {
+ close(sv[1]);
+ pos = 0;
+ for (;;) {
+ error = sendfile(from, sv[0], pos, st.st_size - pos,
+ NULL, &written, 0);
+ if (error == -1) {
+ if (errno != EAGAIN)
+ err(1, "sendfile");
+ }
+ pos += written;
+ if (pos == st.st_size)
+ break;
+ }
+ close(sv[0]);
+ waitpid(child, &status, 0);
+ } else {
+ close(sv[0]);
+ buf = malloc(st.st_size);
+ if (buf == NULL)
+ err(1, "malloc %jd", st.st_size);
+ pos = 0;
+ for (;;) {
+ written = 413;
+ if (written > st.st_size - pos)
+ written = st.st_size - pos;
+#if 0
+ written = st.st_size - pos;
+ if (written > 1000)
+ written = 1000;
+ written = arc4random_uniform(written) + 1;
+#endif
+ error = read(sv[1], buf + pos, written);
+ if (error == -1)
+ err(1, "read");
+ else if (error == 0)
+ errx(1, "short read");
+ pos += error;
+ if (pos == st.st_size)
+ break;
+ }
+ close(sv[1]);
+ _exit(0);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sendfile6.sh b/tools/test/stress2/misc/sendfile6.sh
new file mode 100755
index 000000000000..dd130b43d401
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile6.sh
@@ -0,0 +1,255 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# Test scenario by Nate Eldredge neldredge math ucsdnedu
+# kern/127213: [tmpfs] sendfile on tmpfs data corruption
+# Variation of tmpfs7.sh where UFS is used instead of tmpfs.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+set -e
+
+odir=`pwd`
+cd /tmp
+cat > sendfile6_server.c <<EOF
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include "util.h"
+
+int main(int argc, char *argv[]) {
+ int f, listener, connection;
+ if (argc < 3) {
+ fprintf(stderr, "Usage: %s filename socketname\n", argv[0]);
+ exit(1);
+ }
+ if ((f = open(argv[1], O_RDONLY)) < 0) {
+ perror(argv[1]);
+ exit(1);
+ }
+ if ((listener = listen_unix_socket(argv[2])) < 0) {
+ exit(1);
+ }
+ if ((connection = accept_unix_socket(listener)) >= 0) {
+ real_sendfile(f, connection);
+ }
+ return 0;
+}
+EOF
+cat > sendfile6_client.c <<EOF
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include "util.h"
+
+int main(int argc, char *argv[]) {
+ int s;
+ if (argc < 2) {
+ fprintf(stderr, "Usage: %s socketname\n", argv[0]);
+ exit(1);
+ }
+ if ((s = connect_unix_socket(argv[1])) < 0) {
+ exit(1);
+ }
+ fake_sendfile(s, 1);
+ return 0;
+}
+EOF
+
+cat > util.c <<EOF
+/* send data from file to unix domain socket */
+
+#include <stdio.h>
+#include <time.h>
+#include <signal.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int create_unix_socket(void) {
+ int fd;
+ if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
+ perror("socket");
+ return -1;
+ }
+ return fd;
+}
+
+int make_unix_sockaddr(const char *pathname, struct sockaddr_un *sa) {
+ memset(sa, 0, sizeof(*sa));
+ sa->sun_family = PF_LOCAL;
+ if (strlen(pathname) + 1 > sizeof(sa->sun_path)) {
+// fprintf(stderr, "%s: pathname too long (max %lu)\n",
+// pathname, sizeof(sa->sun_path));
+ errno = ENAMETOOLONG;
+ return -1;
+ }
+ strcpy(sa->sun_path, pathname);
+ return 0;
+}
+
+static char *sockname;
+void delete_socket(void) {
+ unlink(sockname);
+}
+
+int listen_unix_socket(const char *path) {
+ int fd;
+ struct sockaddr_un sa;
+ if (make_unix_sockaddr(path, &sa) < 0)
+ return -1;
+ if ((fd = create_unix_socket()) < 0)
+ return -1;
+ if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
+ perror("bind");
+ close(fd);
+ return -1;
+ }
+ sockname = strdup(path);
+ atexit(delete_socket);
+
+ if (listen(fd, 5) < 0) {
+ perror("listen");
+ close(fd);
+ return -1;
+ }
+ return fd;
+}
+
+int accept_unix_socket(int fd) {
+ int s;
+ if ((s = accept(fd, NULL, 0)) < 0) {
+ perror("accept");
+ return -1;
+ }
+ return s;
+}
+
+int connect_unix_socket(const char *path) {
+ int fd;
+ struct sockaddr_un sa;
+ if (make_unix_sockaddr(path, &sa) < 0)
+ return -1;
+ if ((fd = create_unix_socket()) < 0)
+ return -1;
+ if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
+ perror("connect");
+ return -1;
+ }
+ return fd;
+}
+
+#define BUFSIZE 65536
+
+int fake_sendfile(int from, int to) {
+ char buf[BUFSIZE];
+ int v;
+ int sent = 0;
+ while ((v = read(from, buf, BUFSIZE)) > 0) {
+ int d = 0;
+ while (d < v) {
+ int w = write(to, buf, v - d);
+ if (w <= 0) {
+ perror("write");
+ return -1;
+ }
+ d += w;
+ sent += w;
+ }
+ }
+ if (v != 0) {
+ perror("read");
+ return -1;
+ }
+ return sent;
+}
+
+int real_sendfile(int from, int to) {
+ int v;
+ v = sendfile(from, to, 0, 0, NULL, NULL, 0);
+ if (v < 0) {
+ perror("sendfile");
+ }
+ return v;
+}
+EOF
+
+cat > util.h <<EOF
+/* send data from file to unix domain socket */
+
+#include <stdio.h>
+#include <time.h>
+#include <signal.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+int create_unix_socket(void);
+int make_unix_sockaddr(const char *pathname, struct sockaddr_un *sa);
+int listen_unix_socket(const char *path);
+int accept_unix_socket(int fd);
+int connect_unix_socket(const char *path);
+int fake_sendfile(int from, int to);
+int real_sendfile(int from, int to);
+EOF
+
+mycc -c -Wall -Wextra -O2 util.c
+mycc -o sendfile6_server -Wall -Wextra -O2 sendfile6_server.c util.o
+mycc -o sendfile6_client -Wall -Wextra -O2 sendfile6_client.c util.o
+rm -f sendfile6_server.c sendfile6_client.c util.c util.o util.h mysocket
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+dd if=/dev/random of=$mntpoint/data bs=123456 count=1 status=none
+./sendfile6_server $mntpoint/data mysocket &
+sleep 0.2
+./sendfile6_client mysocket > data.$$
+wait
+cmp $mntpoint/data data.$$ ||
+ { echo "FAIL Data mismatch"; ls -l $mntpoint/data data.$$; }
+rm -f data.$$ sendfile6_server sendfile6_client mysocket
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/sendfile7.sh b/tools/test/stress2/misc/sendfile7.sh
new file mode 100755
index 000000000000..da27a8eb2724
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile7.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# Run sendfile5.sh with VM pressure added.
+# "panic: vm_page_requeue: page 0xc47fdcc0 is not queued" seen.
+
+# OOM seen:
+# https://people.freebsd.org/~pho/stress/log/sendfile7.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+./sendfile5.sh &
+
+while kill -0 $! 2>/dev/null; do
+ ../testcases/swap/swap -t 2m -i 40 > /dev/null 2>&1
+done
+wait
diff --git a/tools/test/stress2/misc/sendfile8.sh b/tools/test/stress2/misc/sendfile8.sh
new file mode 100755
index 000000000000..ddbcb74ffc02
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile8.sh
@@ -0,0 +1,267 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# sendfile(2) + parallel file size change. Bug 217789.
+# "panic: Assertion size > 0 failed at ../../../kern/subr_vmem.c:1082" seen:
+# Fixed in r315910
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile8.c
+mycc -o sendfile8 -Wall -Wextra -O0 -g sendfile8.c || exit 1
+rm -f sendfile8.c
+cd $odir
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+cd $mntpoint
+dd if=/dev/random of=in bs=1m count=50 status=none
+/tmp/sendfile8 in out 76543
+s=$?
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1
+mdconfig -d -u $mdstart
+rm -rf /tmp/sendfile8
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static int port;
+static char *input, *output;
+
+#define BUFSIZE 4096
+#define PARALLEL 1
+#define RUNTIME (1 * 60)
+
+static void
+mess(void)
+{
+ off_t length;
+ int fd;
+
+ if ((fd = open(input, O_RDWR)) == -1)
+ err(1, "open(%s)", input);
+ length = arc4random() % 100 * 1024 * 1024;
+ if (ftruncate(fd, length) == -1)
+ err(1, "truncate(%jd)", length);
+ close(fd);
+}
+
+static void
+reader(void) {
+ struct sockaddr_in inetaddr, inetpeer;
+ socklen_t len;
+ int tcpsock, msgsock;
+ int on;
+ int n, t, *buf, fd;
+
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ t = 0;
+ if ((buf = malloc(BUFSIZE)) == NULL)
+ err(1, "malloc(%d), %s:%d", BUFSIZE, __FILE__, __LINE__);
+
+ if ((fd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1, "open(%s)", output);
+
+ for (;;) {
+ if ((n = read(msgsock, buf, BUFSIZE)) < 0)
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ t += n;
+ if (n == 0) break;
+
+ if ((write(fd, buf, n)) != n)
+ err(1, "write");
+ }
+ close(msgsock);
+ close(fd);
+ return;
+}
+
+static void
+writer(void) {
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ struct stat statb;
+ off_t off = 0;
+ size_t size;
+ int i, r, fd;
+ int tcpsock, on;
+
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ size = getpagesize() -4;
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_SNDBUF, (void *)&size, sizeof(size)) < 0)
+ err(1, "setsockopt(SO_SNDBUF), %s:%d", __FILE__,
+ __LINE__);
+
+ hostent = gethostbyname ("localhost");
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if (stat(input, &statb) != 0)
+ err(1, "stat(%s)", input);
+
+ if ((fd = open(input, O_RDONLY)) == -1)
+ err(1, "open(%s)", input);
+
+ if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1)
+ err(1, "sendfile");
+
+ return;
+}
+
+static void
+test(void)
+{
+ pid_t pid;
+
+ if ((pid = fork()) == 0) {
+ writer();
+ _exit(0);
+ }
+ reader();
+ kill(pid, SIGINT);
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ time_t start;
+ int e, i, j, pids[PARALLEL], status;
+
+ if (argc != 4) {
+ fprintf(stderr, "Usage: %s <input file> <output file>\n",
+ argv[0]);
+ exit(1);
+ }
+ input = argv[1];
+ output = argv[2];
+ port = atoi(argv[3]);
+ e = 0;
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ for (j = 0; j < 100; j++) {
+ mess();
+ usleep(arc4random() % 10000);
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/sendfile9.sh b/tools/test/stress2/misc/sendfile9.sh
new file mode 100755
index 000000000000..989098e09321
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile9.sh
@@ -0,0 +1,266 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# "kmem are D+ 0 0:00,10 /tmp/sendfile9 in out 76543" seen.
+# Fixed in r315910
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile9.c
+mycc -o sendfile9 -Wall -Wextra -O0 -g sendfile9.c || exit 1
+rm -f sendfile9.c
+cd $odir
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags -n md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+cd $mntpoint
+dd if=/dev/random of=in bs=1m count=50 status=none
+/tmp/sendfile9 in out 76543
+s=$?
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1
+mdconfig -d -u $mdstart
+rm -rf /tmp/sendfile9
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static int port;
+static char *input, *output;
+
+#define BUFSIZE 4096
+#define PARALLEL 1
+#define RUNTIME (1 * 60)
+
+static void
+mess(void)
+{
+ off_t length;
+ int fd;
+
+ if ((fd = open(input, O_RDWR)) == -1)
+ err(1, "open(%s)", input);
+ length = arc4random() % 100 * 1024 * 1024;
+ if (ftruncate(fd, length) == -1)
+ err(1, "truncate(%jd)", length);
+ close(fd);
+}
+
+static void
+reader(void) {
+ struct sockaddr_in inetaddr, inetpeer;
+ socklen_t len;
+ int tcpsock, msgsock;
+ int on;
+ int n, t, *buf, fd;
+
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ t = 0;
+ if ((buf = malloc(BUFSIZE)) == NULL)
+ err(1, "malloc(%d), %s:%d", BUFSIZE, __FILE__, __LINE__);
+
+ if ((fd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1, "open(%s)", output);
+
+ for (;;) {
+ if ((n = read(msgsock, buf, BUFSIZE)) < 0)
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ t += n;
+ if (n == 0) break;
+
+ if ((write(fd, buf, n)) != n)
+ err(1, "write");
+ }
+ close(msgsock);
+ close(fd);
+ return;
+}
+
+static void
+writer(void) {
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ struct stat statb;
+ off_t off = 0;
+ size_t size;
+ int i, r, fd;
+ int tcpsock, on;
+
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ size = getpagesize() -4;
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_SNDBUF, (void *)&size, sizeof(size)) < 0)
+ err(1, "setsockopt(SO_SNDBUF), %s:%d", __FILE__,
+ __LINE__);
+
+ hostent = gethostbyname ("localhost");
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if (stat(input, &statb) != 0)
+ err(1, "stat(%s)", input);
+
+ if ((fd = open(input, O_RDONLY)) == -1)
+ err(1, "open(%s)", input);
+
+ if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1)
+ err(1, "sendfile");
+
+ return;
+}
+
+static void
+test(void)
+{
+ pid_t pid;
+
+ if ((pid = fork()) == 0) {
+ writer();
+ _exit(0);
+ }
+ reader();
+ kill(pid, SIGINT);
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ time_t start;
+ int e, i, j, pids[PARALLEL], status;
+
+ if (argc != 4) {
+ fprintf(stderr, "Usage: %s <input file> <output file>\n",
+ argv[0]);
+ exit(1);
+ }
+ input = argv[1];
+ output = argv[2];
+ port = atoi(argv[3]);
+ e = 0;
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ for (j = 0; j < 100; j++) {
+ mess();
+ usleep(arc4random() % 10000);
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/sendfile_shm.sh b/tools/test/stress2/misc/sendfile_shm.sh
new file mode 100755
index 000000000000..8f0fd8bb0ed7
--- /dev/null
+++ b/tools/test/stress2/misc/sendfile_shm.sh
@@ -0,0 +1,200 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Sendfile(2) over posix shmfd
+# Test scenario by kib@
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+[ -r /boot/kernel/kernel ] || exit 0
+here=`pwd`
+dir=`dirname $diskimage`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > sendfile_shm.c
+mycc -o sendfile_shm -Wall -Wextra -O2 sendfile_shm.c || exit 1
+rm -f sendfile_shm.c
+cd $here
+
+daemon ../testcases/swap/swap -t 2m -i 20 > /dev/null 2>&1
+sleep 5
+for i in `jot 10`; do
+ /tmp/sendfile_shm /boot/kernel/kernel $dir/sendfile_shm.$i > \
+ /dev/null &
+done
+wait
+while pkill -9 swap; do
+ sleep .5
+done
+for i in `jot 10`; do
+ cmp -s /boot/kernel/kernel $dir/sendfile_shm.$i 2>/dev/null ||
+ { e=1; ls -al $dir/sendfile_shm.* /boot/kernel/kernel; }
+ rm -f $dir/sendfile_shm.$i
+ [ $e ] && break
+done
+[ -n "$e" ] && echo FAIL
+wait
+
+rm -f /tmp/sendfile_shm
+exit
+EOF
+/* $Id: sendfile_shm1.c,v 1.2 2013/08/25 14:35:14 kostik Exp kostik $ */
+
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/uio.h>
+#include <err.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define PAGE_SIZE 4096
+
+static void
+load(int infd, int shmfd, off_t size)
+{
+ off_t c, r;
+ char buf[100];
+
+ for (c = 0; c < size; c += r) {
+ r = read(infd, buf, sizeof(buf));
+ if (r == -1)
+ err(1, "read disk");
+ else if (r == 0)
+ errx(1, "too short");
+ write(shmfd, buf, r);
+ }
+}
+
+static void
+sendfd(int s, int shmfd, off_t size)
+{
+ off_t sbytes;
+ int error;
+
+ error = sendfile(shmfd, s, 0, size, NULL, &sbytes, 0);
+ if (error == -1)
+ err(1, "sendfile(%d, %d, %jd)", s, shmfd, size);
+ printf("sent %jd bytes, requested %jd\n", (uintmax_t)sbytes,
+ (uintmax_t)size);
+}
+
+static void
+receivefd(int s, int outfd, off_t size)
+{
+ char buf[100];
+ off_t c, r, r1;
+
+ for (c = 0; c < size; c += r) {
+ r = read(s, buf, sizeof(buf));
+ if (r == -1)
+ err(1, "read sock");
+ else if (r == 0)
+ break;
+ else {
+ r1 = write(outfd, buf, r);
+ if (r1 == -1)
+ err(1, "write disk");
+ else if (r1 != r) {
+ err(1, "short write %jd %jd",
+ (uintmax_t)r, (uintmax_t)r1);
+ }
+ }
+ }
+ printf("received %jd bytes\n", (uintmax_t)c);
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct stat st;
+ struct sigaction sa;
+ pid_t child;
+ int s[2], infd, outfd, shmfd, error;
+
+ if (argc != 3) {
+ fprintf(stderr, "usage: sendfile_shm infile outfile\n");
+ exit(2);
+ }
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_handler = SIG_IGN;
+ error = sigaction(SIGPIPE, &sa, NULL);
+ if (error == -1)
+ err(1, "sigaction SIGPIPE");
+
+ infd = open(argv[1], O_RDONLY);
+ if (infd == -1)
+ err(1, "open %s", argv[1]);
+ error = fstat(infd, &st);
+ if (error == -1)
+ err(1, "stat");
+
+ outfd = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0666);
+ if (outfd == -1)
+ err(1, "open %s", argv[2]);
+
+ shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
+ if (shmfd == -1)
+ err(1, "shm_open");
+ error = ftruncate(shmfd, st.st_size);
+ if (error == -1)
+ err(1, "ftruncate");
+ load(infd, shmfd, st.st_size);
+
+ error = socketpair(AF_UNIX, SOCK_STREAM, 0, s);
+ if (error == -1)
+ err(1, "socketpair");
+
+ fflush(stdout);
+ fflush(stderr);
+ child = fork();
+ if (child == -1)
+ err(1, "fork");
+ else if (child == 0) {
+ close(s[1]);
+ sendfd(s[0], shmfd, st.st_size);
+ exit(0);
+ } else {
+ close(shmfd);
+ close(s[0]);
+ sleep(1);
+ receivefd(s[1], outfd, st.st_size);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sendmsg.sh b/tools/test/stress2/misc/sendmsg.sh
new file mode 100755
index 000000000000..a76a90ec956d
--- /dev/null
+++ b/tools/test/stress2/misc/sendmsg.sh
@@ -0,0 +1,224 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# sendmsg(2) fuzz test.
+
+# Looping test program seen:
+# https://people.freebsd.org/~pho/stress/log/sendmsg.txt
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sendmsg.c
+mycc -o sendmsg -Wall -Wextra -O0 -g sendmsg.c || exit 1
+rm -f sendmsg.c
+cd $odir
+
+daemon sh -c "(cd ../testcases/swap; ./swap -t 5m -i 20 -k -h)" > /dev/null
+sleep 2
+
+/tmp/sendmsg 2>/dev/null
+
+while pgrep -q swap; do
+ pkill -9 swap
+done
+rm -f /tmp/sendmsg sendmsg.core
+
+n=0
+while pgrep -q sendmsg; do
+ pkill -9 sendmsg
+ n=$((n + 1))
+ [ $n -gt 20 ] && { echo "Looping sendmsg"; exit 1; }
+ sleep 1
+done
+exit 0
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+volatile u_int *share;
+
+#define PARALLEL 16
+#define RUNTIME (5 * 60)
+#define SYNC 0
+
+int
+setflag(void)
+{
+ int flag, i;
+
+ i = arc4random() % 100;
+
+ if (i < 33)
+ flag = 0;
+ else if (i >= 33 && i < 66)
+ flag = 2 << (arc4random() % 9);
+ else
+ flag = arc4random();
+
+ return(flag);
+}
+
+void
+corrupt(unsigned char *buf, int len)
+{
+ unsigned char byte, mask;
+ int bit, i;
+
+ i = arc4random() % len;
+ byte = buf[i];
+ bit = arc4random() % 8;
+ mask = ~(1 << bit);
+ byte = (byte & mask) | (~byte & ~mask);
+ buf[i] = byte;
+}
+
+/*
+ Based on https://www.win.tue.nl/~aeb/linux/lk/sendfd.c
+ */
+void
+test(void)
+{
+ struct cmsghdr *cmsg;
+ struct msghdr msg;
+ pid_t pid;
+ int fd, flag, n, pair[2];
+ char buf[1024];
+ char fdbuf[CMSG_SPACE(sizeof(int))];
+
+ /* dummy */
+ struct iovec vec;
+ char ch = '\0';
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ memset(&msg, 0, sizeof(msg));
+
+ /* having zero msg_iovlen or iov_len doesnt seem to work */
+ vec.iov_base = &ch;
+ vec.iov_len = 1;
+ msg.msg_iov = &vec;
+ msg.msg_iovlen = 1;
+
+ msg.msg_control = fdbuf;
+ msg.msg_controllen = CMSG_LEN(sizeof(int));
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+
+ if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair))
+ err(1, "socketpair");
+
+ if ((pid = fork()) == -1)
+ err(1, "fork");
+
+ if (pid == 0) {
+ fd = open("/etc/passwd", O_RDONLY);
+ if (fd < 0)
+ err(1, "/etc/passwd");
+#if defined(DEBUG)
+ printf("child: sending fd=%d for /etc/passwd\n", fd);
+#endif
+
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ *(int *)CMSG_DATA(cmsg) = fd;
+ flag = setflag();
+ if (arc4random() % 2 == 0)
+ corrupt((unsigned char *)&msg, sizeof(msg));
+ else
+ corrupt((unsigned char *)&cmsg, sizeof(cmsg));
+ if (sendmsg(pair[0], &msg, flag) < 0)
+ err(1, "sendmsg");
+ _exit(0);
+ }
+ alarm(2);
+ if (recvmsg(pair[1], &msg, 0) < 0)
+ err(1, "recvmsg");
+ if (cmsg->cmsg_type != SCM_RIGHTS)
+ err(1, "didnt get a fd?\n");
+ fd = *(int *)CMSG_DATA(cmsg);
+#if defined(DEBUG)
+ printf("parent: received fd=%d\n", fd);
+#endif
+ n = read(fd, buf, sizeof(buf));
+ if (n < 0)
+ err(1, "read");
+ if (n != sizeof(buf))
+ printf("read %d bytes\n", n);
+ wait(NULL);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ size_t len;
+ time_t start;
+ int e, i, pids[PARALLEL], status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ waitpid(pids[i], &status, 0);
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/sendmsg2.sh b/tools/test/stress2/misc/sendmsg2.sh
new file mode 100755
index 000000000000..08c91d945c5b
--- /dev/null
+++ b/tools/test/stress2/misc/sendmsg2.sh
@@ -0,0 +1,142 @@
+#!/bin/sh
+
+#
+# Test scenario by Tanaka Akira <akr@fsij.org>
+# from Bug 131876 - [socket] FD leak by receiving SCM_RIGHTS by recvmsg with
+# small control message buffer
+# Fixed by 337423
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sendmsg2.c
+mycc -o sendmsg2 -Wall -Wextra -O0 -g sendmsg2.c || exit 1
+rm -f sendmsg2.c
+cd $odir
+
+start=`date +%s`
+/tmp/sendmsg2 &
+while [ $((`date +%s` - start)) -lt 300 ]; do
+ kill -0 $! > /dev/null 2>&1 || break
+ sleep 2
+done
+if kill -0 $! > /dev/null 2>&1; then
+ echo FAIL
+ ps -lp $!
+ procstat -k $!
+ kill -9 $!
+fi
+while pkill sendmsg2; do :; done
+wait
+s=$?
+rm -f /tmp/sendmsg2
+exit $s
+EOF
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/uio.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define MAX_FDS 10
+#define SEND_FDS 10
+#define RECV_FDS 3
+
+void
+test(void)
+{
+ struct msghdr msg;
+ struct iovec iov;
+ union {
+ struct cmsghdr header;
+ char bytes[CMSG_SPACE(sizeof(int)*MAX_FDS)];
+ } cmsg;
+ struct cmsghdr *cmh = &cmsg.header, *c;
+ int *fds;
+ int i;
+ int ret;
+ int sv[2];
+ char buf[1024];
+ char cmdline[1024];
+
+ snprintf(cmdline, sizeof(cmdline), "fstat -p %u", (unsigned)getpid());
+
+ ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
+ if (ret == -1) { perror("socketpair"); exit(1); }
+
+ iov.iov_base = "a";
+ iov.iov_len = 1;
+
+ cmh->cmsg_len = CMSG_LEN(sizeof(int)*SEND_FDS);
+ cmh->cmsg_level = SOL_SOCKET;
+ cmh->cmsg_type = SCM_RIGHTS;
+ fds = (int *)CMSG_DATA(cmh);
+ for (i = 0; i < SEND_FDS; i++) {
+ fds[i] = 0; /* stdin */
+ }
+
+ msg.msg_name = NULL;
+ msg.msg_namelen = 0;
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+ msg.msg_control = cmh;
+ msg.msg_controllen = CMSG_SPACE(sizeof(int)*SEND_FDS);
+ msg.msg_flags = 0;
+
+ ret = sendmsg(sv[0], &msg, 0);
+ if (ret == -1) { perror("sendmsg"); exit(1); }
+
+ iov.iov_base = buf;
+ iov.iov_len = sizeof(buf);
+
+ msg.msg_name = NULL;
+ msg.msg_namelen = 0;
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+ msg.msg_control = cmh;
+ msg.msg_controllen = CMSG_SPACE(sizeof(int)*RECV_FDS);
+ msg.msg_flags = 0;
+
+#if defined(DEBUG)
+ printf("before recvmsg: msg_controllen=%d\n", msg.msg_controllen);
+#endif
+
+ ret = recvmsg(sv[1], &msg, 0);
+ if (ret == -1) { perror("sendmsg"); exit(1); }
+
+#if defined(DEBUG)
+ printf("after recvmsg: msg_controllen=%d\n", msg.msg_controllen);
+#endif
+
+ for (c = CMSG_FIRSTHDR(&msg); c != NULL; c = CMSG_NXTHDR(&msg, c)) {
+ if (c->cmsg_len == 0) { printf("cmsg_len is zero\n"); exit(1); }
+ if (c->cmsg_level == SOL_SOCKET && c->cmsg_type == SCM_RIGHTS) {
+ int *fdp, *end;
+ printf("cmsg_len=%d\n", c->cmsg_len);
+ fdp = (int *)CMSG_DATA(c);
+ end = (int *)((char *)c + c->cmsg_len);
+ for (i = 0; fdp+i < end; i++) {
+ printf("fd[%d]=%d\n", i, fdp[i]);
+ }
+ }
+ }
+ /* Note the missing sockets close */
+}
+
+int
+main(void)
+{
+ time_t start;
+
+ alarm(600);
+ start = time(NULL);
+ while (time(NULL) - start < 300)
+ test();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sethostname.sh b/tools/test/stress2/misc/sethostname.sh
new file mode 100755
index 000000000000..3684c465f041
--- /dev/null
+++ b/tools/test/stress2/misc/sethostname.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# "panic: pmap active 0xfffff80121ec04b8" seen:
+# http://people.freebsd.org/~pho/stress/log/kostik656.txt
+# Fixed in r263878.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+export sleeptime=18
+./syscall4.sh 88 # 88 is old sethostname
diff --git a/tools/test/stress2/misc/setsockopt.sh b/tools/test/stress2/misc/setsockopt.sh
new file mode 100755
index 000000000000..969c7b23069b
--- /dev/null
+++ b/tools/test/stress2/misc/setsockopt.sh
@@ -0,0 +1,152 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# setsockopt() fuzz test scenario inspired by syzkaller.
+
+# "panic: mtx_lock() of spin mutex (null) @
+# ../../../dev/hyperv/hvsock/hv_sock.c:519" seen.
+# https://people.freebsd.org/~pho/stress/log/setsockopt.txt
+# Introduced by r361275
+# Fixed by r361360
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/setsockopt.c
+mycc -o setsockopt -Wall -Wextra -O0 -g setsockopt.c || exit 1
+rm -f setsockopt.c
+
+daemon sh -c "(cd $odir/../testcases/swap; ./swap -t 5m)" > /dev/null 2>&1
+/tmp/setsockopt
+s=$?
+while pgrep -q swap; do
+ pkill -9 swap
+done
+rm -rf /tmp/setsockopt /tmp/setsockopt.c
+exit $s
+EOF
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define PARALLEL 128
+#define RUNTIME (3 * 60)
+
+static int port;
+
+static void
+test(int idx) {
+ struct hostent *hostent;
+ struct sockaddr_in inetaddr;
+ int i, j, r;
+ int tcpsock, on;
+
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ for (j = 0; j < 10000; j++) {
+ if ((tcpsock = socket(arc4random() % 64, arc4random() % 10, arc4random() % 10)) != -1)
+ break;
+ }
+ if (tcpsock == -1)
+ _exit(0);
+
+ /*
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+ */
+ for (j = 0; j < 10000; j++) {
+ r = setsockopt(tcpsock, arc4random(), arc4random(), (char *)&on, sizeof(on));
+ if (r != -1)
+ break;
+ }
+
+ hostent = gethostbyname ("localhost");
+ r = -1;
+ for (j = 0; j < 10000 && r != 0; j++) {
+ bzero((char *) &inetaddr, sizeof(inetaddr));
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+// inetaddr.sin_family = AF_INET;
+ inetaddr.sin_family = arc4random() % 128;
+// inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_addr.s_addr = arc4random();
+ inetaddr.sin_port = htons(port + idx);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ alarm(1);
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ alarm(0);
+ }
+ usleep(1000);
+ close(tcpsock);
+ }
+ write(tcpsock, "a", 1);
+ _exit(0);
+}
+
+int
+main(void)
+{
+ time_t start;
+ int i, pids[PARALLEL], status;
+
+ port = 77665;
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test(i);
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ }
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/setsockopt2.sh b/tools/test/stress2/misc/setsockopt2.sh
new file mode 100755
index 000000000000..89f22602a11e
--- /dev/null
+++ b/tools/test/stress2/misc/setsockopt2.sh
@@ -0,0 +1,208 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# "panic: Assertion in_epoch(net_epoch_preempt) failed at raw_ip6.c:742"
+# https://people.freebsd.org/~pho/stress/log/setsockopt2.txt
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/setsockopt2.c
+mycc -o setsockopt2 -Wall -Wextra -O0 -g setsockopt2.c || exit 1
+rm -f setsockopt2.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+newfs $newfs_flags md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+(cd ../testcases/swap; ./swap -t 5m -i 20) &
+cd $mntpoint
+$dir/setsockopt2
+s=$?
+[ -f setsockopt2.core -a $s -eq 0 ] &&
+ { ls -l setsockopt2.core; mv setsockopt2.core $dir; s=1; }
+cd $odir
+while pkill swap; do :; done
+wait
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/setsockopt2
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/un.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#define PARALLEL 128
+#define RUNTIME (2 * 60)
+#define SYNC 0
+
+static void
+test(void)
+{
+ struct sockaddr_un sun;
+ pid_t pid;
+ time_t start;
+ char file[80];
+ int domain, fd, i, one, prot, success, typ;
+
+ success = 0;
+ snprintf(file, sizeof(file), "setsockopt2.socket.%d", getpid());
+ memset(&sun, 0, sizeof(sun));
+ sun.sun_family = AF_LOCAL;
+ sun.sun_len = sizeof(sun);
+ snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", file);
+ unlink(file);
+
+ start = time(NULL);
+ while (time(NULL) - start < 30) {
+ share[SYNC] = 0;
+ fd = -1;
+ while (fd == -1) {
+ domain = arc4random() % 256;
+ typ = arc4random() % 10;
+ if (arc4random() % 100 < 20)
+ prot = arc4random() % 10;
+ else
+ prot = 0;
+ if ((fd = socket(domain, typ, prot)) == -1)
+ continue;
+ close(fd);
+ }
+ pid = fork();
+ if (pid < 0)
+ err(1, "fork");
+ if (pid == 0) {
+ // listen
+ fd = socket(domain, typ, prot);
+ if (fd < 0)
+ _exit(0);
+ one = 1;
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one,
+ sizeof(one)) < 0)
+ err(1, "setsockopt");
+ if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) < 0)
+ _exit(0);
+ if (listen(fd, 10) == 0) {
+ share[SYNC] = 1;
+ usleep(random() % 10000);
+ }
+ (void)close(fd);
+ (void)unlink(file);
+ _exit(0);
+ }
+ fd = socket(domain, typ, prot);
+ if (fd == -1)
+ goto bad;
+
+ setsockopt(fd, 0xffff, 0x80, 0x0, 0x0);
+ sun.sun_len = arc4random() % 128;
+ sun.sun_family = arc4random() % 10;
+ setsockopt(fd, 0x6, 0x401, &sun, 0x14);
+
+ for (i = 0; share[SYNC] == 0 && i < 10; i++)
+ usleep(100);
+ if (connect(fd, (struct sockaddr *)&sun, sizeof(sun)) != -1)
+ success++;
+ usleep(random() % 100);
+bad:
+ (void)close(fd);
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/setuid.sh b/tools/test/stress2/misc/setuid.sh
new file mode 100755
index 000000000000..4703ffe68e77
--- /dev/null
+++ b/tools/test/stress2/misc/setuid.sh
@@ -0,0 +1,122 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Regression test for r218019: "panic: oof, we didn't get our fd"
+
+# Page fault seen:
+# https://people.freebsd.org/~pho/stress/log/kostik895.txt
+# Caused by r300792 + r300793.
+# Fixed by r301580.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > setuid.c
+rm -f /tmp/setuid
+mycc -o setuid -Wall -Wextra -O2 -g setuid.c -static || exit 1
+rm -f setuid.c
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+mkdir $mntpoint/root
+
+cp /tmp/setuid $mntpoint/root/nop
+chown nobody:nobody $mntpoint/root/nop
+chmod 4755 $mntpoint/root/nop
+
+chmod 777 $mntpoint/root
+
+echo "Expect Abort trap"
+./setuid $mntpoint/root 1
+./setuid $mntpoint/root 2
+./setuid $mntpoint/root 3
+./setuid $mntpoint/root 4
+./setuid $mntpoint/root 5
+./setuid $mntpoint/root 6
+./setuid $mntpoint/root 7
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/setuid /tmp/nop
+exit
+EOF
+#include <sys/types.h>
+#include <pwd.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(int argc, char **argv)
+{
+ char *av[4];
+ int fd;
+
+ if (argc == 1)
+ return (0);
+
+ if (chroot(argv[1]) != 0)
+ err(1, "chroot(%s)", argv[1]);
+ fd = atoi(argv[2]);
+
+ if (fd & 1) {
+ fprintf(stderr, "Close fd 0 ");
+ close(0);
+ }
+ if (fd & 2) {
+ fprintf(stderr, "Close fd 1 ");
+ close(1);
+ }
+ if (fd & 4) {
+ fprintf(stderr, "Close fd 2\n");
+ close(2);
+ } else
+ fprintf(stderr, "\n");
+
+ if (chdir("/") != 0)
+ err(1, "chdir");
+ av[0] = "nop";
+ av[1] = "nop";
+ av[3] = 0;
+ if (execve(av[0], av, NULL) == -1)
+ err(1, "execve");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/shm.sh b/tools/test/stress2/misc/shm.sh
new file mode 100755
index 000000000000..eac2e0c7baa0
--- /dev/null
+++ b/tools/test/stress2/misc/shm.sh
@@ -0,0 +1,201 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC Isilon
+# 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 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.
+#
+
+# Regression test for r310849. Hang in "vmpfw" was seen.
+
+# Test scenario suggestion by kib.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+RUNTIME=60
+old=`sysctl -n kern.ipc.shm_use_phys`
+trap "sysctl kern.ipc.shm_use_phys=$old" EXIT INT
+sysctl kern.ipc.shm_use_phys=1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/shm.c
+mycc -o shm -Wall -Wextra -O0 -g shm.c -pthread || exit 1
+rm -f shm.c
+
+/tmp/shm &
+
+s=0
+sleep $RUNTIME
+if pgrep -q shm; then
+ if pgrep shm | xargs ps -lHp | grep -q vmpfw; then
+ s=1
+ echo FAIL
+ pgrep shm | xargs ps -lHp
+ pkill -9 shm
+ fi
+fi
+wait
+
+rm -rf /tmp/shm
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/ipc.h>
+#include <sys/mman.h>
+#include <sys/shm.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static void *shmp;
+static size_t len;
+static int cont, shmid;
+
+#define PAGES 64
+#define RUNTIME (1 * 60)
+#define STOP 1
+#define SYNC 0
+
+static void
+cleanup(void)
+{
+ if (shmp != MAP_FAILED)
+ shmdt(shmp);
+ if (shmid != 0)
+ shmctl(shmid, IPC_RMID, NULL);
+}
+
+static void *
+t1(void *arg __unused)
+{
+ time_t start;
+ char *cp;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ start = time(NULL);
+ while (cont == 1 && (time(NULL) - start) < RUNTIME) {
+ if ((cp = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ usleep(arc4random() % 400);
+ if (munmap(cp, len) == -1)
+ warn("unmap(%p, %zu)", shmp, len);
+ }
+
+ return (NULL);
+}
+
+static void *
+t2(void *arg __unused)
+{
+ key_t shmkey;
+ size_t i;
+ time_t start;
+ char *cp;
+
+ pthread_set_name_np(pthread_self(), __func__);
+ shmkey = ftok("/tmp", getpid());
+ start = time(NULL);
+ atexit(cleanup);
+ while ((time(NULL) - start) < RUNTIME) {
+ if ((shmid = shmget(shmkey, len, IPC_CREAT | IPC_EXCL |
+ 0640)) == -1) {
+ if (errno != ENOSPC && errno != EEXIST)
+ err(1, "shmget (%s:%d)", __FILE__, __LINE__);
+ break;
+ }
+ if ((shmp = shmat(shmid, NULL, 0)) == (void *) -1)
+ break;
+ cp = (char *)(shmp);
+ for (i = 0; i < len; i = i + PAGE_SIZE)
+ cp[i] = 1;
+ if (shmdt(shmp) == -1) {
+ if (errno != EINVAL)
+ warn("shmdt(%p)", shmp);
+ }
+ if (shmctl(shmid, IPC_RMID, NULL) == -1)
+ warn("shmctl IPC_RMID");
+ usleep(50);
+
+ shmp = MAP_FAILED;
+ shmid = 0;
+ }
+ cont = 0;
+
+ return (NULL);
+}
+
+static int
+test(void)
+{
+ pthread_t tid[2];
+ int i, rc;
+
+ shmp = MAP_FAILED;
+
+ cont = 1;
+ if ((rc = pthread_create(&tid[0], NULL, t1, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+ if ((rc = pthread_create(&tid[1], NULL, t2, NULL)) != 0)
+ errc(1, rc, "pthread_create()");
+
+ for (i = 0; i < 2; i++)
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+
+ return (0);
+}
+
+int
+main(void)
+{
+ pid_t pid;
+ time_t start;
+
+ len = PAGES * PAGE_SIZE;
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ if ((pid = fork()) == 0) {
+ test();
+ exit(0); /* calls cleanup() */
+ }
+ if (waitpid(pid, NULL,0) != pid)
+ err(1, "waitpid(%d)", pid);
+ }
+
+ return(0);
+}
diff --git a/tools/test/stress2/misc/shm2.sh b/tools/test/stress2/misc/shm2.sh
new file mode 100755
index 000000000000..e1b229030b4a
--- /dev/null
+++ b/tools/test/stress2/misc/shm2.sh
@@ -0,0 +1,167 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# No problems seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/shm2.c
+mycc -o shm2 -Wall -Wextra -O0 -g shm2.c || exit 1
+rm -f shm2.c
+
+(cd $odir/../testcases/swap; ./swap -t 2m -i 20 -h -l 100) &
+/tmp/shm2
+while pkill swap; do sleep 1; done
+wait
+
+rm -rf /tmp/shm2
+exit $s
+
+EOF
+#include <sys/types.h>
+#include <sys/resource.h>
+#include <sys/shm.h>
+#include <sys/sysctl.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include <vm/vm_param.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define INCARNATIONS 32
+
+static unsigned long size, original;
+static int runtime, utime;
+
+static void
+setup(void)
+{
+ struct rlimit rlp;
+
+ size = size / INCARNATIONS;
+ original = size;
+ if (size == 0)
+ errx(1, "Argument too small");
+
+ if (getrlimit(RLIMIT_DATA, &rlp) < 0)
+ err(1,"getrlimit");
+ rlp.rlim_cur -= 1024 * 1024;
+
+ if (size > (unsigned long)rlp.rlim_cur)
+ size = rlp.rlim_cur;
+
+#if 0
+ printf("setup: pid %d. Total %luMb\n",
+ getpid(), size / 1024 / 1024 * INCARNATIONS);
+#endif
+
+ if (size == 0)
+ errx(1, "Argument too small");
+
+ return;
+}
+
+static int
+test(void)
+{
+ key_t shmkey;
+ volatile char *c;
+ int page, shmid;
+ unsigned long i, j;
+ time_t start;
+
+ shmkey = ftok("/tmp", getpid());
+ shmid = -1;
+ while (shmid == -1) {
+ if ((shmid = shmget(shmkey, size, IPC_CREAT | IPC_EXCL | 0640)) == -1) {
+ if (errno != ENOSPC && errno != EEXIST)
+ err(1, "shmget (%s:%d)", __FILE__, __LINE__);
+ } else
+ break;
+ size -= 1024;
+ }
+ if ((c = shmat(shmid, NULL, 0)) == (void *) -1)
+ err(1, "shmat");
+ if (size != original)
+ printf("shm size changed from %ld kb to %ld kb\n",
+ original / 1024, size / 1024);
+ page = getpagesize();
+ start = time(NULL);
+ while ((time(NULL) - start) < runtime) {
+ i = j = 0;
+ while (i < size) {
+ c[i] = 1;
+ i += page;
+ if ((time(NULL) - start) >= runtime)
+ break;
+// usleep(utime);
+ }
+ }
+ if (shmdt((void *)c) == -1) {
+ if (errno != EINVAL)
+ warn("shmdt(%p)", c);
+ }
+ if (shmctl(shmid, IPC_RMID, NULL) == -1)
+ warn("shmctl IPC_RMID");
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[INCARNATIONS];
+ int i, verbose;
+
+ runtime = 120; /* 2 minutes */
+ utime = 1000; /* 0.001 sec */
+ verbose = 0;
+ size = 512 * 1024 * 1024;
+ setup();
+
+ for (i = 0; i < INCARNATIONS; i++)
+ if ((pids[i] = fork()) == 0)
+ test();
+
+ for (i = 0; i < INCARNATIONS; i++)
+ if (waitpid(pids[i], NULL, 0) != pids[i])
+ err(1, "waitpid(%d)", pids[i]);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/shm_open.sh b/tools/test/stress2/misc/shm_open.sh
new file mode 100755
index 000000000000..be446c41476b
--- /dev/null
+++ b/tools/test/stress2/misc/shm_open.sh
@@ -0,0 +1,85 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# "panic: kmem_malloc(4096): kmem_map too small" seen.
+
+. ../default.cfg
+[ $((`sysctl -n vm.kmem_size` / 1024 / 1024 / 1024)) -eq 0 ] && exit 0
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/shm_open.c
+mycc -o shm_open -Wall -Wextra -O2 -g shm_open.c || exit 1
+rm -f shm_open.c
+cd $odir
+
+v1=`vmstat -m | awk '/shmfd/{gsub("K", ""); print $3}'`
+su $testuser -c /tmp/shm_open
+v2=`vmstat -m | awk '/shmfd/{gsub("K", ""); print $3}'`
+v2=$((v2 - v1))
+[ $v2 -gt 10 ] && { echo "shmfd leaked ${v2}K"; s=1; } || s=0
+
+rm -f /tmp/shm_open
+exit $s
+
+EOF
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ pid_t pid;
+ off_t len;
+ int fd, i;
+ char name[80];
+
+ len = 1024 * 1024;
+ pid = getpid();
+ for (i = 0; i < 500000; i++) {
+ sprintf(name, "/testing.%06d.%06d", pid, i);
+
+ if ((fd = shm_open(name, O_RDWR | O_CREAT, 0644)) == -1)
+ err(1, "shm_open()");
+ if (ftruncate(fd, len) == -1)
+ err(1, "ftruncate");
+ close(fd);
+ }
+ for (i = 0; i < 500000; i++) {
+ sprintf(name, "/testing.%06d.%06d", pid, i);
+ if (shm_unlink(name) == -1)
+ err(1, "shm_unlink(%s)", name);
+ }
+
+ return(0);
+}
diff --git a/tools/test/stress2/misc/shm_super.sh b/tools/test/stress2/misc/shm_super.sh
new file mode 100755
index 000000000000..78f237bf789d
--- /dev/null
+++ b/tools/test/stress2/misc/shm_super.sh
@@ -0,0 +1,87 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Konstantin Belousov <kib@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Demonstrate superpage mapping.
+
+cat > /tmp/shm_super.c <<EOF
+/* $Id: shm_super.c,v 1.1 2018/10/13 23:49:37 kostik Exp kostik $ */
+
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define M(x) ((x) * 1024 * 1024)
+#define SZ M(4)
+
+int
+main(void)
+{
+ char buf[128];
+ void *ptr;
+ off_t cnt;
+ int error, shmfd;
+
+ shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
+ if (shmfd == -1)
+ err(1, "shm_open");
+ error = ftruncate(shmfd, SZ);
+ if (error == -1)
+ err(1, "truncate");
+ memset(buf, 0, sizeof(buf));
+ for (cnt = 0; cnt < SZ; cnt += sizeof(buf)) {
+ error = write(shmfd, buf, sizeof(buf));
+ if (error == -1)
+ err(1, "write");
+ else if (error != sizeof(buf))
+ errx(1, "short write %d", (int)error);
+ }
+ ptr = mmap(NULL, SZ, PROT_READ | PROT_WRITE, MAP_SHARED |
+ MAP_ALIGNED_SUPER, shmfd, 0);
+ if (ptr == MAP_FAILED)
+ err(1, "mmap");
+ for (cnt = 0; cnt < SZ; cnt += PAGE_SIZE)
+ *((char *)ptr + cnt) = 0;
+ printf("ptr %p\n", ptr);
+ snprintf(buf, sizeof(buf), "procstat -v %d", getpid());
+ system(buf);
+}
+EOF
+cc -o /tmp/shm_super -Wall -Wextra -O2 -g /tmp/shm_super.c || exit 1
+rm /tmp/shm_super.c
+
+/tmp/shm_super > /tmp/shm_super.log 2>&1
+grep -wq S /tmp/shm_super.log && s=0 || { cat /tmp/shm_super.log; s=1; }
+
+rm -f /tmp/shm_super /tmp/shm_super.log
+exit $s
diff --git a/tools/test/stress2/misc/sigaltstack.sh b/tools/test/stress2/misc/sigaltstack.sh
new file mode 100755
index 000000000000..cd8f7004decb
--- /dev/null
+++ b/tools/test/stress2/misc/sigaltstack.sh
@@ -0,0 +1,128 @@
+#!/bin/sh
+
+# sigaltstack(2) regression test by Steven Hartland <killing@multiplay.co.uk>
+# Wrong altsigstack clearing on exec
+# https://github.com/golang/go/issues/15658#issuecomment-287276856
+
+# Fixed by r315453
+
+cd /tmp
+cat > test-sigs.c <<EOF
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <time.h>
+#include <pthread.h>
+
+extern char **environ;
+
+static void
+die(const char *s)
+{
+ perror(s);
+ exit(EXIT_FAILURE);
+}
+
+static void
+setstack(void *arg __unused)
+{
+ stack_t ss;
+
+ ss.ss_sp = malloc(SIGSTKSZ);
+ if (ss.ss_sp == NULL)
+ die("malloc");
+
+ ss.ss_size = SIGSTKSZ;
+ ss.ss_flags = 0;
+ if (sigaltstack(&ss, NULL) < 0)
+ die("sigaltstack set");
+}
+
+static void *
+thread_exec(void *arg)
+{
+ struct timespec ts = {0, 1000};
+ char *argv[] = {"./test-sigs", "no-more", 0};
+
+ setstack(arg);
+ nanosleep(&ts, NULL);
+
+ execve(argv[0], &argv[0], environ);
+ die("exec failed");
+
+ return NULL;
+}
+
+static void *
+thread_sleep(void *arg __unused)
+{
+ sleep(10);
+
+ return NULL;
+}
+
+int
+main(int argc, char** argv __unused)
+{
+ int j;
+ pthread_t tid1, tid2;
+
+ if (argc != 1) {
+ stack_t ss;
+
+ if (sigaltstack(NULL, &ss) < 0)
+ die("sigaltstack get");
+
+ if (ss.ss_sp != NULL || ss.ss_flags != SS_DISABLE ||
+ ss.ss_size != 0) {
+ fprintf(stderr, "invalid signal stack after execve: "
+ "ss_sp=%p ss_size=%lu ss_flags=0x%x\n", ss.ss_sp,
+ (unsigned long)ss.ss_size,
+ (unsigned int)ss.ss_flags);
+ return 1;
+ }
+
+ printf("valid signal stack is valid after execve\n");
+
+ return 0;
+ }
+
+ // We have to use two threads to ensure that can detect the
+ // issue when new threads are added to the head (pre 269095)
+ // and the tail of the process thread list.
+ j = pthread_create(&tid1, NULL, thread_exec, NULL);
+ if (j != 0) {
+ errno = j;
+ die("pthread_create");
+ }
+
+ j = pthread_create(&tid2, NULL, thread_sleep, NULL);
+ if (j != 0) {
+ errno = j;
+ die("pthread_create");
+ }
+
+ j = pthread_join(tid1, NULL);
+ if (j != 0) {
+ errno = j;
+ die("pthread_join");
+ }
+
+ j = pthread_join(tid2, NULL);
+ if (j != 0) {
+ errno = j;
+ }
+
+ return 0;
+}
+EOF
+
+cc -o test-sigs -Wall -Wextra -O2 -g test-sigs.c -lpthread || exit 1
+./test-sigs
+s=$?
+
+rm -f test-sigs test-sigs.c
+exit $s
diff --git a/tools/test/stress2/misc/sigfastblock.sh b/tools/test/stress2/misc/sigfastblock.sh
new file mode 100755
index 000000000000..403dbde31553
--- /dev/null
+++ b/tools/test/stress2/misc/sigfastblock.sh
@@ -0,0 +1,141 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Konstantin Belousov
+#
+# 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 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.
+#
+
+# sigfastblock test scenario by Konstantin Belousov <kib@FreeBSD.org>
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -p` != "amd64" ] && exit 0
+
+cat > /tmp/sigfastblock.c <<EOF
+/* $Id: sigfastblock1.c,v 1.4 2020/02/16 19:53:14 kostik Exp kostik $ */
+
+#include <sys/types.h>
+#include <sys/signalvar.h>
+#include <sys/sysctl.h>
+#include <err.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#ifdef __ILP32
+#define BITNESS "32"
+#else
+#define BITNESS "64"
+#endif
+
+int block;
+
+static void
+sighandler(int signo, siginfo_t *si, void *ucp __unused)
+{
+ printf("block %#x sig %d si_code %d si_pid %d\n",
+ block, signo, si->si_code, si->si_pid);
+}
+
+int
+main(void)
+{
+ struct timespec rqts;
+ struct sigaction sa;
+ int error, val;
+ size_t valsize;
+ pid_t child;
+
+ valsize = sizeof(val);
+ error = sysctlbyname("kern.elf" BITNESS ".sigfastblock", &val,
+ &valsize, NULL, 0);
+ if (error != 0)
+ err(1, "sigfastblock sysctl");
+ if (val != 0)
+ errx(1, "sigfastblock use in rtld must be turned off");
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_sigaction = sighandler;
+ sa.sa_flags = SA_SIGINFO;
+ error = sigaction(SIGUSR1, &sa, NULL);
+
+ child = fork();
+ if (child == -1)
+ err(1, "fork");
+ if (child == 0) {
+ sleep(2);
+ kill(getppid(), SIGUSR1);
+ _exit(0);
+ }
+
+ error = __sys_sigfastblock(SIGFASTBLOCK_SETPTR, &block);
+ if (error != 0)
+ err(1, "sigfastblock setptr");
+
+ printf("Registered block at %p\n", &block);
+ block = SIGFASTBLOCK_INC;
+ rqts.tv_sec = 4;
+ rqts.tv_nsec = 0;
+ errno = 0;
+ error = nanosleep(&rqts, 0);
+ printf("nanosleep 1 returned %d, errno %d, block %#x\n",
+ error, errno, block);
+ raise(SIGUSR1);
+ errno = 0;
+ error = nanosleep(&rqts, 0);
+ printf("nanosleep 2 returned %d, errno %d, block %#x\n",
+ error, errno, block);
+
+ errno = 0;
+ block -= SIGFASTBLOCK_INC;
+ error = __sys_sigfastblock(SIGFASTBLOCK_UNBLOCK, NULL);
+ printf("unblock returned %d, errno %d, block %#x\n",
+ error, errno, block);
+}
+
+EOF
+mycc -o /tmp/sigfastblock -Wall -Wextra -O2 -g /tmp/sigfastblock.c || exit 1
+
+echo "Expect:
+Registered block at 0x2030c0
+nanosleep 1 returned -1, errno 4, block 0x11
+nanosleep 2 returned 0, errno 0, block 0x11
+unblock returned 0, errno 0, block 0
+block 0 sig 30 si_code 65543 si_pid 1151
+block 0 sig 30 si_code 65537 si_pid 1152
+"
+
+cd /tmp
+sysctl kern.elf64.sigfastblock=0
+sysctl kern.elf32.sigfastblock=0
+./sigfastblock; s=$?
+sysctl kern.elf64.sigfastblock=1
+sysctl kern.elf32.sigfastblock=1
+
+rm -f /tmp/sigfastblock.c /tmp/sigfastblock
+exit $s
diff --git a/tools/test/stress2/misc/sigfastblock2.sh b/tools/test/stress2/misc/sigfastblock2.sh
new file mode 100755
index 000000000000..8354307f6f7a
--- /dev/null
+++ b/tools/test/stress2/misc/sigfastblock2.sh
@@ -0,0 +1,132 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 corydoras@ridiculousfish.com
+#
+# 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 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.
+#
+
+# The test scenario is from:
+# Bug 246385 - SIGCHLD dropped if generated while blocked in sigfastblock
+# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=246385
+# Fixed by r360940
+
+. ../default.cfg
+
+cat > /tmp/sigfastblock2.c <<EOF
+#include <assert.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/time.h>
+
+static int pipes[2];
+
+static void assert_noerr(int res)
+{
+ if (res < 0)
+ {
+ fprintf(stderr, "error: %s", strerror(res));
+ exit(EXIT_FAILURE);
+ }
+}
+
+static void sigchld_handler(int sig)
+{
+ // Write a byte to the write end of pipes.
+ (void)sig;
+ char c = 0;
+ ssize_t amt = write(pipes[1], &c, 1);
+ if (amt < 0)
+ {
+ perror("write");
+ exit(EXIT_FAILURE);
+ }
+}
+
+static pid_t spawn_child()
+{
+ pid_t pid = fork();
+ assert_noerr(pid);
+ if (pid == 0)
+ {
+ _exit(0);
+ }
+ return pid;
+}
+
+int main(void)
+{
+ // Install SIGCHLD handler with SA_RESTART.
+ struct sigaction chld_act = {};
+ sigemptyset(&chld_act.sa_mask);
+ chld_act.sa_handler = &sigchld_handler;
+ chld_act.sa_flags = SA_RESTART;
+ assert_noerr(sigaction(SIGCHLD, &chld_act, NULL));
+
+ // Make our self-pipes.
+ assert_noerr(pipe(pipes));
+
+ // Spawn and reap children in a loop.
+ for (;;)
+ {
+ // Spawn a child.
+ pid_t child = spawn_child();
+ assert(child >= 0 && "Invalid pid");
+
+ // Wait for the SIGCHLD handler to write to us.
+ for (;;)
+ {
+ char buf[PIPE_BUF];
+ alarm(5);
+ ssize_t amt = read(pipes[0], buf, sizeof buf);
+ if (amt >= 0)
+ break;
+ alarm(0);
+ }
+
+ // Reap it.
+ int status = 0;
+ int pid_res = waitpid(child, &status, WUNTRACED | WCONTINUED);
+ assert(pid_res == child && "Unexpected pid");
+ }
+
+ return 0;
+}
+EOF
+mycc -o /tmp/sigfastblock2 -Wall -Wextra /tmp/sigfastblock2.c -lpthread ||
+ exit 1
+
+timeout 5m /tmp/sigfastblock2; s=$?
+[ $s -eq 124 ] && s=0
+
+rm -f /tmp/sigfastblock2 /tmp/sigfastblock2.c
+exit $s
diff --git a/tools/test/stress2/misc/signal.sh b/tools/test/stress2/misc/signal.sh
new file mode 100755
index 000000000000..0466184b1f14
--- /dev/null
+++ b/tools/test/stress2/misc/signal.sh
@@ -0,0 +1,247 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Regression test for kern/150138: signal sent to stopped, traced process
+# not immediately handled on continue.
+# Fixed in r212047.
+
+# Test scenario by Dan McNulty <dkmcnulty gmail.com>
+
+. ../default.cfg
+
+here=`pwd`
+wd=/tmp/signal.dir
+rm -rf $wd
+mkdir -p $wd
+cd $wd
+cat > waitthread.c <<EOF
+#include <unistd.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <assert.h>
+#include <signal.h>
+#include <time.h>
+#include <sys/syscall.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+struct threadArg {
+ int id;
+ pthread_mutex_t *mutex;
+};
+
+void *entry(void *arg) {
+ struct threadArg *thisArg = (struct threadArg *)arg;
+
+ long lwp_id = thisArg->id;
+ if( syscall(SYS_thr_self, &lwp_id) ) {
+ perror("syscall");
+ }
+
+ printf("%ld waiting on lock\n", lwp_id);
+
+ if( pthread_mutex_lock(thisArg->mutex) != 0 ) {
+ perror("pthread_mutex_lock");
+ return NULL;
+ }
+
+ printf("%ld obtained lock\n", lwp_id);
+
+ if( pthread_mutex_unlock(thisArg->mutex) != 0 ) {
+ perror("pthread_mutex_unlock");
+ return NULL;
+ }
+
+ printf("%ld released lock\n", lwp_id);
+
+ return NULL;
+}
+
+int main(int argc, char **argv) {
+ if( 2 != argc ) {
+ printf("Usage: %s <num. of threads>\n", argv[0]);
+ return EXIT_FAILURE;
+ }
+
+ printf("%d\n", getpid());
+
+ int numThreads;
+ sscanf(argv[1], "%d", &numThreads);
+ if( numThreads < 1 ) numThreads = 1;
+
+ pthread_t *threads = (pthread_t *)malloc(sizeof(pthread_t)*numThreads);
+
+ pthread_mutex_t *mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
+
+ if( pthread_mutex_init(mutex, NULL) != 0 ) {
+ perror("pthread_mutex_init");
+ return EXIT_FAILURE;
+ }
+
+ if( pthread_mutex_lock(mutex) != 0 ) {
+ perror("pthread_mutex_lock");
+ return EXIT_FAILURE;
+ }
+
+ int i;
+ for(i = 0; i < numThreads; ++i) {
+ struct threadArg *arg = (struct threadArg *)malloc(sizeof(struct threadArg));
+ arg->id = i;
+ arg->mutex = mutex;
+ assert( !pthread_create(&threads[i], NULL, &entry, (void *)arg) );
+ }
+
+ // Wait on the named pipe
+ unlink("/tmp/waitthread");
+ if( mkfifo("/tmp/waitthread", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) ) {
+ perror("mkfifo");
+ return EXIT_FAILURE;
+ }
+
+ FILE *fifo;
+ do {
+ if( (fifo = fopen("/tmp/waitthread", "r")) == NULL ) {
+ if( errno == EINTR ) continue;
+
+ perror("fopen");
+ return EXIT_FAILURE;
+ }
+ break;
+ }while(1);
+
+ unsigned char byte;
+ if( fread(&byte, sizeof(unsigned char), 1, fifo) != 1 ) {
+ perror("fread");
+ }
+
+ fclose(fifo);
+
+ unlink("/tmp/waitthread");
+
+ printf("Received notification\n");
+
+ if( pthread_mutex_unlock(mutex) != 0 ) {
+ perror("pthread_mutex_unlock");
+ return EXIT_FAILURE;
+ }
+
+ printf("Unlocked mutex, joining\n");
+
+ for(i = 0; i < numThreads; ++i ) {
+ assert( !pthread_join(threads[i], NULL) );
+ }
+
+ return EXIT_SUCCESS;
+}
+EOF
+cat > tkill.c <<EOF
+#include <sys/syscall.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <signal.h>
+
+int main(int argc, char *argv[]) {
+ if( argc != 4 ) {
+ printf("Usage: %s <pid> <lwp_id> <sig>\n", argv[0]);
+ return EXIT_FAILURE;
+ }
+
+ pid_t pid;
+ sscanf(argv[1], "%d", &pid);
+
+ long id;
+ sscanf(argv[2], "%ld", &id);
+
+ int sig;
+ sscanf(argv[3], "%d", &sig);
+
+ if( syscall(SYS_thr_kill2, pid, id, sig) ) {
+ perror("syscall");
+ }
+
+ return EXIT_SUCCESS;
+}
+EOF
+mycc -o waitthread -Wall -Wextra waitthread.c -lpthread || exit
+mycc -o tkill -Wall -Wextra tkill.c || exit
+rm -f waitthread.c tkill.c
+
+rm -f gdbfifo gdbout pstat /tmp/waitthread
+pkill -9 waitthread
+pgrep -q waitthread && { pgrep waitthread | xargs ps -lHp; exit 1; }
+mkfifo gdbfifo
+trap "rm -f gdbfifo" EXIT INT
+sleep 300 > gdbfifo & # Keep the fifo open
+sleep .5
+fifopid=$!
+
+(trap - INT; gdb ./waitthread < gdbfifo > gdbout 2>&1) &
+sleep .8
+gdbpid=$!
+echo "set args 8" > gdbfifo
+echo "run" > gdbfifo
+sleep .8
+
+for i in `jot 5`; do
+ pid=`ps -x | grep -v grep | grep "waitthread 8" |
+ sed 's/^ *//;s/ .*//'`
+ [ -n "$pid" ] && break
+ sleep 1
+done
+s=0
+if [ -n "$pid" ]; then
+ procstat -t $pid > pstat
+
+ t1=`grep fifo pstat | awk '{print $2}'`
+ t2=`grep umtxn pstat | awk '{print $2}' | tail -1`
+
+ set -e
+ ./tkill $pid $t1 5 # SIGTRAP
+ ./tkill $pid $t2 2 # SIGINT
+ set +e
+
+ echo "c" > gdbfifo
+ echo "quit" > gdbfifo
+ sleep 1
+ grep -q "signal SIGINT" gdbout || { echo FAIL; cat gdbout; s=1; }
+else
+ echo "Did not find pid for test program waitthread"
+ s=2
+fi
+
+kill -9 $fifopid $gdbpid > /dev/null 2>&1
+pgrep -q waitthread && pkill -9 waitthread
+cd $here
+rm -rf $wd /tmp/waitthread
+exit $s
diff --git a/tools/test/stress2/misc/signal0.sh b/tools/test/stress2/misc/signal0.sh
new file mode 100755
index 000000000000..3c5ea8b006e2
--- /dev/null
+++ b/tools/test/stress2/misc/signal0.sh
@@ -0,0 +1,102 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Zero signal number seen.
+# Test scenario by Vitaly Magerya <vmagerya gmail com>
+# http://people.freebsd.org/~pho/stress/log/kostik646.txt
+# Panic fixed in r258497
+# Signal number fixed in r258499
+
+# panic: vm_fault: fault on nofault entry, addr: cdbbe000
+# https://people.freebsd.org/~pho/stress/log/kostik838.txt
+# Fixed by r289661.
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/signal0.c
+c99 -o signal0 signal0.c -lpthread || exit 1
+rm -f signal0.c
+cd $odir
+
+(cd ../testcases/swap; ./swap -t 5m -i 20 -h -v) > /dev/null 2>&1 &
+s=0
+# The timeout was added after r345702, due to a longer runtime for
+# an INVARIANTS kernel
+start=`date +%s`
+for i in `jot 500`; do
+ /tmp/signal0 || { s=1; break; }
+ [ $((`date +%s` - start)) -gt 900 ] &&
+ { echo "Timeout @ loop $i/500"; s=1; break; }
+done
+while pkill -9 swap; do :; done
+wait
+
+rm -f /tmp/signal0
+exit $s
+
+EOF
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#define N 3000
+
+static void
+signal_handler(int signum, siginfo_t *si, void *context) {
+ if (signum != SIGUSR1) {
+ printf("FAIL bad signal, signum=%d\n", signum);
+ exit(1);
+ }
+}
+
+static void
+*thread_func(void *arg) {
+ return arg;
+}
+
+int
+main(void)
+{
+ struct sigaction sa = { 0 };
+
+ sa.sa_flags = SA_SIGINFO;
+ sa.sa_sigaction = signal_handler;
+ if (sigfillset(&sa.sa_mask) != 0) abort();
+ if (sigaction(SIGUSR1, &sa, NULL) != 0) abort();
+ for (int i = 0; i < N; i++) {
+ pthread_t t;
+
+ if (pthread_create(&t, NULL, thread_func, NULL) == 0)
+ pthread_kill(t, SIGUSR1);
+
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sigreturn.sh b/tools/test/stress2/misc/sigreturn.sh
new file mode 100755
index 000000000000..55e12343a284
--- /dev/null
+++ b/tools/test/stress2/misc/sigreturn.sh
@@ -0,0 +1,121 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# "panic: smp_tlb_shootdown: interrupts disabled" seen.
+# http://people.freebsd.org/~pho/stress/log/freebsd4_sigreturn.txt
+# Fixed in r251033.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sigreturn.c
+mycc -o sigreturn -Wall -Wextra sigreturn.c -lpthread || exit 1
+rm -f sigreturn.c
+cd $odir
+
+/tmp/sigreturn
+
+rm -f /tmp/sigreturn ./sigreturn.core
+exit
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/param.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+unsigned long buf[] = {
+0xe8243489, 0x000e7cc9, 0x85204e8b, 0xc0940fc9, 0x0fc2950f, 0x5589d2b6,
+0x56bf0fe4, 0xe855890e, 0x890fd285, 0x0000014a, 0x850fc084, 0x00000142,
+0x891c468b, 0xd1ff2404, 0x00012ee9, 0x04b3e800, 0x388bffff, 0xe8243489,
+0xfffefbd9, 0xff04a4e8, 0xe93889ff, 0x00000343, 0x0fc08566, 0x00031f84,
+0x46bf0f00, 0x2404890e, 0x042444c7, 0x00000003, 0xfefb90e8, 0x85c789ff,
+0x0d880fff, 0x89000002, 0x03e083f8, 0x7402f883, 0xf04d8b0e, 0x3903e183,
+0xe1850fc8, 0xf6000002, 0x74080c46, 0x24348908, 0x0e7c2ce8, 0xf0458b00,
+0xf931c189, 0x7408c1f6, 0xf7e7832b, 0x0908e083, 0x4ebf0ff8, 0x2444890e,
+0x240c8908, 0x042444c7, 0x00000004, 0xfefb34e8, 0x0fc085ff, 0x0001b388,
+0xf0458b00, 0x7404c4f6, 0x46bf0f1f, 0x2404890e, 0x082444c7, 0x00000000,
+0x042444c7, 0x00000000, 0xff0898e8, 0xf0458bff, 0x237508a8, 0xc7243489,
+0x000c2444, 0xc7000000, 0x00082444, 0xc7000000, 0x00042444, 0xe8000000,
+0x000e86ad, 0xa9f0458b, 0x00100000, 0xbf0f1c74, 0x04890e46, 0x2444c724,
+0x00000108, 0x2444c700, 0x00000204, 0xfabbe800, 0xbf0ffffe, 0x45c70e7e,
+0xffffffe8, 0xe445c7ff, 0x00000000, 0xc76634eb, 0x00200c46, 0xffe845c7
+};
+
+#if !defined(SYS_freebsd4_sigreturn)
+#define SYS_freebsd4_sigreturn 344
+#endif
+
+static void
+hand(int i __unused) { /* handler */
+ _exit(1);
+}
+
+void *
+calls(void *arg __unused)
+{
+ alarm(1);
+ syscall(SYS_freebsd4_sigreturn, buf);
+
+ return (0);
+}
+
+int
+main(void)
+{
+ pthread_t cp;
+ int e;
+
+ signal(SIGALRM, hand);
+ signal(SIGILL, hand);
+ signal(SIGFPE, hand);
+ signal(SIGSEGV, hand);
+ signal(SIGBUS, hand);
+ signal(SIGURG, hand);
+ signal(SIGSYS, hand);
+ signal(SIGTRAP, hand);
+
+ if (fork() == 0) {
+ if ((e = pthread_create(&cp, NULL, calls, NULL)) != 0)
+ errc(1, e, "pthread_create");
+
+ pthread_join(cp, NULL);
+ _exit(0);
+ }
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sigstop.sh b/tools/test/stress2/misc/sigstop.sh
new file mode 100755
index 000000000000..3c8b19cc76a0
--- /dev/null
+++ b/tools/test/stress2/misc/sigstop.sh
@@ -0,0 +1,98 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Regression test.
+# Will display "FAIL exit status = 0" without r240467
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > sigstop.c
+mycc -o sigstop -Wall -Wextra sigstop.c || exit 1
+rm -f sigstop.c
+cd $here
+
+su $testuser -c "/tmp/sigstop"
+
+rm -f /tmp/sigstop
+exit
+
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <signal.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#define PARALLEL 4
+
+void
+test(void)
+{
+ pid_t pid;
+ int i, status;
+
+ for (i = 0; i < 1000; i++) {
+ if ((pid = fork()) == 0) {
+ _exit(64);
+ }
+ if (pid == -1)
+ err(1, "fork()");
+ if (kill(pid, SIGSTOP) == -1)
+ warn("sigstop");
+ if (kill(pid, SIGCONT) == -1)
+ warn("sigcont");
+ wait(&status);
+ if (WEXITSTATUS(status) != 64) {
+ fprintf(stderr, "FAIL exit status = %d\n",
+ WEXITSTATUS(status));
+ break;
+ }
+ }
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i;
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ test();
+ }
+
+ for (i = 0; i < PARALLEL; i++)
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/sigstop2.sh b/tools/test/stress2/misc/sigstop2.sh
new file mode 100755
index 000000000000..286936c2fb1a
--- /dev/null
+++ b/tools/test/stress2/misc/sigstop2.sh
@@ -0,0 +1,144 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# No problems seen.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/sigstop2.c
+mycc -o sigstop2 -Wall -Wextra -O0 -g sigstop2.c || exit 1
+rm -f sigstop2.c
+
+$dir/sigstop2
+s=$?
+[ -f sigstop2.core -a $s -eq 0 ] &&
+ { ls -l sigstop2.core; mv sigstop2.core $dir; s=1; }
+
+rm -rf $dir/sigstop2
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#define N 100
+#define PARALLEL 4
+#define RUNTIME (1 * 60)
+#define SYNC 0
+
+static void
+test(void)
+{
+ pid_t pids[N];
+ int i;
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ for (i = 0; i < N; i++) {
+ if ((pids[i] = fork()) == 0) {
+ for (;;)
+ pause();
+ _exit(0);
+ }
+ }
+
+ for (i = 0; i < N; i++)
+ if (kill(pids[i], SIGSTOP) == -1)
+ err(1, "kill(%d)", pids[i]);
+ for (i = 0; i < N; i++)
+ if (kill(pids[i], SIGINT) == -1)
+ err(1, "kill(%d)", pids[i]);
+ for (i = 0; i < N; i++)
+ if (kill(pids[i], SIGKILL) == -1)
+ err(1, "kill(%d)", pids[i]);
+ for (i = 0; i < N; i++)
+ if (waitpid(pids[i], NULL, 0) != pids[i])
+ err(1, "waitpid(%d)", pids[i]);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ time_t start;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/sigxcpu.sh b/tools/test/stress2/misc/sigxcpu.sh
new file mode 100755
index 000000000000..3d478fd19286
--- /dev/null
+++ b/tools/test/stress2/misc/sigxcpu.sh
@@ -0,0 +1,155 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "panic: softdep_deallocate_dependencies: dangling deps" seen:
+# http://people.freebsd.org/~pho/stress/log/sigxcpu7.txt
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > sigxcpu.c
+mycc -o sigxcpu -Wall -Wextra sigxcpu.c
+rm -f sigxcpu.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 4g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=4m
+export RUNDIR=$mntpoint/stressX
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export KBLOCKS=$(($1 / 4))
+export INODES=$(($2 / 4))
+
+su $testuser -c 'ulimit -t 3; cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1 &
+sleep 5
+for i in `jot 10`; do
+ /tmp/sigxcpu $mntpoint
+done
+kill $! > /dev/null 2>&1
+../tools/killall.sh || ../tools/killall.sh
+wait
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/sigxcpu
+exit 0
+EOF
+#include <sys/param.h>
+#include <err.h>
+#include <errno.h>
+#include <fts.h>
+#include <libutil.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static void
+hand(int i __unused) { /* handler */
+ _exit(1);
+}
+
+int
+test(char *path)
+{
+
+ FTS *fts;
+ FTSENT *p;
+ int ftsoptions;
+ char *args[2];
+
+ ftsoptions = FTS_PHYSICAL;
+ args[0] = path;
+ args[1] = 0;
+
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ while ((p = fts_read(fts)) != NULL) {
+ switch (p->fts_info) {
+ case FTS_F: /* Ignore. */
+ break;
+ case FTS_D: /* Ignore. */
+ break;
+ case FTS_DP:
+ break;
+ case FTS_DC: /* Ignore. */
+ break;
+ case FTS_SL: /* Ignore. */
+ break;
+ case FTS_DNR: /* Warn, continue. */
+ case FTS_ERR:
+ case FTS_NS:
+ case FTS_DEFAULT:
+ break;
+ default:
+ printf("%s: default, %d\n", getprogname(), p->fts_info);
+ break;
+ }
+ }
+
+ if (errno != 0 && errno != ENOENT)
+ err(1, "fts_read");
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+
+ return (0);
+}
+
+int
+main(int argc, char **argv)
+{
+ int i;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <path>", argv[0]);
+ signal(SIGALRM, hand);
+ alarm(20);
+ for (i = 0; i < 100; i++)
+ test(argv[1]);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/smrstress.sh b/tools/test/stress2/misc/smrstress.sh
new file mode 100755
index 000000000000..5806f9231520
--- /dev/null
+++ b/tools/test/stress2/misc/smrstress.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+# Run the tools/uma/smrstress test by Jeffrey Roberson <jeff@FreeBSD.org>
+
+# "panic: vm_page_assert_xbusied: page 0xf... not exclusive busy" seen
+# https://people.freebsd.org/~pho/stress/log/jeff146.txt
+# Fixed by r357392
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+dir=/usr/src/tools/uma/smrstress
+mod=/boot/modules/smrstress.ko
+[ -d $dir ] || exit 0
+
+rm -f $mod
+here=`pwd`
+cd $dir
+make all install
+[ -f $mod ] || exit 1
+
+(cd $here/../testcases/swap; ./swap -t 2m -i 20 -l 100) &
+sleep 5
+kldload smrstress.ko
+kldunload smrstress.ko
+pkill swap
+wait
+
+make clean
+rm -f kldload.core $mod
+exit 0
diff --git a/tools/test/stress2/misc/smrstress2.sh b/tools/test/stress2/misc/smrstress2.sh
new file mode 100755
index 000000000000..8f9f4d86a4fd
--- /dev/null
+++ b/tools/test/stress2/misc/smrstress2.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Run the tools/uma/smrstress test by Jeffrey Roberson <jeff@FreeBSD.org>
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+dir=/usr/src/tools/uma/smrstress
+mod=/boot/modules/smrstress.ko
+[ -d $dir ] || exit 0
+
+rm -f $mod
+here=`pwd`
+cd $dir
+make all install
+[ -f $mod ] || exit 1
+cd $here
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+newfs $newfs_flags md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+chmod 777 $mntpoint
+export LOAD=80
+export rwLOAD=80
+export runRUNTIME=5m
+export RUNDIR=$mntpoint/stressX
+export MAXSWAPPCT=80
+export TESTPROGS=`cd ..; find testcases/ -perm -1 -type f | \
+ egrep -v "/run/"`
+
+(cd ..; ./run.sh $TESTPROGS) &
+sleep 30
+
+while pgrep -q run; do
+ kldunload smrstress.ko
+ kldload smrstress.ko
+ sleep 5
+done
+wait
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+checkfs /dev/md$mdstart; s=$?
+mdconfig -d -u $mdstart
+cd $dir; make clean; cd $here
+rm -f kldload.core $mod
+exit 0
diff --git a/tools/test/stress2/misc/snap.sh b/tools/test/stress2/misc/snap.sh
new file mode 100755
index 000000000000..8f91312cb6fb
--- /dev/null
+++ b/tools/test/stress2/misc/snap.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008, 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q "on /tmp (ufs," || exit 0
+mount | grep -q "/dev/md$mdstart on $mntpoint" && umount $mntpoint
+rm -f /tmp/.snap/stress2
+trap "rm -f /tmp/.snap/stress2" 0
+
+s=0
+for i in `jot 2`; do
+ mksnap_ffs /tmp /tmp/.snap/stress2
+ mdconfig -a -t vnode -f /tmp/.snap/stress2 -u $mdstart -o readonly || s=1
+ mount -r /dev/md$mdstart $mntpoint || s=2
+
+ ls -l $mntpoint > /dev/null
+
+ umount $mntpoint
+ mdconfig -d -u $mdstart
+ rm -f /tmp/.snap/stress2
+done
+
+for i in `jot 2`; do
+ mksnap_ffs /tmp /tmp/.snap/stress2
+ rm -f /tmp/.snap/stress2
+done
+exit $s
diff --git a/tools/test/stress2/misc/snap10.sh b/tools/test/stress2/misc/snap10.sh
new file mode 100755
index 000000000000..53fba0ee652d
--- /dev/null
+++ b/tools/test/stress2/misc/snap10.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "kernel: g_vfs_done():md5a[READ(offset=103087603712,
+# length=28672)]error = 5" seen.
+
+# Fixed by: r321040
+
+. ../default.cfg
+
+s=0
+log=/tmp/snap10.log
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 512m -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+while ! mksnap_ffs $mntpoint $mntpoint/.snap/stress2; do :; done
+
+m2=$((mdstart + 1))
+[ -c /dev/md$m2 ] && mdconfig -d -u $m2
+mdconfig -a -t vnode -f $mntpoint/.snap/stress2 -u $m2 -o readonly
+fsck -t ufs -y /dev/md$m2 > $log 2>&1 || { echo "fsck failed"; s=1; }
+egrep -v "WARNING|WRITE" $log | LANG=C grep -q "[A-Z][A-Z]" &&
+ { cat $log; s=2; }
+egrep -q "check-hash|Can't open" $log && { cat $log; s=3; }
+mdconfig -d -u $m2
+
+sleep 1
+tail -50 /var/log/messages | grep -m 1 "g_vfs_done():md5a" && s=4
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1
+mdconfig -d -u $mdstart
+rm -rf $log
+exit $s
diff --git a/tools/test/stress2/misc/snap11.sh b/tools/test/stress2/misc/snap11.sh
new file mode 100755
index 000000000000..f4e499d7deda
--- /dev/null
+++ b/tools/test/stress2/misc/snap11.sh
@@ -0,0 +1,87 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Page fault seen in call from handle_workitem_freeblocks():
+# https://people.freebsd.org/~pho/stress/log/snap11.txt
+# Fixed by r321347-9.
+
+# "panic: handle_written_inodeblock: not started" seen:
+# https://people.freebsd.org/~pho/stress/log/snap11-2.txt
+
+# "panic: Barrier request in delayed write" seen:
+# https://people.freebsd.org/~pho/stress/log/snap11-3.txt
+
+. ../default.cfg
+
+s=0
+log=/tmp/snap11.log
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+last=`tail -1 /var/log/messages | cut -c1-15`
+[ -z "$last" ] && last=dummy
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=2m
+(cd ..; ./run.sh io.cfg) > /dev/null &
+
+while kill -0 $! 2>/dev/null; do
+ rm -f $mntpoint/.snap/stress2
+ mksnap_ffs $mntpoint $mntpoint/.snap/stress2 || continue
+
+ m2=$((mdstart + 1))
+ [ -c /dev/md$m2 ] && mdconfig -d -u $m2
+ mdconfig -a -t vnode -f $mntpoint/.snap/stress2 -u $m2 -o readonly
+ fsck -t ufs -y /dev/md$m2 > $log 2>&1 || { echo "fsck failed"; s=1; }
+ egrep -v "WARNING|WRITE" $log | LANG=C grep -q "[A-Z][A-Z]" &&
+ { cat $log; s=1; }
+ egrep -q "check-hash|Can't open" $log && { cat $log; s=1; }
+ mdconfig -d -u $m2
+ [ $s -ne 0 ] && { ../tools/killall.sh; break; }
+done
+wait
+
+sleep 1
+tail -50 /var/log/messages | grep -m 1 "g_vfs_done():md5a" && s=1
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1
+fsck -t ufs -y /dev/md${mdstart}$part > $log 2>&1 || s=1
+egrep -v "IS CLEAN" $log | grep -q "[A-Z]" $log || { cat $log; s=1; }
+mdconfig -d -u $mdstart
+rm -rf $log
+sed "1,/$last/d" < /var/log/messages | tail -20 | grep -m 1 "check-hash" && s=1
+exit $s
diff --git a/tools/test/stress2/misc/snap12.sh b/tools/test/stress2/misc/snap12.sh
new file mode 100755
index 000000000000..137e1930fa27
--- /dev/null
+++ b/tools/test/stress2/misc/snap12.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Simple regression test for
+# "Superblock check-hash failed: recorded check-hash 0xba55a4ff != computed
+# check-hash 0x709ea926".
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+s=0
+log=/tmp/snap12.log
+m2=$((mdstart + 1))
+mp2=${mntpoint}$m2
+[ -d $mp2 ] || mkdir -p $mp2
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+[ -c /dev/md$m2 ] && mdconfig -d -u $m2
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+touch $mntpoint/file
+
+rm -f $mntpoint/.snap/stress2
+for i in 1; do
+ mksnap_ffs $mntpoint $mntpoint/.snap/stress2 || { s=1; break; }
+ mdconfig -a -t vnode -f $mntpoint/.snap/stress2 -u $m2 -o readonly ||
+ { s=2; break; }
+ mount -t ufs -o ro /dev/md$m2 $mp2 || { s=3; break; }
+ [ -f $mp2/file ] || { s=4; ls -l $mp2; }
+ umount $mp2
+ mdconfig -d -u $m2
+done
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1
+fsck -t ufs -y /dev/md${mdstart}$part > $log 2>&1 || s=5
+egrep -v "IS CLEAN" $log | grep -q "[A-Z]" $log || { cat $log; s=6; }
+mdconfig -d -u $mdstart
+rm -rf $log
+exit $s
diff --git a/tools/test/stress2/misc/snap2-1.sh b/tools/test/stress2/misc/snap2-1.sh
new file mode 100755
index 000000000000..1c30a01cf477
--- /dev/null
+++ b/tools/test/stress2/misc/snap2-1.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008, 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# Regression test: Delete an active md device
+#
+# panic(c088cd33,deadc000,c0943aa0,0,c08753e1) at panic+0x14b
+# vm_fault(c1060000,deadc000,1,0,c54de480) at vm_fault+0x1e0
+# trap_pfault(e76728b8,0,deadc112) at trap_pfault+0x137
+# trap(8,c0870028,28,deadc0de,deadc0de) at trap+0x355
+# calltrap() at calltrap+0x5
+# --- trap 0xc, eip = 0xc060bcfb, esp = 0xe76728f8, ebp = 0xe767291c ---
+# g_io_request(c53ff7bc,c5051d40,d8c72408,c54ca110,e7672950) at g_io_request+0x5f
+
+mount | grep -q "on /tmp (ufs," || exit 0
+rm -f /tmp/.snap/stress2
+[ -d /tmp/.snap ] || mkdir /tmp/.snap
+trap "rm -f /tmp/.snap/stress2" 0
+mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mksnap_ffs /tmp /tmp/.snap/stress2
+mdconfig -a -t vnode -o readonly -f /tmp/.snap/stress2 -u $mdstart
+mount -o ro /dev/md$mdstart $mntpoint
+
+ls -lR $mntpoint > /dev/null 2>&1 &
+mdconfig -d -u $mdstart > /dev/null 2>&1
+
+umount -f $mntpoint
+mdconfig -d -u $mdstart
+rm -f /tmp/.snap/stress2
+wait
diff --git a/tools/test/stress2/misc/snap2.sh b/tools/test/stress2/misc/snap2.sh
new file mode 100755
index 000000000000..f9517c12cbe9
--- /dev/null
+++ b/tools/test/stress2/misc/snap2.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Regression test: Delete an active md device
+#
+# panic(c088cd33,deadc000,c0943aa0,0,c08753e1) at panic+0x14b
+# vm_fault(c1060000,deadc000,1,0,c54de480) at vm_fault+0x1e0
+# trap_pfault(e76728b8,0,deadc112) at trap_pfault+0x137
+# trap(8,c0870028,28,deadc0de,deadc0de) at trap+0x355
+# calltrap() at calltrap+0x5
+# --- trap 0xc, eip = 0xc060bcfb, esp = 0xe76728f8, ebp = 0xe767291c ---
+# g_io_request(c53ff7bc,c5051d40,d8c72408,c54ca110,e7672950) at g_io_request+0x5f
+
+. ../default.cfg
+
+mount | grep -q "on /tmp (ufs," || exit 0
+rm -f /tmp/.snap/stress2
+trap "rm -f /tmp/.snap/stress2" 0
+mount | grep "$mntpoint" | grep -q md$mdstart && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mksnap_ffs /tmp /tmp/.snap/stress2
+mdconfig -a -t vnode -f /tmp/.snap/stress2 -u $mdstart -o readonly
+mount -r /dev/md$mdstart $mntpoint
+
+ls -lR > /dev/null 2>&1 &
+mdconfig -d -u $mdstart > /dev/null 2>&1
+
+umount $mntpoint > /dev/null 2>&1
+umount -f $mntpoint > /dev/null 2>&1
+mdconfig -d -u $mdstart
+rm -f /tmp/.snap/stress2
+wait
diff --git a/tools/test/stress2/misc/snap3.sh b/tools/test/stress2/misc/snap3.sh
new file mode 100755
index 000000000000..04c514f9864d
--- /dev/null
+++ b/tools/test/stress2/misc/snap3.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008, 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# Test with two snapshots
+# 20070506 Page fault in g_io_request+0x7f
+
+mount | grep -q "on /tmp (ufs," || exit 0
+rm -f /tmp/.snap/stress2.1
+rm -f /tmp/.snap/stress2.2
+trap "rm -f /tmp/.snap/stress2.?" 0
+mount | grep $mntpoint | grep -q md && umount $mntpoint
+m1=$mdstart
+m2=$((m1 + 1))
+[ -c /dev/md$m1 ] && mdconfig -d -u $m1
+[ -c /dev/md$m2 ] && mdconfig -d -u $m2
+
+start=`date '+%s'`
+while [ `date '+%s'` -lt $((start + 1200)) ]; do
+ mksnap_ffs /tmp /tmp/.snap/stress2.1
+ mksnap_ffs /tmp /tmp/.snap/stress2.2
+ if [ -r /tmp/.snap/stress2.1 -a -r /tmp/.snap/stress2.2 ]; then
+ mdconfig -a -t vnode -f /tmp/.snap/stress2.1 -u $m1 -o readonly
+ mdconfig -a -t vnode -f /tmp/.snap/stress2.2 -u $m2 -o readonly
+ mount -o ro /dev/md$m1 $mntpoint
+ sleep 3
+ for i in `jot 3`; do
+ umount $mntpoint && break
+ sleep 2
+ done
+ mdconfig -d -u $m1
+ mdconfig -d -u $m2
+ fi
+ rm -f /tmp/.snap/stress2.1 /tmp/.snap/stress2.2
+done
+exit 0
diff --git a/tools/test/stress2/misc/snap4.sh b/tools/test/stress2/misc/snap4.sh
new file mode 100755
index 000000000000..7364e487afea
--- /dev/null
+++ b/tools/test/stress2/misc/snap4.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008, 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Test with snapshot file unlinked before unmount.
+
+# "panic: snapacct_ufs2: bad block" seen:
+# https://people.freebsd.org/~pho/stress/log/snap4.txt
+
+. ../default.cfg
+
+mount | grep -q "on /tmp (ufs," || exit 0
+mount | grep -q "/dev/md$mdstart on $mntpoint" && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+rm -f /tmp/.snap/stress2
+trap "rm -f /tmp/.snap/stress2" 0
+
+start=`date '+%s'`
+while [ `date '+%s'` -lt $((start + 1200)) ]; do
+ mksnap_ffs /tmp /tmp/.snap/stress2
+ mdconfig -a -t vnode -f /tmp/.snap/stress2 -u $mdstart -o readonly
+ mount -o ro /dev/md$mdstart $mntpoint
+
+ ls -l $mntpoint > /dev/null
+ rm -f /tmp/.snap/stress2
+ sleep 1
+
+ umount $mntpoint
+ mdconfig -d -u $mdstart
+ rm -f /tmp/.snap/stress2
+done
diff --git a/tools/test/stress2/misc/snap5-1.sh b/tools/test/stress2/misc/snap5-1.sh
new file mode 100755
index 000000000000..e6e2ea91bd20
--- /dev/null
+++ b/tools/test/stress2/misc/snap5-1.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008, 2011 Peter Holm <pho@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 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.
+#
+
+# "panic: snapacct_ufs2: bad block" seen:
+# https://people.freebsd.org/~pho/stress/log/snap5-1.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q "on /tmp (ufs," || exit 0
+mnt2=$mntpoint
+[ ! -d $mnt2 ] && mkdir $mnt2
+
+mkdir -p /tmp/.snap
+trap "rm -f /tmp/.snap/stress2" 0
+start=`date '+%s'`
+while [ `date '+%s'` -lt $((start + 600)) ]; do
+ if mount | grep -q "/dev/md$mdstart on $mnt2"; then
+ umount $mnt2 || exit 2
+ fi
+ if mdconfig -l | grep -q md$mdstart; then
+ mdconfig -d -u $mdstart || exit 3
+ fi
+ rm -f /tmp/.snap/stress2
+
+ mksnap_ffs /tmp /tmp/.snap/stress2 || continue
+ mdconfig -a -t vnode -f /tmp/.snap/stress2 -u $mdstart -o readonly ||
+ exit 4
+ mount -o ro /dev/md$mdstart $mnt2 || exit 5
+
+ ls -l $mnt2 > /dev/null
+ sleep `jot -r 1 0 119`
+done
+mount | grep -q "/dev/md$mdstart on $mnt2" && umount $mnt2
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/snap5.sh b/tools/test/stress2/misc/snap5.sh
new file mode 100755
index 000000000000..7b446c4acbcd
--- /dev/null
+++ b/tools/test/stress2/misc/snap5.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008, 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q "on /tmp (ufs," || exit 0
+mnt2=${mntpoint}2
+[ ! -d $mnt2 ] && mkdir $mnt2
+mount | grep -q "/dev/md$mdstart on $mnt2" && umount $mnt2
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+rm -f /tmp/.snap/stress2
+trap "rm -f /tmp/.snap/stress2" 0
+
+start=`date '+%s'`
+while [ `date '+%s'` -lt $((start + 1200)) ]; do
+ date '+%T'
+ mksnap_ffs /tmp /tmp/.snap/stress2
+ mdconfig -a -t vnode -f /tmp/.snap/stress2 -u $mdstart -o readonly
+ mount -o ro /dev/md$mdstart $mnt2
+
+ ls -l $mnt2 > /dev/null
+ r=`head -c4 /dev/random | od -N2 -tu4 | sed -ne '1s/ *$//;1s/.* //p'`
+ sleep $(( r % 120 ))
+
+ umount $mnt2
+ mdconfig -d -u $mdstart
+ rm -f /tmp/.snap/stress2
+done
diff --git a/tools/test/stress2/misc/snap6.sh b/tools/test/stress2/misc/snap6.sh
new file mode 100755
index 000000000000..4ad9b3c3d58c
--- /dev/null
+++ b/tools/test/stress2/misc/snap6.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+# Problem Report kern/92272 : [ffs] [hang] Filling a filesystem while creating
+# a snapshot on it locks the system
+
+# John Kozubik <john@kozubik.com>
+
+# If a filesystem is completely full (approaching 110% in `df` output on
+# most systems), mksnap_ffs will refuse to begin a snapshot for that reason.
+# There seem to be no negative consequences.
+
+# However, if the filesystem is not quite completely full, mksnap_ffs will
+# (correctly) begin creating the snapshot as expected. If, during the course
+# of snapshot creation, the filesystem being snapshotted becomes full,
+# mksnap_ffs will exit with an error exactly as it does if it is started on
+# an already full filesystem.
+
+# However, several hours later, the system will lock up completely. It still
+# responds to pings, and open connections to and from it remain in that
+# state, but they cannot be used and new connections cannot be established.
+
+# *** This script does not provoke the problem. ***
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+root=/var
+
+mount | grep "on `df /var | tail -1 | awk '{print $NF}'`" |
+ grep -q 'journaled soft-updates' && exit 0
+rm -f $root/.snap/stress2 $root/big $root/big2
+trap "rm -f $root/.snap/stress2 $root/big $root/big2" 0
+free=`df $root | tail -1 | awk '{print $4}'`
+timeout 5m dd if=/dev/zero of=$root/big bs=1m count=$(( free / 1024 - 90)) \
+ status=none
+df $root
+
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 300 ]; do
+ date
+ nice -20 mksnap_ffs $root $root/.snap/stress2 &
+ timeout 5m dd if=/dev/zero of=$root/big2 bs=1m status=none
+ wait
+ [ -f $root/.snap/stress2 ] && exit 0
+ rm -f $root/.snap/stress2 $root/big2
+done
+df $root
+
+rm -f $root/.snap/stress2 $root/big $root/big2
diff --git a/tools/test/stress2/misc/snap7.sh b/tools/test/stress2/misc/snap7.sh
new file mode 100755
index 000000000000..b106be9e552d
--- /dev/null
+++ b/tools/test/stress2/misc/snap7.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008, 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# Test with unmount and paralless access to mountpoint
+# 20070508 page fault in g_io_request+0xa6
+
+mount | grep -q "on /tmp (ufs," || exit 0
+mount | grep -q "/dev/md$mdstart on $mntpoint" && umount $mntpoint
+rm -f /tmp/.snap/stress2.1
+trap "rm -f /tmp/.snap/stress2.1" 0
+mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+start=`date '+%s'`
+while [ `date '+%s'` -lt $((start + 1200)) ]; do
+ mksnap_ffs /tmp /tmp/.snap/stress2.1
+ mdconfig -a -t vnode -f /tmp/.snap/stress2.1 -u $mdstart -o readonly
+ sh -c "while true; do ls $mntpoint > /dev/null;done" &
+ for i in `jot 64`; do
+ mount -o ro /dev/md$mdstart $mntpoint 2>/dev/null
+ umount $mntpoint 2>/dev/null
+ done
+ kill $!
+ wait
+ while mount | grep -q "/dev/md$mdstart on $mntpoint"; do
+ umount $mntpoint 2>/dev/null
+ done
+ mdconfig -d -u $mdstart
+ rm -f /tmp/.snap/stress2.1
+done
diff --git a/tools/test/stress2/misc/snap8.sh b/tools/test/stress2/misc/snap8.sh
new file mode 100755
index 000000000000..412cd06805af
--- /dev/null
+++ b/tools/test/stress2/misc/snap8.sh
@@ -0,0 +1,92 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Deadlock seen when deleting the snapshots during an "ls" of the FS
+
+# Based on test scenario by John Kozubik <john at kozubik dot com>
+# kern/94769: [ufs] Multiple file deletions on multi-snapshotted filesystems
+# causes hang
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q /dev/md${mdstart}$part && umount -f /dev/md${mdstart}$part
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+parallel=20
+size=25 # Gb
+mx=$((`sysctl -n hw.physmem` / 1024 / 1024 / 1024 / 2))
+[ $size -gt $mx ] && size=$mx
+
+[ `df -k $(dirname $diskimage) | tail -1 | awk '{print $4}'` -lt \
+ $((size * 1024 * 1024)) ] && \
+ echo "Not enough disk space." && exit 1
+truncate -s ${size}G $diskimage
+
+mdconfig -a -t vnode -f $diskimage -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -O2 $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+mycc -o /tmp/fstool -Wall ../tools/fstool.c
+for i in `jot $parallel`; do
+ (mkdir $mntpoint/test$i; cd $mntpoint/test$i; \
+ timeout 10m /tmp/fstool -l -f 50 -n 500 -s 8k) &
+done
+wait
+rm -f /tmp/fstool
+
+mksnap_ffs $mntpoint $mntpoint/.snap/snap1
+mksnap_ffs $mntpoint $mntpoint/.snap/snap2
+mksnap_ffs $mntpoint $mntpoint/.snap/snap3
+mksnap_ffs $mntpoint $mntpoint/.snap/snap4
+mksnap_ffs $mntpoint $mntpoint/.snap/snap5
+mksnap_ffs $mntpoint $mntpoint/.snap/snap6
+mksnap_ffs $mntpoint $mntpoint/.snap/snap7
+mksnap_ffs $mntpoint $mntpoint/.snap/snap8
+mksnap_ffs $mntpoint $mntpoint/.snap/snap9
+
+for i in `jot $parallel`; do
+ rm -rf $mntpoint/test$i &
+done
+wait
+
+rm -rf $mntpoint/.snap/snap? &
+
+for i in `jot 10`; do
+ ls -lsrt $mntpoint > /dev/null 2>&1
+ sleep 2
+done
+wait
+
+umount /dev/md${mdstart}$part
+
+mount | grep -q /dev/md${mdstart}$part && umount -f /dev/md${mdstart}$part
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+rm -f $diskimage
diff --git a/tools/test/stress2/misc/snap9.sh b/tools/test/stress2/misc/snap9.sh
new file mode 100755
index 000000000000..d1ba0e968427
--- /dev/null
+++ b/tools/test/stress2/misc/snap9.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# Disk full with one snapshot scenario
+# "panic: softdep_deallocate_dependencies: unrecovered I/O error" seen.
+
+# kern/162362: ufs with snapshot(s) panics when getting full
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+dd if=/dev/zero of=$mntpoint/big1 bs=1m count=512 status=none
+dd if=/dev/zero of=$mntpoint/big2 bs=1m count=512 status=none
+dd if=/dev/zero of=$mntpoint/big3 bs=1m count=512 status=none
+
+mksnap_ffs $mntpoint $mntpoint/.snap/snap
+
+for i in `jot 10`; do
+ dd if=/dev/zero of=$mntpoint/big.$i bs=1m count=512 status=none ||
+ break
+done
+rm $mntpoint/big.*
+rm -f $mntpoint/.snap/snap
+
+while mount | grep "$mntpoint" | grep -q md$mdstart; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/sndstat.sh b/tools/test/stress2/misc/sndstat.sh
new file mode 100755
index 000000000000..ddd5b5087334
--- /dev/null
+++ b/tools/test/stress2/misc/sndstat.sh
@@ -0,0 +1,138 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Test of access to /dev/sndstat
+
+# Permanent "/dev/sndstat: Device busy" seen.
+# "panic: sbuf_put_byte called with finished or corrupt sbuf" seen.
+# Fixed in r234932
+
+. ../default.cfg
+
+[ -r /dev/sndstat ] || exit 0
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > sndstat.c
+mycc -o sndstat -Wall -Wextra sndstat.c -lpthread
+rm -f sndstat.c
+
+/tmp/sndstat > /dev/null
+
+rm -f /tmp/sndstat
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/syscall.h>
+#include <sys/uio.h>
+#include <dirent.h>
+#include <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+int fd;
+char path[] = "/dev/sndstat";
+
+void *
+test1(void *arg __unused)
+{
+
+ int sfd, i, n;
+ char buf[512];
+
+ n = 0;
+ for (i = 0; i < 5; i++) {
+ if ((sfd = open(path, O_RDONLY)) == -1)
+ continue;
+
+ read(sfd, buf, sizeof(buf));
+ fprintf(stdout, "%s\n", buf);
+
+ n++;
+ close(sfd);
+ }
+ if (n == 0) {
+ if ((sfd = open(path, O_RDONLY)) == -1)
+ warn("FAIL open(%s)", path);
+ if (sfd > 0)
+ close(sfd);
+ }
+
+ return (0);
+}
+
+void *
+test2(void *arg __unused)
+{
+ char buf[512];
+
+ bzero(buf, sizeof(buf));
+ if (read(fd, buf, sizeof(buf)) != -1)
+ fprintf(stdout, "%s\n", buf);
+ else
+ warn("read()");
+
+ return (0);
+}
+
+int
+main(void)
+{
+ pthread_t rp[10];
+ int e, i, j;
+
+ /* Parallel open test */
+ for (i = 0; i < 10; i++) {
+ if ((e = pthread_create(&rp[i], NULL, test1, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ }
+ for (i = 0; i < 10; i++)
+ pthread_join(rp[i], NULL);
+
+ /* Parallel read test */
+ for (i = 0; i < 10; i++) {
+ if ((fd = open(path, O_RDONLY)) == -1) {
+ warn("open()");
+ continue;
+ }
+ for (j = 0; j < 4; j++)
+ if ((e = pthread_create(&rp[j], NULL, test2, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ for (j = 0; j < 4; j++)
+ pthread_join(rp[j], NULL);
+
+ close(fd);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/socketpair.sh b/tools/test/stress2/misc/socketpair.sh
new file mode 100755
index 000000000000..dae52b3ed35a
--- /dev/null
+++ b/tools/test/stress2/misc/socketpair.sh
@@ -0,0 +1,104 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Page fault due to recursion. Fixed in r216150.
+
+# Looping in kernel: http://people.freebsd.org/~pho/stress/log/kostik737.txt
+# Fixed in r274712
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > socketpair.c
+mycc -o socketpair -Wall -Wextra -O2 socketpair.c
+rm -f socketpair.c
+[ -d $RUNDIR ] || mkdir -p $RUNDIR
+cd $RUNDIR
+
+ulimit -b 10485760
+/tmp/socketpair
+
+cd $here
+rm -f /tmp/socketpair
+exit 0
+EOF
+/* From http://lkml.org/lkml/2010/11/25/8 */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mount.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+static int
+send_fd(int unix_fd, int fd)
+{
+ struct msghdr msgh;
+ struct cmsghdr *cmsg;
+ char buf[CMSG_SPACE(sizeof(fd))];
+ memset(&msgh, 0, sizeof(msgh));
+ memset(buf, 0, sizeof(buf));
+
+ msgh.msg_control = buf;
+ msgh.msg_controllen = sizeof(buf);
+
+ cmsg = CMSG_FIRSTHDR(&msgh);
+ cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+
+ msgh.msg_controllen = cmsg->cmsg_len;
+
+ memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
+ return sendmsg(unix_fd, &msgh, 0);
+}
+
+int
+main()
+{
+ int fd[2], ff[2];
+
+ if (socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fd) == -1)
+ return 1;
+ for (;;) {
+ if (socketpair(PF_UNIX, SOCK_SEQPACKET, 0, ff) == -1)
+ return 2;
+ send_fd(ff[0], fd[0]);
+ send_fd(ff[0], fd[1]);
+ close(fd[1]);
+ close(fd[0]);
+ fd[0] = ff[0];
+ fd[1] = ff[1];
+ }
+}
diff --git a/tools/test/stress2/misc/socketpair2.sh b/tools/test/stress2/misc/socketpair2.sh
new file mode 100755
index 000000000000..fe4a277052b5
--- /dev/null
+++ b/tools/test/stress2/misc/socketpair2.sh
@@ -0,0 +1,273 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Default unix domain socket limits causes hang:
+# 1001 880 871 0 52 0 5780 1524 keglimit D+ 0 0:00.35 /tmp/socketpair2
+
+# Test scenario by peter@
+
+# Fixed in r269489.
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > socketpair2.c
+mycc -o socketpair2 -Wall -Wextra -O2 socketpair2.c || exit
+rm -f socketpair2.c
+
+/tmp/socketpair2 > /dev/null 2>&1
+
+rm -f /tmp/socketpair2
+exit 0
+EOF
+/*
+ Peter Wemm <peter wemm org>
+
+ Some systems seem to base how much can be written to the pipe based
+ on the size of the socket receive buffer (read-side), while others
+ on the size of the socket send buffer (send-side).
+
+ This little hack tries to make an educated guess as to what is the
+ case on this particular system.
+*/
+
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#ifndef MIN
+#define MIN(a,b) ((a) < (b) ? (a) : (b))
+#endif /* !MIN */
+
+#ifndef MAX
+#define MAX(a,b) ((a) > (b) ? (a) : (b))
+#endif /* !MAX */
+
+#if NEED_AF_LOCAL
+#define AF_LOCAL AF_UNIX
+#endif /* NEED_AF_LOCAL */
+
+#define PACKETSIZE (1024)
+
+#define SEND_PIPE (0)
+#define RECV_PIPE (1)
+
+#define EXIT_SENDSIDE (1)
+#define EXIT_READSIDE (0) /* looking for readside - exit 0 */
+#define EXIT_UNKNOWN (1)
+
+static void
+setsockets(const int doreverse, const size_t packetsize,
+ const int s, const int r,
+ size_t *sndbuf, size_t *sndbuf_set,
+ size_t *rcvbuf, size_t *rcvbuf_set);
+
+static size_t
+sendtest(const int s, const char *buf, const size_t buflen);
+
+int
+main(void)
+{
+ size_t sent, packetcount, sndbuf, sndbuf_set, rcvbuf, rcvbuf_set;
+ char buf[PACKETSIZE - 64]; /* allow for some padding between messages. */
+ int datapipev[2];
+
+ if (socketpair(AF_LOCAL, SOCK_DGRAM, 0, datapipev) != 0) {
+ perror("socketpair()");
+ exit(EXIT_UNKNOWN);
+ }
+
+ setsockets(0,
+ PACKETSIZE,
+ datapipev[SEND_PIPE],
+ datapipev[RECV_PIPE],
+ &sndbuf, &sndbuf_set,
+ &rcvbuf, &rcvbuf_set);
+
+ packetcount = MIN(sndbuf, sndbuf_set) / PACKETSIZE;
+ fprintf(stderr, "Requested sndbuf to be %ld, is %ld. "
+ "Requested rcvbuf to be %ld, is %ld.\n"
+ "Calculated packetcount is %lu\n",
+ (long)sndbuf, (long)sndbuf_set,
+ (long)rcvbuf, (long)rcvbuf_set, (unsigned long)packetcount);
+
+ sent = sendtest(datapipev[SEND_PIPE], buf, sizeof(buf));
+ if (sent >= (size_t)sndbuf) {
+ fprintf(stderr, "status determined by send-side\n");
+ return EXIT_SENDSIDE;
+ }
+
+ /*
+ * Try the reverse. Perhaps this system wants a large rcvbuf rather than
+ * a large sndbuf.
+ */
+ close(datapipev[SEND_PIPE]);
+ close(datapipev[RECV_PIPE]);
+
+ if (socketpair(AF_LOCAL, SOCK_DGRAM, 0, datapipev) != 0) {
+ perror("socketpair()");
+ exit(EXIT_UNKNOWN);
+ }
+
+ setsockets(1,
+ PACKETSIZE,
+ datapipev[SEND_PIPE],
+ datapipev[RECV_PIPE],
+ &sndbuf, &sndbuf_set,
+ &rcvbuf, &rcvbuf_set);
+
+ packetcount = MIN(rcvbuf, rcvbuf_set) / PACKETSIZE;
+ fprintf(stderr, "Requested sndbuf to be %ld, is %ld. "
+ "Requested rcvbuf to be %ld, is %ld.\n"
+ "Calculated packetcount is %lu\n",
+ (long)sndbuf, (long)sndbuf_set,
+ (long)rcvbuf, (long)rcvbuf_set, (unsigned long)packetcount);
+
+ sent = sendtest(datapipev[SEND_PIPE], buf, sizeof(buf));
+ if (sent >= (size_t)rcvbuf) {
+ fprintf(stderr, "status determined by read-side\n");
+ return EXIT_READSIDE;
+ }
+
+ fprintf(stderr, "status is unknown\n");
+ return EXIT_UNKNOWN;
+}
+
+static void
+setsockets(doreverse, packetsize, s, r, sndbuf, sndbuf_set, rcvbuf, rcvbuf_set)
+ const int doreverse;
+ const size_t packetsize;
+ const int s;
+ const int r;
+ size_t *sndbuf, *sndbuf_set;
+ size_t *rcvbuf, *rcvbuf_set;
+{
+ socklen_t len;
+ int p;
+
+ if ((p = fcntl(s, F_GETFL, 0)) == -1
+ || fcntl(s, F_SETFL, p | O_NONBLOCK) == -1
+ || fcntl(r, F_SETFL, p | O_NONBLOCK) == -1) {
+ perror("fcntl(F_SETFL/F_GETFL, O_NONBLOCK) failed");
+ exit(EXIT_UNKNOWN);
+ }
+
+ len = sizeof(*sndbuf_set);
+
+ if (doreverse) {
+ *sndbuf = packetsize;
+ if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, sndbuf, sizeof(*sndbuf)) != 0) {
+ perror("setsockopt(SO_SNDBUF)");
+ exit(EXIT_UNKNOWN);
+ }
+
+ if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, sndbuf_set, &len) != 0) {
+ perror("getsockopt(SO_SNDBUF)");
+ exit(EXIT_UNKNOWN);
+ }
+
+ *rcvbuf = *sndbuf_set * 10;
+ if (setsockopt(r, SOL_SOCKET, SO_RCVBUF, rcvbuf, sizeof(*rcvbuf)) != 0) {
+ perror("setsockopt(SO_RCVBUF)");
+ exit(EXIT_UNKNOWN);
+ }
+ }
+ else {
+ *rcvbuf = packetsize;
+ if (setsockopt(r, SOL_SOCKET, SO_RCVBUF, rcvbuf, sizeof(*rcvbuf)) != 0) {
+ perror("setsockopt(SO_RCVBUF)");
+ exit(EXIT_UNKNOWN);
+ }
+
+ if (getsockopt(r, SOL_SOCKET, SO_RCVBUF, rcvbuf_set, &len) != 0) {
+ perror("getsockopt(SO_RCVBUF)");
+ exit(EXIT_UNKNOWN);
+ }
+
+ *sndbuf = *rcvbuf_set * 10;
+ if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, sndbuf, sizeof(*sndbuf)) != 0) {
+ perror("setsockopt(SO_SNDBUF)");
+ exit(EXIT_UNKNOWN);
+ }
+ }
+
+ if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, sndbuf_set, &len) != 0
+ || getsockopt(r, SOL_SOCKET, SO_RCVBUF, rcvbuf_set, &len) != 0) {
+ perror("getsockopt(SO_SNDBUF/SO_RCVBUF)");
+ exit(EXIT_UNKNOWN);
+ }
+
+ fprintf(stderr, "sndbuf is %lu, rcvbuf is %lu\n",
+ (unsigned long)*sndbuf_set, (unsigned long)*rcvbuf_set);
+
+ if (doreverse) {
+ if (*rcvbuf_set < *rcvbuf) {
+ fprintf(stderr, "failed to set rcvbuf to %lu. Is %lu\n",
+ (unsigned long)*rcvbuf, (unsigned long)*rcvbuf_set);
+ exit(EXIT_UNKNOWN);
+ }
+ }
+ else {
+ if (*sndbuf_set < *sndbuf) {
+ fprintf(stderr, "failed to set sndbuf to %lu (is %lu)\n",
+ (unsigned long)*sndbuf, (unsigned long)*sndbuf_set);
+ exit(EXIT_UNKNOWN);
+ }
+ }
+}
+
+static size_t
+sendtest(s, buf, buflen)
+ const int s;
+ const char *buf;
+ const size_t buflen;
+{
+ ssize_t rc;
+ int i;
+
+ i = 1;
+ errno = 0;
+ while (errno == 0) {
+ if ((rc = sendto(s, buf, buflen, 0, NULL, 0)) != (ssize_t)buflen)
+ fprintf(stderr, "sendto(2) failed on iteration %d, sent %ld/%lu. "
+ "Total bytes sent: %lu. Error on last packet: %s\n",
+ i, (long)rc, (unsigned long)buflen,
+ (unsigned long)(i * buflen + MAX(rc, 0)), strerror(errno));
+ else
+ ++i;
+ }
+
+ return (size_t)(i * buflen + MAX(rc, 0));
+}
diff --git a/tools/test/stress2/misc/socketpair3.sh b/tools/test/stress2/misc/socketpair3.sh
new file mode 100755
index 000000000000..d96719cc6e0a
--- /dev/null
+++ b/tools/test/stress2/misc/socketpair3.sh
@@ -0,0 +1,144 @@
+#!/bin/sh
+
+# Bug 227285 - File descriptor passing does not work reliably on SMP system
+# (cache coherency issue?)
+# "socketpair3: read failed in parent: 0, so_error: No error: 0 (0), ret2: 0"
+# seen.
+
+# Original test scenario by: jan.kokemueller@gmail.com
+
+# Page fault seen in WiP socket code.
+
+. ../default.cfg
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/socketpair3.c
+mycc -o socketpair3 -Wall -Wextra -O0 -g socketpair3.c -lnv || exit 1
+rm -f socketpair3.c
+cd $odir
+
+for i in `jot 6`; do
+ $dir/socketpair3 &
+ pids="$pids $!"
+done
+s=0
+for i in $pids; do
+ wait $i
+ [ $? -ne 0 ] && s=1
+done
+[ -f socketpair3.core -a $s -eq 0 ] &&
+ { ls -l socketpair3.core; mv socketpair3.core /tmp; s=1; }
+
+rm -rf $dir/socketpair3
+exit $s
+
+EOF
+#include <sys/types.h>
+
+#include <sys/procdesc.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <poll.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+// From libnv.
+extern int fd_send(int sock, const int *fds, size_t nfds);
+extern int fd_recv(int sock, int *fds, size_t nfds);
+
+int main(void)
+{
+ pid_t pid;
+ time_t start;
+ int child_fd;
+ int sock[2];
+
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ if (socketpair(PF_UNIX, SOCK_STREAM, 0, sock) < 0)
+ err(1, "socketpair");
+
+ pid = pdfork(&child_fd, PD_CLOEXEC);
+ if (pid < 0)
+ err(1, "pdfork");
+
+ if (pid == 0) {
+ ssize_t ret;
+ int sock_child[2];
+ int dummy = 0;
+
+ close(sock[0]);
+ if (socketpair(PF_UNIX, SOCK_STREAM, /**/
+ 0, sock_child) < 0)
+ err(1, "socketpair");
+
+ if (fd_send(sock[1], &sock_child[0], 1) != 0)
+ errx(1, "fd_send failed");
+#ifdef WORKAROUND
+ if (read(sock[1], &dummy, 1) != 1)
+ err(1, "write");
+#endif
+
+ close(sock_child[0]);
+
+ if (write(sock_child[1], &dummy, 1) != 1)
+ err(1, "write");
+
+ if ((ret = read(sock_child[1], &dummy, 1)) != 1)
+ errx(1, "read failed in child: %d",
+ (int)ret);
+
+ close(sock_child[1]);
+
+ _exit(0);
+ }
+
+ close(sock[1]);
+
+ int sock_child;
+ uint8_t dummy;
+
+ if (fd_recv(sock[0], &sock_child, 1) != 0)
+ errx(1, "fd_recv failed");
+#ifdef WORKAROUND
+ if (write(sock[0], &dummy, 1) != 1)
+ err(1, "write");
+#endif
+
+ ssize_t ret;
+ if ((ret = read(sock_child, &dummy, 1)) != 1) {
+ int error;
+ socklen_t err_len = sizeof(error);
+
+ if (getsockopt(sock_child, SOL_SOCKET, SO_ERROR,
+ &error, &err_len) < 0)
+ err(1, "getsockopt");
+
+ ssize_t ret2 = read(sock_child, &dummy, 1);
+
+ errx(1,
+ "read failed in parent: %d, so_error: %s (%d), "
+ "ret2: %d", (int)ret, strerror(error), error,
+ (int)ret2);
+ }
+
+ if (write(sock_child, &dummy, 1) != 1)
+ err(1, "write");
+
+ close(sock_child);
+
+ struct pollfd pfd = { .fd = child_fd };
+ poll(&pfd, 1, -1);
+
+ close(child_fd);
+ close(sock[0]);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/socketpair4.sh b/tools/test/stress2/misc/socketpair4.sh
new file mode 100755
index 000000000000..c9bfd52a3bfc
--- /dev/null
+++ b/tools/test/stress2/misc/socketpair4.sh
@@ -0,0 +1,207 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# A socketpair() test scenario illustrating issues with a missing EOF
+# Fixed by r361037 + r361056
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/socketpair4.c
+mycc -o socketpair4 -Wall -Wextra -O0 -g socketpair4.c || exit 1
+rm -f socketpair4.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs_flags=""
+newfs $newfs_flags -n md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+set +e
+
+cd $mntpoint
+dd if=/dev/random of=input bs=4k count=100 status=none
+
+n=1
+start=` date +%s`
+while [ $((` date +%s` - start)) -lt 180 ]; do
+ rm -f output
+ umount $mntpoint 2>/dev/null # busy umount
+ $dir/socketpair4
+ s=$?
+ [ $s -ne 0 ] && { echo "Loop #$n"; ls -l; s=1; break; }
+ cmp -s input output || break
+ [ `stat -f '%z' input` -ne ` stat -f '%z' output` ] && break
+ n=$((n + 1))
+done
+while pgrep -q swap; do
+ pkill swap
+done
+cmp -s input output || { echo "Loop #$n"; ls -l; s=1; }
+[ -f socketpair4.core -a $s -eq 0 ] &&
+ { ls -l socketpair4.core; mv socketpair4.core $dir; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+rm -rf $dir/socketpair4
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/fcntl.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static void
+test(void)
+{
+ struct stat st;
+ off_t rd, written;
+ pid_t pid;
+ int error, from, n, status, sv[2], to;
+ char buf[4086];
+ const char *from_name, *to_name;
+
+ from_name = "input";
+ to_name = "output";
+
+ if ((error = socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) == -1)
+ err(1, "socketpair");
+
+ if ((from = open(from_name, O_RDONLY)) == -1)
+ err(1, "open read %s", from_name);
+
+ if ((error = fstat(from, &st)) == -1)
+ err(1, "stat %s", from_name);
+
+ pid = fork();
+ if (pid == -1)
+ err(1, "fork");
+ else if (pid != 0) {
+ setproctitle("parent");
+ close(sv[1]);
+
+ written = 0;
+ for (;;) {
+ n = read(from, buf, sizeof(buf));
+ if (n == -1)
+ err(1, "read(from)");
+ else if (n == 0)
+ break;
+ written += n;
+ if (write(sv[0], buf, n) != n)
+ err(1, "write()");
+ if (error == -1)
+ err(1, "write");
+ }
+ if (written != st.st_size)
+ fprintf(stderr, "%d written, expected %d\n",
+ (int)written, (int)st.st_size);
+ close(from);
+ close(sv[0]);
+ if (waitpid(pid, &status, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+ _exit(WEXITSTATUS(status));
+ } else {
+ setproctitle("child");
+ sleep(1);
+ close(from);
+ close(sv[0]);
+
+ if ((to = open(to_name, O_RDWR | O_CREAT, DEFFILEMODE)) == -1)
+ err(1, "open write %s", to_name);
+
+ rd = 0;
+ for (;;) {
+ n = read(sv[1], buf, sizeof(buf));
+ if (n == -1) {
+ if (rd == st.st_size) {
+ err(1, "Expected EOF on read(socket)");
+ break;
+ }
+ err(1, "read(socket), n=%d, rd=%d", n,
+ (int)rd);
+ }
+ if (n == 0)
+ break;
+ rd += n;
+ if (write(to, buf, n) != n)
+ err(1, "write()");
+ }
+ close(to);
+ close(sv[1]);
+ if (rd != st.st_size)
+ fprintf(stderr, "Short read %d, expected %d\n",
+ (int)rd, (int)st.st_size);
+ _exit(0);
+ }
+}
+
+int
+main(void)
+{
+ pid_t pid;
+ int status;
+
+ if ((pid = fork()) == 0)
+ test();
+ if (pid == -1)
+ err(1, "fork()");
+ if (waitpid(pid, &status, 0) == -1)
+ err(1, "waitpid(%d)", pid);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pid, WTERMSIG(status));
+ }
+
+ return (WEXITSTATUS(status));
+}
diff --git a/tools/test/stress2/misc/softupdate.sh b/tools/test/stress2/misc/softupdate.sh
new file mode 100755
index 000000000000..ecf7c5d04e9c
--- /dev/null
+++ b/tools/test/stress2/misc/softupdate.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Like the symlink.sh test, this test shows the problem with Soft Update and
+# incorrect statfs(2).
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+RUNTIME=2m
+runRUNTIME=2m
+RUNDIR=$mntpoint/stressX
+
+D=$diskimage
+dd if=/dev/zero of=$D bs=1m count=512 status=none || exit 1
+
+mount | grep "$mntpoint" | grep md$mdstart > /dev/null && umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+
+for mode in "" "-U"; do
+ printf "newfs -O2 $mode /dev/md${mdstart}\n\n"
+ newfs -O2 $mode /dev/md$mdstart > /dev/null 2>&1
+ mount /dev/md$mdstart $mntpoint
+
+ for i in `jot 5`; do
+ (cd ..;./run.sh disk.cfg)
+ done
+
+ umount -f $mntpoint
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/sort.sh b/tools/test/stress2/misc/sort.sh
new file mode 100755
index 000000000000..4807f05b1701
--- /dev/null
+++ b/tools/test/stress2/misc/sort.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# VM stress.
+
+# "panic: freeing free block: ffff40, size 8, mask 1" seen in WiP code:
+# https://people.freebsd.org/~pho/stress/log/sort.txt
+
+[ `sysctl -n vm.swap_total` -eq 0 ] && exit 0
+
+for i in `jot 6`; do
+ sort /dev/zero &
+ pids="$pids $!"
+done
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 180 ]; do
+ sleep 10
+ pgrep -q sort || break
+done
+while pgrep -q sort; do
+ kill -9 $pids > /dev/null 2>&1
+ sleep 1
+done
+wait
+exit 0
diff --git a/tools/test/stress2/misc/sort2.sh b/tools/test/stress2/misc/sort2.sh
new file mode 100755
index 000000000000..ba3d5a0a01f8
--- /dev/null
+++ b/tools/test/stress2/misc/sort2.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# VM stress. Variation of sort.sh
+
+[ `sysctl -n vm.swap_total` -eq 0 ] && exit 0
+
+for i in `jot 32`; do
+ sort /dev/zero &
+ pids="$pids $!"
+done
+pct=-1
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 300 ]; do
+ sleep 10
+ p=`swapinfo | tail -1 | awk '{sub("%","");print $NF}'`
+ [ $p -gt $pct ] && pct=$p
+ pgrep -q sort || break
+done
+echo "Max swap used is ${pct}%"
+while pgrep -q sort; do
+ kill -9 $pids > /dev/null 2>&1
+ sleep 1
+done
+wait
+exit 0
diff --git a/tools/test/stress2/misc/sparse.sh b/tools/test/stress2/misc/sparse.sh
new file mode 100755
index 000000000000..1ae95de12558
--- /dev/null
+++ b/tools/test/stress2/misc/sparse.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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.
+#
+
+# Test sparse file read
+# No problems seen.
+
+. ../default.cfg
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 5g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+set +e
+
+in=$mntpoint/in
+out=$mntpoint/out
+
+size=`jot -r 1 1 2048`
+size=$((size * 1024 * 1024))
+truncate -s $size $in
+
+for i in `jot $(jot -r 1 10 100)`; do
+ bs=`jot -r 1 1 4096`
+ pos=`jot -r 1 0 $((size - bs))`
+ pos=$((pos / bs)) # in blocks
+ [ $((pos + bs)) -gt $size ] && { echo "seek error"; exit 1; }
+ dd if=/dev/random of=$in seek=$pos bs=$bs count=1 conv=notrunc \
+ status=none
+done
+[ `stat -f%z $in` -ne $size ] && { ls -l $in; exit 1; }
+
+cp $in $out
+md1=`md5 < $in`
+md2=`md5 < $out`
+[ $md1 == $md2 ] && s=0 ||
+ { echo "md5 differs: $in: $md1, $out: $md2"; s=1
+ ls -l $in $out; }
+rm -f $in $out
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/spin.sh b/tools/test/stress2/misc/spin.sh
new file mode 100755
index 000000000000..0128e3ff3529
--- /dev/null
+++ b/tools/test/stress2/misc/spin.sh
@@ -0,0 +1,124 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Demonstrate starvation: Thread stuck in "ufs" for minutes.
+# Only seen with >= 16 CPUs.
+# Not a problem with 4BSD.
+# http://people.freebsd.org/~pho/stress/log/spin.txt
+# Fixed by r273966.
+
+. ../default.cfg
+
+timeout=1200
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > spin.c
+mycc -o spin -Wall -Wextra -O0 spin.c || exit 1
+rm -f spin.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags -n md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+cpus=`sysctl hw.ncpu | sed 's/.*: //'`
+(cd $mntpoint; /tmp/spin $((cpus + 1))) &
+error=0
+n=0
+while kill -0 $! 2>/dev/null; do
+ sleep 1
+ if [ $((n += 1)) -gt $timeout ]; then
+ echo FAIL
+ ps -l | grep -v sed | sed -n '1p;/ufs/p'
+ pkill spin
+ error=1
+ fi
+done
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/spin
+exit $error
+EOF
+#include <sys/param.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+void
+work(void)
+{
+
+ while (access("rendezvous", R_OK) != 0)
+ ;
+
+ _exit(0);
+}
+
+int
+main(int argc, char **argv)
+{
+ int fd, i, parallel;
+
+ if (argc == 2)
+ parallel = atoi(argv[1]);
+ else
+ errx(1, "Usage: %s <cpus>", argv[0]);
+
+ for (i = 0; i < parallel; i++) {
+ if (fork() == 0)
+ work();
+ }
+
+ /* open(2) blocked on "ufs" for minutes */
+ if ((fd = open("rendezvous", O_CREAT, 0644)) == -1)
+ err(1, "open()");
+ close(fd);
+
+ for (i = 0; i < parallel; i++)
+ wait(NULL);
+
+ if (unlink("rendezvous") == -1)
+ err(1, "unlink()");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/split.sh b/tools/test/stress2/misc/split.sh
new file mode 100755
index 000000000000..59e3c6f140e1
--- /dev/null
+++ b/tools/test/stress2/misc/split.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# No problems seen
+
+# fsync: giving up on dirty 0xc9659438: tag devfs, type VCHR
+# usecount 1, writecount 0, refcount 41 mountedhere 0xc96a2000
+# flags (VI_ACTIVE)
+# v_object 0xc9824aa0 ref 0 pages 355 cleanbuf 37 dirtybuf 2
+# lock type devfs: EXCL by thread 0xccbca6a0 (pid 42225, dd, tid 100234)
+# #0 0xc0c26533 at __lockmgr_args+0xae3
+# #1 0xc0d06cc3 at vop_stdlock+0x53
+# #2 0xc1212cc0 at VOP_LOCK1_APV+0xe0
+# #3 0xc0d2b24a at _vn_lock+0xba
+# #4 0xc0f3f22b at ffs_sync+0x34b
+# #5 0xc0f22343 at softdep_ast_cleanup_proc+0x213
+# #6 0xc0ca7fbb at userret+0x1ab
+# #7 0xc11e0618 at syscall+0x5e8
+# #8 0xc11cb09e at Xint0x80_syscall+0x2e
+# dev label/tmp
+
+. ../default.cfg
+
+[ -d $RUNDIR ] || mkdir -p $RUNDIR &&
+ find $RUNDIR -name "split.*" -delete
+export LANG=C
+s=0
+kfree=`df -k $RUNDIR | tail -1 | awk '{print $4}'`
+[ $kfree -gt 100 ] && kfree=$((kfree - 100))
+parallel=$((`sysctl -n hw.ncpu` + 1))
+parallel=2
+cap=$((9 * 1024 * 1024)) # 9GB
+mx=$((kfree / parallel / 2))
+[ $mx -gt $cap ] && mx=$cap
+
+run() {
+ local blocks md5 md5a orig s spmax spmin
+
+ s=0
+ cd $RUNDIR
+ blocks=`jot -r 1 1 $mx`
+ dd if=/dev/random of=file.$1 bs=1023 count=$blocks status=none
+ orig=`ls -l file.$1 | tail -1`
+ md5=`md5 -q file.$1`
+ spmin=$((1023 * 4))
+ spmax=$((1023 * blocks / 2))
+ rm -f split.$1.*
+ split -a 4 -b `jot -r 1 $spmin $spmax` file.$1 split.$1.
+ rm file.$1
+ cat split.$1.* > file.$1
+ rm split.$1.*
+ md5a=`md5 -q file.$1`
+ if [ $md5 != $md5a ]; then
+ echo FAIL
+ echo "orig $orig $md5"
+ orig=`ls -l file.$1 | tail -1`
+ echo "new $orig $md5a"
+ s=1
+ fi
+ rm file.$1
+ return $s
+
+}
+
+for i in `jot $parallel`; do
+ run $i &
+ pids="$pids $!"
+done
+for pid in $pids; do
+ wait $pid
+ [ $? -ne 0 ] && s=1
+done
+exit $s
diff --git a/tools/test/stress2/misc/stack_guard_page.sh b/tools/test/stress2/misc/stack_guard_page.sh
new file mode 100755
index 000000000000..db437870c25e
--- /dev/null
+++ b/tools/test/stress2/misc/stack_guard_page.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Test with stack_guard_page set between 1 and 512.
+# A negative value is considered invalid.
+
+[ `sysctl -n security.bsd.stack_guard_page` -eq 0 ] && exit 0
+
+old=`sysctl -n security.bsd.stack_guard_page`
+trap "sysctl security.bsd.stack_guard_page=$old" EXIT INT
+
+../misc/marcus.sh > /dev/null 2>&1 &
+
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 60 ]; do
+ sysctl security.bsd.stack_guard_page=`jot -r 1 1 512` > \
+ /dev/null 2>&1
+ sleep 1
+done
+kill $!
+../tools/killall.sh
+wait
+./cleanup.sh
+
+exit 0
diff --git a/tools/test/stress2/misc/statfs.sh b/tools/test/stress2/misc/statfs.sh
new file mode 100755
index 000000000000..d0ab99e0c253
--- /dev/null
+++ b/tools/test/stress2/misc/statfs.sh
@@ -0,0 +1,126 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Regression test for statfs problems with deleting a large number of files
+
+# $ ./statfs.sh
+# Filesystem 1K-blocks Used Avail Capacity iused ifree %iused Mounted on
+# /dev/ad0s1e 1982798 1782134 42042 98% 4965 254105 2% /tmp
+# Free inodes on /tmp: 254105
+# Creating 100000 files...
+# Deleting 100000 files...
+# Filesystem 1K-blocks Used Avail Capacity iused ifree %iused Mounted on
+# /dev/ad0s1e 1982798 -284096 2108272 -16% 4965 254105 2% /tmp
+# $ umount -f /tmp; mount /tmp
+# $ df -i /tmp
+# Filesystem 1K-blocks Used Avail Capacity iused ifree %iused Mounted on
+# /dev/ad0s1e 1982798 1784528 39648 98% 4965 254105 2% /tmp
+
+. ../default.cfg
+
+odir=`pwd`
+dir=/tmp
+
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/statfs.c
+mycc -o statfs -Wall statfs.c
+rm -f statfs.c
+
+df -i /tmp
+./statfs
+df -i /tmp
+
+exit
+EOF
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/param.h>
+#include <sys/mount.h>
+#include <errno.h>
+#include <err.h>
+
+int64_t
+inodes(void)
+{
+ char path[MAXPATHLEN+1];
+ struct statfs buf;
+
+ sync();
+ if (getcwd(path, sizeof(path)) == NULL)
+ err(1, "getcwd()");
+
+ if (statfs(path, &buf) < 0)
+ err(1, "statfs(%s)", path);
+ printf("Free inodes on %s: %jd\n", path, buf.f_ffree);
+ return (buf.f_ffree);
+}
+
+int
+main()
+{
+ int fd, i, j;
+ int64_t size;
+ pid_t pid;
+ char file[128];
+
+ size = inodes() - 1000;
+
+ if (size > 100000)
+ size = 100000;
+
+ printf("Creating %jd files...", size); fflush(stdout);
+ pid = getpid();
+ for (j = 0; j < size; j++) {
+ sprintf(file,"p%06d.%05d", pid, j);
+ if ((fd = open(file, O_CREAT | O_TRUNC | O_WRONLY, 0666)) == -1) {
+ if (errno != EINTR) {
+ warn("creat(%s)", file);
+ printf("break out at %d, errno %d\n", j, errno);
+ break;
+ }
+ }
+ if (fd != -1 && close(fd) == -1)
+ err(2, "close(%d)", j);
+
+ }
+
+ printf("\nDeleting %jd files...", size); fflush(stdout);
+ for (i = --j; i >= 0; i--) {
+ sprintf(file,"p%06d.%05d", pid, i);
+ if (unlink(file) == -1)
+ err(3, "unlink(%s)", file);
+
+ }
+ printf("\n");
+ return (0);
+}
diff --git a/tools/test/stress2/misc/stealer.sh b/tools/test/stress2/misc/stealer.sh
new file mode 100755
index 000000000000..e118a311e006
--- /dev/null
+++ b/tools/test/stress2/misc/stealer.sh
@@ -0,0 +1,133 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Process stuck in objtrm wait state
+# http://people.freebsd.org/~pho/stress/log/stealer.txt
+# Fixed in r263328.
+
+# "panic: freeing free block" seen:
+# https://people.freebsd.org/~pho/stress/log/stealer-2.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > stealer.c
+mycc -o stealer -Wall -Wextra stealer.c || exit 1
+rm -f stealer.c
+cd $here
+swapoff -a > /dev/null
+
+dd if=/dev/zero of=$diskimage bs=1m count=1k status=none
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t vnode -f $diskimage -u $mdstart
+swapon /dev/md$mdstart
+
+hw=`sysctl hw.pagesize | sed 's/.*: //'`
+pages=`sysctl hw.usermem | sed 's/.*: //'`
+pages=$((pages / hw))
+echo "`date '+%T'` Test with $pages pages."
+su $testuser -c "sh -c \"/tmp/stealer $pages\"" &
+sleep 30
+while swapinfo | grep -q /dev/md$mdstart; do
+ swapoff /dev/md$mdstart 2>&1 |
+ grep -v "Cannot allocate memory"
+ sleep 2
+done
+ps auxwwl | grep -v grep | grep objtrm && echo FAIL
+wait
+
+swapon -a > /dev/null
+
+mdconfig -d -u $mdstart
+rm -rf /tmp/stealer $diskimage
+exit 0
+EOF
+#include <err.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#define N 200
+
+void
+handler(int i __unused)
+{
+ _exit(0);
+}
+
+void
+stealer(int pages)
+{
+ volatile char *c;
+ int i, page, size;
+
+ page = getpagesize();
+ size = pages * page;
+ if ((c = malloc(size)) == 0)
+ err(1, "malloc(%d pages)", pages);
+
+ signal(SIGALRM, handler);
+ alarm(3 * 60);
+ for (;;) {
+ i = 0;
+ while (i < size) {
+ c[i] = 0;
+ i += page;
+ }
+ }
+}
+
+int
+main(int argc __unused, char **argv)
+{
+ int i, j, n, pages, status;
+
+ pages = atoi(argv[1]);
+ n = pages / N;
+
+ j = 0;
+ for ( i = 0; i < N; i++, j++) {
+ if (fork() == 0)
+ stealer(n);
+ pages = pages - n;
+ }
+ if (pages > 0) {
+ j++;
+ if (fork() == 0)
+ stealer(pages);
+ }
+ while (j-- > 0)
+ if (wait(&status) == -1)
+ err(1, "wait()");
+ return (0);
+}
diff --git a/tools/test/stress2/misc/su.sh b/tools/test/stress2/misc/su.sh
new file mode 100755
index 000000000000..da29146c3ab6
--- /dev/null
+++ b/tools/test/stress2/misc/su.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# su(1) Test inspired by Andriy Gapon for Bug 222027 - panic on non-zero
+# RACCT destroy
+
+# M_SIGIO leaks seen.
+
+# "spin lock (turnstile lock) held by (tid 103097) too long" seen in WiP
+# kernel code.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+(cd ../testcases/swap; ./swap -t 5m -i 5 -v -l 100) > /dev/null &
+sleep .5
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 300 ]; do
+ pids=""
+ for i in `jot 5`; do
+ for i in `jot 500`; do
+ exec su -c xuser -m root -c ':' &
+ done &
+ pids="$pids $!"
+ done
+ wait $pids
+ while [ `pgrep su | wc -l` -gt 100 ]; do sleep 1; done
+done
+wait
+exit 0
diff --git a/tools/test/stress2/misc/suj.sh b/tools/test/stress2/misc/suj.sh
new file mode 100755
index 000000000000..5beb93ec7388
--- /dev/null
+++ b/tools/test/stress2/misc/suj.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# SUJ suspfs deadlock seen
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -j md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=20m
+su $testuser -c "cd ..; ./run.sh rw.cfg"
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/md${mdstart}$part; s=$?
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/suj10.sh b/tools/test/stress2/misc/suj10.sh
new file mode 100755
index 000000000000..dae7e36c3561
--- /dev/null
+++ b/tools/test/stress2/misc/suj10.sh
@@ -0,0 +1,180 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Page fault in softdep_revert_mkdir+0x4d seen and
+
+# fsck updates "clean" FS:
+# *** /tmp/dumpfs.1 2010-12-24 19:18:44.000000000 +0100
+# --- /tmp/dumpfs.2 2010-12-24 19:18:46.000000000 +0100
+# ***************
+# *** 5,11 ****
+# frag 8 shift 3 fsbtodb 2
+# minfree 8% optim time symlinklen 120
+# maxbsize 16384 maxbpg 2048 maxcontig 8 contigsumsize 8
+# ! nbfree 62794 ndir 2 nifree 141307 nffree 25
+# bpg 11761 fpg 94088 ipg 23552 unrefs 0
+# nindir 2048 inopb 64 maxfilesize 140806241583103
+# sbsize 2048 cgsize 16384 csaddr 3000 cssize 2048
+# --- 5,11 ----
+# frag 8 shift 3 fsbtodb 2
+# minfree 8% optim time symlinklen 120
+# maxbsize 16384 maxbpg 2048 maxcontig 8 contigsumsize 8
+# ! nbfree 62794 ndir 30 nifree 141307 nffree 25
+# bpg 11761 fpg 94088 ipg 23552 unrefs 0
+# nindir 2048 inopb 64 maxfilesize 140806241583103
+# sbsize 2048 cgsize 16384 csaddr 3000 cssize 2048
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > suj10.c
+mycc -o suj10 -Wall -Wextra -O2 suj10.c || exit 1
+rm -f suj10.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+newfs -j md$mdstart > /dev/null
+mount /dev/md$mdstart $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c "cd $mntpoint; /tmp/suj10"
+
+while mount | grep "$mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+dumpfs /dev/md$mdstart | grep -vE "UFS2|last.*time" > /tmp/dumpfs.1
+sleep 1
+fsck -t ufs -y -v /dev/md$mdstart > /tmp/fsck.log 2>&1
+dumpfs /dev/md$mdstart | grep -vE "UFS2|last.*time" > /tmp/dumpfs.2
+diff -c /tmp/dumpfs.1 /tmp/dumpfs.2 || { s=1; cat /tmp/fsck.log; } && s=0
+mdconfig -d -u $mdstart
+rm -f /tmp/fsck.log /tmp/dumpfs.? /tmp/suj10
+exit $s
+EOF
+#include <sys/param.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define PARALLEL 10
+/*
+static int size = 14100;
+Causes:
+ Fatal trap 12: page fault while in kernel mode
+ Stopped at softdep_revert_mkdir+0x4d: movl 0x28(%ebx),%eax
+*/
+static int size = 13000;
+
+static void
+test(void)
+{
+ pid_t pid;
+ int fd, i, j;
+ char file[128];
+
+ for (;;) {
+ if (access("rendezvous", R_OK) == 0)
+ break;
+ sched_yield();
+ }
+ pid = getpid();
+ sprintf(file,"d%05d", pid);
+ if (mkdir(file, 0740) == -1)
+ err(1, "mkdir(%s)", file);
+ chdir(file);
+ for (j = 0; j < size; j++) {
+ sprintf(file,"p%05d.%05d", pid, j);
+ if ((fd = mkdir(file, 0740)) == -1) {
+ if (errno != EINTR) {
+ warn("mkdir(%s). %s:%d", file, __FILE__, __LINE__);
+ unlink("continue");
+ break;
+ }
+ }
+
+ }
+ sleep(3);
+
+ for (i = --j; i >= 0; i--) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if (rmdir(file) == -1)
+ err(3, "unlink(%s)", file);
+
+ }
+ chdir("..");
+ sprintf(file,"d%05d", pid);
+ if (rmdir(file) == -1)
+ err(3, "unlink(%s)", file);
+}
+
+int
+main(void)
+{
+ int fd, i, j;
+
+ umask(0);
+ if ((fd = open("continue", O_CREAT, 0644)) == -1)
+ err(1, "open()");
+ close(fd);
+ for (i = 0; i < 1; i++) {
+ for (j = 0; j < PARALLEL; j++) {
+ if (fork() == 0) {
+ test();
+ exit(0);
+ }
+ }
+
+ if ((fd = open("rendezvous", O_CREAT, 0644)) == -1)
+ err(1, "open()");
+ close(fd);
+
+ for (j = 0; j < PARALLEL; j++)
+ wait(NULL);
+
+ unlink("rendezvous");
+ if (access("continue", R_OK) == -1) {
+ fprintf(stderr, "Loop #%d\n", i + 1); fflush(stderr);
+ break;
+ }
+ }
+ unlink("continue");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/suj11.sh b/tools/test/stress2/misc/suj11.sh
new file mode 100755
index 000000000000..ccb97fa8d189
--- /dev/null
+++ b/tools/test/stress2/misc/suj11.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Run with marcus.cfg on a 2g swap backed MD
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs -j md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=30m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/md${mdstart}$part; s=$?
+tunefs -j disable /dev/md${mdstart}$part
+checkfs /dev/md${mdstart}$part || s=$?
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/suj12.sh b/tools/test/stress2/misc/suj12.sh
new file mode 100755
index 000000000000..d0cbecfb1a37
--- /dev/null
+++ b/tools/test/stress2/misc/suj12.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Test with different sector size using gnop(8).
+
+# OOM seen: https://people.freebsd.org/~pho/stress/log/suj12.txt
+
+. ../default.cfg
+
+gnop load || exit 0
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+gnop status || exit 1
+gnop create -S 4k /dev/md$mdstart
+newfs -j /dev/md$mdstart.nop || exit 1
+mount /dev/md$mdstart.nop $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=20m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg'
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/md$mdstart.nop; s=$?
+gnop destroy /dev/md$mdstart.nop
+gnop unload
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/suj13.sh b/tools/test/stress2/misc/suj13.sh
new file mode 100755
index 000000000000..7a5616488f21
--- /dev/null
+++ b/tools/test/stress2/misc/suj13.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Run with marcus.cfg on a malloc backed MD with non standard sector size.
+
+# OOM seen: https://people.freebsd.org/~pho/stress/log/suj13.txt
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+size="128m"
+[ `uname -m` = "amd64" ] && size="1g"
+mdconfig -a -t malloc -o reserve -s $size -S 4096 -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs -j md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=20m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/md${mdstart}$part; s=$?
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/suj14.sh b/tools/test/stress2/misc/suj14.sh
new file mode 100755
index 000000000000..6587d4f554fd
--- /dev/null
+++ b/tools/test/stress2/misc/suj14.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "filesystem is full" scenario.
+
+# "panic: softdep_revert_mkdir: dot addref parent mismatch" seen.
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs -j md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+
+# Populate the FS with some random files
+for i in `jot 102`; do
+ mkdir $mntpoint/d$i
+ cp -a /usr/include $mntpoint/d$i
+done 2>/dev/null
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/suj15.sh b/tools/test/stress2/misc/suj15.sh
new file mode 100755
index 000000000000..011cb0bebdf9
--- /dev/null
+++ b/tools/test/stress2/misc/suj15.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# mksnap_ffs(8) test scenario inspired by Christian Gusenbauer, c47g gmx at.
+
+# Three problems seen with this scenario:
+#
+# 1) mksnap_ffs looping in ffs_sync()
+# 2) mksnap_ffs stuck in wsuspfs
+# 3) softdep_waitidle: Failed to flush worklist for ...
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs -j md${mdstart}$part > /dev/null
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=30m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh jeff.cfg > /dev/null' &
+rpid=$!
+while kill -0 $rpid 2> /dev/null; do
+ sleep 10
+ dump -0aLf /dev/null /dev/md${mdstart}$part
+done
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/suj16.sh b/tools/test/stress2/misc/suj16.sh
new file mode 100755
index 000000000000..8eaf5d678534
--- /dev/null
+++ b/tools/test/stress2/misc/suj16.sh
@@ -0,0 +1,82 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# mksnap_ffs(8) test scenario
+
+# Livelock seen in ffs_sync().
+# "panic: initiate_write_bmsafemap: block 846816 marked free" seen.
+# Zero size snapshot file seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs -j md${mdstart}$part > /dev/null 2>&1
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=30m
+export RUNDIR=$mntpoint/stressX
+export creatINCARNATIONS=2
+
+export TESTPROGS="
+testcases/creat/creat
+"
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > /dev/null 2>&1 &
+rpid=$!
+mdstart2=$((mdstart + 1))
+mntpoint2=${mntpoint}2
+[ -d $mntpoint2 ] || mkdir $mntpoint2
+mount | grep $mntpoint2 | grep -q /dev/md && umount -f $mntpoint2
+mdconfig -l | grep -q md$mdstart2 && mdconfig -d -u $mdstart2
+while kill -0 $rpid 2> /dev/null; do
+ rm -f $mntpoint/.snap/suj16
+ { mksnap_ffs $mntpoint $mntpoint/.snap/suj16 || continue; } 2>&1 | \
+ grep -v "Resource temporarily unavailable"
+ [ ! -s $mntpoint/.snap/suj16 ] && continue
+ mdconfig -a -t vnode -f $mntpoint/.snap/suj16 -u $mdstart2 -o readonly
+ mount -r /dev/md$mdstart2 $mntpoint2
+
+ ls -l $mntpoint2 > /dev/null
+
+ umount $mntpoint2
+ mdconfig -d -u $mdstart2
+done
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/suj17.sh b/tools/test/stress2/misc/suj17.sh
new file mode 100755
index 000000000000..319b61ecab8b
--- /dev/null
+++ b/tools/test/stress2/misc/suj17.sh
@@ -0,0 +1,136 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Truncate scenario for suj.
+# "panic: worklist_insert: 0xc8bc5b00 freework(0x8009) already on list"
+# seen.
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > suj17.c
+mycc -o suj17 -Wall -Wextra -O2 suj17.c
+rm -f suj17.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+[ $# -eq 1 ] && opt="$1"
+[ $# -eq 0 ] && opt="-j"
+echo "newfs $opt md${mdstart}$part"
+newfs $opt md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c "cd $mntpoint; /tmp/suj17"
+s=$?
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/md${mdstart}$part || s=$?
+mdconfig -d -u $mdstart
+rm -f /tmp/suj17
+exit $s
+EOF
+#include <fcntl.h>
+#include <err.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#define SIZ (1024 * 1024 - 1)
+char buf[SIZ];
+
+void
+test()
+{
+ int fd, i;
+ char name[128];
+ off_t len = 104857600LL; /* 100 Mb */
+ off_t pos;
+
+ sprintf(name, "%06d", getpid());
+ if ((fd = open(name, O_WRONLY | O_CREAT, 0666)) == -1)
+ err(1, "open(%s)", name);
+ for (i = 0; i < 100; i++) {
+ if (write(fd, buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write");
+ }
+
+ for (;;) {
+ if (access("rendezvous", R_OK) == 0)
+ break;
+ sched_yield();
+ }
+
+ srand48(getpid());
+ for (i = 0; i < 50000; i++) {
+ pos = lrand48() % (len - sizeof(buf));
+ if (ftruncate(fd, pos) == -1)
+ err(1, "ftruncate");
+ pos = lrand48() % (len - sizeof(buf));
+ if (lseek(fd, pos, SEEK_SET) == -1)
+ err(1, "lseek");
+ if (write(fd, buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write");
+ }
+ close(fd);
+ unlink(name);
+ _exit(0);
+}
+
+int
+main()
+{
+ int fd, i, j, status;
+
+ for (i = 0; i < 1; i++) {
+ for (j = 0; j < 6; j++) {
+ if (fork() == 0)
+ test();
+ }
+ if ((fd = open("rendezvous", O_CREAT, 0644)) == -1)
+ err(1, "open()");
+ close(fd);
+
+ for (j = 0; j < 6; j++)
+ wait(&status);
+ unlink("rendezvous");
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/suj18.sh b/tools/test/stress2/misc/suj18.sh
new file mode 100755
index 000000000000..120a8e53ab67
--- /dev/null
+++ b/tools/test/stress2/misc/suj18.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# SUJ and quota test scenario.
+# "panic: check_inode_unwritten: busy inode" seen.
+
+# Deadlock seen:
+# https://people.freebsd.org/~pho/stress/log/suj18.txt
+# Fixed by r305594.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ "`sysctl -in kern.features.ufs_quota`" != "1" ] && exit 0
+
+. ../default.cfg
+
+mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -j md${mdstart}$part > /dev/null
+export PATH_FSTAB=/tmp/fstab
+echo "/dev/md${mdstart}$part $mntpoint ufs rw,userquota 2 2" > \
+ $PATH_FSTAB
+mount $mntpoint
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export QK=$(($1 / 10 * 8))
+export QI=$(($2 / 10 * 8))
+edquota -u -f $mntpoint -e ${mntpoint}:$((QK - 50)):$QK:$((QI - 50 )):$QI \
+ $testuser
+quotaon $mntpoint
+export RUNDIR=$mntpoint/stressX
+chmod 777 $mntpoint
+su $testuser -c 'sh -c "(cd ..;runRUNTIME=20m ./run.sh disk.cfg > \
+ /dev/null 2>&1)"'
+
+rm -f $PATH_FSTAB
+for i in `jot 6`; do
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/suj19.sh b/tools/test/stress2/misc/suj19.sh
new file mode 100755
index 000000000000..580ebf131700
--- /dev/null
+++ b/tools/test/stress2/misc/suj19.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# SUJ, quota and snapshots test scenario
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ "`sysctl -in kern.features.ufs_quota`" != "1" ] && exit 0
+
+. ../default.cfg
+
+snap () {
+ for i in `jot 5`; do
+ mksnap_ffs $1 $2 2>&1 | grep -v "Resource temporarily unavailable"
+ [ ! -s $2 ] && rm -f $2 || return 0
+ sleep 1
+ done
+ return 1
+}
+
+mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -j md${mdstart}$part > /dev/null
+export PATH_FSTAB=/tmp/fstab
+echo "/dev/md${mdstart}$part $mntpoint ufs rw,userquota 2 2" > $PATH_FSTAB
+mount $mntpoint
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export QK=$(($1 / 4))
+export QI=$(($2 / 4))
+edquota -u -f $mntpoint -e ${mntpoint}:$((QK - 50)):$QK:$((QI - 50 )):$QI $testuser
+quotaon $mntpoint
+export RUNDIR=$mntpoint/stressX
+chmod 777 $mntpoint
+su $testuser -c 'sh -c "(cd ..;runRUNTIME=20m ./run.sh disk.cfg > /dev/null 2>&1)"' &
+
+for i in `jot 20`; do
+ echo "`date '+%T'` mksnap_ffs $mntpoint $mntpoint/.snap/snap$i"
+ snap $mntpoint $mntpoint/.snap/snap$i || break
+ sleep 1
+done
+i=$(($(date '+%S') % 20 + 1))
+echo "rm -f $mntpoint/.snap/snap$i"
+rm -f $mntpoint/.snap/snap$i
+wait
+
+while mount | grep -q $mntpoint; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f $PATH_FSTAB
diff --git a/tools/test/stress2/misc/suj2.sh b/tools/test/stress2/misc/suj2.sh
new file mode 100755
index 000000000000..bc00f09d3045
--- /dev/null
+++ b/tools/test/stress2/misc/suj2.sh
@@ -0,0 +1,78 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# Page fault on SUJ enabled FS
+# Fix: http://docs.freebsd.org/cgi/mid.cgi?20100823211257.GI2396
+
+# Tets scenario by Mateusz Guzik mjguzik gmail com
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > suj2.c
+mycc -o suj2 -Wall -Wextra -O2 suj2.c
+rm -f suj2.c
+
+mount | grep "$mntpoint" | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -j md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+cd $mntpoint
+
+rm -rf foo bar
+
+/tmp/suj2
+
+cd /
+while mount | grep "$mntpoint" | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -rf foo bar /tmp/suj2
+exit
+EOF
+#include <sys/stat.h>
+#include <stdio.h>
+
+int
+main(void)
+{
+
+ mkdir("foo", 00700);
+ mkdir("bar", 00700);
+
+ rename("foo", "bar");
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/suj20.sh b/tools/test/stress2/misc/suj20.sh
new file mode 100755
index 000000000000..9e73a480f333
--- /dev/null
+++ b/tools/test/stress2/misc/suj20.sh
@@ -0,0 +1,177 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# Looping mksnap_ffs seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# Scenario by mckusick@
+#
+# create a bunch of files/directories
+# create a snapshot
+# remove many (but not all) of those files/directories
+# create some new files/directories in what remains of those
+# original files/directories.
+# create another snapshot
+# repeat {
+# remove many (somewhat different) of those files/directories
+# create some new files/directories in what remains of those
+# remaining files/directories.
+# create a new snapshot
+# remove oldest snapshot
+# }
+
+snap () {
+ for i in `jot 5`; do
+ mksnap_ffs $1 $2 2>&1 | grep -v "Resource temporarily unavailable"
+ [ ! -s $2 ] && rm -f $2 || return 0
+ sleep 1
+ done
+ return 1
+}
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > suj20.c
+mycc -o suj20 -Wall -Wextra -g -O2 suj20.c
+rm -f suj20.c
+
+mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -j md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+cd $mntpoint
+chmod 777 $mntpoint
+/tmp/suj20
+snap $mntpoint $mntpoint/.snap/snap1
+/tmp/suj20 prune
+snap $mntpoint $mntpoint/.snap/snap2
+/tmp/suj20
+for i in `jot 10`; do
+ /tmp/suj20 prune
+ /tmp/suj20
+ snap $mntpoint $mntpoint/.snap/snap$((i + 2))
+ sn=`ls -tU $mntpoint/.snap | tail -1`
+ rm -f $mntpoint/.snap/$sn
+done
+cd $here
+
+while mount | grep -q $mntpoint; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/suj20
+exit 0
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+static char buf[4096];
+#define ND 100
+#define NF 100
+
+void
+setup(void)
+{
+ int d, f, fd, i, n;
+ char name[128];
+
+ for (d = 0; d < ND; d++) {
+ snprintf(name, sizeof(name), "d%03d", d);
+ if (mkdir(name, 00700) == -1 && errno != EEXIST)
+ err(1, "mkdir(%s)", name);
+ if (chdir(name) == -1)
+ err(1, "chdir(%s)", name);
+ for (f = 0; f < NF; f++) {
+ if (arc4random() % 100 < 33)
+ continue;
+ snprintf(name, sizeof(name), "f%03d", f);
+ if ((fd = open(name, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1, "open(%s)", name);
+ n = arc4random() % 10;
+ for (i = 0; i < n; i++) {
+ if (write(fd, buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write()");
+ }
+ close(fd);
+ }
+ if (chdir("..") == -1)
+ err(1, "chdir(%s)", "..");
+ }
+}
+void
+
+prune(void)
+{
+ int d, f;
+ char name[128];
+
+ for (d = 0; d < ND; d++) {
+ snprintf(name, sizeof(name), "d%03d", d);
+ if (chdir(name) == -1)
+ err(1, "chdir(%s)", name);
+ for (f = 0; f < NF; f++) {
+ if (arc4random() % 100 < 33)
+ continue;
+ snprintf(name, sizeof(name), "f%03d", f);
+ if (unlink(name) == -1 && errno != ENOENT)
+ err(1, "unlink(%s)", name);
+ }
+ if (chdir("..") == -1)
+ err(1, "chdir(%s)", "..");
+ }
+ for (d = 0; d < ND; d++) {
+ if (arc4random() % 100 > 10)
+ continue;
+ snprintf(name, sizeof(name), "rm -rf d%03d", d);
+ system(name);
+ }
+}
+
+int
+main(int argc, char **argv __unused)
+{
+ if (argc == 1)
+ setup();
+ if (argc == 2)
+ prune();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/suj21.sh b/tools/test/stress2/misc/suj21.sh
new file mode 100755
index 000000000000..cbf2d4c730ab
--- /dev/null
+++ b/tools/test/stress2/misc/suj21.sh
@@ -0,0 +1,178 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# Copy of suj20.sh, but with test user as non root.
+# "panic: handle_disk_write_complete: Unknown type freedep" seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# Scenario by mckusick@
+#
+# create a bunch of files/directories
+# create a snapshot
+# remove many (but not all) of those files/directories
+# create some new files/directories in what remains of those
+# original files/directories.
+# create another snapshot
+# repeat {
+# remove many (somewhat different) of those files/directories
+# create some new files/directories in what remains of those
+# remaining files/directories.
+# create a new snapshot
+# remove oldest snapshot
+# }
+
+snap () {
+ for i in `jot 5`; do
+ mksnap_ffs $1 $2 2>&1 | grep -v "Resource temporarily unavailable"
+ [ ! -s $2 ] && rm -f $2 || return 0
+ sleep 1
+ done
+ return 1
+}
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > suj21.c
+mycc -o suj21 -Wall -Wextra -g -O2 suj21.c
+rm -f suj21.c
+
+mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -j md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+cd $mntpoint
+chmod 777 $mntpoint
+su $testuser -c '/tmp/suj21'
+snap $mntpoint $mntpoint/.snap/snap1
+su $testuser -c '/tmp/suj21 prune'
+snap $mntpoint $mntpoint/.snap/snap2
+su $testuser -c '/tmp/suj21'
+for i in `jot 10`; do
+ su $testuser -c '/tmp/suj21 prune'
+ su $testuser -c '/tmp/suj21'
+ snap $mntpoint $mntpoint/.snap/snap$((i + 2))
+ sn=`ls -tU $mntpoint/.snap | tail -1`
+ rm -f $mntpoint/.snap/$sn
+done
+cd $here
+
+while mount | grep -q $mntpoint; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/suj21
+exit 0
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+static char buf[4096];
+#define ND 100
+#define NF 100
+
+void
+setup(void)
+{
+ int d, f, fd, i, n;
+ char name[128];
+
+ for (d = 0; d < ND; d++) {
+ snprintf(name, sizeof(name), "d%03d", d);
+ if (mkdir(name, 00700) == -1 && errno != EEXIST)
+ err(1, "mkdir(%s)", name);
+ if (chdir(name) == -1)
+ err(1, "chdir(%s)", name);
+ for (f = 0; f < NF; f++) {
+ if (arc4random() % 100 < 33)
+ continue;
+ snprintf(name, sizeof(name), "f%03d", f);
+ if ((fd = open(name, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1, "open(%s)", name);
+ n = arc4random() % 10;
+ for (i = 0; i < n; i++) {
+ if (write(fd, buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write()");
+ }
+ close(fd);
+ }
+ if (chdir("..") == -1)
+ err(1, "chdir(%s)", "..");
+ }
+}
+void
+
+prune(void)
+{
+ int d, f;
+ char name[128];
+
+ for (d = 0; d < ND; d++) {
+ snprintf(name, sizeof(name), "d%03d", d);
+ if (chdir(name) == -1)
+ err(1, "chdir(%s)", name);
+ for (f = 0; f < NF; f++) {
+ if (arc4random() % 100 < 33)
+ continue;
+ snprintf(name, sizeof(name), "f%03d", f);
+ if (unlink(name) == -1 && errno != ENOENT)
+ err(1, "unlink(%s)", name);
+ }
+ if (chdir("..") == -1)
+ err(1, "chdir(%s)", "..");
+ }
+ for (d = 0; d < ND; d++) {
+ if (arc4random() % 100 > 10)
+ continue;
+ snprintf(name, sizeof(name), "rm -rf d%03d", d);
+ system(name);
+ }
+}
+
+int
+main(int argc, char **argv __unused)
+{
+ if (argc == 1)
+ setup();
+ if (argc == 2)
+ prune();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/suj22.sh b/tools/test/stress2/misc/suj22.sh
new file mode 100755
index 000000000000..beff053378c1
--- /dev/null
+++ b/tools/test/stress2/misc/suj22.sh
@@ -0,0 +1,195 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# Variation of suj20.sh, with focus on the FS state after a panic / power
+# cycle.
+
+# Demonstrate "multiple references to blocks" in FS after reboot from a
+# looping mksnap_ffs(8).
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+# Scenario by mckusick@
+#
+# create a bunch of files/directories
+# create a snapshot
+# remove many (but not all) of those files/directories
+# create some new files/directories in what remains of those
+# original files/directories.
+# create another snapshot
+# repeat {
+# remove many (somewhat different) of those files/directories
+# create some new files/directories in what remains of those
+# remaining files/directories.
+# create a new snapshot
+# remove oldest snapshot
+# }
+
+snap () {
+ for i in `jot 5`; do
+ mksnap_ffs $1 $2 2>&1 | grep -v "Resource temporarily unavailable"
+ [ ! -s $2 ] && rm -f $2 || return 0
+ sleep 1
+ done
+ return 1
+}
+
+D=$diskimage
+
+if [ -n "`find $D -mtime -1h 2>/dev/null`" ]; then
+ # FS left by previous crash
+ mdconfig -a -t vnode -f $D -u $mdstart
+ fsck -t ufs -y md${mdstart}$part
+ fsck -t ufs -y md${mdstart}$part
+ rm -f $D
+ exit 0
+fi
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > suj22.c
+mycc -o suj22 -Wall -Wextra -O2 suj22.c
+rm -f suj22.c
+
+mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+dd if=/dev/zero of=$D bs=1m count=1k status=none || exit 1
+mdconfig -a -t vnode -f $D -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -j md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+cd $mntpoint
+chmod 777 $mntpoint
+/tmp/suj22
+snap $mntpoint $mntpoint/.snap/snap1
+/tmp/suj22 prune
+snap $mntpoint $mntpoint/.snap/snap2
+/tmp/suj22
+for i in `jot 10`; do
+ /tmp/suj22 prune
+ /tmp/suj22
+ snap $mntpoint $mntpoint/.snap/snap$((i + 2)) || break
+ sn=`ls -tU $mntpoint/.snap | tail -1`
+ rm -f $mntpoint/.snap/$sn
+done
+cd $here
+
+while mount | grep -q $mntpoint; do
+ umount $mntpoint || sleep 1
+done
+fsck -t ufs -y md${mdstart}$part
+fsck -t ufs -y md${mdstart}$part
+mdconfig -d -u $mdstart
+rm -f /tmp/suj22 $D
+exit 0
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+static char buf[4096];
+#define ND 100
+#define NF 100
+
+void
+setup(void)
+{
+ int d, f, fd, i, n;
+ char name[128];
+
+ for (d = 0; d < ND; d++) {
+ snprintf(name, sizeof(name), "d%03d", d);
+ if (mkdir(name, 00700) == -1 && errno != EEXIST)
+ err(1, "mkdir(%s)", name);
+ if (chdir(name) == -1)
+ err(1, "chdir(%s)", name);
+ for (f = 0; f < NF; f++) {
+ if (arc4random() % 100 < 33)
+ continue;
+ snprintf(name, sizeof(name), "f%03d", f);
+ if ((fd = open(name, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1, "open(%s)", name);
+ n = arc4random() % 10;
+ for (i = 0; i < n; i++) {
+ if (write(fd, buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write()");
+ }
+ close(fd);
+ }
+ if (chdir("..") == -1)
+ err(1, "chdir(%s)", "..");
+ }
+}
+void
+
+prune(void)
+{
+ int d, f;
+ char name[128];
+
+ for (d = 0; d < ND; d++) {
+ snprintf(name, sizeof(name), "d%03d", d);
+ if (chdir(name) == -1)
+ err(1, "chdir(%s)", name);
+ for (f = 0; f < NF; f++) {
+ if (arc4random() % 100 < 33)
+ continue;
+ snprintf(name, sizeof(name), "f%03d", f);
+ if (unlink(name) == -1 && errno != ENOENT)
+ err(1, "unlink(%s)", name);
+ }
+ if (chdir("..") == -1)
+ err(1, "chdir(%s)", "..");
+ }
+ for (d = 0; d < ND; d++) {
+ if (arc4random() % 100 > 10)
+ continue;
+ snprintf(name, sizeof(name), "rm -rf d%03d", d);
+ system(name);
+ }
+}
+
+int
+main(int argc, char **argv __unused)
+{
+ if (argc == 1)
+ setup();
+ if (argc == 2)
+ prune();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/suj23.sh b/tools/test/stress2/misc/suj23.sh
new file mode 100755
index 000000000000..aca810d42485
--- /dev/null
+++ b/tools/test/stress2/misc/suj23.sh
@@ -0,0 +1,440 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# Scenario from kern/159971
+# bstg0003.c by Kirk Russell <kirk ba23 org>
+
+# panic: ino 0xc84c9b00(0x3C8209) 65554, 32780 != 65570
+# https://people.freebsd.org/~pho/stress/log/suj23.txt
+
+# panic: first_unlinked_inodedep: prev != next. inodedep = 0xcadf9e00
+# https://people.freebsd.org/~pho/stress/log/jeff091.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > suj23.c
+mycc -o suj23 -Wall -Wextra -O2 suj23.c
+rm -f suj23.c
+
+mount | grep "on $mntpoint " | grep -q md$mdstart && umount $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -j md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c '/tmp/suj23'
+
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/suj23
+exit 0
+EOF
+/*
+ * Copyright 2011 Kirk J. Russell
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <unistd.h>
+#include <assert.h>
+#include <err.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
+#include <sys/wait.h>
+
+#define RUNTIME 600
+
+static char *bstg_pathstore[] = {
+ "/mnt/111/z",
+ "/mnt/111/aaaa",
+ "/mnt/111/bbbbb",
+ "/mnt/111/ccccc",
+ "/mnt/111/d",
+ "/mnt/111/e",
+ "/mnt/111/ffffff.fff.f",
+ "/mnt/111/gggggggggggg",
+ "/mnt/111/hhhh",
+ "/mnt/111/iiiii.ii",
+ "/mnt/111/jjjj.jj.jjjjjjjj",
+ "/mnt/111/kkkk.kkkkkkkk",
+ "/mnt/111/lllll",
+ "/mnt/222/z",
+ "/mnt/222/aaaa",
+ "/mnt/222/bbbbb",
+ "/mnt/222/ccccc",
+ "/mnt/222/d",
+ "/mnt/222/e",
+ "/mnt/222/ffffff.fff.f",
+ "/mnt/222/gggggggggggg",
+ "/mnt/222/hhhh",
+ "/mnt/222/iiiii.ii",
+ "/mnt/222/jjjj.jj.jjjjjjjj",
+ "/mnt/222/kkkk.kkkkkkkk",
+ "/mnt/222/lllll",
+ "/mnt/333/z",
+ "/mnt/333/aaaa",
+ "/mnt/333/bbbbb",
+ "/mnt/333/ccccc",
+ "/mnt/333/d",
+ "/mnt/333/e",
+ "/mnt/333/ffffff.fff.f",
+ "/mnt/333/gggggggggggg",
+ "/mnt/333/hhhh",
+ "/mnt/333/iiiii.ii",
+ "/mnt/333/jjjj.jj.jjjjjjjj",
+ "/mnt/333/kkkk.kkkkkkkk",
+ "/mnt/333/lllll",
+ "/mnt/444/z",
+ "/mnt/444/aaaa",
+ "/mnt/444/bbbbb",
+ "/mnt/444/ccccc",
+ "/mnt/444/d",
+ "/mnt/444/e",
+ "/mnt/444/ffffff.fff.f",
+ "/mnt/444/gggggggggggg",
+ "/mnt/444/hhhh",
+ "/mnt/444/iiiii.ii",
+ "/mnt/444/jjjj.jj.jjjjjjjj",
+ "/mnt/444/kkkk.kkkkkkkk",
+ "/mnt/444/lllll",
+ "/mnt/555/z",
+ "/mnt/555/aaaa",
+ "/mnt/555/bbbbb",
+ "/mnt/555/ccccc",
+ "/mnt/555/d",
+ "/mnt/555/e",
+ "/mnt/555/ffffff.fff.f",
+ "/mnt/555/gggggggggggg",
+ "/mnt/555/hhhh",
+ "/mnt/555/iiiii.ii",
+ "/mnt/555/jjjj.jj.jjjjjjjj",
+ "/mnt/555/kkkk.kkkkkkkk",
+ "/mnt/555/lllll",
+ "/mnt/666/z",
+ "/mnt/666/aaaa",
+ "/mnt/666/bbbbb",
+ "/mnt/666/ccccc",
+ "/mnt/666/d",
+ "/mnt/666/e",
+ "/mnt/666/ffffff.fff.f",
+ "/mnt/666/gggggggggggg",
+ "/mnt/666/hhhh",
+ "/mnt/666/iiiii.ii",
+ "/mnt/666/jjjj.jj.jjjjjjjj",
+ "/mnt/666/kkkk.kkkkkkkk",
+ "/mnt/666/lllll",
+ "/mnt/777/z",
+ "/mnt/777/aaaa",
+ "/mnt/777/bbbbb",
+ "/mnt/777/ccccc",
+ "/mnt/777/d",
+ "/mnt/777/e",
+ "/mnt/777/ffffff.fff.f",
+ "/mnt/777/gggggggggggg",
+ "/mnt/777/hhhh",
+ "/mnt/777/iiiii.ii",
+ "/mnt/777/jjjj.jj.jjjjjjjj",
+ "/mnt/777/kkkk.kkkkkkkk",
+ "/mnt/777/lllll",
+ "/mnt/888/z",
+ "/mnt/888/aaaa",
+ "/mnt/888/bbbbb",
+ "/mnt/888/ccccc",
+ "/mnt/888/d",
+ "/mnt/888/e",
+ "/mnt/888/ffffff.fff.f",
+ "/mnt/888/gggggggggggg",
+ "/mnt/888/hhhh",
+ "/mnt/888/iiiii.ii",
+ "/mnt/888/jjjj.jj.jjjjjjjj",
+ "/mnt/888/kkkk.kkkkkkkk",
+ "/mnt/888/lllll",
+ "/mnt/999/z",
+ "/mnt/999/aaaa",
+ "/mnt/999/bbbbb",
+ "/mnt/999/ccccc",
+ "/mnt/999/d",
+ "/mnt/999/e",
+ "/mnt/999/ffffff.fff.f",
+ "/mnt/999/gggggggggggg",
+ "/mnt/999/hhhh",
+ "/mnt/999/iiiii.ii",
+ "/mnt/999/jjjj.jj.jjjjjjjj",
+ "/mnt/999/kkkk.kkkkkkkk",
+ "/mnt/999/lllll",
+ "/mnt/aaa/z",
+ "/mnt/aaa/aaaa",
+ "/mnt/aaa/bbbbb",
+ "/mnt/aaa/ccccc",
+ "/mnt/aaa/d",
+ "/mnt/aaa/e",
+ "/mnt/aaa/ffffff.fff.f",
+ "/mnt/aaa/gggggggggggg",
+ "/mnt/aaa/hhhh",
+ "/mnt/aaa/iiiii.ii",
+ "/mnt/aaa/jjjj.jj.jjjjjjjj",
+ "/mnt/aaa/kkkk.kkkkkkkk",
+ "/mnt/aaa/lllll",
+ "/mnt/bbb/z",
+ "/mnt/bbb/aaaa",
+ "/mnt/bbb/bbbbb",
+ "/mnt/bbb/ccccc",
+ "/mnt/bbb/d",
+ "/mnt/bbb/e",
+ "/mnt/bbb/ffffff.fff.f",
+ "/mnt/bbb/gggggggggggg",
+ "/mnt/bbb/hhhh",
+ "/mnt/bbb/iiiii.ii",
+ "/mnt/bbb/jjjj.jj.jjjjjjjj",
+ "/mnt/bbb/kkkk.kkkkkkkk",
+ "/mnt/bbb/lllll",
+ "/mnt/ccc/z",
+ "/mnt/ccc/aaaa",
+ "/mnt/ccc/bbbbb",
+ "/mnt/ccc/ccccc",
+ "/mnt/ccc/d",
+ "/mnt/ccc/e",
+ "/mnt/ccc/ffffff.fff.f",
+ "/mnt/ccc/gggggggggggg",
+ "/mnt/ccc/hhhh",
+ "/mnt/ccc/iiiii.ii",
+ "/mnt/ccc/jjjj.jj.jjjjjjjj",
+ "/mnt/ccc/kkkk.kkkkkkkk",
+ "/mnt/ccc/lllll",
+ "/mnt/ddd/z",
+ "/mnt/ddd/aaaa",
+ "/mnt/ddd/bbbbb",
+ "/mnt/ddd/ccccc",
+ "/mnt/ddd/d",
+ "/mnt/ddd/e",
+ "/mnt/ddd/ffffff.fff.f",
+ "/mnt/ddd/gggggggggggg",
+ "/mnt/ddd/hhhh",
+ "/mnt/ddd/iiiii.ii",
+ "/mnt/ddd/jjjj.jj.jjjjjjjj",
+ "/mnt/ddd/kkkk.kkkkkkkk",
+ "/mnt/ddd/lllll",
+ "/mnt/eee/z",
+ "/mnt/eee/aaaa",
+ "/mnt/eee/bbbbb",
+ "/mnt/eee/ccccc",
+ "/mnt/eee/d",
+ "/mnt/eee/e",
+ "/mnt/eee/ffffff.fff.f",
+ "/mnt/eee/gggggggggggg",
+ "/mnt/eee/hhhh",
+ "/mnt/eee/iiiii.ii",
+ "/mnt/eee/jjjj.jj.jjjjjjjj",
+ "/mnt/eee/kkkk.kkkkkkkk",
+ "/mnt/eee/lllll",
+ "/mnt/fff/z",
+ "/mnt/fff/aaaa",
+ "/mnt/fff/bbbbb",
+ "/mnt/fff/ccccc",
+ "/mnt/fff/d",
+ "/mnt/fff/e",
+ "/mnt/fff/ffffff.fff.f",
+ "/mnt/fff/gggggggggggg",
+ "/mnt/fff/hhhh",
+ "/mnt/fff/iiiii.ii",
+ "/mnt/fff/jjjj.jj.jjjjjjjj",
+ "/mnt/fff/kkkk.kkkkkkkk",
+ "/mnt/fff/lllll"
+};
+
+char *
+bstg_pathstore_get()
+{
+ return bstg_pathstore[rand() %
+ ((sizeof(bstg_pathstore) / sizeof(bstg_pathstore[0])))];
+}
+
+void
+dogcore()
+{
+ pid_t sleepchild, gcorechild;
+ extern char **environ;
+
+ /* create a child for the gcore target */
+ if ((sleepchild = fork()) == 0) {
+ sleep(30);
+ _exit(1);
+ } else if (sleepchild > 0) {
+ char *token[] = {NULL, NULL, NULL, NULL, NULL};
+ char buf[64];
+ int status;
+
+ /* use the first process as the target */
+ snprintf(buf, sizeof(buf), "%d", sleepchild);
+ token[0] = "gcore";
+ token[1] = "-c";
+ token[2] = bstg_pathstore_get();
+ token[3] = buf;
+ assert(token[4] == NULL);
+
+ if ((gcorechild = fork()) > 0) {
+ waitpid(gcorechild, &status, 0);
+ } else if (gcorechild == 0) {
+ execve("/usr/bin/gcore", token, environ);
+ _exit(1);
+ }
+ kill(sleepchild, SIGKILL);
+ waitpid(sleepchild, &status, 0);
+ }
+}
+
+void
+dowrite()
+{
+ struct iovec data[] = {
+ {"12", 2},
+ {NULL, 0},
+ {"12345678", 8},
+ };
+ static int fd = -1;
+
+ if (fd == -1) {
+ /* keep existing file open during life of this process */
+ fd = open(bstg_pathstore_get(), O_RDWR | O_NONBLOCK | O_NOCTTY);
+ }
+ data[1].iov_base = bstg_pathstore_get();
+ data[1].iov_len = strlen((char *)data[1].iov_base);
+ ftruncate(fd, 0);
+ pwritev(fd, data, 3, 0);
+}
+
+void
+dounlink()
+{
+ unlink(bstg_pathstore_get());
+}
+
+void
+dolink()
+{
+ link(bstg_pathstore_get(), bstg_pathstore_get());
+}
+
+void
+domkdir()
+{
+ char **pdir;
+ static char *bstg_dirs[] = {
+ "/mnt/111", "/mnt/222", "/mnt/333", "/mnt/444",
+ "/mnt/555", "/mnt/666", "/mnt/777", "/mnt/888",
+ "/mnt/999", "/mnt/aaa", "/mnt/bbb", "/mnt/ccc",
+ "/mnt/ddd", "/mnt/eee", "/mnt/fff", NULL
+ };
+
+ for (pdir = bstg_dirs; *pdir; pdir++) {
+ if (mkdir(*pdir, 0777) == -1)
+ err(1, "mkdir(%s)", *pdir);
+ }
+}
+
+void
+dosync()
+{
+ sync();
+}
+
+int
+main()
+{
+ time_t start;
+ unsigned x;
+ int i, status;
+ void (*funcs[]) () = {
+ dogcore,
+ dowrite,
+ dounlink,
+ dolink,
+ dowrite,
+ dounlink,
+ dolink,
+ dowrite,
+ dosync,
+ dowrite,
+ dounlink,
+ dolink,
+ dowrite,
+ dounlink,
+ dolink,
+ dowrite,
+ };
+
+ /* we only can domkdir() once at startup */
+ domkdir();
+
+ /* create 128 children that loop forever running 4 operations */
+ dosync();
+ for (x = 0; x < 128; x++) {
+ if (fork() == 0) {
+ /* give child a new seed for the pathname selection */
+ srand(x);
+
+ start = time(NULL);
+ for (i = 0; i < 1000; i++) {
+ /* each child will start looping at different
+ * function */
+ (*funcs[x++ % 16]) ();
+ if (time(NULL) - start > RUNTIME)
+ break;
+ }
+ /* we never expect this code to run */
+ _exit(1);
+ }
+ }
+
+ /* block forever for all our children */
+ while (wait(&status) > 0);
+ return 0;
+}
diff --git a/tools/test/stress2/misc/suj24.sh b/tools/test/stress2/misc/suj24.sh
new file mode 100755
index 000000000000..68cb3dae2ff4
--- /dev/null
+++ b/tools/test/stress2/misc/suj24.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# Scenario from kern/160662
+# Scenario by Hans Ottevanger <hans beastielabs net>
+# Fixed in r225806
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+
+mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -j md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+(cd $mntpoint; mksnap_ffs $mntpoint/.snap/snapshot)
+
+while mount | grep -q $mntpoint; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/suj25.sh b/tools/test/stress2/misc/suj25.sh
new file mode 100755
index 000000000000..325fb9c39dc8
--- /dev/null
+++ b/tools/test/stress2/misc/suj25.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+# Destill of problems found by suj16.sh:
+# - fsync: giving up on dirty
+# - mksnap_ffs looping
+# Also seen: panic: ffs_update: bad link cnt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -w $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs -j md${mdstart}$part > /dev/null 2>&1
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+cp -R /usr/include $mntpoint/1
+cp -R /usr/include $mntpoint/2
+cp -R /usr/include $mntpoint/3
+cp -R /usr/include $mntpoint/4
+rm -rf $mntpoint/1 &
+sleep 2
+
+while [ ! -s $mntpoint/.snap/suj25 ]; do
+ rm -f $mntpoint/.snap/suj25
+ mksnap_ffs $mntpoint $mntpoint/.snap/suj25
+ n=$((n + 1))
+ [ $n -gt 5 ] && break
+done
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/suj26.sh b/tools/test/stress2/misc/suj26.sh
new file mode 100755
index 000000000000..90888c3f3892
--- /dev/null
+++ b/tools/test/stress2/misc/suj26.sh
@@ -0,0 +1,85 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Variation of suj16.sh with more test programs and VM pressure.
+
+# Deadlocks seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+[ $# -eq 1 ] && opt="$1" || opt="-j"
+newfs $opt md${mdstart}$part > /dev/null 2>&1
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=30m
+export RUNDIR=$mntpoint/stressX
+export creatINCARNATIONS=2
+
+export TESTPROGS="
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/swap/swap
+"
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > /dev/null 2>&1 &
+rpid=$!
+mdstart2=$((mdstart + 1))
+mntpoint2=${mntpoint}2
+[ -d $mntpoint2 ] || mkdir $mntpoint2
+mount | grep $mntpoint2 | grep -q /dev/md && umount -f $mntpoint2
+mdconfig -l | grep -q md$mdstart2 && mdconfig -d -u $mdstart2
+while kill -0 $rpid 2> /dev/null; do
+ rm -f $mntpoint/.snap/suj26
+ { mksnap_ffs $mntpoint $mntpoint/.snap/suj26 || continue; } 2>&1 | \
+ grep -v "Resource temporarily unavailable"
+ [ ! -s $mntpoint/.snap/suj26 ] && continue
+ mdconfig -a -t vnode -f $mntpoint/.snap/suj26 -u $mdstart2 -o readonly
+ mount -r /dev/md$mdstart2 $mntpoint2
+
+ ls -l $mntpoint2 > /dev/null
+
+ while mount | grep $mntpoint2 | grep -q /dev/md; do
+ umount $mntpoint2 || sleep 1
+ done
+ mdconfig -d -u $mdstart2
+done
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/suj27.sh b/tools/test/stress2/misc/suj27.sh
new file mode 100755
index 000000000000..f4f014260343
--- /dev/null
+++ b/tools/test/stress2/misc/suj27.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Variation of suj26.sh with one more test program.
+
+# Deadlocks seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+[ $# -eq 1 ] && opt="$1" || opt="-j"
+newfs $opt md${mdstart}$part > /dev/null 2>&1
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=30m
+export RUNDIR=$mntpoint/stressX
+export creatINCARNATIONS=2
+
+export TESTPROGS="
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rw/rw
+testcases/swap/swap
+"
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > /dev/null 2>&1 &
+rpid=$!
+mdstart2=$((mdstart + 1))
+mntpoint2=${mntpoint}2
+[ -d $mntpoint2 ] || mkdir $mntpoint2
+mount | grep $mntpoint2 | grep -q /dev/md && umount -f $mntpoint2
+mdconfig -l | grep -q md$mdstart2 && mdconfig -d -u $mdstart2
+while kill -0 $rpid 2> /dev/null; do
+ rm -f $mntpoint/.snap/suj27
+ { mksnap_ffs $mntpoint $mntpoint/.snap/suj27 || continue; } 2>&1 | \
+ grep -v "Resource temporarily unavailable"
+ [ ! -s $mntpoint/.snap/suj27 ] && continue
+ mdconfig -a -t vnode -f $mntpoint/.snap/suj27 -u $mdstart2 -o readonly
+ mount -r /dev/md$mdstart2 $mntpoint2
+
+ ls -l $mntpoint2 > /dev/null
+
+ umount $mntpoint2
+ mdconfig -d -u $mdstart2
+done
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/suj28.sh b/tools/test/stress2/misc/suj28.sh
new file mode 100755
index 000000000000..39f31428a196
--- /dev/null
+++ b/tools/test/stress2/misc/suj28.sh
@@ -0,0 +1,87 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Variation of suj26.sh where a missing "umount /mnt2" causes the following
+# mksnap_ffs to loop.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+[ $# -eq 1 ] && opt="$1" || opt="-j"
+newfs $opt md${mdstart}$part > /dev/null 2>&1
+
+mount /dev/md${mdstart}$part $mntpoint
+mount
+chmod 777 $mntpoint
+
+export runRUNTIME=5m
+export RUNDIR=$mntpoint/stressX
+export creatINCARNATIONS=2
+
+export TESTPROGS="
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/swap/swap
+"
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > /dev/null 2>&1 &
+rpid=$!
+mdstart2=$((mdstart + 1))
+mntpoint2=${mntpoint}2
+[ -d $mntpoint2 ] || mkdir $mntpoint2
+mount | grep $mntpoint2 | grep -q /dev/md && umount -f $mntpoint2
+mdconfig -l | grep -q md$mdstart2 && mdconfig -d -u $mdstart2
+while kill -0 $rpid 2> /dev/null; do
+ rm -f $mntpoint/.snap/suj28
+ { mksnap_ffs $mntpoint $mntpoint/.snap/suj28 || continue; } 2>&1 | \
+ grep -v "Resource temporarily unavailable"
+ [ ! -s $mntpoint/.snap/suj28 ] && continue
+ mdconfig -a -t vnode -f $mntpoint/.snap/suj28 -u $mdstart2 -o readonly
+ mount -r /dev/md$mdstart2 $mntpoint2
+
+ ls -l $mntpoint2 > /dev/null
+
+# Missing "umount $mntpoint2" causing the following mksnap_ffs to loop
+ mdconfig -d -u $mdstart2
+done
+wait
+
+while mount | grep $mntpoint2 | grep -q /dev/md; do
+ umount $mntpoint2 || sleep 1
+done
+mdconfig -l | grep -q md$mdstart2 && mdconfig -d -u $mdstart2
+while mount | grep "$mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/suj29.sh b/tools/test/stress2/misc/suj29.sh
new file mode 100755
index 000000000000..69f07abc2396
--- /dev/null
+++ b/tools/test/stress2/misc/suj29.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# tunefs -j enable -S xxx regression test
+# causes "Sparse journal inode 4."
+
+# Deadlocks seen:
+# https://people.freebsd.org/~pho/stress/log/jeff116.txt
+# https://people.freebsd.org/~pho/stress/log/jeff117.txt
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+newfs -U md${mdstart}$part > /dev/null
+echo "tunefs -j enable -S 10000000 /dev/md${mdstart}$part"
+tunefs -j enable -S 10000000 /dev/md${mdstart}$part
+
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=5m
+export RUNDIR=$mntpoint/stressX
+
+export LOAD=80
+export rwLOAD=80
+export TESTPROGS="
+testcases/creat/creat
+testcases/rw/rw
+testcases/swap/swap
+testcases/mkdir/mkdir
+"
+
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/md${mdstart}$part; s=$?
+tunefs -j disable /dev/md${mdstart}$part
+checkfs /dev/md${mdstart}$part || s=$?
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/suj3.sh b/tools/test/stress2/misc/suj3.sh
new file mode 100755
index 000000000000..aa46b39db2dd
--- /dev/null
+++ b/tools/test/stress2/misc/suj3.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Sector size > 512 test.
+
+# "panic: Memory modified after free ..." seen.
+# https://people.freebsd.org/~pho/stress/log/suj3-2.txt
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+
+dd if=/dev/random of=/tmp/suj3.key bs=64 count=1 > /dev/null 2>&1
+echo test | geli init -s 4096 -J - -K /tmp/suj3.key /dev/md$mdstart > /dev/null
+echo test | geli attach -j - -k /tmp/suj3.key /dev/md$mdstart
+newfs /dev/md$mdstart.eli > /dev/null
+
+tunefs -j enable /dev/md$mdstart.eli
+mount /dev/md$mdstart.eli $mntpoint
+chmod 777 $mntpoint
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=5m
+
+mount | grep -q md$mdstart.eli && \
+ su $testuser -c "cd ..; ./run.sh rw.cfg"
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+checkfs /dev/md$mdstart.eli; s=$?
+geli kill /dev/md$mdstart.eli
+mdconfig -d -u $mdstart
+rm -f /tmp/suj3.key
+exit $s
diff --git a/tools/test/stress2/misc/suj30.sh b/tools/test/stress2/misc/suj30.sh
new file mode 100755
index 000000000000..7e168e7c3f1c
--- /dev/null
+++ b/tools/test/stress2/misc/suj30.sh
@@ -0,0 +1,278 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# SUJ rename test scenario by Andrey Zonov <zont@FreeBSD.org>
+# "panic: flush_pagedep_deps: MKDIR_PARENT" seen:
+# http://people.freebsd.org/~pho/stress/log/suj30.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > suj30.c
+mycc -o suj30 -Wall -Wextra -O2 suj30.c -lpthread
+rm -f suj30.c
+
+mount | grep "on $mntpoint " | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 4g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -j md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+for i in `jot 10`; do
+ /tmp/suj30 $mntpoint/test-$i 100000 &
+done
+wait
+
+while mount | grep -q $mntpoint; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/suj30
+exit 0
+EOF
+/*
+ * Andrey Zonov (c) 2012
+ *
+ * compile as `cc -o rename rename.c -lpthread'
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/queue.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#ifdef __FreeBSD__
+#include <pthread_np.h>
+#define __NP__
+#endif
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define LOCK(x) pthread_mutex_lock(&x.mtx)
+#define UNLOCK(x) pthread_mutex_unlock(&x.mtx)
+#define SIGNAL(x) pthread_cond_signal(&x.wait)
+#define WAIT(x) pthread_cond_wait(&x.wait, &x.mtx)
+
+int max;
+int exited;
+char *dirname1;
+char *dirname2;
+
+struct file {
+ char *name;
+ STAILQ_ENTRY(file) next;
+};
+
+struct files {
+ pthread_mutex_t mtx;
+ pthread_cond_t wait;
+ STAILQ_HEAD(, file) list;
+};
+
+static struct files newfiles;
+static struct files renamedfiles;
+
+void *loop_create(void *arg __unused);
+void *loop_rename(void *arg __unused);
+void *loop_unlink(void *arg __unused);
+
+int
+main(int argc, char **argv)
+{
+ int i;
+ int rc;
+ pthread_t tid[3];
+
+ if (argc != 3)
+ errx(1, "usage: pthread_count <dirname> <max>");
+
+ asprintf(&dirname1, "%s.1", argv[1]);
+ asprintf(&dirname2, "%s.2", argv[1]);
+ if (mkdir(dirname1, 0755) == -1)
+ err(1, "mkdir(%s)", dirname1);
+ if (mkdir(dirname2, 0755) == -1)
+ err(1, "mkdir(%s)", dirname2);
+ max = atoi(argv[2]);
+
+ STAILQ_INIT(&newfiles.list);
+ STAILQ_INIT(&renamedfiles.list);
+
+ rc = pthread_mutex_init(&newfiles.mtx, NULL);
+ if (rc != 0)
+ errc(1, rc, "pthread_mutex_init()");
+ rc = pthread_cond_init(&newfiles.wait, NULL);
+ if (rc != 0)
+ errc(1, rc, "pthread_cond_init()");
+ rc = pthread_mutex_init(&renamedfiles.mtx, NULL);
+ if (rc != 0)
+ errc(1, rc, "pthread_mutex_init()");
+ rc = pthread_cond_init(&renamedfiles.wait, NULL);
+ if (rc != 0)
+ errc(1, rc, "pthread_cond_init()");
+
+ rc = pthread_create(&tid[0], NULL, loop_create, NULL);
+ if (rc != 0)
+ errc(1, rc, "pthread_create()");
+ rc = pthread_create(&tid[1], NULL, loop_rename, NULL);
+ if (rc != 0)
+ errc(1, rc, "pthread_create()");
+ rc = pthread_create(&tid[2], NULL, loop_unlink, NULL);
+ if (rc != 0)
+ errc(1, rc, "pthread_create()");
+
+ for (i = 0; i < 3; i++) {
+ rc = pthread_join(tid[i], NULL);
+ if (rc != 0)
+ errc(1, rc, "pthread_join(%d)", i);
+ }
+
+ rc = pthread_mutex_destroy(&newfiles.mtx);
+ if (rc != 0)
+ errc(1, rc, "pthread_mutex_destroy(newfiles)");
+ rc = pthread_cond_destroy(&newfiles.wait);
+ if (rc != 0)
+ errc(1, rc, "pthread_cond_destroy(newfiles)");
+ rc = pthread_mutex_destroy(&renamedfiles.mtx);
+ if (rc != 0)
+ errc(1, rc, "pthread_mutex_destroy(renamedfiles)");
+ rc = pthread_cond_destroy(&renamedfiles.wait);
+ if (rc != 0)
+ errc(1, rc, "pthread_cond_destroy(renamedfiles)");
+ rmdir(dirname1);
+ rmdir(dirname2);
+ free(dirname1);
+ free(dirname2);
+
+ exit(0);
+}
+
+void *
+loop_create(void *arg __unused)
+{
+ int i;
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ for (i = 0; i < max; i++) {
+ file = malloc(sizeof(*file));
+ asprintf(&file->name, "%s/filename_too-long:%d", dirname1, i);
+ if (mkdir(file->name, 0666) == -1) {
+ warn("mkdir(%s)", file->name);
+ free(file->name);
+ free(file);
+ break;
+ }
+ LOCK(newfiles);
+ STAILQ_INSERT_TAIL(&newfiles.list, file, next);
+ UNLOCK(newfiles);
+ SIGNAL(newfiles);
+ }
+ exited = 1;
+ SIGNAL(newfiles);
+ pthread_exit(NULL);
+}
+
+void *
+loop_rename(void *arg __unused)
+{
+ char *filename, *newname;
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ for ( ;; ) {
+ LOCK(newfiles);
+ while (STAILQ_EMPTY(&newfiles.list) && exited < 1)
+ WAIT(newfiles);
+ if (STAILQ_EMPTY(&newfiles.list) && exited == 1) {
+ UNLOCK(newfiles);
+ break;
+ }
+ file = STAILQ_FIRST(&newfiles.list);
+ STAILQ_REMOVE_HEAD(&newfiles.list, next);
+ UNLOCK(newfiles);
+ filename = strrchr(file->name, '/');
+ asprintf(&newname, "%s/%s", dirname2, filename);
+ if (rename(file->name, newname) == -1)
+ err(1, "rename(%s, %s)", file->name, newname);
+ free(file->name);
+ file->name = newname;
+ LOCK(renamedfiles);
+ STAILQ_INSERT_TAIL(&renamedfiles.list, file, next);
+ UNLOCK(renamedfiles);
+ SIGNAL(renamedfiles);
+ }
+ exited = 2;
+ SIGNAL(renamedfiles);
+ pthread_exit(NULL);
+}
+
+void *
+loop_unlink(void *arg __unused)
+{
+ struct file *file;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+ for ( ;; ) {
+ LOCK(renamedfiles);
+ while (STAILQ_EMPTY(&renamedfiles.list) && exited < 2)
+ WAIT(renamedfiles);
+ if (STAILQ_EMPTY(&renamedfiles.list) && exited == 2) {
+ UNLOCK(renamedfiles);
+ break;
+ }
+ file = STAILQ_FIRST(&renamedfiles.list);
+ STAILQ_REMOVE_HEAD(&renamedfiles.list, next);
+ UNLOCK(renamedfiles);
+ rmdir(file->name);
+ free(file->name);
+ free(file);
+ }
+ pthread_exit(NULL);
+}
diff --git a/tools/test/stress2/misc/suj31.sh b/tools/test/stress2/misc/suj31.sh
new file mode 100755
index 000000000000..e875d1f94076
--- /dev/null
+++ b/tools/test/stress2/misc/suj31.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Run with marcus.cfg on a 2g swap backed MD
+# Scenario: Update old disk to SU+J
+
+# OOVM seen:
+# https://people.freebsd.org/~pho/stress/log/suj31.txt
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs -U md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=10m
+export RUNDIR=$mntpoint/stressX
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+
+tunefs -j enable /dev/md${mdstart}$part
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+
+checkfs /dev/md${mdstart}$part; s=$?
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/suj32.sh b/tools/test/stress2/misc/suj32.sh
new file mode 100755
index 000000000000..b9c14339ccbb
--- /dev/null
+++ b/tools/test/stress2/misc/suj32.sh
@@ -0,0 +1,92 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# tunefs -j enable is not aware of the indirect blocks.
+# Problem fixed in r247399.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > suj32.c
+mycc -o suj32 -Wall -Wextra -O2 suj32.c || exit 1
+rm -f suj32.c
+cd $here
+
+mount | grep "on $mntpoint " | grep -q md$mdstart && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 5g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -U md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint || exit 1
+
+# fill the root directory to become larger than NIDIR * blksize
+(cd $mntpoint; /tmp/suj32)
+while mount | grep -q $mntpoint; do
+ umount $mntpoint || sleep 1
+done
+
+tunefs -j enable /dev/md${mdstart}$part
+
+if ! mount /dev/md${mdstart}$part $mntpoint; then
+ echo FAIL
+ fsck_ffs -y /dev/md${mdstart}$part
+else
+ umount $mntpoint
+fi
+
+mdconfig -d -u $mdstart
+rm -f /tmp/suj32
+exit 0
+EOF
+#include <sys/stat.h>
+#include <err.h>
+#include <stdio.h>
+
+int
+main(void)
+{
+ int i;
+ char name[800];
+ char *filler =
+ "fillerfillerfillerfillerfillerfillerfillerfillerfiller"
+ "fillerfillerfillerfillerfillerfillerfillerfillerfiller"
+ "fillerfillerfillerfillerfillerfillerfillerfillerfiller"
+ "fillerfillerfillerfillerfillerfillerfillerfillerfiller";
+
+ for (i = 0; i < 2000; i++) {
+ snprintf(name, sizeof(name), "%s.%d", filler, i);
+ if (mkdir(name, 00700) == -1)
+ err(1, "mkdir(%s)", name);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/suj33.sh b/tools/test/stress2/misc/suj33.sh
new file mode 100755
index 000000000000..44861d1b6739
--- /dev/null
+++ b/tools/test/stress2/misc/suj33.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# Test "umount" of active file system
+# Triggers "fsync: giving up on dirty" with ease.
+
+# umount -f fails with
+# "softdep_waitidle: Failed to flush worklist for 0xfffff80308cea000"
+# Fixed by r356763
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep "$mntpoint" | grep -q md${mdstart}$part && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -j md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=3m # Run tests for three minutes
+chmod 777 $mntpoint
+
+su $testuser -c '(cd ..; ./run.sh disk.cfg)' > /dev/null 2>&1 &
+sleep 20
+umount $mntpoint > /dev/null 2>&1
+kill $!
+../tools/killall.sh || exit
+wait
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FAIL; fstat -mf $mntpoint; exit 1; }
+done
+checkfs /dev/md${mdstart}$part; s=$?
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/suj34.sh b/tools/test/stress2/misc/suj34.sh
new file mode 100755
index 000000000000..2b60c289b163
--- /dev/null
+++ b/tools/test/stress2/misc/suj34.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Simple suj / nullfs test scenario.
+# http://people.freebsd.org/~pho/stress/log/suj34.txt
+# More debug info: https://people.freebsd.org/~pho/stress/log/kostik831.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q "$mntpoint" && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 4g -u $mdstart
+bsdlabel -w md$mdstart auto
+
+newfs -j md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+mkdir $mntpoint/null
+
+mnt2=${mntpoint}2
+[ -d $mnt2 ] || mkdir -p $mnt2
+mount | grep -q "on $mnt2 " && umount -f $mnt2
+mount -t nullfs $mntpoint/null $mnt2
+
+export RUNDIR=$mnt2/stressX
+export runRUNTIME=10m
+(cd ..; ./run.sh marcus.cfg)
+
+while mount | grep -q "on $mnt2 "; do
+ umount $mnt2 || sleep 1
+done
+while mount | grep -q "on $mntpoint "; do
+ umount $mntpoint || sleep 1
+done
diff --git a/tools/test/stress2/misc/suj35.sh b/tools/test/stress2/misc/suj35.sh
new file mode 100755
index 000000000000..304fc0826874
--- /dev/null
+++ b/tools/test/stress2/misc/suj35.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Demonstrate bogus "no inodes free" with SU.
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ "$newfs_flags" = "-U" ] || exit 0
+set -e
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2m -u $mdstart
+newfs -n -U /dev/md$mdstart > /dev/null
+
+mount /dev/md$mdstart $mntpoint
+inodes=`df -i $mntpoint | grep "$mntpoint" | awk '{print $7}'`
+inodes=$((inodes - 2))
+
+here=`pwd`
+s=0
+for j in `jot 2`; do
+ cd $mntpoint
+ for i in `jot $inodes`; do
+ mkdir $i || { df -i $mntpoint; s=1; break 2; }
+ cd $i
+ done
+ cd $mntpoint
+ rm -rf 1
+done
+cd $here
+
+umount $mntpoint
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/suj4.sh b/tools/test/stress2/misc/suj4.sh
new file mode 100755
index 000000000000..975f572a162d
--- /dev/null
+++ b/tools/test/stress2/misc/suj4.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "panic: worklist_remove: 0xca821840 jnewblk(0x109) not on list" seen
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -j md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=30m
+set `df -ik $mntpoint | tail -1 | awk '{print $4,$7}'`
+export KBLOCKS=$(($1 / 2))
+export INODES=$(($2 / 2))
+export rwINCARNATIONS=10
+export rwHOG=1
+
+su $testuser -c "cd ..; ./run.sh rw.cfg"
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/suj5.sh b/tools/test/stress2/misc/suj5.sh
new file mode 100755
index 000000000000..9dbaa90282ed
--- /dev/null
+++ b/tools/test/stress2/misc/suj5.sh
@@ -0,0 +1,180 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Deadlock seen
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > suj5.c
+mycc -o suj5 -Wall -O2 suj5.c
+rm -f suj5.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -j md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c "cd $mntpoint; /tmp/suj5" > /dev/null
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/suj5
+exit
+EOF
+#include <sys/param.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define PARALLEL 10
+
+static int
+random_int(int mi, int ma)
+{
+ return (arc4random() % (ma - mi + 1) + mi);
+}
+
+static int64_t
+df(void)
+{
+ char path[MAXPATHLEN+1];
+ struct statfs buf;
+
+ if (getcwd(path, sizeof(path)) == NULL)
+ err(1, "getcwd()");
+
+ if (statfs(path, &buf) < 0)
+ err(1, "statfs(%s)", path);
+ printf("Free space on %s: %jd Mb\n", path, buf.f_bavail * buf.f_bsize / 1024 / 1024);
+ return (buf.f_bavail * buf.f_bsize);
+}
+
+static void
+test(int size)
+{
+ int buf[1024], index, to;
+#ifdef TEST
+ int i;
+#endif
+ int fd;
+ char file[128];
+
+ sprintf(file,"p%05d", getpid());
+ if ((fd = creat(file, 0660)) == -1)
+ err(1, "creat(%s)", file);
+
+ to = sizeof(buf);
+ index = 0;
+ while (index < size) {
+ if (index + to > size)
+ to = size - index;
+#ifdef TEST
+ for (i = 0; i < to; i++)
+ buf[i] = index + i;
+#endif
+ index += to;
+ if (write(fd, buf, to) != to)
+ err(1, "write(%s), %s:%d", file, __FILE__, __LINE__);
+ }
+ if (close(fd) == -1)
+ err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);
+
+ if ((fd = open(file, O_RDONLY)) == -1)
+ err(1, "open(%s), %s:%d", file, __FILE__, __LINE__);
+
+ index = 0;
+ while (index < size) {
+ if (index + to > size)
+ to = size - index;
+ if (read(fd, buf, to) != to)
+ err(1, "rw read. %s.%d", __FILE__, __LINE__);
+#ifdef TEST
+ for (i = 0; i < to; i++) {
+ if (buf[i] != index + i) {
+ fprintf(stderr,
+ "%s, pid %d: expected %d @ %d, got %d\n",
+ getprogname(), getpid(), index+i, index+i,
+ buf[i]);
+ exit(EXIT_FAILURE);
+ }
+ }
+#endif
+ index += to;
+ }
+ if (close(fd) == -1)
+ err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s), %s:%d", file, __FILE__, __LINE__);
+ exit(0);
+}
+
+int
+main()
+{
+ int i, j, pct;
+ int size; /* in k */
+ int64_t bl;
+
+ bl = df();
+ if (bl > (int64_t)INT_MAX * PARALLEL)
+ bl = (int64_t)INT_MAX * PARALLEL;
+ size = bl / PARALLEL / 1024;
+
+ pct = random_int(1, 50);
+ size = size / 100 * pct + 1;
+ if (random_int(1, 100) <= 50)
+ size = 34 * 1024; /* Known good deadlock value */
+ printf("Max file size: %d Mb\n", size / 1024);
+
+ for (i = 0; i < 100; i++) {
+ for (j = 0; j < PARALLEL; j++) {
+ if (fork() == 0) {
+ test(random_int(1, size) * 1024);
+ }
+ }
+ for (j = 0; j < PARALLEL; j++)
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/suj6.sh b/tools/test/stress2/misc/suj6.sh
new file mode 100755
index 000000000000..b769e1d3c6e1
--- /dev/null
+++ b/tools/test/stress2/misc/suj6.sh
@@ -0,0 +1,183 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Variation of suj5.sh
+# "panic: indir_trunc: Index out of range -2 parent -2061 lbn -2060" seen
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > suj6.c
+mycc -o suj6 -Wall -O2 suj6.c
+rm -f suj6.c
+cd $here
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -j md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c "cd $mntpoint; /tmp/suj6" > /dev/null
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/suj6
+exit
+EOF
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mount.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#define PARALLEL 10
+
+static int
+random_int(int mi, int ma)
+{
+ return (arc4random() % (ma - mi + 1) + mi);
+}
+
+static int64_t
+df(void)
+{
+ char path[MAXPATHLEN+1];
+ struct statfs buf;
+
+ if (getcwd(path, sizeof(path)) == NULL)
+ err(1, "getcwd()");
+
+ if (statfs(path, &buf) < 0)
+ err(1, "statfs(%s)", path);
+ printf("Free space on %s: %jd Mb\n", path, buf.f_bavail * buf.f_bsize / 1024 / 1024);
+ return (buf.f_bavail * buf.f_bsize);
+}
+
+static void
+test(int size)
+{
+ int buf[1024], index, to;
+#ifdef TEST
+ int i;
+#endif
+ int fd;
+ char file[128];
+
+ sprintf(file,"p%05d", getpid());
+ if ((fd = creat(file, 0660)) == -1)
+ err(1, "creat(%s)", file);
+
+ to = sizeof(buf);
+ index = 0;
+ while (index < size) {
+ if (index + to > size)
+ to = size - index;
+#ifdef TEST
+ for (i = 0; i < to; i++)
+ buf[i] = index + i;
+#endif
+ index += to;
+ if (write(fd, buf, to) != to)
+ err(1, "write(%s), %s:%d", file, __FILE__, __LINE__);
+ }
+ if (close(fd) == -1)
+ err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);
+
+#if 0
+ if ((fd = open(file, O_RDONLY)) == -1)
+ err(1, "open(%s), %s:%d", file, __FILE__, __LINE__);
+
+ index = 0;
+ while (index < size) {
+ if (index + to > size)
+ to = size - index;
+ if (read(fd, buf, to) != to)
+ err(1, "rw read. %s.%d", __FILE__, __LINE__);
+#ifdef TEST
+ for (i = 0; i < to; i++) {
+ if (buf[i] != index + i) {
+ fprintf(stderr,
+ "%s, pid %d: expected %d @ %d, got %d\n",
+ getprogname(), getpid(), index+i, index+i,
+ buf[i]);
+ exit(EXIT_FAILURE);
+ }
+ }
+#endif
+ index += to;
+ }
+ if (close(fd) == -1)
+ err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);
+#endif
+ if (unlink(file) == -1)
+ err(1, "unlink(%s), %s:%d", file, __FILE__, __LINE__);
+ exit(0);
+}
+
+int
+main()
+{
+ int i, j, pct;
+ int size; /* in k */
+ int64_t bl;
+
+ bl = df();
+ if (bl > (int64_t)INT_MAX * PARALLEL)
+ bl = (int64_t)INT_MAX * PARALLEL;
+ size = bl / PARALLEL / 1024;
+
+ pct = random_int(1, 50);
+ size = size / 100 * pct + 1;
+ if (random_int(1, 100) <= 50)
+ size = 34 * 1024; /* Known good deadlock value */
+ printf("Max file size: %d Mb\n", size / 1024);
+
+ for (i = 0; i < 100; i++) {
+ for (j = 0; j < PARALLEL; j++) {
+ if (fork() == 0) {
+ test(random_int(1, size) * 1024);
+ }
+ }
+ for (j = 0; j < PARALLEL; j++)
+ wait(NULL);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/suj7.sh b/tools/test/stress2/misc/suj7.sh
new file mode 100755
index 000000000000..9975e701e86b
--- /dev/null
+++ b/tools/test/stress2/misc/suj7.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# umount stuck in mntref. Fixed in r216099
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs -j md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+chflags 0 $mntpoint/.sujournal
+rm -f $mntpoint/.sujournal
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint > /dev/null 2>&1 || sleep 1
+done
+
+# Expect console output:
+# Failed to find journal. Use tunefs to create one
+# Failed to start journal: 2
+
+echo "Expect: \"mount: /dev/md5a: No such file or directory\""
+[ -c /dev/md${mdstart}$part ] &&
+ mount /dev/md${mdstart}$part $mntpoint
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint > /dev/null 2>&1 || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/suj8.sh b/tools/test/stress2/misc/suj8.sh
new file mode 100755
index 000000000000..890caf2a27fd
--- /dev/null
+++ b/tools/test/stress2/misc/suj8.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# fsck modifies the fs_time without reason for SU+J, when "disk full" or
+# "out of inodes" has been reached.
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 128M -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+n=0
+s=0
+for i in 1 2 ; do
+ n=$((n + 1))
+ echo "newfs -U md${mdstart}$part"
+ newfs -U md${mdstart}$part > /dev/null
+ [ $n -eq 2 ] && tunefs -j enable /dev/md${mdstart}$part
+
+ mount /dev/md${mdstart}$part $mntpoint
+
+ dd if=/dev/zero of=$mntpoint/big bs=4k > /dev/null 2>&1
+
+ while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ done
+ checkfs /dev/md${mdstart}$part || s=1
+done
+
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/suj9.sh b/tools/test/stress2/misc/suj9.sh
new file mode 100755
index 000000000000..72d9f7382061
--- /dev/null
+++ b/tools/test/stress2/misc/suj9.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Run with marcus.cfg on a 128 Mb swap backed MD
+# "panic: softdep_revert_mkdir: dot addref parent mismatch" seen.
+
+# "panic: ufsdirhash_dirtrunc: bad offset" seen with WiP kernel code:
+# https://people.freebsd.org/~pho/stress/log/suj9.txt
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 128m -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+for i in `jot 2`; do
+ newfs -j md${mdstart}$part > /dev/null
+
+ mount /dev/md${mdstart}$part $mntpoint
+ chmod 777 $mntpoint
+
+ export runRUNTIME=5m
+ export RUNDIR=$mntpoint/stressX
+
+ su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1
+
+ for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FAIL; fstat -mf $mntpoint; exit 1; }
+ done
+done
+mdconfig -d -u $mdstart
+exit
diff --git a/tools/test/stress2/misc/swap.sh b/tools/test/stress2/misc/swap.sh
new file mode 100755
index 000000000000..0399e061cbcf
--- /dev/null
+++ b/tools/test/stress2/misc/swap.sh
@@ -0,0 +1,157 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Swap test. Variation of testcases/swap.
+# OOM seen: https://people.freebsd.org/~pho/stress/log/swap.txt
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/swap.c
+mycc -o swap -Wall -Wextra -O2 swap.c || exit 1
+rm -f swap.c
+
+usermem=`sysctl hw.usermem | sed 's/.* //'`
+
+if [ `sysctl -n vm.swap_total` -gt 0 ]; then
+ size=$((usermem/10*11))
+else
+ size=$((usermem/10*8))
+fi
+
+log=/tmp/swap.log
+tail -F -n 0 /var/log/messages > $log & lpid=$!
+/tmp/swap $((size / 4096))
+kill $lpid
+grep -m 1 "swp_pager_getswapspace" $log && s=1 || s=0
+rm $log
+
+rm -f /tmp/swap
+exit $s
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/resource.h>
+#include <sys/sysctl.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define RUNTIME (5 * 60)
+#define INCARNATIONS 32
+
+static unsigned long size, original;
+
+void
+setup(void)
+{
+ struct rlimit rlp;
+
+ size = size / INCARNATIONS;
+ original = size;
+ if (size == 0)
+ errx(1, "Argument too small");
+
+ if (getrlimit(RLIMIT_DATA, &rlp) < 0)
+ err(1,"getrlimit");
+ rlp.rlim_cur -= 1024 * 1024;
+
+ if (size > (unsigned long)rlp.rlim_cur)
+ size = rlp.rlim_cur;
+
+#if 0
+ printf("setup: pid %d. Total %luMb\n",
+ getpid(), size / 1024 / 1024 * INCARNATIONS);
+#endif
+
+ if (size == 0)
+ errx(1, "Argument too small");
+
+ return;
+}
+
+int
+test(void)
+{
+ volatile char *c;
+ int page;
+ unsigned long i, j;
+ time_t start;
+
+ c = malloc(size);
+ while (c == NULL) {
+ size -= 1024 * 1024;
+ c = malloc(size);
+ }
+ if (size != original)
+ printf("Malloc size changed from %ld Mb to %ld Mb\n",
+ original / 1024 / 1024, size / 1024 / 1024);
+ page = getpagesize();
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ i = j = 0;
+ while (i < size) {
+ c[i] = 0;
+ i += page;
+ if (++j % 1024 == 0) {
+ if ((time(NULL) - start) >= RUNTIME)
+ break;
+ if (arc4random() % 100 < 5)
+ usleep(1000);
+ }
+ }
+ }
+ free((void *)c);
+
+ _exit(0);
+}
+
+int
+main(int argc, char **argv)
+{
+ int i;
+
+ if (argc != 2)
+ errx(1, "Usage: %s bytes", argv[0]);
+
+ size = atol(argv[1]) * 4096;
+ setup();
+
+ for (i = 0; i < INCARNATIONS; i++)
+ if (fork() == 0)
+ test();
+
+ for (i = 0; i < INCARNATIONS; i++)
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/swap2.sh b/tools/test/stress2/misc/swap2.sh
new file mode 100755
index 000000000000..faba6c6ec956
--- /dev/null
+++ b/tools/test/stress2/misc/swap2.sh
@@ -0,0 +1,176 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Swap test. Variation of swap.sh
+# panic: vm_radix_remove: impossible to locate the key
+# panic: vm_page_alloc: cache page 0xff... is missing from the free queue
+# panic: vm_radix_node_put: rnode 0xfffffe00099035a0 has a child
+# panic: vm_radix_remove: impossible to locate the key
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export RUNDIR=$mntpoint/stressX
+rm -rf /tmp/stressX.control
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/swap2.c
+mycc -o swap2 -Wall -Wextra -O2 swap2.c || exit 1
+rm -f swap2.c
+cd $odir
+
+usermem=`sysctl -n hw.usermem`
+swap=`sysctl -n vm.swap_total`
+
+if [ $swap -gt 0 ]; then
+ size=$((usermem/10*11))
+else
+ size=$((usermem/10*8))
+fi
+
+/tmp/swap2 $((size / 4096)) &
+sleep 30
+su $testuser -c "(cd ../testcases/rw; ./rw -t 3m -i 40 -l 100 -v -h -h)" &
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/swap2
+exit
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/resource.h>
+#include <sys/sysctl.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define RUNTIME (5 * 60)
+#define INCARNATIONS 32
+
+static unsigned long size, original;
+
+void
+setup(void)
+{
+ struct rlimit rlp;
+
+ size = size / INCARNATIONS;
+ original = size;
+ if (size == 0)
+ errx(1, "Argument too small");
+
+ if (getrlimit(RLIMIT_DATA, &rlp) < 0)
+ err(1,"getrlimit");
+ rlp.rlim_cur -= 1024 * 1024;
+
+ if (size > (unsigned long)rlp.rlim_cur)
+ size = rlp.rlim_cur;
+
+#if 0
+ printf("setup: pid %d. Total %luMb\n",
+ getpid(), size / 1024 / 1024 * INCARNATIONS);
+#endif
+
+ if (size == 0)
+ errx(1, "Argument too small");
+
+ return;
+}
+
+int
+test(void)
+{
+ volatile char *c;
+ int page;
+ unsigned long i, j;
+ time_t start;
+
+ c = malloc(size);
+ while (c == NULL) {
+ size -= 1024 * 1024;
+ c = malloc(size);
+ }
+ if (size != original)
+ printf("Malloc size changed from %ld Mb to %ld Mb\n",
+ original / 1024 / 1024, size / 1024 / 1024);
+ page = getpagesize();
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ i = j = 0;
+ while (i < size) {
+ c[i] = 0;
+ i += page;
+ if (++j % 1024 == 0) {
+ if ((time(NULL) - start) >= RUNTIME)
+ break;
+ if (arc4random() % 100 < 5)
+ usleep(1000);
+ }
+ }
+ }
+ free((void *)c);
+
+ _exit(0);
+}
+
+int
+main(int argc, char **argv)
+{
+ int i;
+
+ if (argc != 2)
+ errx(1, "Usage: %s bytes", argv[0]);
+
+ size = atol(argv[1]) * 4096;
+ setup();
+
+ for (i = 0; i < INCARNATIONS; i++)
+ if (fork() == 0)
+ test();
+
+ for (i = 0; i < INCARNATIONS; i++)
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/swap3.sh b/tools/test/stress2/misc/swap3.sh
new file mode 100755
index 000000000000..476e92eb4725
--- /dev/null
+++ b/tools/test/stress2/misc/swap3.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Run swap2.sh with 1 GB RAM and no swap disk.
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `sysctl -n hw.physmem` -gt $(( 1 * 1024 * 1024 * 1024)) ] &&
+ echo "RAM should be capped to 1GB in this test."
+[ `swapinfo | wc -l` -eq 1 ] || { swapoff -a; off=1; }
+./swap2.sh
+[ -n "$off" ] && swapon -a
diff --git a/tools/test/stress2/misc/swap4.sh b/tools/test/stress2/misc/swap4.sh
new file mode 100755
index 000000000000..98bd3fb9f34a
--- /dev/null
+++ b/tools/test/stress2/misc/swap4.sh
@@ -0,0 +1,152 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Copy of swap.sh, but without a swap disk and using 110% of hw.usermem
+# All memory is in active or in laundry, and there are several processes
+# swapped out:
+# https://people.freebsd.org/~pho/stress/log/kostik1068.txt
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/swap4.c
+mycc -o swap4 -Wall -Wextra -O2 swap4.c || exit 1
+rm -f swap4.c
+
+usermem=`sysctl hw.usermem | sed 's/.* //'`
+size=$((usermem/10*11))
+
+[ `sysctl -n vm.swap_total` -gt 0 ] && { swapoff -a; off=1; }
+echo "Expect: swap4, uid 0, was killed: out of swap space"
+$dir/swap4 $((size / 4096)) &
+sleep .5
+while [ `pgrep swap4 | wc -l` -eq 11 ]; do sleep 5; done
+pkill swap4
+wait
+[ $off ] && swapon -a
+
+rm -f $dir/swap4
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/resource.h>
+#include <sys/sysctl.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define INCARNATIONS 10
+#define RUNTIME (5 * 60)
+
+static unsigned long size, original;
+
+static void
+setup(void)
+{
+ struct rlimit rlp;
+
+ size = size / INCARNATIONS;
+ original = size;
+ if (size == 0)
+ errx(1, "Argument too small");
+
+ if (getrlimit(RLIMIT_DATA, &rlp) < 0)
+ err(1,"getrlimit");
+ rlp.rlim_cur -= 1024 * 1024;
+
+ if (size > (unsigned long)rlp.rlim_cur)
+ size = rlp.rlim_cur;
+
+ if (size == 0)
+ errx(1, "Argument too small");
+
+ return;
+}
+
+static int
+test(void)
+{
+ time_t start;
+ unsigned long i, j;
+ int page;
+ volatile char *c;
+
+ c = malloc(size);
+ while (c == NULL) {
+ size -= 1024 * 1024;
+ c = malloc(size);
+ }
+ if (size != original)
+ printf("Malloc size changed from %ld Mb to %ld Mb\n",
+ original / 1024 / 1024, size / 1024 / 1024);
+ page = getpagesize();
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ i = j = 0;
+ while (i < size) {
+ c[i] = 0;
+ i += page;
+ if (++j % 1024 == 0) {
+ if ((time(NULL) - start) >= RUNTIME)
+ break;
+ if (arc4random() % 100 < 5)
+ usleep(1000);
+ }
+ }
+ }
+ free((void *)c);
+
+ _exit(0);
+}
+
+int
+main(int argc, char **argv)
+{
+ int i;
+
+ if (argc != 2)
+ errx(1, "Usage: %s bytes", argv[0]);
+
+ size = atol(argv[1]) * 4096;
+ setup();
+
+ for (i = 0; i < INCARNATIONS; i++)
+ if (fork() == 0)
+ test();
+
+ for (i = 0; i < INCARNATIONS; i++)
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/swap5.sh b/tools/test/stress2/misc/swap5.sh
new file mode 100755
index 000000000000..a0d746e1d2e2
--- /dev/null
+++ b/tools/test/stress2/misc/swap5.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Test with out of swap space:
+# swp_pager_getswapspace(32): failed
+
+# "panic: freeing free block" sen with WiP kernel code:
+# https://people.freebsd.org/~pho/stress/log/dougm027.txt
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+[ `sysctl -n hw.physmem` -lt $(( 5 * 1024 * 1024 * 1024)) ] && exit 0
+cc -o /tmp/swap5 -Wall -Wextra -O2 ../tools/swap.c || exit 1
+swapoff -a
+mdconfig -a -t malloc -o reserve -s 4g -u $mdstart || exit 1
+swapon /dev/md$mdstart || exit 1
+for i in `jot 3`; do
+ /tmp/swap5 -t 180 -p 85
+done
+swapoff /dev/md$mdstart
+mdconfig -d -u $mdstart
+swapon -a
+rm -f /tmp/swap5
+exit 0
diff --git a/tools/test/stress2/misc/swap6.sh b/tools/test/stress2/misc/swap6.sh
new file mode 100755
index 000000000000..30a3fd8e703a
--- /dev/null
+++ b/tools/test/stress2/misc/swap6.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+# Verify that the VM pressure generated is sufficient for stress testing.
+# Regression test for OOM fix in r357253
+
+. ../default.cfg
+
+[ `sysctl -n vm.swap_total` -eq 0 ] && exit 0
+min=5 # percent swap usage
+(cd ../testcases/swap; ./swap -t 10m -i 100 -l 100 -h > /dev/null) &
+sleep 1
+mx=0
+while pgrep -q swap; do
+ n=`swapinfo | tail -1 | sed 's/.* //; s/%//'`
+ [ $n -gt $mx ] && mx=$n
+ [ $mx -ge $min ] && pkill swap
+done
+wait
+if [ $mx -lt $min ]; then
+ echo "FAIL Only $mx% swap used"
+ exit 1
+else
+ echo "$mx% swap used"
+ exit 0
+fi
diff --git a/tools/test/stress2/misc/swapoff.sh b/tools/test/stress2/misc/swapoff.sh
new file mode 100755
index 000000000000..3251653d472f
--- /dev/null
+++ b/tools/test/stress2/misc/swapoff.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "panic: swapoff: failed to locate 17590 swap blocks" seen:
+# https://people.freebsd.org/~pho/stress/log/swapoff.txt
+
+# Fixed by r305129.
+
+. ../default.cfg
+
+[ `sysctl -n vm.swap_total` -eq 0 ] && exit 0
+[ `sysctl -n hw.physmem` -gt $(( 4 * 1024 * 1024 * 1024)) ] &&
+ echo "RAM should be capped to 4GB for this test."
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=5m
+export RUNDIR=$mntpoint/stressX
+
+su $testuser -c 'cd ..; ./run.sh marcus.cfg' &
+
+sleep 120
+swapoff -a
+sleep 30
+swapon -a
+../tools/killall.sh
+wait
+
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ n=$((n + 1))
+ [ $n -gt 300 ] && { echo FAIL; exit 1; }
+done
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/swapoff2.sh b/tools/test/stress2/misc/swapoff2.sh
new file mode 100755
index 000000000000..e86f868ddb01
--- /dev/null
+++ b/tools/test/stress2/misc/swapoff2.sh
@@ -0,0 +1,76 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# swapoff(8) with swap file gone missing (known issue).
+
+# panic: swap_pager_force_pagein: read from swap failed
+# https://people.freebsd.org/~pho/stress/log/swapoff2.txt
+
+. ../default.cfg
+
+set -e
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+set +e
+
+: ${size:=2m}
+dd if=/dev/zero of=$mntpoint/swapfile bs=$size count=1 status=none
+ls -lh $mntpoint/swapfile
+swapoff -a
+md2=$((mdstart + 1))
+mdconfig -l | grep -q md$md2 && mdconfig -d -u $md2
+mdconfig -a -t vnode -f $mntpoint/swapfile -u $md2
+set +e
+swapon /dev/md$md2
+
+su $testuser -c 'cd ../testcases/swap; nice ./swap -t 2m -i 20 -h -l 100' &
+for i in `jot 20`; do
+ [ `swapinfo | tail -1 | awk '{print $3}'` -gt 0 ] && break
+ sleep 5
+done
+umount -f $mntpoint
+swapoff /dev/md$md2
+
+while pgrep -q swap; do pkill -9 swap; done
+wait
+
+n=0
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ n=$((n + 1))
+ [ $n -gt 300 ] && { echo FAIL; exit 1; }
+done
+mdconfig -d -u $mdstart
+swapon -a
+exit 0
diff --git a/tools/test/stress2/misc/swapoff3.sh b/tools/test/stress2/misc/swapoff3.sh
new file mode 100755
index 000000000000..89b775a4f59c
--- /dev/null
+++ b/tools/test/stress2/misc/swapoff3.sh
@@ -0,0 +1,144 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Mark Johnston
+#
+# 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 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.
+#
+
+# "panic: Swapdev not found" seen:
+# https://people.freebsd.org/~pho/stress/log/swapoff3.txt
+# Fixed by r358024-6
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+dir=`dirname $diskimage`
+[ `df -k $dir | tail -1 | awk '{print $4}'` -lt \
+ $((20 * 1024 * 1024)) ] &&
+ { echo "Need 20GB on $dir"; exit 0; }
+
+cat > /tmp/swapoff3.c <<EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/sysctl.h>
+
+#include <assert.h>
+#include <err.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ char *addr, *naddr, *res;
+ size_t i, len, swap, vsz;
+ u_int free;
+
+ vsz = sizeof(free);
+ if (sysctlbyname("vm.stats.vm.v_free_count", &free, &vsz, NULL, 0) != 0)
+ err(1, "sysctl(vm.stats.vm.v_free_count)");
+ vsz = sizeof(swap);
+ if (sysctlbyname("vm.swap_total", &swap, &vsz, NULL, 0) != 0)
+ err(1, "sysctl(vm.swap_total)");
+
+ len = (size_t)free * PAGE_SIZE + swap / 4;
+ addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
+ -1, 0);
+ if (addr == MAP_FAILED)
+ err(1, "mmap");
+
+ res = malloc(howmany(len, PAGE_SIZE));
+ if (res == NULL)
+ err(1, "malloc");
+
+ for (i = 0; i < len; i += PAGE_SIZE)
+ addr[i] = 1;
+
+ for (;;) {
+ if (mincore(addr, len, res) != 0)
+ err(1, "mincore");
+ for (i = 0; i < howmany(len, PAGE_SIZE); i++)
+ if ((res[i] & MINCORE_INCORE) == 0) {
+ naddr = addr + i * PAGE_SIZE;
+ if (munmap(naddr, PAGE_SIZE) != 0)
+ err(1, "munmap");
+ if (mmap(naddr, PAGE_SIZE,
+ PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_PRIVATE | MAP_FIXED,
+ -1, 0) == MAP_FAILED)
+ err(1, "mmap");
+ assert(*naddr == 0);
+ *naddr = 1;
+ }
+ }
+
+ return (0);
+}
+EOF
+mycc -o /tmp/swapoff3 -Wall -Wextra -O2 -g /tmp/swapoff3.c || exit
+
+set -e
+md1=$mdstart
+md2=$((md1 + 1))
+[ `sysctl -n vm.swap_total` -gt 0 ] && { swapoff -a > /dev/null; off=1; }
+truncate -s 10G $dir/swap1
+mdconfig -a -t vnode -f $dir/swap1 -u $md1
+swapon /dev/md$md1
+truncate -s 10G $dir/swap2
+mdconfig -a -t vnode -f $dir/swap2 -u $md2
+swapon /dev/md$md2
+set +e
+
+timeout 4m /tmp/swapoff3 &
+for i in `jot 60`; do
+ n=`swapinfo | tail -1 | awk '{print $3}'`
+ [ $n -gt 0 ] && break
+ sleep 1
+done
+echo "Total in use: $n"
+
+n=0
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 120 ]; do
+ for dev in /dev/md$md1 /dev/md$md2; do
+ for i in `jot 3`; do
+ swapoff $dev && break
+ done
+ swapon $dev
+ n=$((n + 1))
+ done
+ sleep .1
+done
+kill $!
+echo "$n swapoffs"
+[ $n -lt 1000 ] && { echo "Only performed $n swapoffs"; s=1; } || s=0
+wait
+for dev in /dev/md$md1 /dev/md$md2; do
+ swapoff $dev
+done
+mdconfig -d -u $md1
+mdconfig -d -u $md2
+rm -f $dir/swap1 $dir/swap2 /tmp/swapoff3 /tmp/swapoff3.c
+[ $off ] && swapon -a > /dev/null
+exit 0
diff --git a/tools/test/stress2/misc/swapoff4.sh b/tools/test/stress2/misc/swapoff4.sh
new file mode 100755
index 000000000000..8bba94faebd1
--- /dev/null
+++ b/tools/test/stress2/misc/swapoff4.sh
@@ -0,0 +1,82 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+# A variation of Mark's swapoff3.sh
+
+# "panic: Last close with active requests" seen:
+# https://people.freebsd.org/~pho/stress/log/mark134.txt
+# Fixed by r358024-6
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+dir=`dirname $diskimage`
+[ `df -k $dir | tail -1 | awk '{print $4}'` -lt \
+ $((20 * 1024 * 1024)) ] &&
+ { echo "Need 20GB on $dir"; exit 0; }
+
+set -e
+md1=$mdstart
+md2=$((md1 + 1))
+[ `sysctl -n vm.swap_total` -gt 0 ] && { swapoff -a > /dev/null; off=1; }
+truncate -s 10G $dir/swap1
+mdconfig -a -t vnode -f $dir/swap1 -u $md1
+swapon /dev/md$md1
+truncate -s 10G $dir/swap2
+mdconfig -a -t vnode -f $dir/swap2 -u $md2
+swapon /dev/md$md2
+set +e
+
+export swapINCARNATIONS=25
+(cd ../testcases/swap; ./swap -t 4m -i 20 -l 100) &
+for i in `jot 90`; do
+ used=`swapinfo | tail -1 | awk '{print $3}'`
+ [ $used -gt 0 ] && break
+ sleep 2
+done
+
+n=0
+while pgrep -q swap; do
+ for dev in /dev/md$md1 /dev/md$md2; do
+ for i in `jot 3`; do
+ swapoff $dev && break
+ done
+ swapon $dev
+ n=$((n + 1))
+ done
+done
+[ $n -lt 100 ] && { echo "Only performed $n swapoffs"; s=1; } || s=0
+wait
+for dev in /dev/md$md1 /dev/md$md2; do
+ swapoff $dev
+done
+mdconfig -d -u $md1
+mdconfig -d -u $md2
+rm -f $dir/swap1 $dir/swap2
+[ $off ] && swapon -a > /dev/null
+exit 0
diff --git a/tools/test/stress2/misc/swapoff5.sh b/tools/test/stress2/misc/swapoff5.sh
new file mode 100755
index 000000000000..fa4e0fe37a17
--- /dev/null
+++ b/tools/test/stress2/misc/swapoff5.sh
@@ -0,0 +1,182 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Mark Johnston
+#
+# 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 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.
+#
+
+# Variation of swapoff3.sh
+
+# Deadlock seen:
+# https://people.freebsd.org/~pho/stress/log/log0005.txt
+# This is a "known" low memory deadlocks caused by using md(4) as a swap
+# device.
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+dir=`dirname $diskimage`
+[ `df -k $dir | tail -1 | awk '{print $4}'` -lt \
+ $((20 * 1024 * 1024)) ] &&
+ { echo "Need 20GB on $dir"; exit 0; }
+
+cat > /tmp/swapoff5.c <<EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/sysctl.h>
+
+#include <assert.h>
+#include <err.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ char *addr, *naddr, *res;
+ size_t i, len, swap, vsz;
+ u_int free;
+
+ vsz = sizeof(free);
+ if (sysctlbyname("vm.stats.vm.v_free_count", &free, &vsz, NULL, 0) != 0)
+ err(1, "sysctl(vm.stats.vm.v_free_count)");
+ vsz = sizeof(swap);
+ if (sysctlbyname("vm.swap_total", &swap, &vsz, NULL, 0) != 0)
+ err(1, "sysctl(vm.swap_total)");
+
+ len = (size_t)free * PAGE_SIZE + swap / 4;
+ addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
+ -1, 0);
+ if (addr == MAP_FAILED)
+ err(1, "mmap");
+
+ res = malloc(howmany(len, PAGE_SIZE));
+ if (res == NULL)
+ err(1, "malloc");
+
+ for (i = 0; i < len; i += PAGE_SIZE)
+ addr[i] = 1;
+
+ for (;;) {
+ if (mincore(addr, len, res) != 0)
+ err(1, "mincore");
+ for (i = 0; i < howmany(len, PAGE_SIZE); i++)
+ if ((res[i] & MINCORE_INCORE) == 0) {
+ naddr = addr + i * PAGE_SIZE;
+ if (munmap(naddr, PAGE_SIZE) != 0)
+ err(1, "munmap");
+ if (mmap(naddr, PAGE_SIZE,
+ PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_PRIVATE | MAP_FIXED,
+ -1, 0) == MAP_FAILED)
+ err(1, "mmap");
+ assert(*naddr == 0);
+ *naddr = 1;
+ }
+ }
+
+ return (0);
+}
+EOF
+mycc -o /tmp/swapoff5 -Wall -Wextra -O2 -g /tmp/swapoff5.c || exit
+
+cat > /tmp/swapoff5x.c <<EOF
+#include <sys/mman.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+int
+main(int argc, char *argv[])
+{
+ time_t start;
+ int n, r;
+ char off1[80], off2[80], on1[80], on2[80];
+
+ if ( argc != 3) {
+ fprintf(stderr, "Usage: %s <dev1> <dev2>\n", argv[0]);
+ exit(1);
+ }
+
+ snprintf(off1, sizeof(off1), "swapoff %s", argv[1]);
+ snprintf(off2, sizeof(off2), "swapoff %s", argv[2]);
+ snprintf(on1, sizeof(on1), "swapon %s", argv[1]);
+ snprintf(on2, sizeof(on2), "swapon %s", argv[2]);
+
+ start = time(NULL);
+ if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
+ err(1, "mlockall(MCL_CURRENT) | MCL_FUTURE");
+ n = 0;
+ while (time(NULL) - start < 240) {
+ if ((r = system(off1)) != 0)
+ errx(1, "%s failed with %d", off1, r);
+ if ((r = system(on1)) != 0)
+ errx(1, "%s failed with %d", on1, r);
+ if ((r = system(off2)) != 0)
+ errx(1, "%s failed with %d", off2, r);
+ if ((r = system(on2)) != 0)
+ errx(1, "%s failed with %d", on2, r);
+ n++;
+ }
+ if (n < 100)
+ fprintf(stderr, "%d swapoffs\n", n);
+
+ return (0);
+}
+EOF
+mycc -o /tmp/swapoff5x -Wall -Wextra -O2 -g /tmp/swapoff5x.c || exit
+
+set -e
+md1=$mdstart
+md2=$((md1 + 1))
+[ `sysctl -n vm.swap_total` -gt 0 ] && { swapoff -a > /dev/null; off=1; }
+truncate -s 10G $dir/swap1
+mdconfig -a -t vnode -f $dir/swap1 -u $md1
+swapon /dev/md$md1
+truncate -s 10G $dir/swap2
+mdconfig -a -t vnode -f $dir/swap2 -u $md2
+swapon /dev/md$md2
+set +e
+
+timeout 4m /tmp/swapoff5 &
+for i in `jot 60`; do
+ n=`swapinfo | tail -1 | awk '{print $3}'`
+ [ $n -gt 0 ] && break
+ sleep 1
+done
+
+/tmp/swapoff5x /dev/md$md1 /dev/md$md2
+kill $! > /dev/null 2>&1
+wait
+for dev in /dev/md$md1 /dev/md$md2; do
+ swapoff $dev
+done
+mdconfig -d -u $md1
+mdconfig -d -u $md2
+rm -f $dir/swap1 $dir/swap2 /tmp/swapoff5 /tmp/swapoff5.c /tmp/swapoff5x \
+ /tmp/swapoff5x.c
+[ $off ] && swapon -a > /dev/null
+exit $s
diff --git a/tools/test/stress2/misc/swappedout.sh b/tools/test/stress2/misc/swappedout.sh
new file mode 100755
index 000000000000..e58b411c2500
--- /dev/null
+++ b/tools/test/stress2/misc/swappedout.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Processes stay in "swapped out" state even after swapoff
+# Broken by r254304.
+
+# "panic: page 0xfffffe00070e7d30 is neither wired nor queued":
+# https://people.freebsd.org/~pho/stress/log/swappedout-3.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+(cd ../testcases/swap; ./swap -t 2m -i 20 -v) > /dev/null
+ps auxww | awk '{print $8}' | egrep -q ".W" || exit 0
+swapoff -a > /dev/null || exit 1
+if ps auxww | awk '{print $8}' | egrep -q ".W"; then
+ echo Fail
+ echo "swapinfo:"
+ swapinfo
+ echo ""
+ ps -l | head -1
+ ps auxww | awk '{print $2, $8}' | egrep "[^ ]W" | while read l; do
+ set $l
+ ps -lp$1 | tail -1
+ done
+fi
+swapon -a > /dev/null
+exit 0
diff --git a/tools/test/stress2/misc/symlink.sh b/tools/test/stress2/misc/symlink.sh
new file mode 100755
index 000000000000..47257aa92a24
--- /dev/null
+++ b/tools/test/stress2/misc/symlink.sh
@@ -0,0 +1,128 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Testing problem with premature disk full problem with symlinks.
+# Not seen as of 20210117.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+dbt=`sysctl -n vfs.dirtybufthresh`
+[ $dbt -lt 1000 ] && echo "Note: vfs.dirtybufthresh = $dbt"
+
+D=$diskimage
+dd if=/dev/zero of=$D bs=1m count=128 status=none || exit 1
+
+odir=`pwd`
+dir=$mntpoint
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > symlink.c
+mycc -o symlink -Wall -Wextra symlink.c || exit 1
+rm -f symlink.c
+cd $odir
+
+mount | grep -q "on $mntpoint " && umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+
+tst() {
+ local i j k
+
+ cd $dir
+ i=`df -ik $mntpoint | tail -1 | awk '{printf "%d\n", ($7 - 500)/2}'`
+ [ $i -gt 20000 ] && i=20000
+
+ for k in `jot 3`; do
+ for j in `jot 2`; do
+ /tmp/symlink $i &
+ done
+ wait
+ done
+ cd $odir
+}
+
+s=0
+for i in "" "-U" "-j"; do
+ newfs $i /dev/md$mdstart > /dev/null 2>&1
+ mount /dev/md$mdstart $mntpoint
+
+ t1=`date +%s`
+ tst; s=$?
+ t2=$((`date +%s` - t1))
+
+ umount -f $mntpoint
+ t2=$((`date +%s` - t1))
+ echo "$t2 seconds elapsed for newfs option \"$i\""
+ [ $t2 -gt 1000 ] && s=111
+done
+rm -f /tmp/symlink $D
+mdconfig -d -u $mdstart
+exit $s
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(int argc __unused, char **argv)
+{
+ pid_t pid;
+ int64_t size;
+ int i, j;
+ char file[128];
+
+ size = atol(argv[1]);
+
+ pid = getpid();
+ for (j = 0; j < size; j++) {
+ sprintf(file,"p%05d.%05d", pid, j);
+ if (symlink("/mnt/not/there", file) == -1) {
+ if (errno != EINTR) {
+ warn("symlink(%s)", file);
+ printf("break out at %d, errno %d\n", j,
+ errno);
+ break;
+ }
+ }
+ }
+
+ for (i = --j; i >= 0; i--) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if (unlink(file) == -1)
+ err(3, "unlink(%s)", file);
+
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/symlink2.sh b/tools/test/stress2/misc/symlink2.sh
new file mode 100755
index 000000000000..f13065badb4a
--- /dev/null
+++ b/tools/test/stress2/misc/symlink2.sh
@@ -0,0 +1,121 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Testing problem with buffer cache inconsistency
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+D=$diskimage
+dd if=/dev/zero of=$D bs=1m count=10 status=none || exit 1
+
+odir=`pwd`
+dir=$mntpoint
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > symlink2.c
+mycc -o symlink2 -Wall symlink2.c
+rm -f symlink2.c
+cd $odir
+
+mount | grep "$mntpoint" | grep md$mdstart > /dev/null && umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+
+for i in "" "-U"; do
+ [ "$i" = "-U" -a "$newfs_flags" != "-U" ] && continue
+ echo "newfs $i /dev/md$mdstart"
+ newfs $i /dev/md$mdstart > /dev/null 2>&1
+ mount /dev/md$mdstart $mntpoint
+ mkdir $mntpoint/dir
+
+ /tmp/symlink2 $mntpoint/dir/link
+
+ ls -l $mntpoint/dir > /dev/null 2>&1
+ if [ $? -ne 0 ]; then
+ set -x
+ ls -l $mntpoint/dir
+ umount $mntpoint
+ mount /dev/md$mdstart $mntpoint
+ ls -l $mntpoint/dir
+ set +x
+ fi
+
+ umount -f $mntpoint
+done
+rm -f /tmp/symlink2 $D
+mdconfig -d -u $mdstart
+exit
+EOF
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include <err.h>
+#include <string.h>
+#include <sys/wait.h>
+
+static char *path;
+
+int
+main(int argc, char **argv)
+{
+ int i, n;
+ pid_t p;
+ char buf[128];
+
+ path = argv[1];
+ for (i = 0; i < 100; i++) {
+ if ((p = fork()) == 0) {
+ if ((n = readlink(path, buf, sizeof(buf) -1)) < 0) {
+ for (i = 0; i < 60; i++) {
+ sleep(1);
+ if ((n = readlink(path, buf, sizeof(buf) -1)) > 0) {
+ break;
+ }
+ }
+ }
+ if (n < 0)
+ err(1, "readlink(%s). %s:%d", path, __FILE__, __LINE__);
+ exit(0);
+ }
+ }
+ (void) unlink(path);
+ sleep(2);
+ if (symlink("1234", path) < 0)
+ err(1, "symlink(%s, %s)", path, "1234");
+
+ for (i = 0; i < 100; i++) {
+ if (wait(&n) == -1)
+ err(1, "wait(), %s:%d", __FILE__, __LINE__);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/symlink3.sh b/tools/test/stress2/misc/symlink3.sh
new file mode 100755
index 000000000000..16f10d1a7bec
--- /dev/null
+++ b/tools/test/stress2/misc/symlink3.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# ufs umount(1) busy test scenario
+# "umount: unmount of /mnt failed: Device busy" seen.
+# Not seen of FreeBSD HEAD.
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+umount $mntpoint
+
+export runRUNTIME=1m
+export RUNDIR=$mntpoint/stressX
+
+export LOAD=100
+export symlinkLOAD=100
+export TESTPROGS="
+testcases/fts/fts
+testcases/symlink/symlink
+"
+
+for i in `jot 10`; do
+ mount /dev/md${mdstart}$part $mntpoint
+
+ su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
+
+ n=0
+ while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ n=$((n + 1))
+ [ $n -gt 30 ] && { echo FAIL; exit 1; }
+ done
+done
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/symlink4.sh b/tools/test/stress2/misc/symlink4.sh
new file mode 100755
index 000000000000..7184b2a1ed28
--- /dev/null
+++ b/tools/test/stress2/misc/symlink4.sh
@@ -0,0 +1,125 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# Testing with links longer than 120 characters, which is not stored in the
+# inode but will be placed in its own data fragment.
+
+# "panic: softdep_deallocate_dependencies: dangling deps" seen with SU:
+# https://people.freebsd.org/~pho/stress/log/symlink4.txt
+# Fixed by r327821
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/symlink4.c
+mycc -o symlink4 -Wall -Wextra -O0 -g symlink4.c || exit 1
+rm -f symlink4.c
+cd $odir
+
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+set +e
+
+cd $mntpoint
+i=`df -ik $mntpoint | tail -1 | awk '{printf "%d\n", ($7 - 500)/2}'`
+/tmp/symlink4 $i
+s=$?
+[ -f symlink4.core -a $s -eq 0 ] &&
+ { ls -l symlink4.core; mv symlink4.core /tmp; s=1; }
+cd $odir
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1
+mdconfig -d -u $mdstart
+rm -rf $dir/symlink4
+exit $s
+
+EOF
+#include <sys/types.h>
+#include <sys/mount.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+
+#include <unistd.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int
+main(int argc, char **argv)
+{
+ int64_t size;
+ pid_t pid;
+ int i, j;
+ char file[128], link[256];
+
+ if (argc != 2)
+ errx(1, "Usage: %s <links>", argv[0]);
+ size = atol(argv[1]);
+ strcpy(link, "/mnt/not/there/");
+ for (i = 15; i < 200; i++)
+ link[i] = '1';
+ link[++i] = 0;
+
+ pid = getpid();
+ for (j = 0; j < size; j++) {
+ sprintf(file,"p%05d.%05d", pid, j);
+ if (symlink(link, file) == -1) {
+ if (errno != EINTR) {
+ warn("symlink(%s, %s)", link, file);
+ printf("break out at %d, errno %d\n", j,
+ errno);
+ break;
+ }
+ }
+ }
+
+ for (i = --j; i >= 0; i--) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if (unlink(file) == -1)
+ err(3, "unlink(%s)", file);
+
+ }
+ return (0);
+}
diff --git a/tools/test/stress2/misc/symlink5.sh b/tools/test/stress2/misc/symlink5.sh
new file mode 100755
index 000000000000..00ada39e5002
--- /dev/null
+++ b/tools/test/stress2/misc/symlink5.sh
@@ -0,0 +1,137 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Variation of symlink.sh using a larger swap backed FS.
+
+# "panic: refcount 0xfffff8093d7ed268 wraparound" seen in WiP kernel code.
+# https://people.freebsd.org/~pho/stress/log/log0024.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+dbt=`sysctl -n vfs.dirtybufthresh`
+[ $dbt -lt 1000 ] && echo "Note: vfs.dirtybufthresh = $dbt"
+
+D=$diskimage
+
+odir=`pwd`
+dir=$mntpoint
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > symlink.c
+mycc -o symlink -Wall -Wextra symlink.c || exit 1
+rm -f symlink.c
+cd $odir
+
+mount | grep -q "on $mntpoint " && umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+
+tst() {
+ local i j k
+
+ cd $dir
+ df -ik $mntpoint
+ i=`df -ik $mntpoint | tail -1 | awk '{printf "%d\n", ($7 - 500)/2}'`
+ [ $i -gt 20000 ] && i=20000
+
+ for k in `jot 3`; do
+ for j in `jot 2`; do
+ /tmp/symlink $i &
+ done
+ for j in `jot 2`; do
+ wait
+ done
+ done
+ df -ik $mntpoint | tail -1
+ cd $odir
+}
+
+s=0
+for i in "" "-U"; do
+ t1=`date +%s`
+ echo "newfs $i /dev/md$mdstart"
+ newfs $i /dev/md$mdstart > /dev/null 2>&1
+ mount /dev/md$mdstart $mntpoint
+
+ tst; s=$?
+
+ umount -f $mntpoint
+ t2=$((`date +%s` - t1))
+ echo "$t2 seconds elapsed for newfs option \"$i\""
+ [ $t2 -gt 1000 ] && s=111
+done
+rm -f /tmp/symlink
+mdconfig -d -u $mdstart
+exit $s
+EOF
+#include <sys/param.h>
+#include <sys/mount.h>
+#include <sys/sysctl.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(int argc __unused, char **argv)
+{
+ pid_t pid;
+ int64_t size;
+ int i, j;
+ char file[128];
+
+ size = atol(argv[1]);
+
+ pid = getpid();
+ for (j = 0; j < size; j++) {
+ sprintf(file,"p%05d.%05d", pid, j);
+ if (symlink("/mnt/not/there", file) == -1) {
+ if (errno != EINTR) {
+ warn("symlink(%s)", file);
+ printf("break out at %d, errno %d\n", j, errno);
+ break;
+ }
+ }
+ }
+
+ for (i = --j; i >= 0; i--) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if (unlink(file) == -1)
+ err(3, "unlink(%s)", file);
+
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/syscall4.sh b/tools/test/stress2/misc/syscall4.sh
new file mode 100755
index 000000000000..f2c5a2f38002
--- /dev/null
+++ b/tools/test/stress2/misc/syscall4.sh
@@ -0,0 +1,386 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011-2013 Peter Holm
+# 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 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.
+#
+
+# Threaded syscall(2) fuzz test inspired by the iknowthis test suite
+# by Tavis Ormandy <taviso cmpxchg8b com>
+
+# Usage: syscall4.sh [syscall number]
+# Without an argument random syscall numbers are tested.
+# With an argument only the specified syscall number is tested.
+
+# Sample problems found:
+# Thread stuck in stopprof.
+# http://people.freebsd.org/~pho/stress/log/kostik732.txt
+# Fixed by r275121.
+
+# panic: td 0xcbe1ac40 is not suspended.
+# https://people.freebsd.org/~pho/stress/log/kostik807.txt
+# Fixed by r282944.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > syscall4.c
+sed -i '' -e "s#MNTPOINT#$mntpoint#" syscall4.c
+rm -f /tmp/syscall4
+mycc -o syscall4 -Wall -Wextra -O2 -g syscall4.c -lpthread || exit 1
+rm -f syscall4.c
+
+kldstat -v | grep -q sysvmsg || $stress2tools/kldload.sh sysvmsg
+kldstat -v | grep -q sysvsem || $stress2tools/kldload.sh sysvsem
+kldstat -v | grep -q sysvshm || $stress2tools/kldload.sh sysvshm
+kldstat -v | grep -q aio || $stress2tools/kldload.sh aio
+kldstat -v | grep -q mqueuefs || $stress2tools/kldload.sh mqueuefs
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags -n md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+[ -z "$noswap" ] &&
+ daemon sh -c "(cd $odir/../testcases/swap; ./swap -t 10m -i 20 -k)" > \
+ /dev/null
+sleeptime=${sleeptime:-12}
+st=`date '+%s'`
+while [ $((`date '+%s'` - st)) -lt $((10 * sleeptime)) ]; do
+ (cd $mntpoint; /tmp/syscall4 $* 1>>stdout 2>>stderr) &
+ start=`date '+%s'`
+ while [ $((`date '+%s'` - start)) -lt $sleeptime ]; do
+ pgrep syscall4 > /dev/null || break
+ sleep .5
+ done
+ while pkill -9 syscall4; do :; done
+ wait
+ ipcs | grep nobody | awk '/^(q|m|s)/ {print " -" $1, $2}' |
+ xargs -L 1 ipcrm
+done
+while pkill -9 swap; do :; done
+while pkill -9 syscall4; do :; done
+
+for i in `jot 10`; do
+ mount | grep -q md${mdstart}$part && \
+ umount $mntpoint && mdconfig -d -u $mdstart && break
+ sleep 10
+done
+if mount | grep -q md${mdstart}$part; then
+ fstat $mntpoint
+ echo "umount $mntpoint failed"
+ exit 1
+fi
+rm -f /tmp/syscall4
+exit 0
+EOF
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/resource.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <libutil.h>
+#include <pthread.h>
+#if defined(__FreeBSD__)
+#include <pthread_np.h>
+#define __NP__
+#endif
+#include <pwd.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static int ignore[] = {
+ SYS_syscall,
+ SYS_exit,
+ SYS_fork,
+ 11, /* 11 is obsolete execv */
+ SYS_reboot,
+ SYS_vfork,
+ 109, /* 109 is old sigblock */
+ 111, /* 111 is old sigsuspend */
+ SYS_shutdown,
+ SYS___syscall,
+ 216, /* custom syscall */
+ SYS_rfork,
+ SYS_sigsuspend,
+ SYS_mac_syscall,
+ SYS_sigtimedwait,
+ SYS_sigwaitinfo,
+ SYS_thr_new, /* Page fault seen */
+};
+
+static int fd[900], fds[2], kq, socketpr[2];
+#ifndef nitems
+#define nitems(x) (sizeof((x)) / sizeof((x)[0]))
+#endif
+#define N 4096
+#define MAGIC 1664
+#define RUNTIME 120
+#define THREADS 50
+
+static uint32_t r[N];
+static int magic1, syscallno, magic2;
+
+static int
+random_int(int mi, int ma)
+{
+ return (arc4random() % (ma - mi + 1) + mi);
+}
+
+static void
+hand(int i __unused) { /* handler */
+ exit(1);
+}
+
+static unsigned long
+makearg(void)
+{
+ unsigned int i;
+ unsigned long val;
+
+ val = arc4random();
+ i = arc4random() % 100;
+ if (i < 20)
+ val = val & 0xff;
+ if (i >= 20 && i < 40)
+ val = val & 0xffff;
+ if (i >= 40 && i < 60)
+ val = (unsigned long)(r) | (val & 0xffff);
+#if defined(__LP64__)
+ if (i >= 60) {
+ val = (val << 32) | arc4random();
+ if (i > 80)
+ val = val & 0x00007fffffffffffUL;
+ }
+#endif
+
+ return(val);
+}
+
+static void *
+test(void *arg __unused)
+{
+ FTS *fts;
+ FTSENT *p;
+ time_t start;
+ int ftsoptions, i, numfiles;
+ char *args[] = {
+ "/dev",
+ "/proc",
+ "MNTPOINT",
+ "mnt2",
+ ".",
+ NULL,
+ };
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+ numfiles = 0;
+ ftsoptions = FTS_PHYSICAL;
+ start = time(NULL);
+ while (time(NULL) - start < 2) {
+ for (i = 0; i < N; i++)
+ r[i] = arc4random();
+
+ if (pipe(fds) == -1)
+ err(1, "pipe()");
+ if (socketpair(PF_UNIX, SOCK_SEQPACKET, 0, socketpr) == -1)
+ err(1, "socketpair()");
+ kq = kqueue();
+
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ i = 0;
+ while ((p = fts_read(fts)) != NULL) {
+ if (fd[i] > 0)
+ close(fd[i]);
+ if ((fd[i] = open(p->fts_path, O_RDWR)) == -1)
+ if ((fd[i] = open(p->fts_path, O_WRONLY)) ==
+ -1)
+ if ((fd[i] = open(p->fts_path,
+ O_RDONLY)) == -1)
+ continue;
+ i++;
+ i = i % nitems(fd);
+ if (numfiles++ < 10) {
+ fprintf(stderr, "%d: pts_path = %s\n",
+ numfiles, p->fts_path);
+ }
+ }
+
+ if (fts_close(fts) == -1)
+ warn("fts_close()");
+ sleep(1);
+ close(socketpr[0]);
+ close(socketpr[1]);
+ close(fds[0]);
+ close(fds[1]);
+ close(kq);
+ }
+ return(NULL);
+}
+
+static void *
+calls(void *arg __unused)
+{
+ time_t start;
+ int i, j, num;
+ unsigned long arg1, arg2, arg3, arg4, arg5, arg6, arg7;
+
+#ifdef __NP__
+ pthread_set_name_np(pthread_self(), __func__);
+#endif
+ start = time(NULL);
+ for (i = 0; time(NULL) - start < 10; i++) {
+ num = syscallno;
+ while (num == 0) {
+ num = random_int(0, SYS_MAXSYSCALL);
+ for (j = 0; j < (int)nitems(ignore); j++)
+ if (num == ignore[j]) {
+ num = 0;
+ break;
+ }
+ }
+ arg1 = makearg();
+ arg2 = makearg();
+ arg3 = makearg();
+ arg4 = makearg();
+ arg5 = makearg();
+ arg6 = makearg();
+ arg7 = makearg();
+
+#if 0 /* Debug mode */
+ fprintf(stderr, "%2d : syscall(%3d, %lx, %lx, %lx, %lx, %lx,"
+ " %lx, %lx)\n",
+ i, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
+ sleep(2);
+#endif
+ alarm(1);
+ syscall(num, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
+ num = 0;
+ if (magic1 != MAGIC || magic2 != MAGIC)
+ exit(1);
+ }
+
+ return (NULL);
+}
+
+int
+main(int argc, char **argv)
+{
+ struct passwd *pw;
+ struct rlimit limit;
+ pthread_t rp, cp[THREADS];
+ time_t start;
+ int e, j;
+
+ magic1 = magic2 = MAGIC;
+ if ((pw = getpwnam("nobody")) == NULL)
+ err(1, "failed to resolve nobody");
+
+ if (getenv("USE_ROOT") && argc == 2)
+ fprintf(stderr, "Running syscall4 as root for %s.\n",
+ argv[1]);
+ else {
+ if (setgroups(1, &pw->pw_gid) ||
+ setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
+ seteuid(pw->pw_uid) || setuid(pw->pw_uid))
+ err(1, "Can't drop privileges to \"nobody\"");
+ endpwent();
+ }
+
+ limit.rlim_cur = limit.rlim_max = 1000;
+#if defined(RLIMIT_NPTS)
+ if (setrlimit(RLIMIT_NPTS, &limit) < 0)
+ err(1, "setrlimit");
+#endif
+
+ signal(SIGALRM, hand);
+ signal(SIGILL, hand);
+ signal(SIGFPE, hand);
+ signal(SIGSEGV, hand);
+ signal(SIGBUS, hand);
+ signal(SIGURG, hand);
+ signal(SIGSYS, hand);
+ signal(SIGTRAP, hand);
+
+ if (argc > 2) {
+ fprintf(stderr, "usage: %s [syscall-num]\n", argv[0]);
+ exit(1);
+ }
+ if (argc == 2) {
+ syscallno = atoi(argv[1]);
+ for (j = 0; j < (int)nitems(ignore); j++)
+ if (syscallno == ignore[j])
+ errx(0, "syscall #%d is on the ignore list.",
+ syscallno);
+ }
+
+ if (daemon(1, 1) == -1)
+ err(1, "daemon()");
+
+ system("touch aaa bbb ccc; mkdir -p ddd");
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ if (fork() == 0) {
+ if ((e = pthread_create(&rp, NULL, test, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ usleep(1000);
+ for (j = 0; j < THREADS; j++)
+ if ((e = pthread_create(&cp[j], NULL, calls,
+ NULL)) != 0)
+ errc(1, e, "pthread_create");
+ for (j = 0; j < THREADS; j++)
+ pthread_join(cp[j], NULL);
+
+ if ((e = pthread_kill(rp, SIGINT)) != 0)
+ errc(1, e, "pthread_kill");
+ exit(0);
+ }
+ wait(NULL);
+ usleep(10000);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/syscall5.sh b/tools/test/stress2/misc/syscall5.sh
new file mode 100755
index 000000000000..f953b63c220d
--- /dev/null
+++ b/tools/test/stress2/misc/syscall5.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm
+# 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 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.
+#
+
+# Call syscall4.sh with syscall number as argument.
+# Only run the last ~16 syscalls, if no argument is specified.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+syscall=`grep SYS_MAXSYSCALL /usr/include/sys/syscall.h | awk '{print $NF}'`
+syscall=$((syscall - 1))
+esyscall=0
+
+last=/tmp/syscall5.last
+log=/tmp/syscall5.log
+
+while getopts ae:nrs:t:u flag; do
+ case "$flag" in
+ a) all=1 ;;
+ e) esyscall="$OPTARG" ;;
+ n) export noswap=1 ;;
+ r) [ -h $last ] &&
+ syscall=`ls -l $last | awk '{print $NF}'`
+ syscall=$((syscall - 1))
+ ;;
+ s) syscall="$OPTARG" ;;
+ t) sleeptime="$OPTARG"
+ export sleeptime=$((sleeptime / 10)) ;; # used in syscall4.sh
+ u) unnamed=1 ;;
+ *) echo "Usage $0 [-a] [-n] [-r] [-s number] [-e number] "\
+ "[-t seconds] [-u]"
+ return 1 ;;
+ esac
+done
+[ $syscall -gt $esyscall ] ||
+ { echo "$syscall must be greater than $esyscall"; exit 1; }
+
+# syscalls with known issues:
+broken="
+swapcontext
+pdfork
+rfork
+pselect
+"
+
+rm -f $log
+n=$syscall
+start=`date '+%s'`
+while [ $n -gt 0 ]; do
+ ps -lUnobody | grep syscall4 | awk '{print $2}' | xargs kill
+ ln -fs $n $last
+ name=`grep -w "$n$" /usr/include/sys/syscall.h | awk '{print $2}' |
+ sed 's/SYS_//'`
+ [ -z "$name" ] && name="unknown"
+ rm -f /tmp/syscall5.name
+ [ -x ../tools/exclude_syscall.sh ] &&
+ ../tools/exclude_syscall.sh $n &&
+ { n=$((n - 1)); continue; }
+ [ "$name" = "unknown" ] && [ -f /tmp/syscall5.name ] &&
+ name=`cat /tmp/syscall5.name`
+ [ -z "$name" -a -n "$unnamed" ] &&
+ { n=$((n - 1)); continue; }
+ echo "`date '+%T'` syscall $n ($name)"
+ echo "`date '+%T'` syscall $n ($name)" >> $log
+ printf "`date '+%T'` syscall $n ($name)\r\n" > /dev/console
+ sync; sleep 1
+ echo "$broken" | grep -qw "$name" ||
+ ./syscall4.sh $n || break
+ [ $n -le $esyscall ] && break
+ n=$((n - 1))
+ [ -z "$all" -a `date '+%s'` -gt $((start + 1800)) ] && break
+done
+rm -f $log $last
diff --git a/tools/test/stress2/misc/syscall6.sh b/tools/test/stress2/misc/syscall6.sh
new file mode 100755
index 000000000000..d5b5db787519
--- /dev/null
+++ b/tools/test/stress2/misc/syscall6.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Run selected syscall fuzz tests as root.
+# https://people.freebsd.org/~pho/stress/log/syscall6.txt
+# Fixed by r336149
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+[ -f /usr/include/sys/syscall.h ] || exit 0
+
+list="fdatasync mount ktrace"
+
+for i in $list; do
+ num=`grep SYS_$i /usr/include/sys/syscall.h | tail -1 | awk '{print $NF}'`
+ [ -z "$num" ] && { echo "Unknown syscall $i"; continue; }
+ echo "Testing $i ($num)"
+ USE_ROOT=1 sleeptime=18 ../misc/syscall4.sh $num
+done
+exit 0
diff --git a/tools/test/stress2/misc/sysctl.sh b/tools/test/stress2/misc/sysctl.sh
new file mode 100755
index 000000000000..cab6b214f4fe
--- /dev/null
+++ b/tools/test/stress2/misc/sysctl.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# sysctl(3) regression test by using pstat and vmstat
+
+for i in `jot 100`; do
+ pstat -T | grep -q files || echo "pstat -T Failed"
+ pstat -f | grep -q inode || echo "pstat -f Failed"
+ pstat -s | grep -q /dev/ || echo "pstat -s Failed"
+ pstat -t | grep -q tty || echo "pstat -t Failed"
+
+ [ `vmstat -P | wc -l` -eq 3 ] || echo "vmstat -P Failed"
+ vmstat -f | grep -q forks || echo "vmstat -f Failed"
+ vmstat -i | grep -q cpu || echo "vmstat -i Failed"
+ vmstat -m | grep -q lockf || echo "vmstat -m Failed"
+ vmstat -s | grep -q inter || echo "vmstat -s Failed"
+ vmstat -z | grep -q dinode || echo "vmstat -z Failed"
+done
+exit 0
diff --git a/tools/test/stress2/misc/sysctl2.sh b/tools/test/stress2/misc/sysctl2.sh
new file mode 100755
index 000000000000..3b69b13981d0
--- /dev/null
+++ b/tools/test/stress2/misc/sysctl2.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# sysctl(8) test
+
+. ../default.cfg
+
+export runRUNTIME=5m
+pkill -9 -f run.sh
+daemon sh -c '(cd ../; ./run.sh > /dev/null 2>&1)'
+
+while pgrep -fq run.sh; do
+ for i in `jot 3`; do
+ sysctl -a > /dev/null 2>&1 &
+ done
+ wait
+done
+exit 0
diff --git a/tools/test/stress2/misc/sysctl3.sh b/tools/test/stress2/misc/sysctl3.sh
new file mode 100755
index 000000000000..ebc747915c13
--- /dev/null
+++ b/tools/test/stress2/misc/sysctl3.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# sysctl(8) test.
+
+. ../default.cfg
+
+export runRUNTIME=10m
+export TESTPROGS="
+testcases/swap/swap
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rw/rw
+"
+export swapLOAD=100
+
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > /dev/null 2>&1 &
+pid=$!
+
+while kill -0 $pid > /dev/null 2>&1; do
+ pids=
+ for i in `jot 3`; do
+ sysctl -a > /dev/null 2>&1 &
+ pids="$pids $!"
+ done
+ for p in $pids; do
+ wait $p
+ done
+done
+wait $pid
+exit 0
diff --git a/tools/test/stress2/misc/sysctl4.sh b/tools/test/stress2/misc/sysctl4.sh
new file mode 100755
index 000000000000..72e4a4ea3ee8
--- /dev/null
+++ b/tools/test/stress2/misc/sysctl4.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Test with short lived threads.
+
+. ../default.cfg
+
+(cd ../testcases/thr2; ./thr2 -t 1h -i 20 -l 100 > /dev/null 2>&1) &
+pid=$!
+
+[ -n "`sysctl -i kern.proc.all_stacks`" ] && all_stacks=1 # Custom OID
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 180 ]; do
+ pids=""
+ for j in `jot 3`; do
+ if [ $all_stacks ]; then
+ sysctl kern.proc.all_stacks > /dev/null &
+ else
+ ps -lH > /dev/null &
+ fi
+ pids="$pids $!"
+ done
+ for j in $pids; do
+ wait $j
+ done
+done
+kill $pid
+pkill thr2
+exit 0
diff --git a/tools/test/stress2/misc/systrace.sh b/tools/test/stress2/misc/systrace.sh
new file mode 100755
index 000000000000..aa7b8426dddf
--- /dev/null
+++ b/tools/test/stress2/misc/systrace.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+# 20200224 13:05:04 all (1/1): systrace.sh
+# [ thread pid 64171 tid 100708 ]
+# Stopped at sdt_modevent+0x1c1: int $3
+# db>
+
+# markj@ wrote:
+# I think I see the problem now. It should happen if you:
+# 1. load systrace.ko
+# 2. enable some system call probes, and disable them again
+# 3. unload systrace.ko
+# 4. load some unrelated module
+# 5. goto 1
+#
+# In particular, if systrace.ko is loaded at different addresses each
+# time, we'll end up calling a stale function pointer.
+
+# https://people.freebsd.org/~pho/stress/log/mark137.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+dtrace -n 'dtrace:::BEGIN { exit(0); }' > /dev/null 2>&1 || exit 0
+for i in `jot 2`; do
+ sleep 4 & pid=$!
+ dtrace -w -n 'syscall::*read:entry,syscall::*write:entry {\
+ @rw[execname,probefunc] = count(); }' -p $pid > /dev/null 2>&1
+ wait $pid
+ kldstat | grep -q dtraceall.ko && kldunload dtraceall.ko
+
+ mount -t tmpfs dummy $mntpoint && umount $mntpoint
+done
+kldstat | grep -q tmpfs && kldunload tmpfs.ko
+exit 0
diff --git a/tools/test/stress2/misc/systrace2.sh b/tools/test/stress2/misc/systrace2.sh
new file mode 100755
index 000000000000..a8c4d05106a7
--- /dev/null
+++ b/tools/test/stress2/misc/systrace2.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+# A variation of systrace.sh
+# "Fatal trap 12: page fault while in kernel mode" seen:
+# https://people.freebsd.org/~pho/stress/log/mark142.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+dtrace -n 'dtrace:::BEGIN { exit(0); }' > /dev/null 2>&1 || exit 0
+
+echo "gvim or top should be running at this point"
+for i in `jot 2`; do
+ sleep 4 & pid=$!
+ dtrace -w -n 'fbt::kern_ioctl:entry {\
+ @io[execname,probefunc] = count(); }' -p $pid > /dev/null 2>&1
+ wait $pid
+ kldstat | grep -q dtraceall.ko && kldunload dtraceall.ko
+ mount -t tmpfs dummy $mntpoint && umount $mntpoint
+done
+kldstat | grep -q tmpfs && kldunload tmpfs.ko
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller1.sh b/tools/test/stress2/misc/syzkaller1.sh
new file mode 100755
index 000000000000..f9f6662d5a0a
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller1.sh
@@ -0,0 +1,246 @@
+#!/bin/sh
+
+# panic: refcount 0xfffff8001dd678e8 wraparound
+# cpuid = 4
+# time = 1580840365
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe00e56517b0
+# vpanic() at vpanic+0x185/frame 0xfffffe00e5651810
+# panic() at panic+0x43/frame 0xfffffe00e5651870
+# _refcount_update_saturated() at _refcount_update_saturated+0x15/frame 0xfffffe00e5651880
+# refcount_release_last() at refcount_release_last+0x7c/frame 0xfffffe00e56518a0
+# closef() at closef+0x1f7/frame 0xfffffe00e5651930
+# fdescfree_fds() at fdescfree_fds+0x3c/frame 0xfffffe00e5651980
+# fdescfree() at fdescfree+0x456/frame 0xfffffe00e5651a40
+# exit1() at exit1+0x4f3/frame 0xfffffe00e5651ab0
+# sys_sys_exit() at sys_sys_exit+0xd/frame 0xfffffe00e5651ac0
+# amd64_syscall() at amd64_syscall+0x2f1/frame 0xfffffe00e5651bf0
+# fast_syscall_common() at fast_syscall_common+0x101/frame 0xfffffe00e5651bf0
+# --- syscall (1, FreeBSD ELF64, sys_sys_exit), rip = 0x8003d53ba, rsp = 0x7fffffffe9f8, rbp = 0x7fffffffea10 ---
+
+# Fixed by r357554
+
+. ../default.cfg
+cat > /tmp/syzkaller1.c <<EOF
+
+// https://syzkaller.appspot.com/bug?id=29c002256f2ce0d521d1ce007c74e456a37495a8
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <time.h>
+#include <unistd.h>
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void thread_start(void* (*fn)(void*), void* arg)
+{
+ pthread_t th;
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ pthread_attr_setstacksize(&attr, 128 << 10);
+ int i;
+ for (i = 0; i < 100; i++) {
+ if (pthread_create(&th, &attr, fn, arg) == 0) {
+ pthread_attr_destroy(&attr);
+ return;
+ }
+ if (errno == EAGAIN) {
+ usleep(50);
+ continue;
+ }
+ break;
+ }
+ exit(1);
+}
+
+typedef struct {
+ pthread_mutex_t mu;
+ pthread_cond_t cv;
+ int state;
+} event_t;
+
+static void event_init(event_t* ev)
+{
+ if (pthread_mutex_init(&ev->mu, 0))
+ exit(1);
+ if (pthread_cond_init(&ev->cv, 0))
+ exit(1);
+ ev->state = 0;
+}
+
+static void event_reset(event_t* ev)
+{
+ ev->state = 0;
+}
+
+static void event_set(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ if (ev->state)
+ exit(1);
+ ev->state = 1;
+ pthread_mutex_unlock(&ev->mu);
+ pthread_cond_broadcast(&ev->cv);
+}
+
+static void event_wait(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ while (!ev->state)
+ pthread_cond_wait(&ev->cv, &ev->mu);
+ pthread_mutex_unlock(&ev->mu);
+}
+
+static int event_isset(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+static int event_timedwait(event_t* ev, uint64_t timeout)
+{
+ uint64_t start = current_time_ms();
+ uint64_t now = start;
+ pthread_mutex_lock(&ev->mu);
+ for (;;) {
+ if (ev->state)
+ break;
+ uint64_t remain = timeout - (now - start);
+ struct timespec ts;
+ ts.tv_sec = remain / 1000;
+ ts.tv_nsec = (remain % 1000) * 1000 * 1000;
+ pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
+ now = current_time_ms();
+ if (now - start > timeout)
+ break;
+ }
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+struct thread_t {
+ int created, call;
+ event_t ready, done;
+};
+
+static struct thread_t threads[16];
+static void execute_call(int call);
+static int running;
+
+static void* thr(void* arg)
+{
+ struct thread_t* th = (struct thread_t*)arg;
+ for (;;) {
+ event_wait(&th->ready);
+ event_reset(&th->ready);
+ execute_call(th->call);
+ __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->done);
+ }
+ return 0;
+}
+
+static void loop(void)
+{
+ int i, call, thread;
+ for (call = 0; call < 7; call++) {
+ for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
+ thread++) {
+ struct thread_t* th = &threads[thread];
+ if (!th->created) {
+ th->created = 1;
+ event_init(&th->ready);
+ event_init(&th->done);
+ event_set(&th->done);
+ thread_start(thr, th);
+ }
+ if (!event_isset(&th->done))
+ continue;
+ event_reset(&th->done);
+ th->call = call;
+ __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->ready);
+ event_timedwait(&th->done, 45);
+ break;
+ }
+ }
+ for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
+ sleep_ms(1);
+}
+
+uint64_t r[3] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff};
+
+void execute_call(int call)
+{
+ intptr_t res;
+ switch (call) {
+ case 0:
+ memcpy((void*)0x20000000, "./file0\000", 8);
+ res = syscall(SYS_open, 0x20000000ul, 0x40000400000002c2ul, 0ul);
+ if (res != -1)
+ r[0] = res;
+ break;
+ case 1:
+ res = syscall(SYS_socket, 0x1cul, 1ul, 0ul);
+ if (res != -1)
+ r[1] = res;
+ break;
+ case 2:
+ syscall(SYS_listen, r[1], 0);
+ break;
+ case 3:
+ syscall(SYS_close, r[0]);
+ break;
+ case 4:
+ syscall(SYS_accept, r[1], 0ul, 0ul);
+ break;
+ case 5:
+ res = syscall(SYS_dup2, r[0], r[1]);
+ if (res != -1)
+ r[2] = res;
+ break;
+ case 6:
+ syscall(SYS_mmap, 0x20ffd000ul, 0x1000ul, 0ul, 0x10ul, r[2], 0ul);
+ break;
+ }
+}
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul);
+ loop();
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller1 -Wall -Wextra -O2 /tmp/syzkaller1.c -lpthread ||
+ exit 1
+
+(cd /tmp; ./syzkaller1)
+
+rm -f /tmp/syzkaller1 /tmp/syzkaller1.c /tmp/syzkaller1.core
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller10.sh b/tools/test/stress2/misc/syzkaller10.sh
new file mode 100755
index 000000000000..7d29f0678949
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller10.sh
@@ -0,0 +1,93 @@
+#!/bin/sh
+
+# panic: sbsndptr_noadv: sb_mb is NULL
+# cpuid = 0
+# time = 1586867804
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x47/frame 0xfffffe0025a68360
+# vpanic() at vpanic+0x1c7/frame 0xfffffe0025a683c0
+# panic() at panic+0x43/frame 0xfffffe0025a68420
+# sbsndptr_noadv() at sbsndptr_noadv+0xae/frame 0xfffffe0025a68460
+# rack_output() at rack_output+0x51f5/frame 0xfffffe0025a68700
+# tcp_usr_send() at tcp_usr_send+0x5c7/frame 0xfffffe0025a687e0
+# sosend_generic() at sosend_generic+0x8fd/frame 0xfffffe0025a688e0
+# sosend() at sosend+0xc6/frame 0xfffffe0025a68950
+# kern_sendit() at kern_sendit+0x33d/frame 0xfffffe0025a68a00
+# sendit() at sendit+0x224/frame 0xfffffe0025a68a60
+# sys_sendto() at sys_sendto+0x5c/frame 0xfffffe0025a68ac0
+# amd64_syscall() at amd64_syscall+0x262/frame 0xfffffe0025a68bf0
+
+[ `uname -p` = "i386" ] && exit 0
+
+. ../default.cfg
+cat > /tmp/syzkaller10.c <<EOF
+// https://syzkaller.appspot.com/bug?id=048f650e99696f881872a285cef0e3b9bd4f4e25
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+
+#define _GNU_SOURCE
+
+#include <pwd.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul);
+ intptr_t res = 0;
+ res = syscall(SYS_socket, 2ul, 1ul, 0);
+ if (res != -1)
+ r[0] = res;
+ memcpy((void*)0x20000080,
+ "rack\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
+ "\000\000\000\000\000\000\000\000\000\000\000\000",
+ 32);
+ *(uint32_t*)0x200000a0 = 0;
+ syscall(SYS_setsockopt, r[0], 6, 0x2000, 0x20000080ul, 0x24ul);
+ *(uint8_t*)0x20000000 = 0x2c;
+ *(uint8_t*)0x20000001 = 2;
+ *(uint16_t*)0x20000002 = htobe16(0x4e21);
+ *(uint32_t*)0x20000004 = htobe32(-1);
+ *(uint8_t*)0x20000008 = 0;
+ *(uint8_t*)0x20000009 = 0;
+ *(uint8_t*)0x2000000a = 0;
+ *(uint8_t*)0x2000000b = 0;
+ *(uint8_t*)0x2000000c = 0;
+ *(uint8_t*)0x2000000d = 0;
+ *(uint8_t*)0x2000000e = 0;
+ *(uint8_t*)0x2000000f = 0;
+ syscall(SYS_sendto, r[0], 0ul, 0ul, 0ul, 0x20000000ul, 0x10ul);
+ *(uint8_t*)0x20000040 = 0x10;
+ *(uint8_t*)0x20000041 = 2;
+ *(uint16_t*)0x20000042 = htobe16(0x4e21);
+ *(uint32_t*)0x20000044 = htobe32(0);
+ *(uint8_t*)0x20000048 = 0;
+ *(uint8_t*)0x20000049 = 0;
+ *(uint8_t*)0x2000004a = 0;
+ *(uint8_t*)0x2000004b = 0;
+ *(uint8_t*)0x2000004c = 0;
+ *(uint8_t*)0x2000004d = 0;
+ *(uint8_t*)0x2000004e = 0;
+ *(uint8_t*)0x2000004f = 0;
+ syscall(SYS_sendto, r[0], 0ul, 0ul, 0x60005ul, 0x20000040ul, 0x10ul);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller10 -Wall -Wextra -O2 /tmp/syzkaller10.c -lpthread ||
+ exit 1
+
+(cd /tmp; ./syzkaller10) &
+sleep 60
+pkill -9 syzkaller10
+wait
+
+rm -f /tmp/syzkaller10 /tmp/syzkaller10.c /tmp/syzkaller10.core
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller11.sh b/tools/test/stress2/misc/syzkaller11.sh
new file mode 100755
index 000000000000..9586092cd5b3
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller11.sh
@@ -0,0 +1,299 @@
+#!/bin/sh
+
+# panic: mtx_lock() of spin mutex (null) @ ../../../netinet/tcp_output.c:350
+# cpuid = 1
+# time = 1587276361
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe01388956f0
+# vpanic() at vpanic+0x182/frame 0xfffffe0138895740
+# panic() at panic+0x43/frame 0xfffffe01388957a0
+# __mtx_lock_flags() at __mtx_lock_flags+0x13c/frame 0xfffffe01388957f0
+# tcp_output() at tcp_output+0x34e/frame 0xfffffe0138895980
+# tcp_usr_connect() at tcp_usr_connect+0x123/frame 0xfffffe01388959e0
+# soconnectat() at soconnectat+0xd0/frame 0xfffffe0138895a30
+# kern_connectat() at kern_connectat+0xe2/frame 0xfffffe0138895a90
+# sys_connect() at sys_connect+0x75/frame 0xfffffe0138895ad0
+# amd64_syscall() at amd64_syscall+0x159/frame 0xfffffe0138895bf0
+# fast_syscall_common() at fast_syscall_common+0x101/frame 0xfffffe0138895bf0
+
+# Reproduced on r358885
+
+[ `uname -p` = "i386" ] && exit 0
+
+. ../default.cfg
+cat > /tmp/syzkaller11.c <<EOF
+// https://syzkaller.appspot.com/bug?id=f81a41d30e772445b13de6dcf07fc1afaf122a61
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <errno.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void thread_start(void* (*fn)(void*), void* arg)
+{
+ pthread_t th;
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ pthread_attr_setstacksize(&attr, 128 << 10);
+ int i;
+ for (i = 0; i < 100; i++) {
+ if (pthread_create(&th, &attr, fn, arg) == 0) {
+ pthread_attr_destroy(&attr);
+ return;
+ }
+ if (errno == EAGAIN) {
+ usleep(50);
+ continue;
+ }
+ break;
+ }
+ exit(1);
+}
+
+typedef struct {
+ pthread_mutex_t mu;
+ pthread_cond_t cv;
+ int state;
+} event_t;
+
+static void event_init(event_t* ev)
+{
+ if (pthread_mutex_init(&ev->mu, 0))
+ exit(1);
+ if (pthread_cond_init(&ev->cv, 0))
+ exit(1);
+ ev->state = 0;
+}
+
+static void event_reset(event_t* ev)
+{
+ ev->state = 0;
+}
+
+static void event_set(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ if (ev->state)
+ exit(1);
+ ev->state = 1;
+ pthread_mutex_unlock(&ev->mu);
+ pthread_cond_broadcast(&ev->cv);
+}
+
+static void event_wait(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ while (!ev->state)
+ pthread_cond_wait(&ev->cv, &ev->mu);
+ pthread_mutex_unlock(&ev->mu);
+}
+
+static int event_isset(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+static int event_timedwait(event_t* ev, uint64_t timeout)
+{
+ uint64_t start = current_time_ms();
+ uint64_t now = start;
+ pthread_mutex_lock(&ev->mu);
+ for (;;) {
+ if (ev->state)
+ break;
+ uint64_t remain = timeout - (now - start);
+ struct timespec ts;
+ ts.tv_sec = remain / 1000;
+ ts.tv_nsec = (remain % 1000) * 1000 * 1000;
+ pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
+ now = current_time_ms();
+ if (now - start > timeout)
+ break;
+ }
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+struct thread_t {
+ int created, call;
+ event_t ready, done;
+};
+
+static struct thread_t threads[16];
+static void execute_call(int call);
+static int running;
+
+static void* thr(void* arg)
+{
+ struct thread_t* th = (struct thread_t*)arg;
+ for (;;) {
+ event_wait(&th->ready);
+ event_reset(&th->ready);
+ execute_call(th->call);
+ __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->done);
+ }
+ return 0;
+}
+
+static void execute_one(void)
+{
+ int i, call, thread;
+ int collide = 0;
+again:
+ for (call = 0; call < 4; call++) {
+ for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
+ thread++) {
+ struct thread_t* th = &threads[thread];
+ if (!th->created) {
+ th->created = 1;
+ event_init(&th->ready);
+ event_init(&th->done);
+ event_set(&th->done);
+ thread_start(thr, th);
+ }
+ if (!event_isset(&th->done))
+ continue;
+ event_reset(&th->done);
+ th->call = call;
+ __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->ready);
+ if (collide && (call % 2) == 0)
+ break;
+ event_timedwait(&th->done, 45);
+ break;
+ }
+ }
+ for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
+ sleep_ms(1);
+ if (!collide) {
+ collide = 1;
+ goto again;
+ }
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter;
+ for (iter = 0;; iter++) {
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ }
+}
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+void execute_call(int call)
+{
+ intptr_t res;
+ switch (call) {
+ case 0:
+ res = syscall(SYS_socket, 2ul, 1ul, 0);
+ if (res != -1)
+ r[0] = res;
+ break;
+ case 1:
+ memcpy((void*)0x20000040,
+ "bbr\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
+ "\000\000\000\000\000\000\000\000\000\000\000\000\000",
+ 32);
+ *(uint32_t*)0x20000060 = 0;
+ syscall(SYS_setsockopt, r[0], 6, 0x2000, 0x20000040ul, 0x24ul);
+ break;
+ case 2:
+ *(uint8_t*)0x20000180 = 0x10;
+ *(uint8_t*)0x20000181 = 2;
+ *(uint16_t*)0x20000182 = htobe16(0x4e22);
+ *(uint32_t*)0x20000184 = htobe32(0xfff);
+ *(uint8_t*)0x20000188 = 0;
+ *(uint8_t*)0x20000189 = 0;
+ *(uint8_t*)0x2000018a = 0;
+ *(uint8_t*)0x2000018b = 0;
+ *(uint8_t*)0x2000018c = 0;
+ *(uint8_t*)0x2000018d = 0;
+ *(uint8_t*)0x2000018e = 0;
+ *(uint8_t*)0x2000018f = 0;
+ syscall(SYS_connect, r[0], 0x20000180ul, 0x10ul);
+ break;
+ case 3:
+ syscall(SYS_listen, r[0], 0);
+ break;
+ }
+}
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ loop();
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller11 -Wall -Wextra -O2 /tmp/syzkaller11.c -lpthread ||
+ exit 1
+
+(cd /tmp; ./syzkaller11) &
+sleep 60
+pkill -9 syzkaller11
+wait
+
+rm -f /tmp/syzkaller11 /tmp/syzkaller11.c /tmp/syzkaller11.core
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller12.sh b/tools/test/stress2/misc/syzkaller12.sh
new file mode 100755
index 000000000000..dc026dc348bc
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller12.sh
@@ -0,0 +1,176 @@
+#!/bin/sh
+
+# Fatal trap 12: page fault while in kernel mode
+# cpuid = 21; apic id = 29
+# fault virtual address = 0x5eb9977d
+# fault code = supervisor read data, page not present
+# instruction pointer = 0x20:0xffffffff80d8f181
+# stack pointer = 0x28:0xfffffe0126709300
+# frame pointer = 0x28:0xfffffe0126709750
+# code segment = base 0x0, limit 0xfffff, type 0x1b
+# = DPL 0, pres 1, long 1, def32 0, gran 1
+# processor eflags = interrupt enabled, resume, IOPL = 0
+# current process = 12 (swi1: netisr 0)
+# trap number = 12
+# panic: page fault
+# cpuid = 21
+# time = 1589220309
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe0126708fb0
+# vpanic() at vpanic+0x182/frame 0xfffffe0126709000
+# panic() at panic+0x43/frame 0xfffffe0126709060
+# trap_fatal() at trap_fatal+0x387/frame 0xfffffe01267090c0
+# trap_pfault() at trap_pfault+0x99/frame 0xfffffe0126709120
+# trap() at trap+0x2a5/frame 0xfffffe0126709230
+# calltrap() at calltrap+0x8/frame 0xfffffe0126709230
+# --- trap 0xc, rip = 0xffffffff80d8f181, rsp = 0xfffffe0126709300, rbp = 0xfffffe0126709750 ---
+# sctp_process_control() at sctp_process_control+0x1351/frame 0xfffffe0126709750
+# sctp_common_input_processing() at sctp_common_input_processing+0x4f1/frame 0xfffffe01267098c0
+# sctp6_input_with_port() at sctp6_input_with_port+0x22c/frame 0xfffffe01267099b0
+# sctp6_input() at sctp6_input+0xb/frame 0xfffffe01267099c0
+# ip6_input() at ip6_input+0xe89/frame 0xfffffe0126709aa0
+# swi_net() at swi_net+0x1a1/frame 0xfffffe0126709b20
+# ithread_loop() at ithread_loop+0x279/frame 0xfffffe0126709bb0
+# fork_exit() at fork_exit+0x80/frame 0xfffffe0126709bf0
+# fork_trampoline() at fork_trampoline+0xe/frame 0xfffffe0126709bf0
+# --- trap 0, rip = 0, rsp = 0, rbp = 0 ---
+# KDB: enter: panic
+# [ thread pid 12 tid 100083 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10ca9a6(%rip)
+# db>
+
+# Reproduced on r360902
+# Fixed by r360942
+
+[ `uname -p` = "i386" ] && exit 0
+
+. ../default.cfg
+kldstat -v | grep -q sctp || kldload sctp.ko
+cat > /tmp/syzkaller12.c <<EOF
+// https://syzkaller.appspot.com/bug?id=978bd3a9b4c66b88dd523a3b8b32a1b0aa47ed83
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <pwd.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter;
+ for (iter = 0;; iter++) {
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ }
+}
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+void execute_one(void)
+{
+ intptr_t res = 0;
+ res = syscall(SYS_socket, 0x1cul, 1ul, 0x84);
+ if (res != -1)
+ r[0] = res;
+ *(uint8_t*)0x20000000 = 0x1c;
+ *(uint8_t*)0x20000001 = 0x1c;
+ *(uint16_t*)0x20000002 = htobe16(0x4e22);
+ *(uint32_t*)0x20000004 = 0;
+ *(uint8_t*)0x20000008 = 0;
+ *(uint8_t*)0x20000009 = 0;
+ *(uint8_t*)0x2000000a = 0;
+ *(uint8_t*)0x2000000b = 0;
+ *(uint8_t*)0x2000000c = 0;
+ *(uint8_t*)0x2000000d = 0;
+ *(uint8_t*)0x2000000e = 0;
+ *(uint8_t*)0x2000000f = 0;
+ *(uint8_t*)0x20000010 = 0;
+ *(uint8_t*)0x20000011 = 0;
+ *(uint8_t*)0x20000012 = 0;
+ *(uint8_t*)0x20000013 = 0;
+ *(uint8_t*)0x20000014 = 0;
+ *(uint8_t*)0x20000015 = 0;
+ *(uint8_t*)0x20000016 = 0;
+ *(uint8_t*)0x20000017 = 0;
+ *(uint32_t*)0x20000018 = 0;
+ syscall(SYS_bind, r[0], 0x20000000ul, 0x1cul);
+ *(uint8_t*)0x20000180 = 0x5f;
+ *(uint8_t*)0x20000181 = 0x1c;
+ *(uint16_t*)0x20000182 = htobe16(0x4e22);
+ *(uint32_t*)0x20000184 = 0;
+ *(uint64_t*)0x20000188 = htobe64(0);
+ *(uint64_t*)0x20000190 = htobe64(1);
+ *(uint32_t*)0x20000198 = 0;
+ syscall(SYS_connect, r[0], 0x20000180ul, 0x1cul);
+}
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ loop();
+ return 0;
+}
+// https://syzkaller.a
+EOF
+mycc -o /tmp/syzkaller12 -Wall -Wextra -O2 /tmp/syzkaller12.c -lpthread ||
+ exit 1
+
+(cd /tmp; ./syzkaller12) &
+sleep 60
+pkill -9 syzkaller12
+wait
+
+rm -f /tmp/syzkaller12 /tmp/syzkaller12.c /tmp/syzkaller12.core
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller13.sh b/tools/test/stress2/misc/syzkaller13.sh
new file mode 100755
index 000000000000..9b46ebd290af
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller13.sh
@@ -0,0 +1,249 @@
+#!/bin/sh
+
+# Kernel page fault with the following non-sleepable locks held:
+# exclusive sleep mutex so_rcv (so_rcv) r = 0 (0xfffffe012b5ee300) locked @ kern/uipc_usrreq.c:1272
+# exclusive sleep mutex socket (socket) r = 0 (0xfffffe012b5ee1a8) locked @ kern/uipc_usrreq.c:1265
+# shared rw unp_link_rwlock (unp_link_rwlock) r = 0 (0xffffffff81d99640) locked @ kern/uipc_usrreq.c:1334
+# stack backtrace:
+# #0 0xffffffff80c35701 at witness_debugger+0x71
+# #1 0xffffffff80c3669d at witness_warn+0x40d
+# #2 0xffffffff8106fb90 at trap_pfault+0x80
+# #3 0xffffffff8106f1a5 at trap+0x2a5
+# #4 0xffffffff810450d8 at calltrap+0x8
+# #5 0xffffffff80bc6fcc at sendfile_iodone+0x1ac
+# #6 0xffffffff80f3d98a at vnode_pager_generic_getpages_done_async+0x3a
+# #7 0xffffffff80c8236c at bufdone+0x6c
+# #8 0xffffffff80b0c06e at g_io_deliver+0x25e
+# #9 0xffffffff80b0c06e at g_io_deliver+0x25e
+# #10 0xffffffff80b08dfd at g_disk_done+0xed
+# #11 0xffffffff803ac1f3 at dadone+0x603
+# #12 0xffffffff8037ddf2 at xpt_done_process+0x382
+# #13 0xffffffff8037fdb5 at xpt_done_td+0xf5
+# #14 0xffffffff80b85b10 at fork_exit+0x80
+# #15 0xffffffff8104611e at fork_trampoline+0xe
+#
+#
+# Fatal trap 12: page fault while in kernel mode
+# cpuid = 23; apic id = 2b
+# fault virtual address = 0x8
+# fault code = supervisor read data, page not present
+# instruction pointer = 0x20:0xffffffff80c74bc0
+# stack pointer = 0x0:0xfffffe0126b118e0
+# frame pointer = 0x0:0xfffffe0126b11920
+# code segment = base 0x0, limit 0xfffff, type 0x1b
+# = DPL 0, pres 1, long 1, def32 0, gran 1
+# processor eflags = interrupt enabled, resume, IOPL = 0
+# current process = 32 (doneq0)
+# trap number = 12
+# panic: page fault
+# cpuid = 23
+# time = 1591531029
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe0126b11590
+# vpanic() at vpanic+0x182/frame 0xfffffe0126b115e0
+# panic() at panic+0x43/frame 0xfffffe0126b11640
+# trap_fatal() at trap_fatal+0x387/frame 0xfffffe0126b116a0
+# trap_pfault() at trap_pfault+0x99/frame 0xfffffe0126b11700
+# trap() at trap+0x2a5/frame 0xfffffe0126b11810
+# calltrap() at calltrap+0x8/frame 0xfffffe0126b11810
+# --- trap 0xc, rip = 0xffffffff80c74bc0, rsp = 0xfffffe0126b118e0, rbp = 0xfffffe0126b11920 ---
+# uipc_ready() at uipc_ready+0x1f0/frame 0xfffffe0126b11920
+# sendfile_iodone() at sendfile_iodone+0x1ac/frame 0xfffffe0126b11960
+# vnode_pager_generic_getpages_done_async() at vnode_pager_generic_getpages_done_async+0x3a/frame 0xfffffe0126b11980
+# bufdone() at bufdone+0x6c/frame 0xfffffe0126b119f0
+# g_io_deliver() at g_io_deliver+0x25e/frame 0xfffffe0126b11a40
+# g_io_deliver() at g_io_deliver+0x25e/frame 0xfffffe0126b11a90
+# g_disk_done() at g_disk_done+0xed/frame 0xfffffe0126b11ad0
+# dadone() at dadone+0x603/frame 0xfffffe0126b11b20
+# xpt_done_process() at xpt_done_process+0x382/frame 0xfffffe0126b11b60
+# xpt_done_td() at xpt_done_td+0xf5/frame 0xfffffe0126b11bb0
+# fork_exit() at fork_exit+0x80/frame 0xfffffe0126b11bf0
+# fork_trampoline() at fork_trampoline+0xe/frame 0xfffffe0126b11bf0
+# --- trap 0, rip = 0, rsp = 0, rbp = 0 ---
+# KDB: enter: panic
+# [ thread pid 32 tid 100163 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10c72c6(%rip)
+# db>
+
+# Reproduced on r361889
+# Fixed by r363682
+
+[ `uname -p` = "i386" ] && exit 0
+
+. ../default.cfg
+cat > /tmp/syzkaller13.c <<EOF
+// https://syzkaller.appspot.com/bug?id=8a63fce7c52d85d6fca9aca543ce5a77cdd15f25
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+// Reported-by: syzbot+6a689cc9c27bd265237a@syzkaller.appspotmail.com
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <dirent.h>
+#include <pwd.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static unsigned long long procid;
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void use_temporary_dir(void)
+{
+ char tmpdir_template[] = "./syzkaller.XXXXXX";
+ char* tmpdir = mkdtemp(tmpdir_template);
+ if (!tmpdir)
+ exit(1);
+ if (chmod(tmpdir, 0777))
+ exit(1);
+ if (chdir(tmpdir))
+ exit(1);
+}
+
+static void remove_dir(const char* dir)
+{
+ DIR* dp;
+ struct dirent* ep;
+ dp = opendir(dir);
+ if (dp == NULL)
+ exit(1);
+ while ((ep = readdir(dp))) {
+ if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
+ continue;
+ char filename[FILENAME_MAX];
+ snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name);
+ struct stat st;
+ if (lstat(filename, &st))
+ exit(1);
+ if (S_ISDIR(st.st_mode)) {
+ remove_dir(filename);
+ continue;
+ }
+ if (unlink(filename))
+ exit(1);
+ }
+ closedir(dp);
+ if (rmdir(dir))
+ exit(1);
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter;
+ for (iter = 0;; iter++) {
+ char cwdbuf[32];
+ sprintf(cwdbuf, "./%d", iter);
+ if (mkdir(cwdbuf, 0777))
+ exit(1);
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ if (chdir(cwdbuf))
+ exit(1);
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ remove_dir(cwdbuf);
+ }
+}
+
+uint64_t r[5] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff,
+ 0xffffffffffffffff, 0xffffffffffffffff};
+
+void execute_one(void)
+{
+ intptr_t res = 0;
+ memcpy((void*)0x20000100, "./file0\000", 8);
+ res = syscall(SYS_open, 0x20000100ul, 0x40000400000002c2ul, 0ul);
+ if (res != -1)
+ r[0] = res;
+ syscall(SYS_fcntl, r[0], 4ul, 0x10048ul);
+ *(uint64_t*)0x20000340 = 0x20000180;
+ *(uint64_t*)0x20000348 = 0x81700;
+ syscall(SYS_writev, r[0], 0x20000340ul, 0x1000000000000013ul);
+ res = syscall(SYS_socket, 2ul, 2ul, 0x88);
+ if (res != -1)
+ r[1] = res;
+ res = syscall(SYS_socketpair, 1ul, 1ul, 0, 0x20000000ul);
+ if (res != -1)
+ r[2] = *(uint32_t*)0x20000000;
+ res = syscall(SYS_dup2, r[2], r[1]);
+ if (res != -1)
+ r[3] = res;
+ memcpy((void*)0x20000140, "./file0\000", 8);
+ res = syscall(SYS_open, 0x20000140ul, 0ul, 0ul);
+ if (res != -1)
+ r[4] = res;
+ syscall(SYS_sendfile, r[4], r[3], 0ul, 1ul, 0ul, 0ul, 0ul);
+}
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ for (procid = 0; procid < 4; procid++) {
+ if (fork() == 0) {
+ use_temporary_dir();
+ loop();
+ }
+ }
+ sleep(1000000);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller13 -Wall -Wextra -O2 /tmp/syzkaller13.c -lpthread ||
+ exit 1
+
+(cd ../testcases/swap; ./swap -t 1m -i 20 -h > /dev/null 2>&1) &
+(cd /tmp; ./syzkaller13) &
+sleep 60
+while pkill swap; do sleep .1; done
+pkill -9 syzkaller13
+wait
+
+rm -f /tmp/syzkaller13 /tmp/syzkaller13.* /tmp/file0
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller14.sh b/tools/test/stress2/misc/syzkaller14.sh
new file mode 100755
index 000000000000..5fa99004f838
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller14.sh
@@ -0,0 +1,295 @@
+#!/bin/sh
+
+# panic: mtx_lock() of spin mutex (null) @ ../../../kern/uipc_ktls.c:1034
+# cpuid = 5
+# time = 1591684934
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe013a91b6c0
+# vpanic() at vpanic+0x182/frame 0xfffffe013a91b710
+# panic() at panic+0x43/frame 0xfffffe013a91b770
+# __mtx_lock_flags() at __mtx_lock_flags+0x13c/frame 0xfffffe013a91b7c0
+# ktls_get_rx_mode() at ktls_get_rx_mode+0x4e/frame 0xfffffe013a91b7f0
+# tcp_default_ctloutput() at tcp_default_ctloutput+0x800/frame 0xfffffe013a91b960
+# tcp_ctloutput() at tcp_ctloutput+0x1f5/frame 0xfffffe013a91b9d0
+# sogetopt() at sogetopt+0x101/frame 0xfffffe013a91ba40
+# kern_getsockopt() at kern_getsockopt+0xb4/frame 0xfffffe013a91baa0
+# sys_getsockopt() at sys_getsockopt+0x52/frame 0xfffffe013a91bad0
+# amd64_syscall() at amd64_syscall+0x159/frame 0xfffffe013a91bbf0
+# fast_syscall_common() at fast_syscall_common+0x101/frame 0xfffffe013a91bbf0
+# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x80045446a, rsp = 0x7fffdffdcf88, rbp = 0x7fffdffdcfc0 ---
+# KDB: enter: panic
+# [ thread pid 5313 tid 101679 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10c7fa6(%rip)
+# db> x/s version
+# version: FreeBSD 13.0-CURRENT #0 r361886: Tue Jun 9 08:32:24 CEST 2020\012 pho@t2.osted.lan:/usr/src/sys/amd64/compile/KTLS\012
+# db>
+
+# Reproduced on r361886 with "options KERN_TLS"
+
+[ `uname -p` = "i386" ] && exit 0
+
+. ../default.cfg
+cat > /tmp/syzkaller14.c <<EOF
+// https://syzkaller.appspot.com/bug?id=d50164ce557a20a785510aac5d6f104d8a62e779
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+// Reported-by: syzbot+a8829e888a93a4a04619@syzkaller.appspotmail.com
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <errno.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static unsigned long long procid;
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void thread_start(void* (*fn)(void*), void* arg)
+{
+ pthread_t th;
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ pthread_attr_setstacksize(&attr, 128 << 10);
+ int i;
+ for (i = 0; i < 100; i++) {
+ if (pthread_create(&th, &attr, fn, arg) == 0) {
+ pthread_attr_destroy(&attr);
+ return;
+ }
+ if (errno == EAGAIN) {
+ usleep(50);
+ continue;
+ }
+ break;
+ }
+ exit(1);
+}
+
+typedef struct {
+ pthread_mutex_t mu;
+ pthread_cond_t cv;
+ int state;
+} event_t;
+
+static void event_init(event_t* ev)
+{
+ if (pthread_mutex_init(&ev->mu, 0))
+ exit(1);
+ if (pthread_cond_init(&ev->cv, 0))
+ exit(1);
+ ev->state = 0;
+}
+
+static void event_reset(event_t* ev)
+{
+ ev->state = 0;
+}
+
+static void event_set(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ if (ev->state)
+ exit(1);
+ ev->state = 1;
+ pthread_mutex_unlock(&ev->mu);
+ pthread_cond_broadcast(&ev->cv);
+}
+
+static void event_wait(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ while (!ev->state)
+ pthread_cond_wait(&ev->cv, &ev->mu);
+ pthread_mutex_unlock(&ev->mu);
+}
+
+static int event_isset(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+static int event_timedwait(event_t* ev, uint64_t timeout)
+{
+ uint64_t start = current_time_ms();
+ uint64_t now = start;
+ pthread_mutex_lock(&ev->mu);
+ for (;;) {
+ if (ev->state)
+ break;
+ uint64_t remain = timeout - (now - start);
+ struct timespec ts;
+ ts.tv_sec = remain / 1000;
+ ts.tv_nsec = (remain % 1000) * 1000 * 1000;
+ pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
+ now = current_time_ms();
+ if (now - start > timeout)
+ break;
+ }
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+struct thread_t {
+ int created, call;
+ event_t ready, done;
+};
+
+static struct thread_t threads[16];
+static void execute_call(int call);
+static int running;
+
+static void* thr(void* arg)
+{
+ struct thread_t* th = (struct thread_t*)arg;
+ for (;;) {
+ event_wait(&th->ready);
+ event_reset(&th->ready);
+ execute_call(th->call);
+ __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->done);
+ }
+ return 0;
+}
+
+static void execute_one(void)
+{
+ int i, call, thread;
+ int collide = 0;
+again:
+ for (call = 0; call < 3; call++) {
+ for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
+ thread++) {
+ struct thread_t* th = &threads[thread];
+ if (!th->created) {
+ th->created = 1;
+ event_init(&th->ready);
+ event_init(&th->done);
+ event_set(&th->done);
+ thread_start(thr, th);
+ }
+ if (!event_isset(&th->done))
+ continue;
+ event_reset(&th->done);
+ th->call = call;
+ __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->ready);
+ if (collide && (call % 2) == 0)
+ break;
+ event_timedwait(&th->done, 45);
+ break;
+ }
+ }
+ for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
+ sleep_ms(1);
+ if (!collide) {
+ collide = 1;
+ goto again;
+ }
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter;
+ for (iter = 0;; iter++) {
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ }
+}
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+void execute_call(int call)
+{
+ intptr_t res = 0;
+ switch (call) {
+ case 0:
+ res = syscall(SYS_socket, 0x1cul, 1ul, 0);
+ if (res != -1)
+ r[0] = res;
+ break;
+ case 1:
+ syscall(SYS_getsockopt, r[0], 6, 0x2a, 0ul, 0ul);
+ break;
+ case 2:
+ syscall(SYS_listen, r[0], 1);
+ break;
+ }
+}
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ for (procid = 0; procid < 4; procid++) {
+ if (fork() == 0) {
+ loop();
+ }
+ }
+ sleep(1000000);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller14 -Wall -Wextra -O2 /tmp/syzkaller14.c -lpthread ||
+ exit 1
+
+(cd /tmp; ./syzkaller14) &
+sleep 60
+pkill -9 syzkaller14
+wait
+
+rm -f /tmp/syzkaller14 /tmp/syzkaller14.c /tmp/syzkaller14.core
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller15.sh b/tools/test/stress2/misc/syzkaller15.sh
new file mode 100755
index 000000000000..d15017d7eb12
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller15.sh
@@ -0,0 +1,190 @@
+#!/bin/sh
+
+# panic: sbflush_internal: ccc 0 mb 0 mbcnt 256
+# cpuid = 4
+# time = 1592126385
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe012b22f8d0
+# vpanic() at vpanic+0x182/frame 0xfffffe012b22f920
+# panic() at panic+0x43/frame 0xfffffe012b22f980
+# sbrelease_internal() at sbrelease_internal+0xbd/frame 0xfffffe012b22f9a0
+# solisten_proto() at solisten_proto+0xab/frame 0xfffffe012b22fa00
+# sctp_listen() at sctp_listen+0x2f4/frame 0xfffffe012b22fa60
+# solisten() at solisten+0x42/frame 0xfffffe012b22fa80
+# kern_listen() at kern_listen+0x7c/frame 0xfffffe012b22fac0
+# ia32_syscall() at ia32_syscall+0x150/frame 0xfffffe012b22fbf0
+# int0x80_syscall_common() at int0x80_syscall_common+0x9c/frame 0xffffdb10
+# KDB: enter: panic
+# [ thread pid 93263 tid 100202 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10c5456(%rip)
+# db> x/s version
+# version: FreeBSD 13.0-CURRENT #0 r362171: Sun Jun 14 09:06:12 CEST 2020
+# pho@mercat1.netperf.freebsd.org:/usr/src/sys/amd64/compile/PHO
+# db>
+
+[ `uname -p` != "amd64" ] && exit 0
+
+. ../default.cfg
+kldstat -v | grep -q sctp || kldload sctp.ko
+cat > /tmp/syzkaller15.c <<EOF
+// https://syzkaller.appspot.com/bug?id=6b9cfdc1689b0adee1c6f75f56ab034a70500d93
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+// Reported-by: syzbot+bca42a93f1254ca91e94@syzkaller.appspotmail.com
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <pwd.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static unsigned long long procid;
+
+static __thread int skip_segv;
+static __thread jmp_buf segv_env;
+
+static void segv_handler(int sig, siginfo_t* info, void* ctx __unused)
+{
+ uintptr_t addr = (uintptr_t)info->si_addr;
+ const uintptr_t prog_start = 1 << 20;
+ const uintptr_t prog_end = 100 << 20;
+ if (__atomic_load_n(&skip_segv, __ATOMIC_RELAXED) &&
+ (addr < prog_start || addr > prog_end)) {
+ _longjmp(segv_env, 1);
+ }
+ exit(sig);
+}
+
+static void install_segv_handler(void)
+{
+ struct sigaction sa;
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_sigaction = segv_handler;
+ sa.sa_flags = SA_NODEFER | SA_SIGINFO;
+ sigaction(SIGSEGV, &sa, NULL);
+ sigaction(SIGBUS, &sa, NULL);
+}
+
+#define NONFAILING(...) \
+ { \
+ __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \
+ if (_setjmp(segv_env) == 0) { \
+ __VA_ARGS__; \
+ } \
+ __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \
+ }
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter;
+ for (iter = 0;; iter++) {
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ }
+}
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+void execute_one(void)
+{
+ intptr_t res = 0;
+ res = syscall(SYS_socket, 0x1c, 1, 0x84);
+ if (res != -1)
+ r[0] = res;
+ NONFAILING(*(uint8_t*)0x10000040 = 0xc8);
+ NONFAILING(*(uint8_t*)0x10000041 = 0x51);
+ NONFAILING(*(uint8_t*)0x10000042 = 0);
+ NONFAILING(*(uint8_t*)0x10000043 = 0);
+ NONFAILING(*(uint8_t*)0x10000044 = 0);
+ NONFAILING(*(uint8_t*)0x10000045 = 0);
+ NONFAILING(*(uint8_t*)0x10000046 = 0);
+ NONFAILING(*(uint8_t*)0x10000047 = 0);
+ NONFAILING(*(uint8_t*)0x10000048 = 0);
+ NONFAILING(*(uint8_t*)0x10000049 = 0);
+ NONFAILING(*(uint8_t*)0x1000004a = 0);
+ syscall(SYS_setsockopt, (intptr_t)r[0], 0x84, 0xc, 0x10000040, 0xb);
+ NONFAILING(*(uint8_t*)0x10000180 = 0x5f);
+ NONFAILING(*(uint8_t*)0x10000181 = 0x1c);
+ NONFAILING(*(uint16_t*)0x10000182 = htobe16(0x4e22 + procid * 4));
+ NONFAILING(*(uint32_t*)0x10000184 = 0);
+ NONFAILING(*(uint64_t*)0x10000188 = htobe64(0));
+ NONFAILING(*(uint64_t*)0x10000190 = htobe64(1));
+ NONFAILING(*(uint32_t*)0x10000198 = 0);
+ syscall(SYS_connect, (intptr_t)r[0], 0x10000180, 0x1c);
+ syscall(SYS_listen, (intptr_t)r[0], 7);
+}
+int main(void)
+{
+ syscall(SYS_mmap, 0x10000000, 0x1000000, 7, 0x1012, -1, 0);
+ install_segv_handler();
+ for (procid = 0; procid < 4; procid++) {
+ if (fork() == 0) {
+ loop();
+ }
+ }
+ sleep(1000000);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller15 -Wall -Wextra -O0 -m32 /tmp/syzkaller15.c ||
+ exit 1
+
+(cd /tmp; ./syzkaller15) &
+sleep 60
+pkill -9 syzkaller15
+wait
+
+rm -f /tmp/syzkaller15 /tmp/syzkaller15.c /tmp/syzkaller15.core
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller16.sh b/tools/test/stress2/misc/syzkaller16.sh
new file mode 100755
index 000000000000..4ded2f61388f
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller16.sh
@@ -0,0 +1,417 @@
+#!/bin/sh
+
+# Fatal trap 9: general protection fault while in kernel mode
+# cpuid = 10; apic id = 0a
+# instruction pointer = 0x20:0xffffffff80db1fd9
+# stack pointer = 0x0:0xfffffe00e2240660
+# frame pointer = 0x0:0xfffffe00e2240830
+# code segment = base 0x0, limit 0xfffff, type 0x1b
+# = DPL 0, pres 1, long 1, def32 0, gran 1
+# processor eflags = interrupt enabled, resume, IOPL = 0
+# current process = 29524 (syzkaller16)
+# trap number = 9
+# panic: general protection fault
+# cpuid = 10
+# time = 1592652215
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe00e2240370
+# vpanic() at vpanic+0x182/frame 0xfffffe00e22403c0
+# panic() at panic+0x43/frame 0xfffffe00e2240420
+# trap_fatal() at trap_fatal+0x387/frame 0xfffffe00e2240480
+# trap() at trap+0x80/frame 0xfffffe00e2240590
+# calltrap() at calltrap+0x8/frame 0xfffffe00e2240590
+# --- trap 0x9, rip = 0xffffffff80db1fd9, rsp = 0xfffffe00e2240660, rbp = 0xfffffe00e2240830 ---
+# sctp_lower_sosend() at sctp_lower_sosend+0x21b9/frame 0xfffffe00e2240830
+# sctp_sosend() at sctp_sosend+0x346/frame 0xfffffe00e2240950
+# sosend() at sosend+0x66/frame 0xfffffe00e2240980
+# kern_sendit() at kern_sendit+0x246/frame 0xfffffe00e2240a20
+# sendit() at sendit+0x1d8/frame 0xfffffe00e2240a70
+# sys_sendto() at sys_sendto+0x4d/frame 0xfffffe00e2240ac0
+# ia32_syscall() at ia32_syscall+0x150/frame 0xfffffe00e2240bf0
+# int0x80_syscall_common() at int0x80_syscall_common+0x9c/frame 0xfbffcf8c
+# db> x/s version
+# version: FreeBSD 13.0-CURRENT #0 r362431: Sat Jun 20 09:52:53 CEST 2020
+# pho@mercat1.netperf.freebsd.org:/usr/src/sys/amd64/compile/PHO
+# db>
+
+[ `uname -p` != "amd64" ] && exit 0
+
+. ../default.cfg
+kldstat -v | grep -q sctp || kldload sctp.ko
+cat > /tmp/syzkaller16.c <<EOF
+// https://syzkaller.appspot.com/bug?id=4b11a1abc41d0a67736ef9a7506ab3ec7e378102
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+// Reported-by: syzbot+bc02585076c3cc977f9b@syzkaller.appspotmail.com
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <errno.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static unsigned long long procid;
+
+static __thread int skip_segv;
+static __thread jmp_buf segv_env;
+
+static void segv_handler(int sig, siginfo_t* info, void* ctx __unused)
+{
+ uintptr_t addr = (uintptr_t)info->si_addr;
+ const uintptr_t prog_start = 1 << 20;
+ const uintptr_t prog_end = 100 << 20;
+ if (__atomic_load_n(&skip_segv, __ATOMIC_RELAXED) &&
+ (addr < prog_start || addr > prog_end)) {
+ _longjmp(segv_env, 1);
+ }
+ exit(sig);
+}
+
+static void install_segv_handler(void)
+{
+ struct sigaction sa;
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_sigaction = segv_handler;
+ sa.sa_flags = SA_NODEFER | SA_SIGINFO;
+ sigaction(SIGSEGV, &sa, NULL);
+ sigaction(SIGBUS, &sa, NULL);
+}
+
+#define NONFAILING(...) \
+ { \
+ __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \
+ if (_setjmp(segv_env) == 0) { \
+ __VA_ARGS__; \
+ } \
+ __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \
+ }
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void thread_start(void* (*fn)(void*), void* arg)
+{
+ pthread_t th;
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ pthread_attr_setstacksize(&attr, 128 << 10);
+ int i;
+ for (i = 0; i < 100; i++) {
+ if (pthread_create(&th, &attr, fn, arg) == 0) {
+ pthread_attr_destroy(&attr);
+ return;
+ }
+ if (errno == EAGAIN) {
+ usleep(50);
+ continue;
+ }
+ break;
+ }
+ exit(1);
+}
+
+typedef struct {
+ pthread_mutex_t mu;
+ pthread_cond_t cv;
+ int state;
+} event_t;
+
+static void event_init(event_t* ev)
+{
+ if (pthread_mutex_init(&ev->mu, 0))
+ exit(1);
+ if (pthread_cond_init(&ev->cv, 0))
+ exit(1);
+ ev->state = 0;
+}
+
+static void event_reset(event_t* ev)
+{
+ ev->state = 0;
+}
+
+static void event_set(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ if (ev->state)
+ exit(1);
+ ev->state = 1;
+ pthread_mutex_unlock(&ev->mu);
+ pthread_cond_broadcast(&ev->cv);
+}
+
+static void event_wait(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ while (!ev->state)
+ pthread_cond_wait(&ev->cv, &ev->mu);
+ pthread_mutex_unlock(&ev->mu);
+}
+
+static int event_isset(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+static int event_timedwait(event_t* ev, uint64_t timeout)
+{
+ uint64_t start = current_time_ms();
+ uint64_t now = start;
+ pthread_mutex_lock(&ev->mu);
+ for (;;) {
+ if (ev->state)
+ break;
+ uint64_t remain = timeout - (now - start);
+ struct timespec ts;
+ ts.tv_sec = remain / 1000;
+ ts.tv_nsec = (remain % 1000) * 1000 * 1000;
+ pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
+ now = current_time_ms();
+ if (now - start > timeout)
+ break;
+ }
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+struct thread_t {
+ int created, call;
+ event_t ready, done;
+};
+
+static struct thread_t threads[16];
+static void execute_call(int call);
+static int running;
+
+static void* thr(void* arg)
+{
+ struct thread_t* th = (struct thread_t*)arg;
+ for (;;) {
+ event_wait(&th->ready);
+ event_reset(&th->ready);
+ execute_call(th->call);
+ __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->done);
+ }
+ return 0;
+}
+
+static void execute_one(void)
+{
+ int i, call, thread;
+ int collide = 0;
+again:
+ for (call = 0; call < 6; call++) {
+ for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
+ thread++) {
+ struct thread_t* th = &threads[thread];
+ if (!th->created) {
+ th->created = 1;
+ event_init(&th->ready);
+ event_init(&th->done);
+ event_set(&th->done);
+ thread_start(thr, th);
+ }
+ if (!event_isset(&th->done))
+ continue;
+ event_reset(&th->done);
+ th->call = call;
+ __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->ready);
+ if (collide && (call % 2) == 0)
+ break;
+ event_timedwait(&th->done, 45);
+ break;
+ }
+ }
+ for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
+ sleep_ms(1);
+ if (!collide) {
+ collide = 1;
+ goto again;
+ }
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter;
+ for (iter = 0;; iter++) {
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ }
+}
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+void execute_call(int call)
+{
+ intptr_t res = 0;
+ switch (call) {
+ case 0:
+ res = syscall(SYS_socket, 0x1c, 1, 0x84);
+ if (res != -1)
+ r[0] = res;
+ break;
+ case 1:
+ NONFAILING(*(uint8_t*)0x10000000 = 0x1c);
+ NONFAILING(*(uint8_t*)0x10000001 = 0x1c);
+ NONFAILING(*(uint16_t*)0x10000002 = htobe16(0x4e22 + procid * 4));
+ NONFAILING(*(uint32_t*)0x10000004 = 0);
+ NONFAILING(*(uint8_t*)0x10000008 = 0);
+ NONFAILING(*(uint8_t*)0x10000009 = 0);
+ NONFAILING(*(uint8_t*)0x1000000a = 0);
+ NONFAILING(*(uint8_t*)0x1000000b = 0);
+ NONFAILING(*(uint8_t*)0x1000000c = 0);
+ NONFAILING(*(uint8_t*)0x1000000d = 0);
+ NONFAILING(*(uint8_t*)0x1000000e = 0);
+ NONFAILING(*(uint8_t*)0x1000000f = 0);
+ NONFAILING(*(uint8_t*)0x10000010 = 0);
+ NONFAILING(*(uint8_t*)0x10000011 = 0);
+ NONFAILING(*(uint8_t*)0x10000012 = 0);
+ NONFAILING(*(uint8_t*)0x10000013 = 0);
+ NONFAILING(*(uint8_t*)0x10000014 = 0);
+ NONFAILING(*(uint8_t*)0x10000015 = 0);
+ NONFAILING(*(uint8_t*)0x10000016 = 0);
+ NONFAILING(*(uint8_t*)0x10000017 = 0);
+ NONFAILING(*(uint32_t*)0x10000018 = 0);
+ syscall(SYS_bind, (intptr_t)r[0], 0x10000000, 0x1c);
+ break;
+ case 2:
+ NONFAILING(*(uint8_t*)0x10000180 = 0x5f);
+ NONFAILING(*(uint8_t*)0x10000181 = 0x1c);
+ NONFAILING(*(uint16_t*)0x10000182 = htobe16(0x4e22 + procid * 4));
+ NONFAILING(*(uint32_t*)0x10000184 = 0);
+ NONFAILING(*(uint64_t*)0x10000188 = htobe64(0));
+ NONFAILING(*(uint64_t*)0x10000190 = htobe64(1));
+ NONFAILING(*(uint32_t*)0x10000198 = 0);
+ syscall(SYS_connect, (intptr_t)r[0], 0x10000180, 0x1c);
+ break;
+ case 3:
+ NONFAILING(*(uint32_t*)0x100001c0 = 0x401);
+ syscall(SYS_setsockopt, (intptr_t)r[0], 0x84, 0x1b, 0x100001c0, 4);
+ break;
+ case 4:
+ NONFAILING(memcpy(
+ (void*)0x10000380,
+ "\xa3\x1b\xe1\x78\x8e\x58\x9b\x38\x59\xf3\xbb\xdd\x7e\xf7\x51\x23\x97"
+ "\x31\xb2\x90\x4a\xd0\x4e\xb7\xdc\x37\xc6\x95\xf6\x05\x5c\xa8\x36\x54"
+ "\x7e\x7b\x6c\xc3\x7d\xae\x2a\xe4\x77\x08\x94\x67\x3c\x89\x65\x93\x24"
+ "\x1c\x56\x3e\x08\x69\x05\x35\xeb\x3b\x7f\x19\x7d\xda\x44\x54\xb4\x42"
+ "\x4f\x7f\xf9\xf9\xfe\x4e\xac\xef\xa6\xd4\xb1\x61\x9d\xd9\x0b\x97\x7c"
+ "\xd9\x82\x16\xc9\x7b\x2e\xb3\x9f\x02\xde\x0f\xae\xe7\x0b\xec\xa3\x66"
+ "\x3c\x2e\x6c\xc5\x1d\xa2\xb4\x8b\x4b\x39\x49\xcc\x14\x5a\x50\x62\x0f"
+ "\xd5\x65\xb9\x7c\x5c\xa0\xea\xfc\xa4\xc9\x13\x73\x14\x16\xba\xcc\xae"
+ "\x89\xe2\x68\x01\x06\x94\x78\xa3\xee\xa8\x45\xf7\xc2\xcb\x48\x93\xe5"
+ "\x83\x52\x45\x26\xe3\xeb\x73\xa2\xe4\xf1\x11\xcf\x40\x5f\xef\x99\xc2"
+ "\xa1\xeb\x2c\x96\x70\x56\x88\xc8\xc7\x6b\xa1\x66\xd2\x23\x20\x07\x62"
+ "\x69\xd2\x1c\x52\xbb\x5e\x86\x43\x7d\x6c\x65\x44\x42\xf6\xd8\x45\xe2"
+ "\x00\x24\x00\x00\xf0\x1d\x29\xf6\xd3\x74\x83\x25\x40\x56\x50\x17\x7f"
+ "\xc3\x60\xd7\xed\xb1\xfb\x7a\x74\x38\x2b\x47\x34\x93\x9c\xee\xc9\xb0"
+ "\xbf\x7d\xc4\x19\xe2\x77\x4c\xa2\x71\x9c\x30",
+ 249));
+ NONFAILING(*(uint8_t*)0x10000140 = 0x10);
+ NONFAILING(*(uint8_t*)0x10000141 = 2);
+ NONFAILING(*(uint16_t*)0x10000142 = htobe16(0x4e22 + procid * 4));
+ NONFAILING(*(uint32_t*)0x10000144 = htobe32(-1));
+ NONFAILING(*(uint8_t*)0x10000148 = 0);
+ NONFAILING(*(uint8_t*)0x10000149 = 0);
+ NONFAILING(*(uint8_t*)0x1000014a = 0);
+ NONFAILING(*(uint8_t*)0x1000014b = 0);
+ NONFAILING(*(uint8_t*)0x1000014c = 0);
+ NONFAILING(*(uint8_t*)0x1000014d = 0);
+ NONFAILING(*(uint8_t*)0x1000014e = 0);
+ NONFAILING(*(uint8_t*)0x1000014f = 0);
+ syscall(SYS_sendto, (intptr_t)r[0], 0x10000380, 0xfffffcaa, 0x20000,
+ 0x10000140, 0x10);
+ break;
+ case 5:
+ NONFAILING(*(uint32_t*)0x10000540 = 0);
+ NONFAILING(*(uint32_t*)0x10000544 = 0);
+ NONFAILING(*(uint32_t*)0x10000548 = 0);
+ NONFAILING(*(uint32_t*)0x1000054c = 0);
+ NONFAILING(*(uint32_t*)0x10000550 = 0);
+ NONFAILING(*(uint32_t*)0x10000554 = 0);
+ NONFAILING(*(uint32_t*)0x10000558 = 0x20008);
+ syscall(SYS_sendmsg, (intptr_t)r[0], 0x10000540, 0x100);
+ break;
+ }
+}
+int main(void)
+{
+ syscall(SYS_mmap, 0x10000000, 0x1000000, 7, 0x1012, -1, 0);
+ install_segv_handler();
+ for (procid = 0; procid < 4; procid++) {
+ if (fork() == 0) {
+ loop();
+ }
+ }
+ sleep(1000000);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller16 -Wall -Wextra -O0 -m32 /tmp/syzkaller16.c -pthread ||
+ exit 1
+
+(cd ../testcases/swap; ./swap -t 1m -i 20 -h > /dev/null 2>&1) &
+(cd /tmp; ./syzkaller16) &
+sleep 60
+pkill -9 syzkaller16 swap
+wait
+
+rm -f /tmp/syzkaller16 /tmp/syzkaller16.c /tmp/syzkaller16.core
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller17.sh b/tools/test/stress2/misc/syzkaller17.sh
new file mode 100755
index 000000000000..94b33771c59c
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller17.sh
@@ -0,0 +1,131 @@
+#!/bin/sh
+
+# Fatal trap 12: page fault while in kernel mode
+# cpuid = 15; apic id = 23
+# fault virtual address = 0x10
+# fault code = supervisor read data, page not present
+# instruction pointer = 0x20:0xffffffff80de3833
+# stack pointer = 0x28:0xfffffe01398a8860
+# frame pointer = 0x28:0xfffffe01398a8880
+# code segment = base 0x0, limit 0xfffff, type 0x1b
+# = DPL 0, pres 1, long 1, def32 0, gran 1
+# processor eflags = interrupt enabled, resume, IOPL = 0
+# current process = 2798 (syzkaller17)
+# trap number = 12
+# panic: page fault
+# cpuid = 15
+# time = 1593500664
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe01398a8510
+# vpanic() at vpanic+0x182/frame 0xfffffe01398a8560
+# panic() at panic+0x43/frame 0xfffffe01398a85c0
+# trap_fatal() at trap_fatal+0x387/frame 0xfffffe01398a8620
+# trap_pfault() at trap_pfault+0x99/frame 0xfffffe01398a8680
+# trap() at trap+0x2a5/frame 0xfffffe01398a8790
+# calltrap() at calltrap+0x8/frame 0xfffffe01398a8790
+# --- trap 0xc, rip = 0xffffffff80de3833, rsp = 0xfffffe01398a8860, rbp = 0xfffffe01398a8880 ---
+# sctp_find_ifa_in_ep() at sctp_find_ifa_in_ep+0x83/frame 0xfffffe01398a8880
+# sctp_addr_mgmt_ep_sa() at sctp_addr_mgmt_ep_sa+0x56/frame 0xfffffe01398a88c0
+# sctp_bindx_delete_address() at sctp_bindx_delete_address+0x9e/frame 0xfffffe01398a8910
+# sctp_setopt() at sctp_setopt+0x2134/frame 0xfffffe01398a8990
+# sctp_ctloutput() at sctp_ctloutput+0x173/frame 0xfffffe01398a89f0
+# sosetopt() at sosetopt+0xed/frame 0xfffffe01398a8a50
+# kern_setsockopt() at kern_setsockopt+0xac/frame 0xfffffe01398a8ab0
+# sys_setsockopt() at sys_setsockopt+0x24/frame 0xfffffe01398a8ad0
+# amd64_syscall() at amd64_syscall+0x159/frame 0xfffffe01398a8bf0
+# fast_syscall_common() at fast_syscall_common+0x101/frame 0xfffffe01398a8bf0
+# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x80042813a, rsp = 0x7fffffffe538, rbp = 0x7fffffffe570 ---
+# KDB: enter: panic
+# [ thread pid 2798 tid 100249 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10c4cf6(%rip)
+# db> x/s version
+# version: FreeBSD 13.0-CURRENT #0 r362791: Tue Jun 30 08:57:50 CEST 2020
+# pho@t2.osted.lan:/usr/src/sys/amd64/compile/PHO
+# db>
+
+[ `uname -p` != "amd64" ] && exit 0
+
+. ../default.cfg
+kldstat -v | grep -q sctp || kldload sctp.ko
+cat > /tmp/syzkaller17.c <<EOF
+// https://syzkaller.appspot.com/bug?id=a038e666f2e27a98b03aa605054c088e6ef2bb5d
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+// Reported-by: syzbot+f3a6fccfa6ae9d3ded29@syzkaller.appspotmail.com
+
+#define _GNU_SOURCE
+
+#include <pwd.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ intptr_t res = 0;
+ res = syscall(SYS_socket, 2ul, 5ul, 0x84);
+ if (res != -1)
+ r[0] = res;
+ *(uint8_t*)0x200001c0 = 0x10;
+ *(uint8_t*)0x200001c1 = 2;
+ *(uint16_t*)0x200001c2 = htobe16(0x4e20);
+ *(uint8_t*)0x200001c4 = 0xac;
+ *(uint8_t*)0x200001c5 = 0x14;
+ *(uint8_t*)0x200001c6 = 0;
+ *(uint8_t*)0x200001c7 = 0xaa;
+ *(uint8_t*)0x200001c8 = 0;
+ *(uint8_t*)0x200001c9 = 0;
+ *(uint8_t*)0x200001ca = 0;
+ *(uint8_t*)0x200001cb = 0;
+ *(uint8_t*)0x200001cc = 0;
+ *(uint8_t*)0x200001cd = 0;
+ *(uint8_t*)0x200001ce = 0;
+ *(uint8_t*)0x200001cf = 0;
+ *(uint64_t*)0x200001d0 = 0;
+ *(uint64_t*)0x200001d8 = 0;
+ *(uint64_t*)0x200001e0 = 0;
+ *(uint64_t*)0x200001e8 = 0;
+ *(uint64_t*)0x200001f0 = 0;
+ *(uint64_t*)0x200001f8 = 0;
+ *(uint64_t*)0x20000200 = 0;
+ *(uint64_t*)0x20000208 = 0;
+ *(uint64_t*)0x20000210 = 0;
+ *(uint64_t*)0x20000218 = 0;
+ *(uint64_t*)0x20000220 = 0;
+ *(uint64_t*)0x20000228 = 0;
+ *(uint64_t*)0x20000230 = 0;
+ *(uint64_t*)0x20000238 = 0;
+ *(uint64_t*)0x20000240 = 0;
+ *(uint32_t*)0x20000248 = 0;
+ *(uint32_t*)0x2000024c = 4;
+ *(uint32_t*)0x20000250 = 0;
+ *(uint32_t*)0x20000254 = 0;
+ *(uint32_t*)0x20000258 = 0;
+ *(uint16_t*)0x2000025c = 0;
+ *(uint8_t*)0x2000025e = 0;
+ syscall(SYS_setsockopt, r[0], 0x84, 0x8002, 0x200001c0ul, 0xa0ul);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller17 -Wall -Wextra -O0 /tmp/syzkaller17.c ||
+ exit 1
+
+(cd ../testcases/swap; ./swap -t 1m -i 20 -h > /dev/null 2>&1) &
+start=`date +%s`
+while pgrep -q swap; do
+ (cd /tmp; timeout 1m ./syzkaller17)
+ [ $((`date +%s` - start)) -ge 60 ] && break
+done
+pkill -9 syzkaller17 swap
+wait
+
+rm -f /tmp/syzkaller17 /tmp/syzkaller17.c /tmp/syzkaller17.core
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller18.sh b/tools/test/stress2/misc/syzkaller18.sh
new file mode 100755
index 000000000000..12432d23bc65
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller18.sh
@@ -0,0 +1,128 @@
+#!/bin/sh
+
+# panic: in6p_lookup_mcast_ifp: not INP_IPV6 inpcb
+# cpuid = 20
+# time = 1593501338
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe013eaf7e90
+# vpanic() at vpanic+0x182/frame 0xfffffe013eaf7ee0
+# panic() at panic+0x43/frame 0xfffffe013eaf7f40
+# ip6_setmoptions() at ip6_setmoptions+0x23b3/frame 0xfffffe013eaf8120
+# ip6_ctloutput() at ip6_ctloutput+0x1f5/frame 0xfffffe013eaf89f0
+# sosetopt() at sosetopt+0xed/frame 0xfffffe013eaf8a50
+# kern_setsockopt() at kern_setsockopt+0xac/frame 0xfffffe013eaf8ab0
+# sys_setsockopt() at sys_setsockopt+0x24/frame 0xfffffe013eaf8ad0
+# amd64_syscall() at amd64_syscall+0x159/frame 0xfffffe013eaf8bf0
+# fast_syscall_common() at fast_syscall_common+0x101/frame 0xfffffe013eaf8bf0
+# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x80042813a, rsp = 0x7fffffffe518, rbp = 0x7fffffffe570 ---
+# KDB: enter: panic
+# [ thread pid 2853 tid 100445 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10c4cf6(%rip)
+# db> x/s version
+# version: FreeBSD 13.0-CURRENT #0 r362791: Tue Jun 30 08:57:50 CEST 2020
+# pho@t2.osted.lan:/usr/src/sys/amd64/compile/PHO
+# db> reset
+
+[ `uname -p` != "amd64" ] && exit 0
+
+. ../default.cfg
+cat > /tmp/syzkaller18.c <<EOF
+// https://syzkaller.appspot.com/bug?id=c3fe7740671182fd120fa95c67cf305caa9c3397
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+// Reported-by: syzbot+d01ab3d5e6c1516a393c@syzkaller.appspotmail.com
+
+#define _GNU_SOURCE
+
+#include <pwd.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ intptr_t res = 0;
+ res = syscall(SYS_socket, 0x1cul, 2ul, 0);
+ if (res != -1)
+ r[0] = res;
+ *(uint32_t*)0x200001c0 = 0;
+ syscall(SYS_setsockopt, r[0], 0x29, 0x1b, 0x200001c0ul, 4ul);
+ *(uint8_t*)0x20000080 = 0x1c;
+ *(uint8_t*)0x20000081 = 0x1c;
+ *(uint16_t*)0x20000082 = htobe16(0x4e20);
+ *(uint32_t*)0x20000084 = 0;
+ *(uint8_t*)0x20000088 = 0;
+ *(uint8_t*)0x20000089 = 0;
+ *(uint8_t*)0x2000008a = 0;
+ *(uint8_t*)0x2000008b = 0;
+ *(uint8_t*)0x2000008c = 0;
+ *(uint8_t*)0x2000008d = 0;
+ *(uint8_t*)0x2000008e = 0;
+ *(uint8_t*)0x2000008f = 0;
+ *(uint8_t*)0x20000090 = 0;
+ *(uint8_t*)0x20000091 = 0;
+ *(uint8_t*)0x20000092 = -1;
+ *(uint8_t*)0x20000093 = -1;
+ *(uint32_t*)0x20000094 = htobe32(0x7f000001);
+ *(uint32_t*)0x20000098 = 0;
+ syscall(SYS_bind, r[0], 0x20000080ul, 0x18ul);
+ *(uint32_t*)0x200002c0 = 0;
+ *(uint8_t*)0x200002c8 = 0x1c;
+ *(uint8_t*)0x200002c9 = 0x1c;
+ *(uint16_t*)0x200002ca = htobe16(0x4e23);
+ *(uint32_t*)0x200002cc = 4;
+ *(uint8_t*)0x200002d0 = -1;
+ *(uint8_t*)0x200002d1 = 1;
+ *(uint8_t*)0x200002d2 = 0;
+ *(uint8_t*)0x200002d3 = 0;
+ *(uint8_t*)0x200002d4 = 0;
+ *(uint8_t*)0x200002d5 = 0;
+ *(uint8_t*)0x200002d6 = 0;
+ *(uint8_t*)0x200002d7 = 0;
+ *(uint8_t*)0x200002d8 = 0;
+ *(uint8_t*)0x200002d9 = 0;
+ *(uint8_t*)0x200002da = 0;
+ *(uint8_t*)0x200002db = 0;
+ *(uint8_t*)0x200002dc = 0;
+ *(uint8_t*)0x200002dd = 0;
+ *(uint8_t*)0x200002de = 0;
+ *(uint8_t*)0x200002df = 1;
+ *(uint32_t*)0x200002e0 = 0x80000000;
+ *(uint64_t*)0x200002e8 = 0;
+ *(uint64_t*)0x200002f0 = 0;
+ *(uint64_t*)0x200002f8 = 0;
+ *(uint64_t*)0x20000300 = 0;
+ *(uint64_t*)0x20000308 = 0;
+ *(uint64_t*)0x20000310 = 0;
+ *(uint64_t*)0x20000318 = 0;
+ *(uint64_t*)0x20000320 = 0;
+ *(uint64_t*)0x20000328 = 0;
+ *(uint64_t*)0x20000330 = 0;
+ *(uint64_t*)0x20000338 = 0;
+ *(uint64_t*)0x20000340 = 0;
+ syscall(SYS_setsockopt, r[0], 0x29, 0x51, 0x200002c0ul, 0x88ul);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller18 -Wall -Wextra -O0 /tmp/syzkaller18.c ||
+ exit 1
+
+(cd ../testcases/swap; ./swap -t 1m -i 20 -h > /dev/null 2>&1) &
+start=`date +%s`
+while pgrep -q swap; do
+ (cd /tmp; timeout 1m ./syzkaller18)
+ [ $((`date +%s` - start)) -ge 60 ] && break
+done
+pkill -9 syzkaller18 swap
+wait
+
+rm -f /tmp/syzkaller18 /tmp/syzkaller18.c /tmp/syzkaller18.core
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller19.sh b/tools/test/stress2/misc/syzkaller19.sh
new file mode 100755
index 000000000000..651f444203e4
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller19.sh
@@ -0,0 +1,171 @@
+#!/bin/sh
+
+# panic: sleeping without a lock
+# cpuid = 17
+# time = 1594020733
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe013e99d6d0
+# vpanic() at vpanic+0x182/frame 0xfffffe013e99d720
+# panic() at panic+0x43/frame 0xfffffe013e99d780
+# _sleep() at _sleep+0x466/frame 0xfffffe013e99d820
+# soclose() at soclose+0x12a/frame 0xfffffe013e99d890
+# _fdrop() at _fdrop+0x1a/frame 0xfffffe013e99d8b0
+# closef() at closef+0x1db/frame 0xfffffe013e99d940
+# fdescfree_fds() at fdescfree_fds+0x3c/frame 0xfffffe013e99d990
+# fdescfree() at fdescfree+0x36c/frame 0xfffffe013e99da50
+# exit1() at exit1+0x487/frame 0xfffffe013e99dac0
+# sys_sys_exit() at sys_sys_exit+0xd/frame 0xfffffe013e99dad0
+# amd64_syscall() at amd64_syscall+0x159/frame 0xfffffe013e99dbf0
+# fast_syscall_common() at fast_syscall_common+0x101/frame 0xfffffe013e99dbf0
+# --- syscall (1, FreeBSD ELF64, sys_sys_exit), rip = 0x8004513da, rsp = 0x7fffffffe548, rbp = 0x7fffffffe560 ---
+# KDB: enter: panic
+# [ thread pid 86998 tid 100362 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10c3a16(%rip)
+# db> x/s version
+# version:FreeBSD 13.0-CURRENT #2 r362887+d0cb7ae7cb9-c248258(mlnx3): Sat
+# Jul 4 10:28:49 CEST 2020 pho@t2.osted.lan:/var/tmp/deviant2/sys/amd64/compile/PHO
+# db>
+
+[ `uname -p` != "amd64" ] && exit 0
+
+. ../default.cfg
+cat > /tmp/syzkaller19.c <<EOF
+// https://syzkaller.appspot.com/bug?id=2196001e8447f2133532abeabaea2ebc0351f67b
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+// Reported-by: syzbot+e625d92c1dd74e402c81@syzkaller.appspotmail.com
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <pwd.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter;
+ for (iter = 0;; iter++) {
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ }
+}
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+void execute_one(void)
+{
+ intptr_t res = 0;
+ res = syscall(SYS_socket, 0x1cul, 1ul, 0x84);
+ if (res != -1)
+ r[0] = res;
+ *(uint8_t*)0x20000000 = 0x1c;
+ *(uint8_t*)0x20000001 = 0x1c;
+ *(uint16_t*)0x20000002 = htobe16(0x4e22);
+ *(uint32_t*)0x20000004 = 0;
+ *(uint8_t*)0x20000008 = 0;
+ *(uint8_t*)0x20000009 = 0;
+ *(uint8_t*)0x2000000a = 0;
+ *(uint8_t*)0x2000000b = 0;
+ *(uint8_t*)0x2000000c = 0;
+ *(uint8_t*)0x2000000d = 0;
+ *(uint8_t*)0x2000000e = 0;
+ *(uint8_t*)0x2000000f = 0;
+ *(uint8_t*)0x20000010 = 0;
+ *(uint8_t*)0x20000011 = 0;
+ *(uint8_t*)0x20000012 = 0;
+ *(uint8_t*)0x20000013 = 0;
+ *(uint8_t*)0x20000014 = 0;
+ *(uint8_t*)0x20000015 = 0;
+ *(uint8_t*)0x20000016 = 0;
+ *(uint8_t*)0x20000017 = 0;
+ *(uint32_t*)0x20000018 = 0;
+ syscall(SYS_bind, r[0], 0x20000000ul, 0x1cul);
+ *(uint8_t*)0x20000180 = 0x1c;
+ *(uint8_t*)0x20000181 = 0x1c;
+ *(uint16_t*)0x20000182 = htobe16(0x4e22);
+ *(uint32_t*)0x20000184 = 0;
+ *(uint64_t*)0x20000188 = htobe64(0);
+ *(uint64_t*)0x20000190 = htobe64(1);
+ *(uint32_t*)0x20000198 = 0;
+ syscall(SYS_connect, r[0], 0x20000180ul, 0x1cul);
+ *(uint64_t*)0x20000080 = 0;
+ *(uint32_t*)0x20000088 = 0;
+ *(uint64_t*)0x20000090 = 0;
+ *(uint64_t*)0x20000098 = 0;
+ *(uint64_t*)0x200000a0 = 0;
+ *(uint64_t*)0x200000a8 = 0x60;
+ *(uint32_t*)0x200000b0 = 0;
+ syscall(SYS_sendmsg, r[0], 0x20000080ul, 0x10cul);
+ *(uint32_t*)0x200000c0 = 0xbd;
+ *(uint32_t*)0x200000c4 = 0;
+ syscall(SYS_setsockopt, r[0], 0xffff, 0x80, 0x200000c0ul, 8ul);
+}
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ loop();
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller19 -Wall -Wextra -O2 /tmp/syzkaller19.c -lpthread ||
+ exit 1
+
+(cd ../testcases/swap; ./swap -t 1m -i 20 -h > /dev/null 2>&1) &
+(cd /tmp; ./syzkaller19) &
+sleep 60
+pkill -9 syzkaller19 swap
+wait
+
+rm -f /tmp/syzkaller19 /tmp/syzkaller19.c /tmp/syzkaller19.core
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller2.sh b/tools/test/stress2/misc/syzkaller2.sh
new file mode 100755
index 000000000000..5ba3ca63dbd0
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller2.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+
+# panic: mutex pcbinfohash not owned at
+# /syzkaller/managers/main/kernel/sys/netinet6/in6_pcb.c:717
+# cpuid = 0
+# time = 1573247472
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x47/frame
+# 0xfffffe0022a56260
+# vpanic() at vpanic+0x1c7/frame 0xfffffe0022a562d0
+# panic() at panic+0x43/frame 0xfffffe0022a56330
+# __mtx_assert() at __mtx_assert+0x18b/frame 0xfffffe0022a56370
+# in6_pcblookup_local() at in6_pcblookup_local+0x53/frame 0xfffffe0022a563c0
+# in_pcb_lport() at in_pcb_lport+0x3fd/frame 0xfffffe0022a56450
+# in_pcbbind_setup() at in_pcbbind_setup+0x28b/frame 0xfffffe0022a564f0
+# in_pcbconnect_setup() at in_pcbconnect_setup+0x4aa/frame 0xfffffe0022a565b0
+# udp_send() at udp_send+0xee4/frame 0xfffffe0022a566c0
+# udp6_send() at udp6_send+0x4e8/frame 0xfffffe0022a56870
+# sosend_dgram() at sosend_dgram+0x54f/frame 0xfffffe0022a568e0
+# sosend() at sosend+0xc6/frame 0xfffffe0022a56950
+# kern_sendit() at kern_sendit+0x32d/frame 0xfffffe0022a56a00
+# sendit() at sendit+0x226/frame 0xfffffe0022a56a60
+# sys_sendto() at sys_sendto+0x5c/frame 0xfffffe0022a56ac0
+# amd64_syscall() at amd64_syscall+0x473/frame 0xfffffe0022a56bf0
+# fast_syscall_common() at fast_syscall_common+0x101/frame 0xfffffe0022a56bf0
+# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x41c3aa, rsp =
+
+. ../default.cfg
+cat > /tmp/syzkaller2.c <<EOF
+// https://syzkaller.appspot.com/bug?id=062e9fde55f117bec30836a4ef1ef121f51faf23
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+
+#define _GNU_SOURCE
+
+#include <pwd.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul);
+ intptr_t res = 0;
+ res = syscall(SYS_socket, 0x1cul, 2ul, 0ul);
+ if (res != -1)
+ r[0] = res;
+ *(uint32_t*)0x200001c0 = 0;
+ syscall(SYS_setsockopt, r[0], 0x29ul, 0x1bul, 0x200001c0ul, 4ul);
+ *(uint8_t*)0x20000100 = 0x1c;
+ *(uint8_t*)0x20000101 = 0x1c;
+ *(uint16_t*)0x20000102 = htobe16(0x4e21);
+ *(uint32_t*)0x20000104 = 0;
+ *(uint8_t*)0x20000108 = 0;
+ *(uint8_t*)0x20000109 = 0;
+ *(uint8_t*)0x2000010a = 0;
+ *(uint8_t*)0x2000010b = 0;
+ *(uint8_t*)0x2000010c = 0;
+ *(uint8_t*)0x2000010d = 0;
+ *(uint8_t*)0x2000010e = 0;
+ *(uint8_t*)0x2000010f = 0;
+ *(uint8_t*)0x20000110 = 0;
+ *(uint8_t*)0x20000111 = 0;
+ *(uint8_t*)0x20000112 = -1;
+ *(uint8_t*)0x20000113 = -1;
+ *(uint8_t*)0x20000114 = 0xac;
+ *(uint8_t*)0x20000115 = 0x14;
+ *(uint8_t*)0x20000116 = 0;
+ *(uint8_t*)0x20000117 = 0xaa;
+ *(uint32_t*)0x20000118 = 0;
+ syscall(SYS_sendto, r[0], 0ul, 0ul, 0ul, 0x20000100ul, 0x1cul);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller2 -Wall -Wextra -O2 /tmp/syzkaller2.c ||
+ exit 1
+rm /tmp/syzkaller2.c
+
+(cd /tmp; ./syzkaller2)
+
+rm -f /tmp/syzkaller2 /tmp/syzkaller2.core
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller20.sh b/tools/test/stress2/misc/syzkaller20.sh
new file mode 100755
index 000000000000..43c961983bf1
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller20.sh
@@ -0,0 +1,108 @@
+#!/bin/sh
+
+# Fatal trap 12: page fault while in kernel mode
+# cpuid = 0; apic id = 00
+# fault virtual address = 0x70
+# fault code = supervisor read data, page not present
+# instruction pointer = 0x20:0xffffffff80dec3cf
+# stack pointer = 0x28:0xfffffe013c9384d0
+# frame pointer = 0x28:0xfffffe013c938510
+# code segment = base 0x0, limit 0xfffff, type 0x1b
+# = DPL 0, pres 1, long 1, def32 0, gran 1
+# processor eflags = interrupt enabled, resume, IOPL = 0
+# current process = 58092 (syzkaller20)
+# trap number = 12
+# panic: page fault
+# cpuid = 0
+# time = 1596109014
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe013c938180
+# vpanic() at vpanic+0x182/frame 0xfffffe013c9381d0
+# panic() at panic+0x43/frame 0xfffffe013c938230
+# trap_fatal() at trap_fatal+0x387/frame 0xfffffe013c938290
+# trap_pfault() at trap_pfault+0x99/frame 0xfffffe013c9382f0
+# trap() at trap+0x2a5/frame 0xfffffe013c938400
+# calltrap() at calltrap+0x8/frame 0xfffffe013c938400
+# --- trap 0xc, rip = 0xffffffff80dec3cf, rsp = 0xfffffe013c9384d0, rbp = 0xfffffe013c938510 ---
+# in6_setscope() at in6_setscope+0x5f/frame 0xfffffe013c938510
+# ip6_output() at ip6_output+0x103c/frame 0xfffffe013c938750
+# udp6_send() at udp6_send+0x79b/frame 0xfffffe013c938900
+# sosend_dgram() at sosend_dgram+0x34e/frame 0xfffffe013c938960
+# sosend() at sosend+0x66/frame 0xfffffe013c938990
+# kern_sendit() at kern_sendit+0x246/frame 0xfffffe013c938a30
+# sendit() at sendit+0x1d8/frame 0xfffffe013c938a80
+# sys_sendto() at sys_sendto+0x4d/frame 0xfffffe013c938ad0
+# amd64_syscall() at amd64_syscall+0x159/frame 0xfffffe013c938bf0
+# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe013c938bf0
+# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x80045513a, rsp = 0x7fffffffe568, rbp = 0x7fffffffe580 ---
+# KDB: enter: panic
+# [ thread pid 58092 tid 100337 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10b8de6(%rip)
+# db> x/s version
+# version: FreeBSD 13.0-CURRENT #0 r363687: Thu Jul 30 08:55:21 CEST 2020
+# pho@t2.osted.lan:/usr/src/sys/amd64/compile/PHO
+# db>
+
+[ `uname -p` != "amd64" ] && exit 0
+
+. ../default.cfg
+cat > /tmp/syzkaller20.c <<EOF
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+
+#define _GNU_SOURCE
+
+#include <pwd.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+uint64_t r[2] = {0xffffffffffffffff, 0xffffffffffffffff};
+
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ intptr_t res = 0;
+ res = syscall(SYS_socket, 0x1cul, 1ul, 0);
+ if (res != -1)
+ r[0] = res;
+ res = syscall(SYS_socket, 0x1cul, 2ul, 0x88);
+ if (res != -1)
+ r[1] = res;
+ syscall(SYS_dup2, r[1], r[0]);
+ *(uint8_t*)0x20000000 = 0x1c;
+ *(uint8_t*)0x20000001 = 0x1c;
+ *(uint16_t*)0x20000002 = htobe16(0x4e21);
+ *(uint32_t*)0x20000004 = 0;
+ *(uint64_t*)0x20000008 = htobe64(0);
+ *(uint64_t*)0x20000010 = htobe64(1);
+ *(uint32_t*)0x20000018 = 0;
+ syscall(SYS_bind, r[0], 0x20000000ul, 0x1cul);
+ *(uint8_t*)0x200002c0 = 0x1c;
+ *(uint8_t*)0x200002c1 = 0x1c;
+ *(uint16_t*)0x200002c2 = htobe16(0x4e23);
+ *(uint32_t*)0x200002c4 = 0;
+ memcpy((void*)0x200002c8,
+ "\xff\x71\x4e\x39\x01\x68\x0f\x3c\xd3\x1d\xb7\x61\x2c\x26\x8b\xac",
+ 16);
+ *(uint32_t*)0x200002d8 = 0;
+ syscall(SYS_sendto, r[0], 0ul, 0ul, 0ul, 0x200002c0ul, 0x1cul);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller20 -Wall -Wextra -O2 /tmp/syzkaller20.c -lpthread ||
+ exit 1
+
+(cd ../testcases/swap; ./swap -t 1m -i 20 -h > /dev/null 2>&1) &
+(cd /tmp; ./syzkaller20) &
+sleep 60
+pkill -9 syzkaller20 swap
+wait
+
+rm -f /tmp/syzkaller20 /tmp/syzkaller20.c /tmp/syzkaller20.core
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller21.sh b/tools/test/stress2/misc/syzkaller21.sh
new file mode 100755
index 000000000000..3a7eeb05ef6c
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller21.sh
@@ -0,0 +1,403 @@
+#!/bin/sh
+
+# $ pgrep syzkaller21 | xargs ps -lHp
+# UID PID PPID CPU PRI NI VSZ RSS MWCHAN STAT TT TIME COMMAND
+# 0 3891 1 0 20 0 29828 2852 - T 0 0:00,14 ./syzkaller21
+# 0 3891 1 0 52 0 29828 2852 ufs T 0 0:00,00 ./syzkaller21
+# 0 3891 1 0 52 0 29828 2852 ufs T 0 0:00,00 ./syzkaller21
+# $ pgrep syzkaller21 | xargs procstat -k
+# PID TID COMM TDNAME KSTACK
+# 3891 100250 syzkaller21 - mi_switch thread_suspend_switch thread_single exit1 sys_sys_exit amd64_syscall fast_syscall_common
+# 3891 100777 syzkaller21 - mi_switch sleepq_switch sleeplk lockmgr_xlock_hard ffs_lock VOP_LOCK1_APV _vn_lock vget_finish vfs_hash_get ffs_vgetf softdep_sync_buf ffs_syncvnode ffs_fsync VOP_FSYNC_APV kern_fsync amd64_syscall fast_syscall_common
+# 3891 100778 syzkaller21 - mi_switch sleepq_switch sleeplk lockmgr_slock_hard ffs_lock VOP_LOCK1_APV _vn_lock vget_finish cache_lookup vfs_cache_lookup VOP_LOOKUP_APV lookup namei kern_chdir amd64_syscall fast_syscall_common
+# $ uname -a
+# FreeBSD t2.osted.lan 13.0-CURRENT FreeBSD 13.0-CURRENT #5 r363786M: Tue Aug 4 16:51:52 CEST 2020
+# pho@t2.osted.lan:/usr/src/sys/amd64/compile/PHO amd64
+# $
+
+[ `uname -p` != "amd64" ] && exit 0
+
+. ../default.cfg
+cat > /tmp/syzkaller21.c <<EOF
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <dirent.h>
+#include <errno.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static __thread int skip_segv;
+static __thread jmp_buf segv_env;
+
+static void segv_handler(int sig, siginfo_t* info, void* ctx __unused)
+{
+ uintptr_t addr = (uintptr_t)info->si_addr;
+ const uintptr_t prog_start = 1 << 20;
+ const uintptr_t prog_end = 100 << 20;
+ if (__atomic_load_n(&skip_segv, __ATOMIC_RELAXED) &&
+ (addr < prog_start || addr > prog_end)) {
+ _longjmp(segv_env, 1);
+ }
+ exit(sig);
+}
+
+static void install_segv_handler(void)
+{
+ struct sigaction sa;
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_sigaction = segv_handler;
+ sa.sa_flags = SA_NODEFER | SA_SIGINFO;
+ sigaction(SIGSEGV, &sa, NULL);
+ sigaction(SIGBUS, &sa, NULL);
+}
+
+#define NONFAILING(...) \
+ { \
+ __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \
+ if (_setjmp(segv_env) == 0) { \
+ __VA_ARGS__; \
+ } \
+ __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \
+ }
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void use_temporary_dir(void)
+{
+ char tmpdir_template[] = "./syzkaller.XXXXXX";
+ char* tmpdir = mkdtemp(tmpdir_template);
+ if (!tmpdir)
+ exit(1);
+ if (chmod(tmpdir, 0777))
+ exit(1);
+ if (chdir(tmpdir))
+ exit(1);
+}
+
+static void remove_dir(const char* dir)
+{
+ DIR* dp;
+ struct dirent* ep;
+ dp = opendir(dir);
+ if (dp == NULL)
+ exit(1);
+ while ((ep = readdir(dp))) {
+ if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
+ continue;
+ char filename[FILENAME_MAX];
+ snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name);
+ struct stat st;
+ if (lstat(filename, &st))
+ exit(1);
+ if (S_ISDIR(st.st_mode)) {
+ remove_dir(filename);
+ continue;
+ }
+ if (unlink(filename))
+ exit(1);
+ }
+ closedir(dp);
+ if (rmdir(dir))
+ exit(1);
+}
+
+static void thread_start(void* (*fn)(void*), void* arg)
+{
+ pthread_t th;
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ pthread_attr_setstacksize(&attr, 128 << 10);
+ int i;
+ for (i = 0; i < 100; i++) {
+ if (pthread_create(&th, &attr, fn, arg) == 0) {
+ pthread_attr_destroy(&attr);
+ return;
+ }
+ if (errno == EAGAIN) {
+ usleep(50);
+ continue;
+ }
+ break;
+ }
+ exit(1);
+}
+
+typedef struct {
+ pthread_mutex_t mu;
+ pthread_cond_t cv;
+ int state;
+} event_t;
+
+static void event_init(event_t* ev)
+{
+ if (pthread_mutex_init(&ev->mu, 0))
+ exit(1);
+ if (pthread_cond_init(&ev->cv, 0))
+ exit(1);
+ ev->state = 0;
+}
+
+static void event_reset(event_t* ev)
+{
+ ev->state = 0;
+}
+
+static void event_set(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ if (ev->state)
+ exit(1);
+ ev->state = 1;
+ pthread_mutex_unlock(&ev->mu);
+ pthread_cond_broadcast(&ev->cv);
+}
+
+static void event_wait(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ while (!ev->state)
+ pthread_cond_wait(&ev->cv, &ev->mu);
+ pthread_mutex_unlock(&ev->mu);
+}
+
+static int event_isset(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+static int event_timedwait(event_t* ev, uint64_t timeout)
+{
+ uint64_t start = current_time_ms();
+ uint64_t now = start;
+ pthread_mutex_lock(&ev->mu);
+ for (;;) {
+ if (ev->state)
+ break;
+ uint64_t remain = timeout - (now - start);
+ struct timespec ts;
+ ts.tv_sec = remain / 1000;
+ ts.tv_nsec = (remain % 1000) * 1000 * 1000;
+ pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
+ now = current_time_ms();
+ if (now - start > timeout)
+ break;
+ }
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+struct thread_t {
+ int created, call;
+ event_t ready, done;
+};
+
+static struct thread_t threads[16];
+static void execute_call(int call);
+static int running;
+
+static void* thr(void* arg)
+{
+ struct thread_t* th = (struct thread_t*)arg;
+ for (;;) {
+ event_wait(&th->ready);
+ event_reset(&th->ready);
+ execute_call(th->call);
+ __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->done);
+ }
+ return 0;
+}
+
+static void execute_one(void)
+{
+ int i, call, thread;
+ int collide = 0;
+again:
+ for (call = 0; call < 9; call++) {
+ for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
+ thread++) {
+ struct thread_t* th = &threads[thread];
+ if (!th->created) {
+ th->created = 1;
+ event_init(&th->ready);
+ event_init(&th->done);
+ event_set(&th->done);
+ thread_start(thr, th);
+ }
+ if (!event_isset(&th->done))
+ continue;
+ event_reset(&th->done);
+ th->call = call;
+ __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->ready);
+ if (collide && (call % 2) == 0)
+ break;
+ event_timedwait(&th->done, 45);
+ break;
+ }
+ }
+ for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
+ sleep_ms(1);
+ if (!collide) {
+ collide = 1;
+ goto again;
+ }
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter;
+ for (iter = 0;; iter++) {
+ char cwdbuf[32];
+ sprintf(cwdbuf, "./%d", iter);
+ if (mkdir(cwdbuf, 0777))
+ exit(1);
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ if (chdir(cwdbuf))
+ exit(1);
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ remove_dir(cwdbuf);
+ }
+}
+
+uint64_t r[4] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff,
+ 0xffffffffffffffff};
+
+void execute_call(int call)
+{
+ intptr_t res = 0;
+ switch (call) {
+ case 0:
+ NONFAILING(memcpy((void*)0x20000000, ".\000", 2));
+ res = syscall(SYS_open, 0x20000000ul, 0ul, 0ul);
+ if (res != -1)
+ r[0] = res;
+ break;
+ case 1:
+ NONFAILING(memcpy((void*)0x20000040, "./file0\000", 8));
+ syscall(SYS_mkdirat, r[0], 0x20000040ul, 0ul);
+ break;
+ case 2:
+ NONFAILING(memcpy((void*)0x20000000, ".\000", 2));
+ res = syscall(SYS_open, 0x20000000ul, 0ul, 0ul);
+ if (res != -1)
+ r[1] = res;
+ break;
+ case 3:
+ NONFAILING(memcpy((void*)0x20000040, "./file1\000", 8));
+ syscall(SYS_mkdirat, r[1], 0x20000040ul, 0ul);
+ break;
+ case 4:
+ NONFAILING(memcpy((void*)0x20000000, ".\000", 2));
+ res = syscall(SYS_open, 0x20000000ul, 0ul, 0ul);
+ if (res != -1)
+ r[2] = res;
+ break;
+ case 5:
+ NONFAILING(memcpy((void*)0x20000080, "./file1\000", 8));
+ NONFAILING(memcpy((void*)0x200000c0, "./file0/file0\000", 14));
+ syscall(SYS_renameat, r[1], 0x20000080ul, r[2], 0x200000c0ul);
+ break;
+ case 6:
+ NONFAILING(memcpy((void*)0x20000100, "./file0/file0\000", 14));
+ res = syscall(SYS_open, 0x20000100ul, 0ul, 0ul);
+ if (res != -1)
+ r[3] = res;
+ break;
+ case 7:
+ syscall(SYS_fsync, r[3]);
+ break;
+ case 8:
+ NONFAILING(memcpy((void*)0x20000140, "./file0/file0\000", 14));
+ syscall(SYS_chdir, 0x20000140ul);
+ break;
+ }
+}
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ install_segv_handler();
+ use_temporary_dir();
+ loop();
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller21 -Wall -Wextra -O2 /tmp/syzkaller21.c -lpthread ||
+ exit 1
+
+(cd ../testcases/swap; ./swap -t 1m -i 20 -h > /dev/null 2>&1) &
+(cd /tmp; ./syzkaller21) &
+sleep 60
+while pkill swap; do sleep .2; done
+pkill -9 syzkaller21
+sleep .5
+if pgrep -q syzkaller21; then
+ pgrep syzkaller21 | xargs ps -lHp
+ pgrep syzkaller21 | xargs procstat -k
+ exit 1
+fi
+wait
+
+rm -rf /tmp/syzkaller21.*
+rm -f /tmp/syzkaller21
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller22.sh b/tools/test/stress2/misc/syzkaller22.sh
new file mode 100755
index 000000000000..90dd1f4c326d
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller22.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+# panic: Assertion *buflen >= 2 failed at ../../../kern/vfs_cache.c:2837
+# cpuid = 21
+# time = 1598296274
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe0149a9e860
+# vpanic() at vpanic+0x182/frame 0xfffffe0149a9e8b0
+# panic() at panic+0x43/frame 0xfffffe0149a9e910
+# vn_fullpath_any_smr() at vn_fullpath_any_smr+0x4df/frame 0xfffffe0149a9e980
+# sys___realpathat() at sys___realpathat+0x20b/frame 0xfffffe0149a9ead0
+# amd64_syscall() at amd64_syscall+0x159/frame 0xfffffe0149a9ebf0
+# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe0149a9ebf0
+# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x8004294ea, rsp = 0x7fffffffe548, rbp = 0x7fffffffe580 ---
+# KDB: enter: panic
+# [ thread pid 2831 tid 100306 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10b4246(%rip)
+# db> x/s version
+# version: FreeBSD 13.0-CURRENT #0 r364722: Mon Aug 24 21:04:09 CEST 2020
+# pho@t2.osted.lan:/usr/src/sys/amd64/compile/PHO\012
+# db>
+
+[ `uname -p` != "amd64" ] && exit 0
+
+# Obtained from markj (syzkaller).
+# Fixed by r364723.
+
+. ../default.cfg
+kldstat -v | grep -q sctp || kldload sctp.ko
+cat > /tmp/syzkaller22.c <<EOF
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+
+#define _GNU_SOURCE
+
+#include <pwd.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ intptr_t res = 0;
+ memcpy((void*)0x20000000, "./file0\000", 8);
+ syscall(SYS_open, 0x20000000ul, 0x200645ul, 0ul);
+ memcpy((void*)0x20000000, ".\000", 2);
+ res = syscall(SYS_open, 0x20000000ul, 0ul, 0ul);
+ if (res != -1)
+ r[0] = res;
+ memcpy((void*)0x20000240, "./file0\000", 8);
+ syscall(SYS___realpathat, r[0], 0x20000240ul, 0x20000280ul, 8ul, 0ul);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller22 -Wall -Wextra -O0 /tmp/syzkaller22.c ||
+ exit 1
+
+(cd /tmp; timeout 1m ./syzkaller22)
+
+rm -f /tmp/syzkaller22 /tmp/syzkaller22.c /tmp/syzkaller22.core /tmp/file0
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller23.sh b/tools/test/stress2/misc/syzkaller23.sh
new file mode 100755
index 000000000000..917093052265
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller23.sh
@@ -0,0 +1,400 @@
+#!/bin/sh
+
+# panic: Bad link elm 0xfffff80b62eb7500 prev->next != elm
+# cpuid = 2
+# time = 1598546886
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe010154a6e0
+# vpanic() at vpanic+0x182/frame 0xfffffe010154a730
+# panic() at panic+0x43/frame 0xfffffe010154a790
+# unp_disconnect() at unp_disconnect+0x279/frame 0xfffffe010154a7c0
+# uipc_disconnect() at uipc_disconnect+0xfd/frame 0xfffffe010154a800
+# soclose() at soclose+0xa2/frame 0xfffffe010154a870
+# _fdrop() at _fdrop+0x1a/frame 0xfffffe010154a890
+# closef() at closef+0x1ea/frame 0xfffffe010154a920
+# fdescfree_fds() at fdescfree_fds+0x8c/frame 0xfffffe010154a980
+# fdescfree() at fdescfree+0x3cd/frame 0xfffffe010154aa50
+# exit1() at exit1+0x487/frame 0xfffffe010154aac0
+# sys_sys_exit() at sys_sys_exit+0xd/frame 0xfffffe010154aad0
+# amd64_syscall() at amd64_syscall+0x159/frame 0xfffffe010154abf0
+# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe010154abf0
+# --- syscall (1, FreeBSD ELF64, sys_sys_exit), rip = 0x80045376a, rsp = 0x7fffffffe4c8, rbp = 0x7fffffffe4e0 ---
+
+[ `uname -p` != "amd64" ] && exit 0
+
+# Obtained from markj (syzkaller).
+
+. ../default.cfg
+cat > /tmp/syzkaller23.c <<EOF
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <dirent.h>
+#include <errno.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static __thread int skip_segv;
+static __thread jmp_buf segv_env;
+
+static void segv_handler(int sig, siginfo_t* info, void* ctx __unused)
+{
+ uintptr_t addr = (uintptr_t)info->si_addr;
+ const uintptr_t prog_start = 1 << 20;
+ const uintptr_t prog_end = 100 << 20;
+ int skip = __atomic_load_n(&skip_segv, __ATOMIC_RELAXED) != 0;
+ int valid = addr < prog_start || addr > prog_end;
+ if (sig == SIGBUS) {
+ valid = 1;
+ }
+ if (skip && valid) {
+ _longjmp(segv_env, 1);
+ }
+ exit(sig);
+}
+
+static void install_segv_handler(void)
+{
+ struct sigaction sa;
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_sigaction = segv_handler;
+ sa.sa_flags = SA_NODEFER | SA_SIGINFO;
+ sigaction(SIGSEGV, &sa, NULL);
+ sigaction(SIGBUS, &sa, NULL);
+}
+
+#define NONFAILING(...) \
+ { \
+ __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \
+ if (_setjmp(segv_env) == 0) { \
+ __VA_ARGS__; \
+ } \
+ __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \
+ }
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void use_temporary_dir(void)
+{
+ char tmpdir_template[] = "./syzkaller.XXXXXX";
+ char* tmpdir = mkdtemp(tmpdir_template);
+ if (!tmpdir)
+ exit(1);
+ if (chmod(tmpdir, 0777))
+ exit(1);
+ if (chdir(tmpdir))
+ exit(1);
+}
+
+static void remove_dir(const char* dir)
+{
+ DIR* dp = opendir(dir);
+ if (dp == NULL)
+ exit(1);
+ struct dirent* ep = 0;
+ while ((ep = readdir(dp))) {
+ if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
+ continue;
+ char filename[FILENAME_MAX];
+ snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name);
+ struct stat st;
+ if (lstat(filename, &st))
+ exit(1);
+ if (S_ISDIR(st.st_mode)) {
+ remove_dir(filename);
+ continue;
+ }
+ if (unlink(filename))
+ exit(1);
+ }
+ closedir(dp);
+ if (rmdir(dir))
+ exit(1);
+}
+
+static void thread_start(void* (*fn)(void*), void* arg)
+{
+ pthread_t th;
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ pthread_attr_setstacksize(&attr, 128 << 10);
+ int i = 0;
+ for (; i < 100; i++) {
+ if (pthread_create(&th, &attr, fn, arg) == 0) {
+ pthread_attr_destroy(&attr);
+ return;
+ }
+ if (errno == EAGAIN) {
+ usleep(50);
+ continue;
+ }
+ break;
+ }
+ exit(1);
+}
+
+typedef struct {
+ pthread_mutex_t mu;
+ pthread_cond_t cv;
+ int state;
+} event_t;
+
+static void event_init(event_t* ev)
+{
+ if (pthread_mutex_init(&ev->mu, 0))
+ exit(1);
+ if (pthread_cond_init(&ev->cv, 0))
+ exit(1);
+ ev->state = 0;
+}
+
+static void event_reset(event_t* ev)
+{
+ ev->state = 0;
+}
+
+static void event_set(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ if (ev->state)
+ exit(1);
+ ev->state = 1;
+ pthread_mutex_unlock(&ev->mu);
+ pthread_cond_broadcast(&ev->cv);
+}
+
+static void event_wait(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ while (!ev->state)
+ pthread_cond_wait(&ev->cv, &ev->mu);
+ pthread_mutex_unlock(&ev->mu);
+}
+
+static int event_isset(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+static int event_timedwait(event_t* ev, uint64_t timeout)
+{
+ uint64_t start = current_time_ms();
+ uint64_t now = start;
+ pthread_mutex_lock(&ev->mu);
+ for (;;) {
+ if (ev->state)
+ break;
+ uint64_t remain = timeout - (now - start);
+ struct timespec ts;
+ ts.tv_sec = remain / 1000;
+ ts.tv_nsec = (remain % 1000) * 1000 * 1000;
+ pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
+ now = current_time_ms();
+ if (now - start > timeout)
+ break;
+ }
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+struct thread_t {
+ int created, call;
+ event_t ready, done;
+};
+
+static struct thread_t threads[16];
+static void execute_call(int call);
+static int running;
+
+static void* thr(void* arg)
+{
+ struct thread_t* th = (struct thread_t*)arg;
+ for (;;) {
+ event_wait(&th->ready);
+ event_reset(&th->ready);
+ execute_call(th->call);
+ __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->done);
+ }
+ return 0;
+}
+
+static void execute_one(void)
+{
+ int i, call, thread;
+ int collide = 0;
+again:
+ for (call = 0; call < 5; call++) {
+ for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
+ thread++) {
+ struct thread_t* th = &threads[thread];
+ if (!th->created) {
+ th->created = 1;
+ event_init(&th->ready);
+ event_init(&th->done);
+ event_set(&th->done);
+ thread_start(thr, th);
+ }
+ if (!event_isset(&th->done))
+ continue;
+ event_reset(&th->done);
+ th->call = call;
+ __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->ready);
+ if (collide && (call % 2) == 0)
+ break;
+ event_timedwait(&th->done, 45);
+ break;
+ }
+ }
+ for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
+ sleep_ms(1);
+ if (!collide) {
+ collide = 1;
+ goto again;
+ }
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter = 0;
+ for (;; iter++) {
+ char cwdbuf[32];
+ sprintf(cwdbuf, "./%d", iter);
+ if (mkdir(cwdbuf, 0777))
+ exit(1);
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ if (chdir(cwdbuf))
+ exit(1);
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ remove_dir(cwdbuf);
+ }
+}
+
+uint64_t r[2] = {0xffffffffffffffff, 0xffffffffffffffff};
+
+void execute_call(int call)
+{
+ intptr_t res = 0;
+ switch (call) {
+ case 0:
+ NONFAILING(memcpy((void*)0x20000000, ".\000", 2));
+ res = syscall(SYS_open, 0x20000000ul, 0ul, 0ul);
+ if (res != -1)
+ r[0] = res;
+ break;
+ case 1:
+ res = syscall(SYS_socketpair, 1ul, 2ul, 0, 0x20000100ul);
+ if (res != -1)
+ NONFAILING(r[1] = *(uint32_t*)0x20000100);
+ break;
+ case 2:
+ NONFAILING(memcpy(
+ (void*)0x20000180,
+ "\x0a\x01\x2e\x2f\x66\x69\x6c\x65\x30\x00\x46\xd3\xc5\xea\xb2\xf9\xd0"
+ "\x81\x73\xef\x4a\xd0\xe9\x00\x9b\x10\xb2\x6d\x40\xab\xf3\x06\x35\x55"
+ "\x92\x6b\xa7\x48\x51\xed\xf1\x47\x2a\x1c\x57\xda\x98\x4d\x23\x4f\x6d"
+ "\x63\x57\x71\x99\x01\x97\x42\xef\xe9\xd6\x91\x78\x55\x43\x7f\x80\x8b"
+ "\xf3\xfa\x29\xeb\x62\xeb\x2f\x4b\xb5\xcd\x3f\xd7\x4f\x78\x35\xfb\xd3"
+ "\x24\xe3\xc4\x10\x31\x53\xdd\xe7\x9a\x1c\x23\x36\x48\xe4\x7a\xf8\x3a"
+ "\x7e\xd0\x1f\x89\xf7\xb6\xee\xff\x69\x1d\xad\x02\x57\x01\xf2\x52\x93"
+ "\x3e\x6b\xda\x65\x2a\xc7\x6c\x27\xf9\x0a\xd4\x3b\x05\x00\x00\x00\x1b"
+ "\x1c\x89\xc9\x3b\x41\x9c\x7b\x4f\x31\xbb\x6a\x5c\x17\xed\x78\xf1\x64"
+ "\xc4\xa3\x17\x5c\x7a\x8c\x02\x75\x00\xaf\x61\xac\x90\x22\x86\x15\x67"
+ "\x90\x03\x17\x8c\x55\xf6\xb9\x3a\xf8\x6c\xab\xcb\xf1\x74\x09\xc0\xbc"
+ "\xf4\x5b\x76\x33\x8f\x9a\x76\xf3\x6e",
+ 196));
+ syscall(SYS_bindat, r[0], r[1], 0x20000180ul, 0xaul);
+ break;
+ case 3:
+ NONFAILING(*(uint8_t*)0x20000040 = 0xa);
+ NONFAILING(*(uint8_t*)0x20000041 = 1);
+ NONFAILING(memcpy((void*)0x20000042, "./file0\000", 8));
+ syscall(SYS_connectat, r[0], r[1], 0x20000040ul, 0xaul);
+ break;
+ case 4:
+ NONFAILING(*(uint8_t*)0x20000000 = 0xa);
+ NONFAILING(*(uint8_t*)0x20000001 = 1);
+ NONFAILING(memcpy((void*)0x20000002, "./file0\000", 8));
+ syscall(SYS_connect, r[1], 0x20000000ul, 0xaul);
+ break;
+ }
+}
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ install_segv_handler();
+ use_temporary_dir();
+ loop();
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller23 -Wall -Wextra -O0 /tmp/syzkaller23.c -lpthread ||
+ exit 1
+
+for i in `jot 256`; do
+ (cd /tmp; timeout 3m ./syzkaller23) &
+done
+wait
+
+rm -rf /tmp/syzkaller23 /tmp/syzkaller23.c /tmp/syzkaller.*
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller24.sh b/tools/test/stress2/misc/syzkaller24.sh
new file mode 100755
index 000000000000..12a816598b8f
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller24.sh
@@ -0,0 +1,133 @@
+#!/bin/sh
+
+# panic: sleepq_add: td 0xfffffe00dffd8800 to sleep on wchan 0xffffffff81d9bee8 with sleeping prohibited
+# cpuid = 7
+# time = 1599685416
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe00e656d5e0
+# vpanic() at vpanic+0x182/frame 0xfffffe00e656d630
+# panic() at panic+0x43/frame 0xfffffe00e656d690
+# sleepq_add() at sleepq_add+0x359/frame 0xfffffe00e656d6e0
+# _sx_xlock_hard() at _sx_xlock_hard+0x405/frame 0xfffffe00e656d780
+# _sx_xlock() at _sx_xlock+0xba/frame 0xfffffe00e656d7c0
+# inp_setmoptions() at inp_setmoptions+0xd5c/frame 0xfffffe00e656d970
+# ip_ctloutput() at ip_ctloutput+0x103/frame 0xfffffe00e656d9c0
+# rip_ctloutput() at rip_ctloutput+0x158/frame 0xfffffe00e656d9f0
+# sosetopt() at sosetopt+0xed/frame 0xfffffe00e656da50
+# kern_setsockopt() at kern_setsockopt+0xa8/frame 0xfffffe00e656dab0
+# sys_setsockopt() at sys_setsockopt+0x24/frame 0xfffffe00e656dad0
+# amd64_syscall() at amd64_syscall+0x159/frame 0xfffffe00e656dbf0
+# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe00e656dbf0
+# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x80042819a, rsp = 0x7fffffffe958, rbp = 0x7fffffffe970 ---
+# KDB: enter: panic
+# [ thread pid 27447 tid 100183 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10b7246(%rip)
+# db> x/s version
+# version: FreeBSD 13.0-CURRENT #0 r365508: Wed Sep 9 21:36:20 CEST 2020
+# pho@mercat1.netperf.freebsd.org:/usr/src/sys/amd64/compile/PHO
+# db>
+
+# Fixed by r366795.
+
+[ `uname -p` != "amd64" ] && exit 0
+
+. ../default.cfg
+cat > /tmp/syzkaller24.c <<EOF
+// https://syzkaller.appspot.com/bug?id=0f4996e03d94b16bb9e945ede3c9de343748e6ed
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+// Reported-by: syzbot+34f419947239a738ed34@syzkaller.appspotmail.com
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <pwd.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter = 0;
+ for (;; iter++) {
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ }
+}
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+void execute_one(void)
+{
+ intptr_t res = 0;
+ res = syscall(SYS_socket, 2ul, 3ul, 1);
+ if (res != -1)
+ r[0] = res;
+ *(uint32_t*)0x20000180 = htobe32(0xe0000002);
+ *(uint32_t*)0x20000184 = htobe32(0x7f000001);
+ *(uint32_t*)0x20000188 = htobe32(0x7f000001);
+ syscall(SYS_setsockopt, r[0], 0, 0x46, 0x20000180ul, 0x4414ae3732a778ful);
+}
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ loop();
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller24 -Wall -Wextra -O0 /tmp/syzkaller24.c ||
+ exit 1
+
+(cd /tmp; timeout 3m ./syzkaller24)
+
+rm -rf /tmp/syzkaller24 /tmp/syzkaller24.c /tmp/syzkaller.*
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller25.sh b/tools/test/stress2/misc/syzkaller25.sh
new file mode 100755
index 000000000000..6981ce061217
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller25.sh
@@ -0,0 +1,476 @@
+#!/bin/sh
+
+# Fatal trap 9: general protection fault while in kernel mode
+# cpuid = 5; apic id = 05
+# instruction pointer = 0x20:0xffffffff8237cbac
+# stack pointer = 0x28:0xfffffe01026e4910
+# frame pointer = 0x28:0xfffffe01026e4980
+# code segment = base 0x0, limit 0xfffff, type 0x1b
+# = DPL 0, pres 1, long 1, def32 0, gran 1
+# processor eflags = interrupt enabled, resume, IOPL = 0
+# current process = 45836 (syzkaller25)
+# trap number = 9
+# panic: general protection fault
+# cpuid = 5
+# time = 1601745366
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe01026e4620
+# vpanic() at vpanic+0x182/frame 0xfffffe01026e4670
+# panic() at panic+0x43/frame 0xfffffe01026e46d0
+# trap_fatal() at trap_fatal+0x387/frame 0xfffffe01026e4730
+# trap() at trap+0xa4/frame 0xfffffe01026e4840
+# calltrap() at calltrap+0x8/frame 0xfffffe01026e4840
+# --- trap 0x9, rip = 0xffffffff8237cbac, rsp = 0xfffffe01026e4910, rbp = 0xfffffe01026e4980 ---
+# sctp_inpcb_bind() at sctp_inpcb_bind+0x3cc/frame 0xfffffe01026e4980
+# sctp_connect() at sctp_connect+0x14f/frame 0xfffffe01026e49e0
+# soconnectat() at soconnectat+0xd0/frame 0xfffffe01026e4a30
+# kern_connectat() at kern_connectat+0xe2/frame 0xfffffe01026e4a90
+# sys_connect() at sys_connect+0x75/frame 0xfffffe01026e4ad0
+# amd64_syscall() at amd64_syscall+0x14e/frame 0xfffffe01026e4bf0
+# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe01026e4bf0
+# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x8003b0a1a, rsp = 0x7fffdfffdbb8, rbp = 0x7fffdfffdf90 ---
+# KDB: enter: panic
+# [ thread pid 45836 tid 101772 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10ac846(%rip)
+# db> x/s version
+# version: FreeBSD 13.0-CURRENT #0 r366401: Sat Oct 3 19:00:37 CEST 2020
+# pho@t2.osted.lan:/usr/src/sys/amd64/compile/PHO
+# db>
+
+[ `uname -p` != "amd64" ] && exit 0
+
+. ../default.cfg
+kldstat -v | grep -q sctp || kldload sctp.ko
+
+cat > /tmp/syzkaller25.c <<EOF
+// https://syzkaller.appspot.com/bug?id=2e4fc04b5a5775777770f7244613571ca85da78a
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+// Reported-by: syzbot+77fcf6a9d28f301bc2e5@syzkaller.appspotmail.com
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <dirent.h>
+#include <errno.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static unsigned long long procid;
+
+static __thread int skip_segv;
+static __thread jmp_buf segv_env;
+
+static void segv_handler(int sig, siginfo_t* info, void* ctx __unused)
+{
+ uintptr_t addr = (uintptr_t)info->si_addr;
+ const uintptr_t prog_start = 1 << 20;
+ const uintptr_t prog_end = 100 << 20;
+ int skip = __atomic_load_n(&skip_segv, __ATOMIC_RELAXED) != 0;
+ int valid = addr < prog_start || addr > prog_end;
+ if (sig == SIGBUS) {
+ valid = 1;
+ }
+ if (skip && valid) {
+ _longjmp(segv_env, 1);
+ }
+ exit(sig);
+}
+
+static void install_segv_handler(void)
+{
+ struct sigaction sa;
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_sigaction = segv_handler;
+ sa.sa_flags = SA_NODEFER | SA_SIGINFO;
+ sigaction(SIGSEGV, &sa, NULL);
+ sigaction(SIGBUS, &sa, NULL);
+}
+
+#define NONFAILING(...) \
+ ({ \
+ int ok = 1; \
+ __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \
+ if (_setjmp(segv_env) == 0) { \
+ __VA_ARGS__; \
+ } else \
+ ok = 0; \
+ __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \
+ ok; \
+ })
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void use_temporary_dir(void)
+{
+ char tmpdir_template[] = "./syzkaller.XXXXXX";
+ char* tmpdir = mkdtemp(tmpdir_template);
+ if (!tmpdir)
+ exit(1);
+ if (chmod(tmpdir, 0777))
+ exit(1);
+ if (chdir(tmpdir))
+ exit(1);
+}
+
+static void __attribute__((noinline)) remove_dir(const char* dir)
+{
+ DIR* dp = opendir(dir);
+ if (dp == NULL) {
+ if (errno == EACCES) {
+ if (rmdir(dir))
+ exit(1);
+ return;
+ }
+ exit(1);
+ }
+ struct dirent* ep = 0;
+ while ((ep = readdir(dp))) {
+ if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
+ continue;
+ char filename[FILENAME_MAX];
+ snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name);
+ struct stat st;
+ if (lstat(filename, &st))
+ exit(1);
+ if (S_ISDIR(st.st_mode)) {
+ remove_dir(filename);
+ continue;
+ }
+ if (unlink(filename))
+ exit(1);
+ }
+ closedir(dp);
+ if (rmdir(dir))
+ exit(1);
+}
+
+static void thread_start(void* (*fn)(void*), void* arg)
+{
+ pthread_t th;
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ pthread_attr_setstacksize(&attr, 128 << 10);
+ int i = 0;
+ for (; i < 100; i++) {
+ if (pthread_create(&th, &attr, fn, arg) == 0) {
+ pthread_attr_destroy(&attr);
+ return;
+ }
+ if (errno == EAGAIN) {
+ usleep(50);
+ continue;
+ }
+ break;
+ }
+ exit(1);
+}
+
+typedef struct {
+ pthread_mutex_t mu;
+ pthread_cond_t cv;
+ int state;
+} event_t;
+
+static void event_init(event_t* ev)
+{
+ if (pthread_mutex_init(&ev->mu, 0))
+ exit(1);
+ if (pthread_cond_init(&ev->cv, 0))
+ exit(1);
+ ev->state = 0;
+}
+
+static void event_reset(event_t* ev)
+{
+ ev->state = 0;
+}
+
+static void event_set(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ if (ev->state)
+ exit(1);
+ ev->state = 1;
+ pthread_mutex_unlock(&ev->mu);
+ pthread_cond_broadcast(&ev->cv);
+}
+
+static void event_wait(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ while (!ev->state)
+ pthread_cond_wait(&ev->cv, &ev->mu);
+ pthread_mutex_unlock(&ev->mu);
+}
+
+static int event_isset(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+static int event_timedwait(event_t* ev, uint64_t timeout)
+{
+ uint64_t start = current_time_ms();
+ uint64_t now = start;
+ pthread_mutex_lock(&ev->mu);
+ for (;;) {
+ if (ev->state)
+ break;
+ uint64_t remain = timeout - (now - start);
+ struct timespec ts;
+ ts.tv_sec = remain / 1000;
+ ts.tv_nsec = (remain % 1000) * 1000 * 1000;
+ pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
+ now = current_time_ms();
+ if (now - start > timeout)
+ break;
+ }
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+static void sandbox_common()
+{
+ if (setsid() == -1)
+ exit(1);
+ struct rlimit rlim;
+ rlim.rlim_cur = rlim.rlim_max = 128 << 20;
+ setrlimit(RLIMIT_AS, &rlim);
+ rlim.rlim_cur = rlim.rlim_max = 8 << 20;
+ setrlimit(RLIMIT_MEMLOCK, &rlim);
+ rlim.rlim_cur = rlim.rlim_max = 1 << 20;
+ setrlimit(RLIMIT_FSIZE, &rlim);
+ rlim.rlim_cur = rlim.rlim_max = 1 << 20;
+ setrlimit(RLIMIT_STACK, &rlim);
+ rlim.rlim_cur = rlim.rlim_max = 0;
+ setrlimit(RLIMIT_CORE, &rlim);
+ rlim.rlim_cur = rlim.rlim_max = 256;
+ setrlimit(RLIMIT_NOFILE, &rlim);
+}
+
+static void loop();
+
+static int do_sandbox_none(void)
+{
+ sandbox_common();
+ loop();
+ return 0;
+}
+
+struct thread_t {
+ int created, call;
+ event_t ready, done;
+};
+
+static struct thread_t threads[16];
+static void execute_call(int call);
+static int running;
+
+static void* thr(void* arg)
+{
+ struct thread_t* th = (struct thread_t*)arg;
+ for (;;) {
+ event_wait(&th->ready);
+ event_reset(&th->ready);
+ execute_call(th->call);
+ __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->done);
+ }
+ return 0;
+}
+
+static void execute_one(void)
+{
+ int i, call, thread;
+ int collide = 0;
+again:
+ for (call = 0; call < 4; call++) {
+ for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
+ thread++) {
+ struct thread_t* th = &threads[thread];
+ if (!th->created) {
+ th->created = 1;
+ event_init(&th->ready);
+ event_init(&th->done);
+ event_set(&th->done);
+ thread_start(thr, th);
+ }
+ if (!event_isset(&th->done))
+ continue;
+ event_reset(&th->done);
+ th->call = call;
+ __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->ready);
+ if (collide && (call % 2) == 0)
+ break;
+ event_timedwait(&th->done, 45);
+ break;
+ }
+ }
+ for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
+ sleep_ms(1);
+ if (!collide) {
+ collide = 1;
+ goto again;
+ }
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter = 0;
+ for (;; iter++) {
+ char cwdbuf[32];
+ sprintf(cwdbuf, "./%d", iter);
+ if (mkdir(cwdbuf, 0777))
+ exit(1);
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ if (chdir(cwdbuf))
+ exit(1);
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ remove_dir(cwdbuf);
+ }
+}
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+void execute_call(int call)
+{
+ intptr_t res = 0;
+ switch (call) {
+ case 0:
+ res = syscall(SYS_socket, 2ul, 5ul, 0x84);
+ if (res != -1)
+ r[0] = res;
+ break;
+ case 1:
+ NONFAILING(*(uint8_t*)0x20000300 = 0x10);
+ NONFAILING(*(uint8_t*)0x20000301 = 2);
+ NONFAILING(*(uint16_t*)0x20000302 = htobe16(0x4e23 + procid * 4));
+ NONFAILING(*(uint32_t*)0x20000304 = htobe32(0));
+ NONFAILING(*(uint8_t*)0x20000308 = 0);
+ NONFAILING(*(uint8_t*)0x20000309 = 0);
+ NONFAILING(*(uint8_t*)0x2000030a = 0);
+ NONFAILING(*(uint8_t*)0x2000030b = 0);
+ NONFAILING(*(uint8_t*)0x2000030c = 0);
+ NONFAILING(*(uint8_t*)0x2000030d = 0);
+ NONFAILING(*(uint8_t*)0x2000030e = 0);
+ NONFAILING(*(uint8_t*)0x2000030f = 0);
+ syscall(SYS_bind, r[0], 0x20000300ul, 0x10ul);
+ break;
+ case 2:
+ NONFAILING(*(uint8_t*)0x20000040 = 0x10);
+ NONFAILING(*(uint8_t*)0x20000041 = 2);
+ NONFAILING(*(uint16_t*)0x20000042 = htobe16(0x4e23 + procid * 4));
+ NONFAILING(*(uint32_t*)0x20000044 = htobe32(0x7f000001));
+ NONFAILING(*(uint8_t*)0x20000048 = 0);
+ NONFAILING(*(uint8_t*)0x20000049 = 0);
+ NONFAILING(*(uint8_t*)0x2000004a = 0);
+ NONFAILING(*(uint8_t*)0x2000004b = 0);
+ NONFAILING(*(uint8_t*)0x2000004c = 0);
+ NONFAILING(*(uint8_t*)0x2000004d = 0);
+ NONFAILING(*(uint8_t*)0x2000004e = 0);
+ NONFAILING(*(uint8_t*)0x2000004f = 0);
+ syscall(SYS_connect, r[0], 0x20000040ul, 0x10ul);
+ break;
+ case 3:
+ NONFAILING(*(uint8_t*)0x20000000 = 0x10);
+ NONFAILING(*(uint8_t*)0x20000001 = 2);
+ NONFAILING(*(uint16_t*)0x20000002 = htobe16(0x4e23 + procid * 4));
+ NONFAILING(*(uint8_t*)0x20000004 = 0xac);
+ NONFAILING(*(uint8_t*)0x20000005 = 0x14);
+ NONFAILING(*(uint8_t*)0x20000006 = 0 + procid * 1);
+ NONFAILING(*(uint8_t*)0x20000007 = 0xaa);
+ NONFAILING(*(uint8_t*)0x20000008 = 0);
+ NONFAILING(*(uint8_t*)0x20000009 = 0);
+ NONFAILING(*(uint8_t*)0x2000000a = 0);
+ NONFAILING(*(uint8_t*)0x2000000b = 0);
+ NONFAILING(*(uint8_t*)0x2000000c = 0);
+ NONFAILING(*(uint8_t*)0x2000000d = 0);
+ NONFAILING(*(uint8_t*)0x2000000e = 0);
+ NONFAILING(*(uint8_t*)0x2000000f = 0);
+ syscall(SYS_connect, r[0], 0x20000000ul, 0x10ul);
+ break;
+ }
+}
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ install_segv_handler();
+ for (procid = 0; procid < 4; procid++) {
+ if (fork() == 0) {
+ use_temporary_dir();
+ do_sandbox_none();
+ }
+ }
+ sleep(1000000);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller25 -Wall -Wextra -O0 /tmp/syzkaller25.c -lpthread ||
+ exit 1
+
+(cd ../testcases/swap; ./swap -t 1m -i 20 -h > /dev/null 2>&1) &
+(cd /tmp; timeout 3m ./syzkaller25)
+while pkill swap; do :; done
+wait
+
+rm -rf /tmp/syzkaller25 syzkaller25.c /tmp/syzkaller.*
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller26.sh b/tools/test/stress2/misc/syzkaller26.sh
new file mode 100755
index 000000000000..f14806199ff5
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller26.sh
@@ -0,0 +1,166 @@
+#!/bin/sh
+
+# panic: Bad link elm 0xfffff8001a83db00 prev->next != elm
+# cpuid = 21
+# time = 1605387390
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe0100a983e0
+# vpanic() at vpanic+0x182/frame 0xfffffe0100a98430
+# panic() at panic+0x43/frame 0xfffffe0100a98490
+# _callout_stop_safe() at _callout_stop_safe+0x726/frame 0xfffffe0100a98520
+# filt_timerdetach() at filt_timerdetach+0x20/frame 0xfffffe0100a98540
+# kqueue_drain() at kqueue_drain+0x2c8/frame 0xfffffe0100a98580
+# kqueue_close() at kqueue_close+0x7d/frame 0xfffffe0100a985c0
+# _fdrop() at _fdrop+0x1b/frame 0xfffffe0100a985e0
+# closef() at closef+0x1ea/frame 0xfffffe0100a98670
+# fdescfree_fds() at fdescfree_fds+0x8c/frame 0xfffffe0100a986d0
+# fdescfree() at fdescfree+0x3cd/frame 0xfffffe0100a987a0
+# exit1() at exit1+0x487/frame 0xfffffe0100a98810
+# sigexit() at sigexit+0x15c/frame 0xfffffe0100a98ae0
+# postsig() at postsig+0x2cc/frame 0xfffffe0100a98bb0
+# ast() at ast+0x5eb/frame 0xfffffe0100a98bf0
+# doreti_ast() at doreti_ast+0x1f/frame 0x7fffffffe570
+# KDB: enter: panic
+# [ thread pid 82279 tid 100717 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10aa7f6(%rip)
+# db> x/s version
+# version: FreeBSD 13.0-CURRENT #0 r367672: Sat Nov 14 08:42:14 CET 2020
+# pho@t2.osted.lan:/usr/src/sys/amd64/compile/PHO
+# db>
+
+[ `uname -p` != "amd64" ] && exit 0
+
+# Fixed by r367849
+
+. ../default.cfg
+cat > /tmp/syzkaller26.c <<EOF
+// https://syzkaller.appspot.com/bug?id=95ac7e30218c63bc9322b1dd775101f8f88de4ff
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+// Reported-by: syzbot+1b27e0237aa22d8adffa@syzkaller.appspotmail.com
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <pwd.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter = 0;
+ for (;; iter++) {
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ }
+}
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+void execute_one(void)
+{
+ intptr_t res = 0;
+ res = syscall(SYS_kqueue);
+ if (res != -1)
+ r[0] = res;
+ *(uint64_t*)0x20000000 = 3;
+ *(uint16_t*)0x20000008 = 0xfff3;
+ *(uint16_t*)0x2000000a = 0x4000;
+ *(uint32_t*)0x2000000c = 0x1000000;
+ *(uint64_t*)0x20000010 = 0xe;
+ *(uint64_t*)0x20000018 = 0xff;
+ *(uint64_t*)0x20000020 = 2;
+ *(uint64_t*)0x20000028 = 2;
+ *(uint64_t*)0x20000030 = 4;
+ *(uint64_t*)0x20000038 = 0xffffffffffffff90;
+ *(uint64_t*)0x20000040 = 0x101;
+ *(uint16_t*)0x20000048 = 0xfff6;
+ *(uint16_t*)0x2000004a = 0x22;
+ *(uint32_t*)0x2000004c = 0x20;
+ *(uint64_t*)0x20000050 = 0xe1;
+ *(uint64_t*)0x20000058 = -1;
+ *(uint64_t*)0x20000060 = 0x81;
+ *(uint64_t*)0x20000068 = 4;
+ *(uint64_t*)0x20000070 = 0x8000;
+ *(uint64_t*)0x20000078 = 9;
+ *(uint64_t*)0x20000080 = 0;
+ *(uint16_t*)0x20000088 = 0xfff9;
+ *(uint16_t*)0x2000008a = 0xefc9;
+ *(uint32_t*)0x2000008c = 4;
+ *(uint64_t*)0x20000090 = 0;
+ *(uint64_t*)0x20000098 = 0x400;
+ *(uint64_t*)0x200000a0 = 0x2e77;
+ *(uint64_t*)0x200000a8 = 2;
+ *(uint64_t*)0x200000b0 = 0x7fffffff;
+ *(uint64_t*)0x200000b8 = 7;
+ syscall(SYS_kevent, r[0], 0x20000000ul, 3ul, 0x200000c0ul, 9ul, 0ul);
+}
+int main(void)
+{
+ sleep(2); /* pho */
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ loop();
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller26 -Wall -Wextra -O0 /tmp/syzkaller26.c ||
+ exit 1
+
+(cd ../testcases/swap; ./swap -t 1m -i 20 -h > /dev/null 2>&1) &
+for i in `jot 256`; do
+ (cd /tmp; timeout 3m ./syzkaller26) &
+done
+wait
+
+rm -rf /tmp/syzkaller26 /tmp/syzkaller26.c /tmp/syzkaller.*
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller27.sh b/tools/test/stress2/misc/syzkaller27.sh
new file mode 100755
index 000000000000..b003f6aec319
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller27.sh
@@ -0,0 +1,150 @@
+#!/bin/sh
+
+# panic: Bad tailq NEXT(0xffffffff81cb0ba0->tqh_last) != NULL
+# cpuid = 0
+# time = 1605424954
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe010125c2e0
+# vpanic() at vpanic+0x182/frame 0xfffffe010125c330
+# panic() at panic+0x43/frame 0xfffffe010125c390
+# callout_process() at callout_process+0x34f/frame 0xfffffe010125c400
+# handleevents() at handleevents+0x185/frame 0xfffffe010125c440
+# timercb() at timercb+0x196/frame 0xfffffe010125c490
+# lapic_handle_timer() at lapic_handle_timer+0x9b/frame 0xfffffe010125c4c0
+# Xtimerint() at Xtimerint+0xb1/frame 0xfffffe010125c4c0
+# --- interrupt, rip = 0xffffffff80c56c69, rsp = 0xfffffe010125c590, rbp = 0xfffffe010125c5c0 ---
+# witness_unlock() at witness_unlock+0x79/frame 0xfffffe010125c5c0
+# __mtx_unlock_flags() at __mtx_unlock_flags+0x4d/frame 0xfffffe010125c5f0
+# kqueue_register() at kqueue_register+0x9e3/frame 0xfffffe010125c680
+# kqueue_kevent() at kqueue_kevent+0x106/frame 0xfffffe010125c950
+# kern_kevent_fp() at kern_kevent_fp+0x95/frame 0xfffffe010125c9a0
+# kern_kevent() at kern_kevent+0x80/frame 0xfffffe010125ca00
+# kern_kevent_generic() at kern_kevent_generic+0x70/frame 0xfffffe010125ca60
+# sys_kevent() at sys_kevent+0x61/frame 0xfffffe010125cac0
+# amd64_syscall() at amd64_syscall+0x144/frame 0xfffffe010125cbf0
+# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe010125cbf0
+# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x800380c6a, rsp = 0x7fffffffe4d8, rbp = 0x7fffffffe500 ---
+# KDB: enter: panic
+# [ thread pid 36082 tid 144846 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10aa7f6(%rip)
+# db> x/s version
+# version: FreeBSD 13.0-CURRENT #0 r367672: Sat Nov 14 08:42:14 CET 2020
+# pho@t2.osted.lan:/usr/src/sys/amd64/compile/PHO
+# db>
+
+[ `uname -p` != "amd64" ] && exit 0
+
+# Fixed by r367849
+
+. ../default.cfg
+cat > /tmp/syzkaller27.c <<EOF
+// https://syzkaller.appspot.com/bug?id=6b64d673525304a89af91e0db71f2dee8e8da0f7
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+// Reported-by: syzbot+79569cd4d76636b2cc1c@syzkaller.appspotmail.com
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <pwd.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter = 0;
+ for (;; iter++) {
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ }
+}
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+void execute_one(void)
+{
+ intptr_t res = 0;
+ res = syscall(SYS_kqueue);
+ if (res != -1)
+ r[0] = res;
+ *(uint64_t*)0x200000c0 = 0;
+ *(uint16_t*)0x200000c8 = 0xfff9;
+ *(uint16_t*)0x200000ca = 0x4001;
+ *(uint32_t*)0x200000cc = 4;
+ *(uint64_t*)0x200000d0 = 0;
+ *(uint64_t*)0x200000d8 = 0;
+ *(uint64_t*)0x200000e0 = 0;
+ *(uint64_t*)0x200000e8 = 0;
+ *(uint64_t*)0x200000f0 = 0;
+ *(uint64_t*)0x200000f8 = 0;
+ syscall(SYS_kevent, r[0], 0x200000c0ul, 1ul, 0ul, 0ul, 0ul);
+}
+int main(void)
+{
+ sleep(2); /* pho */
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ loop();
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller27 -Wall -Wextra -O0 /tmp/syzkaller27.c ||
+ exit 1
+
+(cd ../testcases/swap; ./swap -t 1m -i 20 -h > /dev/null 2>&1) &
+for i in `jot 256`; do
+ (cd /tmp; timeout 3m ./syzkaller27) &
+done
+wait
+
+rm -rf /tmp/syzkaller27 /tmp/syzkaller27.c /tmp/syzkaller.*
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller28.sh b/tools/test/stress2/misc/syzkaller28.sh
new file mode 100755
index 000000000000..1119b067f047
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller28.sh
@@ -0,0 +1,356 @@
+#!/bin/sh
+
+# panic: About to free ctl:0xfffff809b0ac1260 so:0xfffff80d97dde760 and its in 1
+# cpuid = 9
+# time = 1605860285
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe0100b1e630
+# vpanic() at vpanic+0x182/frame 0xfffffe0100b1e680
+# panic() at panic+0x43/frame 0xfffffe0100b1e6e0
+# sctp_sorecvmsg() at sctp_sorecvmsg+0x1a96/frame 0xfffffe0100b1e810
+# sctp_soreceive() at sctp_soreceive+0x1b2/frame 0xfffffe0100b1ea00
+# soreceive() at soreceive+0x59/frame 0xfffffe0100b1ea20
+# dofileread() at dofileread+0x81/frame 0xfffffe0100b1ea70
+# sys_readv() at sys_readv+0x6e/frame 0xfffffe0100b1eac0
+# amd64_syscall() at amd64_syscall+0x147/frame 0xfffffe0100b1ebf0
+# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe0100b1ebf0
+# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x8003aed4a, rsp = 0x7fffdfffdf68, rbp = 0x7fffdfffdf90 ---
+# KDB: enter: panic
+# [ thread pid 3933 tid 102941 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10a91b6(%rip)
+# db> x/s version
+# version: FreeBSD 13.0-CURRENT #0 r367842: Thu Nov 19 13:08:17 CET 2020
+# pho@t2.osted.lan:/usr/src/sys/amd64/compile/PHO
+# db>
+
+[ `uname -p` != "amd64" ] && exit 0
+
+. ../default.cfg
+kldstat -v | grep -q sctp || kldload sctp.ko
+
+cat > /tmp/syzkaller28.c <<EOF
+// https://syzkaller.appspot.com/bug?id=b0e5bd1e2a4ac3caf8e2ad16ae6054d9fcc2e9d2
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+// Reported-by: syzbot+b2d3e3f439385340e35f@syzkaller.appspotmail.com
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <errno.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static unsigned long long procid;
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void thread_start(void* (*fn)(void*), void* arg)
+{
+ pthread_t th;
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ pthread_attr_setstacksize(&attr, 128 << 10);
+ int i = 0;
+ for (; i < 100; i++) {
+ if (pthread_create(&th, &attr, fn, arg) == 0) {
+ pthread_attr_destroy(&attr);
+ return;
+ }
+ if (errno == EAGAIN) {
+ usleep(50);
+ continue;
+ }
+ break;
+ }
+ exit(1);
+}
+
+typedef struct {
+ pthread_mutex_t mu;
+ pthread_cond_t cv;
+ int state;
+} event_t;
+
+static void event_init(event_t* ev)
+{
+ if (pthread_mutex_init(&ev->mu, 0))
+ exit(1);
+ if (pthread_cond_init(&ev->cv, 0))
+ exit(1);
+ ev->state = 0;
+}
+
+static void event_reset(event_t* ev)
+{
+ ev->state = 0;
+}
+
+static void event_set(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ if (ev->state)
+ exit(1);
+ ev->state = 1;
+ pthread_mutex_unlock(&ev->mu);
+ pthread_cond_broadcast(&ev->cv);
+}
+
+static void event_wait(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ while (!ev->state)
+ pthread_cond_wait(&ev->cv, &ev->mu);
+ pthread_mutex_unlock(&ev->mu);
+}
+
+static int event_isset(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+static int event_timedwait(event_t* ev, uint64_t timeout)
+{
+ uint64_t start = current_time_ms();
+ uint64_t now = start;
+ pthread_mutex_lock(&ev->mu);
+ for (;;) {
+ if (ev->state)
+ break;
+ uint64_t remain = timeout - (now - start);
+ struct timespec ts;
+ ts.tv_sec = remain / 1000;
+ ts.tv_nsec = (remain % 1000) * 1000 * 1000;
+ pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
+ now = current_time_ms();
+ if (now - start > timeout)
+ break;
+ }
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+struct thread_t {
+ int created, call;
+ event_t ready, done;
+};
+
+static struct thread_t threads[16];
+static void execute_call(int call);
+static int running;
+
+static void* thr(void* arg)
+{
+ struct thread_t* th = (struct thread_t*)arg;
+ for (;;) {
+ event_wait(&th->ready);
+ event_reset(&th->ready);
+ execute_call(th->call);
+ __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->done);
+ }
+ return 0;
+}
+
+static void execute_one(void)
+{
+ int i, call, thread;
+ for (call = 0; call < 9; call++) {
+ for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
+ thread++) {
+ struct thread_t* th = &threads[thread];
+ if (!th->created) {
+ th->created = 1;
+ event_init(&th->ready);
+ event_init(&th->done);
+ event_set(&th->done);
+ thread_start(thr, th);
+ }
+ if (!event_isset(&th->done))
+ continue;
+ event_reset(&th->done);
+ th->call = call;
+ __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->ready);
+ event_timedwait(&th->done, 45);
+ break;
+ }
+ }
+ for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
+ sleep_ms(1);
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter = 0;
+ for (;; iter++) {
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ }
+}
+
+uint64_t r[2] = {0xffffffffffffffff, 0xffffffffffffffff};
+
+void execute_call(int call)
+{
+ intptr_t res = 0;
+ switch (call) {
+ case 0:
+ res = syscall(SYS_socket, 0x1cul, 1ul, 0x84);
+ if (res != -1)
+ r[0] = res;
+ break;
+ case 1:
+ *(uint32_t*)0x20000040 = 0;
+ syscall(SYS_setsockopt, r[0], 0x84, 0x11, 0x20000040ul, 4ul);
+ break;
+ case 2:
+ *(uint8_t*)0x20000000 = 0x1c;
+ *(uint8_t*)0x20000001 = 0x1c;
+ *(uint16_t*)0x20000002 = htobe16(0x4e22 + procid * 4);
+ *(uint32_t*)0x20000004 = 0;
+ *(uint8_t*)0x20000008 = 0;
+ *(uint8_t*)0x20000009 = 0;
+ *(uint8_t*)0x2000000a = 0;
+ *(uint8_t*)0x2000000b = 0;
+ *(uint8_t*)0x2000000c = 0;
+ *(uint8_t*)0x2000000d = 0;
+ *(uint8_t*)0x2000000e = 0;
+ *(uint8_t*)0x2000000f = 0;
+ *(uint8_t*)0x20000010 = 0;
+ *(uint8_t*)0x20000011 = 0;
+ *(uint8_t*)0x20000012 = 0;
+ *(uint8_t*)0x20000013 = 0;
+ *(uint8_t*)0x20000014 = 0;
+ *(uint8_t*)0x20000015 = 0;
+ *(uint8_t*)0x20000016 = 0;
+ *(uint8_t*)0x20000017 = 0;
+ *(uint32_t*)0x20000018 = 6;
+ syscall(SYS_bind, r[0], 0x20000000ul, 0x1cul);
+ break;
+ case 3:
+ *(uint8_t*)0x20000180 = 0x5f;
+ *(uint8_t*)0x20000181 = 0x1c;
+ *(uint16_t*)0x20000182 = htobe16(0x4e22 + procid * 4);
+ *(uint32_t*)0x20000184 = 0;
+ *(uint64_t*)0x20000188 = htobe64(0);
+ *(uint64_t*)0x20000190 = htobe64(1);
+ *(uint32_t*)0x20000198 = 0;
+ syscall(SYS_connect, r[0], 0x20000180ul, 0x1cul);
+ break;
+ case 4:
+ *(uint64_t*)0x20001500 = 0x20000200;
+ *(uint64_t*)0x20001508 = 0xb8;
+ *(uint64_t*)0x20001510 = 0;
+ *(uint64_t*)0x20001518 = 0;
+ *(uint64_t*)0x20001520 = 0;
+ *(uint64_t*)0x20001528 = 0;
+ *(uint64_t*)0x20001530 = 0;
+ *(uint64_t*)0x20001538 = 0;
+ *(uint64_t*)0x20001540 = 0;
+ *(uint64_t*)0x20001548 = 0;
+ syscall(SYS_readv, r[0], 0x20001500ul, 5ul);
+ break;
+ case 5:
+ *(uint32_t*)0x20000140 = 0xb2;
+ syscall(SYS_setsockopt, r[0], 0x84, 0x1b, 0x20000140ul, 4ul);
+ break;
+ case 6:
+ res = syscall(SYS_fcntl, r[0], 0ul, r[0]);
+ if (res != -1)
+ r[1] = res;
+ break;
+ case 7:
+ *(uint64_t*)0x200004c0 = 0;
+ *(uint32_t*)0x200004c8 = 0;
+ *(uint64_t*)0x200004d0 = 0x200003c0;
+ *(uint64_t*)0x200003c0 = 0x200001c0;
+ memcpy((void*)0x200001c0, "\xb0", 1);
+ *(uint64_t*)0x200003c8 = 1;
+ *(uint32_t*)0x200004d8 = 1;
+ *(uint64_t*)0x200004e0 = 0;
+ *(uint32_t*)0x200004e8 = 0;
+ *(uint32_t*)0x200004ec = 0;
+ syscall(SYS_sendmsg, r[1], 0x200004c0ul, 0ul);
+ break;
+ case 8:
+ syscall(SYS_shutdown, r[0], 1ul);
+ break;
+ }
+}
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ for (procid = 0; procid < 4; procid++) {
+ if (fork() == 0) {
+ loop();
+ }
+ }
+ sleep(1000000);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller28 -Wall -Wextra -O0 /tmp/syzkaller28.c -lpthread ||
+ exit 1
+
+(cd ../testcases/swap; ./swap -t 1m -i 20 -h > /dev/null 2>&1) &
+(cd /tmp; timeout 3m ./syzkaller28)
+while pkill swap; do :; done
+wait
+
+rm -rf /tmp/syzkaller28 syzkaller28.c /tmp/syzkaller.*
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller29.sh b/tools/test/stress2/misc/syzkaller29.sh
new file mode 100755
index 000000000000..7718c569482d
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller29.sh
@@ -0,0 +1,128 @@
+#!/bin/sh
+
+# panic: uma_zalloc_debug: called within spinlock or critical section
+# cpuid = 9
+# time = 1606151277
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe0101c628a0
+# vpanic() at vpanic+0x181/frame 0xfffffe0101c628f0
+# panic() at panic+0x43/frame 0xfffffe0101c62950
+# uma_zalloc_arg() at uma_zalloc_arg+0x1e6/frame 0xfffffe0101c62990
+# cpuset_modify_domain() at cpuset_modify_domain+0x1bf/frame 0xfffffe0101c62a10
+# kern_cpuset_setdomain() at kern_cpuset_setdomain+0x402/frame 0xfffffe0101c62aa0
+# sys_cpuset_setdomain() at sys_cpuset_setdomain+0x26/frame 0xfffffe0101c62ac0
+# amd64_syscall() at amd64_syscall+0x147/frame 0xfffffe0101c62bf0
+# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe0101c62bf0
+# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x800380d4a, rsp = 0x7fffffffe4b8, rbp = 0x7fffffffe4f0 ---
+# KDB: enter: panic
+# [ thread pid 81187 tid 100369 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10b3236(%rip)
+# db> x/s version
+# version: FreeBSD 13.0-CURRENT #0 r367945: Mon Nov 23 09:10:40 CET 2020
+# pho@t2.osted.lan:/usr/src/sys/amd64/compile/PHO
+# db>
+
+[ `uname -p` != "amd64" ] && exit 0
+
+# Fixed by r368116
+
+. ../default.cfg
+cat > /tmp/syzkaller29.c <<EOF
+// https://syzkaller.appspot.com/bug?id=c3452066dba9238c4551010eb130944442d9174f
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+// Reported-by: syzbot+6743fa07b9b7528dc561@syzkaller.appspotmail.com
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <pwd.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter = 0;
+ for (;; iter++) {
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ }
+}
+
+void execute_one(void)
+{
+ *(uint64_t*)0x20000240 = 1;
+ *(uint64_t*)0x20000248 = 0;
+ *(uint64_t*)0x20000250 = 0;
+ *(uint64_t*)0x20000258 = 0;
+ syscall(SYS_cpuset_setdomain, 2ul, 2ul, 0ul, 0x20ul, 0x20000240ul, 4ul);
+ *(uint64_t*)0x20000240 = 1;
+ *(uint64_t*)0x20000248 = 0;
+ *(uint64_t*)0x20000250 = 0;
+ *(uint64_t*)0x20000258 = 0;
+ syscall(SYS_cpuset_setdomain, 3ul, 2ul, 0ul, 0x20ul, 0x20000240ul, 1ul);
+}
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+ loop();
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller29 -Wall -Wextra -O0 /tmp/syzkaller29.c ||
+ exit 1
+
+(cd /tmp; timeout 3m ./syzkaller29)
+
+rm -rf /tmp/syzkaller29 syzkaller29.c /tmp/syzkaller.*
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller3.sh b/tools/test/stress2/misc/syzkaller3.sh
new file mode 100755
index 000000000000..ffc5d6dc41bb
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller3.sh
@@ -0,0 +1,49 @@
+#!/bin/sh
+
+# panic: mutex process lock not owned at ../../../kern/kern_time.c:261
+# cpuid = 17
+# time = 1580844484
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe01387b7920
+# vpanic() at vpanic+0x185/frame 0xfffffe01387b7980
+# panic() at panic+0x43/frame 0xfffffe01387b79e0
+# __mtx_assert() at __mtx_assert+0xb0/frame 0xfffffe01387b79f0
+# kern_thread_cputime() at kern_thread_cputime+0x99/frame 0xfffffe01387b7a30
+# kern_clock_gettime() at kern_clock_gettime+0x2a6/frame 0xfffffe01387b7a90
+# sys_clock_gettime() at sys_clock_gettime+0x17/frame 0xfffffe01387b7ac0
+# amd64_syscall() at amd64_syscall+0x2f1/frame 0xfffffe01387b7bf0
+# fast_syscall_common() at fast_syscall_common+0x101/frame 0xfffffe01387b7bf0
+# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x80041b8ca, rsp = 0x7fffffffe9f8, rbp = 0x7fffffffea10 ---
+
+. ../default.cfg
+cat > /tmp/syzkaller3.c <<EOF
+// https://syzkaller.appspot.com/bug?id=6245c550ba855e94618dbc1dec0a21e9e89a2ddd
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+
+#define _GNU_SOURCE
+
+#include <pwd.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul);
+ syscall(SYS_clock_gettime, 0xeul, 0ul);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller3 -Wall -Wextra -O2 /tmp/syzkaller3.c ||
+ exit 1
+
+(cd /tmp; ./syzkaller3)
+
+rm /tmp/syzkaller3 /tmp/syzkaller3.c
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller30.sh b/tools/test/stress2/misc/syzkaller30.sh
new file mode 100755
index 000000000000..a224652865db
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller30.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+# panic: sched_pickcpu: Failed to find a cpu.
+# cpuid = 1
+# time = 1607419071
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe01bb640770
+# vpanic() at vpanic+0x181/frame 0xfffffe01bb6407c0
+# panic() at panic+0x43/frame 0xfffffe01bb640820
+# sched_pickcpu() at sched_pickcpu+0x4a2/frame 0xfffffe01bb6408d0
+# sched_add() at sched_add+0x5d/frame 0xfffffe01bb640900
+# setrunnable() at setrunnable+0x77/frame 0xfffffe01bb640930
+# wakeup_one() at wakeup_one+0x1d/frame 0xfffffe01bb640950
+# do_lock_umutex() at do_lock_umutex+0x64c/frame 0xfffffe01bb640a40
+# __umtx_op_wait_umutex() at __umtx_op_wait_umutex+0x49/frame 0xfffffe01bb640a80
+# sys__umtx_op() at sys__umtx_op+0x7a/frame 0xfffffe01bb640ac0
+# amd64_syscall() at amd64_syscall+0x147/frame 0xfffffe01bb640bf0
+# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe01bb640bf0
+# --- syscall (454, FreeBSD ELF64, sys__umtx_op), rip = 0x800254a8c, rsp = 0x7fffdf3f7e88, rbp = 0x7fffdf3f7eb0 ---
+# KDB: enter: panic
+# [ thread pid 58597 tid 106100 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10a7766(%rip)
+# db> x/s version
+# version: FreeBSD 13.0-CURRENT #0 r368405: Mon Dec 7 10:33:35 CET 2020
+# pho@t2.osted.lan:/usr/src/sys/amd64/compile/PHO
+# db>
+
+[ `uname -p` != "amd64" ] && exit 0
+
+# Fixed by r368462
+
+. ../default.cfg
+cat > /tmp/syzkaller30.c <<EOF
+// https://syzkaller.appspot.com/bug?id=6652adb41773e5c471c98342fefcbfb041af9ac8
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+// Reported-by: syzbot+4e3b1009de98d2fabcda@syzkaller.appspotmail.com
+
+#define _GNU_SOURCE
+
+#include <pwd.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
+
+ *(uint64_t*)0x200000c0 = 0;
+ syscall(SYS_cpuset_setaffinity, 2ul, 2ul, 0x100000000000000ul, 0x20ul,
+ 0x200000c0ul);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller30 -Wall -Wextra -O0 /tmp/syzkaller30.c ||
+ exit 1
+
+(cd /tmp; timeout 3m ./syzkaller30)
+
+rm -rf /tmp/syzkaller30 syzkaller30.c /tmp/syzkaller.*
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller4.sh b/tools/test/stress2/misc/syzkaller4.sh
new file mode 100755
index 000000000000..2a14d4d2b776
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller4.sh
@@ -0,0 +1,382 @@
+#!/bin/sh
+
+# panic: Assertion mtx_unowned(m) failed at ../../../kern/kern_mutex.c:1179
+# cpuid = 2
+# time = 1581180711
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe00e57958c0
+# vpanic() at vpanic+0x185/frame 0xfffffe00e5795920
+# panic() at panic+0x43/frame 0xfffffe00e5795980
+# _mtx_destroy() at _mtx_destroy+0x4c/frame 0xfffffe00e57959a0
+# solisten_proto() at solisten_proto+0xdb/frame 0xfffffe00e5795a00
+# tcp6_usr_listen() at tcp6_usr_listen+0x16d/frame 0xfffffe00e5795a60
+# solisten() at solisten+0x42/frame 0xfffffe00e5795a80
+# kern_listen() at kern_listen+0x80/frame 0xfffffe00e5795ac0
+# amd64_syscall() at amd64_syscall+0x2f1/frame 0xfffffe00e5795bf0
+# fast_syscall_common() at fast_syscall_common+0x101/frame 0xfffffe00e5795bf0
+
+[ `uname -p` = "i386" ] && exit 0
+
+. ../default.cfg
+cat > /tmp/syzkaller4.c <<EOF
+// https://syzkaller.appspot.com/bug?id=db195d1b0b8ca408a46f301eba33f9457bd2d429
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <errno.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void thread_start(void* (*fn)(void*), void* arg)
+{
+ pthread_t th;
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ pthread_attr_setstacksize(&attr, 128 << 10);
+ int i;
+ for (i = 0; i < 100; i++) {
+ if (pthread_create(&th, &attr, fn, arg) == 0) {
+ pthread_attr_destroy(&attr);
+ return;
+ }
+ if (errno == EAGAIN) {
+ usleep(50);
+ continue;
+ }
+ break;
+ }
+ exit(1);
+}
+
+typedef struct {
+ pthread_mutex_t mu;
+ pthread_cond_t cv;
+ int state;
+} event_t;
+
+static void event_init(event_t* ev)
+{
+ if (pthread_mutex_init(&ev->mu, 0))
+ exit(1);
+ if (pthread_cond_init(&ev->cv, 0))
+ exit(1);
+ ev->state = 0;
+}
+
+static void event_reset(event_t* ev)
+{
+ ev->state = 0;
+}
+
+static void event_set(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ if (ev->state)
+ exit(1);
+ ev->state = 1;
+ pthread_mutex_unlock(&ev->mu);
+ pthread_cond_broadcast(&ev->cv);
+}
+
+static void event_wait(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ while (!ev->state)
+ pthread_cond_wait(&ev->cv, &ev->mu);
+ pthread_mutex_unlock(&ev->mu);
+}
+
+static int event_isset(event_t* ev)
+{
+ pthread_mutex_lock(&ev->mu);
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+static int event_timedwait(event_t* ev, uint64_t timeout)
+{
+ uint64_t start = current_time_ms();
+ uint64_t now = start;
+ pthread_mutex_lock(&ev->mu);
+ for (;;) {
+ if (ev->state)
+ break;
+ uint64_t remain = timeout - (now - start);
+ struct timespec ts;
+ ts.tv_sec = remain / 1000;
+ ts.tv_nsec = (remain % 1000) * 1000 * 1000;
+ pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
+ now = current_time_ms();
+ if (now - start > timeout)
+ break;
+ }
+ int res = ev->state;
+ pthread_mutex_unlock(&ev->mu);
+ return res;
+}
+
+struct thread_t {
+ int created, call;
+ event_t ready, done;
+};
+
+static struct thread_t threads[16];
+static void execute_call(int call);
+static int running;
+
+static void* thr(void* arg)
+{
+ struct thread_t* th = (struct thread_t*)arg;
+ for (;;) {
+ event_wait(&th->ready);
+ event_reset(&th->ready);
+ execute_call(th->call);
+ __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->done);
+ }
+ return 0;
+}
+
+static void execute_one(void)
+{
+ int i, call, thread;
+ int collide = 0;
+again:
+ for (call = 0; call < 19; call++) {
+ for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
+ thread++) {
+ struct thread_t* th = &threads[thread];
+ if (!th->created) {
+ th->created = 1;
+ event_init(&th->ready);
+ event_init(&th->done);
+ event_set(&th->done);
+ thread_start(thr, th);
+ }
+ if (!event_isset(&th->done))
+ continue;
+ event_reset(&th->done);
+ th->call = call;
+ __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
+ event_set(&th->ready);
+ if (collide && (call % 2) == 0)
+ break;
+ event_timedwait(&th->done, 45);
+ break;
+ }
+ }
+ for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
+ sleep_ms(1);
+ if (!collide) {
+ collide = 1;
+ goto again;
+ }
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter;
+ for (iter = 0;; iter++) {
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ }
+}
+
+uint64_t r[6] = {0x0, 0xffffffffffffffff, 0x0, 0xffffffffffffffff,
+ 0x0, 0xffffffffffffffff};
+
+void execute_call(int call)
+{
+ intptr_t res;
+ switch (call) {
+ case 0:
+ *(uint32_t*)0x20000040 = 0x284002b0;
+ *(uint32_t*)0x20000044 = 0;
+ *(uint32_t*)0x20000048 = 0;
+ *(uint32_t*)0x2000004c = 0;
+ *(uint32_t*)0x20000050 = 0;
+ *(uint32_t*)0x20000054 = 0;
+ *(uint16_t*)0x20000058 = 0;
+ *(uint16_t*)0x2000005a = 0;
+ *(uint64_t*)0x20000060 = 0;
+ *(uint64_t*)0x20000068 = 0;
+ *(uint64_t*)0x20000070 = 0;
+ *(uint64_t*)0x20000078 = 0;
+ *(uint64_t*)0x20000080 = 0;
+ *(uint64_t*)0x20000088 = 0;
+ *(uint64_t*)0x20000090 = 0;
+ syscall(SYS___semctl, 0, 0ul, 0xaul, 0x20000040ul);
+ break;
+ case 1:
+ memcpy(
+ (void*)0x20000340,
+ "\x82\x02\xf3\x89\x9c\x53\x3e\x9e\x79\x9b\xd7\xc7\x3c\xcc\x8e\xf9\xba"
+ "\xb3\x80\xca\x99\xbc\x30\xf1\x8e\x7c\xb0\xc5\x3f\xd6\xa9\xe6\xc6\xa8"
+ "\xa8\x85\x82\x5b\xdf\x69\xb0\xb9\xed\x55\x59\xd8\x3d\x18\x64\x0d\x49"
+ "\xdd\x0c\x5b\xba\x14\x0e\xe4\x61\xce\xcf\x6c\x04\x6a\xa1\xda\xa6\x9a"
+ "\x50\xf8\xa5\xbf\x52\xc2\x63\xf1\x48\xad\xb2\x3e\x5b\x74\xd4\xd3\xe2"
+ "\x77\x4e\xe8\xef\x92\x6d\x3e\xf6\x35\x76\x60\x9b\x83\xfd\xbc\x00\x45"
+ "\xd8\x01\x38\x8b\x7b\x9f\x82\x1e\xf2\xe6\x42\xd3\x73\x00\x00\xc4\x05"
+ "\xc0\xc2\x1a\x82\xc5\x8e\x64\x2d\x07\x86\x09\x4f\xb0\x60\x2a\x5b\xfd"
+ "\x33\x73\x24\x41\xb5\xaa\x99\xd6\xdf\xbe\x06\xc7\x27\x48\x7e\x13\xfb"
+ "\x57\xd6\x2f\xcb\x0c\xda\x92\xcc\xc7\x0f\xb4\x6f\x95\xcb\x5d\x0c\x28"
+ "\x93\x70\xbc\x25\x88\x76\x2f\xd7\x86\x9e\x5e\x03\xfa\x9c\x68\xde\x52"
+ "\x23\xc5\xae\xa1\x1c\x58\x79\x1a\x6f\xfa\x52\x31\xfc\x2b\xd5\x33\x3d"
+ "\x49\x60\x80\xa0\x31\x16\x7e\xa5\xd5\x09\x94\x53\x1e\x3b\x56\x3f\x1e"
+ "\x4d\x95\x76\x44\x9d\x59\x7a\x2d\xbc\xea\xe3\x26\x0d\xf6\x68\xee\xba"
+ "\xc3\xbb\x9f\xb6\xf1\xa2\x44\xc2\x96\xd5\xdc\xc4\x1c\xa2\xaf\xeb\x92"
+ "\x4e\xaf\xfc\x1d\x5d\xaf\x30\x9b\x0b\xfd\x19\x1c\x40\xf9\xd0\x0d\xf5"
+ "\x15\x94\xb7\xe9",
+ 276);
+ syscall(SYS_connect, -1, 0x20000340ul, 0x10ul);
+ break;
+ case 2:
+ res = syscall(SYS_semget, 0ul, 0ul, 0x284ul);
+ if (res != -1)
+ r[0] = res;
+ break;
+ case 3:
+ res = syscall(SYS_socket, 0x1cul, 1ul, 0ul);
+ if (res != -1)
+ r[1] = res;
+ break;
+ case 4:
+ syscall(SYS_listen, r[1], 0);
+ break;
+ case 5:
+ res = syscall(SYS_fstat, r[1], 0x20000080ul);
+ if (res != -1)
+ r[2] = *(uint32_t*)0x200000a0;
+ break;
+ case 6:
+ *(uint32_t*)0x20000180 = 0x2840029c;
+ *(uint32_t*)0x20000184 = 0;
+ *(uint32_t*)0x20000188 = 0;
+ *(uint32_t*)0x2000018c = 0;
+ *(uint32_t*)0x20000190 = r[2];
+ *(uint32_t*)0x20000194 = 0;
+ *(uint16_t*)0x20000198 = 0;
+ *(uint16_t*)0x2000019a = 0;
+ *(uint64_t*)0x200001a0 = 0;
+ *(uint64_t*)0x200001a8 = 0;
+ *(uint64_t*)0x200001b0 = 0;
+ *(uint64_t*)0x200001b8 = 0;
+ *(uint64_t*)0x200001c0 = 0;
+ *(uint64_t*)0x200001c8 = 0;
+ *(uint64_t*)0x200001d0 = 0;
+ syscall(SYS___semctl, r[0], 0ul, 0xaul, 0x20000180ul);
+ break;
+ case 7:
+ res = syscall(SYS_socket, 0x1cul, 1ul, 0x84ul);
+ if (res != -1)
+ r[3] = res;
+ break;
+ case 8:
+ syscall(SYS_connect, r[3], 0ul, 0ul);
+ break;
+ case 9:
+ syscall(SYS_fcntl, r[3], 5ul, 0);
+ break;
+ case 10:
+ syscall(SYS_getresuid, 0x20000040ul, 0ul, 0ul);
+ break;
+ case 11:
+ syscall(SYS___semctl, 0, 0ul, 0xaul, 0ul);
+ break;
+ case 12:
+ syscall(SYS___semctl, 0, 0ul, 1ul, 0ul);
+ break;
+ case 13:
+ res = syscall(SYS_semget, 0ul, 0ul, 0x284ul);
+ if (res != -1)
+ r[4] = res;
+ break;
+ case 14:
+ syscall(SYS_freebsd11_fstat, -1, 0ul);
+ break;
+ case 15:
+ syscall(SYS___semctl, r[4], 0ul, 1ul, 0ul);
+ break;
+ case 16:
+ res = syscall(SYS_socket, 0x1cul, 1ul, 0ul);
+ if (res != -1)
+ r[5] = res;
+ break;
+ case 17:
+ syscall(SYS_listen, r[5], 0);
+ break;
+ case 18:
+ syscall(SYS_fstat, r[5], 0ul);
+ break;
+ }
+}
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul);
+ loop();
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller4 -Wall -Wextra -O2 /tmp/syzkaller4.c -lpthread ||
+ exit 1
+
+(cd /tmp; timeout 5m ./syzkaller4)
+
+rm -f /tmp/syzkaller4 /tmp/syzkaller4.c
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller5.sh b/tools/test/stress2/misc/syzkaller5.sh
new file mode 100755
index 000000000000..473fc5fba3ad
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller5.sh
@@ -0,0 +1,83 @@
+#!/bin/sh
+
+# panic: to_ticks == 0 for timer type 11
+# cpuid = 0
+# time = 1585113766
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x47/frame 0xfffffe0024a5e4b0
+# vpanic() at vpanic+0x1c7/frame 0xfffffe0024a5e510
+# panic() at panic+0x43/frame 0xfffffe0024a5e570
+# sctp_timer_start() at sctp_timer_start+0xc7f/frame 0xfffffe0024a5e5d0
+# sctp_lower_sosend() at sctp_lower_sosend+0x4b9a/frame 0xfffffe0024a5e7b0
+# sctp_sosend() at sctp_sosend+0x501/frame 0xfffffe0024a5e8e0
+# sosend() at sosend+0xc6/frame 0xfffffe0024a5e950
+# kern_sendit() at kern_sendit+0x33d/frame 0xfffffe0024a5ea00
+# sendit() at sendit+0x224/frame 0xfffffe0024a5ea60
+# sys_sendto() at sys_sendto+0x5c/frame 0xfffffe0024a5eac0
+# amd64_syscall() at amd64_syscall+0x2f4/frame 0xfffffe0024a5ebf0
+# fast_syscall_common() at fast_syscall_common+0x101/frame 0xfffffe0024a5ebf0
+
+# Fixed by r359405
+
+[ `uname -p` = "i386" ] && exit 0
+
+. ../default.cfg
+kldstat -v | grep -q sctp || kldload sctp.ko
+cat > /tmp/syzkaller5.c <<EOF
+// https://syzkaller.appspot.com/bug?id=0cebb51a059e6a7e259573cc2dd635db8dffcb80
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+
+#define _GNU_SOURCE
+
+#include <pwd.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul);
+ intptr_t res = 0;
+ res = syscall(SYS_socket, 0x1cul, 5ul, 0x84);
+ if (res != -1)
+ r[0] = res;
+ *(uint32_t*)0x20000200 = 0;
+ *(uint32_t*)0x20000204 = 0;
+ *(uint32_t*)0x20000208 = 0xfffffffb;
+ *(uint32_t*)0x2000020c = 0;
+ syscall(SYS_setsockopt, r[0], 0x84, 1, 0x20000200ul, 0x3f6ul);
+ memcpy((void*)0x20000040, "\x11", 1);
+ *(uint8_t*)0x20000100 = 0x10;
+ *(uint8_t*)0x20000101 = 2;
+ *(uint16_t*)0x20000102 = htobe16(0x4e21);
+ *(uint8_t*)0x20000104 = 0xac;
+ *(uint8_t*)0x20000105 = 0x14;
+ *(uint8_t*)0x20000106 = 0;
+ *(uint8_t*)0x20000107 = 0xbb;
+ *(uint8_t*)0x20000108 = 0;
+ *(uint8_t*)0x20000109 = 0;
+ *(uint8_t*)0x2000010a = 0;
+ *(uint8_t*)0x2000010b = 0;
+ *(uint8_t*)0x2000010c = 0;
+ *(uint8_t*)0x2000010d = 0;
+ *(uint8_t*)0x2000010e = 0;
+ *(uint8_t*)0x2000010f = 0;
+ syscall(SYS_sendto, r[0], 0x20000040ul, 1ul, 0x104ul, 0x20000100ul, 0x10ul);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller5 -Wall -Wextra -O2 /tmp/syzkaller5.c -lpthread ||
+ exit 1
+
+(cd /tmp; ./syzkaller5)
+
+rm /tmp/syzkaller5 /tmp/syzkaller5.c
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller6.sh b/tools/test/stress2/misc/syzkaller6.sh
new file mode 100755
index 000000000000..f6e3950a7577
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller6.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+
+# panic: to_ticks == 0 for timer type 2
+# cpuid = 1
+# time = 1585113958
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x47/frame 0xfffffe0024a54420
+# vpanic() at vpanic+0x1c7/frame 0xfffffe0024a54480
+# panic() at panic+0x43/frame 0xfffffe0024a544e0
+# sctp_timer_start() at sctp_timer_start+0xc7f/frame 0xfffffe0024a54540
+# sctp_send_initiate() at sctp_send_initiate+0x10b/frame 0xfffffe0024a545d0
+# sctp_lower_sosend() at sctp_lower_sosend+0x3f54/frame 0xfffffe0024a547b0
+# sctp_sosend() at sctp_sosend+0x501/frame 0xfffffe0024a548e0
+# sosend() at sosend+0xc6/frame 0xfffffe0024a54950
+# kern_sendit() at kern_sendit+0x33d/frame 0xfffffe0024a54a00
+# sendit() at sendit+0x224/frame 0xfffffe0024a54a60
+# sys_sendto() at sys_sendto+0x5c/frame 0xfffffe0024a54ac0
+# amd64_syscall() at amd64_syscall+0x2f4/frame 0xfffffe0024a54bf0
+# fast_syscall_common() at fast_syscall_common+0x101/frame 0xfffffe0024a54bf0
+
+# Fixed by r359405
+
+[ `uname -p` = "i386" ] && exit 0
+
+. ../default.cfg
+kldstat -v | grep -q sctp || kldload sctp.ko
+cat > /tmp/syzkaller6.c <<EOF
+// https://syzkaller.appspot.com/bug?id=86fc212419e315473f7db833b9c835be21f17029
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+
+#define _GNU_SOURCE
+
+#include <pwd.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+uint64_t r[1] = {0xffffffffffffffff};
+
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul);
+ intptr_t res = 0;
+ res = syscall(SYS_socket, 0x1cul, 5ul, 0x84);
+ if (res != -1)
+ r[0] = res;
+ *(uint32_t*)0x20000200 = 0;
+ *(uint32_t*)0x20000204 = 0xfffffff9;
+ *(uint32_t*)0x20000208 = 0xfffffffb;
+ *(uint32_t*)0x2000020c = 0;
+ syscall(SYS_setsockopt, r[0], 0x84, 1, 0x20000200ul, 0x39eul);
+ memcpy((void*)0x20000040, "\x11", 1);
+ *(uint8_t*)0x20000100 = 0x10;
+ *(uint8_t*)0x20000101 = 2;
+ *(uint16_t*)0x20000102 = htobe16(0x4e21);
+ *(uint8_t*)0x20000104 = 0xac;
+ *(uint8_t*)0x20000105 = 0x14;
+ *(uint8_t*)0x20000106 = 0;
+ *(uint8_t*)0x20000107 = 0xbb;
+ *(uint8_t*)0x20000108 = 0;
+ *(uint8_t*)0x20000109 = 0;
+ *(uint8_t*)0x2000010a = 0;
+ *(uint8_t*)0x2000010b = 0;
+ *(uint8_t*)0x2000010c = 0;
+ *(uint8_t*)0x2000010d = 0;
+ *(uint8_t*)0x2000010e = 0;
+ *(uint8_t*)0x2000010f = 0;
+ syscall(SYS_sendto, r[0], 0x20000040ul, 1ul, 0ul, 0x20000100ul, 0x10ul);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller6 -Wall -Wextra -O2 /tmp/syzkaller6.c -lpthread ||
+ exit 1
+
+(cd /tmp; ./syzkaller6)
+
+rm /tmp/syzkaller6 /tmp/syzkaller6.c
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller7.sh b/tools/test/stress2/misc/syzkaller7.sh
new file mode 100755
index 000000000000..f43747006862
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller7.sh
@@ -0,0 +1,161 @@
+#!/bin/sh
+
+# panic: Duplicate free of 0xfffff800049ad800 from zone
+# 0xfffff800041e82c0(mbuf) slab 0xfffff800049adf90(8)
+#
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x47/frame
+# 0xfffffe0016b2c4a0
+# vpanic() at vpanic+0x1e0/frame 0xfffffe0016b2c500
+# panic() at panic+0x43/frame 0xfffffe0016b2c560
+# uma_dbg_free() at uma_dbg_free+0x246/frame 0xfffffe0016b2c5b0
+# uma_zfree_arg() at uma_zfree_arg+0x1aa/frame 0xfffffe0016b2c640
+# uipc_ready() at uipc_ready+0x19f/frame 0xfffffe0016b2c690
+# sendfile_iodone() at sendfile_iodone+0x342/frame 0xfffffe0016b2c6f0
+# vnode_pager_generic_getpages_done_async() at
+# vnode_pager_generic_getpages_done_async+0x4a/frame 0xfffffe0016b2c720
+# bufdone() at bufdone+0xa1/frame 0xfffffe0016b2c7a0
+# g_io_deliver() at g_io_deliver+0x35b/frame 0xfffffe0016b2c800
+# g_io_deliver() at g_io_deliver+0x35b/frame 0xfffffe0016b2c860
+# g_io_deliver() at g_io_deliver+0x35b/frame 0xfffffe0016b2c8c0
+# g_disk_done() at g_disk_done+0x179/frame 0xfffffe0016b2c910
+# dadone() at dadone+0x655/frame 0xfffffe0016b2c9a0
+# xpt_done_process() at xpt_done_process+0x5b2/frame 0xfffffe0016b2ca00
+# xpt_done_td() at xpt_done_td+0x175/frame 0xfffffe0016b2ca60
+# fork_exit() at fork_exit+0xb0/frame 0xfffffe0016b2cab0
+# fork_trampoline() at fork_trampoline+0xe/frame 0xfffffe0016b2cab0
+# --- trap 0, rip = 0, rsp = 0, rbp = 0 ---
+
+# Reproduced on r359769
+# Fixed by r359779
+
+[ `uname -p` = "i386" ] && exit 0
+
+. ../default.cfg
+cat > /tmp/syzkaller7.c <<EOF
+// https://syzkaller.appspot.com/bug?id=3a27f6d4656356facc22a9329be797ef1143d0ad
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+
+#include <pwd.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+static void kill_and_wait(int pid, int* status)
+{
+ kill(pid, SIGKILL);
+ while (waitpid(-1, status, 0) != pid) {
+ }
+}
+
+static void sleep_ms(uint64_t ms)
+{
+ usleep(ms * 1000);
+}
+
+static uint64_t current_time_ms(void)
+{
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ exit(1);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
+static void execute_one(void);
+
+#define WAIT_FLAGS 0
+
+static void loop(void)
+{
+ int iter;
+ for (iter = 0;; iter++) {
+ int pid = fork();
+ if (pid < 0)
+ exit(1);
+ if (pid == 0) {
+ execute_one();
+ exit(0);
+ }
+ int status = 0;
+ uint64_t start = current_time_ms();
+ for (;;) {
+ if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
+ break;
+ sleep_ms(1);
+ if (current_time_ms() - start < 5 * 1000)
+ continue;
+ kill_and_wait(pid, &status);
+ break;
+ }
+ }
+}
+
+uint64_t r[5] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff,
+ 0xffffffffffffffff, 0xffffffffffffffff};
+
+void execute_one(void)
+{
+ long res = 0;
+ memcpy((void*)0x20001180, "./file0\000", 8);
+ res = syscall(SYS_open, 0x20001180, 0x8240, 0);
+ if (res != -1)
+ r[0] = res;
+ res = syscall(SYS_socketpair, 1, 1, 0, 0x20000100);
+ if (res != -1) {
+ r[1] = *(uint32_t*)0x20000100;
+ r[2] = *(uint32_t*)0x20000104;
+ }
+ memcpy((void*)0x20000480, "./file0\000", 8);
+ res = syscall(SYS_open, 0x20000480, 0x80000000000206, 0);
+ if (res != -1)
+ r[3] = res;
+ res = syscall(SYS_dup, r[3]);
+ if (res != -1)
+ r[4] = res;
+ *(uint64_t*)0x20000100 = 0x200002c0;
+ memcpy((void*)0x200002c0, "\xdd", 1);
+ *(uint64_t*)0x20000108 = 1;
+ syscall(SYS_pwritev, r[4], 0x20000100, 1, 0);
+ *(uint64_t*)0x20002e80 = 0x20000540;
+ memcpy((void*)0x20000540, "\x7f", 1);
+ *(uint64_t*)0x20002e88 = 1;
+ syscall(SYS_pwritev, r[3], 0x20002e80, 1, 0xbf24);
+ memcpy((void*)0x200004c0,
+ "\x89\x88\xaa\x4a\xc3\x95\x23\x77\x54\xee\x66\xf3\x8d\xa4\xae\xf3\x47"
+ "\x6d\x78\xb7\x1f\xe6\x0d\xb7\x4a\x9f\xb9\xc9\x99\x91\x6c\x98",
+ 32);
+ syscall(SYS_setsockopt, r[2], 0, 2, 0x200004c0, 0x20);
+ syscall(SYS_fcntl, r[4], 4, 0x10044);
+ syscall(SYS_read, r[4], 0x20000000, 0x6d999);
+ syscall(SYS_sendfile, r[0], r[1], 0, 2);
+}
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000, 0x1000000, 3, 0x1012, -1, 0ul);
+ loop();
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller7 -Wall -Wextra -O2 /tmp/syzkaller7.c -lpthread ||
+ exit 1
+
+(cd /tmp; timeout 5m ./syzkaller7) &
+sleep 60
+while pkill -9 syzkaller7; do :; done
+wait
+
+rm -f /tmp/syzkaller7 /tmp/syzkaller7.c /tmp/syzkaller7.core /tmp/file0
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller8.sh b/tools/test/stress2/misc/syzkaller8.sh
new file mode 100755
index 000000000000..32121a4cc1da
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller8.sh
@@ -0,0 +1,111 @@
+#!/bin/sh
+
+# panic: prot 0x3 is not subset of max_prot 0x5
+# cpuid = 0
+# time = 1586745417
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x47/frame 0xfffffe0015fe8650
+# vpanic() at vpanic+0x1c7/frame 0xfffffe0015fe86b0
+# panic() at panic+0x43/frame 0xfffffe0015fe8710
+# vm_map_insert() at vm_map_insert+0xa11/frame 0xfffffe0015fe87d0
+# vm_map_fixed() at vm_map_fixed+0x19c/frame 0xfffffe0015fe8850
+# vm_mmap_object() at vm_mmap_object+0x3be/frame 0xfffffe0015fe88e0
+# shm_mmap() at shm_mmap+0x2e3/frame 0xfffffe0015fe8980
+# kern_mmap_req() at kern_mmap_req+0xa72/frame 0xfffffe0015fe8a70
+# sys_mmap() at sys_mmap+0x53/frame 0xfffffe0015fe8ac0
+# amd64_syscall() at amd64_syscall+0x236/frame 0xfffffe0015fe8bf0
+
+# Reproduced on r359818
+# Fixed by r359892
+
+[ `uname -p` = "i386" ] && exit 0
+
+. ../default.cfg
+cat > /tmp/syzkaller8.c <<EOF
+// http://syzkaller.backtrace.io:8080/report?id=ba93405590ed607e9b2d65025a44ff60dc6c4bcc
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+
+#define _GNU_SOURCE
+
+#include <pwd.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+static __thread int skip_segv;
+static __thread jmp_buf segv_env;
+
+static void segv_handler(int sig, siginfo_t* info, void* ctx __unused)
+{
+ uintptr_t addr = (uintptr_t)info->si_addr;
+ const uintptr_t prog_start = 1 << 20;
+ const uintptr_t prog_end = 100 << 20;
+ if (__atomic_load_n(&skip_segv, __ATOMIC_RELAXED) &&
+ (addr < prog_start || addr > prog_end)) {
+ _longjmp(segv_env, 1);
+ }
+ exit(sig);
+}
+
+static void install_segv_handler(void)
+{
+ struct sigaction sa;
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_sigaction = segv_handler;
+ sa.sa_flags = SA_NODEFER | SA_SIGINFO;
+ sigaction(SIGSEGV, &sa, NULL);
+ sigaction(SIGBUS, &sa, NULL);
+}
+
+#define NONFAILING(...) \
+ { \
+ __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \
+ if (_setjmp(segv_env) == 0) { \
+ __VA_ARGS__; \
+ } \
+ __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \
+ }
+
+uint64_t r[3] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff};
+
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul);
+ install_segv_handler();
+ intptr_t res = 0;
+ syscall(SYS___getcwd, 0x20000000ul, 0xc7ul);
+ res = syscall(SYS_socket, 0x1cul, 3ul, 0x3a);
+ if (res != -1)
+ r[0] = res;
+ res = syscall(SYS_fcntl, r[0], 1ul, -1);
+ if (res != -1)
+ r[1] = res;
+ syscall(SYS_close, r[1]);
+ syscall(SYS_mprotect, 0x20000000ul, 0x4000ul, 1ul);
+ NONFAILING(memcpy((void*)0x20000000, "./file1\000", 8));
+ syscall(SYS_freebsd12_shm_open, 0x20000000ul, 0x200ul, 0ul);
+ res = syscall(SYS_freebsd10_pipe, 0x20000080ul);
+ if (res != -1)
+ NONFAILING(r[2] = *(uint32_t*)0x20000084);
+ syscall(SYS_mmap, 0x20d43000ul, 0x200000ul, 3ul, 0x12ul, r[2], 0ul);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller8 -Wall -Wextra -O2 /tmp/syzkaller8.c -lpthread ||
+ exit 1
+
+(cd /tmp; ./syzkaller8) &
+sleep 60
+pkill -9 syzkaller8
+wait
+
+rm -f /tmp/syzkaller8 /tmp/syzkaller8.c /tmp/syzkaller8.core /tmp/file0
+exit 0
diff --git a/tools/test/stress2/misc/syzkaller9.sh b/tools/test/stress2/misc/syzkaller9.sh
new file mode 100755
index 000000000000..0336d215cc1d
--- /dev/null
+++ b/tools/test/stress2/misc/syzkaller9.sh
@@ -0,0 +1,102 @@
+#!/bin/sh
+
+# Fatal trap 12: page fault while in kernel mode
+# cpuid = 0; apic id = 00
+# fault virtual address = 0x18
+# fault code = supervisor read data, page not present
+# instruction pointer = 0x20:0xffffffff80ffe995
+# stack pointer = 0x28:0xfffffe0015e1d6f0
+# frame pointer = 0x28:0xfffffe0015e1d720
+# code segment = base 0x0, limit 0xfffff, type 0x1b
+# = DPL 0, pres 1, long 1, def32 0, gran 1
+# processor eflags = interrupt enabled, resume, IOPL = 0
+# current process = 744 (syz-executor7538254)
+# trap number = 12
+# panic: page fault
+# cpuid = 0
+# time = 1575776142
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x47/frame 0xfffffe0015e1d2f0
+# vpanic() at vpanic+0x1c7/frame 0xfffffe0015e1d360
+# panic() at panic+0x43/frame 0xfffffe0015e1d3c0
+# trap_fatal() at trap_fatal+0x4cc/frame 0xfffffe0015e1d440
+# trap_pfault() at trap_pfault+0xdc/frame 0xfffffe0015e1d4e0
+# trap() at trap+0x441/frame 0xfffffe0015e1d620
+# calltrap() at calltrap+0x8/frame 0xfffffe0015e1d620
+# --- trap 0xc, rip = 0xffffffff80ffe995, rsp = 0xfffffe0015e1d6f0, rbp = 0xfffffe0015e1d720 ---
+# __mtx_assert() at __mtx_assert+0x85/frame 0xfffffe0015e1d720
+# uipc_send() at uipc_send+0xeec/frame 0xfffffe0015e1d810
+# sosend_generic() at sosend_generic+0x707/frame 0xfffffe0015e1d8d0
+# sosend() at sosend+0xc6/frame 0xfffffe0015e1d940
+# kern_sendit() at kern_sendit+0x32d/frame 0xfffffe0015e1d9f0
+# sendit() at sendit+0x226/frame 0xfffffe0015e1da50
+# sys_sendmsg() at sys_sendmsg+0x8b/frame 0xfffffe0015e1dab0
+# amd64_syscall() at amd64_syscall+0x46d/frame 0xfffffe0015e1dbf0
+# fast_syscall_common() at fast_syscall_common+0x101/frame 0xfffffe0015e1dbf0
+# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x41c3ea, rsp = 0x7fffffffeb98, rbp = 0x7fffffffebf0 ---
+
+# Reproduced on r359818
+# Fixed by r359893
+
+[ `uname -p` = "i386" ] && exit 0
+
+. ../default.cfg
+cat > /tmp/syzkaller9.c <<EOF
+// http://syzkaller.backtrace.io:8080/report?id=03a08d05681c3e7f38ab6ab3f0d025105c3dcea3
+// autogenerated by syzkaller (https://github.com/google/syzkaller)
+
+#define _GNU_SOURCE
+
+#include <pwd.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+uint64_t r[2] = {0xffffffffffffffff, 0xffffffffffffffff};
+
+int main(void)
+{
+ syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul);
+ intptr_t res = 0;
+ res = syscall(SYS_socket, 1ul, 5ul, 0ul);
+ if (res != -1)
+ r[0] = res;
+ memcpy((void*)0x20000000, "\x1d\x01\x2e\x2f\x66\x69\x6c\x65\x30\x00", 10);
+ syscall(SYS_bind, r[0], 0x20000000ul, 0xaul);
+ syscall(SYS_listen, r[0], 0);
+ res = syscall(SYS_socket, 1ul, 5ul, 0ul);
+ if (res != -1)
+ r[1] = res;
+ *(uint64_t*)0x20002640 = 0x20000000;
+ *(uint8_t*)0x20000000 = 0xa;
+ *(uint8_t*)0x20000001 = 1;
+ memcpy((void*)0x20000002, "./file0\000", 8);
+ *(uint32_t*)0x20002648 = 0xa;
+ *(uint64_t*)0x20002650 = 0;
+ *(uint64_t*)0x20002658 = 0;
+ *(uint64_t*)0x20002660 = 0x20002580;
+ memcpy((void*)0x20002580,
+ "\x10\x00\x00\x00\xff\xff\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00",
+ 16);
+ *(uint64_t*)0x20002668 = 0x10;
+ *(uint32_t*)0x20002670 = 0;
+ syscall(SYS_sendmsg, r[1], 0x20002640ul, 0ul);
+ return 0;
+}
+EOF
+mycc -o /tmp/syzkaller9 -Wall -Wextra -O2 /tmp/syzkaller9.c -lpthread ||
+ exit 1
+
+(cd /tmp; ./syzkaller9) &
+sleep 60
+pkill -9 syzkaller9
+wait
+
+rm -f /tmp/syzkaller9 /tmp/syzkaller9.c /tmp/syzkaller9.core /tmp/file0
+exit 0
diff --git a/tools/test/stress2/misc/tar.sh b/tools/test/stress2/misc/tar.sh
new file mode 100755
index 000000000000..c1632495cdc2
--- /dev/null
+++ b/tools/test/stress2/misc/tar.sh
@@ -0,0 +1,132 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# tar x simulator
+# Create 100.000 files of size 64k; a total of 6GB
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/tar.c
+mycc -o tar -Wall -Wextra -O0 -g tar.c || exit 1
+rm -f tar.c
+cd $odir
+
+export LANG=C
+wdir=`dirname $diskimage`
+rm -rf $wdir/tar.tmp
+[ `df -k $wdir | tail -1 | awk '{print $4}'` -lt \
+ $((6 * 1024 * 1024)) ] &&
+ echo "Not enough disk space on $wdir." && exit 0
+
+for i in `jot $(sysctl -n hw.ncpu)`; do
+ /tmp/tar $wdir | ministat -n | tail -2 &
+ sleep 3
+done
+wait
+
+rm -rf /tmp/tar $wdir/tar.tmp
+exit 0
+
+EOF
+#include <sys/stat.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <time.h>
+#include <unistd.h>
+
+#define RUNTIME 1200
+
+char buf[64*1024];
+
+int
+main(int argc, char *argv[])
+{
+ struct timeval start, stop, diff;
+ time_t stime;
+ uint64_t mx, usec;
+ int fd, i, j, k;
+ char path[128];
+
+ if (argc != 2)
+ errx(1, "Usage: %s <path>", argv[0]);
+ mx = 0;
+ stime = time(NULL);
+ snprintf(path, sizeof(path), "%s/tar.tmp", argv[1]);
+ mkdir(path, 0777);
+ for (i = 0; i < 100; i++) {
+ snprintf(path, sizeof(path), "%s/tar.tmp/d%d", argv[1], i);
+ if (mkdir(path, 0777) == -1)
+ if (errno != EEXIST)
+ err(1, "mkdir(%s)", path);
+ for (j = 0; j < 100; j++) {
+ snprintf(path, sizeof(path), "%s/tar.tmp/d%d/d%d",
+ argv[1], i, j);
+ if (mkdir(path, 0777) == -1)
+ if (errno != EEXIST)
+ err(1, "mkdir(%s)", path);
+ }
+ }
+ for (i = 0; i < 100; i++) {
+ for (j = 0; j < 100; j++) {
+ for (k = 0; k < 10; k++) {
+ snprintf(path, sizeof(path),
+ "%s/tar.tmp/d%d/d%d/f%d", argv[1], i, j, k);
+ gettimeofday(&start, NULL);
+ unlink(path);
+ if ((fd = open(path, O_RDWR | O_CREAT, 0644))
+ == -1)
+ err(1, "open(%s)", path);
+ write(fd, buf, sizeof(buf));
+ close(fd);
+ gettimeofday(&stop, NULL);
+ timersub(&stop, &start, &diff);
+ usec = ((uint64_t)1000000 *
+ diff.tv_sec + diff.tv_usec);
+ if (usec > mx)
+ mx = usec;
+ fprintf(stdout, "%.3f %s\n",
+ (double)usec / 1000000, path);
+ }
+ }
+ if (time(NULL) - stime > RUNTIME) {
+ fprintf(stderr, "Timed out at %s.\n", path);
+ break;
+ }
+ }
+ if (mx > 5000000) {
+// fprintf(stderr, "%.3f %s : FAIL\n", (double)mx / 1000000, path);
+ return (1);
+ } else
+ return (0);
+}
diff --git a/tools/test/stress2/misc/tcp.sh b/tools/test/stress2/misc/tcp.sh
new file mode 100755
index 000000000000..b2655d1d4ef3
--- /dev/null
+++ b/tools/test/stress2/misc/tcp.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# "panic: tcp_do_segment: TCPS_LISTEN" seen:
+# http://people.freebsd.org/~pho/stress/log/tcp.txt
+
+# "panic: m_uiotombuf: progress != total" seen:
+# https://people.freebsd.org/~pho/stress/log/gleb010.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+[ $((`sysctl -n hw.usermem` / 1024 / 1024 / 1024)) -le 3 ] && exit 0
+
+. ../default.cfg
+
+rm -rf /tmp/stressX.control
+[ ! -d $RUNDIR ] && mkdir -p $RUNDIR
+chmod 777 $RUNDIR
+export runRUNTIME=15m
+export tcpLOAD=100
+n=`su $testuser -c "limits | grep maxprocesses | awk '{print \\$NF}'"`
+n=$((n - `ps aux | wc -l`))
+export tcpINCARNATIONS=$((n / 2 - 400))
+[ $tcpINCARNATIONS -le 0 ] && exit 0
+export TESTPROGS=" ./testcases/tcp/tcp"
+
+su $testuser -c '(cd ..; ./testcases/run/run $TESTPROGS)' &
+
+sleep $((15 * 60))
+while pkill -9 -U $testuser "run|tcp"; do
+ sleep .5
+done
+wait
diff --git a/tools/test/stress2/misc/tcp2.sh b/tools/test/stress2/misc/tcp2.sh
new file mode 100755
index 000000000000..5cffe71d7e1b
--- /dev/null
+++ b/tools/test/stress2/misc/tcp2.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# "panic: kmem_malloc(4096): kmem_map too small ..." seen.
+# "panic: sackhint bytes rtx >= 0" seen.
+# "panic: pmap_release: pmap 0xfffffe012fb61b08 resident count 1 != 0" seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+[ $((`sysctl -n hw.usermem` / 1024 / 1024 / 1024)) -le 3 ] && exit 0
+
+. ../default.cfg
+
+rm -rf /tmp/stressX.control
+[ ! -d $RUNDIR ] && mkdir -p $RUNDIR
+chmod 777 $RUNDIR
+export runRUNTIME=15m
+export tcpLOAD=100
+export swapLOAD=100
+n=`su $testuser -c "limits | grep maxprocesses | awk '{print \\$NF}'"`
+[ $n -gt 50000 ] && n=50000
+n=$((n - `ps aux | wc -l`))
+export tcpINCARNATIONS=$((n / 2 - 400))
+[ $tcpINCARNATIONS -le 0 ] && exit 0
+export TESTPROGS="
+./testcases/tcp/tcp
+./testcases/swap/swap
+"
+
+su $testuser -c '(cd ..; ./testcases/run/run $TESTPROGS)' &
+
+sleep $((15 * 60))
+while pkill -9 -U $testuser "run|tcp|swap"; do
+ sleep .5
+done
+wait
diff --git a/tools/test/stress2/misc/tcp3.sh b/tools/test/stress2/misc/tcp3.sh
new file mode 100755
index 000000000000..ca3161c25821
--- /dev/null
+++ b/tools/test/stress2/misc/tcp3.sh
@@ -0,0 +1,288 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# No problems seen
+# Stand alone version of testcases/tcp
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/tcp3.c
+mycc -o tcp3 -Wall -Wextra -O0 -g tcp3.c || exit 1
+rm -f tcp3.c
+
+daemon sh -c "(cd $odir/../testcases/swap; ./swap -t 5m)" > /dev/null 2>&1
+timeout 2m /tmp/tcp3
+s=$?
+while pgrep -q swap; do
+ pkill -9 swap
+done
+
+if pgrep -q tcp3; then
+ echo FAIL
+ pgrep tcp3 | while read pid; do
+ ps -lp$pid
+ procstat -kk $pid
+ done
+ exit 1
+fi
+rm -rf /tmp/tcp3 /tmp/tcp3.c
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define NB (400 * 1024 * 1024)
+#define SYNC 0
+#define PARALLEL 8
+#define RUNTIME (1 * 60)
+
+static sig_atomic_t done_testing;
+static volatile u_int *share;
+static int bufsize;
+static int port;
+
+static void
+alarm_handler(int i __unused)
+{
+
+ done_testing++;
+}
+
+static void
+exit_handler(int i __unused)
+{
+
+ _exit(1);
+}
+
+static void
+callcleanup(void)
+{
+}
+
+static int
+random_int(int mi, int ma)
+{
+ return (arc4random() % (ma - mi + 1) + mi);
+}
+
+static void
+reader(void) {
+ struct sockaddr_in inetaddr, inetpeer;
+ socklen_t len;
+ int n, t, *buf;
+ int on;
+ int tcpsock, msgsock;
+
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ if (random_int(1,100) > 60)
+ sleep(6);
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ t = 0;
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__,
+ __LINE__);
+ while (done_testing == 0) {
+ if ((n = read(msgsock, buf, bufsize)) < 0)
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ t += n;
+ if (n == 0) break;
+ }
+ close(msgsock);
+ return;
+}
+
+static void
+writer(void) {
+ struct hostent *hostent;
+ struct sockaddr_in inetaddr;
+ int i, *buf, r;
+ int tcpsock, on;
+
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ hostent = gethostbyname ("localhost");
+ bzero((char *) &inetaddr, sizeof(inetaddr));
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__,
+ __LINE__);
+ for (i = 0; i < bufsize / (int)sizeof(int); i++)
+ buf[i] = i;
+
+ for (;;) {
+ for (i = 0; i < NB; i+= bufsize) {
+ if (write(tcpsock, buf, bufsize) < 0) {
+ if (errno == EPIPE)
+ return;
+ if (errno != ECONNRESET)
+ err(1, "write(%d), %s:%d", tcpsock,
+ __FILE__, __LINE__);
+ _exit(EXIT_SUCCESS);
+ }
+ }
+ }
+ return;
+}
+
+static int
+test2(void)
+{
+ pid_t pid;
+
+ if ((pid = fork()) == 0) {
+ writer();
+ _exit(EXIT_SUCCESS);
+
+ } else if (pid > 0) {
+ reader();
+ kill(pid, SIGINT);
+ waitpid(pid, NULL, 0);
+ } else
+ err(1, "fork(), %s:%d", __FILE__, __LINE__);
+
+ return (0);
+}
+
+static void
+test(int nb)
+{
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ port = 12340 + nb;
+ bufsize = 2 << random_int(1, 12);
+ signal(SIGALRM, alarm_handler);
+ signal(SIGINT, exit_handler);
+ atexit(callcleanup);
+
+ alarm(2);
+ test2();
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ size_t len;
+ time_t start;
+ int e, i, pids[PARALLEL], status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test(i);
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ e += status == 0 ? 0 : 1;
+ }
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/tcp4.sh b/tools/test/stress2/misc/tcp4.sh
new file mode 100755
index 000000000000..514d73595e54
--- /dev/null
+++ b/tools/test/stress2/misc/tcp4.sh
@@ -0,0 +1,249 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# "panic: in_pcbrele_wlocked: refcount 0" seen in WiP kernel code.
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+cd /tmp
+
+cat > /tmp/tcp4.c <<EOF
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <errno.h>
+#include <netdb.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define NB (400 * 1024 * 1024)
+#define RUNTIME (16 * 60)
+
+static int port;
+static int bufsize;
+
+static int
+random_int(int mi, int ma)
+{
+ return (arc4random() % (ma - mi + 1) + mi);
+}
+
+static void
+reader(void) {
+ socklen_t len;
+ struct sockaddr_in inetaddr, inetpeer;
+ int tcpsock, msgsock ,on, n, t, *buf;
+
+ on = 1;
+ setproctitle("%s", __func__);
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ if ((random_int(1,100) > 60)) {
+ usleep(random_int(1000000,1000000) * 60);
+ }
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ t = 0;
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+ for (;;) {
+ if ((n = read(msgsock, buf, bufsize)) < 0)
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ t += n;
+ if (n == 0) break;
+ }
+ close(msgsock);
+ return;
+}
+
+static void
+writer(void) {
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ time_t start;
+ int i, *buf, r;
+ int tcpsock, on;
+
+ on = 1;
+ setproctitle("%s", __func__);
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ hostent = gethostbyname ("localhost");
+ bzero((char *) &inetaddr, sizeof(inetaddr));
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+ for (i = 0; i < bufsize / (int)sizeof(int); i++)
+ buf[i] = i;
+
+ start = time(NULL);
+ while (time(NULL) - start < 60) {
+ for (i = 0; i < NB; i+= bufsize) {
+ if (write(tcpsock, buf, bufsize) < 0) {
+ if (errno == EPIPE)
+ return;
+ if (errno != ECONNRESET)
+ err(1, "write(%d), %s:%d", tcpsock,
+ __FILE__, __LINE__);
+ _exit(EXIT_SUCCESS);
+ }
+ }
+ }
+ return;
+}
+
+int
+setup(int nb)
+{
+ port = 12340 + nb;
+ bufsize = 2 << random_int(1, 12);
+ return (0);
+}
+
+int
+test(void)
+{
+ pid_t pid;
+
+ sleep(1);
+ if ((pid = fork()) == 0) {
+ alarm(180);
+ writer();
+ _exit(EXIT_SUCCESS);
+
+ } else if (pid > 0) {
+ alarm(180);
+ reader();
+ kill(pid, SIGINT);
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+ } else
+ err(1, "fork(), %s:%d", __FILE__, __LINE__);
+
+ return (0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ pid_t *pid;
+ time_t start;
+ int i, n;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <threads>\n", argv[0]);
+ exit(1);
+ }
+ n = atoi(argv[1]);
+ pid = calloc(sizeof(pid_t *), n);
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ for (i = 0; i < n; i++) {
+ if ((pid[i] = fork()) == 0) {
+ setup(i);
+ test();
+ _exit(0);
+ }
+ }
+ for (i = 0; i < n; i++)
+ if (waitpid(pid[i], NULL, 0) != pid[i])
+ err(1, "waitpid(%d) l%d", pid[i], __LINE__);
+ fprintf(stderr, "*** Done loop\n");
+ }
+
+ return (0);
+}
+EOF
+
+mycc -o tcp4 -Wall -Wextra -O2 tcp4.c || exit 1
+rm tcp4.c
+
+n=`sysctl -n kern.maxprocperuid`
+n=$((n / 2 / 10 * 8))
+./tcp4 $n > /dev/null 2>&1 &
+sleep 30
+while pgrep -q tcp4; do
+ pkill tcp4
+done
+wait
+rm -f tcp4
+exit 0
diff --git a/tools/test/stress2/misc/temp.sh b/tools/test/stress2/misc/temp.sh
new file mode 100755
index 000000000000..5c1a73cd18e1
--- /dev/null
+++ b/tools/test/stress2/misc/temp.sh
@@ -0,0 +1,167 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Temp file test scenario.
+# "temp: unlink(file.034434.48): Permission denied" seen.
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+export LANG=C
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/temp.c
+mycc -o temp -Wall -Wextra -O0 -g temp.c || exit 1
+rm -f temp.c
+cd $odir
+
+[ -z "$nfs_export" ] && exit 0
+ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
+ exit 0
+
+mount | grep "on $mntpoint " | grep -q nfs && umount $mntpoint
+mount -t nfs -o tcp -o retrycnt=3 -o soft -o rw $nfs_export $mntpoint
+mp2=$mntpoint/temp.`jot -rc 8 a z | tr -d '\n'`/temp.dir
+rm -rf $mp2
+mkdir -p $mp2
+chmod 0777 $mp2
+
+log=/tmp/temp.log
+/tmp/temp $mp2 2> $log
+s=$?
+#[ $s -eq 0 ] && ministat -C 3 -A < $log
+rm -rf $mp2
+
+while mount | grep "on $mntpoint " | grep -q nfs; do
+ umount $mntpoint || sleep 1
+done
+rm -rf /tmp/temp $log
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+volatile u_int *share;
+
+#define SYNC 0
+
+#define FILES 1024
+#define PARALLEL 4
+#define RUNTIME (10 * 60)
+
+void
+test(char *dir)
+{
+ pid_t pid;
+ int fd, i;
+ char path[1024];
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ pid = getpid();
+ for (i = 0; i < FILES; i++) {
+ snprintf(path, sizeof(path), "%s/file.%06d.%d", dir, pid, i);
+ if ((fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
+ err(1, "open(%s)", path);
+ if (write(fd, path, sizeof(path)) != sizeof(path))
+ err(1, "write");
+ close(fd);
+ if (unlink(path) == -1)
+ err(1, "unlink(%s)", path);
+ }
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ size_t len;
+ struct timeval start, stop, diff;
+ struct tm *tp;
+ time_t now, then;
+ uint64_t usec;
+ int e, i, loop, pids[PARALLEL], status;
+ char buf[80];
+
+ e = 0;
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <path>\n", argv[0]);
+ _exit(1);
+ }
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ loop = 0;
+ alarm(2 * RUNTIME);
+ now = time(NULL);
+ while ((time(NULL) - now) < RUNTIME && e == 0) {
+ share[SYNC] = 0;
+ gettimeofday(&start, NULL);
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test(argv[1]);
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ waitpid(pids[i], &status, 0);
+ e += status == 0 ? 0 : 1;
+ }
+ gettimeofday(&stop, NULL);
+ timersub(&stop, &start, &diff);
+ usec = ((uint64_t)1000000 *
+ diff.tv_sec + diff.tv_usec);
+ then = time(NULL);
+ tp = localtime(&then);
+ strftime(buf, sizeof(buf), "%H:%M:%S", tp);
+ if (loop != 0) /* Skip warmup */
+ fprintf(stderr, "%s %6d %.3f\n",
+ buf, loop,
+ (double)usec / 1000000);
+ loop++;
+ sleep(5);
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/thr.sh b/tools/test/stress2/misc/thr.sh
new file mode 100755
index 000000000000..4d77436e1892
--- /dev/null
+++ b/tools/test/stress2/misc/thr.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# Used for PAE test
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/thr.c
+mycc -o thr -Wall -Wextra thr.c -pthread || exit 1
+rm -f thr.c
+
+touch thr.continue
+max=0
+s=0
+start=`date '+%s'`
+while [ -r thr.continue ]; do
+ ./thr &
+ sleep 2
+ n=`ps -xH | egrep "thr$" | grep -v grep | wc -l | sed 's/ //g'`
+ [ $max -lt $n ] && max=$n
+ [ $max -gt 5000 ] && break
+ [ `date '+%s'` -gt $((start + 120)) ] && break
+done
+killall thr
+wait
+[ $max -lt 2000 ] &&
+ { s=1; printf "FAIL\nMax threads created is %d\n" $max; }
+
+rm -f /tmp/thr /tmp/thr.continue
+exit $s
+
+EOF
+
+#include <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define NTHREADS 1499
+static pthread_t threads[NTHREADS];
+
+void *
+thr_routine(void *arg __unused)
+{
+ sleep(120);
+ return (0);
+}
+
+int
+main(void)
+{
+ int i, r;
+
+ for (i = 0; i < NTHREADS; i++) {
+ if ((r = pthread_create(&threads[i], NULL, thr_routine, 0)) != 0) {
+ unlink("thr.continue");
+ sleep(120);
+ errc(1, r, "pthread_create()");
+ }
+ }
+
+ for (i = 0; i < NTHREADS; i++)
+ if ((r = pthread_join(threads[i], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", i);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/thr2.sh b/tools/test/stress2/misc/thr2.sh
new file mode 100755
index 000000000000..669e2c38516e
--- /dev/null
+++ b/tools/test/stress2/misc/thr2.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Threads stuck in umtxn.
+# Bug report https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=204178
+# Only seen on arm64.
+
+. ../default.cfg
+
+export runRUNTIME=20m
+export thr1LOAD=100
+export TESTPROGS="
+testcases/swap/swap
+testcases/thr1/thr1
+"
+
+(cd ..; ./testcases/run/run $TESTPROGS)
diff --git a/tools/test/stress2/misc/thr3.sh b/tools/test/stress2/misc/thr3.sh
new file mode 100755
index 000000000000..97147365289b
--- /dev/null
+++ b/tools/test/stress2/misc/thr3.sh
@@ -0,0 +1,104 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Demonstrate VM leakage. Not seen on FreeBSD HEAD.
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > thr3.c
+mycc -o thr3 -Wall -Wextra -O0 thr3.c -lpthread || exit 1
+rm -f thr3.c
+
+./thr3 &
+pid=$!
+
+log=/tmp/$0.$$
+trap "rm -f $log" EXIT INT
+r=`ps -Ovsz -p $pid | head -1`
+echo " $r" > $log
+export max=0
+export n=0
+while true; do
+ sleep 30
+ vsz=`ps -Ovsz -p $pid | tail -1 | awk '{print $2}'`
+ [ -z "$vsz" -o "$vsz" = VSZ ] && break
+ if [ $vsz -gt $max ]; then
+ max=$vsz
+ r=`ps -Ovsz -p $pid | tail -1`
+ echo "`date '+%T'` $r"
+ n=$((n + 1))
+ fi
+done >> $log 2>&1
+[ $n -gt 1 ] && cat $log
+wait
+rm -f thr3
+[ $n -gt 1 ] && exit 1 || exit 0
+EOF
+#include <sys/types.h>
+#include <sys/time.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define NTHREADS 256
+#define RUNTIME (3 * 60)
+
+static void *
+thr_routine(void *arg __unused)
+{
+ getuid();
+ return (NULL);
+}
+
+int
+main(void)
+{
+ pthread_t threads[NTHREADS];
+ time_t start;
+ int i, r;
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ for (i = 0; i < NTHREADS; i++)
+ if ((r = pthread_create(&threads[i], NULL,
+ thr_routine, NULL)) != 0)
+ errc(1, r, "pthread_create()");
+
+ for (i = 0; i < NTHREADS; i++)
+ if ((r = pthread_join(threads[i], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", i);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/timeout.sh b/tools/test/stress2/misc/timeout.sh
new file mode 100755
index 000000000000..079efe3006b5
--- /dev/null
+++ b/tools/test/stress2/misc/timeout.sh
@@ -0,0 +1,51 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Process group orphanage test scenario.
+# "panic: Assertion pgrp->pg_jobc > 0 failed at kern_proc.c:740" seen.
+# Fixed in r364495.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+sed '1,/^EOF/d' < $0 > /tmp/timeout.sh
+chmod +x /tmp/timeout.sh
+
+timeout 30 su $testuser -c 'cd /tmp; ./timeout.sh'
+
+rm /tmp/timeout.sh
+exit 0
+EOF
+#!/bin/sh
+
+for i in `jot 1000`; do
+ sleep 120 &
+done
+wait
+exit 0
diff --git a/tools/test/stress2/misc/tmpfs.sh b/tools/test/stress2/misc/tmpfs.sh
new file mode 100755
index 000000000000..1645eb8999bf
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009-2011 Peter Holm <pho@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 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.
+#
+
+# panic: tmpfs_alloc_vp: type 0xc866ce58 0, seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep "$mntpoint" | grep -q tmpfs && umount $mntpoint
+usermem=`sysctl -n hw.usermem`
+[ `swapinfo | wc -l` -eq 1 ] && usermem=$((usermem/100*80))
+size=$((usermem / 1024 / 1024 / 2))
+mount -o size=${size}m -t tmpfs tmpfs $mntpoint
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=10m # Run tests for 10 minutes
+
+(cd ..; ./run.sh marcus.cfg)
+
+while mount | grep $mntpoint | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
diff --git a/tools/test/stress2/misc/tmpfs10.sh b/tools/test/stress2/misc/tmpfs10.sh
new file mode 100755
index 000000000000..870530c57ea7
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs10.sh
@@ -0,0 +1,155 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# tmpfs(5) name lookup problem seen:
+
+# $ ./tmpfs10.sh
+# tmpfs10: unlink(p01193.14729) at loop #2: No such file or directory
+# unlink(p01193.14729) succeeded at call #2.
+# tmpfs10: unlink(p01186.14409) at loop #2: No such file or directory
+# unlink(p01186.14409) succeeded at call #2.
+# FAIL
+# $
+
+# Fixed in r253967.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > tmpfs10.c
+mycc -o tmpfs10 -Wall -Wextra -O2 -g tmpfs10.c || exit 1
+rm -f tmpfs10.c
+cd $odir
+
+mount | grep -q "on $mntpoint " && umount $mntpoint
+mount -t tmpfs tmpfs $mntpoint
+cd $mntpoint
+/tmp/tmpfs10 ||
+{ find $mntpoint ! -type d | xargs ls -il; echo FAIL; }
+cd $odir
+umount $mntpoint
+rm -f /tmp/tmpfs10
+exit 0
+EOF
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+static int loop;
+int error;
+
+#define PARALLEL 20
+#define SIZE 16000
+
+void
+test2(void)
+{
+ int i, j, k;
+ pid_t pid;
+ char file[128], dir[128];
+
+ loop++;
+ pid = getpid();
+ sprintf(dir,"%s.%05d", getprogname(), pid);
+ if (mkdir(dir, 0770) < 0)
+ err(1, "mkdir(%s), %s:%d", dir, __FILE__, __LINE__);
+
+ if (chdir(dir) == -1)
+ err(1, "chdir(%s), %s:%d", dir, __FILE__, __LINE__);
+
+ for (j = 0; j < SIZE; j++) {
+ sprintf(file,"p%05d.%05d", pid, j);
+ if (symlink("/tmp/not/there", file) == -1) {
+ if (errno != EINTR) {
+ warn("symlink(%s). %s.%d", file, __FILE__, __LINE__);
+ break;
+ }
+ }
+ }
+
+ for (i = --j; i >= 0; i--) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if (unlink(file) == -1) {
+ warn("unlink(%s) at loop #%d", file, loop);
+ error++;
+ for (k = 0; k < 10; k++) {
+ usleep(10000);
+ if (unlink(file) == 0) {
+ fprintf(stderr,
+ "unlink(%s) succeeded at call #%d.\n",
+ file, k + 2);
+ break;
+ }
+ }
+ }
+ }
+
+ (void)chdir("..");
+ if (rmdir(dir) == -1)
+ warn("rmdir(%s), %s:%d", dir, __FILE__, __LINE__);
+
+}
+
+void
+test(void)
+{
+ sleep(arc4random() % 3 + 1);
+
+ test2();
+ if (error == 0)
+ test2();
+
+ _exit(error);
+}
+
+int
+main(void)
+{
+ int e, i, status;
+
+ for (i = 0; i < PARALLEL; i++)
+ if (fork() == 0)
+ test();
+
+ e = 0;
+ for (i = 0; i < PARALLEL; i++) {
+ wait(&status);
+ e += WEXITSTATUS(status);
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/tmpfs11.sh b/tools/test/stress2/misc/tmpfs11.sh
new file mode 100755
index 000000000000..efb535b8ea94
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs11.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Test with two tmpfs(5) file systems mounted.
+
+. ../default.cfg
+
+mp1=$mntpoint
+mp2=${mntpoint}2
+[ -d $mp2 ] || mkdir -p $mp2
+md1=$mdstart
+md2=$((mdstart + 1))
+
+mount | grep $mp1 | grep -q tmpfs && umount -f $mp1
+mount -o size=1g -t tmpfs tmpfs $mp1
+chmod 777 $mp1
+
+mount | grep $mp2 | grep -q tmpfs && umount -f $mp2
+mount -o size=1g -t tmpfs tmpfs $mp2
+chmod 777 $mp2
+
+export runRUNTIME=15m
+export RUNDIR=$mp1/stressX
+export CTRLDIR=$mp1/stressX.control
+export LOAD=80
+export symlinkLOAD=80
+export rwLOAD=80
+export TESTPROGS="
+testcases/lockf2/lockf2
+testcases/symlink/symlink
+testcases/openat/openat
+testcases/rw/rw
+testcases/fts/fts
+testcases/link/link
+testcases/lockf/lockf
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/mkfifo/mkfifo
+"
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > /dev/null 2>&1 &
+
+export TESTPROGS="$TESTPROGS testcases/swap/swap"
+export RUNDIR=$mp2/stressX
+export CTRLDIR=$mp2/stressX.control
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > /dev/null 2>&1 &
+wait
+
+while mount | grep "$mp2 " | grep -q tmpfs; do
+ umount $mp2 || sleep 1
+done
+while mount | grep "$mp1 " | grep -q tmpfs; do
+ umount $mp1 || sleep 1
+done
diff --git a/tools/test/stress2/misc/tmpfs12.sh b/tools/test/stress2/misc/tmpfs12.sh
new file mode 100755
index 000000000000..eab0b768029a
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs12.sh
@@ -0,0 +1,95 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# umount -f test scenario
+
+# Problems seen:
+# panic: cache_enter: Doomed vnode used as src
+# http://people.freebsd.org/~pho/stress/log/kostik676.txt
+# panic: Bad link elm 0xfffff800b384e828 next->prev != elm
+# http://people.freebsd.org/~pho/stress/log/kostik677.txt
+# panic: unrhdr has 1 allocations
+# http://people.freebsd.org/~pho/stress/log/kostik678.txt
+# Fixed in r268605 - r268617 and r268766.
+
+[ $((`sysctl -n hw.usermem` / 1024 / 1024 / 1024)) -lt 6 ] && exit 0
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+N=3
+
+export runRUNTIME=15m
+export LOAD=80
+export symlinkLOAD=80
+export rwLOAD=80
+export TESTPROGS="
+testcases/lockf2/lockf2
+testcases/symlink/symlink
+testcases/openat/openat
+testcases/rw/rw
+testcases/fts/fts
+testcases/link/link
+testcases/lockf/lockf
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/mkfifo/mkfifo
+"
+
+for i in `jot $N 1`; do
+ eval mp$i=${mntpoint}$i
+done
+
+for i in `jot $N 1`; do
+ eval mp=\$mp$i
+ [ -d $mp ] || mkdir -p $mp
+ mount | grep $mp | grep -q tmpfs && umount -f $mp
+ mount -o size=2g -t tmpfs tmpfs $mp
+ chmod 777 $mp
+ export RUNDIR=$mp/stressX
+ export CTRLDIR=$mp/stressX.control
+ su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > \
+ /dev/null 2>&1 &
+done
+daemon sh -c '(cd ../testcases/swap; ./swap -t 5m -i 20 -h -l 100)' > \
+ /dev/null 2>&1
+
+sleep 60
+
+for i in `jot $N 1`; do
+ eval mp=\$mp$i
+ while mount | grep "$mp " | grep -q tmpfs; do
+ umount -f $mp || sleep 1
+ done
+done
+../tools/killall.sh
+while pgrep -q swap; do
+ pkill -9 swap
+done
+wait
diff --git a/tools/test/stress2/misc/tmpfs13.sh b/tools/test/stress2/misc/tmpfs13.sh
new file mode 100755
index 000000000000..29b44cbc9ad4
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs13.sh
@@ -0,0 +1,95 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# umount -f test scenario
+
+# "panic: deadlkres: possible deadlock detected..." seen.
+# http://people.freebsd.org/~pho/stress/log/tmpfs13.txt
+# Fixed by r269457.
+
+# "panic: vm_domainset_iter_first: Invalid domain 2" seen.
+# Fixed by r339075
+
+. ../default.cfg
+
+N=`sysctl -n hw.ncpu`
+usermem=`sysctl -n hw.usermem`
+[ `swapinfo | wc -l` -eq 1 ] && usermem=$((usermem/100*80))
+size=$((usermem / 1024 / 1024 / 2))
+
+export runRUNTIME=15m
+export RUNDIR=$mp1/stressX
+export CTRLDIR=$mp1/stressX.control
+export LOAD=80
+export symlinkLOAD=80
+export rwLOAD=80
+export TESTPROGS="
+testcases/lockf2/lockf2
+testcases/symlink/symlink
+testcases/openat/openat
+testcases/rw/rw
+testcases/fts/fts
+testcases/link/link
+testcases/lockf/lockf
+testcases/creat/creat
+testcases/mkdir/mkdir
+testcases/rename/rename
+testcases/mkfifo/mkfifo
+"
+
+for i in `jot $N 1`; do
+ eval mp$i=${mntpoint}$i
+done
+
+for i in `jot $N 1`; do
+ eval mp=\$mp$i
+ [ -d $mp ] || mkdir -p $mp
+ mount | grep $mp | grep -q tmpfs && umount -f $mp
+ mount -o size=${size}m -t tmpfs tmpfs $mp
+ chmod 777 $mp
+ export RUNDIR=$mp/stressX
+ export CTRLDIR=$mp/stressX.control
+ su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS' > \
+ /dev/null 2>&1 &
+done
+
+sleep 60
+
+for i in `jot $N 1`; do
+ eval mp=\$mp$i
+ while mount | grep "$mp " | grep -q tmpfs; do
+ umount -f $mp || sleep 1
+ done
+done
+../tools/killall.sh
+while pgrep -q swap; do
+ pkill -9 swap
+done
+wait
diff --git a/tools/test/stress2/misc/tmpfs14.sh b/tools/test/stress2/misc/tmpfs14.sh
new file mode 100755
index 000000000000..feb23b9952ae
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs14.sh
@@ -0,0 +1,112 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Verify that mmap access updates mtime and ctime on files located on a
+# tmpfs FS.
+# Fixed by r277828 and r277969.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > tmpfs14.c
+rm -f /tmp/tmpfs14
+mycc -o tmpfs14 -Wall -Wextra -g -O2 tmpfs14.c || exit 1
+rm -f tmpfs14.c
+cd $odir
+
+mount | grep -q "$mntpoint " && umount -f $mntpoint
+mount -t tmpfs tmpfs $mntpoint
+
+(cd $mntpoint; /tmp/tmpfs14) &
+
+sleep .5
+set `stat -f "%m %c" $mntpoint/test`
+m1=$1
+c1=$2
+while pgrep -q tmpfs14; do
+ set `stat -f "%m %c" $mntpoint/test`
+ [ "$m1" != "$1" ] && break
+ [ "$c1" != "$2" ] && break
+ sleep 1
+done
+[ "$m1" = "$1" -o "$c1" = "$2" ] &&
+ echo "FAIL Unchanged time $m1 $c1 / $1 $2"
+wait
+
+while mount | grep $mntpoint | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
+rm -f /tmp/tmpfs14
+exit 0
+EOF
+
+#include <sys/types.h>
+#include <sys/mman.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <time.h>
+
+char *file = "test";
+
+int
+main(void)
+{
+ char *p;
+ size_t len;
+ int fd, i;
+
+ if ((fd = open(file, O_RDWR | O_CREAT, 0644)) == -1)
+ err(1, "open(%s)", file);
+ if (write(fd, "abcdef", 7) < 0)
+ err(1, "write");
+ close(fd);
+
+ len = getpagesize();
+ for (i = 0; i < 10; i++) {
+ sleep(1);
+ if ((fd = open(file, O_RDWR | O_CREAT, 0644)) == -1)
+ err(1, "open(%s)", file);
+
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ p[0] = '0' + i;
+ munmap(p, len);
+ close(fd);
+ }
+
+ return 0;
+}
diff --git a/tools/test/stress2/misc/tmpfs15.sh b/tools/test/stress2/misc/tmpfs15.sh
new file mode 100755
index 000000000000..1eb470a7c3cb
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs15.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "panic: Assertion de->td_cookie == de->td_hash failed at
+# /usr/src/sys/modules/tmpfs/../../fs/tmpfs/tmpfs_subr.c:964" seen.
+# https://people.freebsd.org/~pho/stress/log/tmpfs15.txt
+# Fixed by 284594.
+
+# Test scenario by kib@
+
+. ../default.cfg
+
+mount -t tmpfs none $mntpoint
+mkdir $mntpoint/1 $mntpoint/2
+cd $mntpoint/1
+mkdir p5-Mojo-Pg-2.06
+mkdir advancemenu-2.7_2
+mv p5-Mojo-Pg-2.06 ../2
+
+cd /
+umount $mntpoint
diff --git a/tools/test/stress2/misc/tmpfs16.sh b/tools/test/stress2/misc/tmpfs16.sh
new file mode 100755
index 000000000000..8cc3c3596a4e
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs16.sh
@@ -0,0 +1,200 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# A fuzz test using random file descriptors with random seeks.
+# "panic: Assertion (cookie & TMPFS_DIRCOOKIE_MASK) == cookie failed":
+# https://people.freebsd.org/~pho/stress/log/kostik922.txt
+# Fixed by r303916.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+mount -o size=1g -t tmpfs tmpfs $mntpoint
+RUNDIR=$mntpoint/stressX
+dir=$RUNDIR
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > tmpfs16.c
+rm -f /tmp/tmpfs16
+mycc -o tmpfs16 -Wall -Wextra -O2 -g tmpfs16.c -lpthread || exit 1
+rm -f tmpfs16.c
+
+rm -rf $dir
+mkdir -p $dir
+chmod 777 $dir
+
+cd $dir
+jot 500 | xargs touch
+jot 500 | xargs chmod 666
+cd $odir
+
+(cd /tmp; /tmp/tmpfs16 $dir)
+e=$?
+
+rm -rf $dir
+umount $mntpoint
+
+rm -f /tmp/tmpfs16
+exit $e
+EOF
+#include <sys/param.h>
+#include <sys/resource.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <libutil.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define N (128 * 1024 / (int)sizeof(u_int32_t))
+#define RUNTIME 180
+#define THREADS 2
+
+static int fd[900];
+static u_int32_t r[N];
+static char *args[2];
+
+static unsigned long
+makearg(void)
+{
+ unsigned long val;
+
+ val = arc4random();
+#if defined(__LP64__)
+ val = (val << 31) | arc4random();
+ val = val & 0x00007fffffffffffUL;
+#endif
+
+ return(val);
+}
+
+static void *
+test(void *arg __unused)
+{
+ FTS *fts;
+ FTSENT *p;
+ int ftsoptions, i, n;
+
+ ftsoptions = FTS_PHYSICAL;
+
+ for (;;) {
+ for (i = 0; i < N; i++)
+ r[i] = arc4random();
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ i = n = 0;
+ while ((p = fts_read(fts)) != NULL) {
+ if (fd[i] > 0)
+ close(fd[i]);
+ if ((fd[i] = open(p->fts_path, O_RDWR)) == -1)
+ if ((fd[i] = open(p->fts_path, O_WRONLY)) == -1)
+ continue;
+ if (ftruncate(fd[i], 0) != 0)
+ err(1, "ftruncate");
+ i++;
+ i = i % nitems(fd);
+ }
+
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+ sleep(1);
+ }
+ return(0);
+}
+
+static void *
+calls(void *arg __unused)
+{
+ off_t offset;
+ time_t start;
+ int fd2;
+
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ fd2 = makearg() % nitems(fd) + 3;
+ offset = makearg();
+ if (lseek(fd2, offset - 1, SEEK_SET) != -1) {
+ if (write(fd2, "x", 1) != 1)
+ if (errno != EBADF && errno != ENOSPC)
+ warn("write");
+ } else
+ if (errno != EBADF)
+ warn("lseek");
+ if (fsync(fd2) == -1)
+ if (errno != EBADF)
+ warn("x");
+
+ }
+
+ return (0);
+}
+
+int
+main(int argc, char **argv)
+{
+ struct passwd *pw;
+ pthread_t rp, cp[THREADS];
+ int e, i;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <dir>\n", argv[0]);
+ exit(1);
+ }
+ args[0] = argv[1];
+ args[1] = 0;
+
+ if ((pw = getpwnam("nobody")) == NULL)
+ err(1, "failed to resolve nobody");
+ if (setgroups(1, &pw->pw_gid) ||
+ setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
+ seteuid(pw->pw_uid) || setuid(pw->pw_uid))
+ err(1, "Can't drop privileges to \"nobody\"");
+ endpwent();
+
+ if ((e = pthread_create(&rp, NULL, test, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ usleep(1000);
+ for (i = 0; i < THREADS; i++)
+ if ((e = pthread_create(&cp[i], NULL, calls, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ for (i = 0; i < THREADS; i++)
+ pthread_join(cp[i], NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/tmpfs17.sh b/tools/test/stress2/misc/tmpfs17.sh
new file mode 100755
index 000000000000..878fcf6c95d1
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs17.sh
@@ -0,0 +1,144 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# tmpfs(5) option nonc test scenario
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/tmpfs17.c
+mycc -o tmpfs17 -Wall -Wextra -O0 -g tmpfs17.c || exit 1
+rm -f tmpfs17.c
+cd $odir
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mount -o nonc -t tmpfs tmpfs $mntpoint || { rm /tmp/tmpfs17; exit 0; }
+
+cd $mntpoint
+/tmp/tmpfs17 &
+pid=$!
+sleep .2
+pid2=`pgrep tmpfs17`
+for i in `jot 5`; do
+ for p in $pid2; do
+ while true; do procstat -f $p > /dev/null 2>&1; done &
+ pids="$pids $!"
+ done
+done
+while kill -0 $pid 2>/dev/null; do
+ sleep 2
+done
+kill $pids 2>/dev/null
+wait
+cd $odir
+
+for i in `jot 10`; do
+ umount $mntpoint && break
+ sleep 1
+done
+s=0
+mount | grep -q "on $mntpoint " && { s=1; umount -f $mntpoint; }
+rm -rf /tmp/tmpfs17
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#define PARALLEL 4
+#define RUNTIME (5 * 60)
+#define SYNC 0
+
+static void
+test(void)
+{
+ pid_t pid;
+ time_t start;
+ int fd, i;
+ char file[80];
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ i= 0;
+ pid = getpid();
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ snprintf(file, sizeof(file), "file.%06d.%03d", pid, i++);
+ if ((fd = open(file, O_RDWR|O_CREAT, DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ close(fd);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ size_t len;
+ int e, i, pids[PARALLEL], status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ e += status == 0 ? 0 : 1;
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/tmpfs18.sh b/tools/test/stress2/misc/tmpfs18.sh
new file mode 100755
index 000000000000..c8519538b3bd
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs18.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# nfs loop back mount of a tmpfs file system.
+# "[tcp] 127.0.0.1:/mnt: Permission denied" seen, when tmpfs options are
+# used.
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+grep -q $mntpoint /etc/exports ||
+ { echo "$mntpoint missing from /etc/exports"; exit 0; }
+
+s=0
+m2=${mntpoint}2
+[ -d $m2 ] || mkdir $m2
+mount | grep "on $m2 " | grep -q nfs && umount $m2
+mount | grep "on $mntpoint " | grep -q tmpfs && umount -f $mntpoint
+
+[ $# -eq 1 ] &&
+ mount -t tmpfs tmpfs $mntpoint || s=1 # This works
+[ $# -eq 0 ] &&
+ mount -o size=100m -t tmpfs tmpfs $mntpoint || s=1
+mount -t nfs -o tcp -o rw,retrycnt=1 127.0.0.1:$mntpoint $m2 || s=2
+
+while mount | grep "on $m2 " | grep -q nfs; do
+ umount $m2
+done
+
+while mount | grep "on $mntpoint " | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
+exit $s
diff --git a/tools/test/stress2/misc/tmpfs19.sh b/tools/test/stress2/misc/tmpfs19.sh
new file mode 100755
index 000000000000..ab1e96835955
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs19.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Write a large file to a tmpfs FS
+# Test scenario suggestion by kib@
+
+# Thread stuck in vmwait seen:
+# https://people.freebsd.org/~pho/stress/log/jeff138.txt
+# Fixed by r355771
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+size=`sysctl -n hw.usermem`
+
+mount | grep "on $mntpoint " && umount -f $mntpoint
+mount -o size=$size -t tmpfs tmpfs $mntpoint
+
+size=$((size / 1024 / 1024))
+size=$((size - 5))
+echo "dd if=/dev/zero of=$mntpoint/file bs=1m count=$size"
+dd if=/dev/zero of=$mntpoint/file bs=1m count=$size status=none
+timeout 1m find `dirname $diskimage` -ls > /dev/null 2>&1
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FATAL; fstat -mf $mntpoint; exit 1; }
+done
+exit 0
diff --git a/tools/test/stress2/misc/tmpfs2.sh b/tools/test/stress2/misc/tmpfs2.sh
new file mode 100755
index 000000000000..80885c331e14
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs2.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# panic: vfs_mount_destroy: nonzero writeopcount, seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mounts=15 # Number of parallel scripts
+
+if [ $# -eq 0 ]; then
+ # start the parallel tests
+ for i in `jot $mounts`; do
+ [ -d ${mntpoint}$i ] || mkdir -p ${mntpoint}$i
+ ./$0 $i &
+ ./$0 find &
+ done
+ wait
+
+else
+ if [ $1 = find ]; then
+ for i in `jot 1024`; do
+ find ${mntpoint}* -type f > /dev/null 2>&1
+ done
+ else
+
+ # The test: Parallel mount and unmounts
+ for i in `jot 1024`; do
+ m=$1
+ opt=`[ $(( m % 2 )) -eq 0 ] && echo -f`
+ mount -t tmpfs tmpfs ${mntpoint}$m
+ cp -r /usr/include/machine/a* ${mntpoint}$m
+ while mount | grep -qw $mntpoint$m; do
+ umount $opt ${mntpoint}$m > /dev/null 2>&1
+ done
+ done
+ fi
+fi
diff --git a/tools/test/stress2/misc/tmpfs20.sh b/tools/test/stress2/misc/tmpfs20.sh
new file mode 100755
index 000000000000..763d70a49ae8
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs20.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+# Run stress2 from a tmpfs file system.
+# Hunt for Assertion error_ == 0 failed at /usr/src/sys/vm/vm_map.c:553
+
+# "vn_vget_ino_get: 0xfffff80de2e269e0 is not locked but should be" seen:
+# https://people.freebsd.org/~pho/stress/log/kostik1219.txt
+# Fixed by r351542
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+mount -o size=2g -t tmpfs null $mntpoint || exit 1
+chmod 777 $mntpoint
+
+cp -r ../../stress2 $mntpoint
+
+export LOAD=80
+export MAXSWAPPCT=80
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=10m
+export runRUNTIME=10m
+export rwLOAD=80
+export TESTPROGS=`cd ..; find testcases/ -perm -1 -type f | \
+ egrep -v "/run/"`
+
+here=`pwd`
+cd $mntpoint/stress2/misc || exit 1
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
+cd $here
+
+umount $mntpoint
+exit 0
diff --git a/tools/test/stress2/misc/tmpfs21.sh b/tools/test/stress2/misc/tmpfs21.sh
new file mode 100755
index 000000000000..295ba0bd20c0
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs21.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Peter Holm
+#
+# 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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+# umount a tmpfs fs while a rename of a directory is in progress
+# Test scenario suggestion by: kib
+
+# umount(8) stuck in suspwt seen.
+# https://people.freebsd.org/~pho/stress/log/tmpfs21.txt
+# Fixed by r355061
+
+(cd ../testcases/swap; ./swap -t 10m -i 20) > /dev/null &
+RUNDIR=$mntpoint/stressX
+start=`date +%s`
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+while [ $((`date +%s` - start)) -lt 60 ]; do
+ mount -t tmpfs tmpfs $mntpoint || exit 1
+ (cd ../testcases/dirnprename; ./dirnprename -t 2m -i 20 > \
+ /dev/null 2>&1 ) &
+ pid=$!
+ sleep .`jot -r 1 1 9`
+ for i in `jot 6`; do
+ umount $mntpoint && break
+ sleep .5
+ done 2>&1 | grep -v "Device busy"
+ mount | grep -q "on $mntpoint " && umount -f $mntpoint
+ wait $pid
+done
+while pkill swap; do sleep .2; done
+wait
+exit 0
diff --git a/tools/test/stress2/misc/tmpfs22.sh b/tools/test/stress2/misc/tmpfs22.sh
new file mode 100755
index 000000000000..36d2cc59dabd
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs22.sh
@@ -0,0 +1,117 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+# A tmpfs "nomtime" mount option test.
+
+# kib@ wrote:
+# A test program should do something along the lines:
+# 1. open tmpfs file, ftruncate it, mmap
+# 2. write to the mmaped file area periodically, for long time
+# e.g. 1 write in 10 secs, for 120 secs
+# 3. the mtime of the file should be the same as after the truncation
+# if the nomtime flag is set. Otherwise, it should be around +-30
+# secs of the last write to the area.
+#
+# Really the current (unpatched) situation is some not well defined
+# mix between nomtime and its absence.
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > tmpfs22.c
+mycc -o tmpfs22 -Wall -Wextra -O2 -g tmpfs22.c || exit 1
+rm -f tmpfs22.c
+cd $odir
+
+mount | grep "$mntpoint" | grep -q tmpfs && umount $mntpoint
+mount -o size=2g,nomtime -t tmpfs tmpfs $mntpoint || exit 1
+
+(cd $mntpoint; /tmp/tmpfs22); s=$?
+
+for i in `jot 6`; do
+ umount $mntpoint && break
+ sleep 2
+done
+rm -f /tmp/tmpfs22
+exit $s
+
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+static char *file = "test";
+
+int
+main(void)
+{
+ struct stat st1, st2;
+ size_t len;
+ int fd, i, s;
+ char *p;
+
+ if ((fd = open(file, O_RDWR | O_CREAT, 0644)) == -1)
+ err(1, "open(%s)", file);
+ len = 2LL * 1024 * 1024;
+ if (ftruncate(fd, len) == -1)
+ err(1, "ftruncate");
+ if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
+ MAP_FAILED) {
+ if (errno == ENOMEM)
+ return (1);
+ err(1, "mmap()");
+ }
+ if (fstat(fd, &st1) == -1)
+ err(1, "fstat 1");
+
+ for (i = 0; i < 12; i++) {
+ sleep(10);
+ p[arc4random() % len] = 1;
+ }
+ if (fstat(fd, &st2) == -1)
+ err(1, "fstat 2");
+ s = 0;
+ if (st1.st_mtime != st2.st_mtime) {
+ fprintf(stderr, "mtime differs: %ld %ld\n",
+ (long)st1.st_mtime, (long)st2.st_mtime);
+ s=1;
+ }
+ munmap(p, len);
+ close(fd);
+
+ return (s);
+}
diff --git a/tools/test/stress2/misc/tmpfs23.sh b/tools/test/stress2/misc/tmpfs23.sh
new file mode 100755
index 000000000000..2f956d512dda
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs23.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# General protection fault seen:
+# https://people.freebsd.org/~pho/stress/log/log0020.txt
+
+# Test scenario suggestion by kib@
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+mount | grep -q "on $mntpoint " && umount $mntpoint
+mount -o size=1g -t tmpfs tmpfs $mntpoint || exit 1
+
+echo 123 >/mnt/1
+tail -f /mnt/1 > /dev/null 2>&1 &
+sleep .5
+rm /mnt/1
+sleep .`jot -r 1 1-9`
+umount -f $mntpoint
+for i in `jot 10`; do
+ kill $! > /dev/null 2>&1 || break
+ sleep 1
+done
+wait
+exit 0
diff --git a/tools/test/stress2/misc/tmpfs3.sh b/tools/test/stress2/misc/tmpfs3.sh
new file mode 100755
index 000000000000..ef1f5c69c5d3
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs3.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009-2011 Peter Holm <pho@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 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.
+#
+
+# panic: tmpfs_alloc_vp: type 0xc866ce58 0, seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep "$mntpoint" | grep -q tmpfs && umount $mntpoint
+mount -t tmpfs tmpfs $mntpoint
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=10m # Run tests for 10 minutes
+
+(cd ..; ./run.sh vfs.cfg)
+
+umount $mntpoint
+mount | grep "$mntpoint" | grep -q tmpfs && umount -f $mntpoint
+exit 0
diff --git a/tools/test/stress2/misc/tmpfs4.sh b/tools/test/stress2/misc/tmpfs4.sh
new file mode 100755
index 000000000000..6160f219c9bd
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs4.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Regression test from kern/122038 by gprspb mail ru
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep "$mntpoint" | grep -q tmpfs && umount $mntpoint
+mount -t tmpfs tmpfs $mntpoint
+
+for i in `jot 100`; do
+ mkdir -p $mntpoint/1/2
+ cd $mntpoint/1/2
+ rm -rf $mntpoint/1
+ cd .. 2>&1 | grep -v "cd: \.\.: No such file or directory"
+done
+
+umount $mntpoint 2>&1 | grep -v busy
+mount | grep "$mntpoint" | grep -q tmpfs && umount -f $mntpoint
diff --git a/tools/test/stress2/misc/tmpfs5.sh b/tools/test/stress2/misc/tmpfs5.sh
new file mode 100755
index 000000000000..1dd8bbe9a6eb
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs5.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# tmpfs + fsx test scenario.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/umountf2.sh > /tmp/fsx.c
+mycc -o /tmp/fsx -O2 /tmp/fsx.c || exit 1
+rm -f fsx.c
+
+mount | grep "$mntpoint" | grep -q tmpfs && umount $mntpoint
+mount -t tmpfs tmpfs $mntpoint
+
+(
+ /tmp/fsx -q -N 100000 $mntpoint/file.1 &
+ /tmp/fsx -q -N 100000 $mntpoint/file.2 &
+) | grep -v A-OK
+wait;wait
+
+while mount | grep "$mntpoint" | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
+rm -f /tmp/fsx
diff --git a/tools/test/stress2/misc/tmpfs6.sh b/tools/test/stress2/misc/tmpfs6.sh
new file mode 100755
index 000000000000..2e9dc0e11473
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs6.sh
@@ -0,0 +1,110 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# panic: deadlkres: possible deadlock detected for 0xc8576a00, blocked for 1801792 ticks
+
+# Scenario by kib@
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > tmpfs6.c
+mycc -o tmpfs6 -Wall -Wextra -O2 tmpfs6.c || exit 1
+rm -f tmpfs6.c
+
+mount | grep $mntpoint | grep -q tmpfs && umount $mntpoint
+mount -t tmpfs tmpfs $mntpoint
+
+(cd $mntpoint; /tmp/tmpfs6)
+rm -f /tmp/tmpfs6
+
+while mount | grep $mntpoint | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
+exit
+EOF
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <err.h>
+
+int pagesize;
+
+#define FILESIZE (32 * 1024)
+char wbuffer[FILESIZE];
+
+void
+test(void)
+{
+ int fd;
+ int len;
+ void *addr;
+ char filename[80];
+
+ snprintf(filename, sizeof(filename), "file.%07d", getpid());
+ if ((fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU)) == -1)
+ err(1, "open(%s)", filename);
+
+ if ((len = write(fd, wbuffer, FILESIZE)) != FILESIZE)
+ err(1, "write()");
+
+ fsync(fd);
+
+ if ((addr = mmap(NULL, FILESIZE, PROT_READ | PROT_WRITE , MAP_SHARED, fd, 0)) == MAP_FAILED)
+ err(1, "mmap()");
+
+ if (lseek(fd, 0, SEEK_SET) != 0)
+ err(1, "lseek()");
+
+ if ((len = write(fd, addr, FILESIZE)) != FILESIZE)
+ err(1, "write() 2");
+
+ if (munmap(addr, FILESIZE) == -1)
+ err(1, "munmap()");
+ close(fd);
+ unlink(filename);
+
+}
+
+int
+main(void)
+{
+ int i;
+
+ for (i = 0; i < 10000; i++)
+ test();
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/tmpfs7.sh b/tools/test/stress2/misc/tmpfs7.sh
new file mode 100755
index 000000000000..07398e548bb3
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs7.sh
@@ -0,0 +1,249 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# Test scenario by Nate Eldredge neldredge math ucsdnedu
+
+# kern/127213: [tmpfs] sendfile on tmpfs data corruption
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+set -e
+
+odir=`pwd`
+cd /tmp
+cat > server.c <<EOF
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include "util.h"
+
+int main(int argc, char *argv[]) {
+ int f, listener, connection;
+ if (argc < 3) {
+ fprintf(stderr, "Usage: %s filename socketname\n", argv[0]);
+ exit(1);
+ }
+ if ((f = open(argv[1], O_RDONLY)) < 0) {
+ perror(argv[1]);
+ exit(1);
+ }
+ if ((listener = listen_unix_socket(argv[2])) < 0) {
+ exit(1);
+ }
+ if ((connection = accept_unix_socket(listener)) >= 0) {
+ real_sendfile(f, connection);
+ }
+ return 0;
+}
+EOF
+cat > client.c <<EOF
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include "util.h"
+
+int main(int argc, char *argv[]) {
+ int s;
+ if (argc < 2) {
+ fprintf(stderr, "Usage: %s socketname\n", argv[0]);
+ exit(1);
+ }
+ if ((s = connect_unix_socket(argv[1])) < 0) {
+ exit(1);
+ }
+ fake_sendfile(s, 1);
+ return 0;
+}
+EOF
+
+cat > util.c <<EOF
+/* send data from file to unix domain socket */
+
+#include <stdio.h>
+#include <time.h>
+#include <signal.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int create_unix_socket(void) {
+ int fd;
+ if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
+ perror("socket");
+ return -1;
+ }
+ return fd;
+}
+
+int make_unix_sockaddr(const char *pathname, struct sockaddr_un *sa) {
+ memset(sa, 0, sizeof(*sa));
+ sa->sun_family = PF_LOCAL;
+ if (strlen(pathname) + 1 > sizeof(sa->sun_path)) {
+// fprintf(stderr, "%s: pathname too long (max %lu)\n",
+// pathname, sizeof(sa->sun_path));
+ errno = ENAMETOOLONG;
+ return -1;
+ }
+ strcpy(sa->sun_path, pathname);
+ return 0;
+}
+
+static char *sockname;
+void delete_socket(void) {
+ unlink(sockname);
+}
+
+int listen_unix_socket(const char *path) {
+ int fd;
+ struct sockaddr_un sa;
+ if (make_unix_sockaddr(path, &sa) < 0)
+ return -1;
+ if ((fd = create_unix_socket()) < 0)
+ return -1;
+ if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
+ perror("bind");
+ close(fd);
+ return -1;
+ }
+ sockname = strdup(path);
+ atexit(delete_socket);
+
+ if (listen(fd, 5) < 0) {
+ perror("listen");
+ close(fd);
+ return -1;
+ }
+ return fd;
+}
+
+int accept_unix_socket(int fd) {
+ int s;
+ if ((s = accept(fd, NULL, 0)) < 0) {
+ perror("accept");
+ return -1;
+ }
+ return s;
+}
+
+int connect_unix_socket(const char *path) {
+ int fd;
+ struct sockaddr_un sa;
+ if (make_unix_sockaddr(path, &sa) < 0)
+ return -1;
+ if ((fd = create_unix_socket()) < 0)
+ return -1;
+ if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
+ perror("connect");
+ return -1;
+ }
+ return fd;
+}
+
+#define BUFSIZE 65536
+
+int fake_sendfile(int from, int to) {
+ char buf[BUFSIZE];
+ int v;
+ int sent = 0;
+ while ((v = read(from, buf, BUFSIZE)) > 0) {
+ int d = 0;
+ while (d < v) {
+ int w = write(to, buf, v - d);
+ if (w <= 0) {
+ perror("write");
+ return -1;
+ }
+ d += w;
+ sent += w;
+ }
+ }
+ if (v != 0) {
+ perror("read");
+ return -1;
+ }
+ return sent;
+}
+
+int real_sendfile(int from, int to) {
+ int v;
+ v = sendfile(from, to, 0, 0, NULL, NULL, 0);
+ if (v < 0) {
+ perror("sendfile");
+ }
+ return v;
+}
+EOF
+
+cat > util.h <<EOF
+/* send data from file to unix domain socket */
+
+#include <stdio.h>
+#include <time.h>
+#include <signal.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+int create_unix_socket(void);
+int make_unix_sockaddr(const char *pathname, struct sockaddr_un *sa);
+int listen_unix_socket(const char *path);
+int accept_unix_socket(int fd);
+int connect_unix_socket(const char *path);
+int fake_sendfile(int from, int to);
+int real_sendfile(int from, int to);
+EOF
+
+mycc -c -Wall -Wextra -O2 util.c
+mycc -o server -Wall -Wextra -O2 server.c util.o
+mycc -o client -Wall -Wextra -O2 client.c util.o
+rm -f server.c client.c util.c util.o util.h mysocket
+
+mount | grep "$mntpoint" | grep -q tmpfs && umount $mntpoint
+mount -t tmpfs tmpfs $mntpoint
+
+dd if=/dev/random of=$mntpoint/data bs=123456 count=1 status=none
+./server $mntpoint/data mysocket &
+sleep 0.2
+./client mysocket > data.$$
+cmp $mntpoint/data data.$$ ||
+ { echo "FAIL Data mismatch"; ls -l $mntpoint/data data.$$; }
+rm -f data.$$ server client mysocket
+
+wait
+while mount | grep "$mntpoint" | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
diff --git a/tools/test/stress2/misc/tmpfs8.sh b/tools/test/stress2/misc/tmpfs8.sh
new file mode 100755
index 000000000000..b17a329f69e7
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs8.sh
@@ -0,0 +1,178 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Demonstrate rename(2) cache problem for tmpfs(5). Fixed in r226987.
+# Variation of rename6.sh
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > tmpfs8.c
+mycc -o tmpfs8 -Wall -Wextra -O2 tmpfs8.c
+rm -f tmpfs8.c
+cd $here
+
+mount | grep $mntpoint | grep -q tmpfs && umount -f $mntpoint
+mount -t tmpfs tmpfs $mntpoint
+chmod 777 $mntpoint
+
+su $testuser -c "cd $mntpoint; /tmp/tmpfs8"
+
+while mount | grep $mntpoint | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
+rm -f /tmp/tmpfs8
+exit
+EOF
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+pid_t spid;
+const char *logfile = "test.log";
+char new[128];
+
+void
+cleanup()
+{
+ kill(spid, SIGINT);
+}
+
+static void
+Stat()
+{
+ struct stat sb;
+ int i;
+
+ setproctitle("Stat");
+ for (;;) {
+ for (i = 0; i < 1000; i++) {
+ stat(logfile, &sb);
+ stat(new, &sb);
+ }
+// usleep(1000);
+ usleep(100);
+ }
+}
+
+int
+main(void)
+{
+ struct stat sb1, sb2, sb3;
+ int fd, i, r1, r2, r3;
+
+ if ((spid = fork()) == 0)
+ Stat();
+
+ setproctitle("main");
+ atexit(cleanup);
+ for (i = 0; i < 200000; i++) {
+ bzero(&sb1, sizeof(sb1));
+ bzero(&sb2, sizeof(sb2));
+ if ((fd = open(logfile, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1)
+ err(1, "creat(%s)", logfile);
+ close(fd);
+
+ sprintf(new, "test.log.%05d", i);
+ if ((fd = open(new, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1)
+ err(1, "creat(%s)", new);
+ write(fd, "xxx", 3);
+ close(fd);
+ if ((r3 = stat(new, &sb3)) == -1)
+ err(1, "stat(%s)", new);
+#if 1
+ if (rename(logfile, new) == -1)
+ warn("rename(%s, %s)", logfile, new);
+#else
+ /* No cache problem is seen */
+ if (link(logfile, new) == -1)
+ err(1, "link(%s, %s)", logfile, new);
+ if (unlink(logfile) == -1)
+ err(1, "unlink(%s)", logfile);
+#endif
+ /*
+ * stat() for logfile and new will be identical sometimes,
+ * but only when Stat() is running.
+ */
+ r1 = stat(logfile, &sb1);
+ r2 = stat(new, &sb2);
+ if (r1 == 0 && r2 == 0 &&
+ bcmp(&sb1, &sb2, sizeof(sb1)) == 0) {
+ fprintf(stderr, "FAIL 1\n");
+ fprintf(stderr, "%-15s: ino = %4ju, nlink = %ju, "
+ "size = %jd\n", logfile, (uintmax_t)sb1.st_ino,
+ (uintmax_t)sb1.st_nlink, sb1.st_blocks);
+ fprintf(stderr, "%-15s: ino = %4ju, nlink = %ju, "
+ "size = %jd\n", new , (uintmax_t)sb2.st_ino,
+ (uintmax_t)sb2.st_nlink, sb2.st_blocks);
+ }
+ if (bcmp(&sb2, &sb3, sizeof(sb2)) == 0) {
+ fprintf(stderr, "Old to file is lingering\n");
+ }
+ if (sb2.st_ino == sb3.st_ino) {
+ fprintf(stderr, "FAIL 2\n");
+ if (r1 == 0)
+ fprintf(stderr,
+ "sb1: %-15s: ino = %4ju, nlink = %ju, "
+ "size = %jd\n", logfile,
+ (uintmax_t)sb1.st_ino,
+ (uintmax_t)sb1.st_nlink, sb1.st_blocks);
+ if (r2 == 0)
+ fprintf(stderr,
+ "sb2: %-15s: ino = %4ju, nlink = %ju, "
+ "size = %jd\n", new, (uintmax_t)sb2.st_ino,
+ (uintmax_t)sb2.st_nlink, sb2.st_blocks);
+ if (r3 == 0)
+ fprintf(stderr,
+ "sb3: %-15s: ino = %4ju, nlink = %ju, "
+ "size = %jd\n", new, (uintmax_t)sb3.st_ino,
+ (uintmax_t)sb3.st_nlink, sb3.st_blocks);
+ exit(1);
+ }
+ unlink(new);
+ }
+
+ kill(spid, SIGINT);
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/tmpfs9.sh b/tools/test/stress2/misc/tmpfs9.sh
new file mode 100755
index 000000000000..cdf04b3f7d1d
--- /dev/null
+++ b/tools/test/stress2/misc/tmpfs9.sh
@@ -0,0 +1,110 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Regression test: exec returning EIO problem scenario for tmpfs
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > tmpfs9.c
+mycc -o tmpfs9 -Wall -Wextra -O2 tmpfs9.c
+rm -f tmpfs9.c
+cd $here
+
+mount | grep $mntpoint | grep -q tmpfs && umount -f $mntpoint
+mount -t tmpfs tmpfs $mntpoint
+chmod 777 $mntpoint
+
+cp /usr/bin/true $mntpoint
+su $testuser -c "/tmp/tmpfs9 $mntpoint" &
+
+while kill -0 $! 2>/dev/null; do
+ ../testcases/swap/swap -t 2m -i 40 -h
+done
+wait
+
+while mount | grep $mntpoint | grep -q tmpfs; do
+ umount $mntpoint || sleep 1
+done
+rm -f /tmp/tmpfs9
+exit
+EOF
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define N 5000
+#define PARALLEL 10
+const char path[] = "./true";
+
+void test(void)
+{
+ pid_t p;
+ int i;
+
+ for (i = 0; i < N; i++) {
+ if ((p = fork()) == 0)
+ if (execl(path, path, (char *)0) == -1)
+ err(1, "exec(%s)", path);
+ if (p > 0)
+ wait(NULL);
+ }
+ _exit(0);
+}
+
+int
+main(int argc, char **argv)
+{
+ int i, status;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <path>", argv[0]);
+ if (chdir(argv[1]) == -1)
+ err(1, "chdir(%s)", argv[1]);
+
+ for (i = 0; i < PARALLEL; i++) {
+ if (fork() == 0)
+ test();
+ }
+
+ for (i = 0; i < PARALLEL; i++) {
+ wait(&status);
+ if (status != 0)
+ return (1);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/trim.sh b/tools/test/stress2/misc/trim.sh
new file mode 100755
index 000000000000..2dee27ec96ec
--- /dev/null
+++ b/tools/test/stress2/misc/trim.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Run with marcus.cfg on a malloc backed MD with option trim.
+# Various panics seen with WiP kernel code. For example:
+# panic: Bad tailq head 0xfffff80160cb7170 first->prev != head
+
+. ../default.cfg
+
+malloc_wait=`sysctl vm.md_malloc_wait | awk {'print $NF}'`
+[ $malloc_wait != 1 ] && sysctl vm.md_malloc_wait=1
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+size="128m"
+[ `uname -m` = "amd64" ] && size="1g"
+[ $# -eq 0 ] && trim=-t
+s=0
+opt=""
+[ "$newfs_flags" = "-U" ] && opt="-U -j"
+export MAXSWAPPCT=80
+for flag in ' ' $opt; do
+ echo "mdconfig -a -t malloc -o reserve -s $size -u $mdstart"
+ mdconfig -a -t malloc -o reserve -s $size -u $mdstart || exit 1
+ bsdlabel -w md$mdstart auto
+
+ echo "newfs $trim $flag md${mdstart}$part"
+ newfs $trim $flag md${mdstart}$part > /dev/null
+ mount /dev/md${mdstart}$part $mntpoint || exit 1
+ chmod 777 $mntpoint
+
+ export runRUNTIME=5m
+ export RUNDIR=$mntpoint/stressX
+
+ su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1
+
+ for i in `jot 6`; do
+ umount $mntpoint && break || sleep 10
+ done
+ [ $i -eq 6 ] && { s=1; break; }
+ checkfs /dev/md${mdstart}$part || s=1
+ mdconfig -d -u $mdstart
+done
+[ $malloc_wait != 1 ] && sysctl vm.md_malloc_wait=$malloc_wait
+exit $s
diff --git a/tools/test/stress2/misc/trim2.sh b/tools/test/stress2/misc/trim2.sh
new file mode 100755
index 000000000000..f1f71cc9087a
--- /dev/null
+++ b/tools/test/stress2/misc/trim2.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Run a test on a 128 MB malloc backed MD with UFS SU fs with option trim.
+# Verify that deleted records are gone.
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t malloc -o reserve -s 128m -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+[ $# -eq 0 ] && trim=-t
+newfs $trim $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+echo "This is a Trim (TRIM) test." >> $mntpoint/file
+for i in `jot 20`; do
+ cat $mntpoint/file $mntpoint/file > $mntpoint/file2
+ mv $mntpoint/file2 $mntpoint/file
+done
+rm $mntpoint/file
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+
+grep -a -qm1 Trim /dev/md${mdstart}$part && { echo "Test failed"; s=1; }
+
+checkfs /dev/md${mdstart}$part; s=$?
+mdconfig -d -u $mdstart
+exit $s
diff --git a/tools/test/stress2/misc/trim3.sh b/tools/test/stress2/misc/trim3.sh
new file mode 100755
index 000000000000..125c763f3546
--- /dev/null
+++ b/tools/test/stress2/misc/trim3.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Run with marcus.cfg on a 1 GB swap backed MD with option trim.
+# A swap backed MD has caused a "vmwait" hang in "CAM taskq".
+# A variation of trim.sh
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+size="1g"
+[ $# -eq 0 ] && trim=-t
+n=0
+opt=""
+s=0
+[ "$newfs_flags" = "-U" ] && opt="-U -j"
+for flag in ' ' $opt; do
+ echo "mdconfig -a -t swap -s $size -u $mdstart"
+ mdconfig -a -t swap -s $size -u $mdstart || exit 1
+ bsdlabel -w md$mdstart auto
+
+ echo "newfs $trim $flag md${mdstart}$part"
+ newfs $trim $flag md${mdstart}$part > /dev/null
+
+ mount /dev/md${mdstart}$part $mntpoint
+ chmod 777 $mntpoint
+
+ export runRUNTIME=7m
+ export RUNDIR=$mntpoint/stressX
+
+ su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1
+
+ for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+ [ $i -eq 6 ] &&
+ { echo FAIL; fstat -mf $mntpoint; exit 1; }
+ done
+ checkfs /dev/md${mdstart}$part || s=$?
+ mdconfig -d -u $mdstart
+done
+exit $s
diff --git a/tools/test/stress2/misc/trim4.sh b/tools/test/stress2/misc/trim4.sh
new file mode 100755
index 000000000000..fc23c53b09af
--- /dev/null
+++ b/tools/test/stress2/misc/trim4.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Run with marcus.cfg on a 1 GB swap backed MD with option trim.
+# SU+J FS corruption seen with option trim.
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+size="1g"
+[ $# -eq 0 ] && trim=-t
+[ "$newfs_flags" = "-U" ] && flag="-j"
+s=0
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt $((15 * 60)) ]; do
+ echo "Test `date '+%T'`"
+ echo "mdconfig -a -t swap -s $size -u $mdstart"
+ mdconfig -a -t swap -s $size -u $mdstart || exit 1
+ bsdlabel -w md$mdstart auto
+
+ echo "newfs $trim $flag md${mdstart}$part"
+ newfs $trim $flag md${mdstart}$part > /dev/null
+
+ mount /dev/md${mdstart}$part $mntpoint
+ chmod 777 $mntpoint
+
+ export runRUNTIME=5m
+ export RUNDIR=$mntpoint/stressX
+
+ su $testuser -c 'cd ..; ./run.sh marcus.cfg' > /dev/null 2>&1
+
+ while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ done
+# Do not break in case of fsck error
+ checkfs /dev/md${mdstart}$part || s=$?
+ mdconfig -d -u $mdstart
+done
+exit $s
diff --git a/tools/test/stress2/misc/trim5.sh b/tools/test/stress2/misc/trim5.sh
new file mode 100755
index 000000000000..a03efbd33e54
--- /dev/null
+++ b/tools/test/stress2/misc/trim5.sh
@@ -0,0 +1,49 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Panic with TRIM and ufs snapshots
+# Scenario by Yamagi Burmeister <lists yamagi org>
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep "$mntpoint " | grep -q md$mdstart && umount $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t malloc -s 128m -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags -t md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+mksnap_ffs $mntpoint $mntpoint/.snap/snap
+
+while mount | grep -q $mntpoint; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/trim6.sh b/tools/test/stress2/misc/trim6.sh
new file mode 100755
index 000000000000..d950bced1be2
--- /dev/null
+++ b/tools/test/stress2/misc/trim6.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Test scenario for exhausting limited malloc KVA on 32bit machine, by
+# extending a file on a UFS SU volume.
+
+# Create a large file on a file system with trim enabled.
+# Watchdog timeout seen:
+# https://people.freebsd.org/~pho/stress/log/trim6.txt
+# Fixed by r287361.
+
+# "panic: negative mnt_ref" seen:
+# https://people.freebsd.org/~pho/stress/log/kostik835.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+r=`mount | grep -w soft-updates | awk '{print $1}' | while read dev; do
+ dumpfs $dev | head -20 | grep -qw trim || continue
+ df -k $dev
+done | sort -rn +3 | head -1 | awk '{print $4, $6}'`
+[ -z "$r" ] && exit # No trim enabled file systems found
+set $r
+free=$(($1 / 1024 / 10 * 9)) # in Mb
+max=$((128 * 1024)) # Max is 128 GB
+[ $free -gt $max ] && free=$max
+mp=$2
+image=`echo $mp/diskimage | sed s#//#/#` # fix "//" for case mp = "/"
+trap "rm -f $image" EXIT INT
+
+echo "dd if=/dev/zero of=$image bs=1m count=$free"
+dd if=/dev/zero of=$image bs=1m count=$free status=none
+exit 0
diff --git a/tools/test/stress2/misc/trim7.sh b/tools/test/stress2/misc/trim7.sh
new file mode 100755
index 000000000000..6095dca543b4
--- /dev/null
+++ b/tools/test/stress2/misc/trim7.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# "panic: Lock Per-Filesystem Softdep Lock not exclusively locked @
+# ../../../ufs/ffs/ffs_softdep.c:1950" seen.
+# https://people.freebsd.org/~pho/stress/log/trim7.txt
+# Fixed by: r297206.
+
+# Test scenario by: Nick Evans <nevans@talkpoint.com>
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+mount | grep -q /media ||
+ echo "Prerequisite: /media is a TRIM enabled file system."
+
+r=`mount | grep -w soft-updates | awk '{print $1}' | while read dev; do
+ dumpfs $dev | grep -m1 flags | grep -qw trim || continue
+ df -k $dev
+done | sort -rn +3 | head -1 | awk '{print $4, $6}'`
+[ -z "$r" ] && exit # No trim enabled file systems found
+set $r
+free=$(($1 / 1024 / 10 * 9)) # in Mb
+max=$((128 * 1024)) # Max is 128 GB
+[ $free -gt $max ] && free=$max
+mp=$2
+[ $mp = /media ] || exit 0
+fs=`mount | grep "on $mp " | sed 's/ .*//'`
+echo "Using $fs"
+image=`echo $mp/diskimage | sed s#//#/#` # fix "//" for case mp = "/"
+
+echo "dd if=/dev/zero of=$image bs=1m count=$free"
+dd if=/dev/zero of=$image bs=1m count=$free status=none
+rm $image
+while mount | grep -q "on $mp "; do
+ umount $mp
+done
+mount $fs $mp
diff --git a/tools/test/stress2/misc/trim8.sh b/tools/test/stress2/misc/trim8.sh
new file mode 100755
index 000000000000..f31a10b5be25
--- /dev/null
+++ b/tools/test/stress2/misc/trim8.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Demonstrate disk queue completely full of TRIM commands.
+# "14 seconds elapsed" seen on r337569.
+# Fixed by rxxxxxx.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+trim=$(df -k | sed 1d | sort -rn +3 | awk '{print $NF}' | while read mp; do
+ mount | grep "on $mp " | grep -q ufs || continue
+ on=`mount | grep "on $mp " | awk '{print $1}'`
+ dumpfs $on | head -20 | grep -q trim || continue
+ echo $on && break
+done)
+[ -z "$trim" ] && exit 0
+mp=`mount | grep "$trim " | awk '{print $3}'`
+echo "Found TRIM on $trim mounted as $mp"
+free=`df -k $mp | tail -1 | awk '{print $4}'`
+free=$((free / 1024 / 1024)) # GB
+[ $free -lt 2 ] && exit 0
+expect=-1
+before=`mount -v | grep "$mp "`
+for i in `jot 40`; do
+ t=`date +%s`
+ dd if=/dev/zero of=$mp/trim.data.$i bs=1m count=1k status=none
+ rm $mp/trim.data.$i
+ t=$((`date +%s` - t))
+ [ $expect -eq -1 ] && expect=$t
+ [ $t -le $((4 * expect)) ] && continue
+ after=`mount -v | grep "$mp "`
+ echo "$t seconds elapsed"
+ echo "Before: $before"
+ echo "After: $after"
+ exit 1
+done
+exit 0
diff --git a/tools/test/stress2/misc/truncate.sh b/tools/test/stress2/misc/truncate.sh
new file mode 100755
index 000000000000..3088f6af7aaa
--- /dev/null
+++ b/tools/test/stress2/misc/truncate.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Test high water mark for freeblks
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > truncate.c
+mycc -o truncate -Wall -Wextra -O2 truncate.c || exit 1
+rm -f truncate.c
+[ -d $RUNDIR ] || mkdir -p $RUNDIR
+cd $RUNDIR
+
+/tmp/truncate
+
+rm -f /tmp/truncate
+exit 0
+EOF
+#include <fcntl.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#define SIZ 512
+char buf[SIZ];
+
+void
+test(void)
+{
+ time_t start;
+ int fd[10], i, j;
+ char name[128];
+
+ for (j = 0; j < 10; j++) {
+ sprintf(name, "%05d.%05d", getpid(), j);
+ if ((fd[j] = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666)) == -1)
+ err(1, "%s", name);
+ unlink(name);
+ }
+
+ start = time(NULL);
+ for (i = 0; i < 100000; i++) {
+ if (time(NULL) - start > 1200)
+ break;
+ for (j = 0; j < 10; j++) {
+ if (write(fd[j], buf, 2) != 2)
+ err(1, "write");
+ if (ftruncate(fd[j], 0) == -1)
+ err(1, "ftruncate");
+ }
+ }
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ int i, status;
+
+ for (i = 0; i < 20; i++) {
+ if (fork() == 0)
+ test();
+ }
+ for (i = 0; i < 20; i++)
+ wait(&status);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/truncate2.sh b/tools/test/stress2/misc/truncate2.sh
new file mode 100755
index 000000000000..b02908d61aed
--- /dev/null
+++ b/tools/test/stress2/misc/truncate2.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > truncate2.c
+mycc -o truncate2 -Wall -O2 truncate2.c
+rm -f truncate2.c
+[ -d $RUNDIR ] || mkdir -p $RUNDIR
+cd $RUNDIR
+
+/tmp/truncate2 &
+
+sleep 1
+while ps -x | grep -v grep | egrep -q "truncate2$"; do
+ $here/../testcases/swap/swap -t 2m -i 20
+done
+
+rm -f /tmp/truncate2
+exit 0
+EOF
+#include <fcntl.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#define SIZ 512
+char buf[SIZ];
+
+void
+test(void)
+{
+ int fd[10], i, j;
+ char name[128];
+ off_t len;
+
+ for (j = 0; j < 10; j++) {
+ sprintf(name, "%05d.%05d", getpid(), j);
+ fd[j] = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
+ if (fd[j] == -1)
+ err(1, "%s", name);
+ unlink(name);
+ }
+
+ for (i = 0; i < 100; i++) {
+ for (j = 0; j < 10; j++) {
+ if (write(fd[j], buf, 2) != 2)
+ err(1, "write");
+ len = arc4random() % 1024 * 1024;
+ if (ftruncate(fd[j], len) == -1)
+ err(1, "ftruncate");
+ }
+ }
+
+ exit(0);
+}
+int
+main(int argc, char **argv)
+{
+ int i, status;
+
+ for (i = 0; i < 20; i++) {
+ if (fork() == 0)
+ test();
+ }
+ for (i = 0; i < 20; i++)
+ wait(&status);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/truncate3.sh b/tools/test/stress2/misc/truncate3.sh
new file mode 100755
index 000000000000..af1b41cdb6c3
--- /dev/null
+++ b/tools/test/stress2/misc/truncate3.sh
@@ -0,0 +1,119 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# Variation of truncate2.sh with FS check added.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q "$mntpoint" && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 400m -u $mdstart
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+export RUNDIR=$mntpoint/stressX
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > truncate3.c
+mycc -o truncate3 -Wall -O2 truncate3.c
+rm -f truncate3.c
+[ -d $RUNDIR ] || mkdir -p $RUNDIR
+cd $RUNDIR
+
+/tmp/truncate3
+
+cd $here
+rm -f /tmp/truncate3
+
+sync;sync;sync
+while mount | grep -q $mntpoint; do
+ sync;sync;sync
+ sleep 1
+ umount $mntpoint
+done
+
+checkfs /dev/md${mdstart}$part; s=$?
+mdconfig -d -u $mdstart
+exit $s
+EOF
+#include <fcntl.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#define SIZ 512
+char buf[SIZ];
+
+void
+test(void)
+{
+ int fd[10], i, j;
+ char name[128];
+ off_t len;
+
+ for (j = 0; j < 10; j++) {
+ sprintf(name, "%05d.%05d", getpid(), j);
+ fd[j] = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
+ if (fd[j] == -1)
+ err(1, "open(%s)", name);
+ unlink(name);
+ }
+
+ for (i = 0; i < 100; i++) {
+ for (j = 0; j < 10; j++) {
+ if (write(fd[j], buf, 2) != 2)
+ err(1, "write");
+ len = arc4random() % 1024 * 1024;
+ if (ftruncate(fd[j], len) == -1)
+ err(1, "ftruncate");
+ }
+ }
+
+ exit(0);
+}
+int
+main(int argc, char **argv)
+{
+ int i, status;
+
+ for (i = 0; i < 20; i++) {
+ if (fork() == 0)
+ test();
+ }
+ for (i = 0; i < 20; i++)
+ wait(&status);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/truncate4.sh b/tools/test/stress2/misc/truncate4.sh
new file mode 100755
index 000000000000..394970c55b12
--- /dev/null
+++ b/tools/test/stress2/misc/truncate4.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# fsck fails with "PARTIALLY ALLOCATED INODE I=4". UFS1 and UFS2
+# Test scenario by Bruce Cran <bruce cran org uk>
+
+. ../default.cfg
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+s=0
+for size in $((4 * 1024 * 1024 * 1024 - 1)) $((4 * 1024 * 1024 * 1024)); do
+ newfs $newfs_flags md${mdstart}$part > /dev/null
+ mount /dev/md${mdstart}$part $mntpoint
+
+ truncate -s $size $mntpoint/f1 && rm $mntpoint/f1
+
+ while mount | grep "$mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+ done
+ checkfs /dev/md${mdstart}$part || s=1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/fsck.log
+exit $s
diff --git a/tools/test/stress2/misc/truncate5.sh b/tools/test/stress2/misc/truncate5.sh
new file mode 100755
index 000000000000..e21432b1f0a4
--- /dev/null
+++ b/tools/test/stress2/misc/truncate5.sh
@@ -0,0 +1,105 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# "panic: handle_workitem_freeblocks: inode 447489 block count 336" seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q "$mntpoint" && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > truncate5.c
+mycc -o truncate5 -Wall -Wextra -O2 truncate5.c
+rm -f truncate5.c
+
+export RUNDIR=$mntpoint/stressX
+[ -d $RUNDIR ] || mkdir -p $RUNDIR
+cd $RUNDIR
+
+/tmp/truncate5
+
+cd $here
+rm -f /tmp/truncate5
+
+while mount | grep -q md${mdstart}$part; do
+ umount $mntpoint || sleep 1
+done
+
+checkfs /dev/md${mdstart}$part; s=$?
+mdconfig -d -u $mdstart
+exit $s
+EOF
+#include <fcntl.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#define SIZ 4096
+char buf[SIZ];
+
+int
+main()
+{
+ int fd, i, j;
+ char name[128];
+ off_t len = 21474837416LL;
+ off_t pos;
+
+ srand48(getpid());
+ sprintf(name, "%05d.%05d", getpid(), 0);
+ fd = open(name, O_WRONLY | O_CREAT, 0666);
+ if (ftruncate(fd, len) == -1)
+ err(1, "ftruncate");
+ unlink(name);
+ sleep(1);
+
+ for (i = 0; i < 100; i++) {
+ for (j = 0; j < 100; j++) {
+ pos = lrand48() % (len - sizeof(buf));
+ if (lseek(fd, pos, SEEK_SET) == -1)
+ err(1, "lseek");
+ if (write(fd, buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write");
+ }
+ }
+ close(fd);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/truncate6.sh b/tools/test/stress2/misc/truncate6.sh
new file mode 100755
index 000000000000..5371fb0f183f
--- /dev/null
+++ b/tools/test/stress2/misc/truncate6.sh
@@ -0,0 +1,122 @@
+#!/bin/sh
+
+#
+# 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 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.
+#
+
+# The issue, found by Maxim, is that sometimes partial truncate could
+# create a UFS inode where the last byte is not populated.
+# Fixed by r295950.
+
+# Test scenario by Maxim Sobolev <sobomax@sippysoft.com>
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q "$mntpoint" && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+
+if [ $# -eq 0 ]; then
+ newfs -n $newfs_flags md$mdstart > /dev/null
+else
+ newfs -n md$mdstart > /dev/null
+fi
+mount /dev/md$mdstart $mntpoint
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > truncate6.c
+mycc -o truncate6 -Wall -Wextra -O2 truncate6.c -lutil
+rm -f truncate6.c
+
+cd $mntpoint
+
+/tmp/truncate6
+inode=$(ls -ail | awk '/file/ {print $1}')
+
+cd $here
+rm -f /tmp/truncate6
+
+while mount | grep -q md$rt; do
+ umount $mntpoint || sleep 1
+done
+
+full=$(
+fsdb -r /dev/md$mdstart <<QUOTE
+inode $inode
+blocks
+quit
+QUOTE
+)
+full=`echo "$full" | sed '/Last Mounted/,+6d'`
+r=`echo "$full" | tail -1`
+expect=" lbn 3 blkno 4712-4719"
+if [ "$r" != "$expect" ]; then
+ e=1
+ echo "FAIL Expected \"$expect\", got \"$r\"."
+ echo "$full" | tail -3
+else
+ e=0
+fi
+
+mdconfig -d -u $mdstart
+exit $e
+EOF
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ off_t hole, data, pos;
+ int fd;
+ char tempname[] = "file";
+
+ pos = 1024 * 128 + 1; // 131073
+ if ((fd = open(tempname, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE)) ==
+ -1)
+ err(1, "open(%s)", tempname);
+ if (ftruncate(fd, pos) < 0)
+ err(1, "ftruncate()");
+ hole = lseek(fd, 0, SEEK_HOLE); // 0
+ data = lseek(fd, 0, SEEK_DATA); // 131072
+ if (ftruncate(fd, data) < 0)
+ err(1, "ftruncate() 2");
+ close(fd);
+ if (hole != 0 && data != 131072) {
+ printf("--> hole = %jd, data = %jd, pos = %jd\n",
+ (intmax_t)hole, (intmax_t)data, (intmax_t)pos);
+ exit (1);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/truncate7.sh b/tools/test/stress2/misc/truncate7.sh
new file mode 100755
index 000000000000..ec216721180c
--- /dev/null
+++ b/tools/test/stress2/misc/truncate7.sh
@@ -0,0 +1,124 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Variation of truncate3.sh.
+# No problems seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep -q "$mntpoint" && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 400m -u $mdstart
+bsdlabel -w md$mdstart auto
+
+if [ `jot -r 1 0 1` -eq 0 ]; then
+ newfs $newfs_flags md${mdstart}$part > /dev/null
+else
+ newfs md${mdstart}$part > /dev/null
+fi
+mount /dev/md${mdstart}$part $mntpoint
+export RUNDIR=$mntpoint/stressX
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > truncate7.c
+mycc -o truncate7 -Wall -O2 truncate7.c
+rm -f truncate7.c
+[ -d $RUNDIR ] || mkdir -p $RUNDIR
+cd $RUNDIR
+
+/tmp/truncate7
+
+cd $here
+rm -f /tmp/truncate7
+
+sync;sync;sync
+while mount | grep -q $mntpoint; do
+ sync;sync;sync
+ sleep 1
+ umount $mntpoint
+done
+
+checkfs /dev/md${mdstart}$part; s=$?
+mdconfig -d -u $mdstart
+exit $s
+EOF
+#include <fcntl.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#define SIZ 512
+char buf[SIZ];
+
+void
+test(void)
+{
+ int fd[10], i, j;
+ char name[128];
+ off_t len;
+
+ for (j = 0; j < 10; j++) {
+ sprintf(name, "%05d.%05d", getpid(), j);
+ fd[j] = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
+ if (fd[j] == -1)
+ err(1, "open(%s)", name);
+ unlink(name);
+ }
+
+ for (i = 0; i < 100; i++) {
+ for (j = 0; j < 10; j++) {
+ if (write(fd[j], buf, 2) != 2)
+ err(1, "write");
+ len = arc4random() % (1024 * 1024);
+ if (ftruncate(fd[j], len) == -1)
+ err(1, "ftruncate");
+ }
+ }
+
+ exit(0);
+}
+int
+main(int argc, char **argv)
+{
+ int i, status;
+
+ for (i = 0; i < 20; i++) {
+ if (fork() == 0)
+ test();
+ }
+ for (i = 0; i < 20; i++)
+ wait(&status);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/truncate8.sh b/tools/test/stress2/misc/truncate8.sh
new file mode 100755
index 000000000000..d175623cb090
--- /dev/null
+++ b/tools/test/stress2/misc/truncate8.sh
@@ -0,0 +1,212 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Bug 236977 - [msdosfs] returns cached truncated data
+# Fixed by r345847
+
+. ../default.cfg
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/truncate8.c
+mycc -o truncate8 -Wall -Wextra -O0 -g truncate8.c || exit 1
+rm -f truncate8.c
+cd $odir
+
+echo ufs:
+mount | grep -q "$mntpoint " && umount $mntpoint
+mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+(cd $mntpoint; /tmp/truncate8)
+s=$?
+[ $s -ne 0 ] && { echo "UFS exit status is $s"; status=1; }
+while mount | grep -q "$mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+
+echo nfs:
+if ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1; then
+ mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export \
+ $mntpoint
+ sleep .2
+ (cd $mntpoint; /tmp/truncate8)
+ s=$?
+ [ $s -ne 0 ] && { echo "NFS exit status is $s"; status=1; }
+
+ umount $mntpoint || umount $mntpoint
+fi
+
+echo msdos:
+if [ -x /sbin/mount_msdosfs ]; then
+ mdconfig -a -t swap -s 1g -u $mdstart
+ bsdlabel -w md$mdstart auto
+ newfs_msdos -F 16 -b 8192 /dev/md${mdstart}$part > /dev/null 2>&1
+ mount_msdosfs -m 777 /dev/md${mdstart}$part $mntpoint
+
+ (cd /mnt; /tmp/truncate8)
+ s=$?
+ [ $s -ne 0 ] && { echo "MSDOS exit status is $s"; status=1; }
+ while mount | grep -q "$mntpoint "; do
+ umount $mntpoint || sleep 1
+ done
+ mdconfig -d -u $mdstart
+fi
+
+echo tmpfs:
+mount -t tmpfs null $mntpoint
+chmod 777 $mntpoint
+
+(cd $mntpoint; /tmp/truncate8)
+s=$?
+[ $s -ne 0 ] && { echo "TMPFS exit status is $s"; status=1; }
+while mount | grep -q "$mntpoint "; do
+ umount $mntpoint || sleep 1
+done
+
+rm -rf /tmp/truncate8
+exit $status
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#define BSIZE 5120
+#define FSIZE (128 * 1024 * 1024)
+#define PARALLEL 2
+#define RUNTIME (1 * 60)
+#define SYNC 0
+
+static void
+test(void)
+{
+ time_t start;
+ off_t pos;
+ int fd, i, n, r;
+ char *buf, name[128];
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != PARALLEL)
+ ;
+
+ srand48(getpid());
+ buf = malloc(BSIZE);
+ for (i = 0; i < (int)sizeof(buf); i++)
+ buf[i] = 123;
+
+ sprintf(name, "f%05d", getpid());
+ if ((fd = open(name, O_RDWR | O_CREAT, 0640)) == -1)
+ err(1, "%s", name);
+ for (i = 0; i < FSIZE / BSIZE; i++)
+ if (write(fd, buf, BSIZE) != BSIZE)
+ err(1, "write");
+
+ start = time(NULL);
+ while (time(NULL) - start < RUNTIME) {
+ pos = lrand48() % (FSIZE - BSIZE);
+// fprintf(stderr, "truncate(%jd)\n", (intmax_t)pos);
+ if (ftruncate(fd, pos) == -1)
+ err(1, "ftruncate");
+
+ if (ftruncate(fd, FSIZE) == -1)
+ err(1, "ftruncate");
+ if (lseek(fd, pos, SEEK_SET) == -1)
+ err(1, "lseek");
+ n = 0;
+ while ((r = read(fd, buf, BSIZE)) == BSIZE) {
+ n++;
+ for (i = 0; i < BSIZE; i++) {
+ if (buf[i] != 0)
+ errx(1, "Bad read value @ %jd = %d",
+ (intmax_t)(pos + i), buf[i]);
+ }
+ }
+ if (r == -1)
+ err(1, "read");
+ if (n != (FSIZE - pos) / BSIZE)
+ fprintf(stderr, "n = %d, target = %d\n", n, (int)(FSIZE - pos) / BSIZE);
+ }
+ unlink(name);
+
+ _exit(0);
+}
+
+int
+main(void)
+{
+ pid_t pids[PARALLEL];
+ size_t len;
+ int e, i, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0)
+ test();
+ if (pids[i] == -1)
+ err(1, "fork()");
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ if (status != 0) {
+ if (WIFSIGNALED(status))
+ fprintf(stderr,
+ "pid %d exit signal %d\n",
+ pids[i], WTERMSIG(status));
+ }
+ e += status == 0 ? 0 : 1;
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/truncate9.sh b/tools/test/stress2/misc/truncate9.sh
new file mode 100755
index 000000000000..1acda51aa7c8
--- /dev/null
+++ b/tools/test/stress2/misc/truncate9.sh
@@ -0,0 +1,119 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Truncate test scenario. No problems seen.
+
+. ../default.cfg
+
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > truncate9.c
+mycc -o truncate9 -Wall -Wextra truncate9.c || exit 1
+rm -f truncate9.c
+set -e
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags -n md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+set +e
+
+(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -l 100) > /dev/null &
+cd $mntpoint
+for i in `jot 5`; do
+ /tmp/truncate9 &
+ pids="$pids $!"
+done
+for i in $pids; do
+ wait $i
+done
+while pkill swap; do :; done
+wait
+cd $odir
+
+umount $mntpoint
+mdconfig -d -u $mdstart
+rm /tmp/truncate9
+exit $s
+EOF
+#include <sys/mman.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+int
+main (void)
+{
+ off_t opos, pos;
+ time_t start;
+ int fd, i;
+ char file[80], *cp;
+
+ snprintf(file, sizeof(file), "file.%d", getpid());
+ start = time(NULL);
+ if ((fd = open(file, O_RDWR | O_TRUNC | O_CREAT, 0644)) == -1)
+ err(1, "open(%s)", file);
+ close(fd);
+ while (time(NULL) - start < 120) {
+ if ((fd = open(file, O_RDWR)) == -1)
+ err(1, "open(%s)", file);
+ pos = arc4random();
+ opos = pos = (pos << 12) | arc4random();
+ if (ftruncate(fd, pos) == -1)
+ err(1, "ftruncate()");
+ if ((cp = mmap(NULL, pos, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ cp[0] = 1;
+ cp[pos - 1] = 2;
+ for (i = 0; i < 10; i++) {
+ while (pos >= opos) {
+ pos = arc4random();
+ pos = (pos << 12) | arc4random();
+ }
+ if (ftruncate(fd, pos) == -1)
+ err(1, "ftruncate()");
+ cp[0] = 1;
+ cp[pos - 1] = 2;
+ }
+
+ if (munmap(cp, opos) == -1)
+ err(1, "munmap");
+ close(fd);
+ }
+ unlink(file);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/truss.sh b/tools/test/stress2/misc/truss.sh
new file mode 100755
index 000000000000..dc2747c5f068
--- /dev/null
+++ b/tools/test/stress2/misc/truss.sh
@@ -0,0 +1,129 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# truss(1) of a multithreaded program.
+
+# FAIL
+# 0 90968 90933 0 52 0 6028 0 wait IW+ 1 0:00,00 truss /tmp/ttruss
+# 0 90970 90968 0 52 0 6436 1560 uwrlck IX+ 1 0:00,00 /tmp/ttruss
+# $ procstat -k 90970
+# PID TID COMM TDNAME KSTACK
+# 90970 101244 ttruss - mi_switch sleepq_switch
+# sleepq_catch_signals sleepq_wait_sig _sleep umtxq_sleep
+# do_rw_wrlock __umtx_op_rw_wrlock syscall Xint0x80_syscall
+# $
+
+# Only seen during testing of a WiP ptrace(2) patch.
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/ttruss.c
+mycc -o ttruss -Wall -Wextra -O0 -g ttruss.c -lpthread || exit 1
+rm -f ttruss.c
+
+# VM pressure is not mandatory, but shortens the time to failure.
+daemon sh -c \
+ "(cd $odir/../testcases/swap; ./swap -t 6m -i 20 -k -l 100)" > \
+ /dev/null
+sleep .5
+for i in `jot 30`; do
+ truss /tmp/ttruss 10 > /dev/null 2>&1 &
+ sleep 11
+ if ps -lx | grep -v grep | grep -q uwrlck; then
+ echo FAIL
+ ps -lH | egrep -v "grep|truss.sh" | grep truss
+ while pkill -9 swap; do
+ :
+ done
+ exit 1
+ fi
+ wait
+done
+while pkill -9 swap; do
+ :
+done
+sleep 2
+if pgrep -q ttruss; then
+ echo FAIL
+ ps -lxH | grep -v grep | grep ttruss
+ s=1
+fi
+
+[ -f /tmp/truss.core ] && { ls -l /tmp/truss.core; s=1; }
+rm -rf /tmp/ttruss /tmp/ttruss.core
+exit $s
+
+EOF
+#include <sys/param.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define THREADS 16
+
+static void *
+t1(void *data __unused)
+{
+ return (NULL);
+}
+
+int
+main(int argc, char *argv[])
+{
+ pthread_t tid[THREADS];
+ time_t start;
+ int i, rc, runtime;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <runtime>", argv[0]);
+ runtime = atoi(argv[1]);
+ start = time(NULL);
+ while ((time(NULL) - start) < runtime) {
+ for (i = 0; i < THREADS; i++) {
+ if ((rc = pthread_create(&tid[i], NULL, t1, NULL)) !=
+ 0)
+ errc(1, rc, "pthread_create");
+ }
+
+ for (i = 0; i < THREADS; i++) {
+ if ((rc = pthread_join(tid[i], NULL)) != 0)
+ errc(1, rc, "pthread_join");
+ }
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/truss3.sh b/tools/test/stress2/misc/truss3.sh
new file mode 100755
index 000000000000..83314ae3165f
--- /dev/null
+++ b/tools/test/stress2/misc/truss3.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+# Regression test from:
+# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=249257
+# by Edward Tomasz Napierala <trasz@freebsd.org>
+
+# panic: pgrp 2191 0xfffff8000585c900 pg_jobc 1 cnt 2
+# cpuid = 10
+# time = 1599941578
+# KDB: stack backtrace:
+# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe0101b378f0
+# vpanic() at vpanic+0x182/frame 0xfffffe0101b37940
+# panic() at panic+0x43/frame 0xfffffe0101b379a0
+# check_pgrp_jobc() at check_pgrp_jobc+0x124/frame 0xfffffe0101b379e0
+# doenterpgrp() at doenterpgrp+0xc6/frame 0xfffffe0101b37a30
+# enterpgrp() at enterpgrp+0x39e/frame 0xfffffe0101b37a80
+# sys_setpgid() at sys_setpgid+0x219/frame 0xfffffe0101b37ad0
+# amd64_syscall() at amd64_syscall+0x159/frame 0xfffffe0101b37bf0
+# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe0101b37bf0
+# --- syscall (82, FreeBSD ELF64, sys_setpgid), rip = 0x8003745ba, rsp = 0x7fffffffe638, rbp = 0x7fffffffe670 ---
+# KDB: enter: panic
+# [ thread pid 2191 tid 100251 ]
+# Stopped at kdb_enter+0x37: movq $0,0x10b29f6(%rip)
+# db> x/s version
+# version: FreeBSD 13.0-CURRENT #0 r365671: Sat Sep 12 22:01:12 CEST 2020
+# pho@t1.osted.lan:/usr/src/sys/amd64/compile/PHO\012
+# db>
+
+# Fixed by r365814
+
+# Needs a controlling terminal for job control to be active
+[ -t 1 ] || { echo "Not a tty"; exit 0; }
+
+set -i
+echo id | truss -o /dev/null -f /bin/sh -i
+exit 0
diff --git a/tools/test/stress2/misc/tvnlru.sh b/tools/test/stress2/misc/tvnlru.sh
new file mode 100755
index 000000000000..09789a22b68e
--- /dev/null
+++ b/tools/test/stress2/misc/tvnlru.sh
@@ -0,0 +1,251 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Scenario: backup of a number (> maxvnodes) of small files.
+
+# Test how vnlru impacts open(2).
+
+# FreeBSD 12.0-CURRENT #0 r312620: Mon Jan 23 23:27:46 CET 2017
+# /usr/src/sys/amd64/compile/BENCH amd64
+# ./tvnlru.sh
+# FAIL 328/306502
+# files = 500000, maxvnodes = 500000, ave=0.000018, max=0.000328, elapsed 4
+# files = 500000, maxvnodes = 500000, ave=0.000018, max=0.000155, elapsed 4
+# files = 500000, maxvnodes = 500000, ave=0.000018, max=0.000227, elapsed 4
+# files = 500000, maxvnodes = 500000, ave=0.000014, max=0.000126, elapsed 3
+#
+# files = 1000000, maxvnodes = 500000, ave=0.000035, max=0.205627, elapsed 14
+# files = 1000000, maxvnodes = 500000, ave=0.000033, max=0.205185, elapsed 14
+# files = 1000000, maxvnodes = 500000, ave=0.000038, max=0.306502, elapsed 14
+# files = 1000000, maxvnodes = 500000, ave=0.000037, max=0.205177, elapsed 14
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `sysctl -n hw.physmem` -lt $(( 4 * 1024 * 1024 * 1024)) ] && exit 0
+
+files=1000000
+[ `sysctl -n kern.maxvnodes` -lt $files ] && exit 0
+
+log=/tmp/tvnlru.log
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/tvnlru.c
+mycc -o tvnlru -Wall -Wextra -O0 -g tvnlru.c || exit 1
+rm -f tvnlru.c
+cd $odir
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 2g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs -n -b 4096 -f 512 -i 512 md${mdstart}$part > /dev/null
+mount -o async /dev/md${mdstart}$part $mntpoint
+
+ncpu=`sysctl -n hw.ncpu`
+[ $ncpu -lt 4 ] && { rm /tmp/tvnlru; exit 0; }
+ncpu=4
+inodes=`df -i $mntpoint | tail -1 | awk "{print \\$7 - $ncpu - 1}"`
+oldmx=`sysctl -n kern.maxvnodes`
+[ $files -gt $inodes ] && { echo "Disk too small"; files=$inodes; }
+[ $files -gt $oldmx ] &&
+ { echo "$files exceed old maxvnods"; files=$oldmx; }
+newmaxvnodes=$((files / 2))
+trap "sysctl kern.maxvnodes=$oldmx > /dev/null" EXIT SIGINT
+sysctl kern.maxvnodes=$newmaxvnodes > /dev/null
+
+# warmup
+cd $mntpoint
+t1=`/tmp/tvnlru $ncpu $newmaxvnodes $newmaxvnodes $mntpoint 2>/dev/null`
+cd $odir
+umount $mntpoint
+newfs -n -b 4096 -f 512 -i 512 md${mdstart}$part > /dev/null
+mount -o async /dev/md${mdstart}$part $mntpoint
+
+cd $mntpoint
+t1=`/tmp/tvnlru $ncpu $newmaxvnodes $newmaxvnodes $mntpoint 2>$log`
+cd $odir
+umount $mntpoint
+newfs -n -b 4096 -f 512 -i 512 md${mdstart}$part > /dev/null
+mount -o async /dev/md${mdstart}$part $mntpoint
+
+cd $mntpoint
+echo >> $log
+t2=`/tmp/tvnlru $ncpu $files $newmaxvnodes $mntpoint 2>>$log`
+s=$?
+cd $odir
+
+s=0
+for i in `jot 10`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint || sleep 2
+done
+mount | grep -q "on $mntpoint " && { s=2; umount -f $mntpoint; }
+mdconfig -d -u $mdstart
+[ $t2 -gt $((t1 * 3)) ] && { s=3; echo "Fail $t1/$t2"; cat $log; }
+rm -rf /tmp/tvnlru /tmp/tvnlru.log
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/time.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#define SYNC 0
+
+static long maxvnodes, parallel, tvnodes, vnodes;
+static char *mp;
+
+static void
+test(int idx)
+{
+ struct timeval diff, start, stop;
+ time_t st;
+ uint64_t mx, tot, usec;
+ pid_t pid;
+ int fd, i, n;
+ char dir[80], file[80], help[80];
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != (unsigned int)parallel)
+ ;
+
+ pid = getpid();
+ snprintf(dir, sizeof(dir), "d%09ld", (long)pid);
+ if (mkdir(dir, 0700) == -1)
+ err(1, "mkdir(%s)", dir);
+ if (chdir(dir) == -1)
+ err(1, "chdir(%s)", dir);
+
+ for (i = 0; i < vnodes; i++) {
+ snprintf(file, sizeof(file), "f%09d", i);
+ if ((fd = open(file, O_RDWR | O_CREAT, DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ close(fd);
+ }
+
+ snprintf(help, sizeof(help), "umount %s > /dev/null 2>&1", mp);
+ system(help); /* flush the cache */
+
+ mx = 0;
+ n = 0;
+ st = time(NULL);
+ tot = 0;
+ for (i = 0; i < vnodes; i++) {
+ snprintf(file, sizeof(file), "f%09d", i);
+ gettimeofday(&start, NULL);
+ if ((fd = open(file, O_RDONLY)) == -1)
+ err(1, "open(%s)", file);
+ gettimeofday(&stop, NULL);
+ timersub(&stop, &start, &diff);
+ usec = ((uint64_t)1000000 * diff.tv_sec + diff.tv_usec);
+ tot += usec;
+ n++;
+ if (mx < usec)
+ mx = usec;
+ close(fd);
+ }
+ fprintf(stderr,
+ "files = %7ld, maxvnodes = %ld, ave=%.6f, max=%.6f, "
+ "elapsed %2ld\n",
+ tvnodes, maxvnodes, (double)tot / 1000000 / n, (double)mx /
+ 1000000, time(NULL) - st);
+ share[idx] = mx;
+
+ for (i = 0; i < vnodes; i++) {
+ snprintf(file, sizeof(file), "f%09d", i);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+ }
+ chdir("..");
+ if (rmdir(dir) == -1)
+ err(1, "rmdir(%s)", dir);
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ size_t len;
+ pid_t *pids;
+ int e, i, status;
+ u_int mx;
+
+ if (argc != 5) {
+ fprintf(stderr, "Usage: %s <ncpu> <inodes> <maxvnodes> <mount point>\n",
+ argv[0]);
+ exit(1);
+ }
+ parallel = atol(argv[1]);
+ pids = calloc(parallel, sizeof(pid_t));
+ tvnodes = atol(argv[2]);
+ vnodes = tvnodes / parallel;
+ maxvnodes = atol(argv[3]);
+ mp = argv[4];
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ for (i = 0; i < parallel; i++) {
+ if ((pids[i] = fork()) == 0)
+ test(i + 1);
+ }
+
+ for (i = 0; i < parallel; i++) {
+ if (waitpid(pids[i], &status, 0) == -1)
+ err(1, "waitpid(%d)", pids[i]);
+ e += status == 0 ? 0 : 1;
+ }
+
+ mx = 0;
+ for (i = 0; i < parallel; i++) {
+// fprintf(stderr, "share[%d] = %u\n", i + 1, share[i + 1]);
+ if (mx < share[i + 1])
+ mx = share[i + 1];
+ }
+ fprintf(stdout, "%lu\n", (unsigned long)mx);
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/udp.sh b/tools/test/stress2/misc/udp.sh
new file mode 100755
index 000000000000..4fb50d40ad54
--- /dev/null
+++ b/tools/test/stress2/misc/udp.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# "panic: buf=0xfffffe01a8db2e00 already enqueue at 3923 prod= ..." seen.
+# http://people.freebsd.org/~pho/stress/log/igb001.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+ping -c 5 -t 2 $BLASTHOST > /dev/null 2>&1 ||
+ { echo "Can not ping $BLASTHOST"; exit 0; }
+
+export runRUNTIME=10m
+export swapLOAD=100
+export udpLOAD=100
+
+export TESTPROGS="
+testcases/swap/swap
+testcases/udp/udp
+"
+
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
diff --git a/tools/test/stress2/misc/udp2.sh b/tools/test/stress2/misc/udp2.sh
new file mode 100755
index 000000000000..3cd72d82734b
--- /dev/null
+++ b/tools/test/stress2/misc/udp2.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# UDP load test
+
+# "panic: Assertion *sd->ifsd_cl != NULL failed at iflib.c:2620" seen
+# in WiP code:
+# https://people.freebsd.org/~pho/stress/log/mark163.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+ping -c 5 -t 2 $BLASTHOST > /dev/null 2>&1 ||
+ { echo "Can not ping $BLASTHOST"; exit 0; }
+
+export RUNTIME=3m
+export runRUNTIME=10m
+export swapLOAD=100
+export swapHOG=1
+export udpINCARNATIONS=200
+export udpLOAD=100
+
+export TESTPROGS="
+testcases/swap/swap
+testcases/udp/udp
+"
+
+su $testuser -c 'cd ..; timeout 12m ./testcases/run/run $TESTPROGS'
+../tools/killall.sh
+exit 0
diff --git a/tools/test/stress2/misc/ufsbench.sh b/tools/test/stress2/misc/ufsbench.sh
new file mode 100755
index 000000000000..9ea572132c52
--- /dev/null
+++ b/tools/test/stress2/misc/ufsbench.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Simple non random fs test
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+mycc -o /tmp/ufsbench -Wall -Wextra -O0 -g ../tools/bench.c || exit 1
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+
+s=0
+for j in `jot 5`; do
+ newfs -n -b 4096 -f 512 -i 1024 md${mdstart}$part > \
+ /dev/null
+ mount -o async /dev/md${mdstart}$part $mntpoint
+ (cd $mntpoint; /tmp/ufsbench)
+ [ $? -ne 0 ] && s=1
+ for i in `jot 6`; do
+ umount $mntpoint && break || sleep 10
+ mount | grep -q "on $mntpoint " || break
+ done
+ [ $i -eq 6 ] && exit 1
+done
+
+mdconfig -d -u $mdstart
+rm -rf /tmp/ufsbench
+exit $s
diff --git a/tools/test/stress2/misc/ufssuspend.sh b/tools/test/stress2/misc/ufssuspend.sh
new file mode 100755
index 000000000000..b4c86490fe25
--- /dev/null
+++ b/tools/test/stress2/misc/ufssuspend.sh
@@ -0,0 +1,131 @@
+#!/bin/sh
+
+# ioctl(g_ufs_suspend_handle, UFSSUSPEND, &statfsp->f_fsid) test scenario.
+
+# Bug 230220 - UFS: the freezing ioctl (i.e.UFSSUSPEND) causes panic or EBUSY
+# "panic: devfs_set_cdevpriv failed" seen.
+# Test scenario by Dexuan Cui <decui microsoft com>
+# Fixed by r337055.
+
+[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+cat > /tmp/ufssuspend.c <<EOF
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/param.h>
+#include <sys/ucred.h>
+#include <sys/mount.h>
+
+#include <ufs/ffs/fs.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <paths.h>
+#include <poll.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+#include <syslog.h>
+#include <unistd.h>
+
+static int g_ufs_suspend_handle = -1;
+static const char *dev = "/dev";
+
+static int
+freeze(void)
+{
+ struct statfs *mntbuf, *statfsp;
+ int mntsize;
+ int error = 0;
+ int i;
+
+ g_ufs_suspend_handle = open(_PATH_UFSSUSPEND, O_RDWR);
+ if (g_ufs_suspend_handle == -1) {
+ printf("unable to open %s", _PATH_UFSSUSPEND);
+ return (errno);
+ }
+
+ mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
+ if (mntsize == 0) {
+ printf("There is no mount information\n");
+ return (EINVAL);
+ }
+
+ for (i = mntsize - 1; i >= 0; --i) {
+ statfsp = &mntbuf[i];
+
+ if (strncmp(statfsp->f_mntonname, dev, strlen(dev)) == 0)
+ continue; /* skip to freeze '/dev' */
+
+ if (statfsp->f_flags & MNT_RDONLY)
+ continue; /* skip to freeze RDONLY partition */
+
+ if (strncmp(statfsp->f_fstypename, "ufs", 3) != 0)
+ continue; /* so far, only UFS can be frozen */
+
+ printf("suspending fs: %s\n", statfsp->f_mntonname);
+ error = ioctl(g_ufs_suspend_handle, UFSSUSPEND, &statfsp->f_fsid);
+ if (error != 0) {
+ printf("error: %d\n", errno);
+ error = errno;
+ } else {
+ printf("Successfully suspend fs: %s\n", statfsp->f_mntonname);
+ }
+ }
+
+ return (error);
+}
+
+/**
+ * closing the opened handle will thaw the FS.
+ */
+static int
+thaw(void)
+{
+ int error = 0;
+
+ if (g_ufs_suspend_handle != -1) {
+ error = close(g_ufs_suspend_handle);
+ if (!error) {
+ g_ufs_suspend_handle = -1;
+ printf("Successfully thaw the fs\n");
+ } else {
+ error = errno;
+ printf("Fail to thaw the fs: "
+ "%d %s\n", errno, strerror(errno));
+ }
+ } else {
+ printf("The fs has already been thawed\n\n");
+ }
+
+ return (error);
+}
+
+int
+main(void)
+{
+ int error;
+
+ error = freeze();
+ printf("freeze: err=%d\n", error);
+
+ error = thaw();
+ printf("thaw: err=%d\n", error);
+
+ return 0;
+}
+EOF
+
+mycc -o /tmp/ufssuspend -Wall -Wextra -O2 -g /tmp/ufssuspend.c || exit 1
+rm /tmp/ufssuspend.c
+
+cd /tmp
+./ufssuspend > /dev/null
+s=$?
+rm /tmp/ufssuspend
+exit $s
diff --git a/tools/test/stress2/misc/uma_zalloc_arg.sh b/tools/test/stress2/misc/uma_zalloc_arg.sh
new file mode 100755
index 000000000000..322f358326b9
--- /dev/null
+++ b/tools/test/stress2/misc/uma_zalloc_arg.sh
@@ -0,0 +1,302 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# uma_zalloc_arc() fail point test scenario.
+# Test scenario by Ryan Libby <rlibby@gmail.com>.
+
+# "panic: backing_object 0xfffff8016dd74420 was somehow re-referenced during
+# collapse!" seen.
+# https://people.freebsd.org/~pho/stress/log/uma_zalloc_arg.txt
+
+# Hang seen:
+# https://people.freebsd.org/~pho/stress/log/kostik869.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+sysctl debug.mnowait_failure.zalloc_whitelist > /dev/null 2>&1 || exit 0
+
+s1=`sysctl -n debug.mnowait_failure.zalloc_whitelist`
+s2=`sysctl -n debug.fail_point.uma_zalloc_arg`
+cleanup() {
+ sysctl debug.mnowait_failure.zalloc_whitelist="$s1" > /dev/null
+ sysctl debug.fail_point.uma_zalloc_arg="$s2" > /dev/null
+}
+trap "cleanup" EXIT INT
+sysctl debug.mnowait_failure.zalloc_whitelist='RADIX NODE'
+sysctl debug.fail_point.uma_zalloc_arg='1%return'
+
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 300 ]; do
+ sh -c "echo | cat | cat > /dev/null"
+done
+exit 0
+
+The patch from
+https://github.com/freebsd/freebsd/compare/master...rlibby:mnowait-dbg
+
+diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c
+index 01aff78..9d557a1 100644
+--- a/sys/kern/kern_malloc.c
++++ b/sys/kern/kern_malloc.c
+@@ -50,6 +50,7 @@ __FBSDID("$FreeBSD$");
+
+ #include <sys/param.h>
+ #include <sys/systm.h>
++#include <sys/fail.h>
+ #include <sys/kdb.h>
+ #include <sys/kernel.h>
+ #include <sys/lock.h>
+@@ -472,6 +473,19 @@ malloc(unsigned long size, struct malloc_type *mtp, int flags)
+ }
+ }
+ #endif
++ if (flags & M_NOWAIT) {
++ KFAIL_POINT_CODE(DEBUG_FP, malloc, {
++ if (uma_dbg_nowait_fail_enabled_malloc(
++ mtp->ks_shortdesc)) {
++ /* XXX record call stack */
++#ifdef MALLOC_MAKE_FAILURES
++ atomic_add_int(&malloc_failure_count, 1);
++ t_malloc_fail = time_uptime;
++#endif
++ return (NULL);
++ }
++ });
++ }
+ if (flags & M_WAITOK)
+ KASSERT(curthread->td_intr_nesting_level == 0,
+ ("malloc(M_WAITOK) in interrupt context"));
+diff --git a/sys/vm/uma_core.c b/sys/vm/uma_core.c
+index 1f57dff..dfa18e6 100644
+--- a/sys/vm/uma_core.c
++++ b/sys/vm/uma_core.c
+@@ -64,6 +64,7 @@ __FBSDID("$FreeBSD$");
+ #include <sys/param.h>
+ #include <sys/systm.h>
+ #include <sys/bitset.h>
++#include <sys/fail.h>
+ #include <sys/kernel.h>
+ #include <sys/types.h>
+ #include <sys/queue.h>
+@@ -2148,6 +2149,23 @@ uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
+ if (flags & M_WAITOK) {
+ WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
+ "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
++ } else {
++ KFAIL_POINT_CODE(DEBUG_FP, uma_zalloc_arg, {
++ /*
++ * XXX hack. Setting the fail point to 0 (default)
++ * causes it to ignore malloc zones, nonzero causes it
++ * to inject failures for malloc zones regardless of
++ * the malloc black/white lists.
++ */
++ if (((zone->uz_flags & UMA_ZONE_MALLOC) == 0 ||
++ RETURN_VALUE != 0) &&
++ uma_dbg_nowait_fail_enabled_zalloc(
++ zone->uz_name)) {
++ /* XXX record call stack */
++ atomic_add_long(&zone->uz_fails, 1);
++ return NULL;
++ }
++ });
+ }
+
+ KASSERT(curthread->td_critnest == 0,
+diff --git a/sys/vm/uma_dbg.c b/sys/vm/uma_dbg.c
+index 3fbd29b..bed3130 100644
+--- a/sys/vm/uma_dbg.c
++++ b/sys/vm/uma_dbg.c
+@@ -42,6 +42,8 @@ __FBSDID("$FreeBSD$");
+ #include <sys/lock.h>
+ #include <sys/mutex.h>
+ #include <sys/malloc.h>
++#include <sys/rwlock.h>
++#include <sys/sysctl.h>
+
+ #include <vm/vm.h>
+ #include <vm/vm_object.h>
+@@ -292,4 +294,143 @@ uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
+ BIT_CLR_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
+ }
+
++/* XXX explain */
++struct rwlock g_uma_dbg_nowait_lock;
++RW_SYSINIT(uma_dbg_nowait, &g_uma_dbg_nowait_lock, "uma dbg nowait");
++
++#define NOWAIT_FAIL_LIST_BUFSIZE 4096
++char malloc_fail_blacklist[NOWAIT_FAIL_LIST_BUFSIZE] = "kobj";
++char malloc_fail_whitelist[NOWAIT_FAIL_LIST_BUFSIZE] = "";
++char zalloc_fail_blacklist[NOWAIT_FAIL_LIST_BUFSIZE] =
++ "BUF TRIE,ata_request,sackhole";
++char zalloc_fail_whitelist[NOWAIT_FAIL_LIST_BUFSIZE] = "";
++
++static bool
++str_in_list(const char *list, char delim, const char *str)
++{
++ const char *b, *e;
++ size_t blen, slen;
++
++ b = list;
++ slen = strlen(str);
++ for (;;) {
++ e = strchr(b, delim);
++ blen = e == NULL ? strlen(b) : e - b;
++ if (blen == slen && strncmp(b, str, slen) == 0)
++ return true;
++ if (e == NULL)
++ break;
++ b = e + 1;
++ }
++ return false;
++}
++
++static bool
++uma_dbg_nowait_fail_enabled_internal(const char *blacklist,
++ const char *whitelist, const char *name)
++{
++ bool fail;
++
++ /* Protect ourselves from the sysctl handlers. */
++ rw_rlock(&g_uma_dbg_nowait_lock);
++ if (whitelist[0] == '\0')
++ fail = !str_in_list(blacklist, ',', name);
++ else
++ fail = str_in_list(whitelist, ',', name);
++ rw_runlock(&g_uma_dbg_nowait_lock);
++
++ return fail;
++}
++
++bool
++uma_dbg_nowait_fail_enabled_malloc(const char *name)
++{
++ return uma_dbg_nowait_fail_enabled_internal(malloc_fail_blacklist,
++ malloc_fail_whitelist, name);
++}
++
++bool
++uma_dbg_nowait_fail_enabled_zalloc(const char *name)
++{
++ return uma_dbg_nowait_fail_enabled_internal(zalloc_fail_blacklist,
++ zalloc_fail_whitelist, name);
++}
++
++/*
++ * XXX provide SYSCTL_STRING_LOCKED / sysctl_string_locked_handler?
++ * This is basically just a different sysctl_string_handler. This one wraps
++ * the string manipulation in a lock and in a way that will not cause a sleep
++ * under that lock.
++ */
++static int
++sysctl_debug_mnowait_failure_list(SYSCTL_HANDLER_ARGS)
++{
++ char *newbuf = NULL;
++ int error, newlen;
++ bool have_lock = false;
++
++ if (req->newptr != NULL) {
++ newlen = req->newlen - req->newidx;
++ if (newlen >= arg2) {
++ error = EINVAL;
++ goto out;
++ }
++ newbuf = malloc(newlen, M_TEMP, M_WAITOK);
++ error = SYSCTL_IN(req, newbuf, newlen);
++ if (error != 0)
++ goto out;
++ }
++
++ error = sysctl_wire_old_buffer(req, arg2);
++ if (error != 0)
++ goto out;
++
++ rw_wlock(&g_uma_dbg_nowait_lock);
++ have_lock = true;
++
++ error = SYSCTL_OUT(req, arg1, strnlen(arg1, arg2 - 1) + 1);
++ if (error != 0)
++ goto out;
++
++ if (newbuf == NULL)
++ goto out;
++
++ bcopy(newbuf, arg1, newlen);
++ ((char *)arg1)[newlen] = '\0';
++ out:
++ if (have_lock)
++ rw_wunlock(&g_uma_dbg_nowait_lock);
++ free(newbuf, M_TEMP);
++ return error;
++}
++
++SYSCTL_NODE(_debug, OID_AUTO, mnowait_failure, CTLFLAG_RW, 0,
++ "Control of M_NOWAIT memory allocation failure injection.");
++
++SYSCTL_PROC(_debug_mnowait_failure, OID_AUTO, malloc_blacklist,
++ CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, malloc_fail_blacklist,
++ sizeof(malloc_fail_blacklist), sysctl_debug_mnowait_failure_list, "A",
++ "With debug.fail_point.malloc and with an empty whitelist, CSV list of "
++ "zones which remain unaffected.");
++
++SYSCTL_PROC(_debug_mnowait_failure, OID_AUTO, malloc_whitelist,
++ CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, malloc_fail_whitelist,
++ sizeof(malloc_fail_whitelist), sysctl_debug_mnowait_failure_list, "A",
++ "With debug.fail_point.malloc, CSV list of zones exclusively affected. "
++ "With an empty whitelist, all zones but those on the blacklist"
++ "are affected.");
++
++SYSCTL_PROC(_debug_mnowait_failure, OID_AUTO, zalloc_blacklist,
++ CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, zalloc_fail_blacklist,
++ sizeof(zalloc_fail_blacklist), sysctl_debug_mnowait_failure_list, "A",
++ "With debug.fail_point.uma_zalloc_arg and with an empty whitelist, CSV "
++ "list of zones which remain unaffected.");
++
++SYSCTL_PROC(_debug_mnowait_failure, OID_AUTO, zalloc_whitelist,
++ CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, zalloc_fail_whitelist,
++ sizeof(zalloc_fail_whitelist), sysctl_debug_mnowait_failure_list, "A",
++ "With debug.fail_point.uma_zalloc_arg, CSV list of zones exclusively "
++ "affected. With an empty whitelist, all zones but those on the blacklist"
++ "are affected.");
++
+ #endif /* INVARIANTS */
+diff --git a/sys/vm/uma_int.h b/sys/vm/uma_int.h
+index ad2a405..284747f 100644
+--- a/sys/vm/uma_int.h
++++ b/sys/vm/uma_int.h
+@@ -427,6 +427,9 @@ vsetslab(vm_offset_t va, uma_slab_t slab)
+ void *uma_small_alloc(uma_zone_t zone, vm_size_t bytes, uint8_t *pflag,
+ int wait);
+ void uma_small_free(void *mem, vm_size_t size, uint8_t flags);
++
++bool uma_dbg_nowait_fail_enabled_malloc(const char *name);
++bool uma_dbg_nowait_fail_enabled_zalloc(const char *name);
+ #endif /* _KERNEL */
+
+ #endif /* VM_UMA_INT_H */
diff --git a/tools/test/stress2/misc/umount.sh b/tools/test/stress2/misc/umount.sh
new file mode 100755
index 000000000000..8ec0351cda31
--- /dev/null
+++ b/tools/test/stress2/misc/umount.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2011 Peter Holm <pho@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 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.
+#
+
+# Test "umount"
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+D=$diskimage
+dd if=/dev/zero of=$D bs=1m count=110 status=none || exit 1
+
+mount | grep "$mntpoint" | grep md${mdstart}$part > /dev/null && umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=3m # Run tests for three minutes
+(cd ..; ./run.sh vfs.cfg)
+umount $mntpoint
+mdconfig -d -u $mdstart
+rm -f $D
diff --git a/tools/test/stress2/misc/umount2.sh b/tools/test/stress2/misc/umount2.sh
new file mode 100755
index 000000000000..d78f7a836f15
--- /dev/null
+++ b/tools/test/stress2/misc/umount2.sh
@@ -0,0 +1,90 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# # ps -l
+# UID PID PPID CPU PRI NI VSZ RSS MWCHAN STAT TT TIME COMMAND
+# 0 72280 1 0 20 0 5992 1768 mntref D 0- 0:03.20 umount /mnt
+
+# https://people.freebsd.org/~pho/stress/log/kostik990.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `swapinfo | wc -l` -eq 1 ] && exit 0
+
+. ../default.cfg
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 5g -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+size=$((`sysctl -n hw.physmem` / 1024 / 1024))
+[ $size -gt $((4 * 1024)) ] &&
+ echo "RAM should be capped to 4GB for this test."
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=5m
+export TESTPROGS="
+testcases/symlink/symlink
+testcases/openat/openat
+testcases/fts/fts
+testcases/lockf/lockf
+"
+
+export ftsINCARNATIONS=192
+export lockfINCARNATIONS=132
+export openatINCARNATIONS=218
+export symlinkINCARNATIONS=201
+
+export ftsLOAD=100
+export lockfLOAD=100
+export openatLOAD=100
+export symlinkLOAD=100
+
+export ftsHOG=1
+export lockfHOG=1
+export openatHOG=1
+export symlinkHOG=1
+
+su $testuser -c 'cd ..; ./testcases/run/run $TESTPROGS'
+
+sync
+umount $mntpoint &
+sleep 5
+kill -0 $! 2>/dev/null && { ps -lp$! | grep mntref && exit 1; }
+wait
+
+for i in `jot 6`; do
+ mount | grep -q "on $mntpoint " || break
+ umount $mntpoint && break || sleep 10
+done
+[ $i -eq 6 ] && exit 1
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/umount3.sh b/tools/test/stress2/misc/umount3.sh
new file mode 100755
index 000000000000..2ff0c8caeb56
--- /dev/null
+++ b/tools/test/stress2/misc/umount3.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Parallel unmount of the same mount point with one mounter thread and 4
+# threads doing unmount.
+
+# "panic: dounmount: invalid lock refcount in the drain path @
+# ../../../kern/vfs_mount.c:1409" seen in WiP code.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+parallel=4
+continue=/tmp/umount3.cont
+touch $continue
+
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 10m -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+for i in `jot $parallel`; do
+ while [ -f $continue ]; do umount $mntpoint; done 2>/dev/null &
+done
+
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 300 ]; do
+ mount | grep -q "on $mntpoint " ||
+ mount /dev/md${mdstart}$part $mntpoint
+done > /dev/null 2>&1
+rm $continue
+wait
+
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/umount4.sh b/tools/test/stress2/misc/umount4.sh
new file mode 100755
index 000000000000..e46f319959a0
--- /dev/null
+++ b/tools/test/stress2/misc/umount4.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Peter Holm
+#
+# 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 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.
+#
+
+# A "(cd /mnt; umount /mnt)" test scenario, which triggered random memory
+# corruptions.
+
+# Page fault seen: https://people.freebsd.org/~pho/stress/log/kostik1230.txt
+# First seen with r354204
+# Initial fix by r354367
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+mount | grep -q "on $mntpoint " && umount $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart || exit 1
+
+for opt in "-U" "-j"; do
+ echo "newfs $opt md$mdstart"
+ newfs $opt md$mdstart > /dev/null
+ mount /dev/md$mdstart $mntpoint || exit 1
+ pids=""
+ for i in `jot 3`; do
+ while true; do
+ dd if=/dev/zero of=$mntpoint/$i bs=1m \
+ count=`jot -r 1 1 10` status=none
+ done &
+ pids="$pids $!"
+ done
+
+ start=`date +%s`
+ while [ $((`date +%s` - start)) -lt 60 ]; do
+ (cd $mntpoint; umount $mntpoint)
+ done > /dev/null 2>&1
+ kill $pids
+ wait
+
+ while true; do
+ umount $mntpoint && break
+ sleep 1
+ done
+done
+mdconfig -d -u $mdstart
+exit 0
diff --git a/tools/test/stress2/misc/umountf.sh b/tools/test/stress2/misc/umountf.sh
new file mode 100755
index 000000000000..125eb336749c
--- /dev/null
+++ b/tools/test/stress2/misc/umountf.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2011 Peter Holm <pho@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 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.
+#
+
+# Test problems with "umount -f"
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+D=$diskimage
+dd if=/dev/zero of=$D bs=1m count=110 status=none || exit 1
+
+mount | grep "$mntpoint" | grep md${mdstart}$part > /dev/null &&
+ umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart || { rm $diskimage; exit 1; }
+bsdlabel -w md$mdstart auto
+newfs md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=2m
+cd ..; ./run.sh vfs.cfg > /dev/null 2>&1 &
+
+sleep 30
+
+umount -f $mntpoint
+mdconfig -d -u $mdstart
+rm -f $D
+
+s=0
+n=0
+while pkill -f "swap|mkdir|creat"; do
+ sleep 1
+ n=$((n + 1))
+ if [ $n -gt 60 ]; then
+ pgrep "swap|mkdir|creat"
+ s=1
+ break
+ fi
+done
+wait
+exit $s
diff --git a/tools/test/stress2/misc/umountf10.sh b/tools/test/stress2/misc/umountf10.sh
new file mode 100755
index 000000000000..6881a58a7e9b
--- /dev/null
+++ b/tools/test/stress2/misc/umountf10.sh
@@ -0,0 +1,133 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC Isilon
+# 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 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.
+#
+
+# Demonstrate deadlock
+# https://people.freebsd.org/~pho/stress/log/umountf10.txt
+
+# Test scenario suggestion by: kib
+# Fixed in r308618.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+cont=/tmp/umountf10.continue
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/umountf10.c
+mycc -o umountf10 -Wall -Wextra -O2 -g umountf10.c || exit 1
+rm -f umountf10.c
+cd $odir
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 512m -u $mdstart
+gpart create -s GPT md$mdstart > /dev/null || exit 1
+gpart add -t freebsd-ufs md$mdstart > /dev/null || exit 1
+newfs -n $newfs_flags md${mdstart}p1 > /dev/null || exit 1
+mount /dev/md${mdstart}p1 $mntpoint
+touch $mntpoint/marker $cont
+trap "rm -f $cont" EXIT INT
+
+daemon sh -c "(cd $odir/../testcases/swap; ./swap -t 4m -i 20)" > \
+ /dev/null 2>&1
+sleep 5
+
+for i in `jot 4`; do
+ /tmp/umountf10 $mntpoint &
+ pid="$pid $!"
+done
+
+for i in `jot 10`; do
+ while [ -e $cont ]; do procstat -f $pid > /dev/null 2>&1; done &
+ pid2="$pid2 $!"
+done
+
+while [ -e $cont ]; do find $mntpoint -ls > /dev/null 2>&1; done &
+tpid=$!
+
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 300 ]; do
+ umount -f $mntpoint 2>/dev/null &&
+ mount /dev/md${mdstart}p1 $mntpoint
+done
+while pgrep -q swap; do
+ pkill -9 swap
+done
+kill $pid $pid2 $tpid
+wait
+
+umount $mntpoint
+rm -f $mntpoint/file.* /tmp/umountf10
+mdconfig -d -u $mdstart
+
+exit 0
+EOF
+#include <sys/param.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(int argc, char *argv[])
+{
+ int fd;
+ char file[MAXPATHLEN + 1];
+ char marker[MAXPATHLEN + 1];
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <file path>", argv[0]);
+ exit(1);
+ }
+
+ alarm(600);
+ snprintf(file, sizeof(file), "%s/file.%06d", argv[1], getpid());
+ snprintf(marker, sizeof(marker), "%s/marker", argv[1]);
+ for (;;) {
+ if (access(marker, R_OK) == -1)
+ continue;
+ if ((fd = open(file, O_RDWR | O_CREAT | O_APPEND,
+ DEFFILEMODE)) == -1) {
+ if (errno != ENOENT && errno != EBUSY)
+ warn("open(%s)", file);
+ continue;
+ }
+ write(fd, "a", 1);
+ usleep(arc4random() % 400);
+ close(fd);
+ unlink(argv[1]);
+ }
+
+ return(0);
+}
diff --git a/tools/test/stress2/misc/umountf11.sh b/tools/test/stress2/misc/umountf11.sh
new file mode 100755
index 000000000000..b2a694037420
--- /dev/null
+++ b/tools/test/stress2/misc/umountf11.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# "panic: vm_map_entry_set_vnode_text: wrong object type, ..." seen:
+# https://people.freebsd.org/~pho/stress/log/kostik1205.txt
+# Fixed by r348701
+
+# Test scenario suggestion by kib@
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+mount -o size=1g -t tmpfs null $mntpoint || exit 1
+
+for i in `jot 10 1`; do
+ cp /bin/sleep $mntpoint/sleep$i
+ $mntpoint/sleep$i .2
+ rm $mntpoint/sleep$i
+done
+cp /bin/sleep $mntpoint/sleep
+$mntpoint/sleep 10 &
+umount -f $mntpoint
+wait
diff --git a/tools/test/stress2/misc/umountf12.sh b/tools/test/stress2/misc/umountf12.sh
new file mode 100755
index 000000000000..8a13f8fd480a
--- /dev/null
+++ b/tools/test/stress2/misc/umountf12.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# acquiring duplicate lock of same type: "vnode interlock"
+# 1st vnode interlock @ /usr/src/sys/fs/nullfs/null_vnops.c:342
+# 2nd vnode interlock @ kern/vfs_default.c:1116
+# seen.
+# Fixed by r348698
+
+# Test scenario suggestion by kib@
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+log=/tmp/umountf13.log
+tail -F -n 0 /var/log/messages > $log & lpid=$!
+set -e
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+mount -o size=1g -t tmpfs null $mntpoint
+mp2=$mntpoint$((mdstart + 1))
+mkdir -p $mp2
+mount | grep -q "on $mp2 " && umount -f $mp2
+
+mount -t nullfs $mntpoint $mp2
+set +e
+
+for i in `jot 10 1`; do
+ cp /bin/sleep $mp2/sleep$i
+ $mp2/sleep$i .2
+ rm $mp2/sleep$i
+done
+cp /bin/sleep $mp2/sleep
+$mp2/sleep 10 & spid=$!
+umount -f $mp2
+wait $spid
+grep -A2 "acquiring duplicate lock of same type" $log && s=1 || s=0
+kill $lpid
+rm $log
+wait
+
+umount $mntpoint
+exit $s
diff --git a/tools/test/stress2/misc/umountf2.sh b/tools/test/stress2/misc/umountf2.sh
new file mode 100755
index 000000000000..6dc62bcd947b
--- /dev/null
+++ b/tools/test/stress2/misc/umountf2.sh
@@ -0,0 +1,1273 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Test scenario by kris@freebsd.org
+
+# Test problems with "umount -f and fsx. Results in a "KDB: enter: watchdog timeout"
+
+# http://people.freebsd.org/~pho/stress/log/kostik745.txt
+# Fixed by r275743
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+sed '1,/^EOF/d' < $0 > /tmp/fsx.c
+mycc -o /tmp/fsx -O2 /tmp/fsx.c || exit 1
+rm -f /tmp/fsx.c
+
+D=$diskimage
+dd if=/dev/zero of=$D bs=1m count=1k status=none || exit 1
+
+mount | grep "$mntpoint" | grep md${mdstart}$part > /dev/null && umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs md${mdstart}$part > /dev/null 2>&1
+mount /dev/md${mdstart}$part $mntpoint
+sleep 5
+for i in `jot 100`; do
+ /tmp/fsx -S $i -q $mntpoint/xxx$i > /dev/null &
+done
+sleep 30
+umount -f $mntpoint &
+for i in `jot 30`; do
+ sleep 30
+ pgrep -q fsx || break
+done
+if pgrep -q fsx; then
+ echo FAIL
+ ps -l $!
+ pkill fsx
+fi
+sleep 5
+wait
+mdconfig -d -u $mdstart
+rm -f $D /tmp/fsx
+exit 0
+EOF
+/*
+ * Copyright (c) 1998-2001 Apple Computer, Inc. All rights reserved.
+ *
+ * @APPLE_LICENSE_HEADER_START@
+ *
+ * The contents of this file constitute Original Code as defined in and
+ * are subject to the Apple Public Source License Version 2.0 (the
+ * "License"). You may not use this file except in compliance with the
+ * License. Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ *
+ * This Original Code and all software distributed under the License are
+ * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * @APPLE_LICENSE_HEADER_END@
+ *
+ * File: fsx.c
+ * Author: Avadis Tevanian, Jr.
+ *
+ * File system exerciser.
+ *
+ * Rewrite and enhancements 1998-2001 Conrad Minshall -- conrad@mac.com
+ *
+ * Various features from Joe Sokol, Pat Dirks, and Clark Warner.
+ *
+ * Small changes to work under Linux -- davej@suse.de
+ *
+ * Sundry porting patches from Guy Harris 12/2001
+ *
+ * Checks for mmap last-page zero fill.
+ *
+ * Updated license to APSL 2.0, 2004/7/27 - Jordan Hubbard
+ *
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#ifdef _UWIN
+# include <sys/param.h>
+# include <limits.h>
+# include <time.h>
+# include <strings.h>
+#endif
+#include <fcntl.h>
+#include <sys/mman.h>
+#ifndef MAP_FILE
+# define MAP_FILE 0
+#endif
+#include <limits.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdarg.h>
+#include <errno.h>
+
+#define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
+
+/*
+ * A log entry is an operation and a bunch of arguments.
+ */
+
+struct log_entry {
+ int operation;
+ int args[3];
+};
+
+#define LOGSIZE 1000
+
+struct log_entry oplog[LOGSIZE]; /* the log */
+int logptr = 0; /* current position in log */
+int logcount = 0; /* total ops */
+
+/*
+ * Define operations
+ */
+
+#define OP_READ 1
+#define OP_WRITE 2
+#define OP_TRUNCATE 3
+#define OP_CLOSEOPEN 4
+#define OP_MAPREAD 5
+#define OP_MAPWRITE 6
+#define OP_SKIPPED 7
+#define OP_INVALIDATE 8
+
+int page_size;
+int page_mask;
+
+char *original_buf; /* a pointer to the original data */
+char *good_buf; /* a pointer to the correct data */
+char *temp_buf; /* a pointer to the current data */
+char *fname; /* name of our test file */
+int fd; /* fd for our test file */
+
+off_t file_size = 0;
+off_t biggest = 0;
+char state[256];
+unsigned long testcalls = 0; /* calls to function "test" */
+
+unsigned long simulatedopcount = 0; /* -b flag */
+int closeprob = 0; /* -c flag */
+int invlprob = 0; /* -i flag */
+int debug = 0; /* -d flag */
+unsigned long debugstart = 0; /* -D flag */
+unsigned long maxfilelen = 256 * 1024; /* -l flag */
+int sizechecks = 1; /* -n flag disables them */
+int maxoplen = 64 * 1024; /* -o flag */
+int quiet = 0; /* -q flag */
+unsigned long progressinterval = 0; /* -p flag */
+int readbdy = 1; /* -r flag */
+int style = 0; /* -s flag */
+int truncbdy = 1; /* -t flag */
+int writebdy = 1; /* -w flag */
+long monitorstart = -1; /* -m flag */
+long monitorend = -1; /* -m flag */
+int lite = 0; /* -L flag */
+long numops = -1; /* -N flag */
+int randomoplen = 1; /* -O flag disables it */
+int seed = 1; /* -S flag */
+int mapped_writes = 1; /* -W flag disables */
+int mapped_reads = 1; /* -R flag disables it */
+int mapped_msync = 1; /* -U flag disables */
+int fsxgoodfd = 0;
+FILE * fsxlogf = NULL;
+int badoff = -1;
+int closeopen = 0;
+int invl = 0;
+
+void
+vwarnc(code, fmt, ap)
+ int code;
+ const char *fmt;
+ va_list ap;
+{
+ fprintf(stderr, "fsx: ");
+ if (fmt != NULL) {
+ vfprintf(stderr, fmt, ap);
+ fprintf(stderr, ": ");
+ }
+ fprintf(stderr, "%s\n", strerror(code));
+}
+
+void
+warn(const char * fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarnc(errno, fmt, ap);
+ va_end(ap);
+}
+
+void
+prt(char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ vfprintf(stdout, fmt, args);
+ va_end(args);
+
+ if (fsxlogf) {
+ va_start(args, fmt);
+ vfprintf(fsxlogf, fmt, args);
+ va_end(args);
+ }
+}
+
+void
+prterr(char *prefix)
+{
+ prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
+}
+
+void
+do_log4(int operation, int arg0, int arg1, int arg2)
+{
+ struct log_entry *le;
+
+ le = &oplog[logptr];
+ le->operation = operation;
+ le->args[0] = arg0;
+ le->args[1] = arg1;
+ le->args[2] = arg2;
+ logptr++;
+ logcount++;
+ if (logptr >= LOGSIZE)
+ logptr = 0;
+}
+
+void
+log4(int operation, int arg0, int arg1, int arg2)
+{
+ do_log4(operation, arg0, arg1, arg2);
+ if (closeopen)
+ do_log4(OP_CLOSEOPEN, 0, 0, 0);
+ if (invl)
+ do_log4(OP_INVALIDATE, 0, 0, 0);
+}
+
+void
+logdump(void)
+{
+ struct log_entry *lp;
+ int i, count, down, opnum;
+
+ prt("LOG DUMP (%d total operations):\n", logcount);
+ if (logcount < LOGSIZE) {
+ i = 0;
+ count = logcount;
+ } else {
+ i = logptr;
+ count = LOGSIZE;
+ }
+
+ opnum = i + 1 + (logcount/LOGSIZE)*LOGSIZE;
+ for ( ; count > 0; count--) {
+ lp = &oplog[i];
+
+ if (lp->operation == OP_CLOSEOPEN ||
+ lp->operation == OP_INVALIDATE) {
+ switch (lp->operation) {
+ case OP_CLOSEOPEN:
+ prt("\t\tCLOSE/OPEN\n");
+ break;
+ case OP_INVALIDATE:
+ prt("\t\tMS_INVALIDATE\n");
+ break;
+ }
+ i++;
+ if (i == LOGSIZE)
+ i = 0;
+ continue;
+ }
+
+ prt("%d(%d mod 256): ", opnum, opnum%256);
+ switch (lp->operation) {
+ case OP_MAPREAD:
+ prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)",
+ lp->args[0], lp->args[0] + lp->args[1] - 1,
+ lp->args[1]);
+ if (badoff >= lp->args[0] && badoff <
+ lp->args[0] + lp->args[1])
+ prt("\t***RRRR***");
+ break;
+ case OP_MAPWRITE:
+ prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
+ lp->args[0], lp->args[0] + lp->args[1] - 1,
+ lp->args[1]);
+ if (badoff >= lp->args[0] && badoff <
+ lp->args[0] + lp->args[1])
+ prt("\t******WWWW");
+ break;
+ case OP_READ:
+ prt("READ\t0x%x thru 0x%x\t(0x%x bytes)",
+ lp->args[0], lp->args[0] + lp->args[1] - 1,
+ lp->args[1]);
+ if (badoff >= lp->args[0] &&
+ badoff < lp->args[0] + lp->args[1])
+ prt("\t***RRRR***");
+ break;
+ case OP_WRITE:
+ prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)",
+ lp->args[0], lp->args[0] + lp->args[1] - 1,
+ lp->args[1]);
+ if (lp->args[0] > lp->args[2])
+ prt(" HOLE");
+ else if (lp->args[0] + lp->args[1] > lp->args[2])
+ prt(" EXTEND");
+ if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
+ badoff < lp->args[0] + lp->args[1])
+ prt("\t***WWWW");
+ break;
+ case OP_TRUNCATE:
+ down = lp->args[0] < lp->args[1];
+ prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
+ down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
+ if (badoff >= lp->args[!down] &&
+ badoff < lp->args[!!down])
+ prt("\t******WWWW");
+ break;
+ case OP_SKIPPED:
+ prt("SKIPPED (no operation)");
+ break;
+ default:
+ prt("BOGUS LOG ENTRY (operation code = %d)!",
+ lp->operation);
+ }
+ prt("\n");
+ opnum++;
+ i++;
+ if (i == LOGSIZE)
+ i = 0;
+ }
+}
+
+void
+save_buffer(char *buffer, off_t bufferlength, int fd)
+{
+ off_t ret;
+ ssize_t byteswritten;
+
+ if (fd <= 0 || bufferlength == 0)
+ return;
+
+ if (bufferlength > SSIZE_MAX) {
+ prt("fsx flaw: overflow in save_buffer\n");
+ exit(67);
+ }
+ if (lite) {
+ off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
+ if (size_by_seek == (off_t)-1)
+ prterr("save_buffer: lseek eof");
+ else if (bufferlength > size_by_seek) {
+ warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
+ (unsigned long long)bufferlength);
+ bufferlength = size_by_seek;
+ }
+ }
+
+ ret = lseek(fd, (off_t)0, SEEK_SET);
+ if (ret == (off_t)-1)
+ prterr("save_buffer: lseek 0");
+
+ byteswritten = write(fd, buffer, (size_t)bufferlength);
+ if (byteswritten != bufferlength) {
+ if (byteswritten == -1)
+ prterr("save_buffer write");
+ else
+ warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
+ (unsigned)byteswritten,
+ (unsigned long long)bufferlength);
+ }
+}
+
+void
+report_failure(int status)
+{
+ logdump();
+
+ if (fsxgoodfd) {
+ if (good_buf) {
+ save_buffer(good_buf, file_size, fsxgoodfd);
+ prt("Correct content saved for comparison\n");
+ prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
+ fname, fname);
+ }
+ close(fsxgoodfd);
+ }
+ exit(status);
+}
+
+#define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
+ *(((unsigned char *)(cp)) + 1)))
+
+void
+check_buffers(unsigned offset, unsigned size)
+{
+ unsigned char c, t;
+ unsigned i = 0;
+ unsigned n = 0;
+ unsigned op = 0;
+ unsigned bad = 0;
+
+ if (memcmp(good_buf + offset, temp_buf, size) != 0) {
+ prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
+ offset, size);
+ prt("OFFSET\tGOOD\tBAD\tRANGE\n");
+ while (size > 0) {
+ c = good_buf[offset];
+ t = temp_buf[i];
+ if (c != t) {
+ if (n == 0) {
+ bad = short_at(&temp_buf[i]);
+ prt("0x%5x\t0x%04x\t0x%04x", offset,
+ short_at(&good_buf[offset]), bad);
+ op = temp_buf[offset & 1 ? i+1 : i];
+ }
+ n++;
+ badoff = offset;
+ }
+ offset++;
+ i++;
+ size--;
+ }
+ if (n) {
+ prt("\t0x%5x\n", n);
+ if (bad)
+ prt("operation# (mod 256) for the bad data may be %u\n", ((unsigned)op & 0xff));
+ else
+ prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n");
+ } else
+ prt("????????????????\n");
+ report_failure(110);
+ }
+}
+
+void
+check_size(void)
+{
+ struct stat statbuf;
+ off_t size_by_seek;
+
+ if (fstat(fd, &statbuf)) {
+ prterr("check_size: fstat");
+ statbuf.st_size = -1;
+ }
+ size_by_seek = lseek(fd, (off_t)0, SEEK_END);
+ if (file_size != statbuf.st_size || file_size != size_by_seek) {
+ prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
+ (unsigned long long)file_size,
+ (unsigned long long)statbuf.st_size,
+ (unsigned long long)size_by_seek);
+ report_failure(120);
+ }
+}
+
+void
+check_trunc_hack(void)
+{
+ struct stat statbuf;
+
+ ftruncate(fd, (off_t)0);
+ ftruncate(fd, (off_t)100000);
+ fstat(fd, &statbuf);
+ if (statbuf.st_size != (off_t)100000) {
+ prt("no extend on truncate! not posix!\n");
+ exit(130);
+ }
+ ftruncate(fd, (off_t)0);
+}
+
+void
+doread(unsigned offset, unsigned size)
+{
+ off_t ret;
+ unsigned iret;
+
+ offset -= offset % readbdy;
+ if (size == 0) {
+ if (!quiet && testcalls > simulatedopcount)
+ prt("skipping zero size read\n");
+ log4(OP_SKIPPED, OP_READ, offset, size);
+ return;
+ }
+ if (size + offset > file_size) {
+ if (!quiet && testcalls > simulatedopcount)
+ prt("skipping seek/read past end of file\n");
+ log4(OP_SKIPPED, OP_READ, offset, size);
+ return;
+ }
+
+ log4(OP_READ, offset, size, 0);
+
+ if (testcalls <= simulatedopcount)
+ return;
+
+ if (!quiet && ((progressinterval &&
+ testcalls % progressinterval == 0) ||
+ (debug &&
+ (monitorstart == -1 ||
+ (offset + size > monitorstart &&
+ (monitorend == -1 || offset <= monitorend))))))
+ prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
+ offset, offset + size - 1, size);
+ ret = lseek(fd, (off_t)offset, SEEK_SET);
+ if (ret == (off_t)-1) {
+ prterr("doread: lseek");
+ report_failure(140);
+ }
+ iret = read(fd, temp_buf, size);
+ if (iret != size) {
+ if (iret == -1)
+ prterr("doread: read");
+ else
+ prt("short read: 0x%x bytes instead of 0x%x\n",
+ iret, size);
+ report_failure(141);
+ }
+ check_buffers(offset, size);
+}
+
+void
+check_eofpage(char *s, unsigned offset, char *p, int size)
+{
+ uintptr_t last_page, should_be_zero;
+
+ if (offset + size <= (file_size & ~page_mask))
+ return;
+ /*
+ * we landed in the last page of the file
+ * test to make sure the VM system provided 0's
+ * beyond the true end of the file mapping
+ * (as required by mmap def in 1996 posix 1003.1)
+ */
+ last_page = ((uintptr_t)p + (offset & page_mask) + size) & ~page_mask;
+
+ for (should_be_zero = last_page + (file_size & page_mask);
+ should_be_zero < last_page + page_size;
+ should_be_zero++)
+ if (*(char *)should_be_zero) {
+ prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
+ s, file_size - 1, should_be_zero & page_mask,
+ short_at(should_be_zero));
+ report_failure(205);
+ }
+}
+
+void
+domapread(unsigned offset, unsigned size)
+{
+ unsigned pg_offset;
+ unsigned map_size;
+ char *p;
+
+ offset -= offset % readbdy;
+ if (size == 0) {
+ if (!quiet && testcalls > simulatedopcount)
+ prt("skipping zero size read\n");
+ log4(OP_SKIPPED, OP_MAPREAD, offset, size);
+ return;
+ }
+ if (size + offset > file_size) {
+ if (!quiet && testcalls > simulatedopcount)
+ prt("skipping seek/read past end of file\n");
+ log4(OP_SKIPPED, OP_MAPREAD, offset, size);
+ return;
+ }
+
+ log4(OP_MAPREAD, offset, size, 0);
+
+ if (testcalls <= simulatedopcount)
+ return;
+
+ if (!quiet && ((progressinterval &&
+ testcalls % progressinterval == 0) ||
+ (debug &&
+ (monitorstart == -1 ||
+ (offset + size > monitorstart &&
+ (monitorend == -1 || offset <= monitorend))))))
+ prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
+ offset, offset + size - 1, size);
+
+ pg_offset = offset & page_mask;
+ map_size = pg_offset + size;
+
+ if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
+ (off_t)(offset - pg_offset))) == (char *)-1) {
+ prterr("domapread: mmap");
+ report_failure(190);
+ }
+ memcpy(temp_buf, p + pg_offset, size);
+
+ check_eofpage("Read", offset, p, size);
+
+ if (munmap(p, map_size) != 0) {
+ prterr("domapread: munmap");
+ report_failure(191);
+ }
+
+ check_buffers(offset, size);
+}
+
+void
+gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
+{
+ while (size--) {
+ good_buf[offset] = testcalls % 256;
+ if (offset % 2)
+ good_buf[offset] += original_buf[offset];
+ offset++;
+ }
+}
+
+void
+dowrite(unsigned offset, unsigned size)
+{
+ off_t ret;
+ unsigned iret;
+
+ offset -= offset % writebdy;
+ if (size == 0) {
+ if (!quiet && testcalls > simulatedopcount)
+ prt("skipping zero size write\n");
+ log4(OP_SKIPPED, OP_WRITE, offset, size);
+ return;
+ }
+
+ log4(OP_WRITE, offset, size, file_size);
+
+ gendata(original_buf, good_buf, offset, size);
+ if (file_size < offset + size) {
+ if (file_size < offset)
+ memset(good_buf + file_size, '\0', offset - file_size);
+ file_size = offset + size;
+ if (lite) {
+ warn("Lite file size bug in fsx!");
+ report_failure(149);
+ }
+ }
+
+ if (testcalls <= simulatedopcount)
+ return;
+
+ if (!quiet && ((progressinterval &&
+ testcalls % progressinterval == 0) ||
+ (debug &&
+ (monitorstart == -1 ||
+ (offset + size > monitorstart &&
+ (monitorend == -1 || offset <= monitorend))))))
+ prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
+ offset, offset + size - 1, size);
+ ret = lseek(fd, (off_t)offset, SEEK_SET);
+ if (ret == (off_t)-1) {
+ prterr("dowrite: lseek");
+ report_failure(150);
+ }
+ iret = write(fd, good_buf + offset, size);
+ if (iret != size) {
+ if (iret == -1)
+ prterr("dowrite: write");
+ else
+ prt("short write: 0x%x bytes instead of 0x%x\n",
+ iret, size);
+ report_failure(151);
+ }
+}
+
+void
+domapwrite(unsigned offset, unsigned size)
+{
+ unsigned pg_offset;
+ unsigned map_size;
+ off_t cur_filesize;
+ char *p;
+
+ offset -= offset % writebdy;
+ if (size == 0) {
+ if (!quiet && testcalls > simulatedopcount)
+ prt("skipping zero size write\n");
+ log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
+ return;
+ }
+ cur_filesize = file_size;
+
+ log4(OP_MAPWRITE, offset, size, 0);
+
+ gendata(original_buf, good_buf, offset, size);
+ if (file_size < offset + size) {
+ if (file_size < offset)
+ memset(good_buf + file_size, '\0', offset - file_size);
+ file_size = offset + size;
+ if (lite) {
+ warn("Lite file size bug in fsx!");
+ report_failure(200);
+ }
+ }
+
+ if (testcalls <= simulatedopcount)
+ return;
+
+ if (!quiet && ((progressinterval &&
+ testcalls % progressinterval == 0) ||
+ (debug &&
+ (monitorstart == -1 ||
+ (offset + size > monitorstart &&
+ (monitorend == -1 || offset <= monitorend))))))
+ prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
+ offset, offset + size - 1, size);
+
+ if (file_size > cur_filesize) {
+ if (ftruncate(fd, file_size) == -1) {
+ prterr("domapwrite: ftruncate");
+ exit(201);
+ }
+ }
+ pg_offset = offset & page_mask;
+ map_size = pg_offset + size;
+
+ if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
+ MAP_FILE | MAP_SHARED, fd,
+ (off_t)(offset - pg_offset))) == MAP_FAILED) {
+ prterr("domapwrite: mmap");
+ report_failure(202);
+ }
+ memcpy(p + pg_offset, good_buf + offset, size);
+ if (mapped_msync && msync(p, map_size, MS_SYNC) != 0) {
+ prterr("domapwrite: msync");
+ report_failure(203);
+ }
+
+ check_eofpage("Write", offset, p, size);
+
+ if (munmap(p, map_size) != 0) {
+ prterr("domapwrite: munmap");
+ report_failure(204);
+ }
+}
+
+void
+dotruncate(unsigned size)
+{
+ int oldsize = file_size;
+
+ size -= size % truncbdy;
+ if (size > biggest) {
+ biggest = size;
+ if (!quiet && testcalls > simulatedopcount)
+ prt("truncating to largest ever: 0x%x\n", size);
+ }
+
+ log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
+
+ if (size > file_size)
+ memset(good_buf + file_size, '\0', size - file_size);
+ file_size = size;
+
+ if (testcalls <= simulatedopcount)
+ return;
+
+ if ((progressinterval && testcalls % progressinterval == 0) ||
+ (debug && (monitorstart == -1 || monitorend == -1 ||
+ size <= monitorend)))
+ prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
+ if (ftruncate(fd, (off_t)size) == -1) {
+ prt("ftruncate1: %x\n", size);
+ prterr("dotruncate: ftruncate");
+ report_failure(160);
+ }
+}
+
+void
+writefileimage()
+{
+ ssize_t iret;
+
+ if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
+ prterr("writefileimage: lseek");
+ report_failure(171);
+ }
+ iret = write(fd, good_buf, file_size);
+ if ((off_t)iret != file_size) {
+ if (iret == -1)
+ prterr("writefileimage: write");
+ else
+ prt("short write: 0x%x bytes instead of 0x%llx\n",
+ iret, (unsigned long long)file_size);
+ report_failure(172);
+ }
+ if (lite ? 0 : ftruncate(fd, file_size) == -1) {
+ prt("ftruncate2: %llx\n", (unsigned long long)file_size);
+ prterr("writefileimage: ftruncate");
+ report_failure(173);
+ }
+}
+
+void
+docloseopen(void)
+{
+ if (testcalls <= simulatedopcount)
+ return;
+
+ if (debug)
+ prt("%lu close/open\n", testcalls);
+ if (close(fd)) {
+ prterr("docloseopen: close");
+ report_failure(180);
+ }
+ fd = open(fname, O_RDWR, 0);
+ if (fd < 0) {
+ prterr("docloseopen: open");
+ report_failure(181);
+ }
+}
+
+void
+doinvl(void)
+{
+ char *p;
+
+ if (file_size == 0)
+ return;
+ if (testcalls <= simulatedopcount)
+ return;
+ if (debug)
+ prt("%lu msync(MS_INVALIDATE)\n", testcalls);
+
+ if ((p = (char *)mmap(0, file_size, PROT_READ | PROT_WRITE,
+ MAP_FILE | MAP_SHARED, fd, 0)) == MAP_FAILED) {
+ prterr("doinvl: mmap");
+ report_failure(205);
+ }
+
+ if (msync(p, 0, MS_SYNC | MS_INVALIDATE) != 0) {
+ prterr("doinvl: msync");
+ report_failure(206);
+ }
+
+ if (munmap(p, file_size) != 0) {
+ prterr("doinvl: munmap");
+ report_failure(207);
+ }
+}
+
+void
+test(void)
+{
+ unsigned long offset;
+ unsigned long size = maxoplen;
+ unsigned long rv = random();
+ unsigned long op = rv % (3 + !lite + mapped_writes);
+
+ /* turn off the map read if necessary */
+
+ if (op == 2 && !mapped_reads)
+ op = 0;
+
+ if (simulatedopcount > 0 && testcalls == simulatedopcount)
+ writefileimage();
+
+ testcalls++;
+
+ if (closeprob)
+ closeopen = (rv >> 3) < (1 << 28) / closeprob;
+ if (invlprob)
+ invl = (rv >> 3) < (1 << 28) / invlprob;
+
+ if (debugstart > 0 && testcalls >= debugstart)
+ debug = 1;
+
+ if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
+ prt("%lu...\n", testcalls);
+
+ /*
+ * READ: op = 0
+ * WRITE: op = 1
+ * MAPREAD: op = 2
+ * TRUNCATE: op = 3
+ * MAPWRITE: op = 3 or 4
+ */
+ if (lite ? 0 : op == 3 && style == 0) /* vanilla truncate? */
+ dotruncate(random() % maxfilelen);
+ else {
+ if (randomoplen)
+ size = random() % (maxoplen+1);
+ if (lite ? 0 : op == 3)
+ dotruncate(size);
+ else {
+ offset = random();
+ if (op == 1 || op == (lite ? 3 : 4)) {
+ offset %= maxfilelen;
+ if (offset + size > maxfilelen)
+ size = maxfilelen - offset;
+ if (op != 1)
+ domapwrite(offset, size);
+ else
+ dowrite(offset, size);
+ } else {
+ if (file_size)
+ offset %= file_size;
+ else
+ offset = 0;
+ if (offset + size > file_size)
+ size = file_size - offset;
+ if (op != 0)
+ domapread(offset, size);
+ else
+ doread(offset, size);
+ }
+ }
+ }
+ if (sizechecks && testcalls > simulatedopcount)
+ check_size();
+ if (invl)
+ doinvl();
+ if (closeopen)
+ docloseopen();
+}
+
+void
+cleanup(sig)
+ int sig;
+{
+ if (sig)
+ prt("signal %d\n", sig);
+ prt("testcalls = %lu\n", testcalls);
+ exit(sig);
+}
+
+void
+usage(void)
+{
+ fprintf(stdout, "usage: %s",
+ "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] fname\n\
+ -b opnum: beginning operation number (default 1)\n\
+ -c P: 1 in P chance of file close+open at each op (default infinity)\n\
+ -d: debug output for all operations\n\
+ -i P: 1 in P chance of calling msync(MS_INVALIDATE) (default infinity)\n\
+ -l flen: the upper bound on file size (default 262144)\n\
+ -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
+ -n: no verifications of file size\n\
+ -o oplen: the upper bound on operation size (default 65536)\n\
+ -p progressinterval: debug output at specified operation interval\n\
+ -q: quieter operation\n\
+ -r readbdy: 4096 would make reads page aligned (default 1)\n\
+ -s style: 1 gives smaller truncates (default 0)\n\
+ -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
+ -w writebdy: 4096 would make writes page aligned (default 1)\n\
+ -D startingop: debug output starting at specified operation\n\
+ -L: fsxLite - no file creations & no file size changes\n\
+ -N numops: total # operations to do (default infinity)\n\
+ -O: use oplen (see -o flag) for every op (default random)\n\
+ -P dirpath: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
+ -S seed: for random # generator (default 1) 0 gets timestamp\n\
+ -W: mapped write operations DISabled\n\
+ -R: mapped read operations DISabled)\n\
+ -U: msync after mapped write operations DISabled\n\
+ fname: this filename is REQUIRED (no default)\n");
+ exit(90);
+}
+
+int
+getnum(char *s, char **e)
+{
+ int ret = -1;
+
+ *e = (char *) 0;
+ ret = strtol(s, e, 0);
+ if (*e)
+ switch (**e) {
+ case 'b':
+ case 'B':
+ ret *= 512;
+ *e = *e + 1;
+ break;
+ case 'k':
+ case 'K':
+ ret *= 1024;
+ *e = *e + 1;
+ break;
+ case 'm':
+ case 'M':
+ ret *= 1024*1024;
+ *e = *e + 1;
+ break;
+ case 'w':
+ case 'W':
+ ret *= 4;
+ *e = *e + 1;
+ break;
+ }
+ return (ret);
+}
+
+int
+main(int argc, char **argv)
+{
+ int i, ch;
+ char *endp;
+ char goodfile[1024];
+ char logfile[1024];
+
+ goodfile[0] = 0;
+ logfile[0] = 0;
+
+ page_size = getpagesize();
+ page_mask = page_size - 1;
+
+ setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
+
+ while ((ch = getopt(argc, argv,
+ "b:c:di:l:m:no:p:qr:s:t:w:D:LN:OP:RS:UW")) != -1)
+ switch (ch) {
+ case 'b':
+ simulatedopcount = getnum(optarg, &endp);
+ if (!quiet)
+ fprintf(stdout, "Will begin at operation %ld\n",
+ simulatedopcount);
+ if (simulatedopcount == 0)
+ usage();
+ simulatedopcount -= 1;
+ break;
+ case 'c':
+ closeprob = getnum(optarg, &endp);
+ if (!quiet)
+ fprintf(stdout,
+ "Chance of close/open is 1 in %d\n",
+ closeprob);
+ if (closeprob <= 0)
+ usage();
+ break;
+ case 'd':
+ debug = 1;
+ break;
+ case 'i':
+ invlprob = getnum(optarg, &endp);
+ if (!quiet)
+ fprintf(stdout,
+ "Chance of MS_INVALIDATE is 1 in %d\n",
+ invlprob);
+ if (invlprob <= 0)
+ usage();
+ break;
+ case 'l':
+ maxfilelen = getnum(optarg, &endp);
+ if (maxfilelen <= 0)
+ usage();
+ break;
+ case 'm':
+ monitorstart = getnum(optarg, &endp);
+ if (monitorstart < 0)
+ usage();
+ if (!endp || *endp++ != ':')
+ usage();
+ monitorend = getnum(endp, &endp);
+ if (monitorend < 0)
+ usage();
+ if (monitorend == 0)
+ monitorend = -1; /* aka infinity */
+ debug = 1;
+ case 'n':
+ sizechecks = 0;
+ break;
+ case 'o':
+ maxoplen = getnum(optarg, &endp);
+ if (maxoplen <= 0)
+ usage();
+ break;
+ case 'p':
+ progressinterval = getnum(optarg, &endp);
+// if (progressinterval < 0)
+// usage();
+ break;
+ case 'q':
+ quiet = 1;
+ break;
+ case 'r':
+ readbdy = getnum(optarg, &endp);
+ if (readbdy <= 0)
+ usage();
+ break;
+ case 's':
+ style = getnum(optarg, &endp);
+ if (style < 0 || style > 1)
+ usage();
+ break;
+ case 't':
+ truncbdy = getnum(optarg, &endp);
+ if (truncbdy <= 0)
+ usage();
+ break;
+ case 'w':
+ writebdy = getnum(optarg, &endp);
+ if (writebdy <= 0)
+ usage();
+ break;
+ case 'D':
+ debugstart = getnum(optarg, &endp);
+ if (debugstart < 1)
+ usage();
+ break;
+ case 'L':
+ lite = 1;
+ break;
+ case 'N':
+ numops = getnum(optarg, &endp);
+ if (numops < 0)
+ usage();
+ break;
+ case 'O':
+ randomoplen = 0;
+ break;
+ case 'P':
+ strncpy(goodfile, optarg, sizeof(goodfile));
+ strcat(goodfile, "/");
+ strncpy(logfile, optarg, sizeof(logfile));
+ strcat(logfile, "/");
+ break;
+ case 'R':
+ mapped_reads = 0;
+ break;
+ case 'S':
+ seed = getnum(optarg, &endp);
+ if (seed == 0)
+ seed = time(0) % 10000;
+ if (!quiet)
+ fprintf(stdout, "Seed set to %d\n", seed);
+ if (seed < 0)
+ usage();
+ break;
+ case 'W':
+ mapped_writes = 0;
+ if (!quiet)
+ fprintf(stdout, "mapped writes DISABLED\n");
+ break;
+ case 'U':
+ mapped_msync = 0;
+ if (!quiet)
+ fprintf(stdout, "mapped msync DISABLED\n");
+ break;
+
+ default:
+ usage();
+ /* NOTREACHED */
+ }
+ argc -= optind;
+ argv += optind;
+ if (argc != 1)
+ usage();
+ fname = argv[0];
+
+ signal(SIGHUP, cleanup);
+ signal(SIGINT, cleanup);
+ signal(SIGPIPE, cleanup);
+ signal(SIGALRM, cleanup);
+ signal(SIGTERM, cleanup);
+ signal(SIGXCPU, cleanup);
+ signal(SIGXFSZ, cleanup);
+ signal(SIGVTALRM, cleanup);
+ signal(SIGUSR1, cleanup);
+ signal(SIGUSR2, cleanup);
+
+ initstate(seed, state, 256);
+ setstate(state);
+ fd = open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666);
+ if (fd < 0) {
+ prterr(fname);
+ exit(91);
+ }
+ strncat(goodfile, fname, 256);
+ strcat (goodfile, ".fsxgood");
+ fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
+ if (fsxgoodfd < 0) {
+ prterr(goodfile);
+ exit(92);
+ }
+ strncat(logfile, fname, 256);
+ strcat (logfile, ".fsxlog");
+ fsxlogf = fopen(logfile, "w");
+ if (fsxlogf == NULL) {
+ prterr(logfile);
+ exit(93);
+ }
+ if (lite) {
+ off_t ret;
+ file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
+ if (file_size == (off_t)-1) {
+ prterr(fname);
+ warn("main: lseek eof");
+ exit(94);
+ }
+ ret = lseek(fd, (off_t)0, SEEK_SET);
+ if (ret == (off_t)-1) {
+ prterr(fname);
+ warn("main: lseek 0");
+ exit(95);
+ }
+ }
+ original_buf = (char *) malloc(maxfilelen);
+ for (i = 0; i < maxfilelen; i++)
+ original_buf[i] = random() % 256;
+ good_buf = (char *) malloc(maxfilelen);
+ memset(good_buf, '\0', maxfilelen);
+ temp_buf = (char *) malloc(maxoplen);
+ memset(temp_buf, '\0', maxoplen);
+ if (lite) { /* zero entire existing file */
+ ssize_t written;
+
+ written = write(fd, good_buf, (size_t)maxfilelen);
+ if (written != maxfilelen) {
+ if (written == -1) {
+ prterr(fname);
+ warn("main: error on write");
+ } else
+ warn("main: short write, 0x%x bytes instead of 0x%x\n",
+ (unsigned)written, maxfilelen);
+ exit(98);
+ }
+ } else
+ check_trunc_hack();
+
+ while (numops == -1 || numops--)
+ test();
+
+ if (close(fd)) {
+ prterr("close");
+ report_failure(99);
+ }
+ prt("All operations completed A-OK!\n");
+
+ exit(0);
+ return 0;
+}
diff --git a/tools/test/stress2/misc/umountf3.sh b/tools/test/stress2/misc/umountf3.sh
new file mode 100755
index 000000000000..dd165fc88d2f
--- /dev/null
+++ b/tools/test/stress2/misc/umountf3.sh
@@ -0,0 +1,118 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Demonstrate livelock with "umount -f" seen both with UFS and MSDOS
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+D=$diskimage
+dd if=/dev/zero of=$D bs=1m count=1k status=none || exit 1
+
+odir=`pwd`
+
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > umountf3.c
+mycc -o umountf3 -Wall umountf3.c
+rm -f umountf3.c
+cd $odir
+
+mount | grep "$mntpoint" | grep md$mdstart > /dev/null && umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs md${mdstart}$part > /dev/null 2>&1
+mount /dev/md${mdstart}$part $mntpoint
+export RUNDIR=$mntpoint/stressX
+for i in `jot 25`; do
+ (cd /$mntpoint; /tmp/umountf3) &
+done
+sleep $((4 * 60))
+echo "umount -f $mntpoint"
+umount -f $mntpoint
+while pkill umountf3; do :; done
+wait
+mdconfig -d -u $mdstart
+rm -f $D /tmp/umountf3
+exit
+EOF
+
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static unsigned long size = 1024 * 1024 * 2;
+
+int
+main(int argc, char **argv)
+{
+ int buf[1024], index, to, n;
+ int fd;
+ char file[128];
+
+ sprintf(file,"p%06d", getpid());
+ if ((fd = open(file, O_CREAT | O_RDWR, 0666)) == -1)
+ err(1, "creat(%s)", file);
+
+ to = sizeof(buf);
+ for (;;) {
+ index = 0;
+ while (index < size) {
+ if (index + to > size)
+ to = size - index;
+ index += to;
+ if (write(fd, buf, to) != to)
+ err(1, "write(%s), %s:%d", file, __FILE__, __LINE__);
+ }
+
+ if (lseek(fd, 0, 0) == -1)
+ err(1, "lseek");
+ index = 0;
+ while (index < size) {
+ if (index + to > size)
+ to = size - index;
+ if ((n = read(fd, buf, to)) != to)
+ err(1, "rw read(%d, %d, %d). %s.%d", n, to, index, __FILE__, __LINE__);
+ index += to;
+ }
+ if (lseek(fd, 0, 0) == -1)
+ err(1, "lseek");
+ }
+
+ if (close(fd) == -1)
+ err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s), %s:%d", file, __FILE__, __LINE__);
+ return (0);
+}
diff --git a/tools/test/stress2/misc/umountf4.sh b/tools/test/stress2/misc/umountf4.sh
new file mode 100755
index 000000000000..e9f802e1daf0
--- /dev/null
+++ b/tools/test/stress2/misc/umountf4.sh
@@ -0,0 +1,85 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Found: Fatal trap 12: page fault while in kernel mode
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mounts=15 # Number of parallel scripts
+mdstart=$mdstart # Use md unit numbers from this point
+D=$diskimage
+
+if [ $# -eq 0 ]; then
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ [ ! -d ${mntpoint}$m ] && mkdir ${mntpoint}$m
+ mount | grep "$mntpoint" | grep -q md$m && umount ${mntpoint}$m
+ mdconfig -l | grep -q md$m && mdconfig -d -u $m
+
+ dd if=/dev/zero of=$D$m bs=1m count=10 status=none
+ mdconfig -a -t vnode -f $D$m -u $m ||
+ { rm -f $D$m; exit 1; }
+ bsdlabel -w md$m auto
+ newfs md${m}$part > /dev/null 2>&1
+ done
+
+ # start the parallel tests
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ ./$0 $m &
+ ./$0 find &
+ done
+ wait
+
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ mdconfig -d -u $m
+ rm -f $D$m
+ done
+
+else
+ if [ $1 = find ]; then
+ for i in `jot 100`; do
+ find ${mntpoint}* -type f > /dev/null 2>&1
+ done
+ else
+
+ # The test: Parallel mount and unmounts
+ for i in `jot 100`; do
+ m=$1
+ opt=`[ $(( m % 2 )) -eq 0 ] && echo -f`
+ mount $opt /dev/md${m}$part ${mntpoint}$m
+ cp -r /usr/include/machine/a* ${mntpoint}$m
+ while mount | grep -qw $mntpoint$m; do
+ umount -f ${mntpoint}$m > /dev/null 2>&1
+ done
+ done
+ fi
+fi
diff --git a/tools/test/stress2/misc/umountf5.sh b/tools/test/stress2/misc/umountf5.sh
new file mode 100755
index 000000000000..f5ebb253134a
--- /dev/null
+++ b/tools/test/stress2/misc/umountf5.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2013 Peter Holm <pho@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 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.
+#
+
+# Test with "umount -f" and SU
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+D=$diskimage
+dd if=/dev/zero of=$D$m bs=1m count=10 status=none
+
+mount | grep "$mntpoint" | grep md${mdstart}$part > /dev/null &&
+ umount $mntpoint
+mdconfig -l | grep md$mdstart > /dev/null && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $D -u $mdstart || { rm -f $diskimage; exit 1; }
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null 2>&1
+mount /dev/md${mdstart}$part $mntpoint
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=2m
+cd ..; ./run.sh vfs.cfg > /dev/null 2>&1 &
+
+sleep 30
+
+umount -f $mntpoint
+mdconfig -d -u $mdstart
+rm -f $D
+
+s=0
+n=0
+while pkill -f "swap|mkdir|creat"; do
+ sleep 1
+ n=$((n + 1))
+ if [ $n -gt 60 ]; then
+ pgrep "swap|mkdir|creat"
+ s=1
+ break
+ fi
+done
+wait
+exit $s
diff --git a/tools/test/stress2/misc/umountf6.sh b/tools/test/stress2/misc/umountf6.sh
new file mode 100755
index 000000000000..b42a8071aaa8
--- /dev/null
+++ b/tools/test/stress2/misc/umountf6.sh
@@ -0,0 +1,85 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2013 Peter Holm <pho@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 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.
+#
+
+# Variation of umountf4.sh; FS with "soft updates"
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mounts=15 # Number of parallel scripts
+mdstart=$mdstart # Use md unit numbers from this point
+D=$diskimage
+
+if [ $# -eq 0 ]; then
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ [ ! -d ${mntpoint}$m ] && mkdir ${mntpoint}$m
+ mount | grep "$mntpoint" | grep -q md$m && umount ${mntpoint}$m
+ mdconfig -l | grep -q md$m && mdconfig -d -u $m
+
+ dd if=/dev/zero of=$D$m bs=1m count=10 status=none
+ mdconfig -a -t vnode -f $D$m -u $m ||
+ { rm -f $D$m; exit 1; }
+ bsdlabel -w md$m auto
+ newfs $newfs_flags md${m}$part > /dev/null 2>&1
+ done
+
+ # start the parallel tests
+ for i in `jot $mounts`; do
+ m=$((i + mdstart - 1))
+ ./$0 $m &
+ ./$0 find &
+ done
+ wait
+
+ for i in `jot $mounts`; do
+ m=$((i + mdstart - 1))
+ mdconfig -d -u $m
+ rm -f $D$m
+ done
+
+else
+ if [ $1 = find ]; then
+ for i in `jot 100`; do
+ find ${mntpoint}* -type f > /dev/null 2>&1
+ done
+ else
+
+ # The test: Parallel mount and unmounts
+ for i in `jot 100`; do
+ m=$1
+ opt=`[ $(( m % 2 )) -eq 0 ] && echo -f`
+ mount $opt /dev/md${m}$part ${mntpoint}$m
+ cp -r /usr/include/machine/a* ${mntpoint}$m
+ while mount | grep -qw $mntpoint$m; do
+ umount -f ${mntpoint}$m > /dev/null 2>&1
+ done
+ done
+ fi
+fi
diff --git a/tools/test/stress2/misc/umountf7.sh b/tools/test/stress2/misc/umountf7.sh
new file mode 100755
index 000000000000..d4a52b4ce8f2
--- /dev/null
+++ b/tools/test/stress2/misc/umountf7.sh
@@ -0,0 +1,168 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# "panic: handle_written_inodeblock: live inodedep 0xcc731200" seen.
+# http://people.freebsd.org/~pho/stress/log/umountf7.txt
+# https://people.freebsd.org/~pho/stress/log/kostik824.txt
+# Problem only seen with SU+J.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/umountf7.c
+mycc -o umountf7 -Wall -Wextra umountf7.c -lpthread || exit 1
+rm -f umountf7.c
+cd $odir
+
+mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+mdconfig -a -t swap -s 3g -u $mdstart || exit 1
+bsdlabel -w md$mdstart auto
+[ "$newfs_flags" = "-U" ] && opt="-j"
+newfs $opt md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+
+daemon sh -c '(cd ../testcases/swap; ./swap -t 2m -i 4)'
+parallel=4
+for j in `jot $parallel`; do
+ [ -d $mntpoint/$j ] || mkdir $mntpoint/$j
+done
+for j in `jot $parallel`; do
+ (cd $mntpoint/$j; /tmp/umountf7 100000) &
+done
+sleep 30
+umount -f $mntpoint
+pkill umountf7
+wait
+while pkill -9 swap; do
+ :
+done
+find $mntpoint -type f
+
+while mount | grep "on $mntpoint " | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f /tmp/umountf7
+exit
+
+EOF
+#include <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+pid_t pid;
+volatile int n, n2;
+int mx;
+
+void *
+cr(void *arg __unused)
+{
+ char file[80];
+ int fd, i;
+
+ for (i = 0; i < mx; i++) {
+ snprintf(file, sizeof(file), "f%06d.%06d", pid, i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
+ err(1, "open(%s)", file);
+ close(fd);
+ n++;
+ }
+ return (0);
+
+}
+
+void *
+mv(void *arg __unused)
+{
+ char from[80], to[80];
+ int i;
+
+ for (i = 0; i < mx; i++) {
+ while (n == -1 || i > n)
+ pthread_yield();
+ snprintf(from, sizeof(from), "f%06d.%06d", pid, i);
+ snprintf(to , sizeof(to ), "g%06d.%06d", pid, i);
+ if (rename(from, to) == -1)
+ warn("rename(%s, %s)", from, to);
+ n2++;
+ }
+
+ return (0);
+}
+
+void *
+rm(void *arg __unused)
+{
+ char file[80];
+ int i;
+
+ for (i = 0; i < mx; i++) {
+ while (n2 == -1 || i > n2)
+ pthread_yield();
+ snprintf(file, sizeof(file), "g%06d.%06d", pid, i);
+ if (unlink(file) == -1)
+ warn("unlink(%s)", file);
+ }
+
+ return (0);
+}
+
+int
+main(int argc, char **argv)
+{
+ pthread_t rp[3];
+ int e, i;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <number of files>", argv[0]);
+ mx = atoi(argv[1]);
+ n = n2 = -1;
+ pid = getpid();
+
+ if ((e = pthread_create(&rp[0], NULL, cr, NULL)) != 0)
+ errc(1, e, "pthread_create");
+ usleep(arc4random() % 1000);
+ if ((e = pthread_create(&rp[1], NULL, mv, NULL)) != 0)
+ errc(1, e, "pthread_mv");
+ usleep(arc4random() % 1000);
+ if ((e = pthread_create(&rp[2], NULL, rm, NULL)) != 0)
+ errc(1, e, "pthread_rm");
+
+ for (i = 0; i < 3; i++)
+ pthread_join(rp[i], NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/umountf8.sh b/tools/test/stress2/misc/umountf8.sh
new file mode 100755
index 000000000000..1de15f3daed9
--- /dev/null
+++ b/tools/test/stress2/misc/umountf8.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 EMC Corp.
+# 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 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.
+#
+
+# Hang seen:
+# http://people.freebsd.org/~pho/stress/log/kostik707.txt
+# Fixed by r270203.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+./marcus.sh > /dev/null 2>&1 &
+sleep 30
+umount -f $mntpoint &
+sleep 120
+../tools/killall.sh
+wait
diff --git a/tools/test/stress2/misc/umountf9.sh b/tools/test/stress2/misc/umountf9.sh
new file mode 100755
index 000000000000..0a2c99445152
--- /dev/null
+++ b/tools/test/stress2/misc/umountf9.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC
+# 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 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.
+#
+
+# "umount -f" test scenario.
+# "panic: handle_written_inodeblock: live inodedep" seen.
+# https://people.freebsd.org/~pho/stress/log/umountf9.txt
+
+. ../default.cfg
+
+../misc/mkfifo2c.sh > /dev/null 2>&1 &
+sleep 60
+kill $!
+wait
+
+while mount | grep -q "on $mntpoint "; do
+ umount -f $mntpoint || sleep 1
+done
+../tools/killall.sh
+./cleanup.sh
+exit
diff --git a/tools/test/stress2/misc/umtx_suspend.sh b/tools/test/stress2/misc/umtx_suspend.sh
new file mode 100755
index 000000000000..aacc17b2d097
--- /dev/null
+++ b/tools/test/stress2/misc/umtx_suspend.sh
@@ -0,0 +1,88 @@
+#!/bin/sh
+
+# Test scenario from
+# Bug 192918 - [patch] A thread will spin if a signal interrupts umtxq_sleep_pi.
+# by eric@vangyzen.net
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > umtx_suspend.c
+mycc -o umtx_suspend -Wall -Wextra -O0 -g umtx_suspend.c \
+ -lpthread || exit 1
+rm -f umtx_suspend.c
+
+/tmp/umtx_suspend
+
+rm -f /tmp/umtx_suspend
+exit 0
+EOF
+// cc -lpthread -o umtx_suspend umtx_suspend.c
+//
+// failure: a thread spins around "umtxpi" the kernel, ignoring signals
+// success: the process exits
+
+#include <sys/cdefs.h>
+
+#include <err.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <stdio.h>
+#include <unistd.h>
+
+pthread_mutex_t the_mutex;
+
+void *
+spinning_thread_func(void *arg __unused)
+{
+ int error;
+
+ error = pthread_mutex_lock(&the_mutex);
+ if (error)
+ errc(1, error, "%s: pthread_mutex_lock", __func__);
+
+ return (NULL);
+}
+
+int
+main(int argc __unused, char *argv[] __unused)
+{
+ int error;
+ pthread_t spinning_thread;
+ pthread_mutexattr_t the_mutex_attr;
+
+ error = pthread_mutexattr_init(&the_mutex_attr);
+ if (error)
+ errc(1, error, "pthread_mutexattr_init");
+
+ error = pthread_mutexattr_setprotocol(&the_mutex_attr, PTHREAD_PRIO_INHERIT);
+ if (error)
+ errc(1, error, "pthread_mutexattr_setprotocol");
+
+ error = pthread_mutex_init(&the_mutex, &the_mutex_attr);
+ if (error)
+ errc(1, error, "pthread_mutex_init");
+
+ error = pthread_mutex_lock(&the_mutex);
+ if (error)
+ errc(1, error, "pthread_mutex_lock");
+
+ error = pthread_create(&spinning_thread, NULL, spinning_thread_func, NULL);
+ if (error)
+ errc(1, error, "pthread_create");
+
+ // Wait for the spinning_thread to call pthread_mutex_lock(3)
+ // and enter the kernel.
+ (void) sleep(1);
+
+ error = pthread_suspend_np(spinning_thread);
+ if (error)
+ errc(1, error, "pthread_suspend_np");
+
+ // The spinning_thread should be spinning in the kernel.
+ // This thread should be blocked in pthread_suspend_np(3).
+ fputs("This test failed to reproduce the bug.\n", stderr);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/union.sh b/tools/test/stress2/misc/union.sh
new file mode 100755
index 000000000000..cef2e4c69868
--- /dev/null
+++ b/tools/test/stress2/misc/union.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Simple union test scenario
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+u1=$mdstart
+u2=$((u1 + 1))
+
+mount | grep -q /dev/md${u2}$part && umount -f /dev/md${u2}$part
+mount | grep -q /dev/md${u1}$part && umount -f /dev/md${u1}$part
+mdconfig -l | grep -q md$u2 && mdconfig -d -u $u2
+mdconfig -l | grep -q md$u1 && mdconfig -d -u $u1
+
+mdconfig -s 256m -u $u1
+bsdlabel -w md$u1 auto
+newfs md${u1}$part > /dev/null
+
+mdconfig -s 256m -u $u2
+bsdlabel -w md$u2 auto
+newfs md${u2}$part > /dev/null
+
+mount -o ro /dev/md${u1}$part $mntpoint
+mount -o union /dev/md${u2}$part $mntpoint
+
+export RUNDIR=$mntpoint/stressX
+export runRUNTIME=10m
+(cd ..; ./run.sh marcus.cfg) > /dev/null
+
+umount /dev/md${u2}$part
+umount /dev/md${u1}$part
+
+mount | grep -q /dev/md${u2}$part && umount -f /dev/md${u2}$part
+mount | grep -q /dev/md${u1}$part && umount -f /dev/md${u1}$part
+
+mdconfig -d -u $u2
+mdconfig -d -u $u1
diff --git a/tools/test/stress2/misc/unionfs.sh b/tools/test/stress2/misc/unionfs.sh
new file mode 100755
index 000000000000..dbf3ba0fbc2e
--- /dev/null
+++ b/tools/test/stress2/misc/unionfs.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# "insmntque: non-locked vp: 0xd2462e10 is not exclusive locked ..." seen.
+
+. ../default.cfg
+
+truncate -s 256M $diskimage
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $diskimage -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+mount -t unionfs -o noatime $mntpoint /tmp
+export RUNDIR=/tmp/stressX
+export runRUNTIME=10m # Run tests for 10 minutes
+(cd ..; ./run.sh disk.cfg)
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f $diskimage
diff --git a/tools/test/stress2/misc/unionfs2.sh b/tools/test/stress2/misc/unionfs2.sh
new file mode 100755
index 000000000000..7af786ae9ce4
--- /dev/null
+++ b/tools/test/stress2/misc/unionfs2.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008-2011 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+saved=`sysctl vfs.lookup_shared | awk '{print $NF}'`
+for i in 1 0; do
+ sysctl vfs.lookup_shared=$i
+ [ -d /var/tmp/unionfs ] || mkdir -p /var/tmp/unionfs
+ mount_unionfs /var/tmp/unionfs /tmp/stressX
+
+ export RUNDIR=/var/tmp/unionfs/stressX
+ export runRUNTIME=10m # Run tests for 10 minutes
+ (cd ..; ./run.sh disk.cfg)
+ false
+ while mount | grep -q /unionfs; do
+ umount /tmp/stressX > /dev/null 2>&1
+ done
+ rm -rf /var/tmp/unionfs
+done
+sysctl vfs.lookup_shared=$saved
diff --git a/tools/test/stress2/misc/unionfs3.sh b/tools/test/stress2/misc/unionfs3.sh
new file mode 100755
index 000000000000..9b1a43344447
--- /dev/null
+++ b/tools/test/stress2/misc/unionfs3.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+# Test with interchanged arguments to mount_unionfs
+# Causes page fault in vfs_mount.c:1975
+
+. ../default.cfg
+
+truncate -s 256M $diskimage
+
+mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t vnode -f $diskimage -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+mount -t unionfs -o noatime /tmp $mntpoint
+umount -f /tmp # panic
+mount /tmp
+while mount | grep $mntpoint | grep -q /dev/md; do
+ umount $mntpoint || sleep 1
+done
+mdconfig -d -u $mdstart
+rm -f $diskimage
diff --git a/tools/test/stress2/misc/unix_socket.sh b/tools/test/stress2/misc/unix_socket.sh
new file mode 100755
index 000000000000..a5486dd7d4f0
--- /dev/null
+++ b/tools/test/stress2/misc/unix_socket.sh
@@ -0,0 +1,161 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# "panic: mutex unp not owned at ../../../kern/uipc_usrreq.c:879" seen:
+# https://people.freebsd.org/~pho/stress/log/mmacy018.txt
+# Fixed by r334756.
+
+. ../default.cfg
+
+cd /tmp
+cat > unix_socket.c <<EOF
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <errno.h>
+#include <netdb.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define SOCK_FILE "/tmp/unix_socket.socket"
+
+static int
+client(void) {
+ struct sockaddr_un addr;
+ int fd, len;
+ char buff[8192];
+
+ if ((fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0)
+ err(1, "socket");
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sun_family = AF_UNIX;
+ strcpy(addr.sun_path, SOCK_FILE);
+ unlink(SOCK_FILE);
+ if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
+ err(1, "bind");
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sun_family = AF_UNIX;
+ strcpy(addr.sun_path, SOCK_FILE);
+ if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
+ err(1, "connect");
+
+ strcpy (buff, "1234567890");
+ if (send(fd, buff, strlen(buff)+1, 0) == -1)
+ err(1, "send");
+ printf ("sent i1234567890\n");
+
+ if ((len = recv(fd, buff, 8192, 0)) < 0)
+ err(1, "recv");
+ printf ("receive %d %s\n", len, buff);
+
+ if (fd >= 0) {
+ close(fd);
+ }
+ unlink (SOCK_FILE);
+
+ return(0);
+}
+
+static int
+server() {
+ struct sockaddr_un addr;
+ struct sockaddr_un from;
+ socklen_t fromlen = sizeof(from);
+ int fd, len, ret;
+ char buff[8192];
+
+ unlink(SOCK_FILE);
+ if ((fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0)
+ err(1, "socket");
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sun_family = AF_UNIX;
+ strcpy(addr.sun_path, SOCK_FILE);
+ unlink(SOCK_FILE);
+ if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
+ err(1, "bind");
+
+ alarm(2);
+ while ((len = recvfrom(fd, buff, 8192, 0, (struct sockaddr *)&from,
+ &fromlen)) > 0) {
+ printf ("recvfrom: %s\n", buff);
+ strcpy (buff, "transmit good!");
+ ret = sendto(fd, buff, strlen(buff)+1, 0,
+ (struct sockaddr *)&from, fromlen);
+ if (ret < 0) {
+ perror("sendto");
+ break;
+ }
+ }
+
+ if (fd != -1)
+ close(fd);
+
+ return (0);
+}
+
+int
+main(void)
+{
+ pid_t pid;
+
+ if ((pid = fork()) == -1)
+ err(1, "fork");
+
+ if (pid == 0) {
+ server();
+ _exit(0);
+ }
+ sleep(2);
+ client();
+
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid");
+
+ return (0);
+}
+EOF
+
+cc -o unix_socket -Wall -Wextra -O2 -g unix_socket.c || exit
+rm unix_socket.c
+
+./unix_socket > /dev/null
+
+rm unix_socket
+exit 0
diff --git a/tools/test/stress2/misc/unix_socket_detach.sh b/tools/test/stress2/misc/unix_socket_detach.sh
new file mode 100755
index 000000000000..66670daf5639
--- /dev/null
+++ b/tools/test/stress2/misc/unix_socket_detach.sh
@@ -0,0 +1,124 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2016 Mark Johnston <markj@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Reproduce problem reported as PR 211531.
+# https://reviews.freebsd.org/D7398
+
+# "panic: __rw_wlock_hard: recursing but non-recursive rw unp_link_rwlock @
+# ../../../kern/uipc_usrreq.c:655" seen:
+# https://people.freebsd.org/~pho/stress/log/unix_socket_detach.txt
+# Fixed in r303855.
+
+. ../default.cfg
+
+cd /tmp
+cat > unix_socket_detach.c <<EOF
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static void
+handler(int i __unused) {
+ _exit(0);
+}
+
+int
+main(void)
+{
+ struct sockaddr_un sun;
+ char *file;
+ pid_t pid;
+ int sd, flags, one;
+
+ file = "unix_socket_detach.socket";
+ memset(&sun, 0, sizeof(sun));
+ sun.sun_family = AF_LOCAL;
+ sun.sun_len = sizeof(sun);
+ snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", file);
+
+ signal(SIGALRM, handler);
+ pid = fork();
+ if (pid < 0)
+ err(1, "fork");
+ if (pid == 0) {
+ alarm(300);
+ for (;;) {
+ sd = socket(PF_LOCAL, SOCK_STREAM, 0);
+ if (sd < 0)
+ err(1, "socket");
+ one = 1;
+ if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &one,
+ sizeof(one)) < 0)
+ err(1, "setsockopt");
+ if (bind(sd, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
+ close(sd);
+ continue;
+ }
+ if (listen(sd, 10) != 0)
+ err(1, "listen");
+ usleep(random() % 10);
+ (void)close(sd);
+ (void)unlink(file);
+ }
+ } else {
+ alarm(300);
+ for (;;) {
+ sd = socket(PF_LOCAL, SOCK_STREAM, 0);
+ if (sd < 0)
+ err(1, "socket");
+ if ((flags = fcntl(sd, F_GETFL, 0)) < 0)
+ err(1, "fcntl(F_GETFL)");
+ flags |= O_NONBLOCK;
+ if (fcntl(sd, F_SETFL, flags) < 0)
+ err(1, "fcntl(F_SETFL)");
+ (void)connect(sd, (struct sockaddr *)&sun, sizeof(sun));
+ usleep(random() % 10);
+ (void)close(sd);
+ }
+ }
+ return (0);
+}
+EOF
+
+mycc -o unix_socket_detach -Wall -Wextra -O2 -g unix_socket_detach.c || exit 1
+
+rm -f unix_socket_detach.socket
+/tmp/unix_socket_detach
+s=$?
+
+rm -f unix_socket_detach.c unix_socket_detach unix_socket_detach.socket
+exit $s
diff --git a/tools/test/stress2/misc/unix_socket_detach2.sh b/tools/test/stress2/misc/unix_socket_detach2.sh
new file mode 100755
index 000000000000..dd25dbd36303
--- /dev/null
+++ b/tools/test/stress2/misc/unix_socket_detach2.sh
@@ -0,0 +1,157 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Threaded version of unix_socket_detach.sh by
+# Mark Johnston <markj@FreeBSD.org>
+
+# "panic: __rw_wlock_hard: recursing but non-recursive rw unp_link_rwlock @
+# ../../../kern/uipc_usrreq.c:655" seen.
+# Fixed in r303855.
+
+. ../default.cfg
+
+cd /tmp
+cat > unix_socket_detach2.c <<EOF
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static sig_atomic_t done_testing;
+static struct sockaddr_un sun;
+static long success;
+static char *file;
+
+static void
+handler(int i __unused) {
+ done_testing = 1;
+}
+
+static void *
+t1(void *data __unused)
+{
+ int one, sd;
+
+ while (done_testing == 0) {
+ sd = socket(PF_LOCAL, SOCK_STREAM, 0);
+ if (sd < 0)
+ err(1, "socket");
+ one = 1;
+ if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &one,
+ sizeof(one)) < 0)
+ err(1, "setsockopt");
+ if (bind(sd, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
+ close(sd);
+ continue;
+ }
+ if (listen(sd, 10) != 0)
+ err(1, "listen");
+ usleep(random() % 10);
+ success++;
+ (void)close(sd);
+ (void)unlink(file);
+ }
+
+ return (NULL);
+}
+
+static void *
+t2(void *data __unused)
+{
+ int flags, sd;
+
+ while (done_testing == 0) {
+ sd = socket(PF_LOCAL, SOCK_STREAM, 0);
+ if (sd < 0)
+ err(1, "socket");
+ if ((flags = fcntl(sd, F_GETFL, 0)) < 0)
+ err(1, "fcntl(F_GETFL)");
+ flags |= O_NONBLOCK;
+ if (fcntl(sd, F_SETFL, flags) < 0)
+ err(1, "fcntl(F_SETFL)");
+ (void)connect(sd, (struct sockaddr *)&sun, sizeof(sun));
+ usleep(random() % 10);
+ (void)close(sd);
+ }
+
+ return (NULL);
+}
+
+int
+main(void)
+{
+ pthread_t tid[2];
+ int r;
+
+ file = "unix_socket_detach2.socket";
+ memset(&sun, 0, sizeof(sun));
+ sun.sun_family = AF_LOCAL;
+ sun.sun_len = sizeof(sun);
+ snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", file);
+
+ signal(SIGALRM, handler);
+ alarm(300);
+
+ if ((r = pthread_create(&tid[0], NULL, t1, NULL)) != 0)
+ errc(1, r, "pthread_create");
+ if ((r = pthread_create(&tid[1], NULL, t2, NULL)) != 0)
+ errc(1, r, "pthread_create");
+
+ if ((r = pthread_join(tid[0], NULL)) != 0)
+ errc(1, r, "pthread_join");
+ if ((r = pthread_join(tid[1], NULL)) != 0)
+ errc(1, r, "pthread_join");
+
+ if (success < 100) {
+ fprintf(stderr, "FAIL with only %ld\n", success);
+ return (1);
+ }
+
+ return (0);
+}
+EOF
+
+mycc -o unix_socket_detach2 -Wall -Wextra -O2 -g unix_socket_detach2.c \
+ -lpthread || exit 1
+
+rm -f unix_socket_detach2.socket
+/tmp/unix_socket_detach2
+s=$?
+
+rm -f unix_socket_detach2.c unix_socket_detach2 unix_socket_detach2.socket
+exit $s
diff --git a/tools/test/stress2/misc/unlink.sh b/tools/test/stress2/misc/unlink.sh
new file mode 100755
index 000000000000..b764555db0cc
--- /dev/null
+++ b/tools/test/stress2/misc/unlink.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+# File-backed shared memory performance
+# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=222356
+
+# For unlinked files, do not msync(2) or sync on inactivation.
+# https://reviews.freebsd.org/D12411
+
+# Original test scenario by: tijl@FreeBSD.org
+
+# Fixed by r323768
+
+. ../default.cfg
+
+cat > /tmp/unlink.c <<EOF
+#include <sys/mman.h>
+#include <sys/time.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+main(void) {
+ struct timeval diff, start, stop;
+ size_t sz;
+ void *base;
+ uint64_t usec;
+ int fd;
+
+ sz = 128 * 1024 * 1024;
+ fd = open("nosync.data", O_RDWR | O_CREAT, 0600);
+ if (fd == -1)
+ err(1, "open");
+ if (unlink("nosync.data") == -1)
+ err(1, "unlink");
+ if (ftruncate(fd, sz) == -1)
+ err(1, "ftruncate");
+ base = mmap(NULL, sz, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_NOSYNC, fd, 0);
+ if (base == MAP_FAILED)
+ err(1, "mmap");
+ memset(base, '0', sz);
+ if (munmap(base, sz) == -1)
+ err(1, "unmap");
+ gettimeofday(&start, NULL);
+ close(fd);
+ gettimeofday(&stop, NULL);
+ timersub(&stop, &start, &diff);
+ usec = ((uint64_t)1000000 * diff.tv_sec + diff.tv_usec);
+ if (usec > 500000) {
+ fprintf(stdout, "%.3f seconds elapsed\n",
+ (double)usec / 1000000);
+ return (0); /* Ignore error for now */
+ } else
+ return(0);
+}
+EOF
+
+mycc -o /tmp/unlink -Wall -Wextra -O2 -g /tmp/unlink.c || exit 1
+rm /tmp/unlink.c
+
+cd $RUNDIR
+/tmp/unlink; s=$?
+
+rm /tmp/unlink
+exit $s
diff --git a/tools/test/stress2/misc/vfork.sh b/tools/test/stress2/misc/vfork.sh
new file mode 100755
index 000000000000..20db95c2b9be
--- /dev/null
+++ b/tools/test/stress2/misc/vfork.sh
@@ -0,0 +1,131 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Regression test for r246484.
+
+# "panic: failed to set signal flags for ast p ... fl 4" seen.
+# Fixed in r302999.
+
+. ../default.cfg
+
+cd /tmp
+cat > vfork1.c <<- EOF
+#include <err.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ pid_t pid;
+
+ fprintf(stderr, "%d\n", getpid());
+ if ((pid = vfork()) == 0) {
+#if 0
+ if (ptrace(PT_TRACE_ME, 0, 0, 0) == -1)
+ err(1, "PT_TRACEME");
+#endif
+ sleep(30);
+ _exit(0);
+ }
+ if (pid == -1)
+ err(1, "vfork");
+
+ return (0);
+}
+EOF
+mycc -o vfork1 -Wall -Wextra -g vfork1.c
+rm vfork1.c
+
+cat > vfork2.c <<- EOF
+#include <sys/types.h>
+#include <err.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/ptrace.h>
+#include <sys/resource.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+int
+main(int argc, char **argv)
+{
+ pid_t pid, rpid;
+ struct rusage ru;
+ int status;
+
+ if (argc != 2)
+ errx(1, "Usage: %s <pid>", argv[0]);
+ pid = atoi(argv[1]);
+
+ if (pid == -1)
+ err(1, "fork()");
+ if (ptrace(PT_ATTACH, pid, NULL, 0) == -1)
+ err(1, "ptrace(%d) attach", pid);
+ if (wait(NULL) == -1)
+ err(1, "wait");
+ bzero(&ru, sizeof(ru));
+ usleep(2000);
+ if ((rpid = wait4(-1, &status, WNOHANG, &ru)) == -1) {
+ err(0, "OK wait4");
+ }
+ if (rpid == 0) {
+// fprintf(stderr, "No rusage info.\n");
+ if (ptrace(PT_DETACH, pid, NULL, 0) == -1)
+ err(1, "ptrace(%d) detach", pid);
+ if (wait(&status) == -1)
+ err(1, "wait");
+ } else {
+ fprintf(stderr, "FAIL Got unexpected rusage.\n");
+ if (ru.ru_utime.tv_sec != 0)
+ fprintf(stderr, "FAIL tv_sec\n");
+ }
+ if (status != 0x4000)
+ fprintf(stderr, "FAIL Child exit status 0x%x\n", status);
+
+ return (0);
+}
+EOF
+mycc -o vfork2 -Wall -Wextra -g vfork2.c
+rm vfork2.c
+
+./vfork1 &
+sleep .2
+childpid=`ps -lx | grep -v grep | grep vfork1 |
+ tail -1 | grep nanslp | awk '{print $2}'`
+# Seen before fix:
+# failed to set signal flags properly for ast()
+./vfork2 $childpid
+s=$?
+
+rm -f vfork1 vfork2
+exit $s
diff --git a/tools/test/stress2/misc/vm_fault_dontneed.sh b/tools/test/stress2/misc/vm_fault_dontneed.sh
new file mode 100755
index 000000000000..112bd97e59c7
--- /dev/null
+++ b/tools/test/stress2/misc/vm_fault_dontneed.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Verify that vm_fault_dontneed() is called during sequential access of a
+# mapped file on a file system.
+
+# Test scenario description by alc@
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+dtrace -n 'dtrace:::BEGIN { exit(0); }' > /dev/null 2>&1 || exit 0
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/vm_fault_dontneed.c
+mycc -o vm_fault_dontneed -Wall -Wextra -O0 -g vm_fault_dontneed.c || exit 1
+rm -f vm_fault_dontneed.c
+cd $odir
+
+size=$((`sysctl -n hw.physmem` / 1024 / 1024))
+[ $size -gt $((2 * 1024)) ] &&
+ { echo "RAM must be capped to 2GB for this test."
+ rm /tmp/vm_fault_dontneed; exit 0; }
+need=2048
+have=`df -k $(dirname $diskimage) | tail -1 | awk '{print int($4 / 1024)}'`
+[ $need -gt $have ] && need=$((have - 1))
+dd if=/dev/zero of=$diskimage bs=1m count=$need status=none
+
+log=/tmp/dtrace.$$
+trap "rm -f $log" EXIT INT
+dtrace -w -n '::*vm_fault_dontneed:entry { @[execname] = count(); }' \
+-c "/tmp/vm_fault_dontneed $diskimage" > $log 2>&1
+
+count=`grep vm_fault_dontneed $log | awk 'NF == 2 {print $NF}'`
+[ -z "$count" ] && count=0
+[ $count -lt 1000 ] &&
+ { echo "vm_fault_dontneed count = $count"; s=1; cat $log; }
+
+rm -rf /tmp/vm_fault_dontneed $diskimage
+exit $s
+
+EOF
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int
+main(int argc __unused, char **argv)
+{
+ struct stat fs;
+ off_t i;
+ char *ptr;
+ int accum, fd;
+
+ fd = open(argv[1], O_RDONLY);
+ if (fd == -1)
+ err(1, "open");
+ if (fstat(fd, &fs) == -1 || !S_ISREG(fs.st_mode))
+ err(1, "fstat");
+ ptr = mmap(0, (size_t)fs.st_size, PROT_READ /*| PROT_EXEC*/, MAP_PRIVATE, fd, 0);
+ if (ptr == MAP_FAILED)
+ err(1, "mmap");
+ accum = 0;
+ for (i = 0; i < fs.st_size; i++)
+ accum += ptr[i];
+ if (accum != 0) /* just to trick the optimizer */
+ printf("accum: %d\nptr: %p\nsize: %ld\n", accum, ptr, (long)fs.st_size);
+}
diff --git a/tools/test/stress2/misc/vm_map.sh b/tools/test/stress2/misc/vm_map.sh
new file mode 100755
index 000000000000..c28beb464022
--- /dev/null
+++ b/tools/test/stress2/misc/vm_map.sh
@@ -0,0 +1,117 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Test scenario based on alc@'s suggestion for D19826
+# No problems seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/vm_map.c
+mycc -o vm_map -Wall -Wextra -O2 vm_map.c || exit 1
+rm -f vm_map.c
+
+pages=$((`sysctl -n hw.usermem` / `sysctl -n hw.pagesize`))
+[ `sysctl -n vm.swap_total` -eq 0 ] &&
+ pages=$((pages / 10 * 8))
+/tmp/vm_map $pages
+
+rm -f /tmp/vm_map
+exit $s
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static long n;
+
+#define RUNTIME (2 * 60)
+#define INCARNATIONS 32
+
+int
+test(void)
+{
+ size_t len;
+ time_t start;
+ void *a;
+ char **c;
+ int i;
+
+ a = NULL;
+ c = calloc(n, sizeof(char *));
+ len = PAGE_SIZE;
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME) {
+ for (i = 0; i < n; i++) {
+ if ((a = mmap((void *)a, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+ c[i] = a;
+ *c[i] = 1;
+ a += PAGE_SIZE;
+ }
+ for (i = 0; i < n; i++) {
+ if (munmap(c[i], PAGE_SIZE) == -1)
+ err(1, "munmap(%p)", c[i]);
+ }
+ a = NULL;
+ }
+
+ _exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int i;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage %s <pages>\n", argv[0]);
+ exit(1);
+ }
+ n = atol(argv[1]) / INCARNATIONS;
+ for (i = 0; i < INCARNATIONS; i++)
+ if (fork() == 0)
+ test();
+
+ for (i = 0; i < INCARNATIONS; i++)
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/vm_reserv_populate.sh b/tools/test/stress2/misc/vm_reserv_populate.sh
new file mode 100755
index 000000000000..2ccf9f8a09e5
--- /dev/null
+++ b/tools/test/stress2/misc/vm_reserv_populate.sh
@@ -0,0 +1,278 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Bug 198163 - Kernel panic in vm_reserv_populate()
+# Test scenario by: ikosarev@accesssoftek.com
+# http://people.freebsd.org/~pho/stress/log/kostik771.txt
+# Fixed by r280238
+
+. ../default.cfg
+
+uname -a | egrep -q "i386|amd64" || exit 0
+odir=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $odir/$0 > vm_reserv_populate.cc
+rm -f /tmp/vm_reserv_populate
+mycc -o vm_reserv_populate -Wall -Wextra -g -O2 vm_reserv_populate.cc ||
+ exit 1
+rm -f vm_reserv_populate.cc
+
+(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -h -l 100 > /dev/null) &
+./vm_reserv_populate
+while pgrep -q swap; do
+ pkill -9 swap
+done
+rm vm_reserv_populate
+exit 0
+EOF
+#include <pthread.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/time.h>
+#include <signal.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <string.h>
+#include <assert.h>
+
+#include <sys/syscall.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#define INLINE inline
+#define NOINLINE __attribute__((noinline))
+#define SYSCALL(name) SYS_ ## name
+#define internal_syscall __syscall
+
+typedef unsigned char u8;
+typedef unsigned int u32;
+typedef unsigned long long u64;
+typedef unsigned long uptr;
+
+struct atomic_uint32_t {
+ typedef u32 Type;
+ volatile Type val_dont_use;
+};
+
+struct atomic_uintptr_t {
+ typedef uptr Type;
+ volatile Type val_dont_use;
+};
+
+uptr internal_sched_yield() {
+ return internal_syscall(SYSCALL(sched_yield));
+}
+
+enum memory_order {
+ memory_order_relaxed = 1 << 0,
+ memory_order_consume = 1 << 1,
+ memory_order_acquire = 1 << 2,
+ memory_order_release = 1 << 3,
+ memory_order_acq_rel = 1 << 4,
+ memory_order_seq_cst = 1 << 5
+};
+
+INLINE void proc_yield(int cnt) {
+ __asm__ __volatile__("" ::: "memory");
+ for (int i = 0; i < cnt; i++)
+ __asm__ __volatile__("pause");
+ __asm__ __volatile__("" ::: "memory");
+}
+
+template<typename T>
+NOINLINE typename T::Type atomic_load(
+ const volatile T *a, memory_order mo) {
+ assert(mo & (memory_order_relaxed | memory_order_consume
+ | memory_order_acquire | memory_order_seq_cst));
+ assert(!((uptr)a % sizeof(*a)));
+ typename T::Type v;
+
+ if (sizeof(*a) < 8 || sizeof(void*) == 8) {
+ // Assume that aligned loads are atomic.
+ if (mo == memory_order_relaxed) {
+ v = a->val_dont_use;
+ } else if (mo == memory_order_consume) {
+ // Assume that processor respects data dependencies
+ // (and that compiler won't break them).
+ __asm__ __volatile__("" ::: "memory");
+ v = a->val_dont_use;
+ __asm__ __volatile__("" ::: "memory");
+ } else if (mo == memory_order_acquire) {
+ __asm__ __volatile__("" ::: "memory");
+ v = a->val_dont_use;
+ // On x86 loads are implicitly acquire.
+ __asm__ __volatile__("" ::: "memory");
+ } else { // seq_cst
+ // On x86 plain MOV is enough for seq_cst store.
+ __asm__ __volatile__("" ::: "memory");
+ v = a->val_dont_use;
+ __asm__ __volatile__("" ::: "memory");
+ }
+ } else {
+ // 64-bit load on 32-bit platform.
+ __asm__ __volatile__(
+ "movq %1, %%mm0;" // Use mmx reg for 64-bit atomic moves
+ "movq %%mm0, %0;" // (ptr could be read-only)
+ "emms;" // Empty mmx state/Reset FP regs
+ : "=m" (v)
+ : "m" (a->val_dont_use)
+ : // mark the FP stack and mmx registers as clobbered
+ "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)",
+#ifdef __MMX__
+ "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
+#endif // #ifdef __MMX__
+ "memory");
+ }
+ return v;
+}
+
+template<typename T>
+INLINE void atomic_store(volatile T *a, typename T::Type v, memory_order mo) {
+ assert(mo & (memory_order_relaxed | memory_order_release
+ | memory_order_seq_cst));
+ assert(!((uptr)a % sizeof(*a)));
+
+ if (sizeof(*a) < 8 || sizeof(void*) == 8) {
+ // Assume that aligned loads are atomic.
+ if (mo == memory_order_relaxed) {
+ a->val_dont_use = v;
+ } else if (mo == memory_order_release) {
+ // On x86 stores are implicitly release.
+ __asm__ __volatile__("" ::: "memory");
+ a->val_dont_use = v;
+ __asm__ __volatile__("" ::: "memory");
+ } else { // seq_cst
+ // On x86 stores are implicitly release.
+ __asm__ __volatile__("" ::: "memory");
+ a->val_dont_use = v;
+ __sync_synchronize();
+ }
+ } else {
+ // 64-bit store on 32-bit platform.
+ __asm__ __volatile__(
+ "movq %1, %%mm0;" // Use mmx reg for 64-bit atomic moves
+ "movq %%mm0, %0;"
+ "emms;" // Empty mmx state/Reset FP regs
+ : "=m" (a->val_dont_use)
+ : "m" (v)
+ : // mark the FP stack and mmx registers as clobbered
+ "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)",
+#ifdef __MMX__
+ "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
+#endif // #ifdef __MMX__
+ "memory");
+ if (mo == memory_order_seq_cst)
+ __sync_synchronize();
+ }
+}
+
+template<typename T>
+INLINE bool atomic_compare_exchange_strong(volatile T *a,
+ typename T::Type *cmp,
+ typename T::Type xchg,
+ memory_order mo __unused) {
+ typedef typename T::Type Type;
+ Type cmpv = *cmp;
+ Type prev = __sync_val_compare_and_swap(&a->val_dont_use, cmpv, xchg);
+ if (prev == cmpv)
+ return true;
+ *cmp = prev;
+ return false;
+}
+
+template<typename T>
+INLINE bool atomic_compare_exchange_weak(volatile T *a,
+ typename T::Type *cmp,
+ typename T::Type xchg,
+ memory_order mo) {
+ return atomic_compare_exchange_strong(a, cmp, xchg, mo);
+}
+
+const u32 kTabSizeLog = 20;
+const int kTabSize = 1 << kTabSizeLog;
+
+static atomic_uintptr_t tab[kTabSize];
+
+int x_fork(void) {
+ for (int i = 0; i < kTabSize; ++i) {
+ atomic_uintptr_t *p = &tab[i];
+ for (int j = 0;; j++) {
+ uptr cmp = atomic_load(p, memory_order_relaxed);
+ if ((cmp & 1) == 0 &&
+ atomic_compare_exchange_weak(p, &cmp, cmp | 1, memory_order_acquire))
+ break;
+ if (j < 10)
+ proc_yield(10);
+ else
+ internal_sched_yield();
+ }
+ }
+
+ int pid = fork();
+
+ for (int i = 0; i < kTabSize; ++i) {
+ atomic_uintptr_t *p = &tab[i];
+ uptr s = atomic_load(p, memory_order_relaxed);
+ atomic_store(p, (s & ~1UL), memory_order_release);
+ }
+
+ return pid;
+}
+
+void test() {
+ pid_t pid = x_fork();
+ if (pid) {
+ pid_t p;
+ while ((p = wait(NULL)) == -1) { }
+ }
+}
+
+int main() {
+ const int kChildren = 1000;
+ for (int i = 0; i < kChildren; ++i) {
+ pid_t pid = x_fork();
+ if (!pid) {
+ test();
+ return 0;
+ }
+ }
+
+ sleep(5);
+
+ for (int i = 0; i < kChildren; ++i) {
+ pid_t p;
+ while ((p = wait(NULL)) == -1) { }
+ }
+
+ return 0;
+}
diff --git a/tools/test/stress2/misc/vmio.sh b/tools/test/stress2/misc/vmio.sh
new file mode 100755
index 000000000000..79ad9cf241d4
--- /dev/null
+++ b/tools/test/stress2/misc/vmio.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Out of swap seen with no swap disk:
+# dd 'if=/dev/zero' 'of=/var/tmp/diskimage.1' 'bs=1m' 'count=3427'
+# pid 598 (ntpd), uid 0, was killed: out of swap space
+# pid 656 (sendmail), uid 0, was killed: out of swap space
+# pid 659 (sendmail), uid 25, was killed: out of swap space
+# https://people.freebsd.org/~pho/stress/log/alan020.txt
+
+# Deadlock seen:
+# https://people.freebsd.org/~pho/stress/log/kostik845.txt
+# Fixed by r290920.
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `sysctl -n hw.physmem` -gt $(( 8 * 1024 * 1024 * 1024)) ] && exit 0
+[ `swapinfo | wc -l` -eq 1 ] || { swapoff -a; off=1; }
+size=$((`sysctl -n hw.physmem` / 1024 / 1024))
+need=$((size * 2))
+d1=$diskimage.1
+d2=$diskimage.2
+rm -f $d1 $d2 || exit 1
+[ `df -k $(dirname $diskimage) | tail -1 | awk '{print int($4 / 1024)}'` \
+ -lt $need ] &&
+ printf "Need %d MB on %s.\n" $need `dirname $diskimage` && exit 0
+trap "rm -f $d1 $d2" EXIT INT
+dd if=/dev/zero of=$d1 bs=1m count=$size status=none
+cp $d1 $d2
+[ $off ] && swapon -a
diff --git a/tools/test/stress2/misc/vmstat.sh b/tools/test/stress2/misc/vmstat.sh
new file mode 100755
index 000000000000..ad3b725b02b6
--- /dev/null
+++ b/tools/test/stress2/misc/vmstat.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2018 Dell EMC Isilon
+# 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 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.
+#
+
+# vmstat(8) test scenario.
+# Deadlock seen:
+# https://people.freebsd.org/~pho/stress/log/vmstat.txt
+# Fixed by r331489.
+
+. ../default.cfg
+
+set -e
+(cd ../testcases/thr1; ./thr1 -t 1m -i 20 -l 100 > /dev/null) & # churn
+pids=$!
+for i in `jot 16`; do
+ while true; do vmstat -z > /dev/null; done &
+ pids="$pids $!"
+done
+sleep 60
+set +e
+kill $pids > /dev/null 2>&1
+wait
+exit 0
diff --git a/tools/test/stress2/misc/vmstat2.sh b/tools/test/stress2/misc/vmstat2.sh
new file mode 100755
index 000000000000..db3cb1a697e2
--- /dev/null
+++ b/tools/test/stress2/misc/vmstat2.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# "0xFFFFFFFFFFFFFFFF in vmstat output" seen.
+
+(cd ../testcases/swap; ./swap -t 2m -i 20 -h -l 100 &)
+sleep .5
+s=0
+while pgrep -q swap; do
+ vmstat -z | awk '{if (length($4) > 11) { print $0; exit 1}}' ||
+ { pkill swap; s=1; }
+ sleep 2
+done
+wait
+exit $s
diff --git a/tools/test/stress2/misc/vmtotal.sh b/tools/test/stress2/misc/vmtotal.sh
new file mode 100755
index 000000000000..78de8274c7a4
--- /dev/null
+++ b/tools/test/stress2/misc/vmtotal.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# sysctl vm.vmtotal SIGSEGV test
+
+# Random threads would:
+# "pid 88456 (pipe), uid 0: exited on signal 11"
+# Fixed by r302063.
+
+while true; do sysctl vm.vmtotal > /dev/null; done &
+
+../misc/pipe.sh
+s=$?
+
+kill $!
+wait
+exit $s
diff --git a/tools/test/stress2/misc/vnodes.sh b/tools/test/stress2/misc/vnodes.sh
new file mode 100755
index 000000000000..4f2f75be61ef
--- /dev/null
+++ b/tools/test/stress2/misc/vnodes.sh
@@ -0,0 +1,87 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2012 Peter Holm <pho@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 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.
+#
+
+# Demonstrate vnode leak
+
+# Test scenario by Petr Salinger <Petr Salinger seznam cz>
+
+. ../default.cfg
+
+kldstat -v | grep -q pty || { kldload pty || exit 0; }
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > vnodes.c
+mycc -o vnodes -Wall -Wextra -O2 vnodes.c || exit 1
+rm -f vnodes.c
+
+s=0
+old=`sysctl -n vfs.numvnodes`
+/tmp/vnodes
+new=`sysctl -n vfs.numvnodes`
+[ $((new - old)) -gt 100 ] && { s=1; echo "FAIL vnode leak"; }
+
+rm -f /tmp/vnodes
+exit $s
+EOF
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+char dname[]= "/dev/ptyrX";
+
+void leak(void)
+{
+ int i, fd;
+
+ if (fork() == 0) {
+ for (i = '0'; i < '9'; i ++) {
+ dname[9] = i;
+ fd = open(dname, O_RDWR);
+ if (fd == -1)
+ if (errno != EBUSY && errno != ENXIO)
+ err(1, "open(%s)", dname);
+ }
+ _exit(0);
+ }
+ wait(NULL);
+}
+
+int main(void)
+{
+ int i;
+
+ for (i = 0 ;i < 100000; i++) {
+ leak();
+ }
+ return (0);
+}
diff --git a/tools/test/stress2/misc/vunref.sh b/tools/test/stress2/misc/vunref.sh
new file mode 100755
index 000000000000..8b245aa20a13
--- /dev/null
+++ b/tools/test/stress2/misc/vunref.sh
@@ -0,0 +1,212 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# Vnode reference leak test scenario by kib@.
+# Will fail with "umount: unmount of /mnt5 failed: Device busy"
+# vnode leak not seen on HEAD.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+here=`pwd`
+mounts=2 # Number of parallel scripts
+D=$diskimage
+
+[ -d "$RUNDIR" ] || mkdir $RUNDIR
+cd $RUNDIR
+
+if [ $# -eq 0 ]; then
+ sed '1,/^EOF/d' < $here/$0 > vunref.c
+ mycc -o /tmp/vunref -Wall -Wextra -O2 vunref.c
+ rm -f vunref.c
+ cd $here
+
+ rm -f $RUNDIR/active.*
+ for i in `jot $mounts`; do
+ m=$(( i + mdstart - 1 ))
+ [ ! -d ${mntpoint}$m ] && mkdir ${mntpoint}$m
+ mount | grep "$mntpoint" | grep -q md$m && umount -f ${mntpoint}$m
+ mdconfig -l | grep -q md$m && mdconfig -d -u $m
+
+ dd if=/dev/zero of=$D.$m bs=1m count=10 status=none
+ mdconfig -a -t vnode -f $D.$m -u $m
+ bsdlabel -w md$m auto
+ newfs md${m}$part > /dev/null 2>&1
+ done
+
+ # start the parallel tests
+ for i in `jot $mounts`; do
+ m=$((i + mdstart - 1))
+ $0 mmap $m &
+ sleep 0.2
+ $0 $m &
+ done
+
+ sleep 2
+
+ while [ ! -z "`ls $RUNDIR/active.* 2>/dev/null`" ] ; do
+ ../testcases/swap/swap -t 2m -i 20
+ done
+ wait
+
+ for i in `jot $mounts`; do
+ m=$((i + mdstart - 1))
+ mdconfig -d -u $m
+ rm -f $D$m
+ done
+ rm -f /tmp/vunref $RUNDIR/active.* $diskimage.* ${mntpoint}*/p*
+else
+ if [ $1 = mmap ]; then
+ touch $RUNDIR/active.$2
+ for i in `jot 500`; do
+ cd ${mntpoint}$2
+ /tmp/vunref > /dev/null 2>&1
+ cd /
+ [ -f $RUNDIR/active.$2 ] || exit
+ sleep 0.1
+ done
+ rm -f $RUNDIR/active.$2
+ else
+ # The test: Parallel mount and unmounts
+ m=$1
+ mount $opt /dev/md${m}$part ${mntpoint}$m
+ while [ -f $RUNDIR/active.$m ] ; do
+ sleep 0.1
+ n=0
+ while mount | grep -qw $mntpoint$m; do
+ umount ${mntpoint}$m > /dev/null 2>&1 && n=0
+ if [ $((n += 1)) -gt 600 ]; then
+ echo "*** Leak detected ***"
+ fstat $mntpoint$m
+ rm -f $RUNDIR/active.*
+ exit 1
+ fi
+ sleep 0.1
+ done
+ mount $opt /dev/md${m}$part ${mntpoint}$m
+ done
+ mount | grep "$mntpoint" | grep -q md$m && umount ${mntpoint}$m
+ fi
+fi
+exit
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define INPUTFILE "/bin/date"
+
+int
+test(void)
+{
+ int i;
+ pid_t pid;
+ char file[128];
+ int fdin, fdout;
+ char *src, *dst;
+ struct stat statbuf;
+
+ pid = getpid();
+ for (i = 0; i < 100; i++) {
+ sprintf(file,"p%05d.%05d", pid, i);
+
+ if ((fdin = open(INPUTFILE, O_RDONLY)) < 0)
+ err(1, INPUTFILE);
+
+ if ((fdout = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
+ err(1, "%s", file);
+
+ if (fstat(fdin, &statbuf) < 0)
+ err(1, "fstat error");
+
+ if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1)
+ err(1, "lseek error");
+
+ /* write a dummy byte at the last location */
+ if (write(fdout, "", 1) != 1)
+ err(1, "write error");
+
+ if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0)) ==
+ (caddr_t) - 1)
+ err(1, "mmap error for input");
+
+ if ((dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE,
+ MAP_SHARED, fdout, 0)) == (caddr_t) - 1)
+ err(1, "mmap error for output");
+
+ memcpy(dst, src, statbuf.st_size);
+
+ if (munmap(src, statbuf.st_size) == -1)
+ err(1, "munmap");
+ close(fdin);
+
+ if (munmap(dst, statbuf.st_size) == -1)
+ err(1, "munmap");
+ close(fdout);
+
+ if (unlink(file) == -1)
+ err(3, "unlink(%s)", file);
+ }
+
+ return (0);
+}
+
+int
+main()
+{
+ int i;
+ char path[MAXPATHLEN+1];
+ struct statfs buf;
+
+ if (getcwd(path, sizeof(path)) == NULL)
+ err(1, "getcwd()");
+
+ if (statfs(path, &buf) < 0)
+ err(1, "statfs(%s)", path);
+
+ if (!strcmp(buf.f_mntonname, "/"))
+ return (1);
+
+ for (i = 0; i < 2; i++) {
+ if (fork() == 0)
+ test();
+ }
+ for (i = 0; i < 2; i++)
+ wait(NULL);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/vunref2.sh b/tools/test/stress2/misc/vunref2.sh
new file mode 100755
index 000000000000..974af4bf8d80
--- /dev/null
+++ b/tools/test/stress2/misc/vunref2.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2010 Peter Holm <pho@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 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.
+#
+
+# Vnode reference leak test scenario.
+# Will fail with "umount: unmount of /mnt5 failed: Device busy"
+# vnode leak not seen on HEAD.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+mount | grep "$mntpoint" | grep -q md${mdstart}$part && umount -f $mntpoint
+mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 12m -u $mdstart
+bsdlabel -w md$mdstart auto
+newfs $newfs_flags md${mdstart}$part > /dev/null
+mount /dev/md${mdstart}$part $mntpoint
+chmod 777 $mntpoint
+
+export runRUNTIME=5m
+export LOAD=100
+export swapLOAD=100
+export RUNDIR=$mntpoint/stressX
+
+for i in `jot 10`; do
+ su $testuser -c "\
+ cd ..; testcases/run/run testcases/mmap/mmap testcases/swap/swap
+ "
+
+ sync;sync;sync
+ sleep 5
+ n=0
+ while mount | grep -qw $mntpoint; do
+ umount $mntpoint > /dev/null 2>&1 || sleep 0.1
+ n=$((n + 1))
+ if [ $n -gt 25 ]; then
+ echo "*** Leak detected ***"
+ fstat $mntpoint
+ exit 1
+ fi
+ done
+ mount /dev/md${mdstart}$part $mntpoint
+done
+
+umount $mntpoint
+mdconfig -d -u $mdstart
diff --git a/tools/test/stress2/misc/watchman.sh b/tools/test/stress2/misc/watchman.sh
new file mode 100755
index 000000000000..37c8ecd3d733
--- /dev/null
+++ b/tools/test/stress2/misc/watchman.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+# Bug 214923 - kqueue hangs with busy loop
+# Test scenario by: Jihyun Yu <yjh0502@gmail.com>
+
+# https://people.freebsd.org/~pho/stress/log/watchman.txt
+# Fixed by: r310302
+
+. ../default.cfg
+
+[ -z `which watchman` ] && { echo "watchman is not installed"; exit 0; }
+
+daemon sh -c "(cd ../testcases/swap; ./swap -t 5m -i 20 -h -l 100)" > \
+ /dev/null
+
+dir=/tmp/watchman
+rm -rf $dir
+mkdir -p $dir
+cd $dir
+
+mkdir -p foo bar
+seq -w 0 100 | xargs -n1 -I{} touch foo/{}.c
+
+echo '["subscribe","./","mysub",{"fields":["name"],"expression":["allof",'\
+ '["type","f"],["not","empty"],["suffix","c"]]}]' | \
+ watchman -p -j --server-encoding=json > /dev/null &
+pids=$!
+while true; do find bar/ -type f | xargs -n1 -P5 -I'{}' mv '{}' foo; done &
+pids="$pids $!"
+while true; do find foo/ -type f | xargs -n1 -P5 -I'{}' mv '{}' bar; done &
+pids="$pids $!"
+
+sleep 180
+while pgrep -q swap; do
+ pkill -9 swap
+done
+kill -9 $pids
+pkill watchman
+wait
+cd /
+sleep 1
+rm -rf $dir
+exit 0
diff --git a/tools/test/stress2/misc/wire_no_page.sh b/tools/test/stress2/misc/wire_no_page.sh
new file mode 100755
index 000000000000..f79fabc59785
--- /dev/null
+++ b/tools/test/stress2/misc/wire_no_page.sh
@@ -0,0 +1,104 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# Test scenario by kib@
+
+# "panic: vm_page_set_invalid: page 0xc3dfe8c0 is busy" seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/wire_no_page.c
+mycc -o wire_no_page -Wall -Wextra wire_no_page.c -lpthread || exit 1
+rm -f wire_no_page.c
+cd $odir
+
+cp /tmp/wire_no_page /tmp/wire_no_page2
+start=`date '+%s'`
+while [ $((`date '+%s'` - start)) -lt 300 ]; do
+ for i in `jot 50`; do
+ /tmp/wire_no_page /tmp/wire_no_page2 &
+ done
+ wait
+done
+
+rm -f /tmp/wire_no_page /tmp/wire_no_page2
+exit
+
+EOF
+/* $Id: wire_no_page.c,v 1.1 2013/05/17 07:50:30 kostik Exp kostik $ */
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <unistd.h>
+
+int
+main(int argc, char *argv[])
+{
+ struct stat st;
+ char *p1, *p2;
+ size_t len;
+ pid_t child;
+ int error, fd;
+
+ if (argc < 2)
+ errx(1, "file name ?");
+ fd = open(argv[1], O_RDWR);
+ if (fd == -1)
+ err(1, "open %s", argv[1]);
+ error = fstat(fd, &st);
+ if (error == -1)
+ err(1, "stat");
+ len = round_page(st.st_size);
+ p1 = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if ((void *)p1 == MAP_FAILED)
+ err(1, "mmap");
+ error = mlock(p1, len);
+ if (error == -1)
+ err(1, "mlock");
+ p2 = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if ((void *)p2 == MAP_FAILED)
+ err(1, "mmap");
+ error = msync(p2, len, MS_SYNC | MS_INVALIDATE);
+ if (error == -1)
+ err(1, "msync");
+ child = fork();
+ if (child == -1)
+ err(1, "fork");
+ else if (child == 0)
+ _exit(0);
+
+ return (0);
+}
diff --git a/tools/test/stress2/misc/write.sh b/tools/test/stress2/misc/write.sh
new file mode 100755
index 000000000000..9fb1cb7be61a
--- /dev/null
+++ b/tools/test/stress2/misc/write.sh
@@ -0,0 +1,231 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 EMC Corp.
+# 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 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.
+#
+
+# Parallel write test.
+
+# fsync: giving up on dirty
+# 0xc9172168: tag devfs, type VCHR
+# usecount 1, writecount 0, refcount 31 mountedhere 0xc96bd300
+# flags (VI_ACTIVE)
+# v_object 0xc96e55a0 ref 0 pages 983 cleanbuf 27 dirtybuf 2
+# lock type devfs: EXCL by thread 0xcbedf340 (pid 58752, write, tid 100254)
+# #0 0xc0c23f33 at __lockmgr_args+0xae3
+# #1 0xc0cff943 at vop_stdlock+0x53
+# #2 0xc12027c8 at VOP_LOCK1_APV+0x118
+# #3 0xc0d2400a at _vn_lock+0xba
+# #4 0xc0f3288b at ffs_sync+0x34b
+# #5 0xc0f16cc5 at softdep_ast_cleanup_proc+0x205
+# #6 0xc0ca3ac7 at userret+0x37
+# #7 0xc11d021e at syscall+0x50e
+# #8 0xc11bae3f at Xint0x80_syscall+0x2f
+# dev label/tmp
+
+# Deadlock seen:
+# https://people.freebsd.org/~pho/stress/log/write.txt
+
+. ../default.cfg
+
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/write.c
+mycc -o write -Wall -Wextra -O2 -g write.c || exit 1
+rm -f write.c
+cd $odir
+
+need=$((25 * 1024))
+[ `df -k $(dirname $diskimage) | tail -1 | awk '{print int($4 / 1024)}'` \
+ -lt $need ] &&
+ printf "Need %d MB on %s.\n" $need `dirname $diskimage` && exit 0
+wd=`dirname $diskimage`
+wd="$wd/write.dir"
+rm -rf $wd
+mkdir -p $wd
+
+(cd $wd; /tmp/write)
+s=$?
+
+rm -rf /tmp/write $wd
+exit $s
+
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define DONE 1
+#define MAXBLK (2 * 1024 * 1024)
+#define MAXPROC 32
+#define MAXSIZ (9LL * 1024 * 1024 *1024)
+#define RUNTIME (15 * 60)
+#define SYNC 0
+
+volatile u_int *share;
+int parallel;
+
+struct typ {
+ off_t blocksize;
+ off_t blocks;
+ int sequential;
+ int mindelay;
+ int maxdelay;
+} t[MAXPROC];
+
+int
+rnd(int mi, int ma)
+{
+ return (arc4random() % (ma - mi + 1) + mi);
+}
+
+void
+test(int indx, int num)
+{
+ ssize_t i, r;
+ time_t start;
+ int fd, n;
+ char *buf, file[80];
+
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != (unsigned int)parallel)
+ ;
+
+ if ((buf = malloc(t[indx].blocksize)) == NULL)
+ err(1, "malloc");
+ snprintf(file, sizeof(file), "file.%06d.%06d", indx, num);
+ n = 0;
+ start = time(NULL);
+ while (share[DONE] != (unsigned int)parallel) {
+ setproctitle("test(%d) num %d, n %d", indx, num, n);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE))
+ == -1)
+ err(1, "open(%s)", file);
+
+ for (i = 0; i < t[indx].blocks; i++) {
+ if (t[indx].sequential == 0)
+ if (lseek(fd, 2LL << (arc4random() % 18),
+ SEEK_SET) == -1)
+ err(1, "lseek");
+ if ((r = write(fd, buf, t[indx].blocksize)) !=
+ t[indx].blocksize) {
+ warn("write returned %zd\n", r);
+ goto done;
+ }
+ usleep(rnd(t[indx].mindelay, t[indx].maxdelay));
+ }
+
+ close(fd);
+ if (n++ == 0)
+ atomic_add_int(&share[DONE], 1);
+ if (time(NULL) - start >= RUNTIME / 4) {
+#if defined(DEBUG)
+ fprintf(stderr, "test(%d), %d Timed out\n", indx, num);
+#endif
+ break;
+ }
+ }
+done:
+ if (n++ == 0)
+ atomic_add_int(&share[DONE], 1);
+
+ _exit(0);
+}
+
+void
+setup(void)
+{
+ int i;
+
+ parallel = arc4random() % MAXPROC + 1;
+ for (i = 0; i < parallel; i++) {
+ if (arc4random() % 100 < 10)
+ t[i].blocksize = (arc4random() + 1) % MAXBLK;
+ else
+ t[i].blocksize = 2 << (arc4random() % 20);
+ t[i].sequential = arc4random() % 2;
+ t[i].mindelay = arc4random() % 50;
+ t[i].maxdelay = t[i].mindelay + arc4random() % 100;
+ t[i].blocks = 2LL << (arc4random() % 18);
+ if (t[i].blocks * t[i].blocksize > MAXSIZ)
+ t[i].blocks = MAXSIZ / t[i].blocksize;
+#if defined(DEBUG)
+ fprintf(stderr, "%3d: blocksize %7lld, sequential %d, "
+ "mindelay %3d, maxdelay %3d, blocks %6lld, size %4lld MB\n",
+ i, (long long)t[i].blocksize, t[i].sequential, t[i].mindelay,
+ t[i].maxdelay, (long long)t[i].blocks,
+ (long long)t[i].blocksize * t[i].blocks / 1024 / 1024);
+#endif
+ }
+}
+
+int
+main(void)
+{
+ size_t len;
+ time_t start;
+ int e, i, n, *pids, status;
+
+ e = 0;
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ n = 0;
+ start = time(NULL);
+ while ((time(NULL) - start) < RUNTIME && e == 0) {
+ setup();
+
+ pids = malloc(sizeof(pid_t) * parallel);
+ share[SYNC] = share[DONE] = 0;
+ for (i = 0; i < parallel; i++) {
+ if ((pids[i] = fork()) == 0)
+ test(i, n);
+ }
+ for (i = 0; i < parallel; i++) {
+ if (waitpid(pids[i], &status, 0) != pids[i])
+ err(1, "waitpid %d", pids[i]);
+ e += status == 0 ? 0 : 1;
+ }
+ n++;
+ n = n % 10;
+ free(pids);
+ }
+
+ return (e);
+}
diff --git a/tools/test/stress2/misc/zfs.sh b/tools/test/stress2/misc/zfs.sh
new file mode 100755
index 000000000000..56ff91781cc3
--- /dev/null
+++ b/tools/test/stress2/misc/zfs.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Simple zfs raidz test scenario
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -m` = "i386" ] && exit 0
+[ `sysctl -n kern.kstack_pages` -lt 4 ] && exit 0
+
+. ../default.cfg
+
+kldstat -v | grep -q zfs.ko || { kldload zfs.ko ||
+ exit 0; loaded=1; }
+
+u1=$mdstart
+u2=$((u1 + 1))
+u3=$((u2 + 1))
+
+mdconfig -l | grep -q md$u1 && mdconfig -d -u $u1
+mdconfig -l | grep -q md$u2 && mdconfig -d -u $u2
+mdconfig -l | grep -q md$u3 && mdconfig -d -u $u3
+
+mdconfig -s 512m -u $u1
+mdconfig -s 512m -u $u2
+mdconfig -s 512m -u $u3
+
+zpool list | egrep -q "^stress2_tank" && zpool destroy stress2_tank
+[ -d /stress2_tank ] && rm -rf /stress2_tank
+zpool create stress2_tank raidz md$u1 md$u2 md$u3
+zfs create stress2_tank/test
+
+export RUNDIR=/stress2_tank/test/stressX
+export runRUNTIME=20m
+(cd ..; ./run.sh marcus.cfg)
+
+zfs destroy -r stress2_tank
+zpool destroy stress2_tank
+
+mdconfig -d -u $u1
+mdconfig -d -u $u2
+mdconfig -d -u $u3
+[ -n "$loaded" ] && kldunload zfs.ko
+exit 0
diff --git a/tools/test/stress2/misc/zfs10.sh b/tools/test/stress2/misc/zfs10.sh
new file mode 100755
index 000000000000..3f3fdd2622d7
--- /dev/null
+++ b/tools/test/stress2/misc/zfs10.sh
@@ -0,0 +1,142 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# sendfile() over zfs. No problems seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -m` = "i386" ] && exit 0
+[ `sysctl -n kern.kstack_pages` -lt 4 ] && exit 0
+
+. ../default.cfg
+
+[ `sysctl -n hw.physmem` -lt $(( 4 * 1024 * 1024 * 1024)) ] && exit 0
+kldstat -v | grep -q zfs.ko || { kldload zfs.ko ||
+ exit 0; loaded=1; }
+
+u1=$mdstart
+u2=$((u1 + 1))
+zdir=/stress2_tank/test
+
+mdconfig -l | grep -q md$u1 && mdconfig -d -u $u1
+mdconfig -l | grep -q md$u2 && mdconfig -d -u $u2
+
+mdconfig -s 1g -u $u1 || exit 1
+mdconfig -s 1g -u $u2 || exit 1
+
+zpool list | egrep -q "^stress2_tank" && zpool destroy stress2_tank
+[ -d /stress2_tank ] && rm -rf /stress2_tank
+zpool create stress2_tank md$u1 md$u2 || exit 1
+zfs create stress2_tank/test || exit 1
+
+(cd ../testcases/swap; ./swap -t 5m -i 20 -l 100) &
+
+odir=`pwd`
+
+echo sendfile.sh
+cd /tmp
+sed '1,/^EOF/d' < $odir/sendfile.sh > sendfile.c
+mycc -o sendfile -Wall sendfile.c -pthread || exit 1
+rm -f sendfile.c
+cd $zdir
+
+in=inputFile
+out=outputFile
+
+for i in 1 2 3 4 8 16 1k 2k 3k 4k 5k 1m 2m 3m 4m 5m ; do
+ rm -f $in $out
+ dd if=/dev/random of=$in bs=$i count=1 status=none
+ /tmp/sendfile $in $out 12345
+ cmp $in $out || { echo FAIL; ls -l $in $out; }
+ rm -f $in $out
+done
+rm -f /tmp/sendfile
+
+echo sendfile4.sh
+cd /tmp
+sed '1,/^EOF/d' < $odir/sendfile4.sh > sendfile4.c
+mycc -o sendfile4 -Wall -Wextra -O2 sendfile4.c || exit
+rm -f sendfile4.c
+cd $zdir
+
+dd if=/dev/zero of=diskimage bs=1m count=6 status=none
+/tmp/sendfile4 diskimage || echo FAIL
+
+rm -f /tmp/sendfile4 diskimage
+
+echo sendfile5.sh
+cd /tmp
+sed '1,/^EOF/d' < $odir/sendfile5.sh > sendfile5.c
+mycc -o sendfile5 -Wall -Wextra -O2 sendfile5.c || exit
+rm -f sendfile5.c
+cd $zdir
+dd if=/dev/zero of=diskimage bs=1m count=1024 status=none
+/tmp/sendfile5 diskimage
+rm -f /tmp/sendfile5 diskimage
+
+echo sendfile8.sh
+cd /tmp
+sed '1,/^EOF/d' < $odir/sendfile8.sh > sendfile8.c
+mycc -o sendfile8 -Wall -Wextra -O2 sendfile8.c || exit
+rm -f sendfile8.c
+cd $zdir
+dd if=/dev/random of=in bs=1m count=50 status=none
+/tmp/sendfile8 in out 76543
+rm -f /tmp/sendfile8 in out
+
+echo sendfile9.sh
+cd /tmp
+sed '1,/^EOF/d' < $odir/sendfile9.sh > sendfile9.c
+mycc -o sendfile9 -Wall -Wextra -O2 sendfile9.c || exit
+rm -f sendfile9.c
+cd $zdir
+dd if=/dev/random of=in bs=1m count=50 status=none
+/tmp/sendfile9 in out 76543
+rm -f /tmp/sendfile9 in out
+
+echo sendfile12.sh
+cd /tmp
+sed '1,/^EOF/d' < $odir/sendfile12.sh > sendfile12.c
+mycc -o sendfile12 -Wall -Wextra -O2 sendfile12.c || exit
+rm -f sendfile12.c
+cd $zdir
+dd if=/dev/zero of=in bs=1m count=512 status=none
+/tmp/sendfile12 in out 76543
+rm -f /tmp/sendfile12 in out
+
+while pkill swap; do sleep .1; done
+wait
+
+cd $odir
+zfs destroy -r stress2_tank
+zpool destroy stress2_tank
+
+mdconfig -d -u $u1
+mdconfig -d -u $u2
+[ -n "$loaded" ] && kldunload zfs.ko
+exit 0
diff --git a/tools/test/stress2/misc/zfs11.sh b/tools/test/stress2/misc/zfs11.sh
new file mode 100755
index 000000000000..97e71a5a13f7
--- /dev/null
+++ b/tools/test/stress2/misc/zfs11.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# zfs deadlock on r360452 relating to busy vm page
+# 0 5804 5803 0 52 0 13048 2740 prev->lr DC+ 0 0:00.00 tail -F blah
+# 0 5805 5802 0 52 0 12976 2636 pgnslp DC+ 0 0:00.00 dd if=/dev/zero of=blah
+
+# Test scenario by Bryan Drewery <bdrewery@FreeBSD.org>
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -m` = "i386" ] && exit 0
+[ `sysctl -n kern.kstack_pages` -lt 4 ] && exit 0
+
+. ../default.cfg
+
+kldstat -v | grep -q zfs.ko || { kldload zfs.ko ||
+ exit 0; loaded=1; }
+
+u1=$mdstart
+u2=$((u1 + 1))
+
+set -e
+mdconfig -l | grep -q md$u1 && mdconfig -d -u $u1
+mdconfig -l | grep -q md$u2 && mdconfig -d -u $u2
+
+mdconfig -s 2g -u $u1
+mdconfig -s 2g -u $u2
+
+zpool list | egrep -q "^stress2_tank" && zpool destroy stress2_tank
+[ -d /stress2_tank ] && rm -rf /stress2_tank
+zpool create stress2_tank raidz md$u1 md$u2
+zfs create stress2_tank/test
+set +e
+
+cd /stress2_tank/test
+timeout 10s sh -c '\
+ dd if=/dev/zero of=blah status=none & tail -F blah > /dev/null' &
+sleep 5
+ps -l | grep -v grep | grep -q pgnslp && { s=1; echo FAIL; }
+ps -l | grep -v grep | grep blah
+wait
+cd /
+
+zfs umount stress2_tank/test
+zfs destroy -r stress2_tank
+zpool destroy stress2_tank
+
+mdconfig -d -u $u1
+mdconfig -d -u $u2
+[ $loaded ] && kldunload zfs.ko
+exit $s
diff --git a/tools/test/stress2/misc/zfs2.sh b/tools/test/stress2/misc/zfs2.sh
new file mode 100755
index 000000000000..3e1ea585db38
--- /dev/null
+++ b/tools/test/stress2/misc/zfs2.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Simple zfs mirror test scenario
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -m` = "i386" ] && exit 0
+[ `sysctl -n kern.kstack_pages` -lt 4 ] && exit 0
+
+. ../default.cfg
+
+kldstat -v | grep -q zfs.ko || { kldload zfs.ko ||
+ exit 0; loaded=1; }
+
+d1=$diskimage.1
+d2=$diskimage.2
+
+dd if=/dev/zero of=$d1 bs=1m count=1k status=none
+dd if=/dev/zero of=$d2 bs=1m count=1k status=none
+
+u1=$mdstart
+u2=$((u1 + 1))
+
+mdconfig -l | grep -q md$u1 && mdconfig -d -u $u1
+mdconfig -l | grep -q md$u2 && mdconfig -d -u $u2
+
+mdconfig -a -t vnode -f $d1 -u $u1
+mdconfig -a -t vnode -f $d2 -u $u2
+
+zpool list | egrep -q "^stress2_tank" && zpool destroy stress2_tank
+[ -d /stress2_tank ] && rm -rf /stress2_tank
+zpool create stress2_tank mirror md$u1 md$u2
+
+export RUNDIR=/stress2_tank/stressX
+export runRUNTIME=10m
+(cd ..; ./run.sh marcus.cfg)
+
+zfs destroy -r stress2_tank
+zpool destroy stress2_tank
+
+mdconfig -d -u $u1
+mdconfig -d -u $u2
+
+rm -rf $d1 $d2
+[ -n "$loaded" ] && kldunload zfs.ko
+exit 0
diff --git a/tools/test/stress2/misc/zfs3.sh b/tools/test/stress2/misc/zfs3.sh
new file mode 100755
index 000000000000..e2a2b2c0e912
--- /dev/null
+++ b/tools/test/stress2/misc/zfs3.sh
@@ -0,0 +1,83 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Simple zfs snapshot test scenario
+
+# page fault from fs/zfs/dbuf.c:1807
+
+# Page fault seen:
+# https://people.freebsd.org/~pho/stress/log/zfs3-2.txt
+# Fixed in r308887.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -m` = "i386" ] && exit 0
+[ `sysctl -n kern.kstack_pages` -lt 4 ] && exit 0
+
+. ../default.cfg
+
+kldstat -v | grep -q zfs.ko || { kldload zfs.ko ||
+ exit 0; loaded=1; }
+
+d1=$diskimage.1
+d2=$diskimage.2
+
+dd if=/dev/zero of=$d1 bs=1m count=1k status=none
+dd if=/dev/zero of=$d2 bs=1m count=1k status=none
+
+u1=$mdstart
+u2=$((u1 + 1))
+
+mdconfig -l | grep -q md$u1 && mdconfig -d -u $u1
+mdconfig -l | grep -q md$u2 && mdconfig -d -u $u2
+
+mdconfig -a -t vnode -f $d1 -u $u1
+mdconfig -a -t vnode -f $d2 -u $u2
+
+zpool list | egrep -q "^stress2_tank" && zpool destroy stress2_tank
+[ -d /stress2_tank ] && rm -rf /stress2_tank
+zpool create stress2_tank md$u1 md$u2
+zfs create stress2_tank/test
+zfs set quota=800m stress2_tank/test
+zfs snapshot stress2_tank/test@snap1
+
+export RUNDIR=/stress2_tank/test/stressX
+export runRUNTIME=10m
+(cd ..; ./run.sh marcus.cfg)
+
+zfs rollback stress2_tank/test@snap1
+zfs destroy stress2_tank/test@snap1
+
+zfs destroy -r stress2_tank
+zpool destroy stress2_tank
+
+mdconfig -d -u $u1
+mdconfig -d -u $u2
+
+rm -rf $d1 $d2
+[ -n "$loaded" ] && kldunload zfs.ko
+exit 0
diff --git a/tools/test/stress2/misc/zfs4.sh b/tools/test/stress2/misc/zfs4.sh
new file mode 100755
index 000000000000..bb28560a937e
--- /dev/null
+++ b/tools/test/stress2/misc/zfs4.sh
@@ -0,0 +1,91 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Simple zfs snapshot test scenario
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -m` = "i386" ] && exit 0
+[ `sysctl -n kern.kstack_pages` -lt 4 ] && exit 0
+
+. ../default.cfg
+
+kldstat -v | grep -q zfs.ko || { kldload zfs.ko ||
+ exit 0; loaded=1; }
+
+d1=$diskimage.1
+d2=$diskimage.2
+
+dd if=/dev/zero of=$d1 bs=1m count=1k status=none
+dd if=/dev/zero of=$d2 bs=1m count=1k status=none
+
+u1=$mdstart
+u2=$((u1 + 1))
+
+mdconfig -l | grep -q md$u1 && mdconfig -d -u $u1
+mdconfig -l | grep -q md$u2 && mdconfig -d -u $u2
+
+mdconfig -a -t vnode -f $d1 -u $u1
+mdconfig -a -t vnode -f $d2 -u $u2
+
+zpool list | egrep -q "^stress2_tank" && zpool destroy stress2_tank
+[ -d /stress2_tank ] && rm -rf /stress2_tank
+zpool create stress2_tank md$u1 md$u2
+zfs create stress2_tank/test
+zfs set quota=100m stress2_tank/test
+zfs set snapdir=visible stress2_tank/test
+
+export RUNDIR=/stress2_tank/test/stressX
+export runRUNTIME=10m
+start=`date +%s`
+(cd ..; ./run.sh marcus.cfg) &
+
+for i in `jot 20`; do
+ zfs snapshot stress2_tank/test@snap$i
+done
+for i in `jot 20`; do
+ zfs destroy stress2_tank/test@snap$i
+done
+while pgrep -fq run.sh; do
+ [ $((`date +%s` - start)) -gt 660 ] &&
+ ../tools/killall.sh
+ sleep 10
+done
+wait
+
+for i in `jot 3`; do
+ zfs destroy -r stress2_tank && break
+ sleep 10
+done
+zpool destroy stress2_tank
+
+mdconfig -d -u $u1
+mdconfig -d -u $u2
+
+rm -rf $d1 $d2
+[ -n "$loaded" ] && kldunload zfs.ko
+exit 0
diff --git a/tools/test/stress2/misc/zfs5.sh b/tools/test/stress2/misc/zfs5.sh
new file mode 100755
index 000000000000..646880fb8b78
--- /dev/null
+++ b/tools/test/stress2/misc/zfs5.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Simple zfs test of vdev as a file and snapshot clones
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -m` = "i386" ] && exit 0
+[ `sysctl -n kern.kstack_pages` -lt 4 ] && exit 0
+
+. ../default.cfg
+
+kldstat -v | grep -q zfs.ko || { kldload zfs.ko ||
+ exit 0; loaded=1; }
+
+d1=$diskimage.1
+d2=$diskimage.2
+
+dd if=/dev/zero of=$d1 bs=1m count=1k status=none
+dd if=/dev/zero of=$d2 bs=1m count=1k status=none
+
+zpool list | egrep -q "^stress2_tank" && zpool destroy stress2_tank
+[ -d /stress2_tank ] && rm -rf /stress2_tank
+zpool create stress2_tank $d1 $d2
+zfs create stress2_tank/test
+zfs set quota=100m stress2_tank/test
+
+export RUNDIR=/stress2_tank/test/stressX
+export runRUNTIME=10m
+(cd ..; ./run.sh vfs.cfg) &
+
+for i in `jot 20`; do
+ zfs snapshot stress2_tank/test@snap$i
+ zfs clone stress2_tank/test@snap$i stress2_tank/snap$i
+done
+for i in `jot 20`; do
+ zfs destroy stress2_tank/snap$i
+ zfs destroy stress2_tank/test@snap$i
+done
+wait
+
+zfs destroy -r stress2_tank
+zpool destroy stress2_tank
+
+rm -rf $d1 $d2
+[ -n "$loaded" ] && kldunload zfs.ko
+exit 0
diff --git a/tools/test/stress2/misc/zfs6.sh b/tools/test/stress2/misc/zfs6.sh
new file mode 100755
index 000000000000..0e16cd8679e7
--- /dev/null
+++ b/tools/test/stress2/misc/zfs6.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2009 Peter Holm <pho@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 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.
+#
+
+# Parallel mount / umount and snapshots. zpool hang seen:
+# https://people.freebsd.org/~pho/stress/log/zfs6.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -m` = "i386" ] && exit 0
+[ `sysctl -n kern.kstack_pages` -lt 4 ] && exit 0
+
+. ../default.cfg
+
+kldstat -v | grep -q zfs.ko || { kldload zfs.ko ||
+ exit 0; loaded=1; }
+
+u1=$mdstart
+u2=$((u1 + 1))
+u3=$((u2 + 1))
+
+mdconfig -l | grep -q md$u1 && mdconfig -d -u $u1
+mdconfig -l | grep -q md$u2 && mdconfig -d -u $u2
+mdconfig -l | grep -q md$u3 && mdconfig -d -u $u3
+
+mdconfig -s 512m -u $u1
+mdconfig -s 512m -u $u2
+mdconfig -s 512m -u $u3
+
+zpool list | egrep -q "^stress2_tank" && zpool destroy stress2_tank
+[ -d /stress2_tank ] && rm -rf /stress2_tank
+zpool create stress2_tank raidz md$u1 md$u2 md$u3 || exit 1
+zfs create stress2_tank/test || exit 1
+
+while true; do
+ zfs umount stress2_tank/test
+ zfs mount stress2_tank/test
+done &
+
+for i in `jot 5000`; do
+ touch /stress2_tank/test/f$i
+ zfs snapshot stress2_tank/test@$i
+ if [ $i -gt 5 ]; then
+ zfs destroy stress2_tank/test@$((i - 5))
+ fi
+done
+kill $!
+wait
+sleep 2
+zfs destroy -r stress2_tank || exit 1
+zpool destroy stress2_tank || exit 1
+
+mdconfig -d -u $u1
+mdconfig -d -u $u2
+mdconfig -d -u $u3
+[ -n "$loaded" ] && kldunload zfs.ko
+exit 0
diff --git a/tools/test/stress2/misc/zfs7.sh b/tools/test/stress2/misc/zfs7.sh
new file mode 100755
index 000000000000..04ceee2e48bc
--- /dev/null
+++ b/tools/test/stress2/misc/zfs7.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2017 Dell EMC Isilon
+# 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 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.
+#
+
+# Non random based test scenario
+# No problems seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -m` = "i386" ] && exit 0
+[ `sysctl -n kern.kstack_pages` -lt 4 ] && exit 0
+
+. ../default.cfg
+
+[ `sysctl -n hw.physmem` -lt $(( 4 * 1024 * 1024 * 1024)) ] && exit 0
+kldstat -v | grep -q zfs.ko || { kldload zfs.ko ||
+ exit 0; loaded=1; }
+
+u1=$mdstart
+u2=$((u1 + 1))
+
+mdconfig -l | grep -q md$u1 && mdconfig -d -u $u1
+mdconfig -l | grep -q md$u2 && mdconfig -d -u $u2
+
+mdconfig -s 1g -u $u1
+mdconfig -s 1g -u $u2
+
+zpool list | egrep -q "^stress2_tank" && zpool destroy stress2_tank
+[ -d /stress2_tank ] && rm -rf /stress2_tank
+zpool create stress2_tank md$u1 md$u2
+zfs create stress2_tank/test
+
+cc -o /tmp/fstool ../tools/fstool.c || exit 1
+start=`date +%s`
+for j in `jot 3`; do
+ mkdir -p /stress2_tank/test/d$j
+ (cd /stress2_tank/test/d$j; /tmp/fstool -f 100 -n 3000 -s 1k -t 5) &
+done
+wait
+echo "Elapsed $((`date +%s` - start))"
+rm /tmp/fstool
+
+zfs destroy -r stress2_tank
+zpool destroy stress2_tank
+
+mdconfig -d -u $u1
+mdconfig -d -u $u2
+[ -n "$loaded" ] && kldunload zfs.ko
+exit 0
diff --git a/tools/test/stress2/misc/zfs8.sh b/tools/test/stress2/misc/zfs8.sh
new file mode 100755
index 000000000000..7d1d9887ce29
--- /dev/null
+++ b/tools/test/stress2/misc/zfs8.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# No problems seen.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -m` = "i386" ] && exit 0
+[ `sysctl -n kern.kstack_pages` -lt 4 ] && exit 0
+
+. ../default.cfg
+
+[ `sysctl -n vm.swap_total` -eq 0 ] && exit 0
+kldstat -v | grep -q zfs.ko || { kldload zfs.ko ||
+ exit 0; loaded=1; }
+
+u1=$mdstart
+u2=$((u1 + 1))
+
+mdconfig -l | grep -q md$u1 && mdconfig -d -u $u1
+mdconfig -l | grep -q md$u2 && mdconfig -d -u $u2
+
+mdconfig -s 10g -u $u1
+mdconfig -s 10g -u $u2
+
+zpool list | egrep -q "^stress2_tank" && zpool destroy stress2_tank
+[ -d /stress2_tank ] && rm -rf /stress2_tank
+zpool create stress2_tank md$u1 md$u2 || exit 1
+zfs create stress2_tank/test || exit 1
+
+dd if=/dev/random of=/stress2_tank/test/file bs=1m count=4k status=none
+
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 300 ]; do
+ zfs snapshot -r stress2_tank/test@1
+ zfs send stress2_tank/test@1 | zfs recv -F stress2_tank/backup
+ zfs destroy stress2_tank/test@1
+ zfs destroy -r stress2_tank/backup
+done
+
+zfs destroy -r stress2_tank || exit 1
+zpool destroy stress2_tank || exit 1
+
+mdconfig -d -u $u1
+mdconfig -d -u $u2
+[ -n "$loaded" ] && kldunload zfs.ko
+rm -f $diskimage
+exit 0
diff --git a/tools/test/stress2/misc/zfs9.sh b/tools/test/stress2/misc/zfs9.sh
new file mode 100755
index 000000000000..274a42f18bdb
--- /dev/null
+++ b/tools/test/stress2/misc/zfs9.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Simple zfs raidz test scenario
+
+# Deadlock seen:
+# https://people.freebsd.org/~pho/stress/log/zfs9.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ `uname -m` = "i386" ] && exit 0
+[ `sysctl -n kern.kstack_pages` -lt 4 ] && exit 0
+
+. ../default.cfg
+
+kldstat -v | grep -q zfs.ko || { kldload zfs.ko ||
+ exit 0; loaded=1; }
+
+u1=$mdstart
+u2=$((u1 + 1))
+
+mdconfig -l | grep -q md$u1 && mdconfig -d -u $u1
+mdconfig -l | grep -q md$u2 && mdconfig -d -u $u2
+
+mdconfig -s 2g -u $u1
+mdconfig -s 2g -u $u2
+
+zpool list | egrep -q "^stress2_tank" && zpool destroy stress2_tank
+[ -d /stress2_tank ] && rm -rf /stress2_tank
+zpool create stress2_tank raidz md$u1 md$u2
+zfs create stress2_tank/test
+
+export RUNDIR=/stress2_tank/test/stressX
+export runRUNTIME=10m
+export LOAD=70
+export TESTPROGS=`cd ..; find testcases/ -perm -1 -type f | \
+ egrep -v "/run/"`
+
+(cd ..; ./testcases/run/run $TESTPROGS)
+
+zfs umount stress2_tank/test
+zfs destroy -r stress2_tank
+zpool destroy stress2_tank
+
+mdconfig -d -u $u1
+mdconfig -d -u $u2
+[ -n "$loaded" ] && kldunload zfs.ko
+exit 0
diff --git a/tools/test/stress2/misc/zz-combo01.sh b/tools/test/stress2/misc/zz-combo01.sh
new file mode 100755
index 000000000000..8899ade3441f
--- /dev/null
+++ b/tools/test/stress2/misc/zz-combo01.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# "panic: pmap_demote_pde: firstpte and newpte map different physical
+# addresses" seen
+# https://people.freebsd.org/~pho/stress/log/kostik1195.txt
+
+# Fixed by r348306
+
+list="su.sh callout_reset_on.sh"
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 1200 ]; do
+ for i in $list; do
+ echo "`date +%T` $i"
+ ./$i
+ done
+done
+exit 0
diff --git a/tools/test/stress2/misc/zz-combo02.sh b/tools/test/stress2/misc/zz-combo02.sh
new file mode 100755
index 000000000000..2d866858bb8f
--- /dev/null
+++ b/tools/test/stress2/misc/zz-combo02.sh
@@ -0,0 +1,34 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+for i in "su.sh mkfifo2c.sh"; do
+ ./$i &
+done
+wait
+exit 0
diff --git a/tools/test/stress2/misc/zz-combo03.sh b/tools/test/stress2/misc/zz-combo03.sh
new file mode 100755
index 000000000000..7afaec11e861
--- /dev/null
+++ b/tools/test/stress2/misc/zz-combo03.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# vnlru deadlock seen in rename12.sh.
+# Introduced by r351825, reverted by r351882
+
+# https://people.freebsd.org/~pho/stress/log/zz-combo03.txt
+
+cd ../misc
+for i in crossmp8.sh rename12.sh; do
+ ./$i
+done
+exit 0
diff --git a/tools/test/stress2/misc/zz-combo04.sh b/tools/test/stress2/misc/zz-combo04.sh
new file mode 100755
index 000000000000..69003ec73371
--- /dev/null
+++ b/tools/test/stress2/misc/zz-combo04.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm
+#
+# 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 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.
+#
+
+# "panic: vm_page_dequeue: queued unlocked page
+# 0xfffffe00295e8800" seen with WiP code.
+# https://people.freebsd.org/~pho/stress/log/zz-combo04.txt
+
+for i in `jot 2`; do
+ ./nullfs5.sh
+ ./cleanup.sh
+done
diff --git a/tools/test/stress2/misc/zzbuildworld.sh b/tools/test/stress2/misc/zzbuildworld.sh
new file mode 100755
index 000000000000..ae0956239648
--- /dev/null
+++ b/tools/test/stress2/misc/zzbuildworld.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2019 Dell EMC Isilon
+#
+# 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 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.
+#
+
+# Run a "make buildworld" as last test in stress2/misc.
+
+. ../default.cfg
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+kernel=`sysctl -n kern.bootfile`
+kerneldir=`strings -a $kernel | egrep -m 1 "$(hostname).*/compile/" | sed 's/.*://;s/ .*//'`
+[ -z "$kerneldir" ] && { echo "Can not find kerneldir"; exit 1; }
+src=`echo $kerneldir | sed 's#/sys/.*##'`
+[ ! -d $src ] && { echo "Can not find source in $src"; exit 1; }
+
+start=`date +%s`
+mount | grep -q "on $mntpoint " && exit 1
+mount -t tmpfs tmpfs $mntpoint
+top=$mntpoint
+export MAKEOBJDIRPREFIX=$top/obj
+export log=$top/buildworld.`date +%Y%m%dT%H%M`
+n=$((`sysctl -n hw.ncpu` + 1))
+cd $src
+make -j$n buildworld > $log 2>&1 && s=0 ||s=1
+grep '\*\*\*' $log && s=2
+stop=`date +%s`
+echo "Elapsed `date -u -j -f '%s' '+%H:%M' $((stop - start))`"
+[ $s -ne 0 ] && { xz $log; cp -a $log.xz /tmp; }
+umount $mntpoint
+exit $s
diff --git a/tools/test/stress2/mkdir.cfg b/tools/test/stress2/mkdir.cfg
new file mode 100644
index 000000000000..df1f9ad34667
--- /dev/null
+++ b/tools/test/stress2/mkdir.cfg
@@ -0,0 +1,9 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+export TESTPROGS="testcases/mkdir/mkdir"
+export mkdirNODELAY=1
+export mkdirLOAD=100
+export mkdirRUNTIME=2m
diff --git a/tools/test/stress2/mkfifo.cfg b/tools/test/stress2/mkfifo.cfg
new file mode 100644
index 000000000000..41d8932bcf23
--- /dev/null
+++ b/tools/test/stress2/mkfifo.cfg
@@ -0,0 +1,8 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+export TESTPROGS="testcases/mkfifo/mkfifo testcases/swap/swap"
+export mkfifoNODELAY=1
+export mkfifoLOAD=100
diff --git a/tools/test/stress2/norw.cfg b/tools/test/stress2/norw.cfg
new file mode 100644
index 000000000000..7d3f04abe903
--- /dev/null
+++ b/tools/test/stress2/norw.cfg
@@ -0,0 +1,6 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+export rwLOAD=0
diff --git a/tools/test/stress2/noswap.cfg b/tools/test/stress2/noswap.cfg
new file mode 100644
index 000000000000..23a892e2a657
--- /dev/null
+++ b/tools/test/stress2/noswap.cfg
@@ -0,0 +1,6 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+export swapLOAD=0
diff --git a/tools/test/stress2/pty.cfg b/tools/test/stress2/pty.cfg
new file mode 100644
index 000000000000..bc22a9bd7715
--- /dev/null
+++ b/tools/test/stress2/pty.cfg
@@ -0,0 +1,8 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+export TESTPROGS="testcases/pty/pty"
+export ptyNODELAY=1
+export ptyLOAD=100
diff --git a/tools/test/stress2/run.sh b/tools/test/stress2/run.sh
new file mode 100755
index 000000000000..4a63dacb7e7e
--- /dev/null
+++ b/tools/test/stress2/run.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Run 1) with no argument: the default test cases
+# 2) with the "-a" arguments all the *.cfg test variations
+# 3) with one argument: that specific test case
+
+while getopts a name; do
+ case $name in
+ a) aflag=1;;
+ ?) printf "Usage: %s: [-a] [arg]\n" $0
+ exit 2;;
+ esac
+done
+shift $(($OPTIND - 1))
+
+[ -x ./testcases/run/run ] ||
+ { echo "Please run \"make\" first." && exit 1; }
+[ `basename ${stress2origin:-X}` != misc ] &&
+ echo "Note: all.sh in stress2/misc is the preferred test to run." \
+ 1>&2
+find ./testcases -perm -1 \( -name "*.debug" -o -name "*.full" \) -delete \
+ 2>/dev/null
+if [ ! -z "$aflag" ]; then
+ . ./default.cfg
+ export runRUNTIME=5m
+ t1=`date '+%s'`
+ while true;do
+ for i in `ls *.cfg | grep -v default`; do
+ t2=`date '+%s'`
+ e=` date -u -j -f '%s' '+%T' $((t2 - t1))`
+ echo "`date '+%Y%m%d %T'` $i, elapsed $e"
+ echo "`date '+%Y%m%d %T'` $i, elapsed $e" >> /tmp/all.log
+ logger "Starting test $i"
+ [ -w /dev/console ] &&
+ printf "`date '+%Y%m%d %T'` run $i\r\n" > /dev/console
+ $0 $i
+ done
+ done
+else
+ CONFIG=./default.cfg
+ if [ $# -eq 1 ]; then
+ [ ! -r $1 ] && echo "$0: $1 not found!" && exit 1
+ CONFIG=$1
+ fi
+ . $CONFIG
+
+ [ -z "$RUNDIR" ] && echo "$0: RUNDIR is unset!" && exit 1
+ [ `basename $RUNDIR` != stressX ] && \
+ echo "$0: Basename of RUNDIR must be stressX!" && exit 2
+ [ -d "$RUNDIR" ] && (cd $RUNDIR && find . -delete)
+
+ [ -z "$EXCLUDETESTS" ] && EXCLUDETESTS=DuMmY
+
+ [ -z "$TESTPROGS" ] && \
+ TESTPROGS=`find testcases/ -perm -1 -type f | \
+ egrep -v "/run/|$EXCLUDETESTS|\.full|\.debug"`
+ ./testcases/run/run $TESTPROGS
+fi
diff --git a/tools/test/stress2/rw.cfg b/tools/test/stress2/rw.cfg
new file mode 100644
index 000000000000..5091266c1b28
--- /dev/null
+++ b/tools/test/stress2/rw.cfg
@@ -0,0 +1,8 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+export TESTPROGS="testcases/rw/rw"
+export rwNODELAY=1
+export rwLOAD=100
diff --git a/tools/test/stress2/syscall.cfg b/tools/test/stress2/syscall.cfg
new file mode 100644
index 000000000000..b4edc1ef4071
--- /dev/null
+++ b/tools/test/stress2/syscall.cfg
@@ -0,0 +1,11 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+export RUNTIME=15s
+export TESTPROGS="testcases/swap/swap testcases/syscall/syscall"
+
+export syscallKILL=1
+export syscallNODELAY=1
+export syscallLOAD=100
diff --git a/tools/test/stress2/sysctl.cfg b/tools/test/stress2/sysctl.cfg
new file mode 100644
index 000000000000..17a7954d96f5
--- /dev/null
+++ b/tools/test/stress2/sysctl.cfg
@@ -0,0 +1,12 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+export RUNTIME=15s
+export TESTPROGS="testcases/swap/swap testcases/sysctl/sysctl testcases/mmap/mmap"
+
+export sysctlKILL=1
+export sysctlNODELAY=1
+export sysctlLOAD=100
+export mmapLOAD=100
diff --git a/tools/test/stress2/testcases/Makefile b/tools/test/stress2/testcases/Makefile
new file mode 100644
index 000000000000..ad1c0eec0085
--- /dev/null
+++ b/tools/test/stress2/testcases/Makefile
@@ -0,0 +1,28 @@
+SUBDIR= \
+badcode \
+creat \
+dirrename \
+dirnprename \
+fts \
+link \
+lockf \
+lockf2 \
+mkdir \
+mkfifo \
+mmap \
+openat \
+pty \
+rename \
+run \
+rw \
+shm \
+socket \
+swap \
+symlink \
+sysctl \
+tcp \
+thr1 \
+thr2 \
+udp
+
+.include <bsd.subdir.mk>
diff --git a/tools/test/stress2/testcases/Makefile.inc b/tools/test/stress2/testcases/Makefile.inc
new file mode 100644
index 000000000000..594cefb61f06
--- /dev/null
+++ b/tools/test/stress2/testcases/Makefile.inc
@@ -0,0 +1,6 @@
+NO_OBJ=
+LDFLAGS+=-L${.OBJDIR}/../../lib
+CFLAGS+=-I${.CURDIR}/../../include -I${.CURDIR}/../include
+LDADD+=-lstress
+
+MAN=
diff --git a/tools/test/stress2/testcases/README b/tools/test/stress2/testcases/README
new file mode 100644
index 000000000000..6604835cf76f
--- /dev/null
+++ b/tools/test/stress2/testcases/README
@@ -0,0 +1,9 @@
+All test programs must implement these three procedures:
+
+int setup(int nb)
+void cleanup(void)
+int test(void)
+
+The setup() procedure is called once before test() is called multiple times
+until timeout. After timeout cleanup() is called.
+test() should periodically test the global variable done_testing and return.
diff --git a/tools/test/stress2/testcases/badcode/Makefile b/tools/test/stress2/testcases/badcode/Makefile
new file mode 100644
index 000000000000..a7b983e75984
--- /dev/null
+++ b/tools/test/stress2/testcases/badcode/Makefile
@@ -0,0 +1,3 @@
+PROG= badcode
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/badcode/badcode.c b/tools/test/stress2/testcases/badcode/badcode.c
new file mode 100644
index 000000000000..a468b99c0167
--- /dev/null
+++ b/tools/test/stress2/testcases/badcode/badcode.c
@@ -0,0 +1,133 @@
+/*
+ * COPYRIGHT (c) 1990 BY *
+ * GEORGE J. CARRETTE, CONCORD, MASSACHUSETTS. *
+ * ALL RIGHTS RESERVED *
+
+Permission to use, copy, modify, distribute and sell this software
+and its documentation for any purpose and without fee is hereby
+granted, provided that the above copyright notice appear in all copies
+and that both that copyright notice and this permission notice appear
+in supporting documentation, and that the name of the author
+not be used in advertising or publicity pertaining to distribution
+of the software without specific, written prior permission.
+
+THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
+ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
+HE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
+ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+SOFTWARE.
+
+This code is based on crashme.c
+
+*/
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/param.h>
+#include <sys/resource.h>
+#include <sys/time.h>
+#include <err.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+static pid_t pid;
+static int failsafe;
+
+static int
+tobemangled(void) {
+ volatile int i, j;
+
+ j = 2;
+ for (i = 0; i < 100; i++)
+ j = j + 3;
+ j = j / 2;
+
+ return (j);
+}
+
+static void
+mangle(void) { /* Change one byte in the code */
+ int i;
+ char *p = (void *)tobemangled;
+
+ i = arc4random() % 50;
+ p[i] = arc4random() & 0xff;
+}
+
+static void
+hand(int i __unused) { /* handler */
+ _exit(1);
+}
+
+static void
+ahand(int i __unused) { /* alarm handler */
+ if (pid != 0) {
+ kill(pid, SIGKILL);
+ }
+ _exit(EXIT_SUCCESS);
+}
+
+int
+setup(int nb __unused)
+{
+ return (0);
+}
+
+void
+cleanup(void)
+{
+}
+
+int
+test(void)
+{
+ void *st;
+ struct rlimit rl;
+
+ if (failsafe)
+ return (0);
+
+ while (done_testing == 0) {
+ signal(SIGALRM, ahand);
+ alarm(3);
+ if ((pid = fork()) == 0) {
+ rl.rlim_max = rl.rlim_cur = 0;
+ if (setrlimit(RLIMIT_CORE, &rl) == -1)
+ warn("setrlimit");
+ st = (void *)trunc_page((unsigned long)tobemangled);
+ if (mprotect(st, PAGE_SIZE, PROT_WRITE | PROT_READ |
+ PROT_EXEC) == -1)
+ err(1, "mprotect()");
+
+ signal(SIGALRM, hand);
+ signal(SIGILL, hand);
+ signal(SIGFPE, hand);
+ signal(SIGSEGV, hand);
+ signal(SIGBUS, hand);
+ signal(SIGURG, hand);
+ signal(SIGSYS, hand);
+ signal(SIGTRAP, hand);
+
+ mangle();
+ failsafe = 1;
+ (void)tobemangled();
+
+ _exit(EXIT_SUCCESS);
+
+ } else if (pid > 0) {
+ if (waitpid(pid, NULL, 0) == -1)
+ warn("waitpid(%d)", pid);
+ alarm(0);
+ } else
+ err(1, "fork(), %s:%d", __FILE__, __LINE__);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/creat/Makefile b/tools/test/stress2/testcases/creat/Makefile
new file mode 100644
index 000000000000..2c536bb10f74
--- /dev/null
+++ b/tools/test/stress2/testcases/creat/Makefile
@@ -0,0 +1,3 @@
+PROG= creat
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/creat/creat.c b/tools/test/stress2/testcases/creat/creat.c
new file mode 100644
index 000000000000..31d36a803ead
--- /dev/null
+++ b/tools/test/stress2/testcases/creat/creat.c
@@ -0,0 +1,136 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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/param.h>
+#include <sys/stat.h>
+#include <sys/mount.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+static char path[128];
+static unsigned long size;
+
+int
+setup(int nb)
+{
+ int pct;
+ int64_t in;
+ int64_t bl;
+ int64_t reserve_in;
+ int64_t reserve_bl;
+
+ umask(0);
+ path[0] = 0;
+ if (nb == 0) {
+ getdf(&bl, &in);
+ size = in / op->incarnations;
+
+ pct = 90;
+ if (op->hog == 0)
+ pct = random_int(1, 90);
+ size = size / 100 * pct + 1;
+
+ if (size > 1000)
+ size = 1000; /* Due to Soft Update lag */
+
+ /* Resource requirements: */
+ while (size > 0) {
+ reserve_in = 1 * size * op->incarnations + size;
+ reserve_bl = 29 * size * op->incarnations;
+ if (reserve_bl <= bl && reserve_in <= in)
+ break;
+ size--;
+ }
+ if (size == 0)
+ reserve_bl = reserve_in = 0;
+
+ if (op->verbose > 1)
+ printf("creat(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
+ size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
+ reservedf(reserve_bl, reserve_in);
+ putval(size);
+ } else {
+ size = getval();
+ }
+
+ if (size == 0)
+ exit(0);
+ sprintf(path,"%s.%05d", getprogname(), getpid());
+ if (mkdir(path, 0770) < 0)
+ err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);
+
+ if (chdir(path) == -1)
+ err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
+
+ return (0);
+}
+
+void
+cleanup(void)
+{
+ (void)chdir("..");
+ if (path[0] != 0 && rmdir(path) == -1) {
+ warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);
+ }
+}
+
+int
+test(void)
+{
+ int fd, i, j;
+ pid_t pid;
+ char file[128];
+
+ pid = getpid();
+ for (j = 0; j < (int)size && done_testing == 0; j++) {
+ sprintf(file,"p%05d.%05d", pid, j);
+ if ((fd = creat(file, 0660)) == -1) {
+ if (errno != EINTR) {
+ warn("creat(%s). %s:%d", file, __FILE__, __LINE__);
+ break;
+ }
+ }
+ if (fd != -1 && close(fd) == -1)
+ err(2, "close(%d)", j);
+
+ }
+
+ for (i = --j; i >= 0; i--) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if (unlink(file) == -1)
+ warn("unlink(%s)", file);
+
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/dirnprename/Makefile b/tools/test/stress2/testcases/dirnprename/Makefile
new file mode 100644
index 000000000000..c51be3fd232e
--- /dev/null
+++ b/tools/test/stress2/testcases/dirnprename/Makefile
@@ -0,0 +1,3 @@
+PROG=dirnprename
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/dirnprename/dirnprename.c b/tools/test/stress2/testcases/dirnprename/dirnprename.c
new file mode 100644
index 000000000000..a5d74cfe5c5c
--- /dev/null
+++ b/tools/test/stress2/testcases/dirnprename/dirnprename.c
@@ -0,0 +1,172 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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/param.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "stress.h"
+
+static char path[128];
+static unsigned long size;
+
+int
+setup(int nb)
+{
+ char file1[512];
+ int64_t in;
+ int64_t bl;
+ int64_t reserve_in;
+ int64_t reserve_bl;
+ int i;
+
+ umask(0);
+
+ if (nb == 0) {
+ getdf(&bl, &in);
+ size = in / op->incarnations;
+
+ if (size > 1000)
+ size = 1000; /* arbitrary limit number of files pr. dir */
+
+ /* Resource requirements: */
+ while (size > 0) {
+ reserve_in = 2 * size * op->incarnations + 2 * op->incarnations;
+ reserve_bl = 30 * size * op->incarnations;
+ if (reserve_bl <= bl && reserve_in <= in)
+ break;
+ size = size / 2;
+ }
+ if (size == 0)
+ reserve_bl = reserve_in = 0;
+
+ if (op->verbose > 1)
+ printf("rename(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
+ size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
+ reservedf(reserve_bl, reserve_in);
+ putval(size);
+ } else {
+ size = getval();
+ }
+ if (size == 0)
+ exit(0);
+
+ sprintf(path, "%s.%05d", getprogname(), getpid());
+ if (mkdir(path, 0770) == -1)
+ err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);
+
+ /*
+ * The value 97 was determined by figuring out how many 255 length
+ * names would cause us to overflow into indirect blocks w/ the
+ * default UFS parameters.
+ */
+ for (i = 0; i < 97; i++) {
+ sprintf(file1, "%s/%0255d", path, i);
+ if (mkdir(file1, 0770) == -1)
+ err(1, "mkdir(%s), %s:%d", file1, __FILE__, __LINE__);
+ }
+ return (0);
+}
+
+void
+cleanup(void)
+{
+ char file1[512];
+ int i;
+
+ /* see comment above */
+ for (i = 0; i < 97; i++) {
+ sprintf(file1, "%s/%0255d", path, i);
+ if (rmdir(file1) == -1)
+ warn("rmdir(%s), %s:%d", file1, __FILE__, __LINE__);
+ }
+ if (rmdir(path) == -1)
+ warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);
+}
+
+static void
+test_rename(void)
+{
+ int i, j;
+ int errnotmp;
+ pid_t pid;
+ char file1[128];
+ char file2[128];
+
+ pid = getpid();
+ for (i = 0; i < (int)size; i++) {
+ sprintf(file1,"p%05d.%05d", pid, i);
+ if (mkdir(file1, 0660) == -1) {
+ j = i;
+ errnotmp = errno;
+ while (j > 0) {
+ j--;
+ sprintf(file1,"p%05d.%05d", pid, j);
+ rmdir(file1);
+ }
+ errno = errnotmp;
+ sprintf(file1,"p%05d.%05d", pid, i);
+ err(1, "mkdir(%s), %s:%d", file1, __FILE__, __LINE__);
+ }
+ }
+ for (j = 0; j < 100 && done_testing == 0; j++) {
+ for (i = 0; i < (int)size; i++) {
+ sprintf(file1,"p%05d.%05d", pid, i);
+ sprintf(file2,"%s/p%05d.%05d.togo", path, pid, i);
+ if (rename(file1, file2) == -1)
+ err(1, "rename(%s, %s). %s:%d", file1, file2,
+ __FILE__, __LINE__);
+ }
+ for (i = 0; i < (int)size; i++) {
+ sprintf(file1,"p%05d.%05d", pid, i);
+ sprintf(file2,"%s/p%05d.%05d.togo", path, pid, i);
+ if (rename(file2, file1) == -1)
+ err(1, "rename(%s, %s). %s:%d", file2, file1,
+ __FILE__, __LINE__);
+ }
+ }
+
+ for (i = 0; i < (int)size; i++) {
+ sprintf(file1,"p%05d.%05d", pid, i);
+ if (rmdir(file1) == -1)
+ err(1, "rmdir(%s), %s:%d", file1, __FILE__, __LINE__);
+ }
+}
+
+int
+test(void)
+{
+ test_rename();
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/dirrename/Makefile b/tools/test/stress2/testcases/dirrename/Makefile
new file mode 100644
index 000000000000..0de876cc0a6c
--- /dev/null
+++ b/tools/test/stress2/testcases/dirrename/Makefile
@@ -0,0 +1,3 @@
+PROG= dirrename
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/dirrename/dirrename.c b/tools/test/stress2/testcases/dirrename/dirrename.c
new file mode 100644
index 000000000000..3c1d3f11f240
--- /dev/null
+++ b/tools/test/stress2/testcases/dirrename/dirrename.c
@@ -0,0 +1,144 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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/param.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+static unsigned long size;
+
+int
+setup(int nb)
+{
+ int64_t in;
+ int64_t bl;
+ int64_t reserve_in;
+ int64_t reserve_bl;
+
+ umask(0);
+
+ if (nb == 0) {
+ getdf(&bl, &in);
+ size = in / op->incarnations;
+
+ if (size > 1000)
+ size = 1000; /* arbitrary limit number of files pr. dir */
+
+ /* Resource requirements: */
+ while (size > 0) {
+ reserve_in = 2 * size * op->incarnations + 2 * op->incarnations;
+ reserve_bl = 30 * size * op->incarnations;
+ if (reserve_bl <= bl && reserve_in <= in)
+ break;
+ size = size / 2;
+ }
+ if (size == 0)
+ reserve_bl = reserve_in = 0;
+
+ if (op->verbose > 1)
+ printf("rename(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
+ size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
+ reservedf(reserve_bl, reserve_in);
+ putval(size);
+ } else {
+ size = getval();
+ }
+ if (size == 0)
+ exit(0);
+
+ return (0);
+}
+
+void
+cleanup(void)
+{
+}
+
+static void
+test_rename(void)
+{
+ int i, j;
+ int errnotmp;
+ pid_t pid;
+ char file1[128];
+ char file2[128];
+
+ pid = getpid();
+ for (i = 0; i < (int)size; i++) {
+ sprintf(file1,"p%05d.%05d", pid, i);
+ if (mkdir(file1, 0660) == -1) {
+ j = i;
+ errnotmp = errno;
+ while (j > 0) {
+ j--;
+ sprintf(file1,"p%05d.%05d", pid, j);
+ rmdir(file1);
+ }
+ errno = errnotmp;
+ sprintf(file1,"p%05d.%05d", pid, i);
+ err(1, "mkdir(%s), %s:%d", file1, __FILE__, __LINE__);
+ }
+ }
+ for (j = 0; j < 100 && done_testing == 0; j++) {
+ for (i = 0; i < (int)size; i++) {
+ sprintf(file1,"p%05d.%05d", pid, i);
+ sprintf(file2,"p%05d.%05d.togo", pid, i);
+ if (rename(file1, file2) == -1)
+ err(1, "rename(%s, %s). %s:%d", file1, file2,
+ __FILE__, __LINE__);
+ }
+ for (i = 0; i < (int)size; i++) {
+ sprintf(file1,"p%05d.%05d", pid, i);
+ sprintf(file2,"p%05d.%05d.togo", pid, i);
+ if (rename(file2, file1) == -1)
+ err(1, "rename(%s, %s). %s:%d", file2, file1,
+ __FILE__, __LINE__);
+ }
+ }
+
+ for (i = 0; i < (int)size; i++) {
+ sprintf(file1,"p%05d.%05d", pid, i);
+ if (rmdir(file1) == -1)
+ err(1, "unlink(%s), %s:%d", file1, __FILE__, __LINE__);
+ }
+}
+
+int
+test(void)
+{
+ test_rename();
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/fts/Makefile b/tools/test/stress2/testcases/fts/Makefile
new file mode 100644
index 000000000000..aac58faf4a6c
--- /dev/null
+++ b/tools/test/stress2/testcases/fts/Makefile
@@ -0,0 +1,3 @@
+PROG= fts
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/fts/fts.c b/tools/test/stress2/testcases/fts/fts.c
new file mode 100644
index 000000000000..591efbf7498a
--- /dev/null
+++ b/tools/test/stress2/testcases/fts/fts.c
@@ -0,0 +1,102 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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.
+ *
+ */
+
+/* Directory scan */
+
+#include <sys/param.h>
+#include <err.h>
+#include <errno.h>
+#include <fts.h>
+#include <libutil.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+int
+setup(int nb __unused)
+{
+ return (0);
+}
+
+void
+cleanup(void)
+{
+}
+
+int
+test(void)
+{
+
+ FTS *fts;
+ FTSENT *p;
+ int ftsoptions;
+ char *args[2] = {NULL, NULL};
+
+ ftsoptions = FTS_LOGICAL;
+ args[0] = strdup(".");
+
+ if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
+ err(1, "fts_open");
+
+ while ((p = fts_read(fts)) != NULL && done_testing == 0) {
+ if (op->verbose > 2)
+ (void) printf("%s\n", p->fts_path);
+ switch (p->fts_info) {
+ case FTS_F: /* Ignore. */
+ break;
+ case FTS_D: /* Ignore. */
+ break;
+ case FTS_DP:
+ break;
+ case FTS_DC: /* Ignore. */
+ break;
+ case FTS_SL: /* Ignore. */
+ break;
+ case FTS_SLNONE: /* Ignore. */
+ break;
+ case FTS_DNR: /* Warn, continue. */
+ case FTS_ERR:
+ case FTS_NS:
+ case FTS_DEFAULT:
+ if (op->verbose > 1)
+ warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
+ break;
+ default:
+ printf("%s: default, %d\n", getprogname(), p->fts_info);
+ break;
+ }
+ }
+
+ if (fts_close(fts) == -1)
+ err(1, "fts_close()");
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/link/Makefile b/tools/test/stress2/testcases/link/Makefile
new file mode 100644
index 000000000000..6d63fa6434c0
--- /dev/null
+++ b/tools/test/stress2/testcases/link/Makefile
@@ -0,0 +1,3 @@
+PROG= link
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/link/link.c b/tools/test/stress2/testcases/link/link.c
new file mode 100644
index 000000000000..30eb9249c550
--- /dev/null
+++ b/tools/test/stress2/testcases/link/link.c
@@ -0,0 +1,144 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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/param.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+static unsigned long size;
+static char path[128];
+
+int
+setup(int nb)
+{
+ int64_t bl;
+ int64_t in;
+ int64_t reserve_bl;
+ int64_t reserve_in;
+ int pct;
+
+ umask(0);
+ path[0] = 0;
+ if (nb == 0) {
+ getdf(&bl, &in);
+ size = in / op->incarnations;
+
+ pct = 90;
+ if (op->hog == 0)
+ pct = random_int(1, 90);
+ size = size / 100 * pct + 1;
+
+ if (size > 32000 && op->hog == 0)
+ size = 32000; /* arbitrary limit number of files pr. dir */
+
+ /* Resource requirements: */
+ reserve_in = 2 * op->incarnations + 7;
+ reserve_bl = 26 * size * op->incarnations;
+ if (reserve_in > in || reserve_bl > bl)
+ size = reserve_in = reserve_bl = 0;
+
+ if (op->verbose > 1)
+ printf("link(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
+ size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
+ reservedf(reserve_bl, reserve_in);
+ putval(size);
+ } else {
+ size = getval();
+ }
+ if (size == 0)
+ exit (1);
+
+ sprintf(path,"%s.%05d", getprogname(), getpid());
+ if (mkdir(path, 0770) < 0)
+ err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);
+
+ if (chdir(path) == -1)
+ err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
+
+ return (0);
+}
+
+void
+cleanup(void)
+{
+ if (size == 0)
+ return;
+ (void)chdir("..");
+ if (path[0] != 0 && rmdir(path) == -1)
+ warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);
+}
+
+int
+test(void)
+{
+ pid_t pid;
+ int fd, i, j;
+ char file[128];
+ char lfile[128];
+
+ pid = getpid();
+ for (j = 0; j < (int)size && done_testing == 0; j++) {
+ sprintf(file,"p%05d.%05d", pid, j);
+ if (j == 0) {
+ if ((fd = creat(file, 0660)) == -1) {
+ if (errno != EINTR) {
+ warn("creat(%s)", file);
+ break;
+ }
+ }
+ if (fd != -1 && close(fd) == -1)
+ err(2, "close(%d)", j);
+ strcpy(lfile, file);
+ } else {
+ if (link(lfile, file) == -1) {
+ if (errno != EINTR) {
+ warn("link(%s, %s)", lfile, file);
+ break;
+ }
+ }
+ }
+
+ }
+
+ for (i = --j; i >= 0; i--) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if (unlink(file) == -1)
+ err(3, "unlink(%s)", file);
+
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/lockf/Makefile b/tools/test/stress2/testcases/lockf/Makefile
new file mode 100644
index 000000000000..af962546b619
--- /dev/null
+++ b/tools/test/stress2/testcases/lockf/Makefile
@@ -0,0 +1,3 @@
+PROG= lockf
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/lockf/lockf.c b/tools/test/stress2/testcases/lockf/lockf.c
new file mode 100644
index 000000000000..510adf230234
--- /dev/null
+++ b/tools/test/stress2/testcases/lockf/lockf.c
@@ -0,0 +1,184 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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.
+ *
+ */
+
+/* Test lockf(3) */
+
+#include <sys/param.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+static pid_t pid;
+static int fd;
+static int freespace;
+static char file[128];
+
+static int
+get(void) {
+ int r, sem;
+
+ do {
+ r = lockf(fd, F_LOCK, 0);
+ } while (r == -1 && errno == EINTR && done_testing == 0);
+ if (r == -1)
+ err(1, "lockf(%s, F_LOCK)", file);
+ if (lseek(fd, 0, SEEK_SET) == -1) // XXX
+ err(1, "lseek"); // XXX
+ r = read(fd, &sem, sizeof(sem));
+ if (r == -1)
+ err(1, "get: read(%d)", fd);
+ if (r == 0)
+ errx(1, "get() read 0 bytes");
+ if (r != sizeof(sem))
+ errx(1, "get() size error: %d", r);
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ err(1, "lseek");
+ if (lockf(fd, F_ULOCK, 0) == -1)
+ err(1, "lockf(%s, F_ULOCK)", file);
+ return (sem);
+}
+
+static void
+incr(void) {
+ int r, sem;
+
+ do {
+ r = lockf(fd, F_LOCK, 0);
+ } while (r == -1 && errno == EINTR && done_testing == 0);
+ if (r == -1)
+ err(1, "lockf(%s, F_LOCK)", file);
+ if (read(fd, &sem, sizeof(sem)) != sizeof(sem))
+ err(1, "incr: read(%d)", fd);
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ err(1, "lseek");
+ sem++;
+ if (write(fd, &sem, sizeof(sem)) != sizeof(sem))
+ err(1, "incr: read");
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ err(1, "lseek");
+ if (lockf(fd, F_ULOCK, 0) == -1)
+ err(1, "lockf(%s, F_ULOCK)", file);
+}
+
+int
+setup(int nb)
+{
+ int64_t bl;
+ int64_t in;
+ int64_t reserve_bl;
+ int64_t reserve_in;
+
+ if (nb == 0) {
+ getdf(&bl, &in);
+
+ /* Resource requirements: */
+ reserve_in = 1 * op->incarnations;
+ reserve_bl = 4096 * op->incarnations;
+ freespace = (reserve_bl <= bl && reserve_in <= in);
+ if (!freespace)
+ reserve_bl = reserve_in = 0;
+
+ if (op->verbose > 1)
+ printf("lockf(incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
+ op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
+ reservedf(reserve_bl, reserve_in);
+ putval(freespace);
+ } else {
+ freespace = getval();
+ }
+ if (!freespace)
+ exit(0);
+
+ return (0);
+}
+
+void
+cleanup(void)
+{
+}
+
+int
+test(void)
+{
+ int i;
+ int sem = 0;
+
+ sprintf(file, "lockf.0.%d", getpid());
+ if ((fd = open(file,O_CREAT | O_TRUNC | O_RDWR, 0600)) == -1) {
+ if (errno == ENOENT)
+ return (0);
+ else
+ err(1, "creat(%s) %s:%d", file, __FILE__, __LINE__);
+ }
+ if (write(fd, &sem, sizeof(sem)) != sizeof(sem))
+ err(1, "write");
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ err(1, "lseek");
+
+ pid = fork();
+ if (pid == -1) {
+ perror("fork");
+ exit(2);
+ }
+
+ if (pid == 0) { /* child */
+ alarm(60);
+ for (i = 0; i < 100 && done_testing == 0; i++) {
+ while ((get() & 1) == 0 && done_testing == 0)
+ ;
+ if (op->verbose > 3)
+ printf("Child %d, sem = %d\n", i, get()),
+ fflush(stdout);
+ incr();
+ }
+ _exit(0);
+ } else { /* parent */
+ for (i = 0; i < 100 && done_testing == 0; i++) {
+ while ((get() & 1) == 1 && done_testing == 0)
+ ;
+ if (op->verbose > 3)
+ printf("Parent %d, sem = %d\n", i, get()),
+ fflush(stdout);
+ incr();
+ }
+ }
+ close(fd);
+ if (done_testing == 1)
+ kill(pid, SIGHUP);
+ waitpid(pid, &i, 0);
+ unlink(file);
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/lockf2/Makefile b/tools/test/stress2/testcases/lockf2/Makefile
new file mode 100644
index 000000000000..d8bd1a0ad6a5
--- /dev/null
+++ b/tools/test/stress2/testcases/lockf2/Makefile
@@ -0,0 +1,3 @@
+PROG= lockf2
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/lockf2/lockf2.c b/tools/test/stress2/testcases/lockf2/lockf2.c
new file mode 100644
index 000000000000..9b4df38d995b
--- /dev/null
+++ b/tools/test/stress2/testcases/lockf2/lockf2.c
@@ -0,0 +1,130 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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.
+ *
+ */
+
+/* Test lockf(3) with overlapping ranges */
+
+/* Provoked:
+ lock order reversal:
+ 1st 0xc50057a0 vnode interlock (vnode interlock) @ kern/kern_lockf.c:190
+ 2nd 0xc14710e8 system map (system map) @ vm/vm_kern.c:296
+ */
+
+#include <sys/types.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+static char file[128];
+static int fd;
+static int freespace;
+
+int
+setup(int nb)
+{
+ int64_t bl;
+ int64_t in;
+ int64_t reserve_bl;
+ int64_t reserve_in;
+ int i;
+ char buf[1024];
+
+ if (nb == 0) {
+ getdf(&bl, &in);
+
+ /* Resource requirements: */
+ reserve_in = 1 * op->incarnations;
+ reserve_bl = 1081344 * op->incarnations;
+ freespace = (reserve_bl <= bl && reserve_in <= in);
+ if (!freespace)
+ reserve_bl = reserve_in = 0;
+
+ if (op->verbose > 1)
+ printf("lockf2(incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
+ op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
+ reservedf(reserve_bl, reserve_in);
+ putval(freespace);
+ } else {
+ freespace = getval();
+ }
+ if (!freespace)
+ exit (0);
+
+ sprintf(file, "lockf.%d", getpid());
+ if ((fd = open(file,O_CREAT | O_TRUNC | O_RDWR, 0600)) == -1)
+ err(1, "creat(%s)", file);
+ bzero(buf, sizeof(buf));
+ for (i = 0; i < 1024; i++)
+ if (write(fd, &buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write");
+ close(fd);
+ return (0);
+}
+
+void
+cleanup(void)
+{
+ unlink(file);
+}
+
+int
+test(void)
+{
+ off_t pos;
+ off_t size;
+ int i, r;
+
+ if ((fd = open(file, O_RDWR, 0600)) == -1)
+ err(1, "open(%s)", file);
+
+ for (i = 0; i < 1024 && done_testing == 0; i++) {
+ pos = random_int(0, 1024 * 1024 - 1);
+ if (lseek(fd, pos, SEEK_SET) == -1)
+ err(1, "lseek");
+ size = random_int(1, 1024 * 1024 - pos);
+ if (size > 64)
+ size = 64;
+ do {
+ r = lockf(fd, F_LOCK, size);
+ } while (r == -1 && errno == EINTR);
+ if (r == -1)
+ err(1, "lockf(%s, F_LOCK)", file);
+ size = random_int(1, size);
+ if (lockf(fd, F_ULOCK, size) == -1)
+ err(1, "lockf(%s, F_ULOCK)", file);
+
+ }
+ close(fd);
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/mkdir/Makefile b/tools/test/stress2/testcases/mkdir/Makefile
new file mode 100644
index 000000000000..baa4bc3e210d
--- /dev/null
+++ b/tools/test/stress2/testcases/mkdir/Makefile
@@ -0,0 +1,3 @@
+PROG= mkdir
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/mkdir/mkdir.c b/tools/test/stress2/testcases/mkdir/mkdir.c
new file mode 100644
index 000000000000..ee625b91aa8b
--- /dev/null
+++ b/tools/test/stress2/testcases/mkdir/mkdir.c
@@ -0,0 +1,135 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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/types.h>
+#include <sys/sysctl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/param.h>
+#include <err.h>
+
+#include "stress.h"
+
+static unsigned long size;
+
+int
+setup(int nb)
+{
+ int64_t in;
+ int64_t bl;
+ int64_t reserve_in;
+ int64_t reserve_bl;
+ int pct;
+
+ if (nb == 0) {
+ getdf(&bl, &in);
+ size = in / op->incarnations;
+
+ pct = 90;
+ if (op->hog == 0)
+ pct = random_int(1, 90);
+ size = size / 100 * pct + 1;
+
+ size = size % 200; /* arbitrary limit depth */
+
+ /* Resource requirements: */
+ while (size > 0) {
+ reserve_in = 1 * size * op->incarnations;
+ reserve_bl = 4096 * size * op->incarnations;
+ if (reserve_bl <= bl && reserve_in <= in)
+ break;
+ size = size / 2;
+ }
+ if (size == 0)
+ reserve_bl = reserve_in = 0;
+
+ if (op->verbose > 1)
+ printf("mkdir(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
+ size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
+ reservedf(reserve_bl, reserve_in);
+ putval(size);
+ } else
+ size = getval();
+
+ if (size == 0)
+ exit(0);
+
+ return (0);
+}
+
+void
+cleanup(void)
+{
+}
+
+static void
+mkDir(char *path, int level) {
+ char newPath[MAXPATHLEN + 1];
+
+ if (mkdir(path, 0770) == -1) {
+ warn("mkdir(%s), level %d. %s:%d", path, level, __FILE__, __LINE__);
+ size = level;
+ } else
+ chdir(path);
+
+ if (done_testing == 1)
+ size = level;
+
+ if (level < (int)size) {
+ sprintf(newPath,"d%d", level+1);
+ mkDir(newPath, level+1);
+ }
+}
+
+static void
+rmDir(char *path, int level) {
+ char newPath[MAXPATHLEN + 1];
+
+ if (level < (int)size) {
+ sprintf(newPath,"d%d", level+1);
+ rmDir(newPath, level+1);
+ }
+ chdir ("..");
+ if (rmdir(path) == -1) {
+ err(1, "rmdir(%s), %s:%d", path, __FILE__, __LINE__);
+ }
+}
+
+int
+test(void)
+{
+ char path[MAXPATHLEN + 1];
+
+ umask(0);
+ sprintf(path,"p%05d.d%d", getpid(), 1);
+ mkDir(path, 1);
+ rmDir(path, 1);
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/mkfifo/Makefile b/tools/test/stress2/testcases/mkfifo/Makefile
new file mode 100644
index 000000000000..5e8de7c29756
--- /dev/null
+++ b/tools/test/stress2/testcases/mkfifo/Makefile
@@ -0,0 +1,4 @@
+PROG= mkfifo
+LIBADD+= util
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/mkfifo/mkfifo.c b/tools/test/stress2/testcases/mkfifo/mkfifo.c
new file mode 100644
index 000000000000..6bd20d6679f9
--- /dev/null
+++ b/tools/test/stress2/testcases/mkfifo/mkfifo.c
@@ -0,0 +1,183 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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/param.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <signal.h>
+#include <sys/wait.h>
+#include <string.h>
+#include <err.h>
+
+#include "stress.h"
+
+static char path[MAXPATHLEN+1];
+#define NB (1 * 1024 * 1024)
+
+static int bufsize;
+static int freespace;
+
+static void
+handler2(int i __unused)
+{
+ _exit(0);
+}
+
+static void
+reader(void) {
+ int fd;
+ int i, n, *buf;
+
+ setproctitle("reader");
+ signal(SIGALRM, handler2);
+ alarm(2);
+ if ((fd = open(path, O_RDWR, 0600)) < 0) {
+ unlink(path);
+ err(1, "open(%s)", path);
+ }
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+ for (i = 0; i < NB; i+= bufsize) {
+ if ((n = read(fd, buf, bufsize)) < 0)
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ if (n == 0) break;
+ }
+ close(fd);
+ free(buf);
+ return;
+}
+
+static void
+writer(void) {
+ int i, *buf;
+ int fd;
+
+ signal(SIGALRM, handler2);
+ alarm(2);
+
+ setproctitle("writer");
+ if ((fd = open(path, O_RDWR, 0600)) < 0) {
+ unlink(path);
+ err(1, "open(%s)", path);
+ }
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+ for (i = 0; i < bufsize / (int)sizeof(int); i++)
+ buf[i] = i;
+
+ for (i = 0; i < NB; i+= bufsize) {
+ if (write(fd, buf, bufsize) < 0) {
+ err(1, "write(%d), %s:%d", fd,
+ __FILE__, __LINE__);
+ }
+ }
+ close(fd);
+ free(buf);
+ return;
+}
+
+int
+setup(int nb)
+{
+ int64_t bl;
+ int64_t in;
+ int64_t reserve_bl;
+ int64_t reserve_in;
+
+ if (nb == 0) {
+ getdf(&bl, &in);
+
+ /* Resource requirements: */
+ reserve_in = 200 * op->incarnations;
+ reserve_bl = 2048 * op->incarnations;
+ freespace = (reserve_bl <= bl && reserve_in <= in);
+ if (!freespace)
+ reserve_bl = reserve_in = 0;
+
+ if (op->verbose > 1)
+ printf("mkfifo(incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
+ op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
+ reservedf(reserve_bl, reserve_in);
+ putval(freespace);
+ fflush(stdout);
+ } else {
+ freespace = getval();
+ }
+ if (!freespace)
+ _exit(0);
+ bufsize = 2 << random_int(2, 12);
+ return (0);
+}
+
+void
+cleanup(void)
+{
+}
+
+int
+test(void)
+{
+ pid_t pid;
+ int i, status;
+
+ for (i = 0; i < 100; i++) {
+ if (sprintf(path, "fifo.%d.%d", getpid(), i) < 0)
+ err(1, "sprintf()");
+ if (mkfifo(path, 0600) < 0)
+ err(1, "mkfifo(%s)", path);
+ }
+ for (i = 0; i < 100; i++) {
+ if (sprintf(path, "fifo.%d.%d", getpid(), i) < 0)
+ err(1, "sprintf()");
+ if (unlink(path) < 0)
+ err(1, "unlink(%s)", path);
+ }
+
+ if (sprintf(path, "fifo.%d", getpid()) < 0)
+ err(1, "sprintf()");
+ if (mkfifo(path, 0600) < 0)
+ err(1, "mkfifo(%s)", path);
+
+ if ((pid = fork()) == 0) {
+ writer();
+ _exit(EXIT_SUCCESS);
+
+ } else if (pid > 0) {
+ reader();
+ kill(pid, SIGINT);
+ if (waitpid(pid, &status, 0) == -1)
+ warn("waitpid(%d)", pid);
+ } else
+ err(1, "fork(), %s:%d", __FILE__, __LINE__);
+
+ unlink(path);
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/mmap/Makefile b/tools/test/stress2/testcases/mmap/Makefile
new file mode 100644
index 000000000000..ded926fe0901
--- /dev/null
+++ b/tools/test/stress2/testcases/mmap/Makefile
@@ -0,0 +1,3 @@
+PROG= mmap
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/mmap/mmap.c b/tools/test/stress2/testcases/mmap/mmap.c
new file mode 100644
index 000000000000..349ffb73088f
--- /dev/null
+++ b/tools/test/stress2/testcases/mmap/mmap.c
@@ -0,0 +1,148 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+static char path[128];
+
+#define INPUTFILE "/bin/date"
+
+int
+setup(int nb)
+{
+ int64_t bl;
+ int64_t in;
+ int64_t reserve_bl;
+ int64_t reserve_in;
+ int freespace;
+
+ if (nb == 0) {
+ getdf(&bl, &in);
+
+ /* Resource requirements: */
+ reserve_in = 2 * op->incarnations;
+ reserve_bl = 20480 * op->incarnations;
+ freespace = (reserve_bl <= bl && reserve_in <= in);
+ if (!freespace)
+ reserve_bl = reserve_in = 0;
+
+ if (op->verbose > 1)
+ printf("mmap(incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
+ op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
+ reservedf(reserve_bl, reserve_in);
+ putval(freespace);
+ } else {
+ freespace = getval();
+ }
+ if (!freespace)
+ exit(0);
+ umask(0);
+
+ sprintf(path,"%s.%05d", getprogname(), getpid());
+ if (mkdir(path, 0770) < 0)
+ err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);
+
+ if (chdir(path) == -1)
+ err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
+
+ return (0);
+}
+
+void
+cleanup(void)
+{
+ (void)chdir("..");
+ if (rmdir(path) == -1) {
+ warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);
+ }
+}
+
+int
+test(void)
+{
+ struct stat statbuf;
+ pid_t pid;
+ char file[128];
+ int fdin, fdout;
+ int i;
+ char *src, *dst;
+
+ pid = getpid();
+ for (i = 0; i < 100 && done_testing == 0; i++) {
+ sprintf(file,"p%05d.%05d", pid, i);
+
+ if ((fdin = open(INPUTFILE, O_RDONLY)) < 0)
+ err(1, INPUTFILE);
+
+ if ((fdout = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
+ err(1, "%s", file);
+
+ if (fstat(fdin, &statbuf) < 0)
+ err(1, "fstat error");
+
+ if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1)
+ err(1, "lseek error");
+
+ /* write a dummy byte at the last location */
+ if (write(fdout, "", 1) != 1)
+ err(1, "write error");
+
+ if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0)) ==
+ (caddr_t) - 1)
+ err(1, "mmap error for input");
+
+ if ((dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE,
+ MAP_SHARED, fdout, 0)) == (caddr_t) - 1)
+ err(1, "mmap error for output");
+
+ memcpy(dst, src, statbuf.st_size);
+
+ if (munmap(src, statbuf.st_size) == -1)
+ err(1, "munmap");
+ close(fdin);
+
+ if (munmap(dst, statbuf.st_size) == -1)
+ err(1, "munmap");
+ close(fdout);
+
+ if (unlink(file) == -1)
+ err(3, "unlink(%s)", file);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/openat/Makefile b/tools/test/stress2/testcases/openat/Makefile
new file mode 100644
index 000000000000..f633fb111039
--- /dev/null
+++ b/tools/test/stress2/testcases/openat/Makefile
@@ -0,0 +1,3 @@
+PROG= openat
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/openat/doat.c b/tools/test/stress2/testcases/openat/doat.c
new file mode 100644
index 000000000000..8aafaa3366e6
--- /dev/null
+++ b/tools/test/stress2/testcases/openat/doat.c
@@ -0,0 +1,688 @@
+/*-
+ * Copyright (c) 2007 Roman Divacky <rdivacky@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 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/param.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+union param {
+ int i;
+ char *cp;
+ mode_t m;
+ dev_t d;
+ void *vp;
+ uid_t u;
+ gid_t g;
+ char **cpp;
+};
+
+struct testcase {
+ int result;
+ union param params[5]; // no *at syscall with more than 5 params
+};
+
+struct test {
+ int syscall;
+ int num_of_cases;
+ char *name;
+ struct testcase tests[10]; // no more than 10 tests
+
+};
+
+struct test *tests;
+#define NUM_OF_TESTS 15
+
+char *absolute_path = NULL;
+char *relative_path = "tmp/";
+char *not_dir_path = "/bin/date";
+
+char *file = "foo";
+char *absolute_file = NULL;
+char *relative_file = NULL;
+char *symlinkf = "link";
+char *newlink = "nlink1";
+char *newlink2 = "nlink2";
+char *newlink3 = "nlink3";
+char *newdir = "newdir";
+char *fifo = "fifo";
+char *nod = "nod";
+char *newfile = "newfile";
+char *newslink = "nslink1";
+
+bool dir_exist = false;
+bool file_exist = false;
+bool link_exist = false;
+
+int rel_fd, abs_fd, notd_fd, exec_fd;
+
+struct timeval times[2];
+struct stat buf;
+char *pargv[2] = { "/bin/date", NULL };
+char cbuf[PATH_MAX];
+
+void
+setup()
+{
+ int i, error;
+ struct stat sb;
+
+ tests = calloc(NUM_OF_TESTS, sizeof(struct test));
+ if (tests == NULL) {
+ perror("");
+ exit(0);
+ }
+
+ absolute_path = getcwd(NULL, 0);
+ if (absolute_path == NULL) {
+ perror("getcwd");
+ exit(0);
+ }
+
+ absolute_path = realloc(absolute_path, strlen(absolute_path) + 5);
+ if (absolute_path == NULL) {
+ perror("realloc");
+ exit(0);
+ }
+
+ strcat(absolute_path, "/");
+ strcat(absolute_path, relative_path);
+
+ absolute_file = malloc(strlen(absolute_path) + 1 + strlen(file));
+ bzero(absolute_file, strlen(absolute_path) + 1 + strlen(file));
+ if (absolute_file == NULL) {
+ perror("malloc");
+ exit(0);
+ }
+ strcpy(absolute_file, absolute_path);
+ absolute_file[strlen(absolute_file)] = '/';
+ strcpy(absolute_file + strlen(absolute_path), file);
+
+ printf("XX: %s\n", absolute_file);
+
+ relative_file = malloc(strlen(relative_path) + 1 + strlen(file));
+ bzero(relative_file, strlen(relative_path) + 1 + strlen(file));
+ if (relative_file == NULL) {
+ perror("malloc");
+ exit(0);
+ }
+ strcpy(relative_file, relative_path);
+ relative_file[strlen(relative_file)] = '/';
+ strcpy(relative_file + strlen(relative_path), file);
+
+ printf("YY: %s\n", relative_file);
+
+ error = mkdir(relative_path, 0744);
+ dir_exist = (errno == EEXIST);
+ if (error && errno != EEXIST) {
+ perror("tmp");
+ exit(0);
+ }
+
+ error = stat("tmp/foo", &sb);
+ file_exist = (errno != ENOENT);
+ i = open("tmp/foo", O_RDONLY | O_CREAT, 0644);
+ if (i == -1) {
+ perror("foo");
+ exit(0);
+ }
+
+ rel_fd = open(relative_path, O_RDONLY);
+ if (rel_fd == -1) {
+ perror("relative path");
+ exit(0);
+ }
+
+ abs_fd = open(absolute_path, O_RDONLY);
+ if (abs_fd == -1) {
+ perror("absolute path");
+ exit(0);
+ }
+
+ notd_fd = open(not_dir_path, O_RDONLY);
+ if (notd_fd == -1) {
+ perror("not a directory");
+ exit(0);
+ }
+
+ exec_fd = open(not_dir_path, O_RDONLY);
+ if (exec_fd == -1) {
+ perror("not a directory");
+ exit(0);
+ }
+
+ error = symlink(absolute_file, symlinkf);
+ link_exist = (errno == EEXIST);
+ if (error && errno != EEXIST) {
+ perror("symlink");
+ exit(0);
+ }
+
+ // faccessat
+ tests[0].syscall = SYS_faccessat;
+ tests[0].num_of_cases = 6;
+ tests[0].name = "faccessat";
+ tests[0].tests[0].result = EBADF;
+ tests[0].tests[0].params[0].i = 106; // invalid fd
+ tests[0].tests[0].params[1].cp = relative_path;
+ tests[0].tests[0].params[2].m = 0;
+ tests[0].tests[0].params[3].i = 0;
+ tests[0].tests[1].result = EBADF;
+ tests[0].tests[1].params[0].i = 106; // invalid fd
+ tests[0].tests[1].params[1].cp = relative_path;
+ tests[0].tests[1].params[2].m = 0;
+ tests[0].tests[1].params[3].i = AT_EACCESS;
+ tests[0].tests[2].result = EINVAL;
+ tests[0].tests[2].params[0].i = rel_fd;
+ tests[0].tests[2].params[1].cp = absolute_path;
+ tests[0].tests[2].params[2].m = 0;
+ tests[0].tests[2].params[3].i = 123; // invalid flag
+ tests[0].tests[3].result = ENOTDIR;
+ tests[0].tests[3].params[0].i = notd_fd;
+ tests[0].tests[3].params[1].cp = relative_file;
+ tests[0].tests[3].params[2].m = 0;
+ tests[0].tests[3].params[3].i = 0;
+ tests[0].tests[4].result = 0;
+ tests[0].tests[4].params[0].i = rel_fd;
+ tests[0].tests[4].params[1].cp = file;
+ tests[0].tests[4].params[2].m = 0;
+ tests[0].tests[4].params[3].i = 0;
+ tests[0].tests[5].result = 0;
+ tests[0].tests[5].params[0].i = rel_fd;
+ tests[0].tests[5].params[1].cp = file;
+ tests[0].tests[5].params[2].m = 0;
+ tests[0].tests[5].params[3].i = AT_EACCESS;
+ tests[0].tests[6].result = 0;
+ tests[0].tests[6].params[0].i = 106; // invalid fd
+ tests[0].tests[6].params[1].cp = absolute_path;
+ tests[0].tests[6].params[2].m = 0;
+ tests[0].tests[6].params[3].i = 0;
+
+ // fchmodat
+ tests[1].syscall = SYS_fchmodat;
+ tests[1].num_of_cases = 6;
+ tests[1].name = "fchmodat";
+ tests[1].tests[0].result = EBADF;
+ tests[1].tests[0].params[0].i = 106; // invalid fd
+ tests[1].tests[0].params[1].cp = relative_path;
+ tests[1].tests[0].params[2].m = 33190;
+ tests[1].tests[0].params[3].i = 0;
+ tests[1].tests[1].result = EINVAL;
+ tests[1].tests[1].params[0].i = rel_fd;
+ tests[1].tests[1].params[1].cp = absolute_path;
+ tests[1].tests[1].params[2].m = 33190; // mode 646 translated
+ tests[1].tests[1].params[3].i = 123; // invalid flag
+ tests[1].tests[2].result = ENOTDIR;
+ tests[1].tests[2].params[0].i = notd_fd;
+ tests[1].tests[2].params[1].cp = relative_file;
+ tests[1].tests[2].params[2].m = 33190;
+ tests[1].tests[2].params[3].i = 0;
+ tests[1].tests[3].result = 0;
+ tests[1].tests[3].params[0].i = notd_fd;
+ tests[1].tests[3].params[1].cp = absolute_file;
+ tests[1].tests[3].params[2].m = 33190;
+ tests[1].tests[3].params[3].i = 0;
+ tests[1].tests[4].result = 0;
+ tests[1].tests[4].params[0].i = AT_FDCWD;
+ tests[1].tests[4].params[1].cp = symlinkf;
+ tests[1].tests[4].params[2].m = 33190;
+ tests[1].tests[4].params[3].i = AT_SYMLINK_NOFOLLOW;
+ tests[1].tests[5].result = 0;
+ tests[1].tests[5].params[0].i = rel_fd;
+ tests[1].tests[5].params[1].cp = file;
+ tests[1].tests[5].params[2].m = 33190;
+ tests[1].tests[5].params[3].i = 0;
+
+ // fchownat
+ tests[2].syscall = SYS_fchownat;
+ tests[2].num_of_cases = 6;
+ tests[2].name = "fchownat";
+ tests[2].tests[0].result = EBADF;
+ tests[2].tests[0].params[0].i = 106; // invalid fd
+ tests[2].tests[0].params[1].cp = relative_file;
+ tests[2].tests[0].params[2].u = 65534;
+ tests[2].tests[0].params[3].g = 65534;
+ tests[2].tests[0].params[4].i = 0;
+ tests[2].tests[1].result = EINVAL;
+ tests[2].tests[1].params[0].i = rel_fd;
+ tests[2].tests[1].params[1].cp = file;
+ tests[2].tests[1].params[2].u = 65534;
+ tests[2].tests[1].params[3].g = 65534;
+ tests[2].tests[1].params[4].i = 123; // invalid flag
+ tests[2].tests[2].result = ENOTDIR;
+ tests[2].tests[2].params[0].i = notd_fd;
+ tests[2].tests[2].params[1].cp = relative_file;
+ tests[2].tests[2].params[2].u = 65534;
+ tests[2].tests[2].params[3].g = 65534;
+ tests[2].tests[2].params[4].i = 0;
+ tests[2].tests[3].result = 0;
+ tests[2].tests[3].params[0].i = notd_fd;
+ tests[2].tests[3].params[1].cp = absolute_file;
+ tests[2].tests[3].params[2].u = 65534;
+ tests[2].tests[3].params[3].g = 65534;
+ tests[2].tests[3].params[4].i = 0;
+ tests[2].tests[4].result = 0;
+ tests[2].tests[4].params[0].i = AT_FDCWD;
+ tests[2].tests[4].params[1].cp = symlinkf;
+ tests[2].tests[4].params[2].u = 65534;
+ tests[2].tests[4].params[3].g = 65534;
+ tests[2].tests[4].params[4].i = AT_SYMLINK_NOFOLLOW;
+ tests[2].tests[5].result = 0;
+ tests[2].tests[5].params[0].i = rel_fd;
+ tests[2].tests[5].params[1].cp = file;
+ tests[2].tests[5].params[2].u = 0;
+ tests[2].tests[5].params[3].g = 0;
+ tests[2].tests[5].params[4].i = 0;
+
+ // fstatat
+ tests[3].syscall = SYS_fstatat;
+ tests[3].num_of_cases = 5;
+ tests[3].name = "fstatat";
+ tests[3].tests[0].result = EBADF;
+ tests[3].tests[0].params[0].i = 106; // invalid fd
+ tests[3].tests[0].params[1].cp = relative_file;
+ tests[3].tests[0].params[2].vp = &buf;
+ tests[3].tests[0].params[3].i = 0;
+ tests[3].tests[1].result = EINVAL;
+ tests[3].tests[1].params[0].i = rel_fd;
+ tests[3].tests[1].params[1].cp = relative_file;
+ tests[3].tests[1].params[2].vp = &buf;
+ tests[3].tests[1].params[3].i = 123; // invalid flags
+ tests[3].tests[2].result = ENOTDIR;
+ tests[3].tests[2].params[0].i = notd_fd;
+ tests[3].tests[2].params[1].cp = relative_file;
+ tests[3].tests[2].params[2].vp = &buf;
+ tests[3].tests[2].params[3].i = 0;
+ tests[3].tests[2].result = 0;
+ tests[3].tests[2].params[0].i = rel_fd;
+ tests[3].tests[2].params[1].cp = file;
+ tests[3].tests[2].params[2].vp = &buf;
+ tests[3].tests[2].params[3].i = 0;
+ tests[3].tests[3].result = 0;
+ tests[3].tests[3].params[0].i = AT_FDCWD;
+ tests[3].tests[3].params[1].cp = symlinkf;
+ tests[3].tests[3].params[2].vp = &buf;
+ tests[3].tests[3].params[3].i = AT_SYMLINK_NOFOLLOW;
+ tests[3].tests[4].result = 0;
+ tests[3].tests[4].params[0].i = notd_fd;
+ tests[3].tests[4].params[1].cp = absolute_file;
+ tests[3].tests[4].params[2].vp = &buf;
+ tests[3].tests[4].params[3].i = 0;
+
+ // futimesat
+ tests[4].syscall = SYS_futimesat;
+ tests[4].num_of_cases = 4;
+ tests[4].name = "futimesat";
+ tests[4].tests[0].result = EBADF;
+ tests[4].tests[0].params[0].i = 106; // invalid fd
+ tests[4].tests[0].params[1].cp = relative_file;
+ tests[4].tests[0].params[2].vp = times;
+ tests[4].tests[1].result = ENOTDIR;
+ tests[4].tests[1].params[0].i = notd_fd;
+ tests[4].tests[1].params[1].cp = relative_file;
+ tests[4].tests[1].params[2].vp = times;
+ tests[4].tests[2].result = 0;
+ tests[4].tests[2].params[0].i = rel_fd;
+ tests[4].tests[2].params[1].cp = file;
+ tests[4].tests[2].params[2].vp = times;
+ tests[4].tests[3].result = 0;
+ tests[4].tests[3].params[0].i = notd_fd;
+ tests[4].tests[3].params[1].cp = absolute_file;
+ tests[4].tests[3].params[2].vp = times;
+
+ // linkat
+ tests[5].syscall = SYS_linkat;
+ tests[5].num_of_cases = 7;
+ tests[5].name = "linkat";
+ tests[5].tests[0].result = EBADF;
+ tests[5].tests[0].params[0].i = 106; // invalid fd
+ tests[5].tests[0].params[1].cp = relative_file;
+ tests[5].tests[0].params[2].i = AT_FDCWD;
+ tests[5].tests[0].params[3].cp = newlink;
+ tests[5].tests[0].params[4].i = 0;
+ tests[5].tests[1].result = EBADF;
+ tests[5].tests[1].params[0].i = AT_FDCWD;
+ tests[5].tests[1].params[1].cp = relative_file;
+ tests[5].tests[1].params[2].i = 106; // invalid fd
+ tests[5].tests[1].params[3].cp = newlink;
+ tests[5].tests[1].params[4].i = 0;
+ tests[5].tests[2].result = EINVAL;
+ tests[5].tests[2].params[0].i = rel_fd;
+ tests[5].tests[2].params[1].cp = relative_file;
+ tests[5].tests[2].params[2].i = AT_FDCWD;
+ tests[5].tests[2].params[3].cp = newlink;
+ tests[5].tests[2].params[4].i = 123; // invalid flag
+ tests[5].tests[3].result = ENOTDIR;
+ tests[5].tests[3].params[0].i = notd_fd;
+ tests[5].tests[3].params[1].cp = relative_file;
+ tests[5].tests[3].params[2].i = AT_FDCWD;
+ tests[5].tests[3].params[3].cp = newlink;
+ tests[5].tests[3].params[4].i = 0;
+ tests[5].tests[4].result = 0;
+ tests[5].tests[4].params[0].i = rel_fd;
+ tests[5].tests[4].params[1].cp = file;
+ tests[5].tests[4].params[2].i = rel_fd;
+ tests[5].tests[4].params[3].cp = newlink;
+ tests[5].tests[4].params[4].i = 0;
+ tests[5].tests[5].result = 0;
+ tests[5].tests[5].params[0].i = AT_FDCWD;
+ tests[5].tests[5].params[1].cp = symlinkf;
+ tests[5].tests[5].params[2].i = rel_fd;
+ tests[5].tests[5].params[3].cp = newlink2;
+ tests[5].tests[5].params[4].i = 0;
+ tests[5].tests[6].result = 0;
+ tests[5].tests[6].params[0].i = AT_FDCWD;
+ tests[5].tests[6].params[1].cp = symlinkf;
+ tests[5].tests[6].params[2].i = rel_fd;
+ tests[5].tests[6].params[3].cp = newlink3;
+ tests[5].tests[6].params[4].i = AT_SYMLINK_FOLLOW;
+
+ // mkdirat
+ tests[6].syscall = SYS_mkdirat;
+ tests[6].num_of_cases = 3;
+ tests[6].name = "mkdirat";
+ tests[6].tests[0].result = EBADF;
+ tests[6].tests[0].params[0].i = 106; // invalid fd
+ tests[6].tests[0].params[1].cp = relative_file;
+ tests[6].tests[0].params[2].m = 33190;
+ tests[6].tests[1].result = ENOTDIR;
+ tests[6].tests[1].params[0].i = notd_fd;
+ tests[6].tests[1].params[1].cp = relative_file;
+ tests[6].tests[1].params[2].m = 33190;
+ tests[6].tests[2].result = 0;
+ tests[6].tests[2].params[0].i = rel_fd;
+ tests[6].tests[2].params[1].cp = newdir;
+ tests[6].tests[2].params[2].m = 33190;
+
+ // mkfifoat
+ tests[7].syscall = SYS_mkfifoat;
+ tests[7].num_of_cases = 3;
+ tests[7].name = "mkfifoat";
+ tests[7].tests[0].result = EBADF;
+ tests[7].tests[0].params[0].i = 107; // invalid fd
+ tests[7].tests[0].params[1].cp = relative_file;
+ tests[7].tests[0].params[2].m = 33190;
+ tests[7].tests[1].result = ENOTDIR;
+ tests[7].tests[1].params[0].i = notd_fd;
+ tests[7].tests[1].params[1].cp = relative_file;
+ tests[7].tests[1].params[2].m = 33190;
+ tests[7].tests[2].result = 0;
+ tests[7].tests[2].params[0].i = rel_fd;
+ tests[7].tests[2].params[1].cp = fifo;
+ tests[7].tests[2].params[2].m = 33190;
+
+ // mknodat
+ tests[8].syscall = SYS_mknodat;
+ tests[8].num_of_cases = 3;
+ tests[8].name = "mknodat";
+ tests[8].tests[0].result = EBADF;
+ tests[8].tests[0].params[0].i = 108; // invalid fd
+ tests[8].tests[0].params[1].cp = relative_file;
+ tests[8].tests[0].params[2].m = 0666 | S_IFCHR;
+ tests[8].tests[0].params[3].d = 15;
+ tests[8].tests[1].result = ENOTDIR;
+ tests[8].tests[1].params[0].i = notd_fd;
+ tests[8].tests[1].params[1].cp = relative_file;
+ tests[8].tests[1].params[2].m = 0666 | S_IFCHR;
+ tests[8].tests[1].params[3].d = 15;
+ tests[8].tests[2].result = 0;
+ tests[8].tests[2].params[0].i = rel_fd;
+ tests[8].tests[2].params[1].cp = nod;
+ tests[8].tests[2].params[2].m = 0666 | S_IFCHR;
+ tests[8].tests[2].params[3].d = 2570;
+
+ // openat
+ tests[9].syscall = SYS_openat;
+ tests[9].num_of_cases = 5;
+ tests[9].name = "openat";
+ tests[9].tests[0].result = EBADF;
+ tests[9].tests[0].params[0].i = 106; // invalid fd
+ tests[9].tests[0].params[1].cp = relative_file;
+ tests[9].tests[0].params[2].i = O_RDONLY;
+ tests[9].tests[0].params[3].i = 0666;
+ tests[9].tests[1].result = ENOTDIR;
+ tests[9].tests[1].params[0].i = notd_fd;
+ tests[9].tests[1].params[1].cp = relative_file;
+ tests[9].tests[1].params[2].i = O_RDONLY;
+ tests[9].tests[1].params[3].i = 0666;
+ tests[9].tests[2].result = 7; // hardcoded fd
+ tests[9].tests[2].params[0].i = rel_fd;
+ tests[9].tests[2].params[1].cp = file;
+ tests[9].tests[2].params[2].i = O_RDONLY;
+ tests[9].tests[2].params[3].i = 0400;
+ tests[9].tests[3].result = 8; // hardcoded fd
+ tests[9].tests[3].params[0].i = notd_fd;
+ tests[9].tests[3].params[1].cp = absolute_file;
+ tests[9].tests[3].params[2].i = O_RDONLY;
+ tests[9].tests[3].params[3].i = 0400;
+ tests[9].tests[4].result = 9; // hardcoded fd
+ tests[9].tests[4].params[0].i = rel_fd;
+ tests[9].tests[4].params[1].cp = newfile;
+ tests[9].tests[4].params[2].i = O_RDONLY | O_CREAT;
+ tests[9].tests[4].params[3].i = 0666;
+
+ // readlinkat
+ tests[10].syscall = SYS_readlinkat;
+ tests[10].num_of_cases = 3;
+ tests[10].name = "readlinkat";
+ tests[10].tests[0].result = EBADF;
+ tests[10].tests[0].params[0].i = 106; // invalid fd
+ tests[10].tests[0].params[1].cp = relative_file;
+ tests[10].tests[0].params[2].vp = cbuf;
+ tests[10].tests[0].params[3].i = PATH_MAX;
+ tests[10].tests[1].result = ENOTDIR;
+ tests[10].tests[1].params[0].i = notd_fd;
+ tests[10].tests[1].params[1].cp = relative_file;
+ tests[10].tests[1].params[2].vp = cbuf;
+ tests[10].tests[1].params[3].i = PATH_MAX;
+ tests[10].tests[2].result = strlen(absolute_file);
+ tests[10].tests[2].params[0].i = AT_FDCWD;
+ tests[10].tests[2].params[1].cp = symlinkf;
+ tests[10].tests[2].params[2].vp = cbuf;
+ tests[10].tests[2].params[3].i = PATH_MAX;
+
+ // renameat
+ tests[11].syscall = SYS_renameat;
+ tests[11].num_of_cases = 5;
+ tests[11].name = "renameat";
+ tests[11].tests[0].result = EBADF;
+ tests[11].tests[0].params[0].i = 106; // invalid fd
+ tests[11].tests[0].params[1].cp = file;
+ tests[11].tests[0].params[2].i = rel_fd;
+ tests[11].tests[0].params[3].cp = file;
+ tests[11].tests[1].result = EBADF;
+ tests[11].tests[1].params[0].i = rel_fd;
+ tests[11].tests[1].params[1].cp = file;
+ tests[11].tests[1].params[2].i = 106; // invalid fd
+ tests[11].tests[1].params[3].cp = file;
+ tests[11].tests[2].result = ENOTDIR;
+ tests[11].tests[2].params[0].i = notd_fd;
+ tests[11].tests[2].params[1].cp = relative_file;
+ tests[11].tests[2].params[2].i = rel_fd;
+ tests[11].tests[2].params[3].cp = file;
+ tests[11].tests[3].result = ENOTDIR;
+ tests[11].tests[3].params[0].i = rel_fd;
+ tests[11].tests[3].params[1].cp = file;
+ tests[11].tests[3].params[2].i = notd_fd;
+ tests[11].tests[3].params[3].cp = relative_file;
+ tests[11].tests[4].result = 0;
+ tests[11].tests[4].params[0].i = rel_fd;
+ tests[11].tests[4].params[1].cp = newfile;
+ tests[11].tests[4].params[2].i = AT_FDCWD;
+ tests[11].tests[4].params[3].cp = newfile;
+
+ // symlinkat
+ tests[12].syscall = SYS_symlinkat;
+ tests[12].num_of_cases = 3;
+ tests[12].name = "symlinkat";
+ tests[12].tests[0].result = EBADF;
+ tests[12].tests[0].params[0].cp = file;
+ tests[12].tests[0].params[1].i = 106; // invalid fd
+ tests[12].tests[0].params[2].cp = file;
+ tests[12].tests[1].result = ENOTDIR;
+ tests[12].tests[1].params[0].cp = file;
+ tests[12].tests[1].params[1].i = notd_fd;
+ tests[12].tests[1].params[2].cp = relative_file;
+ tests[12].tests[2].result = 0;
+ tests[12].tests[2].params[0].cp = absolute_file;
+ tests[12].tests[2].params[1].i = rel_fd;
+ tests[12].tests[2].params[2].cp = newslink;
+
+ // unlinkat
+ tests[13].syscall = SYS_unlinkat;
+ tests[13].num_of_cases = 7;
+ tests[13].name = "unlinkat";
+ tests[13].tests[0].result = EBADF;
+ tests[13].tests[0].params[0].i = 106; // invalid fd
+ tests[13].tests[0].params[1].cp = relative_file;
+ tests[13].tests[0].params[2].i = 0;
+ tests[13].tests[1].result = ENOTDIR;
+ tests[13].tests[1].params[0].i = notd_fd;
+ tests[13].tests[1].params[1].cp = relative_file;
+ tests[13].tests[1].params[2].i = 0;
+ tests[13].tests[2].result = EINVAL;
+ tests[13].tests[2].params[0].i = rel_fd;
+ tests[13].tests[2].params[1].cp = file;
+ tests[13].tests[2].params[2].i = 123; // invalid flag
+ tests[13].tests[3].result = ENOTDIR;
+ tests[13].tests[3].params[0].i = rel_fd;
+ tests[13].tests[3].params[1].cp = not_dir_path;
+ tests[13].tests[3].params[2].i = AT_REMOVEDIR;
+ tests[13].tests[4].result = ENOTEMPTY;
+ tests[13].tests[4].params[0].i = AT_FDCWD;
+ tests[13].tests[4].params[1].cp = relative_path;
+ tests[13].tests[4].params[2].i = AT_REMOVEDIR;
+ tests[13].tests[5].result = 0;
+ tests[13].tests[5].params[0].i = rel_fd;
+ tests[13].tests[5].params[1].cp = newdir;
+ tests[13].tests[5].params[2].i = AT_REMOVEDIR;
+ tests[13].tests[6].result = 0;
+ tests[13].tests[6].params[0].i = AT_FDCWD;
+ tests[13].tests[6].params[1].cp = newfile;
+ tests[13].tests[6].params[2].i = 0;
+
+ // fexecve
+ tests[14].syscall = SYS_fexecve;
+ tests[14].num_of_cases = 2;
+ tests[14].name = "fexecve";
+ tests[14].tests[0].result = EBADF;
+ tests[14].tests[0].params[0].i = 106; // invalid fd
+ tests[14].tests[0].params[1].cpp = pargv;
+ tests[14].tests[0].params[2].cpp = NULL;
+ // This is EXPECTED to execve /bin/date, so dont expect OK output
+ tests[14].tests[1].result = 0;
+ tests[14].tests[1].params[0].i = exec_fd;
+ tests[14].tests[1].params[1].cpp = pargv;
+ tests[14].tests[1].params[2].cpp = NULL;
+}
+
+void
+cleanup()
+{
+ int error;
+
+ close(notd_fd);
+ close(rel_fd);
+ close(abs_fd);
+
+ if (!file_exist) {
+ error = unlink("tmp/foo");
+ if (error) {
+ perror("unlink");
+ exit(0);
+ }
+ }
+ if (!dir_exist) {
+ error = rmdir(absolute_path);
+ if (error) {
+ perror("rmdir");
+ exit(0);
+ }
+ }
+ if (link_exist) {
+ error = unlink(symlinkf);
+ if (error) {
+ perror("unlink");
+ exit(0);
+ }
+ }
+}
+
+void
+setup_once()
+{
+}
+
+int
+main(int argc, char *argv[])
+{
+ int i,j;
+ int error;
+
+ setup();
+
+ for (i = 0; i < NUM_OF_TESTS; i++) {
+ printf("\nTest: %s\n", tests[i].name);
+ for (j = 0; j < tests[i].num_of_cases; j++) {
+ error = syscall(tests[i].syscall,
+ tests[i].tests[j].params[0],
+ tests[i].tests[j].params[1],
+ tests[i].tests[j].params[2],
+ tests[i].tests[j].params[3],
+ tests[i].tests[j].params[4]);
+ if (error == 0) {
+ if (tests[i].tests[j].result == 0)
+ printf("#%i ... OK\n", j);
+ else {
+ printf("#%i ... BAD: ", j);
+ printf("expected %i, but got %i\n", tests[i].tests[j].result, error);
+ }
+ } else {
+ if (tests[i].tests[j].result == errno)
+ printf("#%i ... OK\n", j);
+ else {
+ if (error != tests[i].tests[j].result) {
+ printf("#%i ... BAD: ", j);
+ printf("expected %i, but got %i\n", tests[i].tests[j].result, error);
+ } else
+ printf("#%i ... OK\n", j);
+ }
+ }
+
+ }
+ }
+
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/openat/openat.c b/tools/test/stress2/testcases/openat/openat.c
new file mode 100644
index 000000000000..1bcc53c5f07a
--- /dev/null
+++ b/tools/test/stress2/testcases/openat/openat.c
@@ -0,0 +1,187 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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/param.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+static char path1[128];
+static char path2[] = "tmp";
+
+static char rpath[128];
+static char apath[128];
+
+static int fd;
+static int freespace;
+
+int
+setup(int nb)
+{
+ int64_t bl;
+ int64_t in;
+ int64_t reserve_bl;
+ int64_t reserve_in;
+
+ umask(0);
+
+ if (nb == 0) {
+ getdf(&bl, &in);
+
+ /* Resource requirements: */
+ reserve_in = 4 * op->incarnations;
+ reserve_bl = 8192 * op->incarnations;
+ freespace = (reserve_bl <= bl && reserve_in <= in);
+ if (!freespace)
+ reserve_bl = reserve_in = 0;
+
+ if (op->verbose > 1)
+ printf("openat(incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
+ op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
+ reservedf(reserve_bl, reserve_in);
+ putval(freespace);
+ } else {
+ freespace = getval();
+ }
+ if (!freespace)
+ exit(0);
+
+ sprintf(path1,"%s.%05d", getprogname(), getpid());
+ if (mkdir(path1, 0770) < 0)
+ err(1, "mkdir(%s), %s:%d", path1, __FILE__, __LINE__);
+ if (chdir(path1) == -1)
+ err(1, "chdir(%s), %s:%d", path1, __FILE__, __LINE__);
+
+ if (mkdir(path2, 0770) < 0)
+ err(1, "mkdir(%s), %s:%d", path2, __FILE__, __LINE__);
+ if (chdir(path2) == -1)
+ err(1, "chdir(%s), %s:%d", path2, __FILE__, __LINE__);
+ if (getcwd(apath, sizeof(apath)) == NULL)
+ err(1, "getcwd(%s), %s:%d", path2, __FILE__, __LINE__);
+
+ if (chdir("..") == -1)
+ err(1, "chdir(%s), %s:%d", path1, __FILE__, __LINE__);
+
+ if ((fd = open(path2, O_RDONLY)) == -1)
+ err(1, "open(%s), %s:%d", path2, __FILE__, __LINE__);
+
+ strcpy(rpath, "tmp");
+ return (0);
+}
+
+void
+cleanup(void)
+{
+ if (rmdir(path2) == -1)
+ warn("rmdir(%s), %s:%d", path2, __FILE__, __LINE__);
+ (void)chdir("..");
+ if (rmdir(path1) == -1)
+ warn("rmdir(%s), %s:%d", path1, __FILE__, __LINE__);
+}
+
+static void
+test_openat(void)
+{
+ pid_t pid;
+ int i;
+ int tfd;
+ char file[128];
+ char p[128];
+
+ pid = getpid();
+ for (i = 0; i < 100 && done_testing == 0; i++) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if ((tfd = openat(fd, file, O_RDONLY|O_CREAT, 0660)) == -1)
+ err(1, "openat(%s), %s:%d", file, __FILE__, __LINE__);
+ close(tfd);
+ strcpy(p, "tmp/");
+ strcat(p, file);
+ if (unlink(p) == -1)
+ err(1, "unlink(%s), %s:%d", p, __FILE__, __LINE__);
+ }
+}
+
+static void
+test_renameat(void)
+{
+ pid_t pid;
+ int i;
+ int tfd;
+ char file[128];
+ char file2[128];
+
+ pid = getpid();
+ for (i = 0; i < 100 && done_testing == 0; i++) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if ((tfd = openat(fd, file, O_RDONLY|O_CREAT, 0660)) == -1)
+ err(1, "openat(%s), %s:%d", file, __FILE__, __LINE__);
+ close(tfd);
+
+ sprintf(file2,"p%05d.%05d.togo", pid, i);
+ if (renameat(fd, file, fd, file2) == -1)
+ err(1, "renameat(%s)", file2);
+
+ sprintf(file2,"tmp/p%05d.%05d.togo", pid, i);
+ if (unlink(file2) == -1)
+ err(1, "unlink(%s), %s:%d", file2, __FILE__, __LINE__);
+ }
+}
+
+static void
+test_unlinkat(void)
+{
+ pid_t pid;
+ int i;
+ int tfd;
+ char file[128];
+
+ pid = getpid();
+ for (i = 0; i < 100 && done_testing == 0; i++) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if ((tfd = openat(fd, file, O_RDONLY|O_CREAT, 0660)) == -1)
+ err(1, "openat(%s), %s:%d", file, __FILE__, __LINE__);
+ close(tfd);
+ if (unlinkat(fd, file, 0) == -1)
+ err(1, "unlinkat(%s), %s:%d", file, __FILE__, __LINE__);
+ }
+}
+
+int
+test(void)
+{
+ test_openat();
+ test_renameat();
+ test_unlinkat();
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/pty/Makefile b/tools/test/stress2/testcases/pty/Makefile
new file mode 100644
index 000000000000..5b365d21a849
--- /dev/null
+++ b/tools/test/stress2/testcases/pty/Makefile
@@ -0,0 +1,4 @@
+PROG= pty
+LIBADD= util
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/pty/pty.c b/tools/test/stress2/testcases/pty/pty.c
new file mode 100644
index 000000000000..8b759cb2d745
--- /dev/null
+++ b/tools/test/stress2/testcases/pty/pty.c
@@ -0,0 +1,111 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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.
+ *
+ */
+
+/* Test PTYs */
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <err.h>
+#include <libutil.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <termios.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+#define TXT "Hello, world!"
+
+int
+setup(int nb __unused)
+{
+ return (0);
+}
+
+void
+cleanup(void)
+{
+}
+
+int
+test(void)
+{
+ struct termios tios;
+ int i, master, slave;
+ int s[32], m[32];
+ char buf[512], slname[1025];
+
+ for (i = 0; i < 32; i++) {
+ if (openpty(&m[i], &s[i], slname, NULL, NULL) == -1)
+ err(1, "openpty");
+ }
+ for (i = 0; i < 32; i++) {
+ close(m[i]);
+ close(s[i]);
+ }
+
+ for (i = 0; i < 1024; i++) {
+ if (openpty(&m[0], &s[0], slname, NULL, NULL) == -1)
+ err(1, "openpty");
+ close(m[0]);
+ close(s[0]);
+ }
+
+ for (i = 0; i < 10 && done_testing == 0; i++) {
+ if (openpty(&master, &slave, slname, NULL, NULL) == -1)
+ err(1, "openpty");
+ if ((i & 1) == 0) {
+ if (close(master) == -1)
+ err(1, "close(master)");
+ if (close(slave) == -1)
+ err(1, "close(%s)", slname);
+ } else {
+ if (close(slave) == -1)
+ err(1, "close(%s)", slname);
+ if (close(master) == -1)
+ err(1, "close(master)");
+ }
+ }
+
+ if (openpty(&master, &slave, slname, NULL, NULL) == -1)
+ err(1, "openpty");
+ if (tcgetattr(slave, &tios) < 0)
+ err(1, "tcgetattr(%s)", slname);
+ cfmakeraw(&tios);
+ if (tcsetattr(slave, TCSAFLUSH, &tios) < 0)
+ err(1, "tcsetattr(%s)", slname);
+
+ for (i = 0; i < 64 && done_testing == 0; i++) {
+ if (write(master, TXT, sizeof(TXT)) == -1)
+ err(1, "write");
+ if (read(slave, buf, sizeof(TXT)) == -1)
+ err(1, "read(%s)", slname);
+ }
+ close(master);
+ close(slave);
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/rename/Makefile b/tools/test/stress2/testcases/rename/Makefile
new file mode 100644
index 000000000000..08cb51eaf3ed
--- /dev/null
+++ b/tools/test/stress2/testcases/rename/Makefile
@@ -0,0 +1,3 @@
+PROG= rename
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/rename/rename.c b/tools/test/stress2/testcases/rename/rename.c
new file mode 100644
index 000000000000..8240cd62b34b
--- /dev/null
+++ b/tools/test/stress2/testcases/rename/rename.c
@@ -0,0 +1,134 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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/param.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+static unsigned long size;
+
+int
+setup(int nb)
+{
+ int64_t in;
+ int64_t bl;
+ int64_t reserve_in;
+ int64_t reserve_bl;
+
+ umask(0);
+
+ if (nb == 0) {
+ getdf(&bl, &in);
+ size = in / op->incarnations;
+
+ if (size > 1000)
+ size = 1000; /* arbitrary limit number of files pr. dir */
+
+ /* Resource requirements: */
+ while (size > 0) {
+ reserve_in = 2 * size * op->incarnations + 2 * op->incarnations;
+ reserve_bl = 30 * size * op->incarnations;
+ if (reserve_bl <= bl && reserve_in <= in)
+ break;
+ size = size / 2;
+ }
+ if (size == 0)
+ reserve_bl = reserve_in = 0;
+
+ if (op->verbose > 1)
+ printf("rename(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
+ size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
+ reservedf(reserve_bl, reserve_in);
+ putval(size);
+ } else {
+ size = getval();
+ }
+ if (size == 0)
+ exit(0);
+
+ return (0);
+}
+
+void
+cleanup(void)
+{
+}
+
+static void
+test_rename(void)
+{
+ int i, j;
+ pid_t pid;
+ char file1[128];
+ char file2[128];
+ int tfd;
+
+ pid = getpid();
+ for (i = 0; i < (int)size; i++) {
+ sprintf(file1,"p%05d.%05d", pid, i);
+ if ((tfd = open(file1, O_RDONLY|O_CREAT, 0660)) == -1)
+ err(1, "openat(%s), %s:%d", file1, __FILE__, __LINE__);
+ close(tfd);
+ }
+ for (j = 0; j < 100 && done_testing == 0; j++) {
+ for (i = 0; i < (int)size; i++) {
+ sprintf(file1,"p%05d.%05d", pid, i);
+ sprintf(file2,"p%05d.%05d.togo", pid, i);
+ if (rename(file1, file2) == -1)
+ err(1, "rename(%s, %s). %s:%d", file1, file2,
+ __FILE__, __LINE__);
+ }
+ for (i = 0; i < (int)size; i++) {
+ sprintf(file1,"p%05d.%05d", pid, i);
+ sprintf(file2,"p%05d.%05d.togo", pid, i);
+ if (rename(file2, file1) == -1)
+ err(1, "rename(%s, %s). %s:%d", file2, file1,
+ __FILE__, __LINE__);
+ }
+ }
+
+ for (i = 0; i < (int)size; i++) {
+ sprintf(file1,"p%05d.%05d", pid, i);
+ if (unlink(file1) == -1)
+ err(1, "unlink(%s), %s:%d", file1, __FILE__, __LINE__);
+ }
+}
+
+int
+test(void)
+{
+ test_rename();
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/run/Makefile b/tools/test/stress2/testcases/run/Makefile
new file mode 100644
index 000000000000..b114128d81b8
--- /dev/null
+++ b/tools/test/stress2/testcases/run/Makefile
@@ -0,0 +1,3 @@
+PROG= run
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/run/run.c b/tools/test/stress2/testcases/run/run.c
new file mode 100644
index 000000000000..74249891b8ae
--- /dev/null
+++ b/tools/test/stress2/testcases/run/run.c
@@ -0,0 +1,117 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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.
+ *
+ */
+
+/* Control program to run all the other test cases */
+
+#include <sys/param.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <libgen.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+#define MAXAV 10
+static char *av[MAXAV];
+static int loop = 1;
+
+int
+setup(int nb __unused)
+{
+ return (0);
+}
+
+void
+cleanup(void)
+{
+}
+
+static char **
+mkargv(char *name)
+{
+ av[0] = name;
+ av[1] = 0;
+ return (av);
+}
+
+static void
+clean(void)
+{
+ char buf[132];
+
+ snprintf(buf, sizeof(buf),
+ "cd %s; rm -rf syscall.[0-9]* fifo.[0-9]* creat.[0-9]* "
+ "p[0-9]*.d1 df lock", op->cd);
+ (void)system(buf);
+}
+
+int
+test(void)
+{
+ struct tm *tm;
+ pid_t *r;
+ time_t t;
+ int i;
+ int s;
+ char fullpath[MAXPATHLEN+1];
+ char ct[80];
+
+ r = (pid_t *)malloc(op->argc * sizeof(pid_t));
+
+ (void)time(&t);
+ tm = localtime(&t);
+ (void) strftime(ct, sizeof(ct), "%T", tm);
+ printf("%s Loop #%d\n", ct, loop++);
+ fflush(stdout);
+
+ for (i = 0; i < op->argc; i++) {
+ r[i] = 0;
+ if (op->argv[i][0] == 0)
+ continue;
+ if ((r[i] = fork()) == 0) {
+ snprintf(fullpath, sizeof(fullpath), "%s/%s", home,
+ op->argv[i]);
+ if (execv(fullpath, mkargv(basename(op->argv[i]))) == -1)
+ err(1, "execl(%s), %s:%d", fullpath, __FILE__,
+ __LINE__);
+ }
+ if (r[i] < 0)
+ err(1, "fork(), %s:%d", __FILE__, __LINE__);
+ }
+ for (i = 0; i < op->argc; i++)
+ if (r[i] != 0 && waitpid(r[i], &s, 0) == -1)
+ err(1, "waitpid(%d), %s:%d", r[i], __FILE__, __LINE__);
+ free(r);
+
+ clean();
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/rw/Makefile b/tools/test/stress2/testcases/rw/Makefile
new file mode 100644
index 000000000000..b6fe1baae94d
--- /dev/null
+++ b/tools/test/stress2/testcases/rw/Makefile
@@ -0,0 +1,3 @@
+PROG= rw
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/rw/rw.c b/tools/test/stress2/testcases/rw/rw.c
new file mode 100644
index 000000000000..b379bcaa3a79
--- /dev/null
+++ b/tools/test/stress2/testcases/rw/rw.c
@@ -0,0 +1,180 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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.
+ *
+ */
+
+/* Write and check read a file */
+
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+static char path[128];
+static int starting_dir;
+static unsigned long size;
+
+#define MAXSIZE 256 * 1024
+
+int
+setup(int nb)
+{
+ int64_t bl;
+ int64_t in;
+ int64_t reserve_in;
+ int64_t reserve_bl;
+ int pct;
+
+ if (nb == 0) {
+ getdf(&bl, &in);
+ size = bl / op->incarnations / 1024;
+
+ pct = 90;
+ if (op->hog == 0)
+ pct = random_int(1, 90);
+ size = size / 100 * pct + 1;
+
+ if (size > MAXSIZE)
+ size = MAXSIZE; /* arbitrary limit size pr. incarnation */
+
+ /* Resource requirements: */
+ while (size > 0) {
+ reserve_in = 2 * op->incarnations + 1;
+ reserve_bl = size * 1024 * op->incarnations +
+ (512 * 1024 * op->incarnations) +
+ 64 * 1024;
+ if (reserve_bl <= bl && reserve_in <= in)
+ break;
+ size = size / 10 * 8;
+ }
+ if (size == 0)
+ reserve_bl = reserve_in = 0;
+
+ if (op->verbose > 1)
+ printf("rw(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
+ size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
+ reservedf(reserve_bl, reserve_in);
+ putval(size);
+ size = size * 1024;
+ } else {
+ size = getval();
+ size = size * 1024;
+ }
+ if (size == 0)
+ exit(0);
+
+ umask(0);
+ sprintf(path,"%s.%05d", getprogname(), getpid());
+ (void)mkdir(path, 0770);
+ if (chdir(path) == -1)
+ err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
+ if ((starting_dir = open(".", 0)) < 0)
+ err(1, ".");
+
+ return (0);
+}
+
+void
+cleanup(void)
+{
+ if (starting_dir == 0)
+ return;
+ if (fchdir(starting_dir) == -1)
+ err(1, "fchdir()");
+ if (close(starting_dir) < 0)
+ err(1, "close(starting_dir:%d)", starting_dir);
+
+ (void)system("find . -delete");
+
+ if (chdir("..") == -1)
+ err(1, "chdir(..)");
+ if (rmdir(path) == -1)
+ err(1, "rmdir(%s), %s:%d", path, __FILE__, __LINE__);
+ size = 0;
+}
+
+int
+test(void)
+{
+ int buf[1024], index, to;
+#ifdef TEST
+ int i;
+#endif
+ int fd;
+ char file[128];
+
+ sprintf(file,"p%05d", getpid());
+ if ((fd = creat(file, 0660)) == -1)
+ err(1, "creat(%s)", file);
+
+ to = sizeof(buf);
+ index = 0;
+ while (index < (int)size) {
+ if (index + to > (int)size)
+ to = size - index;
+#ifdef TEST
+ for (i = 0; i < to; i++)
+ buf[i] = index + i;
+#endif
+ index += to;
+ if (write(fd, buf, to) != to)
+ err(1, "write(%s), %s:%d", file, __FILE__, __LINE__);
+ }
+ if (close(fd) == -1)
+ err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);
+
+ if ((fd = open(file, O_RDONLY)) == -1)
+ err(1, "open(%s), %s:%d", file, __FILE__, __LINE__);
+
+ index = 0;
+ while (index < (int)size && done_testing == 0) {
+ if (index + to > (int)size)
+ to = size - index;
+ if (read(fd, buf, to) != to)
+ err(1, "rw read. %s.%d", __FILE__, __LINE__);
+#ifdef TEST
+ for (i = 0; i < to; i++) {
+ if (buf[i] != index + i) {
+ fprintf(stderr,
+ "%s, pid %d: expected %d @ %d, got %d\n",
+ getprogname(), getpid(), index+i, index+i,
+ buf[i]);
+ exit(EXIT_FAILURE);
+ }
+ }
+#endif
+ index += to;
+ }
+ if (close(fd) == -1)
+ err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s), %s:%d", file, __FILE__, __LINE__);
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/shm/Makefile b/tools/test/stress2/testcases/shm/Makefile
new file mode 100644
index 000000000000..8ebdfb8c2d4e
--- /dev/null
+++ b/tools/test/stress2/testcases/shm/Makefile
@@ -0,0 +1,4 @@
+PROG= shm
+LIBADD= util
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/shm/shm.c b/tools/test/stress2/testcases/shm/shm.c
new file mode 100644
index 000000000000..7c465ecd31b7
--- /dev/null
+++ b/tools/test/stress2/testcases/shm/shm.c
@@ -0,0 +1,182 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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.
+ *
+ */
+
+/* Test shared memory */
+
+#include <sys/param.h>
+#include <sys/ipc.h>
+#include <sys/msg.h>
+#include <sys/sem.h>
+#include <sys/shm.h>
+#include <err.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+static int shmid = -1;
+static key_t shmkey;
+static char *shm_buf;
+
+static int semid = -1;
+static key_t semkey;
+static struct sembuf sop[2];
+
+static size_t pgsize;
+static pid_t pid;
+
+int
+setup(int nb __unused)
+{
+ int seed;
+
+ pgsize = sysconf(_SC_PAGESIZE);
+
+ seed = getpid();
+ shmkey = ftok("/tmp", seed);
+ if ((shmid = shmget(shmkey, 10 * pgsize, IPC_CREAT | IPC_EXCL | 0640)) == -1) {
+ if (errno == ENOSPC) {
+ fprintf(stderr, "Max number of semaphores reached.\n");
+ exit(1);
+ }
+ err(1, "shmget (%s:%d)", __FILE__, __LINE__);
+ }
+
+ shm_buf = 0;
+ if ((shm_buf = shmat(shmid, NULL, 0)) == (void *) -1)
+ err(1, "sender: shmat (%s:%d)", __FILE__, __LINE__);
+
+ semkey = ftok("/var", seed);
+ if ((semid = semget(semkey, 2, IPC_CREAT | IPC_EXCL | 0640)) == -1) {
+ if (errno == ENOSPC) {
+ fprintf(stderr, "Max number of semaphores reached.\n");
+ exit(1);
+ }
+ err(1, "semget (%s:%d)", __FILE__, __LINE__);
+ }
+ /* Initialize the semaphore. */
+ sop[0].sem_num = 0;
+ sop[0].sem_op = 0; /* This is the number of runs without queuing. */
+ sop[0].sem_flg = 0;
+ sop[1].sem_num = 1;
+ sop[1].sem_op = 0; /* This is the number of runs without queuing. */
+ sop[1].sem_flg = 0;
+ if (semop(semid, sop, 2) == -1)
+ err(1, "init: semop (%s:%d)", __FILE__, __LINE__);
+ return (0);
+}
+
+void
+cleanup(void)
+{
+ if (shmid != -1)
+ if (shmctl(shmid, IPC_RMID, NULL) == -1 && errno != EINVAL)
+ warn("shmctl IPC_RMID (%s:%d)", __FILE__, __LINE__);
+ if (semid != -1)
+ if (semctl(semid, 0, IPC_RMID, 0) == -1 && errno != EINVAL)
+ warn("shmctl IPC_RMID (%s:%d)", __FILE__, __LINE__);
+}
+
+static void
+Wait(int i) {
+ sop[0].sem_num = i;
+ sop[0].sem_op = -1;
+ if (semop(semid, sop, 1) == -1) {
+ if (errno != EINTR && errno != EIDRM && errno != EINVAL)
+ warn("Wait: semop (%s:%d)", __FILE__, __LINE__);
+ done_testing = 1;
+ }
+}
+
+static void
+Sig(int i) {
+ sop[0].sem_num = i;
+ sop[0].sem_op = 1;
+ if (semop(semid, sop, 1) == -1) {
+ if (errno != EINTR && errno != EIDRM && errno != EINVAL)
+ warn("Sig: semop (%s:%d)", __FILE__, __LINE__);
+ done_testing = 1;
+ }
+}
+
+int
+test(void)
+{
+ int i = 0;
+
+ pid = fork();
+ if (pid == -1) {
+ perror("fork");
+ exit(2);
+ }
+
+ if (pid == 0) { /* child */
+ i = 0;
+ for (;;) {
+ Wait(1);
+ if (done_testing == 1)
+ break;
+ if (shm_buf[i] != (i % 128)) {
+ fprintf(stderr,
+ "child %d: expected %d, got %d\n",
+ getpid(), i % 128, shm_buf[i]);
+ break;
+ }
+ shm_buf[i] = 0;
+ i = (i + 1) % (10 * pgsize);
+ shm_buf[i] = (i % 128);
+ i = (i + 1) % (10 * pgsize);
+ Sig(0);
+ }
+ _exit(0);
+
+ } else { /* parent */
+ i = 0;
+ for (;;) {
+ shm_buf[i] = (i % 128);
+ Sig(1);
+ i = (i + 1) % (10 * pgsize);
+ Wait(0);
+ if (done_testing == 1)
+ break;
+ if (shm_buf[i] != (i % 128)) {
+ fprintf(stderr,
+ "parent(%d): expected %d, got %d\n",
+ getpid(), i % 128, shm_buf[i]);
+ break;
+ }
+ shm_buf[i] = 0;
+ i = (i + 1) % (10 * pgsize);
+ }
+ kill(pid, SIGHUP);
+ kill(pid, SIGKILL);
+ }
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/socket/Makefile b/tools/test/stress2/testcases/socket/Makefile
new file mode 100644
index 000000000000..3ab56a246faa
--- /dev/null
+++ b/tools/test/stress2/testcases/socket/Makefile
@@ -0,0 +1,3 @@
+PROG= socket
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/socket/socket.c b/tools/test/stress2/testcases/socket/socket.c
new file mode 100644
index 000000000000..965ce6b852c3
--- /dev/null
+++ b/tools/test/stress2/testcases/socket/socket.c
@@ -0,0 +1,118 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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/param.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <err.h>
+#include <errno.h>
+#include <netdb.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+#define NB (400 * 1024 * 1024)
+
+static int port;
+static int bufsize;
+static int sv[2];
+
+static void
+reader(void) {
+ int n, t, *buf;
+
+ t = 0;
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+ while (done_testing == 0) {
+ if ((n = read(sv[0], buf, bufsize)) < 0)
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ t += n;
+ if (n == 0) break;
+ }
+ close(sv[0]);
+ return;
+}
+
+static void
+writer(void) {
+ int i, *buf;
+
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+ for (i = 0; i < bufsize / (int)sizeof(int); i++)
+ buf[i] = i;
+
+ for (;;) {
+ for (i = 0; i < NB; i+= bufsize) {
+ if (write(sv[1], buf, bufsize) < 0) {
+ if (errno == EPIPE)
+ return;
+ err(1, "write(%d), %s:%d", sv[1],
+ __FILE__, __LINE__);
+ }
+ }
+ }
+ return;
+}
+
+int
+setup(int nb)
+{
+ port = 12340 + nb;
+ bufsize = 2 << random_int(2, 12);
+ return (0);
+}
+
+void
+cleanup(void)
+{
+}
+
+int
+test(void)
+{
+ pid_t pid;
+
+ if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) != 0)
+ err(1, "socketpair()");
+ if ((pid = fork()) == 0) {
+ writer();
+ _exit(EXIT_SUCCESS);
+
+ } else if (pid > 0) {
+ reader();
+ kill(pid, SIGINT);
+ } else
+ err(1, "fork(), %s:%d", __FILE__, __LINE__);
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/swap/Makefile b/tools/test/stress2/testcases/swap/Makefile
new file mode 100644
index 000000000000..712b6146a2ae
--- /dev/null
+++ b/tools/test/stress2/testcases/swap/Makefile
@@ -0,0 +1,3 @@
+PROG= swap
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/swap/swap.c b/tools/test/stress2/testcases/swap/swap.c
new file mode 100644
index 000000000000..75bfe7856aed
--- /dev/null
+++ b/tools/test/stress2/testcases/swap/swap.c
@@ -0,0 +1,166 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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/types.h>
+#include <sys/resource.h>
+#include <sys/sysctl.h>
+#include <sys/time.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+#if defined(__LP64__)
+#define MINLEFT (1792LL * 1024 * 1024)
+#else
+#define MINLEFT (1024LL * 1024 * 1024)
+#endif
+
+static int64_t size;
+
+int
+setup(int nb)
+{
+ struct rlimit rlp;
+ int64_t mem, swapinfo;
+ int mi, mx, pct;
+ char *cp;
+
+ if (nb == 0) {
+ mem = usermem();
+ swapinfo = swap();
+
+ pct = 0;
+ if (op->hog == 0) {
+ mi = 80;
+ mx = 100;
+ }
+
+ if (op->hog == 1) {
+ mi = 100;
+ mx = 110;
+ }
+
+ if (op->hog == 2) {
+ mi = 110;
+ mx = 120;
+ }
+
+ if (op->hog >= 3) {
+ mi = 120;
+ mx = 130;
+ }
+ if ((cp = getenv("MAXSWAPPCT")) != NULL && *cp != '\0') {
+ mx = atoi(cp);
+ mi = mx - 10;
+ }
+ pct = random_int(mi, mx);
+
+ if (swapinfo == 0) {
+ pct = random_int(30, 50);
+ if (mem <= MINLEFT) {
+ putval(0);
+ _exit(1);
+ }
+ mem -= MINLEFT;
+ size = mem / 100 * pct;
+ } else {
+ size = mem / 100 * pct;
+ if (size > mem + swapinfo / 4) {
+ size = mem + swapinfo / 4;
+ pct = size * 100 / mem;
+ }
+ }
+
+ size = size / op->incarnations;
+
+ if (getrlimit(RLIMIT_DATA, &rlp) < 0)
+ err(1,"getrlimit");
+ rlp.rlim_cur -= 1024 * 1024;
+
+ if (size > rlp.rlim_cur)
+ size = rlp.rlim_cur;
+ putval(size);
+
+ if (op->verbose > 1 && nb == 0)
+ printf("setup: pid %d, %d%%. Total %dMb, %d thread(s).\n",
+ getpid(), pct, (int)(size / 1024 / 1024 *
+ op->incarnations), op->incarnations);
+ } else
+ size = getval();
+
+ if (size == 0)
+ exit(1);
+
+ return (0);
+}
+
+void
+cleanup(void)
+{
+}
+
+int
+test(void)
+{
+ time_t start;
+ int64_t i, oldsize;
+ int page;
+ char *c;
+
+ if (size == 0)
+ return (0);
+ oldsize = size;
+ c = malloc(size);
+ while (c == NULL && done_testing == 0) {
+ size -= 1024 * 1024;
+ c = malloc(size);
+ }
+ if (op->verbose > 1 && size != oldsize)
+ printf("Malloc size changed from %d Mb to %d Mb\n",
+ (int)(oldsize / 1024 / 1024), (int)(size / 1024 / 102));
+ page = getpagesize();
+ start = time(NULL); /* Livelock workaround */
+ while (done_testing == 0 &&
+ (time(NULL) - start) < op->run_time) {
+ i = 0;
+ while (i < size && done_testing == 0) {
+ c[i] = 0;
+ i += page;
+ }
+#if 0
+ if (op->hog != 1)
+ usleep(1000);
+#endif
+ }
+ free((void *)c);
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/symlink/Makefile b/tools/test/stress2/testcases/symlink/Makefile
new file mode 100644
index 000000000000..d7bb11e7d027
--- /dev/null
+++ b/tools/test/stress2/testcases/symlink/Makefile
@@ -0,0 +1,3 @@
+PROG=symlink
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/symlink/symlink.c b/tools/test/stress2/testcases/symlink/symlink.c
new file mode 100644
index 000000000000..d48470f57e52
--- /dev/null
+++ b/tools/test/stress2/testcases/symlink/symlink.c
@@ -0,0 +1,138 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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/param.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+static char path[128];
+static unsigned long size;
+
+int
+setup(int nb)
+{
+ int64_t in;
+ int64_t bl;
+ int64_t reserve_in;
+ int64_t reserve_bl;
+ int pct;
+
+ umask(0);
+ if (nb == 0) {
+ getdf(&bl, &in);
+ size = in / op->incarnations;
+
+ pct = 90;
+ if (op->hog == 0)
+ pct = random_int(1, 90);
+ size = size / 100 * pct + 1;
+
+ if (size > 16000)
+ size = 16000; /* arbitrary limit number of files pr. dir */
+
+ /* Resource requirements: */
+ while (size > 0) {
+ reserve_in = 1 * size * op->incarnations + op->incarnations;
+ reserve_bl = 26 * size * op->incarnations;
+ if (reserve_bl <= bl && reserve_in <= in)
+ break;
+ size = size / 2;
+ }
+ if (size == 0)
+ reserve_bl = reserve_in = 0;
+
+ if (op->verbose > 1)
+ printf("symlink(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
+ size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
+ reservedf(reserve_bl, reserve_in);
+ putval(size);
+ } else {
+ size = getval();
+ }
+ if (size == 0)
+ exit(0);
+
+ sprintf(path,"%s.%05d", getprogname(), getpid());
+ if (mkdir(path, 0770) < 0)
+ err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);
+
+ if (chdir(path) == -1)
+ err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
+
+ return (0);
+}
+
+void
+cleanup(void)
+{
+ if (path[0] != 0) {
+ (void)chdir("..");
+ if (rmdir(path) == -1) {
+ warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);
+ }
+ }
+}
+
+int
+test(void)
+{
+ pid_t pid;
+ int i, j, error = 0;
+ char file[128];
+
+ pid = getpid();
+ for (j = 0; j < (int)size && done_testing == 0; j++) {
+ sprintf(file,"p%05d.%05d", pid, j);
+ if (symlink("/tmp/not/there", file) == -1) {
+ if (errno != EINTR) {
+ warn("symlink(%s). %s.%d", file, __FILE__, __LINE__);
+ error = 1;
+ exit(1);
+ break;
+ }
+ }
+ }
+
+ for (i = --j; i >= 0; i--) {
+ sprintf(file,"p%05d.%05d", pid, i);
+ if (unlink(file) == -1)
+ err(3, "unlink(%s)", file);
+ }
+
+ if (error != 0)
+ exit(1);
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/sysctl/Makefile b/tools/test/stress2/testcases/sysctl/Makefile
new file mode 100644
index 000000000000..0f22b3b69a4c
--- /dev/null
+++ b/tools/test/stress2/testcases/sysctl/Makefile
@@ -0,0 +1,3 @@
+PROG= sysctl
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/sysctl/sysctl.c b/tools/test/stress2/testcases/sysctl/sysctl.c
new file mode 100644
index 000000000000..75c41cc5b4bc
--- /dev/null
+++ b/tools/test/stress2/testcases/sysctl/sysctl.c
@@ -0,0 +1,63 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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.
+ *
+ */
+
+/* Call sysctl(8) and fstat(1) a few times */
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+int
+setup(int nb __unused)
+{
+ return (0);
+}
+
+void
+cleanup(void)
+{
+}
+
+int
+test(void)
+{
+ int i;
+
+ for (i = 0; i < 64 && done_testing == 0; i++) {
+ if (system("sysctl -a > /dev/null 2>&1") == -1)
+ err(1, "system(\"sysctl -a\"), %s:%d", __FILE__, __LINE__);
+
+ if (system("fstat > /dev/null 2>&1") == -1)
+ err(1, "system(\"fstat\"), %s:%d", __FILE__, __LINE__);
+ sleep(1);
+ }
+ return (0);
+}
+
diff --git a/tools/test/stress2/testcases/tcp/Makefile b/tools/test/stress2/testcases/tcp/Makefile
new file mode 100644
index 000000000000..0510fe60df69
--- /dev/null
+++ b/tools/test/stress2/testcases/tcp/Makefile
@@ -0,0 +1,3 @@
+PROG=tcp
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/tcp/tcp.c b/tools/test/stress2/testcases/tcp/tcp.c
new file mode 100644
index 000000000000..5d8d7197b5be
--- /dev/null
+++ b/tools/test/stress2/testcases/tcp/tcp.c
@@ -0,0 +1,188 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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/param.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <errno.h>
+#include <netdb.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+#define NB (400 * 1024 * 1024)
+
+static int port;
+static int bufsize;
+
+static void
+reader(void) {
+ struct sockaddr_in inetaddr, inetpeer;
+ socklen_t len;
+ int on;
+ int n, t, *buf;
+ int tcpsock, msgsock;
+
+ alarm(op->run_time + 30);
+ on = 1;
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_addr.s_addr = INADDR_ANY;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ if (bind(tcpsock,
+ (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
+ err(1, "bind(), %s:%d", __FILE__, __LINE__);
+
+ if (listen(tcpsock, 5) < 0)
+ err(1, "listen(), %s:%d", __FILE__, __LINE__);
+
+ if ((random_int(1,100) > 60) || (op->hog == 1)) {
+ usleep(random_int(1000000,1000000) * 60);
+ }
+
+ len = sizeof(inetpeer);
+ if ((msgsock = accept(tcpsock,
+ (struct sockaddr *)&inetpeer, &len)) < 0)
+ err(1, "accept(), %s:%d", __FILE__, __LINE__);
+
+ t = 0;
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+ while (done_testing == 0) {
+ if ((n = read(msgsock, buf, bufsize)) < 0)
+ err(1, "read(), %s:%d", __FILE__, __LINE__);
+ t += n;
+ if (n == 0) break;
+ }
+ close(msgsock);
+ return;
+}
+
+static void
+writer(void) {
+ struct sockaddr_in inetaddr;
+ struct hostent *hostent;
+ int i, *buf, r;
+ int tcpsock, on;
+
+ alarm(op->run_time + 30);
+ on = 1;
+ for (i = 1; i < 5; i++) {
+ if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ err(1, "socket(), %s:%d", __FILE__, __LINE__);
+
+ if (setsockopt(tcpsock,
+ SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
+ err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
+
+ hostent = gethostbyname ("localhost");
+ bzero((char *) &inetaddr, sizeof(inetaddr));
+ memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
+ sizeof (struct in_addr));
+
+ inetaddr.sin_family = AF_INET;
+ inetaddr.sin_port = htons(port);
+ inetaddr.sin_len = sizeof(inetaddr);
+
+ r = connect(tcpsock, (struct sockaddr *) &inetaddr,
+ sizeof(inetaddr));
+ if (r == 0)
+ break;
+ sleep(1);
+ close(tcpsock);
+ }
+ if (r < 0)
+ err(1, "connect(), %s:%d", __FILE__, __LINE__);
+
+ if ((buf = malloc(bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+ for (i = 0; i < bufsize / (int)sizeof(int); i++)
+ buf[i] = i;
+
+ for (;;) {
+ for (i = 0; i < NB; i+= bufsize) {
+ if (write(tcpsock, buf, bufsize) < 0) {
+ if (errno == EPIPE)
+ return;
+ if (errno != ECONNRESET)
+ err(1, "write(%d), %s:%d", tcpsock,
+ __FILE__, __LINE__);
+ _exit(EXIT_SUCCESS);
+ }
+ }
+ }
+ return;
+}
+
+int
+setup(int nb)
+{
+ port = 12340 + nb;
+ bufsize = 2 << random_int(1, 12);
+ return (0);
+}
+
+void
+cleanup(void)
+{
+}
+
+int
+test(void)
+{
+ pid_t pid;
+
+ if ((pid = fork()) == 0) {
+ writer();
+ _exit(EXIT_SUCCESS);
+
+ } else if (pid > 0) {
+ reader();
+ kill(pid, SIGINT);
+ if (waitpid(pid, NULL, 0) != pid)
+ err(1, "waitpid(%d)", pid);
+ } else
+ err(1, "fork(), %s:%d", __FILE__, __LINE__);
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/thr1/Makefile b/tools/test/stress2/testcases/thr1/Makefile
new file mode 100644
index 000000000000..e19f6f58e6ca
--- /dev/null
+++ b/tools/test/stress2/testcases/thr1/Makefile
@@ -0,0 +1,5 @@
+PROG= thr1
+
+.include <bsd.prog.mk>
+
+CFLAGS+= -pthread
diff --git a/tools/test/stress2/testcases/thr1/thr1.c b/tools/test/stress2/testcases/thr1/thr1.c
new file mode 100644
index 000000000000..ce05e5d94840
--- /dev/null
+++ b/tools/test/stress2/testcases/thr1/thr1.c
@@ -0,0 +1,75 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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 <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+#define NTHREADS 256
+
+int
+setup(int nb __unused)
+{
+ return (0);
+}
+
+void
+cleanup(void)
+{
+}
+
+static void *
+thr_routine(void *arg __unused)
+{
+ (void) getpid();
+ return (0);
+}
+
+int
+test(void)
+{
+ pthread_t threads[NTHREADS];
+ int i;
+ int r;
+
+ for (i = 0; i < NTHREADS; i++)
+ if ((r = pthread_create(&threads[i], NULL, thr_routine, 0)) != 0)
+ errc(1, r, "pthread_create()");
+
+ for (i = 0; i < NTHREADS; i++)
+ if ((r = pthread_join(threads[i], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", i);
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/thr2/Makefile b/tools/test/stress2/testcases/thr2/Makefile
new file mode 100644
index 000000000000..b54daf2ceb7c
--- /dev/null
+++ b/tools/test/stress2/testcases/thr2/Makefile
@@ -0,0 +1,5 @@
+PROG= thr2
+
+.include <bsd.prog.mk>
+
+CFLAGS+= -pthread
diff --git a/tools/test/stress2/testcases/thr2/thr2.c b/tools/test/stress2/testcases/thr2/thr2.c
new file mode 100644
index 000000000000..22549b3f81dd
--- /dev/null
+++ b/tools/test/stress2/testcases/thr2/thr2.c
@@ -0,0 +1,93 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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 <err.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+#define NTHREADS 256
+
+static volatile int done = 0;
+
+int
+setup(int nb __unused)
+{
+ return (0);
+}
+
+void
+cleanup(void)
+{
+}
+
+static void *
+thr1(void *arg __unused)
+{
+ return (0);
+}
+
+static void *
+thr2(void *arg __unused)
+{
+ while (done == 0)
+ pthread_yield();
+ return (0);
+}
+
+int
+test(void)
+{
+ pthread_t threads[NTHREADS];
+ int i;
+ int r;
+
+ for (i = 0; i < NTHREADS; i++)
+ if ((r = pthread_create(&threads[i], NULL, thr1, 0)) != 0)
+ errc(1, r, "pthread_create()");
+
+ for (i = 0; i < NTHREADS; i++)
+ if (pthread_join(threads[i], NULL) != 0)
+ errc(1, r, "pthread_join(%d)", i);
+
+ for (i = 0; i < NTHREADS; i++)
+ if ((r = pthread_create(&threads[i], NULL, thr2, 0)) != 0)
+ errc(1, r, "pthread_create()");
+ done = 1;
+
+ for (i = 0; i < NTHREADS; i++)
+ if ((r = pthread_join(threads[i], NULL)) != 0)
+ errc(1, r, "pthread_join(%d)", i);
+
+ return (0);
+}
diff --git a/tools/test/stress2/testcases/udp/Makefile b/tools/test/stress2/testcases/udp/Makefile
new file mode 100644
index 000000000000..9640d8069b77
--- /dev/null
+++ b/tools/test/stress2/testcases/udp/Makefile
@@ -0,0 +1,3 @@
+PROG= udp
+
+.include <bsd.prog.mk>
diff --git a/tools/test/stress2/testcases/udp/udp.c b/tools/test/stress2/testcases/udp/udp.c
new file mode 100644
index 000000000000..7c805d6a965b
--- /dev/null
+++ b/tools/test/stress2/testcases/udp/udp.c
@@ -0,0 +1,112 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <err.h>
+#include <errno.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "stress.h"
+
+static int bufsize;
+
+int
+setup(int nb __unused)
+{
+ bufsize = 2 << random_int(1, 12);
+ return (0);
+}
+
+void
+cleanup(void)
+{
+}
+
+int
+test(void)
+{
+ struct sockaddr_in sock_in;
+ struct hostent *host;
+ const char *hostname;
+ int f, i, n;
+ int *buf;
+
+ bzero((char *)&sock_in, sizeof(sock_in));
+ sock_in.sin_family = AF_INET;
+ f = socket(AF_INET, SOCK_DGRAM, 0);
+ if (f < 0)
+ err(1, "socket");
+ if (bind(f, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0) {
+ warn("bind");
+ return (1);
+ }
+ if (getenv("BLASTHOST") == NULL)
+ hostname = "localhost";
+ else
+ hostname = getenv("BLASTHOST");
+ host = gethostbyname(hostname);
+ if (host) {
+ sock_in.sin_family = host->h_addrtype;
+ bcopy(host->h_addr, &sock_in.sin_addr, host->h_length);
+ } else {
+ sock_in.sin_family = AF_INET;
+ sock_in.sin_addr.s_addr = inet_addr(hostname);
+ if (sock_in.sin_addr.s_addr == INADDR_NONE) {
+ err(1, "host: %s", hostname);
+ }
+ }
+ sock_in.sin_port = htons(9);
+
+ if (connect(f, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0)
+ err(1, "connect");
+
+ if ((buf = calloc(1, bufsize)) == NULL)
+ err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
+
+ if (op->verbose > 1)
+ printf("udp %s:9 with %d bytes\n", hostname, bufsize);
+ for (i = 0; i < 128 && done_testing == 0; i++) {
+ n = write(f, buf, bufsize);
+ if (n == -1 && errno == ENOBUFS)
+ continue;
+ if (n == -1 && errno == ECONNREFUSED)
+ break;
+ if (n == -1)
+ err(1, "write(%d) #%d", bufsize, i);
+ if (n == 0) break;
+ }
+ free(buf);
+ close(f);
+ return (0);
+}
diff --git a/tools/test/stress2/tools/bench.c b/tools/test/stress2/tools/bench.c
new file mode 100644
index 000000000000..e986fb6db791
--- /dev/null
+++ b/tools/test/stress2/tools/bench.c
@@ -0,0 +1,356 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2018 Dell EMC Isilon
+ *
+ * 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 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/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <machine/atomic.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define PARALLEL 3
+#define SYNC 0
+
+static void cr1(void);
+static void cr2(void);
+static void cr3(void);
+static void rn1(void);
+static void rw1(void);
+static void rw2(void);
+static void (*functions[])(void) = {&cr1, &cr2, &cr3, &rn1, &rw1, &rw2};
+
+static volatile u_int *share;
+static int tests;
+
+static void
+cr1(void)
+{
+ int fd, i, j;
+ int loops = 9000;
+ char file[128];
+
+ setproctitle("%s sync", __func__);
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != (volatile u_int)tests * PARALLEL)
+ usleep(100);
+ setproctitle("%s", __func__);
+ for (j = 0; j < 10; j++) {
+ for (i = 0; i < loops; i++) {
+ snprintf(file, sizeof(file), "%s.%06d.%03d",
+ __func__, getpid(), i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
+ DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ close(fd);
+ if (i % 1000 == 0)
+ usleep(100);
+ }
+ for (i = 0; i < loops; i++) {
+ snprintf(file, sizeof(file), "%s.%06d.%03d",
+ __func__, getpid(), i);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+ }
+ }
+}
+
+static void
+cr2(void)
+{
+ int fd, i, j;
+ char file[1024];
+
+ setproctitle("%s sync", __func__);
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != (volatile u_int)tests * PARALLEL)
+ usleep(100);
+ setproctitle("%s", __func__);
+ for (j = 0; j < 3; j++) {
+ for (i = 0; i < 40000; i++) {
+ snprintf(file, sizeof(file), "%s.%06d.%03d",
+ __func__, getpid(), i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
+ DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ close(fd);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+ if (i % 1000 == 0)
+ usleep(100);
+ }
+ }
+}
+
+static void
+cr3(void)
+{
+ int fd, i, j;
+ int loops = 10000;
+ char file[1024], path[1024];
+
+ setproctitle("%s sync", __func__);
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != (volatile u_int)tests * PARALLEL)
+ usleep(100);
+ setproctitle("%s", __func__);
+ getcwd(path, sizeof(path));
+ for (j = 0; j < 7; j++) {
+ for (i = 0; i < loops; i++) {
+ snprintf(file, sizeof(file), "%s/%s.%06d.%03d",
+ path, __func__, getpid(), i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
+ DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ close(fd);
+ if (i % 1000 == 0)
+ usleep(100);
+ }
+ for (i = 0; i < loops; i++) {
+ snprintf(file, sizeof(file), "%s/%s.%06d.%03d",
+ path, __func__, getpid(), i);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+ }
+ }
+}
+
+static void
+rn1(void)
+{
+ int fd, i, j;
+ int loops = 10000;
+ char file[128], new[128];
+
+ setproctitle("%s sync", __func__);
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != (volatile u_int)tests * PARALLEL)
+ usleep(100);
+ setproctitle("%s", __func__);
+
+ for (j = 0; j < 8; j++) {
+ for (i = 0; i < loops; i++) {
+ snprintf(file, sizeof(file), "%s.%06d.%03d",
+ __func__, getpid(), i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
+ DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ close(fd);
+ snprintf(new, sizeof(new), "%s.%06d.%03d.new",
+ __func__, getpid(), i);
+ if (rename(file, new) == -1)
+ err(1, "rename(%s, %s)", file, new);
+ if (unlink(new) == -1)
+ err(1, "unlink(%s)", new);
+ if (i % 1000 == 0)
+ usleep(100);
+ }
+ }
+}
+
+static void
+rw1(void)
+{
+ int fd, i;
+ int loops = 10000;
+ char buf[512], file[128];
+
+ setproctitle("%s sync", __func__);
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != (volatile u_int)tests * PARALLEL)
+ usleep(100);
+
+ setproctitle("%s", __func__);
+ memset(buf, 0, sizeof(buf));
+ for (i = 0; i < loops; i++) {
+ snprintf(file, sizeof(file), "rw1.%06d.%03d", getpid(), i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
+ DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ if (write(fd, buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write(%s)", file);
+ close(fd);
+ }
+ for (i = 0; i < loops; i++) {
+ snprintf(file, sizeof(file), "rw1.%06d.%03d", getpid(), i);
+ if ((fd = open(file, O_RDONLY)) == -1)
+ err(1, "open(%s)", file);
+ if (read(fd, buf, sizeof(buf)) != sizeof(buf))
+ err(1, "write(%s)", file);
+ close(fd);
+ usleep(100);
+ }
+ for (i = 0; i < loops; i++) {
+ snprintf(file, sizeof(file), "rw1.%06d.%03d", getpid(), i);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+ }
+}
+
+static void
+rw2(void)
+{
+ int fd, i;
+ int loops = 8000;
+ int siz = 4096;
+ char *buf, file[128];
+
+ setproctitle("%s sync", __func__);
+ atomic_add_int(&share[SYNC], 1);
+ while (share[SYNC] != (volatile u_int)tests * PARALLEL)
+ usleep(100);
+
+ setproctitle("%s", __func__);
+ buf = calloc(1, siz);
+ for (i = 0; i < loops; i++) {
+ snprintf(file, sizeof(file), "rw1.%06d.%03d", getpid(), i);
+ if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
+ DEFFILEMODE)) == -1)
+ err(1, "open(%s)", file);
+ if (write(fd, buf, siz) != siz)
+ err(1, "write(%s)", file);
+ close(fd);
+ }
+ for (i = 0; i < loops; i++) {
+ snprintf(file, sizeof(file), "rw1.%06d.%03d", getpid(), i);
+ if ((fd = open(file, O_RDONLY)) == -1)
+ err(1, "open(%s)", file);
+ if (read(fd, buf, siz) != siz)
+ err(1, "write(%s)", file);
+ close(fd);
+ usleep(100);
+ }
+ for (i = 0; i < loops; i++) {
+ snprintf(file, sizeof(file), "rw1.%06d.%03d", getpid(), i);
+ if (unlink(file) == -1)
+ err(1, "unlink(%s)", file);
+ }
+}
+
+static void
+spawn(void f(), int idx)
+{
+ pid_t pids[PARALLEL];
+ int i, status;
+ char dir[128];
+
+ snprintf(dir, sizeof(dir), "f%d.%d.d",getpid(), idx);
+ rmdir(dir);
+ if (mkdir(dir, 0770) == -1)
+ err(1, "mkdir(%s)", dir);
+ if (chdir(dir) == -1)
+ err(1, "chdir(%s)", dir);
+ for (i = 0; i < PARALLEL; i++) {
+ if ((pids[i] = fork()) == 0) {
+ f();
+ _exit(0);
+ }
+ if (pids[i] == -1)
+ err(1, "fork(). %s:%d", __func__, __LINE__);
+ }
+ for (i = 0; i < PARALLEL; i++) {
+ if (waitpid(pids[i], &status, 0) != pids[i])
+ err(1, "waitpid(). %s:%d", __func__, __LINE__);
+ }
+ if (chdir("..") == -1)
+ err(1, "chdir(..)");
+ if (rmdir(dir) == -1)
+ err(1, "rmdir(%s)", dir);
+
+}
+
+void
+usage(void)
+{
+ fprintf(stderr, "Usage: %s [-t]\n", getprogname());
+ exit(1);
+}
+
+int
+main(int argc, char *argv[])
+{
+ pid_t *pids;
+ struct timeval t1, t2, diff;
+ size_t len;
+ time_t start;
+ int ch, i, status, timing;
+
+ timing = 0;
+ while ((ch = getopt(argc, argv, "t")) != -1)
+ switch(ch) {
+ case 't':
+ timing = 1;
+ break;
+ default:
+ usage();
+ }
+ argc -= optind;
+ argv += optind;
+
+ setproctitle("%s", __func__);
+ tests = (int)(sizeof(functions) / sizeof(functions[0]));
+ pids = malloc(tests * sizeof(pid_t));
+ len = PAGE_SIZE;
+ if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ gettimeofday(&t1, NULL);
+ for (i = 0; i < tests; i++) {
+ if ((pids[i] = fork()) == 0) {
+ start = time(NULL);
+ spawn(functions[i], i);
+#if defined(DEBUG)
+ fprintf(stderr, "%d: %d elapsed\n", i ,
+ time(NULL) - start);
+#endif
+ _exit(0);
+ }
+ if (pids[i] == -1)
+ err(1, "fork(). %s:%d", __func__, __LINE__);
+ }
+ for (i = 0; i < tests; i++) {
+ if (waitpid(pids[i], &status, 0) != pids[i])
+ err(1, "waitpid(%d). i=%d %s:%d", pids[i], i,
+ __func__, __LINE__);
+ }
+ gettimeofday(&t2, NULL);
+ timersub(&t2, &t1, &diff);
+ if (timing == 1)
+ printf("%jd.%06ld\n",(intmax_t)diff.tv_sec, diff.tv_usec);
+
+ return (0);
+}
diff --git a/tools/test/stress2/tools/calc_mem_use.pl b/tools/test/stress2/tools/calc_mem_use.pl
new file mode 100644
index 000000000000..08bcad828eb7
--- /dev/null
+++ b/tools/test/stress2/tools/calc_mem_use.pl
@@ -0,0 +1,61 @@
+#!/usr/bin/env perl
+# $Id: calc_mem_use.pl,v 1.3 2015/04/21 13:20:29 kostik Exp kostik $
+
+use strict;
+use warnings;
+require Data::Dumper;
+
+my $in_z = 0;
+my $in_m = 0;
+my $total_z = 0;
+my $total_zf = 0;
+my $total_m = 0;
+
+while (<>) {
+ chomp;
+ if (/Type\s+InUse\s+MemUse\s+HighUse\s+Requests\s+Size\(s\)/) {
+ $in_z = 0;
+ $in_m = 1;
+ next;
+ }
+ if (/ITEM\s+SIZE\s+LIMIT\s+USED\s+FREE\s+REQ\s+FAIL\s+SLEEP/) {
+ $in_z = 1;
+ $in_m = 0;
+ next;
+ }
+ if ($in_z) {
+ (my @fields) = split /:|,/;
+#print Data::Dumper::Dumper(\@fields);
+ next unless ($#fields >= 7);
+ my $size = $fields[1];
+ my $used = $fields[3];
+ my $free = $fields[4];
+ $total_z += int($size) * int($used);
+ $total_zf += int($size) * int($free);
+ next;
+ }
+ if ($in_m) {
+ my $line = $_;
+ while (1) {
+ $line =~ s/^\s+//;
+ last unless ($line =~ s/^[a-zA-Z][a-zA-Z0-9\.\-_]*//);
+ }
+ my @fields;
+ @fields = split(/\s+/, $line);
+ my $memuse_s = $fields[1];
+ my $memuse;
+ if ($memuse_s =~ s/K$//) {
+ $memuse = int($memuse_s) * 1024;
+ } elsif ($memuse_s =~ s/M$//) {
+ $memuse = int($memuse_s) * 1024 * 1024 ;
+ } else {
+ $memuse = int($memuse_s);
+ }
+ $total_m += $memuse;
+ next;
+ }
+}
+
+printf "Zones: %dK ZoneFree: %dK Malloc: %dK Total: %dK\n",
+ $total_z / 1024, $total_zf / 1024, $total_m / 1024,
+ ($total_z + $total_zf + $total_m) / 1024;
diff --git a/tools/test/stress2/tools/df.sh b/tools/test/stress2/tools/df.sh
new file mode 100755
index 000000000000..be1c4bd9b278
--- /dev/null
+++ b/tools/test/stress2/tools/df.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Trace the df of RUNDIR
+
+. ../default.cfg
+
+old=x
+while true;do
+ d=`df -i $RUNDIR | tail -1`
+ p1=`echo $d | awk '{print $5}' | sed 's/\%//'`
+ p2=`echo $d | awk '{print $8}' | sed 's/\%//'`
+ [ $p1 -lt 80 -a $p2 -lt 80 -a $old != x ] && continue
+ [ "$old" != "$d" ] && echo `date '+%T'` $d
+ old=$d
+ sleep 30
+done
diff --git a/tools/test/stress2/tools/fail.sh b/tools/test/stress2/tools/fail.sh
new file mode 100755
index 000000000000..6d83d8fe0d22
--- /dev/null
+++ b/tools/test/stress2/tools/fail.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# List tests returning a non zero exit code from today and yesterday.
+
+log=/tmp/stress2.d/fail
+out=/tmp/fail.sh.out
+[ -r $log ] || exit 1
+
+egrep "^(`date +%Y%m%d`|`date -v-1d +%Y%m%d`)" $log | while read l; do
+ name=`echo $l | sed 's/.* //'`
+ grep -q "^$name" $0 && continue
+ echo $name
+done | sort -u | while read f; do
+ grep $f $log | tail -1
+done | sort -n > $out
+if [ -s $out ]; then
+ echo
+ echo "Summary: Tests which failed within the last two days."
+ while read l; do
+ n=` echo $l | sed 's/.* //'`
+ c=`grep -c $n $log`
+ [ $c -eq 1 ] &&
+ echo "$l (new)" || echo "$l"
+ done < $out
+fi
+rm -f $out
+exit 0
+
+# List of known failures:
+
+credleak.sh 20170321 Known lockd issue
+db.sh 20170323 ls stalls for more than 90 secs
+dev3.sh 20170323 pts memory leak
+gnop5.sh 20170905 mount: Invalid sectorsize 16384
+graid1_7.sh 20170430 FAIL Remove component gptid/02b9...
+graid1_8.sh 20170512 Known: do not run fsck
+mountu.sh 20170321 Known NFS problem
+nfs13.sh 20170323 tmpfs using options triggers this
+nfs15lockd.sh 20170330 unmount of /mnt failed: Device busy
+nfs15lockd3.sh 20170330 unmount of /mnt failed: Device busy
+nfssillyrename.sh 20170321 Known problem
+rename11.sh 20170329 Too many links seen with SU.
+swappedout.sh 20170321 Known to fail
+tmpfs18.sh 20170323 tmpfs -o issue
+tvnlru.sh 20170329 Known performance issue
+umountf2.sh 20170323 umount hangs
+vm_fault_dontneed.sh 20170321 Recognition of sequential access
diff --git a/tools/test/stress2/tools/fast.sh b/tools/test/stress2/tools/fast.sh
new file mode 100755
index 000000000000..ce33eaa4c103
--- /dev/null
+++ b/tools/test/stress2/tools/fast.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Sort tests by increasing run time.
+
+log=/tmp/stress2.d/elapsed
+lst=/tmp/elapsed.lst
+lst2=/tmp/elapsed.order
+tim=/tmp/elapsed.tim
+[ ! -f $log ] && { echo "$0: No such file: $log" 1>&2; exit 1; }
+[ ! -s $log ] && { echo "$0: Empty file: $log" 1>&2; exit 1; }
+[ `wc -l < $log` -lt 100 ] && exit 1
+
+export LANG=C
+cd ../misc
+ls *.sh | grep -E -v "all.sh|cleanup.sh" > $lst
+rm -f $lst2
+cat $lst | while read l; do
+ [ -f ./$l ] || continue
+ grep -w $l $log | sed 's/.* //' > $tim
+ if [ `wc -l < $tim` -gt 2 ]; then
+ # Get the average value
+ maxtime=`ministat -n < $tim | tail -1 | awk '{print int($6 + 0.5)}'`
+ else
+ maxtime=`sed 's/.* //' < $tim | sort -n | tail -1`
+ fi
+ [ -z "$maxtime" ] && maxtime=999
+ echo "$maxtime $l" >> $lst2
+done
+sort -n < $lst2 | awk '{print $2}' > $lst
+cat $lst
+rm -f $lst2 $tim
+exit 0
diff --git a/tools/test/stress2/tools/flip.c b/tools/test/stress2/tools/flip.c
new file mode 100644
index 000000000000..816162947c45
--- /dev/null
+++ b/tools/test/stress2/tools/flip.c
@@ -0,0 +1,121 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2020 Peter Holm <pho@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 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.
+ */
+
+/*
+ * Flip one or more bits in a file.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+extern char *optarg;
+extern int optind;
+
+static long
+random_long(long mi, long ma)
+{
+ return (arc4random() % (ma - mi + 1) + mi);
+}
+
+static void
+flip(void *ap, size_t len)
+{
+ unsigned char *cp;
+ int byte;
+ unsigned char bit, buf, mask, old;
+
+ cp = (unsigned char *)ap;
+ byte = random_long(0, len);
+ bit = random_long(0,7);
+ mask = ~(1 << bit);
+ buf = cp[byte];
+ old = cp[byte];
+ buf = (buf & mask) | (~buf & ~mask);
+ cp[byte] = buf;
+#if defined(DEBUG)
+ printf("Change %2x to %2x at %d by flipping bit %d\n",
+ old, buf, byte, bit);
+#endif
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct stat st;
+ off_t pos;
+ int fd, i, times;
+ char c;
+
+ times = 1;
+ while ((c = getopt(argc, argv, "n:")) != -1) {
+ switch (c) {
+ case 'n':
+ times = atoi(optarg);
+ break;
+ case '?':
+ default:
+ fprintf(stderr,
+ "Usage: %s [ -n <num> <file>]\n",
+ argv[0]);
+ exit(1);
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (argc != 1) {
+ fprintf(stderr, "Missing file name\n");
+ exit(1);
+ }
+
+ if ((fd = open(argv[0], O_RDWR)) == -1)
+ err(1, "open(%s)", argv[0]);
+
+ if (fstat(fd, &st) == -1)
+ err(1, "stat %s", argv[0]);
+
+ for (i = 0; i < times; i++) {
+ pos = arc4random() % st.st_size;
+ if (lseek(fd, pos, SEEK_SET) == -1)
+ err(1, "lseek()");
+ read(fd, &c, 1);
+ flip(&c, 1);
+ if (lseek(fd, pos, SEEK_SET) == -1)
+ err(1, "lseek()");
+ write(fd, &c, 1);
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/tools/freeze.sh b/tools/test/stress2/tools/freeze.sh
new file mode 100755
index 000000000000..5f4b237e4697
--- /dev/null
+++ b/tools/test/stress2/tools/freeze.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Freeze detection script
+
+while true; do
+ t1=`date '+%s'`
+ sleep 60
+ t2=`date '+%s'`
+ delta=$((t2 - $t1))
+ if [ $delta -gt 120 ]; then
+ d1=`date -j -f '%s' '+%T' $t1`
+ d2=`date -j -f '%s' '+%T' $t2`
+ e=` date -u -j -f '%s' '+%T' $delta`
+ echo "Freeze from $d1 to $d2, $e elapsed."
+ fi
+done
diff --git a/tools/test/stress2/tools/freeze2.sh b/tools/test/stress2/tools/freeze2.sh
new file mode 100755
index 000000000000..de66754cbeb4
--- /dev/null
+++ b/tools/test/stress2/tools/freeze2.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Freeze detection script
+
+while true; do
+ t1=`date '+%s'`
+ ls -l /tmp /mnt > /dev/null
+ sleep 30
+ t2=`date '+%s'`
+ delta=$((t2 - $t1))
+ if [ $delta -gt 120 ]; then
+ d1=`date -j -f '%s' '+%T' $t1`
+ d2=`date -j -f '%s' '+%T' $t2`
+ e=` date -u -j -f '%s' '+%T' $delta`
+ echo "Freeze from $d1 to $d2, $e elapsed."
+ fi
+done
diff --git a/tools/test/stress2/tools/fstool.c b/tools/test/stress2/tools/fstool.c
new file mode 100644
index 000000000000..fcc8689441a8
--- /dev/null
+++ b/tools/test/stress2/tools/fstool.c
@@ -0,0 +1,236 @@
+/*-
+ * Copyright (c) 2008 Peter Holm <pho@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 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/types.h>
+#include <sys/errno.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+extern char *optarg;
+static pid_t pid;
+static int edirs, dirs, files, fs;
+static int max, rnd;
+static char *buffer;
+static char here[MAXPATHLEN + 1];
+
+static void
+mkDir2(char *path) {
+ int fd, i, j, len;
+ char newPath[MAXPATHLEN + 1];
+ char file[128];
+
+ if (mkdir(path, 0700) == -1)
+ err(1, "mkdir(%s)", path);
+ if (chdir(path) == -1)
+ err(1, "chdir(%s) @ %d", path, __LINE__);
+
+ for (i = 0; i < max; i++) {
+ sprintf(newPath,"d%d", i);
+ if (mkdir(newPath, 0700) == -1)
+ err(1, "mkdir(%s) @ %d", newPath, __LINE__);
+ if (chdir(newPath) == -1)
+ err(1, "chdir(%s) @ %d", newPath, __LINE__);
+
+ len = fs;
+ for (j = 0; j < files; j++) {
+ if (rnd)
+ len = arc4random() % fs + 1;
+ sprintf(file,"f%05d", j);
+ if ((fd = creat(file, 0660)) == -1) {
+ if (errno != EINTR) {
+ err(1, "%d: creat(%s)", j, file);
+ break;
+ }
+ }
+ if (write(fd, buffer, len) != len)
+ err(1, "%d: write(%s), %s:%d", j, file, __FILE__, __LINE__);
+
+ if (fd != -1 && close(fd) == -1)
+ err(2, "%d: close(%d)", j, j);
+
+ }
+ for (j = 0; j < edirs; j++) {
+ sprintf(newPath,"e%d", j);
+ if (mkdir(newPath, 0700) == -1)
+ err(1, "mkdir(%s) @ %d", newPath, __LINE__);
+ }
+ }
+ chdir(here);
+}
+
+static void
+mkDir(void) {
+ int i;
+ char path[MAXPATHLEN + 1];
+
+ for (i = 0; i < dirs; i++) {
+ sprintf(path,"fstool.%06d.%d", pid, i);
+ mkDir2(path);
+ }
+}
+
+static void
+rmFile(void)
+{
+ int j;
+ char file[128], newPath[128];
+
+ for (j = 0; j < files; j++) {
+ sprintf(file,"f%05d", j);
+ (void) unlink(file);
+ }
+ for (j = 0; j < edirs; j++) {
+ sprintf(newPath,"e%d", j);
+ if (rmdir(newPath) == -1)
+ err(1, "rmdir(%s) @ %d", newPath, __LINE__);
+ }
+}
+
+static void
+rmDir2(char *path) {
+ int i, j;
+ char newPath[10];
+
+ if (chdir(path) == -1)
+ err(1, "chdir(%s)", path);
+
+ for (i = 0; i < max; i++) {
+ sprintf(newPath,"d%d", i);
+ if (chdir(newPath) == -1)
+ err(1, "chdir(%s)", newPath);
+ }
+ for (j = 0; j < max; j++) {
+ rmFile();
+ if (chdir ("..") == -1)
+ err(1, "chdir(\"..\")");
+ sprintf(newPath,"d%d", max - j - 1);
+ if (rmdir(newPath) == -1)
+ err(1, "rmdir(%s) @ %d", newPath, __LINE__);
+ }
+ if (chdir ("..") == -1)
+ err(1, "chdir(\"..\")");
+ if (rmdir(path) == -1)
+ err(1, "rmdir(%s) @ %d", path, __LINE__);
+}
+
+static void
+rmDir(void) {
+ int i;
+ char path[MAXPATHLEN + 1];
+
+ for (i = 0; i < dirs; i++) {
+ sprintf(path,"fstool.%06d.%d", pid, i);
+ rmDir2(path);
+ }
+}
+
+int
+main(int argc, char **argv)
+{
+ int c, i, levels, leave, times;
+ char ch = 0;
+
+ edirs = 0;
+ dirs = 1;
+ files = 5;
+ fs = 1024;
+ leave = 0;
+ levels = 1;
+ times = 1;
+
+ while ((c = getopt(argc, argv, "ad:e:ln:r:f:s:t:")) != -1)
+ switch (c) {
+ case 'a':
+ rnd = 1;
+ break;
+ case 'd':
+ dirs = atoi(optarg);
+ break;
+ case 'e':
+ edirs = atoi(optarg);
+ break;
+ case 'f':
+ files = atoi(optarg);
+ break;
+ case 'l':
+ leave = 1;
+ break;
+ case 'n':
+ levels = atoi(optarg);
+ break;
+ case 's':
+ sscanf(optarg, "%d%c", &fs, &ch);
+ if (ch == 'k' || ch == 'K')
+ fs = fs * 1024;
+ if (ch == 'm' || ch == 'M')
+ fs = fs * 1024 * 1024;
+ break;
+ case 't':
+ times = atoi(optarg);
+ break;
+ default:
+ fprintf(stderr,
+ "Usage: %s [ -a ] "
+ "[ -d <parallel dirs> ] [ -e <dirs> ] "
+ "[ -f <num> ][ -l] [ -n <depth> ] "
+ " [ -s <file size> ]\n", argv[0]);
+ printf(" -a: random file size 1-s.\n");
+ printf(" -d: Tree width (defaults to 1).\n");
+ printf(" -e: Directories at each level.\n");
+ printf(" -f: Number of files.\n");
+ printf(" -l: Leave the files and dirs.\n");
+ printf(" -n: Tree depth.\n");
+ printf(" -s: Size of each file.\n");
+ printf(" -t: Number of times to repeat\n");
+ exit(1);
+ }
+
+ if (times != 1)
+ leave = 0;
+ max = levels;
+ pid = getpid();
+ if ((buffer = calloc(1, fs)) == NULL)
+ err(1, "calloc(%d)", fs);
+ if (getcwd(here, sizeof(here)) == NULL)
+ err(1, "getcwd()");
+
+ for (i = 0; i < times; i++) {
+ mkDir();
+ if (leave == 0)
+ rmDir();
+ }
+
+ return (0);
+}
diff --git a/tools/test/stress2/tools/iwatch.sh b/tools/test/stress2/tools/iwatch.sh
new file mode 100755
index 000000000000..82052d5a2ab0
--- /dev/null
+++ b/tools/test/stress2/tools/iwatch.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Watch max inode usage in RUNDIR
+
+. ../default.cfg
+
+[ ! -d $RUNDIR ] && mkdir $RUNDIR
+imax=0
+kmax=0
+istart=`df -ik $RUNDIR | tail -1 | awk '{print $6}'`
+kstart=`df -ik $RUNDIR | tail -1 | awk '{print $3}'`
+while true;do
+ i=`df -ik $RUNDIR | tail -1 | awk '{print $6}'`
+ k=`df -ik $RUNDIR | tail -1 | awk '{print $3}'`
+ if [ $i -gt $imax -o $k -gt $kmax ]; then
+ imax=$i
+ kmax=$k
+ printf "%s %d %dk (%d %dk)\n" `date '+%T'` $i $k $((imax - istart)) $((kmax - kstart))
+ fi
+ sleep 1
+done
diff --git a/tools/test/stress2/tools/killall.sh b/tools/test/stress2/tools/killall.sh
new file mode 100755
index 000000000000..0082c993c686
--- /dev/null
+++ b/tools/test/stress2/tools/killall.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2013 Peter Holm <pho@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 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.
+#
+
+# Kill all running test programs from stress2/testcases
+
+list="`find ../testcases -type f -perm -1 | xargs basename`"
+[ -z "$list" ] && exit 1
+
+i=0
+while pkill -9 $list; do
+ [ $((i += 1)) -lt 3 ] && continue
+ if [ -z "$(ps `pgrep $list` | sed 1d)" ]; then # <defunct>
+ for j in "`pgrep $list`"; do
+ [ -z "$j" ] && break
+ ps auxwwl | awk "\$2 == $j"
+ done
+ fi
+ if [ $i -eq 30 ]; then
+ pgrep $list | xargs ps -lp
+ echo "$0 FAIL @ $i"
+ exit 3
+ fi
+ sleep 5
+done
+exit 0
diff --git a/tools/test/stress2/tools/kldload.sh b/tools/test/stress2/tools/kldload.sh
new file mode 100755
index 000000000000..194be665a436
--- /dev/null
+++ b/tools/test/stress2/tools/kldload.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Add support for kldload(8) of compressed modules.
+
+[ $# -ne 1 ] && echo "Usage: $0 <module>" && exit 1
+
+# If possible avoid using a temporary file due to kgdb.
+kldstat | grep -q $1 && exit 0
+kldload $1 2>/dev/null && exit 0
+
+module=$(basename $1 .ko)
+paths=`sysctl -n kern.module_path`
+
+: ${TMPDIR:=/tmp}
+tmpkld=$(mktemp $TMPDIR/$module.XXXXXX) || exit
+trap "rm -f $tmpkld" EXIT INT TERM
+IFS=';'
+for path in $paths; do
+ kld_base=$path/$module.ko
+ for ext in "" .gz .bz2 .xz; do
+ kld=$kld_base$ext
+ [ -s $kld ] || continue
+ case "$kld" in
+ *.gz)
+ gzcat $kld
+ ;;
+ *.bz2)
+ bzcat $kld
+ ;;
+ *.xz)
+ xzcat $kld
+ ;;
+ *)
+ cat $kld
+ ;;
+ esac > $tmpkld
+ [ $? -eq 0 ] && break
+ rm -f $tmpkld
+ done
+ [ -s $tmpkld ] && kldload $tmpkld; ec=$?
+ [ $ec -eq 0 ] && exit
+done
+
+echo "Could not kldload $kld_base*"
+exit $ec
diff --git a/tools/test/stress2/tools/leaks.sh b/tools/test/stress2/tools/leaks.sh
new file mode 100755
index 000000000000..bc70eefff4e7
--- /dev/null
+++ b/tools/test/stress2/tools/leaks.sh
@@ -0,0 +1,112 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Script to catch possible leaks in vm, malloc and mbufs
+# Report values growing in 10 consecuitive samples
+
+i=0
+while true; do
+ # Check for leaks in vm.zone
+
+# ITEM SIZE LIMIT USED FREE REQUESTS
+#
+# UMA Kegs: 140, 0, 66, 6, 66
+# UMA Zones: 120, 0, 66, 24, 66
+
+ vmstat -z | sed '1,3d;s/://g' | \
+ sed 's/ */ /g;s/\([0-9]\) *\([0-9]\)/\1,\2/g;s/ \([0-9]\)/,\1/;s/^ *//' | \
+ awk -F, '
+/^..*$/{
+ gsub("^ *", "", $1);
+ size=$4;
+ printf "vmstat -z %s,%s\n", $1, size;
+}
+'
+ # vmstat -m
+
+ # Type InUse MemUse HighUse Requests Size(s)
+ # DEVFS3 168 21K - 169 128
+ # DEVFS1 157 40K - 157 256
+ # DEVFS 12 1K - 13 16,128
+ vmstat -m | \
+ sed '1,1d;s/K .*//;s/ [0-9][0-9]* / /;s/ */ /g;s/^ *//;s/ /_/g;s/_\([0-9][0-9]*$\)/ \1/' | \
+ awk '{printf "vmstat -m %s, %d\n", $1, $2}'
+
+ # Check for leaks in mbufs
+
+# $ netstat -m
+# 1233/597/1830 mbufs in use (current/cache/total)
+# 1232/196/1428/8896 mbuf clusters in use (current/cache/total/max)
+# 1232/74 mbuf+clusters out of packet secondary zone in use (current/cache)
+# 0/0/0/0 4k (page size) jumbo clusters in use (current/cache/total/max)
+# 0/0/0/0 9k jumbo clusters in use (current/cache/total/max)
+# 0/0/0/0 16k jumbo clusters in use (current/cache/total/max)
+# 2772K/541K/3313K bytes allocated to network (current/cache/total)
+# 508/7778/5734 requests for mbufs denied (mbufs/clusters/mbuf+clusters)
+# 0/0/0 requests for jumbo clusters denied (4k/9k/16k)
+# 0/6/2480 sfbufs in use (current/peak/max)
+# 0 requests for sfbufs denied
+# 0 requests for sfbufs delayed
+# 0 requests for I/O initiated by sendfile
+# 251 calls to protocol drain routines
+
+ netstat -m | head -10 | sed 's#/# #g;s/k / /;s/K / /' | awk '
+/mbufs / {mbufs=$1};
+/ clusters/ {clusters=$2};
+/sfbufs in use/ {sfbufs=$3};
+/allocated/ {allocated=$1}
+END {
+ print "mbufs,", mbufs;
+ print "clusters,", clusters;
+ print "sfbufs,", sfbufs;
+ print "allocatedToNetwork,", allocated;
+}
+'
+ sysctl vm.kvm_free | tail -1 | sed 's/:/,/'
+ sysctl vm.swap_reserved | tail -1 | sed 's/:/,/'
+ sleep 10
+done | awk -F, '
+{
+# Pairs of "name, value" are passed to this awk script
+ name=$1;
+ size=$2;
+# print "name, size :", name, size;
+ if (NF != 2)
+ print "Number of fields for ", name, "is ", NF;
+ if (size > s[name]) {
+ n[name]++;
+ if (n[name] > 50) {
+ cmd="date '+%T'";
+ cmd | getline t;
+ close(cmd);
+ printf "%s \"%s\" may be leaking, used %d\r\n", t, name, size;
+ n[name] = 0;
+ }
+ s[name] = size;
+ }
+}'
diff --git a/tools/test/stress2/tools/leaks2.sh b/tools/test/stress2/tools/leaks2.sh
new file mode 100755
index 000000000000..87361fac258f
--- /dev/null
+++ b/tools/test/stress2/tools/leaks2.sh
@@ -0,0 +1,113 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Script to catch possible leaks in vm, malloc and mbufs
+# This version looks for increse in minimum values
+
+i=0
+while true; do
+ # Check for leaks in vm.zone
+
+# ITEM SIZE LIMIT USED FREE REQUESTS
+#
+# UMA Kegs: 140, 0, 66, 6, 66
+# UMA Zones: 120, 0, 66, 24, 66
+
+ vmstat -z | sed '1,3d;s/://g' | \
+ sed 's/ */ /g;s/\([0-9]\) *\([0-9]\)/\1,\2/g;s/ \([0-9]\)/,\1/;s/^ *//' | \
+ awk -F, '
+/^..*$/{
+ gsub("^ *", "", $1);
+ size=$4;
+ printf "vmstat -z %s,%s\n", $1, size;
+}
+'
+ # Check for leaks in mbufs
+
+# $ netstat -m
+# 1233/597/1830 mbufs in use (current/cache/total)
+# 1232/196/1428/8896 mbuf clusters in use (current/cache/total/max)
+# 1232/74 mbuf+clusters out of packet secondary zone in use (current/cache)
+# 0/0/0/0 4k (page size) jumbo clusters in use (current/cache/total/max)
+# 0/0/0/0 9k jumbo clusters in use (current/cache/total/max)
+# 0/0/0/0 16k jumbo clusters in use (current/cache/total/max)
+# 2772K/541K/3313K bytes allocated to network (current/cache/total)
+# 508/7778/5734 requests for mbufs denied (mbufs/clusters/mbuf+clusters)
+# 0/0/0 requests for jumbo clusters denied (4k/9k/16k)
+# 0/6/2480 sfbufs in use (current/peak/max)
+# 0 requests for sfbufs denied
+# 0 requests for sfbufs delayed
+# 0 requests for I/O initiated by sendfile
+# 251 calls to protocol drain routines
+
+ netstat -m | head -10 | sed 's#/# #g;s/k / /;s/K / /' | awk '
+/mbufs / {mbufs=$1};
+/ clusters/ {clusters=$2};
+/sfbufs in use/ {sfbufs=$3};
+/allocated/ {allocated=$1}
+END {
+ print "mbufs,", mbufs;
+ print "clusters,", clusters;
+ print "sfbufs,", sfbufs;
+ print "allocatedToNetwork,", allocated;
+}
+'
+# sysctl vm.kvm_free | tail -1 | sed 's/:/,/' # Need used here!
+ sleep 1
+done | awk -F, '
+{
+# Pairs of "name, value" are passed to this awk script
+ name=$1
+ size=$2
+ if (NF != 2)
+ print "Number of fields for ", name, "is ", NF
+ if (size == s[name])
+ next;
+# print "name, size, old minimum :", name, size, s[name]
+
+ if ((size - 10 < s[name]) || (f[name] == 0)) {
+# print "Initial value / new minimum", n[name], s[name], size
+ n[name] = 0
+ if (f[name] == 0)
+ f[name] = 1
+ if (f[name] != 2)
+ s[name] = size
+ }
+
+ if (++n[name] > 120) {
+ cmd="date '+%T'"
+ cmd | getline t
+ close(cmd)
+# if (++w[name] > 4) {
+ printf "%s \"%s\" may be leaking, size %d\n", t, name, size
+ f[name] = 2
+# }
+ n[name] = 0
+ s[name] = size
+ }
+}'
diff --git a/tools/test/stress2/tools/maxvnodes.sh b/tools/test/stress2/tools/maxvnodes.sh
new file mode 100755
index 000000000000..a5d888499a84
--- /dev/null
+++ b/tools/test/stress2/tools/maxvnodes.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Test tool for Kirk McKusick's implementation of
+# "Resizing the vnode and name cache sizes on the fly".
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+oldmx=`sysctl -n kern.maxvnodes`
+trap "sysctl kern.maxvnodes=$oldmx" EXIT
+trap break SIGINT
+
+min=$((oldmx / 4))
+[ `uname -m` = "i386" ] && max=$((oldmx * 4)) || max=$((oldmx * 32))
+while true; do
+ sysctl kern.maxvnodes=`jot -r 1 $min $max` > /dev/null
+ sleep `jot -r 1 5 120`
+done
diff --git a/tools/test/stress2/tools/ministat.sh b/tools/test/stress2/tools/ministat.sh
new file mode 100755
index 000000000000..552828e97e59
--- /dev/null
+++ b/tools/test/stress2/tools/ministat.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Report excessive runtime using /tmp/stress2.d/elapsed
+
+[ $# -lt 2 ] && { echo "Usage: $0 <logfile> <test> [debug]"; exit 1; }
+log=$1
+[ -f $log ] || { echo "No such $log"; exit 1; }
+
+export LANG=C
+data=/tmp/ministat.log
+trap "rm -f $data" EXIT INT
+test=`basename $2`
+grep " $test" $log | awk '$NF > 2 {print $NF}' | sed '$d' > $data
+[ `wc -l < $data` -lt 5 ] && exit 0
+
+max=`ministat -n < $data | tail -1 | \
+ awk '{print int($6 + $7 * 2 + 0.5)}'` # avg + 2 * stddev
+run=`grep $test $log | awk '{print $NF}' | tail -1`
+[ $run -lt 3 ] && exit 0
+if [ $run -gt $max -o $# -eq 3 ]; then
+ echo "Note: $test's runtime was $run seconds."
+ ministat -n < $data | tail -2
+ exit 1
+fi
+exit 0
diff --git a/tools/test/stress2/tools/monitor.sh b/tools/test/stress2/tools/monitor.sh
new file mode 100755
index 000000000000..fa6734bff295
--- /dev/null
+++ b/tools/test/stress2/tools/monitor.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Script to monitor test box
+
+font="fixed"
+xterm -geo 164x21+1+1 -fn "$font" \
+ -T top \
+ -e "sh -c \"ssh -t crashbox 'top -s 1';sleep 36000\""&
+xterm -geo 164x5+1+307 -fn "$font" \
+ -T leaks.sh \
+ -e "sh -c \"ssh -n crashbox '~pho/stress2/tools/leaks.sh';sleep 36000\""&
+xterm -geo 164x14+1+405 -fn "$font" \
+ -T 'tail -F /var/log/messages' \
+ -e "sh -c \"ssh crashbox 'tail -F /var/log/messages';sleep 36000\""&
diff --git a/tools/test/stress2/tools/ps.sh b/tools/test/stress2/tools/ps.sh
new file mode 100755
index 000000000000..238192db1b0e
--- /dev/null
+++ b/tools/test/stress2/tools/ps.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
+#
+# 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 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.
+#
+
+# Show all of all.sh's children
+
+prt() {
+ parent=$1
+ [ -z "$parent" ] && return
+ awk "\$1 == $parent"'{print $0}' < $log
+
+ awk "\$2 == $parent"'{print $1}' < $log | while read pid; do
+ prt $pid
+ done
+}
+
+log=/tmp/ps.$$
+trap "rm -f $log" EXIT INT
+
+ps -ax -Oppid,wchan | grep -vw Z > $log
+
+all=`grep all\.sh $log | grep -v '0:00,00' | tail -1 | awk '{print $1}'`
+[ -z "$all" ] && exit 0
+
+prt $all
diff --git a/tools/test/stress2/tools/ptsleak.sh b/tools/test/stress2/tools/ptsleak.sh
new file mode 100755
index 000000000000..a8f303fb4249
--- /dev/null
+++ b/tools/test/stress2/tools/ptsleak.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Keep count of pts that hasn't been in use for the last 15 minutes
+
+max=0
+while true; do
+ n=`find /dev/pts -mtime +15m 2>/dev/null | wc -l`
+ if [ $n -gt $max ]; then
+ max=$n
+ echo "`date '+%T'` $n"
+ fi
+ sleep 30
+done
diff --git a/tools/test/stress2/tools/ptyleak.sh b/tools/test/stress2/tools/ptyleak.sh
new file mode 100755
index 000000000000..81b8a4f59340
--- /dev/null
+++ b/tools/test/stress2/tools/ptyleak.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Keep count of ptys that hasn't been in use for the last 15 minutes
+
+max=0
+while true; do
+ n=`find /dev -mtime +15m 2>/dev/null | \
+ egrep "/dev/pty[p-sP-S][0-9a-v]|/dev/tty[p-sP-S][0-9a-v]" | wc -l`
+ if [ $n -gt $max ]; then
+ max=$n
+ echo "`date '+%T'` $n"
+ fi
+ sleep 30
+done
diff --git a/tools/test/stress2/tools/rwatch.sh b/tools/test/stress2/tools/rwatch.sh
new file mode 100755
index 000000000000..503be783fd61
--- /dev/null
+++ b/tools/test/stress2/tools/rwatch.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2008 Peter Holm <pho@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 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.
+#
+
+# Watch disk resources for a test program
+
+unset LOAD
+unset runLOAD
+unset swapLOAD
+unset rwLOAD
+unset mkdirLOAD
+unset creatLOAD
+unset symlinkLOAD
+
+[ ! -d $RUNDIR ] && mkdir $RUNDIR
+imax=0
+kmax=0
+istart=`df -ik $RUNDIR | tail -1 | awk '{print $6}'`
+kstart=`df -ik $RUNDIR | tail -1 | awk '{print $3}'`
+
+"$@" &
+
+while ps -p $! > /dev/null; do
+ i=`df -ik $RUNDIR | tail -1 | awk '{print $6}'`
+ k=`df -ik $RUNDIR | tail -1 | awk '{print $3}'`
+ if [ $i -gt $imax -o $k -gt $kmax ]; then
+ imax=$i
+ kmax=$k
+ fi
+ sleep .2
+done
+printf "Disk usage: %d inodes and %dk\n" $((imax - istart)) $((kmax - kstart))
+wait
diff --git a/tools/test/stress2/tools/setup.sh b/tools/test/stress2/tools/setup.sh
new file mode 100755
index 000000000000..27cdc7420f0e
--- /dev/null
+++ b/tools/test/stress2/tools/setup.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+# Create the local configuration file.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+allconfig=${allconfig:-/tmp/stress2.d/`hostname`}
+[ -f $allconfig ] && grep -q "^testuser" $allconfig && exit 0
+set -e
+echo -n "Enter non-root test user name: "
+read testuser
+id $testuser > /dev/null 2>&1 ||
+ { echo "User \"$testuser\" not found."; exit 1; }
+[ $testuser ] || exit 1
+su $testuser -c 'echo Hello' 2>&1 | grep -qE '^Hello$' ||
+ { echo "User \"$testuser\" not usable."; exit 1; }
+mkdir -p `dirname $allconfig`
+(
+ echo '# Local stress2 config file'
+ echo '# Overwrite default values in stress2/default.cfg'
+ echo
+ echo '# nfs_export=${nfs_export:-t1:/tmp}'
+ echo '# BLASTHOST=${BLASTHOST:-t1}'
+ echo '# RUNDIR=${RUNDIR:-/work/stressX}'
+ echo '# diskimage=${diskimage:-/work/diskfile}'
+ echo
+ echo "testuser=$testuser"
+) >> $allconfig
+cat > `dirname $allconfig`/README <<EOF
+The /tmp/stress2.d directory contain the following files:
+
+bench.sh.log Performance info for the bench.sh test
+elapsed The runtime for each test
+excessive Test that ran for more than 30 minutes
+all.exclude Local list of tests to exclude, one test per line starting in column one
+fail List of the failed test
+last Last test run
+list Current list of tests to run
+log List of test run
+output Output from the last test run
+`hostname` Local configuration file
+vmstat Memory leak report
+EOF
+[ -f README ] && echo && cat README
+exit 0
diff --git a/tools/test/stress2/tools/shuffle b/tools/test/stress2/tools/shuffle
new file mode 100755
index 000000000000..f6467cc34e80
--- /dev/null
+++ b/tools/test/stress2/tools/shuffle
@@ -0,0 +1,47 @@
+#!/usr/bin/awk -f
+
+#
+# Copyright (c) 2013 EMC Corp.
+# 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 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.
+#
+
+# Shuffle arguments. Alternative to:
+# perl -e 'print splice(@ARGV,rand(@ARGV),1), " " while @ARGV;'
+# or
+# echo $list | tr ' ' '\n' | random -w | tr '\n' ' '
+
+BEGIN {
+ srand(); rand
+}
+{
+ n = split($0, a, " ")
+ for (i = 1; i <= n ; i++) {
+ do
+ ix = int(rand * n) + 1
+ while (! (ix in a))
+ printf "%s ", a[ix]
+ delete a[ix]
+ }
+ printf "\n"
+}
diff --git a/tools/test/stress2/tools/splitall.sh b/tools/test/stress2/tools/splitall.sh
new file mode 100755
index 000000000000..4fca2365a72a
--- /dev/null
+++ b/tools/test/stress2/tools/splitall.sh
@@ -0,0 +1,60 @@
+#/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# A way to split all.sh across different hosts.
+# For example "./splitall.sh 2 3" will run the second third of the tests.
+
+# Split the test list up in n parts and test one of them.
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+[ $# -ne 2 ] && echo "Usage $0 <part number> <parts>" && exit 1
+
+pno=$1
+parts=$2
+[ $pno -lt 1 -o $pno -gt $parts -o $parts -lt 1 ] &&
+ { echo "<part number> must be between 1 and <parts> ($parts)"; exit 1; }
+
+cd ../misc
+exclude=`cat all.exclude $alllocal 2>/dev/null | sed '/^#/d' |
+ grep "\.sh" | awk '{print $1}'`
+
+list=$(echo `ls *.sh` | sed "s/all\.sh//; s/cleanup\.sh//")
+
+lst=""
+for i in $list; do
+ echo $exclude | grep -qw $i && continue
+ lst="$lst $i"
+done
+n=`echo $lst | wc -w`
+(cd /tmp; echo $lst | tr ' ' '\n' | split -d -l $((n / parts + 1)) - str)
+part=`printf "/tmp/str%02d" $((pno - 1))`
+plist=`cat $part | tr '\n' ' '`
+rm -f /tmp/str0?
+echo "./all.sh -onc $plist"
+sleep 10
+./all.sh -onc $plist
diff --git a/tools/test/stress2/tools/swap.c b/tools/test/stress2/tools/swap.c
new file mode 100644
index 000000000000..ce272b794ea7
--- /dev/null
+++ b/tools/test/stress2/tools/swap.c
@@ -0,0 +1,225 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2020 Peter Holm <pho@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 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/types.h>
+#include <sys/resource.h>
+#include <sys/sysctl.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include <vm/vm_param.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define INCARNATIONS 32
+
+static unsigned long size, original;
+static int runtime, utime;
+
+void
+usage(void)
+{
+ fprintf(stderr, "Usage: %s [-d delay] [-p pct] [-t runtime] "
+ "[-v]\n",
+ getprogname());
+ exit(1);
+}
+
+static unsigned long
+usermem(void)
+{
+ unsigned long mem;
+ size_t nlen = sizeof(mem);
+
+ if (sysctlbyname("hw.usermem", &mem, &nlen, NULL, 0) == -1)
+ err(1, "sysctlbyname() %s:%d", __FILE__, __LINE__);
+
+#if defined(DEBUG)
+ printf("Total free user memory %lu Mb\n",
+ mem / 1024 / 1024);
+#endif
+
+ return (mem);
+}
+static int64_t
+swap(void)
+{
+ struct xswdev xsw;
+ size_t mibsize, size;
+ int mib[16], n;
+ int64_t sz;
+
+ mibsize = sizeof mib / sizeof mib[0];
+ sz = 0;
+
+ if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
+ err(1, "sysctlnametomib()");
+
+ for (n = 0; ; ++n) {
+ mib[mibsize] = n;
+ size = sizeof xsw;
+ if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1)
+ break;
+ if (xsw.xsw_version != XSWDEV_VERSION)
+ errx(1, "xswdev version mismatch");
+ sz = sz + xsw.xsw_nblks - xsw.xsw_used;
+ }
+ if (errno != ENOENT)
+ err(1, "sysctl()");
+
+#if defined(DEBUG)
+ printf("Total free swap space %jd Mb\n",
+ sz * getpagesize() / 1024 / 1024);
+#endif
+
+ return (sz * getpagesize());
+}
+
+static void
+setup(void)
+{
+ struct rlimit rlp;
+
+ size = size / INCARNATIONS;
+ original = size;
+ if (size == 0)
+ errx(1, "Argument too small");
+
+ if (getrlimit(RLIMIT_DATA, &rlp) < 0)
+ err(1,"getrlimit");
+ rlp.rlim_cur -= 1024 * 1024;
+
+ if (size > (unsigned long)rlp.rlim_cur)
+ size = rlp.rlim_cur;
+
+#if 0
+ printf("setup: pid %d. Total %luMb\n",
+ getpid(), size / 1024 / 1024 * INCARNATIONS);
+#endif
+
+ if (size == 0)
+ errx(1, "Argument too small");
+
+ return;
+}
+
+static int
+test(void)
+{
+ volatile char *c;
+ int page;
+ unsigned long i, j;
+ time_t start;
+
+ c = malloc(size);
+ while (c == NULL) {
+ size -= 1024 * 1024;
+ c = malloc(size);
+ }
+ if (size != original)
+ printf("Malloc size changed from %ld Mb to %ld Mb\n",
+ original / 1024 / 1024, size / 1024 / 1024);
+ page = getpagesize();
+ start = time(NULL);
+ while ((time(NULL) - start) < runtime) {
+ i = j = 0;
+ while (i < size) {
+ c[i] = 1;
+ i += page;
+ if ((time(NULL) - start) >= runtime)
+ break;
+ usleep(utime);
+ }
+ }
+ free((void *)c);
+
+ _exit(0);
+}
+
+int
+main(int argc, char **argv)
+{
+ int64_t s;
+ pid_t pids[INCARNATIONS];
+ unsigned long u;
+ int ch, i, pct, verbose;
+
+ s = swap();
+ u = usermem();
+
+ runtime = 120; /* 2 minutes */
+ utime = 1000; /* 0.001 sec */
+ verbose = 0;
+ if (s == 0)
+ pct = 80;
+ else
+ pct = 101;
+ while ((ch = getopt(argc, argv, "d:p:t:v")) != -1) {
+ switch(ch) {
+ case 'd': /* delay in usec */
+ if (sscanf(optarg, "%d", &utime) != 1)
+ usage();
+ break;
+ case 'p': /* % of usermem */
+ if (sscanf(optarg, "%d", &pct) != 1)
+ usage();
+ break;
+ case 't': /* runtime in sec*/
+ if (sscanf(optarg, "%d", &runtime) != 1)
+ usage();
+ break;
+ case 'v': /* verbose*/
+ verbose = 1;
+ break;
+ default:
+ usage();
+ }
+ }
+
+ if (s == 0 && pct > 80)
+ errx(1, "pct range with no swap is 0-80");
+ size = u / 100 * pct;
+ if (verbose == 1)
+ fprintf(stderr, "pct = %d, sleep = %d usec, size = %lumb\n",
+ pct, utime, size / 1024 / 1024);
+ setup();
+
+ for (i = 0; i < INCARNATIONS; i++)
+ if ((pids[i] = fork()) == 0)
+ test();
+
+ for (i = 0; i < INCARNATIONS; i++)
+ if (waitpid(pids[i], NULL, 0) != pids[i])
+ err(1, "paitpid(%d)", pids[i]);
+
+ return (0);
+}
diff --git a/tools/test/stress2/tools/uleak.sh b/tools/test/stress2/tools/uleak.sh
new file mode 100755
index 000000000000..a7f77f1cac2d
--- /dev/null
+++ b/tools/test/stress2/tools/uleak.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Report user mode leaks.
+
+export COLUMNS=130
+i=0
+start=`date '+%s'`
+/bin/echo -n " "
+ps -l | head -1
+trap break SIGINT
+
+while true; do
+ i=$((i + 1))
+ ps -axOvsz | sed 1d | awk -v loop=$i '$2 > 0 {print $1 "," $2 "," loop}'
+ sleep 10
+done | awk -F, '
+{
+ pid=$1
+ size=$2
+ loop=$3
+ if (size > s[pid]) {
+ n[pid]++
+ if (n[pid] > 6) {
+ print pid
+ fflush
+ n[pid] = 0
+ }
+ s[pid] = size
+ }
+ l[pid] = loop
+
+ # Reap dead processes
+ for (p in s) {
+ if (l[p] < loop - 1) {
+ delete s[p]
+ delete n[p]
+ delete l[p]
+ }
+ }
+}' | while read p; do
+ d=$(((`date '+%s'` - start) / 86400))
+ r=`ps -lp$p | sed 1d`
+ [ -n "$r" ] && echo "$d `date '+%T'` $r"
+done
+echo
diff --git a/tools/test/stress2/tools/vmstat.sh b/tools/test/stress2/tools/vmstat.sh
new file mode 100755
index 000000000000..89ff1e331fc5
--- /dev/null
+++ b/tools/test/stress2/tools/vmstat.sh
@@ -0,0 +1,113 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2015 EMC Corp.
+# 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 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.
+#
+
+# Memory leak detector: run vmstat -m & -z in a loop.
+
+export LANG=en_US.ISO8859-1
+while getopts dmz flag; do
+ case "$flag" in
+ d) debug="-v debug=1" ;;
+ m) optz=n ;;
+ z) optm=n ;;
+ *) echo "Usage $0 [-d] [-m] [-z]"
+ return 1 ;;
+ esac
+done
+
+pages=`sysctl -n vm.stats.vm.v_page_count`
+start=`date '+%s'`
+OIFS=$IFS
+while true; do
+ # Type InUse MemUse
+ [ -z "$optm" ] && vmstat -m | sed 1d |
+ sed 's/\(.* \)\([0-9][0-9]*\) *\(.*\)K .*/\1:\2:\3/' |
+ while IFS=: read -r p1 p2 p3; do
+ name=`echo $p1 | sed 's/^ *//;s/ *$//'`
+ memuse=$p3
+ [ "$memuse" -ne 0 ] && echo "vmstat -m $name,$memuse"
+ done
+
+ # ITEM SIZE LIMIT USED
+ [ -z "$optz" ] && vmstat -z |
+ grep -vE '(rl_entry):' |
+ sed "1,2d;/^$/d;s/: /, /" |
+ sed -E 's/[^[:print:]\r\t]/ /g' |
+ while read l; do
+ IFS=','
+ set $l
+ [ $# -lt 8 ] &&
+ { echo "# args must be >= 8, but is $# in $l" 1>&2;
+ continue; }
+ size=$2
+ used=$4
+ [ -z "$used" -o -z "$size" ] &&
+ { echo "used/size not set $l" 1>&2; continue; }
+ echo $used | egrep -q '^ *[0-9]{1,10}$' ||
+ { echo "Bad used: $used. l=$l" 1>&2; continue; }
+ tot=$((((size * used) + 1023) / 1024))
+ [ $tot -ne 0 ] &&
+ echo "vmstat -z $1,$tot"
+ done
+
+ r=`sysctl -n vm.stats.vm.v_wire_count`
+ [ -n "$r" ] &&
+ echo "vm.stats.vm.v_wire_count, \
+ $((r * 4))"
+ r=`sysctl -n vm.stats.vm.v_free_count`
+ [ -n "$r" ] &&
+ echo "pages in use, \
+ $(((pages - r) * 4))"
+ r=`sysctl -n vm.kmem_map_size`
+ [ -n "$r" ] &&
+ echo "kmem_map_size, $r"
+ sleep 10
+done | awk $debug -F, '
+{
+# Pairs of "name, value" are passed to this awk script.
+ name=$1;
+ size=$2;
+ if (size > s[name]) {
+ if (++n[name] > 60) {
+ cmd="date '+%T'";
+ cmd | getline t;
+ close(cmd);
+ printf "%s \"%s\" %'\''dK\r\n", t,
+ name, size;
+ n[name] = 0;
+ }
+ s[name] = size;
+ if (debug == 1 && n[name] > 1)
+ printf "%s, size %d, count %d\r\n",
+ name, s[name], n[name] > "/dev/stderr"
+ } else if (size < s[name] && n[name] > 0)
+ n[name]--
+}' | while read l; do
+ d=$(((`date '+%s'` - start) / 86400))
+ echo "$d $l"
+done
+# Note: the %'d is used to trigger a thousands-separator character.
diff --git a/tools/test/stress2/udp.cfg b/tools/test/stress2/udp.cfg
new file mode 100644
index 000000000000..a8f79c957c83
--- /dev/null
+++ b/tools/test/stress2/udp.cfg
@@ -0,0 +1,9 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+# Only run these three test programs for VFS tests
+
+export udpLOAD=100
+export TESTPROGS="testcases/swap/swap testcases/udp/udp"
diff --git a/tools/test/stress2/vfs.cfg b/tools/test/stress2/vfs.cfg
new file mode 100644
index 000000000000..ea73a6c3575b
--- /dev/null
+++ b/tools/test/stress2/vfs.cfg
@@ -0,0 +1,8 @@
+# Stress Test Suite Configuration
+
+# Default values
+. ./default.cfg
+
+# Only run these three test programs for VFS tests
+
+export TESTPROGS="testcases/swap/swap testcases/creat/creat testcases/mkdir/mkdir"