Path: blob/main/sys/contrib/openzfs/cmd/zdb/zdb_il.c
48378 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2009 Sun Microsystems, Inc. All rights reserved.23* Copyright (c) 2012 Cyril Plisko. All rights reserved.24* Use is subject to license terms.25*/2627/*28* Copyright (c) 2013, 2017 by Delphix. All rights reserved.29*/3031/*32* Print intent log header and statistics.33*/3435#include <stdio.h>36#include <stdlib.h>37#include <ctype.h>38#include <sys/zfs_context.h>39#include <sys/spa.h>40#include <sys/dmu.h>41#include <sys/stat.h>42#include <sys/resource.h>43#include <sys/zil.h>44#include <sys/zil_impl.h>45#include <sys/spa_impl.h>46#include <sys/abd.h>4748#include "zdb.h"4950static char tab_prefix[4] = "\t\t\t";5152static void53print_log_bp(const blkptr_t *bp, const char *prefix)54{55char blkbuf[BP_SPRINTF_LEN];5657snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);58(void) printf("%s%s\n", prefix, blkbuf);59}6061static void62zil_prt_rec_create(zilog_t *zilog, int txtype, const void *arg)63{64(void) zilog;65const lr_create_t *lrc = arg;66const _lr_create_t *lr = &lrc->lr_create;67time_t crtime = lr->lr_crtime[0];68const char *name, *link;69lr_attr_t *lrattr;7071name = (const char *)&lrc->lr_data[0];7273if (lr->lr_common.lrc_txtype == TX_CREATE_ATTR ||74lr->lr_common.lrc_txtype == TX_MKDIR_ATTR) {75lrattr = (lr_attr_t *)&lrc->lr_data[0];76name += ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);77}7879if (txtype == TX_SYMLINK) {80link = (const char *)&lrc->lr_data[strlen(name) + 1];81(void) printf("%s%s -> %s\n", tab_prefix, name, link);82} else if (txtype != TX_MKXATTR) {83(void) printf("%s%s\n", tab_prefix, name);84}8586(void) printf("%s%s", tab_prefix, ctime(&crtime));87(void) printf("%sdoid %llu, foid %llu, slots %llu, mode %llo\n",88tab_prefix, (u_longlong_t)lr->lr_doid,89(u_longlong_t)LR_FOID_GET_OBJ(lr->lr_foid),90(u_longlong_t)LR_FOID_GET_SLOTS(lr->lr_foid),91(longlong_t)lr->lr_mode);92(void) printf("%suid %llu, gid %llu, gen %llu, rdev 0x%llx\n",93tab_prefix,94(u_longlong_t)lr->lr_uid, (u_longlong_t)lr->lr_gid,95(u_longlong_t)lr->lr_gen, (u_longlong_t)lr->lr_rdev);96}9798static void99zil_prt_rec_remove(zilog_t *zilog, int txtype, const void *arg)100{101(void) zilog, (void) txtype;102const lr_remove_t *lr = arg;103104(void) printf("%sdoid %llu, name %s\n", tab_prefix,105(u_longlong_t)lr->lr_doid, (const char *)&lr->lr_data[0]);106}107108static void109zil_prt_rec_link(zilog_t *zilog, int txtype, const void *arg)110{111(void) zilog, (void) txtype;112const lr_link_t *lr = arg;113114(void) printf("%sdoid %llu, link_obj %llu, name %s\n", tab_prefix,115(u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_link_obj,116(const char *)&lr->lr_data[0]);117}118119static void120zil_prt_rec_rename(zilog_t *zilog, int txtype, const void *arg)121{122(void) zilog, (void) txtype;123const lr_rename_t *lrr = arg;124const _lr_rename_t *lr = &lrr->lr_rename;125const char *snm = (const char *)&lrr->lr_data[0];126const char *tnm = (const char *)&lrr->lr_data[strlen(snm) + 1];127128(void) printf("%ssdoid %llu, tdoid %llu\n", tab_prefix,129(u_longlong_t)lr->lr_sdoid, (u_longlong_t)lr->lr_tdoid);130(void) printf("%ssrc %s tgt %s\n", tab_prefix, snm, tnm);131switch (txtype) {132case TX_RENAME_EXCHANGE:133(void) printf("%sflags RENAME_EXCHANGE\n", tab_prefix);134break;135case TX_RENAME_WHITEOUT:136(void) printf("%sflags RENAME_WHITEOUT\n", tab_prefix);137break;138}139}140141static int142zil_prt_rec_write_cb(void *data, size_t len, void *unused)143{144(void) unused;145char *cdata = data;146147for (size_t i = 0; i < len; i++) {148if (isprint(*cdata))149(void) printf("%c ", *cdata);150else151(void) printf("%2X", *cdata);152cdata++;153}154return (0);155}156157static void158zil_prt_rec_write(zilog_t *zilog, int txtype, const void *arg)159{160const lr_write_t *lr = arg;161abd_t *data;162const blkptr_t *bp = &lr->lr_blkptr;163zbookmark_phys_t zb;164int verbose = MAX(dump_opt['d'], dump_opt['i']);165int error;166167(void) printf("%sfoid %llu, offset %llx, length %llx\n", tab_prefix,168(u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset,169(u_longlong_t)lr->lr_length);170171if (txtype == TX_WRITE2 || verbose < 4)172return;173174if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {175(void) printf("%shas blkptr, %s\n", tab_prefix,176!BP_IS_HOLE(bp) && BP_GET_BIRTH(bp) >=177spa_min_claim_txg(zilog->zl_spa) ?178"will claim" : "won't claim");179print_log_bp(bp, tab_prefix);180181if (verbose < 5)182return;183if (BP_IS_HOLE(bp)) {184(void) printf("\t\t\tLSIZE 0x%llx\n",185(u_longlong_t)BP_GET_LSIZE(bp));186(void) printf("%s<hole>\n", tab_prefix);187return;188}189if (BP_GET_BIRTH(bp) < zilog->zl_header->zh_claim_txg) {190(void) printf("%s<block already committed>\n",191tab_prefix);192return;193}194195ASSERT3U(BP_GET_LSIZE(bp), !=, 0);196SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os),197lr->lr_foid, ZB_ZIL_LEVEL,198lr->lr_offset / BP_GET_LSIZE(bp));199200data = abd_alloc(BP_GET_LSIZE(bp), B_FALSE);201error = zio_wait(zio_read(NULL, zilog->zl_spa,202bp, data, BP_GET_LSIZE(bp), NULL, NULL,203ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &zb));204if (error)205goto out;206} else {207if (verbose < 5)208return;209210/* data is stored after the end of the lr_write record */211data = abd_alloc(lr->lr_length, B_FALSE);212abd_copy_from_buf(data, &lr->lr_data[0], lr->lr_length);213}214215(void) printf("%s", tab_prefix);216(void) abd_iterate_func(data,2170, MIN(lr->lr_length, (verbose < 6 ? 20 : SPA_MAXBLOCKSIZE)),218zil_prt_rec_write_cb, NULL);219(void) printf("\n");220221out:222abd_free(data);223}224225static void226zil_prt_rec_write_enc(zilog_t *zilog, int txtype, const void *arg)227{228(void) txtype;229const lr_write_t *lr = arg;230const blkptr_t *bp = &lr->lr_blkptr;231int verbose = MAX(dump_opt['d'], dump_opt['i']);232233(void) printf("%s(encrypted)\n", tab_prefix);234235if (verbose < 4)236return;237238if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {239(void) printf("%shas blkptr, %s\n", tab_prefix,240!BP_IS_HOLE(bp) && BP_GET_BIRTH(bp) >=241spa_min_claim_txg(zilog->zl_spa) ?242"will claim" : "won't claim");243print_log_bp(bp, tab_prefix);244}245}246247static void248zil_prt_rec_truncate(zilog_t *zilog, int txtype, const void *arg)249{250(void) zilog, (void) txtype;251const lr_truncate_t *lr = arg;252253(void) printf("%sfoid %llu, offset 0x%llx, length 0x%llx\n", tab_prefix,254(u_longlong_t)lr->lr_foid, (longlong_t)lr->lr_offset,255(u_longlong_t)lr->lr_length);256}257258static void259zil_prt_rec_setattr(zilog_t *zilog, int txtype, const void *arg)260{261(void) zilog, (void) txtype;262const lr_setattr_t *lr = arg;263time_t atime = (time_t)lr->lr_atime[0];264time_t mtime = (time_t)lr->lr_mtime[0];265266(void) printf("%sfoid %llu, mask 0x%llx\n", tab_prefix,267(u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_mask);268269if (lr->lr_mask & AT_MODE) {270(void) printf("%sAT_MODE %llo\n", tab_prefix,271(longlong_t)lr->lr_mode);272}273274if (lr->lr_mask & AT_UID) {275(void) printf("%sAT_UID %llu\n", tab_prefix,276(u_longlong_t)lr->lr_uid);277}278279if (lr->lr_mask & AT_GID) {280(void) printf("%sAT_GID %llu\n", tab_prefix,281(u_longlong_t)lr->lr_gid);282}283284if (lr->lr_mask & AT_SIZE) {285(void) printf("%sAT_SIZE %llu\n", tab_prefix,286(u_longlong_t)lr->lr_size);287}288289if (lr->lr_mask & AT_ATIME) {290(void) printf("%sAT_ATIME %llu.%09llu %s", tab_prefix,291(u_longlong_t)lr->lr_atime[0],292(u_longlong_t)lr->lr_atime[1],293ctime(&atime));294}295296if (lr->lr_mask & AT_MTIME) {297(void) printf("%sAT_MTIME %llu.%09llu %s", tab_prefix,298(u_longlong_t)lr->lr_mtime[0],299(u_longlong_t)lr->lr_mtime[1],300ctime(&mtime));301}302}303304static void305zil_prt_rec_setsaxattr(zilog_t *zilog, int txtype, const void *arg)306{307(void) zilog, (void) txtype;308const lr_setsaxattr_t *lr = arg;309310const char *name = (const char *)&lr->lr_data[0];311(void) printf("%sfoid %llu\n", tab_prefix,312(u_longlong_t)lr->lr_foid);313314(void) printf("%sXAT_NAME %s\n", tab_prefix, name);315if (lr->lr_size == 0) {316(void) printf("%sXAT_VALUE NULL\n", tab_prefix);317} else {318(void) printf("%sXAT_VALUE ", tab_prefix);319const char *val = (const char *)&lr->lr_data[strlen(name) + 1];320for (int i = 0; i < lr->lr_size; i++) {321(void) printf("%c", *val);322val++;323}324}325}326327static void328zil_prt_rec_acl(zilog_t *zilog, int txtype, const void *arg)329{330(void) zilog, (void) txtype;331const lr_acl_t *lr = arg;332333(void) printf("%sfoid %llu, aclcnt %llu\n", tab_prefix,334(u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_aclcnt);335}336337static void338zil_prt_rec_clone_range(zilog_t *zilog, int txtype, const void *arg)339{340(void) zilog, (void) txtype;341const lr_clone_range_t *lr = arg;342int verbose = MAX(dump_opt['d'], dump_opt['i']);343344(void) printf("%sfoid %llu, offset %llx, length %llx, blksize %llx\n",345tab_prefix, (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset,346(u_longlong_t)lr->lr_length, (u_longlong_t)lr->lr_blksz);347348if (verbose < 4)349return;350351for (unsigned int i = 0; i < lr->lr_nbps; i++) {352(void) printf("%s[%u/%llu] ", tab_prefix, i + 1,353(u_longlong_t)lr->lr_nbps);354print_log_bp(&lr->lr_bps[i], "");355}356}357358static void359zil_prt_rec_clone_range_enc(zilog_t *zilog, int txtype, const void *arg)360{361(void) zilog, (void) txtype;362const lr_clone_range_t *lr = arg;363int verbose = MAX(dump_opt['d'], dump_opt['i']);364365(void) printf("%s(encrypted)\n", tab_prefix);366367if (verbose < 4)368return;369370for (unsigned int i = 0; i < lr->lr_nbps; i++) {371(void) printf("%s[%u/%llu] ", tab_prefix, i + 1,372(u_longlong_t)lr->lr_nbps);373print_log_bp(&lr->lr_bps[i], "");374}375}376377typedef void (*zil_prt_rec_func_t)(zilog_t *, int, const void *);378typedef struct zil_rec_info {379zil_prt_rec_func_t zri_print;380zil_prt_rec_func_t zri_print_enc;381const char *zri_name;382uint64_t zri_count;383} zil_rec_info_t;384385static zil_rec_info_t zil_rec_info[TX_MAX_TYPE] = {386{.zri_print = NULL, .zri_name = "Total "},387{.zri_print = zil_prt_rec_create, .zri_name = "TX_CREATE "},388{.zri_print = zil_prt_rec_create, .zri_name = "TX_MKDIR "},389{.zri_print = zil_prt_rec_create, .zri_name = "TX_MKXATTR "},390{.zri_print = zil_prt_rec_create, .zri_name = "TX_SYMLINK "},391{.zri_print = zil_prt_rec_remove, .zri_name = "TX_REMOVE "},392{.zri_print = zil_prt_rec_remove, .zri_name = "TX_RMDIR "},393{.zri_print = zil_prt_rec_link, .zri_name = "TX_LINK "},394{.zri_print = zil_prt_rec_rename, .zri_name = "TX_RENAME "},395{.zri_print = zil_prt_rec_write,396.zri_print_enc = zil_prt_rec_write_enc,397.zri_name = "TX_WRITE "},398{.zri_print = zil_prt_rec_truncate, .zri_name = "TX_TRUNCATE "},399{.zri_print = zil_prt_rec_setattr, .zri_name = "TX_SETATTR "},400{.zri_print = zil_prt_rec_acl, .zri_name = "TX_ACL_V0 "},401{.zri_print = zil_prt_rec_acl, .zri_name = "TX_ACL_ACL "},402{.zri_print = zil_prt_rec_create, .zri_name = "TX_CREATE_ACL "},403{.zri_print = zil_prt_rec_create, .zri_name = "TX_CREATE_ATTR "},404{.zri_print = zil_prt_rec_create, .zri_name = "TX_CREATE_ACL_ATTR "},405{.zri_print = zil_prt_rec_create, .zri_name = "TX_MKDIR_ACL "},406{.zri_print = zil_prt_rec_create, .zri_name = "TX_MKDIR_ATTR "},407{.zri_print = zil_prt_rec_create, .zri_name = "TX_MKDIR_ACL_ATTR "},408{.zri_print = zil_prt_rec_write, .zri_name = "TX_WRITE2 "},409{.zri_print = zil_prt_rec_setsaxattr,410.zri_name = "TX_SETSAXATTR "},411{.zri_print = zil_prt_rec_rename, .zri_name = "TX_RENAME_EXCHANGE "},412{.zri_print = zil_prt_rec_rename, .zri_name = "TX_RENAME_WHITEOUT "},413{.zri_print = zil_prt_rec_clone_range,414.zri_print_enc = zil_prt_rec_clone_range_enc,415.zri_name = "TX_CLONE_RANGE "},416};417418static int419print_log_record(zilog_t *zilog, const lr_t *lr, void *arg, uint64_t claim_txg)420{421(void) arg, (void) claim_txg;422int txtype;423int verbose = MAX(dump_opt['d'], dump_opt['i']);424425/* reduce size of txtype to strip off TX_CI bit */426txtype = lr->lrc_txtype;427428ASSERT(txtype != 0 && (uint_t)txtype < TX_MAX_TYPE);429ASSERT(lr->lrc_txg);430431(void) printf("\t\t%s%s len %6llu, txg %llu, seq %llu\n",432(lr->lrc_txtype & TX_CI) ? "CI-" : "",433zil_rec_info[txtype].zri_name,434(u_longlong_t)lr->lrc_reclen,435(u_longlong_t)lr->lrc_txg,436(u_longlong_t)lr->lrc_seq);437438if (txtype && verbose >= 3) {439if (!zilog->zl_os->os_encrypted) {440zil_rec_info[txtype].zri_print(zilog, txtype, lr);441} else if (zil_rec_info[txtype].zri_print_enc) {442zil_rec_info[txtype].zri_print_enc(zilog, txtype, lr);443} else {444(void) printf("%s(encrypted)\n", tab_prefix);445}446}447448zil_rec_info[txtype].zri_count++;449zil_rec_info[0].zri_count++;450451return (0);452}453454static int455print_log_block(zilog_t *zilog, const blkptr_t *bp, void *arg,456uint64_t claim_txg)457{458(void) arg;459char blkbuf[BP_SPRINTF_LEN + 10];460int verbose = MAX(dump_opt['d'], dump_opt['i']);461const char *claim;462463if (verbose <= 3)464return (0);465466if (verbose >= 5) {467(void) strcpy(blkbuf, ", ");468snprintf_blkptr(blkbuf + strlen(blkbuf),469sizeof (blkbuf) - strlen(blkbuf), bp);470} else {471blkbuf[0] = '\0';472}473474if (claim_txg != 0)475claim = "already claimed";476else if (BP_GET_BIRTH(bp) >= spa_min_claim_txg(zilog->zl_spa))477claim = "will claim";478else479claim = "won't claim";480481(void) printf("\tBlock seqno %llu, %s%s\n",482(u_longlong_t)bp->blk_cksum.zc_word[ZIL_ZC_SEQ], claim, blkbuf);483484return (0);485}486487static void488print_log_stats(int verbose)489{490unsigned i, w, p10;491492if (verbose > 3)493(void) printf("\n");494495if (zil_rec_info[0].zri_count == 0)496return;497498for (w = 1, p10 = 10; zil_rec_info[0].zri_count >= p10; p10 *= 10)499w++;500501for (i = 0; i < TX_MAX_TYPE; i++)502if (zil_rec_info[i].zri_count || verbose >= 3)503(void) printf("\t\t%s %*llu\n",504zil_rec_info[i].zri_name, w,505(u_longlong_t)zil_rec_info[i].zri_count);506(void) printf("\n");507}508509void510dump_intent_log(zilog_t *zilog)511{512const zil_header_t *zh = zilog->zl_header;513int verbose = MAX(dump_opt['d'], dump_opt['i']);514int i;515516if (BP_IS_HOLE(&zh->zh_log) || verbose < 1)517return;518519(void) printf("\n ZIL header: claim_txg %llu, "520"claim_blk_seq %llu, claim_lr_seq %llu",521(u_longlong_t)zh->zh_claim_txg,522(u_longlong_t)zh->zh_claim_blk_seq,523(u_longlong_t)zh->zh_claim_lr_seq);524(void) printf(" replay_seq %llu, flags 0x%llx\n",525(u_longlong_t)zh->zh_replay_seq, (u_longlong_t)zh->zh_flags);526527for (i = 0; i < TX_MAX_TYPE; i++)528zil_rec_info[i].zri_count = 0;529530/* see comment in zil_claim() or zil_check_log_chain() */531if (zilog->zl_spa->spa_uberblock.ub_checkpoint_txg != 0 &&532zh->zh_claim_txg == 0)533return;534535if (verbose >= 2) {536(void) printf("\n");537(void) zil_parse(zilog, print_log_block, print_log_record, NULL,538zh->zh_claim_txg, B_FALSE);539print_log_stats(verbose);540}541}542543544