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 Unix open by name (namei) interface. */
12 #include <afsconfig.h>
13 #include <afs/param.h>
37 #include <sys/param.h>
40 #include <afs/afs_assert.h>
43 #include <afs/afsutil.h>
46 #include <afs/afsint.h>
50 #include "viceinode.h"
52 #include "partition.h"
54 #include "volume_inline.h"
56 #include <afs/errors.h>
58 #include <afs/errmap_nt.h>
61 /*@+fcnmacros +macrofcndecl@*/
64 #endif /*S_SPLINT_S */
65 #define afs_stat stat64
66 #define afs_fstat fstat64
68 #define afs_open nt_open
70 #define afs_open open64
72 #define afs_fopen fopen64
73 #else /* !O_LARGEFILE */
75 #endif /*S_SPLINT_S */
77 #define afs_fstat fstat
79 #define afs_open nt_open
83 #define afs_fopen fopen
84 #endif /* !O_LARGEFILE */
85 /*@=fcnmacros =macrofcndecl@*/
88 #define LOCK_SH 1 /* shared lock */
89 #define LOCK_EX 2 /* exclusive lock */
90 #define LOCK_NB 4 /* don't block when locking */
91 #define LOCK_UN 8 /* unlock */
96 #include <afs/work_queue.h>
97 #include <afs/thread_pool.h>
98 #include <vol/vol-salvage.h>
101 #if !defined(HAVE_FLOCK) && !defined(AFS_NT40_ENV)
105 * This function emulates a subset of flock()
108 emul_flock(int fd, int cmd)
111 memset(&f, 0, sizeof (f));
120 return fcntl(fd, (cmd & LOCK_NB) ? F_SETLK : F_SETLKW, &f);
123 #define flock(f,c) emul_flock(f,c)
130 namei_iread(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
139 nBytes = FDH_PREAD(fdP, buf, size, offset);
145 namei_iwrite(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
154 nBytes = FDH_PWRITE(fdP, buf, size, offset);
162 /* Inode number format:
163 * low 32 bits - if a regular file or directory, the vnode; else the type.
164 * 32-36 - uniquifier tag and index into counts array for this vnode. Only
165 * two of the available bits are currently used. The rest are
166 * present in case we ever increase the number of types of volumes
167 * in the volume group.
168 * bit 37 : 1 == special, 0 == regular
170 # define NAMEI_VNODEMASK 0x00ffffffff
171 # define NAMEI_TAGSHIFT 32
172 # define NAMEI_INODESPECIAL 0x2000000000
173 # define NAMEI_SPECDIR "R"
174 # define NAMEI_SPECDIRC 'R'
175 #else /* !AFS_NT40_ENV */
176 /* Inode number format:
177 * low 26 bits - vnode number - all 1's if volume special file.
179 * next 3 bits spare (0's)
180 * high 32 bits - uniquifier (regular) or type if spare
182 # define NAMEI_VNODEMASK 0x003ffffff
183 # define NAMEI_TAGSHIFT 26
184 # define NAMEI_UNIQMASK 0xffffffff
185 # define NAMEI_UNIQSHIFT 32
186 # define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
187 /* dir1 is the high 8 bits of the 26 bit vnode */
188 # define VNO_DIR1(vno) ((vno >> 14) & 0xff)
189 /* dir2 is the next 9 bits */
190 # define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
191 /* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
192 # define NAMEI_SPECDIR "special"
193 #endif /* !AFS_NT40_ENV */
194 #define NAMEI_TAGMASK 0x7
195 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
197 #define NAMEI_SPECDIRLEN (sizeof(NAMEI_SPECDIR)-1)
199 #define NAMEI_MAXVOLS 5 /* Maximum supported number of volumes per volume
200 * group, not counting temporary (move) volumes.
201 * This is the number of separate files, all having
202 * the same vnode number, which can occur in a volume
214 static int GetFreeTag(IHandle_t * ih, int vno);
216 /* namei_HandleToInodeDir
218 * Construct the path name of the directory holding the inode data.
219 * Format: /<vicepx>/INODEDIR
224 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
226 memset(name, '\0', sizeof(*name));
227 nt_DevToDrive(name->n_drive, ih->ih_dev);
228 strlcpy(name->n_path, name->n_drive, sizeof(name->n_path));
232 /* Format: /<vicepx>/INODEDIR */
233 #define PNAME_BLEN 64
235 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
239 memset(name, '\0', sizeof(*name));
242 * Add the /vicepXX string to the start of name->n_base and then calculate
243 * offset as the number of bytes we know we added.
245 * FIXME: This embeds knowledge of the vice partition naming scheme and
246 * mapping from device numbers. There needs to be an API that tells us
249 volutil_PartitionName_r(ih->ih_dev, name->n_base, sizeof(name->n_base));
250 offset = VICE_PREFIX_SIZE + (ih->ih_dev > 25 ? 2 : 1);
251 name->n_base[offset] = OS_DIRSEPC;
253 strlcpy(name->n_base + offset, INODEDIR, sizeof(name->n_base) - offset);
254 strlcpy(name->n_path, name->n_base, sizeof(name->n_path));
258 #define addtoname(N, C) \
260 if ((N)->n_path[strlen((N)->n_path)-1] != OS_DIRSEPC) \
261 strlcat((N)->n_path, OS_DIRSEP, sizeof((N)->n_path)); \
262 strlcat((N)->n_path, (C), sizeof((N)->n_path)); \
268 namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
270 /* X:\Vol_XXXXXXX.data */
274 namei_HandleToInodeDir(name, ih);
275 /* nt_drive added to name by namei_HandleToInodeDir() */
276 namep = name->n_voldir;
277 (void)memcpy(namep, "\\Vol_", 5);
279 (void)strcpy(namep, int_to_base32(str1, ih->ih_vid));
280 namep += strlen(namep);
281 memcpy(namep, ".data", 5);
284 addtoname(name, name->n_voldir);
288 namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
292 namei_HandleToInodeDir(name, ih);
293 (void)int32_to_flipbase64(tmp, (int64_t) (ih->ih_vid & 0xff));
294 strlcpy(name->n_voldir1, tmp, sizeof(name->n_voldir1));
295 addtoname(name, name->n_voldir1);
296 (void)int32_to_flipbase64(tmp, (int64_t) ih->ih_vid);
297 strlcpy(name->n_voldir2, tmp, sizeof(name->n_voldir2));
298 addtoname(name, name->n_voldir2);
302 /* namei_HandleToName
304 * Constructs a file name for the fully qualified handle.
307 /* Note that special files end up in X:\Vol_XXXXXXX.data\R */
309 namei_HandleToName(namei_t * name, IHandle_t * ih)
311 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
312 int tag = (int)((ih->ih_ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
315 namei_HandleToVolDir(name, ih);
317 if (vno == NAMEI_VNODESPECIAL) {
318 name->n_dir[0] = 'R';
321 name->n_dir[0] = 'Q';
323 name->n_dir[0] = ((vno & 0x1f) >> 1) + 'A';
326 name->n_dir[1] = '\0';
327 addtoname(name, name->n_dir);
328 /* X:\Vol_XXXXXXX.data\X\V_XXXXXXX.XXX */
329 namep = name->n_inode;
330 (void)memcpy(namep, "\\V_", 3);
332 (void)strcpy(namep, int_to_base32(str1, vno));
333 namep += strlen(namep);
335 (void)strcpy(namep, int_to_base32(str1, tag));
336 namep += strlen(namep);
337 addtoname(name, name->n_inode);
340 /* Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special */
342 namei_HandleToName(namei_t * name, IHandle_t * ih)
344 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
347 namei_HandleToVolDir(name, ih);
349 if (vno == NAMEI_VNODESPECIAL) {
350 strlcpy(name->n_dir1, NAMEI_SPECDIR, sizeof(name->n_dir1));
351 addtoname(name, name->n_dir1);
352 name->n_dir2[0] = '\0';
354 (void)int32_to_flipbase64(str, VNO_DIR1(vno));
355 strlcpy(name->n_dir1, str, sizeof(name->n_dir1));
356 addtoname(name, name->n_dir1);
357 (void)int32_to_flipbase64(str, VNO_DIR2(vno));
358 strlcpy(name->n_dir2, str, sizeof(name->n_dir2));
359 addtoname(name, name->n_dir2);
361 (void)int64_to_flipbase64(str, (int64_t) ih->ih_ino);
362 strlcpy(name->n_inode, str, sizeof(name->n_inode));
363 addtoname(name, name->n_inode);
368 /* The following is a warning to tell sys-admins to not muck about in this
371 #define VICE_README "These files and directories are a part of the AFS \
372 namespace. Modifying them\nin any way will result in loss of AFS data,\n\
373 ownership and permissions included.\n"
375 namei_ViceREADME(char *partition)
380 /* Create the inode directory if we're starting for the first time */
381 (void)afs_snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s", partition,
383 mkdir(filename, 0700);
385 (void)afs_snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s" OS_DIRSEP "README",
386 partition, INODEDIR);
387 fd = afs_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
389 (void)write(fd, VICE_README, strlen(VICE_README));
396 /* namei_CreateDataDirectories
398 * If creating the file failed because of ENOENT or ENOTDIR, try
399 * creating all the directories first.
403 namei_CreateDataDirectories(namei_t * name, int *created)
410 afs_snprintf(tmp, 256, "%s\\%s", name->n_drive, name->n_voldir);
412 if (mkdir(tmp) < 0) {
423 for (i = 'A'; i <= 'R'; i++) {
425 if (mkdir(tmp) < 0 && errno != EEXIST)
431 #define create_dir() \
433 if (mkdir(tmp, 0700)<0) { \
434 if (errno != EEXIST) \
442 #define create_nextdir(A) \
444 strcat(tmp, "/"); strcat(tmp, A); create_dir(); \
448 namei_CreateDataDirectories(namei_t * name, int *created)
454 strlcpy(tmp, name->n_base, sizeof(tmp));
457 create_nextdir(name->n_voldir1);
458 create_nextdir(name->n_voldir2);
459 create_nextdir(name->n_dir1);
460 if (name->n_dir2[0]) {
461 create_nextdir(name->n_dir2);
468 /* delTree(): Deletes an entire tree of directories (no files)
470 * root : Full path to the subtree. Should be big enough for PATH_MAX
471 * tree : the subtree to be deleted is rooted here. Specifies only the
472 * subtree beginning at tree (not the entire path). It should be
473 * a pointer into the "root" buffer.
475 * errp : errno of the first error encountered during the directory cleanup.
476 * *errp should have been initialized to 0.
479 * -1 : If errors were encountered during cleanup and error is set to
483 * If there are errors, we try to work around them and delete as many
484 * directories as possible. We don't attempt to remove directories that still
485 * have non-dir entries in them.
488 delTree(char *root, char *tree, int *errp)
496 /* delete the children first */
497 cp = strchr(tree, '/');
499 delTree(root, cp + 1, errp);
502 cp = tree + strlen(tree); /* move cp to the end of string tree */
504 /* now delete all entries in this dir */
505 if ((ds = opendir(root)) != (DIR *) NULL) {
507 while ((dirp = readdir(ds))) {
508 /* ignore . and .. */
509 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
511 /* since root is big enough, we reuse the space to
512 * concatenate the dirname to the current tree
515 strcat(root, dirp->d_name);
516 if (afs_stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
517 /* delete this subtree */
518 delTree(root, cp + 1, errp);
520 *errp = *errp ? *errp : errno;
522 /* recover path to our cur tree by truncating it to
527 /* if (!errno) -- closedir not implicit if we got an error */
531 /* finally axe the current dir */
533 *errp = *errp ? *errp : errno;
535 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
537 #endif /* !AFS_PTHREAD_ENV */
542 /* if we encountered errors during cleanup, we return a -1 */
551 /* namei_RemoveDataDirectories
553 * Returns 0 on success.
554 * Returns -1 on error. Typically, callers ignore this error because we
555 * can continue running if the removes fail. The salvage process will
556 * finish tidying up for us.
561 namei_RemoveDataDirectories(namei_t * name)
568 afs_snprintf(tmp, 256, "%s\\%s", name->n_drive, name->n_voldir);
571 path += strlen(path);
574 for (i = 'A'; i <= 'R'; i++) {
576 if (rmdir(name->n_path) < 0 && errno != ENOENT)
581 /* Delete the Vol_NNNNNN.data directory. */
584 if (rmdir(name->n_path) < 0 && errno != ENOENT) {
592 * We only use the n_base and n_voldir1 entries
593 * and only do rmdir's.
596 namei_RemoveDataDirectories(namei_t * name)
600 int prefixlen = strlen(name->n_base), err = 0;
601 int vollen = strlen(name->n_voldir1);
602 char pbuf[MAXPATHLEN];
606 strlcpy(path, name->n_path, sizeof(pbuf));
608 /* move past the prefix and n_voldir1 */
609 path = path + prefixlen + 1 + vollen + 1; /* skip over the trailing / */
611 /* now delete all dirs upto path */
612 code = delTree(pbuf, path, &err);
614 /* We've now deleted everything under /n_base/n_voldir1/n_voldir2 that
615 * we could. Do not delete /n_base/n_voldir1, since doing such might
616 * interrupt another thread trying to create a volume. We could introduce
617 * some locking to make this safe (or only remove it for whole-partition
618 * salvages), but by not deleting it we only leave behind a maximum of
619 * 256 empty directories. So at least for now, don't bother. */
624 /* Create the file in the name space.
626 * Parameters stored as follows:
628 * p1 - volid - implied in containing directory.
629 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
630 * p3 - uniq -- bits 4-0 are in mode bits 4-0
631 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
633 * p1 - volid - creation time - dwHighDateTime
634 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
635 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
636 * p4 - parid - parent volume id - implied in containing directory.
638 * Return value is the inode number or (Inode)-1 if error.
639 * We "know" there is only one link table, so return EEXIST if there already
640 * is a link table. It's up to the calling code to test errno and increment
646 * This function is called by VCreateVolume to hide the implementation
647 * details of the inode numbers. This only allows for 7 volume special
648 * types, but if we get that far, this could should be dead by then.
651 namei_MakeSpecIno(int volid, int type)
654 ino = NAMEI_INODESPECIAL;
658 type &= NAMEI_TAGMASK;
659 ino |= ((Inode) type) << NAMEI_TAGSHIFT;
660 ino |= ((Inode) volid) << NAMEI_UNIQSHIFT;
665 /* SetOGM - set owner group and mode bits from parm and tag */
667 SetOGM(FD_t fd, int parm, int tag)
671 * owner - low 15 bits of parm.
672 * group - next 15 bits of parm.
673 * mode - 2 bits of parm, then lowest = 3 bits of tag.
675 int owner, group, mode;
677 owner = parm & 0x7fff;
678 group = (parm >> 15) & 0x7fff;
679 if (fchown(fd, owner, group) < 0)
682 mode = (parm >> 27) & 0x18;
684 if (fchmod(fd, mode) < 0)
692 CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
694 WIN32_FIND_DATA info;
698 FindFirstFileEx(name->n_path, FindExInfoStandard, &info,
699 FindExSearchNameMatch, NULL,
700 FIND_FIRST_EX_CASE_SENSITIVE);
703 return -1; /* Can't get info, leave alone */
707 if (info.ftCreationTime.dwHighDateTime != (unsigned int)p1)
713 /* GetOGM - get parm and tag from owner, group and mode bits. */
715 GetOGMFromStat(struct afs_stat *status, int *parm, int *tag)
717 *parm = status->st_uid | (status->st_gid << 15);
718 *parm |= (status->st_mode & 0x18) << 27;
719 *tag = status->st_mode & 0x7;
723 CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
725 struct afs_stat status;
727 if (afs_fstat(fdP->fd_fd, &status) < 0)
730 GetOGMFromStat(&status, &parm, &tag);
738 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
740 /* Derive the name and create it O_EXCL. If that fails we have an error.
741 * Get the tag from a free column in the link table.
745 namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
748 FD_t fd = INVALID_FD;
759 memset((void *)&tmp, 0, sizeof(IHandle_t));
760 memset(&tfd, 0, sizeof(FdHandle_t));
762 tmp.ih_dev = nt_DriveToDev(part);
763 if (tmp.ih_dev == -1) {
769 /* Parameters for special file:
770 * p1 - volume id - goes into owner/group/mode
773 * p4 - parent volume id
775 ftime.dwHighDateTime = p1;
776 ftime.dwLowDateTime = p2;
778 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
779 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
781 int vno = p2 & NAMEI_VNODEMASK;
782 /* Parameters for regular file:
796 tmp.ih_ino = (Inode) p2;
797 ftime.dwHighDateTime = p3;
798 ftime.dwLowDateTime = p4;
801 namei_HandleToName(&name, &tmp);
802 p = strrchr((char *)&name.n_path, '.');
804 for (i = 0; i < NAMEI_MAXVOLS; i++) {
805 *p = *int_to_base32(str1, i);
806 fd = nt_open((char *)&name.n_path, O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0666);
807 if (fd != INVALID_FD)
809 if (p2 == -1 && p3 == VI_LINKTABLE)
812 if (fd == INVALID_FD) {
816 tmp.ih_ino &= ~((Inode) NAMEI_TAGMASK << NAMEI_TAGSHIFT);
817 tmp.ih_ino |= ((Inode) i << NAMEI_TAGSHIFT);
820 if (!SetFileTime((HANDLE) fd, &ftime, NULL, NULL)) {
821 errno = OS_ERROR(EBADF);
828 if (fd == INVALID_FD) {
830 code = nt_unlink((char *)&name.n_path);
839 code = namei_SetLinkCount(fdP, tmp.ih_ino, 1, 0);
841 } else if (p2 == -1 && p3 == VI_LINKTABLE) {
842 if (fd == INVALID_FD)
844 /* hack at tmp to setup for set link count call. */
846 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
851 if (fd != INVALID_FD)
854 if (code || (fd == INVALID_FD)) {
858 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
864 int save_errno = errno;
865 namei_RemoveDataDirectories(&name);
869 return (code || (fd == INVALID_FD)) ? (Inode) - 1 : tmp.ih_ino;
873 namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
885 memset((void *)&tmp, 0, sizeof(IHandle_t));
886 memset(&tfd, 0, sizeof(FdHandle_t));
888 tmp.ih_dev = volutil_GetPartitionID(part);
889 if (tmp.ih_dev == -1) {
895 /* Parameters for special file:
896 * p1 - volume id - goes into owner/group/mode
899 * p4 - parent volume id
904 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
905 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
907 int vno = p2 & NAMEI_VNODEMASK;
908 /* Parameters for regular file:
920 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
921 tag = GetFreeTag(lh, p2);
926 tmp.ih_ino = (Inode) p2;
927 /* name is <uniq(p3)><tag><vno(p2)> */
928 tmp.ih_ino |= ((Inode) tag) << NAMEI_TAGSHIFT;
929 tmp.ih_ino |= ((Inode) p3) << NAMEI_UNIQSHIFT;
934 namei_HandleToName(&name, &tmp);
935 fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
937 if (errno == ENOTDIR || errno == ENOENT) {
938 if (namei_CreateDataDirectories(&name, &created_dir) < 0)
940 fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR,
948 if (SetOGM(fd, ogm_parm, tag) < 0) {
954 if (p2 == -1 && p3 == VI_LINKTABLE) {
955 /* hack at tmp to setup for set link count call. */
956 memset((void *)&tfd, 0, sizeof(FdHandle_t)); /* minimalistic still, but a little cleaner */
959 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
967 if (code || (fd < 0)) {
971 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
976 return (code || (fd < 0)) ? (Inode) - 1 : tmp.ih_ino;
982 namei_iopen(IHandle_t * h)
987 /* Convert handle to file name. */
988 namei_HandleToName(&name, h);
989 fd = afs_open((char *)&name.n_path, O_RDWR, 0666);
993 /* Need to detect vol special file and just unlink. In those cases, the
994 * handle passed in _is_ for the inode. We only check p1 for the special
998 namei_dec(IHandle_t * ih, Inode ino, int p1)
1005 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1007 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1009 /* Verify this is the right file. */
1010 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
1012 namei_HandleToName(&name, tmp);
1017 errno = OS_ERROR(ENOENT);
1021 if (CheckOGM(&name, fdP, p1) < 0) {
1022 FDH_REALLYCLOSE(fdP);
1024 errno = OS_ERROR(EINVAL);
1028 /* If it's the link table itself, decrement the link count. */
1029 if (type == VI_LINKTABLE) {
1030 if ((count = namei_GetLinkCount(fdP, (Inode) 0, 1, 0, 1)) < 0) {
1031 FDH_REALLYCLOSE(fdP);
1037 if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) <
1039 FDH_REALLYCLOSE(fdP);
1045 FDH_REALLYCLOSE(fdP);
1051 if ((code = OS_UNLINK(name.n_path)) == 0) {
1052 if (type == VI_LINKTABLE) {
1053 /* Try to remove directory. If it fails, that's ok.
1054 * Salvage will clean up.
1056 (void)namei_RemoveDataDirectories(&name);
1059 FDH_REALLYCLOSE(fdP);
1062 /* Get a file descriptor handle for this Inode */
1068 if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0) {
1069 FDH_REALLYCLOSE(fdP);
1075 if (namei_SetLinkCount(fdP, ino, count, 1) < 0) {
1076 FDH_REALLYCLOSE(fdP);
1081 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
1082 Log("Warning: Lost ref on ihandle dev %d vid %d ino %" AFS_INT64_FMT "\n",
1083 th->ih_dev, th->ih_vid, (afs_int64)th->ih_ino);
1086 /* If we're less than 0, someone presumably unlinked;
1087 don't bother setting count to 0, but we need to drop a lock */
1088 if (namei_SetLinkCount(fdP, ino, 0, 1) < 0) {
1089 FDH_REALLYCLOSE(fdP);
1095 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
1097 namei_HandleToName(&name, th);
1099 code = OS_UNLINK(name.n_path);
1108 namei_inc(IHandle_t * h, Inode ino, int p1)
1114 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1115 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1116 if (type != VI_LINKTABLE)
1121 /* Get a file descriptor handle for this Inode */
1127 if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0)
1132 errno = OS_ERROR(EINVAL);
1136 if (namei_SetLinkCount(fdP, ino, count, 1) < 0)
1140 FDH_REALLYCLOSE(fdP);
1147 #ifndef AFS_NT40_ENV
1149 namei_replace_file_by_hardlink(IHandle_t *hLink, IHandle_t *hTarget)
1155 /* Convert handle to file name. */
1156 namei_HandleToName(&nameLink, hLink);
1157 namei_HandleToName(&nameTarget, hTarget);
1159 OS_UNLINK(nameLink.n_path);
1160 code = link(nameTarget.n_path, nameLink.n_path);
1165 namei_copy_on_write(IHandle_t *h)
1167 afs_int32 fd, code = 0;
1170 struct afs_stat tstat;
1173 namei_HandleToName(&name, h);
1174 if (afs_stat(name.n_path, &tstat) < 0)
1176 if (tstat.st_nlink > 1) { /* do a copy on write */
1185 afs_snprintf(path, sizeof(path), "%s-tmp", name.n_path);
1186 fd = afs_open(path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
1198 size = tstat.st_size;
1201 tlen = size > 8192 ? 8192 : size;
1202 if (FDH_PREAD(fdP, buf, tlen, offset) != tlen)
1204 if (write(fd, buf, tlen) != tlen)
1210 FDH_REALLYCLOSE(fdP);
1215 OS_UNLINK(name.n_path);
1216 code = rename(path, name.n_path);
1223 /************************************************************************
1224 * File Name Structure
1225 ************************************************************************
1227 * Each AFS file needs a unique name and it needs to be findable with
1228 * minimal lookup time. Note that the constraint on the number of files and
1229 * directories in a volume is the size of the vnode index files and the
1230 * max file size AFS supports (for internal files) of 2^31. Since a record
1231 * in the small vnode index file is 64 bytes long, we can have at most
1232 * (2^31)/64 or 33554432 files. A record in the large index file is
1233 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
1234 * Another layout parameter is that there is roughly a 16 to 1 ratio between
1235 * the number of files and the number of directories.
1237 * Using this information we can see that a layout of 256 directories, each
1238 * with 512 subdirectories and each of those having 512 files gives us
1239 * 256*512*512 = 67108864 AFS files and directories.
1241 * The volume, vnode, uniquifier and data version, as well as the tag
1242 * are required, either for finding the file or for salvaging. It's best to
1243 * restrict the name to something that can be mapped into 64 bits so the
1244 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
1245 * is used to distinguish between different versions of the same file
1246 * which are currently in the RW and clones of a volume. See "Link Table
1247 * Organization" below for more information on the tag. The tag is
1248 * required in the name of the file to ensure a unique name.
1250 * ifdef AFS_NT40_ENV
1251 * The data for each volume group is in a separate directory. The name of the
1252 * volume is of the form: Vol_NNNNNN.data, where NNNNNN is a base 32
1253 * representation of the RW volume ID (even where the RO is the only volume
1254 * on the partition). Below that are separate subdirectories for the
1255 * AFS directories and special files. There are also 16 directories for files,
1256 * hashed on the low 5 bits (recall bit0 is always 0) of the vnode number.
1257 * These directories are named:
1258 * A - P - 16 file directories.
1259 * Q ----- data directory
1260 * R ----- special files directory
1262 * The vnode is hashed into the directory using the low bits of the
1265 * The format of a file name for a regular file is:
1266 * Y:\Vol_NNNNNN.data\X\V_IIIIII.J
1267 * Y - partition encoded as drive letter, starting with D
1268 * NNNNNN - base 32 encoded volume number of RW volume
1269 * X - hash directory, as above
1270 * IIIIII - base 32 encoded vnode number
1271 * J - base 32 encoded tag
1273 * uniq is stored in the dwHighDateTime creation time field
1274 * dv is stored in the dwLowDateTime creation time field
1276 * Special inodes are always in the R directory, as above, and are
1278 * True child volid is stored in the dwHighDateTime creation time field
1279 * vnode number is always -1 (Special)
1280 * type is the IIIIII part of the filename
1281 * uniq is the J part of the filename
1282 * parent volume id is implied in the containing directory
1285 * We can store data in the uid, gid and mode bits of the files, provided
1286 * the directories have root only access. This gives us 15 bits for each
1287 * of uid and gid (GNU chown considers 65535 to mean "don't change").
1288 * There are 9 available mode bits. Adn we need to store a total of
1289 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
1290 * or 131 bits somewhere.
1292 * The format of a file name for a regular file is:
1293 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
1294 * V1 - low 8 bits of RW volume id
1295 * V2 - all bits of RW volume id
1296 * AA - high 8 bits of vnode number.
1297 * BB - next 9 bits of vnode number.
1298 * <tag><uniq><vno> - file name
1300 * Volume special files are stored in a separate directory:
1301 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
1304 * The vnode is hashed into the directory using the high bits of the
1305 * vnode number. This is so that consecutively created vnodes are in
1306 * roughly the same area on the disk. This will at least be optimal if
1307 * the user is creating many files in the same AFS directory. The name
1308 * should be formed so that the leading characters are different as quickly
1309 * as possible, leading to faster discards of incorrect matches in the
1317 /************************************************************************
1318 * Link Table Organization
1319 ************************************************************************
1321 * The link table volume special file is used to hold the link counts that
1322 * are held in the inodes in inode based AFS vice filesystems. For user
1323 * space access, the link counts are being kept in a separate
1324 * volume special file. The file begins with the usual version stamp
1325 * information and is then followed by one row per vnode number. vnode 0
1326 * is used to hold the link count of the link table itself. That is because
1327 * the same link table is shared among all the volumes of the volume group
1328 * and is deleted only when the last volume of a volume group is deleted.
1330 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
1331 * link count from 0 through 7. Each colume represents a unique instance of
1332 * that vnode. Say we have a file shared between the RW and a RO and a
1333 * different version of the file (or a different uniquifer) for the BU volume.
1334 * Then one column would be holding the link count of 2 for the RW and RO
1335 * and a different column would hold the link count of 1 for the BU volume.
1336 * # ifdef AFS_NT40_ENV
1337 * The column used is determined for NT by the uniquifier tag applied to
1338 * generate a unique file name in the NTFS namespace. The file name is
1339 * of the form "V_<vno>.<tag>" . And the <tag> is also the column number
1340 * in the link table.
1342 * Note that we allow only 5 volumes per file, giving 15 bits used in the
1346 #define LINKTABLE_WIDTH 2
1347 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
1350 * compute namei link table file and bit offset from inode number.
1352 * @param[in] ino inode number
1353 * @param[out] offset link table file offset
1354 * @param[out] index bit offset within 2-byte record
1359 namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
1361 int toff = (int)(ino & NAMEI_VNODEMASK);
1362 int tindex = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1364 *offset = (afs_foff_t) ((toff << LINKTABLE_SHIFT) + 8); /* * 2 + sizeof stamp */
1365 *index = (tindex << 1) + tindex;
1368 #ifdef AFS_PTHREAD_ENV
1369 /* XXX do static initializers work for WINNT/pthread? */
1370 pthread_mutex_t _namei_glc_lock = PTHREAD_MUTEX_INITIALIZER;
1371 #define NAMEI_GLC_LOCK MUTEX_ENTER(&_namei_glc_lock)
1372 #define NAMEI_GLC_UNLOCK MUTEX_EXIT(&_namei_glc_lock)
1373 #else /* !AFS_PTHREAD_ENV */
1374 #define NAMEI_GLC_LOCK
1375 #define NAMEI_GLC_UNLOCK
1376 #endif /* !AFS_PTHREAD_ENV */
1379 * get the link count of an inode.
1381 * @param[in] h namei link count table file handle
1382 * @param[in] ino inode number for which we are requesting a link count
1383 * @param[in] lockit if asserted, return with lock held on link table file
1384 * @param[in] fixup if asserted, write 1 to link count when read() returns
1386 * @param[in] nowrite return success on zero byte read or ZLC
1388 * @post if lockit asserted and lookup was successful, will return with write
1389 * lock on link table file descriptor
1391 * @return link count
1392 * @retval -1 namei link table i/o error
1397 namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite)
1399 unsigned short row = 0;
1404 /* there's no linktable yet. the salvager will create one later */
1405 if (h->fd_fd == INVALID_FD && fixup)
1407 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1410 if (FDH_LOCKFILE(h, offset) != 0)
1414 rc = FDH_PREAD(h, (char*)&row, sizeof(row), offset);
1415 if ((rc == 0 || !((row >> index) & NAMEI_TAGMASK)) && fixup && nowrite)
1417 if (rc == 0 && fixup) {
1419 * extend link table and write a link count of 1 for ino
1421 * in order to make MT-safe, truncation (extension really)
1422 * must happen under a mutex
1425 if (FDH_SIZE(h) >= offset+sizeof(row)) {
1427 goto bad_getLinkByte;
1429 FDH_TRUNC(h, offset+sizeof(row));
1431 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
1434 if (rc != sizeof(row)) {
1435 goto bad_getLinkByte;
1438 if (fixup && !((row >> index) & NAMEI_TAGMASK)) {
1442 * in order to make this mt-safe, we need to do the read-modify-write
1443 * under a mutex. thus, we repeat the read inside the lock.
1446 rc = FDH_PREAD(h, (char *)&row, sizeof(row), offset);
1447 if (rc == sizeof(row)) {
1449 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
1452 if (rc != sizeof(row))
1453 goto bad_getLinkByte;
1456 return (int)((row >> index) & NAMEI_TAGMASK);
1460 FDH_UNLOCKFILE(h, offset);
1465 namei_SetNonZLC(FdHandle_t * h, Inode ino)
1467 return namei_GetLinkCount(h, ino, 0, 1, 0);
1470 /* Return a free column index for this vnode. */
1472 GetFreeTag(IHandle_t * ih, int vno)
1486 /* Only one manipulates at a time. */
1487 if (FDH_LOCKFILE(fdP, offset) != 0) {
1488 FDH_REALLYCLOSE(fdP);
1492 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
1494 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1495 if (nBytes != sizeof(row)) {
1501 /* Now find a free column in this row and claim it. */
1502 for (col = 0; col < NAMEI_MAXVOLS; col++) {
1503 coldata = 7 << (col * 3);
1504 if ((row & coldata) == 0)
1507 if (col >= NAMEI_MAXVOLS) {
1512 coldata = 1 << (col * 3);
1515 if (FDH_PWRITE(fdP, (char *)&row, sizeof(row), offset) != sizeof(row)) {
1519 FDH_UNLOCKFILE(fdP, offset);
1520 FDH_REALLYCLOSE(fdP);
1524 FDH_UNLOCKFILE(fdP, offset);
1525 FDH_REALLYCLOSE(fdP);
1531 /* namei_SetLinkCount
1532 * If locked is set, assume file is locked. Otherwise, lock file before
1533 * proceeding to modify it.
1536 namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
1542 ssize_t nBytes = -1;
1544 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1547 if (FDH_LOCKFILE(fdP, offset) != 0) {
1552 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1553 if (nBytes != sizeof(row)) {
1555 errno = OS_ERROR(EBADF);
1556 goto bad_SetLinkCount;
1561 bytesRead = 7 << index;
1563 row &= (unsigned short)~bytesRead;
1564 row |= (unsigned short)count;
1566 if (FDH_PWRITE(fdP, (char *)&row, sizeof(short), offset) != sizeof(short)) {
1567 errno = OS_ERROR(EBADF);
1568 goto bad_SetLinkCount;
1576 FDH_UNLOCKFILE(fdP, offset);
1578 /* disallowed above 7, so... */
1583 /* ListViceInodes - write inode data to a results file. */
1584 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1585 unsigned int volid);
1586 static int DecodeVolumeName(char *name, unsigned int *vid);
1587 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
1588 int (*write_fun) (FILE *,
1589 struct ViceInodeInfo *,
1590 char *, char *), FILE * fp,
1591 int (*judgeFun) (struct ViceInodeInfo *,
1592 afs_uint32 vid, void *),
1593 afs_uint32 singleVolumeNumber, void *rock);
1598 * Write the inode data to the results file.
1600 * Returns -2 on error, 0 on success.
1602 * This is written as a callback simply so that other listing routines
1603 * can use the same inode reading code.
1606 WriteInodeInfo(FILE * fp, struct ViceInodeInfo *info, char *dir, char *name)
1609 n = fwrite(info, sizeof(*info), 1, fp);
1610 return (n == 1) ? 0 : -2;
1614 int mode_errors; /* Number of errors found in mode bits on directories. */
1616 VerifyDirPerms(char *path)
1618 struct afs_stat status;
1620 if (afs_stat(path, &status) < 0) {
1621 Log("Unable to stat %s. Please manually verify mode bits for this"
1622 " directory\n", path);
1624 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1630 * Fill the results file with the requested inode information.
1632 * This code optimizes single volume salvages by just looking at that one
1633 * volume's directory.
1635 * @param[in] devname device name string
1636 * @param[in] moutnedOn vice partition mount point
1637 * @param[in] resultFile result file in which to write inode
1638 * metadata. If NULL, write routine is not
1640 * @param[in] judgeInode filter function pointer. if not NULL, only
1641 * inodes for which this routine returns non-
1642 * zero will be written to the results file.
1643 * @param[in] singleVolumeNumber volume id filter
1644 * @param[out] forcep always set to 0 for namei impl
1645 * @param[in] forceR not used by namei impl
1646 * @param[in] wpath not used by namei impl
1647 * @param[in] rock opaque pointer passed to judgeInode
1649 * @return operation status
1651 * @retval -1 complete failure, salvage should terminate.
1652 * @retval -2 not enough space on partition, salvager has error message
1656 ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
1657 int (*judgeInode) (struct ViceInodeInfo * info, afs_uint32 vid, void *rock),
1658 afs_uint32 singleVolumeNumber, int *forcep, int forceR, char *wpath,
1662 struct afs_stat status;
1664 *forcep = 0; /* no need to salvage until further notice */
1666 /* Verify protections on directories. */
1668 VerifyDirPerms(mountedOn);
1671 namei_ListAFSFiles(mountedOn, WriteInodeInfo, inodeFile, judgeInode,
1672 singleVolumeNumber, rock);
1681 if (fflush(inodeFile) == EOF) {
1682 Log("Unable to successfully flush inode file for %s\n", mountedOn);
1685 if (fsync(fileno(inodeFile)) == -1) {
1686 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1691 * Paranoia: check that the file is really the right size
1693 if (afs_fstat(fileno(inodeFile), &status) == -1) {
1694 Log("Unable to successfully stat inode file for %s\n", mountedOn);
1697 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
1698 Log("Wrong size (%d instead of %lu) in inode file for %s\n",
1699 (int) status.st_size,
1700 (long unsigned int) ninodes * sizeof(struct ViceInodeInfo),
1709 * Collect all the matching AFS files on the drive.
1710 * If singleVolumeNumber is non-zero, just return files for that volume.
1712 * @param[in] dev vice partition path
1713 * @param[in] writeFun function pointer to a function which
1714 * writes inode information to FILE fp
1715 * @param[in] fp file stream where inode metadata is sent
1716 * @param[in] judgeFun filter function pointer. if not NULL,
1717 * only entries for which a non-zero value
1718 * is returned are written to fp
1719 * @param[in] singleVolumeNumber volume id filter. if nonzero, only
1720 * process files for that specific volume id
1721 * @param[in] rock opaque pointer passed into writeFun and
1724 * @return operation status
1726 * @retval >=0 number of matching files found
1729 namei_ListAFSFiles(char *dev,
1730 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1733 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1734 afs_uint32 singleVolumeNumber, void *rock)
1741 #ifndef AFS_NT40_ENV
1747 static void FreeZLCList(void);
1750 memset((void *)&ih, 0, sizeof(IHandle_t));
1752 ih.ih_dev = nt_DriveToDev(dev);
1754 ih.ih_dev = volutil_GetPartitionID(dev);
1757 if (singleVolumeNumber) {
1758 ih.ih_vid = singleVolumeNumber;
1759 namei_HandleToVolDir(&name, &ih);
1761 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1762 singleVolumeNumber, rock);
1766 /* Find all volume data directories and descend through them. */
1767 namei_HandleToInodeDir(&name, &ih);
1769 dirp1 = opendir(name.n_path);
1772 while ((dp1 = readdir(dirp1))) {
1774 /* Heirarchy is one level on Windows */
1775 if (!DecodeVolumeName(dp1->d_name, &ih.ih_vid)) {
1777 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1781 if (*dp1->d_name == '.')
1783 afs_snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", name.n_path,
1785 dirp2 = opendir(path2);
1787 while ((dp2 = readdir(dirp2))) {
1788 if (*dp2->d_name == '.')
1790 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1792 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1809 static void AddToZLCDeleteList(char dir, char *name);
1810 static void DeleteZLCFiles(char *path);
1814 * examine a namei volume special file.
1816 * @param[in] path1 volume special directory path
1817 * @param[in] dname directory entry name
1818 * @param[in] myIH inode handle to volume directory
1819 * @param[out] linkHandle namei link count fd handle. if
1820 * the inode in question is the link
1821 * table, then the FdHandle is populated
1822 * @param[in] writeFun metadata write function pointer
1823 * @param[in] fp file pointer where inode metadata
1824 * is written by (*writeFun)()
1825 * @param[in] judgeFun inode filter function pointer. if
1826 * not NULL, only inodes for which this
1827 * function returns non-zero are recorded
1828 * into fp by writeFun
1829 * @param[in] singleVolumeNumer volume id filter. if non-zero, only
1830 * inodes associated with this volume id
1831 * are recorded by writeFun
1832 * @param[in] rock opaque pointer passed to writeFun and
1835 * @return operation status
1836 * @retval 1 count this inode
1837 * @retval 0 don't count this inode
1838 * @retval -1 failure
1843 _namei_examine_special(char * path1,
1846 FdHandle_t * linkHandle,
1847 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1850 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1851 int singleVolumeNumber,
1855 struct ViceInodeInfo info;
1856 afs_uint32 inode_vgid;
1858 if (DecodeInode(path1, dname, &info, myIH->ih_vid) < 0) {
1864 inode_vgid = myIH->ih_vid;
1866 inode_vgid = (info.inodeNumber >> NAMEI_UNIQSHIFT) & NAMEI_UNIQMASK;
1869 if (info.u.param[2] != VI_LINKTABLE) {
1871 } else if ((info.u.param[0] != myIH->ih_vid) ||
1872 (inode_vgid != myIH->ih_vid)) {
1873 /* VGID encoded in linktable filename and/or OGM data isn't
1874 * consistent with VGID encoded in namei path */
1875 Log("namei_ListAFSSubDirs: warning: inconsistent linktable "
1876 "filename \"%s" OS_DIRSEP "%s\"; salvager will delete it "
1877 "(dir_vgid=%u, inode_vgid=%u, ogm_vgid=%u)\n",
1878 path1, dname, myIH->ih_vid,
1879 (unsigned int)inode_vgid,
1883 /* Open this handle */
1884 (void)afs_snprintf(path2, sizeof(path2),
1885 "%s" OS_DIRSEP "%s", path1, dname);
1886 linkHandle->fd_fd = afs_open(path2, Testing ? O_RDONLY : O_RDWR, 0666);
1888 namei_GetLinkCount(linkHandle, (Inode) 0, 1, 1, Testing);
1892 (*judgeFun) (&info, singleVolumeNumber, rock)) {
1894 if ((*writeFun) (fp, &info, path1, dname) < 0) {
1904 * examine a namei file.
1906 * @param[in] path1 volume special directory path
1907 * @param[in] dname directory entry name
1908 * @param[in] myIH inode handle to volume directory
1909 * @param[in] linkHandle namei link count fd handle.
1910 * @param[in] writeFun metadata write function pointer
1911 * @param[in] fp file pointer where inode metadata
1912 * is written by (*writeFun)()
1913 * @param[in] judgeFun inode filter function pointer. if
1914 * not NULL, only inodes for which this
1915 * function returns non-zero are recorded
1916 * into fp by writeFun
1917 * @param[in] singleVolumeNumer volume id filter. if non-zero, only
1918 * inodes associated with this volume id
1919 * are recorded by writeFun
1920 * @param[in] rock opaque pointer passed to writeFun and
1923 * @return operation status
1924 * @retval 1 count this inode
1925 * @retval 0 don't count this inode
1926 * @retval -1 failure
1927 * @retval -2 request ZLC delete
1932 _namei_examine_reg(char * path3,
1935 FdHandle_t * linkHandle,
1936 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1939 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1940 int singleVolumeNumber,
1944 struct ViceInodeInfo info;
1946 int i; /* Windows-only (one level hash dir) */
1949 if (DecodeInode(path3, dname, &info, myIH->ih_vid) < 0) {
1954 namei_GetLinkCount(linkHandle,
1955 info.inodeNumber, 1, 1, Testing);
1956 if (info.linkCount == 0) {
1958 Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n", path3, dname);
1959 #ifdef AFS_SALSRV_ENV
1960 /* defer -- the AddToZLCDeleteList() interface is not MT-safe */
1962 #else /* !AFS_SALSRV_ENV */
1963 AddToZLCDeleteList((char)i, dname);
1964 #endif /* !AFS_SALSRV_ENV */
1965 #else /* !DELETE_ZLC */
1966 Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path3,
1973 (*judgeFun) (&info, singleVolumeNumber, rock)) {
1975 if ((*writeFun) (fp, &info, path3, dname) < 0) {
1985 * listsubdirs work queue node.
1987 struct listsubdirs_work_node {
1988 #ifdef AFS_SALSRV_ENV
1989 int *error; /**< *error set if an error was
1990 * encountered in any listsubdirs
1994 IHandle_t * IH; /**< volume directory handle */
1995 FdHandle_t *linkHandle; /**< namei link count fd handle. when
1996 * examinining the link table special
1997 * inode, this will be pointed at the
2000 FILE * fp; /**< file pointer for writeFun */
2002 /** function which will write inode metadata to fp */
2003 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *, char *);
2005 /** inode filter function */
2006 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *);
2007 int singleVolumeNumber; /**< volume id filter */
2008 void * rock; /**< pointer passed to writeFun and judgeFun */
2009 int code; /**< return code from examine function */
2010 int special; /**< asserted when this is a volume
2015 * simple wrapper around _namei_examine_special and _namei_examine_reg.
2017 * @param[in] work the struct listsubdirs_work_node for the associated
2018 * "list subdirs" job
2019 * @param[in] dir the directory to examine
2020 * @param[in] filename the filename in 'dir' to examine
2022 * @return operation status
2023 * @retval 1 count this inode
2024 * @retval 0 don't count this inode
2025 * @retval -1 failure
2028 _namei_examine_file(const struct listsubdirs_work_node *work, char *dir,
2031 if (work->special) {
2032 return _namei_examine_special(dir, filename, work->IH,
2033 work->linkHandle, work->writeFun, work->fp,
2034 work->judgeFun, work->singleVolumeNumber,
2037 return _namei_examine_reg(dir, filename, work->IH,
2038 work->linkHandle, work->writeFun, work->fp,
2039 work->judgeFun, work->singleVolumeNumber,
2045 #ifdef AFS_SALSRV_ENV
2046 /** @addtogroup afs_vol_salsrv_pario */
2050 * arguments for the _namei_examine_file_cbk callback function.
2052 struct listsubdirs_args {
2053 const struct listsubdirs_work_node *work; /**< arguments that are the same
2054 * for all invocations of
2055 * _namei_examine_file_cbk, in
2057 int *result; /**< where we can store the return code of _namei_examine_file */
2059 char dir[512]; /**< directory to examine */
2060 char filename[256]; /**< filename in 'dir' to examine */
2064 * a node in the list of results of listsubdir jobs.
2066 struct listsubdirs_result {
2068 int inodes; /**< return value from _namei_examine_file */
2072 * clean up a list of 'struct listsubdirs_result's and interpret the results.
2074 * @param[in] resultlist a list of 'struct listsubdirs_result's
2076 * @return number of inodes found
2080 _namei_listsubdirs_cleanup_results(struct rx_queue *resultlist)
2082 struct listsubdirs_result *res, *nres;
2085 for(queue_Scan(resultlist, res, nres, listsubdirs_result)) {
2087 /* noop, retain erroneous error code */
2088 } else if (res->inodes < 0) {
2103 * wait for the spawned listsubdirs jobs to finish, and return how many inodes
2106 * @param[in] queue queue to wait to finish
2107 * @param[in] resultlist list of 'struct listsubdirs_result's that the queued
2108 * jobs are storing their results in
2110 * @return number of inodes found
2114 _namei_listsubdirs_wait(struct afs_work_queue *queue, struct rx_queue *resultlist)
2118 code = afs_wq_wait_all(queue);
2123 return _namei_listsubdirs_cleanup_results(resultlist);
2127 * work queue entry point for examining namei files.
2129 * @param[in] queue pointer to struct Vwork_queue
2130 * @param[in] node pointer to struct Vwork_queue_node
2131 * @param[in] queue_rock opaque pointer to struct salsrv_pool_state
2132 * @param[in] node_rock opaque pointer to struct listsubdirs_work_node
2133 * @param[in] caller_rock opaque pointer to struct salsrv_worker_thread_state
2135 * @return operation status
2137 * @see Vwork_queue_callback_func_t
2142 _namei_examine_file_cbk(struct afs_work_queue *queue,
2143 struct afs_work_queue_node *node,
2149 struct listsubdirs_args *args = node_rock;
2150 const struct listsubdirs_work_node * work = args->work;
2151 char *dir = args->dir;
2152 char *filename = args->filename;
2154 code = _namei_examine_file(work, dir, filename);
2156 *(args->result) = code;
2160 /* we've errored, so no point in letting more jobs continue */
2161 afs_wq_shutdown(queue);
2167 static pthread_once_t wq_once = PTHREAD_ONCE_INIT;
2168 static pthread_key_t wq_key;
2171 * create the wq_key key for storing a work queue.
2174 _namei_wq_keycreate(void)
2176 osi_Assert(pthread_key_create(&wq_key, NULL) == 0);
2180 * set the work queue for this thread to use for backgrounding namei ops.
2182 * The work queue will be used in ListAFSSubdirs (called indirectly by
2183 * ListViceInodes) to examine files in parallel.
2185 * @param[in] wq the work queue to use
2188 namei_SetWorkQueue(struct afs_work_queue *wq)
2190 osi_Assert(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
2192 osi_Assert(pthread_setspecific(wq_key, wq) == 0);
2196 * enqueue an examine file work unit.
2198 * @param[in] work the _namei_examine_file arguments that are common to
2199 * all callers within the same ListAFSFiles operation
2200 * @param[in] dir the specific directory to look at (string will be
2201 * copied; can be stack/temporary memory)
2202 * @param[in] filename the filename to look at (string will be copied; can be
2203 * stack/temporary memory)
2204 * @param[in] wq work queue to enqueue this work unit to
2205 * @param[in] resultlist the list to append the 'struct listsubdirs_result' to
2206 * for the enqueued work unit
2208 * @return operation status
2210 * @retval -1 fatal error
2212 * @note errors MUST be indicated by a -1 error code and nothing else, to be
2213 * compatible with _namei_examine_reg and _namei_examine_special
2218 _namei_examine_file_spawn(const struct listsubdirs_work_node *work,
2219 const char *dir, const char *filename,
2220 struct afs_work_queue *wq,
2221 struct rx_queue *resultlist)
2224 struct listsubdirs_args *args = NULL;
2225 struct listsubdirs_result *result = NULL;
2226 struct afs_work_queue_node *node = NULL;
2227 struct afs_work_queue_add_opts opts;
2229 args = malloc(sizeof(*args));
2235 result = malloc(sizeof(*result));
2236 if (result == NULL) {
2241 code = afs_wq_node_alloc(&node);
2246 code = afs_wq_node_set_detached(node);
2253 args->result = &result->inodes;
2254 strlcpy(args->dir, dir, sizeof(args->dir));
2255 strlcpy(args->filename, filename, sizeof(args->filename));
2257 code = afs_wq_node_set_callback(node,
2258 &_namei_examine_file_cbk,
2266 afs_wq_add_opts_init(&opts);
2269 code = afs_wq_add(wq, node, &opts);
2276 queue_Append(resultlist, result);
2281 afs_wq_node_put(node);
2297 #else /* !AFS_SALSRV_ENV */
2298 # define _namei_examine_file_spawn(work, dir, file, wq, resultlist) \
2299 _namei_examine_file(work, dir, file)
2300 #endif /* !AFS_SALSRV_ENV */
2303 * traverse and check inodes.
2305 * @param[in] dirIH volume group directory handle
2306 * @param[in] writeFun function pointer which will write inode
2307 * metadata to FILE stream fp
2308 * @param[in] fp file stream where inode metadata gets
2310 * @param[in] judgeFun inode filter function. if not NULL, only
2311 * inodes for which the filter returns non-zero
2312 * will be written out by writeFun
2313 * @param[in] singleVolumeNumber volume id filter. only inodes matching this
2314 * filter are written out by writeFun
2315 * @param[in] rock opaque pointer passed to judgeFun and writeFun
2317 * @return operation status
2319 * @retval >=0 number of matching inodes found
2321 * @todo the salsrv implementation may consume a lot of
2322 * memory for a large volume. at some point we should
2323 * probably write a background thread to asynchronously
2324 * clean up the resultlist nodes to reduce memory footprint
2329 namei_ListAFSSubDirs(IHandle_t * dirIH,
2330 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
2333 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
2334 afs_uint32 singleVolumeNumber, void *rock)
2336 int code = 0, ret = 0;
2337 IHandle_t myIH = *dirIH;
2339 char path1[512], path3[512];
2340 DIR *dirp1, *dirp2, *dirp3;
2341 #ifndef AFS_NT40_ENV
2345 struct dirent *dp1, *dp3;
2346 FdHandle_t linkHandle;
2348 struct listsubdirs_work_node work;
2349 #ifdef AFS_SALSRV_ENV
2351 struct afs_work_queue *wq;
2353 struct rx_queue resultlist;
2357 static void AddToZLCDeleteList(char dir, char *name);
2358 static void DeleteZLCFiles(char *path);
2361 namei_HandleToVolDir(&name, &myIH);
2362 strlcpy(path1, name.n_path, sizeof(path1));
2364 /* Do the directory containing the special files first to pick up link
2367 (void)strcat(path1, OS_DIRSEP);
2368 (void)strcat(path1, NAMEI_SPECDIR);
2370 linkHandle.fd_fd = INVALID_FD;
2371 #ifdef AFS_SALSRV_ENV
2372 osi_Assert(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
2374 wq = pthread_getspecific(wq_key);
2380 queue_Init(&resultlist);
2383 memset(&work, 0, sizeof(work));
2384 work.linkHandle = &linkHandle;
2387 work.writeFun = writeFun;
2388 work.judgeFun = judgeFun;
2389 work.singleVolumeNumber = singleVolumeNumber;
2392 #ifdef AFS_SALSRV_ENV
2393 work.error = &error;
2396 dirp1 = opendir(path1);
2398 while ((dp1 = readdir(dirp1))) {
2399 if (*dp1->d_name == '.')
2402 #ifdef AFS_SALSRV_ENV
2408 #endif /* AFS_SALSRV_ENV */
2410 code = _namei_examine_file_spawn(&work, path1, dp1->d_name, wq, &resultlist);
2420 /* count this inode */
2421 #ifndef AFS_SALSRV_ENV
2430 #ifdef AFS_SALSRV_ENV
2431 /* effectively a barrier */
2432 code = _namei_listsubdirs_wait(wq, &resultlist);
2433 if (code < 0 || error) {
2441 if (linkHandle.fd_fd == INVALID_FD) {
2442 Log("namei_ListAFSSubDirs: warning: VG %u does not have a link table; "
2443 "salvager will recreate it.\n", dirIH->ih_vid);
2446 /* Now run through all the other subdirs */
2447 namei_HandleToVolDir(&name, &myIH);
2448 strlcpy(path1, name.n_path, sizeof(path1));
2452 dirp1 = opendir(path1);
2454 while ((dp1 = readdir(dirp1))) {
2455 #ifndef AFS_NT40_ENV
2456 if (*dp1->d_name == '.')
2459 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
2462 #ifndef AFS_NT40_ENV /* This level missing on Windows */
2463 /* Now we've got a next level subdir. */
2464 afs_snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", path1, dp1->d_name);
2465 dirp2 = opendir(path2);
2467 while ((dp2 = readdir(dirp2))) {
2468 if (*dp2->d_name == '.')
2471 /* Now we've got to the actual data */
2472 afs_snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s", path2,
2475 /* Now we've got to the actual data */
2476 afs_snprintf(path3, sizeof(path3), "%s\\%s", path1,
2479 dirp3 = opendir(path3);
2481 while ((dp3 = readdir(dirp3))) {
2482 #ifndef AFS_NT40_ENV
2483 if (*dp3->d_name == '.')
2487 #ifdef AFS_SALSRV_ENV
2495 #endif /* AFS_SALSRV_ENV */
2497 code = _namei_examine_file_spawn(&work, path3,
2510 #ifndef AFS_SALSRV_ENV
2518 #ifndef AFS_NT40_ENV /* This level missing on Windows */
2527 #ifdef AFS_SALSRV_ENV
2528 /* effectively a barrier */
2529 code = _namei_listsubdirs_wait(wq, &resultlist);
2530 if (code < 0 || error) {
2540 /* Then why does this directory exist? Blow it away. */
2541 namei_HandleToVolDir(&name, dirIH);
2542 namei_RemoveDataDirectories(&name);
2546 #ifdef AFS_SALSRV_ENV
2548 afs_wq_wait_all(wq);
2550 _namei_listsubdirs_cleanup_results(&resultlist);
2552 if (linkHandle.fd_fd != INVALID_FD)
2553 OS_CLOSE(linkHandle.fd_fd);
2565 DecodeVolumeName(char *name, unsigned int *vid)
2567 /* Name begins with "Vol_" and ends with .data. See nt_HandleToVolDir() */
2574 if (strncmp(name, "Vol_", 4))
2576 if (strcmp(name + len - 5, ".data"))
2579 stmp[len - 5] = '\0';
2580 *vid = base32_to_int(stmp + 4);
2585 DecodeVolumeName(char *name, unsigned int *vid)
2587 if (strlen(name) < 1)
2589 *vid = (unsigned int)flipbase64_to_int64(name);
2597 * Get the inode number from the name.
2602 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
2607 WIN32_FIND_DATA data;
2611 FdHandle_t linkHandle;
2614 afs_snprintf(fpath, sizeof(fpath), "%s\\%s", dpath, name);
2616 dirH = FindFirstFileEx(fpath, FindExInfoStandard, &data,
2617 FindExSearchNameMatch, NULL,
2618 FIND_FIRST_EX_CASE_SENSITIVE);
2619 if (dirH == INVALID_HANDLE_VALUE)
2622 (void)strcpy(stmp, name);
2623 s = strrchr(stmp, '_');
2627 t = strrchr(s, '.');
2632 vno = base32_to_int(s);
2633 tag = base32_to_int(t+1);
2634 info->inodeNumber = (Inode) tag << NAMEI_TAGSHIFT;
2635 info->inodeNumber |= (Inode) vno;
2636 info->byteCount = data.nFileSizeLow;
2638 dirl = dpath[strlen(dpath)-1];
2639 if (dirl == 'R') { /* Special inode. */
2640 info->inodeNumber |= NAMEI_INODESPECIAL;
2641 info->u.param[0] = data.ftCreationTime.dwHighDateTime;
2642 info->u.param[1] = data.ftCreationTime.dwLowDateTime;
2643 info->u.param[2] = tag;
2644 info->u.param[3] = volid;
2645 if (tag != VI_LINKTABLE)
2646 info->linkCount = 1;
2648 /* Open this handle */
2650 (void)sprintf(lpath, "%s\\%s", fpath, data.cFileName);
2651 linkHandle.fd_fd = nt_open(lpath, O_RDONLY, 0666);
2653 namei_GetLinkCount(&linkHandle, (Inode) 0, 0, 0, 0);
2657 namei_GetLinkCount(&linkHandle, info->inodeNumber, 0, 0, 0);
2658 if (info->linkCount == 0) {
2660 Log("Found 0 link count file %s\\%s, deleting it.\n",
2661 fpath, data.cFileName);
2662 AddToZLCDeleteList(dirl, data.cFileName);
2664 Log("Found 0 link count file %s\\%s.\n", path,
2668 info->u.param[2] = data.ftCreationTime.dwHighDateTime;
2669 info->u.param[3] = data.ftCreationTime.dwLowDateTime;
2670 info->u.param[1] = vno;
2671 info->u.param[0] = volid;
2678 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
2682 struct afs_stat status;
2684 lb64_string_t check;
2686 afs_snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
2688 if (afs_stat(fpath, &status) < 0) {
2692 info->byteCount = status.st_size;
2693 info->inodeNumber = (Inode) flipbase64_to_int64(name);
2695 int64_to_flipbase64(check, info->inodeNumber);
2696 if (strcmp(name, check))
2699 GetOGMFromStat(&status, &parm, &tag);
2700 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
2701 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
2702 info->u.param[0] = parm;
2703 info->u.param[1] = -1;
2704 info->u.param[2] = tag;
2705 info->u.param[3] = volid;
2707 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
2708 info->u.param[0] = volid;
2709 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
2710 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
2711 & (Inode) NAMEI_UNIQMASK);
2712 info->u.param[3] = parm;
2719 * Convert the VolumeInfo file from RO to RW
2720 * this routine is called by namei_convertROtoRWvolume()
2723 #ifdef FSSYNC_BUILD_CLIENT
2725 convertVolumeInfo(FD_t fdr, FD_t fdw, afs_uint32 vid)
2727 struct VolumeDiskData vd;
2730 if (OS_READ(fdr, (char *)&vd, sizeof(struct VolumeDiskData)) !=
2731 sizeof(struct VolumeDiskData)) {
2732 Log("1 convertVolumeInfo: read failed for %lu with code %d\n",
2733 afs_printable_uint32_lu(vid),
2737 vd.restoredFromId = vd.id; /* remember the RO volume here */
2739 vd.id = vd.parentId;
2743 vd.uniquifier += 5000; /* just in case there are still file copies from
2744 * the old RW volume around */
2745 p = strrchr(vd.name, '.');
2746 if (p && !strcmp(p, ".readonly")) {
2749 if (OS_WRITE(fdw, (char *)&vd, sizeof(struct VolumeDiskData)) !=
2750 sizeof(struct VolumeDiskData)) {
2751 Log("1 convertVolumeInfo: write failed for %lu with code %d\n",
2752 afs_printable_uint32_lu(vid),
2761 * Convert a RO-volume into a RW-volume
2763 * This function allows to recover very fast from the loss of a partition
2764 * from RO-copies if all RO-Copies exist on another partition.
2765 * Then these RO-volumes can be made to the new RW-volumes.
2766 * Backup of RW-volumes then consists in "vos release".
2768 * We must make sure in this partition exists only the RO-volume which
2769 * is typical for remote replicas.
2771 * Then the linktable is already ok,
2772 * the vnode files need to be renamed
2773 * the volinfo file needs to be replaced by another one with
2774 * slightly different contents and new name.
2775 * The volume header file of the RO-volume in the /vicep<x> directory
2776 * is destroyed by this call. A new header file for the RW-volume must
2777 * be created after return from this routine.
2781 namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
2784 #ifdef FSSYNC_BUILD_CLIENT
2786 char dir_name[512], oldpath[512], newpath[512];
2801 struct DiskPartition64 *partP;
2802 struct ViceInodeInfo info;
2803 struct VolumeDiskHeader h;
2804 # ifdef AFS_DEMAND_ATTACH_FS
2806 # endif /* AFS_DEMAND_ATTACH_FS */
2808 for (partP = DiskPartitionList; partP && strcmp(partP->name, pname);
2809 partP = partP->next);
2811 Log("1 namei_ConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
2816 # ifdef AFS_DEMAND_ATTACH_FS
2817 locktype = VVolLockType(V_VOLUPD, 1);
2818 code = VLockVolumeByIdNB(volumeId, partP, locktype);
2824 # endif /* AFS_DEMAND_ATTACH_FS */
2826 if (VReadVolumeDiskHeader(volumeId, partP, &h)) {
2827 Log("1 namei_ConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n",
2828 afs_printable_uint32_lu(volumeId));
2833 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
2835 ino = namei_MakeSpecIno(h.parent, VI_LINKTABLE);
2836 IH_INIT(ih, partP->device, h.parent, ino);
2838 namei_HandleToName(&n, ih);
2839 strlcpy(dir_name, n.n_path, sizeof(dir_name));
2840 p = strrchr(dir_name, OS_DIRSEPC);
2842 dirp = opendir(dir_name);
2844 Log("1 namei_ConvertROtoRWvolume: Could not opendir(%s)\n", dir_name);
2849 while ((dp = readdir(dirp))) {
2850 /* struct ViceInodeInfo info; */
2851 #ifndef AFS_NT40_ENV
2852 if (*dp->d_name == '.')
2855 if (DecodeInode(dir_name, dp->d_name, &info, ih->ih_vid) < 0) {
2856 Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s" OS_DIRSEP "%s\n",
2857 dir_name, dp->d_name);
2862 if (info.u.param[1] != -1) {
2863 Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s" OS_DIRSEP "%s\n", dir_name, dp->d_name);
2868 if (info.u.param[0] != volumeId) {
2869 if (info.u.param[0] == ih->ih_vid) {
2870 if (info.u.param[2] == VI_LINKTABLE) { /* link table */
2875 Log("1 namei_ConvertROtoRWvolume: found special file %s" OS_DIRSEP "%s"
2876 " for volume %lu\n", dir_name, dp->d_name,
2877 afs_printable_uint32_lu(info.u.param[0]));
2882 if (info.u.param[2] == VI_VOLINFO) { /* volume info file */
2883 strlcpy(infoName, dp->d_name, sizeof(infoName));
2885 } else if (info.u.param[2] == VI_SMALLINDEX) { /* small vnodes file */
2886 strlcpy(smallName, dp->d_name, sizeof(smallName));
2888 } else if (info.u.param[2] == VI_LARGEINDEX) { /* large vnodes file */
2889 strlcpy(largeName, dp->d_name, sizeof(largeName));
2893 Log("1 namei_ConvertROtoRWvolume: unknown type %d of special file found : %s" OS_DIRSEP "%s\n", info.u.param[2], dir_name, dp->d_name);
2900 if (!infoSeen || !smallSeen || !largeSeen || !linkSeen) {
2901 Log("1 namei_ConvertROtoRWvolume: not all special files found in %s\n", dir_name);
2907 * If we come here then there was only a RO-volume and we can safely
2911 memset(&t_ih, 0, sizeof(t_ih));
2912 t_ih.ih_dev = ih->ih_dev;
2913 t_ih.ih_vid = ih->ih_vid;
2915 (void)afs_snprintf(oldpath, sizeof oldpath, "%s" OS_DIRSEP "%s", dir_name,
2917 fd = afs_open(oldpath, O_RDWR, 0);
2919 Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
2924 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_VOLINFO);
2925 namei_HandleToName(&n, &t_ih);
2926 fd2 = afs_open(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
2928 Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
2933 code = convertVolumeInfo(fd, fd2, ih->ih_vid);
2937 OS_UNLINK(n.n_path);
2941 SetOGM(fd2, ih->ih_vid, 1);
2944 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_SMALLINDEX);
2945 namei_HandleToName(&n, &t_ih);
2946 (void)afs_snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
2948 fd = afs_open(newpath, O_RDWR, 0);
2950 Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
2954 SetOGM(fd, ih->ih_vid, 2);
2957 MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
2959 link(newpath, n.n_path);
2963 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_LARGEINDEX);
2964 namei_HandleToName(&n, &t_ih);
2965 (void)afs_snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
2967 fd = afs_open(newpath, O_RDWR, 0);
2969 Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
2973 SetOGM(fd, ih->ih_vid, 3);
2976 MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
2978 link(newpath, n.n_path);
2985 h.volumeInfo_hi = h.id;
2986 h.smallVnodeIndex_hi = h.id;
2987 h.largeVnodeIndex_hi = h.id;
2988 h.linkTable_hi = h.id;
2990 if (VCreateVolumeDiskHeader(&h, partP)) {
2991 Log("1 namei_ConvertROtoRWvolume: Couldn't write header for RW-volume %lu\n",
2992 afs_printable_uint32_lu(h.id));
2997 if (VDestroyVolumeDiskHeader(partP, volumeId, h.parent)) {
2998 Log("1 namei_ConvertROtoRWvolume: Couldn't unlink header for RO-volume %lu\n",
2999 afs_printable_uint32_lu(volumeId));
3002 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
3003 FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
3006 # ifdef AFS_DEMAND_ATTACH_FS
3008 VUnlockVolumeById(volumeId, partP);
3010 # endif /* AFS_DEMAND_ATTACH_FS */
3018 * returns a static string used to print either 32 or 64 bit inode numbers.
3021 PrintInode(char *s, Inode ino)
3023 static afs_ino_str_t result;
3027 (void)afs_snprintf(s, sizeof(afs_ino_str_t), "%" AFS_UINT64_FMT, (afs_uintmax_t) ino);
3034 /* Routines to facilitate removing zero link count files. */
3035 #define MAX_ZLC_NAMES 32
3036 #define MAX_ZLC_NAMELEN 16
3037 typedef struct zlcList_s {
3038 struct zlcList_s *zlc_next;
3040 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
3043 static zlcList_t *zlcAnchor = NULL;
3044 static zlcList_t *zlcCur = NULL;
3047 AddToZLCDeleteList(char dir, char *name)
3049 osi_Assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
3051 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
3052 if (zlcCur && zlcCur->zlc_next)
3053 zlcCur = zlcCur->zlc_next;
3055 zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
3061 zlcCur->zlc_next = tmp;
3065 zlcCur->zlc_next = NULL;
3070 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
3072 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%s", name);
3078 DeleteZLCFiles(char *path)
3084 for (z = zlcAnchor; z; z = z->zlc_next) {
3085 for (i = 0; i < z->zlc_n; i++) {
3087 (void)sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
3089 (void)sprintf(fname, "%s", z->zlc_names[i]);
3090 if (namei_unlink(fname) < 0) {
3091 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
3095 z->zlc_n = 0; /* Can reuse space. */
3108 tnext = i->zlc_next;
3112 zlcCur = zlcAnchor = NULL;
3116 #endif /* AFS_NAMEI_ENV */