dhclient - Drop medium/alias useless utilization.
[dragonfly.git] / sbin / hammer / cmd_recover.c
blob43b6a4558850ba32272674a7bd59dd3a9c78e9e5
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 llid;
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 llid);
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 *scan;
68 struct volume_info *volume;
69 hammer_off_t off;
70 hammer_off_t off_end;
71 char *ptr;
73 AssertOnFailure = 0;
74 TargetDir = target_dir;
76 printf("Running raw scan of HAMMER image, recovering to %s\n",
77 TargetDir);
78 mkdir(TargetDir, 0777);
80 data_buffer = NULL;
81 TAILQ_FOREACH(scan, &VolList, entry) {
82 volume = get_volume(scan->vol_no);
84 off = HAMMER_ZONE_RAW_BUFFER + 0;
85 off |= HAMMER_VOL_ENCODE(volume->vol_no);
86 off_end = off + (volume->ondisk->vol_buf_end - volume->ondisk->vol_buf_beg);
87 while (off < off_end) {
88 ptr = get_buffer_data(off, &data_buffer, 0);
89 if (ptr) {
90 recover_top(ptr);
91 off += HAMMER_BUFSIZE;
95 if (data_buffer)
96 rel_buffer(data_buffer);
98 if (CachedPath) {
99 free(CachedPath);
100 close(CachedFd);
101 CachedPath = NULL;
102 CachedFd = -1;
105 AssertOnFailure = 1;
109 * Top level recovery processor. Assume the data is a B-Tree node.
110 * If the CRC is good we attempt to process the node, building the
111 * object space and creating the dictionary as we go.
113 static void
114 recover_top(char *ptr)
116 struct hammer_node_ondisk *node;
117 hammer_btree_elm_t elm;
118 int maxcount;
119 int i;
121 for (node = (void *)ptr; (char *)node < ptr + HAMMER_BUFSIZE; ++node) {
122 if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) ==
123 node->crc &&
124 node->type == HAMMER_BTREE_TYPE_LEAF) {
126 * Scan elements
128 maxcount = HAMMER_BTREE_LEAF_ELMS;
129 for (i = 0; i < node->count && i < maxcount; ++i) {
130 elm = &node->elms[i];
131 if (elm->base.btype != 'R')
132 continue;
133 recover_elm(&elm->leaf);
139 static void
140 recover_elm(hammer_btree_leaf_elm_t leaf)
142 struct buffer_info *data_buffer = NULL;
143 struct recover_dict *dict;
144 struct recover_dict *dict2;
145 hammer_data_ondisk_t ondisk;
146 hammer_off_t data_offset;
147 struct stat st;
148 int chunk;
149 int len;
150 int zfill;
151 int64_t file_offset;
152 uint16_t llid;
153 size_t nlen;
154 int fd;
155 char *name;
156 char *path1;
157 char *path2;
160 * Ignore deleted records
162 if (leaf->delete_ts)
163 return;
164 if ((data_offset = leaf->data_offset) != 0)
165 ondisk = get_buffer_data(data_offset, &data_buffer, 0);
166 else
167 ondisk = NULL;
168 if (ondisk == NULL)
169 goto done;
171 len = leaf->data_len;
172 chunk = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
173 if (chunk > len)
174 chunk = len;
176 if (len < 0 || len > HAMMER_XBUFSIZE || len > chunk)
177 goto done;
179 llid = leaf->base.localization >> 16;
181 dict = get_dict(leaf->base.obj_id, llid);
183 switch(leaf->base.rec_type) {
184 case HAMMER_RECTYPE_INODE:
186 * We found an inode which also tells us where the file
187 * or directory is in the directory hierarchy.
189 if (VerboseOpt) {
190 printf("file %016jx:%05d inode found\n",
191 (uintmax_t)leaf->base.obj_id, llid);
193 path1 = recover_path(dict);
196 * Attach the inode to its parent. This isn't strictly
197 * necessary because the information is also in the
198 * directory entries, but if we do not find the directory
199 * entry this ensures that the files will still be
200 * reasonably well organized in their proper directories.
202 if ((dict->flags & DICTF_PARENT) == 0 &&
203 dict->obj_id != 1 && ondisk->inode.parent_obj_id != 0) {
204 dict->flags |= DICTF_PARENT;
205 dict->parent = get_dict(ondisk->inode.parent_obj_id,
206 llid);
207 if (dict->parent &&
208 (dict->parent->flags & DICTF_MADEDIR) == 0) {
209 dict->parent->flags |= DICTF_MADEDIR;
210 path2 = recover_path(dict->parent);
211 printf("mkdir %s\n", path2);
212 mkdir(path2, 0777);
213 free(path2);
214 path2 = NULL;
217 if (dict->obj_type == 0)
218 dict->obj_type = ondisk->inode.obj_type;
219 dict->size = ondisk->inode.size;
220 path2 = recover_path(dict);
222 if (lstat(path1, &st) == 0) {
223 if (ondisk->inode.obj_type == HAMMER_OBJTYPE_REGFILE) {
224 truncate(path1, dict->size);
225 /* chmod(path1, 0666); */
227 if (strcmp(path1, path2)) {
228 printf("Rename %s -> %s\n", path1, path2);
229 rename(path1, path2);
231 } else if (ondisk->inode.obj_type == HAMMER_OBJTYPE_REGFILE) {
232 printf("mkinode (file) %s\n", path2);
233 fd = open(path2, O_RDWR|O_CREAT, 0666);
234 if (fd > 0)
235 close(fd);
236 } else if (ondisk->inode.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
237 printf("mkinode (dir) %s\n", path2);
238 mkdir(path2, 0777);
239 dict->flags |= DICTF_MADEDIR;
241 free(path1);
242 free(path2);
243 break;
244 case HAMMER_RECTYPE_DATA:
246 * File record data
248 if (leaf->base.obj_id == 0)
249 break;
250 if (VerboseOpt) {
251 printf("file %016jx:%05d data %016jx,%d\n",
252 (uintmax_t)leaf->base.obj_id,
253 llid,
254 (uintmax_t)leaf->base.key - len,
255 len);
259 * Update the dictionary entry
261 if (dict->obj_type == 0)
262 dict->obj_type = HAMMER_OBJTYPE_REGFILE;
265 * If the parent directory has not been created we
266 * have to create it (typically a PFS%05d)
268 if (dict->parent &&
269 (dict->parent->flags & DICTF_MADEDIR) == 0) {
270 dict->parent->flags |= DICTF_MADEDIR;
271 path2 = recover_path(dict->parent);
272 printf("mkdir %s\n", path2);
273 mkdir(path2, 0777);
274 free(path2);
275 path2 = NULL;
279 * Create the file if necessary, report file creations
281 path1 = recover_path(dict);
282 if (CachedPath && strcmp(CachedPath, path1) == 0) {
283 fd = CachedFd;
284 } else {
285 fd = open(path1, O_CREAT|O_RDWR, 0666);
287 if (fd < 0) {
288 printf("Unable to create %s: %s\n",
289 path1, strerror(errno));
290 free(path1);
291 break;
293 if ((dict->flags & DICTF_MADEFILE) == 0) {
294 dict->flags |= DICTF_MADEFILE;
295 printf("mkfile %s\n", path1);
299 * And write the record. A HAMMER data block is aligned
300 * and may contain trailing zeros after the file EOF. The
301 * inode record is required to get the actual file size.
303 * However, when the inode record is not available
304 * we can do a sparse write and that will get it right
305 * most of the time even if the inode record is never
306 * found.
308 file_offset = (int64_t)leaf->base.key - len;
309 lseek(fd, (off_t)file_offset, SEEK_SET);
310 while (len) {
311 if (dict->size == -1) {
312 for (zfill = chunk - 1; zfill >= 0; --zfill) {
313 if (((char *)ondisk)[zfill])
314 break;
316 ++zfill;
317 } else {
318 zfill = chunk;
321 if (zfill)
322 write(fd, ondisk, zfill);
323 if (zfill < chunk)
324 lseek(fd, chunk - zfill, SEEK_CUR);
326 len -= chunk;
327 data_offset += chunk;
328 file_offset += chunk;
329 ondisk = get_buffer_data(data_offset, &data_buffer, 0);
330 if (ondisk == NULL)
331 break;
332 chunk = HAMMER_BUFSIZE -
333 ((int)data_offset & HAMMER_BUFMASK);
334 if (chunk > len)
335 chunk = len;
337 if (dict->size >= 0 && file_offset > dict->size) {
338 ftruncate(fd, dict->size);
339 /* fchmod(fd, 0666); */
342 if (fd == CachedFd) {
343 free(path1);
344 } else if (CachedPath) {
345 free(CachedPath);
346 close(CachedFd);
347 CachedPath = path1;
348 CachedFd = fd;
349 } else {
350 CachedPath = path1;
351 CachedFd = fd;
353 break;
354 case HAMMER_RECTYPE_DIRENTRY:
355 nlen = len - offsetof(struct hammer_entry_data, name[0]);
356 if ((int)nlen < 0) /* illegal length */
357 break;
358 if (ondisk->entry.obj_id == 0 || ondisk->entry.obj_id == 1)
359 break;
360 name = malloc(nlen + 1);
361 bcopy(ondisk->entry.name, name, nlen);
362 name[nlen] = 0;
363 sanitize_string(name);
366 * We can't deal with hardlinks so if the object already
367 * has a name assigned to it we just keep using that name.
369 dict2 = get_dict(ondisk->entry.obj_id, llid);
370 path1 = recover_path(dict2);
372 if (dict2->name == NULL)
373 dict2->name = name;
374 else
375 free(name);
378 * Attach dict2 to its directory (dict), create the
379 * directory (dict) if necessary. We must ensure
380 * that the directory entry exists in order to be
381 * able to properly rename() the file without creating
382 * a namespace conflict.
384 if ((dict2->flags & DICTF_PARENT) == 0) {
385 dict2->flags |= DICTF_PARENT;
386 dict2->parent = dict;
387 if ((dict->flags & DICTF_MADEDIR) == 0) {
388 dict->flags |= DICTF_MADEDIR;
389 path2 = recover_path(dict);
390 printf("mkdir %s\n", path2);
391 mkdir(path2, 0777);
392 free(path2);
393 path2 = NULL;
396 path2 = recover_path(dict2);
397 if (strcmp(path1, path2) != 0 && lstat(path1, &st) == 0) {
398 printf("Rename %s -> %s\n", path1, path2);
399 rename(path1, path2);
401 free(path1);
402 free(path2);
404 printf("dir %016jx:%05d entry %016jx \"%s\"\n",
405 (uintmax_t)leaf->base.obj_id,
406 llid,
407 (uintmax_t)ondisk->entry.obj_id,
408 name);
409 break;
410 default:
412 * Ignore any other record types
414 break;
416 done:
417 if (data_buffer)
418 rel_buffer(data_buffer);
421 #define RD_HSIZE 32768
422 #define RD_HMASK (RD_HSIZE - 1)
424 struct recover_dict *RDHash[RD_HSIZE];
426 static
427 struct recover_dict *
428 get_dict(int64_t obj_id, uint16_t llid)
430 struct recover_dict *dict;
431 int i;
433 if (obj_id == 0)
434 return(NULL);
436 i = crc32(&obj_id, sizeof(obj_id)) & RD_HMASK;
437 for (dict = RDHash[i]; dict; dict = dict->next) {
438 if (dict->obj_id == obj_id &&
439 dict->llid == llid) {
440 break;
443 if (dict == NULL) {
444 dict = malloc(sizeof(*dict));
445 bzero(dict, sizeof(*dict));
446 dict->obj_id = obj_id;
447 dict->llid = llid;
448 dict->next = RDHash[i];
449 dict->size = -1;
450 RDHash[i] = dict;
453 * Always connect dangling dictionary entries to object 1
454 * (the root of the PFS).
456 * DICTF_PARENT will not be set until we know what the
457 * real parent directory object is.
459 if (dict->obj_id != 1)
460 dict->parent = get_dict(1, llid);
462 return(dict);
465 struct path_info {
466 enum { PI_FIGURE, PI_LOAD } state;
467 uint16_t llid;
468 char *base;
469 char *next;
470 int len;
473 static void recover_path_helper(struct recover_dict *, struct path_info *);
475 static
476 char *
477 recover_path(struct recover_dict *dict)
479 struct path_info info;
481 bzero(&info, sizeof(info));
482 info.llid = dict->llid;
483 info.state = PI_FIGURE;
484 recover_path_helper(dict, &info);
485 info.base = malloc(info.len);
486 info.next = info.base;
487 info.state = PI_LOAD;
488 recover_path_helper(dict, &info);
490 return(info.base);
493 static
494 void
495 recover_path_helper(struct recover_dict *dict, struct path_info *info)
498 * Calculate path element length
500 dict->flags |= DICTF_TRAVERSED;
502 switch(info->state) {
503 case PI_FIGURE:
504 if (dict->obj_id == 1)
505 info->len += 8;
506 else if (dict->name)
507 info->len += strlen(dict->name);
508 else
509 info->len += 6 + 16;
510 ++info->len;
512 if (dict->parent &&
513 (dict->parent->flags & DICTF_TRAVERSED) == 0) {
514 recover_path_helper(dict->parent, info);
515 } else {
516 info->len += strlen(TargetDir) + 1;
518 break;
519 case PI_LOAD:
520 if (dict->parent &&
521 (dict->parent->flags & DICTF_TRAVERSED) == 0) {
522 recover_path_helper(dict->parent, info);
523 } else {
524 strcpy(info->next, TargetDir);
525 info->next += strlen(info->next);
528 *info->next++ = '/';
529 if (dict->obj_id == 1) {
530 snprintf(info->next, 8+1, "PFS%05d", info->llid);
531 } else if (dict->name) {
532 strcpy(info->next, dict->name);
533 } else {
534 snprintf(info->next, 6+16+1, "obj_0x%016jx",
535 (uintmax_t)dict->obj_id);
537 info->next += strlen(info->next);
538 break;
540 dict->flags &= ~DICTF_TRAVERSED;
543 static
544 void
545 sanitize_string(char *str)
547 while (*str) {
548 if (!isprint(*str))
549 *str = 'x';
550 ++str;