-
Notifications
You must be signed in to change notification settings - Fork 3
/
dnode.py
401 lines (365 loc) · 13 KB
/
dnode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# Copyright (c) 2017 Hristo Iliev <github@hiliev.eu>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import struct, datetime
from zfs.blockptr import BlockPtr
from zfs.obj_desc import DMU_TYPE_DESC
BLKPTR_OFFSET = 64
class BonusDataset:
def __init__(self, data):
(self.ds_dir_obj, self.ds_prev_snap_obj, self.ds_prev_snap_txg, self.ds_prev_next_obj, self.ds_snapnames_zapobj,
self.ds_num_children, self.ds_creation_time, self.ds_creation_txg, self.ds_deadlist_obj, self.ds_used_bytes,
self.ds_compressed_bytes, self.ds_uncompressed_bytes, self.ds_unique_bytes, self.ds_fsid_guid, self.ds_guid,
self.ds_restoring) = struct.unpack("=16Q", data[:16*8])
self.bptr = BlockPtr()
self.bptr.parse(data[16*8:16*8+128])
def __str__(self):
fields = [
'ds_dir_obj',
'ds_prev_snap_obj',
'ds_prev_snap_txg',
'ds_prev_next_obj',
'ds_snapnames_zapobj',
'ds_num_children',
'ds_creation_time',
'ds_creation_txg',
'ds_deadlist_obj',
'ds_used_bytes',
'ds_compressed_bytes',
'ds_uncompressed_bytes',
'ds_unique_bytes',
'ds_fsid_guid',
'ds_guid',
'ds_restoring',
'ds_bp'
]
fmt = ' '.join([f + '={}' for f in fields])
return fmt.format(
self.ds_dir_obj,
self.ds_prev_snap_obj,
self.ds_prev_snap_txg,
self.ds_prev_next_obj,
self.ds_snapnames_zapobj,
self.ds_num_children,
self.ds_creation_time,
self.ds_creation_txg,
self.ds_deadlist_obj,
self.ds_used_bytes,
self.ds_compressed_bytes,
self.ds_uncompressed_bytes,
self.ds_unique_bytes,
self.ds_fsid_guid,
self.ds_guid,
self.ds_restoring,
self.bptr
)
class BonusDirectory:
def __init__(self, data):
(
self.dd_creation_time,
self.dd_head_dataset_obj,
self.dd_parent_obj,
self.dd_clone_parent_obj,
self.dd_child_dir_zapobj,
self.dd_used_bytes,
self.dd_compressed_bytes,
self.dd_uncompressed_bytes,
self.dd_quota,
self.dd_reserved,
self.dd_props_zapobj
) = struct.unpack("=11Q", data[:11*8])
def __str__(self):
fields = [
'dd_creation_time',
'dd_head_dataset_obj',
'dd_parent_obj',
'dd_clone_parent_obj',
'dd_child_dir_zapobj',
'dd_used_bytes',
'dd_compressed_bytes',
'dd_uncompressed_bytes',
'dd_quota',
'dd_reserved',
'dd_props_zapobj',
]
fmt = ' '.join([f+'={}' for f in fields])
return fmt.format(
self.dd_creation_time,
self.dd_head_dataset_obj,
self.dd_parent_obj,
self.dd_clone_parent_obj,
self.dd_child_dir_zapobj,
self.dd_used_bytes,
self.dd_compressed_bytes,
self.dd_uncompressed_bytes,
self.dd_quota,
self.dd_reserved,
self.dd_props_zapobj
)
class BonusZnode:
def __init__(self, data):
(
self.zp_atime, self.zp_atime_ns,
self.zp_mtime, self.zp_mtime_ns,
self.zp_ctime, self.zp_ctime_ns,
self.zp_crtime, self.zp_crtime_ns,
self.zp_gen,
self.zp_mode,
self.zp_size,
self.zp_parent,
self.zp_links,
self.zp_xattr,
self.zp_rdev,
self.zp_flags,
self.zp_uid, self.zp_gid
) = struct.unpack("=18Q", data[:18*8])
self.zp_inline_content = data[264:]
def size(self):
return self.zp_size
def mtime(self):
return self.zp_mtime
def mode(self):
return self.zp_mode
def uid(self):
return self.zp_uid
def gid(self):
return self.zp_gid
def __str__(self):
fields = [
'zp_atime', 'zp_atime_ns',
'zp_mtime', 'zp_mtime_ns',
'zp_ctime', 'zp_ctime_ns',
'zp_crtime', 'zp_crtime_ns',
'zp_gen',
'zp_mode',
'zp_size',
'zp_parent',
'zp_links',
'zp_xattr',
'zp_rdev',
'zp_flags',
'zp_uid', 'zp_gid'
]
fmt = ' '.join([f+'={}' for f in fields])
return fmt.format(
self.zp_atime, self.zp_atime_ns,
self.zp_mtime, self.zp_mtime_ns,
self.zp_ctime, self.zp_ctime_ns,
self.zp_crtime, self.zp_crtime_ns,
self.zp_gen,
self.zp_mode,
self.zp_size,
self.zp_parent,
self.zp_links,
self.zp_xattr,
self.zp_rdev,
self.zp_flags,
self.zp_uid, self.zp_gid
)
class BonusSysAttr:
def __init__(self, objset, data):
if objset is None:
return;
try:
SA_MAGIC=0x2F505A
(magic,layoutid,hdrsz,l) = struct.unpack("=IBBH",data[0:8])
if not (magic == SA_MAGIC):
print("[-] Error: SA_MAGIC wrong")
hdrsz *= 2
if layoutid == 3:
print("Symlink")
lenidx = 0
if (hdrsz < 8):
hdrsz = 8
ptr = hdrsz
#ptr = 8 #skip sa_hdr_phys_t
for f in objset._sa._lay[str(layoutid)]:
l = f['len']
b = data[ptr:ptr+l]
v = None
if (l == 16):
(v0,v1) = struct.unpack("=QQ",b)
v = [v0,v1];
elif (l == 8):
v, = struct.unpack("=Q",b)
elif (l == 4):
v, = struct.unpack("=I",b)
elif (l == 0):
l, = struct.unpack("=H",data[6+lenidx*2:6+lenidx*2+2])
lenidx += 1
if (f['name'] == "zpl_dacl_aces"):
pass
elif (f['name'] == "zpl_symlink"):
v = data[ptr:ptr+l]
#ptr = len(data)
ptr += l
setattr(self,f['name'], v);
n = f['name'].replace("zpl_","zp_");
setattr(self,n, v);
self.zp_inline_content = None
#ZFS_OLD_ZNODE_PHYS_SIZE=0x108
#if (len(data) > ZFS_OLD_ZNODE_PHYS_SIZE):
self.zp_inline_content = data[ptr:]
except:
pass
def size(self):
return self.zpl_size
def mtime(self):
try:
return self.zpl_mtime[0]
except:
return datetime.datetime.now().timestamp()
def mode(self):
return self.zpl_mode
def uid(self):
return self.zpl_uid
def gid(self):
return self.zpl_gid
def __str__(self):
pass
DNODE_FLAG_USED_BYTES=(1 << 0)
class DNode:
def __init__(self, data=None, objset=None):
self._data = None
self._type = None # uint8_t 1
self._indblkshift = None # uint8_t 1
self._nlevels = None # uint8_t 1
self._nblkptr = None # uint8_t 1
self._bonustype = None # uint8_t 1
self._checksum = None # uint8_t 1
self._compress = None # uint8_t 1
self._flags = None # uint8_t 1
self._datablkszsec = None # uint16_t 2
self._bonuslen = None # uint16_t 2
self._extra_slots = None # uint8_t 1
self._pad2 = None # uint8_t[4] 4
self._maxblkid = None # uint64_t 8
self._used = None # uint64_t 8
self._pad3 = None # uint64_t[4] 32
self._blkptr = None # blkptr_t[N] @64
self._bonus = None # uint8_t[BONUSLEN]
self._datablksize = None
self._objset = objset
if data is not None:
self.parse(data)
def parse(self, data):
if len(data) < 512:
raise ValueError("Data is too small")
# Save data for dumping purposes
self._data = data[:]
(self._type, self._indblkshift, self._nlevels, self._nblkptr,
self._bonustype, self._checksum, self._compress, self._flags,
self._datablkszsec, self._bonuslen, self._extra_slots, self._maxblkid,
self._used) = struct.unpack("=8B2HB3xQQ32x", data[:BLKPTR_OFFSET])
if self._type == 0:
return
# Object type > 100 (or even 53) is probably due to data error
elif self._type > 100:
if self._type==196: # on linux 196 is "zap" with "bonustype dataset"
pass
else:
self._invalidate()
return
self._blkptr = []
if self._nblkptr > 3:
# More than three block pointers is a sign of data error
self._invalidate()
return
self._used = self._used << 9 if not self._flags & DNODE_FLAG_USED_BYTES else self._used;
self._datablksize = self._datablkszsec << 9
ptr = BLKPTR_OFFSET
for bn in range(self._nblkptr):
b = BlockPtr(data=data[ptr:ptr+128])
self._blkptr.append(b)
ptr += 128
bonus_data = data[ptr:ptr+self._bonuslen]
if self._bonuslen and self._bonustype == 12:
self._bonus = BonusDirectory(bonus_data)
elif self._bonuslen and self._bonustype == 16:
self._bonus = BonusDataset(bonus_data)
elif self._bonuslen and self._bonustype == 17:
self._bonus = BonusZnode(bonus_data)
elif self._bonuslen and self._bonustype == 0x2c:
self._bonus = BonusSysAttr(self._objset, bonus_data)
else:
self._bonus = bonus_data
@property
def blkptrs(self):
return self._blkptr
@property
def maxblkid(self):
return self._maxblkid
@property
def bonus(self):
return self._bonus
@property
def type(self):
return self._type
@property
def levels(self):
return self._nlevels
@property
def datablksize(self):
return self._datablksize
@property
def indblkshift(self):
return self._indblkshift
def dump_data(self, file_path):
with open(file_path, 'wb') as f:
f.write(self._data)
def _invalidate(self):
self._type = None
def __str__(self):
if self._type is None:
return "<invalid dnode>"
elif self._type == 0:
return "<unallocated dnode>"
try:
if self._type == 196:
dmu_type = "zap"
else:
dmu_type = DMU_TYPE_DESC[self._type]
except IndexError:
dmu_type = "unk_{}".format(self._type)
bptrs = " ".join(["blkptr[{}]={}".format(i, v) for i, v in enumerate(self._blkptr)])
bonus = " bonus[{}]".format(self._bonuslen) if self._bonuslen else ""
if self._bonustype in [12, 16]:
bonus += "=[{}]".format(self._bonus)
return "[{}] {}B {}L/{} {}{}".format(dmu_type, self._maxblkid+1,
self._nlevels, 1 << self._indblkshift, bptrs, bonus)
@staticmethod
def from_bptr(vdev, bptr, dvas=(0, 1), objset=None):
data = None
for dva in dvas:
data,c = vdev.read_block(bptr, dva=dva)
if data and c:
break
if data is None:
return None
dn = DNode(objset=objset)
dn.parse(data)
return dn