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>
29 #include <sys/types.h>
39 #include <sys/param.h>
42 #include <afs/afs_assert.h>
45 #include <afs/afsutil.h>
48 #include <afs/afsint.h>
52 #include "viceinode.h"
54 #include "partition.h"
56 #include "volume_inline.h"
58 #include <afs/errors.h>
60 #include <afs/errmap_nt.h>
64 #define LOCK_SH 1 /* shared lock */
65 #define LOCK_EX 2 /* exclusive lock */
66 #define LOCK_NB 4 /* don't block when locking */
67 #define LOCK_UN 8 /* unlock */
72 #include <afs/work_queue.h>
73 #include <afs/thread_pool.h>
74 #include <vol/vol-salvage.h>
77 #if !defined(HAVE_FLOCK) && !defined(AFS_NT40_ENV)
81 * This function emulates a subset of flock()
84 emul_flock(int fd, int cmd)
87 memset(&f, 0, sizeof (f));
96 return fcntl(fd, (cmd & LOCK_NB) ? F_SETLK : F_SETLKW, &f);
99 #define flock(f,c) emul_flock(f,c)
106 namei_iread(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
115 nBytes = FDH_PREAD(fdP, buf, size, offset);
121 namei_iwrite(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
130 nBytes = FDH_PWRITE(fdP, buf, size, offset);
136 /* Inode number format:
137 * low 32 bits - if a regular file or directory, the vnode; else the type.
138 * 32-36 - uniquifier tag and index into counts array for this vnode. Only
139 * two of the available bits are currently used. The rest are
140 * present in case we ever increase the number of types of volumes
141 * in the volume group.
142 * bit 37 : 1 == special, 0 == regular
144 # define NAMEI_VNODEMASK 0x00ffffffff
145 # define NAMEI_TAGSHIFT 32
146 # define NAMEI_INODESPECIAL 0x2000000000
147 # define NAMEI_SPECDIR "R"
148 # define NAMEI_SPECDIRC 'R'
149 #else /* !AFS_NT40_ENV */
150 /* Inode number format:
151 * low 26 bits - vnode number - all 1's if volume special file.
153 * next 3 bits spare (0's)
154 * high 32 bits - uniquifier (regular) or type if spare
156 # define NAMEI_VNODEMASK 0x003ffffff
157 # define NAMEI_TAGSHIFT 26
158 # define NAMEI_UNIQMASK 0xffffffff
159 # define NAMEI_UNIQSHIFT 32
160 # define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
161 /* dir1 is the high 8 bits of the 26 bit vnode */
162 # define VNO_DIR1(vno) ((vno >> 14) & 0xff)
163 /* dir2 is the next 9 bits */
164 # define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
165 /* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
166 # define NAMEI_SPECDIR "special"
167 #endif /* !AFS_NT40_ENV */
168 #define NAMEI_TAGMASK 0x7
169 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
171 #define NAMEI_SPECDIRLEN (sizeof(NAMEI_SPECDIR)-1)
173 #define NAMEI_MAXVOLS 5 /* Maximum supported number of volumes per volume
174 * group, not counting temporary (move) volumes.
175 * This is the number of separate files, all having
176 * the same vnode number, which can occur in a volume
188 static int GetFreeTag(IHandle_t * ih, int vno);
190 /* namei_HandleToInodeDir
192 * Construct the path name of the directory holding the inode data.
193 * Format: /<vicepx>/INODEDIR
198 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
200 memset(name, '\0', sizeof(*name));
201 nt_DevToDrive(name->n_drive, ih->ih_dev);
202 strlcpy(name->n_path, name->n_drive, sizeof(name->n_path));
206 /* Format: /<vicepx>/INODEDIR */
207 #define PNAME_BLEN 64
209 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
213 memset(name, '\0', sizeof(*name));
216 * Add the /vicepXX string to the start of name->n_base and then calculate
217 * offset as the number of bytes we know we added.
219 * FIXME: This embeds knowledge of the vice partition naming scheme and
220 * mapping from device numbers. There needs to be an API that tells us
223 volutil_PartitionName_r(ih->ih_dev, name->n_base, sizeof(name->n_base));
224 offset = VICE_PREFIX_SIZE + (ih->ih_dev > 25 ? 2 : 1);
225 name->n_base[offset] = OS_DIRSEPC;
227 strlcpy(name->n_base + offset, INODEDIR, sizeof(name->n_base) - offset);
228 strlcpy(name->n_path, name->n_base, sizeof(name->n_path));
232 #define addtoname(N, C) \
234 if ((N)->n_path[strlen((N)->n_path)-1] != OS_DIRSEPC) \
235 strlcat((N)->n_path, OS_DIRSEP, sizeof((N)->n_path)); \
236 strlcat((N)->n_path, (C), sizeof((N)->n_path)); \
242 namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
244 /* X:\Vol_XXXXXXX.data */
248 namei_HandleToInodeDir(name, ih);
249 /* nt_drive added to name by namei_HandleToInodeDir() */
250 namep = name->n_voldir;
251 (void)memcpy(namep, "\\Vol_", 5);
253 (void)strcpy(namep, int_to_base32(str1, ih->ih_vid));
254 namep += strlen(namep);
255 memcpy(namep, ".data", 5);
258 addtoname(name, name->n_voldir);
262 namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
266 namei_HandleToInodeDir(name, ih);
267 (void)int32_to_flipbase64(tmp, (int64_t) (ih->ih_vid & 0xff));
268 strlcpy(name->n_voldir1, tmp, sizeof(name->n_voldir1));
269 addtoname(name, name->n_voldir1);
270 (void)int32_to_flipbase64(tmp, (int64_t) ih->ih_vid);
271 strlcpy(name->n_voldir2, tmp, sizeof(name->n_voldir2));
272 addtoname(name, name->n_voldir2);
276 /* namei_HandleToName
278 * Constructs a file name for the fully qualified handle.
281 /* Note that special files end up in X:\Vol_XXXXXXX.data\R */
283 namei_HandleToName(namei_t * name, IHandle_t * ih)
285 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
286 int special = (ih->ih_ino & NAMEI_INODESPECIAL)?1:0;
287 int tag = (int)((ih->ih_ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
290 namei_HandleToVolDir(name, ih);
293 name->n_dir[0] = NAMEI_SPECDIRC;
296 name->n_dir[0] = 'Q';
298 name->n_dir[0] = ((vno & 0x1f) >> 1) + 'A';
301 name->n_dir[1] = '\0';
302 addtoname(name, name->n_dir);
303 /* X:\Vol_XXXXXXX.data\X\V_XXXXXXX.XXX */
304 namep = name->n_inode;
305 (void)memcpy(namep, "\\V_", 3);
307 (void)strcpy(namep, int_to_base32(str1, vno));
308 namep += strlen(namep);
310 (void)strcpy(namep, int_to_base32(str1, tag));
311 namep += strlen(namep);
312 addtoname(name, name->n_inode);
315 /* Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special */
317 namei_HandleToName(namei_t * name, IHandle_t * ih)
319 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
322 namei_HandleToVolDir(name, ih);
324 if (vno == NAMEI_VNODESPECIAL) {
325 strlcpy(name->n_dir1, NAMEI_SPECDIR, sizeof(name->n_dir1));
326 addtoname(name, name->n_dir1);
327 name->n_dir2[0] = '\0';
329 (void)int32_to_flipbase64(str, VNO_DIR1(vno));
330 strlcpy(name->n_dir1, str, sizeof(name->n_dir1));
331 addtoname(name, name->n_dir1);
332 (void)int32_to_flipbase64(str, VNO_DIR2(vno));
333 strlcpy(name->n_dir2, str, sizeof(name->n_dir2));
334 addtoname(name, name->n_dir2);
336 (void)int64_to_flipbase64(str, (int64_t) ih->ih_ino);
337 strlcpy(name->n_inode, str, sizeof(name->n_inode));
338 addtoname(name, name->n_inode);
343 /* The following is a warning to tell sys-admins to not muck about in this
346 #define VICE_README "These files and directories are a part of the AFS \
347 namespace. Modifying them\nin any way will result in loss of AFS data,\n\
348 ownership and permissions included.\n"
350 namei_ViceREADME(char *partition)
355 /* Create the inode directory if we're starting for the first time */
356 (void)afs_snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s", partition,
358 mkdir(filename, 0700);
360 (void)afs_snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s" OS_DIRSEP "README",
361 partition, INODEDIR);
362 fd = OS_OPEN(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
363 if (fd != INVALID_FD) {
364 (void)OS_WRITE(fd, VICE_README, strlen(VICE_README));
371 /* namei_CreateDataDirectories
373 * If creating the file failed because of ENOENT or ENOTDIR, try
374 * creating all the directories first.
378 namei_CreateDataDirectories(namei_t * name, int *created)
385 afs_snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
387 if (mkdir(tmp) < 0) {
398 for (i = 'A'; i <= NAMEI_SPECDIRC; i++) {
400 if (mkdir(tmp) < 0 && errno != EEXIST)
406 #define create_dir() \
408 if (mkdir(tmp, 0700)<0) { \
409 if (errno != EEXIST) \
417 #define create_nextdir(A) \
419 strcat(tmp, OS_DIRSEP); strcat(tmp, A); create_dir(); \
423 namei_CreateDataDirectories(namei_t * name, int *created)
429 strlcpy(tmp, name->n_base, sizeof(tmp));
432 create_nextdir(name->n_voldir1);
433 create_nextdir(name->n_voldir2);
434 create_nextdir(name->n_dir1);
435 if (name->n_dir2[0]) {
436 create_nextdir(name->n_dir2);
443 /* delTree(): Deletes an entire tree of directories (no files)
445 * root : Full path to the subtree. Should be big enough for PATH_MAX
446 * tree : the subtree to be deleted is rooted here. Specifies only the
447 * subtree beginning at tree (not the entire path). It should be
448 * a pointer into the "root" buffer.
450 * errp : errno of the first error encountered during the directory cleanup.
451 * *errp should have been initialized to 0.
454 * -1 : If errors were encountered during cleanup and error is set to
458 * If there are errors, we try to work around them and delete as many
459 * directories as possible. We don't attempt to remove directories that still
460 * have non-dir entries in them.
463 delTree(char *root, char *tree, int *errp)
471 /* delete the children first */
472 cp = strchr(tree, OS_DIRSEPC);
474 delTree(root, cp + 1, errp);
477 cp = tree + strlen(tree); /* move cp to the end of string tree */
479 /* now delete all entries in this dir */
480 if ((ds = opendir(root)) != (DIR *) NULL) {
482 while ((dirp = readdir(ds))) {
483 /* ignore . and .. */
484 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
486 /* since root is big enough, we reuse the space to
487 * concatenate the dirname to the current tree
489 strcat(root, OS_DIRSEP);
490 strcat(root, dirp->d_name);
491 if (afs_stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
492 /* delete this subtree */
493 delTree(root, cp + 1, errp);
495 *errp = *errp ? *errp : errno;
497 /* recover path to our cur tree by truncating it to
502 /* if (!errno) -- closedir not implicit if we got an error */
506 /* finally axe the current dir */
508 *errp = *errp ? *errp : errno;
510 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
512 #endif /* !AFS_PTHREAD_ENV */
517 /* if we encountered errors during cleanup, we return a -1 */
526 /* namei_RemoveDataDirectories
528 * Returns 0 on success.
529 * Returns -1 on error. Typically, callers ignore this error because we
530 * can continue running if the removes fail. The salvage process will
531 * finish tidying up for us.
536 namei_RemoveDataDirectories(namei_t * name)
543 afs_snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
546 path += strlen(path);
547 *path++ = OS_DIRSEPC;
549 for (i = 'A'; i <= NAMEI_SPECDIRC; i++) {
551 if (rmdir(name->n_path) < 0 && errno != ENOENT)
556 /* Delete the Vol_NNNNNN.data directory. */
559 if (rmdir(name->n_path) < 0 && errno != ENOENT) {
567 * We only use the n_base and n_voldir1 entries
568 * and only do rmdir's.
571 namei_RemoveDataDirectories(namei_t * name)
575 int prefixlen = strlen(name->n_base), err = 0;
576 int vollen = strlen(name->n_voldir1);
577 char pbuf[MAXPATHLEN];
581 strlcpy(path, name->n_path, sizeof(pbuf));
583 /* move past the prefix and n_voldir1 */
584 path = path + prefixlen + 1 + vollen + 1; /* skip over the trailing / */
586 /* now delete all dirs upto path */
587 code = delTree(pbuf, path, &err);
589 /* We've now deleted everything under /n_base/n_voldir1/n_voldir2 that
590 * we could. Do not delete /n_base/n_voldir1, since doing such might
591 * interrupt another thread trying to create a volume. We could introduce
592 * some locking to make this safe (or only remove it for whole-partition
593 * salvages), but by not deleting it we only leave behind a maximum of
594 * 256 empty directories. So at least for now, don't bother. */
599 /* Create the file in the name space.
601 * Parameters stored as follows:
603 * p1 - volid - implied in containing directory.
604 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
605 * p3 - uniq -- bits 4-0 are in mode bits 4-0
606 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
608 * p1 - volid - creation time - dwHighDateTime
609 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
610 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
611 * p4 - parid - parent volume id - implied in containing directory.
613 * Return value is the inode number or (Inode)-1 if error.
614 * We "know" there is only one link table, so return EEXIST if there already
615 * is a link table. It's up to the calling code to test errno and increment
621 * This function is called by VCreateVolume to hide the implementation
622 * details of the inode numbers. This only allows for 7 volume special
623 * types, but if we get that far, this could should be dead by then.
626 namei_MakeSpecIno(int volid, int type)
629 ino = NAMEI_INODESPECIAL;
632 /* tag is always 0 for special */
634 type &= NAMEI_TAGMASK;
635 ino |= ((Inode) type) << NAMEI_TAGSHIFT;
636 ino |= ((Inode) volid) << NAMEI_UNIQSHIFT;
644 SetOGM(FD_t fd, int parm, int tag)
650 CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
652 WIN32_FIND_DATA info;
656 FindFirstFileEx(name->n_path, FindExInfoStandard, &info,
657 FindExSearchNameMatch, NULL,
658 FIND_FIRST_EX_CASE_SENSITIVE);
661 return -1; /* Can't get info, leave alone */
665 if (info.ftCreationTime.dwHighDateTime != (unsigned int)p1)
670 #else /* AFS_NT40_ENV */
671 /* SetOGM - set owner group and mode bits from parm and tag */
673 SetOGM(FD_t fd, int parm, int tag)
676 * owner - low 15 bits of parm.
677 * group - next 15 bits of parm.
678 * mode - 2 bits of parm, then lowest = 3 bits of tag.
680 int owner, group, mode;
682 owner = parm & 0x7fff;
683 group = (parm >> 15) & 0x7fff;
684 if (fchown(fd, owner, group) < 0)
687 mode = (parm >> 27) & 0x18;
689 if (fchmod(fd, mode) < 0)
694 /* GetOGM - get parm and tag from owner, group and mode bits. */
696 GetOGMFromStat(struct afs_stat_st *status, int *parm, int *tag)
698 *parm = status->st_uid | (status->st_gid << 15);
699 *parm |= (status->st_mode & 0x18) << 27;
700 *tag = status->st_mode & 0x7;
704 CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
706 struct afs_stat_st status;
708 if (afs_fstat(fdP->fd_fd, &status) < 0)
711 GetOGMFromStat(&status, &parm, &tag);
717 #endif /* !AFS_NT40_ENV */
719 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
721 /* Derive the name and create it O_EXCL. If that fails we have an error.
722 * Get the tag from a free column in the link table.
726 namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
729 FD_t fd = INVALID_FD;
740 memset((void *)&tmp, 0, sizeof(IHandle_t));
741 memset(&tfd, 0, sizeof(FdHandle_t));
743 tmp.ih_dev = nt_DriveToDev(part);
744 if (tmp.ih_dev == -1) {
749 if (p2 == INODESPECIAL) {
750 /* Parameters for special file:
751 * p1 - volume id - goes into owner/group/mode
752 * p2 - vnode == INODESPECIAL
754 * p4 - parent volume id
756 ftime.dwHighDateTime = p1;
757 ftime.dwLowDateTime = p2;
759 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
760 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
762 int vno = p2 & NAMEI_VNODEMASK;
763 /* Parameters for regular file:
777 tmp.ih_ino = (Inode) p2;
778 ftime.dwHighDateTime = p3;
779 ftime.dwLowDateTime = p4;
782 namei_HandleToName(&name, &tmp);
783 p = strrchr((char *)&name.n_path, '.');
785 for (tag = 0; tag < NAMEI_MAXVOLS; tag++) {
786 *p = *int_to_base32(str1, tag);
787 fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0666);
788 if (fd == INVALID_FD) {
789 if (errno == ENOTDIR || errno == ENOENT) {
790 if (namei_CreateDataDirectories(&name, &created_dir) == 0)
791 fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0666);
795 if (fd != INVALID_FD)
797 if (p2 == INODESPECIAL && p3 == VI_LINKTABLE)
800 if (fd == INVALID_FD) {
804 tmp.ih_ino &= ~(((Inode) NAMEI_TAGMASK) << NAMEI_TAGSHIFT);
805 tmp.ih_ino |= (((Inode) tag) << NAMEI_TAGSHIFT);
808 if (!SetFileTime((HANDLE) fd, &ftime, NULL, NULL)) {
809 errno = OS_ERROR(EBADF);
815 if (p2 != INODESPECIAL) {
816 if (fd == INVALID_FD) {
818 code = nt_unlink((char *)&name.n_path);
827 code = namei_SetLinkCount(fdP, tmp.ih_ino, 1, 0);
829 } else if (p2 == INODESPECIAL && p3 == VI_LINKTABLE) {
830 if (fd == INVALID_FD)
832 /* hack at tmp to setup for set link count call. */
834 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
839 if (fd != INVALID_FD)
842 if (code || (fd == INVALID_FD)) {
843 if (p2 != INODESPECIAL) {
846 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
852 int save_errno = errno;
853 namei_RemoveDataDirectories(&name);
857 return (code || (fd == INVALID_FD)) ? (Inode) -1 : tmp.ih_ino;
859 #else /* !AFS_NT40_ENV */
861 namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
873 memset((void *)&tmp, 0, sizeof(IHandle_t));
874 memset(&tfd, 0, sizeof(FdHandle_t));
876 tmp.ih_dev = volutil_GetPartitionID(part);
877 if (tmp.ih_dev == -1) {
883 /* Parameters for special file:
884 * p1 - volume id - goes into owner/group/mode
887 * p4 - parent volume id
892 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
893 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
895 int vno = p2 & NAMEI_VNODEMASK;
896 /* Parameters for regular file:
908 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
909 tag = GetFreeTag(lh, p2);
914 tmp.ih_ino = (Inode) p2;
915 /* name is <uniq(p3)><tag><vno(p2)> */
916 tmp.ih_ino |= ((Inode) tag) << NAMEI_TAGSHIFT;
917 tmp.ih_ino |= ((Inode) p3) << NAMEI_UNIQSHIFT;
922 namei_HandleToName(&name, &tmp);
923 fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
924 if (fd == INVALID_FD) {
925 if (errno == ENOTDIR || errno == ENOENT) {
926 if (namei_CreateDataDirectories(&name, &created_dir) < 0)
928 fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR,
930 if (fd == INVALID_FD)
936 if (SetOGM(fd, ogm_parm, tag) < 0) {
942 if (p2 == (afs_uint32)-1 && p3 == VI_LINKTABLE) {
943 /* hack at tmp to setup for set link count call. */
944 memset((void *)&tfd, 0, sizeof(FdHandle_t)); /* minimalistic still, but a little cleaner */
947 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
951 if (fd != INVALID_FD)
955 if (code || (fd == INVALID_FD)) {
959 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
964 return (code || (fd == INVALID_FD)) ? (Inode) - 1 : tmp.ih_ino;
970 namei_iopen(IHandle_t * h)
975 /* Convert handle to file name. */
976 namei_HandleToName(&name, h);
977 fd = OS_OPEN((char *)&name.n_path, O_RDWR, 0666);
981 /* Need to detect vol special file and just unlink. In those cases, the
982 * handle passed in _is_ for the inode. We only check p1 for the special
986 namei_dec(IHandle_t * ih, Inode ino, int p1)
993 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
995 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
997 /* Verify this is the right file. */
998 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
1000 namei_HandleToName(&name, tmp);
1005 errno = OS_ERROR(ENOENT);
1009 if (CheckOGM(&name, fdP, p1) < 0) {
1010 FDH_REALLYCLOSE(fdP);
1012 errno = OS_ERROR(EINVAL);
1016 /* If it's the link table itself, decrement the link count. */
1017 if (type == VI_LINKTABLE) {
1018 if ((count = namei_GetLinkCount(fdP, (Inode) 0, 1, 0, 1)) < 0) {
1019 FDH_REALLYCLOSE(fdP);
1025 if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) <
1027 FDH_REALLYCLOSE(fdP);
1033 FDH_REALLYCLOSE(fdP);
1039 if ((code = OS_UNLINK(name.n_path)) == 0) {
1040 if (type == VI_LINKTABLE) {
1041 /* Try to remove directory. If it fails, that's ok.
1042 * Salvage will clean up.
1044 (void)namei_RemoveDataDirectories(&name);
1047 FDH_REALLYCLOSE(fdP);
1050 /* Get a file descriptor handle for this Inode */
1056 if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0) {
1057 FDH_REALLYCLOSE(fdP);
1063 if (namei_SetLinkCount(fdP, ino, count, 1) < 0) {
1064 FDH_REALLYCLOSE(fdP);
1069 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
1070 Log("Warning: Lost ref on ihandle dev %d vid %d ino %" AFS_INT64_FMT "\n",
1071 th->ih_dev, th->ih_vid, (afs_int64)th->ih_ino);
1074 /* If we're less than 0, someone presumably unlinked;
1075 don't bother setting count to 0, but we need to drop a lock */
1076 if (namei_SetLinkCount(fdP, ino, 0, 1) < 0) {
1077 FDH_REALLYCLOSE(fdP);
1083 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
1085 namei_HandleToName(&name, th);
1087 code = OS_UNLINK(name.n_path);
1096 namei_inc(IHandle_t * h, Inode ino, int p1)
1102 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1103 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1104 if (type != VI_LINKTABLE)
1109 /* Get a file descriptor handle for this Inode */
1115 if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0)
1120 errno = OS_ERROR(EINVAL);
1124 if (namei_SetLinkCount(fdP, ino, count, 1) < 0)
1128 FDH_REALLYCLOSE(fdP);
1135 #ifndef AFS_NT40_ENV
1137 namei_replace_file_by_hardlink(IHandle_t *hLink, IHandle_t *hTarget)
1143 /* Convert handle to file name. */
1144 namei_HandleToName(&nameLink, hLink);
1145 namei_HandleToName(&nameTarget, hTarget);
1147 OS_UNLINK(nameLink.n_path);
1148 code = link(nameTarget.n_path, nameLink.n_path);
1153 namei_copy_on_write(IHandle_t *h)
1159 struct afs_stat_st tstat;
1162 namei_HandleToName(&name, h);
1163 if (afs_stat(name.n_path, &tstat) < 0)
1165 if (tstat.st_nlink > 1) { /* do a copy on write */
1174 afs_snprintf(path, sizeof(path), "%s-tmp", name.n_path);
1175 fd = OS_OPEN(path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
1176 if (fd == INVALID_FD) {
1187 size = tstat.st_size;
1190 tlen = size > 8192 ? 8192 : size;
1191 if (FDH_PREAD(fdP, buf, tlen, offset) != tlen)
1193 if (OS_WRITE(fd, buf, tlen) != tlen)
1199 FDH_REALLYCLOSE(fdP);
1204 OS_UNLINK(name.n_path);
1205 code = rename(path, name.n_path);
1212 /************************************************************************
1213 * File Name Structure
1214 ************************************************************************
1216 * Each AFS file needs a unique name and it needs to be findable with
1217 * minimal lookup time. Note that the constraint on the number of files and
1218 * directories in a volume is the size of the vnode index files and the
1219 * max file size AFS supports (for internal files) of 2^31. Since a record
1220 * in the small vnode index file is 64 bytes long, we can have at most
1221 * (2^31)/64 or 33554432 files. A record in the large index file is
1222 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
1223 * Another layout parameter is that there is roughly a 16 to 1 ratio between
1224 * the number of files and the number of directories.
1226 * Using this information we can see that a layout of 256 directories, each
1227 * with 512 subdirectories and each of those having 512 files gives us
1228 * 256*512*512 = 67108864 AFS files and directories.
1230 * The volume, vnode, uniquifier and data version, as well as the tag
1231 * are required, either for finding the file or for salvaging. It's best to
1232 * restrict the name to something that can be mapped into 64 bits so the
1233 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
1234 * is used to distinguish between different versions of the same file
1235 * which are currently in the RW and clones of a volume. See "Link Table
1236 * Organization" below for more information on the tag. The tag is
1237 * required in the name of the file to ensure a unique name.
1239 * ifdef AFS_NT40_ENV
1240 * The data for each volume group is in a separate directory. The name of the
1241 * volume is of the form: Vol_NNNNNN.data, where NNNNNN is a base 32
1242 * representation of the RW volume ID (even where the RO is the only volume
1243 * on the partition). Below that are separate subdirectories for the
1244 * AFS directories and special files. There are also 16 directories for files,
1245 * hashed on the low 5 bits (recall bit0 is always 0) of the vnode number.
1246 * These directories are named:
1247 * A - P - 16 file directories.
1248 * Q ----- data directory
1249 * R ----- special files directory
1251 * The vnode is hashed into the directory using the low bits of the
1254 * The format of a file name for a regular file is:
1255 * Y:\Vol_NNNNNN.data\X\V_IIIIII.J
1256 * Y - partition encoded as drive letter, starting with D
1257 * NNNNNN - base 32 encoded volume number of RW volume
1258 * X - hash directory, as above
1259 * IIIIII - base 32 encoded vnode number
1260 * J - base 32 encoded tag
1262 * uniq is stored in the dwHighDateTime creation time field
1263 * dv is stored in the dwLowDateTime creation time field
1265 * Special inodes are always in the R directory, as above, and are
1267 * True child volid is stored in the dwHighDateTime creation time field
1268 * vnode number is always -1 (Special)
1269 * type is the IIIIII part of the filename
1270 * uniq is the J part of the filename
1271 * parent volume id is implied in the containing directory
1274 * We can store data in the uid, gid and mode bits of the files, provided
1275 * the directories have root only access. This gives us 15 bits for each
1276 * of uid and gid (GNU chown considers 65535 to mean "don't change").
1277 * There are 9 available mode bits. Adn we need to store a total of
1278 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
1279 * or 131 bits somewhere.
1281 * The format of a file name for a regular file is:
1282 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
1283 * V1 - low 8 bits of RW volume id
1284 * V2 - all bits of RW volume id
1285 * AA - high 8 bits of vnode number.
1286 * BB - next 9 bits of vnode number.
1287 * <tag><uniq><vno> - file name
1289 * Volume special files are stored in a separate directory:
1290 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
1293 * The vnode is hashed into the directory using the high bits of the
1294 * vnode number. This is so that consecutively created vnodes are in
1295 * roughly the same area on the disk. This will at least be optimal if
1296 * the user is creating many files in the same AFS directory. The name
1297 * should be formed so that the leading characters are different as quickly
1298 * as possible, leading to faster discards of incorrect matches in the
1306 /************************************************************************
1307 * Link Table Organization
1308 ************************************************************************
1310 * The link table volume special file is used to hold the link counts that
1311 * are held in the inodes in inode based AFS vice filesystems. For user
1312 * space access, the link counts are being kept in a separate
1313 * volume special file. The file begins with the usual version stamp
1314 * information and is then followed by one row per vnode number. vnode 0
1315 * is used to hold the link count of the link table itself. That is because
1316 * the same link table is shared among all the volumes of the volume group
1317 * and is deleted only when the last volume of a volume group is deleted.
1319 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
1320 * link count from 0 through 7. Each colume represents a unique instance of
1321 * that vnode. Say we have a file shared between the RW and a RO and a
1322 * different version of the file (or a different uniquifer) for the BU volume.
1323 * Then one column would be holding the link count of 2 for the RW and RO
1324 * and a different column would hold the link count of 1 for the BU volume.
1325 * # ifdef AFS_NT40_ENV
1326 * The column used is determined for NT by the uniquifier tag applied to
1327 * generate a unique file name in the NTFS namespace. The file name is
1328 * of the form "V_<vno>.<tag>" . And the <tag> is also the column number
1329 * in the link table.
1331 * Note that we allow only 5 volumes per file, giving 15 bits used in the
1335 #define LINKTABLE_WIDTH 2
1336 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
1339 * compute namei link table file and bit offset from inode number.
1341 * @param[in] ino inode number
1342 * @param[out] offset link table file offset
1343 * @param[out] index bit offset within 2-byte record
1348 namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
1350 int toff = (int)(ino & NAMEI_VNODEMASK);
1351 int tindex = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1353 *offset = (afs_foff_t) ((toff << LINKTABLE_SHIFT) + 8); /* * 2 + sizeof stamp */
1354 *index = (tindex << 1) + tindex;
1357 #ifdef AFS_PTHREAD_ENV
1358 /* XXX do static initializers work for WINNT/pthread? */
1359 pthread_mutex_t _namei_glc_lock = PTHREAD_MUTEX_INITIALIZER;
1360 #define NAMEI_GLC_LOCK MUTEX_ENTER(&_namei_glc_lock)
1361 #define NAMEI_GLC_UNLOCK MUTEX_EXIT(&_namei_glc_lock)
1362 #else /* !AFS_PTHREAD_ENV */
1363 #define NAMEI_GLC_LOCK
1364 #define NAMEI_GLC_UNLOCK
1365 #endif /* !AFS_PTHREAD_ENV */
1368 * get the link count of an inode.
1370 * @param[in] h namei link count table file handle
1371 * @param[in] ino inode number for which we are requesting a link count
1372 * @param[in] lockit if asserted, return with lock held on link table file
1373 * @param[in] fixup if asserted, write 1 to link count when read() returns
1375 * @param[in] nowrite return success on zero byte read or ZLC
1377 * @post if lockit asserted and lookup was successful, will return with write
1378 * lock on link table file descriptor
1380 * @return link count
1381 * @retval -1 namei link table i/o error
1386 namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite)
1388 unsigned short row = 0;
1393 /* there's no linktable yet. the salvager will create one later */
1394 if (h->fd_fd == INVALID_FD && fixup)
1396 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1399 if (FDH_LOCKFILE(h, offset) != 0)
1403 rc = FDH_PREAD(h, (char*)&row, sizeof(row), offset);
1404 if ((rc == 0 || !((row >> index) & NAMEI_TAGMASK)) && fixup && nowrite)
1406 if (rc == 0 && fixup) {
1408 * extend link table and write a link count of 1 for ino
1410 * in order to make MT-safe, truncation (extension really)
1411 * must happen under a mutex
1414 if (FDH_SIZE(h) >= offset+sizeof(row)) {
1416 goto bad_getLinkByte;
1418 FDH_TRUNC(h, offset+sizeof(row));
1420 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
1423 if (rc != sizeof(row)) {
1424 goto bad_getLinkByte;
1427 if (fixup && !((row >> index) & NAMEI_TAGMASK)) {
1431 * in order to make this mt-safe, we need to do the read-modify-write
1432 * under a mutex. thus, we repeat the read inside the lock.
1435 rc = FDH_PREAD(h, (char *)&row, sizeof(row), offset);
1436 if (rc == sizeof(row)) {
1438 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
1441 if (rc != sizeof(row))
1442 goto bad_getLinkByte;
1445 return (int)((row >> index) & NAMEI_TAGMASK);
1449 FDH_UNLOCKFILE(h, offset);
1454 namei_SetNonZLC(FdHandle_t * h, Inode ino)
1456 return namei_GetLinkCount(h, ino, 0, 1, 0);
1459 /* Return a free column index for this vnode. */
1461 GetFreeTag(IHandle_t * ih, int vno)
1475 /* Only one manipulates at a time. */
1476 if (FDH_LOCKFILE(fdP, offset) != 0) {
1477 FDH_REALLYCLOSE(fdP);
1481 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
1483 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1484 if (nBytes != sizeof(row)) {
1490 /* Now find a free column in this row and claim it. */
1491 for (col = 0; col < NAMEI_MAXVOLS; col++) {
1492 coldata = 7 << (col * 3);
1493 if ((row & coldata) == 0)
1496 if (col >= NAMEI_MAXVOLS) {
1501 coldata = 1 << (col * 3);
1504 if (FDH_PWRITE(fdP, (char *)&row, sizeof(row), offset) != sizeof(row)) {
1508 FDH_UNLOCKFILE(fdP, offset);
1509 FDH_REALLYCLOSE(fdP);
1513 FDH_UNLOCKFILE(fdP, offset);
1514 FDH_REALLYCLOSE(fdP);
1520 /* namei_SetLinkCount
1521 * If locked is set, assume file is locked. Otherwise, lock file before
1522 * proceeding to modify it.
1525 namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
1531 ssize_t nBytes = -1;
1533 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1536 if (FDH_LOCKFILE(fdP, offset) != 0) {
1541 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1542 if (nBytes != sizeof(row)) {
1544 errno = OS_ERROR(EBADF);
1545 goto bad_SetLinkCount;
1550 bytesRead = 7 << index;
1552 row &= (unsigned short)~bytesRead;
1553 row |= (unsigned short)count;
1555 if (FDH_PWRITE(fdP, (char *)&row, sizeof(short), offset) != sizeof(short)) {
1556 errno = OS_ERROR(EBADF);
1557 goto bad_SetLinkCount;
1565 FDH_UNLOCKFILE(fdP, offset);
1567 /* disallowed above 7, so... */
1572 /* ListViceInodes - write inode data to a results file. */
1573 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1574 unsigned int volid);
1575 static int DecodeVolumeName(char *name, unsigned int *vid);
1576 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
1577 int (*write_fun) (FD_t,
1578 struct ViceInodeInfo *,
1579 char *, char *), FD_t fp,
1580 int (*judgeFun) (struct ViceInodeInfo *,
1581 afs_uint32 vid, void *),
1582 afs_uint32 singleVolumeNumber, void *rock);
1587 * Write the inode data to the results file.
1589 * Returns -2 on error, 0 on success.
1591 * This is written as a callback simply so that other listing routines
1592 * can use the same inode reading code.
1595 WriteInodeInfo(FD_t fp, struct ViceInodeInfo *info, char *dir, char *name)
1598 n = OS_WRITE(fp, info, sizeof(*info));
1599 return (n == 1) ? 0 : -2;
1603 int mode_errors; /* Number of errors found in mode bits on directories. */
1605 VerifyDirPerms(char *path)
1607 struct afs_stat_st status;
1609 if (afs_stat(path, &status) < 0) {
1610 Log("Unable to stat %s. Please manually verify mode bits for this"
1611 " directory\n", path);
1613 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1619 * Fill the results file with the requested inode information.
1621 * This code optimizes single volume salvages by just looking at that one
1622 * volume's directory.
1624 * @param[in] devname device name string
1625 * @param[in] moutnedOn vice partition mount point
1626 * @param[in] resultFile result file in which to write inode
1627 * metadata. If NULL, write routine is not
1629 * @param[in] judgeInode filter function pointer. if not NULL, only
1630 * inodes for which this routine returns non-
1631 * zero will be written to the results file.
1632 * @param[in] singleVolumeNumber volume id filter
1633 * @param[out] forcep always set to 0 for namei impl
1634 * @param[in] forceR not used by namei impl
1635 * @param[in] wpath not used by namei impl
1636 * @param[in] rock opaque pointer passed to judgeInode
1638 * @return operation status
1640 * @retval -1 complete failure, salvage should terminate.
1641 * @retval -2 not enough space on partition, salvager has error message
1645 ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
1646 int (*judgeInode) (struct ViceInodeInfo * info, afs_uint32 vid, void *rock),
1647 afs_uint32 singleVolumeNumber, int *forcep, int forceR, char *wpath,
1652 *forcep = 0; /* no need to salvage until further notice */
1654 /* Verify protections on directories. */
1656 VerifyDirPerms(mountedOn);
1659 namei_ListAFSFiles(mountedOn, WriteInodeInfo, inodeFile, judgeInode,
1660 singleVolumeNumber, rock);
1662 if (inodeFile == INVALID_FD)
1669 if (OS_SYNC(inodeFile) == -1) {
1670 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1675 * Paranoia: check that the file is really the right size
1677 if (OS_SIZE(inodeFile) * sizeof(struct ViceInodeInfo)) {
1678 Log("Wrong size (%d instead of %lu) in inode file for %s\n",
1679 (int) OS_SIZE(inodeFile),
1680 (long unsigned int) ninodes * sizeof(struct ViceInodeInfo),
1689 * Collect all the matching AFS files on the drive.
1690 * If singleVolumeNumber is non-zero, just return files for that volume.
1692 * @param[in] dev vice partition path
1693 * @param[in] writeFun function pointer to a function which
1694 * writes inode information to FILE fp
1695 * @param[in] fp file stream where inode metadata is sent
1696 * @param[in] judgeFun filter function pointer. if not NULL,
1697 * only entries for which a non-zero value
1698 * is returned are written to fp
1699 * @param[in] singleVolumeNumber volume id filter. if nonzero, only
1700 * process files for that specific volume id
1701 * @param[in] rock opaque pointer passed into writeFun and
1704 * @return operation status
1706 * @retval >=0 number of matching files found
1709 namei_ListAFSFiles(char *dev,
1710 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1713 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1714 afs_uint32 singleVolumeNumber, void *rock)
1721 #ifndef AFS_NT40_ENV
1727 static void FreeZLCList(void);
1730 memset((void *)&ih, 0, sizeof(IHandle_t));
1732 ih.ih_dev = nt_DriveToDev(dev);
1734 ih.ih_dev = volutil_GetPartitionID(dev);
1737 if (singleVolumeNumber) {
1738 ih.ih_vid = singleVolumeNumber;
1739 namei_HandleToVolDir(&name, &ih);
1741 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1742 singleVolumeNumber, rock);
1746 /* Find all volume data directories and descend through them. */
1747 namei_HandleToInodeDir(&name, &ih);
1749 dirp1 = opendir(name.n_path);
1752 while ((dp1 = readdir(dirp1))) {
1754 /* Heirarchy is one level on Windows */
1755 if (!DecodeVolumeName(dp1->d_name, &ih.ih_vid)) {
1757 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1761 if (*dp1->d_name == '.')
1763 afs_snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", name.n_path,
1765 dirp2 = opendir(path2);
1767 while ((dp2 = readdir(dirp2))) {
1768 if (*dp2->d_name == '.')
1770 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1772 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1789 static void AddToZLCDeleteList(char dir, char *name);
1790 static void DeleteZLCFiles(char *path);
1794 * examine a namei volume special file.
1796 * @param[in] path1 volume special directory path
1797 * @param[in] dname directory entry name
1798 * @param[in] myIH inode handle to volume directory
1799 * @param[out] linkHandle namei link count fd handle. if
1800 * the inode in question is the link
1801 * table, then the FdHandle is populated
1802 * @param[in] writeFun metadata write function pointer
1803 * @param[in] fp file pointer where inode metadata
1804 * is written by (*writeFun)()
1805 * @param[in] judgeFun inode filter function pointer. if
1806 * not NULL, only inodes for which this
1807 * function returns non-zero are recorded
1808 * into fp by writeFun
1809 * @param[in] singleVolumeNumer volume id filter. if non-zero, only
1810 * inodes associated with this volume id
1811 * are recorded by writeFun
1812 * @param[in] rock opaque pointer passed to writeFun and
1815 * @return operation status
1816 * @retval 1 count this inode
1817 * @retval 0 don't count this inode
1818 * @retval -1 failure
1823 _namei_examine_special(char * path1,
1826 FdHandle_t * linkHandle,
1827 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1830 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1831 int singleVolumeNumber,
1835 struct ViceInodeInfo info;
1836 afs_uint32 inode_vgid;
1838 if (DecodeInode(path1, dname, &info, myIH->ih_vid) < 0) {
1844 inode_vgid = myIH->ih_vid;
1846 inode_vgid = (info.inodeNumber >> NAMEI_UNIQSHIFT) & NAMEI_UNIQMASK;
1849 if (info.u.param[2] != VI_LINKTABLE) {
1851 } else if ((info.u.param[0] != myIH->ih_vid) ||
1852 (inode_vgid != myIH->ih_vid)) {
1853 /* VGID encoded in linktable filename and/or OGM data isn't
1854 * consistent with VGID encoded in namei path */
1855 Log("namei_ListAFSSubDirs: warning: inconsistent linktable "
1856 "filename \"%s" OS_DIRSEP "%s\"; salvager will delete it "
1857 "(dir_vgid=%u, inode_vgid=%u, ogm_vgid=%u)\n",
1858 path1, dname, myIH->ih_vid,
1859 (unsigned int)inode_vgid,
1863 /* Open this handle */
1864 (void)afs_snprintf(path2, sizeof(path2),
1865 "%s" OS_DIRSEP "%s", path1, dname);
1866 linkHandle->fd_fd = OS_OPEN(path2, Testing ? O_RDONLY : O_RDWR, 0666);
1868 namei_GetLinkCount(linkHandle, (Inode) 0, 1, 1, Testing);
1872 (*judgeFun) (&info, singleVolumeNumber, rock)) {
1874 if ((*writeFun) (fp, &info, path1, dname) < 0) {
1884 * examine a namei file.
1886 * @param[in] path3 volume special directory path
1887 * @param[in] dname directory entry name
1888 * @param[in] myIH inode handle to volume directory
1889 * @param[in] linkHandle namei link count fd handle.
1890 * @param[in] writeFun metadata write function pointer
1891 * @param[in] fp file pointer where inode metadata
1892 * is written by (*writeFun)()
1893 * @param[in] judgeFun inode filter function pointer. if
1894 * not NULL, only inodes for which this
1895 * function returns non-zero are recorded
1896 * into fp by writeFun
1897 * @param[in] singleVolumeNumer volume id filter. if non-zero, only
1898 * inodes associated with this volume id
1899 * are recorded by writeFun
1900 * @param[in] rock opaque pointer passed to writeFun and
1903 * @return operation status
1904 * @retval 1 count this inode
1905 * @retval 0 don't count this inode
1906 * @retval -1 failure
1907 * @retval -2 request ZLC delete
1912 _namei_examine_reg(char * path3,
1915 FdHandle_t * linkHandle,
1916 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1919 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1920 int singleVolumeNumber,
1924 struct ViceInodeInfo info;
1926 int dirl; /* Windows-only (one level hash dir) */
1929 if (DecodeInode(path3, dname, &info, myIH->ih_vid) < 0) {
1934 namei_GetLinkCount(linkHandle,
1935 info.inodeNumber, 1, 1, Testing);
1936 if (info.linkCount == 0) {
1938 Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n", path3, dname);
1939 #ifdef AFS_SALSRV_ENV
1940 /* defer -- the AddToZLCDeleteList() interface is not MT-safe */
1942 #else /* !AFS_SALSRV_ENV */
1943 dirl = path3[strlen(path3)-1];
1944 AddToZLCDeleteList((char)dirl, dname);
1945 #endif /* !AFS_SALSRV_ENV */
1946 #else /* !DELETE_ZLC */
1947 Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path3,
1954 (*judgeFun) (&info, singleVolumeNumber, rock)) {
1956 if ((*writeFun) (fp, &info, path3, dname) < 0) {
1966 * listsubdirs work queue node.
1968 struct listsubdirs_work_node {
1969 #ifdef AFS_SALSRV_ENV
1970 int *error; /**< *error set if an error was
1971 * encountered in any listsubdirs
1975 IHandle_t * IH; /**< volume directory handle */
1976 FdHandle_t *linkHandle; /**< namei link count fd handle. when
1977 * examinining the link table special
1978 * inode, this will be pointed at the
1981 FD_t fp; /**< file pointer for writeFun */
1983 /** function which will write inode metadata to fp */
1984 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *, char *);
1986 /** inode filter function */
1987 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *);
1988 int singleVolumeNumber; /**< volume id filter */
1989 void * rock; /**< pointer passed to writeFun and judgeFun */
1990 int code; /**< return code from examine function */
1991 int special; /**< asserted when this is a volume
1996 * simple wrapper around _namei_examine_special and _namei_examine_reg.
1998 * @param[in] work the struct listsubdirs_work_node for the associated
1999 * "list subdirs" job
2000 * @param[in] dir the directory to examine
2001 * @param[in] filename the filename in 'dir' to examine
2003 * @return operation status
2004 * @retval 1 count this inode
2005 * @retval 0 don't count this inode
2006 * @retval -1 failure
2009 _namei_examine_file(const struct listsubdirs_work_node *work, char *dir,
2012 if (work->special) {
2013 return _namei_examine_special(dir, filename, work->IH,
2014 work->linkHandle, work->writeFun, work->fp,
2015 work->judgeFun, work->singleVolumeNumber,
2018 return _namei_examine_reg(dir, filename, work->IH,
2019 work->linkHandle, work->writeFun, work->fp,
2020 work->judgeFun, work->singleVolumeNumber,
2026 #ifdef AFS_SALSRV_ENV
2027 /** @addtogroup afs_vol_salsrv_pario */
2031 * arguments for the _namei_examine_file_cbk callback function.
2033 struct listsubdirs_args {
2034 const struct listsubdirs_work_node *work; /**< arguments that are the same
2035 * for all invocations of
2036 * _namei_examine_file_cbk, in
2038 int *result; /**< where we can store the return code of _namei_examine_file */
2040 char dir[512]; /**< directory to examine */
2041 char filename[256]; /**< filename in 'dir' to examine */
2045 * a node in the list of results of listsubdir jobs.
2047 struct listsubdirs_result {
2049 int inodes; /**< return value from _namei_examine_file */
2053 * clean up a list of 'struct listsubdirs_result's and interpret the results.
2055 * @param[in] resultlist a list of 'struct listsubdirs_result's
2057 * @return number of inodes found
2061 _namei_listsubdirs_cleanup_results(struct rx_queue *resultlist)
2063 struct listsubdirs_result *res, *nres;
2066 for(queue_Scan(resultlist, res, nres, listsubdirs_result)) {
2068 /* noop, retain erroneous error code */
2069 } else if (res->inodes < 0) {
2084 * wait for the spawned listsubdirs jobs to finish, and return how many inodes
2087 * @param[in] queue queue to wait to finish
2088 * @param[in] resultlist list of 'struct listsubdirs_result's that the queued
2089 * jobs are storing their results in
2091 * @return number of inodes found
2095 _namei_listsubdirs_wait(struct afs_work_queue *queue, struct rx_queue *resultlist)
2099 code = afs_wq_wait_all(queue);
2104 return _namei_listsubdirs_cleanup_results(resultlist);
2108 * work queue entry point for examining namei files.
2110 * @param[in] queue pointer to struct Vwork_queue
2111 * @param[in] node pointer to struct Vwork_queue_node
2112 * @param[in] queue_rock opaque pointer to struct salsrv_pool_state
2113 * @param[in] node_rock opaque pointer to struct listsubdirs_work_node
2114 * @param[in] caller_rock opaque pointer to struct salsrv_worker_thread_state
2116 * @return operation status
2118 * @see Vwork_queue_callback_func_t
2123 _namei_examine_file_cbk(struct afs_work_queue *queue,
2124 struct afs_work_queue_node *node,
2130 struct listsubdirs_args *args = node_rock;
2131 const struct listsubdirs_work_node * work = args->work;
2132 char *dir = args->dir;
2133 char *filename = args->filename;
2135 code = _namei_examine_file(work, dir, filename);
2137 *(args->result) = code;
2141 /* we've errored, so no point in letting more jobs continue */
2142 afs_wq_shutdown(queue);
2148 static pthread_once_t wq_once = PTHREAD_ONCE_INIT;
2149 static pthread_key_t wq_key;
2152 * create the wq_key key for storing a work queue.
2155 _namei_wq_keycreate(void)
2157 osi_Assert(pthread_key_create(&wq_key, NULL) == 0);
2161 * set the work queue for this thread to use for backgrounding namei ops.
2163 * The work queue will be used in ListAFSSubdirs (called indirectly by
2164 * ListViceInodes) to examine files in parallel.
2166 * @param[in] wq the work queue to use
2169 namei_SetWorkQueue(struct afs_work_queue *wq)
2171 osi_Assert(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
2173 osi_Assert(pthread_setspecific(wq_key, wq) == 0);
2177 * enqueue an examine file work unit.
2179 * @param[in] work the _namei_examine_file arguments that are common to
2180 * all callers within the same ListAFSFiles operation
2181 * @param[in] dir the specific directory to look at (string will be
2182 * copied; can be stack/temporary memory)
2183 * @param[in] filename the filename to look at (string will be copied; can be
2184 * stack/temporary memory)
2185 * @param[in] wq work queue to enqueue this work unit to
2186 * @param[in] resultlist the list to append the 'struct listsubdirs_result' to
2187 * for the enqueued work unit
2189 * @return operation status
2191 * @retval -1 fatal error
2193 * @note errors MUST be indicated by a -1 error code and nothing else, to be
2194 * compatible with _namei_examine_reg and _namei_examine_special
2199 _namei_examine_file_spawn(const struct listsubdirs_work_node *work,
2200 const char *dir, const char *filename,
2201 struct afs_work_queue *wq,
2202 struct rx_queue *resultlist)
2205 struct listsubdirs_args *args = NULL;
2206 struct listsubdirs_result *result = NULL;
2207 struct afs_work_queue_node *node = NULL;
2208 struct afs_work_queue_add_opts opts;
2210 args = malloc(sizeof(*args));
2216 result = malloc(sizeof(*result));
2217 if (result == NULL) {
2222 code = afs_wq_node_alloc(&node);
2227 code = afs_wq_node_set_detached(node);
2234 args->result = &result->inodes;
2235 strlcpy(args->dir, dir, sizeof(args->dir));
2236 strlcpy(args->filename, filename, sizeof(args->filename));
2238 code = afs_wq_node_set_callback(node,
2239 &_namei_examine_file_cbk,
2247 afs_wq_add_opts_init(&opts);
2250 code = afs_wq_add(wq, node, &opts);
2257 queue_Append(resultlist, result);
2262 afs_wq_node_put(node);
2278 #else /* !AFS_SALSRV_ENV */
2279 # define _namei_examine_file_spawn(work, dir, file, wq, resultlist) \
2280 _namei_examine_file(work, dir, file)
2281 #endif /* !AFS_SALSRV_ENV */
2284 * traverse and check inodes.
2286 * @param[in] dirIH volume group directory handle
2287 * @param[in] writeFun function pointer which will write inode
2288 * metadata to FILE stream fp
2289 * @param[in] fp file stream where inode metadata gets
2291 * @param[in] judgeFun inode filter function. if not NULL, only
2292 * inodes for which the filter returns non-zero
2293 * will be written out by writeFun
2294 * @param[in] singleVolumeNumber volume id filter. only inodes matching this
2295 * filter are written out by writeFun
2296 * @param[in] rock opaque pointer passed to judgeFun and writeFun
2298 * @return operation status
2300 * @retval >=0 number of matching inodes found
2302 * @todo the salsrv implementation may consume a lot of
2303 * memory for a large volume. at some point we should
2304 * probably write a background thread to asynchronously
2305 * clean up the resultlist nodes to reduce memory footprint
2310 namei_ListAFSSubDirs(IHandle_t * dirIH,
2311 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
2314 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
2315 afs_uint32 singleVolumeNumber, void *rock)
2317 int code = 0, ret = 0;
2318 IHandle_t myIH = *dirIH;
2320 char path1[512], path3[512];
2322 #ifndef AFS_NT40_ENV
2327 struct dirent *dp1, *dp3;
2328 FdHandle_t linkHandle;
2330 struct listsubdirs_work_node work;
2331 #ifdef AFS_SALSRV_ENV
2333 struct afs_work_queue *wq;
2335 struct rx_queue resultlist;
2338 namei_HandleToVolDir(&name, &myIH);
2339 strlcpy(path1, name.n_path, sizeof(path1));
2341 /* Do the directory containing the special files first to pick up link
2344 (void)strcat(path1, OS_DIRSEP);
2345 (void)strcat(path1, NAMEI_SPECDIR);
2347 linkHandle.fd_fd = INVALID_FD;
2348 #ifdef AFS_SALSRV_ENV
2349 osi_Assert(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
2351 wq = pthread_getspecific(wq_key);
2357 queue_Init(&resultlist);
2360 memset(&work, 0, sizeof(work));
2361 work.linkHandle = &linkHandle;
2364 work.writeFun = writeFun;
2365 work.judgeFun = judgeFun;
2366 work.singleVolumeNumber = singleVolumeNumber;
2369 #ifdef AFS_SALSRV_ENV
2370 work.error = &error;
2373 dirp1 = opendir(path1);
2375 while ((dp1 = readdir(dirp1))) {
2376 if (*dp1->d_name == '.')
2379 #ifdef AFS_SALSRV_ENV
2385 #endif /* AFS_SALSRV_ENV */
2387 code = _namei_examine_file_spawn(&work, path1, dp1->d_name, wq, &resultlist);
2397 /* count this inode */
2398 #ifndef AFS_SALSRV_ENV
2407 #ifdef AFS_SALSRV_ENV
2408 /* effectively a barrier */
2409 code = _namei_listsubdirs_wait(wq, &resultlist);
2410 if (code < 0 || error) {
2418 if (linkHandle.fd_fd == INVALID_FD) {
2419 Log("namei_ListAFSSubDirs: warning: VG %u does not have a link table; "
2420 "salvager will recreate it.\n", dirIH->ih_vid);
2423 /* Now run through all the other subdirs */
2424 namei_HandleToVolDir(&name, &myIH);
2425 strlcpy(path1, name.n_path, sizeof(path1));
2429 dirp1 = opendir(path1);
2431 while ((dp1 = readdir(dirp1))) {
2432 #ifndef AFS_NT40_ENV
2433 if (*dp1->d_name == '.')
2436 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
2439 #ifndef AFS_NT40_ENV /* This level missing on Windows */
2440 /* Now we've got a next level subdir. */
2441 afs_snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", path1, dp1->d_name);
2442 dirp2 = opendir(path2);
2444 while ((dp2 = readdir(dirp2))) {
2445 if (*dp2->d_name == '.')
2448 /* Now we've got to the actual data */
2449 afs_snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s", path2,
2452 /* Now we've got to the actual data */
2453 afs_snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s", path1,
2456 dirp3 = opendir(path3);
2458 while ((dp3 = readdir(dirp3))) {
2459 #ifndef AFS_NT40_ENV
2460 if (*dp3->d_name == '.')
2464 #ifdef AFS_SALSRV_ENV
2467 #ifndef AFS_NT40_ENV
2474 #endif /* AFS_SALSRV_ENV */
2476 code = _namei_examine_file_spawn(&work, path3,
2483 #ifndef AFS_NT40_ENV
2491 #ifndef AFS_SALSRV_ENV
2499 #ifndef AFS_NT40_ENV /* This level missing on Windows */
2508 #ifdef AFS_SALSRV_ENV
2509 /* effectively a barrier */
2510 code = _namei_listsubdirs_wait(wq, &resultlist);
2511 if (code < 0 || error) {
2521 /* Then why does this directory exist? Blow it away. */
2522 namei_HandleToVolDir(&name, dirIH);
2523 namei_RemoveDataDirectories(&name);
2527 #ifdef AFS_SALSRV_ENV
2529 afs_wq_wait_all(wq);
2531 _namei_listsubdirs_cleanup_results(&resultlist);
2533 if (linkHandle.fd_fd != INVALID_FD)
2534 OS_CLOSE(linkHandle.fd_fd);
2546 DecodeVolumeName(char *name, unsigned int *vid)
2548 /* Name begins with "Vol_" and ends with .data. See nt_HandleToVolDir() */
2555 if (strncmp(name, "Vol_", 4))
2557 if (strcmp(name + len - 5, ".data"))
2560 stmp[len - 5] = '\0';
2561 *vid = base32_to_int(stmp + 4);
2566 DecodeVolumeName(char *name, unsigned int *vid)
2568 if (strlen(name) < 1)
2570 *vid = (unsigned int)flipbase64_to_int64(name);
2578 * Get the inode number from the name.
2583 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
2588 WIN32_FIND_DATA data;
2592 FdHandle_t linkHandle;
2595 afs_snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
2597 dirH = FindFirstFileEx(fpath, FindExInfoStandard, &data,
2598 FindExSearchNameMatch, NULL,
2599 FIND_FIRST_EX_CASE_SENSITIVE);
2600 if (dirH == INVALID_HANDLE_VALUE)
2603 (void)strcpy(stmp, name);
2604 s = strrchr(stmp, '_');
2608 t = strrchr(s, '.');
2613 vno = base32_to_int(s); /* type for special files */
2614 tag = base32_to_int(t+1);
2615 info->inodeNumber = ((Inode) tag) << NAMEI_TAGSHIFT;
2616 info->inodeNumber |= vno;
2617 info->byteCount = data.nFileSizeLow;
2619 dirl = dpath[strlen(dpath)-1];
2620 if (dirl == NAMEI_SPECDIRC) { /* Special inode. */
2621 info->inodeNumber |= NAMEI_INODESPECIAL;
2622 info->u.param[0] = data.ftCreationTime.dwHighDateTime;
2623 info->u.param[1] = data.ftCreationTime.dwLowDateTime;
2624 info->u.param[2] = vno; /* type */
2625 info->u.param[3] = volid;
2626 if (vno != VI_LINKTABLE)
2627 info->linkCount = 1;
2629 /* Open this handle */
2631 (void)sprintf(lpath, "%s" OS_DIRSEP "%s", fpath, data.cFileName);
2632 linkHandle.fd_fd = OS_OPEN(lpath, O_RDONLY, 0666);
2634 namei_GetLinkCount(&linkHandle, (Inode) 0, 0, 0, 0);
2638 namei_GetLinkCount(&linkHandle, info->inodeNumber, 0, 0, 0);
2639 if (info->linkCount == 0) {
2641 Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n",
2642 fpath, data.cFileName);
2643 AddToZLCDeleteList(dirl, data.cFileName);
2645 Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path,
2649 info->u.param[2] = data.ftCreationTime.dwHighDateTime;
2650 info->u.param[3] = data.ftCreationTime.dwLowDateTime;
2651 info->u.param[1] = vno;
2652 info->u.param[0] = volid;
2659 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
2663 struct afs_stat_st status;
2665 lb64_string_t check;
2667 afs_snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
2669 if (afs_stat(fpath, &status) < 0) {
2673 info->byteCount = status.st_size;
2674 info->inodeNumber = (Inode) flipbase64_to_int64(name);
2676 int64_to_flipbase64(check, info->inodeNumber);
2677 if (strcmp(name, check))
2680 GetOGMFromStat(&status, &parm, &tag);
2681 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
2682 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
2683 info->u.param[0] = parm;
2684 info->u.param[1] = -1;
2685 info->u.param[2] = tag;
2686 info->u.param[3] = volid;
2688 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
2689 info->u.param[0] = volid;
2690 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
2691 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
2692 & (Inode) NAMEI_UNIQMASK);
2693 info->u.param[3] = parm;
2700 * Convert the VolumeInfo file from RO to RW
2701 * this routine is called by namei_convertROtoRWvolume()
2704 #ifdef FSSYNC_BUILD_CLIENT
2706 convertVolumeInfo(FD_t fdr, FD_t fdw, afs_uint32 vid)
2708 struct VolumeDiskData vd;
2711 if (OS_READ(fdr, &vd, sizeof(struct VolumeDiskData)) !=
2712 sizeof(struct VolumeDiskData)) {
2713 Log("1 convertVolumeInfo: read failed for %lu with code %d\n",
2714 afs_printable_uint32_lu(vid),
2718 vd.restoredFromId = vd.id; /* remember the RO volume here */
2720 vd.id = vd.parentId;
2724 vd.uniquifier += 5000; /* just in case there are still file copies from
2725 * the old RW volume around */
2727 /* For ROs, the copyDate contains the time that the RO volume was actually
2728 * created, and the creationDate just contains the last time the RO was
2729 * copied from the RW data. So, make the new RW creationDate more accurate
2730 * by setting it to copyDate, if copyDate is older. Since, we know the
2731 * volume is at least as old as copyDate. */
2732 if (vd.copyDate < vd.creationDate) {
2733 vd.creationDate = vd.copyDate;
2735 /* If copyDate is newer, just make copyDate and creationDate the same,
2736 * for consistency with other RWs */
2737 vd.copyDate = vd.creationDate;
2740 p = strrchr(vd.name, '.');
2741 if (p && !strcmp(p, ".readonly")) {
2744 if (OS_WRITE(fdw, &vd, sizeof(struct VolumeDiskData)) !=
2745 sizeof(struct VolumeDiskData)) {
2746 Log("1 convertVolumeInfo: write failed for %lu with code %d\n",
2747 afs_printable_uint32_lu(vid),
2756 * Convert a RO-volume into a RW-volume
2758 * This function allows to recover very fast from the loss of a partition
2759 * from RO-copies if all RO-Copies exist on another partition.
2760 * Then these RO-volumes can be made to the new RW-volumes.
2761 * Backup of RW-volumes then consists in "vos release".
2763 * We must make sure in this partition exists only the RO-volume which
2764 * is typical for remote replicas.
2766 * Then the linktable is already ok,
2767 * the vnode files need to be renamed
2768 * the volinfo file needs to be replaced by another one with
2769 * slightly different contents and new name.
2770 * The volume header file of the RO-volume in the /vicep<x> directory
2771 * is destroyed by this call. A new header file for the RW-volume must
2772 * be created after return from this routine.
2776 namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
2779 #ifdef FSSYNC_BUILD_CLIENT
2781 char dir_name[512], oldpath[512], newpath[512];
2796 struct DiskPartition64 *partP;
2797 struct ViceInodeInfo info;
2798 struct VolumeDiskHeader h;
2799 # ifdef AFS_DEMAND_ATTACH_FS
2801 # endif /* AFS_DEMAND_ATTACH_FS */
2803 for (partP = DiskPartitionList; partP && strcmp(partP->name, pname);
2804 partP = partP->next);
2806 Log("1 namei_ConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
2811 # ifdef AFS_DEMAND_ATTACH_FS
2812 locktype = VVolLockType(V_VOLUPD, 1);
2813 code = VLockVolumeByIdNB(volumeId, partP, locktype);
2819 # endif /* AFS_DEMAND_ATTACH_FS */
2821 if (VReadVolumeDiskHeader(volumeId, partP, &h)) {
2822 Log("1 namei_ConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n",
2823 afs_printable_uint32_lu(volumeId));
2828 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
2830 ino = namei_MakeSpecIno(h.parent, VI_LINKTABLE);
2831 IH_INIT(ih, partP->device, h.parent, ino);
2833 namei_HandleToName(&n, ih);
2834 strlcpy(dir_name, n.n_path, sizeof(dir_name));
2835 p = strrchr(dir_name, OS_DIRSEPC);
2837 dirp = opendir(dir_name);
2839 Log("1 namei_ConvertROtoRWvolume: Could not opendir(%s)\n", dir_name);
2844 while ((dp = readdir(dirp))) {
2845 /* struct ViceInodeInfo info; */
2846 #ifndef AFS_NT40_ENV
2847 if (*dp->d_name == '.')
2850 if (DecodeInode(dir_name, dp->d_name, &info, ih->ih_vid) < 0) {
2851 Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s" OS_DIRSEP "%s\n",
2852 dir_name, dp->d_name);
2857 if (info.u.param[1] != -1) {
2858 Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s" OS_DIRSEP "%s\n", dir_name, dp->d_name);
2863 if (info.u.param[0] != volumeId) {
2864 if (info.u.param[0] == ih->ih_vid) {
2865 if (info.u.param[2] == VI_LINKTABLE) { /* link table */
2870 Log("1 namei_ConvertROtoRWvolume: found special file %s" OS_DIRSEP "%s"
2871 " for volume %lu\n", dir_name, dp->d_name,
2872 afs_printable_uint32_lu(info.u.param[0]));
2877 if (info.u.param[2] == VI_VOLINFO) { /* volume info file */
2878 strlcpy(infoName, dp->d_name, sizeof(infoName));
2880 } else if (info.u.param[2] == VI_SMALLINDEX) { /* small vnodes file */
2881 strlcpy(smallName, dp->d_name, sizeof(smallName));
2883 } else if (info.u.param[2] == VI_LARGEINDEX) { /* large vnodes file */
2884 strlcpy(largeName, dp->d_name, sizeof(largeName));
2888 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);
2895 if (!infoSeen || !smallSeen || !largeSeen || !linkSeen) {
2896 Log("1 namei_ConvertROtoRWvolume: not all special files found in %s\n", dir_name);
2902 * If we come here then there was only a RO-volume and we can safely
2906 memset(&t_ih, 0, sizeof(t_ih));
2907 t_ih.ih_dev = ih->ih_dev;
2908 t_ih.ih_vid = ih->ih_vid;
2910 (void)afs_snprintf(oldpath, sizeof oldpath, "%s" OS_DIRSEP "%s", dir_name,
2912 fd = OS_OPEN(oldpath, O_RDWR, 0);
2913 if (fd == INVALID_FD) {
2914 Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
2919 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_VOLINFO);
2920 namei_HandleToName(&n, &t_ih);
2921 fd2 = OS_OPEN(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
2922 if (fd2 == INVALID_FD) {
2923 Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
2928 code = convertVolumeInfo(fd, fd2, ih->ih_vid);
2932 OS_UNLINK(n.n_path);
2936 SetOGM(fd2, ih->ih_vid, 1);
2939 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_SMALLINDEX);
2940 namei_HandleToName(&n, &t_ih);
2941 (void)afs_snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
2943 fd = OS_OPEN(newpath, O_RDWR, 0);
2944 if (fd == INVALID_FD) {
2945 Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
2949 SetOGM(fd, ih->ih_vid, 2);
2952 MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
2954 link(newpath, n.n_path);
2958 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_LARGEINDEX);
2959 namei_HandleToName(&n, &t_ih);
2960 (void)afs_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 LargeIndex file: %s\n", newpath);
2968 SetOGM(fd, ih->ih_vid, 3);
2971 MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
2973 link(newpath, n.n_path);
2980 h.volumeInfo_hi = h.id;
2981 h.smallVnodeIndex_hi = h.id;
2982 h.largeVnodeIndex_hi = h.id;
2983 h.linkTable_hi = h.id;
2985 if (VCreateVolumeDiskHeader(&h, partP)) {
2986 Log("1 namei_ConvertROtoRWvolume: Couldn't write header for RW-volume %lu\n",
2987 afs_printable_uint32_lu(h.id));
2992 if (VDestroyVolumeDiskHeader(partP, volumeId, h.parent)) {
2993 Log("1 namei_ConvertROtoRWvolume: Couldn't unlink header for RO-volume %lu\n",
2994 afs_printable_uint32_lu(volumeId));
2997 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
2998 FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
3001 # ifdef AFS_DEMAND_ATTACH_FS
3003 VUnlockVolumeById(volumeId, partP);
3005 # endif /* AFS_DEMAND_ATTACH_FS */
3013 * returns a static string used to print either 32 or 64 bit inode numbers.
3016 PrintInode(char *s, Inode ino)
3018 static afs_ino_str_t result;
3022 (void)afs_snprintf(s, sizeof(afs_ino_str_t), "%" AFS_UINT64_FMT, (afs_uintmax_t) ino);
3029 /* Routines to facilitate removing zero link count files. */
3030 #define MAX_ZLC_NAMES 32
3031 #define MAX_ZLC_NAMELEN 16
3032 typedef struct zlcList_s {
3033 struct zlcList_s *zlc_next;
3035 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
3038 static zlcList_t *zlcAnchor = NULL;
3039 static zlcList_t *zlcCur = NULL;
3042 AddToZLCDeleteList(char dir, char *name)
3044 osi_Assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
3046 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
3047 if (zlcCur && zlcCur->zlc_next)
3048 zlcCur = zlcCur->zlc_next;
3050 zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
3056 zlcCur->zlc_next = tmp;
3060 zlcCur->zlc_next = NULL;
3065 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c" OS_DIRSEP "%s", dir, name);
3067 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%s", name);
3073 DeleteZLCFiles(char *path)
3079 for (z = zlcAnchor; z; z = z->zlc_next) {
3080 for (i = 0; i < z->zlc_n; i++) {
3082 (void)sprintf(fname, "%s" OS_DIRSEP "%s", path, z->zlc_names[i]);
3084 (void)sprintf(fname, "%s", z->zlc_names[i]);
3085 if (namei_unlink(fname) < 0) {
3086 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
3090 z->zlc_n = 0; /* Can reuse space. */
3103 tnext = i->zlc_next;
3107 zlcCur = zlcAnchor = NULL;
3111 #endif /* AFS_NAMEI_ENV */