-
Notifications
You must be signed in to change notification settings - Fork 0
/
hammer2.c
135 lines (125 loc) · 4.59 KB
/
hammer2.c
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
#include <u.h>
#include <libc.h>
#include <stdio.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
// for crc32
#include <flate.h>
#include "uuid.h"
#include "hammer2_disk.h"
#include "hammer2.h"
#include "9phammer.h"
hammer2_volume_data_t volumehdr;
// Loads an inode from dataoff in fd into the inode argument.
void loadinode(hammer2_blockref_t *block, hammer2_inode_data_t *inode) {
char *err = loadblock(block, inode, sizeof(hammer2_inode_data_t), nil);
if(err != nil) {
fprint(2, "error loading inode: %s\n", err);
}
};
void dumpinode(hammer2_inode_data_t *i) {
if (i == nil) {
fprintf(stderr, "No inode\n");
return;
}
fprintf(stderr, "Inode: %d\nSize: %d\n", i->meta.inum, i->meta.size);
fprintf(stderr, "Type: %d\n", i->meta.type);
fprintf(stderr, "Name_key: %d\n", i->meta.name_key);
fprintf(stderr, "OpFlags: %d\n", i->meta.op_flags);
fprintf(stderr, "CapFlags: %d\n", i->meta.cap_flags);
fprintf(stderr, "NLinks: %d\n", i->meta.nlinks);
fprintf(stderr, "pfs_inum: %d: %d\n", i->meta.pfs_inum);
fprintf(stderr, "Comp_Algo: %d\n", i->meta.comp_algo);
}
void dumpblock(hammer2_blockref_t *b) {
if (b == nil) {
fprintf(stderr, "No blockref\n");
return;
}
fprintf(stderr, "Block type: %d\n", b->type);
fprintf(stderr, "Methods: %d (Check: %d Comp: %d)\n", b->methods, HAMMER2_DEC_CHECK(b->methods), HAMMER2_DEC_COMP(b->methods));
fprintf(stderr, "Keybits: %d\n", b->keybits);
fprintf(stderr, "vradix: %d\n", b->vradix);
fprintf(stderr, "flags: %d\n", b->flags);
fprintf(stderr, "leaf_count: %d\n", b->leaf_count);
fprintf(stderr, "key: %d\n", b->key);
fprintf(stderr, "key: %d\n", b->key);
fprintf(stderr, "(if applicable) data_count %d inode_count: %d\n", b->embed.stats.data_count, b->embed.stats.inode_count);
}
void readvolume(int fd, hammer2_dev_t *hd) {
hammer2_volume_data_t vol;
int valid;
int i;
valid = 0;
for(i = 0; i < 4; i++){
seek(fd, i*HAMMER2_ZONE_BYTES64, SEEK_SET);
read(fd, &vol, sizeof(hammer2_volume_data_t));
if(vol.magic != HAMMER2_VOLUME_ID_HBO) continue;
// printf("good magic");
// printf(" Total size: %u Free: %u\n", vol.allocator_size, vol.allocator_free);
// printf("Mirror_tid: %16d\n", vol.mirror_tid);
if(icrc32(&vol, 512-4) != vol.icrc_sects[7]) {
fprint(2,"Invalid CRC for volume header sector 1\n");
continue;
}
if(icrc32(&vol.sroot_blockset, 512) != vol.icrc_sects[6]) {
fprint(2,"Invalid CRC for superoot block\n");
continue;
}
/*
These seem to always be 0 on real DragonFly systems, despite
the documentation in hammer2_disk.h claiming otherwise.
if(icrc32(vol.sector2, 512) != vol.icrc_sects[5]) {
fprint(2,"Invalid CRC for volume sector 2 got %ux want %x\n", icrc32(vol.sector2, 512), vol.icrc_sects[5]);
continue;
}
if(icrc32(vol.sector3, 512) != vol.icrc_sects[4]) {
fprint(2,"Invalid CRC for volume sector 3\n");
continue;
}
if(icrc32(&vol.freemap_blockset, 512) != vol.icrc_sects[3]) {
fprint(2,"Invalid CRC for volume freemap got %ux want %ux\n", icrc32(&vol.freemap_blockset, 512), vol.icrc_sects[3]);
continue;
}
// sector 5? 6? 7?
*/
if(icrc32(&vol, sizeof(hammer2_volume_data_t)-4) != vol.icrc_volheader) {
fprint(2, "Invalid full volume header CRC\n");
continue;
}
if(valid == 0 || hd->voldata.mirror_tid < vol.mirror_tid){
valid = 1;
hd->voldata = vol;
hd->volhdrno = i;
}
}
volumehdr = vol;
/*
print("Using volume header #%d\n", hd->volhdrno);
print("Volume size: %ulld\n", hd->voldata.allocator_size);
print("Freemap version: %d\n", hd->voldata.freemap_version);
print("Allocator size: %ulld\n", hd->voldata.volu_size);
print("Allocator free: %ulld\n", hd->voldata.allocator_free);
print("Allocator beg: %ulld\n", hd->voldata.allocator_beg);
*/
/*
For looking into the freemap on start/debugging
char buf[HAMMER2_BLOCKREF_LEAF_MAX];
int j = 0;
for(i = 0; i < 4; i++) {
hammer2_blockref_t *block =&(hd->voldata.freemap_blockset.blockref[i]);
print("Freemap 0.%d Type %d\n", i, block->type);
print("Key: %ulld keysize: %d\n", block->key, 1<<block->keybits);
assert(HAMMER2_DEC_COMP(block->methods) == 0);
pread(fd, buf, HAMMER2_BLOCKREF_LEAF_MAX, block->data_off & HAMMER2_OFF_MASK);
for(j = 0; j < 1<<block->keybits;j++) {
block = ((hammer2_blockref_t*) buf) + j;
print("Freemap 1 Level: %d.%d Type %d\n", i, j, block->type);
print("Key: %ulld keysize: %uld\n", block->key, 1<<block->keybits);
print("Available: %ulld Radixes: %ulb\n", block->check.freemap.avail, block->check.freemap.bigmask);
}
}
//print("mirror_tid: %x freemap tid: %8.x bulkfree tid: %x\n", hd->voldata.mirror_tid, hd->voldata.freemap_tid, hd->voldata.bulkfree_tid);
*/
}