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 snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s", partition,
358 mkdir(filename, 0700);
360 snprintf(filename, sizeof filename,
361 "%s" OS_DIRSEP "%s" OS_DIRSEP "README",
362 partition, INODEDIR);
363 fd = OS_OPEN(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
364 if (fd != INVALID_FD) {
365 (void)OS_WRITE(fd, VICE_README, strlen(VICE_README));
372 /* namei_CreateDataDirectories
374 * If creating the file failed because of ENOENT or ENOTDIR, try
375 * creating all the directories first.
379 namei_CreateDataDirectories(namei_t * name, int *created)
386 snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
388 if (mkdir(tmp) < 0) {
399 for (i = 'A'; i <= NAMEI_SPECDIRC; i++) {
401 if (mkdir(tmp) < 0 && errno != EEXIST)
407 #define create_dir() \
409 if (mkdir(tmp, 0700)<0) { \
410 if (errno != EEXIST) \
418 #define create_nextdir(A) \
420 strcat(tmp, OS_DIRSEP); strcat(tmp, A); create_dir(); \
424 namei_CreateDataDirectories(namei_t * name, int *created)
430 strlcpy(tmp, name->n_base, sizeof(tmp));
433 create_nextdir(name->n_voldir1);
434 create_nextdir(name->n_voldir2);
435 create_nextdir(name->n_dir1);
436 if (name->n_dir2[0]) {
437 create_nextdir(name->n_dir2);
444 /* delTree(): Deletes an entire tree of directories (no files)
446 * root : Full path to the subtree. Should be big enough for PATH_MAX
447 * tree : the subtree to be deleted is rooted here. Specifies only the
448 * subtree beginning at tree (not the entire path). It should be
449 * a pointer into the "root" buffer.
451 * errp : errno of the first error encountered during the directory cleanup.
452 * *errp should have been initialized to 0.
455 * -1 : If errors were encountered during cleanup and error is set to
459 * If there are errors, we try to work around them and delete as many
460 * directories as possible. We don't attempt to remove directories that still
461 * have non-dir entries in them.
464 delTree(char *root, char *tree, int *errp)
472 /* delete the children first */
473 cp = strchr(tree, OS_DIRSEPC);
475 delTree(root, cp + 1, errp);
478 cp = tree + strlen(tree); /* move cp to the end of string tree */
480 /* now delete all entries in this dir */
481 if ((ds = opendir(root)) != (DIR *) NULL) {
483 while ((dirp = readdir(ds))) {
484 /* ignore . and .. */
485 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
487 /* since root is big enough, we reuse the space to
488 * concatenate the dirname to the current tree
490 strcat(root, OS_DIRSEP);
491 strcat(root, dirp->d_name);
492 if (afs_stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
493 /* delete this subtree */
494 delTree(root, cp + 1, errp);
496 *errp = *errp ? *errp : errno;
498 /* recover path to our cur tree by truncating it to
503 /* if (!errno) -- closedir not implicit if we got an error */
507 /* finally axe the current dir */
509 *errp = *errp ? *errp : errno;
511 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
513 #endif /* !AFS_PTHREAD_ENV */
518 /* if we encountered errors during cleanup, we return a -1 */
527 /* namei_RemoveDataDirectories
529 * Returns 0 on success.
530 * Returns -1 on error. Typically, callers ignore this error because we
531 * can continue running if the removes fail. The salvage process will
532 * finish tidying up for us.
537 namei_RemoveDataDirectories(namei_t * name)
544 snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
547 path += strlen(path);
548 *path++ = OS_DIRSEPC;
550 for (i = 'A'; i <= NAMEI_SPECDIRC; i++) {
552 if (rmdir(name->n_path) < 0 && errno != ENOENT)
557 /* Delete the Vol_NNNNNN.data directory. */
560 if (rmdir(name->n_path) < 0 && errno != ENOENT) {
568 * We only use the n_base and n_voldir1 entries
569 * and only do rmdir's.
572 namei_RemoveDataDirectories(namei_t * name)
576 int prefixlen = strlen(name->n_base), err = 0;
577 int vollen = strlen(name->n_voldir1);
578 char pbuf[MAXPATHLEN];
582 strlcpy(path, name->n_path, sizeof(pbuf));
584 /* move past the prefix and n_voldir1 */
585 path = path + prefixlen + 1 + vollen + 1; /* skip over the trailing / */
587 /* now delete all dirs upto path */
588 code = delTree(pbuf, path, &err);
590 /* We've now deleted everything under /n_base/n_voldir1/n_voldir2 that
591 * we could. Do not delete /n_base/n_voldir1, since doing such might
592 * interrupt another thread trying to create a volume. We could introduce
593 * some locking to make this safe (or only remove it for whole-partition
594 * salvages), but by not deleting it we only leave behind a maximum of
595 * 256 empty directories. So at least for now, don't bother. */
600 /* Create the file in the name space.
602 * Parameters stored as follows:
604 * p1 - volid - implied in containing directory.
605 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
606 * p3 - uniq -- bits 4-0 are in mode bits 4-0
607 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
609 * p1 - volid - creation time - dwHighDateTime
610 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
611 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
612 * p4 - parid - parent volume id - implied in containing directory.
614 * Return value is the inode number or (Inode)-1 if error.
615 * We "know" there is only one link table, so return EEXIST if there already
616 * is a link table. It's up to the calling code to test errno and increment
622 * This function is called by VCreateVolume to hide the implementation
623 * details of the inode numbers. This only allows for 7 volume special
624 * types, but if we get that far, this could should be dead by then.
627 namei_MakeSpecIno(int volid, int type)
630 ino = NAMEI_INODESPECIAL;
633 /* tag is always 0 for special */
635 type &= NAMEI_TAGMASK;
636 ino |= ((Inode) type) << NAMEI_TAGSHIFT;
637 ino |= ((Inode) volid) << NAMEI_UNIQSHIFT;
645 SetOGM(FD_t fd, int parm, int tag)
651 CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
653 WIN32_FIND_DATA info;
657 FindFirstFileEx(name->n_path, FindExInfoStandard, &info,
658 FindExSearchNameMatch, NULL,
659 FIND_FIRST_EX_CASE_SENSITIVE);
662 return -1; /* Can't get info, leave alone */
666 if (info.ftCreationTime.dwHighDateTime != (unsigned int)p1)
671 #else /* AFS_NT40_ENV */
672 /* SetOGM - set owner group and mode bits from parm and tag */
674 SetOGM(FD_t fd, int parm, int tag)
677 * owner - low 15 bits of parm.
678 * group - next 15 bits of parm.
679 * mode - 2 bits of parm, then lowest = 3 bits of tag.
681 int owner, group, mode;
683 owner = parm & 0x7fff;
684 group = (parm >> 15) & 0x7fff;
685 if (fchown(fd, owner, group) < 0)
688 mode = (parm >> 27) & 0x18;
690 if (fchmod(fd, mode) < 0)
695 /* GetOGM - get parm and tag from owner, group and mode bits. */
697 GetOGMFromStat(struct afs_stat_st *status, int *parm, int *tag)
699 *parm = status->st_uid | (status->st_gid << 15);
700 *parm |= (status->st_mode & 0x18) << 27;
701 *tag = status->st_mode & 0x7;
705 CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
707 struct afs_stat_st status;
709 if (afs_fstat(fdP->fd_fd, &status) < 0)
712 GetOGMFromStat(&status, &parm, &tag);
718 #endif /* !AFS_NT40_ENV */
720 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
722 /* Derive the name and create it O_EXCL. If that fails we have an error.
723 * Get the tag from a free column in the link table.
727 namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
730 FD_t fd = INVALID_FD;
741 memset((void *)&tmp, 0, sizeof(IHandle_t));
742 memset(&tfd, 0, sizeof(FdHandle_t));
744 tmp.ih_dev = nt_DriveToDev(part);
745 if (tmp.ih_dev == -1) {
750 if (p2 == INODESPECIAL) {
751 /* Parameters for special file:
752 * p1 - volume id - goes into owner/group/mode
753 * p2 - vnode == INODESPECIAL
755 * p4 - parent volume id
757 ftime.dwHighDateTime = p1;
758 ftime.dwLowDateTime = p2;
760 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
761 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
763 int vno = p2 & NAMEI_VNODEMASK;
764 /* Parameters for regular file:
778 tmp.ih_ino = (Inode) p2;
779 ftime.dwHighDateTime = p3;
780 ftime.dwLowDateTime = p4;
783 namei_HandleToName(&name, &tmp);
784 p = strrchr((char *)&name.n_path, '.');
786 for (tag = 0; tag < NAMEI_MAXVOLS; tag++) {
787 *p = *int_to_base32(str1, tag);
788 fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0666);
789 if (fd == INVALID_FD) {
790 if (errno == ENOTDIR || errno == ENOENT) {
791 if (namei_CreateDataDirectories(&name, &created_dir) == 0)
792 fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0666);
796 if (fd != INVALID_FD)
798 if (p2 == INODESPECIAL && p3 == VI_LINKTABLE)
801 if (fd == INVALID_FD) {
805 tmp.ih_ino &= ~(((Inode) NAMEI_TAGMASK) << NAMEI_TAGSHIFT);
806 tmp.ih_ino |= (((Inode) tag) << NAMEI_TAGSHIFT);
809 if (!SetFileTime((HANDLE) fd, &ftime, NULL, NULL)) {
810 errno = OS_ERROR(EBADF);
816 if (p2 != INODESPECIAL) {
817 if (fd == INVALID_FD) {
819 code = nt_unlink((char *)&name.n_path);
828 code = namei_SetLinkCount(fdP, tmp.ih_ino, 1, 0);
830 } else if (p2 == INODESPECIAL && p3 == VI_LINKTABLE) {
831 if (fd == INVALID_FD)
833 /* hack at tmp to setup for set link count call. */
835 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
840 if (fd != INVALID_FD)
843 if (code || (fd == INVALID_FD)) {
844 if (p2 != INODESPECIAL) {
847 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
853 int save_errno = errno;
854 namei_RemoveDataDirectories(&name);
858 return (code || (fd == INVALID_FD)) ? (Inode) -1 : tmp.ih_ino;
860 #else /* !AFS_NT40_ENV */
862 namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
874 memset((void *)&tmp, 0, sizeof(IHandle_t));
875 memset(&tfd, 0, sizeof(FdHandle_t));
877 tmp.ih_dev = volutil_GetPartitionID(part);
878 if (tmp.ih_dev == -1) {
884 /* Parameters for special file:
885 * p1 - volume id - goes into owner/group/mode
888 * p4 - parent volume id
893 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
894 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
896 int vno = p2 & NAMEI_VNODEMASK;
897 /* Parameters for regular file:
909 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
910 tag = GetFreeTag(lh, p2);
915 tmp.ih_ino = (Inode) p2;
916 /* name is <uniq(p3)><tag><vno(p2)> */
917 tmp.ih_ino |= ((Inode) tag) << NAMEI_TAGSHIFT;
918 tmp.ih_ino |= ((Inode) p3) << NAMEI_UNIQSHIFT;
923 namei_HandleToName(&name, &tmp);
924 fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
925 if (fd == INVALID_FD) {
926 if (errno == ENOTDIR || errno == ENOENT) {
927 if (namei_CreateDataDirectories(&name, &created_dir) < 0)
929 fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR,
931 if (fd == INVALID_FD)
937 if (SetOGM(fd, ogm_parm, tag) < 0) {
943 if (p2 == (afs_uint32)-1 && p3 == VI_LINKTABLE) {
944 /* hack at tmp to setup for set link count call. */
945 memset((void *)&tfd, 0, sizeof(FdHandle_t)); /* minimalistic still, but a little cleaner */
948 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
952 if (fd != INVALID_FD)
956 if (code || (fd == INVALID_FD)) {
960 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
965 return (code || (fd == INVALID_FD)) ? (Inode) - 1 : tmp.ih_ino;
971 namei_iopen(IHandle_t * h)
976 /* Convert handle to file name. */
977 namei_HandleToName(&name, h);
978 fd = OS_OPEN((char *)&name.n_path, O_RDWR, 0666);
982 /* Need to detect vol special file and just unlink. In those cases, the
983 * handle passed in _is_ for the inode. We only check p1 for the special
987 namei_dec(IHandle_t * ih, Inode ino, int p1)
994 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
996 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
998 /* Verify this is the right file. */
999 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
1001 namei_HandleToName(&name, tmp);
1006 errno = OS_ERROR(ENOENT);
1010 if (CheckOGM(&name, fdP, p1) < 0) {
1011 FDH_REALLYCLOSE(fdP);
1013 errno = OS_ERROR(EINVAL);
1017 /* If it's the link table itself, decrement the link count. */
1018 if (type == VI_LINKTABLE) {
1019 if ((count = namei_GetLinkCount(fdP, (Inode) 0, 1, 0, 1)) < 0) {
1020 FDH_REALLYCLOSE(fdP);
1026 if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) <
1028 FDH_REALLYCLOSE(fdP);
1034 FDH_REALLYCLOSE(fdP);
1040 if ((code = OS_UNLINK(name.n_path)) == 0) {
1041 if (type == VI_LINKTABLE) {
1042 /* Try to remove directory. If it fails, that's ok.
1043 * Salvage will clean up.
1045 (void)namei_RemoveDataDirectories(&name);
1048 FDH_REALLYCLOSE(fdP);
1051 /* Get a file descriptor handle for this Inode */
1057 if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0) {
1058 FDH_REALLYCLOSE(fdP);
1064 if (namei_SetLinkCount(fdP, ino, count, 1) < 0) {
1065 FDH_REALLYCLOSE(fdP);
1070 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
1071 Log("Warning: Lost ref on ihandle dev %d vid %d ino %lld\n",
1072 th->ih_dev, th->ih_vid, (afs_int64)th->ih_ino);
1075 /* If we're less than 0, someone presumably unlinked;
1076 don't bother setting count to 0, but we need to drop a lock */
1077 if (namei_SetLinkCount(fdP, ino, 0, 1) < 0) {
1078 FDH_REALLYCLOSE(fdP);
1084 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
1086 namei_HandleToName(&name, th);
1088 code = OS_UNLINK(name.n_path);
1097 namei_inc(IHandle_t * h, Inode ino, int p1)
1103 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1104 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1105 if (type != VI_LINKTABLE)
1110 /* Get a file descriptor handle for this Inode */
1116 if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0)
1121 errno = OS_ERROR(EINVAL);
1125 if (namei_SetLinkCount(fdP, ino, count, 1) < 0)
1129 FDH_REALLYCLOSE(fdP);
1136 #ifndef AFS_NT40_ENV
1138 namei_replace_file_by_hardlink(IHandle_t *hLink, IHandle_t *hTarget)
1144 /* Convert handle to file name. */
1145 namei_HandleToName(&nameLink, hLink);
1146 namei_HandleToName(&nameTarget, hTarget);
1148 OS_UNLINK(nameLink.n_path);
1149 code = link(nameTarget.n_path, nameLink.n_path);
1154 namei_copy_on_write(IHandle_t *h)
1160 struct afs_stat_st tstat;
1163 namei_HandleToName(&name, h);
1164 if (afs_stat(name.n_path, &tstat) < 0)
1166 if (tstat.st_nlink > 1) { /* do a copy on write */
1175 snprintf(path, sizeof(path), "%s-tmp", name.n_path);
1176 fd = OS_OPEN(path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
1177 if (fd == INVALID_FD) {
1188 size = tstat.st_size;
1191 tlen = size > 8192 ? 8192 : size;
1192 if (FDH_PREAD(fdP, buf, tlen, offset) != tlen)
1194 if (OS_WRITE(fd, buf, tlen) != tlen)
1200 FDH_REALLYCLOSE(fdP);
1205 OS_UNLINK(name.n_path);
1206 code = rename(path, name.n_path);
1213 /************************************************************************
1214 * File Name Structure
1215 ************************************************************************
1217 * Each AFS file needs a unique name and it needs to be findable with
1218 * minimal lookup time. Note that the constraint on the number of files and
1219 * directories in a volume is the size of the vnode index files and the
1220 * max file size AFS supports (for internal files) of 2^31. Since a record
1221 * in the small vnode index file is 64 bytes long, we can have at most
1222 * (2^31)/64 or 33554432 files. A record in the large index file is
1223 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
1224 * Another layout parameter is that there is roughly a 16 to 1 ratio between
1225 * the number of files and the number of directories.
1227 * Using this information we can see that a layout of 256 directories, each
1228 * with 512 subdirectories and each of those having 512 files gives us
1229 * 256*512*512 = 67108864 AFS files and directories.
1231 * The volume, vnode, uniquifier and data version, as well as the tag
1232 * are required, either for finding the file or for salvaging. It's best to
1233 * restrict the name to something that can be mapped into 64 bits so the
1234 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
1235 * is used to distinguish between different versions of the same file
1236 * which are currently in the RW and clones of a volume. See "Link Table
1237 * Organization" below for more information on the tag. The tag is
1238 * required in the name of the file to ensure a unique name.
1240 * ifdef AFS_NT40_ENV
1241 * The data for each volume group is in a separate directory. The name of the
1242 * volume is of the form: Vol_NNNNNN.data, where NNNNNN is a base 32
1243 * representation of the RW volume ID (even where the RO is the only volume
1244 * on the partition). Below that are separate subdirectories for the
1245 * AFS directories and special files. There are also 16 directories for files,
1246 * hashed on the low 5 bits (recall bit0 is always 0) of the vnode number.
1247 * These directories are named:
1248 * A - P - 16 file directories.
1249 * Q ----- data directory
1250 * R ----- special files directory
1252 * The vnode is hashed into the directory using the low bits of the
1255 * The format of a file name for a regular file is:
1256 * Y:\Vol_NNNNNN.data\X\V_IIIIII.J
1257 * Y - partition encoded as drive letter, starting with D
1258 * NNNNNN - base 32 encoded volume number of RW volume
1259 * X - hash directory, as above
1260 * IIIIII - base 32 encoded vnode number
1261 * J - base 32 encoded tag
1263 * uniq is stored in the dwHighDateTime creation time field
1264 * dv is stored in the dwLowDateTime creation time field
1266 * Special inodes are always in the R directory, as above, and are
1268 * True child volid is stored in the dwHighDateTime creation time field
1269 * vnode number is always -1 (Special)
1270 * type is the IIIIII part of the filename
1271 * uniq is the J part of the filename
1272 * parent volume id is implied in the containing directory
1275 * We can store data in the uid, gid and mode bits of the files, provided
1276 * the directories have root only access. This gives us 15 bits for each
1277 * of uid and gid (GNU chown considers 65535 to mean "don't change").
1278 * There are 9 available mode bits. Adn we need to store a total of
1279 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
1280 * or 131 bits somewhere.
1282 * The format of a file name for a regular file is:
1283 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
1284 * V1 - low 8 bits of RW volume id
1285 * V2 - all bits of RW volume id
1286 * AA - high 8 bits of vnode number.
1287 * BB - next 9 bits of vnode number.
1288 * <tag><uniq><vno> - file name
1290 * Volume special files are stored in a separate directory:
1291 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
1294 * The vnode is hashed into the directory using the high bits of the
1295 * vnode number. This is so that consecutively created vnodes are in
1296 * roughly the same area on the disk. This will at least be optimal if
1297 * the user is creating many files in the same AFS directory. The name
1298 * should be formed so that the leading characters are different as quickly
1299 * as possible, leading to faster discards of incorrect matches in the
1307 /************************************************************************
1308 * Link Table Organization
1309 ************************************************************************
1311 * The link table volume special file is used to hold the link counts that
1312 * are held in the inodes in inode based AFS vice filesystems. For user
1313 * space access, the link counts are being kept in a separate
1314 * volume special file. The file begins with the usual version stamp
1315 * information and is then followed by one row per vnode number. vnode 0
1316 * is used to hold the link count of the link table itself. That is because
1317 * the same link table is shared among all the volumes of the volume group
1318 * and is deleted only when the last volume of a volume group is deleted.
1320 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
1321 * link count from 0 through 7. Each colume represents a unique instance of
1322 * that vnode. Say we have a file shared between the RW and a RO and a
1323 * different version of the file (or a different uniquifer) for the BU volume.
1324 * Then one column would be holding the link count of 2 for the RW and RO
1325 * and a different column would hold the link count of 1 for the BU volume.
1326 * # ifdef AFS_NT40_ENV
1327 * The column used is determined for NT by the uniquifier tag applied to
1328 * generate a unique file name in the NTFS namespace. The file name is
1329 * of the form "V_<vno>.<tag>" . And the <tag> is also the column number
1330 * in the link table.
1332 * Note that we allow only 5 volumes per file, giving 15 bits used in the
1336 #define LINKTABLE_WIDTH 2
1337 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
1340 * compute namei link table file and bit offset from inode number.
1342 * @param[in] ino inode number
1343 * @param[out] offset link table file offset
1344 * @param[out] index bit offset within 2-byte record
1349 namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
1351 int toff = (int)(ino & NAMEI_VNODEMASK);
1352 int tindex = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1354 *offset = (afs_foff_t) ((toff << LINKTABLE_SHIFT) + 8); /* * 2 + sizeof stamp */
1355 *index = (tindex << 1) + tindex;
1358 #ifdef AFS_PTHREAD_ENV
1359 /* XXX do static initializers work for WINNT/pthread? */
1360 pthread_mutex_t _namei_glc_lock = PTHREAD_MUTEX_INITIALIZER;
1361 #define NAMEI_GLC_LOCK MUTEX_ENTER(&_namei_glc_lock)
1362 #define NAMEI_GLC_UNLOCK MUTEX_EXIT(&_namei_glc_lock)
1363 #else /* !AFS_PTHREAD_ENV */
1364 #define NAMEI_GLC_LOCK
1365 #define NAMEI_GLC_UNLOCK
1366 #endif /* !AFS_PTHREAD_ENV */
1369 * get the link count of an inode.
1371 * @param[in] h namei link count table file handle
1372 * @param[in] ino inode number for which we are requesting a link count
1373 * @param[in] lockit if asserted, return with lock held on link table file
1374 * @param[in] fixup if asserted, write 1 to link count when read() returns
1376 * @param[in] nowrite return success on zero byte read or ZLC
1378 * @post if lockit asserted and lookup was successful, will return with write
1379 * lock on link table file descriptor
1381 * @return link count
1382 * @retval -1 namei link table i/o error
1387 namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite)
1389 unsigned short row = 0;
1394 /* there's no linktable yet. the salvager will create one later */
1395 if (h->fd_fd == INVALID_FD && fixup)
1397 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1400 if (FDH_LOCKFILE(h, offset) != 0)
1404 rc = FDH_PREAD(h, (char*)&row, sizeof(row), offset);
1405 if ((rc == 0 || !((row >> index) & NAMEI_TAGMASK)) && fixup && nowrite)
1407 if (rc == 0 && fixup) {
1409 * extend link table and write a link count of 1 for ino
1411 * in order to make MT-safe, truncation (extension really)
1412 * must happen under a mutex
1415 if (FDH_SIZE(h) >= offset+sizeof(row)) {
1417 goto bad_getLinkByte;
1419 FDH_TRUNC(h, offset+sizeof(row));
1421 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
1424 if (rc != sizeof(row)) {
1425 goto bad_getLinkByte;
1428 if (fixup && !((row >> index) & NAMEI_TAGMASK)) {
1432 * in order to make this mt-safe, we need to do the read-modify-write
1433 * under a mutex. thus, we repeat the read inside the lock.
1436 rc = FDH_PREAD(h, (char *)&row, sizeof(row), offset);
1437 if (rc == sizeof(row)) {
1439 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
1442 if (rc != sizeof(row))
1443 goto bad_getLinkByte;
1446 return (int)((row >> index) & NAMEI_TAGMASK);
1450 FDH_UNLOCKFILE(h, offset);
1455 namei_SetNonZLC(FdHandle_t * h, Inode ino)
1457 return namei_GetLinkCount(h, ino, 0, 1, 0);
1460 /* Return a free column index for this vnode. */
1462 GetFreeTag(IHandle_t * ih, int vno)
1476 /* Only one manipulates at a time. */
1477 if (FDH_LOCKFILE(fdP, offset) != 0) {
1478 FDH_REALLYCLOSE(fdP);
1482 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
1484 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1485 if (nBytes != sizeof(row)) {
1491 /* Now find a free column in this row and claim it. */
1492 for (col = 0; col < NAMEI_MAXVOLS; col++) {
1493 coldata = 7 << (col * 3);
1494 if ((row & coldata) == 0)
1497 if (col >= NAMEI_MAXVOLS) {
1502 coldata = 1 << (col * 3);
1505 if (FDH_PWRITE(fdP, (char *)&row, sizeof(row), offset) != sizeof(row)) {
1509 FDH_UNLOCKFILE(fdP, offset);
1510 FDH_REALLYCLOSE(fdP);
1514 FDH_UNLOCKFILE(fdP, offset);
1515 FDH_REALLYCLOSE(fdP);
1521 /* namei_SetLinkCount
1522 * If locked is set, assume file is locked. Otherwise, lock file before
1523 * proceeding to modify it.
1526 namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
1532 ssize_t nBytes = -1;
1534 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1537 if (FDH_LOCKFILE(fdP, offset) != 0) {
1542 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1543 if (nBytes != sizeof(row)) {
1545 errno = OS_ERROR(EBADF);
1546 goto bad_SetLinkCount;
1551 bytesRead = 7 << index;
1553 row &= (unsigned short)~bytesRead;
1554 row |= (unsigned short)count;
1556 if (FDH_PWRITE(fdP, (char *)&row, sizeof(short), offset) != sizeof(short)) {
1557 errno = OS_ERROR(EBADF);
1558 goto bad_SetLinkCount;
1566 FDH_UNLOCKFILE(fdP, offset);
1568 /* disallowed above 7, so... */
1573 /* ListViceInodes - write inode data to a results file. */
1574 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1575 unsigned int volid);
1576 static int DecodeVolumeName(char *name, unsigned int *vid);
1577 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
1578 int (*write_fun) (FD_t,
1579 struct ViceInodeInfo *,
1580 char *, char *), FD_t fp,
1581 int (*judgeFun) (struct ViceInodeInfo *,
1582 afs_uint32 vid, void *),
1583 afs_uint32 singleVolumeNumber, void *rock);
1588 * Write the inode data to the results file.
1590 * Returns -2 on error, 0 on success.
1592 * This is written as a callback simply so that other listing routines
1593 * can use the same inode reading code.
1596 WriteInodeInfo(FD_t fp, struct ViceInodeInfo *info, char *dir, char *name)
1599 n = OS_WRITE(fp, info, sizeof(*info));
1600 return (n == sizeof(*info)) ? 0 : -2;
1604 int mode_errors; /* Number of errors found in mode bits on directories. */
1606 VerifyDirPerms(char *path)
1608 struct afs_stat_st status;
1610 if (afs_stat(path, &status) < 0) {
1611 Log("Unable to stat %s. Please manually verify mode bits for this"
1612 " directory\n", path);
1614 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1620 * Fill the results file with the requested inode information.
1622 * This code optimizes single volume salvages by just looking at that one
1623 * volume's directory.
1625 * @param[in] devname device name string
1626 * @param[in] moutnedOn vice partition mount point
1627 * @param[in] resultFile result file in which to write inode
1628 * metadata. If NULL, write routine is not
1630 * @param[in] judgeInode filter function pointer. if not NULL, only
1631 * inodes for which this routine returns non-
1632 * zero will be written to the results file.
1633 * @param[in] singleVolumeNumber volume id filter
1634 * @param[out] forcep always set to 0 for namei impl
1635 * @param[in] forceR not used by namei impl
1636 * @param[in] wpath not used by namei impl
1637 * @param[in] rock opaque pointer passed to judgeInode
1639 * @return operation status
1641 * @retval -1 complete failure, salvage should terminate.
1642 * @retval -2 not enough space on partition, salvager has error message
1646 ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
1647 int (*judgeInode) (struct ViceInodeInfo * info, afs_uint32 vid, void *rock),
1648 afs_uint32 singleVolumeNumber, int *forcep, int forceR, char *wpath,
1653 *forcep = 0; /* no need to salvage until further notice */
1655 /* Verify protections on directories. */
1657 VerifyDirPerms(mountedOn);
1660 namei_ListAFSFiles(mountedOn, WriteInodeInfo, inodeFile, judgeInode,
1661 singleVolumeNumber, rock);
1663 if (inodeFile == INVALID_FD)
1670 if (OS_SYNC(inodeFile) == -1) {
1671 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1676 * Paranoia: check that the file is really the right size
1678 if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
1679 Log("Wrong size (%d instead of %lu) in inode file for %s\n",
1680 (int) OS_SIZE(inodeFile),
1681 (long unsigned int) ninodes * sizeof(struct ViceInodeInfo),
1690 * Collect all the matching AFS files on the drive.
1691 * If singleVolumeNumber is non-zero, just return files for that volume.
1693 * @param[in] dev vice partition path
1694 * @param[in] writeFun function pointer to a function which
1695 * writes inode information to FILE fp
1696 * @param[in] fp file stream where inode metadata is sent
1697 * @param[in] judgeFun filter function pointer. if not NULL,
1698 * only entries for which a non-zero value
1699 * is returned are written to fp
1700 * @param[in] singleVolumeNumber volume id filter. if nonzero, only
1701 * process files for that specific volume id
1702 * @param[in] rock opaque pointer passed into writeFun and
1705 * @return operation status
1707 * @retval >=0 number of matching files found
1710 namei_ListAFSFiles(char *dev,
1711 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1714 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1715 afs_uint32 singleVolumeNumber, void *rock)
1722 #ifndef AFS_NT40_ENV
1728 static void FreeZLCList(void);
1731 memset((void *)&ih, 0, sizeof(IHandle_t));
1733 ih.ih_dev = nt_DriveToDev(dev);
1735 ih.ih_dev = volutil_GetPartitionID(dev);
1738 if (singleVolumeNumber) {
1739 ih.ih_vid = singleVolumeNumber;
1740 namei_HandleToVolDir(&name, &ih);
1742 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1743 singleVolumeNumber, rock);
1747 /* Find all volume data directories and descend through them. */
1748 namei_HandleToInodeDir(&name, &ih);
1750 dirp1 = opendir(name.n_path);
1753 while ((dp1 = readdir(dirp1))) {
1755 /* Heirarchy is one level on Windows */
1756 if (!DecodeVolumeName(dp1->d_name, &ih.ih_vid)) {
1758 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1762 if (*dp1->d_name == '.')
1764 snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", name.n_path,
1766 dirp2 = opendir(path2);
1768 while ((dp2 = readdir(dirp2))) {
1769 if (*dp2->d_name == '.')
1771 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1773 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1790 static void AddToZLCDeleteList(char dir, char *name);
1791 static void DeleteZLCFiles(char *path);
1795 * examine a namei volume special file.
1797 * @param[in] path1 volume special directory path
1798 * @param[in] dname directory entry name
1799 * @param[in] myIH inode handle to volume directory
1800 * @param[out] linkHandle namei link count fd handle. if
1801 * the inode in question is the link
1802 * table, then the FdHandle is populated
1803 * @param[in] writeFun metadata write function pointer
1804 * @param[in] fp file pointer where inode metadata
1805 * is written by (*writeFun)()
1806 * @param[in] judgeFun inode filter function pointer. if
1807 * not NULL, only inodes for which this
1808 * function returns non-zero are recorded
1809 * into fp by writeFun
1810 * @param[in] singleVolumeNumer volume id filter. if non-zero, only
1811 * inodes associated with this volume id
1812 * are recorded by writeFun
1813 * @param[in] rock opaque pointer passed to writeFun and
1816 * @return operation status
1817 * @retval 1 count this inode
1818 * @retval 0 don't count this inode
1819 * @retval -1 failure
1824 _namei_examine_special(char * path1,
1827 FdHandle_t * linkHandle,
1828 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1831 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1832 int singleVolumeNumber,
1836 struct ViceInodeInfo info;
1837 afs_uint32 inode_vgid;
1839 if (DecodeInode(path1, dname, &info, myIH->ih_vid) < 0) {
1845 inode_vgid = myIH->ih_vid;
1847 inode_vgid = (info.inodeNumber >> NAMEI_UNIQSHIFT) & NAMEI_UNIQMASK;
1850 if (info.u.param[2] != VI_LINKTABLE) {
1852 } else if ((info.u.param[0] != myIH->ih_vid) ||
1853 (inode_vgid != myIH->ih_vid)) {
1854 /* VGID encoded in linktable filename and/or OGM data isn't
1855 * consistent with VGID encoded in namei path */
1856 Log("namei_ListAFSSubDirs: warning: inconsistent linktable "
1857 "filename \"%s" OS_DIRSEP "%s\"; salvager will delete it "
1858 "(dir_vgid=%u, inode_vgid=%u, ogm_vgid=%u)\n",
1859 path1, dname, myIH->ih_vid,
1860 (unsigned int)inode_vgid,
1864 /* Open this handle */
1865 snprintf(path2, sizeof(path2),
1866 "%s" OS_DIRSEP "%s", path1, dname);
1867 linkHandle->fd_fd = OS_OPEN(path2, Testing ? O_RDONLY : O_RDWR, 0666);
1869 namei_GetLinkCount(linkHandle, (Inode) 0, 1, 1, Testing);
1873 (*judgeFun) (&info, singleVolumeNumber, rock)) {
1874 ret = (*writeFun) (fp, &info, path1, dname);
1876 Log("_namei_examine_special: writeFun returned %d\n", ret);
1888 * examine a namei file.
1890 * @param[in] path3 volume special directory path
1891 * @param[in] dname directory entry name
1892 * @param[in] myIH inode handle to volume directory
1893 * @param[in] linkHandle namei link count fd handle.
1894 * @param[in] writeFun metadata write function pointer
1895 * @param[in] fp file pointer where inode metadata
1896 * is written by (*writeFun)()
1897 * @param[in] judgeFun inode filter function pointer. if
1898 * not NULL, only inodes for which this
1899 * function returns non-zero are recorded
1900 * into fp by writeFun
1901 * @param[in] singleVolumeNumer volume id filter. if non-zero, only
1902 * inodes associated with this volume id
1903 * are recorded by writeFun
1904 * @param[in] rock opaque pointer passed to writeFun and
1907 * @return operation status
1908 * @retval 1 count this inode
1909 * @retval 0 don't count this inode
1910 * @retval -1 failure
1911 * @retval -2 request ZLC delete
1916 _namei_examine_reg(char * path3,
1919 FdHandle_t * linkHandle,
1920 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1923 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1924 int singleVolumeNumber,
1928 struct ViceInodeInfo info;
1930 int dirl; /* Windows-only (one level hash dir) */
1933 if (DecodeInode(path3, dname, &info, myIH->ih_vid) < 0) {
1938 namei_GetLinkCount(linkHandle,
1939 info.inodeNumber, 1, 1, Testing);
1940 if (info.linkCount == 0) {
1942 Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n", path3, dname);
1943 #ifdef AFS_SALSRV_ENV
1944 /* defer -- the AddToZLCDeleteList() interface is not MT-safe */
1946 #else /* !AFS_SALSRV_ENV */
1947 dirl = path3[strlen(path3)-1];
1948 AddToZLCDeleteList((char)dirl, dname);
1949 #endif /* !AFS_SALSRV_ENV */
1950 #else /* !DELETE_ZLC */
1951 Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path3,
1958 (*judgeFun) (&info, singleVolumeNumber, rock)) {
1959 ret = (*writeFun) (fp, &info, path3, dname);
1961 Log("_namei_examine_reg: writeFun returned %d\n", ret);
1973 * listsubdirs work queue node.
1975 struct listsubdirs_work_node {
1976 #ifdef AFS_SALSRV_ENV
1977 int *error; /**< *error set if an error was
1978 * encountered in any listsubdirs
1982 IHandle_t * IH; /**< volume directory handle */
1983 FdHandle_t *linkHandle; /**< namei link count fd handle. when
1984 * examinining the link table special
1985 * inode, this will be pointed at the
1988 FD_t fp; /**< file pointer for writeFun */
1990 /** function which will write inode metadata to fp */
1991 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *, char *);
1993 /** inode filter function */
1994 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *);
1995 int singleVolumeNumber; /**< volume id filter */
1996 void * rock; /**< pointer passed to writeFun and judgeFun */
1997 int code; /**< return code from examine function */
1998 int special; /**< asserted when this is a volume
2003 * simple wrapper around _namei_examine_special and _namei_examine_reg.
2005 * @param[in] work the struct listsubdirs_work_node for the associated
2006 * "list subdirs" job
2007 * @param[in] dir the directory to examine
2008 * @param[in] filename the filename in 'dir' to examine
2010 * @return operation status
2011 * @retval 1 count this inode
2012 * @retval 0 don't count this inode
2013 * @retval -1 failure
2016 _namei_examine_file(const struct listsubdirs_work_node *work, char *dir,
2019 if (work->special) {
2020 return _namei_examine_special(dir, filename, work->IH,
2021 work->linkHandle, work->writeFun, work->fp,
2022 work->judgeFun, work->singleVolumeNumber,
2025 return _namei_examine_reg(dir, filename, work->IH,
2026 work->linkHandle, work->writeFun, work->fp,
2027 work->judgeFun, work->singleVolumeNumber,
2033 #ifdef AFS_SALSRV_ENV
2034 /** @addtogroup afs_vol_salsrv_pario */
2038 * arguments for the _namei_examine_file_cbk callback function.
2040 struct listsubdirs_args {
2041 const struct listsubdirs_work_node *work; /**< arguments that are the same
2042 * for all invocations of
2043 * _namei_examine_file_cbk, in
2045 int *result; /**< where we can store the return code of _namei_examine_file */
2047 char dir[512]; /**< directory to examine */
2048 char filename[256]; /**< filename in 'dir' to examine */
2052 * a node in the list of results of listsubdir jobs.
2054 struct listsubdirs_result {
2056 int inodes; /**< return value from _namei_examine_file */
2060 * clean up a list of 'struct listsubdirs_result's and interpret the results.
2062 * @param[in] resultlist a list of 'struct listsubdirs_result's
2064 * @return number of inodes found
2068 _namei_listsubdirs_cleanup_results(struct rx_queue *resultlist)
2070 struct listsubdirs_result *res, *nres;
2073 for(queue_Scan(resultlist, res, nres, listsubdirs_result)) {
2075 /* noop, retain erroneous error code */
2076 } else if (res->inodes < 0) {
2091 * wait for the spawned listsubdirs jobs to finish, and return how many inodes
2094 * @param[in] queue queue to wait to finish
2095 * @param[in] resultlist list of 'struct listsubdirs_result's that the queued
2096 * jobs are storing their results in
2098 * @return number of inodes found
2102 _namei_listsubdirs_wait(struct afs_work_queue *queue, struct rx_queue *resultlist)
2106 code = afs_wq_wait_all(queue);
2111 return _namei_listsubdirs_cleanup_results(resultlist);
2115 * work queue entry point for examining namei files.
2117 * @param[in] queue pointer to struct Vwork_queue
2118 * @param[in] node pointer to struct Vwork_queue_node
2119 * @param[in] queue_rock opaque pointer to struct salsrv_pool_state
2120 * @param[in] node_rock opaque pointer to struct listsubdirs_work_node
2121 * @param[in] caller_rock opaque pointer to struct salsrv_worker_thread_state
2123 * @return operation status
2125 * @see Vwork_queue_callback_func_t
2130 _namei_examine_file_cbk(struct afs_work_queue *queue,
2131 struct afs_work_queue_node *node,
2137 struct listsubdirs_args *args = node_rock;
2138 const struct listsubdirs_work_node * work = args->work;
2139 char *dir = args->dir;
2140 char *filename = args->filename;
2142 code = _namei_examine_file(work, dir, filename);
2144 *(args->result) = code;
2148 /* we've errored, so no point in letting more jobs continue */
2149 afs_wq_shutdown(queue);
2155 static pthread_once_t wq_once = PTHREAD_ONCE_INIT;
2156 static pthread_key_t wq_key;
2159 * create the wq_key key for storing a work queue.
2162 _namei_wq_keycreate(void)
2164 osi_Assert(pthread_key_create(&wq_key, NULL) == 0);
2168 * set the work queue for this thread to use for backgrounding namei ops.
2170 * The work queue will be used in ListAFSSubdirs (called indirectly by
2171 * ListViceInodes) to examine files in parallel.
2173 * @param[in] wq the work queue to use
2176 namei_SetWorkQueue(struct afs_work_queue *wq)
2178 osi_Assert(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
2180 osi_Assert(pthread_setspecific(wq_key, wq) == 0);
2184 * enqueue an examine file work unit.
2186 * @param[in] work the _namei_examine_file arguments that are common to
2187 * all callers within the same ListAFSFiles operation
2188 * @param[in] dir the specific directory to look at (string will be
2189 * copied; can be stack/temporary memory)
2190 * @param[in] filename the filename to look at (string will be copied; can be
2191 * stack/temporary memory)
2192 * @param[in] wq work queue to enqueue this work unit to
2193 * @param[in] resultlist the list to append the 'struct listsubdirs_result' to
2194 * for the enqueued work unit
2196 * @return operation status
2198 * @retval -1 fatal error
2200 * @note errors MUST be indicated by a -1 error code and nothing else, to be
2201 * compatible with _namei_examine_reg and _namei_examine_special
2206 _namei_examine_file_spawn(const struct listsubdirs_work_node *work,
2207 const char *dir, const char *filename,
2208 struct afs_work_queue *wq,
2209 struct rx_queue *resultlist)
2212 struct listsubdirs_args *args = NULL;
2213 struct listsubdirs_result *result = NULL;
2214 struct afs_work_queue_node *node = NULL;
2215 struct afs_work_queue_add_opts opts;
2217 args = malloc(sizeof(*args));
2223 result = malloc(sizeof(*result));
2224 if (result == NULL) {
2229 code = afs_wq_node_alloc(&node);
2234 code = afs_wq_node_set_detached(node);
2241 args->result = &result->inodes;
2242 strlcpy(args->dir, dir, sizeof(args->dir));
2243 strlcpy(args->filename, filename, sizeof(args->filename));
2245 code = afs_wq_node_set_callback(node,
2246 &_namei_examine_file_cbk,
2254 afs_wq_add_opts_init(&opts);
2257 code = afs_wq_add(wq, node, &opts);
2264 queue_Append(resultlist, result);
2269 afs_wq_node_put(node);
2285 #else /* !AFS_SALSRV_ENV */
2286 # define _namei_examine_file_spawn(work, dir, file, wq, resultlist) \
2287 _namei_examine_file(work, dir, file)
2288 #endif /* !AFS_SALSRV_ENV */
2291 * traverse and check inodes.
2293 * @param[in] dirIH volume group directory handle
2294 * @param[in] writeFun function pointer which will write inode
2295 * metadata to FILE stream fp
2296 * @param[in] fp file stream where inode metadata gets
2298 * @param[in] judgeFun inode filter function. if not NULL, only
2299 * inodes for which the filter returns non-zero
2300 * will be written out by writeFun
2301 * @param[in] singleVolumeNumber volume id filter. only inodes matching this
2302 * filter are written out by writeFun
2303 * @param[in] rock opaque pointer passed to judgeFun and writeFun
2305 * @return operation status
2307 * @retval >=0 number of matching inodes found
2309 * @todo the salsrv implementation may consume a lot of
2310 * memory for a large volume. at some point we should
2311 * probably write a background thread to asynchronously
2312 * clean up the resultlist nodes to reduce memory footprint
2317 namei_ListAFSSubDirs(IHandle_t * dirIH,
2318 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
2321 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
2322 afs_uint32 singleVolumeNumber, void *rock)
2324 int code = 0, ret = 0;
2325 IHandle_t myIH = *dirIH;
2327 char path1[512], path3[512];
2329 #ifndef AFS_NT40_ENV
2334 struct dirent *dp1, *dp3;
2335 FdHandle_t linkHandle;
2337 struct listsubdirs_work_node work;
2338 #ifdef AFS_SALSRV_ENV
2340 struct afs_work_queue *wq;
2342 struct rx_queue resultlist;
2345 namei_HandleToVolDir(&name, &myIH);
2346 strlcpy(path1, name.n_path, sizeof(path1));
2348 /* Do the directory containing the special files first to pick up link
2351 (void)strcat(path1, OS_DIRSEP);
2352 (void)strcat(path1, NAMEI_SPECDIR);
2354 linkHandle.fd_fd = INVALID_FD;
2355 #ifdef AFS_SALSRV_ENV
2356 osi_Assert(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
2358 wq = pthread_getspecific(wq_key);
2364 queue_Init(&resultlist);
2367 memset(&work, 0, sizeof(work));
2368 work.linkHandle = &linkHandle;
2371 work.writeFun = writeFun;
2372 work.judgeFun = judgeFun;
2373 work.singleVolumeNumber = singleVolumeNumber;
2376 #ifdef AFS_SALSRV_ENV
2377 work.error = &error;
2380 dirp1 = opendir(path1);
2382 while ((dp1 = readdir(dirp1))) {
2383 if (*dp1->d_name == '.')
2386 #ifdef AFS_SALSRV_ENV
2392 #endif /* AFS_SALSRV_ENV */
2394 code = _namei_examine_file_spawn(&work, path1, dp1->d_name, wq, &resultlist);
2404 /* count this inode */
2405 #ifndef AFS_SALSRV_ENV
2414 #ifdef AFS_SALSRV_ENV
2415 /* effectively a barrier */
2416 code = _namei_listsubdirs_wait(wq, &resultlist);
2417 if (code < 0 || error) {
2425 if (linkHandle.fd_fd == INVALID_FD) {
2426 Log("namei_ListAFSSubDirs: warning: VG %u does not have a link table; "
2427 "salvager will recreate it.\n", dirIH->ih_vid);
2430 /* Now run through all the other subdirs */
2431 namei_HandleToVolDir(&name, &myIH);
2432 strlcpy(path1, name.n_path, sizeof(path1));
2436 dirp1 = opendir(path1);
2438 while ((dp1 = readdir(dirp1))) {
2439 #ifndef AFS_NT40_ENV
2440 if (*dp1->d_name == '.')
2443 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
2446 #ifndef AFS_NT40_ENV /* This level missing on Windows */
2447 /* Now we've got a next level subdir. */
2448 snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s",
2449 path1, dp1->d_name);
2450 dirp2 = opendir(path2);
2452 while ((dp2 = readdir(dirp2))) {
2453 if (*dp2->d_name == '.')
2456 /* Now we've got to the actual data */
2457 snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
2458 path2, dp2->d_name);
2460 /* Now we've got to the actual data */
2461 snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
2462 path1, dp1->d_name);
2464 dirp3 = opendir(path3);
2466 while ((dp3 = readdir(dirp3))) {
2467 #ifndef AFS_NT40_ENV
2468 if (*dp3->d_name == '.')
2472 #ifdef AFS_SALSRV_ENV
2475 #ifndef AFS_NT40_ENV
2482 #endif /* AFS_SALSRV_ENV */
2484 code = _namei_examine_file_spawn(&work, path3,
2491 #ifndef AFS_NT40_ENV
2499 #ifndef AFS_SALSRV_ENV
2507 #ifndef AFS_NT40_ENV /* This level missing on Windows */
2516 #ifdef AFS_SALSRV_ENV
2517 /* effectively a barrier */
2518 code = _namei_listsubdirs_wait(wq, &resultlist);
2519 if (code < 0 || error) {
2529 /* Then why does this directory exist? Blow it away. */
2530 namei_HandleToVolDir(&name, dirIH);
2531 namei_RemoveDataDirectories(&name);
2535 #ifdef AFS_SALSRV_ENV
2537 afs_wq_wait_all(wq);
2539 _namei_listsubdirs_cleanup_results(&resultlist);
2541 if (linkHandle.fd_fd != INVALID_FD)
2542 OS_CLOSE(linkHandle.fd_fd);
2554 DecodeVolumeName(char *name, unsigned int *vid)
2556 /* Name begins with "Vol_" and ends with .data. See nt_HandleToVolDir() */
2563 if (strncmp(name, "Vol_", 4))
2565 if (strcmp(name + len - 5, ".data"))
2568 stmp[len - 5] = '\0';
2569 *vid = base32_to_int(stmp + 4);
2574 DecodeVolumeName(char *name, unsigned int *vid)
2576 if (strlen(name) < 1)
2578 *vid = (unsigned int)flipbase64_to_int64(name);
2586 * Get the inode number from the name.
2591 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
2596 WIN32_FIND_DATA data;
2600 FdHandle_t linkHandle;
2603 snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
2605 dirH = FindFirstFileEx(fpath, FindExInfoStandard, &data,
2606 FindExSearchNameMatch, NULL,
2607 FIND_FIRST_EX_CASE_SENSITIVE);
2608 if (dirH == INVALID_HANDLE_VALUE)
2611 (void)strcpy(stmp, name);
2612 s = strrchr(stmp, '_');
2616 t = strrchr(s, '.');
2621 vno = base32_to_int(s); /* type for special files */
2622 tag = base32_to_int(t+1);
2623 info->inodeNumber = ((Inode) tag) << NAMEI_TAGSHIFT;
2624 info->inodeNumber |= vno;
2625 info->byteCount = data.nFileSizeLow;
2627 dirl = dpath[strlen(dpath)-1];
2628 if (dirl == NAMEI_SPECDIRC) { /* Special inode. */
2629 info->inodeNumber |= NAMEI_INODESPECIAL;
2630 info->u.param[0] = data.ftCreationTime.dwHighDateTime;
2631 info->u.param[1] = data.ftCreationTime.dwLowDateTime;
2632 info->u.param[2] = vno; /* type */
2633 info->u.param[3] = volid;
2634 if (vno != VI_LINKTABLE)
2635 info->linkCount = 1;
2637 /* Open this handle */
2639 (void)sprintf(lpath, "%s" OS_DIRSEP "%s", fpath, data.cFileName);
2640 linkHandle.fd_fd = OS_OPEN(lpath, O_RDONLY, 0666);
2642 namei_GetLinkCount(&linkHandle, (Inode) 0, 0, 0, 0);
2646 namei_GetLinkCount(&linkHandle, info->inodeNumber, 0, 0, 0);
2647 if (info->linkCount == 0) {
2649 Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n",
2650 fpath, data.cFileName);
2651 AddToZLCDeleteList(dirl, data.cFileName);
2653 Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path,
2657 info->u.param[2] = data.ftCreationTime.dwHighDateTime;
2658 info->u.param[3] = data.ftCreationTime.dwLowDateTime;
2659 info->u.param[1] = vno;
2660 info->u.param[0] = volid;
2667 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
2671 struct afs_stat_st status;
2673 lb64_string_t check;
2675 snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
2677 if (afs_stat(fpath, &status) < 0) {
2681 info->byteCount = status.st_size;
2682 info->inodeNumber = (Inode) flipbase64_to_int64(name);
2684 int64_to_flipbase64(check, info->inodeNumber);
2685 if (strcmp(name, check))
2688 GetOGMFromStat(&status, &parm, &tag);
2689 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
2690 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
2691 info->u.param[0] = parm;
2692 info->u.param[1] = -1;
2693 info->u.param[2] = tag;
2694 info->u.param[3] = volid;
2696 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
2697 info->u.param[0] = volid;
2698 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
2699 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
2700 & (Inode) NAMEI_UNIQMASK);
2701 info->u.param[3] = parm;
2708 * Convert the VolumeInfo file from RO to RW
2709 * this routine is called by namei_convertROtoRWvolume()
2712 #ifdef FSSYNC_BUILD_CLIENT
2714 convertVolumeInfo(FD_t fdr, FD_t fdw, afs_uint32 vid)
2716 struct VolumeDiskData vd;
2719 if (OS_READ(fdr, &vd, sizeof(struct VolumeDiskData)) !=
2720 sizeof(struct VolumeDiskData)) {
2721 Log("1 convertVolumeInfo: read failed for %lu with code %d\n",
2722 afs_printable_uint32_lu(vid),
2726 vd.restoredFromId = vd.id; /* remember the RO volume here */
2728 vd.id = vd.parentId;
2732 vd.uniquifier += 5000; /* just in case there are still file copies from
2733 * the old RW volume around */
2735 /* For ROs, the copyDate contains the time that the RO volume was actually
2736 * created, and the creationDate just contains the last time the RO was
2737 * copied from the RW data. So, make the new RW creationDate more accurate
2738 * by setting it to copyDate, if copyDate is older. Since, we know the
2739 * volume is at least as old as copyDate. */
2740 if (vd.copyDate < vd.creationDate) {
2741 vd.creationDate = vd.copyDate;
2743 /* If copyDate is newer, just make copyDate and creationDate the same,
2744 * for consistency with other RWs */
2745 vd.copyDate = vd.creationDate;
2748 p = strrchr(vd.name, '.');
2749 if (p && !strcmp(p, ".readonly")) {
2752 if (OS_WRITE(fdw, &vd, sizeof(struct VolumeDiskData)) !=
2753 sizeof(struct VolumeDiskData)) {
2754 Log("1 convertVolumeInfo: write failed for %lu with code %d\n",
2755 afs_printable_uint32_lu(vid),
2764 * Convert a RO-volume into a RW-volume
2766 * This function allows to recover very fast from the loss of a partition
2767 * from RO-copies if all RO-Copies exist on another partition.
2768 * Then these RO-volumes can be made to the new RW-volumes.
2769 * Backup of RW-volumes then consists in "vos release".
2771 * We must make sure in this partition exists only the RO-volume which
2772 * is typical for remote replicas.
2774 * Then the linktable is already ok,
2775 * the vnode files need to be renamed
2776 * the volinfo file needs to be replaced by another one with
2777 * slightly different contents and new name.
2778 * The volume header file of the RO-volume in the /vicep<x> directory
2779 * is destroyed by this call. A new header file for the RW-volume must
2780 * be created after return from this routine.
2784 namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
2787 #ifdef FSSYNC_BUILD_CLIENT
2789 char dir_name[512], oldpath[512], newpath[512];
2804 struct DiskPartition64 *partP;
2805 struct ViceInodeInfo info;
2806 struct VolumeDiskHeader h;
2807 # ifdef AFS_DEMAND_ATTACH_FS
2809 # endif /* AFS_DEMAND_ATTACH_FS */
2811 for (partP = DiskPartitionList; partP && strcmp(partP->name, pname);
2812 partP = partP->next);
2814 Log("1 namei_ConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
2819 # ifdef AFS_DEMAND_ATTACH_FS
2820 locktype = VVolLockType(V_VOLUPD, 1);
2821 code = VLockVolumeByIdNB(volumeId, partP, locktype);
2827 # endif /* AFS_DEMAND_ATTACH_FS */
2829 if (VReadVolumeDiskHeader(volumeId, partP, &h)) {
2830 Log("1 namei_ConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n",
2831 afs_printable_uint32_lu(volumeId));
2836 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
2838 ino = namei_MakeSpecIno(h.parent, VI_LINKTABLE);
2839 IH_INIT(ih, partP->device, h.parent, ino);
2841 namei_HandleToName(&n, ih);
2842 strlcpy(dir_name, n.n_path, sizeof(dir_name));
2843 p = strrchr(dir_name, OS_DIRSEPC);
2845 dirp = opendir(dir_name);
2847 Log("1 namei_ConvertROtoRWvolume: Could not opendir(%s)\n", dir_name);
2852 while ((dp = readdir(dirp))) {
2853 /* struct ViceInodeInfo info; */
2854 #ifndef AFS_NT40_ENV
2855 if (*dp->d_name == '.')
2858 if (DecodeInode(dir_name, dp->d_name, &info, ih->ih_vid) < 0) {
2859 Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s" OS_DIRSEP "%s\n",
2860 dir_name, dp->d_name);
2865 if (info.u.param[1] != -1) {
2866 Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s" OS_DIRSEP "%s\n", dir_name, dp->d_name);
2871 if (info.u.param[0] != volumeId) {
2872 if (info.u.param[0] == ih->ih_vid) {
2873 if (info.u.param[2] == VI_LINKTABLE) { /* link table */
2878 Log("1 namei_ConvertROtoRWvolume: found special file %s" OS_DIRSEP "%s"
2879 " for volume %lu\n", dir_name, dp->d_name,
2880 afs_printable_uint32_lu(info.u.param[0]));
2885 if (info.u.param[2] == VI_VOLINFO) { /* volume info file */
2886 strlcpy(infoName, dp->d_name, sizeof(infoName));
2888 } else if (info.u.param[2] == VI_SMALLINDEX) { /* small vnodes file */
2889 strlcpy(smallName, dp->d_name, sizeof(smallName));
2891 } else if (info.u.param[2] == VI_LARGEINDEX) { /* large vnodes file */
2892 strlcpy(largeName, dp->d_name, sizeof(largeName));
2896 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);
2903 if (!infoSeen || !smallSeen || !largeSeen || !linkSeen) {
2904 Log("1 namei_ConvertROtoRWvolume: not all special files found in %s\n", dir_name);
2910 * If we come here then there was only a RO-volume and we can safely
2914 memset(&t_ih, 0, sizeof(t_ih));
2915 t_ih.ih_dev = ih->ih_dev;
2916 t_ih.ih_vid = ih->ih_vid;
2918 snprintf(oldpath, sizeof oldpath, "%s" OS_DIRSEP "%s", dir_name,
2920 fd = OS_OPEN(oldpath, O_RDWR, 0);
2921 if (fd == INVALID_FD) {
2922 Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
2927 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_VOLINFO);
2928 namei_HandleToName(&n, &t_ih);
2929 fd2 = OS_OPEN(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
2930 if (fd2 == INVALID_FD) {
2931 Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
2936 code = convertVolumeInfo(fd, fd2, ih->ih_vid);
2940 OS_UNLINK(n.n_path);
2944 SetOGM(fd2, ih->ih_vid, 1);
2947 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_SMALLINDEX);
2948 namei_HandleToName(&n, &t_ih);
2949 snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
2951 fd = OS_OPEN(newpath, O_RDWR, 0);
2952 if (fd == INVALID_FD) {
2953 Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
2957 SetOGM(fd, ih->ih_vid, 2);
2960 MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
2962 link(newpath, n.n_path);
2966 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_LARGEINDEX);
2967 namei_HandleToName(&n, &t_ih);
2968 snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
2970 fd = OS_OPEN(newpath, O_RDWR, 0);
2971 if (fd == INVALID_FD) {
2972 Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
2976 SetOGM(fd, ih->ih_vid, 3);
2979 MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
2981 link(newpath, n.n_path);
2988 h.volumeInfo_hi = h.id;
2989 h.smallVnodeIndex_hi = h.id;
2990 h.largeVnodeIndex_hi = h.id;
2991 h.linkTable_hi = h.id;
2993 if (VCreateVolumeDiskHeader(&h, partP)) {
2994 Log("1 namei_ConvertROtoRWvolume: Couldn't write header for RW-volume %lu\n",
2995 afs_printable_uint32_lu(h.id));
3000 if (VDestroyVolumeDiskHeader(partP, volumeId, h.parent)) {
3001 Log("1 namei_ConvertROtoRWvolume: Couldn't unlink header for RO-volume %lu\n",
3002 afs_printable_uint32_lu(volumeId));
3005 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
3006 FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
3009 # ifdef AFS_DEMAND_ATTACH_FS
3011 VUnlockVolumeById(volumeId, partP);
3013 # endif /* AFS_DEMAND_ATTACH_FS */
3021 * returns a static string used to print either 32 or 64 bit inode numbers.
3024 PrintInode(char *s, Inode ino)
3026 static afs_ino_str_t result;
3030 snprintf(s, sizeof(afs_ino_str_t), "%llu", (afs_uintmax_t) ino);
3037 /* Routines to facilitate removing zero link count files. */
3038 #define MAX_ZLC_NAMES 32
3039 #define MAX_ZLC_NAMELEN 16
3040 typedef struct zlcList_s {
3041 struct zlcList_s *zlc_next;
3043 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
3046 static zlcList_t *zlcAnchor = NULL;
3047 static zlcList_t *zlcCur = NULL;
3050 AddToZLCDeleteList(char dir, char *name)
3052 osi_Assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
3054 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
3055 if (zlcCur && zlcCur->zlc_next)
3056 zlcCur = zlcCur->zlc_next;
3058 zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
3064 zlcCur->zlc_next = tmp;
3068 zlcCur->zlc_next = NULL;
3073 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c" OS_DIRSEP "%s", dir, name);
3075 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%s", name);
3081 DeleteZLCFiles(char *path)
3087 for (z = zlcAnchor; z; z = z->zlc_next) {
3088 for (i = 0; i < z->zlc_n; i++) {
3090 (void)sprintf(fname, "%s" OS_DIRSEP "%s", path, z->zlc_names[i]);
3092 (void)sprintf(fname, "%s", z->zlc_names[i]);
3093 if (namei_unlink(fname) < 0) {
3094 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
3098 z->zlc_n = 0; /* Can reuse space. */
3111 tnext = i->zlc_next;
3115 zlcCur = zlcAnchor = NULL;
3119 #endif /* AFS_NAMEI_ENV */