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);
88 namei_iwrite(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
97 nBytes = FDH_PWRITE(fdP, buf, size, offset);
106 /* Inode number format:
107 * low 32 bits - if a regular file or directory, the vnode; else the type.
108 * 32-36 - uniquifier tag and index into counts array for this vnode. Only
109 * two of the available bits are currently used. The rest are
110 * present in case we ever increase the number of types of volumes
111 * in the volume group.
112 * bit 37 : 1 == special, 0 == regular
114 # define NAMEI_VNODEMASK 0x00ffffffff
115 # define NAMEI_TAGSHIFT 32
116 # define NAMEI_INODESPECIAL 0x2000000000
117 # define NAMEI_SPECDIR "R"
118 # define NAMEI_SPECDIRC 'R'
119 #else /* !AFS_NT40_ENV */
120 /* Inode number format:
121 * low 26 bits - vnode number - all 1's if volume special file.
123 * next 3 bits spare (0's)
124 * high 32 bits - uniquifier (regular) or type if spare
126 # define NAMEI_VNODEMASK 0x003ffffff
127 # define NAMEI_TAGSHIFT 26
128 # define NAMEI_UNIQMASK 0xffffffff
129 # define NAMEI_UNIQSHIFT 32
130 # define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
131 /* dir1 is the high 8 bits of the 26 bit vnode */
132 # define VNO_DIR1(vno) ((vno >> 14) & 0xff)
133 /* dir2 is the next 9 bits */
134 # define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
135 /* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
136 # define NAMEI_SPECDIR "special"
137 #endif /* !AFS_NT40_ENV */
138 #define NAMEI_TAGMASK 0x7
139 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
141 #define NAMEI_SPECDIRLEN (sizeof(NAMEI_SPECDIR)-1)
143 #define NAMEI_MAXVOLS 5 /* Maximum supported number of volumes per volume
144 * group, not counting temporary (move) volumes.
145 * This is the number of separate files, all having
146 * the same vnode number, which can occur in a volume
158 static int GetFreeTag(IHandle_t * ih, int vno);
160 /* namei_HandleToInodeDir
162 * Construct the path name of the directory holding the inode data.
163 * Format: /<vicepx>/INODEDIR
168 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
170 memset(name, '\0', sizeof(*name));
171 nt_DevToDrive(name->n_drive, ih->ih_dev);
172 strlcpy(name->n_path, name->n_drive, sizeof(name->n_path));
176 /* Format: /<vicepx>/INODEDIR */
177 #define PNAME_BLEN 64
179 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
183 memset(name, '\0', sizeof(*name));
186 * Add the /vicepXX string to the start of name->n_base and then calculate
187 * offset as the number of bytes we know we added.
189 * FIXME: This embeds knowledge of the vice partition naming scheme and
190 * mapping from device numbers. There needs to be an API that tells us
193 volutil_PartitionName_r(ih->ih_dev, name->n_base, sizeof(name->n_base));
194 offset = VICE_PREFIX_SIZE + (ih->ih_dev > 25 ? 2 : 1);
195 name->n_base[offset] = OS_DIRSEPC;
197 strlcpy(name->n_base + offset, INODEDIR, sizeof(name->n_base) - offset);
198 strlcpy(name->n_path, name->n_base, sizeof(name->n_path));
202 #define addtoname(N, C) \
204 if ((N)->n_path[strlen((N)->n_path)-1] != OS_DIRSEPC) \
205 strlcat((N)->n_path, OS_DIRSEP, sizeof((N)->n_path)); \
206 strlcat((N)->n_path, (C), sizeof((N)->n_path)); \
212 namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
214 /* X:\Vol_XXXXXXX.data */
218 namei_HandleToInodeDir(name, ih);
219 /* nt_drive added to name by namei_HandleToInodeDir() */
220 namep = name->n_voldir;
221 (void)memcpy(namep, "\\Vol_", 5);
223 (void)strcpy(namep, int_to_base32(str1, ih->ih_vid));
224 namep += strlen(namep);
225 memcpy(namep, ".data", 5);
228 addtoname(name, name->n_voldir);
232 namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
236 namei_HandleToInodeDir(name, ih);
237 (void)int32_to_flipbase64(tmp, (int64_t) (ih->ih_vid & 0xff));
238 strlcpy(name->n_voldir1, tmp, sizeof(name->n_voldir1));
239 addtoname(name, name->n_voldir1);
240 (void)int32_to_flipbase64(tmp, (int64_t) ih->ih_vid);
241 strlcpy(name->n_voldir2, tmp, sizeof(name->n_voldir2));
242 addtoname(name, name->n_voldir2);
246 /* namei_HandleToName
248 * Constructs a file name for the fully qualified handle.
251 /* Note that special files end up in X:\Vol_XXXXXXX.data\R */
253 namei_HandleToName(namei_t * name, IHandle_t * ih)
255 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
256 int special = (ih->ih_ino & NAMEI_INODESPECIAL)?1:0;
257 int tag = (int)((ih->ih_ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
260 namei_HandleToVolDir(name, ih);
263 name->n_dir[0] = NAMEI_SPECDIRC;
266 name->n_dir[0] = 'Q';
268 name->n_dir[0] = ((vno & 0x1f) >> 1) + 'A';
271 name->n_dir[1] = '\0';
272 addtoname(name, name->n_dir);
273 /* X:\Vol_XXXXXXX.data\X\V_XXXXXXX.XXX */
274 namep = name->n_inode;
275 (void)memcpy(namep, "\\V_", 3);
277 (void)strcpy(namep, int_to_base32(str1, vno));
278 namep += strlen(namep);
280 (void)strcpy(namep, int_to_base32(str1, tag));
281 namep += strlen(namep);
282 addtoname(name, name->n_inode);
285 /* Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special */
287 namei_HandleToName(namei_t * name, IHandle_t * ih)
289 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
292 namei_HandleToVolDir(name, ih);
294 if (vno == NAMEI_VNODESPECIAL) {
295 strlcpy(name->n_dir1, NAMEI_SPECDIR, sizeof(name->n_dir1));
296 addtoname(name, name->n_dir1);
297 name->n_dir2[0] = '\0';
299 (void)int32_to_flipbase64(str, VNO_DIR1(vno));
300 strlcpy(name->n_dir1, str, sizeof(name->n_dir1));
301 addtoname(name, name->n_dir1);
302 (void)int32_to_flipbase64(str, VNO_DIR2(vno));
303 strlcpy(name->n_dir2, str, sizeof(name->n_dir2));
304 addtoname(name, name->n_dir2);
306 (void)int64_to_flipbase64(str, (int64_t) ih->ih_ino);
307 strlcpy(name->n_inode, str, sizeof(name->n_inode));
308 addtoname(name, name->n_inode);
313 /* The following is a warning to tell sys-admins to not muck about in this
316 #define VICE_README "These files and directories are a part of the AFS \
317 namespace. Modifying them\nin any way will result in loss of AFS data,\n\
318 ownership and permissions included.\n"
320 namei_ViceREADME(char *partition)
325 /* Create the inode directory if we're starting for the first time */
326 snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s", partition,
328 mkdir(filename, 0700);
330 snprintf(filename, sizeof filename,
331 "%s" OS_DIRSEP "%s" OS_DIRSEP "README",
332 partition, INODEDIR);
333 fd = OS_OPEN(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
334 if (fd != INVALID_FD) {
335 (void)OS_WRITE(fd, VICE_README, strlen(VICE_README));
342 /* namei_CreateDataDirectories
344 * If creating the file failed because of ENOENT or ENOTDIR, try
345 * creating all the directories first.
349 namei_CreateDataDirectories(namei_t * name, int *created)
356 snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
358 if (mkdir(tmp) < 0) {
369 for (i = 'A'; i <= NAMEI_SPECDIRC; i++) {
371 if (mkdir(tmp) < 0 && errno != EEXIST)
377 #define create_dir() \
379 if (mkdir(tmp, 0700)<0) { \
380 if (errno != EEXIST) \
388 #define create_nextdir(A) \
390 strcat(tmp, OS_DIRSEP); strcat(tmp, A); create_dir(); \
394 namei_CreateDataDirectories(namei_t * name, int *created)
400 strlcpy(tmp, name->n_base, sizeof(tmp));
403 create_nextdir(name->n_voldir1);
404 create_nextdir(name->n_voldir2);
405 create_nextdir(name->n_dir1);
406 if (name->n_dir2[0]) {
407 create_nextdir(name->n_dir2);
414 /* delTree(): Deletes an entire tree of directories (no files)
416 * root : Full path to the subtree. Should be big enough for PATH_MAX
417 * tree : the subtree to be deleted is rooted here. Specifies only the
418 * subtree beginning at tree (not the entire path). It should be
419 * a pointer into the "root" buffer.
421 * errp : errno of the first error encountered during the directory cleanup.
422 * *errp should have been initialized to 0.
425 * -1 : If errors were encountered during cleanup and error is set to
429 * If there are errors, we try to work around them and delete as many
430 * directories as possible. We don't attempt to remove directories that still
431 * have non-dir entries in them.
434 delTree(char *root, char *tree, int *errp)
442 /* delete the children first */
443 cp = strchr(tree, OS_DIRSEPC);
445 delTree(root, cp + 1, errp);
448 cp = tree + strlen(tree); /* move cp to the end of string tree */
450 /* now delete all entries in this dir */
451 if ((ds = opendir(root)) != (DIR *) NULL) {
453 while ((dirp = readdir(ds))) {
454 /* ignore . and .. */
455 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
457 /* since root is big enough, we reuse the space to
458 * concatenate the dirname to the current tree
460 strcat(root, OS_DIRSEP);
461 strcat(root, dirp->d_name);
462 if (afs_stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
463 /* delete this subtree */
464 delTree(root, cp + 1, errp);
466 *errp = *errp ? *errp : errno;
468 /* recover path to our cur tree by truncating it to
473 /* if (!errno) -- closedir not implicit if we got an error */
477 /* finally axe the current dir */
479 *errp = *errp ? *errp : errno;
481 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
483 #endif /* !AFS_PTHREAD_ENV */
488 /* if we encountered errors during cleanup, we return a -1 */
497 /* namei_RemoveDataDirectories
499 * Returns 0 on success.
500 * Returns -1 on error. Typically, callers ignore this error because we
501 * can continue running if the removes fail. The salvage process will
502 * finish tidying up for us.
507 namei_RemoveDataDirectories(namei_t * name)
514 snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
517 path += strlen(path);
518 *path++ = OS_DIRSEPC;
520 for (i = 'A'; i <= NAMEI_SPECDIRC; i++) {
522 if (rmdir(name->n_path) < 0 && errno != ENOENT)
527 /* Delete the Vol_NNNNNN.data directory. */
530 if (rmdir(name->n_path) < 0 && errno != ENOENT) {
538 * We only use the n_base and n_voldir1 entries
539 * and only do rmdir's.
542 namei_RemoveDataDirectories(namei_t * name)
546 int prefixlen = strlen(name->n_base), err = 0;
547 int vollen = strlen(name->n_voldir1);
548 char pbuf[MAXPATHLEN];
552 strlcpy(path, name->n_path, sizeof(pbuf));
554 /* move past the prefix and n_voldir1 */
555 path = path + prefixlen + 1 + vollen + 1; /* skip over the trailing / */
557 /* now delete all dirs upto path */
558 code = delTree(pbuf, path, &err);
560 /* We've now deleted everything under /n_base/n_voldir1/n_voldir2 that
561 * we could. Do not delete /n_base/n_voldir1, since doing such might
562 * interrupt another thread trying to create a volume. We could introduce
563 * some locking to make this safe (or only remove it for whole-partition
564 * salvages), but by not deleting it we only leave behind a maximum of
565 * 256 empty directories. So at least for now, don't bother. */
570 /* Create the file in the name space.
572 * Parameters stored as follows:
574 * p1 - volid - implied in containing directory.
575 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
576 * p3 - uniq -- bits 4-0 are in mode bits 4-0
577 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
579 * p1 - volid - creation time - dwHighDateTime
580 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
581 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
582 * p4 - parid - parent volume id - implied in containing directory.
584 * Return value is the inode number or (Inode)-1 if error.
585 * We "know" there is only one link table, so return EEXIST if there already
586 * is a link table. It's up to the calling code to test errno and increment
592 * This function is called by VCreateVolume to hide the implementation
593 * details of the inode numbers. This only allows for 7 volume special
594 * types, but if we get that far, this could should be dead by then.
597 namei_MakeSpecIno(int volid, int type)
600 ino = NAMEI_INODESPECIAL;
603 /* tag is always 0 for special */
605 type &= NAMEI_TAGMASK;
606 ino |= ((Inode) type) << NAMEI_TAGSHIFT;
607 ino |= ((Inode) volid) << NAMEI_UNIQSHIFT;
615 SetOGM(FD_t fd, int parm, int tag)
621 CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
623 WIN32_FIND_DATA info;
627 FindFirstFileEx(name->n_path, FindExInfoStandard, &info,
628 FindExSearchNameMatch, NULL,
629 FIND_FIRST_EX_CASE_SENSITIVE);
632 return -1; /* Can't get info, leave alone */
636 if (info.ftCreationTime.dwHighDateTime != (unsigned int)p1)
641 #else /* AFS_NT40_ENV */
642 /* SetOGM - set owner group and mode bits from parm and tag */
644 SetOGM(FD_t fd, int parm, int tag)
647 * owner - low 15 bits of parm.
648 * group - next 15 bits of parm.
649 * mode - 2 bits of parm, then lowest = 3 bits of tag.
651 int owner, group, mode;
653 owner = parm & 0x7fff;
654 group = (parm >> 15) & 0x7fff;
655 if (fchown(fd, owner, group) < 0)
658 mode = (parm >> 27) & 0x18;
660 if (fchmod(fd, mode) < 0)
665 /* GetOGM - get parm and tag from owner, group and mode bits. */
667 GetOGMFromStat(struct afs_stat_st *status, int *parm, int *tag)
669 *parm = status->st_uid | (status->st_gid << 15);
670 *parm |= (status->st_mode & 0x18) << 27;
671 *tag = status->st_mode & 0x7;
675 CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
677 struct afs_stat_st status;
679 if (afs_fstat(fdP->fd_fd, &status) < 0)
682 GetOGMFromStat(&status, &parm, &tag);
688 #endif /* !AFS_NT40_ENV */
690 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
692 /* Derive the name and create it O_EXCL. If that fails we have an error.
693 * Get the tag from a free column in the link table.
697 namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
700 FD_t fd = INVALID_FD;
711 memset((void *)&tmp, 0, sizeof(IHandle_t));
712 memset(&tfd, 0, sizeof(FdHandle_t));
714 tmp.ih_dev = nt_DriveToDev(part);
715 if (tmp.ih_dev == -1) {
720 if (p2 == INODESPECIAL) {
721 /* Parameters for special file:
722 * p1 - volume id - goes into owner/group/mode
723 * p2 - vnode == INODESPECIAL
725 * p4 - parent volume id
727 ftime.dwHighDateTime = p1;
728 ftime.dwLowDateTime = p2;
730 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
731 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
733 int vno = p2 & NAMEI_VNODEMASK;
734 /* Parameters for regular file:
748 tmp.ih_ino = (Inode) p2;
749 ftime.dwHighDateTime = p3;
750 ftime.dwLowDateTime = p4;
753 namei_HandleToName(&name, &tmp);
754 p = strrchr((char *)&name.n_path, '.');
756 for (tag = 0; tag < NAMEI_MAXVOLS; tag++) {
757 *p = *int_to_base32(str1, tag);
758 fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_EXCL, 0666);
759 if (fd == INVALID_FD) {
760 if (errno == ENOTDIR || errno == ENOENT) {
761 if (namei_CreateDataDirectories(&name, &created_dir) == 0)
762 fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_EXCL, 0666);
766 if (fd != INVALID_FD)
768 if (p2 == INODESPECIAL && p3 == VI_LINKTABLE)
771 if (fd == INVALID_FD) {
775 tmp.ih_ino &= ~(((Inode) NAMEI_TAGMASK) << NAMEI_TAGSHIFT);
776 tmp.ih_ino |= (((Inode) tag) << NAMEI_TAGSHIFT);
779 if (!SetFileTime((HANDLE) fd, &ftime, NULL, NULL)) {
780 errno = OS_ERROR(EBADF);
786 if (p2 != INODESPECIAL) {
787 if (fd == INVALID_FD) {
789 code = nt_unlink((char *)&name.n_path);
798 code = namei_SetLinkCount(fdP, tmp.ih_ino, 1, 0);
800 } else if (p2 == INODESPECIAL && p3 == VI_LINKTABLE) {
801 if (fd == INVALID_FD)
803 /* hack at tmp to setup for set link count call. */
805 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
810 if (fd != INVALID_FD)
813 if (code || (fd == INVALID_FD)) {
814 if (p2 != INODESPECIAL) {
817 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
823 int save_errno = errno;
824 namei_RemoveDataDirectories(&name);
828 return (code || (fd == INVALID_FD)) ? (Inode) -1 : tmp.ih_ino;
830 #else /* !AFS_NT40_ENV */
832 namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
844 memset((void *)&tmp, 0, sizeof(IHandle_t));
845 memset(&tfd, 0, sizeof(FdHandle_t));
847 tmp.ih_dev = volutil_GetPartitionID(part);
848 if (tmp.ih_dev == -1) {
854 /* Parameters for special file:
855 * p1 - volume id - goes into owner/group/mode
858 * p4 - parent volume id
863 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
864 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
866 int vno = p2 & NAMEI_VNODEMASK;
867 /* Parameters for regular file:
879 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
880 tag = GetFreeTag(lh, p2);
885 tmp.ih_ino = (Inode) p2;
886 /* name is <uniq(p3)><tag><vno(p2)> */
887 tmp.ih_ino |= ((Inode) tag) << NAMEI_TAGSHIFT;
888 tmp.ih_ino |= ((Inode) p3) << NAMEI_UNIQSHIFT;
893 namei_HandleToName(&name, &tmp);
894 fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_RDWR, 0);
895 if (fd == INVALID_FD) {
896 if (errno == ENOTDIR || errno == ENOENT) {
897 if (namei_CreateDataDirectories(&name, &created_dir) < 0)
899 fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_RDWR,
901 if (fd == INVALID_FD)
907 if (SetOGM(fd, ogm_parm, tag) < 0) {
913 if (p2 == (afs_uint32)-1 && p3 == VI_LINKTABLE) {
914 /* hack at tmp to setup for set link count call. */
915 memset((void *)&tfd, 0, sizeof(FdHandle_t)); /* minimalistic still, but a little cleaner */
918 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
922 if (fd != INVALID_FD)
926 if (code || (fd == INVALID_FD)) {
930 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
935 return (code || (fd == INVALID_FD)) ? (Inode) - 1 : tmp.ih_ino;
941 namei_iopen(IHandle_t * h)
946 /* Convert handle to file name. */
947 namei_HandleToName(&name, h);
948 fd = OS_OPEN((char *)&name.n_path, O_RDWR, 0666);
952 /* Need to detect vol special file and just unlink. In those cases, the
953 * handle passed in _is_ for the inode. We only check p1 for the special
957 namei_dec(IHandle_t * ih, Inode ino, int p1)
964 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
966 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
968 /* Verify this is the right file. */
969 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
971 namei_HandleToName(&name, tmp);
976 errno = OS_ERROR(ENOENT);
980 if (CheckOGM(&name, fdP, p1) < 0) {
981 FDH_REALLYCLOSE(fdP);
983 errno = OS_ERROR(EINVAL);
987 /* If it's the link table itself, decrement the link count. */
988 if (type == VI_LINKTABLE) {
989 if ((count = namei_GetLinkCount(fdP, (Inode) 0, 1, 0, 1)) < 0) {
990 FDH_REALLYCLOSE(fdP);
996 if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) <
998 FDH_REALLYCLOSE(fdP);
1010 if ((code = OS_UNLINK(name.n_path)) == 0) {
1011 if (type == VI_LINKTABLE) {
1012 /* Try to remove directory. If it fails, that's ok.
1013 * Salvage will clean up.
1015 char *slash = strrchr(name.n_path, OS_DIRSEPC);
1017 /* avoid an rmdir() on the file we just unlinked */
1020 (void)namei_RemoveDataDirectories(&name);
1023 FDH_REALLYCLOSE(fdP);
1026 /* Get a file descriptor handle for this Inode */
1032 if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0) {
1033 FDH_REALLYCLOSE(fdP);
1039 if (namei_SetLinkCount(fdP, ino, count, 1) < 0) {
1040 FDH_REALLYCLOSE(fdP);
1045 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
1046 Log("Warning: Lost ref on ihandle dev %d vid %d ino %lld\n",
1047 th->ih_dev, th->ih_vid, (afs_int64)th->ih_ino);
1050 /* If we're less than 0, someone presumably unlinked;
1051 don't bother setting count to 0, but we need to drop a lock */
1052 if (namei_SetLinkCount(fdP, ino, 0, 1) < 0) {
1053 FDH_REALLYCLOSE(fdP);
1059 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
1061 namei_HandleToName(&name, th);
1063 code = OS_UNLINK(name.n_path);
1072 namei_inc(IHandle_t * h, Inode ino, int p1)
1078 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1079 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1080 if (type != VI_LINKTABLE)
1085 /* Get a file descriptor handle for this Inode */
1091 if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0)
1096 errno = OS_ERROR(EINVAL);
1100 if (namei_SetLinkCount(fdP, ino, count, 1) < 0)
1104 FDH_REALLYCLOSE(fdP);
1111 #ifndef AFS_NT40_ENV
1113 namei_replace_file_by_hardlink(IHandle_t *hLink, IHandle_t *hTarget)
1119 /* Convert handle to file name. */
1120 namei_HandleToName(&nameLink, hLink);
1121 namei_HandleToName(&nameTarget, hTarget);
1123 OS_UNLINK(nameLink.n_path);
1124 code = link(nameTarget.n_path, nameLink.n_path);
1129 namei_copy_on_write(IHandle_t *h)
1135 struct afs_stat_st tstat;
1138 namei_HandleToName(&name, h);
1139 if (afs_stat(name.n_path, &tstat) < 0)
1141 if (tstat.st_nlink > 1) { /* do a copy on write */
1150 snprintf(path, sizeof(path), "%s-tmp", name.n_path);
1151 fd = OS_OPEN(path, O_CREAT | O_EXCL | O_RDWR, 0);
1152 if (fd == INVALID_FD) {
1163 size = tstat.st_size;
1166 tlen = size > 8192 ? 8192 : size;
1167 if (FDH_PREAD(fdP, buf, tlen, offset) != tlen)
1169 if (OS_WRITE(fd, buf, tlen) != tlen)
1175 FDH_REALLYCLOSE(fdP);
1180 OS_UNLINK(name.n_path);
1181 code = rename(path, name.n_path);
1188 /************************************************************************
1189 * File Name Structure
1190 ************************************************************************
1192 * Each AFS file needs a unique name and it needs to be findable with
1193 * minimal lookup time. Note that the constraint on the number of files and
1194 * directories in a volume is the size of the vnode index files and the
1195 * max file size AFS supports (for internal files) of 2^31. Since a record
1196 * in the small vnode index file is 64 bytes long, we can have at most
1197 * (2^31)/64 or 33554432 files. A record in the large index file is
1198 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
1199 * Another layout parameter is that there is roughly a 16 to 1 ratio between
1200 * the number of files and the number of directories.
1202 * Using this information we can see that a layout of 256 directories, each
1203 * with 512 subdirectories and each of those having 512 files gives us
1204 * 256*512*512 = 67108864 AFS files and directories.
1206 * The volume, vnode, uniquifier and data version, as well as the tag
1207 * are required, either for finding the file or for salvaging. It's best to
1208 * restrict the name to something that can be mapped into 64 bits so the
1209 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
1210 * is used to distinguish between different versions of the same file
1211 * which are currently in the RW and clones of a volume. See "Link Table
1212 * Organization" below for more information on the tag. The tag is
1213 * required in the name of the file to ensure a unique name.
1215 * ifdef AFS_NT40_ENV
1216 * The data for each volume group is in a separate directory. The name of the
1217 * volume is of the form: Vol_NNNNNN.data, where NNNNNN is a base 32
1218 * representation of the RW volume ID (even where the RO is the only volume
1219 * on the partition). Below that are separate subdirectories for the
1220 * AFS directories and special files. There are also 16 directories for files,
1221 * hashed on the low 5 bits (recall bit0 is always 0) of the vnode number.
1222 * These directories are named:
1223 * A - P - 16 file directories.
1224 * Q ----- data directory
1225 * R ----- special files directory
1227 * The vnode is hashed into the directory using the low bits of the
1230 * The format of a file name for a regular file is:
1231 * Y:\Vol_NNNNNN.data\X\V_IIIIII.J
1232 * Y - partition encoded as drive letter, starting with D
1233 * NNNNNN - base 32 encoded volume number of RW volume
1234 * X - hash directory, as above
1235 * IIIIII - base 32 encoded vnode number
1236 * J - base 32 encoded tag
1238 * uniq is stored in the dwHighDateTime creation time field
1239 * dv is stored in the dwLowDateTime creation time field
1241 * Special inodes are always in the R directory, as above, and are
1243 * True child volid is stored in the dwHighDateTime creation time field
1244 * vnode number is always -1 (Special)
1245 * type is the IIIIII part of the filename
1246 * uniq is the J part of the filename
1247 * parent volume id is implied in the containing directory
1250 * We can store data in the uid, gid and mode bits of the files, provided
1251 * the directories have root only access. This gives us 15 bits for each
1252 * of uid and gid (GNU chown considers 65535 to mean "don't change").
1253 * There are 9 available mode bits. Adn we need to store a total of
1254 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
1255 * or 131 bits somewhere.
1257 * The format of a file name for a regular file is:
1258 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
1259 * V1 - low 8 bits of RW volume id
1260 * V2 - all bits of RW volume id
1261 * AA - high 8 bits of vnode number.
1262 * BB - next 9 bits of vnode number.
1263 * <tag><uniq><vno> - file name
1265 * Volume special files are stored in a separate directory:
1266 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
1269 * The vnode is hashed into the directory using the high bits of the
1270 * vnode number. This is so that consecutively created vnodes are in
1271 * roughly the same area on the disk. This will at least be optimal if
1272 * the user is creating many files in the same AFS directory. The name
1273 * should be formed so that the leading characters are different as quickly
1274 * as possible, leading to faster discards of incorrect matches in the
1282 /************************************************************************
1283 * Link Table Organization
1284 ************************************************************************
1286 * The link table volume special file is used to hold the link counts that
1287 * are held in the inodes in inode based AFS vice filesystems. For user
1288 * space access, the link counts are being kept in a separate
1289 * volume special file. The file begins with the usual version stamp
1290 * information and is then followed by one row per vnode number. vnode 0
1291 * is used to hold the link count of the link table itself. That is because
1292 * the same link table is shared among all the volumes of the volume group
1293 * and is deleted only when the last volume of a volume group is deleted.
1295 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
1296 * link count from 0 through 7. Each colume represents a unique instance of
1297 * that vnode. Say we have a file shared between the RW and a RO and a
1298 * different version of the file (or a different uniquifer) for the BU volume.
1299 * Then one column would be holding the link count of 2 for the RW and RO
1300 * and a different column would hold the link count of 1 for the BU volume.
1301 * # ifdef AFS_NT40_ENV
1302 * The column used is determined for NT by the uniquifier tag applied to
1303 * generate a unique file name in the NTFS namespace. The file name is
1304 * of the form "V_<vno>.<tag>" . And the <tag> is also the column number
1305 * in the link table.
1307 * Note that we allow only 5 volumes per file, giving 15 bits used in the
1311 #define LINKTABLE_WIDTH 2
1312 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
1315 * compute namei link table file and bit offset from inode number.
1317 * @param[in] ino inode number
1318 * @param[out] offset link table file offset
1319 * @param[out] index bit offset within 2-byte record
1324 namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
1326 int toff = (int)(ino & NAMEI_VNODEMASK);
1327 int tindex = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1329 *offset = (afs_foff_t) ((toff << LINKTABLE_SHIFT) + 8); /* * 2 + sizeof stamp */
1330 *index = (tindex << 1) + tindex;
1333 #ifdef AFS_PTHREAD_ENV
1334 /* XXX do static initializers work for WINNT/pthread? */
1335 pthread_mutex_t _namei_glc_lock = PTHREAD_MUTEX_INITIALIZER;
1336 #define NAMEI_GLC_LOCK MUTEX_ENTER(&_namei_glc_lock)
1337 #define NAMEI_GLC_UNLOCK MUTEX_EXIT(&_namei_glc_lock)
1338 #else /* !AFS_PTHREAD_ENV */
1339 #define NAMEI_GLC_LOCK
1340 #define NAMEI_GLC_UNLOCK
1341 #endif /* !AFS_PTHREAD_ENV */
1344 * get the link count of an inode.
1346 * @param[in] h namei link count table file handle
1347 * @param[in] ino inode number for which we are requesting a link count
1348 * @param[in] lockit if asserted, return with lock held on link table file
1349 * @param[in] fixup if asserted, write 1 to link count when read() returns
1351 * @param[in] nowrite return success on zero byte read or ZLC
1353 * @post if lockit asserted and lookup was successful, will return with write
1354 * lock on link table file descriptor
1356 * @return link count
1357 * @retval -1 namei link table i/o error
1362 namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite)
1364 unsigned short row = 0;
1369 /* there's no linktable yet. the salvager will create one later */
1370 if (h->fd_fd == INVALID_FD && fixup)
1372 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1375 if (FDH_LOCKFILE(h, offset) != 0)
1379 rc = FDH_PREAD(h, (char*)&row, sizeof(row), offset);
1381 goto bad_getLinkByte;
1383 if ((rc == 0 || !((row >> index) & NAMEI_TAGMASK)) && fixup && nowrite)
1385 if (rc == 0 && fixup) {
1387 * extend link table and write a link count of 1 for ino
1389 * in order to make MT-safe, truncation (extension really)
1390 * must happen under a mutex
1393 if (FDH_SIZE(h) >= offset+sizeof(row)) {
1395 goto bad_getLinkByte;
1397 FDH_TRUNC(h, offset+sizeof(row));
1399 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
1402 if (rc != sizeof(row)) {
1403 goto bad_getLinkByte;
1406 if (fixup && !((row >> index) & NAMEI_TAGMASK)) {
1410 * in order to make this mt-safe, we need to do the read-modify-write
1411 * under a mutex. thus, we repeat the read inside the lock.
1414 rc = FDH_PREAD(h, (char *)&row, sizeof(row), offset);
1415 if (rc == sizeof(row)) {
1417 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
1420 if (rc != sizeof(row))
1421 goto bad_getLinkByte;
1424 return (int)((row >> index) & NAMEI_TAGMASK);
1428 FDH_UNLOCKFILE(h, offset);
1433 namei_SetNonZLC(FdHandle_t * h, Inode ino)
1435 return namei_GetLinkCount(h, ino, 0, 1, 0);
1438 /* Return a free column index for this vnode. */
1440 GetFreeTag(IHandle_t * ih, int vno)
1454 /* Only one manipulates at a time. */
1455 if (FDH_LOCKFILE(fdP, offset) != 0) {
1456 FDH_REALLYCLOSE(fdP);
1460 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
1462 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1463 if (nBytes != sizeof(row)) {
1469 /* Now find a free column in this row and claim it. */
1470 for (col = 0; col < NAMEI_MAXVOLS; col++) {
1471 coldata = 7 << (col * 3);
1472 if ((row & coldata) == 0)
1475 if (col >= NAMEI_MAXVOLS) {
1480 coldata = 1 << (col * 3);
1483 if (FDH_PWRITE(fdP, (char *)&row, sizeof(row), offset) != sizeof(row)) {
1486 (void)FDH_SYNC(fdP);
1487 FDH_UNLOCKFILE(fdP, offset);
1492 FDH_UNLOCKFILE(fdP, offset);
1493 FDH_REALLYCLOSE(fdP);
1499 /* namei_SetLinkCount
1500 * If locked is set, assume file is locked. Otherwise, lock file before
1501 * proceeding to modify it.
1504 namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
1510 ssize_t nBytes = -1;
1512 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1515 if (FDH_LOCKFILE(fdP, offset) != 0) {
1520 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1521 if (nBytes != sizeof(row)) {
1523 errno = OS_ERROR(EBADF);
1524 goto bad_SetLinkCount;
1529 bytesRead = 7 << index;
1531 row &= (unsigned short)~bytesRead;
1532 row |= (unsigned short)count;
1534 if (FDH_PWRITE(fdP, (char *)&row, sizeof(short), offset) != sizeof(short)) {
1535 errno = OS_ERROR(EBADF);
1536 goto bad_SetLinkCount;
1538 (void)FDH_SYNC(fdP);
1544 FDH_UNLOCKFILE(fdP, offset);
1546 /* disallowed above 7, so... */
1551 /* ListViceInodes - write inode data to a results file. */
1552 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1553 unsigned int volid);
1554 static int DecodeVolumeName(char *name, unsigned int *vid);
1555 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
1556 int (*write_fun) (FD_t,
1557 struct ViceInodeInfo *,
1558 char *, char *), FD_t fp,
1559 int (*judgeFun) (struct ViceInodeInfo *,
1560 afs_uint32 vid, void *),
1561 afs_uint32 singleVolumeNumber, void *rock);
1566 * Write the inode data to the results file.
1568 * Returns -2 on error, 0 on success.
1570 * This is written as a callback simply so that other listing routines
1571 * can use the same inode reading code.
1574 WriteInodeInfo(FD_t fp, struct ViceInodeInfo *info, char *dir, char *name)
1577 n = OS_WRITE(fp, info, sizeof(*info));
1578 return (n == sizeof(*info)) ? 0 : -2;
1582 int mode_errors; /* Number of errors found in mode bits on directories. */
1584 VerifyDirPerms(char *path)
1586 struct afs_stat_st status;
1588 if (afs_stat(path, &status) < 0) {
1589 Log("Unable to stat %s. Please manually verify mode bits for this"
1590 " directory\n", path);
1592 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1598 * Fill the results file with the requested inode information.
1600 * This code optimizes single volume salvages by just looking at that one
1601 * volume's directory.
1603 * @param[in] devname device name string
1604 * @param[in] moutnedOn vice partition mount point
1605 * @param[in] resultFile result file in which to write inode
1606 * metadata. If NULL, write routine is not
1608 * @param[in] judgeInode filter function pointer. if not NULL, only
1609 * inodes for which this routine returns non-
1610 * zero will be written to the results file.
1611 * @param[in] singleVolumeNumber volume id filter
1612 * @param[out] forcep always set to 0 for namei impl
1613 * @param[in] forceR not used by namei impl
1614 * @param[in] wpath not used by namei impl
1615 * @param[in] rock opaque pointer passed to judgeInode
1617 * @return operation status
1619 * @retval -1 complete failure, salvage should terminate.
1620 * @retval -2 not enough space on partition, salvager has error message
1624 ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
1625 int (*judgeInode) (struct ViceInodeInfo * info, afs_uint32 vid, void *rock),
1626 afs_uint32 singleVolumeNumber, int *forcep, int forceR, char *wpath,
1631 *forcep = 0; /* no need to salvage until further notice */
1633 /* Verify protections on directories. */
1635 VerifyDirPerms(mountedOn);
1638 namei_ListAFSFiles(mountedOn, WriteInodeInfo, inodeFile, judgeInode,
1639 singleVolumeNumber, rock);
1641 if (inodeFile == INVALID_FD)
1648 if (OS_SYNC(inodeFile) == -1) {
1649 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1654 * Paranoia: check that the file is really the right size
1656 if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
1657 Log("Wrong size (%d instead of %lu) in inode file for %s\n",
1658 (int) OS_SIZE(inodeFile),
1659 (long unsigned int) ninodes * sizeof(struct ViceInodeInfo),
1668 * Collect all the matching AFS files on the drive.
1669 * If singleVolumeNumber is non-zero, just return files for that volume.
1671 * @param[in] dev vice partition path
1672 * @param[in] writeFun function pointer to a function which
1673 * writes inode information to FILE fp
1674 * @param[in] fp file stream where inode metadata is sent
1675 * @param[in] judgeFun filter function pointer. if not NULL,
1676 * only entries for which a non-zero value
1677 * is returned are written to fp
1678 * @param[in] singleVolumeNumber volume id filter. if nonzero, only
1679 * process files for that specific volume id
1680 * @param[in] rock opaque pointer passed into writeFun and
1683 * @return operation status
1685 * @retval >=0 number of matching files found
1688 namei_ListAFSFiles(char *dev,
1689 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1692 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1693 afs_uint32 singleVolumeNumber, void *rock)
1700 #ifndef AFS_NT40_ENV
1706 static void FreeZLCList(void);
1709 memset((void *)&ih, 0, sizeof(IHandle_t));
1711 ih.ih_dev = nt_DriveToDev(dev);
1713 ih.ih_dev = volutil_GetPartitionID(dev);
1716 if (singleVolumeNumber) {
1717 ih.ih_vid = singleVolumeNumber;
1718 namei_HandleToVolDir(&name, &ih);
1720 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1721 singleVolumeNumber, rock);
1725 /* Find all volume data directories and descend through them. */
1726 namei_HandleToInodeDir(&name, &ih);
1728 dirp1 = opendir(name.n_path);
1731 while ((dp1 = readdir(dirp1))) {
1733 /* Heirarchy is one level on Windows */
1734 if (!DecodeVolumeName(dp1->d_name, &ih.ih_vid)) {
1736 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1740 if (*dp1->d_name == '.')
1742 snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", name.n_path,
1744 dirp2 = opendir(path2);
1746 while ((dp2 = readdir(dirp2))) {
1747 if (*dp2->d_name == '.')
1749 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1751 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1768 static void AddToZLCDeleteList(char dir, char *name);
1769 static void DeleteZLCFiles(char *path);
1773 * examine a namei volume special file.
1775 * @param[in] path1 volume special directory path
1776 * @param[in] dname directory entry name
1777 * @param[in] myIH inode handle to volume directory
1778 * @param[out] linkHandle namei link count fd handle. if
1779 * the inode in question is the link
1780 * table, then the FdHandle is populated
1781 * @param[in] writeFun metadata write function pointer
1782 * @param[in] fp file pointer where inode metadata
1783 * is written by (*writeFun)()
1784 * @param[in] judgeFun inode filter function pointer. if
1785 * not NULL, only inodes for which this
1786 * function returns non-zero are recorded
1787 * into fp by writeFun
1788 * @param[in] singleVolumeNumer volume id filter. if non-zero, only
1789 * inodes associated with this volume id
1790 * are recorded by writeFun
1791 * @param[in] rock opaque pointer passed to writeFun and
1794 * @return operation status
1795 * @retval 1 count this inode
1796 * @retval 0 don't count this inode
1797 * @retval -1 failure
1802 _namei_examine_special(char * path1,
1805 FdHandle_t * linkHandle,
1806 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1809 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1810 int singleVolumeNumber,
1814 struct ViceInodeInfo info;
1815 afs_uint32 inode_vgid;
1817 if (DecodeInode(path1, dname, &info, myIH->ih_vid) < 0) {
1823 inode_vgid = myIH->ih_vid;
1825 inode_vgid = (info.inodeNumber >> NAMEI_UNIQSHIFT) & NAMEI_UNIQMASK;
1828 if (info.u.param[2] != VI_LINKTABLE) {
1830 } else if ((info.u.param[0] != myIH->ih_vid) ||
1831 (inode_vgid != myIH->ih_vid)) {
1832 /* VGID encoded in linktable filename and/or OGM data isn't
1833 * consistent with VGID encoded in namei path */
1834 Log("namei_ListAFSSubDirs: warning: inconsistent linktable "
1835 "filename \"%s" OS_DIRSEP "%s\"; salvager will delete it "
1836 "(dir_vgid=%u, inode_vgid=%u, ogm_vgid=%u)\n",
1837 path1, dname, myIH->ih_vid,
1838 (unsigned int)inode_vgid,
1842 /* Open this handle */
1843 snprintf(path2, sizeof(path2),
1844 "%s" OS_DIRSEP "%s", path1, dname);
1845 linkHandle->fd_fd = OS_OPEN(path2, Testing ? O_RDONLY : O_RDWR, 0666);
1847 namei_GetLinkCount(linkHandle, (Inode) 0, 1, 1, Testing);
1851 (*judgeFun) (&info, singleVolumeNumber, rock)) {
1852 ret = (*writeFun) (fp, &info, path1, dname);
1854 Log("_namei_examine_special: writeFun returned %d\n", ret);
1866 * examine a namei file.
1868 * @param[in] path3 volume special directory path
1869 * @param[in] dname directory entry name
1870 * @param[in] myIH inode handle to volume directory
1871 * @param[in] linkHandle namei link count fd handle.
1872 * @param[in] writeFun metadata write function pointer
1873 * @param[in] fp file pointer where inode metadata
1874 * is written by (*writeFun)()
1875 * @param[in] judgeFun inode filter function pointer. if
1876 * not NULL, only inodes for which this
1877 * function returns non-zero are recorded
1878 * into fp by writeFun
1879 * @param[in] singleVolumeNumer volume id filter. if non-zero, only
1880 * inodes associated with this volume id
1881 * are recorded by writeFun
1882 * @param[in] rock opaque pointer passed to writeFun and
1885 * @return operation status
1886 * @retval 1 count this inode
1887 * @retval 0 don't count this inode
1888 * @retval -1 failure
1889 * @retval -2 request ZLC delete
1894 _namei_examine_reg(char * path3,
1897 FdHandle_t * linkHandle,
1898 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1901 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1902 int singleVolumeNumber,
1906 struct ViceInodeInfo info;
1908 int dirl; /* Windows-only (one level hash dir) */
1911 if (DecodeInode(path3, dname, &info, myIH->ih_vid) < 0) {
1916 namei_GetLinkCount(linkHandle,
1917 info.inodeNumber, 1, 1, Testing);
1918 if (info.linkCount == 0) {
1920 Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n", path3, dname);
1921 #ifdef AFS_SALSRV_ENV
1922 /* defer -- the AddToZLCDeleteList() interface is not MT-safe */
1924 #else /* !AFS_SALSRV_ENV */
1925 dirl = path3[strlen(path3)-1];
1926 AddToZLCDeleteList((char)dirl, dname);
1927 #endif /* !AFS_SALSRV_ENV */
1928 #else /* !DELETE_ZLC */
1929 Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path3,
1936 (*judgeFun) (&info, singleVolumeNumber, rock)) {
1937 ret = (*writeFun) (fp, &info, path3, dname);
1939 Log("_namei_examine_reg: writeFun returned %d\n", ret);
1951 * listsubdirs work queue node.
1953 struct listsubdirs_work_node {
1954 #ifdef AFS_SALSRV_ENV
1955 int *error; /**< *error set if an error was
1956 * encountered in any listsubdirs
1960 IHandle_t * IH; /**< volume directory handle */
1961 FdHandle_t *linkHandle; /**< namei link count fd handle. when
1962 * examinining the link table special
1963 * inode, this will be pointed at the
1966 FD_t fp; /**< file pointer for writeFun */
1968 /** function which will write inode metadata to fp */
1969 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *, char *);
1971 /** inode filter function */
1972 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *);
1973 int singleVolumeNumber; /**< volume id filter */
1974 void * rock; /**< pointer passed to writeFun and judgeFun */
1975 int code; /**< return code from examine function */
1976 int special; /**< asserted when this is a volume
1981 * simple wrapper around _namei_examine_special and _namei_examine_reg.
1983 * @param[in] work the struct listsubdirs_work_node for the associated
1984 * "list subdirs" job
1985 * @param[in] dir the directory to examine
1986 * @param[in] filename the filename in 'dir' to examine
1988 * @return operation status
1989 * @retval 1 count this inode
1990 * @retval 0 don't count this inode
1991 * @retval -1 failure
1994 _namei_examine_file(const struct listsubdirs_work_node *work, char *dir,
1997 if (work->special) {
1998 return _namei_examine_special(dir, filename, work->IH,
1999 work->linkHandle, work->writeFun, work->fp,
2000 work->judgeFun, work->singleVolumeNumber,
2003 return _namei_examine_reg(dir, filename, work->IH,
2004 work->linkHandle, work->writeFun, work->fp,
2005 work->judgeFun, work->singleVolumeNumber,
2011 #ifdef AFS_SALSRV_ENV
2012 /** @addtogroup afs_vol_salsrv_pario */
2016 * arguments for the _namei_examine_file_cbk callback function.
2018 struct listsubdirs_args {
2019 const struct listsubdirs_work_node *work; /**< arguments that are the same
2020 * for all invocations of
2021 * _namei_examine_file_cbk, in
2023 int *result; /**< where we can store the return code of _namei_examine_file */
2025 char dir[512]; /**< directory to examine */
2026 char filename[256]; /**< filename in 'dir' to examine */
2030 * a node in the list of results of listsubdir jobs.
2032 struct listsubdirs_result {
2034 int inodes; /**< return value from _namei_examine_file */
2038 * clean up a list of 'struct listsubdirs_result's and interpret the results.
2040 * @param[in] resultlist a list of 'struct listsubdirs_result's
2042 * @return number of inodes found
2046 _namei_listsubdirs_cleanup_results(struct rx_queue *resultlist)
2048 struct listsubdirs_result *res, *nres;
2051 for(queue_Scan(resultlist, res, nres, listsubdirs_result)) {
2053 /* noop, retain erroneous error code */
2054 } else if (res->inodes < 0) {
2069 * wait for the spawned listsubdirs jobs to finish, and return how many inodes
2072 * @param[in] queue queue to wait to finish
2073 * @param[in] resultlist list of 'struct listsubdirs_result's that the queued
2074 * jobs are storing their results in
2076 * @return number of inodes found
2080 _namei_listsubdirs_wait(struct afs_work_queue *queue, struct rx_queue *resultlist)
2084 code = afs_wq_wait_all(queue);
2089 return _namei_listsubdirs_cleanup_results(resultlist);
2093 * work queue entry point for examining namei files.
2095 * @param[in] queue pointer to struct Vwork_queue
2096 * @param[in] node pointer to struct Vwork_queue_node
2097 * @param[in] queue_rock opaque pointer to struct salsrv_pool_state
2098 * @param[in] node_rock opaque pointer to struct listsubdirs_work_node
2099 * @param[in] caller_rock opaque pointer to struct salsrv_worker_thread_state
2101 * @return operation status
2103 * @see Vwork_queue_callback_func_t
2108 _namei_examine_file_cbk(struct afs_work_queue *queue,
2109 struct afs_work_queue_node *node,
2115 struct listsubdirs_args *args = node_rock;
2116 const struct listsubdirs_work_node * work = args->work;
2117 char *dir = args->dir;
2118 char *filename = args->filename;
2120 code = _namei_examine_file(work, dir, filename);
2122 *(args->result) = code;
2126 /* we've errored, so no point in letting more jobs continue */
2127 afs_wq_shutdown(queue);
2133 static pthread_once_t wq_once = PTHREAD_ONCE_INIT;
2134 static pthread_key_t wq_key;
2137 * create the wq_key key for storing a work queue.
2140 _namei_wq_keycreate(void)
2142 osi_Assert(pthread_key_create(&wq_key, NULL) == 0);
2146 * set the work queue for this thread to use for backgrounding namei ops.
2148 * The work queue will be used in ListAFSSubdirs (called indirectly by
2149 * ListViceInodes) to examine files in parallel.
2151 * @param[in] wq the work queue to use
2154 namei_SetWorkQueue(struct afs_work_queue *wq)
2156 osi_Assert(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
2158 osi_Assert(pthread_setspecific(wq_key, wq) == 0);
2162 * enqueue an examine file work unit.
2164 * @param[in] work the _namei_examine_file arguments that are common to
2165 * all callers within the same ListAFSFiles operation
2166 * @param[in] dir the specific directory to look at (string will be
2167 * copied; can be stack/temporary memory)
2168 * @param[in] filename the filename to look at (string will be copied; can be
2169 * stack/temporary memory)
2170 * @param[in] wq work queue to enqueue this work unit to
2171 * @param[in] resultlist the list to append the 'struct listsubdirs_result' to
2172 * for the enqueued work unit
2174 * @return operation status
2176 * @retval -1 fatal error
2178 * @note errors MUST be indicated by a -1 error code and nothing else, to be
2179 * compatible with _namei_examine_reg and _namei_examine_special
2184 _namei_examine_file_spawn(const struct listsubdirs_work_node *work,
2185 const char *dir, const char *filename,
2186 struct afs_work_queue *wq,
2187 struct rx_queue *resultlist)
2190 struct listsubdirs_args *args = NULL;
2191 struct listsubdirs_result *result = NULL;
2192 struct afs_work_queue_node *node = NULL;
2193 struct afs_work_queue_add_opts opts;
2195 args = malloc(sizeof(*args));
2201 result = malloc(sizeof(*result));
2202 if (result == NULL) {
2207 code = afs_wq_node_alloc(&node);
2212 code = afs_wq_node_set_detached(node);
2219 args->result = &result->inodes;
2220 strlcpy(args->dir, dir, sizeof(args->dir));
2221 strlcpy(args->filename, filename, sizeof(args->filename));
2223 code = afs_wq_node_set_callback(node,
2224 &_namei_examine_file_cbk,
2232 afs_wq_add_opts_init(&opts);
2235 code = afs_wq_add(wq, node, &opts);
2242 queue_Append(resultlist, result);
2247 afs_wq_node_put(node);
2263 #else /* !AFS_SALSRV_ENV */
2264 # define _namei_examine_file_spawn(work, dir, file, wq, resultlist) \
2265 _namei_examine_file(work, dir, file)
2266 #endif /* !AFS_SALSRV_ENV */
2269 * traverse and check inodes.
2271 * @param[in] dirIH volume group directory handle
2272 * @param[in] writeFun function pointer which will write inode
2273 * metadata to FILE stream fp
2274 * @param[in] fp file stream where inode metadata gets
2276 * @param[in] judgeFun inode filter function. if not NULL, only
2277 * inodes for which the filter returns non-zero
2278 * will be written out by writeFun
2279 * @param[in] singleVolumeNumber volume id filter. only inodes matching this
2280 * filter are written out by writeFun
2281 * @param[in] rock opaque pointer passed to judgeFun and writeFun
2283 * @return operation status
2285 * @retval >=0 number of matching inodes found
2287 * @todo the salsrv implementation may consume a lot of
2288 * memory for a large volume. at some point we should
2289 * probably write a background thread to asynchronously
2290 * clean up the resultlist nodes to reduce memory footprint
2295 namei_ListAFSSubDirs(IHandle_t * dirIH,
2296 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
2299 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
2300 afs_uint32 singleVolumeNumber, void *rock)
2302 int code = 0, ret = 0;
2303 IHandle_t myIH = *dirIH;
2305 char path1[512], path3[512];
2307 #ifndef AFS_NT40_ENV
2312 struct dirent *dp1, *dp3;
2313 FdHandle_t linkHandle;
2315 struct listsubdirs_work_node work;
2316 #ifdef AFS_SALSRV_ENV
2318 struct afs_work_queue *wq;
2320 struct rx_queue resultlist;
2323 namei_HandleToVolDir(&name, &myIH);
2324 strlcpy(path1, name.n_path, sizeof(path1));
2326 /* Do the directory containing the special files first to pick up link
2329 (void)strcat(path1, OS_DIRSEP);
2330 (void)strcat(path1, NAMEI_SPECDIR);
2332 linkHandle.fd_fd = INVALID_FD;
2333 #ifdef AFS_SALSRV_ENV
2334 osi_Assert(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
2336 wq = pthread_getspecific(wq_key);
2342 queue_Init(&resultlist);
2345 memset(&work, 0, sizeof(work));
2346 work.linkHandle = &linkHandle;
2349 work.writeFun = writeFun;
2350 work.judgeFun = judgeFun;
2351 work.singleVolumeNumber = singleVolumeNumber;
2354 #ifdef AFS_SALSRV_ENV
2355 work.error = &error;
2358 dirp1 = opendir(path1);
2360 while ((dp1 = readdir(dirp1))) {
2361 if (*dp1->d_name == '.')
2364 #ifdef AFS_SALSRV_ENV
2370 #endif /* AFS_SALSRV_ENV */
2372 code = _namei_examine_file_spawn(&work, path1, dp1->d_name, wq, &resultlist);
2382 /* count this inode */
2383 #ifndef AFS_SALSRV_ENV
2392 #ifdef AFS_SALSRV_ENV
2393 /* effectively a barrier */
2394 code = _namei_listsubdirs_wait(wq, &resultlist);
2395 if (code < 0 || error) {
2403 if (linkHandle.fd_fd == INVALID_FD) {
2404 Log("namei_ListAFSSubDirs: warning: VG %u does not have a link table; "
2405 "salvager will recreate it.\n", dirIH->ih_vid);
2408 /* Now run through all the other subdirs */
2409 namei_HandleToVolDir(&name, &myIH);
2410 strlcpy(path1, name.n_path, sizeof(path1));
2414 dirp1 = opendir(path1);
2416 while ((dp1 = readdir(dirp1))) {
2417 #ifndef AFS_NT40_ENV
2418 if (*dp1->d_name == '.')
2421 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
2424 #ifndef AFS_NT40_ENV /* This level missing on Windows */
2425 /* Now we've got a next level subdir. */
2426 snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s",
2427 path1, dp1->d_name);
2428 dirp2 = opendir(path2);
2430 while ((dp2 = readdir(dirp2))) {
2431 if (*dp2->d_name == '.')
2434 /* Now we've got to the actual data */
2435 snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
2436 path2, dp2->d_name);
2438 /* Now we've got to the actual data */
2439 snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
2440 path1, dp1->d_name);
2442 dirp3 = opendir(path3);
2444 while ((dp3 = readdir(dirp3))) {
2445 #ifndef AFS_NT40_ENV
2446 if (*dp3->d_name == '.')
2450 #ifdef AFS_SALSRV_ENV
2453 #ifndef AFS_NT40_ENV
2460 #endif /* AFS_SALSRV_ENV */
2462 code = _namei_examine_file_spawn(&work, path3,
2469 #ifndef AFS_NT40_ENV
2477 #ifndef AFS_SALSRV_ENV
2485 #ifndef AFS_NT40_ENV /* This level missing on Windows */
2494 #ifdef AFS_SALSRV_ENV
2495 /* effectively a barrier */
2496 code = _namei_listsubdirs_wait(wq, &resultlist);
2497 if (code < 0 || error) {
2507 /* Then why does this directory exist? Blow it away. */
2508 namei_HandleToVolDir(&name, dirIH);
2509 namei_RemoveDataDirectories(&name);
2513 #ifdef AFS_SALSRV_ENV
2515 afs_wq_wait_all(wq);
2517 _namei_listsubdirs_cleanup_results(&resultlist);
2519 if (linkHandle.fd_fd != INVALID_FD)
2520 OS_CLOSE(linkHandle.fd_fd);
2532 DecodeVolumeName(char *name, unsigned int *vid)
2534 /* Name begins with "Vol_" and ends with .data. See nt_HandleToVolDir() */
2541 if (strncmp(name, "Vol_", 4))
2543 if (strcmp(name + len - 5, ".data"))
2546 stmp[len - 5] = '\0';
2547 *vid = base32_to_int(stmp + 4);
2552 DecodeVolumeName(char *name, unsigned int *vid)
2554 if (strlen(name) < 1)
2556 *vid = (unsigned int)flipbase64_to_int64(name);
2564 * Get the inode number from the name.
2569 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
2574 WIN32_FIND_DATA data;
2578 FdHandle_t linkHandle;
2581 snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
2583 dirH = FindFirstFileEx(fpath, FindExInfoStandard, &data,
2584 FindExSearchNameMatch, NULL,
2585 FIND_FIRST_EX_CASE_SENSITIVE);
2586 if (dirH == INVALID_HANDLE_VALUE)
2589 (void)strcpy(stmp, name);
2590 s = strrchr(stmp, '_');
2594 t = strrchr(s, '.');
2599 vno = base32_to_int(s); /* type for special files */
2600 tag = base32_to_int(t+1);
2601 info->inodeNumber = ((Inode) tag) << NAMEI_TAGSHIFT;
2602 info->inodeNumber |= vno;
2603 info->byteCount = data.nFileSizeLow;
2605 dirl = dpath[strlen(dpath)-1];
2606 if (dirl == NAMEI_SPECDIRC) { /* Special inode. */
2607 info->inodeNumber |= NAMEI_INODESPECIAL;
2608 info->u.param[0] = data.ftCreationTime.dwHighDateTime;
2609 info->u.param[1] = data.ftCreationTime.dwLowDateTime;
2610 info->u.param[2] = vno; /* type */
2611 info->u.param[3] = volid;
2612 if (vno != VI_LINKTABLE)
2613 info->linkCount = 1;
2615 /* Open this handle */
2617 (void)sprintf(lpath, "%s" OS_DIRSEP "%s", fpath, data.cFileName);
2618 linkHandle.fd_fd = OS_OPEN(lpath, O_RDONLY, 0666);
2620 namei_GetLinkCount(&linkHandle, (Inode) 0, 0, 0, 0);
2624 namei_GetLinkCount(&linkHandle, info->inodeNumber, 0, 0, 0);
2625 if (info->linkCount == 0) {
2627 Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n",
2628 fpath, data.cFileName);
2629 AddToZLCDeleteList(dirl, data.cFileName);
2631 Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path,
2635 info->u.param[2] = data.ftCreationTime.dwHighDateTime;
2636 info->u.param[3] = data.ftCreationTime.dwLowDateTime;
2637 info->u.param[1] = vno;
2638 info->u.param[0] = volid;
2645 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
2649 struct afs_stat_st status;
2651 lb64_string_t check;
2653 snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
2655 if (afs_stat(fpath, &status) < 0) {
2659 info->byteCount = status.st_size;
2660 info->inodeNumber = (Inode) flipbase64_to_int64(name);
2662 int64_to_flipbase64(check, info->inodeNumber);
2663 if (strcmp(name, check))
2666 GetOGMFromStat(&status, &parm, &tag);
2667 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
2668 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
2669 info->u.param[0] = parm;
2670 info->u.param[1] = -1;
2671 info->u.param[2] = tag;
2672 info->u.param[3] = volid;
2674 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
2675 info->u.param[0] = volid;
2676 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
2677 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
2678 & (Inode) NAMEI_UNIQMASK);
2679 info->u.param[3] = parm;
2686 * Convert the VolumeInfo file from RO to RW
2687 * this routine is called by namei_convertROtoRWvolume()
2690 #ifdef FSSYNC_BUILD_CLIENT
2692 convertVolumeInfo(FD_t fdr, FD_t fdw, afs_uint32 vid)
2694 struct VolumeDiskData vd;
2697 if (OS_READ(fdr, &vd, sizeof(struct VolumeDiskData)) !=
2698 sizeof(struct VolumeDiskData)) {
2699 Log("1 convertVolumeInfo: read failed for %lu with code %d\n",
2700 afs_printable_uint32_lu(vid),
2704 vd.restoredFromId = vd.id; /* remember the RO volume here */
2706 vd.id = vd.parentId;
2710 vd.uniquifier += 5000; /* just in case there are still file copies from
2711 * the old RW volume around */
2713 /* For ROs, the copyDate contains the time that the RO volume was actually
2714 * created, and the creationDate just contains the last time the RO was
2715 * copied from the RW data. So, make the new RW creationDate more accurate
2716 * by setting it to copyDate, if copyDate is older. Since, we know the
2717 * volume is at least as old as copyDate. */
2718 if (vd.copyDate < vd.creationDate) {
2719 vd.creationDate = vd.copyDate;
2721 /* If copyDate is newer, just make copyDate and creationDate the same,
2722 * for consistency with other RWs */
2723 vd.copyDate = vd.creationDate;
2726 p = strrchr(vd.name, '.');
2727 if (p && !strcmp(p, ".readonly")) {
2730 if (OS_WRITE(fdw, &vd, sizeof(struct VolumeDiskData)) !=
2731 sizeof(struct VolumeDiskData)) {
2732 Log("1 convertVolumeInfo: write failed for %lu with code %d\n",
2733 afs_printable_uint32_lu(vid),
2742 * Convert a RO-volume into a RW-volume
2744 * This function allows to recover very fast from the loss of a partition
2745 * from RO-copies if all RO-Copies exist on another partition.
2746 * Then these RO-volumes can be made to the new RW-volumes.
2747 * Backup of RW-volumes then consists in "vos release".
2749 * We must make sure in this partition exists only the RO-volume which
2750 * is typical for remote replicas.
2752 * Then the linktable is already ok,
2753 * the vnode files need to be renamed
2754 * the volinfo file needs to be replaced by another one with
2755 * slightly different contents and new name.
2756 * The volume header file of the RO-volume in the /vicep<x> directory
2757 * is destroyed by this call. A new header file for the RW-volume must
2758 * be created after return from this routine.
2762 namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
2765 #ifdef FSSYNC_BUILD_CLIENT
2767 char dir_name[512], oldpath[512], newpath[512];
2782 struct DiskPartition64 *partP;
2783 struct ViceInodeInfo info;
2784 struct VolumeDiskHeader h;
2785 # ifdef AFS_DEMAND_ATTACH_FS
2787 # endif /* AFS_DEMAND_ATTACH_FS */
2789 for (partP = DiskPartitionList; partP && strcmp(partP->name, pname);
2790 partP = partP->next);
2792 Log("1 namei_ConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
2797 # ifdef AFS_DEMAND_ATTACH_FS
2798 locktype = VVolLockType(V_VOLUPD, 1);
2799 code = VLockVolumeByIdNB(volumeId, partP, locktype);
2805 # endif /* AFS_DEMAND_ATTACH_FS */
2807 if (VReadVolumeDiskHeader(volumeId, partP, &h)) {
2808 Log("1 namei_ConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n",
2809 afs_printable_uint32_lu(volumeId));
2814 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
2816 ino = namei_MakeSpecIno(h.parent, VI_LINKTABLE);
2817 IH_INIT(ih, partP->device, h.parent, ino);
2819 namei_HandleToName(&n, ih);
2820 strlcpy(dir_name, n.n_path, sizeof(dir_name));
2821 p = strrchr(dir_name, OS_DIRSEPC);
2823 dirp = opendir(dir_name);
2825 Log("1 namei_ConvertROtoRWvolume: Could not opendir(%s)\n", dir_name);
2830 while ((dp = readdir(dirp))) {
2831 /* struct ViceInodeInfo info; */
2832 #ifndef AFS_NT40_ENV
2833 if (*dp->d_name == '.')
2836 if (DecodeInode(dir_name, dp->d_name, &info, ih->ih_vid) < 0) {
2837 Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s" OS_DIRSEP "%s\n",
2838 dir_name, dp->d_name);
2843 if (info.u.param[1] != -1) {
2844 Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s" OS_DIRSEP "%s\n", dir_name, dp->d_name);
2849 if (info.u.param[0] != volumeId) {
2850 if (info.u.param[0] == ih->ih_vid) {
2851 if (info.u.param[2] == VI_LINKTABLE) { /* link table */
2856 Log("1 namei_ConvertROtoRWvolume: found special file %s" OS_DIRSEP "%s"
2857 " for volume %lu\n", dir_name, dp->d_name,
2858 afs_printable_uint32_lu(info.u.param[0]));
2863 if (info.u.param[2] == VI_VOLINFO) { /* volume info file */
2864 strlcpy(infoName, dp->d_name, sizeof(infoName));
2866 } else if (info.u.param[2] == VI_SMALLINDEX) { /* small vnodes file */
2867 strlcpy(smallName, dp->d_name, sizeof(smallName));
2869 } else if (info.u.param[2] == VI_LARGEINDEX) { /* large vnodes file */
2870 strlcpy(largeName, dp->d_name, sizeof(largeName));
2874 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);
2881 if (!infoSeen || !smallSeen || !largeSeen || !linkSeen) {
2882 Log("1 namei_ConvertROtoRWvolume: not all special files found in %s\n", dir_name);
2888 * If we come here then there was only a RO-volume and we can safely
2892 memset(&t_ih, 0, sizeof(t_ih));
2893 t_ih.ih_dev = ih->ih_dev;
2894 t_ih.ih_vid = ih->ih_vid;
2896 snprintf(oldpath, sizeof oldpath, "%s" OS_DIRSEP "%s", dir_name,
2898 fd = OS_OPEN(oldpath, O_RDWR, 0);
2899 if (fd == INVALID_FD) {
2900 Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
2905 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_VOLINFO);
2906 namei_HandleToName(&n, &t_ih);
2907 fd2 = OS_OPEN(n.n_path, O_CREAT | O_EXCL | O_RDWR, 0);
2908 if (fd2 == INVALID_FD) {
2909 Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
2914 code = convertVolumeInfo(fd, fd2, ih->ih_vid);
2918 OS_UNLINK(n.n_path);
2922 SetOGM(fd2, ih->ih_vid, 1);
2925 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_SMALLINDEX);
2926 namei_HandleToName(&n, &t_ih);
2927 snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
2929 fd = OS_OPEN(newpath, O_RDWR, 0);
2930 if (fd == INVALID_FD) {
2931 Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
2935 SetOGM(fd, ih->ih_vid, 2);
2938 MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
2940 link(newpath, n.n_path);
2944 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_LARGEINDEX);
2945 namei_HandleToName(&n, &t_ih);
2946 snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
2948 fd = OS_OPEN(newpath, O_RDWR, 0);
2949 if (fd == INVALID_FD) {
2950 Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
2954 SetOGM(fd, ih->ih_vid, 3);
2957 MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
2959 link(newpath, n.n_path);
2966 h.volumeInfo_hi = h.id;
2967 h.smallVnodeIndex_hi = h.id;
2968 h.largeVnodeIndex_hi = h.id;
2969 h.linkTable_hi = h.id;
2971 if (VCreateVolumeDiskHeader(&h, partP)) {
2972 Log("1 namei_ConvertROtoRWvolume: Couldn't write header for RW-volume %lu\n",
2973 afs_printable_uint32_lu(h.id));
2978 if (VDestroyVolumeDiskHeader(partP, volumeId, h.parent)) {
2979 Log("1 namei_ConvertROtoRWvolume: Couldn't unlink header for RO-volume %lu\n",
2980 afs_printable_uint32_lu(volumeId));
2983 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
2984 FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
2987 # ifdef AFS_DEMAND_ATTACH_FS
2989 VUnlockVolumeById(volumeId, partP);
2991 # endif /* AFS_DEMAND_ATTACH_FS */
2999 * returns a static string used to print either 32 or 64 bit inode numbers.
3002 PrintInode(char *s, Inode ino)
3004 static afs_ino_str_t result;
3008 snprintf(s, sizeof(afs_ino_str_t), "%llu", (afs_uintmax_t) ino);
3015 /* Routines to facilitate removing zero link count files. */
3016 #define MAX_ZLC_NAMES 32
3017 #define MAX_ZLC_NAMELEN 16
3018 typedef struct zlcList_s {
3019 struct zlcList_s *zlc_next;
3021 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
3024 static zlcList_t *zlcAnchor = NULL;
3025 static zlcList_t *zlcCur = NULL;
3028 AddToZLCDeleteList(char dir, char *name)
3030 osi_Assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
3032 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
3033 if (zlcCur && zlcCur->zlc_next)
3034 zlcCur = zlcCur->zlc_next;
3036 zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
3042 zlcCur->zlc_next = tmp;
3046 zlcCur->zlc_next = NULL;
3051 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c" OS_DIRSEP "%s", dir, name);
3053 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%s", name);
3059 DeleteZLCFiles(char *path)
3065 for (z = zlcAnchor; z; z = z->zlc_next) {
3066 for (i = 0; i < z->zlc_n; i++) {
3068 (void)sprintf(fname, "%s" OS_DIRSEP "%s", path, z->zlc_names[i]);
3070 (void)sprintf(fname, "%s", z->zlc_names[i]);
3071 if (namei_unlink(fname) < 0) {
3072 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
3076 z->zlc_n = 0; /* Can reuse space. */
3089 tnext = i->zlc_next;
3093 zlcCur = zlcAnchor = NULL;
3097 #endif /* AFS_NAMEI_ENV */