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 <afs/param.h>
20 #include <afs/assert.h>
23 #include <sys/param.h>
25 #include <afs/afsutil.h>
28 #include <afs/afsint.h>
32 #include "viceinode.h"
34 #include "partition.h"
36 extern char *volutil_PartitionName_r(int volid, char *buf, int buflen);
38 int namei_iread(IHandle_t *h, int offset, char *buf, int size)
47 if (FDH_SEEK(fdP, offset, SEEK_SET)<0) {
52 nBytes = FDH_READ(fdP, buf, size);
57 int namei_iwrite(IHandle_t *h, int offset, char *buf, int size)
66 if (FDH_SEEK(fdP, offset, SEEK_SET)<0) {
70 nBytes = FDH_WRITE(fdP, buf, size);
77 /* Inode number format:
78 * low 26 bits - vnode number - all 1's if volume special file.
80 * next 3 bits spare (0's)
81 * high 32 bits - uniquifier (regular) or type if spare
83 #define NAMEI_VNODEMASK 0x003ffffff
84 #define NAMEI_TAGMASK 0x7
85 #define NAMEI_TAGSHIFT 26
86 #define NAMEI_UNIQMASK 0xffffffff
87 #define NAMEI_UNIQSHIFT 32
88 #define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
89 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
91 /* dir1 is the high 8 bits of the 26 bit vnode */
92 #define VNO_DIR1(vno) ((vno >> 14) & 0xff)
93 /* dir2 is the next 9 bits */
94 #define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
95 /* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
97 #define NAMEI_SPECDIR "special"
98 #define NAMEI_SPECDIRLEN (sizeof(NAMEI_SPECDIR)-1)
100 #define NAMEI_MAXVOLS 5 /* Maximum supported number of volumes per volume
101 * group, not counting temporary (move) volumes.
102 * This is the number of separate files, all having
103 * the same vnode number, which can occur in a volume
114 int namei_SetLinkCount(FdHandle_t *h, Inode ino, int count, int locked);
115 static int GetFreeTag(IHandle_t *ih, int vno);
117 /* namei_HandleToInodeDir
119 * Construct the path name of the directory holding the inode data.
120 * Format: /<vicepx>/INODEDIR
123 #define PNAME_BLEN 64
124 static void namei_HandleToInodeDir(namei_t *name, IHandle_t *ih)
126 char *tmp = name->n_base;
128 memset(name, '\0', sizeof(*name));
130 (void) volutil_PartitionName_r(ih->ih_dev, tmp, NAMEI_LCOMP_LEN);
131 tmp += VICE_PREFIX_SIZE;
132 tmp += ih->ih_dev > 25 ? 2 : 1;
134 (void) strcpy(tmp, INODEDIR);
135 (void) strcpy(name->n_path, name->n_base);
138 #define addtoname(N, C) \
140 strcat((N)->n_path, "/"); strcat((N)->n_path, C); \
144 static void namei_HandleToVolDir(namei_t *name, IHandle_t *ih)
148 namei_HandleToInodeDir(name, ih);
149 (void) int32_to_flipbase64(tmp, (int64_t)(ih->ih_vid & 0xff));
150 (void) strcpy(name->n_voldir1, tmp);
151 addtoname(name, name->n_voldir1);
152 (void) int32_to_flipbase64(tmp, (int64_t)ih->ih_vid);
153 (void) strcpy(name->n_voldir2, tmp);
154 addtoname(name, name->n_voldir2);
157 /* namei_HandleToName
159 * Constructs a file name for the fully qualified handle.
160 * Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special
162 void namei_HandleToName(namei_t *name, IHandle_t *ih)
165 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
168 namei_HandleToVolDir(name, ih);
170 if (vno == NAMEI_VNODESPECIAL) {
171 (void) strcpy(name->n_dir1, NAMEI_SPECDIR);
172 addtoname(name, name->n_dir1);
173 name->n_dir2[0] = '\0';
176 (void) int32_to_flipbase64(str, VNO_DIR1(vno));
177 (void) strcpy(name->n_dir1, str);
178 addtoname(name, name->n_dir1);
179 (void) int32_to_flipbase64(str, VNO_DIR2(vno));
180 (void) strcpy(name->n_dir2, str);
181 addtoname(name, name->n_dir2);
183 (void) int64_to_flipbase64(str, (int64_t)ih->ih_ino);
184 (void) strcpy(name->n_inode, str);
185 addtoname(name, name->n_inode);
188 /* The following is a warning to tell sys-admins to not muck about in this
191 #define VICE_README "These files and directories are a part of the AFS \
192 namespace. Modifying them\nin any way will result in loss of AFS data.\n"
193 int namei_ViceREADME(char *partition)
198 sprintf(filename, "%s/%s/README", partition, INODEDIR);
199 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
201 write(fd, VICE_README, strlen(VICE_README));
208 #define create_dir() \
210 if (mkdir(tmp, 0700)<0) { \
211 if (errno != EEXIST) \
219 #define create_nextdir(A) \
221 strcat(tmp, "/"); strcat(tmp, A); create_dir(); \
224 /* namei_CreateDataDirectories
226 * If creating the file failed because of ENOENT or ENOTDIR, try
227 * creating all the directories first.
229 static int namei_CreateDataDirectories(namei_t *name, int *created)
237 (void) strcpy(tmp, name->n_base);
240 create_nextdir(name->n_voldir1);
241 create_nextdir(name->n_voldir2);
242 create_nextdir(name->n_dir1);
243 if (name->n_dir2[0]) {
244 create_nextdir(name->n_dir2);
249 /* delTree(): Deletes an entire tree of directories (no files)
251 * root : Full path to the subtree. Should be big enough for PATH_MAX
252 * tree : the subtree to be deleted is rooted here. Specifies only the
253 * subtree beginning at tree (not the entire path). It should be
254 * a pointer into the "root" buffer.
256 * errp : errno of the first error encountered during the directory cleanup.
257 * *errp should have been initialized to 0.
260 * -1 : If errors were encountered during cleanup and error is set to
264 * If there are errors, we try to work around them and delete as many
265 * directories as possible. We don't attempt to remove directories that still
266 * have non-dir entries in them.
269 delTree(char *root, char *tree, int *errp)
278 /* delete the children first */
279 cp = strchr(tree, '/');
281 delTree(root, cp+1, errp);
285 cp = tree + strlen(tree); /* move cp to the end of string tree */
287 /* now delete all entries in this dir */
288 if ( (ds = opendir(root)) != (DIR *)NULL) {
290 while (dirp = readdir(ds)) {
291 /* ignore . and .. */
292 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
294 /* since root is big enough, we reuse the space to
295 * concatenate the dirname to the current tree
298 strcat(root, dirp->d_name);
299 if ( stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
300 /* delete this subtree */
301 delTree(root, cp+1, errp);
303 *errp = *errp ? *errp : errno;
305 /* recover path to our cur tree by truncating it to
314 /* finally axe the current dir */
316 *errp = *errp ? *errp : errno;
318 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
320 #endif /* !AFS_PTHREAD_ENV */
322 } /* if valid tree */
324 /* if we encountered errors during cleanup, we return a -1 */
332 /* namei_RemoveDataDirectories
334 * Returns 0 on success.
335 * Returns -1 on error. Typically, callers ignore this error bcause we
336 * can continue running if the removes fail. The salvage process will
337 * finish tidying up for us. We only use the n_base and n_voldir1 entries
338 * and only do rmdir's.
341 static int namei_RemoveDataDirectories(namei_t *name)
343 char pbuf[MAXPATHLEN], *path = pbuf;
344 int prefixlen = strlen(name->n_base), err = 0;
346 strcpy(path, name->n_path);
348 /* move past the prefix */
349 path = path+prefixlen+1; /* skip over the trailing / */
351 /* now delete all dirs upto path */
352 return delTree(pbuf, path, &err);
356 /* Create the file in the name space.
358 * Parameters stored as follows:
360 * p1 - volid - implied in containing directory.
361 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
362 * p3 - uniq -- bits 4-0 are in mode bits 4-0
363 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
365 * p1 - volid - creation time - dwHighDateTime
366 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
367 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
368 * p4 - parid - parent volume id - implied in containing directory.
370 * Return value is the inode number or (Inode)-1 if error.
371 * We "know" there is only one link table, so return EEXIST if there already
372 * is a link table. It's up to the calling code to test errno and increment
378 * This function is called by VCreateVolume to hide the implementation
379 * details of the inode numbers. This only allows for 7 volume special
380 * types, but if we get that far, this could should be dead by then.
382 Inode namei_MakeSpecIno(int volid, int type)
385 ino = NAMEI_INODESPECIAL;
386 type &= NAMEI_TAGMASK;
387 ino |= ((Inode)type) << NAMEI_TAGSHIFT;
388 ino |= ((Inode)volid) << NAMEI_UNIQSHIFT;
392 /* SetOGM - set owner group and mode bits from parm and tag
394 * owner - low 15 bits of parm.
395 * group - next 15 bits of parm.
396 * mode - 2 bits of parm, then lowest = 3 bits of tag.
398 static int SetOGM(int fd, int parm, int tag)
400 int owner, group, mode;
402 owner = parm & 0x7fff;
403 group = (parm >> 15) & 0x7fff;
404 if (fchown(fd, owner, group)<0)
407 mode = (parm >> 27) & 0x18;
409 if (fchmod(fd, mode)<0)
416 /* GetOGM - get parm and tag from owner, group and mode bits. */
417 static void GetOGMFromStat(struct stat *status, int *parm, int *tag)
421 *parm = status->st_uid | (status->st_gid << 15);
422 *parm |= (status->st_mode & 0x18) << 27;
423 *tag = status->st_mode & 0x7;
426 static int GetOGM(int fd, int *parm, int *tag)
429 if (fstat(fd, &status)<0)
432 GetOGMFromStat(&status, parm, tag);
436 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
438 /* Derive the name and create it O_EXCL. If that fails we have an error.
439 * Get the tag from a free column in the link table.
441 Inode namei_icreate(IHandle_t *lh, char *part, int p1, int p2, int p3, int p4)
459 memset((void*)&tmp, 0, sizeof(IHandle_t));
462 tmp.ih_dev = volutil_GetPartitionID(part);
463 if (tmp.ih_dev == -1) {
469 /* Parameters for special file:
470 * p1 - volume id - goes into owner/group/mode
473 * p4 - parent volume id
478 tmp.ih_vid = p4; /* Use parent volume id, where this file will be.*/
479 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
482 int vno = p2 & NAMEI_VNODEMASK;
483 /* Parameters for regular file:
495 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
496 tag = GetFreeTag(lh, p2);
500 /* name is <uniq(p3)><tag><vno(p2)> */
502 tmp.ih_ino = (Inode)p2;
503 tmp.ih_ino |= ((Inode)tag)<<NAMEI_TAGSHIFT;
504 tmp.ih_ino |= ((Inode)p3)<<NAMEI_UNIQSHIFT;
509 namei_HandleToName(&name, &tmp);
510 fd = open(name.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
512 if (errno == ENOTDIR || errno == ENOENT) {
513 if (namei_CreateDataDirectories(&name, &created_dir)<0)
515 fd = open(name.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
523 if (SetOGM(fd, ogm_parm, tag)<0) {
529 if (p2 == -1 && p3 == VI_LINKTABLE) {
530 /* hack at tmp to setup for set link count call. */
532 code = namei_SetLinkCount(&tfd, (Inode)0, 1, 0);
540 if (code || (fd < 0)) {
544 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
549 return (code || (fd<0)) ? (Inode)-1 : tmp.ih_ino;
554 int namei_iopen(IHandle_t *h)
559 /* Convert handle to file name. */
560 namei_HandleToName(&name, h);
561 fd = open(name.n_path, O_RDWR, 0666);
565 /* Need to detect vol special file and just unlink. In those cases, the
566 * handle passed in _is_ for the inode. We only check p1 for the special
569 int namei_dec(IHandle_t *ih, Inode ino, int p1)
576 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
580 int type = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
582 /* Verify this is the right file. */
583 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
592 if ((GetOGM(fdP->fd_fd, &inode_p1, &tag)<0) || (inode_p1 != p1)) {
593 FDH_REALLYCLOSE(fdP);
599 /* If it's the link table itself, decrement the link count. */
600 if (type == VI_LINKTABLE) {
601 if ((count = namei_GetLinkCount(fdP, (Inode)0, 1))<0) {
602 FDH_REALLYCLOSE(fdP);
608 if (namei_SetLinkCount(fdP, (Inode)0, count<0 ? 0 : count, 1)<0) {
609 FDH_REALLYCLOSE(fdP);
615 FDH_REALLYCLOSE(fdP);
621 namei_HandleToName(&name, tmp);
622 if ((code = unlink(name.n_path)) == 0) {
623 if (type == VI_LINKTABLE) {
624 /* Try to remove directory. If it fails, that's ok.
625 * Salvage will clean up.
627 (void) namei_RemoveDataDirectories(&name);
630 FDH_REALLYCLOSE(fdP);
634 /* Get a file descriptor handle for this Inode */
640 if ((count = namei_GetLinkCount(fdP, ino, 1))<0) {
641 FDH_REALLYCLOSE(fdP);
647 if (namei_SetLinkCount(fdP, ino, count, 1)<0) {
648 FDH_REALLYCLOSE(fdP);
655 namei_HandleToName(&name, &th);
656 code = unlink(name.n_path);
664 int namei_inc(IHandle_t *h, Inode ino, int p1)
670 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
671 int type = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
672 if (type != VI_LINKTABLE)
677 /* Get a file descriptor handle for this Inode */
683 if ((count = namei_GetLinkCount(fdP, ino, 1))<0)
692 if (namei_SetLinkCount(fdP, ino, count, 1)<0)
696 FDH_REALLYCLOSE(fdP);
703 /************************************************************************
704 * File Name Structure
705 ************************************************************************
707 * Each AFS file needs a unique name and it needs to be findable with
708 * minimal lookup time. Note that the constraint on the number of files and
709 * directories in a volume is the size of the vnode index files and the
710 * max file size AFS supports (for internal files) of 2^31. Since a record
711 * in the small vnode index file is 64 bytes long, we can have at most
712 * (2^31)/64 or 33554432 files. A record in the large index file is
713 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
714 * Another layout parameter is that there is roughly a 16 to 1 ratio between
715 * the number of files and the number of directories.
717 * Using this information we can see that a layout of 256 directories, each
718 * with 512 subdirectories and each of those having 512 files gives us
719 * 256*512*512 = 67108864 AFS files and directories.
721 * The volume, vnode, uniquifier and data version, as well as the tag
722 * are required, either for finding the file or for salvaging. It's best to
723 * restrict the name to something that can be mapped into 64 bits so the
724 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
725 * is used to distinguish between different versions of the same file
726 * which are currently in the RW and clones of a volume. See "Link Table
727 * Organization" below for more information on the tag. The tag is
728 * required in the name of the file to ensure a unique name.
730 * We can store data in the uid, gid and mode bits of the files, provided
731 * the directories have root only access. This gives us 15 bits for each
732 * of uid and gid (GNU chown considers 65535 to mean "don't change").
733 * There are 9 available mode bits. Adn we need to store a total of
734 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
735 * or 131 bits somewhere.
737 * The format of a file name for a regular file is:
738 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
739 * V1 - low 8 bits of RW volume id
740 * V2 - all bits of RW volume id
741 * AA - high 8 bits of vnode number.
742 * BB - next 9 bits of vnode number.
743 * <tag><uniq><vno> - file name
745 * Volume special files are stored in a separate directory:
746 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
749 * The vnode is hashed into the directory using the high bits of the
750 * vnode number. This is so that consecutively created vnodes are in
751 * roughly the same area on the disk. This will at least be optimal if
752 * the user is creating many files in the same AFS directory. The name
753 * should be formed so that the leading characters are different as quickly
754 * as possible, leading to faster discards of incorrect matches in the
760 /************************************************************************
761 * Link Table Organization
762 ************************************************************************
764 * The link table volume special file is used to hold the link counts that
765 * are held in the inodes in inode based AFS vice filesystems. For user
766 * space access, the link counts are being kept in a separate
767 * volume special file. The file begins with the usual version stamp
768 * information and is then followed by one row per vnode number. vnode 0
769 * is used to hold the link count of the link table itself. That is because
770 * the same link table is shared among all the volumes of the volume group
771 * and is deleted only when the last volume of a volume group is deleted.
773 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
774 * link count from 0 through 7. Each colume represents a unique instance of
775 * that vnode. Say we have a file shared between the RW and a RO and a
776 * different version of the file (or a different uniquifer) for the BU volume.
777 * Then one column would be holding the link count of 2 for the RW and RO
778 * and a different column would hold the link count of 1 for the BU volume.
779 * Note that we allow only 5 volumes per file, giving 15 bits used in the
782 #define LINKTABLE_WIDTH 2
783 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
785 static void namei_GetLCOffsetAndIndexFromIno(Inode ino, int *offset, int *index)
787 int toff = (int) (ino & NAMEI_VNODEMASK);
788 int tindex = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
790 *offset = (toff << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
791 *index = (tindex << 1) + tindex;
795 /* namei_GetLinkCount
796 * If lockit is set, lock the file and leave it locked upon a successful
799 int namei_GetLinkCount(FdHandle_t *h, Inode ino, int lockit)
801 unsigned short row = 0;
805 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
808 if (flock(h->fd_fd, LOCK_EX)<0)
812 if (lseek(h->fd_fd, offset, SEEK_SET) == -1)
813 goto bad_getLinkByte;
815 if (read(h->fd_fd, (char*)&row, sizeof(row))!=sizeof(row)) {
816 goto bad_getLinkByte;
819 return (int) ((row >> index) & NAMEI_TAGMASK);
823 flock(h->fd_fd, LOCK_UN);
827 /* Return a free column index for this vnode. */
828 static int GetFreeTag(IHandle_t *ih, int vno)
842 /* Only one manipulates at a time. */
843 if (flock(fdP->fd_fd, LOCK_EX)<0) {
844 FDH_REALLYCLOSE(fdP);
848 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
849 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
853 code = read(fdP->fd_fd, (char*)&row, sizeof(row));
854 if (code != sizeof(row)) {
860 /* Now find a free column in this row and claim it. */
862 for (col = 0; col<NAMEI_MAXVOLS; col++) {
864 if ((row & coldata) == 0)
867 if (col >= NAMEI_MAXVOLS)
870 coldata = 1 << (col * 3);
873 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
876 if (write(fdP->fd_fd, (char*)&row, sizeof(row))!=sizeof(row)) {
880 flock(fdP->fd_fd, LOCK_UN);
881 FDH_REALLYCLOSE(fdP);
885 flock(fdP->fd_fd, LOCK_UN);
886 FDH_REALLYCLOSE(fdP);
892 /* namei_SetLinkCount
893 * If locked is set, assume file is locked. Otherwise, lock file before
894 * proceeding to modify it.
896 int namei_SetLinkCount(FdHandle_t *fdP, Inode ino, int count, int locked)
903 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
907 if (flock(fdP->fd_fd, LOCK_EX)<0) {
911 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
913 goto bad_SetLinkCount;
917 code = read(fdP->fd_fd, (char*)&row, sizeof(row));
918 if (code != sizeof(row)) {
921 goto bad_SetLinkCount;
928 row &= (unsigned short)~junk;
929 row |= (unsigned short)count;
931 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
933 goto bad_SetLinkCount;
936 if (write(fdP->fd_fd, (char*)&row, sizeof(short)) != sizeof(short)) {
938 goto bad_SetLinkCount;
946 flock(fdP->fd_fd, LOCK_UN);
952 /* ListViceInodes - write inode data to a results file. */
953 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
955 static int DecodeVolumeName(char *name, int *vid);
956 static int namei_ListAFSSubDirs(IHandle_t *dirIH,
957 int (*write_fun)(FILE *, struct ViceInodeInfo *,
960 int (*judgeFun)(struct ViceInodeInfo *, int vid),
961 int singleVolumeNumber);
966 * Write the inode data to the results file.
968 * Returns -2 on error, 0 on success.
970 * This is written as a callback simply so that other listing routines
971 * can use the same inode reading code.
973 static int WriteInodeInfo(FILE *fp, struct ViceInodeInfo *info, char *dir,
977 n = fwrite(info, sizeof(*info), 1, fp);
978 return (n == 1) ? 0 : -2;
982 int mode_errors; /* Number of errors found in mode bits on directories. */
983 void VerifyDirPerms(char *path)
987 if (stat(path, &status)<0) {
988 Log("Unable to stat %s. Please manually verify mode bits for this"
989 " directory\n", path);
992 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
998 * Fill the results file with the requested inode information.
1002 * -1 - complete failure, salvage should terminate.
1003 * -2 - not enough space on partition, salvager has error message for this.
1005 * This code optimizes single volume salvages by just looking at that one
1006 * volume's directory.
1008 * If the resultFile is NULL, then don't call the write routine.
1010 int ListViceInodes(char *devname, char *mountedOn, char *resultFile,
1011 int (*judgeInode)(struct ViceInodeInfo *info, int vid),
1012 int singleVolumeNumber, int *forcep,
1013 int forceR, char *wpath)
1015 FILE *fp = (FILE*)-1;
1020 fp = fopen(resultFile, "w");
1022 Log("Unable to create inode description file %s\n", resultFile);
1027 /* Verify protections on directories. */
1029 VerifyDirPerms(mountedOn);
1031 ninodes = namei_ListAFSFiles(mountedOn, WriteInodeInfo, fp,
1032 judgeInode, singleVolumeNumber);
1042 if (fflush(fp) == EOF) {
1043 Log("Unable to successfully flush inode file for %s\n", mountedOn);
1047 if (fsync(fileno(fp)) == -1) {
1048 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1052 if (fclose(fp) == EOF) {
1053 Log("Unable to successfully close inode file for %s\n", mountedOn);
1058 * Paranoia: check that the file is really the right size
1060 if (stat(resultFile, &status) == -1) {
1061 Log("Unable to successfully stat inode file for %s\n", mountedOn);
1064 if (status.st_size != ninodes * sizeof (struct ViceInodeInfo)) {
1065 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1066 status.st_size, ninodes * sizeof (struct ViceInodeInfo),
1074 /* namei_ListAFSFiles
1076 * Collect all the matching AFS files on the drive.
1077 * If singleVolumeNumber is non-zero, just return files for that volume.
1079 * Returns <0 on error, else number of files found to match.
1081 int namei_ListAFSFiles(char *dev,
1082 int (*writeFun)(FILE *, struct ViceInodeInfo *, char *,
1085 int (*judgeFun)(struct ViceInodeInfo *, int),
1086 int singleVolumeNumber)
1092 struct dirent *dp1, *dp2;
1095 static void FreeZLCList(void);
1098 memset((void*)&ih, 0, sizeof(IHandle_t));
1099 ih.ih_dev = volutil_GetPartitionID(dev);
1101 if (singleVolumeNumber) {
1102 ih.ih_vid = singleVolumeNumber;
1103 namei_HandleToVolDir(&name, &ih);
1104 ninodes = namei_ListAFSSubDirs(&ih, writeFun, fp,
1105 judgeFun, singleVolumeNumber);
1110 /* Find all volume data directories and descend through them. */
1111 namei_HandleToInodeDir(&name, &ih);
1113 dirp1 = opendir(name.n_path);
1116 while (dp1 = readdir(dirp1)) {
1117 if (*dp1->d_name == '.') continue;
1118 (void) strcpy(path2, name.n_path);
1119 (void) strcat(path2, "/");
1120 (void) strcat(path2, dp1->d_name);
1121 dirp2 = opendir(path2);
1123 while (dp2 = readdir(dirp2)) {
1124 if (*dp2->d_name == '.') continue;
1125 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1126 ninodes += namei_ListAFSSubDirs(&ih, writeFun, fp,
1143 /* namei_ListAFSSubDirs
1148 * > = 0 - number of AFS files found.
1150 static int namei_ListAFSSubDirs(IHandle_t *dirIH,
1151 int (*writeFun)(FILE *, struct ViceInodeInfo *,
1154 int (*judgeFun)(struct ViceInodeInfo *, int),
1155 int singleVolumeNumber)
1158 IHandle_t myIH = *dirIH;
1160 char path1[512], path2[512], path3[512];
1161 DIR *dirp1, *dirp2, *dirp3;
1162 struct dirent *dp1, *dp2, *dp3;
1164 struct ViceInodeInfo info;
1166 FdHandle_t linkHandle;
1169 static void AddToZLCDeleteList(char dir, char *name);
1170 static void DeleteZLCFiles(char *path);
1173 namei_HandleToVolDir(&name, &myIH);
1174 (void) strcpy(path1, name.n_path);
1176 /* Do the directory containing the special files first to pick up link
1179 (void) strcat(path1, "/");
1180 (void) strcat(path1, NAMEI_SPECDIR);
1182 linkHandle.fd_fd = -1;
1183 dirp1 = opendir(path1);
1185 while (dp1 = readdir(dirp1)) {
1186 if (*dp1->d_name == '.') continue;
1187 if (DecodeInode(path1, dp1->d_name, &info, myIH.ih_vid)<0)
1189 if (info.u.param[2] != VI_LINKTABLE) {
1193 /* Open this handle */
1194 (void) sprintf(path2, "%s/%s", path1, dp1->d_name);
1195 linkHandle.fd_fd = open(path2, O_RDONLY, 0666);
1196 info.linkCount = namei_GetLinkCount(&linkHandle, (Inode)0, 0);
1198 if (judgeFun && !(*judgeFun)(&info, singleVolumeNumber))
1201 if ((*writeFun)(fp, &info, path1, dp1->d_name)<0) {
1202 if (linkHandle.fd_fd >= 0)
1203 close(linkHandle.fd_fd);
1212 /* Now run through all the other subdirs */
1213 namei_HandleToVolDir(&name, &myIH);
1214 (void) strcpy(path1, name.n_path);
1216 dirp1 = opendir(path1);
1218 while (dp1 = readdir(dirp1)) {
1219 if (*dp1->d_name == '.') continue;
1220 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
1223 /* Now we've got a next level subdir. */
1224 (void) strcpy(path2, path1);
1225 (void) strcat(path2, "/");
1226 (void) strcat(path2, dp1->d_name);
1227 dirp2 = opendir(path2);
1229 while (dp2 = readdir(dirp2)) {
1230 if (*dp2->d_name == '.') continue;
1232 /* Now we've got to the actual data */
1233 (void) strcpy(path3, path2);
1234 (void) strcat(path3, "/");
1235 (void) strcat(path3, dp2->d_name);
1236 dirp3 = opendir(path3);
1238 while (dp3 = readdir(dirp3)) {
1239 if (*dp3->d_name == '.') continue;
1240 if (DecodeInode(path3, dp3->d_name, &info,
1243 info.linkCount = namei_GetLinkCount(&linkHandle,
1244 info.inodeNumber, 0);
1245 if (info.linkCount == 0) {
1247 Log("Found 0 link count file %s/%s, deleting it.\n",
1248 path3, dp3->d_name);
1249 AddToZLCDeleteList((char)i, dp3->d_name);
1251 Log("Found 0 link count file %s/%s.\n",
1252 path3, dp3->d_name);
1257 && !(*judgeFun)(&info, singleVolumeNumber))
1260 if ((*writeFun)(fp, &info, path3, dp3->d_name)<0) {
1261 close(linkHandle.fd_fd);
1278 if (linkHandle.fd_fd >= 0)
1279 close(linkHandle.fd_fd);
1281 /* Then why does this directory exist? Blow it away. */
1282 namei_HandleToVolDir(&name, dirIH);
1283 namei_RemoveDataDirectories(&name);
1289 static int DecodeVolumeName(char *name, int *vid)
1291 if (strlen(name) <= 2)
1293 *vid = (int) flipbase64_to_int64(name);
1300 * Get the inode number from the name.
1303 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1310 (void) strcpy(fpath, dpath);
1311 (void) strcat(fpath, "/");
1312 (void) strcat(fpath, name);
1314 if (stat(fpath, &status)<0) {
1318 info->byteCount = status.st_size;
1319 info->inodeNumber = flipbase64_to_int64(name);
1321 GetOGMFromStat(&status, &parm, &tag);
1322 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1323 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
1324 info->u.param[0] = parm;
1325 info->u.param[1] = -1;
1326 info->u.param[2] = tag;
1327 info->u.param[3] = volid;
1330 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
1331 info->u.param[0] = volid;
1332 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
1333 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
1334 & (Inode)NAMEI_UNIQMASK);
1335 info->u.param[3] = parm;
1343 * returns a static string used to print either 32 or 64 bit inode numbers.
1345 char * PrintInode(char *s, Inode ino)
1347 static afs_ino_str_t result;
1351 (void) sprintf((char*)s, "%Lu", ino);
1358 /* Routines to facilitate removing zero link count files. */
1359 #define MAX_ZLC_NAMES 32
1360 #define MAX_ZLC_NAMELEN 16
1361 typedef struct zlcList_s {
1362 struct zlcList_s *zlc_next;
1364 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1367 static zlcList_t *zlcAnchor = NULL;
1368 static zlcList_t *zlcCur = NULL;
1370 static void AddToZLCDeleteList(char dir, char *name)
1372 assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1374 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1375 if (zlcCur && zlcCur->zlc_next)
1376 zlcCur = zlcCur->zlc_next;
1378 zlcList_t *tmp = (zlcList_t*)malloc(sizeof(zlcList_t));
1385 zlcCur->zlc_next = tmp;
1389 zlcCur->zlc_next = NULL;
1393 (void) sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1397 static void DeleteZLCFiles(char *path)
1403 for (z = zlcAnchor; z; z = z->zlc_next) {
1404 for (i=0; i < z->zlc_n; i++) {
1405 (void) sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1406 if (namei_unlink(fname)<0) {
1407 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1411 z->zlc_n = 0; /* Can reuse space. */
1416 static void FreeZLCList(void)
1423 tnext = i->zlc_next;
1427 zlcCur = zlcAnchor = NULL;
1431 #endif /* AFS_NAMEI_ENV */