3d4b277a3f512c2245d27fd5e0547342693e87ae
[openafs.git] / src / vol / vol-info.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11    System:              VICE-TWO
12    Module:              vol-info.c
13    Institution: The Information Technology Center, Carnegie-Mellon University
14
15    */
16
17 #include <afsconfig.h>
18 #include <afs/param.h>
19
20 #include <roken.h>
21
22 #include <ctype.h>
23
24 #ifdef HAVE_SYS_FILE_H
25 #include <sys/file.h>
26 #endif
27
28 #include <afs/cmd.h>
29 #include <afs/dir.h>
30 #include <afs/afsint.h>
31 #include <afs/errors.h>
32 #include <afs/acl.h>
33 #include <afs/prs_fs.h>
34 #include <rx/rx_queue.h>
35
36 #include "nfs.h"
37 #include "lock.h"
38 #include "ihandle.h"
39 #include "vnode.h"
40 #include "volume.h"
41 #include "partition.h"
42 #include "salvage.h"
43 #include "daemon_com_inline.h"
44 #include "fssync_inline.h"
45 #include "vol-info.h"
46
47 static int volinfo_init = 0;
48 static const char *progname = "";
49 static const char *PLACEHOLDER = "-";
50
51
52 /* Numeric column type ids */
53 typedef enum columnType {
54 #define c(x) col_##x,
55     VOLSCAN_COLUMNS
56 #undef c
57     max_column_type
58 } columnType;
59
60 struct columnName {
61     columnType type;
62     const char *name;
63 };
64
65 /* Table of id:name tuples of possible columns. */
66 struct columnName ColumnName[] = {
67 #define c(x) { col_##x, #x },
68     VOLSCAN_COLUMNS
69 #undef c
70     {max_column_type, NULL}
71 };
72
73
74 /* VnodeDetails union descriminator */
75 typedef enum {
76     VNODE_U_NONE,
77     VNODE_U_MOUNT,
78     VNODE_U_SYMLINK,
79     VNODE_U_POS_ACCESS,
80     VNODE_U_NEG_ACCESS
81 } vnode_details_u_t;
82
83 struct VnodeDetails {
84     Volume *vp;
85     VnodeClass class;
86     VnodeDiskObject *vnode;
87     VnodeId vnodeNumber;
88     afs_foff_t offset;
89     int index;
90     char *path;
91     vnode_details_u_t t;
92     union {
93         struct {
94             char type;
95             char *cell;
96             char *vol;
97         } mnt;
98         char *target;
99         struct acl_accessEntry *access;
100     } u;
101 };
102
103
104 static int NeedDirIndex;        /**< Large vnode index handle is needed for path lookups. */
105 static FdHandle_t *DirIndexFd = NULL; /**< Current large vnode index handle for path lookups. */
106
107 static int NumOutputColumns = 0;
108 static columnType OutputColumn[max_column_type];
109
110 /**
111  * Volume size running totals
112  */
113 struct sizeTotals {
114     afs_uint64 diskused_k;      /**< volume size from disk data file, in kilobytes */
115     afs_uint64 auxsize;         /**< size of header files, in bytes  */
116     afs_uint64 auxsize_k;       /**< size of header files, in kilobytes */
117     afs_uint64 vnodesize;       /**< size of the large and small vnodes, in bytes */
118     afs_uint64 vnodesize_k;     /**< size of the large and small vnodes, in kilobytes */
119     afs_uint64 size_k;          /**< size of headers and vnodes, in kilobytes */
120 };
121
122 static struct sizeTotals volumeTotals = { 0, 0, 0, 0, 0, 0 };
123 static struct sizeTotals partitionTotals = { 0, 0, 0, 0, 0, 0 };
124 static struct sizeTotals serverTotals = { 0, 0, 0, 0, 0, 0 };
125 static int PrintingVolumeSizes = 0;     /*print volume size lines */
126
127 /**
128  * List of procedures to call when scanning vnodes.
129  */
130 struct VnodeScanProc {
131     struct opr_queue link;
132     const char *heading;
133     void (*proc) (struct VolInfoOpt *opt, struct VnodeDetails * vdp);
134 };
135 static struct opr_queue VnodeScanLists[nVNODECLASSES];
136
137 /* Forward Declarations */
138 static void PrintHeader(Volume * vp);
139 static void HandleAllPart(struct VolInfoOpt *opt);
140 static void HandlePart(struct VolInfoOpt *opt, struct DiskPartition64 *partP);
141 static void HandleVolume(struct VolInfoOpt *opt, struct DiskPartition64 *partP, char *name);
142 static struct DiskPartition64 *FindCurrentPartition(void);
143 static Volume *AttachVolume(struct VolInfoOpt *opt, struct DiskPartition64 *dp, char *volname,
144                      struct VolumeHeader *header);
145 static void HandleVnodes(struct VolInfoOpt *opt, Volume * vp, VnodeClass class);
146 static void PrintColumnHeading(struct VolInfoOpt *opt);
147 static void PrintColumns(struct VolInfoOpt *opt, struct VnodeDetails *vdp, const char *desc);
148
149 /* externs */
150 extern void SetSalvageDirHandle(DirHandle * dir, afs_int32 volume,
151                                 Device device, Inode inode,
152                                 int *volumeChanged);
153 extern void FidZap(DirHandle * file);
154
155 /**
156  * Format time as a timestamp string
157  *
158  * @param[in] date  time value to format
159  *
160  * @return timestamp string in the form YYYY/MM/DD.hh:mm:ss
161  *
162  * @note A static array of 8 strings are stored by this
163  *       function. The array slots are overwritten, so the
164  *       caller must not reference the returned string after
165  *       seven additional calls to this function.
166  */
167 static char *
168 date(time_t date)
169 {
170 #define MAX_DATE_RESULT 100
171     static char results[8][MAX_DATE_RESULT];
172     static int next;
173     struct tm *tm = localtime(&date);
174     char buf[32];
175
176     (void)strftime(buf, 32, "%Y/%m/%d.%H:%M:%S", tm);   /* NT does not have %T */
177     snprintf(results[next = (next + 1) & 7], MAX_DATE_RESULT,
178              "%lu (%s)", (unsigned long)date, buf);
179     return results[next];
180 }
181
182 #ifdef AFS_NT40_ENV
183 /**
184  * Format file time as a timestamp string
185  *
186  * @param[in] ft  file time
187  *
188  * @return timestamp string in the form YYYY/MM/DD.hh:mm:ss
189  *
190  * @note A static array of 8 strings are stored by this
191  *       function. The array slots are overwritten, so the
192  *       caller must not reference the returned string after
193  *       seven additional calls to this function.
194  */
195 static char *
196 NT_date(FILETIME * ft)
197 {
198     static char result[8][64];
199     static int next = 0;
200     SYSTEMTIME st;
201     FILETIME lft;
202
203     if (!FileTimeToLocalFileTime(ft, &lft)
204         || !FileTimeToSystemTime(&lft, &st)) {
205         fprintf(stderr, "%s: Time conversion failed.\n", progname);
206         exit(1);
207     }
208     sprintf(result[next = ((next + 1) & 7)], "%4d/%02d/%02d.%2d:%2d:%2d",
209             st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
210     return result[next];
211 }
212 #endif
213
214 /**
215  * Add vnode size to the running volume totals.
216  *
217  * @param[in]  vdp   vnode details object
218  *
219  * @return none
220  */
221 void
222 volinfo_AddVnodeToSizeTotals(struct VolInfoOpt *opt, struct VnodeDetails *vdp)
223 {
224     afs_fsize_t fileLength;
225
226     VNDISK_GET_LEN(fileLength, vdp->vnode);
227     if (fileLength > 0) {
228         volumeTotals.vnodesize += fileLength;
229         volumeTotals.vnodesize_k += fileLength / 1024;
230     }
231 }
232
233 /**
234  * Print the volume size table heading line, if needed.
235  *
236  * @return none
237  */
238 static void
239 PrintVolumeSizeHeading(void)
240 {
241     if (!PrintingVolumeSizes) {
242         printf
243             ("Volume-Id\t  Volsize  Auxsize Inodesize  AVolsize SizeDiff                (VolName)\n");
244         PrintingVolumeSizes = 1;
245     }
246 }
247
248 /**
249  * Accumulate totals.
250  *
251  * @return 0
252  */
253 static void
254 AddSizeTotals(struct sizeTotals *a, struct sizeTotals *b)
255 {
256     a->diskused_k += b->diskused_k;
257     a->auxsize += b->auxsize;
258     a->auxsize_k += b->auxsize_k;
259     a->vnodesize += b->vnodesize;
260     a->vnodesize_k += b->vnodesize_k;
261     a->size_k += b->size_k;
262 }
263
264 /**
265  * Print the sizes for a volume.
266  *
267  * @return none
268  */
269 static void
270 PrintVolumeSizes(Volume * vp)
271 {
272     afs_int64 diff_k = volumeTotals.size_k - volumeTotals.diskused_k;
273
274     PrintVolumeSizeHeading();
275     printf("%" AFS_VOLID_FMT "\t%9llu%9llu%10llu%10llu%9lld\t%24s\n",
276            afs_printable_VolumeId_lu(V_id(vp)),
277            volumeTotals.diskused_k,
278            volumeTotals.auxsize_k, volumeTotals.vnodesize_k,
279            volumeTotals.size_k, diff_k, V_name(vp));
280 }
281
282 /**
283  * Print the size totals for the partition.
284  *
285  * @return none
286  */
287 static void
288 PrintPartitionTotals(afs_uint64 nvols)
289 {
290     afs_int64 diff_k = partitionTotals.size_k - partitionTotals.diskused_k;
291
292     PrintVolumeSizeHeading();
293     printf("\nPart Totals  %12llu%9llu%10llu%10llu%9lld (%llu volumes)\n\n",
294            partitionTotals.diskused_k, partitionTotals.auxsize,
295            partitionTotals.vnodesize, partitionTotals.size_k, diff_k, nvols);
296     PrintingVolumeSizes = 0;
297 }
298
299 /**
300  * Print the size totals for all the partitions.
301  *
302  * @return none
303  */
304 static void
305 PrintServerTotals(void)
306 {
307     afs_int64 diff_k = serverTotals.size_k - serverTotals.diskused_k;
308
309     printf("\nServer Totals%12lld%9lld%10lld%10lld%9lld\n",
310            serverTotals.diskused_k, serverTotals.auxsize,
311            serverTotals.vnodesize, serverTotals.size_k, diff_k);
312 }
313
314 /**
315  * Read a volume header file
316  *
317  * @param[in] ih        ihandle of the header file
318  * @param[in] to        destination
319  * @param[in] size      expected header size
320  * @param[in] magic     expected header magic number
321  * @param[in] version   expected header version number
322  *
323  * @return error code
324  *   @retval 0 success
325  *   @retval -1 failed to read file
326  */
327 static int
328 ReadHdr1(struct VolInfoOpt *opt, IHandle_t * ih, char *to, afs_sfsize_t size,
329          u_int magic, u_int version)
330 {
331     struct versionStamp *vsn;
332     int bad = 0;
333     int code;
334
335     vsn = (struct versionStamp *)to;
336
337     code = IH_IREAD(ih, 0, to, size);
338     if (code != size)
339         return -1;
340
341     if (vsn->magic != magic) {
342         bad++;
343         fprintf(stderr, "%s: Inode %s: Bad magic %x (%x): IGNORED\n",
344                 progname, PrintInode(NULL, ih->ih_ino), vsn->magic, magic);
345     }
346
347     /* Check is conditional, in case caller wants to inspect version himself */
348     if (version && vsn->version != version) {
349         bad++;
350         fprintf(stderr, "%s: Inode %s: Bad version %x (%x): IGNORED\n",
351                 progname,
352                 PrintInode(NULL, ih->ih_ino), vsn->version, version);
353     }
354     if (bad && opt->fixHeader) {
355         vsn->magic = magic;
356         vsn->version = version;
357         printf("Special index inode %s has a bad header. Reconstructing...\n",
358                PrintInode(NULL, ih->ih_ino));
359         code = IH_IWRITE(ih, 0, to, size);
360         if (code != size) {
361             fprintf(stderr,
362                     "%s: Write failed for inode %s; header left in damaged state\n",
363                     progname, PrintInode(NULL, ih->ih_ino));
364         }
365     }
366     if (!bad && opt->dumpInfo) {
367         printf("Inode %s: Good magic %x and version %x\n",
368                PrintInode(NULL, ih->ih_ino), magic, version);
369     }
370     return 0;
371 }
372
373 /**
374  * Simplified attach volume
375  *
376  * param[in] dp       vice disk partition object
377  * param[in] volname  volume header file name
378  * param[in] header   volume header object
379  *
380  * @return volume object or null on error
381  */
382 static Volume *
383 AttachVolume(struct VolInfoOpt *opt, struct DiskPartition64 *dp,
384              char *volname, struct VolumeHeader *header)
385 {
386     Volume *vp;
387     afs_int32 ec = 0;
388
389     if (opt->checkout) {
390         afs_int32 code;
391         SYNC_response response;
392         VolumeId volid = header->id;
393
394         memset(&response, 0, sizeof(response));
395         code =
396             FSYNC_VolOp(volid, dp->name, FSYNC_VOL_NEEDVOLUME, V_DUMP,
397                         &response);
398         if (code != SYNC_OK) {
399             if (response.hdr.reason == FSYNC_SALVAGE) {
400                 fprintf(stderr,
401                         "%s: file server says volume %lu is salvaging.\n",
402                         progname, afs_printable_uint32_lu(volid));
403                 return NULL;
404             } else {
405                 fprintf(stderr,
406                         "%s: attach of volume %lu denied by file server.\n",
407                         progname, afs_printable_uint32_lu(volid));
408                 return NULL;
409             }
410         }
411     }
412
413     vp = (Volume *) calloc(1, sizeof(Volume));
414     if (!vp) {
415         fprintf(stderr, "%s: Failed to allocate volume object.\n", progname);
416         return NULL;
417     }
418     vp->specialStatus = 0;
419     vp->device = dp->device;
420     vp->partition = dp;
421     IH_INIT(vp->vnodeIndex[vLarge].handle, dp->device, header->parent,
422             header->largeVnodeIndex);
423     IH_INIT(vp->vnodeIndex[vSmall].handle, dp->device, header->parent,
424             header->smallVnodeIndex);
425     IH_INIT(vp->diskDataHandle, dp->device, header->parent,
426             header->volumeInfo);
427     IH_INIT(V_linkHandle(vp), dp->device, header->parent, header->linkTable);
428     vp->cacheCheck = 0;         /* XXXX */
429     vp->shuttingDown = 0;
430     vp->goingOffline = 0;
431     vp->nUsers = 1;
432     vp->header = calloc(1, sizeof(*vp->header));
433     if (!vp->header) {
434         fprintf(stderr, "%s: Failed to allocate volume header.\n", progname);
435         free(vp);
436         return NULL;
437     }
438     ec = ReadHdr1(opt, V_diskDataHandle(vp), (char *)&V_disk(vp),
439                   sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
440     if (!ec) {
441         struct IndexFileHeader iHead;
442         ec = ReadHdr1(opt, vp->vnodeIndex[vSmall].handle, (char *)&iHead,
443                       sizeof(iHead), SMALLINDEXMAGIC, SMALLINDEXVERSION);
444     }
445     if (!ec) {
446         struct IndexFileHeader iHead;
447         ec = ReadHdr1(opt, vp->vnodeIndex[vLarge].handle, (char *)&iHead,
448                       sizeof(iHead), LARGEINDEXMAGIC, LARGEINDEXVERSION);
449     }
450 #ifdef AFS_NAMEI_ENV
451     if (!ec) {
452         struct versionStamp stamp;
453         ec = ReadHdr1(opt, V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
454                       LINKTABLEMAGIC, LINKTABLEVERSION);
455     }
456 #endif
457     if (ec)
458         return (Volume *) 0;
459     return vp;
460 }
461
462 /**
463  * Simplified detach volume
464  *
465  * param[in] vp       volume object from AttachVolume
466  *
467  * @return none
468  */
469 static void
470 DetachVolume(struct VolInfoOpt *opt, Volume * vp)
471 {
472     if (opt->checkout) {
473         afs_int32 code;
474         SYNC_response response;
475         memset(&response, 0, sizeof(response));
476
477         code = FSYNC_VolOp(V_id(vp), V_partition(vp)->name,
478                            FSYNC_VOL_ON, FSYNC_WHATEVER, &response);
479         if (code != SYNC_OK) {
480             fprintf(stderr, "%s: FSSYNC error %d (%s)\n", progname, code,
481                     SYNC_res2string(code));
482             fprintf(stderr, "%s:  protocol response code was %d (%s)\n",
483                     progname, response.hdr.response,
484                     SYNC_res2string(response.hdr.response));
485             fprintf(stderr, "%s:  protocol reason code was %d (%s)\n",
486                     progname, response.hdr.reason,
487                     FSYNC_reason2string(response.hdr.reason));
488         }
489     }
490
491     IH_RELEASE(vp->vnodeIndex[vLarge].handle);
492     IH_RELEASE(vp->vnodeIndex[vSmall].handle);
493     IH_RELEASE(vp->diskDataHandle);
494     IH_RELEASE(V_linkHandle(vp));
495     free(vp->header);
496     free(vp);
497 }
498
499 /**
500  * Convert the partition device number into a partition name.
501  *
502  * @param[in]   partId      partition number, 0 to 254
503  * @param[out]  partName    buffer to hold partition name (e.g. /vicepa)
504  * @param[in]   partNameSize    size of partName buffer
505  *
506  * @return status
507  *   @retval 0 success
508  *   @retval -1 error, partId is out of range
509  *   @retval -2 error, partition name exceeds partNameSize
510  */
511 static int
512 GetPartitionName(afs_uint32 partId, char *partName, afs_size_t partNameSize)
513 {
514     const afs_uint32 OLD_NUM_DEVICES = 26;      /* a..z */
515
516     if (partId < OLD_NUM_DEVICES) {
517         if (partNameSize < 8) {
518             return -2;
519         }
520         strlcpy(partName, "/vicep", partNameSize);
521         partName[6] = partId + 'a';
522         partName[7] = '\0';
523         return 0;
524     }
525     if (partId < VOLMAXPARTS) {
526         if (partNameSize < 9) {
527             return -2;
528         }
529         strlcpy(partName, "/vicep", partNameSize);
530         partId -= OLD_NUM_DEVICES;
531         partName[6] = 'a' + (partId / OLD_NUM_DEVICES);
532         partName[7] = 'a' + (partId % OLD_NUM_DEVICES);
533         partName[8] = '\0';
534         return 0;
535     }
536     return -1;
537 }
538
539 /**
540  * Scan the volumes in the partitions
541  *
542  * Scan the specified volume in the specified partition if both
543  * are given. Scan all the volumes in the specified partition if
544  * only the partition is given.  If neither a partition nor volume
545  * is given, scan all the volumes in all the partitions.  If only
546  * the volume is given, attempt to find it in the current working
547  * directory.
548  *
549  * @param[in] partNameOrId   partition name or id to be scannned
550  * @param[in] volumeId       volume id to be scanned
551  *
552  * @return 0 if successful
553  */
554 int
555 volinfo_ScanPartitions(struct VolInfoOpt *opt, char *partNameOrId, VolumeId volumeId)
556 {
557     int err = 0;
558     char partName[64] = "";
559     struct DiskPartition64 *partP = NULL;
560
561     if (opt->checkout) {
562         if (!FSYNC_clientInit()) {
563             fprintf(stderr, "%s: Failed to connect to fileserver.\n",
564                     progname);
565             return 1;
566         }
567     }
568
569
570     /* Allow user to specify partition by name or id. */
571     if (partNameOrId) {
572         afs_uint32 partId = volutil_GetPartitionID(partNameOrId);
573         if (partId == -1) {
574             fprintf(stderr, "%s: Could not parse '%s' as a partition name.\n",
575                     progname, partNameOrId);
576             err = 1;
577             goto cleanup;
578         }
579         if (GetPartitionName(partId, partName, sizeof(partName))) {
580             fprintf(stderr,
581                     "%s: Could not format '%s' as a partition name.\n",
582                     progname, partNameOrId);
583             err = 1;
584             goto cleanup;
585         }
586     }
587
588     err = VAttachPartitions();
589     if (err) {
590         fprintf(stderr, "%s: %d partitions had errors during attach.\n",
591                 progname, err);
592         err = 1;
593         goto cleanup;
594     }
595
596     if (partName[0]) {
597         partP = VGetPartition(partName, 0);
598         if (!partP) {
599             fprintf(stderr,
600                     "%s: %s is not an AFS partition name on this server.\n",
601                     progname, partName);
602             err = 1;
603             goto cleanup;
604         }
605     }
606
607     if (!volumeId) {
608         if (opt->printHeading) {
609             PrintColumnHeading(opt);
610         }
611         if (!partP) {
612             HandleAllPart(opt);
613         } else {
614             HandlePart(opt, partP);
615         }
616     } else {
617         char name1[128];
618
619         if (!partP) {
620             partP = FindCurrentPartition();
621             if (!partP) {
622                 fprintf(stderr,
623                         "%s: Current partition is not a vice partition.\n",
624                         progname);
625                 err = 1;
626                 goto cleanup;
627             }
628         }
629         snprintf(name1, sizeof name1, VFORMAT,
630                  afs_printable_VolumeId_lu(volumeId));
631         if (opt->printHeading) {
632             PrintColumnHeading(opt);
633         }
634         HandleVolume(opt, partP, name1);
635     }
636
637   cleanup:
638     if (opt->checkout) {
639         FSYNC_clientFinis();
640     }
641     return err;
642 }
643
644 /**
645  * Add a vnode scanning procedure
646  *
647  * @param[in]   class   vnode class for this handler
648  * @param[in]   proc    handler proc to be called by HandleVnodes
649  * @param[in]   heading optional string to pring before scanning vnodes
650  *
651  * @return none
652  */
653 void
654 volinfo_AddVnodeHandler(int vnodeClass,
655                 void (*proc) (struct VolInfoOpt * opt,
656                               struct VnodeDetails * vdp), const char *heading)
657 {
658     struct VnodeScanProc *entry = malloc(sizeof(struct VnodeScanProc));
659     entry->proc = proc;
660     entry->heading = heading;
661     opr_queue_Append(&VnodeScanLists[vnodeClass], (struct opr_queue *)entry);
662 }
663
664 /**
665  * Initialize the vol-info module.
666  */
667 int
668 volinfo_Init(const char *aprogname)
669 {
670     if (!volinfo_init) {
671         progname = aprogname;  /* for error messages */
672 #ifndef AFS_NT40_ENV
673         if (geteuid() != 0) {
674             fprintf(stderr, "%s: Must be run as root.\n", progname);
675             return EPERM;
676         }
677 #endif
678         DInit(1024);
679         VInitVnodes(vLarge, 0);
680         VInitVnodes(vSmall, 0);
681         opr_queue_Init(&VnodeScanLists[vLarge]);
682         opr_queue_Init(&VnodeScanLists[vSmall]);
683         volinfo_init = 1;
684     }
685     return 0;
686 }
687
688 /**
689  * Alloc an options structure with default values.
690  *
691  * The caller must free the options pointer after use.
692  *
693  * @param[out] optp address of options pointer
694  * @return 0 on success
695  */
696 int
697 volinfo_Options(struct VolInfoOpt **optp)
698 {
699     struct VolInfoOpt *opt;
700
701     opt = calloc(1, sizeof(struct VolInfoOpt));
702     if (!opt) {
703         fprintf(stderr, "%s: Failed to allocate options (%d).\n", progname, ENOMEM);
704         return ENOMEM;
705     }
706     opt->dumpInfo = 1;
707     gethostname(opt->hostname, sizeof(opt->hostname));
708     opt->columnDelim[0] = ' ';
709     opt->checkMagic = 1;
710     *optp = opt;
711     return 0;
712 }
713
714 /**
715  * Add a column type to be displayed.
716  *
717  * @param[in]  name   column type name
718  *
719  * @return error code
720  *   @retval 0 success
721  *   @retval 1 too many columns
722  *   @retval 2 invalid column name
723  */
724 int
725 volinfo_AddOutputColumn(char *name)
726 {
727     int i;
728
729     if (NumOutputColumns >= sizeof(OutputColumn) / sizeof(*OutputColumn)) {
730         fprintf(stderr, "%s: Too many output columns (%d).\n", progname,
731                 NumOutputColumns);
732         return 1;
733     }
734     for (i = 0; i < max_column_type; i++) {
735         if (!strcmp(ColumnName[i].name, name)) {
736             columnType t = ColumnName[i].type;
737             OutputColumn[NumOutputColumns++] = t;
738
739             if (t == col_path) {
740                 NeedDirIndex = 1;
741             }
742             return 0;
743         }
744     }
745     return 2;
746 }
747
748
749 /**
750  * Determine if the current directory is a vice partition
751  *
752  * @return disk partition object
753  */
754 #ifdef AFS_NT40_ENV
755 #include <direct.h>
756 static struct DiskPartition64 *
757 FindCurrentPartition(void)
758 {
759     int dr = _getdrive();
760     struct DiskPartition64 *dp;
761
762     dr--;
763     for (dp = DiskPartitionList; dp; dp = dp->next) {
764         if (*dp->devName - 'A' == dr)
765             break;
766     }
767     if (!dp) {
768         fprintf(stderr, "%s: Current drive is not a valid vice partition.\n",
769                 progname);
770     }
771     return dp;
772 }
773 #else
774 static struct DiskPartition64 *
775 FindCurrentPartition(void)
776 {
777     char partName[1024];
778     char tmp = '\0';
779     char *p;
780     struct DiskPartition64 *dp;
781
782     if (!getcwd(partName, 1023)) {
783         fprintf(stderr, "%s: Failed to get current working directory: ",
784                 progname);
785         perror("pwd");
786         return NULL;
787     }
788     p = strchr(&partName[1], OS_DIRSEPC);
789     if (p) {
790         tmp = *p;
791         *p = '\0';
792     }
793     if (!(dp = VGetPartition(partName, 0))) {
794         if (tmp)
795             *p = tmp;
796         fprintf(stderr, "%s: %s is not a valid vice partition.\n", progname,
797                 partName);
798         return NULL;
799     }
800     return dp;
801 }
802 #endif
803
804 /**
805  * Scan and handle all the partitions detected on this server
806  *
807  * @return none
808  */
809 static void
810 HandleAllPart(struct VolInfoOpt *opt)
811 {
812     struct DiskPartition64 *partP;
813
814     for (partP = DiskPartitionList; partP; partP = partP->next) {
815         if (opt->dumpInfo || opt->saveInodes || opt->showSizes) {
816             printf("Processing Partition %s:\n", partP->name);
817         }
818         HandlePart(opt, partP);
819         if (opt->showSizes) {
820             AddSizeTotals(&serverTotals, &partitionTotals);
821         }
822     }
823
824     if (opt->showSizes) {
825         PrintServerTotals();
826     }
827 }
828
829 /**
830  * Scan the partition and handle volumes
831  *
832  * @param[in] partP  disk partition to scan
833  *
834  * @return none
835  */
836 static void
837 HandlePart(struct VolInfoOpt *opt, struct DiskPartition64 *partP)
838 {
839     afs_int64 nvols = 0;
840     DIR *dirp;
841     struct dirent *dp;
842 #ifdef AFS_NT40_ENV
843     char pname[64];
844     char *p = pname;
845     (void)sprintf(pname, "%s\\", VPartitionPath(partP));
846 #else
847     char *p = VPartitionPath(partP);
848 #endif
849
850     if ((dirp = opendir(p)) == NULL) {
851         fprintf(stderr, "%s: Can't read directory %s; giving up\n", progname,
852                 p);
853         return;
854     }
855     while ((dp = readdir(dirp))) {
856         p = (char *)strrchr(dp->d_name, '.');
857         if (p != NULL && strcmp(p, VHDREXT) == 0) {
858             HandleVolume(opt, partP, dp->d_name);
859             if (opt->showSizes) {
860                 nvols++;
861                 AddSizeTotals(&partitionTotals, &volumeTotals);
862             }
863         }
864     }
865     closedir(dirp);
866     if (opt->showSizes) {
867         PrintPartitionTotals(nvols);
868     }
869 }
870
871 /**
872  * Inspect a volume header special file.
873  *
874  * @param[in]    name       descriptive name of the type of header special file
875  * @param[in]    dp         partition object for this volume
876  * @param[in]    header     header object for this volume
877  * @param[in]    inode      fileserver inode number for this header special file
878  * @param[inout] psize      total of the header special file sizes
879  *
880  * @return none
881  */
882 static void
883 HandleSpecialFile(struct VolInfoOpt *opt, const char *name, struct DiskPartition64 *dp,
884                   struct VolumeHeader *header, Inode inode,
885                   afs_sfsize_t * psize)
886 {
887     afs_sfsize_t size = -1;
888     IHandle_t *ih = NULL;
889     FdHandle_t *fdP = NULL;
890 #ifdef AFS_NAMEI_ENV
891     namei_t filename;
892 #endif /* AFS_NAMEI_ENV */
893
894     IH_INIT(ih, dp->device, header->parent, inode);
895     fdP = IH_OPEN(ih);
896     if (fdP == NULL) {
897         fprintf(stderr,
898                 "%s: Error opening header file '%s' for volume %" AFS_VOLID_FMT "\n", progname,
899                 name, afs_printable_VolumeId_lu(header->id));
900         perror("open");
901         goto error;
902     }
903     size = FDH_SIZE(fdP);
904     if (size == -1) {
905         fprintf(stderr,
906                 "%s: Error getting size of header file '%s' for volume %" AFS_VOLID_FMT "\n",
907                 progname, name, afs_printable_VolumeId_lu(header->id));
908         perror("fstat");
909         goto error;
910     }
911     *psize += size;
912
913   error:
914     if (opt->dumpInfo) {
915         printf("\t%s inode\t= %s (size = ", name, PrintInode(NULL, inode));
916         if (size != -1) {
917             printf("%lld)\n", size);
918         } else {
919             printf("unknown)\n");
920         }
921 #ifdef AFS_NAMEI_ENV
922         namei_HandleToName(&filename, ih);
923         printf("\t%s namei\t= %s\n", name, filename.n_path);
924 #endif /* AFS_NAMEI_ENV */
925     }
926
927     if (fdP != NULL) {
928         FDH_REALLYCLOSE(fdP);
929     }
930     if (ih != NULL) {
931         IH_RELEASE(ih);
932     }
933 }
934
935 /**
936  * Inspect this volume header files.
937  *
938  * @param[in]  dp         partition object for this volume
939  * @param[in]  header_fd  volume header file descriptor
940  * @param[in]  header     volume header object
941  *
942  * @return none
943  */
944 static void
945 HandleHeaderFiles(struct VolInfoOpt *opt, struct DiskPartition64 *dp, FD_t header_fd,
946                   struct VolumeHeader *header)
947 {
948     afs_sfsize_t size = 0;
949
950     if (opt->dumpInfo) {
951         size = OS_SIZE(header_fd);
952         printf("Volume header (size = %lld):\n", size);
953         printf("\tstamp\t= 0x%x\n", header->stamp.version);
954         printf("\tVolId\t= %" AFS_VOLID_FMT "\n", afs_printable_VolumeId_lu(header->id));
955         printf("\tparent\t= %" AFS_VOLID_FMT "\n", afs_printable_VolumeId_lu(header->parent));
956     }
957
958     HandleSpecialFile(opt, "Info", dp, header, header->volumeInfo, &size);
959     HandleSpecialFile(opt, "Small", dp, header, header->smallVnodeIndex,
960                       &size);
961     HandleSpecialFile(opt, "Large", dp, header, header->largeVnodeIndex,
962                       &size);
963 #ifdef AFS_NAMEI_ENV
964     HandleSpecialFile(opt, "Link", dp, header, header->linkTable, &size);
965 #endif /* AFS_NAMEI_ENV */
966
967     if (opt->dumpInfo) {
968         printf("Total aux volume size = %lld\n\n", size);
969     }
970
971     if (opt->showSizes) {
972         volumeTotals.auxsize = size;
973         volumeTotals.auxsize_k = size / 1024;
974     }
975 }
976
977 /**
978  * Determine if the vnodes of this volume should be scanned.
979  *
980  * @param[in]  vp   volume object
981  *
982  * @return true if vnodes should be scanned
983  */
984 static int
985 IsScannable(struct VolInfoOpt *opt, Volume * vp)
986 {
987     if (opr_queue_IsEmpty(&VnodeScanLists[vLarge]) &&
988         opr_queue_IsEmpty(&VnodeScanLists[vSmall])) {
989         return 0;
990     }
991     if (!opt->scanVolType) {
992         return 1;               /* filtering disabled; do not check vol type */
993     }
994     switch (V_type(vp)) {
995     case RWVOL:
996         return opt->scanVolType & SCAN_RW;
997     case ROVOL:
998         return opt->scanVolType & SCAN_RO;
999     case BACKVOL:
1000         return opt->scanVolType & SCAN_BK;
1001     default:
1002         fprintf(stderr, "%s: Volume %" AFS_VOLID_FMT "; Unknown volume type %d\n", progname,
1003                 afs_printable_VolumeId_lu(V_id(vp)), V_type(vp));
1004         break;
1005     }
1006     return 0;
1007 }
1008
1009 /**
1010  * Attach and scan the volume and handle the header and vnodes
1011  *
1012  * Print the volume header and vnode information, depending on the
1013  * current modes.
1014  *
1015  * @param[in] dp    vice partition object for this volume
1016  * @param[in] name  volume header file name
1017  *
1018  * @return none
1019  */
1020 static void
1021 HandleVolume(struct VolInfoOpt *opt, struct DiskPartition64 *dp, char *name)
1022 {
1023     struct VolumeHeader header;
1024     struct VolumeDiskHeader diskHeader;
1025     FD_t fd = INVALID_FD;
1026     Volume *vp = NULL;
1027     char headerName[1024];
1028     afs_sfsize_t n;
1029
1030     snprintf(headerName, sizeof headerName, "%s" OS_DIRSEP "%s",
1031              VPartitionPath(dp), name);
1032     if ((fd = OS_OPEN(headerName, O_RDONLY, 0666)) == INVALID_FD) {
1033         fprintf(stderr, "%s: Cannot open volume header %s\n", progname, name);
1034         goto cleanup;
1035     }
1036     if (OS_SIZE(fd) < 0) {
1037         fprintf(stderr, "%s: Cannot read volume header %s\n", progname, name);
1038         goto cleanup;
1039     }
1040     n = OS_READ(fd, &diskHeader, sizeof(diskHeader));
1041     if (n != sizeof(diskHeader)
1042         || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
1043         fprintf(stderr, "%s: Error reading volume header %s\n", progname,
1044                 name);
1045         goto cleanup;
1046     }
1047     if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
1048         fprintf(stderr,
1049                 "%s: Volume %s, version number is incorrect; volume needs to be salvaged\n",
1050                 progname, name);
1051         goto cleanup;
1052     }
1053
1054     DiskToVolumeHeader(&header, &diskHeader);
1055     if (opt->dumpHeader || opt->showSizes) {
1056         HandleHeaderFiles(opt, dp, fd, &header);
1057     }
1058
1059     vp = AttachVolume(opt, dp, name, &header);
1060     if (!vp) {
1061         fprintf(stderr, "%s: Error attaching volume header %s\n",
1062                 progname, name);
1063         goto cleanup;
1064     }
1065
1066     if (opt->dumpInfo) {
1067         PrintHeader(vp);
1068     }
1069     if (IsScannable(opt, vp)) {
1070         if (NeedDirIndex) {
1071             IHandle_t *ih = vp->vnodeIndex[vLarge].handle;
1072             DirIndexFd = IH_OPEN(ih);
1073             if (DirIndexFd == NULL) {
1074                 fprintf(stderr, "%s: Failed to open index for directories.",
1075                         progname);
1076             }
1077         }
1078
1079         HandleVnodes(opt, vp, vLarge);
1080         HandleVnodes(opt, vp, vSmall);
1081
1082         if (DirIndexFd) {
1083             FDH_CLOSE(DirIndexFd);
1084             DirIndexFd = NULL;
1085         }
1086     }
1087     if (opt->showSizes) {
1088         volumeTotals.diskused_k = V_diskused(vp);
1089         volumeTotals.size_k =
1090             volumeTotals.auxsize_k + volumeTotals.vnodesize_k;
1091         if (opt->saveInodes) {
1092             PrintingVolumeSizes = 0;    /* print heading again */
1093         }
1094         PrintVolumeSizes(vp);
1095     }
1096
1097   cleanup:
1098     if (fd != INVALID_FD) {
1099         OS_CLOSE(fd);
1100     }
1101     if (vp) {
1102         DetachVolume(opt, vp);
1103     }
1104 }
1105
1106 /**
1107  * Return a display string for the volume type.
1108  *
1109  * @param[in]  type  volume type
1110  *
1111  * @return volume type description string
1112  */
1113 static_inline char *
1114 volumeTypeString(int type)
1115 {
1116     return
1117         (type == RWVOL ? "read/write" :
1118          (type == ROVOL ? "readonly" :
1119           (type == BACKVOL ? "backup" : "unknown")));
1120 }
1121
1122 /**
1123  * Return a short display string for the volume type.
1124  *
1125  * @param[in]  type  volume type
1126  *
1127  * @return volume type short description string
1128  */
1129 static_inline char *
1130 volumeTypeShortString(int type)
1131 {
1132     return
1133         (type == RWVOL ? "RW" :
1134          (type == ROVOL ? "RO" : (type == BACKVOL ? "BK" : "??")));
1135 }
1136
1137 /**
1138  * Print the volume header information
1139  *
1140  * @param[in]  volume object
1141  *
1142  * @return none
1143  */
1144 static void
1145 PrintHeader(Volume * vp)
1146 {
1147     printf("Volume header for volume %" AFS_VOLID_FMT " (%s)\n", afs_printable_VolumeId_lu(V_id(vp)), V_name(vp));
1148     printf("stamp.magic = %x, stamp.version = %u\n", V_stamp(vp).magic,
1149            V_stamp(vp).version);
1150     printf
1151         ("inUse = %d, inService = %d, blessed = %d, needsSalvaged = %d, dontSalvage = %d\n",
1152          V_inUse(vp), V_inService(vp), V_blessed(vp), V_needsSalvaged(vp),
1153          V_dontSalvage(vp));
1154     printf
1155         ("type = %d (%s), uniquifier = %u, needsCallback = %d, destroyMe = %x\n",
1156          V_type(vp), volumeTypeString(V_type(vp)), V_uniquifier(vp),
1157          V_needsCallback(vp), V_destroyMe(vp));
1158     printf
1159         ("id = %" AFS_VOLID_FMT ", parentId = %" AFS_VOLID_FMT ", cloneId = %" AFS_VOLID_FMT ", backupId = %" AFS_VOLID_FMT ", restoredFromId = %" AFS_VOLID_FMT "\n",
1160          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)));
1161     printf
1162         ("maxquota = %d, minquota = %d, maxfiles = %d, filecount = %d, diskused = %d\n",
1163          V_maxquota(vp), V_minquota(vp), V_maxfiles(vp), V_filecount(vp),
1164          V_diskused(vp));
1165     printf("creationDate = %s, copyDate = %s\n", date(V_creationDate(vp)),
1166            date(V_copyDate(vp)));
1167     printf("backupDate = %s, expirationDate = %s\n", date(V_backupDate(vp)),
1168            date(V_expirationDate(vp)));
1169     printf("accessDate = %s, updateDate = %s\n", date(V_accessDate(vp)),
1170            date(V_updateDate(vp)));
1171     printf("owner = %u, accountNumber = %u\n", V_owner(vp),
1172            V_accountNumber(vp));
1173     printf
1174         ("dayUse = %u; week = (%u, %u, %u, %u, %u, %u, %u), dayUseDate = %s\n",
1175          V_dayUse(vp), V_weekUse(vp)[0], V_weekUse(vp)[1], V_weekUse(vp)[2],
1176          V_weekUse(vp)[3], V_weekUse(vp)[4], V_weekUse(vp)[5],
1177          V_weekUse(vp)[6], date(V_dayUseDate(vp)));
1178     printf("volUpdateCounter = %u\n", V_volUpdateCounter(vp));
1179 }
1180
1181 /**
1182  * Get the size and times of a file
1183  *
1184  * @param[in]  fd     file descriptor of file to stat
1185  * @param[out] size   size of the file
1186  * @param[out] ctime  ctime of file as a formatted string
1187  * @param[out] mtime  mtime of file as a formatted string
1188  * @param[out] atime  atime of file as a formatted string
1189  *
1190  * @return error code
1191  *   @retval 0 success
1192  *   @retval -1 failed to retrieve file information
1193  */
1194 static int
1195 GetFileInfo(FD_t fd, afs_sfsize_t * size, char **ctime, char **mtime,
1196             char **atime)
1197 {
1198 #ifdef AFS_NT40_ENV
1199     BY_HANDLE_FILE_INFORMATION fi;
1200     LARGE_INTEGER fsize;
1201     if (!GetFileInformationByHandle(fd, &fi)) {
1202         fprintf(stderr, "%s: GetFileInformationByHandle failed\n", progname);
1203         return -1;
1204     }
1205     if (!GetFileSizeEx(fd, &fsize)) {
1206         fprintf(stderr, "%s: GetFileSizeEx failed\n", progname);
1207         return -1;
1208     }
1209     *size = fsize.QuadPart;
1210     *ctime = "N/A";
1211     *mtime = NT_date(&fi.ftLastWriteTime);
1212     *atime = NT_date(&fi.ftLastAccessTime);
1213 #else
1214     struct afs_stat_st status;
1215     if (afs_fstat(fd, &status) == -1) {
1216         fprintf(stderr, "%s: fstat failed %d\n", progname, errno);
1217         return -1;
1218     }
1219     *size = status.st_size;
1220     *ctime = date(status.st_ctime);
1221     *mtime = date(status.st_mtime);
1222     *atime = date(status.st_atime);
1223 #endif
1224     return 0;
1225 }
1226
1227 /**
1228  * Copy the inode data to a file in the current directory.
1229  *
1230  * @param[in] vdp     vnode details object
1231  *
1232  * @return none
1233  */
1234 void
1235 volinfo_SaveInode(struct VolInfoOpt *opt, struct VnodeDetails *vdp)
1236 {
1237     IHandle_t *ih;
1238     FdHandle_t *fdP;
1239     char nfile[50], buffer[256];
1240     int ofd = 0;
1241     afs_foff_t total;
1242     ssize_t len;
1243     Inode ino = VNDISK_GET_INO(vdp->vnode);
1244
1245     if (!VALID_INO(ino)) {
1246         return;
1247     }
1248
1249     IH_INIT(ih, V_device(vdp->vp), V_parentId(vdp->vp), ino);
1250     fdP = IH_OPEN(ih);
1251     if (fdP == NULL) {
1252         fprintf(stderr,
1253                 "%s: Can't open inode %s error %d (ignored)\n",
1254                 progname, PrintInode(NULL, ino), errno);
1255         return;
1256     }
1257     snprintf(nfile, sizeof nfile, "TmpInode.%s", PrintInode(NULL, ino));
1258     ofd = afs_open(nfile, O_CREAT | O_RDWR | O_TRUNC, 0600);
1259     if (ofd < 0) {
1260         fprintf(stderr,
1261                 "%s: Can't create file %s; error %d (ignored)\n",
1262                 progname, nfile, errno);
1263
1264         FDH_REALLYCLOSE(fdP);
1265         IH_RELEASE(ih);
1266         return;
1267     }
1268     total = 0;
1269     while (1) {
1270         ssize_t nBytes;
1271         len = FDH_PREAD(fdP, buffer, sizeof(buffer), total);
1272         if (len < 0) {
1273             FDH_REALLYCLOSE(fdP);
1274             IH_RELEASE(ih);
1275             close(ofd);
1276             unlink(nfile);
1277             fprintf(stderr,
1278                     "%s: Error while reading from inode %s (%d)\n",
1279                     progname, PrintInode(NULL, ino), errno);
1280             return;
1281         }
1282         if (len == 0)
1283             break;              /* No more input */
1284         nBytes = write(ofd, buffer, (size_t)len);
1285         if (nBytes != len) {
1286             FDH_REALLYCLOSE(fdP);
1287             IH_RELEASE(ih);
1288             close(ofd);
1289             unlink(nfile);
1290             fprintf(stderr,
1291                     "%s: Error while writing to \"%s\" (%d - ignored)\n",
1292                     progname, nfile, errno);
1293             return;
1294         }
1295         total += len;
1296     }
1297
1298     FDH_REALLYCLOSE(fdP);
1299     IH_RELEASE(ih);
1300     close(ofd);
1301     printf("... Copied inode %s to file %s (%lu bytes)\n",
1302            PrintInode(NULL, ino), nfile, (unsigned long)total);
1303 }
1304
1305 /**
1306  * get the VnodeDiskObject for a directory given its vnode id.
1307  *
1308  * @param[in]   vp       volume object
1309  * @param[in]   parent   vnode id to read
1310  * @param[out]  pvn      vnode disk object to populate
1311  *
1312  * @post pvn contains copy of disk object for parent id
1313  *
1314  * @return operation status
1315  *   @retval 0   success
1316  *   @retval -1  failure
1317  */
1318 static int
1319 GetDirVnode(struct VolInfoOpt *opt, Volume * vp, VnodeId parent, VnodeDiskObject * pvn)
1320 {
1321     afs_int32 code;
1322     afs_foff_t offset;
1323
1324     if (!DirIndexFd) {
1325         return -1;              /* previously failed to open the large vnode index. */
1326     }
1327     if (parent % 2 == 0) {
1328         fprintf(stderr, "%s: Invalid parent vnode id %lu in volume %lu\n",
1329                 progname,
1330                 afs_printable_uint32_lu(parent),
1331                 afs_printable_uint32_lu(V_id(vp)));
1332     }
1333     offset = vnodeIndexOffset(&VnodeClassInfo[vLarge], parent);
1334     code = FDH_SEEK(DirIndexFd, offset, 0);
1335     if (code == -1) {
1336         fprintf(stderr,
1337                 "%s: GetDirVnode: seek failed for %lu.%lu to offset %llu\n",
1338                 progname, afs_printable_uint32_lu(V_id(vp)),
1339                 afs_printable_uint32_lu(parent), (long long unsigned)offset);
1340         return -1;
1341     }
1342     code = FDH_READ(DirIndexFd, pvn, SIZEOF_LARGEDISKVNODE);
1343     if (code != SIZEOF_LARGEDISKVNODE) {
1344         fprintf(stderr,
1345                 "%s: GetDirVnode: read failed for %lu.%lu at offset %llu\n",
1346                 progname, afs_printable_uint32_lu(V_id(vp)),
1347                 afs_printable_uint32_lu(parent), (long long unsigned)offset);
1348         return -1;
1349     }
1350     if (opt->checkMagic && (pvn->vnodeMagic != LARGEVNODEMAGIC)) {
1351         fprintf(stderr, "%s: GetDirVnode: bad vnode magic for %lu.%lu at offset %llu\n",
1352                 progname, afs_printable_uint32_lu(V_id(vp)),
1353                 afs_printable_uint32_lu(parent), (long long unsigned)offset);
1354         return -1;
1355     }
1356     if (!pvn->dataVersion) {
1357         fprintf(stderr, "%s: GetDirVnode: dv is zero for %lu.%lu at offset %llu\n",
1358                 progname, afs_printable_uint32_lu(V_id(vp)),
1359                 afs_printable_uint32_lu(parent), (long long unsigned)offset);
1360         return -1;
1361     }
1362     return 0;
1363 }
1364
1365 /**
1366  * Perform inverse lookup on a vice directory object to map a fid onto a dirent string.
1367  *
1368  * @param[in]   vp          volume object
1369  * @param[in]   pvnode      parent directory vnode object
1370  * @param[in]   cvnid       child vnode id to inverse lookup
1371  * @param[in]   cuniq       child uniquifier to inverse lookup
1372  * @param[out]  dirent      buffer in which to store dirent string
1373  * @param[out]  dirent_len  length of dirent buffer
1374  *
1375  * @post dirent contains string for the (cvnid, cuniq) entry
1376  *
1377  * @return operation status
1378  *    @retval 0 success
1379  */
1380 static int
1381 GetDirEntry(Volume * vp, VnodeDiskObject * pvnode, VnodeId cvnid,
1382             afs_uint32 cuniq, char *dirent, size_t dirent_len)
1383 {
1384     DirHandle dir;
1385     Inode ino;
1386     int volumeChanged;
1387     afs_int32 code;
1388
1389     ino = VNDISK_GET_INO(pvnode);
1390     if (!VALID_INO(ino)) {
1391         fprintf(stderr, "%s: GetDirEntry invalid parent ino\n", progname);
1392         return -1;
1393     }
1394     SetSalvageDirHandle(&dir, V_parentId(vp), V_device(vp), ino,
1395                         &volumeChanged);
1396     code = afs_dir_InverseLookup(&dir, cvnid, cuniq, dirent, dirent_len);
1397     if (code) {
1398         fprintf(stderr, "%s: afs_dir_InverseLookup failed with code %d\n",
1399                 progname, code);
1400     }
1401     FidZap(&dir);
1402     return code;
1403 }
1404
1405 /**
1406  * Lookup the path of this vnode, relative to the root of the volume.
1407  *
1408  * @param[in] vdp    vnode details
1409  *
1410  * @return status
1411  *   @retval 0 success
1412  *   @retval -1 error
1413  */
1414 static int
1415 LookupPath(struct VolInfoOpt *opt, struct VnodeDetails *vdp)
1416 {
1417 #define MAX_PATH_LEN 1023
1418     static char path_buffer[MAX_PATH_LEN + 1];
1419     static char dirent[MAX_PATH_LEN + 1];
1420     char vnode_buffer[SIZEOF_LARGEDISKVNODE];
1421     struct VnodeDiskObject *pvn = (struct VnodeDiskObject *)vnode_buffer;
1422     int code = 0;
1423     int space;
1424     char *cursor;
1425     Volume *vp = vdp->vp;
1426     VnodeId parent = vdp->vnode->parent;
1427     VnodeId cvnid = vdp->vnodeNumber;
1428     afs_uint32 cuniq = vdp->vnode->uniquifier;
1429
1430     if (!parent) {
1431         vdp->path = "/";        /* this is root */
1432         return 0;
1433     }
1434
1435     space = sizeof(path_buffer) - 1;
1436     cursor = &path_buffer[space];
1437     *cursor = '\0';
1438
1439     while (parent) {
1440         int len;
1441
1442         code = GetDirVnode(opt, vp, parent, pvn);
1443         if (code) {
1444             cursor = NULL;
1445             break;
1446         }
1447         code = GetDirEntry(vp, pvn, cvnid, cuniq, dirent, MAX_PATH_LEN);
1448         if (code) {
1449             cursor = NULL;
1450             break;
1451         }
1452
1453         len = strlen(dirent);
1454         if (len == 0) {
1455             fprintf(stderr,
1456                     "%s: Failed to lookup path for fid %lu.%lu.%lu: empty dir entry\n",
1457                     progname, afs_printable_uint32_lu(V_id(vdp->vp)),
1458                     afs_printable_uint32_lu(vdp->vnodeNumber),
1459                     afs_printable_uint32_lu(vdp->vnode->uniquifier));
1460             cursor = NULL;
1461             code = -1;
1462             break;
1463         }
1464
1465         if (space < (len + 1)) {
1466             fprintf(stderr,
1467                     "%s: Failed to lookup path for fid %lu.%lu.%lu: path exceeds max length (%u).\n",
1468                     progname, afs_printable_uint32_lu(V_id(vdp->vp)),
1469                     afs_printable_uint32_lu(vdp->vnodeNumber),
1470                     afs_printable_uint32_lu(vdp->vnode->uniquifier),
1471                     MAX_PATH_LEN);
1472             cursor = NULL;
1473             code = -1;
1474             break;
1475         }
1476
1477         /* prepend path component */
1478         cursor -= len;
1479         memcpy(cursor, dirent, len);
1480         *--cursor = '/';
1481         space -= (len + 1);
1482
1483         /* next parent */
1484         cvnid = parent;
1485         cuniq = pvn->uniquifier;
1486         parent = pvn->parent;
1487     }
1488
1489     if (cursor) {
1490         vdp->path = cursor;
1491     }
1492     return code;
1493 }
1494
1495 /**
1496  * Read the symlink target and determine if this vnode is a mount point.
1497  *
1498  * @param[inout]   vdp    vnode details object
1499  *
1500  * @return error code
1501  *   @retval 0 success
1502  *   @retval -1 failure
1503  */
1504 static int
1505 ReadSymlinkTarget(struct VnodeDetails *vdp)
1506 {
1507 #define MAX_SYMLINK_LEN 1023
1508     static char buffer[MAX_SYMLINK_LEN + 1];
1509     int code;
1510     Volume *vp = vdp->vp;
1511     VnodeDiskObject *vnode = vdp->vnode;
1512     VnodeId vnodeNumber = vdp->vnodeNumber;
1513     IHandle_t *ihP = NULL;
1514     FdHandle_t *fdP = NULL;
1515     afs_fsize_t fileLength;
1516     int readLength;
1517     Inode ino;
1518
1519     ino = VNDISK_GET_INO(vnode);
1520     VNDISK_GET_LEN(fileLength, vnode);
1521
1522     if (fileLength > MAX_SYMLINK_LEN) {
1523         fprintf(stderr,
1524                 "%s: Symlink contents for fid (%lu.%lu.%lu.%lu) exceeds "
1525                 "%u, file length is %llu)!\n", progname,
1526                 afs_printable_uint32_lu(V_id(vp)),
1527                 afs_printable_uint32_lu(vnodeNumber),
1528                 afs_printable_uint32_lu(vnode->uniquifier),
1529                 afs_printable_uint32_lu(vnode->dataVersion),
1530                 MAX_SYMLINK_LEN,
1531                 fileLength);
1532         return -1;
1533     }
1534     if (fileLength == 0) {
1535         fprintf(stderr,
1536                 "%s: Symlink contents for fid (%lu.%lu.%lu.%lu) is empty.\n",
1537                 progname,
1538                 afs_printable_uint32_lu(V_id(vp)),
1539                 afs_printable_uint32_lu(vnodeNumber),
1540                 afs_printable_uint32_lu(vnode->uniquifier),
1541                 afs_printable_uint32_lu(vnode->dataVersion));
1542         return -1;
1543     }
1544
1545     IH_INIT(ihP, V_device(vp), V_parentId(vp), ino);
1546     fdP = IH_OPEN(ihP);
1547     if (fdP == NULL) {
1548         code = -1;
1549         goto cleanup;
1550     }
1551     if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
1552         code = -1;
1553         goto cleanup;
1554     }
1555     readLength = FDH_READ(fdP, buffer, fileLength);
1556     if (readLength < 0) {
1557         fprintf(stderr,
1558                 "%s: Error reading symlink contents for fid (%lu.%lu.%lu.%lu); "
1559                 "errno %d\n",
1560                 progname,
1561                 afs_printable_uint32_lu(V_id(vp)),
1562                 afs_printable_uint32_lu(vnodeNumber),
1563                 afs_printable_uint32_lu(vnode->uniquifier),
1564                 afs_printable_uint32_lu(vnode->dataVersion), errno);
1565         code = -1;
1566         goto cleanup;
1567     } else if (readLength != fileLength) {
1568         fprintf(stderr,
1569                 "%s: Symlink contents for fid (%lu.%lu.%lu.%lu) don't match "
1570                 "vnode file length metadata (len=%llu, actual=%lld)!\n",
1571                 progname,
1572                 afs_printable_uint32_lu(V_id(vp)),
1573                 afs_printable_uint32_lu(vnodeNumber),
1574                 afs_printable_uint32_lu(vnode->uniquifier),
1575                 afs_printable_uint32_lu(vnode->dataVersion), fileLength,
1576                 (long long)readLength);
1577         code = -1;
1578         goto cleanup;
1579     }
1580     code = 0;
1581
1582     if (readLength > 1 && (buffer[0] == '#' || buffer[0] == '%')
1583         && buffer[readLength - 1] == '.') {
1584         char *sep;
1585         buffer[readLength - 1] = '\0';  /* stringify; clobbers trailing dot */
1586         sep = strchr(buffer, ':');
1587         vdp->t = VNODE_U_MOUNT;
1588         vdp->u.mnt.type = buffer[0];
1589         if (!sep) {
1590             vdp->u.mnt.cell = NULL;
1591             vdp->u.mnt.vol = buffer + 1;
1592         } else {
1593             *sep = '\0';
1594             vdp->u.mnt.cell = buffer + 1;
1595             vdp->u.mnt.vol = sep + 1;
1596         }
1597     } else {
1598         buffer[readLength] = '\0';
1599         vdp->t = VNODE_U_SYMLINK;
1600         vdp->u.target = buffer;
1601     }
1602
1603   cleanup:
1604     if (fdP) {
1605         FDH_CLOSE(fdP);
1606     }
1607     if (ihP) {
1608         IH_RELEASE(ihP);
1609     }
1610     return code;
1611 }
1612
1613 /**
1614  * Print vnode details line
1615  *
1616  * @param[inout]  vdp   vnode details object
1617  *
1618  * @return none
1619  */
1620 void
1621 volinfo_PrintVnodeDetails(struct VolInfoOpt *opt, struct VnodeDetails *vdp)
1622 {
1623     switch (vdp->vnode->type) {
1624     case vNull:
1625         break;
1626     case vFile:
1627         if (opt->findVnType & FIND_FILE) {
1628             PrintColumns(opt, vdp, "file");
1629         }
1630         break;
1631     case vDirectory:
1632         if (opt->findVnType & FIND_DIR) {
1633             PrintColumns(opt, vdp, "dir");
1634         }
1635         break;
1636     case vSymlink:
1637         if (opt->findVnType & (FIND_MOUNT | FIND_SYMLINK)) {
1638             ReadSymlinkTarget(vdp);
1639             if ((opt->findVnType & FIND_MOUNT) && (vdp->t == VNODE_U_MOUNT)) {
1640                 PrintColumns(opt, vdp, "mount");
1641             }
1642             if ((opt->findVnType & FIND_SYMLINK) && (vdp->t == VNODE_U_SYMLINK)) {
1643                 PrintColumns(opt, vdp, "symlink");
1644             }
1645         }
1646         break;
1647     default:
1648         fprintf(stderr,
1649                 "%s: Warning: unexpected vnode type %u on fid %lu.%lu.%lu",
1650                 progname, vdp->vnode->type,
1651                 afs_printable_uint32_lu(V_id(vdp->vp)),
1652                 afs_printable_uint32_lu(vdp->vnodeNumber),
1653                 afs_printable_uint32_lu(vdp->vnode->uniquifier));
1654     }
1655 }
1656
1657 /**
1658  * Print each access entry of a vnode
1659  *
1660  * @param[in]  vdp   vnode details object
1661  *
1662  * @return none
1663  */
1664 void
1665 volinfo_ScanAcl(struct VolInfoOpt *opt, struct VnodeDetails *vdp)
1666 {
1667     int i;
1668     struct acl_accessList *acl;
1669     VnodeDiskObject *vnode = vdp->vnode;
1670
1671     if (vnode->type == vNull) {
1672         return;
1673     }
1674
1675     acl = VVnodeDiskACL(vnode);
1676     for (i = 0; i < acl->positive; i++) {
1677         vdp->t = VNODE_U_POS_ACCESS;
1678         vdp->u.access = &(acl->entries[i]);
1679         PrintColumns(opt, vdp, "acl");
1680     }
1681     for (i = (acl->total - 1); i >= (acl->total - acl->negative); i--) {
1682         vdp->t = VNODE_U_NEG_ACCESS;
1683         vdp->u.access = &(acl->entries[i]);
1684         PrintColumns(opt, vdp, "acl");
1685     }
1686 }
1687
1688 /**
1689  * Determine if the mode matches all the given masks.
1690  *
1691  * Returns true if the mode bits match all the given masks. A mask matches if at
1692  * least one bit in the mask is present in the mode bits.  An empty mode mask
1693  * list matches all modes (even if all the mode bits are zero.)
1694  *
1695  * param[in]  modeBits  unix mode bits of a vnode
1696  *
1697  */
1698 static int
1699 ModeMaskMatch(struct VolInfoOpt *opt, unsigned int modeBits)
1700 {
1701     int i;
1702
1703     for (i = 0; i < sizeof(opt->modeMask) / sizeof(*opt->modeMask) && opt->modeMask[i]; i++) {
1704         if ((opt->modeMask[i] & modeBits) == 0) {
1705             return 0;           /* at least one mode bit is not present */
1706         }
1707     }
1708     return 1;
1709 }
1710
1711 /**
1712  * Scan a volume index and handle each vnode
1713  *
1714  * @param[in] vp      volume object
1715  * @param[in] class   which index to scan
1716  *
1717  * @return none
1718  */
1719 static void
1720 HandleVnodes(struct VolInfoOpt *opt, Volume * vp, VnodeClass class)
1721 {
1722     afs_int32 diskSize =
1723         (class == vSmall ? SIZEOF_SMALLDISKVNODE : SIZEOF_LARGEDISKVNODE);
1724     char buf[SIZEOF_LARGEDISKVNODE];
1725     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
1726     StreamHandle_t *file = NULL;
1727     int vnodeIndex;
1728     afs_sfsize_t nVnodes;
1729     afs_foff_t offset = 0;
1730     IHandle_t *ih = vp->vnodeIndex[class].handle;
1731     FdHandle_t *fdP = NULL;
1732     afs_sfsize_t size;
1733     char *ctime, *atime, *mtime;
1734     struct opr_queue *scanList = &VnodeScanLists[class];
1735     struct opr_queue *cursor;
1736
1737     if (opr_queue_IsEmpty(scanList)) {
1738         return;
1739     }
1740
1741     for (opr_queue_Scan(scanList, cursor)) {
1742         struct VnodeScanProc *entry = (struct VnodeScanProc *)cursor;
1743         if (entry->heading) {
1744             printf("%s", entry->heading);
1745         }
1746     }
1747
1748     fdP = IH_OPEN(ih);
1749     if (fdP == NULL) {
1750         fprintf(stderr, "%s: open failed: ", progname);
1751         goto error;
1752     }
1753
1754     file = FDH_FDOPEN(fdP, "r");
1755     if (!file) {
1756         fprintf(stderr, "%s: fdopen failed\n", progname);
1757         goto error;
1758     }
1759
1760     if (GetFileInfo(fdP->fd_fd, &size, &ctime, &atime, &mtime) != 0) {
1761         goto error;
1762     }
1763     if (opt->dumpInodeTimes) {
1764         printf("ichanged : %s\nimodified: %s\niaccessed: %s\n\n", ctime,
1765                mtime, atime);
1766     }
1767
1768     nVnodes = (size / diskSize) - 1;
1769     if (nVnodes > 0) {
1770         STREAM_ASEEK(file, diskSize);
1771     } else
1772         nVnodes = 0;
1773
1774     for (vnodeIndex = 0;
1775          nVnodes && STREAM_READ(vnode, diskSize, 1, file) == 1;
1776          nVnodes--, vnodeIndex++, offset += diskSize) {
1777
1778         struct VnodeDetails vnodeDetails;
1779
1780         if (!ModeMaskMatch(opt, vnode->modeBits)) {
1781             continue;
1782         }
1783
1784         memset(&vnodeDetails, 0, sizeof(struct VnodeDetails));
1785         vnodeDetails.vp = vp;
1786         vnodeDetails.class = class;
1787         vnodeDetails.vnode = vnode;
1788         vnodeDetails.vnodeNumber = bitNumberToVnodeNumber(vnodeIndex, class);
1789         vnodeDetails.offset = offset;
1790         vnodeDetails.index = vnodeIndex;
1791
1792         for (opr_queue_Scan(scanList, cursor)) {
1793             struct VnodeScanProc *entry = (struct VnodeScanProc *)cursor;
1794             if (entry->proc) {
1795                 (*entry->proc) (opt, &vnodeDetails);
1796             }
1797         }
1798     }
1799
1800   error:
1801     if (file) {
1802         STREAM_CLOSE(file);
1803     }
1804     if (fdP) {
1805         FDH_CLOSE(fdP);
1806     }
1807 }
1808
1809 /**
1810  * Print vnode information
1811  *
1812  * @param[in] vdp          vnode details object
1813  *
1814  * @return none
1815  */
1816 void
1817 volinfo_PrintVnode(struct VolInfoOpt *opt, struct VnodeDetails *vdp)
1818 {
1819 #if defined(AFS_NAMEI_ENV)
1820     IHandle_t *ihtmpp;
1821     namei_t filename;
1822 #endif
1823     afs_foff_t offset = vdp->offset;
1824     VnodeDiskObject *vnode = vdp->vnode;
1825     afs_fsize_t fileLength;
1826     Inode ino;
1827
1828     ino = VNDISK_GET_INO(vnode);
1829     VNDISK_GET_LEN(fileLength, vnode);
1830
1831     /* The check for orphaned vnodes is currently limited to non-empty
1832      * vnodes with a parent of zero (and which are not the first entry
1833      * in the index). */
1834     if (opt->showOrphaned && (fileLength == 0 || vnode->parent || !offset))
1835         return;
1836
1837     printf
1838         ("%10lld Vnode %u.%u.%u cloned: %u, length: %llu linkCount: %d parent: %u",
1839          (long long)offset, vdp->vnodeNumber, vnode->uniquifier,
1840          vnode->dataVersion, vnode->cloned, (afs_uintmax_t) fileLength,
1841          vnode->linkCount, vnode->parent);
1842     if (opt->dumpInodeNumber)
1843         printf(" inode: %s", PrintInode(NULL, ino));
1844     if (opt->dumpDate)
1845         printf(" ServerModTime: %s", date(vnode->serverModifyTime));
1846 #if defined(AFS_NAMEI_ENV)
1847     if (opt->dumpFileNames) {
1848         IH_INIT(ihtmpp, V_device(vdp->vp), V_parentId(vdp->vp), ino);
1849         namei_HandleToName(&filename, ihtmpp);
1850 #if !defined(AFS_NT40_ENV)
1851         printf(" UFS-Filename: %s", filename.n_path);
1852 #else
1853         printf(" NTFS-Filename: %s", filename.n_path);
1854 #endif
1855     }
1856 #endif
1857     printf("\n");
1858 }
1859
1860 /**
1861  * Print the volume partition id
1862  *
1863  * @param[in]  vp  volume object
1864  *
1865  * @return none
1866  */
1867 static void
1868 PrintPartitionId(Volume * vp)
1869 {
1870     char *partition = VPartitionPath(V_partition(vp));
1871
1872     if (!strncmp(partition, "/vicep", 6)) {
1873         printf("%s", partition + 6);
1874     } else if (!strncmp(partition, "vicep", 5)) {
1875         printf("%s", partition + 5);
1876     } else {
1877         fprintf(stderr, "Invalid partition for volume id %lu\n",
1878                 afs_printable_uint32_lu(V_id(vp)));
1879         printf("%s", PLACEHOLDER);
1880     }
1881 }
1882
1883 /**
1884  * Print the vnode type description string
1885  *
1886  * @param[in]  type  vnode type
1887  *
1888  * @return none
1889  */
1890 static void
1891 volinfo_PrintVnodeType(int type)
1892 {
1893     switch (type) {
1894     case vNull:
1895         printf("null");
1896         break;
1897     case vFile:
1898         printf("file");
1899         break;
1900     case vDirectory:
1901         printf("dir");
1902         break;
1903     case vSymlink:
1904         printf("symlink");
1905         break;
1906     default:
1907         printf("unknown");
1908     }
1909 }
1910
1911 /**
1912  * Print right bits as string.
1913  *
1914  * param[in] rights  rights bitmap
1915  */
1916 static void
1917 PrintRights(int rights)
1918 {
1919     if (rights & PRSFS_READ) {
1920         printf("r");
1921     }
1922     if (rights & PRSFS_LOOKUP) {
1923         printf("l");
1924     }
1925     if (rights & PRSFS_INSERT) {
1926         printf("i");
1927     }
1928     if (rights & PRSFS_DELETE) {
1929         printf("d");
1930     }
1931     if (rights & PRSFS_WRITE) {
1932         printf("w");
1933     }
1934     if (rights & PRSFS_LOCK) {
1935         printf("k");
1936     }
1937     if (rights & PRSFS_ADMINISTER) {
1938         printf("a");
1939     }
1940     if (rights & PRSFS_USR0) {
1941         printf("A");
1942     }
1943     if (rights & PRSFS_USR1) {
1944         printf("B");
1945     }
1946     if (rights & PRSFS_USR2) {
1947         printf("C");
1948     }
1949     if (rights & PRSFS_USR3) {
1950         printf("D");
1951     }
1952     if (rights & PRSFS_USR4) {
1953         printf("E");
1954     }
1955     if (rights & PRSFS_USR5) {
1956         printf("F");
1957     }
1958     if (rights & PRSFS_USR6) {
1959         printf("G");
1960     }
1961     if (rights & PRSFS_USR7) {
1962         printf("H");
1963     }
1964 }
1965
1966 /**
1967  * Print the path to the namei file.
1968  */
1969 static void
1970 PrintNamei(Volume * vp, VnodeDiskObject * vnode)
1971 {
1972 #ifdef AFS_NAMEI_ENV
1973     namei_t name;
1974     IHandle_t *ihP = NULL;
1975     Inode ino;
1976     ino = VNDISK_GET_INO(vnode);
1977     IH_INIT(ihP, V_device(vp), V_parentId(vp), ino);
1978     namei_HandleToName(&name, ihP);
1979     printf("%s", name.n_path);
1980     IH_RELEASE(ihP);
1981 #else
1982     printf("%s", PLACEHOLDER);
1983 #endif
1984 }
1985
1986 /**
1987  * Print the column heading line.
1988  */
1989 static void
1990 PrintColumnHeading(struct VolInfoOpt *opt)
1991 {
1992     int i;
1993     const char *name;
1994
1995     for (i = 0; i < NumOutputColumns; i++) {
1996         if (i > 0) {
1997             printf("%s", opt->columnDelim);
1998         }
1999         name = ColumnName[OutputColumn[i]].name;
2000         while (*name) {
2001             putchar(toupper(*name++));
2002         }
2003     }
2004     printf("\n");
2005 }
2006
2007 /**
2008  * Print output columns for the vnode/acess entry.
2009  *
2010  * @param[in]  vdp   vnode details object
2011  * @param[in]  desc  type of line to be printed
2012  *
2013  * @return none
2014  */
2015 static void
2016 PrintColumns(struct VolInfoOpt *opt, struct VnodeDetails *vdp, const char *desc)
2017 {
2018     int i;
2019     afs_fsize_t length;
2020
2021     for (i = 0; i < NumOutputColumns; i++) {
2022         if (i > 0) {
2023             printf("%s", opt->columnDelim);
2024         }
2025         switch (OutputColumn[i]) {
2026         case col_host:
2027             printf("%s", opt->hostname);
2028             break;
2029         case col_desc:
2030             printf("%s", desc);
2031             break;
2032         case col_vid:
2033             printf("%lu", afs_printable_uint32_lu(V_id(vdp->vp)));
2034             break;
2035         case col_offset:
2036             printf("%llu", vdp->offset);
2037             break;
2038         case col_vtype:
2039             printf("%s", volumeTypeShortString(V_type(vdp->vp)));
2040             break;
2041         case col_vname:
2042             printf("%s", V_name(vdp->vp));
2043             break;
2044         case col_part:
2045             printf("%s", VPartitionPath(V_partition(vdp->vp)));
2046             break;
2047         case col_partid:
2048             PrintPartitionId(vdp->vp);
2049             break;
2050         case col_fid:
2051             printf("%lu.%lu.%lu",
2052                    afs_printable_uint32_lu(V_id(vdp->vp)),
2053                    afs_printable_uint32_lu(vdp->vnodeNumber),
2054                    afs_printable_uint32_lu(vdp->vnode->uniquifier));
2055             break;
2056         case col_path:
2057             if (!vdp->path) {
2058                 LookupPath(opt, vdp);
2059             }
2060             printf("%s", vdp->path ? vdp->path : PLACEHOLDER);
2061             break;
2062         case col_target:
2063             printf("%s",
2064                    (vdp->t == VNODE_U_SYMLINK ? vdp->u.target : PLACEHOLDER));
2065             break;
2066         case col_mount:
2067             if (vdp->t != VNODE_U_MOUNT) {
2068                 printf("%s", PLACEHOLDER);
2069             } else {
2070                 printf("%c", vdp->u.mnt.type);
2071                 if (vdp->u.mnt.cell) {
2072                     printf("%s:", vdp->u.mnt.cell);
2073                 }
2074                 printf("%s.", vdp->u.mnt.vol);
2075             }
2076             break;
2077         case col_mtype:
2078             printf("%c", (vdp->t == VNODE_U_MOUNT ? vdp->u.mnt.type : '-'));
2079             break;
2080         case col_mcell:
2081             printf("%s",
2082                    (vdp->t == VNODE_U_MOUNT && vdp->u.mnt.cell ? vdp->u.mnt.cell : PLACEHOLDER));
2083             break;
2084         case col_mvol:
2085             printf("%s",
2086                    (vdp->t == VNODE_U_MOUNT ? vdp->u.mnt.vol : PLACEHOLDER));
2087             break;
2088         case col_aid:
2089             if (vdp->t == VNODE_U_POS_ACCESS || vdp->t == VNODE_U_NEG_ACCESS) {
2090                 printf("%d", vdp->u.access->id);
2091             } else {
2092                 printf("%s", PLACEHOLDER);
2093             }
2094             break;
2095         case col_arights:
2096             if (vdp->t == VNODE_U_POS_ACCESS) {
2097                 printf("+");
2098                 PrintRights(vdp->u.access->rights);
2099             } else if (vdp->t == VNODE_U_NEG_ACCESS) {
2100                 printf("-");
2101                 PrintRights(vdp->u.access->rights);
2102             }
2103             break;
2104         case col_vntype:
2105             volinfo_PrintVnodeType(vdp->vnode->type);
2106             break;
2107         case col_cloned:
2108             printf("%c", vdp->vnode->cloned ? 'y' : 'n');
2109             break;
2110         case col_mode:
2111             printf("0%o", vdp->vnode->modeBits);
2112             break;
2113         case col_links:
2114             printf("%lu", afs_printable_uint32_lu(vdp->vnode->linkCount));
2115             break;
2116         case col_length:
2117             VNDISK_GET_LEN(length, vdp->vnode);
2118             printf("%llu", length);
2119             break;
2120         case col_uniq:
2121             printf("%lu", afs_printable_uint32_lu(vdp->vnode->uniquifier));
2122             break;
2123         case col_dv:
2124             printf("%lu", afs_printable_uint32_lu(vdp->vnode->dataVersion));
2125             break;
2126         case col_inode:
2127             printf("%" AFS_UINT64_FMT, VNDISK_GET_INO(vdp->vnode));
2128             break;
2129         case col_namei:
2130             PrintNamei(vdp->vp, vdp->vnode);
2131             break;
2132         case col_modtime:
2133             printf("%lu",
2134                    afs_printable_uint32_lu(vdp->vnode->unixModifyTime));
2135             break;
2136         case col_author:
2137             printf("%lu", afs_printable_uint32_lu(vdp->vnode->author));
2138             break;
2139         case col_owner:
2140             printf("%lu", afs_printable_uint32_lu(vdp->vnode->owner));
2141             break;
2142         case col_parent:
2143             printf("%lu", afs_printable_uint32_lu(vdp->vnode->parent));
2144             break;
2145         case col_magic:
2146             printf("0x%08X", vdp->vnode->vnodeMagic);
2147             break;
2148         case col_lock:
2149             printf("%lu.%lu",
2150                    afs_printable_uint32_lu(vdp->vnode->lock.lockCount),
2151                    afs_printable_uint32_lu(vdp->vnode->lock.lockTime));
2152             break;
2153         case col_smodtime:
2154             printf("%lu",
2155                    afs_printable_uint32_lu(vdp->vnode->serverModifyTime));
2156             break;
2157         case col_group:
2158             printf("%lu", afs_printable_uint32_lu(vdp->vnode->group));
2159             break;
2160         default:
2161             fprintf(stderr, "%s: Unknown column type: %d (%d)\n", progname,
2162                     OutputColumn[i], i);
2163             break;
2164         }
2165     }
2166     printf("\n");
2167 }