Support the fallocate() file operation.
[zfs.git] / cmd / zdb / zdb.c
index ff73072..2f5f19e 100644 (file)
@@ -55,7 +55,6 @@
 #include <sys/arc.h>
 #include <sys/ddt.h>
 #undef ZFS_MAXNAMELEN
-#undef verify
 #include <libzfs.h>
 
 #define        ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
@@ -87,7 +86,7 @@ libzfs_handle_t *g_zfs;
  * debugging facilities.
  */
 const char *
-_umem_debug_init()
+_umem_debug_init(void)
 {
        return ("default,verbose"); /* $UMEM_DEBUG setting */
 }
@@ -645,8 +644,9 @@ dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
        char *types[4] = { "ditto", "single", "double", "triple" };
        char blkbuf[BP_SPRINTF_LEN];
        blkptr_t blk;
+       int p;
 
-       for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
+       for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
                if (ddp->ddp_phys_birth == 0)
                        continue;
                ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
@@ -695,12 +695,12 @@ dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
                return;
        ASSERT(error == 0);
 
-       count = ddt_object_count(ddt, type, class);
+       if ((count = ddt_object_count(ddt, type, class)) == 0)
+               return;
+
        dspace = doi.doi_physical_blocks_512 << 9;
        mspace = doi.doi_fill_count * doi.doi_data_block_size;
 
-       ASSERT(count != 0);     /* we should have destroyed it */
-
        ddt_object_name(ddt, type, class, name);
 
        (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
@@ -733,13 +733,19 @@ dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
 static void
 dump_all_ddts(spa_t *spa)
 {
-       ddt_histogram_t ddh_total = { 0 };
-       ddt_stat_t dds_total = { 0 };
+       ddt_histogram_t ddh_total;
+       ddt_stat_t dds_total;
+       enum zio_checksum c;
+       enum ddt_type type;
+       enum ddt_class class;
 
-       for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
+       bzero(&ddh_total, sizeof (ddt_histogram_t));
+       bzero(&dds_total, sizeof (ddt_stat_t));
+
+       for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
                ddt_t *ddt = spa->spa_ddt[c];
-               for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
-                       for (enum ddt_class class = 0; class < DDT_CLASSES;
+               for (type = 0; type < DDT_TYPES; type++) {
+                       for (class = 0; class < DDT_CLASSES;
                            class++) {
                                dump_ddt(ddt, type, class);
                        }
@@ -783,6 +789,7 @@ dump_dtl(vdev_t *vd, int indent)
        boolean_t required;
        char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
        char prefix[256];
+       int c, t;
 
        spa_vdev_state_enter(spa, SCL_NONE);
        required = vdev_dtl_required(vd);
@@ -796,7 +803,7 @@ dump_dtl(vdev_t *vd, int indent)
            vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
            required ? "DTL-required" : "DTL-expendable");
 
-       for (int t = 0; t < DTL_TYPES; t++) {
+       for (t = 0; t < DTL_TYPES; t++) {
                space_map_t *sm = &vd->vdev_dtl[t];
                if (sm->sm_space == 0)
                        continue;
@@ -810,7 +817,7 @@ dump_dtl(vdev_t *vd, int indent)
                            &vd->vdev_dtl_smo, sm);
        }
 
-       for (int c = 0; c < vd->vdev_children; c++)
+       for (c = 0; c < vd->vdev_children; c++)
                dump_dtl(vd->vdev_child[c], indent + 4);
 }
 
@@ -826,6 +833,7 @@ dump_history(spa_t *spa)
        struct tm t;
        char tbuf[30];
        char internalstr[MAXPATHLEN];
+       int i;
 
        do {
                len = sizeof (buf);
@@ -843,7 +851,7 @@ dump_history(spa_t *spa)
        } while (len != 0);
 
        (void) printf("\nHistory:\n");
-       for (int i = 0; i < num; i++) {
+       for (i = 0; i < num; i++) {
                uint64_t time, txg, ievent;
                char *cmd, *intstr;
 
@@ -865,8 +873,8 @@ dump_history(spa_t *spa)
                        (void) snprintf(internalstr,
                            sizeof (internalstr),
                            "[internal %s txg:%lld] %s",
-                           zfs_history_event_names[ievent], txg,
-                           intstr);
+                           zfs_history_event_names[ievent],
+                           (longlong_t)txg, intstr);
                        cmd = internalstr;
                }
                tsec = time;
@@ -904,6 +912,7 @@ sprintf_blkptr_compact(char *blkbuf, const blkptr_t *bp)
 {
        const dva_t *dva = bp->blk_dva;
        int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
+       int i;
 
        if (dump_opt['b'] >= 5) {
                sprintf_blkptr(blkbuf, bp);
@@ -912,7 +921,7 @@ sprintf_blkptr_compact(char *blkbuf, const blkptr_t *bp)
 
        blkbuf[0] = '\0';
 
-       for (int i = 0; i < ndvas; i++)
+       for (i = 0; i < ndvas; i++)
                (void) sprintf(blkbuf + strlen(blkbuf), "%llu:%llx:%llx ",
                    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
                    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
@@ -1211,7 +1220,7 @@ static boolean_t sa_loaded;
 sa_attr_type_t *sa_attr_table;
 
 static void
-fuid_table_destroy()
+fuid_table_destroy(void)
 {
        if (fuid_table_loaded) {
                zfs_fuid_table_destroy(&idx_tree, &domain_tree);
@@ -1290,8 +1299,12 @@ dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
                        VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
                            8, 1, &sa_attrs) == 0);
                }
-               sa_attr_table = sa_setup(os, sa_attrs,
-                   zfs_attr_table, ZPL_END);
+               if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
+                   ZPL_END, &sa_attr_table)) != 0) {
+                       (void) printf("sa_setup failed errno %d, can't "
+                           "display znode contents\n", error);
+                       return;
+               }
                sa_loaded = B_TRUE;
        }
 
@@ -1455,7 +1468,7 @@ dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
        }
 
        if (object == 0) {
-               dn = os->os_meta_dnode;
+               dn = DMU_META_DNODE(os);
        } else {
                error = dmu_bonus_hold(os, object, FTAG, &db);
                if (error)
@@ -1463,7 +1476,7 @@ dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
                            object, error);
                bonus = db->db_data;
                bsize = db->db_size;
-               dn = ((dmu_buf_impl_t *)db)->db_dnode;
+               dn = DB_DNODE((dmu_buf_impl_t *)db);
        }
        dmu_object_info_from_dnode(dn, &doi);
 
@@ -1627,8 +1640,8 @@ dump_dir(objset_t *os)
 
        dump_object(os, 0, verbosity, &print_header);
        object_count = 0;
-       if (os->os_userused_dnode &&
-           os->os_userused_dnode->dn_type != 0) {
+       if (DMU_USERUSED_DNODE(os) != NULL &&
+           DMU_USERUSED_DNODE(os)->dn_type != 0) {
                dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
                dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
        }
@@ -1654,7 +1667,7 @@ dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
 {
        time_t timestamp = ub->ub_timestamp;
 
-       (void) printf(header ? header : "");
+       (void) printf("%s", header ? header : "");
        (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
        (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
        (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
@@ -1666,7 +1679,7 @@ dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
                sprintf_blkptr(blkbuf, &ub->ub_rootbp);
                (void) printf("\trootbp = %s\n", blkbuf);
        }
-       (void) printf(footer ? footer : "");
+       (void) printf("%s", footer ? footer : "");
 }
 
 static void
@@ -1747,11 +1760,12 @@ dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
        vdev_t vd;
        vdev_t *vdp = &vd;
        char header[ZDB_MAX_UB_HEADER_SIZE];
+       int i;
 
        vd.vdev_ashift = ashift;
        vdp->vdev_top = vdp;
 
-       for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
+       for (i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
                uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
                uberblock_t *ub = (void *)((char *)lbl + uoff);
 
@@ -1773,6 +1787,7 @@ dump_label(const char *dev)
        struct stat64 statbuf;
        uint64_t psize, ashift;
        int len = strlen(dev) + 1;
+       int l;
 
        if (strncmp(dev, "/dev/dsk/", 9) == 0) {
                len++;
@@ -1788,7 +1803,7 @@ dump_label(const char *dev)
                exit(1);
        }
 
-       if (fstat64(fd, &statbuf) != 0) {
+       if (fstat64_blk(fd, &statbuf) != 0) {
                (void) printf("failed to stat '%s': %s\n", path,
                    strerror(errno));
                free(path);
@@ -1796,18 +1811,10 @@ dump_label(const char *dev)
                exit(1);
        }
 
-       if (S_ISBLK(statbuf.st_mode)) {
-               (void) printf("cannot use '%s': character device required\n",
-                   path);
-               free(path);
-               (void) close(fd);
-               exit(1);
-       }
-
        psize = statbuf.st_size;
        psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
 
-       for (int l = 0; l < VDEV_LABELS; l++) {
+       for (l = 0; l < VDEV_LABELS; l++) {
                nvlist_t *config = NULL;
 
                (void) printf("--------------------------------------------\n");
@@ -1901,13 +1908,14 @@ zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
     dmu_object_type_t type)
 {
        uint64_t refcnt = 0;
+       int i;
 
        ASSERT(type < ZDB_OT_TOTAL);
 
        if (zilog && zil_bp_tree_add(zilog, bp) != 0)
                return;
 
-       for (int i = 0; i < 4; i++) {
+       for (i = 0; i < 4; i++) {
                int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
                int t = (i & 1) ? type : ZDB_OT_TOTAL;
                zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
@@ -2059,6 +2067,7 @@ zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
        ddt_bookmark_t ddb = { 0 };
        ddt_entry_t dde;
        int error;
+       int p;
 
        while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
                blkptr_t blk;
@@ -2069,7 +2078,7 @@ zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
 
                ASSERT(ddt_phys_total_refcnt(&dde) > 1);
 
-               for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
+               for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
                        if (ddp->ddp_phys_birth == 0)
                                continue;
                        ddt_bp_create(ddb.ddb_checksum,
@@ -2097,12 +2106,13 @@ static void
 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
 {
        zcb->zcb_spa = spa;
+       int c, m;
 
        if (!dump_opt['L']) {
                vdev_t *rvd = spa->spa_root_vdev;
-               for (int c = 0; c < rvd->vdev_children; c++) {
+               for (c = 0; c < rvd->vdev_children; c++) {
                        vdev_t *vd = rvd->vdev_child[c];
-                       for (int m = 0; m < vd->vdev_ms_count; m++) {
+                       for (m = 0; m < vd->vdev_ms_count; m++) {
                                metaslab_t *msp = vd->vdev_ms[m];
                                mutex_enter(&msp->ms_lock);
                                space_map_unload(&msp->ms_map);
@@ -2125,11 +2135,13 @@ zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
 static void
 zdb_leak_fini(spa_t *spa)
 {
+       int c, m;
+
        if (!dump_opt['L']) {
                vdev_t *rvd = spa->spa_root_vdev;
-               for (int c = 0; c < rvd->vdev_children; c++) {
+               for (c = 0; c < rvd->vdev_children; c++) {
                        vdev_t *vd = rvd->vdev_child[c];
-                       for (int m = 0; m < vd->vdev_ms_count; m++) {
+                       for (m = 0; m < vd->vdev_ms_count; m++) {
                                metaslab_t *msp = vd->vdev_ms[m];
                                mutex_enter(&msp->ms_lock);
                                space_map_unload(&msp->ms_map);
@@ -2158,11 +2170,12 @@ count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
 static int
 dump_block_stats(spa_t *spa)
 {
-       zdb_cb_t zcb = { 0 };
+       zdb_cb_t zcb;
        zdb_blkstats_t *zb, *tzb;
        uint64_t norm_alloc, norm_space, total_alloc, total_found;
        int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
        int leaks = 0;
+       int e;
 
        (void) printf("\nTraversing all blocks %s%s%s%s%s...\n",
            (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
@@ -2179,6 +2192,7 @@ dump_block_stats(spa_t *spa)
         * it's not part of any space map) is a double allocation,
         * reference to a freed block, or an unclaimed log block.
         */
+       bzero(&zcb, sizeof(zdb_cb_t));
        zdb_leak_init(spa, &zcb);
 
        /*
@@ -2197,7 +2211,7 @@ dump_block_stats(spa_t *spa)
        if (zcb.zcb_haderrors) {
                (void) printf("\nError counts:\n\n");
                (void) printf("\t%5s  %s\n", "errno", "count");
-               for (int e = 0; e < 256; e++) {
+               for (e = 0; e < 256; e++) {
                        if (zcb.zcb_errors[e] != 0) {
                                (void) printf("\t%5d  %llu\n",
                                    e, (u_longlong_t)zcb.zcb_errors[e]);
@@ -2391,8 +2405,11 @@ dump_simulated_ddt(spa_t *spa)
        avl_tree_t t;
        void *cookie = NULL;
        zdb_ddt_entry_t *zdde;
-       ddt_histogram_t ddh_total = { 0 };
-       ddt_stat_t dds_total = { 0 };
+       ddt_histogram_t ddh_total;
+       ddt_stat_t dds_total;
+
+       bzero(&ddh_total, sizeof (ddt_histogram_t));
+       bzero(&dds_total, sizeof (ddt_stat_t));
 
        avl_create(&t, ddt_entry_compare,
            sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
@@ -2532,7 +2549,7 @@ zdb_dump_block_raw(void *buf, uint64_t size, int flags)
 {
        if (flags & ZDB_FLAG_BSWAP)
                byteswap_uint64_array(buf, size);
-       (void) write(1, buf, size);
+       VERIFY(write(fileno(stdout), buf, size) == size);
 }
 
 static void
@@ -2862,7 +2879,7 @@ find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
        nvlist_t *match = NULL;
        char *name = NULL;
        char *sepp = NULL;
-       char sep;
+       char sep = 0;
        int count = 0;
        importargs_t args = { 0 };
 
@@ -3011,8 +3028,8 @@ main(int argc, char **argv)
        }
 
        kernel_init(FREAD);
-       g_zfs = libzfs_init();
-       ASSERT(g_zfs != NULL);
+       if ((g_zfs = libzfs_init()) == NULL)
+               return (1);
 
        if (dump_all)
                verbose = MAX(verbose, 1);
@@ -3072,8 +3089,11 @@ main(int argc, char **argv)
                                fatal("can't open '%s': %s",
                                    target, strerror(ENOMEM));
                        }
-                       if ((error = spa_import(name, cfg, NULL)) != 0)
-                               error = spa_import_verbatim(name, cfg, NULL);
+                       if ((error = spa_import(name, cfg, NULL,
+                           ZFS_IMPORT_MISSING_LOG)) != 0) {
+                               error = spa_import(name, cfg, NULL,
+                                   ZFS_IMPORT_VERBATIM);
+                       }
                }
        }