2 #define _FILE_OFFSET_BITS 64
11 /* read the device blocks and state blocks from this device.
12 * If valid, return a 'lafs_dev' suitably filled in.
13 * The most recent state block is found and index is recorded in lafs_dev
15 * device block should be near the start and near the end.
16 * Should probably determine block size, but for not just iterate
20 static int valid_devblock(struct lafs_dev *db, unsigned long long addr,
21 unsigned long long device_bytes)
23 /* check that this devblock is valid, given that
24 * it was found at sector 'addr'
28 unsigned long long segsize;
31 if (strncmp(db->idtag, "LaFS-DeviceBlock", 16) != 0)
33 if (strncmp(db->version, "AlphaDevel ", 16) != 0)
35 /* uuid can be anything */
38 crc2 = crc32(0, (uint32_t*)db, LAFS_DEVBLK_SIZE);
43 byteaddr = addr << 9; /* convert to byte */
44 if (__le64_to_cpu(db->devaddr[0]) != byteaddr &&
45 __le64_to_cpu(db->devaddr[1]) != byteaddr)
48 if (db->statebits < 10 || db->statebits > 16)
50 if (db->blockbits < 9 || db->blockbits > 20)
52 if (__le16_to_cpu(db->width) < 1 || __le16_to_cpu(db->width) >= 512)
54 if (__le32_to_cpu(db->stride) < 1)
56 /* devaddr[0] must be early, [1] must be late */
57 if (__le64_to_cpu(db->devaddr[0]) >=
58 __le64_to_cpu(db->segment_offset))
61 if (__le64_to_cpu(db->devaddr[1]) <
62 __le64_to_cpu(db->segment_offset) +
63 ((((unsigned long long)__le32_to_cpu(db->segment_count)
64 * __le32_to_cpu(db->segment_size)))
68 /* 2 is an absolute minimum segment size, a few hundred is more
69 * likely. We'll put a lower limit of 8, and an upper of 800000
71 if (__le32_to_cpu(db->segment_size) < 8 ||
72 __le32_to_cpu(db->segment_size) > 800000)
75 if (__le32_to_cpu(db->segment_offset) >
76 (__le32_to_cpu(db->segment_size)<<db->blockbits) * 10)
79 /* The 4 state blocks live before the first or after the last segment.
80 * The distance from start of first to end of last is either:
81 * - segment_count * segment_size if width*stride <= segment_size
82 * - (width-1) * stride + segment_size / width * segment_count
83 * if width * stride > segment_size
85 segsize = __le32_to_cpu(db->segment_size);
86 segsize *= __le32_to_cpu(db->segment_count);
87 if (__le16_to_cpu(db->width) * __le32_to_cpu(db->stride)
88 > __le32_to_cpu(db->segment_size)) {
89 int stride = __le32_to_cpu(db->stride);
90 int width = __le16_to_cpu(db->width);
93 segsize += (width - 1) * stride;
95 segsize <<= db->blockbits;
96 for (i = 0; i < 4; i++) {
97 unsigned long long addr = __le64_to_cpu(db->stateaddr[i]);
98 int offset = __le32_to_cpu(db->segment_offset);
99 if (addr + (1<<db->statebits) > offset &&
100 addr < offset + segsize)
102 if (addr + (1<<db->statebits) > device_bytes)
106 /* Check all segments fit within device */
107 if (__le32_to_cpu(db->segment_offset) + segsize > device_bytes)
110 if (__le32_to_cpu(db->level) > 10)
113 /* I guess it look sane enough... */
117 static int compare_dev(struct lafs_dev *curr, struct lafs_dev *new)
119 if (memcmp(curr->uuid, new->uuid, 16) != 0)
120 /* there are different */
122 if (memcmp(curr->version, new->version, 16) != 0)
125 if (__le32_to_cpu(curr->seq) >= __le32_to_cpu(new->seq))
126 return 0; /*current is best */
130 static int check_state(struct lafs_device *dev, int which)
132 /* Load this state block, perform basic check and update
133 * dev->state_seq if seq number is new
135 struct lafs_state *sb = malloc(dev->statesize);
139 lseek64(dev->fd, dev->stateaddr[which], 0);
140 if (read(dev->fd, sb, dev->statesize) != dev->statesize)
142 if (memcmp(sb->idtag, "LaFS-State-Block", 16) != 0)
144 if (memcmp(sb->uuid, dev->uuid, 16) != 0)
148 crc2 = crc32(0, (uint32_t*)sb, dev->statesize);
152 if (__le32_to_cpu(sb->seq) > dev->state_seq) {
153 dev->recent_state = which;
154 dev->state_seq = __le32_to_cpu(sb->seq);
155 dev->devices = __le32_to_cpu(sb->devices);
164 static int destroy(struct lafs_device *dev)
172 struct lafs_device *lafs_load(int fd, long long device_bytes, char **err)
174 char buf[LAFS_DEVBLK_SIZE];
175 struct lafs_dev *d = (void*)buf;
176 struct lafs_dev best;
178 unsigned long long addr, best_addr;
179 struct lafs_device *dev;
185 for (addr = 0; addr < device_bytes; addr += 512) {
186 if (addr == 32*512 && device_bytes > 64*512)
187 addr = device_bytes - 32*512;
189 lseek64(fd, addr, 0);
190 if (read(fd, buf, LAFS_DEVBLK_SIZE) != LAFS_DEVBLK_SIZE)
192 if (!valid_devblock(d, addr, device_bytes))
200 switch(compare_dev(&best, d)) {
201 case 0: /* best is still best */
203 case 1: /* d is better */
207 default: /* incompatible */
208 *err = "inconsistent device information blocks found";
213 *err = "no valid device block found";
217 /* talloc device, fill in details, record where loaded from */
218 dev = talloc(NULL, struct lafs_device);
219 talloc_set_destructor(dev, destroy);
220 memset(dev, 0, sizeof(*dev));
222 dev->seq = __le32_to_cpu(best.seq);
223 dev->start = __le64_to_cpu(best.start);
224 dev->size = __le64_to_cpu(best.size);
225 for (i=0 ; i<2; i++) {
226 dev->devaddr[i] = __le64_to_cpu(best.devaddr[i]);
227 if (dev->devaddr[i] == best_addr)
228 dev->recent_super = i;
231 dev->stateaddr[i] = __le64_to_cpu(best.stateaddr[i]);
233 lafs_decode_timeval(&dev->ctime, best.ctime);
234 dev->width = __le16_to_cpu(best.width);
235 dev->stride = __le32_to_cpu(best.stride);
236 dev->segment_size = __le32_to_cpu(best.segment_size);
237 dev->segment_offset = __le32_to_cpu(best.segment_offset);
238 dev->segment_count = __le32_to_cpu(best.segment_count);
239 dev->usage_inum = __le32_to_cpu(best.usage_inum);
241 memcpy(dev->version, best.version, 16);
242 memcpy(dev->uuid, best.uuid, 16);
243 dev->blockbits = best.blockbits;
244 dev->statesize = 1 << best.statebits;
248 found += check_state(dev, i);
251 *err = "No valid state block found on device";