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 SetWinOGM(FD_t fd, int p1, int p2)
626 ftime.dwHighDateTime = p1;
627 ftime.dwLowDateTime = p2;
629 code = SetFileTime(fd, &ftime, NULL /*access*/, NULL /*write*/);
636 GetWinOGM(FD_t fd, int *p1, int *p2)
641 code = GetFileTime(fd, &ftime, NULL /*access*/, NULL /*write*/);
645 *p1 = ftime.dwHighDateTime;
646 *p2 = ftime.dwLowDateTime;
652 CheckOGM(FdHandle_t *fdP, int p1)
656 if (GetWinOGM(fdP->fd_fd, &ogm_p1, &ogm_p2)) {
666 #else /* AFS_NT40_ENV */
667 /* SetOGM - set owner group and mode bits from parm and tag */
669 SetOGM(FD_t fd, int parm, int tag)
672 * owner - low 15 bits of parm.
673 * group - next 15 bits of parm.
674 * mode - 2 bits of parm, then lowest = 3 bits of tag.
676 int owner, group, mode;
678 owner = parm & 0x7fff;
679 group = (parm >> 15) & 0x7fff;
680 if (fchown(fd, owner, group) < 0)
683 mode = (parm >> 27) & 0x18;
685 if (fchmod(fd, mode) < 0)
690 /* GetOGM - get parm and tag from owner, group and mode bits. */
692 GetOGMFromStat(struct afs_stat_st *status, int *parm, int *tag)
694 *parm = status->st_uid | (status->st_gid << 15);
695 *parm |= (status->st_mode & 0x18) << 27;
696 *tag = status->st_mode & 0x7;
700 GetOGM(FdHandle_t *fdP, int *parm, int *tag)
702 struct afs_stat_st status;
703 if (afs_fstat(fdP->fd_fd, &status) < 0)
705 GetOGMFromStat(&status, parm, tag);
710 CheckOGM(FdHandle_t *fdP, int p1)
714 if (GetOGM(fdP, &parm, &tag) < 0)
721 #endif /* !AFS_NT40_ENV */
723 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
725 /* Derive the name and create it O_EXCL. If that fails we have an error.
726 * Get the tag from a free column in the link table.
730 namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
733 FD_t fd = INVALID_FD;
744 memset((void *)&tmp, 0, sizeof(IHandle_t));
745 memset(&tfd, 0, sizeof(FdHandle_t));
747 tmp.ih_dev = nt_DriveToDev(part);
748 if (tmp.ih_dev == -1) {
753 if (p2 == INODESPECIAL) {
754 /* Parameters for special file:
755 * p1 - volume id - goes into owner/group/mode
756 * p2 - vnode == INODESPECIAL
758 * p4 - parent volume id
763 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
764 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
766 int vno = p2 & NAMEI_VNODEMASK;
767 /* Parameters for regular file:
781 tmp.ih_ino = (Inode) p2;
786 namei_HandleToName(&name, &tmp);
787 p = strrchr((char *)&name.n_path, '.');
789 for (tag = 0; tag < NAMEI_MAXVOLS; tag++) {
790 *p = *int_to_base32(str1, tag);
791 fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_EXCL, 0666);
792 if (fd == INVALID_FD) {
793 if (errno == ENOTDIR || errno == ENOENT) {
794 if (namei_CreateDataDirectories(&name, &created_dir) == 0)
795 fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_EXCL, 0666);
799 if (fd != INVALID_FD)
801 if (p2 == INODESPECIAL && p3 == VI_LINKTABLE)
804 if (fd == INVALID_FD) {
808 tmp.ih_ino &= ~(((Inode) NAMEI_TAGMASK) << NAMEI_TAGSHIFT);
809 tmp.ih_ino |= (((Inode) tag) << NAMEI_TAGSHIFT);
812 if (SetWinOGM(fd, ogm_p1, ogm_p2)) {
813 errno = OS_ERROR(EBADF);
819 if (p2 != INODESPECIAL) {
820 if (fd == INVALID_FD) {
822 code = nt_unlink((char *)&name.n_path);
831 code = namei_SetLinkCount(fdP, tmp.ih_ino, 1, 0);
833 } else if (p2 == INODESPECIAL && p3 == VI_LINKTABLE) {
834 if (fd == INVALID_FD)
836 /* hack at tmp to setup for set link count call. */
838 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
843 if (fd != INVALID_FD)
846 if (code || (fd == INVALID_FD)) {
847 if (p2 != INODESPECIAL) {
850 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
856 int save_errno = errno;
857 namei_RemoveDataDirectories(&name);
861 return (code || (fd == INVALID_FD)) ? (Inode) -1 : tmp.ih_ino;
863 #else /* !AFS_NT40_ENV */
865 namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
877 memset((void *)&tmp, 0, sizeof(IHandle_t));
878 memset(&tfd, 0, sizeof(FdHandle_t));
880 tmp.ih_dev = volutil_GetPartitionID(part);
881 if (tmp.ih_dev == -1) {
887 /* Parameters for special file:
888 * p1 - volume id - goes into owner/group/mode
891 * p4 - parent volume id
896 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
897 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
899 int vno = p2 & NAMEI_VNODEMASK;
900 /* Parameters for regular file:
912 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
913 tag = GetFreeTag(lh, p2);
918 tmp.ih_ino = (Inode) p2;
919 /* name is <uniq(p3)><tag><vno(p2)> */
920 tmp.ih_ino |= ((Inode) tag) << NAMEI_TAGSHIFT;
921 tmp.ih_ino |= ((Inode) p3) << NAMEI_UNIQSHIFT;
926 namei_HandleToName(&name, &tmp);
927 fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_RDWR, 0);
928 if (fd == INVALID_FD) {
929 if (errno == ENOTDIR || errno == ENOENT) {
930 if (namei_CreateDataDirectories(&name, &created_dir) < 0)
932 fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_RDWR,
934 if (fd == INVALID_FD)
940 if (SetOGM(fd, ogm_parm, tag) < 0) {
946 if (p2 == (afs_uint32)-1 && p3 == VI_LINKTABLE) {
947 /* hack at tmp to setup for set link count call. */
948 memset((void *)&tfd, 0, sizeof(FdHandle_t)); /* minimalistic still, but a little cleaner */
951 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
955 if (fd != INVALID_FD)
959 if (code || (fd == INVALID_FD)) {
963 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
968 return (code || (fd == INVALID_FD)) ? (Inode) - 1 : tmp.ih_ino;
974 namei_iopen(IHandle_t * h)
979 /* Convert handle to file name. */
980 namei_HandleToName(&name, h);
981 fd = OS_OPEN((char *)&name.n_path, O_RDWR, 0666);
985 /* Need to detect vol special file and just unlink. In those cases, the
986 * handle passed in _is_ for the inode. We only check p1 for the special
990 namei_dec(IHandle_t * ih, Inode ino, int p1)
997 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
999 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1001 /* Verify this is the right file. */
1002 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
1004 namei_HandleToName(&name, tmp);
1009 errno = OS_ERROR(ENOENT);
1013 if (CheckOGM(fdP, p1) < 0) {
1014 FDH_REALLYCLOSE(fdP);
1016 errno = OS_ERROR(EINVAL);
1020 /* If it's the link table itself, decrement the link count. */
1021 if (type == VI_LINKTABLE) {
1022 if ((count = namei_GetLinkCount(fdP, (Inode) 0, 1, 0, 1)) < 0) {
1023 FDH_REALLYCLOSE(fdP);
1029 if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) <
1031 FDH_REALLYCLOSE(fdP);
1043 if ((code = OS_UNLINK(name.n_path)) == 0) {
1044 if (type == VI_LINKTABLE) {
1045 /* Try to remove directory. If it fails, that's ok.
1046 * Salvage will clean up.
1048 char *slash = strrchr(name.n_path, OS_DIRSEPC);
1050 /* avoid an rmdir() on the file we just unlinked */
1053 (void)namei_RemoveDataDirectories(&name);
1056 FDH_REALLYCLOSE(fdP);
1059 /* Get a file descriptor handle for this Inode */
1065 if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0) {
1066 FDH_REALLYCLOSE(fdP);
1072 if (namei_SetLinkCount(fdP, ino, count, 1) < 0) {
1073 FDH_REALLYCLOSE(fdP);
1078 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
1079 Log("Warning: Lost ref on ihandle dev %d vid %d ino %lld\n",
1080 th->ih_dev, th->ih_vid, (afs_int64)th->ih_ino);
1083 /* If we're less than 0, someone presumably unlinked;
1084 don't bother setting count to 0, but we need to drop a lock */
1085 if (namei_SetLinkCount(fdP, ino, 0, 1) < 0) {
1086 FDH_REALLYCLOSE(fdP);
1092 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
1094 namei_HandleToName(&name, th);
1096 code = OS_UNLINK(name.n_path);
1105 namei_inc(IHandle_t * h, Inode ino, int p1)
1111 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1112 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1113 if (type != VI_LINKTABLE)
1118 /* Get a file descriptor handle for this Inode */
1124 if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0)
1129 errno = OS_ERROR(EINVAL);
1133 if (namei_SetLinkCount(fdP, ino, count, 1) < 0)
1137 FDH_REALLYCLOSE(fdP);
1144 #ifndef AFS_NT40_ENV
1146 namei_replace_file_by_hardlink(IHandle_t *hLink, IHandle_t *hTarget)
1152 /* Convert handle to file name. */
1153 namei_HandleToName(&nameLink, hLink);
1154 namei_HandleToName(&nameTarget, hTarget);
1156 OS_UNLINK(nameLink.n_path);
1157 code = link(nameTarget.n_path, nameLink.n_path);
1162 namei_copy_on_write(IHandle_t *h)
1168 struct afs_stat_st tstat;
1171 namei_HandleToName(&name, h);
1172 if (afs_stat(name.n_path, &tstat) < 0)
1174 if (tstat.st_nlink > 1) { /* do a copy on write */
1183 snprintf(path, sizeof(path), "%s-tmp", name.n_path);
1184 fd = OS_OPEN(path, O_CREAT | O_EXCL | O_RDWR, 0);
1185 if (fd == INVALID_FD) {
1196 size = tstat.st_size;
1199 tlen = size > 8192 ? 8192 : size;
1200 if (FDH_PREAD(fdP, buf, tlen, offset) != tlen)
1202 if (OS_WRITE(fd, buf, tlen) != tlen)
1208 FDH_REALLYCLOSE(fdP);
1213 OS_UNLINK(name.n_path);
1214 code = rename(path, name.n_path);
1221 /************************************************************************
1222 * File Name Structure
1223 ************************************************************************
1225 * Each AFS file needs a unique name and it needs to be findable with
1226 * minimal lookup time. Note that the constraint on the number of files and
1227 * directories in a volume is the size of the vnode index files and the
1228 * max file size AFS supports (for internal files) of 2^31. Since a record
1229 * in the small vnode index file is 64 bytes long, we can have at most
1230 * (2^31)/64 or 33554432 files. A record in the large index file is
1231 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
1232 * Another layout parameter is that there is roughly a 16 to 1 ratio between
1233 * the number of files and the number of directories.
1235 * Using this information we can see that a layout of 256 directories, each
1236 * with 512 subdirectories and each of those having 512 files gives us
1237 * 256*512*512 = 67108864 AFS files and directories.
1239 * The volume, vnode, uniquifier and data version, as well as the tag
1240 * are required, either for finding the file or for salvaging. It's best to
1241 * restrict the name to something that can be mapped into 64 bits so the
1242 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
1243 * is used to distinguish between different versions of the same file
1244 * which are currently in the RW and clones of a volume. See "Link Table
1245 * Organization" below for more information on the tag. The tag is
1246 * required in the name of the file to ensure a unique name.
1248 * ifdef AFS_NT40_ENV
1249 * The data for each volume group is in a separate directory. The name of the
1250 * volume is of the form: Vol_NNNNNN.data, where NNNNNN is a base 32
1251 * representation of the RW volume ID (even where the RO is the only volume
1252 * on the partition). Below that are separate subdirectories for the
1253 * AFS directories and special files. There are also 16 directories for files,
1254 * hashed on the low 5 bits (recall bit0 is always 0) of the vnode number.
1255 * These directories are named:
1256 * A - P - 16 file directories.
1257 * Q ----- data directory
1258 * R ----- special files directory
1260 * The vnode is hashed into the directory using the low bits of the
1263 * The format of a file name for a regular file is:
1264 * Y:\Vol_NNNNNN.data\X\V_IIIIII.J
1265 * Y - partition encoded as drive letter, starting with D
1266 * NNNNNN - base 32 encoded volume number of RW volume
1267 * X - hash directory, as above
1268 * IIIIII - base 32 encoded vnode number
1269 * J - base 32 encoded tag
1271 * uniq is stored in the dwHighDateTime creation time field
1272 * dv is stored in the dwLowDateTime creation time field
1274 * Special inodes are always in the R directory, as above, and are
1276 * True child volid is stored in the dwHighDateTime creation time field
1277 * vnode number is always -1 (Special)
1278 * type is the IIIIII part of the filename
1279 * uniq is the J part of the filename
1280 * parent volume id is implied in the containing directory
1283 * We can store data in the uid, gid and mode bits of the files, provided
1284 * the directories have root only access. This gives us 15 bits for each
1285 * of uid and gid (GNU chown considers 65535 to mean "don't change").
1286 * There are 9 available mode bits. Adn we need to store a total of
1287 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
1288 * or 131 bits somewhere.
1290 * The format of a file name for a regular file is:
1291 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
1292 * V1 - low 8 bits of RW volume id
1293 * V2 - all bits of RW volume id
1294 * AA - high 8 bits of vnode number.
1295 * BB - next 9 bits of vnode number.
1296 * <tag><uniq><vno> - file name
1298 * Volume special files are stored in a separate directory:
1299 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
1302 * The vnode is hashed into the directory using the high bits of the
1303 * vnode number. This is so that consecutively created vnodes are in
1304 * roughly the same area on the disk. This will at least be optimal if
1305 * the user is creating many files in the same AFS directory. The name
1306 * should be formed so that the leading characters are different as quickly
1307 * as possible, leading to faster discards of incorrect matches in the
1315 /************************************************************************
1316 * Link Table Organization
1317 ************************************************************************
1319 * The link table volume special file is used to hold the link counts that
1320 * are held in the inodes in inode based AFS vice filesystems. For user
1321 * space access, the link counts are being kept in a separate
1322 * volume special file. The file begins with the usual version stamp
1323 * information and is then followed by one row per vnode number. vnode 0
1324 * is used to hold the link count of the link table itself. That is because
1325 * the same link table is shared among all the volumes of the volume group
1326 * and is deleted only when the last volume of a volume group is deleted.
1328 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
1329 * link count from 0 through 7. Each colume represents a unique instance of
1330 * that vnode. Say we have a file shared between the RW and a RO and a
1331 * different version of the file (or a different uniquifer) for the BU volume.
1332 * Then one column would be holding the link count of 2 for the RW and RO
1333 * and a different column would hold the link count of 1 for the BU volume.
1334 * # ifdef AFS_NT40_ENV
1335 * The column used is determined for NT by the uniquifier tag applied to
1336 * generate a unique file name in the NTFS namespace. The file name is
1337 * of the form "V_<vno>.<tag>" . And the <tag> is also the column number
1338 * in the link table.
1340 * Note that we allow only 5 volumes per file, giving 15 bits used in the
1344 #define LINKTABLE_WIDTH 2
1345 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
1348 * compute namei link table file and bit offset from inode number.
1350 * @param[in] ino inode number
1351 * @param[out] offset link table file offset
1352 * @param[out] index bit offset within 2-byte record
1357 namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
1359 int toff = (int)(ino & NAMEI_VNODEMASK);
1360 int tindex = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1362 *offset = (afs_foff_t) ((toff << LINKTABLE_SHIFT) + 8); /* * 2 + sizeof stamp */
1363 *index = (tindex << 1) + tindex;
1366 #ifdef AFS_PTHREAD_ENV
1367 /* XXX do static initializers work for WINNT/pthread? */
1368 pthread_mutex_t _namei_glc_lock = PTHREAD_MUTEX_INITIALIZER;
1369 #define NAMEI_GLC_LOCK MUTEX_ENTER(&_namei_glc_lock)
1370 #define NAMEI_GLC_UNLOCK MUTEX_EXIT(&_namei_glc_lock)
1371 #else /* !AFS_PTHREAD_ENV */
1372 #define NAMEI_GLC_LOCK
1373 #define NAMEI_GLC_UNLOCK
1374 #endif /* !AFS_PTHREAD_ENV */
1377 * get the link count of an inode.
1379 * @param[in] h namei link count table file handle
1380 * @param[in] ino inode number for which we are requesting a link count
1381 * @param[in] lockit if asserted, return with lock held on link table file
1382 * @param[in] fixup if asserted, write 1 to link count when read() returns
1384 * @param[in] nowrite return success on zero byte read or ZLC
1386 * @post if lockit asserted and lookup was successful, will return with write
1387 * lock on link table file descriptor
1389 * @return link count
1390 * @retval -1 namei link table i/o error
1395 namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite)
1397 unsigned short row = 0;
1402 /* there's no linktable yet. the salvager will create one later */
1403 if (h->fd_fd == INVALID_FD && fixup)
1405 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1408 if (FDH_LOCKFILE(h, offset) != 0)
1412 rc = FDH_PREAD(h, (char*)&row, sizeof(row), offset);
1414 goto bad_getLinkByte;
1416 if ((rc == 0 || !((row >> index) & NAMEI_TAGMASK)) && fixup && nowrite)
1418 if (rc == 0 && fixup) {
1420 * extend link table and write a link count of 1 for ino
1422 * in order to make MT-safe, truncation (extension really)
1423 * must happen under a mutex
1426 if (FDH_SIZE(h) >= offset+sizeof(row)) {
1428 goto bad_getLinkByte;
1430 FDH_TRUNC(h, offset+sizeof(row));
1432 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
1435 if (rc != sizeof(row)) {
1436 goto bad_getLinkByte;
1439 if (fixup && !((row >> index) & NAMEI_TAGMASK)) {
1443 * in order to make this mt-safe, we need to do the read-modify-write
1444 * under a mutex. thus, we repeat the read inside the lock.
1447 rc = FDH_PREAD(h, (char *)&row, sizeof(row), offset);
1448 if (rc == sizeof(row)) {
1450 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
1453 if (rc != sizeof(row))
1454 goto bad_getLinkByte;
1457 return (int)((row >> index) & NAMEI_TAGMASK);
1461 FDH_UNLOCKFILE(h, offset);
1466 namei_SetNonZLC(FdHandle_t * h, Inode ino)
1468 return namei_GetLinkCount(h, ino, 0, 1, 0);
1471 /* Return a free column index for this vnode. */
1473 GetFreeTag(IHandle_t * ih, int vno)
1487 /* Only one manipulates at a time. */
1488 if (FDH_LOCKFILE(fdP, offset) != 0) {
1489 FDH_REALLYCLOSE(fdP);
1493 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
1495 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1496 if (nBytes != sizeof(row)) {
1502 /* Now find a free column in this row and claim it. */
1503 for (col = 0; col < NAMEI_MAXVOLS; col++) {
1504 coldata = 7 << (col * 3);
1505 if ((row & coldata) == 0)
1508 if (col >= NAMEI_MAXVOLS) {
1513 coldata = 1 << (col * 3);
1516 if (FDH_PWRITE(fdP, (char *)&row, sizeof(row), offset) != sizeof(row)) {
1519 (void)FDH_SYNC(fdP);
1520 FDH_UNLOCKFILE(fdP, offset);
1525 FDH_UNLOCKFILE(fdP, offset);
1526 FDH_REALLYCLOSE(fdP);
1532 /* namei_SetLinkCount
1533 * If locked is set, assume file is locked. Otherwise, lock file before
1534 * proceeding to modify it.
1537 namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
1543 ssize_t nBytes = -1;
1545 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1548 if (FDH_LOCKFILE(fdP, offset) != 0) {
1553 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1554 if (nBytes != sizeof(row)) {
1556 errno = OS_ERROR(EBADF);
1557 goto bad_SetLinkCount;
1562 bytesRead = 7 << index;
1564 row &= (unsigned short)~bytesRead;
1565 row |= (unsigned short)count;
1567 if (FDH_PWRITE(fdP, (char *)&row, sizeof(short), offset) != sizeof(short)) {
1568 errno = OS_ERROR(EBADF);
1569 goto bad_SetLinkCount;
1571 (void)FDH_SYNC(fdP);
1577 FDH_UNLOCKFILE(fdP, offset);
1579 /* disallowed above 7, so... */
1584 /* ListViceInodes - write inode data to a results file. */
1585 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1586 unsigned int volid);
1587 static int DecodeVolumeName(char *name, unsigned int *vid);
1588 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
1589 int (*write_fun) (FD_t,
1590 struct ViceInodeInfo *,
1591 char *, char *), FD_t fp,
1592 int (*judgeFun) (struct ViceInodeInfo *,
1593 afs_uint32 vid, void *),
1594 afs_uint32 singleVolumeNumber, void *rock);
1599 * Write the inode data to the results file.
1601 * Returns -2 on error, 0 on success.
1603 * This is written as a callback simply so that other listing routines
1604 * can use the same inode reading code.
1607 WriteInodeInfo(FD_t fp, struct ViceInodeInfo *info, char *dir, char *name)
1610 n = OS_WRITE(fp, info, sizeof(*info));
1611 return (n == sizeof(*info)) ? 0 : -2;
1615 int mode_errors; /* Number of errors found in mode bits on directories. */
1617 VerifyDirPerms(char *path)
1619 struct afs_stat_st status;
1621 if (afs_stat(path, &status) < 0) {
1622 Log("Unable to stat %s. Please manually verify mode bits for this"
1623 " directory\n", path);
1625 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1631 * Fill the results file with the requested inode information.
1633 * This code optimizes single volume salvages by just looking at that one
1634 * volume's directory.
1636 * @param[in] devname device name string
1637 * @param[in] moutnedOn vice partition mount point
1638 * @param[in] resultFile result file in which to write inode
1639 * metadata. If NULL, write routine is not
1641 * @param[in] judgeInode filter function pointer. if not NULL, only
1642 * inodes for which this routine returns non-
1643 * zero will be written to the results file.
1644 * @param[in] singleVolumeNumber volume id filter
1645 * @param[out] forcep always set to 0 for namei impl
1646 * @param[in] forceR not used by namei impl
1647 * @param[in] wpath not used by namei impl
1648 * @param[in] rock opaque pointer passed to judgeInode
1650 * @return operation status
1652 * @retval -1 complete failure, salvage should terminate.
1653 * @retval -2 not enough space on partition, salvager has error message
1657 ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
1658 int (*judgeInode) (struct ViceInodeInfo * info, afs_uint32 vid, void *rock),
1659 afs_uint32 singleVolumeNumber, int *forcep, int forceR, char *wpath,
1664 *forcep = 0; /* no need to salvage until further notice */
1666 /* Verify protections on directories. */
1668 VerifyDirPerms(mountedOn);
1671 namei_ListAFSFiles(mountedOn, WriteInodeInfo, inodeFile, judgeInode,
1672 singleVolumeNumber, rock);
1674 if (inodeFile == INVALID_FD)
1681 if (OS_SYNC(inodeFile) == -1) {
1682 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1687 * Paranoia: check that the file is really the right size
1689 if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
1690 Log("Wrong size (%d instead of %lu) in inode file for %s\n",
1691 (int) OS_SIZE(inodeFile),
1692 (long unsigned int) ninodes * sizeof(struct ViceInodeInfo),
1701 * Collect all the matching AFS files on the drive.
1702 * If singleVolumeNumber is non-zero, just return files for that volume.
1704 * @param[in] dev vice partition path
1705 * @param[in] writeFun function pointer to a function which
1706 * writes inode information to FILE fp
1707 * @param[in] fp file stream where inode metadata is sent
1708 * @param[in] judgeFun filter function pointer. if not NULL,
1709 * only entries for which a non-zero value
1710 * is returned are written to fp
1711 * @param[in] singleVolumeNumber volume id filter. if nonzero, only
1712 * process files for that specific volume id
1713 * @param[in] rock opaque pointer passed into writeFun and
1716 * @return operation status
1718 * @retval >=0 number of matching files found
1721 namei_ListAFSFiles(char *dev,
1722 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1725 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1726 afs_uint32 singleVolumeNumber, void *rock)
1733 #ifndef AFS_NT40_ENV
1739 static void FreeZLCList(void);
1742 memset((void *)&ih, 0, sizeof(IHandle_t));
1744 ih.ih_dev = nt_DriveToDev(dev);
1746 ih.ih_dev = volutil_GetPartitionID(dev);
1749 if (singleVolumeNumber) {
1750 ih.ih_vid = singleVolumeNumber;
1751 namei_HandleToVolDir(&name, &ih);
1753 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1754 singleVolumeNumber, rock);
1758 /* Find all volume data directories and descend through them. */
1759 namei_HandleToInodeDir(&name, &ih);
1761 dirp1 = opendir(name.n_path);
1764 while ((dp1 = readdir(dirp1))) {
1766 /* Heirarchy is one level on Windows */
1767 if (!DecodeVolumeName(dp1->d_name, &ih.ih_vid)) {
1769 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1773 if (*dp1->d_name == '.')
1775 snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", name.n_path,
1777 dirp2 = opendir(path2);
1779 while ((dp2 = readdir(dirp2))) {
1780 if (*dp2->d_name == '.')
1782 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1784 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1801 static void AddToZLCDeleteList(char dir, char *name);
1802 static void DeleteZLCFiles(char *path);
1806 * examine a namei volume special file.
1808 * @param[in] path1 volume special directory path
1809 * @param[in] dname directory entry name
1810 * @param[in] myIH inode handle to volume directory
1811 * @param[out] linkHandle namei link count fd handle. if
1812 * the inode in question is the link
1813 * table, then the FdHandle is populated
1814 * @param[in] writeFun metadata write function pointer
1815 * @param[in] fp file pointer where inode metadata
1816 * is written by (*writeFun)()
1817 * @param[in] judgeFun inode filter function pointer. if
1818 * not NULL, only inodes for which this
1819 * function returns non-zero are recorded
1820 * into fp by writeFun
1821 * @param[in] singleVolumeNumer volume id filter. if non-zero, only
1822 * inodes associated with this volume id
1823 * are recorded by writeFun
1824 * @param[in] rock opaque pointer passed to writeFun and
1827 * @return operation status
1828 * @retval 1 count this inode
1829 * @retval 0 don't count this inode
1830 * @retval -1 failure
1835 _namei_examine_special(char * path1,
1838 FdHandle_t * linkHandle,
1839 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1842 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1843 int singleVolumeNumber,
1847 struct ViceInodeInfo info;
1848 afs_uint32 inode_vgid;
1850 if (DecodeInode(path1, dname, &info, myIH->ih_vid) < 0) {
1856 inode_vgid = myIH->ih_vid;
1858 inode_vgid = (info.inodeNumber >> NAMEI_UNIQSHIFT) & NAMEI_UNIQMASK;
1861 if (info.u.param[2] != VI_LINKTABLE) {
1863 } else if ((info.u.param[0] != myIH->ih_vid) ||
1864 (inode_vgid != myIH->ih_vid)) {
1865 /* VGID encoded in linktable filename and/or OGM data isn't
1866 * consistent with VGID encoded in namei path */
1867 Log("namei_ListAFSSubDirs: warning: inconsistent linktable "
1868 "filename \"%s" OS_DIRSEP "%s\"; salvager will delete it "
1869 "(dir_vgid=%u, inode_vgid=%u, ogm_vgid=%u)\n",
1870 path1, dname, myIH->ih_vid,
1871 (unsigned int)inode_vgid,
1875 /* Open this handle */
1876 snprintf(path2, sizeof(path2),
1877 "%s" OS_DIRSEP "%s", path1, dname);
1878 linkHandle->fd_fd = OS_OPEN(path2, Testing ? O_RDONLY : O_RDWR, 0666);
1880 namei_GetLinkCount(linkHandle, (Inode) 0, 1, 1, Testing);
1884 (*judgeFun) (&info, singleVolumeNumber, rock)) {
1885 ret = (*writeFun) (fp, &info, path1, dname);
1887 Log("_namei_examine_special: writeFun returned %d\n", ret);
1899 * examine a namei file.
1901 * @param[in] path3 volume special directory path
1902 * @param[in] dname directory entry name
1903 * @param[in] myIH inode handle to volume directory
1904 * @param[in] linkHandle namei link count fd handle.
1905 * @param[in] writeFun metadata write function pointer
1906 * @param[in] fp file pointer where inode metadata
1907 * is written by (*writeFun)()
1908 * @param[in] judgeFun inode filter function pointer. if
1909 * not NULL, only inodes for which this
1910 * function returns non-zero are recorded
1911 * into fp by writeFun
1912 * @param[in] singleVolumeNumer volume id filter. if non-zero, only
1913 * inodes associated with this volume id
1914 * are recorded by writeFun
1915 * @param[in] rock opaque pointer passed to writeFun and
1918 * @return operation status
1919 * @retval 1 count this inode
1920 * @retval 0 don't count this inode
1921 * @retval -1 failure
1922 * @retval -2 request ZLC delete
1927 _namei_examine_reg(char * path3,
1930 FdHandle_t * linkHandle,
1931 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1934 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1935 int singleVolumeNumber,
1939 struct ViceInodeInfo info;
1941 int dirl; /* Windows-only (one level hash dir) */
1944 if (DecodeInode(path3, dname, &info, myIH->ih_vid) < 0) {
1949 namei_GetLinkCount(linkHandle,
1950 info.inodeNumber, 1, 1, Testing);
1951 if (info.linkCount == 0) {
1953 Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n", path3, dname);
1954 #ifdef AFS_SALSRV_ENV
1955 /* defer -- the AddToZLCDeleteList() interface is not MT-safe */
1957 #else /* !AFS_SALSRV_ENV */
1958 dirl = path3[strlen(path3)-1];
1959 AddToZLCDeleteList((char)dirl, dname);
1960 #endif /* !AFS_SALSRV_ENV */
1961 #else /* !DELETE_ZLC */
1962 Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path3,
1969 (*judgeFun) (&info, singleVolumeNumber, rock)) {
1970 ret = (*writeFun) (fp, &info, path3, dname);
1972 Log("_namei_examine_reg: writeFun returned %d\n", ret);
1984 * listsubdirs work queue node.
1986 struct listsubdirs_work_node {
1987 #ifdef AFS_SALSRV_ENV
1988 int *error; /**< *error set if an error was
1989 * encountered in any listsubdirs
1993 IHandle_t * IH; /**< volume directory handle */
1994 FdHandle_t *linkHandle; /**< namei link count fd handle. when
1995 * examinining the link table special
1996 * inode, this will be pointed at the
1999 FD_t fp; /**< file pointer for writeFun */
2001 /** function which will write inode metadata to fp */
2002 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *, char *);
2004 /** inode filter function */
2005 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *);
2006 int singleVolumeNumber; /**< volume id filter */
2007 void * rock; /**< pointer passed to writeFun and judgeFun */
2008 int code; /**< return code from examine function */
2009 int special; /**< asserted when this is a volume
2014 * simple wrapper around _namei_examine_special and _namei_examine_reg.
2016 * @param[in] work the struct listsubdirs_work_node for the associated
2017 * "list subdirs" job
2018 * @param[in] dir the directory to examine
2019 * @param[in] filename the filename in 'dir' to examine
2021 * @return operation status
2022 * @retval 1 count this inode
2023 * @retval 0 don't count this inode
2024 * @retval -1 failure
2027 _namei_examine_file(const struct listsubdirs_work_node *work, char *dir,
2030 if (work->special) {
2031 return _namei_examine_special(dir, filename, work->IH,
2032 work->linkHandle, work->writeFun, work->fp,
2033 work->judgeFun, work->singleVolumeNumber,
2036 return _namei_examine_reg(dir, filename, work->IH,
2037 work->linkHandle, work->writeFun, work->fp,
2038 work->judgeFun, work->singleVolumeNumber,
2044 #ifdef AFS_SALSRV_ENV
2045 /** @addtogroup afs_vol_salsrv_pario */
2049 * arguments for the _namei_examine_file_cbk callback function.
2051 struct listsubdirs_args {
2052 const struct listsubdirs_work_node *work; /**< arguments that are the same
2053 * for all invocations of
2054 * _namei_examine_file_cbk, in
2056 int *result; /**< where we can store the return code of _namei_examine_file */
2058 char dir[512]; /**< directory to examine */
2059 char filename[256]; /**< filename in 'dir' to examine */
2063 * a node in the list of results of listsubdir jobs.
2065 struct listsubdirs_result {
2067 int inodes; /**< return value from _namei_examine_file */
2071 * clean up a list of 'struct listsubdirs_result's and interpret the results.
2073 * @param[in] resultlist a list of 'struct listsubdirs_result's
2075 * @return number of inodes found
2079 _namei_listsubdirs_cleanup_results(struct rx_queue *resultlist)
2081 struct listsubdirs_result *res, *nres;
2084 for(queue_Scan(resultlist, res, nres, listsubdirs_result)) {
2086 /* noop, retain erroneous error code */
2087 } else if (res->inodes < 0) {
2102 * wait for the spawned listsubdirs jobs to finish, and return how many inodes
2105 * @param[in] queue queue to wait to finish
2106 * @param[in] resultlist list of 'struct listsubdirs_result's that the queued
2107 * jobs are storing their results in
2109 * @return number of inodes found
2113 _namei_listsubdirs_wait(struct afs_work_queue *queue, struct rx_queue *resultlist)
2117 code = afs_wq_wait_all(queue);
2122 return _namei_listsubdirs_cleanup_results(resultlist);
2126 * work queue entry point for examining namei files.
2128 * @param[in] queue pointer to struct Vwork_queue
2129 * @param[in] node pointer to struct Vwork_queue_node
2130 * @param[in] queue_rock opaque pointer to struct salsrv_pool_state
2131 * @param[in] node_rock opaque pointer to struct listsubdirs_work_node
2132 * @param[in] caller_rock opaque pointer to struct salsrv_worker_thread_state
2134 * @return operation status
2136 * @see Vwork_queue_callback_func_t
2141 _namei_examine_file_cbk(struct afs_work_queue *queue,
2142 struct afs_work_queue_node *node,
2148 struct listsubdirs_args *args = node_rock;
2149 const struct listsubdirs_work_node * work = args->work;
2150 char *dir = args->dir;
2151 char *filename = args->filename;
2153 code = _namei_examine_file(work, dir, filename);
2155 *(args->result) = code;
2159 /* we've errored, so no point in letting more jobs continue */
2160 afs_wq_shutdown(queue);
2166 static pthread_once_t wq_once = PTHREAD_ONCE_INIT;
2167 static pthread_key_t wq_key;
2170 * create the wq_key key for storing a work queue.
2173 _namei_wq_keycreate(void)
2175 osi_Assert(pthread_key_create(&wq_key, NULL) == 0);
2179 * set the work queue for this thread to use for backgrounding namei ops.
2181 * The work queue will be used in ListAFSSubdirs (called indirectly by
2182 * ListViceInodes) to examine files in parallel.
2184 * @param[in] wq the work queue to use
2187 namei_SetWorkQueue(struct afs_work_queue *wq)
2189 osi_Assert(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
2191 osi_Assert(pthread_setspecific(wq_key, wq) == 0);
2195 * enqueue an examine file work unit.
2197 * @param[in] work the _namei_examine_file arguments that are common to
2198 * all callers within the same ListAFSFiles operation
2199 * @param[in] dir the specific directory to look at (string will be
2200 * copied; can be stack/temporary memory)
2201 * @param[in] filename the filename to look at (string will be copied; can be
2202 * stack/temporary memory)
2203 * @param[in] wq work queue to enqueue this work unit to
2204 * @param[in] resultlist the list to append the 'struct listsubdirs_result' to
2205 * for the enqueued work unit
2207 * @return operation status
2209 * @retval -1 fatal error
2211 * @note errors MUST be indicated by a -1 error code and nothing else, to be
2212 * compatible with _namei_examine_reg and _namei_examine_special
2217 _namei_examine_file_spawn(const struct listsubdirs_work_node *work,
2218 const char *dir, const char *filename,
2219 struct afs_work_queue *wq,
2220 struct rx_queue *resultlist)
2223 struct listsubdirs_args *args = NULL;
2224 struct listsubdirs_result *result = NULL;
2225 struct afs_work_queue_node *node = NULL;
2226 struct afs_work_queue_add_opts opts;
2228 args = malloc(sizeof(*args));
2234 result = malloc(sizeof(*result));
2235 if (result == NULL) {
2240 code = afs_wq_node_alloc(&node);
2245 code = afs_wq_node_set_detached(node);
2252 args->result = &result->inodes;
2253 strlcpy(args->dir, dir, sizeof(args->dir));
2254 strlcpy(args->filename, filename, sizeof(args->filename));
2256 code = afs_wq_node_set_callback(node,
2257 &_namei_examine_file_cbk,
2265 afs_wq_add_opts_init(&opts);
2268 code = afs_wq_add(wq, node, &opts);
2275 queue_Append(resultlist, result);
2280 afs_wq_node_put(node);
2296 #else /* !AFS_SALSRV_ENV */
2297 # define _namei_examine_file_spawn(work, dir, file, wq, resultlist) \
2298 _namei_examine_file(work, dir, file)
2299 #endif /* !AFS_SALSRV_ENV */
2302 * traverse and check inodes.
2304 * @param[in] dirIH volume group directory handle
2305 * @param[in] writeFun function pointer which will write inode
2306 * metadata to FILE stream fp
2307 * @param[in] fp file stream where inode metadata gets
2309 * @param[in] judgeFun inode filter function. if not NULL, only
2310 * inodes for which the filter returns non-zero
2311 * will be written out by writeFun
2312 * @param[in] singleVolumeNumber volume id filter. only inodes matching this
2313 * filter are written out by writeFun
2314 * @param[in] rock opaque pointer passed to judgeFun and writeFun
2316 * @return operation status
2318 * @retval >=0 number of matching inodes found
2320 * @todo the salsrv implementation may consume a lot of
2321 * memory for a large volume. at some point we should
2322 * probably write a background thread to asynchronously
2323 * clean up the resultlist nodes to reduce memory footprint
2328 namei_ListAFSSubDirs(IHandle_t * dirIH,
2329 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
2332 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
2333 afs_uint32 singleVolumeNumber, void *rock)
2335 int code = 0, ret = 0;
2336 IHandle_t myIH = *dirIH;
2338 char path1[512], path3[512];
2340 #ifndef AFS_NT40_ENV
2345 struct dirent *dp1, *dp3;
2346 FdHandle_t linkHandle;
2348 struct listsubdirs_work_node work;
2349 #ifdef AFS_SALSRV_ENV
2351 struct afs_work_queue *wq;
2353 struct rx_queue resultlist;
2356 namei_HandleToVolDir(&name, &myIH);
2357 strlcpy(path1, name.n_path, sizeof(path1));
2359 /* Do the directory containing the special files first to pick up link
2362 (void)strcat(path1, OS_DIRSEP);
2363 (void)strcat(path1, NAMEI_SPECDIR);
2365 linkHandle.fd_fd = INVALID_FD;
2366 #ifdef AFS_SALSRV_ENV
2367 osi_Assert(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
2369 wq = pthread_getspecific(wq_key);
2375 queue_Init(&resultlist);
2378 memset(&work, 0, sizeof(work));
2379 work.linkHandle = &linkHandle;
2382 work.writeFun = writeFun;
2383 work.judgeFun = judgeFun;
2384 work.singleVolumeNumber = singleVolumeNumber;
2387 #ifdef AFS_SALSRV_ENV
2388 work.error = &error;
2391 dirp1 = opendir(path1);
2393 while ((dp1 = readdir(dirp1))) {
2394 if (*dp1->d_name == '.')
2397 #ifdef AFS_SALSRV_ENV
2403 #endif /* AFS_SALSRV_ENV */
2405 code = _namei_examine_file_spawn(&work, path1, dp1->d_name, wq, &resultlist);
2415 /* count this inode */
2416 #ifndef AFS_SALSRV_ENV
2425 #ifdef AFS_SALSRV_ENV
2426 /* effectively a barrier */
2427 code = _namei_listsubdirs_wait(wq, &resultlist);
2428 if (code < 0 || error) {
2436 if (linkHandle.fd_fd == INVALID_FD) {
2437 Log("namei_ListAFSSubDirs: warning: VG %u does not have a link table; "
2438 "salvager will recreate it.\n", dirIH->ih_vid);
2441 /* Now run through all the other subdirs */
2442 namei_HandleToVolDir(&name, &myIH);
2443 strlcpy(path1, name.n_path, sizeof(path1));
2447 dirp1 = opendir(path1);
2449 while ((dp1 = readdir(dirp1))) {
2450 #ifndef AFS_NT40_ENV
2451 if (*dp1->d_name == '.')
2454 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
2457 #ifndef AFS_NT40_ENV /* This level missing on Windows */
2458 /* Now we've got a next level subdir. */
2459 snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s",
2460 path1, dp1->d_name);
2461 dirp2 = opendir(path2);
2463 while ((dp2 = readdir(dirp2))) {
2464 if (*dp2->d_name == '.')
2467 /* Now we've got to the actual data */
2468 snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
2469 path2, dp2->d_name);
2471 /* Now we've got to the actual data */
2472 snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
2473 path1, dp1->d_name);
2475 dirp3 = opendir(path3);
2477 while ((dp3 = readdir(dirp3))) {
2478 #ifndef AFS_NT40_ENV
2479 if (*dp3->d_name == '.')
2483 #ifdef AFS_SALSRV_ENV
2486 #ifndef AFS_NT40_ENV
2493 #endif /* AFS_SALSRV_ENV */
2495 code = _namei_examine_file_spawn(&work, path3,
2502 #ifndef AFS_NT40_ENV
2510 #ifndef AFS_SALSRV_ENV
2518 #ifndef AFS_NT40_ENV /* This level missing on Windows */
2527 #ifdef AFS_SALSRV_ENV
2528 /* effectively a barrier */
2529 code = _namei_listsubdirs_wait(wq, &resultlist);
2530 if (code < 0 || error) {
2540 /* Then why does this directory exist? Blow it away. */
2541 namei_HandleToVolDir(&name, dirIH);
2542 namei_RemoveDataDirectories(&name);
2546 #ifdef AFS_SALSRV_ENV
2548 afs_wq_wait_all(wq);
2550 _namei_listsubdirs_cleanup_results(&resultlist);
2552 if (linkHandle.fd_fd != INVALID_FD)
2553 OS_CLOSE(linkHandle.fd_fd);
2565 DecodeVolumeName(char *name, unsigned int *vid)
2567 /* Name begins with "Vol_" and ends with .data. See nt_HandleToVolDir() */
2574 if (strncmp(name, "Vol_", 4))
2576 if (strcmp(name + len - 5, ".data"))
2579 stmp[len - 5] = '\0';
2580 *vid = base32_to_int(stmp + 4);
2585 DecodeVolumeName(char *name, unsigned int *vid)
2587 if (strlen(name) < 1)
2589 *vid = (unsigned int)flipbase64_to_int64(name);
2597 * Get the inode number from the name.
2602 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
2607 WIN32_FIND_DATA data;
2611 FdHandle_t linkHandle;
2614 snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
2616 dirH = FindFirstFileEx(fpath, FindExInfoStandard, &data,
2617 FindExSearchNameMatch, NULL,
2618 FIND_FIRST_EX_CASE_SENSITIVE);
2619 if (dirH == INVALID_HANDLE_VALUE)
2622 (void)strcpy(stmp, name);
2623 s = strrchr(stmp, '_');
2627 t = strrchr(s, '.');
2632 vno = base32_to_int(s); /* type for special files */
2633 tag = base32_to_int(t+1);
2634 info->inodeNumber = ((Inode) tag) << NAMEI_TAGSHIFT;
2635 info->inodeNumber |= vno;
2636 info->byteCount = data.nFileSizeLow;
2638 dirl = dpath[strlen(dpath)-1];
2639 if (dirl == NAMEI_SPECDIRC) { /* Special inode. */
2640 info->inodeNumber |= NAMEI_INODESPECIAL;
2641 info->u.param[0] = data.ftCreationTime.dwHighDateTime;
2642 info->u.param[1] = data.ftCreationTime.dwLowDateTime;
2643 info->u.param[2] = vno; /* type */
2644 info->u.param[3] = volid;
2645 if (vno != VI_LINKTABLE)
2646 info->linkCount = 1;
2648 /* Open this handle */
2650 (void)sprintf(lpath, "%s" OS_DIRSEP "%s", fpath, data.cFileName);
2651 linkHandle.fd_fd = OS_OPEN(lpath, O_RDONLY, 0666);
2653 namei_GetLinkCount(&linkHandle, (Inode) 0, 0, 0, 0);
2657 namei_GetLinkCount(&linkHandle, info->inodeNumber, 0, 0, 0);
2658 if (info->linkCount == 0) {
2660 Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n",
2661 fpath, data.cFileName);
2662 AddToZLCDeleteList(dirl, data.cFileName);
2664 Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path,
2668 info->u.param[2] = data.ftCreationTime.dwHighDateTime;
2669 info->u.param[3] = data.ftCreationTime.dwLowDateTime;
2670 info->u.param[1] = vno;
2671 info->u.param[0] = volid;
2678 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
2682 struct afs_stat_st status;
2684 lb64_string_t check;
2686 snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
2688 if (afs_stat(fpath, &status) < 0) {
2692 info->byteCount = status.st_size;
2693 info->inodeNumber = (Inode) flipbase64_to_int64(name);
2695 int64_to_flipbase64(check, info->inodeNumber);
2696 if (strcmp(name, check))
2699 GetOGMFromStat(&status, &parm, &tag);
2700 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
2701 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
2702 info->u.param[0] = parm;
2703 info->u.param[1] = -1;
2704 info->u.param[2] = tag;
2705 info->u.param[3] = volid;
2707 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
2708 info->u.param[0] = volid;
2709 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
2710 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
2711 & (Inode) NAMEI_UNIQMASK);
2712 info->u.param[3] = parm;
2719 * Convert the VolumeInfo file from RO to RW
2720 * this routine is called by namei_convertROtoRWvolume()
2723 #ifdef FSSYNC_BUILD_CLIENT
2725 convertVolumeInfo(FD_t fdr, FD_t fdw, afs_uint32 vid)
2727 struct VolumeDiskData vd;
2730 if (OS_READ(fdr, &vd, sizeof(struct VolumeDiskData)) !=
2731 sizeof(struct VolumeDiskData)) {
2732 Log("1 convertVolumeInfo: read failed for %lu with code %d\n",
2733 afs_printable_uint32_lu(vid),
2737 vd.restoredFromId = vd.id; /* remember the RO volume here */
2739 vd.id = vd.parentId;
2743 vd.uniquifier += 5000; /* just in case there are still file copies from
2744 * the old RW volume around */
2746 /* For ROs, the copyDate contains the time that the RO volume was actually
2747 * created, and the creationDate just contains the last time the RO was
2748 * copied from the RW data. So, make the new RW creationDate more accurate
2749 * by setting it to copyDate, if copyDate is older. Since, we know the
2750 * volume is at least as old as copyDate. */
2751 if (vd.copyDate < vd.creationDate) {
2752 vd.creationDate = vd.copyDate;
2754 /* If copyDate is newer, just make copyDate and creationDate the same,
2755 * for consistency with other RWs */
2756 vd.copyDate = vd.creationDate;
2759 p = strrchr(vd.name, '.');
2760 if (p && !strcmp(p, ".readonly")) {
2763 if (OS_WRITE(fdw, &vd, sizeof(struct VolumeDiskData)) !=
2764 sizeof(struct VolumeDiskData)) {
2765 Log("1 convertVolumeInfo: write failed for %lu with code %d\n",
2766 afs_printable_uint32_lu(vid),
2775 * Convert a RO-volume into a RW-volume
2777 * This function allows to recover very fast from the loss of a partition
2778 * from RO-copies if all RO-Copies exist on another partition.
2779 * Then these RO-volumes can be made to the new RW-volumes.
2780 * Backup of RW-volumes then consists in "vos release".
2782 * We must make sure in this partition exists only the RO-volume which
2783 * is typical for remote replicas.
2785 * Then the linktable is already ok,
2786 * the vnode files need to be renamed
2787 * the volinfo file needs to be replaced by another one with
2788 * slightly different contents and new name.
2789 * The volume header file of the RO-volume in the /vicep<x> directory
2790 * is destroyed by this call. A new header file for the RW-volume must
2791 * be created after return from this routine.
2795 namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
2798 #ifdef FSSYNC_BUILD_CLIENT
2800 char dir_name[512], oldpath[512], newpath[512];
2815 struct DiskPartition64 *partP;
2816 struct ViceInodeInfo info;
2817 struct VolumeDiskHeader h;
2818 # ifdef AFS_DEMAND_ATTACH_FS
2820 # endif /* AFS_DEMAND_ATTACH_FS */
2822 for (partP = DiskPartitionList; partP && strcmp(partP->name, pname);
2823 partP = partP->next);
2825 Log("1 namei_ConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
2830 # ifdef AFS_DEMAND_ATTACH_FS
2831 locktype = VVolLockType(V_VOLUPD, 1);
2832 code = VLockVolumeByIdNB(volumeId, partP, locktype);
2838 # endif /* AFS_DEMAND_ATTACH_FS */
2840 if (VReadVolumeDiskHeader(volumeId, partP, &h)) {
2841 Log("1 namei_ConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n",
2842 afs_printable_uint32_lu(volumeId));
2847 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
2849 ino = namei_MakeSpecIno(h.parent, VI_LINKTABLE);
2850 IH_INIT(ih, partP->device, h.parent, ino);
2852 namei_HandleToName(&n, ih);
2853 strlcpy(dir_name, n.n_path, sizeof(dir_name));
2854 p = strrchr(dir_name, OS_DIRSEPC);
2856 dirp = opendir(dir_name);
2858 Log("1 namei_ConvertROtoRWvolume: Could not opendir(%s)\n", dir_name);
2863 while ((dp = readdir(dirp))) {
2864 /* struct ViceInodeInfo info; */
2865 #ifndef AFS_NT40_ENV
2866 if (*dp->d_name == '.')
2869 if (DecodeInode(dir_name, dp->d_name, &info, ih->ih_vid) < 0) {
2870 Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s" OS_DIRSEP "%s\n",
2871 dir_name, dp->d_name);
2876 if (info.u.param[1] != -1) {
2877 Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s" OS_DIRSEP "%s\n", dir_name, dp->d_name);
2882 if (info.u.param[0] != volumeId) {
2883 if (info.u.param[0] == ih->ih_vid) {
2884 if (info.u.param[2] == VI_LINKTABLE) { /* link table */
2889 Log("1 namei_ConvertROtoRWvolume: found special file %s" OS_DIRSEP "%s"
2890 " for volume %lu\n", dir_name, dp->d_name,
2891 afs_printable_uint32_lu(info.u.param[0]));
2896 if (info.u.param[2] == VI_VOLINFO) { /* volume info file */
2897 strlcpy(infoName, dp->d_name, sizeof(infoName));
2899 } else if (info.u.param[2] == VI_SMALLINDEX) { /* small vnodes file */
2900 strlcpy(smallName, dp->d_name, sizeof(smallName));
2902 } else if (info.u.param[2] == VI_LARGEINDEX) { /* large vnodes file */
2903 strlcpy(largeName, dp->d_name, sizeof(largeName));
2907 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);
2914 if (!infoSeen || !smallSeen || !largeSeen || !linkSeen) {
2915 Log("1 namei_ConvertROtoRWvolume: not all special files found in %s\n", dir_name);
2921 * If we come here then there was only a RO-volume and we can safely
2925 memset(&t_ih, 0, sizeof(t_ih));
2926 t_ih.ih_dev = ih->ih_dev;
2927 t_ih.ih_vid = ih->ih_vid;
2929 snprintf(oldpath, sizeof oldpath, "%s" OS_DIRSEP "%s", dir_name,
2931 fd = OS_OPEN(oldpath, O_RDWR, 0);
2932 if (fd == INVALID_FD) {
2933 Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
2938 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_VOLINFO);
2939 namei_HandleToName(&n, &t_ih);
2940 fd2 = OS_OPEN(n.n_path, O_CREAT | O_EXCL | O_RDWR, 0);
2941 if (fd2 == INVALID_FD) {
2942 Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
2947 code = convertVolumeInfo(fd, fd2, ih->ih_vid);
2951 OS_UNLINK(n.n_path);
2955 SetOGM(fd2, ih->ih_vid, 1);
2958 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_SMALLINDEX);
2959 namei_HandleToName(&n, &t_ih);
2960 snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
2962 fd = OS_OPEN(newpath, O_RDWR, 0);
2963 if (fd == INVALID_FD) {
2964 Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
2968 SetOGM(fd, ih->ih_vid, 2);
2971 MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
2973 link(newpath, n.n_path);
2977 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_LARGEINDEX);
2978 namei_HandleToName(&n, &t_ih);
2979 snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
2981 fd = OS_OPEN(newpath, O_RDWR, 0);
2982 if (fd == INVALID_FD) {
2983 Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
2987 SetOGM(fd, ih->ih_vid, 3);
2990 MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
2992 link(newpath, n.n_path);
2999 h.volumeInfo_hi = h.id;
3000 h.smallVnodeIndex_hi = h.id;
3001 h.largeVnodeIndex_hi = h.id;
3002 h.linkTable_hi = h.id;
3004 if (VCreateVolumeDiskHeader(&h, partP)) {
3005 Log("1 namei_ConvertROtoRWvolume: Couldn't write header for RW-volume %lu\n",
3006 afs_printable_uint32_lu(h.id));
3011 if (VDestroyVolumeDiskHeader(partP, volumeId, h.parent)) {
3012 Log("1 namei_ConvertROtoRWvolume: Couldn't unlink header for RO-volume %lu\n",
3013 afs_printable_uint32_lu(volumeId));
3016 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
3017 FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
3020 # ifdef AFS_DEMAND_ATTACH_FS
3022 VUnlockVolumeById(volumeId, partP);
3024 # endif /* AFS_DEMAND_ATTACH_FS */
3032 * returns a static string used to print either 32 or 64 bit inode numbers.
3035 PrintInode(char *s, Inode ino)
3037 static afs_ino_str_t result;
3041 snprintf(s, sizeof(afs_ino_str_t), "%llu", (afs_uintmax_t) ino);
3048 /* Routines to facilitate removing zero link count files. */
3049 #define MAX_ZLC_NAMES 32
3050 #define MAX_ZLC_NAMELEN 16
3051 typedef struct zlcList_s {
3052 struct zlcList_s *zlc_next;
3054 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
3057 static zlcList_t *zlcAnchor = NULL;
3058 static zlcList_t *zlcCur = NULL;
3061 AddToZLCDeleteList(char dir, char *name)
3063 osi_Assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
3065 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
3066 if (zlcCur && zlcCur->zlc_next)
3067 zlcCur = zlcCur->zlc_next;
3069 zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
3075 zlcCur->zlc_next = tmp;
3079 zlcCur->zlc_next = NULL;
3084 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c" OS_DIRSEP "%s", dir, name);
3086 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%s", name);
3092 DeleteZLCFiles(char *path)
3098 for (z = zlcAnchor; z; z = z->zlc_next) {
3099 for (i = 0; i < z->zlc_n; i++) {
3101 (void)sprintf(fname, "%s" OS_DIRSEP "%s", path, z->zlc_names[i]);
3103 (void)sprintf(fname, "%s", z->zlc_names[i]);
3104 if (namei_unlink(fname) < 0) {
3105 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
3109 z->zlc_n = 0; /* Can reuse space. */
3122 tnext = i->zlc_next;
3126 zlcCur = zlcAnchor = NULL;
3130 #endif /* AFS_NAMEI_ENV */