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>
29 #include <afs/afsutil.h>
31 #include <afs/afsint.h>
35 #include "viceinode.h"
37 #include <afs/afs_assert.h>
38 #include <afs/errmap_nt.h>
40 #define BASEFILEATTRIBUTE FILE_ATTRIBUTE_NORMAL
44 static void AddToZLCDeleteList(char dir, char *name);
46 /* nt_unlink - unlink a case sensitive name.
48 * nt_unlink supports the nt_dec call.
50 * This nt_unlink has the delete on last close semantics of the Unix unlink
51 * with a minor twist. Subsequent CreateFile calls on this file can succeed
52 * if they open for delete. It's also unclear what happens if a CreateFile
53 * call tries to create a new file with the same name. Fortunately, neither
54 * case should occur as part of nt_dec.
61 fh = CreateFile(name, GENERIC_READ | GENERIC_WRITE,
62 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
64 BASEFILEATTRIBUTE | FILE_FLAG_DELETE_ON_CLOSE |
65 FILE_FLAG_POSIX_SEMANTICS, NULL);
66 if (fh != INVALID_HANDLE_VALUE)
69 errno = nterr_nt2unix(GetLastError(), ENOENT);
75 /* nt_open - open an NT handle for a file.
78 * the handle or -1 on error.
81 nt_open(char *name, int flags, int mode)
85 DWORD nt_share = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
87 /* Really use the sequential one for data files, random for meta data. */
88 DWORD FandA = BASEFILEATTRIBUTE | FILE_FLAG_SEQUENTIAL_SCAN;
91 if ((flags & O_RDWR) || (flags & O_WRONLY))
92 nt_access |= GENERIC_WRITE;
93 if ((flags & O_RDWR) || (flags == O_RDONLY))
94 nt_access |= GENERIC_READ;
97 switch (flags & (O_CREAT | O_EXCL | O_TRUNC)) {
99 nt_create = OPEN_EXISTING;
102 nt_create = OPEN_ALWAYS;
104 case O_CREAT | O_TRUNC:
105 nt_create = CREATE_ALWAYS;
107 case O_CREAT | O_EXCL:
108 case O_CREAT | O_EXCL | O_TRUNC:
109 nt_create = CREATE_NEW;
112 nt_create = TRUNCATE_EXISTING;
114 case O_TRUNC | O_EXCL:
122 fh = CreateFile(name, nt_access, nt_share, NULL, nt_create, FandA, NULL);
124 if (fh == INVALID_HANDLE_VALUE) {
126 errno = nterr_nt2unix(GetLastError(), EBADF);
136 code = CloseHandle(fd);
138 errno = nterr_nt2unix(GetLastError(), EBADF);
145 nt_write(FD_t fd, char *buf, size_t size)
150 code = WriteFile((HANDLE) fd, (void *)buf, (DWORD) size, &nbytes, NULL);
153 errno = nterr_nt2unix(GetLastError(), EBADF);
160 nt_read(FD_t fd, char *buf, size_t size)
165 code = ReadFile((HANDLE) fd, (void *)buf, (DWORD) size, &nbytes, NULL);
168 errno = nterr_nt2unix(GetLastError(), EBADF);
175 nt_iread(IHandle_t * h, int offset, char *buf, int size)
184 nBytes = FDH_PREAD(fdP, buf, size, offset);
190 nt_iwrite(IHandle_t * h, int offset, char *buf, int size)
199 nBytes = FDH_PWRITE(fdP, buf, size, offset);
208 BY_HANDLE_FILE_INFORMATION finfo;
210 if (!GetFileInformationByHandle(fd, &finfo))
213 return finfo.nFileSizeLow;
218 nt_getFileCreationTime(FD_t fd, FILETIME * ftime)
220 BY_HANDLE_FILE_INFORMATION finfo;
222 if (!GetFileInformationByHandle(fd, &finfo))
225 *ftime = finfo.ftCreationTime;
231 nt_setFileCreationTime(FD_t fd, FILETIME * ftime)
233 return !SetFileTime(fd, ftime, NULL, NULL);
245 cdrive = 'A' + (n - 1);
248 cdrive = _toupper(cdrive);
250 (void)sprintf(sdrive, "\\\\.\\%c:", cdrive);
251 drive_fd = nt_open(sdrive, O_RDWR, 0666);
252 if (drive_fd == INVALID_FD) {
256 if (!FlushFileBuffers((HANDLE) drive_fd)) {
257 errno = nterr_nt2unix(GetLastError(), EBADF);
266 /* Currently nt_ftruncate only tested to shrink a file. */
268 nt_ftruncate(FD_t fd, int len)
270 if (SetFilePointer(fd, (LONG) len, NULL, FILE_BEGIN)
272 errno = nterr_nt2unix(GetLastError(), EBADF);
275 if (!SetEndOfFile(fd)) {
276 errno = nterr_nt2unix(GetLastError(), EBADF);
286 int code = FlushFileBuffers(fd);
287 return code == 0 ? -1 : 0;
292 nt_seek(FD_t fd, int off, int where)
294 int code = SetFilePointer(fd, off, NULL, where);
299 /* Inode number format:
300 * low 32 bits - if a regular file or directory, the vnode. Else the type.
301 * 32-36 - unquifier tag and index into counts array for this vnode. Only
302 * two of the available bits are currently used. The rest are
303 * present in case we ever increase the number of types of volumes
304 * in the volume grou.
305 * bit 37 : 1 == special, 0 == regular
307 #define NT_VNODEMASK 0x00ffffffff
308 /* While the TAGMASK is 7, note that we are leaving 1 more bit available. */
309 #define NT_TAGMASK 0x7
310 #define NT_TAGSHIFT 32
311 #define NT_INODESPECIAL 0x2000000000
313 #define NT_MAXVOLS 5 /* Maximum supported number of volumes per volume
314 * group, not counting temporary (move) volumes.
315 * This is the number of separate files, all having
316 * the same vnode number, which can occur in a volume
321 int nt_SetLinkCount(FdHandle_t * h, Inode ino, int count, int locked);
325 * converts a device number (2-25) into a drive letter name.
328 * drive - assumes drive is a pointer to a string at least 3 bytes long.
329 * dev - drive number 2-25, since A-C already in use.
332 * Returns pointer to end of drive if successful, else NULL.
336 nt_DevToDrive(char *drive, int dev)
338 if (dev < 2 || dev > 25) {
340 return NULL; /* Invalid drive */
342 drive[0] = (char)('A' + dev);
350 /* Returns pointer to end of name if successful, else NULL. */
352 nt_HandleToVolDir(char *name, IHandle_t * h)
356 if (!(name = nt_DevToDrive(name, h->ih_dev)))
359 (void)memcpy(name, "\\Vol_", 5);
361 (void)strcpy(name, int_to_base32(str1, h->ih_vid));
362 name += strlen(name);
363 memcpy(name, ".data", 5);
372 * Constructs a file name for the fully qualified handle.
375 nt_HandleToName(char *name, IHandle_t * h)
378 int tag = (int)((h->ih_ino >> NT_TAGSHIFT) & NT_TAGMASK);
379 int vno = (int)(h->ih_ino & NT_VNODEMASK);
381 if (!(name = nt_HandleToVolDir(name, h)))
385 if (h->ih_ino & NT_INODESPECIAL)
391 str1[1] = ((vno & 0x1f) >> 1) + 'A';
394 memcpy(name, str1, 2);
396 (void)memcpy(name, "\\V_", 3);
398 (void)strcpy(name, int_to_base32(str1, vno));
399 name += strlen(name);
401 (void)strcpy(name, int_to_base32(str1, tag));
402 name += strlen(name);
408 /* nt_CreateDataDirectories
410 * The data for each volume is in a separate directory. The name of the
411 * volume is of the form: Vol_NNNNNN.data, where NNNNNN is a base 32
412 * representation of the RW volume ID (even where the RO is the only volume
413 * on the partition). Below that are separate subdirectories for the
414 * AFS directories and special files. There are also 16 directories for files,
415 * hashed on the low 5 bits (recall bit0 is always 0) of the vnode number.
416 * These directories are named:
417 * A - P - 16 file directories.
418 * Q ----- data directory
419 * R ----- special files directory
422 nt_CreateDataDirectories(IHandle_t * h, int *created)
428 if (!(s = nt_HandleToVolDir(name, h)))
431 if (mkdir(name) < 0) {
439 for (i = 'A'; i <= 'R'; i++) {
441 if (mkdir(name) < 0 && errno != EEXIST)
447 /* nt_RemoveDataDirectories
449 * Returns -1 on error. Typically, callers ignore this error bcause we
450 * can continue running if the removes fail. The salvage process will
451 * finish tidying up for us.
454 nt_RemoveDataDirectories(IHandle_t * h)
460 if (!(s = nt_HandleToVolDir(name, h)))
465 for (i = 'A'; i <= 'R'; i++) {
467 if (rmdir(name) < 0 && errno != ENOENT)
471 /* Delete the Vol_NNNNNN.data directory. */
474 if (rmdir(name) < 0 && errno != ENOENT) {
482 /* Create the file in the name space.
484 * Parameters stored as follows:
486 * p1 - volid - implied in containing directory.
487 * p2 - vnode - name is <vnode>.<tag> where tag is a file name unqiquifier.
488 * p3 - uniq -- creation time - dwHighDateTime
489 * p4 - dv ---- creation time - dwLowDateTime
491 * p1 - volid - creation time - dwHighDateTime
492 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
493 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
494 * p4 - parid - parent volume id - implied in containing directory.
496 * Return value is the inode number or (Inode)-1 if error.
497 * We "know" there is only one link table, so return EEXIST if there already
498 * is a link table. It's up to the calling code to test errno and increment
504 * This function is called by VCreateVolume to hide the implementation
505 * details of the inode numbers.
508 nt_MakeSpecIno(int type)
510 return ((Inode) type | (Inode) NT_INODESPECIAL);
514 nt_icreate(IHandle_t * h, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
529 memset((void *)&tmp, 0, sizeof(IHandle_t));
532 tmp.ih_dev = tolower(*part) - 'a';
535 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
537 if (nt_CreateDataDirectories(&tmp, &created_dir) < 0)
540 tmp.ih_ino = nt_MakeSpecIno(p3);
541 ftime.dwHighDateTime = p1;
542 ftime.dwLowDateTime = p2;
544 /* Regular file or directory.
545 * Encoding: p1 -> dir, p2 -> name, p3,p4 -> Create time
547 tmp.ih_ino = (Inode) p2;
550 ftime.dwHighDateTime = p3;
551 ftime.dwLowDateTime = p4;
554 /* Now create file. */
555 if ((code = nt_HandleToName(filename, &tmp)) < 0)
558 p = filename + strlen(filename);
560 for (i = 0; i < NT_MAXVOLS; i++) {
561 *p = *int_to_base32(str1, i);
562 fd = nt_open(filename, O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0666);
563 if (fd != INVALID_FD)
565 if (p2 == -1 && p3 == VI_LINKTABLE)
568 if (fd == INVALID_FD) {
573 tmp.ih_ino &= ~((Inode) NT_TAGMASK << NT_TAGSHIFT);
574 tmp.ih_ino |= ((Inode) i << NT_TAGSHIFT);
577 if (!SetFileTime((HANDLE) fd, &ftime, NULL, NULL)) {
585 if (fd == INVALID_FD) {
587 code = nt_unlink(filename);
598 code = nt_SetLinkCount(fdP, tmp.ih_ino, 1, 0);
600 } else if (p2 == -1 && p3 == VI_LINKTABLE) {
601 if (fd == INVALID_FD)
603 /* hack at tmp to setup for set link count call. */
605 code = nt_SetLinkCount(&tfd, (Inode) 0, 1, 0);
610 if (fd != INVALID_FD)
613 if (code && created_dir) {
615 nt_RemoveDataDirectories(&tmp);
618 return code ? (Inode) - 1 : tmp.ih_ino;
623 nt_iopen(IHandle_t * h)
628 /* Convert handle to file name. */
629 if (nt_HandleToName(name, h) < 0)
632 fd = nt_open(name, O_RDWR, 0666);
636 /* Need to detect vol special file and just unlink. In those cases, the
637 * handle passed in _is_ for the inode. We only check p1 for the special
641 nt_dec(IHandle_t * h, Inode ino, int p1)
648 if (ino & NT_INODESPECIAL) {
651 WIN32_FIND_DATA info;
654 /* Verify this is the right file. */
655 IH_INIT(tmp, h->ih_dev, h->ih_vid, ino);
657 if (nt_HandleToName(name, tmp) < 0) {
664 FindFirstFileEx(name, FindExInfoStandard, &info,
665 FindExSearchNameMatch, NULL,
666 FIND_FIRST_EX_CASE_SENSITIVE);
670 return -1; /* Can't get info, leave alone */
674 if (info.ftCreationTime.dwHighDateTime != (unsigned int)p1) {
679 /* If it's the link table itself, decrement the link count. */
680 if ((ino & NT_VNODEMASK) == VI_LINKTABLE) {
687 if ((count = nt_GetLinkCount(fdP, (Inode) 0, 1)) < 0) {
688 FDH_REALLYCLOSE(fdP);
694 if (nt_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) < 0) {
695 FDH_REALLYCLOSE(fdP);
700 FDH_REALLYCLOSE(fdP);
707 if ((code = nt_unlink(name)) == 0) {
708 if ((ino & NT_VNODEMASK) == VI_LINKTABLE) {
709 /* Try to remove directory. If it fails, that's ok.
710 * Salvage will clean up.
712 (void)nt_RemoveDataDirectories(tmp);
718 /* Get a file descriptor handle for this Inode */
724 if ((count = nt_GetLinkCount(fdP, ino, 1)) < 0) {
725 FDH_REALLYCLOSE(fdP);
731 if (nt_SetLinkCount(fdP, ino, count, 1) < 0) {
732 FDH_REALLYCLOSE(fdP);
739 nt_HandleToName(name, &th);
740 code = nt_unlink(name);
749 nt_inc(IHandle_t * h, Inode ino, int p1)
755 if (ino & NT_INODESPECIAL) {
756 if ((ino & NT_VNODEMASK) != VI_LINKTABLE)
761 /* Get a file descriptor handle for this Inode */
767 if ((count = nt_GetLinkCount(fdP, ino, 1)) < 0)
776 if (nt_SetLinkCount(fdP, ino, count, 1) < 0)
780 FDH_REALLYCLOSE(fdP);
789 /************************************************************************
790 * Link Table Organization
791 ************************************************************************
793 * The link table volume special file is used to hold the link counts that
794 * are held in the inodes in inode based AFS vice filesystems. Since NTFS
795 * doesn't provide us that access, the link counts are being kept in a separate
796 * volume special file. The file begins with the usual version stamp
797 * information and is then followed by one row per vnode number. vnode 0
798 * is used to hold the link count of the link table itself. That is because
799 * the same link table is shared among all the volumes of the volume group
800 * and is deleted only when the last volume of a volume group is deleted.
802 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
803 * link count from 0 through 7. Each colume represents a unique instance of
804 * that vnode. Say we have a file shared between the RW and a RO and a
805 * different version of the file (or a different uniquifer) for the BU volume.
806 * Then one column would be holding the link count of 2 for the RW and RO
807 * and a different column would hold the link count of 1 for the BU volume.
808 * The column used is determined for NT by the uiquifier tag applied to
809 * generate a unique file name in the NTFS namespace. The file name is
810 * of the form "V_<vno>.<tag>" . And the <tag> is also the column number
813 #define LINKTABLE_WIDTH 2
814 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
817 nt_GetLCOffsetAndIndexFromIno(Inode ino, int *offset, int *index)
819 int toff = (int)(ino & NT_VNODEMASK);
820 int tindex = (int)((ino >> NT_TAGSHIFT) & NT_TAGMASK);
822 *offset = (toff << LINKTABLE_SHIFT) + 8; /* *2 + sizeof stamp */
823 *index = (tindex << 1) + tindex;
828 * If lockit is set, lock the file and leave it locked upon a successful
832 nt_GetLinkCountInternal(FdHandle_t * h, Inode ino, int lockit, int fixup)
834 unsigned short row = 0;
835 DWORD bytesRead, bytesWritten;
838 /* there's no linktable yet. the salvager will create one later */
839 if (h->fd_fd == INVALID_HANDLE_VALUE && fixup)
842 nt_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
845 if (!LockFile(h->fd_fd, offset, 0, 2, 0))
849 if (!SetFilePointer(h->fd_fd, (LONG) offset, NULL, FILE_BEGIN))
850 goto bad_getLinkByte;
852 if (!ReadFile(h->fd_fd, (void *)&row, 2, &bytesRead, NULL))
853 goto bad_getLinkByte;
855 if (bytesRead == 0 && fixup) {
858 if (!GetFileSizeEx(h->fd_fd, &size) || size.QuadPart >= offset+sizeof(row))
859 goto bad_getLinkByte;
860 FDH_TRUNC(h, offset+sizeof(row));
863 WriteFile(h->fd_fd, (char *)&row, sizeof(row), &bytesWritten, NULL);
866 if (fixup && !((row >> index) & NT_TAGMASK)) {
871 return (int)((row >> index) & NT_TAGMASK);
875 UnlockFile(h->fd_fd, offset, 0, 2, 0);
880 nt_GetLinkCount(FdHandle_t * h, Inode ino, int lockit)
882 return nt_GetLinkCountInternal(h, ino, lockit, 0);
886 nt_SetNonZLC(FdHandle_t * h, Inode ino)
888 (void)nt_GetLinkCountInternal(h, ino, 0, 1);
893 * If locked is set, assume file is locked. Otherwise, lock file before
894 * proceeding to modify it.
897 nt_SetLinkCount(FdHandle_t * h, Inode ino, int count, int locked)
901 DWORD bytesRead, bytesWritten;
904 nt_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
908 if (!LockFile(h->fd_fd, offset, 0, 2, 0)) {
909 errno = nterr_nt2unix(GetLastError(), EBADF);
913 if (!SetFilePointer(h->fd_fd, (LONG) offset, NULL, FILE_BEGIN)) {
914 errno = nterr_nt2unix(GetLastError(), EBADF);
915 goto bad_SetLinkCount;
919 if (!ReadFile(h->fd_fd, (void *)&row, 2, &bytesRead, NULL)) {
920 errno = nterr_nt2unix(GetLastError(), EBADF);
921 goto bad_SetLinkCount;
926 bytesRead = 7 << index;
928 row &= (unsigned short)~bytesRead;
929 row |= (unsigned short)count;
931 if (!SetFilePointer(h->fd_fd, (LONG) offset, NULL, FILE_BEGIN)) {
932 errno = nterr_nt2unix(GetLastError(), EBADF);
933 goto bad_SetLinkCount;
936 if (!WriteFile(h->fd_fd, (void *)&row, 2, &bytesWritten, NULL)) {
937 errno = nterr_nt2unix(GetLastError(), EBADF);
938 goto bad_SetLinkCount;
945 UnlockFile(h->fd_fd, offset, 0, 2, 0);
951 /* ListViceInodes - write inode data to a results file. */
952 static int DecodeInodeName(char *name, int *p1, int *p2);
953 static int DecodeVolumeName(char *name, afs_uint32 *vid);
954 static int nt_ListAFSSubDirs(IHandle_t * dirIH,
955 int (*write_fun) (FILE *, struct ViceInodeInfo *,
956 char *, char *), FILE * fp,
957 int (*judgeFun) (struct ViceInodeInfo *,
958 afs_uint32 vid, void *rock),
959 afs_uint32 singleVolumeNumber, void *rock);
964 * Write the inode data to the results file.
966 * Returns -2 on error, 0 on success.
968 * This is written as a callback simply so that other listing routines
969 * can use the same inode reading code.
972 WriteInodeInfo(FILE * fp, struct ViceInodeInfo *info, char *dir, char *name)
975 n = fwrite(info, sizeof(*info), 1, fp);
976 return (n == 1) ? 0 : -2;
981 * Fill the results file with the requested inode information.
985 * -1 - complete failure, salvage should terminate.
986 * -2 - not enough space on partition, salvager has error message for this.
988 * This code optimizes single volume salvages by just looking at that one
989 * volume's directory.
991 * If the inodeFile is NULL, then don't call the write routine.
994 ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
995 int (*judgeInode) (struct ViceInodeInfo * info, afs_uint32 vid, void *rock),
996 afs_uint32 singleVolumeNumber, int *forcep, int forceR, char *wpath,
1003 nt_ListAFSFiles(wpath, WriteInodeInfo, inodeFile, judgeInode,
1004 singleVolumeNumber, rock);
1013 if (fflush(inodeFile) == EOF) {
1014 Log("Unable to successfully flush inode file for %s\n", mountedOn);
1017 if (fsync(fileno(inodeFile)) == -1) {
1018 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1023 * Paranoia: check that the file is really the right size
1025 if (fstat(fileno(inodeFile), &status) == -1) {
1026 Log("Unable to successfully stat inode file for %s\n", mountedOn);
1029 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
1030 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1031 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
1041 * Collect all the matching AFS files on the drive.
1042 * If singleVolumeNumber is non-zero, just return files for that volume.
1044 * Returns <0 on error, else number of files found to match.
1047 nt_ListAFSFiles(char *dev,
1048 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1050 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1051 afs_uint32 singleVolumeNumber, void *rock)
1054 char name[MAX_PATH];
1058 static void FreeZLCList(void);
1060 memset((void *)&h, 0, sizeof(IHandle_t));
1061 h.ih_dev = toupper(*dev) - 'A';
1063 if (singleVolumeNumber) {
1064 h.ih_vid = singleVolumeNumber;
1065 if (!nt_HandleToVolDir(name, &h))
1068 nt_ListAFSSubDirs(&h, writeFun, fp, judgeFun, singleVolumeNumber, rock);
1072 /* Find all Vol_*.data directories and descend through them. */
1073 if (!nt_DevToDrive(name, h.ih_dev))
1076 dirp = opendir(name);
1079 while (dp = readdir(dirp)) {
1080 if (!DecodeVolumeName(dp->d_name, &h.ih_vid)) {
1081 ninodes += nt_ListAFSSubDirs(&h, writeFun, fp, judgeFun, 0, rock);
1091 /* nt_ListAFSSubDirs
1093 * List the S, F, and D subdirectories of this volume's directory.
1097 * > = 0 - number of AFS files found.
1100 nt_ListAFSSubDirs(IHandle_t * dirIH,
1101 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1103 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1104 afs_uint32 singleVolumeNumber, void *rock)
1107 IHandle_t myIH = *dirIH;
1109 WIN32_FIND_DATA data;
1111 char basePath[1024];
1113 char findPath[1024];
1114 struct ViceInodeInfo info;
1116 FdHandle_t linkHandle;
1118 static void DeleteZLCFiles(char *path);
1120 s = nt_HandleToVolDir(path, &myIH);
1121 strcpy(basePath, path);
1128 /* Do the directory containing the special files first to pick up link
1131 for (i = 'R'; i >= 'A'; i--) {
1133 (void)strcpy(findPath, path);
1134 (void)strcat(findPath, "\\*");
1136 FindFirstFileEx(findPath, FindExInfoStandard, &data,
1137 FindExSearchNameMatch, NULL,
1138 FIND_FIRST_EX_CASE_SENSITIVE);
1139 if (dirH == INVALID_HANDLE_VALUE)
1142 /* Store the vice info. */
1143 memset((void *)&info, 0, sizeof(info));
1144 if (*data.cFileName == '.')
1146 if (DecodeInodeName(data.cFileName, &vno, &tag) < 0) {
1147 Log("Error parsing %s\\%s\n", path, data.cFileName);
1149 info.inodeNumber = (Inode) tag << NT_TAGSHIFT;
1150 info.inodeNumber |= (Inode) vno;
1151 info.byteCount = data.nFileSizeLow;
1153 if (i == 'R') { /* Special inode. */
1154 info.inodeNumber |= NT_INODESPECIAL;
1155 info.u.param[0] = data.ftCreationTime.dwHighDateTime;
1156 info.u.param[1] = data.ftCreationTime.dwLowDateTime;
1157 info.u.param[2] = vno;
1158 info.u.param[3] = dirIH->ih_vid;
1159 if (info.u.param[2] != VI_LINKTABLE) {
1162 /* Open this handle */
1164 (void)sprintf(lpath, "%s\\%s", path, data.cFileName);
1165 linkHandle.fd_fd = nt_open(lpath, O_RDONLY, 0666);
1167 nt_GetLinkCount(&linkHandle, (Inode) 0, 0);
1169 } else { /* regular file. */
1171 nt_GetLinkCount(&linkHandle, info.inodeNumber, 0);
1172 if (info.linkCount == 0) {
1174 Log("Found 0 link count file %s\\%s, deleting it.\n",
1175 path, data.cFileName);
1176 AddToZLCDeleteList((char)i, data.cFileName);
1178 Log("Found 0 link count file %s\\%s.\n", path,
1183 info.u.param[0] = dirIH->ih_vid;
1184 info.u.param[1] = vno;
1185 info.u.param[2] = data.ftCreationTime.dwHighDateTime;
1186 info.u.param[3] = data.ftCreationTime.dwLowDateTime;
1188 if (judgeFun && !(*judgeFun) (&info, singleVolumeNumber, rock))
1190 if ((*writeFun) (fp, &info, path, data.cFileName) < 0) {
1191 nt_close(linkHandle.fd_fd);
1198 if (!FindNextFile(dirH, &data)) {
1204 nt_close(linkHandle.fd_fd);
1205 DeleteZLCFiles(basePath);
1207 /* Then why does this directory exist? Blow it away. */
1208 nt_RemoveDataDirectories(dirIH);
1214 /* The name begins with "Vol_" and ends with .data. See nt_HandleToVolDir() */
1216 DecodeVolumeName(char *name, afs_uint32 *vid)
1224 if (strncmp(name, "Vol_", 4))
1226 if (strcmp(name + len - 5, ".data"))
1229 stmp[len - 5] = '\0';
1230 *vid = base32_to_int(stmp + 4);
1234 /* Recall that the name beings with a "V_" */
1236 DecodeInodeName(char *name, int *p1, int *p2)
1241 (void)strcpy(stmp, name);
1242 s = strrchr(stmp, '_');
1246 t = strrchr(s, '.');
1251 *p1 = base32_to_int(s);
1252 *p2 = base32_to_int(t + 1);
1259 * returns a static string used to print either 32 or 64 bit inode numbers.
1262 PrintInode(char *s, Inode ino)
1264 static afs_ino_str_t result;
1268 (void)sprintf((char *)s, "%I64u", ino);
1274 /* Routines to facilitate removing zero link count files. */
1275 #define MAX_ZLC_NAMES 32
1276 #define MAX_ZLC_NAMELEN 16
1277 typedef struct zlcList_s {
1278 struct zlcList_s *zlc_next;
1280 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1283 static zlcList_t *zlcAnchor = NULL;
1284 static zlcList_t *zlcCur = NULL;
1287 AddToZLCDeleteList(char dir, char *name)
1289 osi_Assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1291 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1292 if (zlcCur && zlcCur->zlc_next)
1293 zlcCur = zlcCur->zlc_next;
1295 zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
1301 zlcCur->zlc_next = tmp;
1305 zlcCur->zlc_next = NULL;
1309 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1314 DeleteZLCFiles(char *path)
1320 for (z = zlcAnchor; z; z = z->zlc_next) {
1321 for (i = 0; i < z->zlc_n; i++) {
1322 (void)sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1323 if (nt_unlink(fname) < 0) {
1324 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1328 z->zlc_n = 0; /* Can reuse space. */
1341 tnext = i->zlc_next;
1345 zlcCur = zlcAnchor = NULL;
1348 #endif /* AFS_NT40_ENV */