#include <roken.h>
#include <ctype.h>
-#include <errno.h>
-#include <sys/stat.h>
-#include <stdio.h>
-#include <string.h>
-#ifdef AFS_NT40_ENV
-#include <fcntl.h>
-#include <time.h>
-#include <io.h>
-#else
-#include <sys/param.h>
+
+#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
-#include <sys/time.h>
#endif
+
#include <afs/cmd.h>
#include <afs/dir.h>
-
-#include <rx/xdr.h>
#include <afs/afsint.h>
-#include "nfs.h"
#include <afs/errors.h>
+#include <afs/acl.h>
+#include <afs/prs_fs.h>
+#include <rx/rx_queue.h>
+
+#include "nfs.h"
#include "lock.h"
-#include "lwp.h"
-#include <afs/afssyscalls.h>
#include "ihandle.h"
#include "vnode.h"
#include "volume.h"
#include "partition.h"
-#include "viceinode.h"
-#include <afs/afssyscalls.h>
-#include <afs/afsutil.h>
-
-#ifdef _AIX
-#include <time.h>
-#endif
-
-#include <dirent.h>
-
-#ifdef O_LARGEFILE
-#define afs_stat stat64
-#define afs_fstat fstat64
-#define afs_open open64
-#else /* !O_LARGEFILE */
-#define afs_stat stat
-#define afs_fstat fstat
-#define afs_open open
-#endif /* !O_LARGEFILE */
-
-int DumpVnodes = 0; /* Dump everything, i.e. summary of all vnodes */
-int DumpInodeNumber = 0; /* Dump inode numbers with vnodes */
-int DumpDate = 0; /* Dump vnode date (server modify date) with vnode */
-int InodeTimes = 0; /* Dump some of the dates associated with inodes */
-#if defined(AFS_NAMEI_ENV)
-int PrintFileNames = 0;
-#endif
-int online = 0;
-int dheader = 0;
-int dsizeOnly = 0, totvolsize = 0, Vauxsize = 0, Vdiskused = 0, Vvnodesize =
- 0;
-int Vvnodesize_k = 0, Vauxsize_k = 0;
-int Totvolsize = 0, TVauxsize = 0, TVdiskused = 0, TVvnodesize = 0;
-int Stotvolsize = 0, SVauxsize = 0, SVdiskused = 0, SVvnodesize = 0;
-int fixheader = 0, saveinodes = 0, orphaned = 0;
-int VolumeChanged;
+#include "salvage.h"
+#include "daemon_com_inline.h"
+#include "fssync_inline.h"
+#include "vol-info.h"
+
+static int volinfo_init = 0;
+static const char *progname = "";
+static const char *PLACEHOLDER = "-";
+
+
+/* Numeric column type ids */
+typedef enum columnType {
+#define c(x) col_##x,
+ VOLSCAN_COLUMNS
+#undef c
+ max_column_type
+} columnType;
+
+struct columnName {
+ columnType type;
+ const char *name;
+};
+
+/* Table of id:name tuples of possible columns. */
+struct columnName ColumnName[] = {
+#define c(x) { col_##x, #x },
+ VOLSCAN_COLUMNS
+#undef c
+ {max_column_type, NULL}
+};
+
+
+/* VnodeDetails union descriminator */
+typedef enum {
+ VNODE_U_NONE,
+ VNODE_U_MOUNT,
+ VNODE_U_SYMLINK,
+ VNODE_U_POS_ACCESS,
+ VNODE_U_NEG_ACCESS
+} vnode_details_u_t;
+
+struct VnodeDetails {
+ Volume *vp;
+ VnodeClass class;
+ VnodeDiskObject *vnode;
+ VnodeId vnodeNumber;
+ afs_foff_t offset;
+ int index;
+ char *path;
+ vnode_details_u_t t;
+ union {
+ struct {
+ char type;
+ char *cell;
+ char *vol;
+ } mnt;
+ char *target;
+ struct acl_accessEntry *access;
+ } u;
+};
+
+
+static int NeedDirIndex; /**< Large vnode index handle is needed for path lookups. */
+static FdHandle_t *DirIndexFd = NULL; /**< Current large vnode index handle for path lookups. */
+
+static int NumOutputColumns = 0;
+static columnType OutputColumn[max_column_type];
+
+/**
+ * Volume size running totals
+ */
+struct sizeTotals {
+ afs_uint64 diskused_k; /**< volume size from disk data file, in kilobytes */
+ afs_uint64 auxsize; /**< size of header files, in bytes */
+ afs_uint64 auxsize_k; /**< size of header files, in kilobytes */
+ afs_uint64 vnodesize; /**< size of the large and small vnodes, in bytes */
+ afs_uint64 vnodesize_k; /**< size of the large and small vnodes, in kilobytes */
+ afs_uint64 size_k; /**< size of headers and vnodes, in kilobytes */
+};
+
+static struct sizeTotals volumeTotals = { 0, 0, 0, 0, 0, 0 };
+static struct sizeTotals partitionTotals = { 0, 0, 0, 0, 0, 0 };
+static struct sizeTotals serverTotals = { 0, 0, 0, 0, 0, 0 };
+static int PrintingVolumeSizes = 0; /*print volume size lines */
+
+/**
+ * List of procedures to call when scanning vnodes.
+ */
+struct VnodeScanProc {
+ struct opr_queue link;
+ const char *heading;
+ void (*proc) (struct VolInfoOpt *opt, struct VnodeDetails * vdp);
+};
+static struct opr_queue VnodeScanLists[nVNODECLASSES];
/* Forward Declarations */
-void PrintHeader(Volume * vp);
-void HandleAllPart(void);
-void HandlePart(struct DiskPartition64 *partP);
-void HandleVolume(struct DiskPartition64 *partP, char *name);
-struct DiskPartition64 *FindCurrentPartition(void);
-Volume *AttachVolume(struct DiskPartition64 *dp, char *volname,
+static void PrintHeader(Volume * vp);
+static void HandleAllPart(struct VolInfoOpt *opt);
+static void HandlePart(struct VolInfoOpt *opt, struct DiskPartition64 *partP);
+static void HandleVolume(struct VolInfoOpt *opt, struct DiskPartition64 *partP, char *name);
+static struct DiskPartition64 *FindCurrentPartition(void);
+static Volume *AttachVolume(struct VolInfoOpt *opt, struct DiskPartition64 *dp, char *volname,
struct VolumeHeader *header);
-#if defined(AFS_NAMEI_ENV)
-void PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
- Inode ino, Volume * vp);
-#else
-void PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
- Inode ino);
-#endif
-void PrintVnodes(Volume * vp, VnodeClass class);
-
-char *
+static void HandleVnodes(struct VolInfoOpt *opt, Volume * vp, VnodeClass class);
+static void PrintColumnHeading(struct VolInfoOpt *opt);
+static void PrintColumns(struct VolInfoOpt *opt, struct VnodeDetails *vdp, const char *desc);
+
+/* externs */
+extern void SetSalvageDirHandle(DirHandle * dir, afs_int32 volume,
+ Device device, Inode inode,
+ int *volumeChanged);
+extern void FidZap(DirHandle * file);
+
+/**
+ * Format time as a timestamp string
+ *
+ * @param[in] date time value to format
+ *
+ * @return timestamp string in the form YYYY/MM/DD.hh:mm:ss
+ *
+ * @note A static array of 8 strings are stored by this
+ * function. The array slots are overwritten, so the
+ * caller must not reference the returned string after
+ * seven additional calls to this function.
+ */
+static char *
date(time_t date)
{
#define MAX_DATE_RESULT 100
char buf[32];
(void)strftime(buf, 32, "%Y/%m/%d.%H:%M:%S", tm); /* NT does not have %T */
- (void)afs_snprintf(results[next = (next + 1) & 7], MAX_DATE_RESULT,
- "%lu (%s)", (unsigned long)date, buf);
+ snprintf(results[next = (next + 1) & 7], MAX_DATE_RESULT,
+ "%lu (%s)", (unsigned long)date, buf);
return results[next];
}
-#ifndef AFS_NT40_ENV
-#include "AFS_component_version_number.c"
+#ifdef AFS_NT40_ENV
+/**
+ * Format file time as a timestamp string
+ *
+ * @param[in] ft file time
+ *
+ * @return timestamp string in the form YYYY/MM/DD.hh:mm:ss
+ *
+ * @note A static array of 8 strings are stored by this
+ * function. The array slots are overwritten, so the
+ * caller must not reference the returned string after
+ * seven additional calls to this function.
+ */
+static char *
+NT_date(FILETIME * ft)
+{
+ static char result[8][64];
+ static int next = 0;
+ SYSTEMTIME st;
+ FILETIME lft;
+
+ if (!FileTimeToLocalFileTime(ft, &lft)
+ || !FileTimeToSystemTime(&lft, &st)) {
+ fprintf(stderr, "%s: Time conversion failed.\n", progname);
+ exit(1);
+ }
+ sprintf(result[next = ((next + 1) & 7)], "%4d/%02d/%02d.%2d:%2d:%2d",
+ st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
+ return result[next];
+}
#endif
-char name[VMAXPATHLEN];
+/**
+ * Add vnode size to the running volume totals.
+ *
+ * @param[in] vdp vnode details object
+ *
+ * @return none
+ */
+void
+volinfo_AddVnodeToSizeTotals(struct VolInfoOpt *opt, struct VnodeDetails *vdp)
+{
+ afs_fsize_t fileLength;
+
+ VNDISK_GET_LEN(fileLength, vdp->vnode);
+ if (fileLength > 0) {
+ volumeTotals.vnodesize += fileLength;
+ volumeTotals.vnodesize_k += fileLength / 1024;
+ }
+}
-char BU[1000];
+/**
+ * Print the volume size table heading line, if needed.
+ *
+ * @return none
+ */
+static void
+PrintVolumeSizeHeading(void)
+{
+ if (!PrintingVolumeSizes) {
+ printf
+ ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
+ PrintingVolumeSizes = 1;
+ }
+}
-int
-ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version)
+/**
+ * Accumulate totals.
+ *
+ * @return 0
+ */
+static void
+AddSizeTotals(struct sizeTotals *a, struct sizeTotals *b)
+{
+ a->diskused_k += b->diskused_k;
+ a->auxsize += b->auxsize;
+ a->auxsize_k += b->auxsize_k;
+ a->vnodesize += b->vnodesize;
+ a->vnodesize_k += b->vnodesize_k;
+ a->size_k += b->size_k;
+}
+
+/**
+ * Print the sizes for a volume.
+ *
+ * @return none
+ */
+static void
+PrintVolumeSizes(Volume * vp)
+{
+ afs_int64 diff_k = volumeTotals.size_k - volumeTotals.diskused_k;
+
+ PrintVolumeSizeHeading();
+ printf("%" AFS_VOLID_FMT "\t%9llu%9llu%10llu%10llu%9lld\t%24s\n",
+ afs_printable_VolumeId_lu(V_id(vp)),
+ volumeTotals.diskused_k,
+ volumeTotals.auxsize_k, volumeTotals.vnodesize_k,
+ volumeTotals.size_k, diff_k, V_name(vp));
+}
+
+/**
+ * Print the size totals for the partition.
+ *
+ * @return none
+ */
+static void
+PrintPartitionTotals(afs_uint64 nvols)
+{
+ afs_int64 diff_k = partitionTotals.size_k - partitionTotals.diskused_k;
+
+ PrintVolumeSizeHeading();
+ printf("\nPart Totals %12llu%9llu%10llu%10llu%9lld (%llu volumes)\n\n",
+ partitionTotals.diskused_k, partitionTotals.auxsize,
+ partitionTotals.vnodesize, partitionTotals.size_k, diff_k, nvols);
+ PrintingVolumeSizes = 0;
+}
+
+/**
+ * Print the size totals for all the partitions.
+ *
+ * @return none
+ */
+static void
+PrintServerTotals(void)
+{
+ afs_int64 diff_k = serverTotals.size_k - serverTotals.diskused_k;
+
+ printf("\nServer Totals%12lld%9lld%10lld%10lld%9lld\n",
+ serverTotals.diskused_k, serverTotals.auxsize,
+ serverTotals.vnodesize, serverTotals.size_k, diff_k);
+}
+
+/**
+ * Read a volume header file
+ *
+ * @param[in] ih ihandle of the header file
+ * @param[in] to destination
+ * @param[in] size expected header size
+ * @param[in] magic expected header magic number
+ * @param[in] version expected header version number
+ *
+ * @return error code
+ * @retval 0 success
+ * @retval -1 failed to read file
+ */
+static int
+ReadHdr1(struct VolInfoOpt *opt, IHandle_t * ih, char *to, afs_sfsize_t size,
+ u_int magic, u_int version)
{
struct versionStamp *vsn;
int bad = 0;
if (vsn->magic != magic) {
bad++;
- printf("Inode %s: Bad magic %x (%x): IGNORED\n",
- PrintInode(NULL, ih->ih_ino), vsn->magic, magic);
+ fprintf(stderr, "%s: Inode %s: Bad magic %x (%x): IGNORED\n",
+ progname, PrintInode(NULL, ih->ih_ino), vsn->magic, magic);
}
/* Check is conditional, in case caller wants to inspect version himself */
if (version && vsn->version != version) {
bad++;
- printf("Inode %s: Bad version %x (%x): IGNORED\n",
- PrintInode(NULL, ih->ih_ino), vsn->version, version);
+ fprintf(stderr, "%s: Inode %s: Bad version %x (%x): IGNORED\n",
+ progname,
+ PrintInode(NULL, ih->ih_ino), vsn->version, version);
}
- if (bad && fixheader) {
+ if (bad && opt->fixHeader) {
vsn->magic = magic;
vsn->version = version;
printf("Special index inode %s has a bad header. Reconstructing...\n",
PrintInode(NULL, ih->ih_ino));
code = IH_IWRITE(ih, 0, to, size);
if (code != size) {
- printf("Write failed; header left in damaged state\n");
- }
- } else {
- if (!dsizeOnly && !saveinodes) {
- printf("Inode %s: Good magic %x and version %x\n",
- PrintInode(NULL, ih->ih_ino), magic, version);
+ fprintf(stderr,
+ "%s: Write failed for inode %s; header left in damaged state\n",
+ progname, PrintInode(NULL, ih->ih_ino));
}
}
+ if (!bad && opt->dumpInfo) {
+ printf("Inode %s: Good magic %x and version %x\n",
+ PrintInode(NULL, ih->ih_ino), magic, version);
+ }
return 0;
}
-
-Volume *
-AttachVolume(struct DiskPartition64 * dp, char *volname,
- struct VolumeHeader * header)
+/**
+ * Simplified attach volume
+ *
+ * param[in] dp vice disk partition object
+ * param[in] volname volume header file name
+ * param[in] header volume header object
+ *
+ * @return volume object or null on error
+ */
+static Volume *
+AttachVolume(struct VolInfoOpt *opt, struct DiskPartition64 *dp,
+ char *volname, struct VolumeHeader *header)
{
Volume *vp;
afs_int32 ec = 0;
+ if (opt->checkout) {
+ afs_int32 code;
+ SYNC_response response;
+ VolumeId volid = header->id;
+
+ memset(&response, 0, sizeof(response));
+ code =
+ FSYNC_VolOp(volid, dp->name, FSYNC_VOL_NEEDVOLUME, V_DUMP,
+ &response);
+ if (code != SYNC_OK) {
+ if (response.hdr.reason == FSYNC_SALVAGE) {
+ fprintf(stderr,
+ "%s: file server says volume %lu is salvaging.\n",
+ progname, afs_printable_uint32_lu(volid));
+ return NULL;
+ } else {
+ fprintf(stderr,
+ "%s: attach of volume %lu denied by file server.\n",
+ progname, afs_printable_uint32_lu(volid));
+ return NULL;
+ }
+ }
+ }
+
vp = (Volume *) calloc(1, sizeof(Volume));
+ if (!vp) {
+ fprintf(stderr, "%s: Failed to allocate volume object.\n", progname);
+ return NULL;
+ }
vp->specialStatus = 0;
vp->device = dp->device;
vp->partition = dp;
vp->shuttingDown = 0;
vp->goingOffline = 0;
vp->nUsers = 1;
- vp->header = (struct volHeader *)calloc(1, sizeof(*vp->header));
- ec = ReadHdr1(V_diskDataHandle(vp), (char *)&V_disk(vp),
+ vp->header = calloc(1, sizeof(*vp->header));
+ if (!vp->header) {
+ fprintf(stderr, "%s: Failed to allocate volume header.\n", progname);
+ free(vp);
+ return NULL;
+ }
+ ec = ReadHdr1(opt, V_diskDataHandle(vp), (char *)&V_disk(vp),
sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
if (!ec) {
struct IndexFileHeader iHead;
- ec = ReadHdr1(vp->vnodeIndex[vSmall].handle, (char *)&iHead,
+ ec = ReadHdr1(opt, vp->vnodeIndex[vSmall].handle, (char *)&iHead,
sizeof(iHead), SMALLINDEXMAGIC, SMALLINDEXVERSION);
}
if (!ec) {
struct IndexFileHeader iHead;
- ec = ReadHdr1(vp->vnodeIndex[vLarge].handle, (char *)&iHead,
+ ec = ReadHdr1(opt, vp->vnodeIndex[vLarge].handle, (char *)&iHead,
sizeof(iHead), LARGEINDEXMAGIC, LARGEINDEXVERSION);
}
#ifdef AFS_NAMEI_ENV
if (!ec) {
struct versionStamp stamp;
- ec = ReadHdr1(V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
+ ec = ReadHdr1(opt, V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
LINKTABLEMAGIC, LINKTABLEVERSION);
}
#endif
return vp;
}
+/**
+ * Simplified detach volume
+ *
+ * param[in] vp volume object from AttachVolume
+ *
+ * @return none
+ */
+static void
+DetachVolume(struct VolInfoOpt *opt, Volume * vp)
+{
+ if (opt->checkout) {
+ afs_int32 code;
+ SYNC_response response;
+ memset(&response, 0, sizeof(response));
+
+ code = FSYNC_VolOp(V_id(vp), V_partition(vp)->name,
+ FSYNC_VOL_ON, FSYNC_WHATEVER, &response);
+ if (code != SYNC_OK) {
+ fprintf(stderr, "%s: FSSYNC error %d (%s)\n", progname, code,
+ SYNC_res2string(code));
+ fprintf(stderr, "%s: protocol response code was %d (%s)\n",
+ progname, response.hdr.response,
+ SYNC_res2string(response.hdr.response));
+ fprintf(stderr, "%s: protocol reason code was %d (%s)\n",
+ progname, response.hdr.reason,
+ FSYNC_reason2string(response.hdr.reason));
+ }
+ }
+
+ IH_RELEASE(vp->vnodeIndex[vLarge].handle);
+ IH_RELEASE(vp->vnodeIndex[vSmall].handle);
+ IH_RELEASE(vp->diskDataHandle);
+ IH_RELEASE(V_linkHandle(vp));
+ free(vp->header);
+ free(vp);
+}
+/**
+ * Convert the partition device number into a partition name.
+ *
+ * @param[in] partId partition number, 0 to 254
+ * @param[out] partName buffer to hold partition name (e.g. /vicepa)
+ * @param[in] partNameSize size of partName buffer
+ *
+ * @return status
+ * @retval 0 success
+ * @retval -1 error, partId is out of range
+ * @retval -2 error, partition name exceeds partNameSize
+ */
static int
-handleit(struct cmd_syndesc *as, void *arock)
+GetPartitionName(afs_uint32 partId, char *partName, afs_size_t partNameSize)
+{
+ const afs_uint32 OLD_NUM_DEVICES = 26; /* a..z */
+
+ if (partId < OLD_NUM_DEVICES) {
+ if (partNameSize < 8) {
+ return -2;
+ }
+ strlcpy(partName, "/vicep", partNameSize);
+ partName[6] = partId + 'a';
+ partName[7] = '\0';
+ return 0;
+ }
+ if (partId < VOLMAXPARTS) {
+ if (partNameSize < 9) {
+ return -2;
+ }
+ strlcpy(partName, "/vicep", partNameSize);
+ partId -= OLD_NUM_DEVICES;
+ partName[6] = 'a' + (partId / OLD_NUM_DEVICES);
+ partName[7] = 'a' + (partId % OLD_NUM_DEVICES);
+ partName[8] = '\0';
+ return 0;
+ }
+ return -1;
+}
+
+/**
+ * Scan the volumes in the partitions
+ *
+ * Scan the specified volume in the specified partition if both
+ * are given. Scan all the volumes in the specified partition if
+ * only the partition is given. If neither a partition nor volume
+ * is given, scan all the volumes in all the partitions. If only
+ * the volume is given, attempt to find it in the current working
+ * directory.
+ *
+ * @param[in] partNameOrId partition name or id to be scannned
+ * @param[in] volumeId volume id to be scanned
+ *
+ * @return 0 if successful
+ */
+int
+volinfo_ScanPartitions(struct VolInfoOpt *opt, char *partNameOrId, VolumeId volumeId)
{
- struct cmd_item *ti;
int err = 0;
- afs_uint32 volumeId = 0;
- char *partName = 0;
+ char partName[64] = "";
struct DiskPartition64 *partP = NULL;
-
-#ifndef AFS_NT40_ENV
- if (geteuid() != 0) {
- printf("vol-info must be run as root; sorry\n");
- exit(1);
+ if (opt->checkout) {
+ if (!FSYNC_clientInit()) {
+ fprintf(stderr, "%s: Failed to connect to fileserver.\n",
+ progname);
+ return 1;
+ }
}
-#endif
- if (as->parms[0].items)
- online = 1;
- else
- online = 0;
- if (as->parms[1].items)
- DumpVnodes = 1;
- else
- DumpVnodes = 0;
- if (as->parms[2].items)
- DumpDate = 1;
- else
- DumpDate = 0;
- if (as->parms[3].items)
- DumpInodeNumber = 1;
- else
- DumpInodeNumber = 0;
- if (as->parms[4].items)
- InodeTimes = 1;
- else
- InodeTimes = 0;
- if ((ti = as->parms[5].items))
- partName = ti->data;
- if ((ti = as->parms[6].items))
- volumeId = strtoul(ti->data, NULL, 10);
- if (as->parms[7].items)
- dheader = 1;
- else
- dheader = 0;
- if (as->parms[8].items) {
- dsizeOnly = 1;
- dheader = 1;
- DumpVnodes = 1;
- } else
- dsizeOnly = 0;
- if (as->parms[9].items) {
- fixheader = 1;
- } else
- fixheader = 0;
- if (as->parms[10].items) {
- saveinodes = 1;
- dheader = 1;
- DumpVnodes = 1;
- } else
- saveinodes = 0;
- if (as->parms[11].items) {
- orphaned = 1;
- DumpVnodes = 1;
- } else
-#if defined(AFS_NAMEI_ENV)
- if (as->parms[12].items) {
- PrintFileNames = 1;
- DumpVnodes = 1;
- } else
-#endif
- orphaned = 0;
- DInit(10);
+ /* Allow user to specify partition by name or id. */
+ if (partNameOrId) {
+ afs_uint32 partId = volutil_GetPartitionID(partNameOrId);
+ if (partId == -1) {
+ fprintf(stderr, "%s: Could not parse '%s' as a partition name.\n",
+ progname, partNameOrId);
+ err = 1;
+ goto cleanup;
+ }
+ if (GetPartitionName(partId, partName, sizeof(partName))) {
+ fprintf(stderr,
+ "%s: Could not format '%s' as a partition name.\n",
+ progname, partNameOrId);
+ err = 1;
+ goto cleanup;
+ }
+ }
err = VAttachPartitions();
if (err) {
- printf("%d partitions had errors during attach.\n", err);
+ fprintf(stderr, "%s: %d partitions had errors during attach.\n",
+ progname, err);
+ err = 1;
+ goto cleanup;
}
- if (partName) {
+ if (partName[0]) {
partP = VGetPartition(partName, 0);
if (!partP) {
- printf("%s is not an AFS partition name on this server.\n",
- partName);
- exit(1);
+ fprintf(stderr,
+ "%s: %s is not an AFS partition name on this server.\n",
+ progname, partName);
+ err = 1;
+ goto cleanup;
}
}
if (!volumeId) {
+ if (opt->printHeading) {
+ PrintColumnHeading(opt);
+ }
if (!partP) {
- HandleAllPart();
+ HandleAllPart(opt);
} else {
- HandlePart(partP);
+ HandlePart(opt, partP);
}
} else {
char name1[128];
if (!partP) {
partP = FindCurrentPartition();
if (!partP) {
- printf("Current partition is not a vice partition.\n");
- exit(1);
+ fprintf(stderr,
+ "%s: Current partition is not a vice partition.\n",
+ progname);
+ err = 1;
+ goto cleanup;
}
}
- (void)afs_snprintf(name1, sizeof name1, VFORMAT,
- afs_printable_uint32_lu(volumeId));
- if (dsizeOnly && !saveinodes)
- printf
- ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
- HandleVolume(partP, name1);
+ snprintf(name1, sizeof name1, VFORMAT,
+ afs_printable_VolumeId_lu(volumeId));
+ if (opt->printHeading) {
+ PrintColumnHeading(opt);
+ }
+ HandleVolume(opt, partP, name1);
+ }
+
+ cleanup:
+ if (opt->checkout) {
+ FSYNC_clientFinis();
+ }
+ return err;
+}
+
+/**
+ * Add a vnode scanning procedure
+ *
+ * @param[in] class vnode class for this handler
+ * @param[in] proc handler proc to be called by HandleVnodes
+ * @param[in] heading optional string to pring before scanning vnodes
+ *
+ * @return none
+ */
+void
+volinfo_AddVnodeHandler(int vnodeClass,
+ void (*proc) (struct VolInfoOpt * opt,
+ struct VnodeDetails * vdp), const char *heading)
+{
+ struct VnodeScanProc *entry = malloc(sizeof(struct VnodeScanProc));
+ entry->proc = proc;
+ entry->heading = heading;
+ opr_queue_Append(&VnodeScanLists[vnodeClass], (struct opr_queue *)entry);
+}
+
+/**
+ * Initialize the vol-info module.
+ */
+int
+volinfo_Init(const char *aprogname)
+{
+ if (!volinfo_init) {
+ progname = aprogname; /* for error messages */
+#ifndef AFS_NT40_ENV
+ if (geteuid() != 0) {
+ fprintf(stderr, "%s: Must be run as root.\n", progname);
+ return EPERM;
+ }
+#endif
+ DInit(1024);
+ VInitVnodes(vLarge, 0);
+ VInitVnodes(vSmall, 0);
+ opr_queue_Init(&VnodeScanLists[vLarge]);
+ opr_queue_Init(&VnodeScanLists[vSmall]);
+ volinfo_init = 1;
+ }
+ return 0;
+}
+
+/**
+ * Alloc an options structure with default values.
+ *
+ * The caller must free the options pointer after use.
+ *
+ * @param[out] optp address of options pointer
+ * @return 0 on success
+ */
+int
+volinfo_Options(struct VolInfoOpt **optp)
+{
+ struct VolInfoOpt *opt;
+
+ opt = calloc(1, sizeof(struct VolInfoOpt));
+ if (!opt) {
+ fprintf(stderr, "%s: Failed to allocate options (%d).\n", progname, ENOMEM);
+ return ENOMEM;
}
+ opt->dumpInfo = 1;
+ gethostname(opt->hostname, sizeof(opt->hostname));
+ opt->columnDelim[0] = ' ';
+ opt->checkMagic = 1;
+ *optp = opt;
return 0;
}
+/**
+ * Add a column type to be displayed.
+ *
+ * @param[in] name column type name
+ *
+ * @return error code
+ * @retval 0 success
+ * @retval 1 too many columns
+ * @retval 2 invalid column name
+ */
+int
+volinfo_AddOutputColumn(char *name)
+{
+ int i;
+
+ if (NumOutputColumns >= sizeof(OutputColumn) / sizeof(*OutputColumn)) {
+ fprintf(stderr, "%s: Too many output columns (%d).\n", progname,
+ NumOutputColumns);
+ return 1;
+ }
+ for (i = 0; i < max_column_type; i++) {
+ if (!strcmp(ColumnName[i].name, name)) {
+ columnType t = ColumnName[i].type;
+ OutputColumn[NumOutputColumns++] = t;
+
+ if (t == col_path) {
+ NeedDirIndex = 1;
+ }
+ return 0;
+ }
+ }
+ return 2;
+}
+
+
+/**
+ * Determine if the current directory is a vice partition
+ *
+ * @return disk partition object
+ */
#ifdef AFS_NT40_ENV
#include <direct.h>
-struct DiskPartition64 *
+static struct DiskPartition64 *
FindCurrentPartition(void)
{
int dr = _getdrive();
break;
}
if (!dp) {
- printf("Current drive is not a valid vice partition.\n");
+ fprintf(stderr, "%s: Current drive is not a valid vice partition.\n",
+ progname);
}
return dp;
}
#else
-struct DiskPartition64 *
+static struct DiskPartition64 *
FindCurrentPartition(void)
{
char partName[1024];
struct DiskPartition64 *dp;
if (!getcwd(partName, 1023)) {
+ fprintf(stderr, "%s: Failed to get current working directory: ",
+ progname);
perror("pwd");
- exit(1);
+ return NULL;
}
- p = strchr(&partName[1], '/');
+ p = strchr(&partName[1], OS_DIRSEPC);
if (p) {
tmp = *p;
*p = '\0';
if (!(dp = VGetPartition(partName, 0))) {
if (tmp)
*p = tmp;
- printf("%s is not a valid vice partition.\n", partName);
- exit(1);
+ fprintf(stderr, "%s: %s is not a valid vice partition.\n", progname,
+ partName);
+ return NULL;
}
return dp;
}
#endif
-void
-HandleAllPart(void)
+/**
+ * Scan and handle all the partitions detected on this server
+ *
+ * @return none
+ */
+static void
+HandleAllPart(struct VolInfoOpt *opt)
{
struct DiskPartition64 *partP;
-
for (partP = DiskPartitionList; partP; partP = partP->next) {
- printf("Processing Partition %s:\n", partP->name);
- HandlePart(partP);
- Stotvolsize += Totvolsize;
- SVauxsize += TVauxsize;
- SVvnodesize += TVvnodesize;
- SVdiskused += TVdiskused;
+ if (opt->dumpInfo || opt->saveInodes || opt->showSizes) {
+ printf("Processing Partition %s:\n", partP->name);
+ }
+ HandlePart(opt, partP);
+ if (opt->showSizes) {
+ AddSizeTotals(&serverTotals, &partitionTotals);
+ }
}
- if (dsizeOnly) {
- printf("\nServer Totals%12d%9d%10d%10d%9d\n", SVdiskused, SVauxsize,
- SVvnodesize, Stotvolsize, Stotvolsize - SVdiskused);
+ if (opt->showSizes) {
+ PrintServerTotals();
}
}
-
-void
-HandlePart(struct DiskPartition64 *partP)
+/**
+ * Scan the partition and handle volumes
+ *
+ * @param[in] partP disk partition to scan
+ *
+ * @return none
+ */
+static void
+HandlePart(struct VolInfoOpt *opt, struct DiskPartition64 *partP)
{
- int nvols = 0;
+ afs_int64 nvols = 0;
DIR *dirp;
struct dirent *dp;
#ifdef AFS_NT40_ENV
#endif
if ((dirp = opendir(p)) == NULL) {
- printf("Can't read directory %s; giving up\n", p);
- exit(1);
+ fprintf(stderr, "%s: Can't read directory %s; giving up\n", progname,
+ p);
+ return;
}
- if (dsizeOnly && !saveinodes)
- printf
- ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
while ((dp = readdir(dirp))) {
p = (char *)strrchr(dp->d_name, '.');
if (p != NULL && strcmp(p, VHDREXT) == 0) {
- HandleVolume(partP, dp->d_name);
- Totvolsize += totvolsize;
- TVauxsize += Vauxsize;
- TVvnodesize += Vvnodesize;
- TVdiskused += Vdiskused;
- nvols++;
+ HandleVolume(opt, partP, dp->d_name);
+ if (opt->showSizes) {
+ nvols++;
+ AddSizeTotals(&partitionTotals, &volumeTotals);
+ }
}
}
closedir(dirp);
- if (dsizeOnly) {
- printf("\nPart Totals %12d%9d%10d%10d%9d (%d volumes)\n\n",
- TVdiskused, TVauxsize, TVvnodesize, Totvolsize,
- Totvolsize - TVdiskused, nvols);
+ if (opt->showSizes) {
+ PrintPartitionTotals(nvols);
}
}
-
-void
-HandleVolume(struct DiskPartition64 *dp, char *name)
+/**
+ * Inspect a volume header special file.
+ *
+ * @param[in] name descriptive name of the type of header special file
+ * @param[in] dp partition object for this volume
+ * @param[in] header header object for this volume
+ * @param[in] inode fileserver inode number for this header special file
+ * @param[inout] psize total of the header special file sizes
+ *
+ * @return none
+ */
+static void
+HandleSpecialFile(struct VolInfoOpt *opt, const char *name, struct DiskPartition64 *dp,
+ struct VolumeHeader *header, Inode inode,
+ afs_sfsize_t * psize)
{
- struct VolumeHeader header;
- struct VolumeDiskHeader diskHeader;
- struct afs_stat status, stat;
- int fd;
- Volume *vp;
- IHandle_t *ih;
- char headerName[1024];
+ afs_sfsize_t size = -1;
+ IHandle_t *ih = NULL;
+ FdHandle_t *fdP = NULL;
+#ifdef AFS_NAMEI_ENV
+ namei_t filename;
+#endif /* AFS_NAMEI_ENV */
- if (online) {
- printf("volinfo: -online not supported\n");
- exit(1);
- } else {
- afs_int32 n;
-
- (void)afs_snprintf(headerName, sizeof headerName, "%s" OS_DIRSEP "%s",
- VPartitionPath(dp), name);
- if ((fd = afs_open(headerName, O_RDONLY)) == -1
- || afs_fstat(fd, &status) == -1) {
- printf("Volinfo: Cannot read volume header %s\n", name);
- close(fd);
- exit(1);
- }
- n = read(fd, &diskHeader, sizeof(diskHeader));
-
- if (n != sizeof(diskHeader)
- || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
- printf("Volinfo: Error reading volume header %s\n", name);
- exit(1);
- }
- if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
- printf
- ("Volinfo: Volume %s, version number is incorrect; volume needs salvage\n",
- name);
- exit(1);
- }
- DiskToVolumeHeader(&header, &diskHeader);
-
- if (dheader) {
- FdHandle_t *fdP;
- afs_sfsize_t size = 0;
- afs_sfsize_t code;
-
- if (afs_fstat(fd, &stat) == -1) {
- perror("stat");
- exit(1);
- }
- if (!dsizeOnly && !saveinodes) {
- size = stat.st_size;
- printf("Volume header (size = %d):\n", (int)size);
- printf("\tstamp\t= 0x%x\n", header.stamp.version);
- printf("\tVolId\t= %u\n", header.id);
- }
+ IH_INIT(ih, dp->device, header->parent, inode);
+ fdP = IH_OPEN(ih);
+ if (fdP == NULL) {
+ fprintf(stderr,
+ "%s: Error opening header file '%s' for volume %" AFS_VOLID_FMT "\n", progname,
+ name, afs_printable_VolumeId_lu(header->id));
+ perror("open");
+ goto error;
+ }
+ size = FDH_SIZE(fdP);
+ if (size == -1) {
+ fprintf(stderr,
+ "%s: Error getting size of header file '%s' for volume %" AFS_VOLID_FMT "\n",
+ progname, name, afs_printable_VolumeId_lu(header->id));
+ perror("fstat");
+ goto error;
+ }
+ *psize += size;
- IH_INIT(ih, dp->device, header.parent, header.volumeInfo);
- fdP = IH_OPEN(ih);
- if (fdP == NULL) {
- perror("opening volume info");
- exit(1);
- }
- code = FDH_SIZE(fdP);
- if (code == -1) {
- perror("fstat");
- exit(1);
- }
- FDH_REALLYCLOSE(fdP);
- IH_RELEASE(ih);
- size += code;
- if (!dsizeOnly && !saveinodes) {
- printf("\tparent\t= %u\n", header.parent);
- printf("\tInfo inode\t= %s (size = %d)\n",
- PrintInode(NULL, header.volumeInfo), (int)code);
- }
+ error:
+ if (opt->dumpInfo) {
+ printf("\t%s inode\t= %s (size = ", name, PrintInode(NULL, inode));
+ if (size != -1) {
+ printf("%lld)\n", size);
+ } else {
+ printf("unknown)\n");
+ }
+#ifdef AFS_NAMEI_ENV
+ namei_HandleToName(&filename, ih);
+ printf("\t%s namei\t= %s\n", name, filename.n_path);
+#endif /* AFS_NAMEI_ENV */
+ }
- IH_INIT(ih, dp->device, header.parent, header.smallVnodeIndex);
- fdP = IH_OPEN(ih);
- if (fdP == NULL) {
- perror("opening small vnode index");
- exit(1);
- }
- code = FDH_SIZE(fdP);
- if (code == -1) {
- perror("fstat");
- exit(1);
- }
- FDH_REALLYCLOSE(fdP);
- IH_RELEASE(ih);
- size += code;
- if (!dsizeOnly && !saveinodes) {
- printf("\tSmall inode\t= %s (size = %d)\n",
- PrintInode(NULL, header.smallVnodeIndex), (int)code);
- }
+ if (fdP != NULL) {
+ FDH_REALLYCLOSE(fdP);
+ }
+ if (ih != NULL) {
+ IH_RELEASE(ih);
+ }
+}
- IH_INIT(ih, dp->device, header.parent, header.largeVnodeIndex);
- fdP = IH_OPEN(ih);
- if (fdP == NULL) {
- perror("opening large vnode index");
- exit(1);
- }
- code = FDH_SIZE(fdP);
- if (code == -1) {
- perror("fstat");
- exit(1);
- }
- FDH_REALLYCLOSE(fdP);
- IH_RELEASE(ih);
- size += code;
- if (!dsizeOnly && !saveinodes) {
- printf("\tLarge inode\t= %s (size = %d)\n",
- PrintInode(NULL, header.largeVnodeIndex), (int)code);
-#ifndef AFS_NT40_ENV
- printf("Total aux volume size = %d\n\n", (int)size);
-#endif
- }
+/**
+ * Inspect this volume header files.
+ *
+ * @param[in] dp partition object for this volume
+ * @param[in] header_fd volume header file descriptor
+ * @param[in] header volume header object
+ *
+ * @return none
+ */
+static void
+HandleHeaderFiles(struct VolInfoOpt *opt, struct DiskPartition64 *dp, FD_t header_fd,
+ struct VolumeHeader *header)
+{
+ afs_sfsize_t size = 0;
+
+ if (opt->dumpInfo) {
+ size = OS_SIZE(header_fd);
+ printf("Volume header (size = %lld):\n", size);
+ printf("\tstamp\t= 0x%x\n", header->stamp.version);
+ printf("\tVolId\t= %" AFS_VOLID_FMT "\n", afs_printable_VolumeId_lu(header->id));
+ printf("\tparent\t= %" AFS_VOLID_FMT "\n", afs_printable_VolumeId_lu(header->parent));
+ }
+
+ HandleSpecialFile(opt, "Info", dp, header, header->volumeInfo, &size);
+ HandleSpecialFile(opt, "Small", dp, header, header->smallVnodeIndex,
+ &size);
+ HandleSpecialFile(opt, "Large", dp, header, header->largeVnodeIndex,
+ &size);
#ifdef AFS_NAMEI_ENV
- IH_INIT(ih, dp->device, header.parent, header.linkTable);
- fdP = IH_OPEN(ih);
- if (fdP == NULL) {
- perror("opening link table index");
- exit(1);
- }
- code = FDH_SIZE(fdP);
- if (code == -1) {
- perror("fstat");
- exit(1);
- }
- FDH_REALLYCLOSE(fdP);
- IH_RELEASE(ih);
- size += code;
- if (!dsizeOnly && !saveinodes) {
- printf("\tLink inode\t= %s (size = %d)\n",
- PrintInode(NULL, header.linkTable), (int)code);
- printf("Total aux volume size = %d\n\n", (int)size);
+ HandleSpecialFile(opt, "Link", dp, header, header->linkTable, &size);
+#endif /* AFS_NAMEI_ENV */
+
+ if (opt->dumpInfo) {
+ printf("Total aux volume size = %lld\n\n", size);
+ }
+
+ if (opt->showSizes) {
+ volumeTotals.auxsize = size;
+ volumeTotals.auxsize_k = size / 1024;
+ }
+}
+
+/**
+ * Determine if the vnodes of this volume should be scanned.
+ *
+ * @param[in] vp volume object
+ *
+ * @return true if vnodes should be scanned
+ */
+static int
+IsScannable(struct VolInfoOpt *opt, Volume * vp)
+{
+ if (opr_queue_IsEmpty(&VnodeScanLists[vLarge]) &&
+ opr_queue_IsEmpty(&VnodeScanLists[vSmall])) {
+ return 0;
+ }
+ if (!opt->scanVolType) {
+ return 1; /* filtering disabled; do not check vol type */
+ }
+ switch (V_type(vp)) {
+ case RWVOL:
+ return opt->scanVolType & SCAN_RW;
+ case ROVOL:
+ return opt->scanVolType & SCAN_RO;
+ case BACKVOL:
+ return opt->scanVolType & SCAN_BK;
+ default:
+ fprintf(stderr, "%s: Volume %" AFS_VOLID_FMT "; Unknown volume type %d\n", progname,
+ afs_printable_VolumeId_lu(V_id(vp)), V_type(vp));
+ break;
+ }
+ return 0;
+}
+
+/**
+ * Attach and scan the volume and handle the header and vnodes
+ *
+ * Print the volume header and vnode information, depending on the
+ * current modes.
+ *
+ * @param[in] dp vice partition object for this volume
+ * @param[in] name volume header file name
+ *
+ * @return none
+ */
+static void
+HandleVolume(struct VolInfoOpt *opt, struct DiskPartition64 *dp, char *name)
+{
+ struct VolumeHeader header;
+ struct VolumeDiskHeader diskHeader;
+ FD_t fd = INVALID_FD;
+ Volume *vp = NULL;
+ char headerName[1024];
+ afs_sfsize_t n;
+
+ snprintf(headerName, sizeof headerName, "%s" OS_DIRSEP "%s",
+ VPartitionPath(dp), name);
+ if ((fd = OS_OPEN(headerName, O_RDONLY, 0666)) == INVALID_FD) {
+ fprintf(stderr, "%s: Cannot open volume header %s\n", progname, name);
+ goto cleanup;
+ }
+ if (OS_SIZE(fd) < 0) {
+ fprintf(stderr, "%s: Cannot read volume header %s\n", progname, name);
+ goto cleanup;
+ }
+ n = OS_READ(fd, &diskHeader, sizeof(diskHeader));
+ if (n != sizeof(diskHeader)
+ || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
+ fprintf(stderr, "%s: Error reading volume header %s\n", progname,
+ name);
+ goto cleanup;
+ }
+ if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
+ fprintf(stderr,
+ "%s: Volume %s, version number is incorrect; volume needs to be salvaged\n",
+ progname, name);
+ goto cleanup;
+ }
+
+ DiskToVolumeHeader(&header, &diskHeader);
+ if (opt->dumpHeader || opt->showSizes) {
+ HandleHeaderFiles(opt, dp, fd, &header);
+ }
+
+ vp = AttachVolume(opt, dp, name, &header);
+ if (!vp) {
+ fprintf(stderr, "%s: Error attaching volume header %s\n",
+ progname, name);
+ goto cleanup;
+ }
+
+ if (opt->dumpInfo) {
+ PrintHeader(vp);
+ }
+ if (IsScannable(opt, vp)) {
+ if (NeedDirIndex) {
+ IHandle_t *ih = vp->vnodeIndex[vLarge].handle;
+ DirIndexFd = IH_OPEN(ih);
+ if (DirIndexFd == NULL) {
+ fprintf(stderr, "%s: Failed to open index for directories.",
+ progname);
}
-#endif
- Vauxsize = size;
- Vauxsize_k = size / 1024;
}
- close(fd);
- vp = AttachVolume(dp, name, &header);
- if (!vp) {
- printf("Volinfo: Error attaching volume header %s\n", name);
- return;
+
+ HandleVnodes(opt, vp, vLarge);
+ HandleVnodes(opt, vp, vSmall);
+
+ if (DirIndexFd) {
+ FDH_CLOSE(DirIndexFd);
+ DirIndexFd = NULL;
}
}
- PrintHeader(vp);
- if (DumpVnodes) {
- if (!dsizeOnly && !saveinodes)
- printf("\nLarge vnodes (directories)\n");
- PrintVnodes(vp, vLarge);
- if (!dsizeOnly && !saveinodes) {
- printf("\nSmall vnodes(files, symbolic links)\n");
- fflush(stdout);
- }
- if (saveinodes)
- printf("Saving all volume files to current directory ...\n");
- PrintVnodes(vp, vSmall);
- }
- if (dsizeOnly) {
- totvolsize = Vauxsize_k + Vvnodesize_k;
- if (saveinodes)
- printf
- ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
- printf("%u\t%9d%9d%10d%10d%9d\t%24s\n", V_id(vp), Vdiskused,
- Vauxsize_k, Vvnodesize_k, totvolsize, totvolsize - Vdiskused,
- V_name(vp));
+ if (opt->showSizes) {
+ volumeTotals.diskused_k = V_diskused(vp);
+ volumeTotals.size_k =
+ volumeTotals.auxsize_k + volumeTotals.vnodesize_k;
+ if (opt->saveInodes) {
+ PrintingVolumeSizes = 0; /* print heading again */
+ }
+ PrintVolumeSizes(vp);
+ }
+
+ cleanup:
+ if (fd != INVALID_FD) {
+ OS_CLOSE(fd);
+ }
+ if (vp) {
+ DetachVolume(opt, vp);
}
- free(vp->header);
- free(vp);
}
-int
-main(int argc, char **argv)
+/**
+ * Return a display string for the volume type.
+ *
+ * @param[in] type volume type
+ *
+ * @return volume type description string
+ */
+static_inline char *
+volumeTypeString(int type)
{
- struct cmd_syndesc *ts;
- afs_int32 code;
-
- ts = cmd_CreateSyntax(NULL, handleit, NULL, "Dump volume's internal state");
- cmd_AddParm(ts, "-online", CMD_FLAG, CMD_OPTIONAL,
- "Get info from running fileserver");
- cmd_AddParm(ts, "-vnode", CMD_FLAG, CMD_OPTIONAL, "Dump vnode info");
- cmd_AddParm(ts, "-date", CMD_FLAG, CMD_OPTIONAL,
- "Also dump vnode's mod date");
- cmd_AddParm(ts, "-inode", CMD_FLAG, CMD_OPTIONAL,
- "Dump vnode's inode number");
- cmd_AddParm(ts, "-itime", CMD_FLAG, CMD_OPTIONAL,
- "Dump special inode's mod times");
- cmd_AddParm(ts, "-part", CMD_LIST, CMD_OPTIONAL,
- "AFS partition name (default current partition)");
- cmd_AddParm(ts, "-volumeid", CMD_LIST, CMD_OPTIONAL, "Volume id");
- cmd_AddParm(ts, "-header", CMD_FLAG, CMD_OPTIONAL,
- "Dump volume's header info");
- cmd_AddParm(ts, "-sizeOnly", CMD_FLAG, CMD_OPTIONAL,
- "Dump volume's size");
- cmd_AddParm(ts, "-fixheader", CMD_FLAG, CMD_OPTIONAL,
- "Try to fix header");
- cmd_AddParm(ts, "-saveinodes", CMD_FLAG, CMD_OPTIONAL,
- "Try to save all inodes");
- cmd_AddParm(ts, "-orphaned", CMD_FLAG, CMD_OPTIONAL,
- "List all dir/files without a parent");
-#if defined(AFS_NAMEI_ENV)
- cmd_AddParm(ts, "-filenames", CMD_FLAG, CMD_OPTIONAL, "Print filenames");
-#endif
- code = cmd_Dispatch(argc, argv);
- return code;
+ return
+ (type == RWVOL ? "read/write" :
+ (type == ROVOL ? "readonly" :
+ (type == BACKVOL ? "backup" : "unknown")));
}
-#define typestring(type) (type == RWVOL? "read/write": type == ROVOL? "readonly": type == BACKVOL? "backup": "unknown")
+/**
+ * Return a short display string for the volume type.
+ *
+ * @param[in] type volume type
+ *
+ * @return volume type short description string
+ */
+static_inline char *
+volumeTypeShortString(int type)
+{
+ return
+ (type == RWVOL ? "RW" :
+ (type == ROVOL ? "RO" : (type == BACKVOL ? "BK" : "??")));
+}
-void
+/**
+ * Print the volume header information
+ *
+ * @param[in] volume object
+ *
+ * @return none
+ */
+static void
PrintHeader(Volume * vp)
{
- Vdiskused = V_diskused(vp);
- if (dsizeOnly || saveinodes)
- return;
- printf("Volume header for volume %u (%s)\n", V_id(vp), V_name(vp));
+ printf("Volume header for volume %" AFS_VOLID_FMT " (%s)\n", afs_printable_VolumeId_lu(V_id(vp)), V_name(vp));
printf("stamp.magic = %x, stamp.version = %u\n", V_stamp(vp).magic,
V_stamp(vp).version);
printf
V_dontSalvage(vp));
printf
("type = %d (%s), uniquifier = %u, needsCallback = %d, destroyMe = %x\n",
- V_type(vp), typestring(V_type(vp)), V_uniquifier(vp),
+ V_type(vp), volumeTypeString(V_type(vp)), V_uniquifier(vp),
V_needsCallback(vp), V_destroyMe(vp));
printf
- ("id = %u, parentId = %u, cloneId = %u, backupId = %u, restoredFromId = %u\n",
- V_id(vp), V_parentId(vp), V_cloneId(vp), V_backupId(vp),
- V_restoredFromId(vp));
+ ("id = %" AFS_VOLID_FMT ", parentId = %" AFS_VOLID_FMT ", cloneId = %" AFS_VOLID_FMT ", backupId = %" AFS_VOLID_FMT ", restoredFromId = %" AFS_VOLID_FMT "\n",
+ afs_printable_VolumeId_lu(V_id(vp)), afs_printable_VolumeId_lu(V_parentId(vp)), afs_printable_VolumeId_lu(V_cloneId(vp)), afs_printable_VolumeId_lu(V_backupId(vp)), afs_printable_VolumeId_lu(V_restoredFromId(vp)));
printf
("maxquota = %d, minquota = %d, maxfiles = %d, filecount = %d, diskused = %d\n",
V_maxquota(vp), V_minquota(vp), V_maxfiles(vp), V_filecount(vp),
V_dayUse(vp), V_weekUse(vp)[0], V_weekUse(vp)[1], V_weekUse(vp)[2],
V_weekUse(vp)[3], V_weekUse(vp)[4], V_weekUse(vp)[5],
V_weekUse(vp)[6], date(V_dayUseDate(vp)));
- printf("volUpdateCounter = %u\n", V_volUpCounter(vp));
+ printf("volUpdateCounter = %u\n", V_volUpdateCounter(vp));
}
-/* GetFileInfo
- * OS independent file info. Die on failure.
+/**
+ * Get the size and times of a file
+ *
+ * @param[in] fd file descriptor of file to stat
+ * @param[out] size size of the file
+ * @param[out] ctime ctime of file as a formatted string
+ * @param[out] mtime mtime of file as a formatted string
+ * @param[out] atime atime of file as a formatted string
+ *
+ * @return error code
+ * @retval 0 success
+ * @retval -1 failed to retrieve file information
*/
-#ifdef AFS_NT40_ENV
-char *
-NT_date(FILETIME * ft)
-{
- static char result[8][64];
- static int next = 0;
- SYSTEMTIME st;
- FILETIME lft;
-
- if (!FileTimeToLocalFileTime(ft, &lft)
- || !FileTimeToSystemTime(&lft, &st)) {
- printf("Time conversion failed.\n");
- exit(1);
- }
- sprintf(result[next = ((next + 1) & 7)], "%4d/%02d/%02d.%2d:%2d:%2d",
- st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
- return result[next];
-}
-#endif
-
-static void
-GetFileInfo(FD_t fd, int *size, char **ctime, char **mtime, char **atime)
+static int
+GetFileInfo(FD_t fd, afs_sfsize_t * size, char **ctime, char **mtime,
+ char **atime)
{
#ifdef AFS_NT40_ENV
BY_HANDLE_FILE_INFORMATION fi;
+ LARGE_INTEGER fsize;
if (!GetFileInformationByHandle(fd, &fi)) {
- printf("GetFileInformationByHandle failed, exiting\n");
- exit(1);
+ fprintf(stderr, "%s: GetFileInformationByHandle failed\n", progname);
+ return -1;
}
- *size = (int)fi.nFileSizeLow;
+ if (!GetFileSizeEx(fd, &fsize)) {
+ fprintf(stderr, "%s: GetFileSizeEx failed\n", progname);
+ return -1;
+ }
+ *size = fsize.QuadPart;
*ctime = "N/A";
*mtime = NT_date(&fi.ftLastWriteTime);
*atime = NT_date(&fi.ftLastAccessTime);
#else
- struct afs_stat status;
+ struct afs_stat_st status;
if (afs_fstat(fd, &status) == -1) {
- printf("fstat failed %d\n", errno);
- exit(1);
+ fprintf(stderr, "%s: fstat failed %d\n", progname, errno);
+ return -1;
}
- *size = (int)status.st_size;
+ *size = status.st_size;
*ctime = date(status.st_ctime);
*mtime = date(status.st_mtime);
*atime = date(status.st_atime);
#endif
+ return 0;
+}
+
+/**
+ * Copy the inode data to a file in the current directory.
+ *
+ * @param[in] vdp vnode details object
+ *
+ * @return none
+ */
+void
+volinfo_SaveInode(struct VolInfoOpt *opt, struct VnodeDetails *vdp)
+{
+ IHandle_t *ih;
+ FdHandle_t *fdP;
+ char nfile[50], buffer[256];
+ int ofd = 0;
+ afs_foff_t total;
+ ssize_t len;
+ Inode ino = VNDISK_GET_INO(vdp->vnode);
+
+ if (!VALID_INO(ino)) {
+ return;
+ }
+
+ IH_INIT(ih, V_device(vdp->vp), V_parentId(vdp->vp), ino);
+ fdP = IH_OPEN(ih);
+ if (fdP == NULL) {
+ fprintf(stderr,
+ "%s: Can't open inode %s error %d (ignored)\n",
+ progname, PrintInode(NULL, ino), errno);
+ return;
+ }
+ snprintf(nfile, sizeof nfile, "TmpInode.%s", PrintInode(NULL, ino));
+ ofd = afs_open(nfile, O_CREAT | O_RDWR | O_TRUNC, 0600);
+ if (ofd < 0) {
+ fprintf(stderr,
+ "%s: Can't create file %s; error %d (ignored)\n",
+ progname, nfile, errno);
+
+ FDH_REALLYCLOSE(fdP);
+ IH_RELEASE(ih);
+ return;
+ }
+ total = 0;
+ while (1) {
+ ssize_t nBytes;
+ len = FDH_PREAD(fdP, buffer, sizeof(buffer), total);
+ if (len < 0) {
+ FDH_REALLYCLOSE(fdP);
+ IH_RELEASE(ih);
+ close(ofd);
+ unlink(nfile);
+ fprintf(stderr,
+ "%s: Error while reading from inode %s (%d)\n",
+ progname, PrintInode(NULL, ino), errno);
+ return;
+ }
+ if (len == 0)
+ break; /* No more input */
+ nBytes = write(ofd, buffer, (size_t)len);
+ if (nBytes != len) {
+ FDH_REALLYCLOSE(fdP);
+ IH_RELEASE(ih);
+ close(ofd);
+ unlink(nfile);
+ fprintf(stderr,
+ "%s: Error while writing to \"%s\" (%d - ignored)\n",
+ progname, nfile, errno);
+ return;
+ }
+ total += len;
+ }
+
+ FDH_REALLYCLOSE(fdP);
+ IH_RELEASE(ih);
+ close(ofd);
+ printf("... Copied inode %s to file %s (%lu bytes)\n",
+ PrintInode(NULL, ino), nfile, (unsigned long)total);
+}
+
+/**
+ * get the VnodeDiskObject for a directory given its vnode id.
+ *
+ * @param[in] vp volume object
+ * @param[in] parent vnode id to read
+ * @param[out] pvn vnode disk object to populate
+ *
+ * @post pvn contains copy of disk object for parent id
+ *
+ * @return operation status
+ * @retval 0 success
+ * @retval -1 failure
+ */
+static int
+GetDirVnode(struct VolInfoOpt *opt, Volume * vp, VnodeId parent, VnodeDiskObject * pvn)
+{
+ afs_int32 code;
+ afs_foff_t offset;
+
+ if (!DirIndexFd) {
+ return -1; /* previously failed to open the large vnode index. */
+ }
+ if (parent % 2 == 0) {
+ fprintf(stderr, "%s: Invalid parent vnode id %lu in volume %lu\n",
+ progname,
+ afs_printable_uint32_lu(parent),
+ afs_printable_uint32_lu(V_id(vp)));
+ }
+ offset = vnodeIndexOffset(&VnodeClassInfo[vLarge], parent);
+ code = FDH_PREAD(DirIndexFd, pvn, SIZEOF_LARGEDISKVNODE, offset);
+ if (code != SIZEOF_LARGEDISKVNODE) {
+ fprintf(stderr,
+ "%s: GetDirVnode: read failed for %lu.%lu at offset %llu\n",
+ progname, afs_printable_uint32_lu(V_id(vp)),
+ afs_printable_uint32_lu(parent), (long long unsigned)offset);
+ return -1;
+ }
+ if (opt->checkMagic && (pvn->vnodeMagic != LARGEVNODEMAGIC)) {
+ fprintf(stderr, "%s: GetDirVnode: bad vnode magic for %lu.%lu at offset %llu\n",
+ progname, afs_printable_uint32_lu(V_id(vp)),
+ afs_printable_uint32_lu(parent), (long long unsigned)offset);
+ return -1;
+ }
+ if (!pvn->dataVersion) {
+ fprintf(stderr, "%s: GetDirVnode: dv is zero for %lu.%lu at offset %llu\n",
+ progname, afs_printable_uint32_lu(V_id(vp)),
+ afs_printable_uint32_lu(parent), (long long unsigned)offset);
+ return -1;
+ }
+ return 0;
+}
+
+/**
+ * Perform inverse lookup on a vice directory object to map a fid onto a dirent string.
+ *
+ * @param[in] vp volume object
+ * @param[in] pvnode parent directory vnode object
+ * @param[in] cvnid child vnode id to inverse lookup
+ * @param[in] cuniq child uniquifier to inverse lookup
+ * @param[out] dirent buffer in which to store dirent string
+ * @param[out] dirent_len length of dirent buffer
+ *
+ * @post dirent contains string for the (cvnid, cuniq) entry
+ *
+ * @return operation status
+ * @retval 0 success
+ */
+static int
+GetDirEntry(Volume * vp, VnodeDiskObject * pvnode, VnodeId cvnid,
+ afs_uint32 cuniq, char *dirent, size_t dirent_len)
+{
+ DirHandle dir;
+ Inode ino;
+ int volumeChanged;
+ afs_int32 code;
+
+ ino = VNDISK_GET_INO(pvnode);
+ if (!VALID_INO(ino)) {
+ fprintf(stderr, "%s: GetDirEntry invalid parent ino\n", progname);
+ return -1;
+ }
+ SetSalvageDirHandle(&dir, V_parentId(vp), V_device(vp), ino,
+ &volumeChanged);
+ code = afs_dir_InverseLookup(&dir, cvnid, cuniq, dirent, dirent_len);
+ if (code) {
+ fprintf(stderr, "%s: afs_dir_InverseLookup failed with code %d\n",
+ progname, code);
+ }
+ FidZap(&dir);
+ return code;
+}
+
+/**
+ * Lookup the path of this vnode, relative to the root of the volume.
+ *
+ * @param[in] vdp vnode details
+ *
+ * @return status
+ * @retval 0 success
+ * @retval -1 error
+ */
+static int
+LookupPath(struct VolInfoOpt *opt, struct VnodeDetails *vdp)
+{
+#define MAX_PATH_LEN 1023
+ static char path_buffer[MAX_PATH_LEN + 1];
+ static char dirent[MAX_PATH_LEN + 1];
+ char vnode_buffer[SIZEOF_LARGEDISKVNODE];
+ struct VnodeDiskObject *pvn = (struct VnodeDiskObject *)vnode_buffer;
+ int code = 0;
+ int space;
+ char *cursor;
+ Volume *vp = vdp->vp;
+ VnodeId parent = vdp->vnode->parent;
+ VnodeId cvnid = vdp->vnodeNumber;
+ afs_uint32 cuniq = vdp->vnode->uniquifier;
+
+ if (!parent) {
+ vdp->path = "/"; /* this is root */
+ return 0;
+ }
+
+ space = sizeof(path_buffer) - 1;
+ cursor = &path_buffer[space];
+ *cursor = '\0';
+
+ while (parent) {
+ int len;
+
+ code = GetDirVnode(opt, vp, parent, pvn);
+ if (code) {
+ cursor = NULL;
+ break;
+ }
+ code = GetDirEntry(vp, pvn, cvnid, cuniq, dirent, MAX_PATH_LEN);
+ if (code) {
+ cursor = NULL;
+ break;
+ }
+
+ len = strlen(dirent);
+ if (len == 0) {
+ fprintf(stderr,
+ "%s: Failed to lookup path for fid %lu.%lu.%lu: empty dir entry\n",
+ progname, afs_printable_uint32_lu(V_id(vdp->vp)),
+ afs_printable_uint32_lu(vdp->vnodeNumber),
+ afs_printable_uint32_lu(vdp->vnode->uniquifier));
+ cursor = NULL;
+ code = -1;
+ break;
+ }
+
+ if (space < (len + 1)) {
+ fprintf(stderr,
+ "%s: Failed to lookup path for fid %lu.%lu.%lu: path exceeds max length (%u).\n",
+ progname, afs_printable_uint32_lu(V_id(vdp->vp)),
+ afs_printable_uint32_lu(vdp->vnodeNumber),
+ afs_printable_uint32_lu(vdp->vnode->uniquifier),
+ MAX_PATH_LEN);
+ cursor = NULL;
+ code = -1;
+ break;
+ }
+
+ /* prepend path component */
+ cursor -= len;
+ memcpy(cursor, dirent, len);
+ *--cursor = '/';
+ space -= (len + 1);
+
+ /* next parent */
+ cvnid = parent;
+ cuniq = pvn->uniquifier;
+ parent = pvn->parent;
+ }
+
+ if (cursor) {
+ vdp->path = cursor;
+ }
+ return code;
+}
+
+/**
+ * Read the symlink target and determine if this vnode is a mount point.
+ *
+ * @param[inout] vdp vnode details object
+ *
+ * @return error code
+ * @retval 0 success
+ * @retval -1 failure
+ */
+static int
+ReadSymlinkTarget(struct VnodeDetails *vdp)
+{
+#define MAX_SYMLINK_LEN 1023
+ static char buffer[MAX_SYMLINK_LEN + 1];
+ int code;
+ Volume *vp = vdp->vp;
+ VnodeDiskObject *vnode = vdp->vnode;
+ VnodeId vnodeNumber = vdp->vnodeNumber;
+ IHandle_t *ihP = NULL;
+ FdHandle_t *fdP = NULL;
+ afs_fsize_t fileLength;
+ int readLength;
+ Inode ino;
+
+ ino = VNDISK_GET_INO(vnode);
+ VNDISK_GET_LEN(fileLength, vnode);
+
+ if (fileLength > MAX_SYMLINK_LEN) {
+ fprintf(stderr,
+ "%s: Symlink contents for fid (%lu.%lu.%lu.%lu) exceeds "
+ "%u, file length is %llu)!\n", progname,
+ afs_printable_uint32_lu(V_id(vp)),
+ afs_printable_uint32_lu(vnodeNumber),
+ afs_printable_uint32_lu(vnode->uniquifier),
+ afs_printable_uint32_lu(vnode->dataVersion),
+ MAX_SYMLINK_LEN,
+ fileLength);
+ return -1;
+ }
+ if (fileLength == 0) {
+ fprintf(stderr,
+ "%s: Symlink contents for fid (%lu.%lu.%lu.%lu) is empty.\n",
+ progname,
+ afs_printable_uint32_lu(V_id(vp)),
+ afs_printable_uint32_lu(vnodeNumber),
+ afs_printable_uint32_lu(vnode->uniquifier),
+ afs_printable_uint32_lu(vnode->dataVersion));
+ return -1;
+ }
+
+ IH_INIT(ihP, V_device(vp), V_parentId(vp), ino);
+ fdP = IH_OPEN(ihP);
+ if (fdP == NULL) {
+ code = -1;
+ goto cleanup;
+ }
+ readLength = FDH_PREAD(fdP, buffer, fileLength, 0);
+ if (readLength < 0) {
+ fprintf(stderr,
+ "%s: Error reading symlink contents for fid (%lu.%lu.%lu.%lu); "
+ "errno %d\n",
+ progname,
+ afs_printable_uint32_lu(V_id(vp)),
+ afs_printable_uint32_lu(vnodeNumber),
+ afs_printable_uint32_lu(vnode->uniquifier),
+ afs_printable_uint32_lu(vnode->dataVersion), errno);
+ code = -1;
+ goto cleanup;
+ } else if (readLength != fileLength) {
+ fprintf(stderr,
+ "%s: Symlink contents for fid (%lu.%lu.%lu.%lu) don't match "
+ "vnode file length metadata (len=%llu, actual=%lld)!\n",
+ progname,
+ afs_printable_uint32_lu(V_id(vp)),
+ afs_printable_uint32_lu(vnodeNumber),
+ afs_printable_uint32_lu(vnode->uniquifier),
+ afs_printable_uint32_lu(vnode->dataVersion), fileLength,
+ (long long)readLength);
+ code = -1;
+ goto cleanup;
+ }
+ code = 0;
+
+ if (readLength > 1 && (buffer[0] == '#' || buffer[0] == '%')
+ && buffer[readLength - 1] == '.') {
+ char *sep;
+ buffer[readLength - 1] = '\0'; /* stringify; clobbers trailing dot */
+ sep = strchr(buffer, ':');
+ vdp->t = VNODE_U_MOUNT;
+ vdp->u.mnt.type = buffer[0];
+ if (!sep) {
+ vdp->u.mnt.cell = NULL;
+ vdp->u.mnt.vol = buffer + 1;
+ } else {
+ *sep = '\0';
+ vdp->u.mnt.cell = buffer + 1;
+ vdp->u.mnt.vol = sep + 1;
+ }
+ } else {
+ buffer[readLength] = '\0';
+ vdp->t = VNODE_U_SYMLINK;
+ vdp->u.target = buffer;
+ }
+
+ cleanup:
+ if (fdP) {
+ FDH_CLOSE(fdP);
+ }
+ if (ihP) {
+ IH_RELEASE(ihP);
+ }
+ return code;
}
+/**
+ * Print vnode details line
+ *
+ * @param[inout] vdp vnode details object
+ *
+ * @return none
+ */
void
-PrintVnodes(Volume * vp, VnodeClass class)
+volinfo_PrintVnodeDetails(struct VolInfoOpt *opt, struct VnodeDetails *vdp)
+{
+ switch (vdp->vnode->type) {
+ case vNull:
+ break;
+ case vFile:
+ if (opt->findVnType & FIND_FILE) {
+ PrintColumns(opt, vdp, "file");
+ }
+ break;
+ case vDirectory:
+ if (opt->findVnType & FIND_DIR) {
+ PrintColumns(opt, vdp, "dir");
+ }
+ break;
+ case vSymlink:
+ if (opt->findVnType & (FIND_MOUNT | FIND_SYMLINK)) {
+ ReadSymlinkTarget(vdp);
+ if ((opt->findVnType & FIND_MOUNT) && (vdp->t == VNODE_U_MOUNT)) {
+ PrintColumns(opt, vdp, "mount");
+ }
+ if ((opt->findVnType & FIND_SYMLINK) && (vdp->t == VNODE_U_SYMLINK)) {
+ PrintColumns(opt, vdp, "symlink");
+ }
+ }
+ break;
+ default:
+ fprintf(stderr,
+ "%s: Warning: unexpected vnode type %u on fid %lu.%lu.%lu",
+ progname, vdp->vnode->type,
+ afs_printable_uint32_lu(V_id(vdp->vp)),
+ afs_printable_uint32_lu(vdp->vnodeNumber),
+ afs_printable_uint32_lu(vdp->vnode->uniquifier));
+ }
+}
+
+/**
+ * Print each access entry of a vnode
+ *
+ * @param[in] vdp vnode details object
+ *
+ * @return none
+ */
+void
+volinfo_ScanAcl(struct VolInfoOpt *opt, struct VnodeDetails *vdp)
+{
+ int i;
+ struct acl_accessList *acl;
+ VnodeDiskObject *vnode = vdp->vnode;
+
+ if (vnode->type == vNull) {
+ return;
+ }
+
+ acl = VVnodeDiskACL(vnode);
+ for (i = 0; i < acl->positive; i++) {
+ vdp->t = VNODE_U_POS_ACCESS;
+ vdp->u.access = &(acl->entries[i]);
+ PrintColumns(opt, vdp, "acl");
+ }
+ for (i = (acl->total - 1); i >= (acl->total - acl->negative); i--) {
+ vdp->t = VNODE_U_NEG_ACCESS;
+ vdp->u.access = &(acl->entries[i]);
+ PrintColumns(opt, vdp, "acl");
+ }
+}
+
+/**
+ * Determine if the mode matches all the given masks.
+ *
+ * Returns true if the mode bits match all the given masks. A mask matches if at
+ * least one bit in the mask is present in the mode bits. An empty mode mask
+ * list matches all modes (even if all the mode bits are zero.)
+ *
+ * param[in] modeBits unix mode bits of a vnode
+ *
+ */
+static int
+ModeMaskMatch(struct VolInfoOpt *opt, unsigned int modeBits)
+{
+ int i;
+
+ for (i = 0; i < sizeof(opt->modeMask) / sizeof(*opt->modeMask) && opt->modeMask[i]; i++) {
+ if ((opt->modeMask[i] & modeBits) == 0) {
+ return 0; /* at least one mode bit is not present */
+ }
+ }
+ return 1;
+}
+
+/**
+ * Scan a volume index and handle each vnode
+ *
+ * @param[in] vp volume object
+ * @param[in] class which index to scan
+ *
+ * @return none
+ */
+static void
+HandleVnodes(struct VolInfoOpt *opt, Volume * vp, VnodeClass class)
{
afs_int32 diskSize =
(class == vSmall ? SIZEOF_SMALLDISKVNODE : SIZEOF_LARGEDISKVNODE);
char buf[SIZEOF_LARGEDISKVNODE];
struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
- StreamHandle_t *file;
- int vnodeIndex, nVnodes;
+ StreamHandle_t *file = NULL;
+ int vnodeIndex;
+ afs_sfsize_t nVnodes;
afs_foff_t offset = 0;
- Inode ino;
IHandle_t *ih = vp->vnodeIndex[class].handle;
- FdHandle_t *fdP;
- int size;
+ FdHandle_t *fdP = NULL;
+ afs_sfsize_t size;
char *ctime, *atime, *mtime;
- char nfile[50], buffer[256];
- int ofd, bad = 0;
- afs_foff_t total;
- ssize_t len;
+ struct opr_queue *scanList = &VnodeScanLists[class];
+ struct opr_queue *cursor;
+
+ if (opr_queue_IsEmpty(scanList)) {
+ return;
+ }
+
+ for (opr_queue_Scan(scanList, cursor)) {
+ struct VnodeScanProc *entry = (struct VnodeScanProc *)cursor;
+ if (entry->heading) {
+ printf("%s", entry->heading);
+ }
+ }
fdP = IH_OPEN(ih);
if (fdP == NULL) {
- printf("open failed\n");
- exit(1);
+ fprintf(stderr, "%s: open failed: ", progname);
+ goto error;
}
file = FDH_FDOPEN(fdP, "r");
if (!file) {
- printf("fdopen failed\n");
- exit(1);
+ fprintf(stderr, "%s: fdopen failed\n", progname);
+ goto error;
}
- GetFileInfo(fdP->fd_fd, &size, &ctime, &atime, &mtime);
- if (InodeTimes && !dsizeOnly) {
+ if (GetFileInfo(fdP->fd_fd, &size, &ctime, &atime, &mtime) != 0) {
+ goto error;
+ }
+ if (opt->dumpInodeTimes) {
printf("ichanged : %s\nimodified: %s\niaccessed: %s\n\n", ctime,
mtime, atime);
}
nVnodes && STREAM_READ(vnode, diskSize, 1, file) == 1;
nVnodes--, vnodeIndex++, offset += diskSize) {
- ino = VNDISK_GET_INO(vnode);
- if (saveinodes) {
- if (VALID_INO(ino) && (class == vSmall)) {
- IHandle_t *ih1;
- FdHandle_t *fdP1;
- IH_INIT(ih1, V_device(vp), V_parentId(vp), ino);
- fdP1 = IH_OPEN(ih1);
- if (fdP1 == NULL) {
- printf("Can't open inode %s error %d (ignored)\n",
- PrintInode(NULL, ino), errno);
- continue;
- }
- (void)afs_snprintf(nfile, sizeof nfile, "TmpInode.%s",
- PrintInode(NULL, ino));
- ofd = afs_open(nfile, O_CREAT | O_RDWR | O_TRUNC, 0600);
- if (ofd < 0) {
- printf("Can't create file %s; error %d (ignored)\n",
- nfile, errno);
- continue;
- }
- total = bad = 0;
- while (1) {
- ssize_t nBytes;
- len = FDH_PREAD(fdP1, buffer, sizeof(buffer), total);
- if (len < 0) {
- FDH_REALLYCLOSE(fdP1);
- IH_RELEASE(ih1);
- close(ofd);
- unlink(nfile);
- printf
- ("Error while reading from inode %s (%d - ignored)\n",
- PrintInode(NULL, ino), errno);
- bad = 1;
- break;
- }
- if (len == 0)
- break; /* No more input */
- nBytes = write(ofd, buffer, len);
- if (nBytes != len) {
- FDH_REALLYCLOSE(fdP1);
- IH_RELEASE(ih1);
- close(ofd);
- unlink(nfile);
- printf
- ("Error while writing to \"%s\" (%d - ignored)\n",
- nfile, errno);
- bad = 1;
- break;
- }
- total += len;
- }
- if (bad)
- continue;
- FDH_REALLYCLOSE(fdP1);
- IH_RELEASE(ih1);
- close(ofd);
- printf("... Copied inode %s to file %s (%lu bytes)\n",
- PrintInode(NULL, ino), nfile, (unsigned long)total);
+ struct VnodeDetails vnodeDetails;
+
+ if (!ModeMaskMatch(opt, vnode->modeBits)) {
+ continue;
+ }
+
+ memset(&vnodeDetails, 0, sizeof(struct VnodeDetails));
+ vnodeDetails.vp = vp;
+ vnodeDetails.class = class;
+ vnodeDetails.vnode = vnode;
+ vnodeDetails.vnodeNumber = bitNumberToVnodeNumber(vnodeIndex, class);
+ vnodeDetails.offset = offset;
+ vnodeDetails.index = vnodeIndex;
+
+ for (opr_queue_Scan(scanList, cursor)) {
+ struct VnodeScanProc *entry = (struct VnodeScanProc *)cursor;
+ if (entry->proc) {
+ (*entry->proc) (opt, &vnodeDetails);
}
- } else {
-#if defined(AFS_NAMEI_ENV)
- PrintVnode(offset, vnode,
- bitNumberToVnodeNumber(vnodeIndex, class), ino, vp);
-#else
- PrintVnode(offset, vnode,
- bitNumberToVnodeNumber(vnodeIndex, class), ino);
-#endif
}
}
- STREAM_CLOSE(file);
- FDH_CLOSE(fdP);
+
+ error:
+ if (file) {
+ STREAM_CLOSE(file);
+ }
+ if (fdP) {
+ FDH_CLOSE(fdP);
+ }
}
-#if defined(AFS_NAMEI_ENV)
-void
-PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
- Inode ino, Volume * vp)
-#else
+/**
+ * Print vnode information
+ *
+ * @param[in] vdp vnode details object
+ *
+ * @return none
+ */
void
-PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
- Inode ino)
-#endif
+volinfo_PrintVnode(struct VolInfoOpt *opt, struct VnodeDetails *vdp)
{
#if defined(AFS_NAMEI_ENV)
IHandle_t *ihtmpp;
namei_t filename;
#endif
+ afs_foff_t offset = vdp->offset;
+ VnodeDiskObject *vnode = vdp->vnode;
afs_fsize_t fileLength;
+ Inode ino;
+ ino = VNDISK_GET_INO(vnode);
VNDISK_GET_LEN(fileLength, vnode);
- Vvnodesize += fileLength;
- Vvnodesize_k += fileLength / 1024;
- if (dsizeOnly)
- return;
- if (orphaned && (fileLength == 0 || vnode->parent || !offset))
+
+ /* The check for orphaned vnodes is currently limited to non-empty
+ * vnodes with a parent of zero (and which are not the first entry
+ * in the index). */
+ if (opt->showOrphaned && (fileLength == 0 || vnode->parent || !offset))
return;
+
printf
("%10lld Vnode %u.%u.%u cloned: %u, length: %llu linkCount: %d parent: %u",
- (long long)offset, vnodeNumber, vnode->uniquifier, vnode->dataVersion,
- vnode->cloned, (afs_uintmax_t) fileLength, vnode->linkCount,
- vnode->parent);
- if (DumpInodeNumber)
+ (long long)offset, vdp->vnodeNumber, vnode->uniquifier,
+ vnode->dataVersion, vnode->cloned, (afs_uintmax_t) fileLength,
+ vnode->linkCount, vnode->parent);
+ if (opt->dumpInodeNumber)
printf(" inode: %s", PrintInode(NULL, ino));
- if (DumpDate)
+ if (opt->dumpDate)
printf(" ServerModTime: %s", date(vnode->serverModifyTime));
#if defined(AFS_NAMEI_ENV)
- if (PrintFileNames) {
- IH_INIT(ihtmpp, V_device(vp), V_parentId(vp), ino);
+ if (opt->dumpFileNames) {
+ IH_INIT(ihtmpp, V_device(vdp->vp), V_parentId(vdp->vp), ino);
namei_HandleToName(&filename, ihtmpp);
#if !defined(AFS_NT40_ENV)
printf(" UFS-Filename: %s", filename.n_path);
#endif
printf("\n");
}
+
+/**
+ * Print the volume partition id
+ *
+ * @param[in] vp volume object
+ *
+ * @return none
+ */
+static void
+PrintPartitionId(Volume * vp)
+{
+ char *partition = VPartitionPath(V_partition(vp));
+
+ if (!strncmp(partition, "/vicep", 6)) {
+ printf("%s", partition + 6);
+ } else if (!strncmp(partition, "vicep", 5)) {
+ printf("%s", partition + 5);
+ } else {
+ fprintf(stderr, "Invalid partition for volume id %lu\n",
+ afs_printable_uint32_lu(V_id(vp)));
+ printf("%s", PLACEHOLDER);
+ }
+}
+
+/**
+ * Print the vnode type description string
+ *
+ * @param[in] type vnode type
+ *
+ * @return none
+ */
+static void
+volinfo_PrintVnodeType(int type)
+{
+ switch (type) {
+ case vNull:
+ printf("null");
+ break;
+ case vFile:
+ printf("file");
+ break;
+ case vDirectory:
+ printf("dir");
+ break;
+ case vSymlink:
+ printf("symlink");
+ break;
+ default:
+ printf("unknown");
+ }
+}
+
+/**
+ * Print right bits as string.
+ *
+ * param[in] rights rights bitmap
+ */
+static void
+PrintRights(int rights)
+{
+ if (rights & PRSFS_READ) {
+ printf("r");
+ }
+ if (rights & PRSFS_LOOKUP) {
+ printf("l");
+ }
+ if (rights & PRSFS_INSERT) {
+ printf("i");
+ }
+ if (rights & PRSFS_DELETE) {
+ printf("d");
+ }
+ if (rights & PRSFS_WRITE) {
+ printf("w");
+ }
+ if (rights & PRSFS_LOCK) {
+ printf("k");
+ }
+ if (rights & PRSFS_ADMINISTER) {
+ printf("a");
+ }
+ if (rights & PRSFS_USR0) {
+ printf("A");
+ }
+ if (rights & PRSFS_USR1) {
+ printf("B");
+ }
+ if (rights & PRSFS_USR2) {
+ printf("C");
+ }
+ if (rights & PRSFS_USR3) {
+ printf("D");
+ }
+ if (rights & PRSFS_USR4) {
+ printf("E");
+ }
+ if (rights & PRSFS_USR5) {
+ printf("F");
+ }
+ if (rights & PRSFS_USR6) {
+ printf("G");
+ }
+ if (rights & PRSFS_USR7) {
+ printf("H");
+ }
+}
+
+/**
+ * Print the path to the namei file.
+ */
+static void
+PrintNamei(Volume * vp, VnodeDiskObject * vnode)
+{
+#ifdef AFS_NAMEI_ENV
+ namei_t name;
+ IHandle_t *ihP = NULL;
+ Inode ino;
+ ino = VNDISK_GET_INO(vnode);
+ IH_INIT(ihP, V_device(vp), V_parentId(vp), ino);
+ namei_HandleToName(&name, ihP);
+ printf("%s", name.n_path);
+ IH_RELEASE(ihP);
+#else
+ printf("%s", PLACEHOLDER);
+#endif
+}
+
+/**
+ * Print the column heading line.
+ */
+static void
+PrintColumnHeading(struct VolInfoOpt *opt)
+{
+ int i;
+ const char *name;
+
+ for (i = 0; i < NumOutputColumns; i++) {
+ if (i > 0) {
+ printf("%s", opt->columnDelim);
+ }
+ name = ColumnName[OutputColumn[i]].name;
+ while (*name) {
+ putchar(toupper(*name++));
+ }
+ }
+ printf("\n");
+}
+
+/**
+ * Print output columns for the vnode/acess entry.
+ *
+ * @param[in] vdp vnode details object
+ * @param[in] desc type of line to be printed
+ *
+ * @return none
+ */
+static void
+PrintColumns(struct VolInfoOpt *opt, struct VnodeDetails *vdp, const char *desc)
+{
+ int i;
+ afs_fsize_t length;
+
+ for (i = 0; i < NumOutputColumns; i++) {
+ if (i > 0) {
+ printf("%s", opt->columnDelim);
+ }
+ switch (OutputColumn[i]) {
+ case col_host:
+ printf("%s", opt->hostname);
+ break;
+ case col_desc:
+ printf("%s", desc);
+ break;
+ case col_vid:
+ printf("%lu", afs_printable_uint32_lu(V_id(vdp->vp)));
+ break;
+ case col_offset:
+ printf("%llu", vdp->offset);
+ break;
+ case col_vtype:
+ printf("%s", volumeTypeShortString(V_type(vdp->vp)));
+ break;
+ case col_vname:
+ printf("%s", V_name(vdp->vp));
+ break;
+ case col_part:
+ printf("%s", VPartitionPath(V_partition(vdp->vp)));
+ break;
+ case col_partid:
+ PrintPartitionId(vdp->vp);
+ break;
+ case col_fid:
+ printf("%lu.%lu.%lu",
+ afs_printable_uint32_lu(V_id(vdp->vp)),
+ afs_printable_uint32_lu(vdp->vnodeNumber),
+ afs_printable_uint32_lu(vdp->vnode->uniquifier));
+ break;
+ case col_path:
+ if (!vdp->path) {
+ LookupPath(opt, vdp);
+ }
+ printf("%s", vdp->path ? vdp->path : PLACEHOLDER);
+ break;
+ case col_target:
+ printf("%s",
+ (vdp->t == VNODE_U_SYMLINK ? vdp->u.target : PLACEHOLDER));
+ break;
+ case col_mount:
+ if (vdp->t != VNODE_U_MOUNT) {
+ printf("%s", PLACEHOLDER);
+ } else {
+ printf("%c", vdp->u.mnt.type);
+ if (vdp->u.mnt.cell) {
+ printf("%s:", vdp->u.mnt.cell);
+ }
+ printf("%s.", vdp->u.mnt.vol);
+ }
+ break;
+ case col_mtype:
+ printf("%c", (vdp->t == VNODE_U_MOUNT ? vdp->u.mnt.type : '-'));
+ break;
+ case col_mcell:
+ printf("%s",
+ (vdp->t == VNODE_U_MOUNT && vdp->u.mnt.cell ? vdp->u.mnt.cell : PLACEHOLDER));
+ break;
+ case col_mvol:
+ printf("%s",
+ (vdp->t == VNODE_U_MOUNT ? vdp->u.mnt.vol : PLACEHOLDER));
+ break;
+ case col_aid:
+ if (vdp->t == VNODE_U_POS_ACCESS || vdp->t == VNODE_U_NEG_ACCESS) {
+ printf("%d", vdp->u.access->id);
+ } else {
+ printf("%s", PLACEHOLDER);
+ }
+ break;
+ case col_arights:
+ if (vdp->t == VNODE_U_POS_ACCESS) {
+ printf("+");
+ PrintRights(vdp->u.access->rights);
+ } else if (vdp->t == VNODE_U_NEG_ACCESS) {
+ printf("-");
+ PrintRights(vdp->u.access->rights);
+ }
+ break;
+ case col_vntype:
+ volinfo_PrintVnodeType(vdp->vnode->type);
+ break;
+ case col_cloned:
+ printf("%c", vdp->vnode->cloned ? 'y' : 'n');
+ break;
+ case col_mode:
+ printf("0%o", vdp->vnode->modeBits);
+ break;
+ case col_links:
+ printf("%lu", afs_printable_uint32_lu(vdp->vnode->linkCount));
+ break;
+ case col_length:
+ VNDISK_GET_LEN(length, vdp->vnode);
+ printf("%llu", length);
+ break;
+ case col_uniq:
+ printf("%lu", afs_printable_uint32_lu(vdp->vnode->uniquifier));
+ break;
+ case col_dv:
+ printf("%lu", afs_printable_uint32_lu(vdp->vnode->dataVersion));
+ break;
+ case col_inode:
+ printf("%" AFS_UINT64_FMT, VNDISK_GET_INO(vdp->vnode));
+ break;
+ case col_namei:
+ PrintNamei(vdp->vp, vdp->vnode);
+ break;
+ case col_modtime:
+ printf("%lu",
+ afs_printable_uint32_lu(vdp->vnode->unixModifyTime));
+ break;
+ case col_author:
+ printf("%lu", afs_printable_uint32_lu(vdp->vnode->author));
+ break;
+ case col_owner:
+ printf("%lu", afs_printable_uint32_lu(vdp->vnode->owner));
+ break;
+ case col_parent:
+ printf("%lu", afs_printable_uint32_lu(vdp->vnode->parent));
+ break;
+ case col_magic:
+ printf("0x%08X", vdp->vnode->vnodeMagic);
+ break;
+ case col_lock:
+ printf("%lu.%lu",
+ afs_printable_uint32_lu(vdp->vnode->lock.lockCount),
+ afs_printable_uint32_lu(vdp->vnode->lock.lockTime));
+ break;
+ case col_smodtime:
+ printf("%lu",
+ afs_printable_uint32_lu(vdp->vnode->serverModifyTime));
+ break;
+ case col_group:
+ printf("%lu", afs_printable_uint32_lu(vdp->vnode->group));
+ break;
+ default:
+ fprintf(stderr, "%s: Unknown column type: %d (%d)\n", progname,
+ OutputColumn[i], i);
+ break;
+ }
+ }
+ printf("\n");
+}