xref: /dragonfly/sbin/hammer/cmd_recover.c (revision 78478697)
1 /*
2  * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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.
20  *
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.
33  */
34 
35 #include "hammer.h"
36 
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;
46 };
47 
48 #define DICTF_MADEDIR	0x01
49 #define DICTF_MADEFILE	0x02
50 #define DICTF_PARENT	0x04	/* parent attached for real */
51 #define DICTF_TRAVERSED	0x80
52 
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);
58 
59 static const char *TargetDir;
60 static int CachedFd = -1;
61 static char *CachedPath;
62 
63 void
64 hammer_cmd_recover(const char *target_dir)
65 {
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;
72 
73 	AssertOnFailure = 0;
74 	TargetDir = target_dir;
75 
76 	printf("Running raw scan of HAMMER image, recovering to %s\n",
77 		TargetDir);
78 	mkdir(TargetDir, 0777);
79 
80 	data_buffer = NULL;
81 	TAILQ_FOREACH(scan, &VolList, entry) {
82 		volume = get_volume(scan->vol_no);
83 
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);
88 			if (ptr) {
89 				recover_top(ptr);
90 				off += HAMMER_BUFSIZE;
91 			}
92 		}
93 	}
94 	rel_buffer(data_buffer);
95 
96 	if (CachedPath) {
97 		free(CachedPath);
98 		close(CachedFd);
99 		CachedPath = NULL;
100 		CachedFd = -1;
101 	}
102 
103 	AssertOnFailure = 1;
104 }
105 
106 /*
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.
110  */
111 static void
112 recover_top(char *ptr)
113 {
114 	struct hammer_node_ondisk *node;
115 	hammer_btree_elm_t elm;
116 	int maxcount;
117 	int i;
118 
119 	for (node = (void *)ptr; (char *)node < ptr + HAMMER_BUFSIZE; ++node) {
120 		if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) ==
121 		    node->crc &&
122 		    node->type == HAMMER_BTREE_TYPE_LEAF) {
123 			/*
124 			 * Scan elements
125 			 */
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')
130 					continue;
131 				recover_elm(&elm->leaf);
132 			}
133 		}
134 	}
135 }
136 
137 static void
138 recover_elm(hammer_btree_leaf_elm_t leaf)
139 {
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;
145 	struct stat st;
146 	int chunk;
147 	int len;
148 	int zfill;
149 	int64_t file_offset;
150 	uint16_t llid;
151 	size_t nlen;
152 	int fd;
153 	char *name;
154 	char *path1;
155 	char *path2;
156 
157 	/*
158 	 * Ignore deleted records
159 	 */
160 	if (leaf->delete_ts)
161 		return;
162 	if ((data_offset = leaf->data_offset) != 0)
163 		ondisk = get_buffer_data(data_offset, &data_buffer, 0);
164 	else
165 		ondisk = NULL;
166 	if (ondisk == NULL)
167 		goto done;
168 
169 	len = leaf->data_len;
170 	chunk = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
171 	if (chunk > len)
172 		chunk = len;
173 
174 	if (len < 0 || len > HAMMER_XBUFSIZE || len > chunk)
175 		goto done;
176 
177 	llid = lo_to_pfs(leaf->base.localization);
178 
179 	dict = get_dict(leaf->base.obj_id, llid);
180 
181 	switch(leaf->base.rec_type) {
182 	case HAMMER_RECTYPE_INODE:
183 		/*
184 		 * We found an inode which also tells us where the file
185 		 * or directory is in the directory hierarchy.
186 		 */
187 		if (VerboseOpt) {
188 			printf("file %016jx:%05d inode found\n",
189 				(uintmax_t)leaf->base.obj_id, llid);
190 		}
191 		path1 = recover_path(dict);
192 
193 		/*
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.
199 		 */
200 		if ((dict->flags & DICTF_PARENT) == 0 &&
201 		    dict->obj_id != 1 && ondisk->inode.parent_obj_id != 0) {
202 			dict->flags |= DICTF_PARENT;
203 			dict->parent = get_dict(ondisk->inode.parent_obj_id,
204 						llid);
205 			if (dict->parent &&
206 			    (dict->parent->flags & DICTF_MADEDIR) == 0) {
207 				dict->parent->flags |= DICTF_MADEDIR;
208 				path2 = recover_path(dict->parent);
209 				printf("mkdir %s\n", path2);
210 				mkdir(path2, 0777);
211 				free(path2);
212 				path2 = NULL;
213 			}
214 		}
215 		if (dict->obj_type == 0)
216 			dict->obj_type = ondisk->inode.obj_type;
217 		dict->size = ondisk->inode.size;
218 		path2 = recover_path(dict);
219 
220 		if (lstat(path1, &st) == 0) {
221 			if (ondisk->inode.obj_type == HAMMER_OBJTYPE_REGFILE) {
222 				truncate(path1, dict->size);
223 				/* chmod(path1, 0666); */
224 			}
225 			if (strcmp(path1, path2)) {
226 				printf("Rename %s -> %s\n", path1, path2);
227 				rename(path1, path2);
228 			}
229 		} else if (ondisk->inode.obj_type == HAMMER_OBJTYPE_REGFILE) {
230 			printf("mkinode (file) %s\n", path2);
231 			fd = open(path2, O_RDWR|O_CREAT, 0666);
232 			if (fd > 0)
233 				close(fd);
234 		} else if (ondisk->inode.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
235 			printf("mkinode (dir) %s\n", path2);
236 			mkdir(path2, 0777);
237 			dict->flags |= DICTF_MADEDIR;
238 		}
239 		free(path1);
240 		free(path2);
241 		break;
242 	case HAMMER_RECTYPE_DATA:
243 		/*
244 		 * File record data
245 		 */
246 		if (leaf->base.obj_id == 0)
247 			break;
248 		if (VerboseOpt) {
249 			printf("file %016jx:%05d data %016jx,%d\n",
250 				(uintmax_t)leaf->base.obj_id,
251 				llid,
252 				(uintmax_t)leaf->base.key - len,
253 				len);
254 		}
255 
256 		/*
257 		 * Update the dictionary entry
258 		 */
259 		if (dict->obj_type == 0)
260 			dict->obj_type = HAMMER_OBJTYPE_REGFILE;
261 
262 		/*
263 		 * If the parent directory has not been created we
264 		 * have to create it (typically a PFS%05d)
265 		 */
266 		if (dict->parent &&
267 		    (dict->parent->flags & DICTF_MADEDIR) == 0) {
268 			dict->parent->flags |= DICTF_MADEDIR;
269 			path2 = recover_path(dict->parent);
270 			printf("mkdir %s\n", path2);
271 			mkdir(path2, 0777);
272 			free(path2);
273 			path2 = NULL;
274 		}
275 
276 		/*
277 		 * Create the file if necessary, report file creations
278 		 */
279 		path1 = recover_path(dict);
280 		if (CachedPath && strcmp(CachedPath, path1) == 0) {
281 			fd = CachedFd;
282 		} else {
283 			fd = open(path1, O_CREAT|O_RDWR, 0666);
284 		}
285 		if (fd < 0) {
286 			printf("Unable to create %s: %s\n",
287 				path1, strerror(errno));
288 			free(path1);
289 			break;
290 		}
291 		if ((dict->flags & DICTF_MADEFILE) == 0) {
292 			dict->flags |= DICTF_MADEFILE;
293 			printf("mkfile %s\n", path1);
294 		}
295 
296 		/*
297 		 * And write the record.  A HAMMER data block is aligned
298 		 * and may contain trailing zeros after the file EOF.  The
299 		 * inode record is required to get the actual file size.
300 		 *
301 		 * However, when the inode record is not available
302 		 * we can do a sparse write and that will get it right
303 		 * most of the time even if the inode record is never
304 		 * found.
305 		 */
306 		file_offset = (int64_t)leaf->base.key - len;
307 		lseek(fd, (off_t)file_offset, SEEK_SET);
308 		while (len) {
309 			if (dict->size == -1) {
310 				for (zfill = chunk - 1; zfill >= 0; --zfill) {
311 					if (((char *)ondisk)[zfill])
312 						break;
313 				}
314 				++zfill;
315 			} else {
316 				zfill = chunk;
317 			}
318 
319 			if (zfill)
320 				write(fd, ondisk, zfill);
321 			if (zfill < chunk)
322 				lseek(fd, chunk - zfill, SEEK_CUR);
323 
324 			len -= chunk;
325 			data_offset += chunk;
326 			file_offset += chunk;
327 			ondisk = get_buffer_data(data_offset, &data_buffer, 0);
328 			if (ondisk == NULL)
329 				break;
330 			chunk = HAMMER_BUFSIZE -
331 				((int)data_offset & HAMMER_BUFMASK);
332 			if (chunk > len)
333 				chunk = len;
334 		}
335 		if (dict->size >= 0 && file_offset > dict->size) {
336 			ftruncate(fd, dict->size);
337 			/* fchmod(fd, 0666); */
338 		}
339 
340 		if (fd == CachedFd) {
341 			free(path1);
342 		} else if (CachedPath) {
343 			free(CachedPath);
344 			close(CachedFd);
345 			CachedPath = path1;
346 			CachedFd = fd;
347 		} else {
348 			CachedPath = path1;
349 			CachedFd = fd;
350 		}
351 		break;
352 	case HAMMER_RECTYPE_DIRENTRY:
353 		nlen = len - offsetof(struct hammer_entry_data, name[0]);
354 		if ((int)nlen < 0)	/* illegal length */
355 			break;
356 		if (ondisk->entry.obj_id == 0 || ondisk->entry.obj_id == 1)
357 			break;
358 		name = malloc(nlen + 1);
359 		bcopy(ondisk->entry.name, name, nlen);
360 		name[nlen] = 0;
361 		sanitize_string(name);
362 
363 		/*
364 		 * We can't deal with hardlinks so if the object already
365 		 * has a name assigned to it we just keep using that name.
366 		 */
367 		dict2 = get_dict(ondisk->entry.obj_id, llid);
368 		path1 = recover_path(dict2);
369 
370 		if (dict2->name == NULL)
371 			dict2->name = name;
372 		else
373 			free(name);
374 
375 		/*
376 		 * Attach dict2 to its directory (dict), create the
377 		 * directory (dict) if necessary.  We must ensure
378 		 * that the directory entry exists in order to be
379 		 * able to properly rename() the file without creating
380 		 * a namespace conflict.
381 		 */
382 		if ((dict2->flags & DICTF_PARENT) == 0) {
383 			dict2->flags |= DICTF_PARENT;
384 			dict2->parent = dict;
385 			if ((dict->flags & DICTF_MADEDIR) == 0) {
386 				dict->flags |= DICTF_MADEDIR;
387 				path2 = recover_path(dict);
388 				printf("mkdir %s\n", path2);
389 				mkdir(path2, 0777);
390 				free(path2);
391 				path2 = NULL;
392 			}
393 		}
394 		path2 = recover_path(dict2);
395 		if (strcmp(path1, path2) != 0 && lstat(path1, &st) == 0) {
396 			printf("Rename %s -> %s\n", path1, path2);
397 			rename(path1, path2);
398 		}
399 		free(path1);
400 		free(path2);
401 
402 		printf("dir  %016jx:%05d entry %016jx \"%s\"\n",
403 			(uintmax_t)leaf->base.obj_id,
404 			llid,
405 			(uintmax_t)ondisk->entry.obj_id,
406 			name);
407 		break;
408 	default:
409 		/*
410 		 * Ignore any other record types
411 		 */
412 		break;
413 	}
414 done:
415 	rel_buffer(data_buffer);
416 }
417 
418 #define RD_HSIZE	32768
419 #define RD_HMASK	(RD_HSIZE - 1)
420 
421 struct recover_dict *RDHash[RD_HSIZE];
422 
423 static
424 struct recover_dict *
425 get_dict(int64_t obj_id, uint16_t llid)
426 {
427 	struct recover_dict *dict;
428 	int i;
429 
430 	if (obj_id == 0)
431 		return(NULL);
432 
433 	i = crc32(&obj_id, sizeof(obj_id)) & RD_HMASK;
434 	for (dict = RDHash[i]; dict; dict = dict->next) {
435 		if (dict->obj_id == obj_id &&
436 		    dict->llid == llid) {
437 			break;
438 		}
439 	}
440 	if (dict == NULL) {
441 		dict = malloc(sizeof(*dict));
442 		bzero(dict, sizeof(*dict));
443 		dict->obj_id = obj_id;
444 		dict->llid = llid;
445 		dict->next = RDHash[i];
446 		dict->size = -1;
447 		RDHash[i] = dict;
448 
449 		/*
450 		 * Always connect dangling dictionary entries to object 1
451 		 * (the root of the PFS).
452 		 *
453 		 * DICTF_PARENT will not be set until we know what the
454 		 * real parent directory object is.
455 		 */
456 		if (dict->obj_id != 1)
457 			dict->parent = get_dict(1, llid);
458 	}
459 	return(dict);
460 }
461 
462 struct path_info {
463 	enum { PI_FIGURE, PI_LOAD } state;
464 	uint16_t llid;
465 	char *base;
466 	char *next;
467 	int len;
468 };
469 
470 static void recover_path_helper(struct recover_dict *, struct path_info *);
471 
472 static
473 char *
474 recover_path(struct recover_dict *dict)
475 {
476 	struct path_info info;
477 
478 	bzero(&info, sizeof(info));
479 	info.llid = dict->llid;
480 	info.state = PI_FIGURE;
481 	recover_path_helper(dict, &info);
482 	info.base = malloc(info.len);
483 	info.next = info.base;
484 	info.state = PI_LOAD;
485 	recover_path_helper(dict, &info);
486 
487 	return(info.base);
488 }
489 
490 static
491 void
492 recover_path_helper(struct recover_dict *dict, struct path_info *info)
493 {
494 	/*
495 	 * Calculate path element length
496 	 */
497 	dict->flags |= DICTF_TRAVERSED;
498 
499 	switch(info->state) {
500 	case PI_FIGURE:
501 		if (dict->obj_id == 1)
502 			info->len += 8;
503 		else if (dict->name)
504 			info->len += strlen(dict->name);
505 		else
506 			info->len += 6 + 16;
507 		++info->len;
508 
509 		if (dict->parent &&
510 		    (dict->parent->flags & DICTF_TRAVERSED) == 0) {
511 			recover_path_helper(dict->parent, info);
512 		} else {
513 			info->len += strlen(TargetDir) + 1;
514 		}
515 		break;
516 	case PI_LOAD:
517 		if (dict->parent &&
518 		    (dict->parent->flags & DICTF_TRAVERSED) == 0) {
519 			recover_path_helper(dict->parent, info);
520 		} else {
521 			strcpy(info->next, TargetDir);
522 			info->next += strlen(info->next);
523 		}
524 
525 		*info->next++ = '/';
526 		if (dict->obj_id == 1) {
527 			snprintf(info->next, 8+1, "PFS%05d", info->llid);
528 		} else if (dict->name) {
529 			strcpy(info->next, dict->name);
530 		} else {
531 			snprintf(info->next, 6+16+1, "obj_0x%016jx",
532 				(uintmax_t)dict->obj_id);
533 		}
534 		info->next += strlen(info->next);
535 		break;
536 	}
537 	dict->flags &= ~DICTF_TRAVERSED;
538 }
539 
540 static
541 void
542 sanitize_string(char *str)
543 {
544 	while (*str) {
545 		if (!isprint(*str))
546 			*str = 'x';
547 		++str;
548 	}
549 }
550