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>
20 #ifdef HAVE_SYS_FILE_H
21 # include <sys/file.h>
33 #include <afs/afsutil.h>
36 #include <afs/afsint.h>
40 #include "viceinode.h"
42 #include "partition.h"
44 #include "volume_inline.h"
46 #include <afs/errors.h>
49 #include <afs/errmap_nt.h>
53 #define LOCK_SH 1 /* shared lock */
54 #define LOCK_EX 2 /* exclusive lock */
55 #define LOCK_NB 4 /* don't block when locking */
56 #define LOCK_UN 8 /* unlock */
61 #include <afs/work_queue.h>
62 #include <afs/thread_pool.h>
63 #include <vol/vol-salvage.h>
70 namei_iread(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
79 nBytes = FDH_PREAD(fdP, buf, size, offset);
85 namei_iwrite(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
94 nBytes = FDH_PWRITE(fdP, buf, size, offset);
100 /* Inode number format:
101 * low 32 bits - if a regular file or directory, the vnode; else the type.
102 * 32-36 - uniquifier tag and index into counts array for this vnode. Only
103 * two of the available bits are currently used. The rest are
104 * present in case we ever increase the number of types of volumes
105 * in the volume group.
106 * bit 37 : 1 == special, 0 == regular
108 # define NAMEI_VNODEMASK 0x00ffffffff
109 # define NAMEI_TAGSHIFT 32
110 # define NAMEI_INODESPECIAL 0x2000000000
111 # define NAMEI_SPECDIR "R"
112 # define NAMEI_SPECDIRC 'R'
113 #else /* !AFS_NT40_ENV */
114 /* Inode number format:
115 * low 26 bits - vnode number - all 1's if volume special file.
117 * next 3 bits spare (0's)
118 * high 32 bits - uniquifier (regular) or type if spare
120 # define NAMEI_VNODEMASK 0x003ffffff
121 # define NAMEI_TAGSHIFT 26
122 # define NAMEI_UNIQMASK 0xffffffff
123 # define NAMEI_UNIQSHIFT 32
124 # define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
125 /* dir1 is the high 8 bits of the 26 bit vnode */
126 # define VNO_DIR1(vno) ((vno >> 14) & 0xff)
127 /* dir2 is the next 9 bits */
128 # define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
129 /* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
130 # define NAMEI_SPECDIR "special"
131 #endif /* !AFS_NT40_ENV */
132 #define NAMEI_TAGMASK 0x7
133 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
135 #define NAMEI_SPECDIRLEN (sizeof(NAMEI_SPECDIR)-1)
137 #define NAMEI_MAXVOLS 5 /* Maximum supported number of volumes per volume
138 * group, not counting temporary (move) volumes.
139 * This is the number of separate files, all having
140 * the same vnode number, which can occur in a volume
152 static int GetFreeTag(IHandle_t * ih, int vno);
154 /* namei_HandleToInodeDir
156 * Construct the path name of the directory holding the inode data.
157 * Format: /<vicepx>/INODEDIR
162 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
164 memset(name, '\0', sizeof(*name));
165 nt_DevToDrive(name->n_drive, ih->ih_dev);
166 strlcpy(name->n_path, name->n_drive, sizeof(name->n_path));
170 /* Format: /<vicepx>/INODEDIR */
171 #define PNAME_BLEN 64
173 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
177 memset(name, '\0', sizeof(*name));
180 * Add the /vicepXX string to the start of name->n_base and then calculate
181 * offset as the number of bytes we know we added.
183 * FIXME: This embeds knowledge of the vice partition naming scheme and
184 * mapping from device numbers. There needs to be an API that tells us
187 volutil_PartitionName_r(ih->ih_dev, name->n_base, sizeof(name->n_base));
188 offset = VICE_PREFIX_SIZE + (ih->ih_dev > 25 ? 2 : 1);
189 name->n_base[offset] = OS_DIRSEPC;
191 strlcpy(name->n_base + offset, INODEDIR, sizeof(name->n_base) - offset);
192 strlcpy(name->n_path, name->n_base, sizeof(name->n_path));
196 #define addtoname(N, C) \
198 if ((N)->n_path[strlen((N)->n_path)-1] != OS_DIRSEPC) \
199 strlcat((N)->n_path, OS_DIRSEP, sizeof((N)->n_path)); \
200 strlcat((N)->n_path, (C), sizeof((N)->n_path)); \
206 namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
208 /* X:\Vol_XXXXXXX.data */
212 namei_HandleToInodeDir(name, ih);
213 /* nt_drive added to name by namei_HandleToInodeDir() */
214 namep = name->n_voldir;
215 (void)memcpy(namep, "\\Vol_", 5);
217 (void)strcpy(namep, int_to_base32(str1, ih->ih_vid));
218 namep += strlen(namep);
219 memcpy(namep, ".data", 5);
222 addtoname(name, name->n_voldir);
226 namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
230 namei_HandleToInodeDir(name, ih);
231 (void)int32_to_flipbase64(tmp, (int64_t) (ih->ih_vid & 0xff));
232 strlcpy(name->n_voldir1, tmp, sizeof(name->n_voldir1));
233 addtoname(name, name->n_voldir1);
234 (void)int32_to_flipbase64(tmp, (int64_t) ih->ih_vid);
235 strlcpy(name->n_voldir2, tmp, sizeof(name->n_voldir2));
236 addtoname(name, name->n_voldir2);
240 /* namei_HandleToName
242 * Constructs a file name for the fully qualified handle.
245 /* Note that special files end up in X:\Vol_XXXXXXX.data\R */
247 namei_HandleToName(namei_t * name, IHandle_t * ih)
249 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
250 int special = (ih->ih_ino & NAMEI_INODESPECIAL)?1:0;
251 int tag = (int)((ih->ih_ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
254 namei_HandleToVolDir(name, ih);
257 name->n_dir[0] = NAMEI_SPECDIRC;
260 name->n_dir[0] = 'Q';
262 name->n_dir[0] = ((vno & 0x1f) >> 1) + 'A';
265 name->n_dir[1] = '\0';
266 addtoname(name, name->n_dir);
267 /* X:\Vol_XXXXXXX.data\X\V_XXXXXXX.XXX */
268 namep = name->n_inode;
269 (void)memcpy(namep, "\\V_", 3);
271 (void)strcpy(namep, int_to_base32(str1, vno));
272 namep += strlen(namep);
274 (void)strcpy(namep, int_to_base32(str1, tag));
275 namep += strlen(namep);
276 addtoname(name, name->n_inode);
279 /* Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special */
281 namei_HandleToName(namei_t * name, IHandle_t * ih)
283 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
286 namei_HandleToVolDir(name, ih);
288 if (vno == NAMEI_VNODESPECIAL) {
289 strlcpy(name->n_dir1, NAMEI_SPECDIR, sizeof(name->n_dir1));
290 addtoname(name, name->n_dir1);
291 name->n_dir2[0] = '\0';
293 (void)int32_to_flipbase64(str, VNO_DIR1(vno));
294 strlcpy(name->n_dir1, str, sizeof(name->n_dir1));
295 addtoname(name, name->n_dir1);
296 (void)int32_to_flipbase64(str, VNO_DIR2(vno));
297 strlcpy(name->n_dir2, str, sizeof(name->n_dir2));
298 addtoname(name, name->n_dir2);
300 (void)int64_to_flipbase64(str, (int64_t) ih->ih_ino);
301 strlcpy(name->n_inode, str, sizeof(name->n_inode));
302 addtoname(name, name->n_inode);
307 /* The following is a warning to tell sys-admins to not muck about in this
310 #define VICE_README "These files and directories are a part of the AFS \
311 namespace. Modifying them\nin any way will result in loss of AFS data,\n\
312 ownership and permissions included.\n"
314 namei_ViceREADME(char *partition)
319 /* Create the inode directory if we're starting for the first time */
320 snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s", partition,
322 mkdir(filename, 0700);
324 snprintf(filename, sizeof filename,
325 "%s" OS_DIRSEP "%s" OS_DIRSEP "README",
326 partition, INODEDIR);
327 fd = OS_OPEN(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
328 if (fd != INVALID_FD) {
329 (void)OS_WRITE(fd, VICE_README, strlen(VICE_README));
336 /* namei_CreateDataDirectories
338 * If creating the file failed because of ENOENT or ENOTDIR, try
339 * creating all the directories first.
343 namei_CreateDataDirectories(namei_t * name, int *created)
350 snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
352 if (mkdir(tmp) < 0) {
363 for (i = 'A'; i <= NAMEI_SPECDIRC; i++) {
365 if (mkdir(tmp) < 0 && errno != EEXIST)
371 #define create_dir() \
373 if (mkdir(tmp, 0700)<0) { \
374 if (errno != EEXIST) \
382 #define create_nextdir(A) \
384 strcat(tmp, OS_DIRSEP); strcat(tmp, A); create_dir(); \
388 namei_CreateDataDirectories(namei_t * name, int *created)
394 strlcpy(tmp, name->n_base, sizeof(tmp));
397 create_nextdir(name->n_voldir1);
398 create_nextdir(name->n_voldir2);
399 create_nextdir(name->n_dir1);
400 if (name->n_dir2[0]) {
401 create_nextdir(name->n_dir2);
408 /* delTree(): Deletes an entire tree of directories (no files)
410 * root : Full path to the subtree. Should be big enough for PATH_MAX
411 * tree : the subtree to be deleted is rooted here. Specifies only the
412 * subtree beginning at tree (not the entire path). It should be
413 * a pointer into the "root" buffer.
415 * errp : errno of the first error encountered during the directory cleanup.
416 * *errp should have been initialized to 0.
419 * -1 : If errors were encountered during cleanup and error is set to
423 * If there are errors, we try to work around them and delete as many
424 * directories as possible. We don't attempt to remove directories that still
425 * have non-dir entries in them.
428 delTree(char *root, char *tree, int *errp)
436 /* delete the children first */
437 cp = strchr(tree, OS_DIRSEPC);
439 delTree(root, cp + 1, errp);
442 cp = tree + strlen(tree); /* move cp to the end of string tree */
444 /* now delete all entries in this dir */
445 if ((ds = opendir(root)) != (DIR *) NULL) {
447 while ((dirp = readdir(ds))) {
448 /* ignore . and .. */
449 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
451 /* since root is big enough, we reuse the space to
452 * concatenate the dirname to the current tree
454 strcat(root, OS_DIRSEP);
455 strcat(root, dirp->d_name);
456 if (afs_stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
457 /* delete this subtree */
458 delTree(root, cp + 1, errp);
460 *errp = *errp ? *errp : errno;
462 /* recover path to our cur tree by truncating it to
467 /* if (!errno) -- closedir not implicit if we got an error */
471 /* finally axe the current dir */
473 *errp = *errp ? *errp : errno;
475 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
477 #endif /* !AFS_PTHREAD_ENV */
482 /* if we encountered errors during cleanup, we return a -1 */
491 /* namei_RemoveDataDirectories
493 * Returns 0 on success.
494 * Returns -1 on error. Typically, callers ignore this error because we
495 * can continue running if the removes fail. The salvage process will
496 * finish tidying up for us.
501 namei_RemoveDataDirectories(namei_t * name)
508 snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
511 path += strlen(path);
512 *path++ = OS_DIRSEPC;
514 for (i = 'A'; i <= NAMEI_SPECDIRC; i++) {
516 if (rmdir(name->n_path) < 0 && errno != ENOENT)
521 /* Delete the Vol_NNNNNN.data directory. */
524 if (rmdir(name->n_path) < 0 && errno != ENOENT) {
532 * We only use the n_base and n_voldir1 entries
533 * and only do rmdir's.
536 namei_RemoveDataDirectories(namei_t * name)
540 int prefixlen = strlen(name->n_base), err = 0;
541 int vollen = strlen(name->n_voldir1);
542 char pbuf[MAXPATHLEN];
546 strlcpy(path, name->n_path, sizeof(pbuf));
548 /* move past the prefix and n_voldir1 */
549 path = path + prefixlen + 1 + vollen + 1; /* skip over the trailing / */
551 /* now delete all dirs upto path */
552 code = delTree(pbuf, path, &err);
554 /* We've now deleted everything under /n_base/n_voldir1/n_voldir2 that
555 * we could. Do not delete /n_base/n_voldir1, since doing such might
556 * interrupt another thread trying to create a volume. We could introduce
557 * some locking to make this safe (or only remove it for whole-partition
558 * salvages), but by not deleting it we only leave behind a maximum of
559 * 256 empty directories. So at least for now, don't bother. */
564 /* Create the file in the name space.
566 * Parameters stored as follows:
568 * p1 - volid - implied in containing directory.
569 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
570 * p3 - uniq -- bits 4-0 are in mode bits 4-0
571 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
573 * p1 - volid - creation time - dwHighDateTime
574 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
575 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
576 * p4 - parid - parent volume id - implied in containing directory.
578 * Return value is the inode number or (Inode)-1 if error.
579 * We "know" there is only one link table, so return EEXIST if there already
580 * is a link table. It's up to the calling code to test errno and increment
586 * This function is called by VCreateVolume to hide the implementation
587 * details of the inode numbers. This only allows for 7 volume special
588 * types, but if we get that far, this could should be dead by then.
591 namei_MakeSpecIno(int volid, int type)
594 ino = NAMEI_INODESPECIAL;
597 /* tag is always 0 for special */
599 type &= NAMEI_TAGMASK;
600 ino |= ((Inode) type) << NAMEI_TAGSHIFT;
601 ino |= ((Inode) volid) << NAMEI_UNIQSHIFT;
609 SetOGM(FD_t fd, int parm, int tag)
615 CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
617 WIN32_FIND_DATA info;
621 FindFirstFileEx(name->n_path, FindExInfoStandard, &info,
622 FindExSearchNameMatch, NULL,
623 FIND_FIRST_EX_CASE_SENSITIVE);
626 return -1; /* Can't get info, leave alone */
630 if (info.ftCreationTime.dwHighDateTime != (unsigned int)p1)
635 #else /* AFS_NT40_ENV */
636 /* SetOGM - set owner group and mode bits from parm and tag */
638 SetOGM(FD_t fd, int parm, int tag)
641 * owner - low 15 bits of parm.
642 * group - next 15 bits of parm.
643 * mode - 2 bits of parm, then lowest = 3 bits of tag.
645 int owner, group, mode;
647 owner = parm & 0x7fff;
648 group = (parm >> 15) & 0x7fff;
649 if (fchown(fd, owner, group) < 0)
652 mode = (parm >> 27) & 0x18;
654 if (fchmod(fd, mode) < 0)
659 /* GetOGM - get parm and tag from owner, group and mode bits. */
661 GetOGMFromStat(struct afs_stat_st *status, int *parm, int *tag)
663 *parm = status->st_uid | (status->st_gid << 15);
664 *parm |= (status->st_mode & 0x18) << 27;
665 *tag = status->st_mode & 0x7;
669 CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
671 struct afs_stat_st status;
673 if (afs_fstat(fdP->fd_fd, &status) < 0)
676 GetOGMFromStat(&status, &parm, &tag);
682 #endif /* !AFS_NT40_ENV */
684 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
686 /* Derive the name and create it O_EXCL. If that fails we have an error.
687 * Get the tag from a free column in the link table.
691 namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
694 FD_t fd = INVALID_FD;
705 memset((void *)&tmp, 0, sizeof(IHandle_t));
706 memset(&tfd, 0, sizeof(FdHandle_t));
708 tmp.ih_dev = nt_DriveToDev(part);
709 if (tmp.ih_dev == -1) {
714 if (p2 == INODESPECIAL) {
715 /* Parameters for special file:
716 * p1 - volume id - goes into owner/group/mode
717 * p2 - vnode == INODESPECIAL
719 * p4 - parent volume id
721 ftime.dwHighDateTime = p1;
722 ftime.dwLowDateTime = p2;
724 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
725 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
727 int vno = p2 & NAMEI_VNODEMASK;
728 /* Parameters for regular file:
742 tmp.ih_ino = (Inode) p2;
743 ftime.dwHighDateTime = p3;
744 ftime.dwLowDateTime = p4;
747 namei_HandleToName(&name, &tmp);
748 p = strrchr((char *)&name.n_path, '.');
750 for (tag = 0; tag < NAMEI_MAXVOLS; tag++) {
751 *p = *int_to_base32(str1, tag);
752 fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_EXCL, 0666);
753 if (fd == INVALID_FD) {
754 if (errno == ENOTDIR || errno == ENOENT) {
755 if (namei_CreateDataDirectories(&name, &created_dir) == 0)
756 fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_EXCL, 0666);
760 if (fd != INVALID_FD)
762 if (p2 == INODESPECIAL && p3 == VI_LINKTABLE)
765 if (fd == INVALID_FD) {
769 tmp.ih_ino &= ~(((Inode) NAMEI_TAGMASK) << NAMEI_TAGSHIFT);
770 tmp.ih_ino |= (((Inode) tag) << NAMEI_TAGSHIFT);
773 if (!SetFileTime((HANDLE) fd, &ftime, NULL, NULL)) {
774 errno = OS_ERROR(EBADF);
780 if (p2 != INODESPECIAL) {
781 if (fd == INVALID_FD) {
783 code = nt_unlink((char *)&name.n_path);
792 code = namei_SetLinkCount(fdP, tmp.ih_ino, 1, 0);
794 } else if (p2 == INODESPECIAL && p3 == VI_LINKTABLE) {
795 if (fd == INVALID_FD)
797 /* hack at tmp to setup for set link count call. */
799 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
804 if (fd != INVALID_FD)
807 if (code || (fd == INVALID_FD)) {
808 if (p2 != INODESPECIAL) {
811 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
817 int save_errno = errno;
818 namei_RemoveDataDirectories(&name);
822 return (code || (fd == INVALID_FD)) ? (Inode) -1 : tmp.ih_ino;
824 #else /* !AFS_NT40_ENV */
826 namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
838 memset((void *)&tmp, 0, sizeof(IHandle_t));
839 memset(&tfd, 0, sizeof(FdHandle_t));
841 tmp.ih_dev = volutil_GetPartitionID(part);
842 if (tmp.ih_dev == -1) {
848 /* Parameters for special file:
849 * p1 - volume id - goes into owner/group/mode
852 * p4 - parent volume id
857 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
858 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
860 int vno = p2 & NAMEI_VNODEMASK;
861 /* Parameters for regular file:
873 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
874 tag = GetFreeTag(lh, p2);
879 tmp.ih_ino = (Inode) p2;
880 /* name is <uniq(p3)><tag><vno(p2)> */
881 tmp.ih_ino |= ((Inode) tag) << NAMEI_TAGSHIFT;
882 tmp.ih_ino |= ((Inode) p3) << NAMEI_UNIQSHIFT;
887 namei_HandleToName(&name, &tmp);
888 fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_RDWR, 0);
889 if (fd == INVALID_FD) {
890 if (errno == ENOTDIR || errno == ENOENT) {
891 if (namei_CreateDataDirectories(&name, &created_dir) < 0)
893 fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_RDWR,
895 if (fd == INVALID_FD)
901 if (SetOGM(fd, ogm_parm, tag) < 0) {
907 if (p2 == (afs_uint32)-1 && p3 == VI_LINKTABLE) {
908 /* hack at tmp to setup for set link count call. */
909 memset((void *)&tfd, 0, sizeof(FdHandle_t)); /* minimalistic still, but a little cleaner */
912 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
916 if (fd != INVALID_FD)
920 if (code || (fd == INVALID_FD)) {
924 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
929 return (code || (fd == INVALID_FD)) ? (Inode) - 1 : tmp.ih_ino;
935 namei_iopen(IHandle_t * h)
940 /* Convert handle to file name. */
941 namei_HandleToName(&name, h);
942 fd = OS_OPEN((char *)&name.n_path, O_RDWR, 0666);
946 /* Need to detect vol special file and just unlink. In those cases, the
947 * handle passed in _is_ for the inode. We only check p1 for the special
951 namei_dec(IHandle_t * ih, Inode ino, int p1)
958 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
960 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
962 /* Verify this is the right file. */
963 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
965 namei_HandleToName(&name, tmp);
970 errno = OS_ERROR(ENOENT);
974 if (CheckOGM(&name, fdP, p1) < 0) {
975 FDH_REALLYCLOSE(fdP);
977 errno = OS_ERROR(EINVAL);
981 /* If it's the link table itself, decrement the link count. */
982 if (type == VI_LINKTABLE) {
983 if ((count = namei_GetLinkCount(fdP, (Inode) 0, 1, 0, 1)) < 0) {
984 FDH_REALLYCLOSE(fdP);
990 if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) <
992 FDH_REALLYCLOSE(fdP);
1004 if ((code = OS_UNLINK(name.n_path)) == 0) {
1005 if (type == VI_LINKTABLE) {
1006 /* Try to remove directory. If it fails, that's ok.
1007 * Salvage will clean up.
1009 (void)namei_RemoveDataDirectories(&name);
1012 FDH_REALLYCLOSE(fdP);
1015 /* Get a file descriptor handle for this Inode */
1021 if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0) {
1022 FDH_REALLYCLOSE(fdP);
1028 if (namei_SetLinkCount(fdP, ino, count, 1) < 0) {
1029 FDH_REALLYCLOSE(fdP);
1034 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
1035 Log("Warning: Lost ref on ihandle dev %d vid %d ino %lld\n",
1036 th->ih_dev, th->ih_vid, (afs_int64)th->ih_ino);
1039 /* If we're less than 0, someone presumably unlinked;
1040 don't bother setting count to 0, but we need to drop a lock */
1041 if (namei_SetLinkCount(fdP, ino, 0, 1) < 0) {
1042 FDH_REALLYCLOSE(fdP);
1048 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
1050 namei_HandleToName(&name, th);
1052 code = OS_UNLINK(name.n_path);
1061 namei_inc(IHandle_t * h, Inode ino, int p1)
1067 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1068 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1069 if (type != VI_LINKTABLE)
1074 /* Get a file descriptor handle for this Inode */
1080 if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0)
1085 errno = OS_ERROR(EINVAL);
1089 if (namei_SetLinkCount(fdP, ino, count, 1) < 0)
1093 FDH_REALLYCLOSE(fdP);
1100 #ifndef AFS_NT40_ENV
1102 namei_replace_file_by_hardlink(IHandle_t *hLink, IHandle_t *hTarget)
1108 /* Convert handle to file name. */
1109 namei_HandleToName(&nameLink, hLink);
1110 namei_HandleToName(&nameTarget, hTarget);
1112 OS_UNLINK(nameLink.n_path);
1113 code = link(nameTarget.n_path, nameLink.n_path);
1118 namei_copy_on_write(IHandle_t *h)
1124 struct afs_stat_st tstat;
1127 namei_HandleToName(&name, h);
1128 if (afs_stat(name.n_path, &tstat) < 0)
1130 if (tstat.st_nlink > 1) { /* do a copy on write */
1139 snprintf(path, sizeof(path), "%s-tmp", name.n_path);
1140 fd = OS_OPEN(path, O_CREAT | O_EXCL | O_RDWR, 0);
1141 if (fd == INVALID_FD) {
1152 size = tstat.st_size;
1155 tlen = size > 8192 ? 8192 : size;
1156 if (FDH_PREAD(fdP, buf, tlen, offset) != tlen)
1158 if (OS_WRITE(fd, buf, tlen) != tlen)
1164 FDH_REALLYCLOSE(fdP);
1169 OS_UNLINK(name.n_path);
1170 code = rename(path, name.n_path);
1177 /************************************************************************
1178 * File Name Structure
1179 ************************************************************************
1181 * Each AFS file needs a unique name and it needs to be findable with
1182 * minimal lookup time. Note that the constraint on the number of files and
1183 * directories in a volume is the size of the vnode index files and the
1184 * max file size AFS supports (for internal files) of 2^31. Since a record
1185 * in the small vnode index file is 64 bytes long, we can have at most
1186 * (2^31)/64 or 33554432 files. A record in the large index file is
1187 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
1188 * Another layout parameter is that there is roughly a 16 to 1 ratio between
1189 * the number of files and the number of directories.
1191 * Using this information we can see that a layout of 256 directories, each
1192 * with 512 subdirectories and each of those having 512 files gives us
1193 * 256*512*512 = 67108864 AFS files and directories.
1195 * The volume, vnode, uniquifier and data version, as well as the tag
1196 * are required, either for finding the file or for salvaging. It's best to
1197 * restrict the name to something that can be mapped into 64 bits so the
1198 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
1199 * is used to distinguish between different versions of the same file
1200 * which are currently in the RW and clones of a volume. See "Link Table
1201 * Organization" below for more information on the tag. The tag is
1202 * required in the name of the file to ensure a unique name.
1204 * ifdef AFS_NT40_ENV
1205 * The data for each volume group is in a separate directory. The name of the
1206 * volume is of the form: Vol_NNNNNN.data, where NNNNNN is a base 32
1207 * representation of the RW volume ID (even where the RO is the only volume
1208 * on the partition). Below that are separate subdirectories for the
1209 * AFS directories and special files. There are also 16 directories for files,
1210 * hashed on the low 5 bits (recall bit0 is always 0) of the vnode number.
1211 * These directories are named:
1212 * A - P - 16 file directories.
1213 * Q ----- data directory
1214 * R ----- special files directory
1216 * The vnode is hashed into the directory using the low bits of the
1219 * The format of a file name for a regular file is:
1220 * Y:\Vol_NNNNNN.data\X\V_IIIIII.J
1221 * Y - partition encoded as drive letter, starting with D
1222 * NNNNNN - base 32 encoded volume number of RW volume
1223 * X - hash directory, as above
1224 * IIIIII - base 32 encoded vnode number
1225 * J - base 32 encoded tag
1227 * uniq is stored in the dwHighDateTime creation time field
1228 * dv is stored in the dwLowDateTime creation time field
1230 * Special inodes are always in the R directory, as above, and are
1232 * True child volid is stored in the dwHighDateTime creation time field
1233 * vnode number is always -1 (Special)
1234 * type is the IIIIII part of the filename
1235 * uniq is the J part of the filename
1236 * parent volume id is implied in the containing directory
1239 * We can store data in the uid, gid and mode bits of the files, provided
1240 * the directories have root only access. This gives us 15 bits for each
1241 * of uid and gid (GNU chown considers 65535 to mean "don't change").
1242 * There are 9 available mode bits. Adn we need to store a total of
1243 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
1244 * or 131 bits somewhere.
1246 * The format of a file name for a regular file is:
1247 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
1248 * V1 - low 8 bits of RW volume id
1249 * V2 - all bits of RW volume id
1250 * AA - high 8 bits of vnode number.
1251 * BB - next 9 bits of vnode number.
1252 * <tag><uniq><vno> - file name
1254 * Volume special files are stored in a separate directory:
1255 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
1258 * The vnode is hashed into the directory using the high bits of the
1259 * vnode number. This is so that consecutively created vnodes are in
1260 * roughly the same area on the disk. This will at least be optimal if
1261 * the user is creating many files in the same AFS directory. The name
1262 * should be formed so that the leading characters are different as quickly
1263 * as possible, leading to faster discards of incorrect matches in the
1271 /************************************************************************
1272 * Link Table Organization
1273 ************************************************************************
1275 * The link table volume special file is used to hold the link counts that
1276 * are held in the inodes in inode based AFS vice filesystems. For user
1277 * space access, the link counts are being kept in a separate
1278 * volume special file. The file begins with the usual version stamp
1279 * information and is then followed by one row per vnode number. vnode 0
1280 * is used to hold the link count of the link table itself. That is because
1281 * the same link table is shared among all the volumes of the volume group
1282 * and is deleted only when the last volume of a volume group is deleted.
1284 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
1285 * link count from 0 through 7. Each colume represents a unique instance of
1286 * that vnode. Say we have a file shared between the RW and a RO and a
1287 * different version of the file (or a different uniquifer) for the BU volume.
1288 * Then one column would be holding the link count of 2 for the RW and RO
1289 * and a different column would hold the link count of 1 for the BU volume.
1290 * # ifdef AFS_NT40_ENV
1291 * The column used is determined for NT by the uniquifier tag applied to
1292 * generate a unique file name in the NTFS namespace. The file name is
1293 * of the form "V_<vno>.<tag>" . And the <tag> is also the column number
1294 * in the link table.
1296 * Note that we allow only 5 volumes per file, giving 15 bits used in the
1300 #define LINKTABLE_WIDTH 2
1301 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
1304 * compute namei link table file and bit offset from inode number.
1306 * @param[in] ino inode number
1307 * @param[out] offset link table file offset
1308 * @param[out] index bit offset within 2-byte record
1313 namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
1315 int toff = (int)(ino & NAMEI_VNODEMASK);
1316 int tindex = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1318 *offset = (afs_foff_t) ((toff << LINKTABLE_SHIFT) + 8); /* * 2 + sizeof stamp */
1319 *index = (tindex << 1) + tindex;
1322 #ifdef AFS_PTHREAD_ENV
1323 /* XXX do static initializers work for WINNT/pthread? */
1324 pthread_mutex_t _namei_glc_lock = PTHREAD_MUTEX_INITIALIZER;
1325 #define NAMEI_GLC_LOCK MUTEX_ENTER(&_namei_glc_lock)
1326 #define NAMEI_GLC_UNLOCK MUTEX_EXIT(&_namei_glc_lock)
1327 #else /* !AFS_PTHREAD_ENV */
1328 #define NAMEI_GLC_LOCK
1329 #define NAMEI_GLC_UNLOCK
1330 #endif /* !AFS_PTHREAD_ENV */
1333 * get the link count of an inode.
1335 * @param[in] h namei link count table file handle
1336 * @param[in] ino inode number for which we are requesting a link count
1337 * @param[in] lockit if asserted, return with lock held on link table file
1338 * @param[in] fixup if asserted, write 1 to link count when read() returns
1340 * @param[in] nowrite return success on zero byte read or ZLC
1342 * @post if lockit asserted and lookup was successful, will return with write
1343 * lock on link table file descriptor
1345 * @return link count
1346 * @retval -1 namei link table i/o error
1351 namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite)
1353 unsigned short row = 0;
1358 /* there's no linktable yet. the salvager will create one later */
1359 if (h->fd_fd == INVALID_FD && fixup)
1361 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1364 if (FDH_LOCKFILE(h, offset) != 0)
1368 rc = FDH_PREAD(h, (char*)&row, sizeof(row), offset);
1369 if ((rc == 0 || !((row >> index) & NAMEI_TAGMASK)) && fixup && nowrite)
1371 if (rc == 0 && fixup) {
1373 * extend link table and write a link count of 1 for ino
1375 * in order to make MT-safe, truncation (extension really)
1376 * must happen under a mutex
1379 if (FDH_SIZE(h) >= offset+sizeof(row)) {
1381 goto bad_getLinkByte;
1383 FDH_TRUNC(h, offset+sizeof(row));
1385 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
1388 if (rc != sizeof(row)) {
1389 goto bad_getLinkByte;
1392 if (fixup && !((row >> index) & NAMEI_TAGMASK)) {
1396 * in order to make this mt-safe, we need to do the read-modify-write
1397 * under a mutex. thus, we repeat the read inside the lock.
1400 rc = FDH_PREAD(h, (char *)&row, sizeof(row), offset);
1401 if (rc == sizeof(row)) {
1403 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
1406 if (rc != sizeof(row))
1407 goto bad_getLinkByte;
1410 return (int)((row >> index) & NAMEI_TAGMASK);
1414 FDH_UNLOCKFILE(h, offset);
1419 namei_SetNonZLC(FdHandle_t * h, Inode ino)
1421 return namei_GetLinkCount(h, ino, 0, 1, 0);
1424 /* Return a free column index for this vnode. */
1426 GetFreeTag(IHandle_t * ih, int vno)
1440 /* Only one manipulates at a time. */
1441 if (FDH_LOCKFILE(fdP, offset) != 0) {
1442 FDH_REALLYCLOSE(fdP);
1446 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
1448 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1449 if (nBytes != sizeof(row)) {
1455 /* Now find a free column in this row and claim it. */
1456 for (col = 0; col < NAMEI_MAXVOLS; col++) {
1457 coldata = 7 << (col * 3);
1458 if ((row & coldata) == 0)
1461 if (col >= NAMEI_MAXVOLS) {
1466 coldata = 1 << (col * 3);
1469 if (FDH_PWRITE(fdP, (char *)&row, sizeof(row), offset) != sizeof(row)) {
1473 FDH_UNLOCKFILE(fdP, offset);
1478 FDH_UNLOCKFILE(fdP, offset);
1479 FDH_REALLYCLOSE(fdP);
1485 /* namei_SetLinkCount
1486 * If locked is set, assume file is locked. Otherwise, lock file before
1487 * proceeding to modify it.
1490 namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
1496 ssize_t nBytes = -1;
1498 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1501 if (FDH_LOCKFILE(fdP, offset) != 0) {
1506 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1507 if (nBytes != sizeof(row)) {
1509 errno = OS_ERROR(EBADF);
1510 goto bad_SetLinkCount;
1515 bytesRead = 7 << index;
1517 row &= (unsigned short)~bytesRead;
1518 row |= (unsigned short)count;
1520 if (FDH_PWRITE(fdP, (char *)&row, sizeof(short), offset) != sizeof(short)) {
1521 errno = OS_ERROR(EBADF);
1522 goto bad_SetLinkCount;
1530 FDH_UNLOCKFILE(fdP, offset);
1532 /* disallowed above 7, so... */
1537 /* ListViceInodes - write inode data to a results file. */
1538 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1539 unsigned int volid);
1540 static int DecodeVolumeName(char *name, unsigned int *vid);
1541 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
1542 int (*write_fun) (FD_t,
1543 struct ViceInodeInfo *,
1544 char *, char *), FD_t fp,
1545 int (*judgeFun) (struct ViceInodeInfo *,
1546 afs_uint32 vid, void *),
1547 afs_uint32 singleVolumeNumber, void *rock);
1552 * Write the inode data to the results file.
1554 * Returns -2 on error, 0 on success.
1556 * This is written as a callback simply so that other listing routines
1557 * can use the same inode reading code.
1560 WriteInodeInfo(FD_t fp, struct ViceInodeInfo *info, char *dir, char *name)
1563 n = OS_WRITE(fp, info, sizeof(*info));
1564 return (n == sizeof(*info)) ? 0 : -2;
1568 int mode_errors; /* Number of errors found in mode bits on directories. */
1570 VerifyDirPerms(char *path)
1572 struct afs_stat_st status;
1574 if (afs_stat(path, &status) < 0) {
1575 Log("Unable to stat %s. Please manually verify mode bits for this"
1576 " directory\n", path);
1578 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1584 * Fill the results file with the requested inode information.
1586 * This code optimizes single volume salvages by just looking at that one
1587 * volume's directory.
1589 * @param[in] devname device name string
1590 * @param[in] moutnedOn vice partition mount point
1591 * @param[in] resultFile result file in which to write inode
1592 * metadata. If NULL, write routine is not
1594 * @param[in] judgeInode filter function pointer. if not NULL, only
1595 * inodes for which this routine returns non-
1596 * zero will be written to the results file.
1597 * @param[in] singleVolumeNumber volume id filter
1598 * @param[out] forcep always set to 0 for namei impl
1599 * @param[in] forceR not used by namei impl
1600 * @param[in] wpath not used by namei impl
1601 * @param[in] rock opaque pointer passed to judgeInode
1603 * @return operation status
1605 * @retval -1 complete failure, salvage should terminate.
1606 * @retval -2 not enough space on partition, salvager has error message
1610 ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
1611 int (*judgeInode) (struct ViceInodeInfo * info, afs_uint32 vid, void *rock),
1612 afs_uint32 singleVolumeNumber, int *forcep, int forceR, char *wpath,
1617 *forcep = 0; /* no need to salvage until further notice */
1619 /* Verify protections on directories. */
1621 VerifyDirPerms(mountedOn);
1624 namei_ListAFSFiles(mountedOn, WriteInodeInfo, inodeFile, judgeInode,
1625 singleVolumeNumber, rock);
1627 if (inodeFile == INVALID_FD)
1634 if (OS_SYNC(inodeFile) == -1) {
1635 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1640 * Paranoia: check that the file is really the right size
1642 if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
1643 Log("Wrong size (%d instead of %lu) in inode file for %s\n",
1644 (int) OS_SIZE(inodeFile),
1645 (long unsigned int) ninodes * sizeof(struct ViceInodeInfo),
1654 * Collect all the matching AFS files on the drive.
1655 * If singleVolumeNumber is non-zero, just return files for that volume.
1657 * @param[in] dev vice partition path
1658 * @param[in] writeFun function pointer to a function which
1659 * writes inode information to FILE fp
1660 * @param[in] fp file stream where inode metadata is sent
1661 * @param[in] judgeFun filter function pointer. if not NULL,
1662 * only entries for which a non-zero value
1663 * is returned are written to fp
1664 * @param[in] singleVolumeNumber volume id filter. if nonzero, only
1665 * process files for that specific volume id
1666 * @param[in] rock opaque pointer passed into writeFun and
1669 * @return operation status
1671 * @retval >=0 number of matching files found
1674 namei_ListAFSFiles(char *dev,
1675 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1678 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1679 afs_uint32 singleVolumeNumber, void *rock)
1686 #ifndef AFS_NT40_ENV
1692 static void FreeZLCList(void);
1695 memset((void *)&ih, 0, sizeof(IHandle_t));
1697 ih.ih_dev = nt_DriveToDev(dev);
1699 ih.ih_dev = volutil_GetPartitionID(dev);
1702 if (singleVolumeNumber) {
1703 ih.ih_vid = singleVolumeNumber;
1704 namei_HandleToVolDir(&name, &ih);
1706 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1707 singleVolumeNumber, rock);
1711 /* Find all volume data directories and descend through them. */
1712 namei_HandleToInodeDir(&name, &ih);
1714 dirp1 = opendir(name.n_path);
1717 while ((dp1 = readdir(dirp1))) {
1719 /* Heirarchy is one level on Windows */
1720 if (!DecodeVolumeName(dp1->d_name, &ih.ih_vid)) {
1722 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1726 if (*dp1->d_name == '.')
1728 snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", name.n_path,
1730 dirp2 = opendir(path2);
1732 while ((dp2 = readdir(dirp2))) {
1733 if (*dp2->d_name == '.')
1735 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1737 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1754 static void AddToZLCDeleteList(char dir, char *name);
1755 static void DeleteZLCFiles(char *path);
1759 * examine a namei volume special file.
1761 * @param[in] path1 volume special directory path
1762 * @param[in] dname directory entry name
1763 * @param[in] myIH inode handle to volume directory
1764 * @param[out] linkHandle namei link count fd handle. if
1765 * the inode in question is the link
1766 * table, then the FdHandle is populated
1767 * @param[in] writeFun metadata write function pointer
1768 * @param[in] fp file pointer where inode metadata
1769 * is written by (*writeFun)()
1770 * @param[in] judgeFun inode filter function pointer. if
1771 * not NULL, only inodes for which this
1772 * function returns non-zero are recorded
1773 * into fp by writeFun
1774 * @param[in] singleVolumeNumer volume id filter. if non-zero, only
1775 * inodes associated with this volume id
1776 * are recorded by writeFun
1777 * @param[in] rock opaque pointer passed to writeFun and
1780 * @return operation status
1781 * @retval 1 count this inode
1782 * @retval 0 don't count this inode
1783 * @retval -1 failure
1788 _namei_examine_special(char * path1,
1791 FdHandle_t * linkHandle,
1792 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1795 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1796 int singleVolumeNumber,
1800 struct ViceInodeInfo info;
1801 afs_uint32 inode_vgid;
1803 if (DecodeInode(path1, dname, &info, myIH->ih_vid) < 0) {
1809 inode_vgid = myIH->ih_vid;
1811 inode_vgid = (info.inodeNumber >> NAMEI_UNIQSHIFT) & NAMEI_UNIQMASK;
1814 if (info.u.param[2] != VI_LINKTABLE) {
1816 } else if ((info.u.param[0] != myIH->ih_vid) ||
1817 (inode_vgid != myIH->ih_vid)) {
1818 /* VGID encoded in linktable filename and/or OGM data isn't
1819 * consistent with VGID encoded in namei path */
1820 Log("namei_ListAFSSubDirs: warning: inconsistent linktable "
1821 "filename \"%s" OS_DIRSEP "%s\"; salvager will delete it "
1822 "(dir_vgid=%u, inode_vgid=%u, ogm_vgid=%u)\n",
1823 path1, dname, myIH->ih_vid,
1824 (unsigned int)inode_vgid,
1828 /* Open this handle */
1829 snprintf(path2, sizeof(path2),
1830 "%s" OS_DIRSEP "%s", path1, dname);
1831 linkHandle->fd_fd = OS_OPEN(path2, Testing ? O_RDONLY : O_RDWR, 0666);
1833 namei_GetLinkCount(linkHandle, (Inode) 0, 1, 1, Testing);
1837 (*judgeFun) (&info, singleVolumeNumber, rock)) {
1838 ret = (*writeFun) (fp, &info, path1, dname);
1840 Log("_namei_examine_special: writeFun returned %d\n", ret);
1852 * examine a namei file.
1854 * @param[in] path3 volume special directory path
1855 * @param[in] dname directory entry name
1856 * @param[in] myIH inode handle to volume directory
1857 * @param[in] linkHandle namei link count fd handle.
1858 * @param[in] writeFun metadata write function pointer
1859 * @param[in] fp file pointer where inode metadata
1860 * is written by (*writeFun)()
1861 * @param[in] judgeFun inode filter function pointer. if
1862 * not NULL, only inodes for which this
1863 * function returns non-zero are recorded
1864 * into fp by writeFun
1865 * @param[in] singleVolumeNumer volume id filter. if non-zero, only
1866 * inodes associated with this volume id
1867 * are recorded by writeFun
1868 * @param[in] rock opaque pointer passed to writeFun and
1871 * @return operation status
1872 * @retval 1 count this inode
1873 * @retval 0 don't count this inode
1874 * @retval -1 failure
1875 * @retval -2 request ZLC delete
1880 _namei_examine_reg(char * path3,
1883 FdHandle_t * linkHandle,
1884 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1887 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1888 int singleVolumeNumber,
1892 struct ViceInodeInfo info;
1894 int dirl; /* Windows-only (one level hash dir) */
1897 if (DecodeInode(path3, dname, &info, myIH->ih_vid) < 0) {
1902 namei_GetLinkCount(linkHandle,
1903 info.inodeNumber, 1, 1, Testing);
1904 if (info.linkCount == 0) {
1906 Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n", path3, dname);
1907 #ifdef AFS_SALSRV_ENV
1908 /* defer -- the AddToZLCDeleteList() interface is not MT-safe */
1910 #else /* !AFS_SALSRV_ENV */
1911 dirl = path3[strlen(path3)-1];
1912 AddToZLCDeleteList((char)dirl, dname);
1913 #endif /* !AFS_SALSRV_ENV */
1914 #else /* !DELETE_ZLC */
1915 Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path3,
1922 (*judgeFun) (&info, singleVolumeNumber, rock)) {
1923 ret = (*writeFun) (fp, &info, path3, dname);
1925 Log("_namei_examine_reg: writeFun returned %d\n", ret);
1937 * listsubdirs work queue node.
1939 struct listsubdirs_work_node {
1940 #ifdef AFS_SALSRV_ENV
1941 int *error; /**< *error set if an error was
1942 * encountered in any listsubdirs
1946 IHandle_t * IH; /**< volume directory handle */
1947 FdHandle_t *linkHandle; /**< namei link count fd handle. when
1948 * examinining the link table special
1949 * inode, this will be pointed at the
1952 FD_t fp; /**< file pointer for writeFun */
1954 /** function which will write inode metadata to fp */
1955 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *, char *);
1957 /** inode filter function */
1958 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *);
1959 int singleVolumeNumber; /**< volume id filter */
1960 void * rock; /**< pointer passed to writeFun and judgeFun */
1961 int code; /**< return code from examine function */
1962 int special; /**< asserted when this is a volume
1967 * simple wrapper around _namei_examine_special and _namei_examine_reg.
1969 * @param[in] work the struct listsubdirs_work_node for the associated
1970 * "list subdirs" job
1971 * @param[in] dir the directory to examine
1972 * @param[in] filename the filename in 'dir' to examine
1974 * @return operation status
1975 * @retval 1 count this inode
1976 * @retval 0 don't count this inode
1977 * @retval -1 failure
1980 _namei_examine_file(const struct listsubdirs_work_node *work, char *dir,
1983 if (work->special) {
1984 return _namei_examine_special(dir, filename, work->IH,
1985 work->linkHandle, work->writeFun, work->fp,
1986 work->judgeFun, work->singleVolumeNumber,
1989 return _namei_examine_reg(dir, filename, work->IH,
1990 work->linkHandle, work->writeFun, work->fp,
1991 work->judgeFun, work->singleVolumeNumber,
1997 #ifdef AFS_SALSRV_ENV
1998 /** @addtogroup afs_vol_salsrv_pario */
2002 * arguments for the _namei_examine_file_cbk callback function.
2004 struct listsubdirs_args {
2005 const struct listsubdirs_work_node *work; /**< arguments that are the same
2006 * for all invocations of
2007 * _namei_examine_file_cbk, in
2009 int *result; /**< where we can store the return code of _namei_examine_file */
2011 char dir[512]; /**< directory to examine */
2012 char filename[256]; /**< filename in 'dir' to examine */
2016 * a node in the list of results of listsubdir jobs.
2018 struct listsubdirs_result {
2020 int inodes; /**< return value from _namei_examine_file */
2024 * clean up a list of 'struct listsubdirs_result's and interpret the results.
2026 * @param[in] resultlist a list of 'struct listsubdirs_result's
2028 * @return number of inodes found
2032 _namei_listsubdirs_cleanup_results(struct rx_queue *resultlist)
2034 struct listsubdirs_result *res, *nres;
2037 for(queue_Scan(resultlist, res, nres, listsubdirs_result)) {
2039 /* noop, retain erroneous error code */
2040 } else if (res->inodes < 0) {
2055 * wait for the spawned listsubdirs jobs to finish, and return how many inodes
2058 * @param[in] queue queue to wait to finish
2059 * @param[in] resultlist list of 'struct listsubdirs_result's that the queued
2060 * jobs are storing their results in
2062 * @return number of inodes found
2066 _namei_listsubdirs_wait(struct afs_work_queue *queue, struct rx_queue *resultlist)
2070 code = afs_wq_wait_all(queue);
2075 return _namei_listsubdirs_cleanup_results(resultlist);
2079 * work queue entry point for examining namei files.
2081 * @param[in] queue pointer to struct Vwork_queue
2082 * @param[in] node pointer to struct Vwork_queue_node
2083 * @param[in] queue_rock opaque pointer to struct salsrv_pool_state
2084 * @param[in] node_rock opaque pointer to struct listsubdirs_work_node
2085 * @param[in] caller_rock opaque pointer to struct salsrv_worker_thread_state
2087 * @return operation status
2089 * @see Vwork_queue_callback_func_t
2094 _namei_examine_file_cbk(struct afs_work_queue *queue,
2095 struct afs_work_queue_node *node,
2101 struct listsubdirs_args *args = node_rock;
2102 const struct listsubdirs_work_node * work = args->work;
2103 char *dir = args->dir;
2104 char *filename = args->filename;
2106 code = _namei_examine_file(work, dir, filename);
2108 *(args->result) = code;
2112 /* we've errored, so no point in letting more jobs continue */
2113 afs_wq_shutdown(queue);
2119 static pthread_once_t wq_once = PTHREAD_ONCE_INIT;
2120 static pthread_key_t wq_key;
2123 * create the wq_key key for storing a work queue.
2126 _namei_wq_keycreate(void)
2128 osi_Assert(pthread_key_create(&wq_key, NULL) == 0);
2132 * set the work queue for this thread to use for backgrounding namei ops.
2134 * The work queue will be used in ListAFSSubdirs (called indirectly by
2135 * ListViceInodes) to examine files in parallel.
2137 * @param[in] wq the work queue to use
2140 namei_SetWorkQueue(struct afs_work_queue *wq)
2142 osi_Assert(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
2144 osi_Assert(pthread_setspecific(wq_key, wq) == 0);
2148 * enqueue an examine file work unit.
2150 * @param[in] work the _namei_examine_file arguments that are common to
2151 * all callers within the same ListAFSFiles operation
2152 * @param[in] dir the specific directory to look at (string will be
2153 * copied; can be stack/temporary memory)
2154 * @param[in] filename the filename to look at (string will be copied; can be
2155 * stack/temporary memory)
2156 * @param[in] wq work queue to enqueue this work unit to
2157 * @param[in] resultlist the list to append the 'struct listsubdirs_result' to
2158 * for the enqueued work unit
2160 * @return operation status
2162 * @retval -1 fatal error
2164 * @note errors MUST be indicated by a -1 error code and nothing else, to be
2165 * compatible with _namei_examine_reg and _namei_examine_special
2170 _namei_examine_file_spawn(const struct listsubdirs_work_node *work,
2171 const char *dir, const char *filename,
2172 struct afs_work_queue *wq,
2173 struct rx_queue *resultlist)
2176 struct listsubdirs_args *args = NULL;
2177 struct listsubdirs_result *result = NULL;
2178 struct afs_work_queue_node *node = NULL;
2179 struct afs_work_queue_add_opts opts;
2181 args = malloc(sizeof(*args));
2187 result = malloc(sizeof(*result));
2188 if (result == NULL) {
2193 code = afs_wq_node_alloc(&node);
2198 code = afs_wq_node_set_detached(node);
2205 args->result = &result->inodes;
2206 strlcpy(args->dir, dir, sizeof(args->dir));
2207 strlcpy(args->filename, filename, sizeof(args->filename));
2209 code = afs_wq_node_set_callback(node,
2210 &_namei_examine_file_cbk,
2218 afs_wq_add_opts_init(&opts);
2221 code = afs_wq_add(wq, node, &opts);
2228 queue_Append(resultlist, result);
2233 afs_wq_node_put(node);
2249 #else /* !AFS_SALSRV_ENV */
2250 # define _namei_examine_file_spawn(work, dir, file, wq, resultlist) \
2251 _namei_examine_file(work, dir, file)
2252 #endif /* !AFS_SALSRV_ENV */
2255 * traverse and check inodes.
2257 * @param[in] dirIH volume group directory handle
2258 * @param[in] writeFun function pointer which will write inode
2259 * metadata to FILE stream fp
2260 * @param[in] fp file stream where inode metadata gets
2262 * @param[in] judgeFun inode filter function. if not NULL, only
2263 * inodes for which the filter returns non-zero
2264 * will be written out by writeFun
2265 * @param[in] singleVolumeNumber volume id filter. only inodes matching this
2266 * filter are written out by writeFun
2267 * @param[in] rock opaque pointer passed to judgeFun and writeFun
2269 * @return operation status
2271 * @retval >=0 number of matching inodes found
2273 * @todo the salsrv implementation may consume a lot of
2274 * memory for a large volume. at some point we should
2275 * probably write a background thread to asynchronously
2276 * clean up the resultlist nodes to reduce memory footprint
2281 namei_ListAFSSubDirs(IHandle_t * dirIH,
2282 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
2285 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
2286 afs_uint32 singleVolumeNumber, void *rock)
2288 int code = 0, ret = 0;
2289 IHandle_t myIH = *dirIH;
2291 char path1[512], path3[512];
2293 #ifndef AFS_NT40_ENV
2298 struct dirent *dp1, *dp3;
2299 FdHandle_t linkHandle;
2301 struct listsubdirs_work_node work;
2302 #ifdef AFS_SALSRV_ENV
2304 struct afs_work_queue *wq;
2306 struct rx_queue resultlist;
2309 namei_HandleToVolDir(&name, &myIH);
2310 strlcpy(path1, name.n_path, sizeof(path1));
2312 /* Do the directory containing the special files first to pick up link
2315 (void)strcat(path1, OS_DIRSEP);
2316 (void)strcat(path1, NAMEI_SPECDIR);
2318 linkHandle.fd_fd = INVALID_FD;
2319 #ifdef AFS_SALSRV_ENV
2320 osi_Assert(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
2322 wq = pthread_getspecific(wq_key);
2328 queue_Init(&resultlist);
2331 memset(&work, 0, sizeof(work));
2332 work.linkHandle = &linkHandle;
2335 work.writeFun = writeFun;
2336 work.judgeFun = judgeFun;
2337 work.singleVolumeNumber = singleVolumeNumber;
2340 #ifdef AFS_SALSRV_ENV
2341 work.error = &error;
2344 dirp1 = opendir(path1);
2346 while ((dp1 = readdir(dirp1))) {
2347 if (*dp1->d_name == '.')
2350 #ifdef AFS_SALSRV_ENV
2356 #endif /* AFS_SALSRV_ENV */
2358 code = _namei_examine_file_spawn(&work, path1, dp1->d_name, wq, &resultlist);
2368 /* count this inode */
2369 #ifndef AFS_SALSRV_ENV
2378 #ifdef AFS_SALSRV_ENV
2379 /* effectively a barrier */
2380 code = _namei_listsubdirs_wait(wq, &resultlist);
2381 if (code < 0 || error) {
2389 if (linkHandle.fd_fd == INVALID_FD) {
2390 Log("namei_ListAFSSubDirs: warning: VG %u does not have a link table; "
2391 "salvager will recreate it.\n", dirIH->ih_vid);
2394 /* Now run through all the other subdirs */
2395 namei_HandleToVolDir(&name, &myIH);
2396 strlcpy(path1, name.n_path, sizeof(path1));
2400 dirp1 = opendir(path1);
2402 while ((dp1 = readdir(dirp1))) {
2403 #ifndef AFS_NT40_ENV
2404 if (*dp1->d_name == '.')
2407 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
2410 #ifndef AFS_NT40_ENV /* This level missing on Windows */
2411 /* Now we've got a next level subdir. */
2412 snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s",
2413 path1, dp1->d_name);
2414 dirp2 = opendir(path2);
2416 while ((dp2 = readdir(dirp2))) {
2417 if (*dp2->d_name == '.')
2420 /* Now we've got to the actual data */
2421 snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
2422 path2, dp2->d_name);
2424 /* Now we've got to the actual data */
2425 snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
2426 path1, dp1->d_name);
2428 dirp3 = opendir(path3);
2430 while ((dp3 = readdir(dirp3))) {
2431 #ifndef AFS_NT40_ENV
2432 if (*dp3->d_name == '.')
2436 #ifdef AFS_SALSRV_ENV
2439 #ifndef AFS_NT40_ENV
2446 #endif /* AFS_SALSRV_ENV */
2448 code = _namei_examine_file_spawn(&work, path3,
2455 #ifndef AFS_NT40_ENV
2463 #ifndef AFS_SALSRV_ENV
2471 #ifndef AFS_NT40_ENV /* This level missing on Windows */
2480 #ifdef AFS_SALSRV_ENV
2481 /* effectively a barrier */
2482 code = _namei_listsubdirs_wait(wq, &resultlist);
2483 if (code < 0 || error) {
2493 /* Then why does this directory exist? Blow it away. */
2494 namei_HandleToVolDir(&name, dirIH);
2495 namei_RemoveDataDirectories(&name);
2499 #ifdef AFS_SALSRV_ENV
2501 afs_wq_wait_all(wq);
2503 _namei_listsubdirs_cleanup_results(&resultlist);
2505 if (linkHandle.fd_fd != INVALID_FD)
2506 OS_CLOSE(linkHandle.fd_fd);
2518 DecodeVolumeName(char *name, unsigned int *vid)
2520 /* Name begins with "Vol_" and ends with .data. See nt_HandleToVolDir() */
2527 if (strncmp(name, "Vol_", 4))
2529 if (strcmp(name + len - 5, ".data"))
2532 stmp[len - 5] = '\0';
2533 *vid = base32_to_int(stmp + 4);
2538 DecodeVolumeName(char *name, unsigned int *vid)
2540 if (strlen(name) < 1)
2542 *vid = (unsigned int)flipbase64_to_int64(name);
2550 * Get the inode number from the name.
2555 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
2560 WIN32_FIND_DATA data;
2564 FdHandle_t linkHandle;
2567 snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
2569 dirH = FindFirstFileEx(fpath, FindExInfoStandard, &data,
2570 FindExSearchNameMatch, NULL,
2571 FIND_FIRST_EX_CASE_SENSITIVE);
2572 if (dirH == INVALID_HANDLE_VALUE)
2575 (void)strcpy(stmp, name);
2576 s = strrchr(stmp, '_');
2580 t = strrchr(s, '.');
2585 vno = base32_to_int(s); /* type for special files */
2586 tag = base32_to_int(t+1);
2587 info->inodeNumber = ((Inode) tag) << NAMEI_TAGSHIFT;
2588 info->inodeNumber |= vno;
2589 info->byteCount = data.nFileSizeLow;
2591 dirl = dpath[strlen(dpath)-1];
2592 if (dirl == NAMEI_SPECDIRC) { /* Special inode. */
2593 info->inodeNumber |= NAMEI_INODESPECIAL;
2594 info->u.param[0] = data.ftCreationTime.dwHighDateTime;
2595 info->u.param[1] = data.ftCreationTime.dwLowDateTime;
2596 info->u.param[2] = vno; /* type */
2597 info->u.param[3] = volid;
2598 if (vno != VI_LINKTABLE)
2599 info->linkCount = 1;
2601 /* Open this handle */
2603 (void)sprintf(lpath, "%s" OS_DIRSEP "%s", fpath, data.cFileName);
2604 linkHandle.fd_fd = OS_OPEN(lpath, O_RDONLY, 0666);
2606 namei_GetLinkCount(&linkHandle, (Inode) 0, 0, 0, 0);
2610 namei_GetLinkCount(&linkHandle, info->inodeNumber, 0, 0, 0);
2611 if (info->linkCount == 0) {
2613 Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n",
2614 fpath, data.cFileName);
2615 AddToZLCDeleteList(dirl, data.cFileName);
2617 Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path,
2621 info->u.param[2] = data.ftCreationTime.dwHighDateTime;
2622 info->u.param[3] = data.ftCreationTime.dwLowDateTime;
2623 info->u.param[1] = vno;
2624 info->u.param[0] = volid;
2631 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
2635 struct afs_stat_st status;
2637 lb64_string_t check;
2639 snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
2641 if (afs_stat(fpath, &status) < 0) {
2645 info->byteCount = status.st_size;
2646 info->inodeNumber = (Inode) flipbase64_to_int64(name);
2648 int64_to_flipbase64(check, info->inodeNumber);
2649 if (strcmp(name, check))
2652 GetOGMFromStat(&status, &parm, &tag);
2653 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
2654 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
2655 info->u.param[0] = parm;
2656 info->u.param[1] = -1;
2657 info->u.param[2] = tag;
2658 info->u.param[3] = volid;
2660 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
2661 info->u.param[0] = volid;
2662 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
2663 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
2664 & (Inode) NAMEI_UNIQMASK);
2665 info->u.param[3] = parm;
2672 * Convert the VolumeInfo file from RO to RW
2673 * this routine is called by namei_convertROtoRWvolume()
2676 #ifdef FSSYNC_BUILD_CLIENT
2678 convertVolumeInfo(FD_t fdr, FD_t fdw, afs_uint32 vid)
2680 struct VolumeDiskData vd;
2683 if (OS_READ(fdr, &vd, sizeof(struct VolumeDiskData)) !=
2684 sizeof(struct VolumeDiskData)) {
2685 Log("1 convertVolumeInfo: read failed for %lu with code %d\n",
2686 afs_printable_uint32_lu(vid),
2690 vd.restoredFromId = vd.id; /* remember the RO volume here */
2692 vd.id = vd.parentId;
2696 vd.uniquifier += 5000; /* just in case there are still file copies from
2697 * the old RW volume around */
2699 /* For ROs, the copyDate contains the time that the RO volume was actually
2700 * created, and the creationDate just contains the last time the RO was
2701 * copied from the RW data. So, make the new RW creationDate more accurate
2702 * by setting it to copyDate, if copyDate is older. Since, we know the
2703 * volume is at least as old as copyDate. */
2704 if (vd.copyDate < vd.creationDate) {
2705 vd.creationDate = vd.copyDate;
2707 /* If copyDate is newer, just make copyDate and creationDate the same,
2708 * for consistency with other RWs */
2709 vd.copyDate = vd.creationDate;
2712 p = strrchr(vd.name, '.');
2713 if (p && !strcmp(p, ".readonly")) {
2716 if (OS_WRITE(fdw, &vd, sizeof(struct VolumeDiskData)) !=
2717 sizeof(struct VolumeDiskData)) {
2718 Log("1 convertVolumeInfo: write failed for %lu with code %d\n",
2719 afs_printable_uint32_lu(vid),
2728 * Convert a RO-volume into a RW-volume
2730 * This function allows to recover very fast from the loss of a partition
2731 * from RO-copies if all RO-Copies exist on another partition.
2732 * Then these RO-volumes can be made to the new RW-volumes.
2733 * Backup of RW-volumes then consists in "vos release".
2735 * We must make sure in this partition exists only the RO-volume which
2736 * is typical for remote replicas.
2738 * Then the linktable is already ok,
2739 * the vnode files need to be renamed
2740 * the volinfo file needs to be replaced by another one with
2741 * slightly different contents and new name.
2742 * The volume header file of the RO-volume in the /vicep<x> directory
2743 * is destroyed by this call. A new header file for the RW-volume must
2744 * be created after return from this routine.
2748 namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
2751 #ifdef FSSYNC_BUILD_CLIENT
2753 char dir_name[512], oldpath[512], newpath[512];
2768 struct DiskPartition64 *partP;
2769 struct ViceInodeInfo info;
2770 struct VolumeDiskHeader h;
2771 # ifdef AFS_DEMAND_ATTACH_FS
2773 # endif /* AFS_DEMAND_ATTACH_FS */
2775 for (partP = DiskPartitionList; partP && strcmp(partP->name, pname);
2776 partP = partP->next);
2778 Log("1 namei_ConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
2783 # ifdef AFS_DEMAND_ATTACH_FS
2784 locktype = VVolLockType(V_VOLUPD, 1);
2785 code = VLockVolumeByIdNB(volumeId, partP, locktype);
2791 # endif /* AFS_DEMAND_ATTACH_FS */
2793 if (VReadVolumeDiskHeader(volumeId, partP, &h)) {
2794 Log("1 namei_ConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n",
2795 afs_printable_uint32_lu(volumeId));
2800 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
2802 ino = namei_MakeSpecIno(h.parent, VI_LINKTABLE);
2803 IH_INIT(ih, partP->device, h.parent, ino);
2805 namei_HandleToName(&n, ih);
2806 strlcpy(dir_name, n.n_path, sizeof(dir_name));
2807 p = strrchr(dir_name, OS_DIRSEPC);
2809 dirp = opendir(dir_name);
2811 Log("1 namei_ConvertROtoRWvolume: Could not opendir(%s)\n", dir_name);
2816 while ((dp = readdir(dirp))) {
2817 /* struct ViceInodeInfo info; */
2818 #ifndef AFS_NT40_ENV
2819 if (*dp->d_name == '.')
2822 if (DecodeInode(dir_name, dp->d_name, &info, ih->ih_vid) < 0) {
2823 Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s" OS_DIRSEP "%s\n",
2824 dir_name, dp->d_name);
2829 if (info.u.param[1] != -1) {
2830 Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s" OS_DIRSEP "%s\n", dir_name, dp->d_name);
2835 if (info.u.param[0] != volumeId) {
2836 if (info.u.param[0] == ih->ih_vid) {
2837 if (info.u.param[2] == VI_LINKTABLE) { /* link table */
2842 Log("1 namei_ConvertROtoRWvolume: found special file %s" OS_DIRSEP "%s"
2843 " for volume %lu\n", dir_name, dp->d_name,
2844 afs_printable_uint32_lu(info.u.param[0]));
2849 if (info.u.param[2] == VI_VOLINFO) { /* volume info file */
2850 strlcpy(infoName, dp->d_name, sizeof(infoName));
2852 } else if (info.u.param[2] == VI_SMALLINDEX) { /* small vnodes file */
2853 strlcpy(smallName, dp->d_name, sizeof(smallName));
2855 } else if (info.u.param[2] == VI_LARGEINDEX) { /* large vnodes file */
2856 strlcpy(largeName, dp->d_name, sizeof(largeName));
2860 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);
2867 if (!infoSeen || !smallSeen || !largeSeen || !linkSeen) {
2868 Log("1 namei_ConvertROtoRWvolume: not all special files found in %s\n", dir_name);
2874 * If we come here then there was only a RO-volume and we can safely
2878 memset(&t_ih, 0, sizeof(t_ih));
2879 t_ih.ih_dev = ih->ih_dev;
2880 t_ih.ih_vid = ih->ih_vid;
2882 snprintf(oldpath, sizeof oldpath, "%s" OS_DIRSEP "%s", dir_name,
2884 fd = OS_OPEN(oldpath, O_RDWR, 0);
2885 if (fd == INVALID_FD) {
2886 Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
2891 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_VOLINFO);
2892 namei_HandleToName(&n, &t_ih);
2893 fd2 = OS_OPEN(n.n_path, O_CREAT | O_EXCL | O_RDWR, 0);
2894 if (fd2 == INVALID_FD) {
2895 Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
2900 code = convertVolumeInfo(fd, fd2, ih->ih_vid);
2904 OS_UNLINK(n.n_path);
2908 SetOGM(fd2, ih->ih_vid, 1);
2911 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_SMALLINDEX);
2912 namei_HandleToName(&n, &t_ih);
2913 snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
2915 fd = OS_OPEN(newpath, O_RDWR, 0);
2916 if (fd == INVALID_FD) {
2917 Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
2921 SetOGM(fd, ih->ih_vid, 2);
2924 MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
2926 link(newpath, n.n_path);
2930 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_LARGEINDEX);
2931 namei_HandleToName(&n, &t_ih);
2932 snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
2934 fd = OS_OPEN(newpath, O_RDWR, 0);
2935 if (fd == INVALID_FD) {
2936 Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
2940 SetOGM(fd, ih->ih_vid, 3);
2943 MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
2945 link(newpath, n.n_path);
2952 h.volumeInfo_hi = h.id;
2953 h.smallVnodeIndex_hi = h.id;
2954 h.largeVnodeIndex_hi = h.id;
2955 h.linkTable_hi = h.id;
2957 if (VCreateVolumeDiskHeader(&h, partP)) {
2958 Log("1 namei_ConvertROtoRWvolume: Couldn't write header for RW-volume %lu\n",
2959 afs_printable_uint32_lu(h.id));
2964 if (VDestroyVolumeDiskHeader(partP, volumeId, h.parent)) {
2965 Log("1 namei_ConvertROtoRWvolume: Couldn't unlink header for RO-volume %lu\n",
2966 afs_printable_uint32_lu(volumeId));
2969 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
2970 FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
2973 # ifdef AFS_DEMAND_ATTACH_FS
2975 VUnlockVolumeById(volumeId, partP);
2977 # endif /* AFS_DEMAND_ATTACH_FS */
2985 * returns a static string used to print either 32 or 64 bit inode numbers.
2988 PrintInode(char *s, Inode ino)
2990 static afs_ino_str_t result;
2994 snprintf(s, sizeof(afs_ino_str_t), "%llu", (afs_uintmax_t) ino);
3001 /* Routines to facilitate removing zero link count files. */
3002 #define MAX_ZLC_NAMES 32
3003 #define MAX_ZLC_NAMELEN 16
3004 typedef struct zlcList_s {
3005 struct zlcList_s *zlc_next;
3007 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
3010 static zlcList_t *zlcAnchor = NULL;
3011 static zlcList_t *zlcCur = NULL;
3014 AddToZLCDeleteList(char dir, char *name)
3016 osi_Assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
3018 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
3019 if (zlcCur && zlcCur->zlc_next)
3020 zlcCur = zlcCur->zlc_next;
3022 zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
3028 zlcCur->zlc_next = tmp;
3032 zlcCur->zlc_next = NULL;
3037 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c" OS_DIRSEP "%s", dir, name);
3039 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%s", name);
3045 DeleteZLCFiles(char *path)
3051 for (z = zlcAnchor; z; z = z->zlc_next) {
3052 for (i = 0; i < z->zlc_n; i++) {
3054 (void)sprintf(fname, "%s" OS_DIRSEP "%s", path, z->zlc_names[i]);
3056 (void)sprintf(fname, "%s", z->zlc_names[i]);
3057 if (namei_unlink(fname) < 0) {
3058 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
3062 z->zlc_n = 0; /* Can reuse space. */
3075 tnext = i->zlc_next;
3079 zlcCur = zlcAnchor = NULL;
3083 #endif /* AFS_NAMEI_ENV */