2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* I/O operations for the Unix open by name (namei) interface. */
12 #include <afsconfig.h>
13 #include <afs/param.h>
20 #ifdef HAVE_SYS_FILE_H
21 # include <sys/file.h>
33 #include <rx/rx_queue.h>
35 #include <afs/afsutil.h>
38 #include <afs/afsint.h>
42 #include "viceinode.h"
44 #include "partition.h"
46 #include "volume_inline.h"
48 #include <afs/errors.h>
51 #include <afs/errmap_nt.h>
55 #define LOCK_SH 1 /* shared lock */
56 #define LOCK_EX 2 /* exclusive lock */
57 #define LOCK_NB 4 /* don't block when locking */
58 #define LOCK_UN 8 /* unlock */
63 #include <afs/work_queue.h>
64 #include <afs/thread_pool.h>
65 #include <vol/vol-salvage.h>
72 namei_iread(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
81 nBytes = FDH_PREAD(fdP, buf, size, offset);
90 namei_iwrite(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
99 nBytes = FDH_PWRITE(fdP, buf, size, offset);
101 FDH_REALLYCLOSE(fdP);
108 /* Inode number format:
109 * low 32 bits - if a regular file or directory, the vnode; else the type.
110 * 32-36 - uniquifier tag and index into counts array for this vnode. Only
111 * two of the available bits are currently used. The rest are
112 * present in case we ever increase the number of types of volumes
113 * in the volume group.
114 * bit 37 : 1 == special, 0 == regular
116 # define NAMEI_VNODEMASK 0x00ffffffff
117 # define NAMEI_TAGSHIFT 32
118 # define NAMEI_INODESPECIAL 0x2000000000
119 # define NAMEI_SPECDIR "R"
120 # define NAMEI_SPECDIRC 'R'
121 #else /* !AFS_NT40_ENV */
122 /* Inode number format:
123 * low 26 bits - vnode number - all 1's if volume special file.
125 * next 3 bits spare (0's)
126 * high 32 bits - uniquifier (regular) or type if spare
128 # define NAMEI_VNODEMASK 0x003ffffff
129 # define NAMEI_TAGSHIFT 26
130 # define NAMEI_UNIQMASK 0xffffffff
131 # define NAMEI_UNIQSHIFT 32
132 # define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
133 /* dir1 is the high 8 bits of the 26 bit vnode */
134 # define VNO_DIR1(vno) ((vno >> 14) & 0xff)
135 /* dir2 is the next 9 bits */
136 # define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
137 /* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
138 # define NAMEI_SPECDIR "special"
139 #endif /* !AFS_NT40_ENV */
140 #define NAMEI_TAGMASK 0x7
141 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
143 #define NAMEI_SPECDIRLEN (sizeof(NAMEI_SPECDIR)-1)
145 #define NAMEI_MAXVOLS 5 /* Maximum supported number of volumes per volume
146 * group, not counting temporary (move) volumes.
147 * This is the number of separate files, all having
148 * the same vnode number, which can occur in a volume
160 static int GetFreeTag(IHandle_t * ih, int vno);
162 /* namei_HandleToInodeDir
164 * Construct the path name of the directory holding the inode data.
165 * Format: /<vicepx>/INODEDIR
170 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
172 memset(name, '\0', sizeof(*name));
173 nt_DevToDrive(name->n_drive, ih->ih_dev);
174 strlcpy(name->n_path, name->n_drive, sizeof(name->n_path));
178 /* Format: /<vicepx>/INODEDIR */
179 #define PNAME_BLEN 64
181 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
185 memset(name, '\0', sizeof(*name));
188 * Add the /vicepXX string to the start of name->n_base and then calculate
189 * offset as the number of bytes we know we added.
191 * FIXME: This embeds knowledge of the vice partition naming scheme and
192 * mapping from device numbers. There needs to be an API that tells us
195 volutil_PartitionName_r(ih->ih_dev, name->n_base, sizeof(name->n_base));
196 offset = VICE_PREFIX_SIZE + (ih->ih_dev > 25 ? 2 : 1);
197 name->n_base[offset] = OS_DIRSEPC;
199 strlcpy(name->n_base + offset, INODEDIR, sizeof(name->n_base) - offset);
200 strlcpy(name->n_path, name->n_base, sizeof(name->n_path));
204 #define addtoname(N, C) \
206 if ((N)->n_path[strlen((N)->n_path)-1] != OS_DIRSEPC) \
207 strlcat((N)->n_path, OS_DIRSEP, sizeof((N)->n_path)); \
208 strlcat((N)->n_path, (C), sizeof((N)->n_path)); \
214 namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
216 /* X:\Vol_XXXXXXX.data */
220 namei_HandleToInodeDir(name, ih);
221 /* nt_drive added to name by namei_HandleToInodeDir() */
222 namep = name->n_voldir;
223 (void)memcpy(namep, "\\Vol_", 5);
225 (void)strcpy(namep, int_to_base32(str1, ih->ih_vid));
226 namep += strlen(namep);
227 memcpy(namep, ".data", 5);
230 addtoname(name, name->n_voldir);
234 namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
238 namei_HandleToInodeDir(name, ih);
239 (void)int32_to_flipbase64(tmp, (int64_t) (ih->ih_vid & 0xff));
240 strlcpy(name->n_voldir1, tmp, sizeof(name->n_voldir1));
241 addtoname(name, name->n_voldir1);
242 (void)int32_to_flipbase64(tmp, (int64_t) ih->ih_vid);
243 strlcpy(name->n_voldir2, tmp, sizeof(name->n_voldir2));
244 addtoname(name, name->n_voldir2);
248 /* namei_HandleToName
250 * Constructs a file name for the fully qualified handle.
253 /* Note that special files end up in X:\Vol_XXXXXXX.data\R */
255 namei_HandleToName(namei_t * name, IHandle_t * ih)
257 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
258 int special = (ih->ih_ino & NAMEI_INODESPECIAL)?1:0;
259 int tag = (int)((ih->ih_ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
262 namei_HandleToVolDir(name, ih);
265 name->n_dir[0] = NAMEI_SPECDIRC;
268 name->n_dir[0] = 'Q';
270 name->n_dir[0] = ((vno & 0x1f) >> 1) + 'A';
273 name->n_dir[1] = '\0';
274 addtoname(name, name->n_dir);
275 /* X:\Vol_XXXXXXX.data\X\V_XXXXXXX.XXX */
276 namep = name->n_inode;
277 (void)memcpy(namep, "\\V_", 3);
279 (void)strcpy(namep, int_to_base32(str1, vno));
280 namep += strlen(namep);
282 (void)strcpy(namep, int_to_base32(str1, tag));
283 namep += strlen(namep);
284 addtoname(name, name->n_inode);
287 /* Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special */
289 namei_HandleToName(namei_t * name, IHandle_t * ih)
291 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
294 namei_HandleToVolDir(name, ih);
296 if (vno == NAMEI_VNODESPECIAL) {
297 strlcpy(name->n_dir1, NAMEI_SPECDIR, sizeof(name->n_dir1));
298 addtoname(name, name->n_dir1);
299 name->n_dir2[0] = '\0';
301 (void)int32_to_flipbase64(str, VNO_DIR1(vno));
302 strlcpy(name->n_dir1, str, sizeof(name->n_dir1));
303 addtoname(name, name->n_dir1);
304 (void)int32_to_flipbase64(str, VNO_DIR2(vno));
305 strlcpy(name->n_dir2, str, sizeof(name->n_dir2));
306 addtoname(name, name->n_dir2);
308 (void)int64_to_flipbase64(str, (int64_t) ih->ih_ino);
309 strlcpy(name->n_inode, str, sizeof(name->n_inode));
310 addtoname(name, name->n_inode);
315 /* The following is a warning to tell sys-admins to not muck about in this
318 #define VICE_README "These files and directories are a part of the AFS \
319 namespace. Modifying them\nin any way will result in loss of AFS data,\n\
320 ownership and permissions included.\n"
322 namei_ViceREADME(char *partition)
327 /* Create the inode directory if we're starting for the first time */
328 snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s", partition,
330 mkdir(filename, 0700);
332 snprintf(filename, sizeof filename,
333 "%s" OS_DIRSEP "%s" OS_DIRSEP "README",
334 partition, INODEDIR);
335 fd = OS_OPEN(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
336 if (fd != INVALID_FD) {
337 (void)OS_WRITE(fd, VICE_README, strlen(VICE_README));
344 /* namei_CreateDataDirectories
346 * If creating the file failed because of ENOENT or ENOTDIR, try
347 * creating all the directories first.
351 namei_CreateDataDirectories(namei_t * name, int *created)
358 snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
360 if (mkdir(tmp) < 0) {
371 for (i = 'A'; i <= NAMEI_SPECDIRC; i++) {
373 if (mkdir(tmp) < 0 && errno != EEXIST)
379 #define create_dir() \
381 if (mkdir(tmp, 0700)<0) { \
382 if (errno != EEXIST) \
390 #define create_nextdir(A) \
392 strcat(tmp, OS_DIRSEP); strcat(tmp, A); create_dir(); \
396 namei_CreateDataDirectories(namei_t * name, int *created)
402 strlcpy(tmp, name->n_base, sizeof(tmp));
405 create_nextdir(name->n_voldir1);
406 create_nextdir(name->n_voldir2);
407 create_nextdir(name->n_dir1);
408 if (name->n_dir2[0]) {
409 create_nextdir(name->n_dir2);
416 /* delTree(): Deletes an entire tree of directories (no files)
418 * root : Full path to the subtree. Should be big enough for PATH_MAX
419 * tree : the subtree to be deleted is rooted here. Specifies only the
420 * subtree beginning at tree (not the entire path). It should be
421 * a pointer into the "root" buffer.
423 * errp : errno of the first error encountered during the directory cleanup.
424 * *errp should have been initialized to 0.
427 * -1 : If errors were encountered during cleanup and error is set to
431 * If there are errors, we try to work around them and delete as many
432 * directories as possible. We don't attempt to remove directories that still
433 * have non-dir entries in them.
436 delTree(char *root, char *tree, int *errp)
444 /* delete the children first */
445 cp = strchr(tree, OS_DIRSEPC);
447 delTree(root, cp + 1, errp);
450 cp = tree + strlen(tree); /* move cp to the end of string tree */
452 /* now delete all entries in this dir */
453 if ((ds = opendir(root)) != NULL) {
455 while ((dirp = readdir(ds))) {
456 /* ignore . and .. */
457 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
459 /* since root is big enough, we reuse the space to
460 * concatenate the dirname to the current tree
462 strcat(root, OS_DIRSEP);
463 strcat(root, dirp->d_name);
464 if (afs_stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
465 /* delete this subtree */
466 delTree(root, cp + 1, errp);
468 *errp = *errp ? *errp : errno;
470 /* recover path to our cur tree by truncating it to
475 /* if (!errno) -- closedir not implicit if we got an error */
479 /* finally axe the current dir */
481 *errp = *errp ? *errp : errno;
483 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
485 #endif /* !AFS_PTHREAD_ENV */
490 /* if we encountered errors during cleanup, we return a -1 */
499 /* namei_RemoveDataDirectories
501 * Returns 0 on success.
502 * Returns -1 on error. Typically, callers ignore this error because we
503 * can continue running if the removes fail. The salvage process will
504 * finish tidying up for us.
509 namei_RemoveDataDirectories(namei_t * name)
516 snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
519 path += strlen(path);
520 *path++ = OS_DIRSEPC;
522 for (i = 'A'; i <= NAMEI_SPECDIRC; i++) {
524 if (rmdir(name->n_path) < 0 && errno != ENOENT)
529 /* Delete the Vol_NNNNNN.data directory. */
532 if (rmdir(name->n_path) < 0 && errno != ENOENT) {
540 * We only use the n_base and n_voldir1 entries
541 * and only do rmdir's.
544 namei_RemoveDataDirectories(namei_t * name)
548 int prefixlen = strlen(name->n_base), err = 0;
549 int vollen = strlen(name->n_voldir1);
550 char pbuf[MAXPATHLEN];
554 strlcpy(path, name->n_path, sizeof(pbuf));
556 /* move past the prefix and n_voldir1 */
557 path = path + prefixlen + 1 + vollen + 1; /* skip over the trailing / */
559 /* now delete all dirs upto path */
560 code = delTree(pbuf, path, &err);
562 /* We've now deleted everything under /n_base/n_voldir1/n_voldir2 that
563 * we could. Do not delete /n_base/n_voldir1, since doing such might
564 * interrupt another thread trying to create a volume. We could introduce
565 * some locking to make this safe (or only remove it for whole-partition
566 * salvages), but by not deleting it we only leave behind a maximum of
567 * 256 empty directories. So at least for now, don't bother. */
572 /* Create the file in the name space.
574 * Parameters stored as follows:
576 * p1 - volid - implied in containing directory.
577 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
578 * p3 - uniq -- bits 4-0 are in mode bits 4-0
579 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
581 * p1 - volid - creation time - dwHighDateTime
582 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
583 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
584 * p4 - parid - parent volume id - implied in containing directory.
586 * Return value is the inode number or (Inode)-1 if error.
587 * We "know" there is only one link table, so return EEXIST if there already
588 * is a link table. It's up to the calling code to test errno and increment
594 * This function is called by VCreateVolume to hide the implementation
595 * details of the inode numbers. This only allows for 7 volume special
596 * types, but if we get that far, this could should be dead by then.
599 namei_MakeSpecIno(int volid, int type)
602 ino = NAMEI_INODESPECIAL;
605 /* tag is always 0 for special */
607 type &= NAMEI_TAGMASK;
608 ino |= ((Inode) type) << NAMEI_TAGSHIFT;
609 ino |= ((Inode) volid) << NAMEI_UNIQSHIFT;
617 SetOGM(FD_t fd, int parm, int tag)
623 SetWinOGM(FD_t fd, int p1, int p2)
628 ftime.dwHighDateTime = p1;
629 ftime.dwLowDateTime = p2;
631 code = SetFileTime(fd, &ftime, NULL /*access*/, NULL /*write*/);
638 GetWinOGM(FD_t fd, int *p1, int *p2)
643 code = GetFileTime(fd, &ftime, NULL /*access*/, NULL /*write*/);
647 *p1 = ftime.dwHighDateTime;
648 *p2 = ftime.dwLowDateTime;
654 CheckOGM(FdHandle_t *fdP, int p1)
658 if (GetWinOGM(fdP->fd_fd, &ogm_p1, &ogm_p2)) {
670 FixSpecialOGM(FdHandle_t *fdP, int check)
672 Inode ino = fdP->fd_ih->ih_ino;
673 VnodeId vno = NAMEI_VNODESPECIAL, ogm_vno;
676 if (GetWinOGM(fdP->fd_fd, &ogm_volid, &ogm_vno)) {
680 /* the only thing we can check is the vnode number; for the volid we have
681 * nothing else to compare against */
682 if (vno != ogm_vno) {
686 if (SetWinOGM(fdP->fd_fd, ogm_volid, vno)) {
693 #else /* AFS_NT40_ENV */
694 /* SetOGM - set owner group and mode bits from parm and tag */
696 SetOGM(FD_t fd, int parm, int tag)
699 * owner - low 15 bits of parm.
700 * group - next 15 bits of parm.
701 * mode - 2 bits of parm, then lowest = 3 bits of tag.
703 int owner, group, mode;
705 owner = parm & 0x7fff;
706 group = (parm >> 15) & 0x7fff;
707 if (fchown(fd, owner, group) < 0)
710 mode = (parm >> 27) & 0x18;
712 if (fchmod(fd, mode) < 0)
717 /* GetOGM - get parm and tag from owner, group and mode bits. */
719 GetOGMFromStat(struct afs_stat_st *status, int *parm, int *tag)
721 *parm = status->st_uid | (status->st_gid << 15);
722 *parm |= (status->st_mode & 0x18) << 27;
723 *tag = status->st_mode & 0x7;
727 GetOGM(FdHandle_t *fdP, int *parm, int *tag)
729 struct afs_stat_st status;
730 if (afs_fstat(fdP->fd_fd, &status) < 0)
732 GetOGMFromStat(&status, parm, tag);
737 CheckOGM(FdHandle_t *fdP, int p1)
741 if (GetOGM(fdP, &parm, &tag) < 0)
750 FixSpecialOGM(FdHandle_t *fdP, int check)
752 int inode_volid, ogm_volid;
753 int inode_type, ogm_type;
754 Inode ino = fdP->fd_ih->ih_ino;
756 inode_volid = ((ino >> NAMEI_UNIQSHIFT) & NAMEI_UNIQMASK);
757 inode_type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
759 if (GetOGM(fdP, &ogm_volid, &ogm_type) < 0) {
760 Log("Error retrieving OGM info\n");
764 if (inode_volid != ogm_volid || inode_type != ogm_type) {
765 Log("%sIncorrect OGM data (ino: vol %u type %d) (ogm: vol %u type %d)\n",
766 check?"":"Fixing ", inode_volid, inode_type, ogm_volid, ogm_type);
772 if (SetOGM(fdP->fd_fd, inode_volid, inode_type) < 0) {
773 Log("Error setting OGM data\n");
780 #endif /* !AFS_NT40_ENV */
783 * Check/fix the OGM data for an inode
785 * @param[in] fdP Open file handle for the inode to check
786 * @param[in] check 1 to just check the OGM data, and return an error if it
787 * is incorrect. 0 to fix the OGM data if it is incorrect.
789 * @pre fdP must be for a special inode
796 namei_FixSpecialOGM(FdHandle_t *fdP, int check)
799 Inode ino = fdP->fd_ih->ih_ino;
801 vnode = (int)(ino & NAMEI_VNODEMASK);
802 if (vnode != NAMEI_VNODESPECIAL) {
803 Log("FixSpecialOGM: non-special vnode %u\n", vnode);
807 return FixSpecialOGM(fdP, check);
810 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
812 /* Derive the name and create it O_EXCL. If that fails we have an error.
813 * Get the tag from a free column in the link table.
817 namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
820 FD_t fd = INVALID_FD;
831 memset((void *)&tmp, 0, sizeof(IHandle_t));
832 memset(&tfd, 0, sizeof(FdHandle_t));
834 tmp.ih_dev = nt_DriveToDev(part);
835 if (tmp.ih_dev == -1) {
840 if (p2 == INODESPECIAL) {
841 /* Parameters for special file:
842 * p1 - volume id - goes into owner/group/mode
843 * p2 - vnode == INODESPECIAL
845 * p4 - parent volume id
850 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
851 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
853 int vno = p2 & NAMEI_VNODEMASK;
854 /* Parameters for regular file:
868 tmp.ih_ino = (Inode) p2;
873 namei_HandleToName(&name, &tmp);
874 p = strrchr((char *)&name.n_path, '.');
876 for (tag = 0; tag < NAMEI_MAXVOLS; tag++) {
877 *p = *int_to_base32(str1, tag);
878 fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_EXCL, 0666);
879 if (fd == INVALID_FD) {
880 if (errno == ENOTDIR || errno == ENOENT) {
881 if (namei_CreateDataDirectories(&name, &created_dir) == 0)
882 fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_EXCL, 0666);
886 if (fd != INVALID_FD)
888 if (p2 == INODESPECIAL && p3 == VI_LINKTABLE)
891 if (fd == INVALID_FD) {
895 tmp.ih_ino &= ~(((Inode) NAMEI_TAGMASK) << NAMEI_TAGSHIFT);
896 tmp.ih_ino |= (((Inode) tag) << NAMEI_TAGSHIFT);
899 if (SetWinOGM(fd, ogm_p1, ogm_p2)) {
900 errno = OS_ERROR(EBADF);
906 if (p2 != INODESPECIAL) {
907 if (fd == INVALID_FD) {
909 code = nt_unlink((char *)&name.n_path);
918 code = namei_SetLinkCount(fdP, tmp.ih_ino, 1, 0);
920 } else if (p2 == INODESPECIAL && p3 == VI_LINKTABLE) {
921 if (fd == INVALID_FD)
923 /* hack at tmp to setup for set link count call. */
925 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
930 if (fd != INVALID_FD)
933 if (code || (fd == INVALID_FD)) {
934 if (p2 != INODESPECIAL) {
937 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
943 int save_errno = errno;
944 namei_RemoveDataDirectories(&name);
948 return (code || (fd == INVALID_FD)) ? (Inode) -1 : tmp.ih_ino;
950 #else /* !AFS_NT40_ENV */
952 namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
964 memset((void *)&tmp, 0, sizeof(IHandle_t));
965 memset(&tfd, 0, sizeof(FdHandle_t));
967 tmp.ih_dev = volutil_GetPartitionID(part);
968 if (tmp.ih_dev == -1) {
974 /* Parameters for special file:
975 * p1 - volume id - goes into owner/group/mode
978 * p4 - parent volume id
983 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
984 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
986 int vno = p2 & NAMEI_VNODEMASK;
987 /* Parameters for regular file:
999 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
1000 tag = GetFreeTag(lh, p2);
1005 tmp.ih_ino = (Inode) p2;
1006 /* name is <uniq(p3)><tag><vno(p2)> */
1007 tmp.ih_ino |= ((Inode) tag) << NAMEI_TAGSHIFT;
1008 tmp.ih_ino |= ((Inode) p3) << NAMEI_UNIQSHIFT;
1013 namei_HandleToName(&name, &tmp);
1014 fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_RDWR, 0);
1015 if (fd == INVALID_FD) {
1016 if (errno == ENOTDIR || errno == ENOENT) {
1017 if (namei_CreateDataDirectories(&name, &created_dir) < 0)
1019 fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_RDWR,
1021 if (fd == INVALID_FD)
1027 if (SetOGM(fd, ogm_parm, tag) < 0) {
1033 if (p2 == (afs_uint32)-1 && p3 == VI_LINKTABLE) {
1034 /* hack at tmp to setup for set link count call. */
1035 memset((void *)&tfd, 0, sizeof(FdHandle_t)); /* minimalistic still, but a little cleaner */
1038 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
1042 if (fd != INVALID_FD)
1046 if (code || (fd == INVALID_FD)) {
1050 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
1055 return (code || (fd == INVALID_FD)) ? (Inode) - 1 : tmp.ih_ino;
1061 namei_iopen(IHandle_t * h)
1066 /* Convert handle to file name. */
1067 namei_HandleToName(&name, h);
1068 fd = OS_OPEN((char *)&name.n_path, O_RDWR, 0666);
1072 /* Need to detect vol special file and just unlink. In those cases, the
1073 * handle passed in _is_ for the inode. We only check p1 for the special
1077 namei_dec(IHandle_t * ih, Inode ino, int p1)
1084 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1086 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1088 /* Verify this is the right file. */
1089 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
1091 namei_HandleToName(&name, tmp);
1096 errno = OS_ERROR(ENOENT);
1100 if (CheckOGM(fdP, p1) < 0) {
1101 FDH_REALLYCLOSE(fdP);
1103 errno = OS_ERROR(EINVAL);
1107 /* If it's the link table itself, decrement the link count. */
1108 if (type == VI_LINKTABLE) {
1109 if ((count = namei_GetLinkCount(fdP, (Inode) 0, 1, 0, 1)) < 0) {
1110 FDH_REALLYCLOSE(fdP);
1116 if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) <
1118 FDH_REALLYCLOSE(fdP);
1130 if ((code = OS_UNLINK(name.n_path)) == 0) {
1131 if (type == VI_LINKTABLE) {
1132 /* Try to remove directory. If it fails, that's ok.
1133 * Salvage will clean up.
1135 char *slash = strrchr(name.n_path, OS_DIRSEPC);
1137 /* avoid an rmdir() on the file we just unlinked */
1140 (void)namei_RemoveDataDirectories(&name);
1143 FDH_REALLYCLOSE(fdP);
1146 /* Get a file descriptor handle for this Inode */
1152 if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0) {
1153 FDH_REALLYCLOSE(fdP);
1159 if (namei_SetLinkCount(fdP, ino, count, 1) < 0) {
1160 FDH_REALLYCLOSE(fdP);
1165 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
1166 Log("Warning: Lost ref on ihandle dev %d vid %d ino %lld\n",
1167 th->ih_dev, th->ih_vid, (afs_int64)th->ih_ino);
1170 /* If we're less than 0, someone presumably unlinked;
1171 don't bother setting count to 0, but we need to drop a lock */
1172 if (namei_SetLinkCount(fdP, ino, 0, 1) < 0) {
1173 FDH_REALLYCLOSE(fdP);
1179 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
1181 namei_HandleToName(&name, th);
1183 code = OS_UNLINK(name.n_path);
1192 namei_inc(IHandle_t * h, Inode ino, int p1)
1198 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1199 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1200 if (type != VI_LINKTABLE)
1205 /* Get a file descriptor handle for this Inode */
1211 if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0)
1216 errno = OS_ERROR(EINVAL);
1220 if (namei_SetLinkCount(fdP, ino, count, 1) < 0)
1224 FDH_REALLYCLOSE(fdP);
1231 #ifndef AFS_NT40_ENV
1233 namei_replace_file_by_hardlink(IHandle_t *hLink, IHandle_t *hTarget)
1239 /* Convert handle to file name. */
1240 namei_HandleToName(&nameLink, hLink);
1241 namei_HandleToName(&nameTarget, hTarget);
1243 OS_UNLINK(nameLink.n_path);
1244 code = link(nameTarget.n_path, nameLink.n_path);
1249 namei_copy_on_write(IHandle_t *h)
1255 struct afs_stat_st tstat;
1258 namei_HandleToName(&name, h);
1259 if (afs_stat(name.n_path, &tstat) < 0)
1261 if (tstat.st_nlink > 1) { /* do a copy on write */
1270 snprintf(path, sizeof(path), "%s-tmp", name.n_path);
1271 fd = OS_OPEN(path, O_CREAT | O_EXCL | O_RDWR, 0);
1272 if (fd == INVALID_FD) {
1283 size = tstat.st_size;
1286 tlen = size > 8192 ? 8192 : size;
1287 if (FDH_PREAD(fdP, buf, tlen, offset) != tlen)
1289 if (OS_WRITE(fd, buf, tlen) != tlen)
1295 FDH_REALLYCLOSE(fdP);
1300 OS_UNLINK(name.n_path);
1301 code = rename(path, name.n_path);
1308 /************************************************************************
1309 * File Name Structure
1310 ************************************************************************
1312 * Each AFS file needs a unique name and it needs to be findable with
1313 * minimal lookup time. Note that the constraint on the number of files and
1314 * directories in a volume is the size of the vnode index files and the
1315 * max file size AFS supports (for internal files) of 2^31. Since a record
1316 * in the small vnode index file is 64 bytes long, we can have at most
1317 * (2^31)/64 or 33554432 files. A record in the large index file is
1318 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
1319 * Another layout parameter is that there is roughly a 16 to 1 ratio between
1320 * the number of files and the number of directories.
1322 * Using this information we can see that a layout of 256 directories, each
1323 * with 512 subdirectories and each of those having 512 files gives us
1324 * 256*512*512 = 67108864 AFS files and directories.
1326 * The volume, vnode, uniquifier and data version, as well as the tag
1327 * are required, either for finding the file or for salvaging. It's best to
1328 * restrict the name to something that can be mapped into 64 bits so the
1329 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
1330 * is used to distinguish between different versions of the same file
1331 * which are currently in the RW and clones of a volume. See "Link Table
1332 * Organization" below for more information on the tag. The tag is
1333 * required in the name of the file to ensure a unique name.
1335 * ifdef AFS_NT40_ENV
1336 * The data for each volume group is in a separate directory. The name of the
1337 * volume is of the form: Vol_NNNNNN.data, where NNNNNN is a base 32
1338 * representation of the RW volume ID (even where the RO is the only volume
1339 * on the partition). Below that are separate subdirectories for the
1340 * AFS directories and special files. There are also 16 directories for files,
1341 * hashed on the low 5 bits (recall bit0 is always 0) of the vnode number.
1342 * These directories are named:
1343 * A - P - 16 file directories.
1344 * Q ----- data directory
1345 * R ----- special files directory
1347 * The vnode is hashed into the directory using the low bits of the
1350 * The format of a file name for a regular file is:
1351 * Y:\Vol_NNNNNN.data\X\V_IIIIII.J
1352 * Y - partition encoded as drive letter, starting with D
1353 * NNNNNN - base 32 encoded volume number of RW volume
1354 * X - hash directory, as above
1355 * IIIIII - base 32 encoded vnode number
1356 * J - base 32 encoded tag
1358 * uniq is stored in the dwHighDateTime creation time field
1359 * dv is stored in the dwLowDateTime creation time field
1361 * Special inodes are always in the R directory, as above, and are
1363 * True child volid is stored in the dwHighDateTime creation time field
1364 * vnode number is always -1 (Special)
1365 * type is the IIIIII part of the filename
1366 * uniq is the J part of the filename
1367 * parent volume id is implied in the containing directory
1370 * We can store data in the uid, gid and mode bits of the files, provided
1371 * the directories have root only access. This gives us 15 bits for each
1372 * of uid and gid (GNU chown considers 65535 to mean "don't change").
1373 * There are 9 available mode bits. Adn we need to store a total of
1374 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
1375 * or 131 bits somewhere.
1377 * The format of a file name for a regular file is:
1378 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
1379 * V1 - low 8 bits of RW volume id
1380 * V2 - all bits of RW volume id
1381 * AA - high 8 bits of vnode number.
1382 * BB - next 9 bits of vnode number.
1383 * <tag><uniq><vno> - file name
1385 * Volume special files are stored in a separate directory:
1386 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
1389 * The vnode is hashed into the directory using the high bits of the
1390 * vnode number. This is so that consecutively created vnodes are in
1391 * roughly the same area on the disk. This will at least be optimal if
1392 * the user is creating many files in the same AFS directory. The name
1393 * should be formed so that the leading characters are different as quickly
1394 * as possible, leading to faster discards of incorrect matches in the
1402 /************************************************************************
1403 * Link Table Organization
1404 ************************************************************************
1406 * The link table volume special file is used to hold the link counts that
1407 * are held in the inodes in inode based AFS vice filesystems. For user
1408 * space access, the link counts are being kept in a separate
1409 * volume special file. The file begins with the usual version stamp
1410 * information and is then followed by one row per vnode number. vnode 0
1411 * is used to hold the link count of the link table itself. That is because
1412 * the same link table is shared among all the volumes of the volume group
1413 * and is deleted only when the last volume of a volume group is deleted.
1415 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
1416 * link count from 0 through 7. Each colume represents a unique instance of
1417 * that vnode. Say we have a file shared between the RW and a RO and a
1418 * different version of the file (or a different uniquifer) for the BU volume.
1419 * Then one column would be holding the link count of 2 for the RW and RO
1420 * and a different column would hold the link count of 1 for the BU volume.
1421 * # ifdef AFS_NT40_ENV
1422 * The column used is determined for NT by the uniquifier tag applied to
1423 * generate a unique file name in the NTFS namespace. The file name is
1424 * of the form "V_<vno>.<tag>" . And the <tag> is also the column number
1425 * in the link table.
1427 * Note that we allow only 5 volumes per file, giving 15 bits used in the
1431 #define LINKTABLE_WIDTH 2
1432 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
1435 * compute namei link table file and bit offset from inode number.
1437 * @param[in] ino inode number
1438 * @param[out] offset link table file offset
1439 * @param[out] index bit offset within 2-byte record
1444 namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
1446 int toff = (int)(ino & NAMEI_VNODEMASK);
1447 int tindex = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
1449 *offset = (afs_foff_t) ((toff << LINKTABLE_SHIFT) + 8); /* * 2 + sizeof stamp */
1450 *index = (tindex << 1) + tindex;
1453 #ifdef AFS_PTHREAD_ENV
1454 /* XXX do static initializers work for WINNT/pthread? */
1455 pthread_mutex_t _namei_glc_lock = PTHREAD_MUTEX_INITIALIZER;
1456 #define NAMEI_GLC_LOCK MUTEX_ENTER(&_namei_glc_lock)
1457 #define NAMEI_GLC_UNLOCK MUTEX_EXIT(&_namei_glc_lock)
1458 #else /* !AFS_PTHREAD_ENV */
1459 #define NAMEI_GLC_LOCK
1460 #define NAMEI_GLC_UNLOCK
1461 #endif /* !AFS_PTHREAD_ENV */
1464 * get the link count of an inode.
1466 * @param[in] h namei link count table file handle
1467 * @param[in] ino inode number for which we are requesting a link count
1468 * @param[in] lockit if asserted, return with lock held on link table file
1469 * @param[in] fixup if asserted, write 1 to link count when read() returns
1471 * @param[in] nowrite return success on zero byte read or ZLC
1473 * @post if lockit asserted and lookup was successful, will return with write
1474 * lock on link table file descriptor
1476 * @return link count
1477 * @retval -1 namei link table i/o error
1482 namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite)
1484 unsigned short row = 0;
1489 /* there's no linktable yet. the salvager will create one later */
1490 if (h->fd_fd == INVALID_FD && fixup)
1492 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1495 if (FDH_LOCKFILE(h, offset) != 0)
1499 rc = FDH_PREAD(h, (char*)&row, sizeof(row), offset);
1501 goto bad_getLinkByte;
1503 if ((rc == 0 || !((row >> index) & NAMEI_TAGMASK)) && fixup && nowrite)
1505 if (rc == 0 && fixup) {
1507 * extend link table and write a link count of 1 for ino
1509 * in order to make MT-safe, truncation (extension really)
1510 * must happen under a mutex
1513 if (FDH_SIZE(h) >= offset+sizeof(row)) {
1515 goto bad_getLinkByte;
1517 FDH_TRUNC(h, offset+sizeof(row));
1519 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
1522 if (rc != sizeof(row)) {
1523 goto bad_getLinkByte;
1526 if (fixup && !((row >> index) & NAMEI_TAGMASK)) {
1530 * in order to make this mt-safe, we need to do the read-modify-write
1531 * under a mutex. thus, we repeat the read inside the lock.
1534 rc = FDH_PREAD(h, (char *)&row, sizeof(row), offset);
1535 if (rc == sizeof(row)) {
1537 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
1540 if (rc != sizeof(row))
1541 goto bad_getLinkByte;
1544 return (int)((row >> index) & NAMEI_TAGMASK);
1548 FDH_UNLOCKFILE(h, offset);
1553 namei_SetNonZLC(FdHandle_t * h, Inode ino)
1555 return namei_GetLinkCount(h, ino, 0, 1, 0);
1558 /* Return a free column index for this vnode. */
1560 GetFreeTag(IHandle_t * ih, int vno)
1574 /* Only one manipulates at a time. */
1575 if (FDH_LOCKFILE(fdP, offset) != 0) {
1576 FDH_REALLYCLOSE(fdP);
1580 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
1582 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1583 if (nBytes != sizeof(row)) {
1589 /* Now find a free column in this row and claim it. */
1590 for (col = 0; col < NAMEI_MAXVOLS; col++) {
1591 coldata = 7 << (col * 3);
1592 if ((row & coldata) == 0)
1595 if (col >= NAMEI_MAXVOLS) {
1600 coldata = 1 << (col * 3);
1603 if (FDH_PWRITE(fdP, (char *)&row, sizeof(row), offset) != sizeof(row)) {
1606 (void)FDH_SYNC(fdP);
1607 FDH_UNLOCKFILE(fdP, offset);
1612 FDH_UNLOCKFILE(fdP, offset);
1613 FDH_REALLYCLOSE(fdP);
1619 /* namei_SetLinkCount
1620 * If locked is set, assume file is locked. Otherwise, lock file before
1621 * proceeding to modify it.
1624 namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
1630 ssize_t nBytes = -1;
1632 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1635 if (FDH_LOCKFILE(fdP, offset) != 0) {
1640 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1641 if (nBytes != sizeof(row)) {
1643 errno = OS_ERROR(EBADF);
1644 goto bad_SetLinkCount;
1649 bytesRead = 7 << index;
1651 row &= (unsigned short)~bytesRead;
1652 row |= (unsigned short)count;
1654 if (FDH_PWRITE(fdP, (char *)&row, sizeof(short), offset) != sizeof(short)) {
1655 errno = OS_ERROR(EBADF);
1656 goto bad_SetLinkCount;
1658 (void)FDH_SYNC(fdP);
1664 FDH_UNLOCKFILE(fdP, offset);
1666 /* disallowed above 7, so... */
1671 /* ListViceInodes - write inode data to a results file. */
1672 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1673 unsigned int volid);
1674 static int DecodeVolumeName(char *name, unsigned int *vid);
1675 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
1676 int (*write_fun) (FD_t,
1677 struct ViceInodeInfo *,
1678 char *, char *), FD_t fp,
1679 int (*judgeFun) (struct ViceInodeInfo *,
1680 afs_uint32 vid, void *),
1681 afs_uint32 singleVolumeNumber, void *rock);
1686 * Write the inode data to the results file.
1688 * Returns -2 on error, 0 on success.
1690 * This is written as a callback simply so that other listing routines
1691 * can use the same inode reading code.
1694 WriteInodeInfo(FD_t fp, struct ViceInodeInfo *info, char *dir, char *name)
1697 n = OS_WRITE(fp, info, sizeof(*info));
1698 return (n == sizeof(*info)) ? 0 : -2;
1702 int mode_errors; /* Number of errors found in mode bits on directories. */
1704 VerifyDirPerms(char *path)
1706 struct afs_stat_st status;
1708 if (afs_stat(path, &status) < 0) {
1709 Log("Unable to stat %s. Please manually verify mode bits for this"
1710 " directory\n", path);
1712 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1718 * Fill the results file with the requested inode information.
1720 * This code optimizes single volume salvages by just looking at that one
1721 * volume's directory.
1723 * @param[in] devname device name string
1724 * @param[in] moutnedOn vice partition mount point
1725 * @param[in] resultFile result file in which to write inode
1726 * metadata. If NULL, write routine is not
1728 * @param[in] judgeInode filter function pointer. if not NULL, only
1729 * inodes for which this routine returns non-
1730 * zero will be written to the results file.
1731 * @param[in] singleVolumeNumber volume id filter
1732 * @param[out] forcep always set to 0 for namei impl
1733 * @param[in] forceR not used by namei impl
1734 * @param[in] wpath not used by namei impl
1735 * @param[in] rock opaque pointer passed to judgeInode
1737 * @return operation status
1739 * @retval -1 complete failure, salvage should terminate.
1740 * @retval -2 not enough space on partition, salvager has error message
1744 ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
1745 int (*judgeInode) (struct ViceInodeInfo * info, afs_uint32 vid, void *rock),
1746 afs_uint32 singleVolumeNumber, int *forcep, int forceR, char *wpath,
1751 *forcep = 0; /* no need to salvage until further notice */
1753 /* Verify protections on directories. */
1755 VerifyDirPerms(mountedOn);
1758 namei_ListAFSFiles(mountedOn, WriteInodeInfo, inodeFile, judgeInode,
1759 singleVolumeNumber, rock);
1761 if (inodeFile == INVALID_FD)
1768 if (OS_SYNC(inodeFile) == -1) {
1769 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1774 * Paranoia: check that the file is really the right size
1776 if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
1777 Log("Wrong size (%d instead of %lu) in inode file for %s\n",
1778 (int) OS_SIZE(inodeFile),
1779 (long unsigned int) ninodes * sizeof(struct ViceInodeInfo),
1788 * Collect all the matching AFS files on the drive.
1789 * If singleVolumeNumber is non-zero, just return files for that volume.
1791 * @param[in] dev vice partition path
1792 * @param[in] writeFun function pointer to a function which
1793 * writes inode information to FILE fp
1794 * @param[in] fp file stream where inode metadata is sent
1795 * @param[in] judgeFun filter function pointer. if not NULL,
1796 * only entries for which a non-zero value
1797 * is returned are written to fp
1798 * @param[in] singleVolumeNumber volume id filter. if nonzero, only
1799 * process files for that specific volume id
1800 * @param[in] rock opaque pointer passed into writeFun and
1803 * @return operation status
1805 * @retval >=0 number of matching files found
1808 namei_ListAFSFiles(char *dev,
1809 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1812 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1813 afs_uint32 singleVolumeNumber, void *rock)
1820 #ifndef AFS_NT40_ENV
1826 static void FreeZLCList(void);
1829 memset((void *)&ih, 0, sizeof(IHandle_t));
1831 ih.ih_dev = nt_DriveToDev(dev);
1833 ih.ih_dev = volutil_GetPartitionID(dev);
1836 if (singleVolumeNumber) {
1837 ih.ih_vid = singleVolumeNumber;
1838 namei_HandleToVolDir(&name, &ih);
1840 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1841 singleVolumeNumber, rock);
1845 /* Find all volume data directories and descend through them. */
1846 namei_HandleToInodeDir(&name, &ih);
1848 dirp1 = opendir(name.n_path);
1851 while ((dp1 = readdir(dirp1))) {
1853 /* Heirarchy is one level on Windows */
1854 if (!DecodeVolumeName(dp1->d_name, &ih.ih_vid)) {
1856 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1860 if (*dp1->d_name == '.')
1862 snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", name.n_path,
1864 dirp2 = opendir(path2);
1866 while ((dp2 = readdir(dirp2))) {
1867 if (*dp2->d_name == '.')
1869 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1871 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1888 static void AddToZLCDeleteList(char dir, char *name);
1889 static void DeleteZLCFiles(char *path);
1893 * examine a namei volume special file.
1895 * @param[in] path1 volume special directory path
1896 * @param[in] dname directory entry name
1897 * @param[in] myIH inode handle to volume directory
1898 * @param[out] linkHandle namei link count fd handle. if
1899 * the inode in question is the link
1900 * table, then the FdHandle is populated
1901 * @param[in] writeFun metadata write function pointer
1902 * @param[in] fp file pointer where inode metadata
1903 * is written by (*writeFun)()
1904 * @param[in] judgeFun inode filter function pointer. if
1905 * not NULL, only inodes for which this
1906 * function returns non-zero are recorded
1907 * into fp by writeFun
1908 * @param[in] singleVolumeNumer volume id filter. if non-zero, only
1909 * inodes associated with this volume id
1910 * are recorded by writeFun
1911 * @param[in] rock opaque pointer passed to writeFun and
1914 * @return operation status
1915 * @retval 1 count this inode
1916 * @retval 0 don't count this inode
1917 * @retval -1 failure
1922 _namei_examine_special(char * path1,
1925 FdHandle_t * linkHandle,
1926 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
1929 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1930 int singleVolumeNumber,
1934 struct ViceInodeInfo info;
1936 if (DecodeInode(path1, dname, &info, myIH->ih_vid) < 0) {
1941 if (info.u.param[2] != VI_LINKTABLE) {
1943 } else if (info.u.param[0] != myIH->ih_vid) {
1944 /* VGID encoded in linktable filename and/or OGM data isn't
1945 * consistent with VGID encoded in namei path */
1946 Log("namei_ListAFSSubDirs: warning: inconsistent linktable "
1947 "filename \"%s" OS_DIRSEP "%s\"; salvager will delete it "
1948 "(dir_vgid=%u, inode_vgid=%u)\n",
1949 path1, dname, myIH->ih_vid,
1953 /* Open this handle */
1954 snprintf(path2, sizeof(path2),
1955 "%s" OS_DIRSEP "%s", path1, dname);
1956 linkHandle->fd_fd = OS_OPEN(path2, Testing ? O_RDONLY : O_RDWR, 0666);
1958 namei_GetLinkCount(linkHandle, (Inode) 0, 1, 1, Testing);
1962 (*judgeFun) (&info, singleVolumeNumber, rock)) {
1963 ret = (*writeFun) (fp, &info, path1, dname);
1965 Log("_namei_examine_special: writeFun returned %d\n", ret);
1977 * examine a namei file.
1979 * @param[in] path3 volume special directory path
1980 * @param[in] dname directory entry name
1981 * @param[in] myIH inode handle to volume directory
1982 * @param[in] linkHandle namei link count fd handle.
1983 * @param[in] writeFun metadata write function pointer
1984 * @param[in] fp file pointer where inode metadata
1985 * is written by (*writeFun)()
1986 * @param[in] judgeFun inode filter function pointer. if
1987 * not NULL, only inodes for which this
1988 * function returns non-zero are recorded
1989 * into fp by writeFun
1990 * @param[in] singleVolumeNumer volume id filter. if non-zero, only
1991 * inodes associated with this volume id
1992 * are recorded by writeFun
1993 * @param[in] rock opaque pointer passed to writeFun and
1996 * @return operation status
1997 * @retval 1 count this inode
1998 * @retval 0 don't count this inode
1999 * @retval -1 failure
2000 * @retval -2 request ZLC delete
2005 _namei_examine_reg(char * path3,
2008 FdHandle_t * linkHandle,
2009 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
2012 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
2013 int singleVolumeNumber,
2017 struct ViceInodeInfo info;
2019 int dirl; /* Windows-only (one level hash dir) */
2022 if (DecodeInode(path3, dname, &info, myIH->ih_vid) < 0) {
2027 namei_GetLinkCount(linkHandle,
2028 info.inodeNumber, 1, 1, Testing);
2029 if (info.linkCount == 0) {
2031 Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n", path3, dname);
2032 #ifdef AFS_SALSRV_ENV
2033 /* defer -- the AddToZLCDeleteList() interface is not MT-safe */
2035 #else /* !AFS_SALSRV_ENV */
2036 dirl = path3[strlen(path3)-1];
2037 AddToZLCDeleteList((char)dirl, dname);
2038 #endif /* !AFS_SALSRV_ENV */
2039 #else /* !DELETE_ZLC */
2040 Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path3,
2047 (*judgeFun) (&info, singleVolumeNumber, rock)) {
2048 ret = (*writeFun) (fp, &info, path3, dname);
2050 Log("_namei_examine_reg: writeFun returned %d\n", ret);
2062 * listsubdirs work queue node.
2064 struct listsubdirs_work_node {
2065 #ifdef AFS_SALSRV_ENV
2066 int *error; /**< *error set if an error was
2067 * encountered in any listsubdirs
2071 IHandle_t * IH; /**< volume directory handle */
2072 FdHandle_t *linkHandle; /**< namei link count fd handle. when
2073 * examinining the link table special
2074 * inode, this will be pointed at the
2077 FD_t fp; /**< file pointer for writeFun */
2079 /** function which will write inode metadata to fp */
2080 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *, char *);
2082 /** inode filter function */
2083 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *);
2084 int singleVolumeNumber; /**< volume id filter */
2085 void * rock; /**< pointer passed to writeFun and judgeFun */
2086 int code; /**< return code from examine function */
2087 int special; /**< asserted when this is a volume
2092 * simple wrapper around _namei_examine_special and _namei_examine_reg.
2094 * @param[in] work the struct listsubdirs_work_node for the associated
2095 * "list subdirs" job
2096 * @param[in] dir the directory to examine
2097 * @param[in] filename the filename in 'dir' to examine
2099 * @return operation status
2100 * @retval 1 count this inode
2101 * @retval 0 don't count this inode
2102 * @retval -1 failure
2105 _namei_examine_file(const struct listsubdirs_work_node *work, char *dir,
2108 if (work->special) {
2109 return _namei_examine_special(dir, filename, work->IH,
2110 work->linkHandle, work->writeFun, work->fp,
2111 work->judgeFun, work->singleVolumeNumber,
2114 return _namei_examine_reg(dir, filename, work->IH,
2115 work->linkHandle, work->writeFun, work->fp,
2116 work->judgeFun, work->singleVolumeNumber,
2122 #ifdef AFS_SALSRV_ENV
2123 /** @addtogroup afs_vol_salsrv_pario */
2127 * arguments for the _namei_examine_file_cbk callback function.
2129 struct listsubdirs_args {
2130 const struct listsubdirs_work_node *work; /**< arguments that are the same
2131 * for all invocations of
2132 * _namei_examine_file_cbk, in
2134 int *result; /**< where we can store the return code of _namei_examine_file */
2136 char dir[512]; /**< directory to examine */
2137 char filename[256]; /**< filename in 'dir' to examine */
2141 * a node in the list of results of listsubdir jobs.
2143 struct listsubdirs_result {
2145 int inodes; /**< return value from _namei_examine_file */
2149 * clean up a list of 'struct listsubdirs_result's and interpret the results.
2151 * @param[in] resultlist a list of 'struct listsubdirs_result's
2153 * @return number of inodes found
2157 _namei_listsubdirs_cleanup_results(struct rx_queue *resultlist)
2159 struct listsubdirs_result *res, *nres;
2162 for(queue_Scan(resultlist, res, nres, listsubdirs_result)) {
2164 /* noop, retain erroneous error code */
2165 } else if (res->inodes < 0) {
2180 * wait for the spawned listsubdirs jobs to finish, and return how many inodes
2183 * @param[in] queue queue to wait to finish
2184 * @param[in] resultlist list of 'struct listsubdirs_result's that the queued
2185 * jobs are storing their results in
2187 * @return number of inodes found
2191 _namei_listsubdirs_wait(struct afs_work_queue *queue, struct rx_queue *resultlist)
2195 code = afs_wq_wait_all(queue);
2200 return _namei_listsubdirs_cleanup_results(resultlist);
2204 * work queue entry point for examining namei files.
2206 * @param[in] queue pointer to struct Vwork_queue
2207 * @param[in] node pointer to struct Vwork_queue_node
2208 * @param[in] queue_rock opaque pointer to struct salsrv_pool_state
2209 * @param[in] node_rock opaque pointer to struct listsubdirs_work_node
2210 * @param[in] caller_rock opaque pointer to struct salsrv_worker_thread_state
2212 * @return operation status
2214 * @see Vwork_queue_callback_func_t
2219 _namei_examine_file_cbk(struct afs_work_queue *queue,
2220 struct afs_work_queue_node *node,
2226 struct listsubdirs_args *args = node_rock;
2227 const struct listsubdirs_work_node * work = args->work;
2228 char *dir = args->dir;
2229 char *filename = args->filename;
2231 code = _namei_examine_file(work, dir, filename);
2233 *(args->result) = code;
2237 /* we've errored, so no point in letting more jobs continue */
2238 afs_wq_shutdown(queue);
2244 static pthread_once_t wq_once = PTHREAD_ONCE_INIT;
2245 static pthread_key_t wq_key;
2248 * create the wq_key key for storing a work queue.
2251 _namei_wq_keycreate(void)
2253 opr_Verify(pthread_key_create(&wq_key, NULL) == 0);
2257 * set the work queue for this thread to use for backgrounding namei ops.
2259 * The work queue will be used in ListAFSSubdirs (called indirectly by
2260 * ListViceInodes) to examine files in parallel.
2262 * @param[in] wq the work queue to use
2265 namei_SetWorkQueue(struct afs_work_queue *wq)
2267 opr_Verify(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
2269 opr_Verify(pthread_setspecific(wq_key, wq) == 0);
2273 * enqueue an examine file work unit.
2275 * @param[in] work the _namei_examine_file arguments that are common to
2276 * all callers within the same ListAFSFiles operation
2277 * @param[in] dir the specific directory to look at (string will be
2278 * copied; can be stack/temporary memory)
2279 * @param[in] filename the filename to look at (string will be copied; can be
2280 * stack/temporary memory)
2281 * @param[in] wq work queue to enqueue this work unit to
2282 * @param[in] resultlist the list to append the 'struct listsubdirs_result' to
2283 * for the enqueued work unit
2285 * @return operation status
2287 * @retval -1 fatal error
2289 * @note errors MUST be indicated by a -1 error code and nothing else, to be
2290 * compatible with _namei_examine_reg and _namei_examine_special
2295 _namei_examine_file_spawn(const struct listsubdirs_work_node *work,
2296 const char *dir, const char *filename,
2297 struct afs_work_queue *wq,
2298 struct rx_queue *resultlist)
2301 struct listsubdirs_args *args = NULL;
2302 struct listsubdirs_result *result = NULL;
2303 struct afs_work_queue_node *node = NULL;
2304 struct afs_work_queue_add_opts opts;
2306 args = malloc(sizeof(*args));
2312 result = malloc(sizeof(*result));
2313 if (result == NULL) {
2318 code = afs_wq_node_alloc(&node);
2323 code = afs_wq_node_set_detached(node);
2330 args->result = &result->inodes;
2331 strlcpy(args->dir, dir, sizeof(args->dir));
2332 strlcpy(args->filename, filename, sizeof(args->filename));
2334 code = afs_wq_node_set_callback(node,
2335 &_namei_examine_file_cbk,
2343 afs_wq_add_opts_init(&opts);
2346 code = afs_wq_add(wq, node, &opts);
2353 queue_Append(resultlist, result);
2358 afs_wq_node_put(node);
2374 #else /* !AFS_SALSRV_ENV */
2375 # define _namei_examine_file_spawn(work, dir, file, wq, resultlist) \
2376 _namei_examine_file(work, dir, file)
2377 #endif /* !AFS_SALSRV_ENV */
2380 * traverse and check inodes.
2382 * @param[in] dirIH volume group directory handle
2383 * @param[in] writeFun function pointer which will write inode
2384 * metadata to FILE stream fp
2385 * @param[in] fp file stream where inode metadata gets
2387 * @param[in] judgeFun inode filter function. if not NULL, only
2388 * inodes for which the filter returns non-zero
2389 * will be written out by writeFun
2390 * @param[in] singleVolumeNumber volume id filter. only inodes matching this
2391 * filter are written out by writeFun
2392 * @param[in] rock opaque pointer passed to judgeFun and writeFun
2394 * @return operation status
2396 * @retval >=0 number of matching inodes found
2398 * @todo the salsrv implementation may consume a lot of
2399 * memory for a large volume. at some point we should
2400 * probably write a background thread to asynchronously
2401 * clean up the resultlist nodes to reduce memory footprint
2406 namei_ListAFSSubDirs(IHandle_t * dirIH,
2407 int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
2410 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
2411 afs_uint32 singleVolumeNumber, void *rock)
2413 int code = 0, ret = 0;
2414 IHandle_t myIH = *dirIH;
2416 char path1[512], path3[512];
2418 #ifndef AFS_NT40_ENV
2423 struct dirent *dp1, *dp3;
2424 FdHandle_t linkHandle;
2426 struct listsubdirs_work_node work;
2427 #ifdef AFS_SALSRV_ENV
2429 struct afs_work_queue *wq;
2431 struct rx_queue resultlist;
2434 namei_HandleToVolDir(&name, &myIH);
2435 strlcpy(path1, name.n_path, sizeof(path1));
2437 /* Do the directory containing the special files first to pick up link
2440 (void)strcat(path1, OS_DIRSEP);
2441 (void)strcat(path1, NAMEI_SPECDIR);
2443 linkHandle.fd_fd = INVALID_FD;
2444 #ifdef AFS_SALSRV_ENV
2445 opr_Verify(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
2447 wq = pthread_getspecific(wq_key);
2453 queue_Init(&resultlist);
2456 memset(&work, 0, sizeof(work));
2457 work.linkHandle = &linkHandle;
2460 work.writeFun = writeFun;
2461 work.judgeFun = judgeFun;
2462 work.singleVolumeNumber = singleVolumeNumber;
2465 #ifdef AFS_SALSRV_ENV
2466 work.error = &error;
2469 dirp1 = opendir(path1);
2471 while ((dp1 = readdir(dirp1))) {
2472 if (*dp1->d_name == '.')
2475 #ifdef AFS_SALSRV_ENV
2481 #endif /* AFS_SALSRV_ENV */
2483 code = _namei_examine_file_spawn(&work, path1, dp1->d_name, wq, &resultlist);
2493 /* count this inode */
2494 #ifndef AFS_SALSRV_ENV
2503 #ifdef AFS_SALSRV_ENV
2504 /* effectively a barrier */
2505 code = _namei_listsubdirs_wait(wq, &resultlist);
2506 if (code < 0 || error) {
2514 if (linkHandle.fd_fd == INVALID_FD) {
2515 Log("namei_ListAFSSubDirs: warning: VG %u does not have a link table; "
2516 "salvager will recreate it.\n", dirIH->ih_vid);
2519 /* Now run through all the other subdirs */
2520 namei_HandleToVolDir(&name, &myIH);
2521 strlcpy(path1, name.n_path, sizeof(path1));
2525 dirp1 = opendir(path1);
2527 while ((dp1 = readdir(dirp1))) {
2528 #ifndef AFS_NT40_ENV
2529 if (*dp1->d_name == '.')
2532 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
2535 #ifndef AFS_NT40_ENV /* This level missing on Windows */
2536 /* Now we've got a next level subdir. */
2537 snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s",
2538 path1, dp1->d_name);
2539 dirp2 = opendir(path2);
2541 while ((dp2 = readdir(dirp2))) {
2542 if (*dp2->d_name == '.')
2545 /* Now we've got to the actual data */
2546 snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
2547 path2, dp2->d_name);
2549 /* Now we've got to the actual data */
2550 snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
2551 path1, dp1->d_name);
2553 dirp3 = opendir(path3);
2555 while ((dp3 = readdir(dirp3))) {
2556 #ifndef AFS_NT40_ENV
2557 if (*dp3->d_name == '.')
2561 #ifdef AFS_SALSRV_ENV
2564 #ifndef AFS_NT40_ENV
2571 #endif /* AFS_SALSRV_ENV */
2573 code = _namei_examine_file_spawn(&work, path3,
2580 #ifndef AFS_NT40_ENV
2588 #ifndef AFS_SALSRV_ENV
2596 #ifndef AFS_NT40_ENV /* This level missing on Windows */
2605 #ifdef AFS_SALSRV_ENV
2606 /* effectively a barrier */
2607 code = _namei_listsubdirs_wait(wq, &resultlist);
2608 if (code < 0 || error) {
2618 /* Then why does this directory exist? Blow it away. */
2619 namei_HandleToVolDir(&name, dirIH);
2620 namei_RemoveDataDirectories(&name);
2624 #ifdef AFS_SALSRV_ENV
2626 afs_wq_wait_all(wq);
2628 _namei_listsubdirs_cleanup_results(&resultlist);
2630 if (linkHandle.fd_fd != INVALID_FD)
2631 OS_CLOSE(linkHandle.fd_fd);
2643 DecodeVolumeName(char *name, unsigned int *vid)
2645 /* Name begins with "Vol_" and ends with .data. See nt_HandleToVolDir() */
2652 if (strncmp(name, "Vol_", 4))
2654 if (strcmp(name + len - 5, ".data"))
2657 stmp[len - 5] = '\0';
2658 *vid = base32_to_int(stmp + 4);
2663 DecodeVolumeName(char *name, unsigned int *vid)
2665 if (strlen(name) < 1)
2667 *vid = (unsigned int)flipbase64_to_int64(name);
2675 * Get the inode number from the name.
2680 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
2685 WIN32_FIND_DATA data;
2689 FdHandle_t linkHandle;
2692 snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
2694 dirH = FindFirstFileEx(fpath, FindExInfoStandard, &data,
2695 FindExSearchNameMatch, NULL,
2696 FIND_FIRST_EX_CASE_SENSITIVE);
2697 if (dirH == INVALID_HANDLE_VALUE)
2700 (void)strcpy(stmp, name);
2701 s = strrchr(stmp, '_');
2705 t = strrchr(s, '.');
2710 vno = base32_to_int(s); /* type for special files */
2711 tag = base32_to_int(t+1);
2712 info->inodeNumber = ((Inode) tag) << NAMEI_TAGSHIFT;
2713 info->inodeNumber |= vno;
2714 info->byteCount = data.nFileSizeLow;
2716 dirl = dpath[strlen(dpath)-1];
2717 if (dirl == NAMEI_SPECDIRC) { /* Special inode. */
2718 info->inodeNumber |= NAMEI_INODESPECIAL;
2719 info->u.param[0] = data.ftCreationTime.dwHighDateTime;
2720 info->u.param[1] = data.ftCreationTime.dwLowDateTime;
2721 info->u.param[2] = vno; /* type */
2722 info->u.param[3] = volid;
2723 if (vno != VI_LINKTABLE)
2724 info->linkCount = 1;
2726 /* Open this handle */
2728 (void)sprintf(lpath, "%s" OS_DIRSEP "%s", fpath, data.cFileName);
2729 linkHandle.fd_fd = OS_OPEN(lpath, O_RDONLY, 0666);
2731 namei_GetLinkCount(&linkHandle, (Inode) 0, 0, 0, 0);
2735 namei_GetLinkCount(&linkHandle, info->inodeNumber, 0, 0, 0);
2736 if (info->linkCount == 0) {
2738 Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n",
2739 fpath, data.cFileName);
2740 AddToZLCDeleteList(dirl, data.cFileName);
2742 Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path,
2746 info->u.param[2] = data.ftCreationTime.dwHighDateTime;
2747 info->u.param[3] = data.ftCreationTime.dwLowDateTime;
2748 info->u.param[1] = vno;
2749 info->u.param[0] = volid;
2756 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
2760 struct afs_stat_st status;
2762 lb64_string_t check;
2764 snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
2766 if (afs_stat(fpath, &status) < 0) {
2770 info->byteCount = status.st_size;
2771 info->inodeNumber = (Inode) flipbase64_to_int64(name);
2773 int64_to_flipbase64(check, info->inodeNumber);
2774 if (strcmp(name, check))
2777 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
2778 parm = ((info->inodeNumber >> NAMEI_UNIQSHIFT) & NAMEI_UNIQMASK);
2779 tag = (int)((info->inodeNumber >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
2781 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
2782 info->u.param[0] = parm;
2783 info->u.param[1] = -1;
2784 info->u.param[2] = tag;
2785 info->u.param[3] = volid;
2787 GetOGMFromStat(&status, &parm, &tag);
2788 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
2789 info->u.param[0] = volid;
2790 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
2791 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
2792 & (Inode) NAMEI_UNIQMASK);
2793 info->u.param[3] = parm;
2800 * Convert the VolumeInfo file from RO to RW
2801 * this routine is called by namei_convertROtoRWvolume()
2804 #ifdef FSSYNC_BUILD_CLIENT
2806 convertVolumeInfo(FD_t fdr, FD_t fdw, afs_uint32 vid)
2808 struct VolumeDiskData vd;
2811 if (OS_READ(fdr, &vd, sizeof(struct VolumeDiskData)) !=
2812 sizeof(struct VolumeDiskData)) {
2813 Log("1 convertVolumeInfo: read failed for %lu with code %d\n",
2814 afs_printable_uint32_lu(vid),
2818 vd.restoredFromId = vd.id; /* remember the RO volume here */
2820 vd.id = vd.parentId;
2824 vd.uniquifier += 5000; /* just in case there are still file copies from
2825 * the old RW volume around */
2827 /* For ROs, the copyDate contains the time that the RO volume was actually
2828 * created, and the creationDate just contains the last time the RO was
2829 * copied from the RW data. So, make the new RW creationDate more accurate
2830 * by setting it to copyDate, if copyDate is older. Since, we know the
2831 * volume is at least as old as copyDate. */
2832 if (vd.copyDate < vd.creationDate) {
2833 vd.creationDate = vd.copyDate;
2835 /* If copyDate is newer, just make copyDate and creationDate the same,
2836 * for consistency with other RWs */
2837 vd.copyDate = vd.creationDate;
2840 p = strrchr(vd.name, '.');
2841 if (p && !strcmp(p, ".readonly")) {
2844 if (OS_WRITE(fdw, &vd, sizeof(struct VolumeDiskData)) !=
2845 sizeof(struct VolumeDiskData)) {
2846 Log("1 convertVolumeInfo: write failed for %lu with code %d\n",
2847 afs_printable_uint32_lu(vid),
2856 * Convert a RO-volume into a RW-volume
2858 * This function allows to recover very fast from the loss of a partition
2859 * from RO-copies if all RO-Copies exist on another partition.
2860 * Then these RO-volumes can be made to the new RW-volumes.
2861 * Backup of RW-volumes then consists in "vos release".
2863 * We must make sure in this partition exists only the RO-volume which
2864 * is typical for remote replicas.
2866 * Then the linktable is already ok,
2867 * the vnode files need to be renamed
2868 * the volinfo file needs to be replaced by another one with
2869 * slightly different contents and new name.
2870 * The volume header file of the RO-volume in the /vicep<x> directory
2871 * is destroyed by this call. A new header file for the RW-volume must
2872 * be created after return from this routine.
2876 namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
2879 #ifdef FSSYNC_BUILD_CLIENT
2881 char dir_name[512], oldpath[512], newpath[512];
2896 struct DiskPartition64 *partP;
2897 struct ViceInodeInfo info;
2898 struct VolumeDiskHeader h;
2899 char *rwpart, *rwname;
2901 # ifdef AFS_DEMAND_ATTACH_FS
2903 # endif /* AFS_DEMAND_ATTACH_FS */
2905 for (partP = DiskPartitionList; partP && strcmp(partP->name, pname);
2906 partP = partP->next);
2908 Log("1 namei_ConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
2913 # ifdef AFS_DEMAND_ATTACH_FS
2914 locktype = VVolLockType(V_VOLUPD, 1);
2915 code = VLockVolumeByIdNB(volumeId, partP, locktype);
2921 # endif /* AFS_DEMAND_ATTACH_FS */
2923 if (VReadVolumeDiskHeader(volumeId, partP, &h)) {
2924 Log("1 namei_ConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n",
2925 afs_printable_uint32_lu(volumeId));
2930 /* check for existing RW on any partition on this server; */
2931 /* if found, return EXDEV - invalid cross-device link */
2933 VGetVolumePath(&ec, h.parent, &rwpart, &rwname);
2935 Log("1 namei_ConvertROtoRWvolume: RW volume %lu already exists on server partition %s.\n",
2936 afs_printable_uint32_lu(h.parent), rwpart);
2943 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
2945 ino = namei_MakeSpecIno(h.parent, VI_LINKTABLE);
2946 IH_INIT(ih, partP->device, h.parent, ino);
2948 namei_HandleToName(&n, ih);
2949 strlcpy(dir_name, n.n_path, sizeof(dir_name));
2950 p = strrchr(dir_name, OS_DIRSEPC);
2952 dirp = opendir(dir_name);
2954 Log("1 namei_ConvertROtoRWvolume: Could not opendir(%s)\n", dir_name);
2959 while ((dp = readdir(dirp))) {
2960 /* struct ViceInodeInfo info; */
2961 #ifndef AFS_NT40_ENV
2962 if (*dp->d_name == '.')
2965 if (DecodeInode(dir_name, dp->d_name, &info, ih->ih_vid) < 0) {
2966 Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s" OS_DIRSEP "%s\n",
2967 dir_name, dp->d_name);
2972 if (info.u.param[1] != -1) {
2973 Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s" OS_DIRSEP "%s\n", dir_name, dp->d_name);
2978 if (info.u.param[0] != volumeId) {
2979 if (info.u.param[0] == ih->ih_vid) {
2980 if (info.u.param[2] == VI_LINKTABLE) { /* link table */
2985 Log("1 namei_ConvertROtoRWvolume: found special file %s" OS_DIRSEP "%s"
2986 " for volume %lu\n", dir_name, dp->d_name,
2987 afs_printable_uint32_lu(info.u.param[0]));
2992 if (info.u.param[2] == VI_VOLINFO) { /* volume info file */
2993 strlcpy(infoName, dp->d_name, sizeof(infoName));
2995 } else if (info.u.param[2] == VI_SMALLINDEX) { /* small vnodes file */
2996 strlcpy(smallName, dp->d_name, sizeof(smallName));
2998 } else if (info.u.param[2] == VI_LARGEINDEX) { /* large vnodes file */
2999 strlcpy(largeName, dp->d_name, sizeof(largeName));
3003 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);
3010 if (!infoSeen || !smallSeen || !largeSeen || !linkSeen) {
3011 Log("1 namei_ConvertROtoRWvolume: not all special files found in %s\n", dir_name);
3017 * If we come here then there was only a RO-volume and we can safely
3021 memset(&t_ih, 0, sizeof(t_ih));
3022 t_ih.ih_dev = ih->ih_dev;
3023 t_ih.ih_vid = ih->ih_vid;
3025 snprintf(oldpath, sizeof oldpath, "%s" OS_DIRSEP "%s", dir_name,
3027 fd = OS_OPEN(oldpath, O_RDWR, 0);
3028 if (fd == INVALID_FD) {
3029 Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
3034 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_VOLINFO);
3035 namei_HandleToName(&n, &t_ih);
3036 fd2 = OS_OPEN(n.n_path, O_CREAT | O_EXCL | O_RDWR, 0);
3037 if (fd2 == INVALID_FD) {
3038 Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
3043 code = convertVolumeInfo(fd, fd2, ih->ih_vid);
3047 OS_UNLINK(n.n_path);
3051 SetOGM(fd2, ih->ih_vid, 1);
3054 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_SMALLINDEX);
3055 namei_HandleToName(&n, &t_ih);
3056 snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
3058 fd = OS_OPEN(newpath, O_RDWR, 0);
3059 if (fd == INVALID_FD) {
3060 Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
3064 SetOGM(fd, ih->ih_vid, 2);
3067 MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
3069 link(newpath, n.n_path);
3073 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_LARGEINDEX);
3074 namei_HandleToName(&n, &t_ih);
3075 snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
3077 fd = OS_OPEN(newpath, O_RDWR, 0);
3078 if (fd == INVALID_FD) {
3079 Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
3083 SetOGM(fd, ih->ih_vid, 3);
3086 MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
3088 link(newpath, n.n_path);
3095 h.volumeInfo_hi = h.id;
3096 h.smallVnodeIndex_hi = h.id;
3097 h.largeVnodeIndex_hi = h.id;
3098 h.linkTable_hi = h.id;
3100 if (VCreateVolumeDiskHeader(&h, partP)) {
3101 Log("1 namei_ConvertROtoRWvolume: Couldn't write header for RW-volume %lu\n",
3102 afs_printable_uint32_lu(h.id));
3107 if (VDestroyVolumeDiskHeader(partP, volumeId, h.parent)) {
3108 Log("1 namei_ConvertROtoRWvolume: Couldn't unlink header for RO-volume %lu\n",
3109 afs_printable_uint32_lu(volumeId));
3112 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
3113 FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
3116 # ifdef AFS_DEMAND_ATTACH_FS
3118 VUnlockVolumeById(volumeId, partP);
3120 # endif /* AFS_DEMAND_ATTACH_FS */
3128 * returns a static string used to print either 32 or 64 bit inode numbers.
3131 PrintInode(char *s, Inode ino)
3133 static afs_ino_str_t result;
3137 snprintf(s, sizeof(afs_ino_str_t), "%llu", (afs_uintmax_t) ino);
3144 /* Routines to facilitate removing zero link count files. */
3145 #define MAX_ZLC_NAMES 32
3146 #define MAX_ZLC_NAMELEN 16
3147 typedef struct zlcList_s {
3148 struct zlcList_s *zlc_next;
3150 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
3153 static zlcList_t *zlcAnchor = NULL;
3154 static zlcList_t *zlcCur = NULL;
3157 AddToZLCDeleteList(char dir, char *name)
3159 opr_Assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
3161 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
3162 if (zlcCur && zlcCur->zlc_next)
3163 zlcCur = zlcCur->zlc_next;
3165 zlcList_t *tmp = malloc(sizeof(zlcList_t));
3171 zlcCur->zlc_next = tmp;
3175 zlcCur->zlc_next = NULL;
3180 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c" OS_DIRSEP "%s", dir, name);
3182 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%s", name);
3188 DeleteZLCFiles(char *path)
3194 for (z = zlcAnchor; z; z = z->zlc_next) {
3195 for (i = 0; i < z->zlc_n; i++) {
3197 (void)sprintf(fname, "%s" OS_DIRSEP "%s", path, z->zlc_names[i]);
3199 (void)sprintf(fname, "%s", z->zlc_names[i]);
3200 if (namei_unlink(fname) < 0) {
3201 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
3205 z->zlc_n = 0; /* Can reuse space. */
3218 tnext = i->zlc_next;
3222 zlcCur = zlcAnchor = NULL;
3226 #endif /* AFS_NAMEI_ENV */