i386 removal, part 54/x: Remove perfmon(4) remains.
[dragonfly.git] / sbin / hammer / cmd_recover.c
blobfe9dcf6d5ec4569b5f18aed1683f8969c66f3caa
1 /*
2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include "hammer.h"
37 struct recover_dict {
38 struct recover_dict *next;
39 struct recover_dict *parent;
40 int64_t obj_id;
41 uint8_t obj_type;
42 uint8_t flags;
43 uint16_t pfs_id;
44 int64_t size;
45 char *name;
48 #define DICTF_MADEDIR 0x01
49 #define DICTF_MADEFILE 0x02
50 #define DICTF_PARENT 0x04 /* parent attached for real */
51 #define DICTF_TRAVERSED 0x80
53 static void recover_top(char *ptr);
54 static void recover_elm(hammer_btree_leaf_elm_t leaf);
55 static struct recover_dict *get_dict(int64_t obj_id, uint16_t pfs_id);
56 static char *recover_path(struct recover_dict *dict);
57 static void sanitize_string(char *str);
59 static const char *TargetDir;
60 static int CachedFd = -1;
61 static char *CachedPath;
63 void
64 hammer_cmd_recover(const char *target_dir)
66 struct buffer_info *data_buffer;
67 struct volume_info *volume;
68 hammer_off_t off;
69 hammer_off_t off_end;
70 char *ptr;
72 AssertOnFailure = 0;
73 TargetDir = target_dir;
75 if (mkdir(TargetDir, 0777) == -1) {
76 if (errno != EEXIST) {
77 perror("mkdir");
78 exit(1);
82 printf("Running raw scan of HAMMER image, recovering to %s\n",
83 TargetDir);
85 data_buffer = NULL;
86 TAILQ_FOREACH(volume, &VolList, entry) {
87 check_volume(volume);
88 printf("Scanning volume %d size %s\n",
89 volume->vol_no, sizetostr(volume->size));
90 off = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no, 0);
91 off_end = off + HAMMER_VOL_BUF_SIZE(volume->ondisk);
92 while (off < off_end) {
93 ptr = get_buffer_data(off, &data_buffer, 0);
94 if (ptr) {
95 recover_top(ptr);
96 off += HAMMER_BUFSIZE;
100 rel_buffer(data_buffer);
102 if (CachedPath) {
103 free(CachedPath);
104 close(CachedFd);
105 CachedPath = NULL;
106 CachedFd = -1;
109 AssertOnFailure = 1;
113 * Top level recovery processor. Assume the data is a B-Tree node.
114 * If the CRC is good we attempt to process the node, building the
115 * object space and creating the dictionary as we go.
117 static void
118 recover_top(char *ptr)
120 struct hammer_node_ondisk *node;
121 hammer_btree_elm_t elm;
122 int maxcount;
123 int i;
125 for (node = (void *)ptr; (char *)node < ptr + HAMMER_BUFSIZE; ++node) {
126 if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) ==
127 node->crc &&
128 node->type == HAMMER_BTREE_TYPE_LEAF) {
130 * Scan elements
132 maxcount = HAMMER_BTREE_LEAF_ELMS;
133 for (i = 0; i < node->count && i < maxcount; ++i) {
134 elm = &node->elms[i];
135 if (elm->base.btype != 'R')
136 continue;
137 recover_elm(&elm->leaf);
143 static void
144 recover_elm(hammer_btree_leaf_elm_t leaf)
146 struct buffer_info *data_buffer = NULL;
147 struct recover_dict *dict;
148 struct recover_dict *dict2;
149 hammer_data_ondisk_t ondisk;
150 hammer_off_t data_offset;
151 struct stat st;
152 int chunk;
153 int len;
154 int zfill;
155 int64_t file_offset;
156 uint16_t pfs_id;
157 size_t nlen;
158 int fd;
159 char *name;
160 char *path1;
161 char *path2;
164 * Ignore deleted records
166 if (leaf->delete_ts)
167 return;
168 if ((data_offset = leaf->data_offset) != 0)
169 ondisk = get_buffer_data(data_offset, &data_buffer, 0);
170 else
171 ondisk = NULL;
172 if (ondisk == NULL)
173 goto done;
175 len = leaf->data_len;
176 chunk = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
177 if (chunk > len)
178 chunk = len;
180 if (len < 0 || len > HAMMER_XBUFSIZE || len > chunk)
181 goto done;
183 pfs_id = lo_to_pfs(leaf->base.localization);
185 dict = get_dict(leaf->base.obj_id, pfs_id);
187 switch(leaf->base.rec_type) {
188 case HAMMER_RECTYPE_INODE:
190 * We found an inode which also tells us where the file
191 * or directory is in the directory hierarchy.
193 if (VerboseOpt) {
194 printf("file %016jx:%05d inode found\n",
195 (uintmax_t)leaf->base.obj_id, pfs_id);
197 path1 = recover_path(dict);
200 * Attach the inode to its parent. This isn't strictly
201 * necessary because the information is also in the
202 * directory entries, but if we do not find the directory
203 * entry this ensures that the files will still be
204 * reasonably well organized in their proper directories.
206 if ((dict->flags & DICTF_PARENT) == 0 &&
207 dict->obj_id != HAMMER_OBJID_ROOT &&
208 ondisk->inode.parent_obj_id != 0) {
209 dict->flags |= DICTF_PARENT;
210 dict->parent = get_dict(ondisk->inode.parent_obj_id,
211 pfs_id);
212 if (dict->parent &&
213 (dict->parent->flags & DICTF_MADEDIR) == 0) {
214 dict->parent->flags |= DICTF_MADEDIR;
215 path2 = recover_path(dict->parent);
216 printf("mkdir %s\n", path2);
217 mkdir(path2, 0777);
218 free(path2);
219 path2 = NULL;
222 if (dict->obj_type == 0)
223 dict->obj_type = ondisk->inode.obj_type;
224 dict->size = ondisk->inode.size;
225 path2 = recover_path(dict);
227 if (lstat(path1, &st) == 0) {
228 if (ondisk->inode.obj_type == HAMMER_OBJTYPE_REGFILE) {
229 truncate(path1, dict->size);
230 /* chmod(path1, 0666); */
232 if (strcmp(path1, path2)) {
233 printf("Rename %s -> %s\n", path1, path2);
234 rename(path1, path2);
236 } else if (ondisk->inode.obj_type == HAMMER_OBJTYPE_REGFILE) {
237 printf("mkinode (file) %s\n", path2);
238 fd = open(path2, O_RDWR|O_CREAT, 0666);
239 if (fd > 0)
240 close(fd);
241 } else if (ondisk->inode.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
242 printf("mkinode (dir) %s\n", path2);
243 mkdir(path2, 0777);
244 dict->flags |= DICTF_MADEDIR;
246 free(path1);
247 free(path2);
248 break;
249 case HAMMER_RECTYPE_DATA:
251 * File record data
253 if (leaf->base.obj_id == 0)
254 break;
255 if (VerboseOpt) {
256 printf("file %016jx:%05d data %016jx,%d\n",
257 (uintmax_t)leaf->base.obj_id,
258 pfs_id,
259 (uintmax_t)leaf->base.key - len,
260 len);
264 * Update the dictionary entry
266 if (dict->obj_type == 0)
267 dict->obj_type = HAMMER_OBJTYPE_REGFILE;
270 * If the parent directory has not been created we
271 * have to create it (typically a PFS%05d)
273 if (dict->parent &&
274 (dict->parent->flags & DICTF_MADEDIR) == 0) {
275 dict->parent->flags |= DICTF_MADEDIR;
276 path2 = recover_path(dict->parent);
277 printf("mkdir %s\n", path2);
278 mkdir(path2, 0777);
279 free(path2);
280 path2 = NULL;
284 * Create the file if necessary, report file creations
286 path1 = recover_path(dict);
287 if (CachedPath && strcmp(CachedPath, path1) == 0) {
288 fd = CachedFd;
289 } else {
290 fd = open(path1, O_CREAT|O_RDWR, 0666);
292 if (fd < 0) {
293 printf("Unable to create %s: %s\n",
294 path1, strerror(errno));
295 free(path1);
296 break;
298 if ((dict->flags & DICTF_MADEFILE) == 0) {
299 dict->flags |= DICTF_MADEFILE;
300 printf("mkfile %s\n", path1);
304 * And write the record. A HAMMER data block is aligned
305 * and may contain trailing zeros after the file EOF. The
306 * inode record is required to get the actual file size.
308 * However, when the inode record is not available
309 * we can do a sparse write and that will get it right
310 * most of the time even if the inode record is never
311 * found.
313 file_offset = (int64_t)leaf->base.key - len;
314 lseek(fd, (off_t)file_offset, SEEK_SET);
315 while (len) {
316 if (dict->size == -1) {
317 for (zfill = chunk - 1; zfill >= 0; --zfill) {
318 if (((char *)ondisk)[zfill])
319 break;
321 ++zfill;
322 } else {
323 zfill = chunk;
326 if (zfill)
327 write(fd, ondisk, zfill);
328 if (zfill < chunk)
329 lseek(fd, chunk - zfill, SEEK_CUR);
331 len -= chunk;
332 data_offset += chunk;
333 file_offset += chunk;
334 ondisk = get_buffer_data(data_offset, &data_buffer, 0);
335 if (ondisk == NULL)
336 break;
337 chunk = HAMMER_BUFSIZE -
338 ((int)data_offset & HAMMER_BUFMASK);
339 if (chunk > len)
340 chunk = len;
342 if (dict->size >= 0 && file_offset > dict->size) {
343 ftruncate(fd, dict->size);
344 /* fchmod(fd, 0666); */
347 if (fd == CachedFd) {
348 free(path1);
349 } else if (CachedPath) {
350 free(CachedPath);
351 close(CachedFd);
352 CachedPath = path1;
353 CachedFd = fd;
354 } else {
355 CachedPath = path1;
356 CachedFd = fd;
358 break;
359 case HAMMER_RECTYPE_DIRENTRY:
360 nlen = len - offsetof(struct hammer_direntry_data, name[0]);
361 if ((int)nlen < 0) /* illegal length */
362 break;
363 if (ondisk->entry.obj_id == 0 ||
364 ondisk->entry.obj_id == HAMMER_OBJID_ROOT)
365 break;
366 name = malloc(nlen + 1);
367 bcopy(ondisk->entry.name, name, nlen);
368 name[nlen] = 0;
369 sanitize_string(name);
372 * We can't deal with hardlinks so if the object already
373 * has a name assigned to it we just keep using that name.
375 dict2 = get_dict(ondisk->entry.obj_id, pfs_id);
376 path1 = recover_path(dict2);
378 if (dict2->name == NULL)
379 dict2->name = name;
380 else
381 free(name);
384 * Attach dict2 to its directory (dict), create the
385 * directory (dict) if necessary. We must ensure
386 * that the directory entry exists in order to be
387 * able to properly rename() the file without creating
388 * a namespace conflict.
390 if ((dict2->flags & DICTF_PARENT) == 0) {
391 dict2->flags |= DICTF_PARENT;
392 dict2->parent = dict;
393 if ((dict->flags & DICTF_MADEDIR) == 0) {
394 dict->flags |= DICTF_MADEDIR;
395 path2 = recover_path(dict);
396 printf("mkdir %s\n", path2);
397 mkdir(path2, 0777);
398 free(path2);
399 path2 = NULL;
402 path2 = recover_path(dict2);
403 if (strcmp(path1, path2) != 0 && lstat(path1, &st) == 0) {
404 printf("Rename %s -> %s\n", path1, path2);
405 rename(path1, path2);
407 free(path1);
408 free(path2);
410 printf("dir %016jx:%05d entry %016jx \"%s\"\n",
411 (uintmax_t)leaf->base.obj_id,
412 pfs_id,
413 (uintmax_t)ondisk->entry.obj_id,
414 name);
415 break;
416 default:
418 * Ignore any other record types
420 break;
422 done:
423 rel_buffer(data_buffer);
426 #define RD_HSIZE 32768
427 #define RD_HMASK (RD_HSIZE - 1)
429 struct recover_dict *RDHash[RD_HSIZE];
431 static
432 struct recover_dict *
433 get_dict(int64_t obj_id, uint16_t pfs_id)
435 struct recover_dict *dict;
436 int i;
438 if (obj_id == 0)
439 return(NULL);
441 i = crc32(&obj_id, sizeof(obj_id)) & RD_HMASK;
442 for (dict = RDHash[i]; dict; dict = dict->next) {
443 if (dict->obj_id == obj_id &&
444 dict->pfs_id == pfs_id) {
445 break;
448 if (dict == NULL) {
449 dict = malloc(sizeof(*dict));
450 bzero(dict, sizeof(*dict));
451 dict->obj_id = obj_id;
452 dict->pfs_id = pfs_id;
453 dict->next = RDHash[i];
454 dict->size = -1;
455 RDHash[i] = dict;
458 * Always connect dangling dictionary entries to object 1
459 * (the root of the PFS).
461 * DICTF_PARENT will not be set until we know what the
462 * real parent directory object is.
464 if (dict->obj_id != HAMMER_OBJID_ROOT)
465 dict->parent = get_dict(1, pfs_id);
467 return(dict);
470 struct path_info {
471 enum { PI_FIGURE, PI_LOAD } state;
472 uint16_t pfs_id;
473 char *base;
474 char *next;
475 int len;
478 static void recover_path_helper(struct recover_dict *, struct path_info *);
480 static
481 char *
482 recover_path(struct recover_dict *dict)
484 struct path_info info;
486 bzero(&info, sizeof(info));
487 info.pfs_id = dict->pfs_id;
488 info.state = PI_FIGURE;
489 recover_path_helper(dict, &info);
490 info.base = malloc(info.len);
491 info.next = info.base;
492 info.state = PI_LOAD;
493 recover_path_helper(dict, &info);
495 return(info.base);
498 static
499 void
500 recover_path_helper(struct recover_dict *dict, struct path_info *info)
503 * Calculate path element length
505 dict->flags |= DICTF_TRAVERSED;
507 switch(info->state) {
508 case PI_FIGURE:
509 if (dict->obj_id == HAMMER_OBJID_ROOT)
510 info->len += 8;
511 else if (dict->name)
512 info->len += strlen(dict->name);
513 else
514 info->len += 6 + 16;
515 ++info->len;
517 if (dict->parent &&
518 (dict->parent->flags & DICTF_TRAVERSED) == 0) {
519 recover_path_helper(dict->parent, info);
520 } else {
521 info->len += strlen(TargetDir) + 1;
523 break;
524 case PI_LOAD:
525 if (dict->parent &&
526 (dict->parent->flags & DICTF_TRAVERSED) == 0) {
527 recover_path_helper(dict->parent, info);
528 } else {
529 strcpy(info->next, TargetDir);
530 info->next += strlen(info->next);
533 *info->next++ = '/';
534 if (dict->obj_id == HAMMER_OBJID_ROOT) {
535 snprintf(info->next, 8+1, "PFS%05d", info->pfs_id);
536 } else if (dict->name) {
537 strcpy(info->next, dict->name);
538 } else {
539 snprintf(info->next, 6+16+1, "obj_0x%016jx",
540 (uintmax_t)dict->obj_id);
542 info->next += strlen(info->next);
543 break;
545 dict->flags &= ~DICTF_TRAVERSED;
548 static
549 void
550 sanitize_string(char *str)
552 while (*str) {
553 if (!isprint(*str))
554 *str = 'x';
555 ++str;