hammer2 - Flesh out span code, API cleanups
[dragonfly.git] / sbin / hammer2 / subs.c
index 23935f6..5ead4f8 100644 (file)
@@ -147,15 +147,144 @@ void
 hammer2_bswap_head(hammer2_msg_hdr_t *head)
 {
        head->magic     = bswap16(head->magic);
-       head->icrc1     = bswap16(head->icrc1);
+       head->reserved02 = bswap16(head->reserved02);
        head->salt      = bswap32(head->salt);
-       head->source    = bswap16(head->source);
-       head->target    = bswap16(head->target);
-       head->msgid     = bswap32(head->msgid);
+
+       head->msgid     = bswap64(head->msgid);
+       head->spanid    = bswap64(head->spanid);
+
        head->cmd       = bswap32(head->cmd);
-       head->error     = bswap16(head->error);
-       head->resv05    = bswap16(head->resv05);
-       head->icrc2     = bswap16(head->icrc2);
-       head->aux_bytes = bswap16(head->aux_bytes);
-       head->aux_icrc  = bswap32(head->aux_icrc);
+       head->aux_crc   = bswap32(head->aux_crc);
+       head->aux_bytes = bswap32(head->aux_bytes);
+       head->error     = bswap32(head->error);
+       head->aux_descr = bswap64(head->aux_descr);
+       head->reserved30= bswap64(head->reserved30);
+       head->reserved38= bswap32(head->reserved38);
+       head->hdr_crc   = bswap32(head->hdr_crc);
+}
+
+const char *
+hammer2_time64_to_str(uint64_t htime64, char **strp)
+{
+       struct tm *tp;
+       time_t t;
+
+       if (*strp) {
+               free(*strp);
+               *strp = NULL;
+       }
+       *strp = malloc(64);
+       t = htime64 / 1000000;
+       tp = localtime(&t);
+       strftime(*strp, 64, "%d-%b-%Y %H:%M:%S", tp);
+       return (*strp);
+}
+
+const char *
+hammer2_uuid_to_str(uuid_t *uuid, char **strp)
+{
+       uint32_t status;
+       if (*strp) {
+               free(*strp);
+               *strp = NULL;
+       }
+       uuid_to_string(uuid, strp, &status);
+       return (*strp);
+}
+
+const char *
+hammer2_iptype_to_str(uint8_t type)
+{
+       switch(type) {
+       case HAMMER2_OBJTYPE_UNKNOWN:
+               return("UNKNOWN");
+       case HAMMER2_OBJTYPE_DIRECTORY:
+               return("DIR");
+       case HAMMER2_OBJTYPE_REGFILE:
+               return("FILE");
+       case HAMMER2_OBJTYPE_FIFO:
+               return("FIFO");
+       case HAMMER2_OBJTYPE_CDEV:
+               return("CDEV");
+       case HAMMER2_OBJTYPE_BDEV:
+               return("BDEV");
+       case HAMMER2_OBJTYPE_SOFTLINK:
+               return("SOFTLINK");
+       case HAMMER2_OBJTYPE_HARDLINK:
+               return("HARDLINK");
+       case HAMMER2_OBJTYPE_SOCKET:
+               return("SOCKET");
+       case HAMMER2_OBJTYPE_WHITEOUT:
+               return("WHITEOUT");
+       default:
+               return("ILLEGAL");
+       }
+}
+
+const char *
+hammer2_pfstype_to_str(uint8_t type)
+{
+       switch(type) {
+       case HAMMER2_PFSTYPE_NONE:
+               return("NONE");
+       case HAMMER2_PFSTYPE_ADMIN:
+               return("ADMIN");
+       case HAMMER2_PFSTYPE_CACHE:
+               return("CACHE");
+       case HAMMER2_PFSTYPE_COPY:
+               return("COPY");
+       case HAMMER2_PFSTYPE_SLAVE:
+               return("SLAVE");
+       case HAMMER2_PFSTYPE_SOFT_SLAVE:
+               return("SOFT_SLAVE");
+       case HAMMER2_PFSTYPE_SOFT_MASTER:
+               return("SOFT_MASTER");
+       case HAMMER2_PFSTYPE_MASTER:
+               return("MASTER");
+       default:
+               return("ILLEGAL");
+       }
+}
+
+const char *
+sizetostr(hammer2_off_t size)
+{
+       static char buf[32];
+
+       if (size < 1024 / 2) {
+               snprintf(buf, sizeof(buf), "%6.2f", (double)size);
+       } else if (size < 1024 * 1024 / 2) {
+               snprintf(buf, sizeof(buf), "%6.2fKB",
+                       (double)size / 1024);
+       } else if (size < 1024 * 1024 * 1024LL / 2) {
+               snprintf(buf, sizeof(buf), "%6.2fMB",
+                       (double)size / (1024 * 1024));
+       } else if (size < 1024 * 1024 * 1024LL * 1024LL / 2) {
+               snprintf(buf, sizeof(buf), "%6.2fGB",
+                       (double)size / (1024 * 1024 * 1024LL));
+       } else {
+               snprintf(buf, sizeof(buf), "%6.2fTB",
+                       (double)size / (1024 * 1024 * 1024LL * 1024LL));
+       }
+       return(buf);
+}
+
+/*
+ * Allocation wrappers give us shims for possible future use
+ */
+void *
+hammer2_alloc(size_t bytes)
+{
+       void *ptr;
+
+       ptr = malloc(bytes);
+       assert(ptr);
+       bzero(ptr, bytes);
+       return (ptr);
+}
+
+void
+hammer2_free(void *ptr)
+{
+       free(ptr);
 }