From 1d9f6aa1a944f07e4e87324c54d65d2d143403bb Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Tue, 24 Jun 2008 17:40:24 +0000 Subject: [PATCH] HAMMER Utilities: Sync to 58B * Update all utilities to deal with the cleaned up ioctl structures and to augment history and undo output with the non-monotonic timestamps. --- sbin/hammer/cmd_history.c | 28 ++++-- sbin/hammer/cmd_reblock.c | 28 +++--- sbin/hammer/cmd_softprune.c | 28 +++--- sbin/newfs_hammer/newfs_hammer.c | 7 +- usr.bin/undo/undo.c | 167 +++++++++++++++---------------- 5 files changed, 135 insertions(+), 123 deletions(-) diff --git a/sbin/hammer/cmd_history.c b/sbin/hammer/cmd_history.c index 09eb3f4df1..e42ccac372 100644 --- a/sbin/hammer/cmd_history.c +++ b/sbin/hammer/cmd_history.c @@ -31,13 +31,14 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/sbin/hammer/cmd_history.c,v 1.3 2008/06/24 02:42:48 dillon Exp $ + * $DragonFly: src/sbin/hammer/cmd_history.c,v 1.4 2008/06/24 17:40:21 dillon Exp $ */ #include "hammer.h" static void hammer_do_history(const char *path, off_t off, int len); static void dumpat(const char *path, off_t off, int len); +static const char *timestr32(u_int32_t time32); /* * history ... @@ -101,11 +102,11 @@ hammer_do_history(const char *path, off_t off, int len) char *hist_path = NULL; asprintf(&hist_path, "%s@@0x%016llx", - path, hist.tid_ary[i]); - if (off < 0) { - printf(" %016llx", hist.tid_ary[i]); - } else { - printf(" %016llx", hist.tid_ary[i]); + path, hist.hist_ary[i].tid); + printf(" %016llx %s", + hist.hist_ary[i].tid, + timestr32(hist.hist_ary[i].time32)); + if (off >= 0) { if (VerboseOpt) { printf(" '"); dumpat(hist_path, off, len); @@ -158,3 +159,18 @@ dumpat(const char *path, off_t off, int len) } } +/* + * Return a human-readable timestamp + */ +static const char * +timestr32(u_int32_t time32) +{ + static char timebuf[64]; + time_t t = (time_t)time32; + struct tm *tp; + + tp = localtime(&t); + strftime(timebuf, sizeof(timebuf), "%d-%b-%Y %H:%M:%S", tp); + return(timebuf); +} + diff --git a/sbin/hammer/cmd_reblock.c b/sbin/hammer/cmd_reblock.c index 566b72478e..cd9e4ea79c 100644 --- a/sbin/hammer/cmd_reblock.c +++ b/sbin/hammer/cmd_reblock.c @@ -31,7 +31,7 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/sbin/hammer/cmd_reblock.c,v 1.7 2008/06/14 01:44:11 dillon Exp $ + * $DragonFly: src/sbin/hammer/cmd_reblock.c,v 1.8 2008/06/24 17:40:21 dillon Exp $ */ #include "hammer.h" @@ -51,12 +51,13 @@ hammer_cmd_reblock(char **av, int ac, int flags) bzero(&reblock, sizeof(reblock)); - reblock.beg_localization = HAMMER_MIN_LOCALIZATION; - reblock.beg_obj_id = HAMMER_MIN_OBJID; - hammer_get_cycle(&reblock.beg_obj_id, &reblock.beg_localization); + reblock.key_beg.localization = HAMMER_MIN_LOCALIZATION; + reblock.key_beg.obj_id = HAMMER_MIN_OBJID; + hammer_get_cycle(&reblock.key_beg.obj_id, + &reblock.key_beg.localization); - reblock.end_localization = HAMMER_MAX_LOCALIZATION; - reblock.end_obj_id = HAMMER_MAX_OBJID; + reblock.key_end.localization = HAMMER_MAX_LOCALIZATION; + reblock.key_end.obj_id = HAMMER_MAX_OBJID; reblock.head.flags = flags & HAMMER_IOC_DO_FLAGS; @@ -66,13 +67,13 @@ hammer_cmd_reblock(char **av, int ac, int flags) */ switch(flags & (HAMMER_IOC_DO_INODES|HAMMER_IOC_DO_DATA|HAMMER_IOC_DO_DIRS)) { case HAMMER_IOC_DO_INODES: - reblock.beg_localization = HAMMER_LOCALIZE_INODE; - reblock.end_localization = HAMMER_LOCALIZE_INODE; + reblock.key_beg.localization = HAMMER_LOCALIZE_INODE; + reblock.key_end.localization = HAMMER_LOCALIZE_INODE; break; case HAMMER_IOC_DO_DIRS: case HAMMER_IOC_DO_DATA: - reblock.beg_localization = HAMMER_LOCALIZE_MISC; - reblock.end_localization = HAMMER_LOCALIZE_MISC; + reblock.key_beg.localization = HAMMER_LOCALIZE_MISC; + reblock.key_end.localization = HAMMER_LOCALIZE_MISC; break; } @@ -101,10 +102,11 @@ hammer_cmd_reblock(char **av, int ac, int flags) } else if (reblock.head.flags & HAMMER_IOC_HEAD_INTR) { printf("Reblock %s interrupted by timer at %016llx %04x\n", filesystem, - reblock.cur_obj_id, reblock.cur_localization); + reblock.key_cur.obj_id, + reblock.key_cur.localization); if (CyclePath) { - hammer_set_cycle(reblock.cur_obj_id, - reblock.cur_localization); + hammer_set_cycle(reblock.key_cur.obj_id, + reblock.key_cur.localization); } } else { if (CyclePath) diff --git a/sbin/hammer/cmd_softprune.c b/sbin/hammer/cmd_softprune.c index 1c2fb33d66..298a78fac3 100644 --- a/sbin/hammer/cmd_softprune.c +++ b/sbin/hammer/cmd_softprune.c @@ -31,7 +31,7 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/sbin/hammer/cmd_softprune.c,v 1.3 2008/06/23 21:31:55 dillon Exp $ + * $DragonFly: src/sbin/hammer/cmd_softprune.c,v 1.4 2008/06/24 17:40:21 dillon Exp $ */ #include "hammer.h" @@ -70,18 +70,19 @@ hammer_cmd_softprune(char **av, int ac, int everything_opt) rcode = 0; /* - * NOTE: To resitrct to a single file XXX we have to set + * NOTE: To restrict to a single file XXX we have to set * the localization the same (not yet implemented). Typically * two passes would be needed, one using HAMMER_LOCALIZE_MISC * and one using HAMMER_LOCALIZE_INODE. */ bzero(&template, sizeof(template)); - template.beg_localization = HAMMER_MIN_LOCALIZATION; - template.beg_obj_id = HAMMER_MIN_OBJID; - template.end_localization = HAMMER_MAX_LOCALIZATION; - template.end_obj_id = HAMMER_MAX_OBJID; - hammer_get_cycle(&template.end_obj_id, &template.end_localization); + template.key_beg.localization = HAMMER_MIN_LOCALIZATION; + template.key_beg.obj_id = HAMMER_MIN_OBJID; + template.key_end.localization = HAMMER_MAX_LOCALIZATION; + template.key_end.obj_id = HAMMER_MAX_OBJID; + hammer_get_cycle(&template.key_end.obj_id, + &template.key_end.localization); template.stat_oldest_tid = HAMMER_MAX_TID; /* @@ -144,8 +145,8 @@ hammer_cmd_softprune(char **av, int ac, int everything_opt) continue; } printf("objspace %016llx %016llx\n", - scan->prune.beg_obj_id, - scan->prune.end_obj_id); + scan->prune.key_beg.obj_id, + scan->prune.key_end.obj_id); if (ioctl(fd, HAMMERIOC_PRUNE, &scan->prune) < 0) { printf("Prune %s failed: %s\n", @@ -155,11 +156,12 @@ hammer_cmd_softprune(char **av, int ac, int everything_opt) printf("Prune %s interrupted by timer at " "%016llx %04x\n", scan->filesystem, - scan->prune.cur_obj_id, - scan->prune.cur_localization); + scan->prune.key_cur.obj_id, + scan->prune.key_cur.localization); if (CyclePath) { - hammer_set_cycle(scan->prune.cur_obj_id, - scan->prune.cur_localization); + hammer_set_cycle( + scan->prune.key_cur.obj_id, + scan->prune.key_cur.localization); } rcode = 0; } else { diff --git a/sbin/newfs_hammer/newfs_hammer.c b/sbin/newfs_hammer/newfs_hammer.c index 8cdfd9f323..2a190ea7c2 100644 --- a/sbin/newfs_hammer/newfs_hammer.c +++ b/sbin/newfs_hammer/newfs_hammer.c @@ -31,7 +31,7 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/sbin/newfs_hammer/newfs_hammer.c,v 1.34 2008/06/23 21:31:58 dillon Exp $ + * $DragonFly: src/sbin/newfs_hammer/newfs_hammer.c,v 1.35 2008/06/24 17:40:22 dillon Exp $ */ #include "newfs_hammer.h" @@ -288,11 +288,9 @@ static hammer_tid_t createtid(void) { static hammer_tid_t lasttid; - struct timeval tv; - if (lasttid == 0) { + if (lasttid == 0) lasttid = 0x0000000100000000ULL; - } return(lasttid++); } @@ -497,6 +495,7 @@ format_root(void) elm->leaf.base.delete_tid = 0; elm->leaf.base.rec_type = HAMMER_RECTYPE_INODE; elm->leaf.base.obj_type = HAMMER_OBJTYPE_DIRECTORY; + elm->leaf.create_ts = (u_int32_t)time(NULL); elm->leaf.data_offset = data_off; elm->leaf.data_len = sizeof(*idata); diff --git a/usr.bin/undo/undo.c b/usr.bin/undo/undo.c index 643c383ee5..d44536317c 100644 --- a/usr.bin/undo/undo.c +++ b/usr.bin/undo/undo.c @@ -31,7 +31,7 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/usr.bin/undo/undo.c,v 1.1 2008/06/01 02:03:10 dillon Exp $ + * $DragonFly: src/usr.bin/undo/undo.c,v 1.2 2008/06/24 17:40:24 dillon Exp $ */ /* * UNDO - retrieve an older version of a file. @@ -57,13 +57,16 @@ static void doiterate(const char *orig_filename, const char *outFileName, static void dogenerate(const char *filename, const char *outFileName, const char *outFilePostfix, int mult, int idx, enum undo_type type, - hammer_tid_t ts1, hammer_tid_t ts2); -static hammer_tid_t find_recent(const char *filename); -static hammer_tid_t output_history(const char *filename, int fd, FILE *fp, - hammer_tid_t **tid_ary, int *tid_num); + struct hammer_ioc_hist_entry ts1, + struct hammer_ioc_hist_entry ts2); +static struct hammer_ioc_hist_entry + find_recent(const char *filename); +static struct hammer_ioc_hist_entry + output_history(const char *filename, int fd, FILE *fp, + struct hammer_ioc_hist_entry **hist_ary, int *tid_num); static hammer_tid_t parse_delta_time(const char *timeStr); static void runcmd(int fd, const char *cmd, ...); -static char *timestamp(hammer_tid_t tid); +static char *timestamp(hammer_ioc_hist_entry_t hen); static void usage(void); static int VerboseOpt; @@ -75,11 +78,14 @@ main(int ac, char **av) const char *outFilePostfix = NULL; enum { CMD_DUMP, CMD_ITERATEALL } cmd; enum undo_type type; - hammer_tid_t ts1 = 0; - hammer_tid_t ts2 = 0; + struct hammer_ioc_hist_entry ts1; + struct hammer_ioc_hist_entry ts2; int c; int mult; + bzero(&ts1, sizeof(ts1)); + bzero(&ts2, sizeof(ts2)); + cmd = CMD_DUMP; type = TYPE_FILE; @@ -113,12 +119,12 @@ main(int ac, char **av) outFileName = optarg; break; case 't': - if (ts1 && ts2) + if (ts1.tid && ts2.tid) usage(); - else if (ts1 == 0) - ts1 = parse_delta_time(optarg); + else if (ts1.tid == 0) + ts1.tid = parse_delta_time(optarg); else - ts2 = parse_delta_time(optarg); + ts2.tid = parse_delta_time(optarg); break; default: usage(); @@ -171,8 +177,7 @@ main(int ac, char **av) switch(cmd) { case CMD_DUMP: dogenerate(*av, outFileName, outFilePostfix, - mult, -1, type, - ts1, ts2); + mult, -1, type, ts1, ts2); break; case CMD_ITERATEALL: doiterate(*av, outFileName, outFilePostfix, @@ -193,19 +198,24 @@ void doiterate(const char *orig_filename, const char *outFileName, const char *outFilePostfix, int mult, enum undo_type type) { - hammer_tid_t *tid_ary = NULL; - hammer_tid_t ts1; + hammer_ioc_hist_entry_t tid_ary = NULL; + struct hammer_ioc_hist_entry tid_max; + struct hammer_ioc_hist_entry ts1; const char *use_filename; char *path = NULL; int tid_num = 0; int i; int fd; + tid_max.tid = HAMMER_MAX_TID; + tid_max.time32 = 0; + use_filename = orig_filename; if ((fd = open(orig_filename, O_RDONLY)) < 0) { ts1 = find_recent(orig_filename); - if (ts1) { - asprintf(&path, "%s@@0x%016llx", orig_filename, ts1); + if (ts1.tid) { + asprintf(&path, "%s@@0x%016llx", + orig_filename, ts1.tid); use_filename = path; } } @@ -216,14 +226,14 @@ doiterate(const char *orig_filename, const char *outFileName, close(fd); for (i = 0; i < tid_num; ++i) { - if (i && tid_ary[i] == tid_ary[i-1]) + if (i && tid_ary[i].tid == tid_ary[i-1].tid) continue; if (i == tid_num - 1) { dogenerate(orig_filename, outFileName, outFilePostfix, mult, i, type, - tid_ary[i], HAMMER_MAX_TID); + tid_ary[i], tid_max); } else { dogenerate(orig_filename, outFileName, outFilePostfix, @@ -249,7 +259,8 @@ void dogenerate(const char *filename, const char *outFileName, const char *outFilePostfix, int mult, int idx, enum undo_type type, - hammer_tid_t ts1, hammer_tid_t ts2) + struct hammer_ioc_hist_entry ts1, + struct hammer_ioc_hist_entry ts2) { struct stat st; const char *elm; @@ -267,9 +278,9 @@ dogenerate(const char *filename, const char *outFileName, * Open the input file. If ts1 is 0 try to locate the most recent * version of the file prior to the current version. */ - if (ts1 == 0) + if (ts1.tid == 0) ts1 = find_recent(filename); - asprintf(&ipath1, "%s@@0x%016llx", filename, ts1); + asprintf(&ipath1, "%s@@0x%016llx", filename, ts1.tid); if (lstat(ipath1, &st) < 0) { if (VerboseOpt) { fprintf(stderr, "Cannot locate src/historical " @@ -279,14 +290,14 @@ dogenerate(const char *filename, const char *outFileName, goto done; } - if (ts2 == 0) { + if (ts2.tid == 0) { asprintf(&ipath2, "%s", filename); } else { - asprintf(&ipath2, "%s@@0x%015llx", filename, ts2); + asprintf(&ipath2, "%s@@0x%015llx", filename, ts2.tid); } if (lstat(ipath2, &st) < 0) { if (VerboseOpt) { - if (ts2) { + if (ts2.tid) { fprintf(stderr, "Cannot locate tgt/historical " "idx=%d %s\n", idx, ipath2); @@ -341,14 +352,14 @@ dogenerate(const char *filename, const char *outFileName, if (mult && type == TYPE_FILE) { if (idx >= 0) { printf("\n>>> %s %04d 0x%016llx %s\n\n", - filename, idx, ts1, timestamp(ts1)); + filename, idx, ts1.tid, timestamp(&ts1)); } else { printf("\n>>> %s ---- 0x%016llx %s\n\n", - filename, ts1, timestamp(ts1)); + filename, ts1.tid, timestamp(&ts1)); } } else if (idx >= 0 && type == TYPE_FILE) { printf("\n>>> %s %04d 0x%016llx %s\n\n", - filename, idx, ts1, timestamp(ts1)); + filename, idx, ts1.tid, timestamp(&ts1)); } fp = stdout; } @@ -362,7 +373,8 @@ dogenerate(const char *filename, const char *outFileName, } break; case TYPE_DIFF: - printf("diff -u %s %s\n", ipath1, ipath2); + printf("diff -u %s %s (to %s)\n", + ipath1, ipath2, timestamp(&ts2)); fflush(stdout); runcmd(fileno(fp), "/usr/bin/diff", "diff", "-u", ipath1, ipath2, NULL); break; @@ -390,21 +402,22 @@ done: * * XXX if file cannot be found */ -static hammer_tid_t +static +struct hammer_ioc_hist_entry find_recent(const char *filename) { - hammer_tid_t *tid_ary = NULL; + hammer_ioc_hist_entry_t tid_ary = NULL; int tid_num = 0; - hammer_tid_t tid; + struct hammer_ioc_hist_entry hen; char *dirname; char *path; int fd; int i; if ((fd = open(filename, O_RDONLY)) >= 0) { - tid = output_history(NULL, fd, NULL, NULL, NULL); + hen = output_history(NULL, fd, NULL, NULL, NULL); close(fd); - return(tid); + return(hen); } /* @@ -418,16 +431,17 @@ find_recent(const char *filename) dirname = strdup("."); } - tid = 0; + hen.tid = 0; + hen.time32 = 0; if ((fd = open(dirname, O_RDONLY)) >= 0) { output_history(NULL, fd, NULL, &tid_ary, &tid_num); close(fd); free(dirname); for (i = tid_num - 1; i >= 0; --i) { - asprintf(&path, "%s@@0x%016llx", filename, tid_ary[i]); + asprintf(&path, "%s@@0x%016llx", filename, tid_ary[i].tid); if ((fd = open(path, O_RDONLY)) >= 0) { - tid = output_history(NULL, fd, NULL, NULL, NULL); + hen = output_history(NULL, fd, NULL, NULL, NULL); close(fd); free(path); break; @@ -435,7 +449,7 @@ find_recent(const char *filename) free(path); } } - return(tid); + return(hen); } /* @@ -447,20 +461,22 @@ find_recent(const char *filename) static int tid_cmp(const void *arg1, const void *arg2) { - const hammer_tid_t *tid1 = arg1; - const hammer_tid_t *tid2 = arg2; + const struct hammer_ioc_hist_entry *tid1 = arg1; + const struct hammer_ioc_hist_entry *tid2 = arg2; - if (*tid1 < *tid2) + if (tid1->tid < tid2->tid) return(-1); - if (*tid1 > *tid2) + if (tid1->tid > tid2->tid) return(1); return(0); } -static hammer_tid_t +static +struct hammer_ioc_hist_entry output_history(const char *filename, int fd, FILE *fp, - hammer_tid_t **tid_aryp, int *tid_nump) + struct hammer_ioc_hist_entry **hist_aryp, int *tid_nump) { + struct hammer_ioc_hist_entry hen; struct hammer_ioc_history hist; char datestr[64]; struct tm *tp; @@ -468,8 +484,7 @@ output_history(const char *filename, int fd, FILE *fp, int tid_max = 32; int tid_num = 0; int i; - hammer_tid_t *tid_ary = malloc(tid_max * sizeof(*tid_ary)); - hammer_tid_t tid; + hammer_ioc_hist_entry_t hist_ary = malloc(tid_max * sizeof(*hist_ary)); bzero(&hist, sizeof(hist)); hist.beg_tid = HAMMER_MIN_TID; @@ -478,7 +493,8 @@ output_history(const char *filename, int fd, FILE *fp, hist.key = 0; hist.nxt_key = HAMMER_MAX_KEY; - tid = 0; + hen.tid = 0; + hen.time32 = 0; if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) { if (filename) @@ -490,10 +506,10 @@ output_history(const char *filename, int fd, FILE *fp, for (;;) { if (tid_num + hist.count >= tid_max) { tid_max = (tid_max * 3 / 2) + hist.count; - tid_ary = realloc(tid_ary, tid_max * sizeof(*tid_ary)); + hist_ary = realloc(hist_ary, tid_max * sizeof(*hist_ary)); } for (i = 0; i < hist.count; ++i) { - tid_ary[tid_num++] = hist.tid_ary[i]; + hist_ary[tid_num++] = hist.hist_ary[i]; } if (hist.head.flags & HAMMER_IOC_HISTORY_EOF) break; @@ -509,27 +525,27 @@ output_history(const char *filename, int fd, FILE *fp, break; } } - qsort(tid_ary, tid_num, sizeof(*tid_ary), tid_cmp); + qsort(hist_ary, tid_num, sizeof(*hist_ary), tid_cmp); if (tid_num == 0) goto done; for (i = 0; fp && i < tid_num; ++i) { - if (i && tid_ary[i] == tid_ary[i-1]) + if (i && hist_ary[i].tid == hist_ary[i-1].tid) continue; - t = (time_t)(tid_ary[i] / 1000000000); + t = (time_t)hist_ary[i].time32; tp = localtime(&t); - strftime(datestr, sizeof(datestr), "%d-%m-%Y %H:%M:%S", tp); - printf("\t0x%016llx %s\n", tid_ary[i], datestr); + strftime(datestr, sizeof(datestr), "%d-%b-%Y %H:%M:%S", tp); + printf("\t0x%016llx %s\n", hist_ary[i].tid, datestr); } if (tid_num > 1) - tid = tid_ary[tid_num-2]; + hen = hist_ary[tid_num-2]; done: - if (tid_aryp) { - *tid_aryp = tid_ary; + if (hist_aryp) { + *hist_aryp = hist_ary; *tid_nump = tid_num; } else { - free(tid_ary); + free(hist_ary); } - return(tid); + return(hen); } static @@ -537,31 +553,8 @@ hammer_tid_t parse_delta_time(const char *timeStr) { hammer_tid_t tid; - char *ptr; - if (timeStr[0] == '0' && (timeStr[1] == 'x' || timeStr[1] == 'X')) { - tid = strtoull(timeStr, NULL, 0); - } else { - tid = strtol(timeStr, &ptr, 0); - - switch(*ptr) { - case 'd': - tid *= 24; - /* fall through */ - case 'h': - tid *= 60; - /* fall through */ - case 'm': - tid *= 60; - /* fall through */ - case 's': - break; - default: - usage(); - } - tid = time(NULL) - tid; - tid *= 1000000000; - } + tid = strtoull(timeStr, NULL, 0); return(tid); } @@ -607,14 +600,14 @@ runcmd(int fd, const char *cmd, ...) * Convert tid to timestamp. */ static char * -timestamp(hammer_tid_t tid) +timestamp(hammer_ioc_hist_entry_t hen) { static char timebuf[64]; - time_t t = (time_t)(tid / 1000000000); + time_t t = (time_t)hen->time32; struct tm *tp; tp = localtime(&t); - strftime(timebuf, sizeof(timebuf), "%d-%m-%Y %H:%M:%S", tp); + strftime(timebuf, sizeof(timebuf), "%d-%b-%Y %H:%M:%S", tp); return(timebuf); } -- 2.41.0