summaryrefslogtreecommitdiffstats
path: root/pickles/zfs.pk
blob: 27fa95ba4bda50ea6cc97de06bfa391ea01cb2bb (plain)
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
/*
 * Preliminary pickle for simple "zfs-send" streams from the ZFS file system.
 */

set_endian(0); // TODO

type ZFS_Fletcher4 = struct {
  // https://en.wikipedia.org/wiki/Fletcher%27s_checksum
  little uint<64> a;
  little uint<64> b;
  little uint<64> c;
  little uint<64> d;
  method varsize = (uint<32>[] buf)int:
    {
      for (var i = 0; i < buf'length; i++)
        {
          a += buf[i];
          b += a;
          c += b;
          d += c;
        }
      return 0;
    }
  method bytes = byte[32]: {
    var out = byte[32]();
    // blit a,b,c,d to a little-endian byte string:
    for(var i = 0; i < 8; i++)
      {
        out[0  +i] = (a .>> i*8) & 0xffU;
      }
    for(var i = 0; i < 8; i++)
      {
        out[8 +i] = (b .>> i*8) & 0xffU;
      }
    for(var i = 0; i < 8; i++)
      {
        out[16+i] = (c .>> i*8) & 0xffU;
      }
    for(var i = 0; i < 8; i++)
      {
        out[24+i] = (d .>> i*8) & 0xffU;
      }
    return out;
  }
  method _print = void:
  {
    var out = bytes;
    for(var i = 0; i < out'length; i++)
      {
        printf("%u8x", out[i]&0xff);
      }
  }
}; assert(sizeof(ZFS_Fletcher4) == 32#B);

type ZFS_drr_type = union
  {
    little uint DRR_BEGIN  == 0;
    little uint DRR_OBJECT == 1;
    little uint DRR_FREEOBJECTS == 2;
    little uint DRR_WRITE == 3;
    little uint DRR_FREE  == 4;
    little uint DRR_END   == 5U;
    little uint DRR_WRITE_BYREF == 6;
    little uint DRR_SPILL == 7;
    little uint DRR_WRITE_EMBEDDED == 8;
    little uint DRR_OBJECT_RANGE == 9;
    little uint DRR_REDACT == 10;
    little uint DRR_NUMTYPES == 11;
  }; assert(sizeof(ZFS_drr_type) == 32#b);

type ZFS_dmu_object_type = union
  {
    little uint DMU_OT_PLAIN_FILE_CONTENTS == 19;
    little uint DMU_OT_DIRECTORY_CONTENTS == 20;
    little uint DMU_OT_MASTER_NODE == 21;
    little uint DMU_OT_UNLINKED_SET == 22;
    little uint DMU_OT_ZVOL == 23;
    little uint DMU_OT_ZVOL_PROP == 24;
    little uint DMU_OT_POOL_PROPS == 31;
    little uint DMU_OT_SA == 44;
    little uint DMU_OT_SA_MASTER_NODE == 45;
    little uint DMU_OT_SA_ATTR_REGISTRATION == 46;
    little uint DMU_OT_SA_ATTR_LAYOUTS == 47;
    little uint DMU_OT_DEDUP == 49;
  }; assert(sizeof(ZFS_dmu_object_type) == 32#b);

type ZFS_drr_begin = struct
  {
    ZFS_drr_type todotype == ZFS_drr_type{DRR_BEGIN=0};
    little offset<uint<32>,B>; // payloadlen
    little uint<64> drr_magic == 0x2f5BAcbAC;
    // ^- DMU_BACKUP_MAGIC ?

    little uint<64> drr_versioninfo;
    little uint<64> drr_creationtime; // unix timestamp
    little uint<32> drr_type;
    little uint<32> drr_flags;
    little uint<64> drr_toguid;
    little uint<64> drr_fromguid;
    string   drr_name : drr_name'length < 256;
    byte[0] @ OFFSET + alignto(drr_name'size,256#B);
  };

type ZFS_drr_object = struct
 {
   little uint<64> drr_object;
   ZFS_dmu_object_type drr_type;
   little uint<32> drr_bonustype;
   little uint<32> drr_blksz;
   little offset<uint<32>,B> drr_bonuslen;
   uint<8> drr_checksumtype;
   uint<8> drr_compress;
   uint<8> drr_dn_slots;
   uint<8> drr_flags;
   little uint<32> drr_raw_bonuslen;
   little uint<64> drr_toguid;
   uint<8> drr_indblkshift;
   uint<8> drr_nlevels;
   uint<8> drr_nblkptr;
   byte[5] drr_pad;
   little uint<64> drr_maxblkid;
   // 304#B == 312#B - the two 32-bit ints we consume in ZFS_drr
   byte[drr_bonuslen] bonus @ 304#B; // TODO what is drr_raw_bonuslen ?
 };

type ZFS_drr_write = struct
  {
    little uint<64> drr_object;
    little uint<32> drr_type;
    little uint<32> drr_pad;
    little uint<64> drr_offset;
    little offset<uint<64>,B> drr_logical_size;
    little uint<64> drr_toguid;
    uint<8> drr_checksumtype; // 2 == off, 7 == fletcher4, ???
    uint<8> drr_flags;
    uint<8> drr_compressiontype;
    byte[32] drr_key_cksum @ 48#B;
    little uint<64> drr_key_prop;
    little offset<uint<64>,B> drr_compressed_size;
    byte[16] drr_salt;
    byte[16] drr_iv;
    byte[16] drr_mac;
    var physical_size = drr_compressed_size==0#B ? drr_logical_size : drr_compressed_size;
    byte[physical_size] content @ 304#B;
  };

type ZFS_drr_freeobjects = struct
  {
    // TODO
  };

type ZFS_drr_free = struct
  {
    little uint<64> drr_object;
    little uint<64> drr_offset;
    little uint<64> drr_length;
    little uint<64> drr_toguid;
  };
type ZFS_drr_end = struct
  {
    little uint<32> drr_type == 5;
    //ZFS_drr_type drr_type == ZFS_drr_type{DRR_END=5};
    /* ^-- TODO this triggers assertions in p
       if you comment out "ZFS_drr_begin drr_begin;" and "method checksum ="
       in the ZFS_send type and run this against /dev/zero:
       poke: pvm-val.c:346: pvm_ref_struct_cstr: Assertion `PVM_IS_SCT (sct)' failed.
       Aborted
     */
    little offset<uint<32>,B> payloadlen == 0#B;
    ZFS_Fletcher4 cksum;
  };

type ZFS_drr = struct
{
  ZFS_drr_type drr_type;
  little offset<uint<32>,B> drr_payloadlen;
  union {
    // we do not accept BEGIN or END in here, see ZFS_send
    ZFS_drr_object drr_object : "DRR_OBJECT" == drr_type'ename(0);
    ZFS_drr_write  drr_write  : "DRR_WRITE"  == drr_type'ename(0);
    ZFS_drr_free   drr_free   : "DRR_FREE"   == drr_type'ename(0);
    ZFS_drr_freeobjects drr_freeobjects : "DRR_FREEOBJECTS" == drr_type'ename(0);
  } p;
  ZFS_Fletcher4 cksum @ 280#B;
  byte[drr_payloadlen] drr_payload;
};

type ZFS_send = struct
  {
     ZFS_drr_begin drr_begin;
     ZFS_drr[] records;
     ZFS_drr_end drr_end;

     // boolean, 1 if the on-disk checksum matches what we compute
     method checksum = int:
       {
         var _checksum = ZFS_Fletcher4{};
         _checksum.varsize(uint<32>[78] @ drr_begin'offset);
         for(var i = 0; i < records'length; i++)
          {
            var reci = records[i];
            // TODO uint<32> only works if that's read as little endian
            _checksum.varsize(uint<32>[70] @ reci'offset);

            // now we can compare to record[i].cksum:
            if (_checksum != reci.cksum)
              {
                printf("\x1b[31mrecord[%u32d] computed:%v\n           != disk:%v\x1b[0m\n", i, _checksum, reci.cksum);
              }
            else
              {
                printf("\x1b[32mrecord[%u32d] good cksum:%v\x1b[0m\n", i, _checksum);
             }

             // now we hash in the on-disk cksum and any extra data:
             // (this means that the extra data in a OBJECT/WRITE is
             // not checksummed before reading the following record
             // (FREE or whichever))
             var extra = (reci'size'magnitude * reci'size'unit)/32 - 70;
             _checksum.varsize(uint<32>[extra] @ (reci'offset+280#B));
         }

       // now the DRR_END record should match what we computed:
       return _checksum == drr_end.cksum;
    }
  };
Generated by cgit on ageinghacker.net.
I am Luca Saiu. If you have reason to request commit access to one of these repositories please contact me. You may also send me patches by email.