kernel - pmap (mainly i386) - fix TLB race and other issues
[dragonfly.git] / usr.bin / undo / undo.c
blobcf78daa2d33f3551f46bac3ac542635c61b2166c
1 /*
2 * Copyright (c) 2008 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:
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.
34 * $DragonFly: src/usr.bin/undo/undo.c,v 1.6 2008/07/17 21:34:47 thomas Exp $
37 * UNDO - retrieve an older version of a file.
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/wait.h>
43 #include <sys/tree.h>
44 #include <sys/param.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <stdarg.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <errno.h>
52 #include <vfs/hammer/hammer_disk.h>
53 #include <vfs/hammer/hammer_ioctl.h>
56 * Sorted list of transaction ids
58 struct undo_hist_entry;
59 RB_HEAD(undo_hist_entry_rb_tree, undo_hist_entry);
60 RB_PROTOTYPE2(undo_hist_entry_rb_tree, undo_hist_entry, rbnode,
61 undo_hist_entry_compare, hammer_tid_t);
63 struct undo_hist_entry {
64 RB_ENTRY(undo_hist_entry) rbnode;
65 struct hammer_ioc_hist_entry tse;
66 ino_t inum;
69 enum undo_type { TYPE_FILE, TYPE_DIFF, TYPE_RDIFF, TYPE_HISTORY };
70 enum undo_cmd { CMD_DUMP, CMD_ITERATEALL };
72 #define UNDO_FLAG_MULT 0x0001
73 #define UNDO_FLAG_INOCHG 0x0002
74 #define UNDO_FLAG_SETTID1 0x0004
75 #define UNDO_FLAG_SETTID2 0x0008
77 static int undo_hist_entry_compare(struct undo_hist_entry *he1,
78 struct undo_hist_entry *he2);
79 static void doiterate(const char *filename, const char *outFileName,
80 const char *outFilePostfix, int flags,
81 struct hammer_ioc_hist_entry ts1,
82 struct hammer_ioc_hist_entry ts2,
83 enum undo_cmd cmd, enum undo_type type);
84 static void dogenerate(const char *filename, const char *outFileName,
85 const char *outFilePostfix,
86 int flags, int idx, enum undo_type type,
87 struct hammer_ioc_hist_entry ts1,
88 struct hammer_ioc_hist_entry ts2);
89 static void collect_history(int fd, int *error,
90 struct undo_hist_entry_rb_tree *tse_tree);
91 static void collect_dir_history(const char *filename, int *error,
92 struct undo_hist_entry_rb_tree *dir_tree);
93 static void clean_tree(struct undo_hist_entry_rb_tree *tree);
94 static hammer_tid_t parse_delta_time(const char *timeStr, int *flags,
95 int ind_flag);
96 static void runcmd(int fd, const char *cmd, ...);
97 static char *timestamp(hammer_ioc_hist_entry_t hen);
98 static void usage(void);
100 static int VerboseOpt;
102 RB_GENERATE2(undo_hist_entry_rb_tree, undo_hist_entry, rbnode,
103 undo_hist_entry_compare, hammer_tid_t, tse.tid);
107 main(int ac, char **av)
109 const char *outFileName = NULL;
110 const char *outFilePostfix = NULL;
111 enum undo_cmd cmd;
112 enum undo_type type;
113 struct hammer_ioc_hist_entry ts1;
114 struct hammer_ioc_hist_entry ts2;
115 int c;
116 int count_t;
117 int flags;
119 bzero(&ts1, sizeof(ts1));
120 bzero(&ts2, sizeof(ts2));
122 cmd = CMD_DUMP;
123 type = TYPE_FILE;
124 count_t = 0;
125 flags = 0;
127 while ((c = getopt(ac, av, "adDiuvo:t:")) != -1) {
128 switch(c) {
129 case 'd':
130 type = TYPE_DIFF;
131 break;
132 case 'D':
133 type = TYPE_RDIFF;
134 break;
135 case 'i':
136 if (type != TYPE_FILE)
137 usage();
138 type = TYPE_HISTORY;
139 cmd = CMD_ITERATEALL;
140 break;
141 case 'a':
142 cmd = CMD_ITERATEALL;
143 break;
144 case 'u':
145 outFilePostfix = ".undo";
146 break;
147 case 'v':
148 ++VerboseOpt;
149 break;
150 case 'o':
151 outFileName = optarg;
152 break;
153 case 't':
155 * Parse one or two -t options. If two are specified
156 * -d is implied (but may be overridden)
158 ++count_t;
159 if (count_t == 1) {
160 ts1.tid = parse_delta_time(optarg, &flags,
161 UNDO_FLAG_SETTID1);
162 } else if (count_t == 2) {
163 ts2.tid = parse_delta_time(optarg, &flags,
164 UNDO_FLAG_SETTID2);
165 if (type == TYPE_FILE)
166 type = TYPE_DIFF;
167 } else {
168 usage();
170 break;
171 default:
172 usage();
173 /* NOT REACHED */
174 break;
179 * Option validation
181 if (outFileName && outFilePostfix) {
182 fprintf(stderr, "The -o option may not be combined with -u\n");
183 usage();
186 ac -= optind;
187 av += optind;
188 if (ac > 1)
189 flags |= UNDO_FLAG_MULT;
191 if (ac == 0)
192 usage();
195 * Validate the output template, if specified.
197 if (outFileName && (flags & UNDO_FLAG_MULT)) {
198 const char *ptr = outFileName;
199 int didStr = 0;
201 while ((ptr = strchr(ptr, '%')) != NULL) {
202 if (ptr[1] == 's') {
203 if (didStr) {
204 fprintf(stderr, "Malformed output "
205 "template\n");
206 usage();
208 didStr = 1;
209 ++ptr;
210 } else if (ptr[1] != '%') {
211 fprintf(stderr, "Malformed output template\n");
212 usage();
213 } else {
214 ptr += 2;
219 while (ac) {
220 doiterate(*av, outFileName, outFilePostfix,
221 flags, ts1, ts2, cmd, type);
222 ++av;
223 --ac;
225 return(0);
229 * Iterate through a file's history. If cmd == CMD_DUMP we take the
230 * next-to-last transaction id, unless another given. Otherwise if
231 * cmd == CMD_ITERATEALL we scan all transaction ids.
233 * Also iterate through the directory's history to locate other inodes that
234 * used the particular file name.
236 static
237 void
238 doiterate(const char *filename, const char *outFileName,
239 const char *outFilePostfix, int flags,
240 struct hammer_ioc_hist_entry ts1,
241 struct hammer_ioc_hist_entry ts2,
242 enum undo_cmd cmd, enum undo_type type)
244 struct undo_hist_entry_rb_tree dir_tree;
245 struct undo_hist_entry_rb_tree tse_tree;
246 struct undo_hist_entry *tse1;
247 struct undo_hist_entry *tse2;
248 struct hammer_ioc_hist_entry tid_max;
249 struct stat sb;
250 char *path = NULL;
251 int i;
252 int fd;
253 int error;
255 RB_INIT(&dir_tree);
256 RB_INIT(&tse_tree);
258 tid_max.tid = HAMMER_MAX_TID;
259 tid_max.time32 = 0;
262 * Use the directory history to locate all possible versions of
263 * the file.
265 collect_dir_history(filename, &error, &dir_tree);
266 RB_FOREACH(tse1, undo_hist_entry_rb_tree, &dir_tree) {
267 asprintf(&path, "%s@@0x%016jx", filename, (uintmax_t)tse1->tse.tid);
268 stat(path, &sb);
269 if (sb.st_mode & S_IFIFO) {
270 fprintf(stderr, "Warning: fake transaction id 0x%016jx\n", (uintmax_t)tse1->tse.tid);
271 continue;
273 if ((fd = open(path, O_RDONLY)) > 0) {
274 collect_history(fd, &error, &tse_tree);
275 close(fd);
277 free(path);
279 if ((fd = open(filename, O_RDONLY)) > 0) {
280 collect_history(fd, &error, &tse_tree);
281 close(fd);
283 if (cmd == CMD_DUMP) {
284 if ((ts1.tid == 0 ||
285 flags & (UNDO_FLAG_SETTID1|UNDO_FLAG_SETTID2)) &&
286 RB_EMPTY(&tse_tree)) {
287 if ((fd = open(filename, O_RDONLY)) > 0) {
288 collect_history(fd, &error, &tse_tree);
289 close(fd);
293 * Find entry if tid set to placeholder index
295 if (flags & UNDO_FLAG_SETTID1){
296 tse1 = RB_MAX(undo_hist_entry_rb_tree, &tse_tree);
297 while (tse1 && ts1.tid--) {
298 tse1 = RB_PREV(undo_hist_entry_rb_tree,
299 &tse_tree, tse1);
301 if (tse1)
302 ts1 = tse1->tse;
303 else
304 ts1.tid = 0;
306 if (flags & UNDO_FLAG_SETTID2){
307 tse2 = RB_MAX(undo_hist_entry_rb_tree, &tse_tree);
308 while (tse2 && ts2.tid--) {
309 tse2 = RB_PREV(undo_hist_entry_rb_tree,
310 &tse_tree, tse2);
312 if (tse2)
313 ts2 = tse2->tse;
314 else
315 ts2.tid = 0;
319 * Single entry, most recent prior to current
321 if (ts1.tid == 0) {
322 tse2 = RB_MAX(undo_hist_entry_rb_tree, &tse_tree);
323 if (tse2) {
324 ts2 = tse2->tse;
325 tse1 = RB_PREV(undo_hist_entry_rb_tree,
326 &tse_tree, tse2);
327 if (tse1)
328 ts1 = tse1->tse;
331 if (ts1.tid == 0) {
332 printf("%s: No UNDO history found\n", filename);
333 } else {
334 dogenerate(filename,
335 outFileName, outFilePostfix,
336 0, 0, type,
337 ts1, ts2);
339 } else if (RB_ROOT(&tse_tree)) {
341 * Iterate entire history
343 printf("%s: ITERATE ENTIRE HISTORY\n", filename);
345 tse1 = NULL;
346 i = 0;
347 RB_FOREACH(tse2, undo_hist_entry_rb_tree, &tse_tree) {
348 if (tse1) {
349 dogenerate(filename,
350 outFileName, outFilePostfix,
351 flags, i, type,
352 tse1->tse, tse2->tse);
354 if (tse1 && tse2->inum != tse1->inum)
355 flags |= UNDO_FLAG_INOCHG;
356 else
357 flags &= ~UNDO_FLAG_INOCHG;
358 tse1 = tse2;
359 ++i;
362 * There is no delta to print for the last pair,
363 * because they are identical.
365 if (type != TYPE_DIFF && type != TYPE_RDIFF) {
366 dogenerate(filename,
367 outFileName, outFilePostfix,
368 flags, i, type,
369 tse1->tse, tid_max);
371 } else {
372 printf("%s: ITERATE ENTIRE HISTORY: %s\n",
373 filename, strerror(error));
375 clean_tree(&dir_tree);
376 clean_tree(&tse_tree);
380 * Generate output for a file as-of ts1 (ts1 may be 0!), if diffing then
381 * through ts2.
383 static
384 void
385 dogenerate(const char *filename, const char *outFileName,
386 const char *outFilePostfix,
387 int flags, int idx, enum undo_type type,
388 struct hammer_ioc_hist_entry ts1,
389 struct hammer_ioc_hist_entry ts2)
391 struct stat st;
392 const char *elm;
393 char *ipath1 = NULL;
394 char *ipath2 = NULL;
395 FILE *fi;
396 FILE *fp;
397 char *buf;
398 char *path;
399 time_t t;
400 struct tm *tp;
401 char datestr[64];
402 int n;
404 buf = malloc(8192);
407 * Open the input file. If ts1 is 0 try to locate the most recent
408 * version of the file prior to the current version.
410 if (ts1.tid == 0)
411 asprintf(&ipath1, "%s", filename);
412 else
413 asprintf(&ipath1, "%s@@0x%016jx", filename, (uintmax_t)ts1.tid);
415 if (ts2.tid == 0)
416 asprintf(&ipath2, "%s", filename);
417 else
418 asprintf(&ipath2, "%s@@0x%016jx", filename, (uintmax_t)ts2.tid);
420 if (lstat(ipath1, &st) < 0 && lstat(ipath2, &st) < 0) {
421 if (idx == 0 || VerboseOpt) {
422 fprintf(stderr, "Unable to access either %s or %s\n",
423 ipath1, ipath2);
425 free(ipath1);
426 free(ipath2);
427 goto done;
431 * elm is the last component of the input file name
433 if ((elm = strrchr(filename, '/')) != NULL)
434 ++elm;
435 else
436 elm = filename;
439 * Where do we stuff our output?
441 if (outFileName) {
442 if (flags & UNDO_FLAG_MULT) {
443 asprintf(&path, outFileName, elm);
444 fp = fopen(path, "w");
445 if (fp == NULL) {
446 perror(path);
447 exit(1);
449 free(path);
450 } else {
451 fp = fopen(outFileName, "w");
452 if (fp == NULL) {
453 perror(outFileName);
454 exit(1);
457 } else if (outFilePostfix) {
458 if (idx >= 0) {
459 asprintf(&path, "%s%s.%04d", filename,
460 outFilePostfix, idx);
461 } else {
462 asprintf(&path, "%s%s", filename, outFilePostfix);
464 fp = fopen(path, "w");
465 if (fp == NULL) {
466 perror(path);
467 exit(1);
469 free(path);
470 } else {
471 if ((flags & UNDO_FLAG_MULT) && type == TYPE_FILE) {
472 if (idx >= 0) {
473 printf("\n>>> %s %04d 0x%016jx %s\n\n",
474 filename, idx, (uintmax_t)ts1.tid,
475 timestamp(&ts1));
476 } else {
477 printf("\n>>> %s ---- 0x%016jx %s\n\n",
478 filename, (uintmax_t)ts1.tid,
479 timestamp(&ts1));
481 } else if (idx >= 0 && type == TYPE_FILE) {
482 printf("\n>>> %s %04d 0x%016jx %s\n\n",
483 filename, idx, (uintmax_t)ts1.tid,
484 timestamp(&ts1));
486 fp = stdout;
489 switch(type) {
490 case TYPE_FILE:
491 if ((fi = fopen(ipath1, "r")) != NULL) {
492 while ((n = fread(buf, 1, 8192, fi)) > 0)
493 fwrite(buf, 1, n, fp);
494 fclose(fi);
496 break;
497 case TYPE_DIFF:
498 printf("diff -N -r -u %s %s (to %s)\n",
499 ipath1, ipath2, timestamp(&ts2));
500 fflush(stdout);
501 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-N", "-r", "-u", ipath1, ipath2, NULL);
502 break;
503 case TYPE_RDIFF:
504 printf("diff -N -r -u %s %s\n", ipath2, ipath1);
505 fflush(stdout);
506 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-N", "-r", "-u", ipath2, ipath1, NULL);
507 break;
508 case TYPE_HISTORY:
509 t = (time_t)ts1.time32;
510 tp = localtime(&t);
511 strftime(datestr, sizeof(datestr), "%d-%b-%Y %H:%M:%S", tp);
512 printf("\t0x%016jx %s", (uintmax_t)ts1.tid, datestr);
513 if (flags & UNDO_FLAG_INOCHG)
514 printf(" inode-change");
515 if (lstat(ipath1, &st) < 0)
516 printf(" file-deleted");
517 printf("\n");
518 break;
521 if (fp != stdout)
522 fclose(fp);
523 done:
524 free(buf);
527 static
528 void
529 clean_tree(struct undo_hist_entry_rb_tree *tree)
531 struct undo_hist_entry *tse;
533 while ((tse = RB_ROOT(tree)) != NULL) {
534 RB_REMOVE(undo_hist_entry_rb_tree, tree, tse);
535 free(tse);
539 static
540 void
541 collect_history(int fd, int *errorp, struct undo_hist_entry_rb_tree *tse_tree)
543 struct hammer_ioc_history hist;
544 struct undo_hist_entry *tse;
545 struct stat st;
546 int istmp;
547 int i;
550 * Setup
552 bzero(&hist, sizeof(hist));
553 hist.beg_tid = HAMMER_MIN_TID;
554 hist.end_tid = HAMMER_MAX_TID;
555 hist.head.flags |= HAMMER_IOC_HISTORY_ATKEY;
556 hist.key = 0;
557 hist.nxt_key = HAMMER_MAX_KEY;
559 *errorp = 0;
561 if (tse_tree == NULL) {
562 tse_tree = malloc(sizeof(*tse_tree));
563 RB_INIT(tse_tree);
564 istmp = 1;
565 } else {
566 istmp = 0;
570 * Save the inode so inode changes can be reported.
572 st.st_ino = 0;
573 fstat(fd, &st);
576 * Collect a unique set of transaction ids
578 if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
579 *errorp = errno;
580 goto done;
582 for (;;) {
583 for (i = 0; i < hist.count; ++i) {
584 tse = malloc(sizeof(*tse));
585 tse->tse = hist.hist_ary[i];
586 tse->inum = st.st_ino;
587 if (RB_INSERT(undo_hist_entry_rb_tree, tse_tree, tse)) {
588 free(tse);
591 if (hist.head.flags & HAMMER_IOC_HISTORY_EOF)
592 break;
593 if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_KEY) {
594 hist.key = hist.nxt_key;
595 hist.nxt_key = HAMMER_MAX_KEY;
597 if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_TID)
598 hist.beg_tid = hist.nxt_tid;
599 if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
600 *errorp = errno;
601 break;
606 * Cleanup
608 done:
609 if (istmp) {
610 clean_tree(tse_tree);
611 free(tse_tree);
615 static
616 void
617 collect_dir_history(const char *filename, int *errorp,
618 struct undo_hist_entry_rb_tree *dir_tree)
620 char *dirname;
621 int fd;
622 int error;
624 *errorp = 0;
625 if (strrchr(filename, '/')) {
626 dirname = strdup(filename);
627 *strrchr(dirname, '/') = 0;
628 } else {
629 dirname = strdup(".");
631 if ((fd = open(dirname, O_RDONLY)) > 0) {
632 collect_history(fd, &error, dir_tree);
633 close(fd);
637 static
638 hammer_tid_t
639 parse_delta_time(const char *timeStr, int *flags, int ind_flag)
641 hammer_tid_t tid;
643 tid = strtoull(timeStr, NULL, 0);
644 if (timeStr[0] == '+')
645 ++timeStr;
646 if (timeStr[0] >= '0' && timeStr[0] <= '9' && timeStr[1] != 'x')
647 *flags |= ind_flag;
648 return(tid);
651 static void
652 runcmd(int fd, const char *cmd, ...)
654 va_list va;
655 pid_t pid;
656 char **av;
657 int ac;
658 int i;
660 va_start(va, cmd);
661 for (ac = 0; va_arg(va, void *) != NULL; ++ac)
663 va_end(va);
665 av = malloc((ac + 1) * sizeof(char *));
666 va_start(va, cmd);
667 for (i = 0; i < ac; ++i)
668 av[i] = va_arg(va, char *);
669 va_end(va);
670 av[i] = NULL;
672 if ((pid = fork()) < 0) {
673 perror("fork");
674 exit(1);
675 } else if (pid == 0) {
676 if (fd != 1) {
677 dup2(fd, 1);
678 close(fd);
680 execv(cmd, av);
681 _exit(1);
682 } else {
683 while (waitpid(pid, NULL, 0) != pid)
686 free(av);
690 * Convert tid to timestamp.
692 static char *
693 timestamp(hammer_ioc_hist_entry_t hen)
695 static char timebuf[64];
696 time_t t = (time_t)hen->time32;
697 struct tm *tp;
699 tp = localtime(&t);
700 strftime(timebuf, sizeof(timebuf), "%d-%b-%Y %H:%M:%S", tp);
701 return(timebuf);
704 static
706 undo_hist_entry_compare(struct undo_hist_entry *he1,
707 struct undo_hist_entry *he2)
709 if (he1->tse.tid < he2->tse.tid)
710 return(-1);
711 if (he1->tse.tid > he2->tse.tid)
712 return(1);
713 return(0);
716 static void
717 usage(void)
719 fprintf(stderr, "undo [-adDiuv] [-o outfile] "
720 "[-t transaction-id] [-t transaction-id] path...\n"
721 " -a Iterate all historical segments\n"
722 " -d Forward diff\n"
723 " -D Reverse diff\n"
724 " -i Dump history transaction ids\n"
725 " -u Generate .undo files\n"
726 " -v Verbose\n"
727 " -o file Output to the specified file\n"
728 " -t TID Retrieve as of transaction-id, TID\n"
729 " (a second `-t TID' to diff two)\n"
730 " transaction ids must be prefixed with 0x, and\n"
731 " otherwise may specify an index starting at 0\n"
732 " and iterating backwards through the history.\n"
734 exit(1);