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
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
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
38 struct recover_dict *next;
39 struct recover_dict *parent;
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;
64 hammer_cmd_recover(const char *target_dir)
66 struct buffer_info *data_buffer;
67 struct volume_info *scan;
68 struct volume_info *volume;
74 TargetDir = target_dir;
76 printf("Running raw scan of HAMMER image, recovering to %s\n",
78 mkdir(TargetDir, 0777);
81 TAILQ_FOREACH(scan, &VolList, entry) {
82 volume = get_volume(scan->vol_no);
84 off = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no, 0);
85 off_end = off + (volume->ondisk->vol_buf_end - volume->ondisk->vol_buf_beg);
86 while (off < off_end) {
87 ptr = get_buffer_data(off, &data_buffer, 0);
90 off += HAMMER_BUFSIZE;
94 rel_buffer(data_buffer);
107 * Top level recovery processor. Assume the data is a B-Tree node.
108 * If the CRC is good we attempt to process the node, building the
109 * object space and creating the dictionary as we go.
112 recover_top(char *ptr)
114 struct hammer_node_ondisk *node;
115 hammer_btree_elm_t elm;
119 for (node = (void *)ptr; (char *)node < ptr + HAMMER_BUFSIZE; ++node) {
120 if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) ==
122 node->type == HAMMER_BTREE_TYPE_LEAF) {
126 maxcount = HAMMER_BTREE_LEAF_ELMS;
127 for (i = 0; i < node->count && i < maxcount; ++i) {
128 elm = &node->elms[i];
129 if (elm->base.btype != 'R')
131 recover_elm(&elm->leaf);
138 recover_elm(hammer_btree_leaf_elm_t leaf)
140 struct buffer_info *data_buffer = NULL;
141 struct recover_dict *dict;
142 struct recover_dict *dict2;
143 hammer_data_ondisk_t ondisk;
144 hammer_off_t data_offset;
158 * Ignore deleted records
162 if ((data_offset = leaf->data_offset) != 0)
163 ondisk = get_buffer_data(data_offset, &data_buffer, 0);
169 len = leaf->data_len;
170 chunk = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
174 if (len < 0 || len > HAMMER_XBUFSIZE || len > chunk)
177 pfs_id = lo_to_pfs(leaf->base.localization);
179 dict = get_dict(leaf->base.obj_id, pfs_id);
181 switch(leaf->base.rec_type) {
182 case HAMMER_RECTYPE_INODE:
184 * We found an inode which also tells us where the file
185 * or directory is in the directory hierarchy.
188 printf("file %016jx:%05d inode found\n",
189 (uintmax_t)leaf->base.obj_id, pfs_id);
191 path1 = recover_path(dict);
194 * Attach the inode to its parent. This isn't strictly
195 * necessary because the information is also in the
196 * directory entries, but if we do not find the directory
197 * entry this ensures that the files will still be
198 * reasonably well organized in their proper directories.
200 if ((dict->flags & DICTF_PARENT) == 0 &&
201 dict->obj_id != HAMMER_OBJID_ROOT &&
202 ondisk->inode.parent_obj_id != 0) {
203 dict->flags |= DICTF_PARENT;
204 dict->parent = get_dict(ondisk->inode.parent_obj_id,
207 (dict->parent->flags & DICTF_MADEDIR) == 0) {
208 dict->parent->flags |= DICTF_MADEDIR;
209 path2 = recover_path(dict->parent);
210 printf("mkdir %s\n", path2);
216 if (dict->obj_type == 0)
217 dict->obj_type = ondisk->inode.obj_type;
218 dict->size = ondisk->inode.size;
219 path2 = recover_path(dict);
221 if (lstat(path1, &st) == 0) {
222 if (ondisk->inode.obj_type == HAMMER_OBJTYPE_REGFILE) {
223 truncate(path1, dict->size);
224 /* chmod(path1, 0666); */
226 if (strcmp(path1, path2)) {
227 printf("Rename %s -> %s\n", path1, path2);
228 rename(path1, path2);
230 } else if (ondisk->inode.obj_type == HAMMER_OBJTYPE_REGFILE) {
231 printf("mkinode (file) %s\n", path2);
232 fd = open(path2, O_RDWR|O_CREAT, 0666);
235 } else if (ondisk->inode.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
236 printf("mkinode (dir) %s\n", path2);
238 dict->flags |= DICTF_MADEDIR;
243 case HAMMER_RECTYPE_DATA:
247 if (leaf->base.obj_id == 0)
250 printf("file %016jx:%05d data %016jx,%d\n",
251 (uintmax_t)leaf->base.obj_id,
253 (uintmax_t)leaf->base.key - len,
258 * Update the dictionary entry
260 if (dict->obj_type == 0)
261 dict->obj_type = HAMMER_OBJTYPE_REGFILE;
264 * If the parent directory has not been created we
265 * have to create it (typically a PFS%05d)
268 (dict->parent->flags & DICTF_MADEDIR) == 0) {
269 dict->parent->flags |= DICTF_MADEDIR;
270 path2 = recover_path(dict->parent);
271 printf("mkdir %s\n", path2);
278 * Create the file if necessary, report file creations
280 path1 = recover_path(dict);
281 if (CachedPath && strcmp(CachedPath, path1) == 0) {
284 fd = open(path1, O_CREAT|O_RDWR, 0666);
287 printf("Unable to create %s: %s\n",
288 path1, strerror(errno));
292 if ((dict->flags & DICTF_MADEFILE) == 0) {
293 dict->flags |= DICTF_MADEFILE;
294 printf("mkfile %s\n", path1);
298 * And write the record. A HAMMER data block is aligned
299 * and may contain trailing zeros after the file EOF. The
300 * inode record is required to get the actual file size.
302 * However, when the inode record is not available
303 * we can do a sparse write and that will get it right
304 * most of the time even if the inode record is never
307 file_offset = (int64_t)leaf->base.key - len;
308 lseek(fd, (off_t)file_offset, SEEK_SET);
310 if (dict->size == -1) {
311 for (zfill = chunk - 1; zfill >= 0; --zfill) {
312 if (((char *)ondisk)[zfill])
321 write(fd, ondisk, zfill);
323 lseek(fd, chunk - zfill, SEEK_CUR);
326 data_offset += chunk;
327 file_offset += chunk;
328 ondisk = get_buffer_data(data_offset, &data_buffer, 0);
331 chunk = HAMMER_BUFSIZE -
332 ((int)data_offset & HAMMER_BUFMASK);
336 if (dict->size >= 0 && file_offset > dict->size) {
337 ftruncate(fd, dict->size);
338 /* fchmod(fd, 0666); */
341 if (fd == CachedFd) {
343 } else if (CachedPath) {
353 case HAMMER_RECTYPE_DIRENTRY:
354 nlen = len - offsetof(struct hammer_direntry_data, name[0]);
355 if ((int)nlen < 0) /* illegal length */
357 if (ondisk->entry.obj_id == 0 ||
358 ondisk->entry.obj_id == HAMMER_OBJID_ROOT)
360 name = malloc(nlen + 1);
361 bcopy(ondisk->entry.name, name, nlen);
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, pfs_id);
370 path1 = recover_path(dict2);
372 if (dict2->name == NULL)
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);
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);
404 printf("dir %016jx:%05d entry %016jx \"%s\"\n",
405 (uintmax_t)leaf->base.obj_id,
407 (uintmax_t)ondisk->entry.obj_id,
412 * Ignore any other record types
417 rel_buffer(data_buffer);
420 #define RD_HSIZE 32768
421 #define RD_HMASK (RD_HSIZE - 1)
423 struct recover_dict *RDHash[RD_HSIZE];
426 struct recover_dict *
427 get_dict(int64_t obj_id, uint16_t pfs_id)
429 struct recover_dict *dict;
435 i = crc32(&obj_id, sizeof(obj_id)) & RD_HMASK;
436 for (dict = RDHash[i]; dict; dict = dict->next) {
437 if (dict->obj_id == obj_id &&
438 dict->pfs_id == pfs_id) {
443 dict = malloc(sizeof(*dict));
444 bzero(dict, sizeof(*dict));
445 dict->obj_id = obj_id;
446 dict->pfs_id = pfs_id;
447 dict->next = RDHash[i];
452 * Always connect dangling dictionary entries to object 1
453 * (the root of the PFS).
455 * DICTF_PARENT will not be set until we know what the
456 * real parent directory object is.
458 if (dict->obj_id != HAMMER_OBJID_ROOT)
459 dict->parent = get_dict(1, pfs_id);
465 enum { PI_FIGURE, PI_LOAD } state;
472 static void recover_path_helper(struct recover_dict *, struct path_info *);
476 recover_path(struct recover_dict *dict)
478 struct path_info info;
480 bzero(&info, sizeof(info));
481 info.pfs_id = dict->pfs_id;
482 info.state = PI_FIGURE;
483 recover_path_helper(dict, &info);
484 info.base = malloc(info.len);
485 info.next = info.base;
486 info.state = PI_LOAD;
487 recover_path_helper(dict, &info);
494 recover_path_helper(struct recover_dict *dict, struct path_info *info)
497 * Calculate path element length
499 dict->flags |= DICTF_TRAVERSED;
501 switch(info->state) {
503 if (dict->obj_id == HAMMER_OBJID_ROOT)
506 info->len += strlen(dict->name);
512 (dict->parent->flags & DICTF_TRAVERSED) == 0) {
513 recover_path_helper(dict->parent, info);
515 info->len += strlen(TargetDir) + 1;
520 (dict->parent->flags & DICTF_TRAVERSED) == 0) {
521 recover_path_helper(dict->parent, info);
523 strcpy(info->next, TargetDir);
524 info->next += strlen(info->next);
528 if (dict->obj_id == HAMMER_OBJID_ROOT) {
529 snprintf(info->next, 8+1, "PFS%05d", info->pfs_id);
530 } else if (dict->name) {
531 strcpy(info->next, dict->name);
533 snprintf(info->next, 6+16+1, "obj_0x%016jx",
534 (uintmax_t)dict->obj_id);
536 info->next += strlen(info->next);
539 dict->flags &= ~DICTF_TRAVERSED;
544 sanitize_string(char *str)