0N/A\ The contents of this file are subject to the terms of the
0N/A\ Common Development and Distribution License (the "License").
0N/A\ You may not use this file except in compliance with the License.
0N/A\ See the License for the specific language governing permissions
0N/A\ and limitations under the License.
0N/A\ When distributing Covered Code, include this CDDL HEADER in each
0N/A\ If applicable, add the following below this CDDL HEADER, with the
0N/A\ fields enclosed by brackets "[]" replaced with your own identifying
0N/A\ information: Portions Copyright [yyyy] [name of copyright owner]
0N/A\ Copyright 2010 Sun Microsystems, Inc. All rights reserved.
0N/A\ Use is subject to license terms.
0N/A\ Copyright 2015 Toomas Soome <tsoome@me.com>
0N/Apurpose: ZFS file system support package
0N/Acopyright: Copyright 2010 Sun Microsystems, Inc. All Rights Reserved
0N/A" /packages" get-package push-package
0N/A fs-pkg$ device-name diag-cr?
0N/A 0 instance value temp-space
0N/A \ fcode is still 32b on 64b sparc-v9, so
0N/A \ we need to override some arithmetic ops
0N/A \ stack ops and logical ops (dup, and, etc) are 64b
0N/A : xcmp ( x1 x2 -- -1|0|1 )
then then then then ( -1|0|1 )
: x< ( x1 x2 -- <? ) xcmp -1 = ;
: x> ( x1 x2 -- >? ) xcmp 1 = ;
\ : x= ( x1 x2 -- =? ) xcmp 0= ;
: x<> ( x1 x2 -- <>? ) xcmp 0<> ;
: x0= ( x -- 0=? ) xlsplit 0= swap 0= and ;
/buf-len instance buffer: numbuf
numbuf /buf-len + swap ( adr u )
d# 10 /mod swap ( adr u' rem )
rot 1- tuck c! ( u adr' )
swap dup 0= ( adr u done? )
dup numbuf - /buf-len swap - ( adr len )
/buf-len instance buffer: bootprop-buf
: bootprop$ ( -- prop$ ) bootprop-buf cscount ;
\ on lzjb, but here's the reader's digest version:
\ repeated phrases are replaced by referenced to the original
\ y a d d a _ y a d d a _ y a d d a , _ b l a h _ b l a h _ b l a h
\ y a d d a _ 6 11 , _ b l a h 5 10
\ where 6 11 means memmove(ptr, ptr - 6, 11)
\ data is separated from metadata with embedded copymap entries
\ 0x40 y a d d a _ 6 11 , 0x20 _ b l a h 5 10
\ the copymap has a set bit for copy refercences
\ and a clear bit for bytes to be copied directly
\ the reference marks are encoded with match-bits and match-min
\ byte[0] = ((mlen - MATCH_MIN) << (NBBY - MATCH_BITS) | (off >> NBBY)
: pow2 ( n -- 2**n ) 1 swap lshift ;
\ assume MATCH_BITS=6 and MATCH_MIN=3
8 mbits - constant mshift
d# 16 mbits - pow2 1- constant mmask
: decode-src ( src -- mlen off )
dup c@ swap 1+ c@ ( c[0] c[1] )
over mshift rshift mmin + ( c[0] c[1] mlen )
-rot swap bwjoin mmask and ( mlen off )
\ equivalent of memmove(dst, dst - off, len)
\ src points to a copy reference to be decoded
: mcopy ( dend dst src -- dend dst' )
decode-src ( dend dst mlen off )
2 pick swap - >r ( dent dst mlen r: cpy )
1- dup 0>= ( dend dst mlen' any? r: cpy )
2over > and ( dend dst mlen !done? r : cpy )
while ( dend dst mlen r: cpy )
swap r> dup 1+ >r c@ ( dend mlen dst c r: cpy' )
over c! 1+ swap ( dend dst' mlen r: cpy )
repeat ( dend dst' mlen r: cpy )
: lzjb ( src dst len -- )
over + swap ( src dend dst )
rot >r ( dend dst r: src )
\ setup mask so 1st while iteration fills map
0 7 pow2 2swap ( map mask dend dst r: src )
2swap 1 lshift ( dend dst map mask' r: src )
2drop ( dend dst r: src )
r> dup 1+ >r c@ 1 ( dend dst map' mask' r: src' )
then ( dend dst map mask r: src' )
\ if (map & mask) we hit a copy reference
2swap 2over and if ( map mask dend dst r: src )
r> dup 2+ >r ( map mask dend dst src r: src' )
mcopy ( map mask dend dst' r: src )
r> dup 1+ >r c@ ( map mask dend dst c r: src' )
over c! 1+ ( map mask dend dst' r: src )
repeat ( map mask dend dst r: src )
\ decode lz4 buffer header, returns src addr and len
: lz4_sbuf ( addr -- s_addr s_len )
dup C@ 8 lshift swap 1+ ( byte0 addr++ )
dup C@ ( byte0 addr byte1 )
or d# 16 lshift swap 1+ ( d addr++ )
dup C@ 8 lshift ( d addr byte2 )
swap 1+ ( d byte2 addr++ )
dup C@ swap 1+ ( d byte2 byte3 addr++ )
-rot ( d s_addr byte2 byte3 )
d# 15 constant ML_MASK \ (1<<ML_BITS)-1
4 constant RUN_BITS \ 8 - ML_BITS
d# 15 constant RUN_MASK \ (1<<RUN_BITS)-1
\ A32(d) = A32(s); d+=4; s+=4
: lz4_copystep ( dest source -- dest' source')
swap 4 + ( dest+4 source+4 )
\ do { LZ4_COPYPACKET(s, d) } while (d < e);
: lz4_copy ( e d s -- e d' s' )
\ lz4 decompress translation from C code
\ could use some factorisation
: lz4 ( src dest len -- )
swap dup >r swap \ save original dest to return stack.
lz4_sbuf ( dest len s_buf s_len )
over + ( dest len s_buf s_end )
2swap ( s_buf s_end dest len )
over + ( s_buf s_end dest dest_end )
2swap ( dest dest_end s_buf s_end )
swap dup C@ ( dest dest_end s_end s_buf token )
swap CHAR+ swap ( dest dest_end s_end s_buf++ token )
dup ML_BITS rshift ( dest dest_end s_end s_buf token length )
>r rot rot r> ( dest dest_end token s_end s_buf length )
d# 255 begin ( dest dest_end token s_end s_buf length s )
swap ( dest dest_end token s_end s_buf s length )
>r >r ( ... R: length s )
2dup > ( dest dest_end token s_end s_buf flag )
r@ d# 255 = and ( dest dest_end token s_end s_buf flag R: length s )
r> swap r> swap ( dest dest_end token s_end s_buf s length flag )
>r swap r> ( dest dest_end token s_end s_buf length s flag )
drop >r ( dest dest_end token s_end s_buf R: length )
dup c@ swap CHAR+ ( dest dest_end token s_end s s_buf++ )
swap ( dest dest_end token s_end s_buf s )
dup ( dest dest_end token s_end s_buf s s )
r> + swap ( dest dest_end token s_end s_buf length s )
drop ( dest dest_end token s_end s_buf length )
-rot ( dest dest_end token length s_end s_buf )
swap >r >r ( dest dest_end token length R: s_end s_buf )
swap >r ( dest dest_end length R: s_end s_buf token )
rot ( dest_end length dest )
2dup + ( dest_end length dest cpy )
3 pick COPYLENGTH - over < ( dest_end length dest cpy flag )
3 pick ( dest_end length dest cpy flag length )
r> ( dest_end length dest cpy flag length token )
r> ( dest_end length dest cpy flag length token s_buf R: s_end )
rot ( dest_end length dest cpy flag token s_buf length )
over + ( dest_end length dest cpy flag token s_buf length+s_buf )
r@ COPYLENGTH - > ( dest_end length dest cpy flag token s_buf flag )
swap >r ( dest_end length dest cpy flag token flag R: s_end s_buf )
swap >r ( dest_end length dest cpy flag flag R: s_end s_buf token )
or if ( dest_end length dest cpy R: s_end s_buf token )
" lz4 write beyond buffer end" die ( write beyond the dest end )
then ( dest_end length dest cpy )
2 pick ( dest_end length dest cpy length )
r> r> swap ( dest_end length dest cpy length s_buf token R: s_end )
r> ( dest_end length dest cpy length s_buf token s_end )
swap >r >r ( dest_end length dest cpy length s_buf R: token s_end )
swap over + ( dest_end length dest cpy s_buf s_buf+length )
r@ > if ( dest_end length dest cpy s_buf R: token s_end )
" lz4 read beyond source" die \ read beyond source buffer
nip ( dest_end length dest s_buf R: token s_end )
>r ( dest_end length dest R: token s_end s_buf )
over r@ ( dest_end length dest length s_buf )
-rot move ( dest_end length )
" lz4 format violation" die \ LZ4 format violation
r> drop \ drop original dest
swap ( dest_end length cpy dest R: s_end s_buf token )
r> r> swap >r ( dest_end length cpy dest s_buf R: s_end token )
lz4_copy ( dest_end length cpy dest s_buf)
-rot ( dest_end length s_buf cpy dest )
over - ( dest_end length s_buf cpy dest-cpy )
rot ( dest_end length cpy dest-cpy s_buf )
swap - ( dest_end length cpy s_buf )
dup C@ swap ( dest_end length cpy b s_buf )
dup 1+ C@ 8 lshift ( dest_end length cpy b s_buf w )
rot or ( dest_end length cpy s_buf w )
2 pick swap - ( dest_end length cpy s_buf ref )
swap 2 + ( dest_end length cpy ref s_buf+2 )
\ note: cpy is also dest, remember to save it
-rot ( dest_end length s_buf cpy ref )
dup ( dest_end length s_buf cpy ref ref )
\ now we need original dest
r> r> swap r@ ( dest_end length s_buf cpy ref ref s_end token dest )
" lz4 reference outside buffer" die \ reference outside dest buffer
then ( dest_end length s_buf op ref )
2swap ( dest_end op ref length s_buf )
swap ( dest_end op ref s_buf length R: dest s_end token )
drop r> ML_MASK and ( dest_end op ref s_buf length R: dest s_end )
dup ML_MASK = if ( dest_end op ref s_buf length R: dest s_end )
rot ( dest_end op ref length flag s_buf )
dup r@ < ( dest_end op ref length flag s_buf flag )
rot and ( dest_end op ref length s_buf flag )
dup c@ ( dest_end op ref length s_buf s )
swap 1+ ( dest_end op ref length s s_buf++ )
-rot ( dest_end op ref s_buf length s )
swap over + swap ( dest_end op ref s_buf length+s s )
then ( dest_end op ref s_buf length R: dest s_end )
2swap ( dest_end s_buf length op ref )
2dup - STEPSIZE < if ( dest_end s_buf length op ref )
\ 4 times *op++ = *ref++;
dup c@ >r ( dest_end s_buf length op ref R: C )
CHAR+ swap ( dest_end s_buf length ref++ op )
dup r> swap c! CHAR+ swap ( dest_end s_buf length op ref )
dup c@ >r ( dest_end s_buf length op ref R: C )
CHAR+ swap ( dest_end s_buf length ref++ op )
dup r> swap c! CHAR+ swap ( dest_end s_buf length op ref )
dup c@ >r ( dest_end s_buf length op ref R: C )
CHAR+ swap ( dest_end s_buf length ref++ op )
dup r> swap c! CHAR+ swap ( dest_end s_buf length op ref )
dup c@ >r ( dest_end s_buf length op ref R: C )
CHAR+ swap ( dest_end s_buf length ref++ op )
dup r> swap c! CHAR+ swap ( dest_end s_buf length op ref )
2dup - ( dest_end s_buf length op ref op-ref )
2dup swap 4 move ( dest_end s_buf length op ref )
swap ( dest_end s_buf length op ref )
lz4_copystep ( dest_end s_buf length op ref )
-rot ( dest_end s_buf ref length op )
swap over ( dest_end s_buf ref op length op )
+ STEPSIZE 4 - - ( dest_end s_buf ref op cpy R: dest s_end )
\ if cpy > oend - COPYLENGTH
4 pick COPYLENGTH - ( dest_end s_buf ref op cpy oend-COPYLENGTH )
2dup > if ( dest_end s_buf ref op cpy oend-COPYLENGTH )
swap ( dest_end s_buf ref op oend-COPYLENGTH cpy )
" lz4 write outside buffer" die \ write outside of dest buffer
then ( dest_end s_buf ref op oend-COPYLENGTH cpy )
>r ( dest_end s_buf ref op oend-COPYLENGTH R: dest s_end cpy )
-rot swap ( dest_end s_buf oend-COPYLENGTH op ref )
lz4_copy ( dest_end s_buf oend-COPYLENGTH op ref )
rot drop swap r> ( dest_end s_buf ref op cpy )
>r ( dest_end s_buf ref op R: cpy )
over ( dest_end s_buf ref op ref )
c@ ( dest_end s_buf ref op C )
over c! ( dest_end s_buf ref op )
>r 1+ r> 1+ r> ( dest_end s_buf ref++ op++ cpy )
nip ( dest_end s_buf ref op )
\ op == dest_end we are done, cleanup
( dest_end s_buf ref op R: dest s_end )
nip ( dest_end s_buf op )
drop ( dest_end s_buf ref op cpy R: dest s_end)
-rot ( dest_end s_buf cpy ref op )
swap ( dest_end s_buf cpy op ref )
2drop ( dest_end s_buf op )
-rot r> ( op dest_end s_buf s_end R: dest )
\ ZFS block (SPA) routines
h# 2.0000 constant /max-bsize
d# 512 constant /disk-block
alias /gang-block /disk-block
\ the ending checksum is larger than 1 byte, but that
\ doesn't affect the math here
: blk_offset ( bp -- n ) h# 8 + x@ -1 h#
7fff.ffff lxjoin and ;
: blk_gang ( bp -- n ) h# 8 + x@ xlsplit nip d# 31 rshift ;
: blk_etype ( bp -- n ) h# 32 + c@ ;
: blk_comp ( bp -- n ) h# 33 + c@ h# 7f and ;
: blk_embedded? ( bp -- flag ) h# 33 + c@ h# 80 and h# 80 = ;
: blk_psize ( bp -- n ) h# 34 + w@ ;
: blk_lsize ( bp -- n ) h# 36 + w@ ;
: blk_birth ( bp -- n ) h# 50 + x@ ;
: blke_psize ( bp -- n ) h# 34 + c@ 1 rshift h# 7f and 1+ ;
: blke_lsize ( bp -- n ) h# 34 + l@ h#
1ff.ffff and 1+ ;
0 instance value blk-space
0 instance value gang-space
: foff>doff ( fs-off -- disk-off ) /disk-block * h# 40.0000 + ;
: fsz>dsz ( fs-size -- disk-size ) 1+ /disk-block * ;
: bp-dsize ( bp -- dsize )
: bp-lsize ( bp -- lsize )
: (read-dva) ( adr len dva -- )
blk_offset foff>doff dev-ih read-disk
: gang-read ( adr len bp gb-adr -- ) tokenizer[ reveal ]tokenizer
tuck /gang-block rot (read-dva) ( adr len gb-adr )
\ loop through indirected bp's
dup /blkp #
blks/gang * ( adr len gb-adr bp-list bp-list-len )
bounds do ( adr len gb-adr )
\ calc subordinate read len
over i bp-dsize min ( adr len gb-adr sub-len )
2swap swap ( gb-adr sub-len len adr )
\ nested gang block - recurse with new gang block area
2swap ( len adr gb-adr sub-len )
3dup swap /gang-block + ( len adr gb-adr sub-len adr sub-len gb-adr' )
i swap gang-read ( len adr gb-adr sub-len )
2swap ( gb-adr sub-len len adr )
3dup nip swap ( gb-adr sub-len len adr adr sub-len )
i (read-dva) ( gb-adr sub-len len adr )
then ( gb-adr sub-len len adr )
\ adjust adr,len and check if done
-rot over - ( gb-adr adr sub-len len' )
-rot + swap ( gb-adr adr' len' )
: read-dva ( adr len dva -- )
: read-embedded ( adr len bp -- )
\ loop over buf len, w in comment is octet count
\ note, we dont increment bp, but use index value of w
\ so we can skip the non-payload octets
swap 0 0 ( adr bp len 0 0 )
I 8 mod 0= if ( adr bp w x )
1+ dup 6 = if 1+ else \ skip 6th word
dup h# a = if 1+ then \ skip 10th word
swap dup ( w bp adr x x )
I 4 mod 8 * rshift h# ff and ( w bp adr x c )
swap over ( w bp x adr c adr )
\ now we need to fix the stack for next pass
\ need to get ( adr bp w x )
swap 2swap ( adr x w bp )
\ block read that check for holes, gangs, compression, etc
: read-bp ( adr len bp -- )
dup x@ x0= ( addr len bp flag0 )
swap dup 8 + x@ x0= ( addr len flag0 bp flag1 )
rot ( addr len bp flag1 flag0 )
dup blk_comp no-comp# = if
\ read into blk-space. read is either from embedded area or disk
dup blk-space over bp-dsize ( adr len bp bp blk-adr rd-len )
rot read-embedded ( adr len bp )
dup blk-space over bp-dsize ( adr len bp bp blk-adr rd-len )
rot read-dva ( adr len bp )
\ set up the stack for decompress
blk_comp >r ( adr len R: alg )
blk-space -rot r> ( blk-adr adr len alg )
def-comp# of lz4 endof \ isn't this writer only?
" : unknown compression algorithm, only lzjb and lz4 are supported"
h# 4000 constant nvpairs-off
\ array of xdr packed nvpairs
: >nvsize ( nv -- size ) l@ ;
: >nvname ( nv -- name$ )
\ convert nvdata to 64b int or string
: nvdata>x ( nvdata -- x )
: nv-lookup ( nv name$ -- nvdata false | true )
rot /nvhead + ( name$ nvpair )
dup >r >nvname ( name$ nvname$ r: nvpair )
2over $= if ( name$ r: nvpair )
2drop r> >nvdata ( nvdata )
false exit ( nvdata found )
r> dup >nvsize + ( name$ nvpair' )
temp-space /nvpairs nvpairs-off ( adr len off )
temp-space " txg" nv-lookup if
temp-space " name" nv-lookup if
bootprop-buf swap move ( )
\ ZFS ueber-block routines
d# 1024 constant /uber-block
#
ub/label /uber-block * constant /ub-ring
h# 2.0000 constant ubring-off
: ub_magic ( ub -- n ) x@ ;
: ub_txg ( ub -- n ) h# 10 + x@ ;
: ub_timestamp ( ub -- n ) h# 20 + x@ ;
: ub_rootbp ( ub -- p ) h# 28 + ;
0 instance value uber-block
: ub-cmp ( ub1 ub2 -- best-ub )
\ ub1 wins if ub2 isn't valid
dup ub_magic h# 00bab10c x<> if
\ if ub1 is 0, ub2 wins by default
over 0= if nip exit then ( ub2 )
\ 2 valid ubs, compare transaction groups
over ub_txg over ub_txg ( ub1 ub2 txg1 txg2 )
then ( ub1 ub2 txg1 txg2 )
x> if drop exit then ( ub1 )
\ same txg, check timestamps
over ub_timestamp over ub_timestamp x> if
\ find best uber-block in ring, and copy it to uber-block
temp-space /ub-ring ubring-off ( adr len off )
0 temp-space /ub-ring ( null-ub adr len )
\ make sure we found a valid ub
dup 0= if " no ub found" die then
uber-block /uber-block move ( )
\ ZFS dnode (DMU) routines
: dn_indblkshift ( dn -- n ) h# 1 + c@ ;
: dn_nlevels ( dn -- n ) h# 2 + c@ ;
: dn_bonustype ( dn -- n ) h# 4 + c@ ;
: dn_datablkszsec ( dn -- n ) h# 8 + w@ ;
: dn_bonuslen ( dn -- n ) h# a + w@ ;
: dn_blkptr ( dn -- p ) h# 40 + ;
: dn_bonus ( dn -- p ) h# c0 + ;
: dn_spill ( dn -- p ) h# 180 + ;
\ ind-cache is a 1 block indirect block cache from dnode ic-dn
\ ic-bp and ic-bplim point into the ic-dn's block ptr array,
\ either in dn_blkptr or in ind-cache ic-bp is the ic-blk#'th
\ block ptr, and ic-bplim is limit of the current bp array
\ the assumption is that reads will be sequential, so we can
0 instance value ind-cache
0 instance value ic-bplim
: dn-bsize ( dn -- bsize ) dn_datablkszsec /disk-block * ;
: dn-indsize ( dn -- indsize ) dn_indblkshift pow2 ;
: dn-indmask ( dn -- mask ) dn-indsize 1- ;
\ recursively climb the block tree from the leaf to the root
: blk@lvl>bp ( dn blk# lvl -- bp ) tokenizer[ reveal ]tokenizer
>r /blkp * over dn_nlevels ( dn bp-off #lvls r: lvl )
\ at top, just add dn_blkptr
r@ = if ( dn bp-off r: lvl )
swap dn_blkptr + ( bp r: lvl )
then ( dn bp-off r: lvl )
\ shift bp-off down and find parent indir blk
2dup over dn_indblkshift rshift ( dn bp-off dn blk# r: lvl )
r> 1+ blk@lvl>bp ( dn bp-off bp )
\ read parent indir blk and index
rot tuck dn-indsize ( bp-off dn bp len )
ind-cache swap rot read-bp ( bp-off dn )
dn-indmask and ( bp-off' )
\ return end of current bp array
: bplim ( dn bp -- bp-lim )
1+ swap dn-indsize ( bp+1 indsz )
\ return the lblk#'th block ptr from dnode
: lblk#>bp ( dn blk# -- bp )
ic-blk# <> swap ic-dn <> or ( dn blk# cache-miss? )
ic-bp ic-bplim = ( dn blk# cache-miss? cache-empty? )
2dup 1 blk@lvl>bp ( dn blk# bp )
dup to ic-bp ( dn blk# bp )
swap to ic-blk# ( dn bp )
2dup bplim to ic-bplim ( dn bp )
ic-bp dup /blkp + to ic-bp ( bp )
\ ZFS attribute (ZAP) routines
h# ffff constant chain-end#
/lf-buf instance buffer: leaf-value
/lf-buf instance buffer: leaf-name
: +le ( len off -- n ) + w@ ;
: le_next ( le -- n ) h# 2 +le ;
: le_name_chunk ( le -- n ) h# 4 +le ;
: le_name_length ( le -- n ) h# 6 +le ;
: le_value_chunk ( le -- n ) h# 8 +le ;
: le_value_length ( le -- n ) h# a +le ;
: la_array ( la -- adr ) 1+ ;
: la_next ( la -- n ) h# 16 + w@ ;
0 instance value zap-space
: >leaf-hash ( dn lh -- hash-adr /hash )
/lf-chunk 2* + ( dn hash-adr )
\ size = (bsize / 32) * 2
swap dn-bsize 4 rshift ( hash-adr /hash )
: >leaf-chunks ( lf -- ch0 ) >leaf-hash + ;
\ convert chunk # to leaf chunk
: ch#>lc ( dn ch# -- lc )
/lf-chunk * ( dn lc-off )
swap zap-space >leaf-chunks ( lc-off ch0 )
\ assemble chunk chain into single buffer
: get-chunk-data ( dn ch# adr -- )
dup >r /lf-buf erase ( dn ch# r: adr )
2dup ch#>lc nip ( dn la r: adr )
dup la_array ( dn la la-arr r: adr )
r@ /lf-arr move ( dn la r: adr )
r> /lf-arr + >r ( dn la r: adr' )
la_next dup chain-end# = ( dn la-ch# end? r: adr )
: entry-name$ ( dn le -- name$ )
2dup le_name_chunk ( dn le dn la-ch# )
leaf-name get-chunk-data ( dn le )
nip le_name_length 1- ( len )
\ return entry value as int
: entry-int-val ( dn le -- n )
le_value_chunk ( dn la-ch# )
leaf-value get-chunk-data ( )
\ get leaf entry's value as string
: entry-val$ ( dn le -- val$ )
2dup le_value_chunk ( dn le dn la-ch# )
leaf-value get-chunk-data ( dn le )
nip le_value_length ( len )
leaf-value swap ( name$ )
: entry-apply ( xt dn le -- xt dn false | ??? true )
over >r ( xt dn le r: dn )
rot dup >r execute if ( ??? r: xt dn )
r> r> 2drop true ( ??? true )
r> r> false ( xt dn false )
\ apply xt to every entry in chain
: chain-apply ( xt dn ch# -- xt dn false | ??? true )
2dup ch#>lc nip ( xt dn le )
dup >r entry-apply if ( ??? r: le )
r> drop true exit ( ??? found )
dup chain-end# = ( xt dn ch# end? )
\ apply xt to every entry in leaf
: leaf-apply ( xt dn blk# -- xt dn false | ??? true )
\ read zap leaf into zap-space
2dup lblk#>bp ( xt dn blk# bp )
nip over dn-bsize zap-space ( xt dn bp len adr )
swap rot read-bp ( xt dn )
\ call chunk-look for every valid chunk list
dup zap-space >leaf-hash ( xt dn hash-adr /hash )
i w@ dup chain-end# <> if ( xt dn ch# )
unloop true exit ( ??? found )
false ( xt dn not-found )
\ apply xt to every entry in fzap
: fzap-apply ( xt dn fz -- ??? not-found? )
\ blk# 1 is always the 1st leaf
>r 1 leaf-apply if ( ??? r: fz )
r> drop true exit ( ??? found )
\ call leaf-apply on every non-duplicate hash entry
\ embedded hash is in 2nd half of fzap block
over dn-bsize tuck + ( xt dn bsize hash-eadr )
swap 2dup 2/ - ( xt dn hash-eadr bsize hash-adr )
i x@ dup 1 <> if ( xt dn blk# )
unloop true exit ( ??? found )
2drop false ( not-found )
: mze_value ( uz -- n ) x@ ;
: mze_name ( uz -- p ) h# e + ;
: uzap-name$ ( uz -- name$ ) mze_name cscount ;
\ apply xt to each entry in micro-zap
: uzap-apply ( xt uz len -- ??? not-found? )
i swap dup >r ( uz xt r: xt )
unloop true exit ( ??? found )
: fz-nmlook ( prop$ dn le -- prop$ false | prop$ dn le true )
2dup entry-name$ ( prop$ dn le name$ )
2rot 2swap ( dn le prop$ name$ )
2over $= if ( dn le prop$ )
2swap true ( prop$ dn le true )
2swap 2drop false ( prop$ false )
then ( prop$ false | prop$ dn le true )
: uz-nmlook ( prop$ uz -- prop$ false | prop$ uz true )
dup >r uzap-name$ ( prop$ name$ r: uz )
2over $= if ( prop$ r: uz )
r> true ( prop$ uz true )
r> drop false ( prop$ false )
then ( prop$ false | prop$ uz true )
: zap-type ( zp -- n ) h# 7 + c@ ;
: >uzap-ent ( adr -- ent ) h# 40 + ;
\ read zap block into temp-space
temp-space swap ( bp adr len )
: zap-lookup ( dn prop$ -- [ n ] not-found? )
rot dup get-zap ( prop$ dn zp )
>uzap-ent swap dn-bsize ( prop$ uz len )
['] uz-nmlook -rot ( prop$ xt uz len )
uzap-apply if ( prop$ uz )
mze_value -rot 2drop ( n )
then ( [ n ] not-found? )
['] fz-nmlook -rot ( prop$ xt dn fz )
fzap-apply if ( prop$ dn le )
entry-int-val ( prop$ n )
-rot 2drop false ( n found )
then ( [ n ] not-found? )
3drop 2drop true ( !found )
endcase ( [ n ] not-found? )
: zap-lookup-str ( dn prop$ -- [ val$ ] not-found? )
rot dup get-zap ( prop$ dn zp )
dup zap-type fzap# <> if ( prop$ dn zp )
2drop 2drop true exit ( !found )
['] fz-nmlook -rot ( prop$ xt dn fz )
fzap-apply if ( prop$ dn le )
entry-val$ 2swap 2drop false ( val$ found )
then ( [ val$ ] not-found? )
: fz-print ( dn le -- false )
entry-name$ type cr false
: uz-print ( uz -- false )
>uzap-ent swap dn-bsize ( uz len )
['] uz-print -rot ( xt uz len )
['] fz-print -rot ( xt dn fz )
\ ZFS object set (DSL) routines
: dd_head_dataset_obj ( dd -- n ) h# 8 + x@ ;
: dd_child_dir_zapobj ( dd -- n ) h# 20 + x@ ;
: ds_snapnames_zapobj ( ds -- n ) h# 20 + x@ ;
: ds_bp ( ds -- p ) h# 80 + ;
0 instance value root-dsl
\ dn-cache contains dc-dn's contents at dc-blk#
\ dc-dn will be either mos-dn or fs-dn
0 instance value dn-cache
: #
dn/blk ( dn -- n ) dn-bsize /dnode / ;
\ read block into dn-cache
: get-dnblk ( dn blk# -- )
lblk#>bp dn-cache swap ( adr bp )
dup bp-lsize swap read-bp ( )
\ read obj# from objset dir dn into dnode
: get-dnode ( dn obj# -- )
2dup swap #
dn/blk /mod ( dn obj# off# blk# )
swap >r nip ( dn blk# r: off# )
2dup dc-blk# <> ( dn blk# dn !blk-hit? r: off# )
swap dc-dn <> or if ( dn blk# r: off# )
\ cache miss, fill from dir
2drop r> /dnode * ( off )
\ read meta object set from uber-block
mos-dn uber-block ub_rootbp ( adr bp )
dup bp-lsize swap read-bp
: get-mos-dnode ( obj# -- )
dnode obj-dir /dnode move
obj-dir " root_dataset" zap-lookup if
dnode root-dsl /dnode move
\ find snapshot of given dataset
: snap-look ( snap$ ds-obj# -- [ss-obj# ] not-found? )
get-mos-dnode dnode >dsl-ds ( snap$ ds )
ds_snapnames_zapobj get-mos-dnode ( snap$ )
dnode -rot zap-lookup ( [ss-obj# ] not-found? )
: dir>ds ( dn -- obj# ) >dsl-dir dd_head_dataset_obj ;
\ look thru the dsl hierarchy for path
\ this looks almost exactly like a FS directory lookup
: dsl-lookup ( path$ -- [ ds-obj# ] not-found? )
root-dsl >r ( path$ r: root-dn )
ascii / left-parse-string ( path$ file$ r: dn )
\ get child dir zap dnode
r> >dsl-dir dd_child_dir_zapobj ( path$ file$ obj# )
get-mos-dnode ( path$ file$ )
\ check for snapshot names
ascii @ left-parse-string ( path$ snap$ file$ )
dnode -rot zap-lookup if ( path$ snap$ )
2drop 2drop true exit ( not-found )
then ( path$ snap$ obj# )
get-mos-dnode ( path$ snap$ )
\ lookup any snapshot name
\ must be last path component
2drop true exit ( not-found )
dnode dir>ds snap-look if ( )
false exit ( obj# found )
else 2drop then ( path$ )
repeat ( path$ file$ r: dn)
\ found it, return dataset obj#
\ get objset from dataset
: get-objset ( adr dn -- )
>dsl-ds ds_bp dup bp-lsize swap read-bp
\ ZFS file-system (ZPL) routines
0 instance value bootfs-obj#
0 instance value root-obj#
0 instance value current-obj#
0 instance value search-obj#
instance defer fsize ( dn -- size )
instance defer mode ( dn -- mode )
instance defer parent ( dn -- obj# )
instance defer readlink ( dst dn -- )
\ routines when bonus pool contains a znode
: zp_mode ( zn -- n ) h# 48 + x@ ;
: zp_size ( zn -- n ) h# 50 + x@ ;
: zp_parent ( zn -- n ) h# 58 + x@ ;
: zn-fsize ( dn -- n ) >znode zp_size ;
: zn-mode ( dn -- n ) >znode zp_mode ;
: zn-parent ( dn -- n ) >znode zp_parent ;
\ copy symlink target to dst
: zn-readlink ( dst dn -- )
dup zn-fsize tuck /zn-slink > if ( dst size dn )
temp-space over dn-bsize ( dst size dn t-adr bsize )
rot 0 lblk#>bp read-bp ( dst size )
temp-space ( dst size src )
>znode /znode + ( dst size src )
\ routines when bonus pool contains sa's
\ SA header size when link is in dn_bonus
d# 16 constant /sahdr-link
: sa_props ( sa -- n ) h# 4 + w@ ;
: sa-hdrsz ( sa -- sz ) sa_props h# 7 >> ;
: >sadata ( dn -- adr ) >sa dup sa-hdrsz + ;
: sa-mode ( dn -- n ) >sadata x@ ;
: sa-fsize ( dn -- n ) >sadata h# 8 + x@ ;
: sa-parent ( dn -- n ) >sadata h# 28 + x@ ;
\ copy symlink target to dst
: sa-readlink ( dst dn -- )
dup >sa sa-hdrsz /sahdr-link <> if
\ contents in 1st attr of dn_spill
temp-space over dn_spill ( dst dn t-adr bp )
dup bp-lsize swap read-bp ( dst dn )
temp-space dup sa-hdrsz + ( dst size src )
dup dn_bonus over dn_bonuslen + ( dst dn ebonus )
swap sa-fsize tuck - ( dst size src )
\ setup attr routines for dn
['] sa-readlink to readlink
['] zn-readlink to readlink
: ftype ( dn -- type ) mode h# f000 and ;
: dir? ( dn -- flag ) ftype h# 4000 = ;
: symlink? ( dn -- flag ) ftype h# a000 = ;
\ read obj# from fs objset
: get-fs-dnode ( obj# -- )
\ get root-obj# from dataset
: get-rootobj# ( ds-obj# -- fsroot-obj# )
\ get root obj# from master node
master-node# get-fs-dnode
dnode " ROOT" zap-lookup if
obj-dir " pool_props" zap-lookup if
dnode " bootfs" zap-lookup if
get-rootobj# ( fsroot-obj# )
: fs>rootobj# ( fs$ -- root-obj# not-found? )
ascii / left-parse-string 2drop
get-rootobj# ( fsroot-obj# )
false ( fsroot-obj# found )
\ lookup file is current directory
: dirlook ( file$ dn -- not-found? )
-rot 2dup " ." $= if ( dn file$ )
3drop false exit ( found )
current-obj# to search-obj#
/buf-len instance buffer: fpath-buf
/buf-len instance buffer: tpath-buf
: tpath-buf$ ( -- path$ ) tpath-buf cscount ;
: fpath-buf$ ( -- path$ ) fpath-buf cscount ;
\ modify tail to account for symlink
: follow-symlink ( tail$ -- tail$' )
" /" tpath-buf$ $append ( tail$ )
tpath-buf$ fpath-buf swap move
\ get directory that starts changed path
over c@ ascii / = if ( path$ )
str++ root-obj# ( path$' obj# )
search-obj# ( path$ obj# )
: lookup ( path$ -- not-found? )
\ get directory that starts path
str++ root-obj# ( path$' obj# )
current-obj# ( path$ obj# )
\ lookup each path component
ascii / left-parse-string ( path$ file$ )
2drop true exit ( not-found )
dnode dirlook if ( path$ )
2drop true exit ( not-found )
follow-symlink ( path$' )
2drop 2drop false ( found )
\ ZFS volume (ZVOL) routines
: get-zvol ( zvol$ -- not-found? )
drop true exit ( failed )
zv-dn zvol-data# get-dnode
zv-dn zvol-prop# get-dnode
dnode " size" zap-lookup if
\ ZFS installation routines
#opens /file-record * constant /file-records
/file-records instance buffer: file-records
-1 instance value current-fd
: fd>record ( fd -- rec ) /file-record * file-records + ;
: file-offset@ ( -- off ) current-fd fd>record >offset x@ ;
: file-offset! ( off -- ) current-fd fd>record >offset x! ;
: file-dnode ( -- dn ) current-fd fd>record >dnode ;
: file-size ( -- size ) current-fd fd>record >fsize x@ ;
: file-bsize ( -- bsize ) file-dnode dn-bsize ;
: get-slot ( -- fd false | true )
i fd>record >busy x@ 0= if
0 swap fd>record >busy x!
\ init fd to offset 0 and copy dnode
: init-fd ( fsize fd -- )
dup >dnode dnode swap /dnode move
dup >fsize rot swap x! ( rec )
: set-fd ( fd -- error? )
dup fd>record >busy x@ 0= if ( fd )
drop true exit ( failed )
to current-fd false ( succeeded )
file-offset@ over / ( adr len blk# )
file-dnode swap lblk#>bp ( adr len bp )
\ advance file io stack by n
: fio+ ( # adr len n -- #+n adr+n len-n )
dup file-offset@ + file-offset!
dup >r - -rot ( len' # adr r: n )
r@ + -rot ( adr' len' # r: n )
r> + -rot ( #' adr' len' )
/disk-block 6 * + ( size )
\ ugh - sg proms can't free 512k allocations
\ that aren't a multiple of 512k in size
h# 8.0000 roundup ( size' )
: allocate-buffers ( -- )
alloc-size h# a0.0000 vmem-alloc dup 0= if
dup to temp-space /max-bsize + ( adr )
dup to dn-cache /max-bsize + ( adr )
dup to blk-space /max-bsize + ( adr )
dup to ind-cache /max-bsize + ( adr )
dup to zap-space /max-bsize + ( adr )
dup to uber-block /uber-block + ( adr )
dup to mos-dn /dnode + ( adr )
dup to obj-dir /dnode + ( adr )
dup to root-dsl /dnode + ( adr )
dup to fs-dn /dnode + ( adr )
dup to zv-dn /dnode + ( adr )
dup to dnode /dnode + ( adr )
file-records /file-records erase
bootprop-buf /buf-len erase
temp-space alloc-size mem-free
my-args dev-open dup 0= if
: open-fs ( fs$ -- okay? )
to root-obj# true ( succeeded )
: open-file ( path$ -- fd true | false )
\ open default fs if no open-fs
prop>rootobj# to root-obj#
2drop false exit ( failed )
drop false exit ( failed )
: open-volume ( vol$ -- okay? )
2drop false exit ( failed )
drop false exit ( failed )
zvol-size over ( fd size fd )
: size-file ( fd -- size )
set-fd if 0 else file-size then
: seek-file ( off fd -- off true | false )
drop false exit ( failed )
dup file-size x> if ( off )
drop false exit ( failed )
dup file-offset! true ( off succeeded )
: read-file ( adr len fd -- #read )
\ adjust len if reading past eof
dup file-offset@ + file-size x> if
dup file-offset@ + file-size - -
file-offset@ file-bsize mod ?dup if ( #read adr len off )
2dup file-bsize swap - min ( #read adr len off cpy-len )
2over drop -rot ( #read adr len adr off cpy-len )
>r temp-space + swap ( #read adr len cpy-src adr r: cpy-len )
r@ move r> fio+ ( #read' adr' len' )
dup file-bsize / 0 ?do ( #read adr len )
file-bsize fio+ ( #read' adr' len' )
2dup temp-space -rot move ( #read adr len )
dup fio+ ( #read' adr' 0 )
: cinfo-file ( fd -- bsize fsize comp? )
file-bsize file-size ( bsize fsize )
\ zfs does internal compression
\ read ramdisk fcode at rd-offset
rd-offset dev-ih read-disk
bootfs-obj# (xu.) bootprop$ $append
bootprop$ encode-string " zfs-bootfs" ( propval propname )
current-obj# -rot ( obj# dir$ )
dnode dir? 0= if ( obj# )
current-obj# get-fs-dnode