2 * Copyright 2000, International Business Machines Corporation and others.
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
10 /* I/O operations for the Windows NT platforms. */
12 #include <afsconfig.h>
13 #include <afs/param.h>
31 #include <afs/afsutil.h>
33 #include <afs/afsint.h>
37 #include "viceinode.h"
39 #include <afs/assert.h>
40 #include <afs/errmap_nt.h>
42 #define BASEFILEATTRIBUTE FILE_ATTRIBUTE_NORMAL
44 /* nt_unlink - unlink a case sensitive name.
46 * nt_unlink supports the nt_dec call.
48 * This nt_unlink has the delete on last close semantics of the Unix unlink
49 * with a minor twist. Subsequent CreateFile calls on this file can succeed
50 * if they open for delete. It's also unclear what happens if a CreateFile
51 * call tries to create a new file with the same name. Fortunately, neither
52 * case should occur as part of nt_dec.
59 fh = CreateFile(name, GENERIC_READ | GENERIC_WRITE,
60 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
62 BASEFILEATTRIBUTE | FILE_FLAG_DELETE_ON_CLOSE |
63 FILE_FLAG_POSIX_SEMANTICS, NULL);
64 if (fh != INVALID_HANDLE_VALUE)
67 errno = nterr_nt2unix(GetLastError(), ENOENT);
73 /* nt_open - open an NT handle for a file.
76 * the handle or -1 on error.
79 nt_open(char *name, int flags, int mode)
83 DWORD nt_share = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
85 /* Really use the sequential one for data files, random for meta data. */
86 DWORD FandA = BASEFILEATTRIBUTE | FILE_FLAG_SEQUENTIAL_SCAN;
89 if ((flags & O_RDWR) || (flags & O_WRONLY))
90 nt_access |= GENERIC_WRITE;
91 if ((flags & O_RDWR) || (flags == O_RDONLY))
92 nt_access |= GENERIC_READ;
95 switch (flags & (O_CREAT | O_EXCL | O_TRUNC)) {
97 nt_create = OPEN_EXISTING;
100 nt_create = OPEN_ALWAYS;
102 case O_CREAT | O_TRUNC:
103 nt_create = CREATE_ALWAYS;
105 case O_CREAT | O_EXCL:
106 case O_CREAT | O_EXCL | O_TRUNC:
107 nt_create = CREATE_NEW;
110 nt_create = TRUNCATE_EXISTING;
112 case O_TRUNC | O_EXCL:
120 fh = CreateFile(name, nt_access, nt_share, NULL, nt_create, FandA, NULL);
122 if (fh == INVALID_HANDLE_VALUE) {
124 errno = nterr_nt2unix(GetLastError(), EBADF);
134 code = CloseHandle(fd);
136 errno = nterr_nt2unix(GetLastError(), EBADF);
143 nt_write(FD_t fd, char *buf, size_t size)
148 code = WriteFile((HANDLE) fd, (void *)buf, (DWORD) size, &nbytes, NULL);
151 errno = nterr_nt2unix(GetLastError(), EBADF);
158 nt_read(FD_t fd, char *buf, size_t size)
163 code = ReadFile((HANDLE) fd, (void *)buf, (DWORD) size, &nbytes, NULL);
166 errno = nterr_nt2unix(GetLastError(), EBADF);
173 nt_iread(IHandle_t * h, int offset, char *buf, int size)
182 if (FDH_SEEK(fdP, offset, SEEK_SET) < 0) {
183 FDH_REALLYCLOSE(fdP);
187 nBytes = FDH_READ(fdP, buf, size);
193 nt_iwrite(IHandle_t * h, int offset, char *buf, int size)
202 if (FDH_SEEK(fdP, offset, SEEK_SET) < 0) {
203 FDH_REALLYCLOSE(fdP);
206 nBytes = FDH_WRITE(fdP, buf, size);
215 BY_HANDLE_FILE_INFORMATION finfo;
217 if (!GetFileInformationByHandle(fd, &finfo))
220 return finfo.nFileSizeLow;
225 nt_getFileCreationTime(FD_t fd, FILETIME * ftime)
227 BY_HANDLE_FILE_INFORMATION finfo;
229 if (!GetFileInformationByHandle(fd, &finfo))
232 *ftime = finfo.ftCreationTime;
238 nt_setFileCreationTime(FD_t fd, FILETIME * ftime)
240 return !SetFileTime(fd, ftime, NULL, NULL);
252 cdrive = 'A' + (n - 1);
255 cdrive = _toupper(cdrive);
257 (void)sprintf(sdrive, "\\\\.\\%c:", cdrive);
258 drive_fd = nt_open(sdrive, O_RDWR, 0666);
259 if (drive_fd == INVALID_FD) {
263 if (!FlushFileBuffers((HANDLE) drive_fd)) {
264 errno = nterr_nt2unix(GetLastError(), EBADF);
273 /* Currently nt_ftruncate only tested to shrink a file. */
275 nt_ftruncate(FD_t fd, int len)
277 if (SetFilePointer(fd, (LONG) len, NULL, FILE_BEGIN)
279 errno = nterr_nt2unix(GetLastError(), EBADF);
282 if (!SetEndOfFile(fd)) {
283 errno = nterr_nt2unix(GetLastError(), EBADF);
293 int code = FlushFileBuffers(fd);
294 return code == 0 ? -1 : 0;
299 nt_seek(FD_t fd, int off, int where)
301 int code = SetFilePointer(fd, off, NULL, where);
306 /* Inode number format:
307 * low 32 bits - if a regular file or directory, the vnode. Else the type.
308 * 32-36 - unquifier tag and index into counts array for this vnode. Only
309 * two of the available bits are currently used. The rest are
310 * present in case we ever increase the number of types of volumes
311 * in the volume grou.
312 * bit 37 : 1 == special, 0 == regular
314 #define NT_VNODEMASK 0x00ffffffff
315 /* While the TAGMASK is 7, note that we are leaving 1 more bit available. */
316 #define NT_TAGMASK 0x7
317 #define NT_TAGSHIFT 32
318 #define NT_INODESPECIAL 0x2000000000
320 #define NT_MAXVOLS 5 /* Maximum supported number of volumes per volume
321 * group, not counting temporary (move) volumes.
322 * This is the number of separate files, all having
323 * the same vnode number, which can occur in a volume
328 int nt_SetLinkCount(FdHandle_t * h, Inode ino, int count, int locked);
332 * converts a device number (2-25) into a drive letter name.
335 * drive - assumes drive is a pointer to a string at least 3 bytes long.
336 * dev - drive number 2-25, since A-C already in use.
339 * Returns pointer to end of drive if successful, else NULL.
343 nt_DevToDrive(char *drive, int dev)
345 if (dev < 2 || dev > 25) {
347 return NULL; /* Invalid drive */
349 drive[0] = (char)('A' + dev);
357 /* Returns pointer to end of name if successful, else NULL. */
359 nt_HandleToVolDir(char *name, IHandle_t * h)
363 if (!(name = nt_DevToDrive(name, h->ih_dev)))
366 (void)memcpy(name, "\\Vol_", 5);
368 (void)strcpy(name, int_to_base32(str1, h->ih_vid));
369 name += strlen(name);
370 memcpy(name, ".data", 5);
379 * Constructs a file name for the fully qualified handle.
382 nt_HandleToName(char *name, IHandle_t * h)
385 int tag = (int)((h->ih_ino >> NT_TAGSHIFT) & NT_TAGMASK);
386 int vno = (int)(h->ih_ino & NT_VNODEMASK);
388 if (!(name = nt_HandleToVolDir(name, h)))
392 if (h->ih_ino & NT_INODESPECIAL)
398 str1[1] = ((vno & 0x1f) >> 1) + 'A';
401 memcpy(name, str1, 2);
403 (void)memcpy(name, "\\V_", 3);
405 (void)strcpy(name, int_to_base32(str1, vno));
406 name += strlen(name);
408 (void)strcpy(name, int_to_base32(str1, tag));
409 name += strlen(name);
415 /* nt_CreateDataDirectories
417 * The data for each volume is in a separate directory. The name of the
418 * volume is of the form: Vol_NNNNNN.data, where NNNNNN is a base 32
419 * representation of the RW volume ID (even where the RO is the only volume
420 * on the partition). Below that are separate subdirectories for the
421 * AFS directories and special files. There are also 16 directories for files,
422 * hashed on the low 5 bits (recall bit0 is always 0) of the vnode number.
423 * These directories are named:
424 * A - P - 16 file directories.
425 * Q ----- data directory
426 * R ----- special files directory
429 nt_CreateDataDirectories(IHandle_t * h, int *created)
435 if (!(s = nt_HandleToVolDir(name, h)))
438 if (mkdir(name) < 0) {
446 for (i = 'A'; i <= 'R'; i++) {
448 if (mkdir(name) < 0 && errno != EEXIST)
454 /* nt_RemoveDataDirectories
456 * Returns -1 on error. Typically, callers ignore this error bcause we
457 * can continue running if the removes fail. The salvage process will
458 * finish tidying up for us.
461 nt_RemoveDataDirectories(IHandle_t * h)
467 if (!(s = nt_HandleToVolDir(name, h)))
472 for (i = 'A'; i <= 'R'; i++) {
474 if (rmdir(name) < 0 && errno != ENOENT)
478 /* Delete the Vol_NNNNNN.data directory. */
481 if (rmdir(name) < 0 && errno != ENOENT) {
489 /* Create the file in the name space.
491 * Parameters stored as follows:
493 * p1 - volid - implied in containing directory.
494 * p2 - vnode - name is <vnode>.<tag> where tag is a file name unqiquifier.
495 * p3 - uniq -- creation time - dwHighDateTime
496 * p4 - dv ---- creation time - dwLowDateTime
498 * p1 - volid - creation time - dwHighDateTime
499 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
500 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
501 * p4 - parid - parent volume id - implied in containing directory.
503 * Return value is the inode number or (Inode)-1 if error.
504 * We "know" there is only one link table, so return EEXIST if there already
505 * is a link table. It's up to the calling code to test errno and increment
511 * This function is called by VCreateVolume to hide the implementation
512 * details of the inode numbers.
515 nt_MakeSpecIno(int type)
517 return ((Inode) type | (Inode) NT_INODESPECIAL);
521 nt_icreate(IHandle_t * h, char *part, int p1, int p2, int p3, int p4)
536 memset((void *)&tmp, 0, sizeof(IHandle_t));
539 tmp.ih_dev = tolower(*part) - 'a';
542 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
544 if (nt_CreateDataDirectories(&tmp, &created_dir) < 0)
547 tmp.ih_ino = nt_MakeSpecIno(p3);
548 ftime.dwHighDateTime = p1;
549 ftime.dwLowDateTime = p2;
551 /* Regular file or directory.
552 * Encoding: p1 -> dir, p2 -> name, p3,p4 -> Create time
554 tmp.ih_ino = (Inode) p2;
557 ftime.dwHighDateTime = p3;
558 ftime.dwLowDateTime = p4;
561 /* Now create file. */
562 if ((code = nt_HandleToName(filename, &tmp)) < 0)
565 p = filename + strlen(filename);
567 for (i = 0; i < NT_MAXVOLS; i++) {
568 *p = *int_to_base32(str1, i);
569 fd = nt_open(filename, O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0666);
570 if (fd != INVALID_FD)
572 if (p2 == -1 && p3 == VI_LINKTABLE)
575 if (fd == INVALID_FD) {
580 tmp.ih_ino &= ~((Inode) NT_TAGMASK << NT_TAGSHIFT);
581 tmp.ih_ino |= ((Inode) i << NT_TAGSHIFT);
584 if (!SetFileTime((HANDLE) fd, &ftime, NULL, NULL)) {
592 if (fd == INVALID_FD) {
594 code = nt_unlink(filename);
605 code = nt_SetLinkCount(fdP, tmp.ih_ino, 1, 0);
607 } else if (p2 == -1 && p3 == VI_LINKTABLE) {
608 if (fd == INVALID_FD)
610 /* hack at tmp to setup for set link count call. */
612 code = nt_SetLinkCount(&tfd, (Inode) 0, 1, 0);
617 if (fd != INVALID_FD)
620 if (code && created_dir) {
622 nt_RemoveDataDirectories(&tmp);
625 return code ? (Inode) - 1 : tmp.ih_ino;
630 nt_iopen(IHandle_t * h)
635 /* Convert handle to file name. */
636 if (nt_HandleToName(name, h) < 0)
639 fd = nt_open(name, O_RDWR, 0666);
643 /* Need to detect vol special file and just unlink. In those cases, the
644 * handle passed in _is_ for the inode. We only check p1 for the special
648 nt_dec(IHandle_t * h, Inode ino, int p1)
655 if (ino & NT_INODESPECIAL) {
658 WIN32_FIND_DATA info;
661 /* Verify this is the right file. */
662 IH_INIT(tmp, h->ih_dev, h->ih_vid, ino);
664 if (nt_HandleToName(name, tmp) < 0) {
671 FindFirstFileEx(name, FindExInfoStandard, &info,
672 FindExSearchNameMatch, NULL,
673 FIND_FIRST_EX_CASE_SENSITIVE);
677 return -1; /* Can't get info, leave alone */
681 if (info.ftCreationTime.dwHighDateTime != (unsigned int)p1) {
686 /* If it's the link table itself, decrement the link count. */
687 if ((ino & NT_VNODEMASK) == VI_LINKTABLE) {
694 if ((count = nt_GetLinkCount(fdP, (Inode) 0, 1)) < 0) {
695 FDH_REALLYCLOSE(fdP);
701 if (nt_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) < 0) {
702 FDH_REALLYCLOSE(fdP);
707 FDH_REALLYCLOSE(fdP);
714 if ((code = nt_unlink(name)) == 0) {
715 if ((ino & NT_VNODEMASK) == VI_LINKTABLE) {
716 /* Try to remove directory. If it fails, that's ok.
717 * Salvage will clean up.
719 (void)nt_RemoveDataDirectories(tmp);
725 /* Get a file descriptor handle for this Inode */
731 if ((count = nt_GetLinkCount(fdP, ino, 1)) < 0) {
732 FDH_REALLYCLOSE(fdP);
738 if (nt_SetLinkCount(fdP, ino, count, 1) < 0) {
739 FDH_REALLYCLOSE(fdP);
746 nt_HandleToName(name, &th);
747 code = nt_unlink(name);
756 nt_inc(IHandle_t * h, Inode ino, int p1)
762 if (ino & NT_INODESPECIAL) {
763 if ((ino & NT_VNODEMASK) != VI_LINKTABLE)
768 /* Get a file descriptor handle for this Inode */
774 if ((count = nt_GetLinkCount(fdP, ino, 1)) < 0)
783 if (nt_SetLinkCount(fdP, ino, count, 1) < 0)
787 FDH_REALLYCLOSE(fdP);
796 /************************************************************************
797 * Link Table Organization
798 ************************************************************************
800 * The link table volume special file is used to hold the link counts that
801 * are held in the inodes in inode based AFS vice filesystems. Since NTFS
802 * doesn't provide us that access, the link counts are being kept in a separate
803 * volume special file. The file begins with the usual version stamp
804 * information and is then followed by one row per vnode number. vnode 0
805 * is used to hold the link count of the link table itself. That is because
806 * the same link table is shared among all the volumes of the volume group
807 * and is deleted only when the last volume of a volume group is deleted.
809 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
810 * link count from 0 through 7. Each colume represents a unique instance of
811 * that vnode. Say we have a file shared between the RW and a RO and a
812 * different version of the file (or a different uniquifer) for the BU volume.
813 * Then one column would be holding the link count of 2 for the RW and RO
814 * and a different column would hold the link count of 1 for the BU volume.
815 * The column used is determined for NT by the uiquifier tag applied to
816 * generate a unique file name in the NTFS namespace. The file name is
817 * of the form "V_<vno>.<tag>" . And the <tag> is also the column number
820 #define LINKTABLE_WIDTH 2
821 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
824 nt_GetLCOffsetAndIndexFromIno(Inode ino, int *offset, int *index)
826 int toff = (int)(ino & NT_VNODEMASK);
827 int tindex = (int)((ino >> NT_TAGSHIFT) & NT_TAGMASK);
829 *offset = (toff << LINKTABLE_SHIFT) + 8; /* *2 + sizeof stamp */
830 *index = (tindex << 1) + tindex;
835 * If lockit is set, lock the file and leave it locked upon a successful
839 nt_GetLinkCount(FdHandle_t * h, Inode ino, int lockit)
841 unsigned short row = 0;
845 nt_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
848 if (!LockFile(h->fd_fd, offset, 0, 2, 0))
852 if (!SetFilePointer(h->fd_fd, (LONG) offset, NULL, FILE_BEGIN))
853 goto bad_getLinkByte;
855 if (!ReadFile(h->fd_fd, (void *)&row, 2, &junk, NULL)) {
856 goto bad_getLinkByte;
859 return (int)((row >> index) & NT_TAGMASK);
863 UnlockFile(h->fd_fd, offset, 0, 2, 0);
871 * If locked is set, assume file is locked. Otherwise, lock file before
872 * proceeding to modify it.
875 nt_SetLinkCount(FdHandle_t * h, Inode ino, int count, int locked)
882 nt_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
886 if (!LockFile(h->fd_fd, offset, 0, 2, 0)) {
887 errno = nterr_nt2unix(GetLastError(), EBADF);
891 if (!SetFilePointer(h->fd_fd, (LONG) offset, NULL, FILE_BEGIN)) {
892 errno = nterr_nt2unix(GetLastError(), EBADF);
893 goto bad_SetLinkCount;
897 if (!ReadFile(h->fd_fd, (void *)&row, 2, &junk, NULL)) {
898 errno = nterr_nt2unix(GetLastError(), EBADF);
899 goto bad_SetLinkCount;
906 row &= (unsigned short)~junk;
907 row |= (unsigned short)count;
909 if (!SetFilePointer(h->fd_fd, (LONG) offset, NULL, FILE_BEGIN)) {
910 errno = nterr_nt2unix(GetLastError(), EBADF);
911 goto bad_SetLinkCount;
914 if (!WriteFile(h->fd_fd, (void *)&row, 2, &junk, NULL)) {
915 errno = nterr_nt2unix(GetLastError(), EBADF);
916 goto bad_SetLinkCount;
923 UnlockFile(h->fd_fd, offset, 0, 2, 0);
929 /* ListViceInodes - write inode data to a results file. */
930 static int DecodeInodeName(char *name, int *p1, int *p2);
931 static int DecodeVolumeName(char *name, int *vid);
932 static int nt_ListAFSSubDirs(IHandle_t * dirIH,
933 int (*write_fun) (FILE *, struct ViceInodeInfo *,
934 char *, char *), FILE * fp,
935 int (*judgeFun) (struct ViceInodeInfo *,
937 int singleVolumeNumber);
942 * Write the inode data to the results file.
944 * Returns -2 on error, 0 on success.
946 * This is written as a callback simply so that other listing routines
947 * can use the same inode reading code.
950 WriteInodeInfo(FILE * fp, struct ViceInodeInfo *info, char *dir, char *name)
953 n = fwrite(info, sizeof(*info), 1, fp);
954 return (n == 1) ? 0 : -2;
959 * Fill the results file with the requested inode information.
963 * -1 - complete failure, salvage should terminate.
964 * -2 - not enough space on partition, salvager has error message for this.
966 * This code optimizes single volume salvages by just looking at that one
967 * volume's directory.
969 * If the resultFile is NULL, then don't call the write routine.
972 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
973 int (*judgeInode) (struct ViceInodeInfo * info, int vid),
974 int singleVolumeNumber, int *forcep, int forceR, char *wpath)
976 FILE *fp = (FILE *) - 1;
981 fp = fopen(resultFile, "w");
983 Log("Unable to create inode description file %s\n", resultFile);
988 nt_ListAFSFiles(wpath, WriteInodeInfo, fp, judgeInode,
999 if (fflush(fp) == EOF) {
1000 Log("Unable to successfully flush inode file for %s\n", mountedOn);
1004 if (fsync(fileno(fp)) == -1) {
1005 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1009 if (fclose(fp) == EOF) {
1010 Log("Unable to successfully close inode file for %s\n", mountedOn);
1015 * Paranoia: check that the file is really the right size
1017 if (stat(resultFile, &status) == -1) {
1018 Log("Unable to successfully stat inode file for %s\n", mountedOn);
1021 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
1022 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1023 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
1033 * Collect all the matching AFS files on the drive.
1034 * If singleVolumeNumber is non-zero, just return files for that volume.
1036 * Returns <0 on error, else number of files found to match.
1039 nt_ListAFSFiles(char *dev,
1040 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1042 int (*judgeFun) (struct ViceInodeInfo *, int),
1043 int singleVolumeNumber)
1046 char name[MAX_PATH];
1050 static void FreeZLCList(void);
1052 memset((void *)&h, 0, sizeof(IHandle_t));
1053 h.ih_dev = toupper(*dev) - 'A';
1055 if (singleVolumeNumber) {
1056 h.ih_vid = singleVolumeNumber;
1057 if (!nt_HandleToVolDir(name, &h))
1060 nt_ListAFSSubDirs(&h, writeFun, fp, judgeFun, singleVolumeNumber);
1064 /* Find all Vol_*.data directories and descend through them. */
1065 if (!nt_DevToDrive(name, h.ih_dev))
1068 dirp = opendir(name);
1071 while (dp = readdir(dirp)) {
1072 if (!DecodeVolumeName(dp->d_name, &h.ih_vid)) {
1073 ninodes += nt_ListAFSSubDirs(&h, writeFun, fp, judgeFun, 0);
1083 /* nt_ListAFSSubDirs
1085 * List the S, F, and D subdirectories of this volume's directory.
1089 * > = 0 - number of AFS files found.
1092 nt_ListAFSSubDirs(IHandle_t * dirIH,
1093 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1095 int (*judgeFun) (struct ViceInodeInfo *, int),
1096 int singleVolumeNumber)
1099 IHandle_t myIH = *dirIH;
1101 WIN32_FIND_DATA data;
1103 char basePath[1024];
1105 char findPath[1024];
1106 struct ViceInodeInfo info;
1108 FdHandle_t linkHandle;
1110 static void AddToZLCDeleteList(char dir, char *name);
1111 static void DeleteZLCFiles(char *path);
1113 s = nt_HandleToVolDir(path, &myIH);
1114 strcpy(basePath, path);
1121 /* Do the directory containing the special files first to pick up link
1124 for (i = 'R'; i >= 'A'; i--) {
1126 (void)strcpy(findPath, path);
1127 (void)strcat(findPath, "\\*");
1129 FindFirstFileEx(findPath, FindExInfoStandard, &data,
1130 FindExSearchNameMatch, NULL,
1131 FIND_FIRST_EX_CASE_SENSITIVE);
1132 if (dirH == INVALID_HANDLE_VALUE)
1135 /* Store the vice info. */
1136 memset((void *)&info, 0, sizeof(info));
1137 if (*data.cFileName == '.')
1139 if (DecodeInodeName(data.cFileName, &vno, &tag) < 0) {
1140 Log("Error parsing %s\\%s\n", path, data.cFileName);
1142 info.inodeNumber = (Inode) tag << NT_TAGSHIFT;
1143 info.inodeNumber |= (Inode) vno;
1144 info.byteCount = data.nFileSizeLow;
1146 if (i == 'R') { /* Special inode. */
1147 info.inodeNumber |= NT_INODESPECIAL;
1148 info.u.param[0] = data.ftCreationTime.dwHighDateTime;
1149 info.u.param[1] = data.ftCreationTime.dwLowDateTime;
1150 info.u.param[2] = vno;
1151 info.u.param[3] = dirIH->ih_vid;
1152 if (info.u.param[2] != VI_LINKTABLE) {
1155 /* Open this handle */
1157 (void)sprintf(lpath, "%s\\%s", path, data.cFileName);
1158 linkHandle.fd_fd = nt_open(lpath, O_RDONLY, 0666);
1160 nt_GetLinkCount(&linkHandle, (Inode) 0, 0);
1162 } else { /* regular file. */
1164 nt_GetLinkCount(&linkHandle, info.inodeNumber, 0);
1165 if (info.linkCount == 0) {
1167 Log("Found 0 link count file %s\\%s, deleting it.\n",
1168 path, data.cFileName);
1169 AddToZLCDeleteList((char)i, data.cFileName);
1171 Log("Found 0 link count file %s\\%s.\n", path,
1176 info.u.param[0] = dirIH->ih_vid;
1177 info.u.param[1] = vno;
1178 info.u.param[2] = data.ftCreationTime.dwHighDateTime;
1179 info.u.param[3] = data.ftCreationTime.dwLowDateTime;
1181 if (judgeFun && !(*judgeFun) (&info, singleVolumeNumber))
1183 if ((*writeFun) (fp, &info, path, data.cFileName) < 0) {
1184 nt_close(linkHandle.fd_fd);
1191 if (!FindNextFile(dirH, &data)) {
1197 nt_close(linkHandle.fd_fd);
1198 DeleteZLCFiles(basePath);
1200 /* Then why does this directory exist? Blow it away. */
1201 nt_RemoveDataDirectories(dirIH);
1207 /* The name begins with "Vol_" and ends with .data. See nt_HandleToVolDir() */
1209 DecodeVolumeName(char *name, int *vid)
1217 if (strncmp(name, "Vol_", 4))
1219 if (strcmp(name + len - 5, ".data"))
1222 stmp[len - 5] = '\0';
1223 *vid = base32_to_int(stmp + 4);
1227 /* Recall that the name beings with a "V_" */
1229 DecodeInodeName(char *name, int *p1, int *p2)
1234 (void)strcpy(stmp, name);
1235 s = strrchr(stmp, '_');
1239 t = strrchr(s, '.');
1244 *p1 = base32_to_int(s);
1245 *p2 = base32_to_int(t + 1);
1252 * returns a static string used to print either 32 or 64 bit inode numbers.
1255 PrintInode(char *s, Inode ino)
1257 static afs_ino_str_t result;
1261 (void)sprintf((char *)s, "%I64u", ino);
1267 /* Routines to facilitate removing zero link count files. */
1268 #define MAX_ZLC_NAMES 32
1269 #define MAX_ZLC_NAMELEN 16
1270 typedef struct zlcList_s {
1271 struct zlcList_s *zlc_next;
1273 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1276 static zlcList_t *zlcAnchor = NULL;
1277 static zlcList_t *zlcCur = NULL;
1280 AddToZLCDeleteList(char dir, char *name)
1282 assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1284 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1285 if (zlcCur && zlcCur->zlc_next)
1286 zlcCur = zlcCur->zlc_next;
1288 zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
1294 zlcCur->zlc_next = tmp;
1298 zlcCur->zlc_next = NULL;
1302 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1307 DeleteZLCFiles(char *path)
1313 for (z = zlcAnchor; z; z = z->zlc_next) {
1314 for (i = 0; i < z->zlc_n; i++) {
1315 (void)sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1316 if (nt_unlink(fname) < 0) {
1317 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1321 z->zlc_n = 0; /* Can reuse space. */
1334 tnext = i->zlc_next;
1338 zlcCur = zlcAnchor = NULL;
1341 #endif /* AFS_NT40_ENV */