Skip to content

Commit 6066a83

Browse files
committed
Add blkptr command to sdb.
1 parent 3e6c69a commit 6066a83

8 files changed

+378
-0
lines changed

sdb/commands/zfs/blkptr.py

Lines changed: 363 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,363 @@
1+
#
2+
# Copyright 2019 Delphix
3+
# Copyright 2021 Datto, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
# pylint: disable=missing-docstring
19+
20+
from typing import Iterable
21+
22+
import drgn
23+
import sdb
24+
from sdb.commands.zfs.internal import BF64_GET, BF64_GET_SB
25+
26+
27+
class BpInfo:
28+
"""
29+
Store attributes of the block pointer and
30+
get formatted representations of them.
31+
"""
32+
33+
# pylint: disable=too-many-instance-attributes
34+
35+
ndvas = 0
36+
level = 0
37+
ot_type = 0
38+
compress = 0
39+
birth = 0
40+
cksum = 0
41+
byte_order = 0
42+
gang = 0
43+
dedup = 0
44+
copies = ""
45+
pbirth = 0
46+
lsize = ""
47+
psize = ""
48+
crypt = ""
49+
c0 = ""
50+
c1 = ""
51+
c2 = ""
52+
c3 = ""
53+
54+
def set_ndvas(self, nd: int) -> None:
55+
self.ndvas = nd
56+
57+
def set_initial_props(self, bp: drgn.Object) -> None:
58+
self.level = BF64_GET(bp.blk_prop, 56, 5)
59+
self.ot_type = int(BF64_GET(bp.blk_prop, 48, 8))
60+
self.compress = BF64_GET(bp.blk_prop, 32, 7)
61+
self.lsize = format(Blkptr.bp_get_lsize(bp), 'x')
62+
self.psize = format(Blkptr.bp_get_psize(bp), 'x')
63+
self.birth = int(bp.blk_birth)
64+
self.cksum = BF64_GET(bp.blk_prop, 40, 8)
65+
self.byte_order = BF64_GET(bp.blk_prop, 63, 1)
66+
self.crypt = Blkptr.bp_get_crypt(bp, self.ot_type, self.level)
67+
self.gang = (0 if Blkptr.bp_is_embedded(bp) else BF64_GET(
68+
bp.blk_dva[0].dva_word[1], 63, 1))
69+
self.dedup = BF64_GET(bp.blk_prop, 62, 1)
70+
self.copies = Blkptr.copyname[self.ndvas]
71+
self.pbirth = Blkptr.bp_physical_birth(bp)
72+
self.c0 = '%x' % bp.blk_cksum.zc_word[0]
73+
self.c1 = '%x' % bp.blk_cksum.zc_word[1]
74+
self.c2 = '%x' % bp.blk_cksum.zc_word[2]
75+
self.c3 = '%x' % bp.blk_cksum.zc_word[3]
76+
77+
def get_compstr(self) -> str:
78+
comps = sdb.get_object("zio_compress_table")
79+
return str(comps[self.compress].ci_name.string_().decode("utf-8"))
80+
81+
def get_ot_name(self) -> str:
82+
if self.ot_type & Blkptr.DMU_OT_NEWTYPE:
83+
if self.ot_type & Blkptr.DMU_OT_BYTESWAP_MASK > 9:
84+
print(str(self.ot_type & Blkptr.DMU_OT_BYTESWAP_MASK))
85+
return str(self.ot_type & Blkptr.DMU_OT_BYTESWAP_MASK)
86+
bswaps = sdb.get_object("dmu_ot_byteswap")
87+
swap_entry = bswaps[self.ot_type & Blkptr.DMU_OT_BYTESWAP_MASK]
88+
byte_swap = swap_entry.ob_name.string_().decode("utf-8")
89+
meta = "metadata" if self.ot_type & Blkptr.DMU_OT_METADATA else "data"
90+
return "bswap %s %s" % (meta, byte_swap)
91+
return str(
92+
sdb.get_object("dmu_ot")[self.ot_type].ot_name.string_().decode(
93+
"utf-8"))
94+
95+
def get_fill(self, bp: drgn.Object) -> int:
96+
if Blkptr.bp_is_encrypted(bp, self.ot_type, self.level):
97+
return BF64_GET(bp.blk_fill, 0, 32)
98+
if Blkptr.bp_is_embedded(bp):
99+
return 1
100+
return int(bp.blk_fill)
101+
102+
def get_cksum_string(self) -> str:
103+
cksums = sdb.get_object("zio_checksum_table")
104+
return str(cksums[self.cksum].ci_name.string_().decode("utf-8"))
105+
106+
def get_endian(self) -> str:
107+
return "BE" if self.byte_order == 0 else "LE"
108+
109+
def get_gang(self) -> str:
110+
return "gang" if self.gang else "contiguous"
111+
112+
def get_dedup(self) -> str:
113+
return "dedup" if self.dedup else "unique"
114+
115+
116+
class Blkptr(sdb.PrettyPrinter):
117+
"""
118+
DESCRIPTION
119+
120+
Pretty-print zfs block pointers.
121+
122+
EXAMPLES
123+
124+
sdb> dbuf | head 1 | member db_blkptr | blkptr
125+
DVA[0]=<0:2cefd5e00:20000> [L0 ZFS plain file] fletcher4 uncompressed unencrypted LE
126+
contiguous unique single 20000L/20000P birth=1624L/1624P fill=1 cksum=3feb86d3fa14:
127+
ff98411222361a1:7cd8eb3816d141e1:2d65ae38a67197c7
128+
129+
sdb> echo 0xffffa0889343c680 | blkptr
130+
DVA[0]=<0:41e90000:30000> [L0 ZFS plain file] fletcher4 uncompressed unencrypted LE
131+
contiguous unique single 20000L/20000P birth=10L/10P fill=1 cksum=3ffba121eb4d:
132+
ffd4345f8d679e2:efa124922f72ec66:34642a9a05fbafef
133+
"""
134+
135+
BP_EMBEDDED_TYPE_DATA = 0
136+
BP_EMBEDDED_TYPE_REDACTED = 2
137+
138+
SPA_MINBLOCKSHIFT = 9
139+
SPA_MAXBLOCKSHIFT = 24
140+
SPA_MAXBLOCKSIZE = 1 << SPA_MAXBLOCKSHIFT
141+
SPA_ASIZEBITS = 24
142+
SPA_VDEVBITS = 24
143+
SPA_LSIZEBITS = 16
144+
SPA_PSIZEBITS = 16
145+
146+
TRUE = 1
147+
FALSE = 0
148+
149+
DMU_OT_NEWTYPE = 128
150+
DMU_OT_ENCRYPTED = 32
151+
DMU_OT_METADATA = 0x40
152+
DMU_OT_BYTESWAP_MASK = 0x1f
153+
154+
copyname = ['zero', 'single', 'double', 'triple']
155+
156+
@staticmethod
157+
def __bp_is_authenticated(bp: drgn.Object, ot: int, level: int) -> int:
158+
return (Blkptr.bp_uses_crypt(bp) and level > 0 and
159+
not Blkptr.dmu_ot_is_encrypted(ot))
160+
161+
@staticmethod
162+
def __bp_has_indirect_mac_checksum(bp: drgn.Object, level: int) -> int:
163+
return Blkptr.bp_uses_crypt(bp) and level > 0
164+
165+
@staticmethod
166+
def __dmu_ot_is_valid(bp: drgn.Object) -> int:
167+
ot = BF64_GET(bp.blk_prop, 48, 8)
168+
if ot & Blkptr.DMU_OT_NEWTYPE:
169+
return (ot & Blkptr.DMU_OT_BYTESWAP_MASK < len(
170+
sdb.get_object("dmu_ot_byteswap")))
171+
return ot < len(sdb.get_object("dmu_ot"))
172+
173+
@staticmethod
174+
def dmu_ot_is_encrypted(ot: int) -> int:
175+
if ot & Blkptr.DMU_OT_NEWTYPE:
176+
return ot & Blkptr.DMU_OT_ENCRYPTED
177+
return int(sdb.get_object("dmu_ot")[ot].ot_encrypt)
178+
179+
@staticmethod
180+
def bp_is_embedded(bp: drgn.Object) -> int:
181+
return BF64_GET(bp.blk_prop, 39, 1)
182+
183+
@staticmethod
184+
def bp_is_redacted(bp: drgn.Object) -> int:
185+
return (Blkptr.bp_is_embedded(bp) and BF64_GET(bp.blk_prop, 40, 8)
186+
== Blkptr.BP_EMBEDDED_TYPE_REDACTED)
187+
188+
@staticmethod
189+
def bp_uses_crypt(bp: drgn.Object) -> int:
190+
return BF64_GET(bp.blk_prop, 61, 1)
191+
192+
@staticmethod
193+
def bp_is_encrypted(bp: drgn.Object, ot: int, level: int) -> int:
194+
return (Blkptr.bp_uses_crypt(bp) and level <= 0 and
195+
Blkptr.dmu_ot_is_encrypted(ot))
196+
197+
@staticmethod
198+
def bp_get_crypt(bp: drgn.Object, ot: int, level: int) -> str:
199+
if Blkptr.bp_is_encrypted(bp, ot, level):
200+
return "encrypted"
201+
if Blkptr.__bp_is_authenticated(bp, ot, level):
202+
return "authenticated"
203+
if Blkptr.__bp_has_indirect_mac_checksum(bp, level):
204+
return "indirect-MAC"
205+
return "unencrypted"
206+
207+
@staticmethod
208+
def dva_is_empty(bp: drgn.Object) -> int:
209+
return int(bp.blk_dva[0].dva_word[0] == 0 and
210+
bp.blk_dva[0].dva_word[1] == 0)
211+
212+
@staticmethod
213+
def dva_get_offset(bp: drgn.Object, which_dva: int) -> int:
214+
return BF64_GET_SB(bp.blk_dva[which_dva].dva_word[1], 0, 63,
215+
Blkptr.SPA_MINBLOCKSHIFT, 0)
216+
217+
@staticmethod
218+
def dva_get_vdev(bp: drgn.Object, which_dva: int) -> int:
219+
return BF64_GET(bp.blk_dva[which_dva].dva_word[0], 32,
220+
Blkptr.SPA_VDEVBITS)
221+
222+
@staticmethod
223+
def __bp_is_hole(bp: drgn.Object) -> int:
224+
return Blkptr.bp_is_embedded(bp) != Blkptr.TRUE and Blkptr.dva_is_empty(
225+
bp)
226+
227+
@staticmethod
228+
def dva_get_asize(bp: drgn.Object, which_dva: int) -> int:
229+
if Blkptr.bp_is_embedded(bp):
230+
return 0
231+
return BF64_GET_SB(bp.blk_dva[which_dva].dva_word[0], 0,
232+
Blkptr.SPA_ASIZEBITS, Blkptr.SPA_MINBLOCKSHIFT, 0)
233+
234+
@staticmethod
235+
def bp_get_ndvas(bp: drgn.Object) -> int:
236+
ndvas = 0
237+
for x in range(0, 3):
238+
ndvas += Blkptr.dva_get_asize(bp, x) != 0
239+
return ndvas
240+
241+
@staticmethod
242+
def bpe_get_lsize(bp: drgn.Object) -> int:
243+
return BF64_GET_SB(bp.blk_prop, 0, 25, 0, 1)
244+
245+
@staticmethod
246+
def bp_get_lsize(bp: drgn.Object) -> int:
247+
if Blkptr.bp_is_embedded(bp):
248+
if BF64_GET(bp.blk_prop, 40, 8) == Blkptr.BP_EMBEDDED_TYPE_DATA:
249+
return Blkptr.bpe_get_lsize(bp)
250+
return 0
251+
return BF64_GET_SB(bp.blk_prop, 0, Blkptr.SPA_LSIZEBITS,
252+
Blkptr.SPA_MINBLOCKSHIFT, 1)
253+
254+
@staticmethod
255+
def bp_get_psize(bp: drgn.Object) -> int:
256+
if Blkptr.bp_is_embedded(bp):
257+
return 0
258+
return BF64_GET_SB(bp.blk_prop, 16, Blkptr.SPA_PSIZEBITS,
259+
Blkptr.SPA_MINBLOCKSHIFT, 1)
260+
261+
@staticmethod
262+
def bp_physical_birth(bp: drgn.Object) -> int:
263+
if Blkptr.bp_is_embedded(bp):
264+
phys = 0
265+
else:
266+
phys = bp.blk_phys_birth if bp.blk_phys_birth != 0 else bp.blk_birth
267+
return int(phys)
268+
269+
@staticmethod
270+
def bp_get_checksum(bp: drgn.Object) -> int:
271+
if Blkptr.bp_is_embedded(bp):
272+
return 2 # ZIO_CHECKSUM_OFF
273+
return BF64_GET(bp.blk_prop, 40, 8)
274+
275+
@staticmethod
276+
def bp_validate(bp: drgn.Object) -> int:
277+
errors = 0
278+
addr = hex(bp)
279+
if not Blkptr.__dmu_ot_is_valid(bp):
280+
tp = BF64_GET(bp.blk_prop, 48, 8)
281+
print(f"blkptr at {addr} has invalid TYPE {tp}")
282+
errors += 1
283+
ck = Blkptr.bp_get_checksum(bp)
284+
if ck >= len(sdb.get_object("zio_checksum_table")) or ck <= 1:
285+
print(f"blkptr at {addr} has invalid CHECKSUM {ck}")
286+
errors += 1
287+
comp = BF64_GET(bp.blk_prop, 32, 7)
288+
if comp >= len(sdb.get_object("zio_compress_table")) or comp <= 1:
289+
print(f"blkptr at {addr} has invalid COMPRESS {comp}")
290+
errors += 1
291+
lsz = Blkptr.bp_get_lsize(bp)
292+
psz = Blkptr.bp_get_psize(bp)
293+
if lsz > Blkptr.SPA_MAXBLOCKSIZE:
294+
print(f"blkptr at {addr} has invalid LSIZE {lsz}")
295+
errors += 1
296+
if psz > Blkptr.SPA_MAXBLOCKSIZE:
297+
print(f"blkptr at {addr} has invalid PSIZE {psz}")
298+
errors += 1
299+
if Blkptr.bp_is_embedded(bp):
300+
etype = BF64_GET(bp.blk_prop, 40, 8)
301+
if etype >= 3:
302+
print(f"blkptr at {addr} has invalid ETYPE {etype}")
303+
errors += 1
304+
return errors
305+
306+
names = ["blkptr"]
307+
input_type = "blkptr_t *"
308+
309+
def pretty_print(self, objs: Iterable[drgn.Object]) -> None:
310+
for bp in objs:
311+
if bp is None:
312+
print("<NULL>")
313+
continue
314+
if not Blkptr.__bp_is_hole(bp) and Blkptr.bp_validate(bp) != 0:
315+
continue
316+
bpi = BpInfo()
317+
if Blkptr.bp_is_embedded(bp) != 0:
318+
bpi.set_ndvas(0)
319+
else:
320+
bpi.set_ndvas(Blkptr.bp_get_ndvas(bp))
321+
for i in range(0, bpi.ndvas):
322+
vdev = Blkptr.dva_get_vdev(bp, i)
323+
offset = format(Blkptr.dva_get_offset(bp, i), 'x')
324+
asize = format(Blkptr.dva_get_asize(bp, i), 'x')
325+
print(f"DVA[{i}]=<{vdev}:{offset}:{asize}> ", end='')
326+
327+
bpi.set_initial_props(bp)
328+
329+
if Blkptr.bp_is_embedded(bp) != 0:
330+
etype = BF64_GET(bp.blk_prop, 40, 8)
331+
lsize = format(BF64_GET_SB(bp.blk_prop, 0, 25, 0, 1), 'x')
332+
psize = format(BF64_GET_SB(bp.blk_prop, 25, 7, 0, 1), 'x')
333+
print(
334+
f"EMBEDDED [L{bpi.level} {bpi.get_ot_name()}] et={etype} ",
335+
end='')
336+
print(f"{bpi.get_compstr()} size=", end='')
337+
print(f"{lsize}L/{psize}P birth={bpi.birth}L")
338+
continue
339+
if Blkptr.__bp_is_hole(bp):
340+
print(
341+
f"HOLE [L{bpi.level} {bpi.get_ot_name()}] size={bpi.lsize}L ",
342+
end='')
343+
print(f"birth={bpi.birth}L")
344+
continue
345+
if Blkptr.bp_is_redacted(bp):
346+
print(
347+
f"REDACTED [L{bpi.level} {bpi.get_ot_name()}] size={bpi.lsize}L ",
348+
end='')
349+
print(f"birth={bpi.birth}L")
350+
continue
351+
352+
print(
353+
f"[L{bpi.level} {bpi.get_ot_name()}] {bpi.get_cksum_string()}",
354+
end='')
355+
print(
356+
f" {bpi.get_compstr()} {bpi.crypt} {bpi.get_endian()} {bpi.get_gang()}",
357+
end='')
358+
print(f" {bpi.get_dedup()} {bpi.copies} {bpi.lsize}L/{bpi.psize}P ",
359+
end='')
360+
print(f"birth={bpi.birth}L/{bpi.pbirth}P", end='')
361+
print(
362+
f" fill={bpi.get_fill(bp)} cksum={bpi.c0}:{bpi.c1}:{bpi.c2}:{bpi.c3}"
363+
)

sdb/commands/zfs/internal/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def BF64_GET(x: drgn.Object, low: int, length: int) -> int:
5151
return BF64_DECODE(x, low, length)
5252

5353

54+
def BF64_GET_SB(x: int, low: int, length: int, shift: int, bias: int) -> int:
55+
return (BF64_GET(x, low, length) + bias) << shift
56+
57+
5458
def WEIGHT_IS_SPACEBASED(weight: int) -> bool:
5559
return weight == 0 or (BF64_GET(weight, 60, 1) != 0)
5660

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
blkptr at 0xffffa088ce68c040 has invalid CHECKSUM 0
2+
blkptr at 0xffffa088ce68c040 has invalid COMPRESS 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
HOLE [L0 unallocated] size=200L birth=0L
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DVA[0]=<0:2cefd5e00:20000> [L0 ZFS plain file] fletcher4 uncompressed unencrypted LE contiguous unique single 20000L/20000P birth=1624L/1624P fill=1 cksum=3feb86d3fa14:ff98411222361a1:7cd8eb3816d141e1:2d65ae38a67197c7
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DVA[0]=<0:80001e00:200> DVA[1]=<0:a0001e00:200> DVA[2]=<0:10000da00:200> [L0 DMU objset] fletcher4 lz4 unencrypted LE contiguous unique triple 1000L/200P birth=609L/609P fill=67 cksum=98a43f544:3eab35f140c:d164e4328324:1da8f37ef09087

tests/integration/test_linux_generic.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
"slabs | pp",
6969
"slabs -s util | slabs",
7070
"slabs | head 2 | slabs",
71+
'slabs | filter \'obj.name == "dnode_t"\' |walk | tail 8 | head 1 | cast dnode_t * | deref |member dn_phys |member dn_blkptr[0] |blkptr',
72+
'slabs | filter \'obj.name == "dnode_t"\' |walk | head 6056 | tail 1| cast dnode_t * | deref |member dn_phys |member dn_blkptr[0] |blkptr',
7173

7274
# slub
7375
'slabs | filter \'obj.name == "zio_cache"\' | slub_cache',

0 commit comments

Comments
 (0)