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>
32 #include <afs/afs_assert.h>
34 #include <afs/afsutil.h>
37 #include <afs/afsint.h>
41 #include "viceinode.h"
43 #include "partition.h"
45 #include "volume_inline.h"
47 #include <afs/errors.h>
50 #include <afs/errmap_nt.h>
54 #define LOCK_SH 1 /* shared lock */
55 #define LOCK_EX 2 /* exclusive lock */
56 #define LOCK_NB 4 /* don't block when locking */
57 #define LOCK_UN 8 /* unlock */
62 #include <afs/work_queue.h>
63 #include <afs/thread_pool.h>
64 #include <vol/vol-salvage.h>
71 namei_iread(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
80 nBytes = FDH_PREAD(fdP, buf, size, offset);
86 namei_iwrite(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
95 nBytes = FDH_PWRITE(fdP, buf, size, offset);
101 /* Inode number format:
102 * low 32 bits - if a regular file or directory, the vnode; else the type.
103 * 32-36 - uniquifier tag and index into counts array for this vnode. Only
104 * two of the available bits are currently used. The rest are
105 * present in case we ever increase the number of types of volumes
106 * in the volume group.
107 * bit 37 : 1 == special, 0 == regular
109 # define NAMEI_VNODEMASK 0x00ffffffff
110 # define NAMEI_TAGSHIFT 32
111 # define NAMEI_INODESPECIAL 0x2000000000
112 # define NAMEI_SPECDIR "R"
113 # define NAMEI_SPECDIRC 'R'
114 #else /* !AFS_NT40_ENV */
115 /* Inode number format:
116 * low 26 bits - vnode number - all 1's if volume special file.
118 * next 3 bits spare (0's)
119 * high 32 bits - uniquifier (regular) or type if spare
121 # define NAMEI_VNODEMASK 0x003ffffff
122 # define NAMEI_TAGSHIFT 26
123 # define NAMEI_UNIQMASK 0xffffffff
124 # define NAMEI_UNIQSHIFT 32
125 # define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
126 /* dir1 is the high 8 bits of the 26 bit vnode */
127 # define VNO_DIR1(vno) ((vno >> 14) & 0xff)
128 /* dir2 is the next 9 bits */
129 # define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
130 /* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
131 # define NAMEI_SPECDIR "special"
132 #endif /* !AFS_NT40_ENV */
133 #define NAMEI_TAGMASK 0x7
134 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
136 #define NAMEI_SPECDIRLEN (sizeof(NAMEI_SPECDIR)-1)
138 #define NAMEI_MAXVOLS 5 /* Maximum supported number of volumes per volume
139 * group, not counting temporary (move) volumes.
140 * This is the number of separate files, all having
141 * the same vnode number, which can occur in a volume
153 static int GetFreeTag(IHandle_t * ih, int vno);
155 /* namei_HandleToInodeDir
157 * Construct the path name of the directory holding the inode data.
158 * Format: /<vicepx>/INODEDIR
163 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
165 memset(name, '\0', sizeof(*name));
166 nt_DevToDrive(name->n_drive, ih->ih_dev);
167 strlcpy(name->n_path, name->n_drive, sizeof(name->n_path));
171 /* Format: /<vicepx>/INODEDIR */
172 #define PNAME_BLEN 64
174 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
178 memset(name, '\0', sizeof(*name));
181 * Add the /vicepXX string to the start of name->n_base and then calculate
182 * offset as the number of bytes we know we added.
184 * FIXME: This embeds knowledge of the vice partition naming scheme and
185 * mapping from device numbers. There needs to be an API that tells us
188 volutil_PartitionName_r(ih->ih_dev, name->n_base, sizeof(name->n_base));
189 offset = VICE_PREFIX_SIZE + (ih->ih_dev > 25 ? 2 : 1);
190 name->n_base[offset] = OS_DIRSEPC;
192 strlcpy(name->n_base + offset, INODEDIR, sizeof(name->n_base) - offset);
193 strlcpy(name->n_path, name->n_base, sizeof(name->n_path));
197 #define addtoname(N, C) \
199 if ((N)->n_path[strlen((N)->n_path)-1] != OS_DIRSEPC) \
200 strlcat((N)->n_path, OS_DIRSEP, sizeof((N)->n_path)); \
201 strlcat((N)->n_path, (C), sizeof((N)->n_path)); \
207 namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
209 /* X:\Vol_XXXXXXX.data */
213 namei_HandleToInodeDir(name, ih);
214 /* nt_drive added to name by namei_HandleToInodeDir() */
215 namep = name->n_voldir;
216 (void)memcpy(namep, "\\Vol_", 5);
218 (void)strcpy(namep, int_to_base32(str1, ih->ih_vid));
219 namep += strlen(namep);
220 memcpy(namep, ".data", 5);
223 addtoname(name, name->n_voldir);
227 namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
231 namei_HandleToInodeDir(name, ih);
232 (void)int32_to_flipbase64(tmp, (int64_t) (ih->ih_vid & 0xff));
233 strlcpy(name->n_voldir1, tmp, sizeof(name->n_voldir1));
234 addtoname(name, name->n_voldir1);
235 (void)int32_to_flipbase64(tmp, (int64_t) ih->ih_vid);
236 strlcpy(name->n_voldir2, tmp, sizeof(name->n_voldir2));
237 addtoname(name, name->n_voldir2);
241 /* namei_HandleToName
243 * Constructs a file name for the fully qualified handle.
246 /* Note that special files end up in X:\Vol_XXXXXXX.data\R */
248 namei_HandleToName(namei_t * name, IHandle_t * ih)
250 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
251 int special = (ih->ih_ino & NAMEI_INODESPECIAL)?1:0;
252 int tag = (int)((ih->ih_ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
255 namei_HandleToVolDir(name, ih);
258 name->n_dir[0] = NAMEI_SPECDIRC;
261 name->n_dir[0] = 'Q';
263 name->n_dir[0] = ((vno & 0x1f) >> 1) + 'A';
266 name->n_dir[1] = '\0';
267 addtoname(name, name->n_dir);
268 /* X:\Vol_XXXXXXX.data\X\V_XXXXXXX.XXX */
269 namep = name->n_inode;
270 (void)memcpy(namep, "\\V_", 3);
272 (void)strcpy(namep, int_to_base32(str1, vno));
273 namep += strlen(namep);
275 (void)strcpy(namep, int_to_base32(str1, tag));
276 namep += strlen(namep);
277 addtoname(name, name->n_inode);
280 /* Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special */
282 namei_HandleToName(namei_t * name, IHandle_t * ih)
284 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
287 namei_HandleToVolDir(name, ih);
289 if (vno == NAMEI_VNODESPECIAL) {
290 strlcpy(name->n_dir1, NAMEI_SPECDIR, sizeof(name->n_dir1));
291 addtoname(name, name->n_dir1);
292 name->n_dir2[0] = '\0';
294 (void)int32_to_flipbase64(str, VNO_DIR1(vno));
295 strlcpy(name->n_dir1, str, sizeof(name->n_dir1));
296 addtoname(name, name->n_dir1);
297 (void)int32_to_flipbase64(str, VNO_DIR2(vno));
298 strlcpy(name->n_dir2, str, sizeof(name->n_dir2));
299 addtoname(name, name->n_dir2);
301 (void)int64_to_flipbase64(str, (int64_t) ih->ih_ino);
302 strlcpy(name->n_inode, str, sizeof(name->n_inode));
303 addtoname(name, name->n_inode);
308 /* The following is a warning to tell sys-admins to not muck about in this
311 #define VICE_README "These files and directories are a part of the AFS \
312 namespace. Modifying them\nin any way will result in loss of AFS data,\n\
313 ownership and permissions included.\n"
315 namei_ViceREADME(char *partition)
320 /* Create the inode directory if we're starting for the first time */
321 snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s", partition,
323 mkdir(filename, 0700);
325 snprintf(filename, sizeof filename,
326 "%s" OS_DIRSEP "%s" OS_DIRSEP "README",
327 partition, INODEDIR);
328 fd = OS_OPEN(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
329 if (fd != INVALID_FD) {
330 (void)OS_WRITE(fd, VICE_README, strlen(VICE_README));
337 /* namei_CreateDataDirectories
339 * If creating the file failed because of ENOENT or ENOTDIR, try
340 * creating all the directories first.
344 namei_CreateDataDirectories(namei_t * name, int *created)
351 snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
353 if (mkdir(tmp) < 0) {
364 for (i = 'A'; i <= NAMEI_SPECDIRC; i++) {
366 if (mkdir(tmp) < 0 && errno != EEXIST)
372 #define create_dir() \
374 if (mkdir(tmp, 0700)<0) { \
375 if (errno != EEXIST) \
383 #define create_nextdir(A) \
385 strcat(tmp, OS_DIRSEP); strcat(tmp, A); create_dir(); \
389 namei_CreateDataDirectories(namei_t * name, int *created)
395 strlcpy(tmp, name->n_base, sizeof(tmp));
398 create_nextdir(name->n_voldir1);
399 create_nextdir(name->n_voldir2);
400 create_nextdir(name->n_dir1);
401 if (name->n_dir2[0]) {
402 create_nextdir(name->n_dir2);
409 /* delTree(): Deletes an entire tree of directories (no files)
411 * root : Full path to the subtree. Should be big enough for PATH_MAX
412 * tree : the subtree to be deleted is rooted here. Specifies only the
413 * subtree beginning at tree (not the entire path). It should be
414 * a pointer into the "root" buffer.
416 * errp : errno of the first error encountered during the directory cleanup.
417 * *errp should have been initialized to 0.
420 * -1 : If errors were encountered during cleanup and error is set to
424 * If there are errors, we try to work around them and delete as many
425 * directories as possible. We don't attempt to remove directories that still
426 * have non-dir entries in them.
429 delTree(char *root, char *tree, int *errp)
437 /* delete the children first */
438 cp = strchr(tree, OS_DIRSEPC);
440 delTree(root, cp + 1, errp);
443 cp = tree + strlen(tree); /* move cp to the end of string tree */
445 /* now delete all entries in this dir */
446 if ((ds = opendir(root)) != (DIR *) NULL) {
448 while ((dirp = readdir(ds))) {
449 /* ignore . and .. */
450 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
452 /* since root is big enough, we reuse the space to
453 * concatenate the dirname to the current tree
455 strcat(root, OS_DIRSEP);
456 strcat(root, dirp->d_name);
457 if (afs_stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
458 /* delete this subtree */
459 delTree(root, cp + 1, errp);
461 *errp = *errp ? *errp : errno;
463 /* recover path to our cur tree by truncating it to
468 /* if (!errno) -- closedir not implicit if we got an error */
472 /* finally axe the current dir */
474 *errp = *errp ? *errp : errno;
476 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
478 #endif /* !AFS_PTHREAD_ENV */
483 /* if we encountered errors during cleanup, we return a -1 */
492 /* namei_RemoveDataDirectories
494 * Returns 0 on success.
495 * Returns -1 on error. Typically, callers ignore this error because we
496 * can continue running if the removes fail. The salvage process will
497 * finish tidying up for us.
502 namei_RemoveDataDirectories(namei_t * name)
509 snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
512 path += strlen(path);
513 *path++ = OS_DIRSEPC;
515 for (i = 'A'; i <= NAMEI_SPECDIRC; i++) {
517 if (rmdir(name->n_path) < 0 && errno != ENOENT)
522 /* Delete the Vol_NNNNNN.data directory. */
525 if (rmdir(name->n_path) < 0 && errno != ENOENT) {
533 * We only use the n_base and n_voldir1 entries
534 * and only do rmdir's.
537 namei_RemoveDataDirectories(namei_t * name)
541 int prefixlen = strlen(name->n_base), err = 0;
542 int vollen = strlen(name->n_voldir1);
543 char pbuf[MAXPATHLEN];
547 strlcpy(path, name->n_path, sizeof(pbuf));
549 /* move past the prefix and n_voldir1 */
550 path = path + prefixlen + 1 + vollen + 1; /* skip over the trailing / */
552 /* now delete all dirs upto path */
553 code = delTree(pbuf, path, &err);
555 /* We've now deleted everything under /n_base/n_voldir1/n_voldir2 that
556 * we could. Do not delete /n_base/n_voldir1, since doing such might
557 * interrupt another thread trying to create a volume. We could introduce
558 * some locking to make this safe (or only remove it for whole-partition
559 * salvages), but by not deleting it we only leave behind a maximum of
560 * 256 empty directories. So at least for now, don't bother. */
565 /* Create the file in the name space.
567 * Parameters stored as follows:
569 * p1 - volid - implied in containing directory.
570 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
571 * p3 - uniq -- bits 4-0 are in mode bits 4-0
572 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
574 * p1 - volid - creation time - dwHighDateTime
575 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
576 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
577 * p4 - parid - parent volume id - implied in containing directory.
579 * Return value is the inode number or (Inode)-1 if error.
580 * We "know" there is only one link table, so return EEXIST if there already
581 * is a link table. It's up to the calling code to test errno and increment
587 * This function is called by VCreateVolume to hide the implementation
588 * details of the inode numbers. This only allows for 7 volume special
589 * types, but if we get that far, this could should be dead by then.
592 namei_MakeSpecIno(int volid, int type)
595 ino = NAMEI_INODESPECIAL;
598 /* tag is always 0 for special */
600 type &= NAMEI_TAGMASK;
601 ino |= ((Inode) type) << NAMEI_TAGSHIFT;
602 ino |= ((Inode) volid) << NAMEI_UNIQSHIFT;
610 SetOGM(FD_t fd, int parm, int tag)
616 CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
618 WIN32_FIND_DATA info;
622 FindFirstFileEx(name->n_path, FindExInfoStandard, &info,
623 FindExSearchNameMatch, NULL,
624 FIND_FIRST_EX_CASE_SENSITIVE);
627 return -1; /* Can't get info, leave alone */
631 if (info.ftCreationTime.dwHighDateTime != (unsigned int)p1)
636 #else /* AFS_NT40_ENV */
637 /* SetOGM - set owner group and mode bits from parm and tag */
639 SetOGM(FD_t fd, int parm, int tag)
642 * owner - low 15 bits of parm.
643 * group - next 15 bits of parm.
644 * mode - 2 bits of parm, then lowest = 3 bits of tag.
646 int owner, group, mode;
648 owner = parm & 0x7fff;
649 group = (parm >> 15) & 0x7fff;
650 if (fchown(fd, owner, group) < 0)
653 mode = (parm >> 27) & 0x18;
655 if (fchmod(fd, mode) < 0)
660 /* GetOGM - get parm and tag from owner, group and mode bits. */
662 GetOGMFromStat(struct afs_stat_st *status, int *parm, int *tag)
664 *parm = status->st_uid | (status->st_gid << 15);
665 *parm |= (status->st_mode & 0x18) << 27;
666 *tag = status->st_mode & 0x7;
670 CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
672 struct afs_stat_st status;
674 if (afs_fstat(fdP->fd_fd, &status) < 0)
677 GetOGMFromStat(&status, &parm, &tag);
683 #endif /* !AFS_NT40_ENV */
685 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
687 /* Derive the name and create it O_EXCL. If that fails we have an error.
688 * Get the tag from a free column in the link table.
692 namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
695 FD_t fd = INVALID_FD;
706 memset((void *)&tmp, 0, sizeof(IHandle_t));
707 memset(&tfd, 0, sizeof(FdHandle_t));
709 tmp.ih_dev = nt_DriveToDev(part);
710 if (tmp.ih_dev == -1) {
715 if (p2 == INODESPECIAL) {
716 /* Parameters for special file:
717 * p1 - volume id - goes into owner/group/mode
718 * p2 - vnode == INODESPECIAL
720 * p4 - parent volume id
722 ftime.dwHighDateTime = p1;
723 ftime.dwLowDateTime = p2;
725 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
726 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
728 int vno = p2 & NAMEI_VNODEMASK;
729 /* Parameters for regular file:
743 tmp.ih_ino = (Inode) p2;
744 ftime.dwHighDateTime = p3;
745 ftime.dwLowDateTime = p4;
748 namei_HandleToName(&name, &tmp);
749 p = strrchr((char *)&name.n_path, '.');
751 for (tag = 0; tag < NAMEI_MAXVOLS; tag++) {
752 *p = *int_to_base32(str1, tag);
753 fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0666);
754 if (fd == INVALID_FD) {
755 if (errno == ENOTDIR || errno == ENOENT) {
756 if (namei_CreateDataDirectories(&name, &created_dir) == 0)
757 fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0666);
761 if (fd != INVALID_FD)
763 if (p2 == INODESPECIAL && p3 == VI_LINKTABLE)
766 if (fd == INVALID_FD) {
770 tmp.ih_ino &= ~(((Inode) NAMEI_TAGMASK) << NAMEI_TAGSHIFT);
771 tmp.ih_ino |= (((Inode) tag) << NAMEI_TAGSHIFT);
774 if (!SetFileTime((HANDLE) fd, &ftime, NULL, NULL)) {
775 errno = OS_ERROR(EBADF);
781 if (p2 != INODESPECIAL) {
782 if (fd == INVALID_FD) {
784 code = nt_unlink((char *)&name.n_path);
793 code = namei_SetLinkCount(fdP, tmp.ih_ino, 1, 0);
795 } else if (p2 == INODESPECIAL && p3 == VI_LINKTABLE) {
796 if (fd == INVALID_FD)
798 /* hack at tmp to setup for set link count call. */
800 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
805 if (fd != INVALID_FD)
808 if (code || (fd == INVALID_FD)) {
809 if (p2 != INODESPECIAL) {
812 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
818 int save_errno = errno;
819 namei_RemoveDataDirectories(&name);
823 return (code || (fd == INVALID_FD)) ? (Inode) -1 : tmp.ih_ino;
825 #else /* !AFS_NT40_ENV */
827 namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
839 memset((void *)&tmp, 0, sizeof(IHandle_t));
840 memset(&tfd, 0, sizeof(FdHandle_t));
842 tmp.ih_dev = volutil_GetPartitionID(part);
843 if (tmp.ih_dev == -1) {
849 /* Parameters for special file:
850 * p1 - volume id - goes into owner/group/mode
853 * p4 - parent volume id
858 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
859 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
861 int vno = p2 & NAMEI_VNODEMASK;
862 /* Parameters for regular file:
874 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
875 tag = GetFreeTag(lh, p2);
880 tmp.ih_ino = (Inode) p2;
881 /* name is <uniq(p3)><tag><vno(p2)> */
882 tmp.ih_ino |= ((Inode) tag) << NAMEI_TAGSHIFT;
883 tmp.ih_ino |= ((Inode) p3) << NAMEI_UNIQSHIFT;
888 namei_HandleToName(&name, &tmp);
889 fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
890 if (fd == INVALID_FD) {
891 if (errno == ENOTDIR || errno == ENOENT) {
892 if (namei_CreateDataDirectories(&name, &created_dir) < 0)
894 fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR,
896 if (fd == INVALID_FD)
902 if (SetOGM(fd, ogm_parm, tag) < 0) {
908 if (p2 == (afs_uint32)-1 && p3 == VI_LINKTABLE) {
909 /* hack at tmp to setup for set link count call. */
910 memset((void *)&tfd, 0, sizeof(FdHandle_t)); /* minimalistic still, but a little cleaner */
913 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
917 if (fd != INVALID_FD)
921 if (code || (fd == INVALID_FD)) {
925 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
930 return (code || (fd == INVALID_FD)) ? (Inode) - 1 : tmp.ih_ino;
936 namei_iopen(IHandle_t * h)
941 /* Convert handle to file name. */
942 namei_HandleToName(&name, h);
943 fd = OS_OPEN((char *)&name.n_path, O_RDWR, 0666);
947 /* Need to detect vol special file and just unlink. In those cases, the
948 * handle passed in _is_ for the inode. We only check p1 for the special
952 namei_dec(IHandle_t * ih, Inode ino, int p1)
959 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
961 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
963 /* Verify this is the right file. */
964 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
966 namei_HandleToName(&name, tmp);
971 errno = OS_ERROR(ENOENT);
975 if (CheckOGM(&name, fdP, p1) < 0) {
976 FDH_REALLYCLOSE(fdP);
978 errno = OS_ERROR(EINVAL);
982 /* If it's the link table itself, decrement the link count. */
983 if (type == VI_LINKTABLE) {
984 if ((count = namei_GetLinkCount(fdP, (Inode) 0, 1, 0, 1)) < 0) {
985 FDH_REALLYCLOSE(fdP);
991 if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) <
993 FDH_REALLYCLOSE(fdP);
999 FDH_REALLYCLOSE(fdP);
1005 if ((code = OS_UNLINK(name.n_path)) == 0) {
1006 if (type == VI_LINKTABLE) {
1007 /* Try to remove directory. If it fails, that's ok.
1008 * Salvage will clean up.
1010 (void)namei_RemoveDataDirectories(&name);
1013 FDH_REALLYCLOSE(fdP);
1016 /* Get a file descriptor handle for this Inode */
1022 if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0) {
1023 FDH_REALLYCLOSE(fdP);
1029 if (namei_SetLinkCount(fdP, ino, count, 1) < 0) {
1030 FDH_REALLYCLOSE(fdP);
1035 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
1036 Log("Warning: Lost ref on ihandle dev %d vid %d ino %lld\n",
1037 th->ih_dev, th->ih_vid, (afs_int64)th->ih_ino);
1040 /* If we're less than 0, someone presumably unlinked;
1041 don't bother setting count to 0, but we need to drop a lock */
1042 if (namei_SetLinkCount(fdP, ino, 0, 1) < 0) {
1043 FDH_REALLYCLOSE(fdP);
1049 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
1051 namei_HandleToName(&name, th);
1053 code = OS_UNLINK(name.n_path);
1062 namei_inc(IHandle_t * h, Inode ino, int p1)
1068 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1069 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1070 if (type != VI_LINKTABLE)
1075 /* Get a file descriptor handle for this Inode */
1081 if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0)
1086 errno = OS_ERROR(EINVAL);
1090 if (namei_SetLinkCount(fdP, ino, count, 1) < 0)
1094 FDH_REALLYCLOSE(fdP);
1101 #ifndef AFS_NT40_ENV
1103 namei_replace_file_by_hardlink(IHandle_t *hLink, IHandle_t *hTarget)
1109 /* Convert handle to file name. */
1110 namei_HandleToName(&nameLink, hLink);
1111 namei_HandleToName(&nameTarget, hTarget);
1113 OS_UNLINK(nameLink.n_path);
1114 code = link(nameTarget.n_path, nameLink.n_path);
1119 namei_copy_on_write(IHandle_t *h)
1125 struct afs_stat_st tstat;
1128 namei_HandleToName(&name, h);
1129 if (afs_stat(name.n_path, &tstat) < 0)
1131 if (tstat.st_nlink > 1) { /* do a copy on write */
1140 snprintf(path, sizeof(path), "%s-tmp", name.n_path);
1141 fd = OS_OPEN(path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
1142 if (fd == INVALID_FD) {
1153 size = tstat.st_size;
1156 tlen = size > 8192 ? 8192 : size;
1157 if (FDH_PREAD(fdP, buf, tlen, offset) != tlen)
1159 if (OS_WRITE(fd, buf, tlen) != tlen)
1165 FDH_REALLYCLOSE(fdP);
1170 OS_UNLINK(name.n_path);
1171 code = rename(path, name.n_path);
1178 /************************************************************************
1179 * File Name Structure
1180 ************************************************************************
1182 * Each AFS file needs a unique name and it needs to be findable with
1183 * minimal lookup time. Note that the constraint on the number of files and
1184 * directories in a volume is the size of the vnode index files and the
1185 * max file size AFS supports (for internal files) of 2^31. Since a record
1186 * in the small vnode index file is 64 bytes long, we can have at most
1187 * (2^31)/64 or 33554432 files. A record in the large index file is
1188 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
1189 * Another layout parameter is that there is roughly a 16 to 1 ratio between
1190 * the number of files and the number of directories.
1192 * Using this information we can see that a layout of 256 directories, each
1193 * with 512 subdirectories and each of those having 512 files gives us
1194 * 256*512*512 = 67108864 AFS files and directories.
1196 * The volume, vnode, uniquifier and data version, as well as the tag
1197 * are required, either for finding the file or for salvaging. It's best to
1198 * restrict the name to something that can be mapped into 64 bits so the
1199 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
1200 * is used to distinguish between different versions of the same file
1201 * which are currently in the RW and clones of a volume. See "Link Table
1202 * Organization" below for more information on the tag. The tag is
1203 * required in the name of the file to ensure a unique name.
1205 * ifdef AFS_NT40_ENV
1206 * The data for each volume group is in a separate directory. The name of the
1207 * volume is of the form: Vol_NNNNNN.data, where NNNNNN is a base 32
1208 * representation of the RW volume ID (even where the RO is the only volume
1209 * on the partition). Below that are separate subdirectories for the
1210 * AFS directories and special files. There are also 16 directories for files,
1211 * hashed on the low 5 bits (recall bit0 is always 0) of the vnode number.
1212 * These directories are named:
1213 * A - P - 16 file directories.
1214 * Q ----- data directory
1215 * R ----- special files directory
1217 * The vnode is hashed into the directory using the low bits of the
1220 * The format of a file name for a regular file is:
1221 * Y:\Vol_NNNNNN.data\X\V_IIIIII.J
1222 * Y - partition encoded as drive letter, starting with D
1223 * NNNNNN - base 32 encoded volume number of RW volume
1224 * X - hash directory, as above
1225 * IIIIII - base 32 encoded vnode number
1226 * J - base 32 encoded tag
1228 * uniq is stored in the dwHighDateTime creation time field
1229 * dv is stored in the dwLowDateTime creation time field
1231 * Special inodes are always in the R directory, as above, and are
1233 * True child volid is stored in the dwHighDateTime creation time field
1234 * vnode number is always -1 (Special)
1235 * type is the IIIIII part of the filename
1236 * uniq is the J part of the filename
1237 * parent volume id is implied in the containing directory
1240 * We can store data in the uid, gid and mode bits of the files, provided
1241 * the directories have root only access. This gives us 15 bits for each
1242 * of uid and gid (GNU chown considers 65535 to mean "don't change").
1243 * There are 9 available mode bits. Adn we need to store a total of
1244 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
1245 * or 131 bits somewhere.
1247 * The format of a file name for a regular file is:
1248 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
1249 * V1 - low 8 bits of RW volume id
1250 * V2 - all bits of RW volume id
1251 * AA - high 8 bits of vnode number.
1252 * BB - next 9 bits of vnode number.
1253 * <tag><uniq><vno> - file name
1255 * Volume special files are stored in a separate directory:
1256 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
1259 * The vnode is hashed into the directory using the high bits of the
1260 * vnode number. This is so that consecutively created vnodes are in
1261 * roughly the same area on the disk. This will at least be optimal if
1262 * the user is creating many files in the same AFS directory. The name
1263 * should be formed so that the leading characters are different as quickly
1264 * as possible, leading to faster discards of incorrect matches in the
1272 /************************************************************************
1273 * Link Table Organization
1274 ************************************************************************
1276 * The link table volume special file is used to hold the link counts that
1277 * are held in the inodes in inode based AFS vice filesystems. For user
1278 * space access, the link counts are being kept in a separate
1279 * volume special file. The file begins with the usual version stamp
1280 * information and is then followed by one row per vnode number. vnode 0
1281 * is used to hold the link count of the link table itself. That is because
1282 * the same link table is shared among all the volumes of the volume group
1283 * and is deleted only when the last volume of a volume group is deleted.
1285 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
1286 * link count from 0 through 7. Each colume represents a unique instance of
1287 * that vnode. Say we have a file shared between the RW and a RO and a
1288 * different version of the file (or a different uniquifer) for the BU volume.
1289 * Then one column would be holding the link count of 2 for the RW and RO
1290 * and a different column would hold the link count of 1 for the BU volume.
1291 * # ifdef AFS_NT40_ENV
1292 * The column used is determined for NT by the uniquifier tag applied to
1293 * generate a unique file name in the NTFS namespace. The file name is
1294 * of the form "V_<vno>.<tag>" . And the <tag> is also the column number
1295 * in the link table.
1297 * Note that we allow only 5 volumes per file, giving 15 bits used in the
1301 #define LINKTABLE_WIDTH 2
1302 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
1305 * compute namei link table file and bit offset from inode number.
1307 * @param[in] ino inode number
1308 * @param[out] offset link table file offset
1309 * @param[out] index bit offset within 2-byte record
1314 namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
1316 int toff = (int)(ino & NAMEI_VNODEMASK);
1317 int tindex = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1319 *offset = (afs_foff_t) ((toff << LINKTABLE_SHIFT) + 8); /* * 2 + sizeof stamp */
1320 *index = (tindex << 1) + tindex;
1323 #ifdef AFS_PTHREAD_ENV
1324 /* XXX do static initializers work for WINNT/pthread? */
1325 pthread_mutex_t _namei_glc_lock = PTHREAD_MUTEX_INITIALIZER;
1326 #define NAMEI_GLC_LOCK MUTEX_ENTER(&_namei_glc_lock)
1327 #define NAMEI_GLC_UNLOCK MUTEX_EXIT(&_namei_glc_lock)
1328 #else /* !AFS_PTHREAD_ENV */
1329 #define NAMEI_GLC_LOCK
1330 #define NAMEI_GLC_UNLOCK
1331 #endif /* !AFS_PTHREAD_ENV */
1334 * get the link count of an inode.
1336 * @param[in] h namei link count table file handle
1337 * @param[in] ino inode number for which we are requesting a link count
1338 * @param[in] lockit if asserted, return with lock held on link table file
1339 * @param[in] fixup if asserted, write 1 to link count when read() returns
1341 * @param[in] nowrite return success on zero byte read or ZLC
1343 * @post if lockit asserted and lookup was successful, will return with write
1344 * lock on link table file descriptor
1346 * @return link count
1347 * @retval -1 namei link table i/o error
1352 namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite)
1354 unsigned short row = 0;
1359 /* there's no linktable yet. the salvager will create one later */
1360 if (h->fd_fd == INVALID_FD && fixup)
1362 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1365 if (FDH_LOCKFILE(h, offset) != 0)
1369 rc = FDH_PREAD(h, (char*)&row, sizeof(row), offset);
1370 if ((rc == 0 || !((row >> index) & NAMEI_TAGMASK)) && fixup && nowrite)
1372 if (rc == 0 && fixup) {
1374 * extend link table and write a link count of 1 for ino
1376 * in order to make MT-safe, truncation (extension really)
1377 * must happen under a mutex
1380 if (FDH_SIZE(h) >= offset+sizeof(row)) {
1382 goto bad_getLinkByte;
1384 FDH_TRUNC(h, offset+sizeof(row));
1386 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
1389 if (rc != sizeof(row)) {
1390 goto bad_getLinkByte;
1393 if (fixup && !((row >> index) & NAMEI_TAGMASK)) {
1397 * in order to make this mt-safe, we need to do the read-modify-write
1398 * under a mutex. thus, we repeat the read inside the lock.
1401 rc = FDH_PREAD(h, (char *)&row, sizeof(row), offset);
1402 if (rc == sizeof(row)) {
1404 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
1407 if (rc != sizeof(row))
1408 goto bad_getLinkByte;
1411 return (int)((row >> index) & NAMEI_TAGMASK);
1415 FDH_UNLOCKFILE(h, offset);
1420 namei_SetNonZLC(FdHandle_t * h, Inode ino)
1422 return namei_GetLinkCount(h, ino, 0, 1, 0);
1425 /* Return a free column index for this vnode. */
1427 GetFreeTag(IHandle_t * ih, int vno)
1441 /* Only one manipulates at a time. */
1442 if (FDH_LOCKFILE(fdP, offset) != 0) {
1443 FDH_REALLYCLOSE(fdP);
1447 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
1449 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1450 if (nBytes != sizeof(row)) {
1456 /* Now find a free column in this row and claim it. */
1457 for (col = 0; col < NAMEI_MAXVOLS; col++) {
1458 coldata = 7 << (col * 3);
1459 if ((row & coldata) == 0)
1462 if (col >= NAMEI_MAXVOLS) {
1467 coldata = 1 << (col * 3);
1470 if (FDH_PWRITE(fdP, (char *)&row, sizeof(row), offset) != sizeof(row)) {
1474 FDH_UNLOCKFILE(fdP, offset);
1475 FDH_REALLYCLOSE(fdP);
1479 FDH_UNLOCKFILE(fdP, offset);
1480 FDH_REALLYCLOSE(fdP);
1486 /* namei_SetLinkCount
1487 * If locked is set, assume file is locked. Otherwise, lock file before
1488 * proceeding to modify it.
1491 namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
1497 ssize_t nBytes = -1;
1499 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1502 if (FDH_LOCKFILE(fdP, offset) != 0) {
1507 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1508 if (nBytes != sizeof(row)) {
1510 errno = OS_ERROR(EBADF);
1511 goto bad_SetLinkCount;
1516 bytesRead = 7 << index;
1518 row &= (unsigned short)~bytesRead;
1519 row |= (unsigned short)count;
1521 if (FDH_PWRITE(fdP, (char *)&row, sizeof(short), offset) != sizeof(short)) {
1522 errno = OS_ERROR(EBADF);
1523 goto bad_SetLinkCount;
1531 FDH_UNLOCKFILE(fdP, offset);
1533 /* disallowed above 7, so... */
1538 /* ListViceInodes - write inode data to a results file. */
1539 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1540 unsigned int volid);
1541 static int DecodeVolumeName(char *name, unsigned int *vid);
1542 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
1543 int (*write_fun) (FD_t,
1544 struct ViceInodeInfo *,
1545 char *, char *), FD_t fp,
1546 int (*judgeFun) (struct ViceInodeInfo *,
1547 afs_uint32 vid, void *),
1548 afs_uint32 singleVolumeNumber, void *rock);
1553 * Write the inode data to the results file.
1555 * Returns -2 on error, 0 on success.
1557 * This is written as a callback simply so that other listing routines
1558 * can use the same inode reading code.
1561 WriteInodeInfo(FD_t fp, struct ViceInodeInfo *info, char *dir, char *name)
1564 n = OS_WRITE(fp, info, sizeof(*info));
1565 return (n == sizeof(*info)) ? 0 : -2;
1569 int mode_errors; /* Number of errors found in mode bits on directories. */
1571 VerifyDirPerms(char *path)
1573 struct afs_stat_st status;
1575 if (afs_stat(path, &status) < 0) {
1576 Log("Unable to stat %s. Please manually verify mode bits for this"
1577 " directory\n", path);
1579 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1585 * Fill the results file with the requested inode information.
1587 * This code optimizes single volume salvages by just looking at that one
1588 * volume's directory.
1590 * @param[in] devname device name string
1591 * @param[in] moutnedOn vice partition mount point
1592 * @param[in] resultFile result file in which to write inode
1593 * metadata. If NULL, write routine is not
1595 * @param[in] judgeInode filter function pointer. if not NULL, only
1596 * inodes for which this routine returns non-
1597 * zero will be written to the results file.
1598 * @param[in] singleVolumeNumber volume id filter
1599 * @param[out] forcep always set to 0 for namei impl
1600 * @param[in] forceR not used by namei impl
1601 * @param[in] wpath not used by namei impl
1602 * @param[in] rock opaque pointer passed to judgeInode
1604 * @return operation status
1606 * @retval -1 complete failure, salvage should terminate.
1607 * @retval -2 not enough space on partition, salvager has error message
1611 ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
1612 int (*judgeInode) (struct ViceInodeInfo * info, afs_uint32 vid, void *rock),
1613 afs_uint32 singleVolumeNumber, int *forcep, int forceR, char *wpath,
1618 *forcep = 0; /* no need to salvage until further notice */
1620 /* Verify protections on directories. */
1622 VerifyDirPerms(mountedOn);
1625 namei_ListAFSFiles(mountedOn, WriteInodeInfo, inodeFile, judgeInode,
1626 singleVolumeNumber, rock);
1628 if (inodeFile == INVALID_FD)
1635 if (OS_SYNC(inodeFile) == -1) {
1636 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1641 * Paranoia: check that the file is really the right size
1643 if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
1644 Log("Wrong size (%d instead of %lu) in inode file for %s\n",
1645 (int) OS_SIZE(inodeFile),
1646 (long unsigned int) ninodes * sizeof(struct ViceInodeInfo),
1655 * Collect all the matching AFS files on the drive.
1656 * If singleVolumeNumber is non-zero, just return files for that volume.
1658 * @param[in] dev vice partition path
1659 * @param[in] writeFun function pointer to a function which
1660 * writes inode information to FILE fp
1661 * @param[in] fp file stream where inode metadata is sent
1662 * @param[in] judgeFun filter function pointer. if not NULL,
1663 * only entries for which a non-zero value
1664 * is returned are written to fp
1665 * @param[in] singleVolumeNumber volume id filter. if nonzero, only
1666 * process files for that specific volume id
1667 * @param[in] rock opaque pointer passed into writeFun and
1670 * @return operation status
1672 * @retval >=0 number of matching files found
1675 namei_ListAFSFiles(char *dev,
1676 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1679 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1680 afs_uint32 singleVolumeNumber, void *rock)
1687 #ifndef AFS_NT40_ENV
1693 static void FreeZLCList(void);
1696 memset((void *)&ih, 0, sizeof(IHandle_t));
1698 ih.ih_dev = nt_DriveToDev(dev);
1700 ih.ih_dev = volutil_GetPartitionID(dev);
1703 if (singleVolumeNumber) {
1704 ih.ih_vid = singleVolumeNumber;
1705 namei_HandleToVolDir(&name, &ih);
1707 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1708 singleVolumeNumber, rock);
1712 /* Find all volume data directories and descend through them. */
1713 namei_HandleToInodeDir(&name, &ih);
1715 dirp1 = opendir(name.n_path);
1718 while ((dp1 = readdir(dirp1))) {
1720 /* Heirarchy is one level on Windows */
1721 if (!DecodeVolumeName(dp1->d_name, &ih.ih_vid)) {
1723 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1727 if (*dp1->d_name == '.')
1729 snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", name.n_path,
1731 dirp2 = opendir(path2);
1733 while ((dp2 = readdir(dirp2))) {
1734 if (*dp2->d_name == '.')
1736 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1738 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1755 static void AddToZLCDeleteList(char dir, char *name);
1756 static void DeleteZLCFiles(char *path);
1760 * examine a namei volume special file.
1762 * @param[in] path1 volume special directory path
1763 * @param[in] dname directory entry name
1764 * @param[in] myIH inode handle to volume directory
1765 * @param[out] linkHandle namei link count fd handle. if
1766 * the inode in question is the link
1767 * table, then the FdHandle is populated
1768 * @param[in] writeFun metadata write function pointer
1769 * @param[in] fp file pointer where inode metadata
1770 * is written by (*writeFun)()
1771 * @param[in] judgeFun inode filter function pointer. if
1772 * not NULL, only inodes for which this
1773 * function returns non-zero are recorded
1774 * into fp by writeFun
1775 * @param[in] singleVolumeNumer volume id filter. if non-zero, only
1776 * inodes associated with this volume id
1777 * are recorded by writeFun
1778 * @param[in] rock opaque pointer passed to writeFun and
1781 * @return operation status
1782 * @retval 1 count this inode
1783 * @retval 0 don't count this inode
1784 * @retval -1 failure
1789 _namei_examine_special(char * path1,
1792 FdHandle_t * linkHandle,
1793 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1796 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1797 int singleVolumeNumber,
1801 struct ViceInodeInfo info;
1802 afs_uint32 inode_vgid;
1804 if (DecodeInode(path1, dname, &info, myIH->ih_vid) < 0) {
1810 inode_vgid = myIH->ih_vid;
1812 inode_vgid = (info.inodeNumber >> NAMEI_UNIQSHIFT) & NAMEI_UNIQMASK;
1815 if (info.u.param[2] != VI_LINKTABLE) {
1817 } else if ((info.u.param[0] != myIH->ih_vid) ||
1818 (inode_vgid != myIH->ih_vid)) {
1819 /* VGID encoded in linktable filename and/or OGM data isn't
1820 * consistent with VGID encoded in namei path */
1821 Log("namei_ListAFSSubDirs: warning: inconsistent linktable "
1822 "filename \"%s" OS_DIRSEP "%s\"; salvager will delete it "
1823 "(dir_vgid=%u, inode_vgid=%u, ogm_vgid=%u)\n",
1824 path1, dname, myIH->ih_vid,
1825 (unsigned int)inode_vgid,
1829 /* Open this handle */
1830 snprintf(path2, sizeof(path2),
1831 "%s" OS_DIRSEP "%s", path1, dname);
1832 linkHandle->fd_fd = OS_OPEN(path2, Testing ? O_RDONLY : O_RDWR, 0666);
1834 namei_GetLinkCount(linkHandle, (Inode) 0, 1, 1, Testing);
1838 (*judgeFun) (&info, singleVolumeNumber, rock)) {
1839 ret = (*writeFun) (fp, &info, path1, dname);
1841 Log("_namei_examine_special: writeFun returned %d\n", ret);
1853 * examine a namei file.
1855 * @param[in] path3 volume special directory path
1856 * @param[in] dname directory entry name
1857 * @param[in] myIH inode handle to volume directory
1858 * @param[in] linkHandle namei link count fd handle.
1859 * @param[in] writeFun metadata write function pointer
1860 * @param[in] fp file pointer where inode metadata
1861 * is written by (*writeFun)()
1862 * @param[in] judgeFun inode filter function pointer. if
1863 * not NULL, only inodes for which this
1864 * function returns non-zero are recorded
1865 * into fp by writeFun
1866 * @param[in] singleVolumeNumer volume id filter. if non-zero, only
1867 * inodes associated with this volume id
1868 * are recorded by writeFun
1869 * @param[in] rock opaque pointer passed to writeFun and
1872 * @return operation status
1873 * @retval 1 count this inode
1874 * @retval 0 don't count this inode
1875 * @retval -1 failure
1876 * @retval -2 request ZLC delete
1881 _namei_examine_reg(char * path3,
1884 FdHandle_t * linkHandle,
1885 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1888 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1889 int singleVolumeNumber,
1893 struct ViceInodeInfo info;
1895 int dirl; /* Windows-only (one level hash dir) */
1898 if (DecodeInode(path3, dname, &info, myIH->ih_vid) < 0) {
1903 namei_GetLinkCount(linkHandle,
1904 info.inodeNumber, 1, 1, Testing);
1905 if (info.linkCount == 0) {
1907 Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n", path3, dname);
1908 #ifdef AFS_SALSRV_ENV
1909 /* defer -- the AddToZLCDeleteList() interface is not MT-safe */
1911 #else /* !AFS_SALSRV_ENV */
1912 dirl = path3[strlen(path3)-1];
1913 AddToZLCDeleteList((char)dirl, dname);
1914 #endif /* !AFS_SALSRV_ENV */
1915 #else /* !DELETE_ZLC */
1916 Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path3,
1923 (*judgeFun) (&info, singleVolumeNumber, rock)) {
1924 ret = (*writeFun) (fp, &info, path3, dname);
1926 Log("_namei_examine_reg: writeFun returned %d\n", ret);
1938 * listsubdirs work queue node.
1940 struct listsubdirs_work_node {
1941 #ifdef AFS_SALSRV_ENV
1942 int *error; /**< *error set if an error was
1943 * encountered in any listsubdirs
1947 IHandle_t * IH; /**< volume directory handle */
1948 FdHandle_t *linkHandle; /**< namei link count fd handle. when
1949 * examinining the link table special
1950 * inode, this will be pointed at the
1953 FD_t fp; /**< file pointer for writeFun */
1955 /** function which will write inode metadata to fp */
1956 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *, char *);
1958 /** inode filter function */
1959 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *);
1960 int singleVolumeNumber; /**< volume id filter */
1961 void * rock; /**< pointer passed to writeFun and judgeFun */
1962 int code; /**< return code from examine function */
1963 int special; /**< asserted when this is a volume
1968 * simple wrapper around _namei_examine_special and _namei_examine_reg.
1970 * @param[in] work the struct listsubdirs_work_node for the associated
1971 * "list subdirs" job
1972 * @param[in] dir the directory to examine
1973 * @param[in] filename the filename in 'dir' to examine
1975 * @return operation status
1976 * @retval 1 count this inode
1977 * @retval 0 don't count this inode
1978 * @retval -1 failure
1981 _namei_examine_file(const struct listsubdirs_work_node *work, char *dir,
1984 if (work->special) {
1985 return _namei_examine_special(dir, filename, work->IH,
1986 work->linkHandle, work->writeFun, work->fp,
1987 work->judgeFun, work->singleVolumeNumber,
1990 return _namei_examine_reg(dir, filename, work->IH,
1991 work->linkHandle, work->writeFun, work->fp,
1992 work->judgeFun, work->singleVolumeNumber,
1998 #ifdef AFS_SALSRV_ENV
1999 /** @addtogroup afs_vol_salsrv_pario */
2003 * arguments for the _namei_examine_file_cbk callback function.
2005 struct listsubdirs_args {
2006 const struct listsubdirs_work_node *work; /**< arguments that are the same
2007 * for all invocations of
2008 * _namei_examine_file_cbk, in
2010 int *result; /**< where we can store the return code of _namei_examine_file */
2012 char dir[512]; /**< directory to examine */
2013 char filename[256]; /**< filename in 'dir' to examine */
2017 * a node in the list of results of listsubdir jobs.
2019 struct listsubdirs_result {
2021 int inodes; /**< return value from _namei_examine_file */
2025 * clean up a list of 'struct listsubdirs_result's and interpret the results.
2027 * @param[in] resultlist a list of 'struct listsubdirs_result's
2029 * @return number of inodes found
2033 _namei_listsubdirs_cleanup_results(struct rx_queue *resultlist)
2035 struct listsubdirs_result *res, *nres;
2038 for(queue_Scan(resultlist, res, nres, listsubdirs_result)) {
2040 /* noop, retain erroneous error code */
2041 } else if (res->inodes < 0) {
2056 * wait for the spawned listsubdirs jobs to finish, and return how many inodes
2059 * @param[in] queue queue to wait to finish
2060 * @param[in] resultlist list of 'struct listsubdirs_result's that the queued
2061 * jobs are storing their results in
2063 * @return number of inodes found
2067 _namei_listsubdirs_wait(struct afs_work_queue *queue, struct rx_queue *resultlist)
2071 code = afs_wq_wait_all(queue);
2076 return _namei_listsubdirs_cleanup_results(resultlist);
2080 * work queue entry point for examining namei files.
2082 * @param[in] queue pointer to struct Vwork_queue
2083 * @param[in] node pointer to struct Vwork_queue_node
2084 * @param[in] queue_rock opaque pointer to struct salsrv_pool_state
2085 * @param[in] node_rock opaque pointer to struct listsubdirs_work_node
2086 * @param[in] caller_rock opaque pointer to struct salsrv_worker_thread_state
2088 * @return operation status
2090 * @see Vwork_queue_callback_func_t
2095 _namei_examine_file_cbk(struct afs_work_queue *queue,
2096 struct afs_work_queue_node *node,
2102 struct listsubdirs_args *args = node_rock;
2103 const struct listsubdirs_work_node * work = args->work;
2104 char *dir = args->dir;
2105 char *filename = args->filename;
2107 code = _namei_examine_file(work, dir, filename);
2109 *(args->result) = code;
2113 /* we've errored, so no point in letting more jobs continue */
2114 afs_wq_shutdown(queue);
2120 static pthread_once_t wq_once = PTHREAD_ONCE_INIT;
2121 static pthread_key_t wq_key;
2124 * create the wq_key key for storing a work queue.
2127 _namei_wq_keycreate(void)
2129 osi_Assert(pthread_key_create(&wq_key, NULL) == 0);
2133 * set the work queue for this thread to use for backgrounding namei ops.
2135 * The work queue will be used in ListAFSSubdirs (called indirectly by
2136 * ListViceInodes) to examine files in parallel.
2138 * @param[in] wq the work queue to use
2141 namei_SetWorkQueue(struct afs_work_queue *wq)
2143 osi_Assert(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
2145 osi_Assert(pthread_setspecific(wq_key, wq) == 0);
2149 * enqueue an examine file work unit.
2151 * @param[in] work the _namei_examine_file arguments that are common to
2152 * all callers within the same ListAFSFiles operation
2153 * @param[in] dir the specific directory to look at (string will be
2154 * copied; can be stack/temporary memory)
2155 * @param[in] filename the filename to look at (string will be copied; can be
2156 * stack/temporary memory)
2157 * @param[in] wq work queue to enqueue this work unit to
2158 * @param[in] resultlist the list to append the 'struct listsubdirs_result' to
2159 * for the enqueued work unit
2161 * @return operation status
2163 * @retval -1 fatal error
2165 * @note errors MUST be indicated by a -1 error code and nothing else, to be
2166 * compatible with _namei_examine_reg and _namei_examine_special
2171 _namei_examine_file_spawn(const struct listsubdirs_work_node *work,
2172 const char *dir, const char *filename,
2173 struct afs_work_queue *wq,
2174 struct rx_queue *resultlist)
2177 struct listsubdirs_args *args = NULL;
2178 struct listsubdirs_result *result = NULL;
2179 struct afs_work_queue_node *node = NULL;
2180 struct afs_work_queue_add_opts opts;
2182 args = malloc(sizeof(*args));
2188 result = malloc(sizeof(*result));
2189 if (result == NULL) {
2194 code = afs_wq_node_alloc(&node);
2199 code = afs_wq_node_set_detached(node);
2206 args->result = &result->inodes;
2207 strlcpy(args->dir, dir, sizeof(args->dir));
2208 strlcpy(args->filename, filename, sizeof(args->filename));
2210 code = afs_wq_node_set_callback(node,
2211 &_namei_examine_file_cbk,
2219 afs_wq_add_opts_init(&opts);
2222 code = afs_wq_add(wq, node, &opts);
2229 queue_Append(resultlist, result);
2234 afs_wq_node_put(node);
2250 #else /* !AFS_SALSRV_ENV */
2251 # define _namei_examine_file_spawn(work, dir, file, wq, resultlist) \
2252 _namei_examine_file(work, dir, file)
2253 #endif /* !AFS_SALSRV_ENV */
2256 * traverse and check inodes.
2258 * @param[in] dirIH volume group directory handle
2259 * @param[in] writeFun function pointer which will write inode
2260 * metadata to FILE stream fp
2261 * @param[in] fp file stream where inode metadata gets
2263 * @param[in] judgeFun inode filter function. if not NULL, only
2264 * inodes for which the filter returns non-zero
2265 * will be written out by writeFun
2266 * @param[in] singleVolumeNumber volume id filter. only inodes matching this
2267 * filter are written out by writeFun
2268 * @param[in] rock opaque pointer passed to judgeFun and writeFun
2270 * @return operation status
2272 * @retval >=0 number of matching inodes found
2274 * @todo the salsrv implementation may consume a lot of
2275 * memory for a large volume. at some point we should
2276 * probably write a background thread to asynchronously
2277 * clean up the resultlist nodes to reduce memory footprint
2282 namei_ListAFSSubDirs(IHandle_t * dirIH,
2283 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
2286 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
2287 afs_uint32 singleVolumeNumber, void *rock)
2289 int code = 0, ret = 0;
2290 IHandle_t myIH = *dirIH;
2292 char path1[512], path3[512];
2294 #ifndef AFS_NT40_ENV
2299 struct dirent *dp1, *dp3;
2300 FdHandle_t linkHandle;
2302 struct listsubdirs_work_node work;
2303 #ifdef AFS_SALSRV_ENV
2305 struct afs_work_queue *wq;
2307 struct rx_queue resultlist;
2310 namei_HandleToVolDir(&name, &myIH);
2311 strlcpy(path1, name.n_path, sizeof(path1));
2313 /* Do the directory containing the special files first to pick up link
2316 (void)strcat(path1, OS_DIRSEP);
2317 (void)strcat(path1, NAMEI_SPECDIR);
2319 linkHandle.fd_fd = INVALID_FD;
2320 #ifdef AFS_SALSRV_ENV
2321 osi_Assert(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
2323 wq = pthread_getspecific(wq_key);
2329 queue_Init(&resultlist);
2332 memset(&work, 0, sizeof(work));
2333 work.linkHandle = &linkHandle;
2336 work.writeFun = writeFun;
2337 work.judgeFun = judgeFun;
2338 work.singleVolumeNumber = singleVolumeNumber;
2341 #ifdef AFS_SALSRV_ENV
2342 work.error = &error;
2345 dirp1 = opendir(path1);
2347 while ((dp1 = readdir(dirp1))) {
2348 if (*dp1->d_name == '.')
2351 #ifdef AFS_SALSRV_ENV
2357 #endif /* AFS_SALSRV_ENV */
2359 code = _namei_examine_file_spawn(&work, path1, dp1->d_name, wq, &resultlist);
2369 /* count this inode */
2370 #ifndef AFS_SALSRV_ENV
2379 #ifdef AFS_SALSRV_ENV
2380 /* effectively a barrier */
2381 code = _namei_listsubdirs_wait(wq, &resultlist);
2382 if (code < 0 || error) {
2390 if (linkHandle.fd_fd == INVALID_FD) {
2391 Log("namei_ListAFSSubDirs: warning: VG %u does not have a link table; "
2392 "salvager will recreate it.\n", dirIH->ih_vid);
2395 /* Now run through all the other subdirs */
2396 namei_HandleToVolDir(&name, &myIH);
2397 strlcpy(path1, name.n_path, sizeof(path1));
2401 dirp1 = opendir(path1);
2403 while ((dp1 = readdir(dirp1))) {
2404 #ifndef AFS_NT40_ENV
2405 if (*dp1->d_name == '.')
2408 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
2411 #ifndef AFS_NT40_ENV /* This level missing on Windows */
2412 /* Now we've got a next level subdir. */
2413 snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s",
2414 path1, dp1->d_name);
2415 dirp2 = opendir(path2);
2417 while ((dp2 = readdir(dirp2))) {
2418 if (*dp2->d_name == '.')
2421 /* Now we've got to the actual data */
2422 snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
2423 path2, dp2->d_name);
2425 /* Now we've got to the actual data */
2426 snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
2427 path1, dp1->d_name);
2429 dirp3 = opendir(path3);
2431 while ((dp3 = readdir(dirp3))) {
2432 #ifndef AFS_NT40_ENV
2433 if (*dp3->d_name == '.')
2437 #ifdef AFS_SALSRV_ENV
2440 #ifndef AFS_NT40_ENV
2447 #endif /* AFS_SALSRV_ENV */
2449 code = _namei_examine_file_spawn(&work, path3,
2456 #ifndef AFS_NT40_ENV
2464 #ifndef AFS_SALSRV_ENV
2472 #ifndef AFS_NT40_ENV /* This level missing on Windows */
2481 #ifdef AFS_SALSRV_ENV
2482 /* effectively a barrier */
2483 code = _namei_listsubdirs_wait(wq, &resultlist);
2484 if (code < 0 || error) {
2494 /* Then why does this directory exist? Blow it away. */
2495 namei_HandleToVolDir(&name, dirIH);
2496 namei_RemoveDataDirectories(&name);
2500 #ifdef AFS_SALSRV_ENV
2502 afs_wq_wait_all(wq);
2504 _namei_listsubdirs_cleanup_results(&resultlist);
2506 if (linkHandle.fd_fd != INVALID_FD)
2507 OS_CLOSE(linkHandle.fd_fd);
2519 DecodeVolumeName(char *name, unsigned int *vid)
2521 /* Name begins with "Vol_" and ends with .data. See nt_HandleToVolDir() */
2528 if (strncmp(name, "Vol_", 4))
2530 if (strcmp(name + len - 5, ".data"))
2533 stmp[len - 5] = '\0';
2534 *vid = base32_to_int(stmp + 4);
2539 DecodeVolumeName(char *name, unsigned int *vid)
2541 if (strlen(name) < 1)
2543 *vid = (unsigned int)flipbase64_to_int64(name);
2551 * Get the inode number from the name.
2556 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
2561 WIN32_FIND_DATA data;
2565 FdHandle_t linkHandle;
2568 snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
2570 dirH = FindFirstFileEx(fpath, FindExInfoStandard, &data,
2571 FindExSearchNameMatch, NULL,
2572 FIND_FIRST_EX_CASE_SENSITIVE);
2573 if (dirH == INVALID_HANDLE_VALUE)
2576 (void)strcpy(stmp, name);
2577 s = strrchr(stmp, '_');
2581 t = strrchr(s, '.');
2586 vno = base32_to_int(s); /* type for special files */
2587 tag = base32_to_int(t+1);
2588 info->inodeNumber = ((Inode) tag) << NAMEI_TAGSHIFT;
2589 info->inodeNumber |= vno;
2590 info->byteCount = data.nFileSizeLow;
2592 dirl = dpath[strlen(dpath)-1];
2593 if (dirl == NAMEI_SPECDIRC) { /* Special inode. */
2594 info->inodeNumber |= NAMEI_INODESPECIAL;
2595 info->u.param[0] = data.ftCreationTime.dwHighDateTime;
2596 info->u.param[1] = data.ftCreationTime.dwLowDateTime;
2597 info->u.param[2] = vno; /* type */
2598 info->u.param[3] = volid;
2599 if (vno != VI_LINKTABLE)
2600 info->linkCount = 1;
2602 /* Open this handle */
2604 (void)sprintf(lpath, "%s" OS_DIRSEP "%s", fpath, data.cFileName);
2605 linkHandle.fd_fd = OS_OPEN(lpath, O_RDONLY, 0666);
2607 namei_GetLinkCount(&linkHandle, (Inode) 0, 0, 0, 0);
2611 namei_GetLinkCount(&linkHandle, info->inodeNumber, 0, 0, 0);
2612 if (info->linkCount == 0) {
2614 Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n",
2615 fpath, data.cFileName);
2616 AddToZLCDeleteList(dirl, data.cFileName);
2618 Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path,
2622 info->u.param[2] = data.ftCreationTime.dwHighDateTime;
2623 info->u.param[3] = data.ftCreationTime.dwLowDateTime;
2624 info->u.param[1] = vno;
2625 info->u.param[0] = volid;
2632 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
2636 struct afs_stat_st status;
2638 lb64_string_t check;
2640 snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
2642 if (afs_stat(fpath, &status) < 0) {
2646 info->byteCount = status.st_size;
2647 info->inodeNumber = (Inode) flipbase64_to_int64(name);
2649 int64_to_flipbase64(check, info->inodeNumber);
2650 if (strcmp(name, check))
2653 GetOGMFromStat(&status, &parm, &tag);
2654 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
2655 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
2656 info->u.param[0] = parm;
2657 info->u.param[1] = -1;
2658 info->u.param[2] = tag;
2659 info->u.param[3] = volid;
2661 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
2662 info->u.param[0] = volid;
2663 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
2664 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
2665 & (Inode) NAMEI_UNIQMASK);
2666 info->u.param[3] = parm;
2673 * Convert the VolumeInfo file from RO to RW
2674 * this routine is called by namei_convertROtoRWvolume()
2677 #ifdef FSSYNC_BUILD_CLIENT
2679 convertVolumeInfo(FD_t fdr, FD_t fdw, afs_uint32 vid)
2681 struct VolumeDiskData vd;
2684 if (OS_READ(fdr, &vd, sizeof(struct VolumeDiskData)) !=
2685 sizeof(struct VolumeDiskData)) {
2686 Log("1 convertVolumeInfo: read failed for %lu with code %d\n",
2687 afs_printable_uint32_lu(vid),
2691 vd.restoredFromId = vd.id; /* remember the RO volume here */
2693 vd.id = vd.parentId;
2697 vd.uniquifier += 5000; /* just in case there are still file copies from
2698 * the old RW volume around */
2700 /* For ROs, the copyDate contains the time that the RO volume was actually
2701 * created, and the creationDate just contains the last time the RO was
2702 * copied from the RW data. So, make the new RW creationDate more accurate
2703 * by setting it to copyDate, if copyDate is older. Since, we know the
2704 * volume is at least as old as copyDate. */
2705 if (vd.copyDate < vd.creationDate) {
2706 vd.creationDate = vd.copyDate;
2708 /* If copyDate is newer, just make copyDate and creationDate the same,
2709 * for consistency with other RWs */
2710 vd.copyDate = vd.creationDate;
2713 p = strrchr(vd.name, '.');
2714 if (p && !strcmp(p, ".readonly")) {
2717 if (OS_WRITE(fdw, &vd, sizeof(struct VolumeDiskData)) !=
2718 sizeof(struct VolumeDiskData)) {
2719 Log("1 convertVolumeInfo: write failed for %lu with code %d\n",
2720 afs_printable_uint32_lu(vid),
2729 * Convert a RO-volume into a RW-volume
2731 * This function allows to recover very fast from the loss of a partition
2732 * from RO-copies if all RO-Copies exist on another partition.
2733 * Then these RO-volumes can be made to the new RW-volumes.
2734 * Backup of RW-volumes then consists in "vos release".
2736 * We must make sure in this partition exists only the RO-volume which
2737 * is typical for remote replicas.
2739 * Then the linktable is already ok,
2740 * the vnode files need to be renamed
2741 * the volinfo file needs to be replaced by another one with
2742 * slightly different contents and new name.
2743 * The volume header file of the RO-volume in the /vicep<x> directory
2744 * is destroyed by this call. A new header file for the RW-volume must
2745 * be created after return from this routine.
2749 namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
2752 #ifdef FSSYNC_BUILD_CLIENT
2754 char dir_name[512], oldpath[512], newpath[512];
2769 struct DiskPartition64 *partP;
2770 struct ViceInodeInfo info;
2771 struct VolumeDiskHeader h;
2772 # ifdef AFS_DEMAND_ATTACH_FS
2774 # endif /* AFS_DEMAND_ATTACH_FS */
2776 for (partP = DiskPartitionList; partP && strcmp(partP->name, pname);
2777 partP = partP->next);
2779 Log("1 namei_ConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
2784 # ifdef AFS_DEMAND_ATTACH_FS
2785 locktype = VVolLockType(V_VOLUPD, 1);
2786 code = VLockVolumeByIdNB(volumeId, partP, locktype);
2792 # endif /* AFS_DEMAND_ATTACH_FS */
2794 if (VReadVolumeDiskHeader(volumeId, partP, &h)) {
2795 Log("1 namei_ConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n",
2796 afs_printable_uint32_lu(volumeId));
2801 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
2803 ino = namei_MakeSpecIno(h.parent, VI_LINKTABLE);
2804 IH_INIT(ih, partP->device, h.parent, ino);
2806 namei_HandleToName(&n, ih);
2807 strlcpy(dir_name, n.n_path, sizeof(dir_name));
2808 p = strrchr(dir_name, OS_DIRSEPC);
2810 dirp = opendir(dir_name);
2812 Log("1 namei_ConvertROtoRWvolume: Could not opendir(%s)\n", dir_name);
2817 while ((dp = readdir(dirp))) {
2818 /* struct ViceInodeInfo info; */
2819 #ifndef AFS_NT40_ENV
2820 if (*dp->d_name == '.')
2823 if (DecodeInode(dir_name, dp->d_name, &info, ih->ih_vid) < 0) {
2824 Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s" OS_DIRSEP "%s\n",
2825 dir_name, dp->d_name);
2830 if (info.u.param[1] != -1) {
2831 Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s" OS_DIRSEP "%s\n", dir_name, dp->d_name);
2836 if (info.u.param[0] != volumeId) {
2837 if (info.u.param[0] == ih->ih_vid) {
2838 if (info.u.param[2] == VI_LINKTABLE) { /* link table */
2843 Log("1 namei_ConvertROtoRWvolume: found special file %s" OS_DIRSEP "%s"
2844 " for volume %lu\n", dir_name, dp->d_name,
2845 afs_printable_uint32_lu(info.u.param[0]));
2850 if (info.u.param[2] == VI_VOLINFO) { /* volume info file */
2851 strlcpy(infoName, dp->d_name, sizeof(infoName));
2853 } else if (info.u.param[2] == VI_SMALLINDEX) { /* small vnodes file */
2854 strlcpy(smallName, dp->d_name, sizeof(smallName));
2856 } else if (info.u.param[2] == VI_LARGEINDEX) { /* large vnodes file */
2857 strlcpy(largeName, dp->d_name, sizeof(largeName));
2861 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);
2868 if (!infoSeen || !smallSeen || !largeSeen || !linkSeen) {
2869 Log("1 namei_ConvertROtoRWvolume: not all special files found in %s\n", dir_name);
2875 * If we come here then there was only a RO-volume and we can safely
2879 memset(&t_ih, 0, sizeof(t_ih));
2880 t_ih.ih_dev = ih->ih_dev;
2881 t_ih.ih_vid = ih->ih_vid;
2883 snprintf(oldpath, sizeof oldpath, "%s" OS_DIRSEP "%s", dir_name,
2885 fd = OS_OPEN(oldpath, O_RDWR, 0);
2886 if (fd == INVALID_FD) {
2887 Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
2892 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_VOLINFO);
2893 namei_HandleToName(&n, &t_ih);
2894 fd2 = OS_OPEN(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
2895 if (fd2 == INVALID_FD) {
2896 Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
2901 code = convertVolumeInfo(fd, fd2, ih->ih_vid);
2905 OS_UNLINK(n.n_path);
2909 SetOGM(fd2, ih->ih_vid, 1);
2912 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_SMALLINDEX);
2913 namei_HandleToName(&n, &t_ih);
2914 snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
2916 fd = OS_OPEN(newpath, O_RDWR, 0);
2917 if (fd == INVALID_FD) {
2918 Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
2922 SetOGM(fd, ih->ih_vid, 2);
2925 MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
2927 link(newpath, n.n_path);
2931 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_LARGEINDEX);
2932 namei_HandleToName(&n, &t_ih);
2933 snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
2935 fd = OS_OPEN(newpath, O_RDWR, 0);
2936 if (fd == INVALID_FD) {
2937 Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
2941 SetOGM(fd, ih->ih_vid, 3);
2944 MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
2946 link(newpath, n.n_path);
2953 h.volumeInfo_hi = h.id;
2954 h.smallVnodeIndex_hi = h.id;
2955 h.largeVnodeIndex_hi = h.id;
2956 h.linkTable_hi = h.id;
2958 if (VCreateVolumeDiskHeader(&h, partP)) {
2959 Log("1 namei_ConvertROtoRWvolume: Couldn't write header for RW-volume %lu\n",
2960 afs_printable_uint32_lu(h.id));
2965 if (VDestroyVolumeDiskHeader(partP, volumeId, h.parent)) {
2966 Log("1 namei_ConvertROtoRWvolume: Couldn't unlink header for RO-volume %lu\n",
2967 afs_printable_uint32_lu(volumeId));
2970 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
2971 FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
2974 # ifdef AFS_DEMAND_ATTACH_FS
2976 VUnlockVolumeById(volumeId, partP);
2978 # endif /* AFS_DEMAND_ATTACH_FS */
2986 * returns a static string used to print either 32 or 64 bit inode numbers.
2989 PrintInode(char *s, Inode ino)
2991 static afs_ino_str_t result;
2995 snprintf(s, sizeof(afs_ino_str_t), "%llu", (afs_uintmax_t) ino);
3002 /* Routines to facilitate removing zero link count files. */
3003 #define MAX_ZLC_NAMES 32
3004 #define MAX_ZLC_NAMELEN 16
3005 typedef struct zlcList_s {
3006 struct zlcList_s *zlc_next;
3008 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
3011 static zlcList_t *zlcAnchor = NULL;
3012 static zlcList_t *zlcCur = NULL;
3015 AddToZLCDeleteList(char dir, char *name)
3017 osi_Assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
3019 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
3020 if (zlcCur && zlcCur->zlc_next)
3021 zlcCur = zlcCur->zlc_next;
3023 zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
3029 zlcCur->zlc_next = tmp;
3033 zlcCur->zlc_next = NULL;
3038 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c" OS_DIRSEP "%s", dir, name);
3040 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%s", name);
3046 DeleteZLCFiles(char *path)
3052 for (z = zlcAnchor; z; z = z->zlc_next) {
3053 for (i = 0; i < z->zlc_n; i++) {
3055 (void)sprintf(fname, "%s" OS_DIRSEP "%s", path, z->zlc_names[i]);
3057 (void)sprintf(fname, "%s", z->zlc_names[i]);
3058 if (namei_unlink(fname) < 0) {
3059 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
3063 z->zlc_n = 0; /* Can reuse space. */
3076 tnext = i->zlc_next;
3080 zlcCur = zlcAnchor = NULL;
3084 #endif /* AFS_NAMEI_ENV */