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/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 if (FDH_SEEK(fdP, offset, SEEK_SET) < 0) {
185 FDH_REALLYCLOSE(fdP);
189 nBytes = FDH_READ(fdP, buf, size);
195 nt_iwrite(IHandle_t * h, int offset, char *buf, int size)
204 if (FDH_SEEK(fdP, offset, SEEK_SET) < 0) {
205 FDH_REALLYCLOSE(fdP);
208 nBytes = FDH_WRITE(fdP, buf, size);
217 BY_HANDLE_FILE_INFORMATION finfo;
219 if (!GetFileInformationByHandle(fd, &finfo))
222 return finfo.nFileSizeLow;
227 nt_getFileCreationTime(FD_t fd, FILETIME * ftime)
229 BY_HANDLE_FILE_INFORMATION finfo;
231 if (!GetFileInformationByHandle(fd, &finfo))
234 *ftime = finfo.ftCreationTime;
240 nt_setFileCreationTime(FD_t fd, FILETIME * ftime)
242 return !SetFileTime(fd, ftime, NULL, NULL);
254 cdrive = 'A' + (n - 1);
257 cdrive = _toupper(cdrive);
259 (void)sprintf(sdrive, "\\\\.\\%c:", cdrive);
260 drive_fd = nt_open(sdrive, O_RDWR, 0666);
261 if (drive_fd == INVALID_FD) {
265 if (!FlushFileBuffers((HANDLE) drive_fd)) {
266 errno = nterr_nt2unix(GetLastError(), EBADF);
275 /* Currently nt_ftruncate only tested to shrink a file. */
277 nt_ftruncate(FD_t fd, int len)
279 if (SetFilePointer(fd, (LONG) len, NULL, FILE_BEGIN)
281 errno = nterr_nt2unix(GetLastError(), EBADF);
284 if (!SetEndOfFile(fd)) {
285 errno = nterr_nt2unix(GetLastError(), EBADF);
295 int code = FlushFileBuffers(fd);
296 return code == 0 ? -1 : 0;
301 nt_seek(FD_t fd, int off, int where)
303 int code = SetFilePointer(fd, off, NULL, where);
308 /* Inode number format:
309 * low 32 bits - if a regular file or directory, the vnode. Else the type.
310 * 32-36 - unquifier tag and index into counts array for this vnode. Only
311 * two of the available bits are currently used. The rest are
312 * present in case we ever increase the number of types of volumes
313 * in the volume grou.
314 * bit 37 : 1 == special, 0 == regular
316 #define NT_VNODEMASK 0x00ffffffff
317 /* While the TAGMASK is 7, note that we are leaving 1 more bit available. */
318 #define NT_TAGMASK 0x7
319 #define NT_TAGSHIFT 32
320 #define NT_INODESPECIAL 0x2000000000
322 #define NT_MAXVOLS 5 /* Maximum supported number of volumes per volume
323 * group, not counting temporary (move) volumes.
324 * This is the number of separate files, all having
325 * the same vnode number, which can occur in a volume
330 int nt_SetLinkCount(FdHandle_t * h, Inode ino, int count, int locked);
334 * converts a device number (2-25) into a drive letter name.
337 * drive - assumes drive is a pointer to a string at least 3 bytes long.
338 * dev - drive number 2-25, since A-C already in use.
341 * Returns pointer to end of drive if successful, else NULL.
345 nt_DevToDrive(char *drive, int dev)
347 if (dev < 2 || dev > 25) {
349 return NULL; /* Invalid drive */
351 drive[0] = (char)('A' + dev);
359 /* Returns pointer to end of name if successful, else NULL. */
361 nt_HandleToVolDir(char *name, IHandle_t * h)
365 if (!(name = nt_DevToDrive(name, h->ih_dev)))
368 (void)memcpy(name, "\\Vol_", 5);
370 (void)strcpy(name, int_to_base32(str1, h->ih_vid));
371 name += strlen(name);
372 memcpy(name, ".data", 5);
381 * Constructs a file name for the fully qualified handle.
384 nt_HandleToName(char *name, IHandle_t * h)
387 int tag = (int)((h->ih_ino >> NT_TAGSHIFT) & NT_TAGMASK);
388 int vno = (int)(h->ih_ino & NT_VNODEMASK);
390 if (!(name = nt_HandleToVolDir(name, h)))
394 if (h->ih_ino & NT_INODESPECIAL)
400 str1[1] = ((vno & 0x1f) >> 1) + 'A';
403 memcpy(name, str1, 2);
405 (void)memcpy(name, "\\V_", 3);
407 (void)strcpy(name, int_to_base32(str1, vno));
408 name += strlen(name);
410 (void)strcpy(name, int_to_base32(str1, tag));
411 name += strlen(name);
417 /* nt_CreateDataDirectories
419 * The data for each volume is in a separate directory. The name of the
420 * volume is of the form: Vol_NNNNNN.data, where NNNNNN is a base 32
421 * representation of the RW volume ID (even where the RO is the only volume
422 * on the partition). Below that are separate subdirectories for the
423 * AFS directories and special files. There are also 16 directories for files,
424 * hashed on the low 5 bits (recall bit0 is always 0) of the vnode number.
425 * These directories are named:
426 * A - P - 16 file directories.
427 * Q ----- data directory
428 * R ----- special files directory
431 nt_CreateDataDirectories(IHandle_t * h, int *created)
437 if (!(s = nt_HandleToVolDir(name, h)))
440 if (mkdir(name) < 0) {
448 for (i = 'A'; i <= 'R'; i++) {
450 if (mkdir(name) < 0 && errno != EEXIST)
456 /* nt_RemoveDataDirectories
458 * Returns -1 on error. Typically, callers ignore this error bcause we
459 * can continue running if the removes fail. The salvage process will
460 * finish tidying up for us.
463 nt_RemoveDataDirectories(IHandle_t * h)
469 if (!(s = nt_HandleToVolDir(name, h)))
474 for (i = 'A'; i <= 'R'; i++) {
476 if (rmdir(name) < 0 && errno != ENOENT)
480 /* Delete the Vol_NNNNNN.data directory. */
483 if (rmdir(name) < 0 && errno != ENOENT) {
491 /* Create the file in the name space.
493 * Parameters stored as follows:
495 * p1 - volid - implied in containing directory.
496 * p2 - vnode - name is <vnode>.<tag> where tag is a file name unqiquifier.
497 * p3 - uniq -- creation time - dwHighDateTime
498 * p4 - dv ---- creation time - dwLowDateTime
500 * p1 - volid - creation time - dwHighDateTime
501 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
502 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
503 * p4 - parid - parent volume id - implied in containing directory.
505 * Return value is the inode number or (Inode)-1 if error.
506 * We "know" there is only one link table, so return EEXIST if there already
507 * is a link table. It's up to the calling code to test errno and increment
513 * This function is called by VCreateVolume to hide the implementation
514 * details of the inode numbers.
517 nt_MakeSpecIno(int type)
519 return ((Inode) type | (Inode) NT_INODESPECIAL);
523 nt_icreate(IHandle_t * h, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
538 memset((void *)&tmp, 0, sizeof(IHandle_t));
541 tmp.ih_dev = tolower(*part) - 'a';
544 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
546 if (nt_CreateDataDirectories(&tmp, &created_dir) < 0)
549 tmp.ih_ino = nt_MakeSpecIno(p3);
550 ftime.dwHighDateTime = p1;
551 ftime.dwLowDateTime = p2;
553 /* Regular file or directory.
554 * Encoding: p1 -> dir, p2 -> name, p3,p4 -> Create time
556 tmp.ih_ino = (Inode) p2;
559 ftime.dwHighDateTime = p3;
560 ftime.dwLowDateTime = p4;
563 /* Now create file. */
564 if ((code = nt_HandleToName(filename, &tmp)) < 0)
567 p = filename + strlen(filename);
569 for (i = 0; i < NT_MAXVOLS; i++) {
570 *p = *int_to_base32(str1, i);
571 fd = nt_open(filename, O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0666);
572 if (fd != INVALID_FD)
574 if (p2 == -1 && p3 == VI_LINKTABLE)
577 if (fd == INVALID_FD) {
582 tmp.ih_ino &= ~((Inode) NT_TAGMASK << NT_TAGSHIFT);
583 tmp.ih_ino |= ((Inode) i << NT_TAGSHIFT);
586 if (!SetFileTime((HANDLE) fd, &ftime, NULL, NULL)) {
594 if (fd == INVALID_FD) {
596 code = nt_unlink(filename);
607 code = nt_SetLinkCount(fdP, tmp.ih_ino, 1, 0);
609 } else if (p2 == -1 && p3 == VI_LINKTABLE) {
610 if (fd == INVALID_FD)
612 /* hack at tmp to setup for set link count call. */
614 code = nt_SetLinkCount(&tfd, (Inode) 0, 1, 0);
619 if (fd != INVALID_FD)
622 if (code && created_dir) {
624 nt_RemoveDataDirectories(&tmp);
627 return code ? (Inode) - 1 : tmp.ih_ino;
632 nt_iopen(IHandle_t * h)
637 /* Convert handle to file name. */
638 if (nt_HandleToName(name, h) < 0)
641 fd = nt_open(name, O_RDWR, 0666);
645 /* Need to detect vol special file and just unlink. In those cases, the
646 * handle passed in _is_ for the inode. We only check p1 for the special
650 nt_dec(IHandle_t * h, Inode ino, int p1)
657 if (ino & NT_INODESPECIAL) {
660 WIN32_FIND_DATA info;
663 /* Verify this is the right file. */
664 IH_INIT(tmp, h->ih_dev, h->ih_vid, ino);
666 if (nt_HandleToName(name, tmp) < 0) {
673 FindFirstFileEx(name, FindExInfoStandard, &info,
674 FindExSearchNameMatch, NULL,
675 FIND_FIRST_EX_CASE_SENSITIVE);
679 return -1; /* Can't get info, leave alone */
683 if (info.ftCreationTime.dwHighDateTime != (unsigned int)p1) {
688 /* If it's the link table itself, decrement the link count. */
689 if ((ino & NT_VNODEMASK) == VI_LINKTABLE) {
696 if ((count = nt_GetLinkCount(fdP, (Inode) 0, 1)) < 0) {
697 FDH_REALLYCLOSE(fdP);
703 if (nt_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) < 0) {
704 FDH_REALLYCLOSE(fdP);
709 FDH_REALLYCLOSE(fdP);
716 if ((code = nt_unlink(name)) == 0) {
717 if ((ino & NT_VNODEMASK) == VI_LINKTABLE) {
718 /* Try to remove directory. If it fails, that's ok.
719 * Salvage will clean up.
721 (void)nt_RemoveDataDirectories(tmp);
727 /* Get a file descriptor handle for this Inode */
733 if ((count = nt_GetLinkCount(fdP, ino, 1)) < 0) {
734 FDH_REALLYCLOSE(fdP);
740 if (nt_SetLinkCount(fdP, ino, count, 1) < 0) {
741 FDH_REALLYCLOSE(fdP);
748 nt_HandleToName(name, &th);
749 code = nt_unlink(name);
758 nt_inc(IHandle_t * h, Inode ino, int p1)
764 if (ino & NT_INODESPECIAL) {
765 if ((ino & NT_VNODEMASK) != VI_LINKTABLE)
770 /* Get a file descriptor handle for this Inode */
776 if ((count = nt_GetLinkCount(fdP, ino, 1)) < 0)
785 if (nt_SetLinkCount(fdP, ino, count, 1) < 0)
789 FDH_REALLYCLOSE(fdP);
798 /************************************************************************
799 * Link Table Organization
800 ************************************************************************
802 * The link table volume special file is used to hold the link counts that
803 * are held in the inodes in inode based AFS vice filesystems. Since NTFS
804 * doesn't provide us that access, the link counts are being kept in a separate
805 * volume special file. The file begins with the usual version stamp
806 * information and is then followed by one row per vnode number. vnode 0
807 * is used to hold the link count of the link table itself. That is because
808 * the same link table is shared among all the volumes of the volume group
809 * and is deleted only when the last volume of a volume group is deleted.
811 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
812 * link count from 0 through 7. Each colume represents a unique instance of
813 * that vnode. Say we have a file shared between the RW and a RO and a
814 * different version of the file (or a different uniquifer) for the BU volume.
815 * Then one column would be holding the link count of 2 for the RW and RO
816 * and a different column would hold the link count of 1 for the BU volume.
817 * The column used is determined for NT by the uiquifier tag applied to
818 * generate a unique file name in the NTFS namespace. The file name is
819 * of the form "V_<vno>.<tag>" . And the <tag> is also the column number
822 #define LINKTABLE_WIDTH 2
823 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
826 nt_GetLCOffsetAndIndexFromIno(Inode ino, int *offset, int *index)
828 int toff = (int)(ino & NT_VNODEMASK);
829 int tindex = (int)((ino >> NT_TAGSHIFT) & NT_TAGMASK);
831 *offset = (toff << LINKTABLE_SHIFT) + 8; /* *2 + sizeof stamp */
832 *index = (tindex << 1) + tindex;
837 * If lockit is set, lock the file and leave it locked upon a successful
841 nt_GetLinkCountInternal(FdHandle_t * h, Inode ino, int lockit, int fixup)
843 unsigned short row = 0;
844 DWORD bytesRead, bytesWritten;
847 /* there's no linktable yet. the salvager will create one later */
848 if (h->fd_fd == INVALID_HANDLE_VALUE && fixup)
851 nt_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
854 if (!LockFile(h->fd_fd, offset, 0, 2, 0))
858 if (!SetFilePointer(h->fd_fd, (LONG) offset, NULL, FILE_BEGIN))
859 goto bad_getLinkByte;
861 if (!ReadFile(h->fd_fd, (void *)&row, 2, &bytesRead, NULL))
862 goto bad_getLinkByte;
864 if (bytesRead == 0 && fixup) {
867 if (!GetFileSizeEx(h->fd_fd, &size) || size.QuadPart >= offset+sizeof(row))
868 goto bad_getLinkByte;
869 FDH_TRUNC(h, offset+sizeof(row));
872 WriteFile(h->fd_fd, (char *)&row, sizeof(row), &bytesWritten, NULL);
875 if (fixup && !((row >> index) & NT_TAGMASK)) {
880 return (int)((row >> index) & NT_TAGMASK);
884 UnlockFile(h->fd_fd, offset, 0, 2, 0);
889 nt_GetLinkCount(FdHandle_t * h, Inode ino, int lockit)
891 return nt_GetLinkCountInternal(h, ino, lockit, 0);
895 nt_SetNonZLC(FdHandle_t * h, Inode ino)
897 (void)nt_GetLinkCountInternal(h, ino, 0, 1);
902 * If locked is set, assume file is locked. Otherwise, lock file before
903 * proceeding to modify it.
906 nt_SetLinkCount(FdHandle_t * h, Inode ino, int count, int locked)
910 DWORD bytesRead, bytesWritten;
913 nt_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
917 if (!LockFile(h->fd_fd, offset, 0, 2, 0)) {
918 errno = nterr_nt2unix(GetLastError(), EBADF);
922 if (!SetFilePointer(h->fd_fd, (LONG) offset, NULL, FILE_BEGIN)) {
923 errno = nterr_nt2unix(GetLastError(), EBADF);
924 goto bad_SetLinkCount;
928 if (!ReadFile(h->fd_fd, (void *)&row, 2, &bytesRead, NULL)) {
929 errno = nterr_nt2unix(GetLastError(), EBADF);
930 goto bad_SetLinkCount;
935 bytesRead = 7 << index;
937 row &= (unsigned short)~bytesRead;
938 row |= (unsigned short)count;
940 if (!SetFilePointer(h->fd_fd, (LONG) offset, NULL, FILE_BEGIN)) {
941 errno = nterr_nt2unix(GetLastError(), EBADF);
942 goto bad_SetLinkCount;
945 if (!WriteFile(h->fd_fd, (void *)&row, 2, &bytesWritten, NULL)) {
946 errno = nterr_nt2unix(GetLastError(), EBADF);
947 goto bad_SetLinkCount;
954 UnlockFile(h->fd_fd, offset, 0, 2, 0);
960 /* ListViceInodes - write inode data to a results file. */
961 static int DecodeInodeName(char *name, int *p1, int *p2);
962 static int DecodeVolumeName(char *name, afs_uint32 *vid);
963 static int nt_ListAFSSubDirs(IHandle_t * dirIH,
964 int (*write_fun) (FILE *, struct ViceInodeInfo *,
965 char *, char *), FILE * fp,
966 int (*judgeFun) (struct ViceInodeInfo *,
967 afs_uint32 vid, void *rock),
968 afs_uint32 singleVolumeNumber, void *rock);
973 * Write the inode data to the results file.
975 * Returns -2 on error, 0 on success.
977 * This is written as a callback simply so that other listing routines
978 * can use the same inode reading code.
981 WriteInodeInfo(FILE * fp, struct ViceInodeInfo *info, char *dir, char *name)
984 n = fwrite(info, sizeof(*info), 1, fp);
985 return (n == 1) ? 0 : -2;
990 * Fill the results file with the requested inode information.
994 * -1 - complete failure, salvage should terminate.
995 * -2 - not enough space on partition, salvager has error message for this.
997 * This code optimizes single volume salvages by just looking at that one
998 * volume's directory.
1000 * If the inodeFile is NULL, then don't call the write routine.
1003 ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
1004 int (*judgeInode) (struct ViceInodeInfo * info, afs_uint32 vid, void *rock),
1005 afs_uint32 singleVolumeNumber, int *forcep, int forceR, char *wpath,
1012 nt_ListAFSFiles(wpath, WriteInodeInfo, inodeFile, judgeInode,
1013 singleVolumeNumber, rock);
1022 if (fflush(inodeFile) == EOF) {
1023 Log("Unable to successfully flush inode file for %s\n", mountedOn);
1026 if (fsync(fileno(inodeFile)) == -1) {
1027 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1032 * Paranoia: check that the file is really the right size
1034 if (fstat(fileno(inodeFile), &status) == -1) {
1035 Log("Unable to successfully stat inode file for %s\n", mountedOn);
1038 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
1039 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1040 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
1050 * Collect all the matching AFS files on the drive.
1051 * If singleVolumeNumber is non-zero, just return files for that volume.
1053 * Returns <0 on error, else number of files found to match.
1056 nt_ListAFSFiles(char *dev,
1057 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1059 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1060 afs_uint32 singleVolumeNumber, void *rock)
1063 char name[MAX_PATH];
1067 static void FreeZLCList(void);
1069 memset((void *)&h, 0, sizeof(IHandle_t));
1070 h.ih_dev = toupper(*dev) - 'A';
1072 if (singleVolumeNumber) {
1073 h.ih_vid = singleVolumeNumber;
1074 if (!nt_HandleToVolDir(name, &h))
1077 nt_ListAFSSubDirs(&h, writeFun, fp, judgeFun, singleVolumeNumber, rock);
1081 /* Find all Vol_*.data directories and descend through them. */
1082 if (!nt_DevToDrive(name, h.ih_dev))
1085 dirp = opendir(name);
1088 while (dp = readdir(dirp)) {
1089 if (!DecodeVolumeName(dp->d_name, &h.ih_vid)) {
1090 ninodes += nt_ListAFSSubDirs(&h, writeFun, fp, judgeFun, 0, rock);
1100 /* nt_ListAFSSubDirs
1102 * List the S, F, and D subdirectories of this volume's directory.
1106 * > = 0 - number of AFS files found.
1109 nt_ListAFSSubDirs(IHandle_t * dirIH,
1110 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1112 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1113 afs_uint32 singleVolumeNumber, void *rock)
1116 IHandle_t myIH = *dirIH;
1118 WIN32_FIND_DATA data;
1120 char basePath[1024];
1122 char findPath[1024];
1123 struct ViceInodeInfo info;
1125 FdHandle_t linkHandle;
1127 static void DeleteZLCFiles(char *path);
1129 s = nt_HandleToVolDir(path, &myIH);
1130 strcpy(basePath, path);
1137 /* Do the directory containing the special files first to pick up link
1140 for (i = 'R'; i >= 'A'; i--) {
1142 (void)strcpy(findPath, path);
1143 (void)strcat(findPath, "\\*");
1145 FindFirstFileEx(findPath, FindExInfoStandard, &data,
1146 FindExSearchNameMatch, NULL,
1147 FIND_FIRST_EX_CASE_SENSITIVE);
1148 if (dirH == INVALID_HANDLE_VALUE)
1151 /* Store the vice info. */
1152 memset((void *)&info, 0, sizeof(info));
1153 if (*data.cFileName == '.')
1155 if (DecodeInodeName(data.cFileName, &vno, &tag) < 0) {
1156 Log("Error parsing %s\\%s\n", path, data.cFileName);
1158 info.inodeNumber = (Inode) tag << NT_TAGSHIFT;
1159 info.inodeNumber |= (Inode) vno;
1160 info.byteCount = data.nFileSizeLow;
1162 if (i == 'R') { /* Special inode. */
1163 info.inodeNumber |= NT_INODESPECIAL;
1164 info.u.param[0] = data.ftCreationTime.dwHighDateTime;
1165 info.u.param[1] = data.ftCreationTime.dwLowDateTime;
1166 info.u.param[2] = vno;
1167 info.u.param[3] = dirIH->ih_vid;
1168 if (info.u.param[2] != VI_LINKTABLE) {
1171 /* Open this handle */
1173 (void)sprintf(lpath, "%s\\%s", path, data.cFileName);
1174 linkHandle.fd_fd = nt_open(lpath, O_RDONLY, 0666);
1176 nt_GetLinkCount(&linkHandle, (Inode) 0, 0);
1178 } else { /* regular file. */
1180 nt_GetLinkCount(&linkHandle, info.inodeNumber, 0);
1181 if (info.linkCount == 0) {
1183 Log("Found 0 link count file %s\\%s, deleting it.\n",
1184 path, data.cFileName);
1185 AddToZLCDeleteList((char)i, data.cFileName);
1187 Log("Found 0 link count file %s\\%s.\n", path,
1192 info.u.param[0] = dirIH->ih_vid;
1193 info.u.param[1] = vno;
1194 info.u.param[2] = data.ftCreationTime.dwHighDateTime;
1195 info.u.param[3] = data.ftCreationTime.dwLowDateTime;
1197 if (judgeFun && !(*judgeFun) (&info, singleVolumeNumber, rock))
1199 if ((*writeFun) (fp, &info, path, data.cFileName) < 0) {
1200 nt_close(linkHandle.fd_fd);
1207 if (!FindNextFile(dirH, &data)) {
1213 nt_close(linkHandle.fd_fd);
1214 DeleteZLCFiles(basePath);
1216 /* Then why does this directory exist? Blow it away. */
1217 nt_RemoveDataDirectories(dirIH);
1223 /* The name begins with "Vol_" and ends with .data. See nt_HandleToVolDir() */
1225 DecodeVolumeName(char *name, afs_uint32 *vid)
1233 if (strncmp(name, "Vol_", 4))
1235 if (strcmp(name + len - 5, ".data"))
1238 stmp[len - 5] = '\0';
1239 *vid = base32_to_int(stmp + 4);
1243 /* Recall that the name beings with a "V_" */
1245 DecodeInodeName(char *name, int *p1, int *p2)
1250 (void)strcpy(stmp, name);
1251 s = strrchr(stmp, '_');
1255 t = strrchr(s, '.');
1260 *p1 = base32_to_int(s);
1261 *p2 = base32_to_int(t + 1);
1268 * returns a static string used to print either 32 or 64 bit inode numbers.
1271 PrintInode(char *s, Inode ino)
1273 static afs_ino_str_t result;
1277 (void)sprintf((char *)s, "%I64u", ino);
1283 /* Routines to facilitate removing zero link count files. */
1284 #define MAX_ZLC_NAMES 32
1285 #define MAX_ZLC_NAMELEN 16
1286 typedef struct zlcList_s {
1287 struct zlcList_s *zlc_next;
1289 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1292 static zlcList_t *zlcAnchor = NULL;
1293 static zlcList_t *zlcCur = NULL;
1296 AddToZLCDeleteList(char dir, char *name)
1298 assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1300 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1301 if (zlcCur && zlcCur->zlc_next)
1302 zlcCur = zlcCur->zlc_next;
1304 zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
1310 zlcCur->zlc_next = tmp;
1314 zlcCur->zlc_next = NULL;
1318 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1323 DeleteZLCFiles(char *path)
1329 for (z = zlcAnchor; z; z = z->zlc_next) {
1330 for (i = 0; i < z->zlc_n; i++) {
1331 (void)sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1332 if (nt_unlink(fname) < 0) {
1333 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1337 z->zlc_n = 0; /* Can reuse space. */
1350 tnext = i->zlc_next;
1354 zlcCur = zlcAnchor = NULL;
1357 #endif /* AFS_NT40_ENV */