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>
24 #include <afs/assert.h>
27 #include <sys/param.h>
30 #include <sys/lockf.h>
35 #include <afs/afsutil.h>
38 #include <afs/afsint.h>
42 #include "viceinode.h"
44 #include "partition.h"
46 extern char *volutil_PartitionName_r(int volid, char *buf, int buflen);
48 int namei_iread(IHandle_t *h, int offset, char *buf, int size)
57 if (FDH_SEEK(fdP, offset, SEEK_SET)<0) {
62 nBytes = FDH_READ(fdP, buf, size);
67 int namei_iwrite(IHandle_t *h, int offset, char *buf, int size)
76 if (FDH_SEEK(fdP, offset, SEEK_SET)<0) {
80 nBytes = FDH_WRITE(fdP, buf, size);
87 /* Inode number format:
88 * low 26 bits - vnode number - all 1's if volume special file.
90 * next 3 bits spare (0's)
91 * high 32 bits - uniquifier (regular) or type if spare
93 #define NAMEI_VNODEMASK 0x003ffffff
94 #define NAMEI_TAGMASK 0x7
95 #define NAMEI_TAGSHIFT 26
96 #define NAMEI_UNIQMASK 0xffffffff
97 #define NAMEI_UNIQSHIFT 32
98 #define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
99 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
101 /* dir1 is the high 8 bits of the 26 bit vnode */
102 #define VNO_DIR1(vno) ((vno >> 14) & 0xff)
103 /* dir2 is the next 9 bits */
104 #define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
105 /* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
107 #define NAMEI_SPECDIR "special"
108 #define NAMEI_SPECDIRLEN (sizeof(NAMEI_SPECDIR)-1)
110 #define NAMEI_MAXVOLS 5 /* Maximum supported number of volumes per volume
111 * group, not counting temporary (move) volumes.
112 * This is the number of separate files, all having
113 * the same vnode number, which can occur in a volume
124 int namei_SetLinkCount(FdHandle_t *h, Inode ino, int count, int locked);
125 static int GetFreeTag(IHandle_t *ih, int vno);
127 /* namei_HandleToInodeDir
129 * Construct the path name of the directory holding the inode data.
130 * Format: /<vicepx>/INODEDIR
133 #define PNAME_BLEN 64
134 static void namei_HandleToInodeDir(namei_t *name, IHandle_t *ih)
136 char *tmp = name->n_base;
138 memset(name, '\0', sizeof(*name));
140 (void) volutil_PartitionName_r(ih->ih_dev, tmp, NAMEI_LCOMP_LEN);
141 tmp += VICE_PREFIX_SIZE;
142 tmp += ih->ih_dev > 25 ? 2 : 1;
144 (void) strcpy(tmp, INODEDIR);
145 (void) strcpy(name->n_path, name->n_base);
148 #define addtoname(N, C) \
150 strcat((N)->n_path, "/"); strcat((N)->n_path, C); \
154 static void namei_HandleToVolDir(namei_t *name, IHandle_t *ih)
158 namei_HandleToInodeDir(name, ih);
159 (void) int32_to_flipbase64(tmp, (int64_t)(ih->ih_vid & 0xff));
160 (void) strcpy(name->n_voldir1, tmp);
161 addtoname(name, name->n_voldir1);
162 (void) int32_to_flipbase64(tmp, (int64_t)ih->ih_vid);
163 (void) strcpy(name->n_voldir2, tmp);
164 addtoname(name, name->n_voldir2);
167 /* namei_HandleToName
169 * Constructs a file name for the fully qualified handle.
170 * Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special
172 void namei_HandleToName(namei_t *name, IHandle_t *ih)
175 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
177 namei_HandleToVolDir(name, ih);
179 if (vno == NAMEI_VNODESPECIAL) {
180 (void) strcpy(name->n_dir1, NAMEI_SPECDIR);
181 addtoname(name, name->n_dir1);
182 name->n_dir2[0] = '\0';
185 (void) int32_to_flipbase64(str, VNO_DIR1(vno));
186 (void) strcpy(name->n_dir1, str);
187 addtoname(name, name->n_dir1);
188 (void) int32_to_flipbase64(str, VNO_DIR2(vno));
189 (void) strcpy(name->n_dir2, str);
190 addtoname(name, name->n_dir2);
192 (void) int64_to_flipbase64(str, (int64_t)ih->ih_ino);
193 (void) strcpy(name->n_inode, str);
194 addtoname(name, name->n_inode);
197 /* The following is a warning to tell sys-admins to not muck about in this
200 #define VICE_README "These files and directories are a part of the AFS \
201 namespace. Modifying them\nin any way will result in loss of AFS data.\n"
202 int namei_ViceREADME(char *partition)
207 /* Create the inode directory if we're starting for the first time */
208 sprintf(filename, "%s/%s", partition, INODEDIR);
209 mkdir(filename, 0700);
211 sprintf(filename, "%s/%s/README", partition, INODEDIR);
212 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
214 write(fd, VICE_README, strlen(VICE_README));
221 #define create_dir() \
223 if (mkdir(tmp, 0700)<0) { \
224 if (errno != EEXIST) \
232 #define create_nextdir(A) \
234 strcat(tmp, "/"); strcat(tmp, A); create_dir(); \
237 /* namei_CreateDataDirectories
239 * If creating the file failed because of ENOENT or ENOTDIR, try
240 * creating all the directories first.
242 static int namei_CreateDataDirectories(namei_t *name, int *created)
248 (void) strcpy(tmp, name->n_base);
251 create_nextdir(name->n_voldir1);
252 create_nextdir(name->n_voldir2);
253 create_nextdir(name->n_dir1);
254 if (name->n_dir2[0]) {
255 create_nextdir(name->n_dir2);
260 /* delTree(): Deletes an entire tree of directories (no files)
262 * root : Full path to the subtree. Should be big enough for PATH_MAX
263 * tree : the subtree to be deleted is rooted here. Specifies only the
264 * subtree beginning at tree (not the entire path). It should be
265 * a pointer into the "root" buffer.
267 * errp : errno of the first error encountered during the directory cleanup.
268 * *errp should have been initialized to 0.
271 * -1 : If errors were encountered during cleanup and error is set to
275 * If there are errors, we try to work around them and delete as many
276 * directories as possible. We don't attempt to remove directories that still
277 * have non-dir entries in them.
280 delTree(char *root, char *tree, int *errp)
288 /* delete the children first */
289 cp = strchr(tree, '/');
291 delTree(root, cp+1, errp);
295 cp = tree + strlen(tree); /* move cp to the end of string tree */
297 /* now delete all entries in this dir */
298 if ( (ds = opendir(root)) != (DIR *)NULL) {
300 while ((dirp = readdir(ds))) {
301 /* ignore . and .. */
302 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
304 /* since root is big enough, we reuse the space to
305 * concatenate the dirname to the current tree
308 strcat(root, dirp->d_name);
309 if ( stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
310 /* delete this subtree */
311 delTree(root, cp+1, errp);
313 *errp = *errp ? *errp : errno;
315 /* recover path to our cur tree by truncating it to
320 /* if (!errno) -- closedir not implicit if we got an error */
324 /* finally axe the current dir */
326 *errp = *errp ? *errp : errno;
328 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
330 #endif /* !AFS_PTHREAD_ENV */
332 } /* if valid tree */
334 /* if we encountered errors during cleanup, we return a -1 */
342 /* namei_RemoveDataDirectories
344 * Returns 0 on success.
345 * Returns -1 on error. Typically, callers ignore this error bcause we
346 * can continue running if the removes fail. The salvage process will
347 * finish tidying up for us. We only use the n_base and n_voldir1 entries
348 * and only do rmdir's.
351 static int namei_RemoveDataDirectories(namei_t *name)
353 char pbuf[MAXPATHLEN], *path = pbuf;
354 int prefixlen = strlen(name->n_base), err = 0;
356 strcpy(path, name->n_path);
358 /* move past the prefix */
359 path = path+prefixlen+1; /* skip over the trailing / */
361 /* now delete all dirs upto path */
362 return delTree(pbuf, path, &err);
366 /* Create the file in the name space.
368 * Parameters stored as follows:
370 * p1 - volid - implied in containing directory.
371 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
372 * p3 - uniq -- bits 4-0 are in mode bits 4-0
373 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
375 * p1 - volid - creation time - dwHighDateTime
376 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
377 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
378 * p4 - parid - parent volume id - implied in containing directory.
380 * Return value is the inode number or (Inode)-1 if error.
381 * We "know" there is only one link table, so return EEXIST if there already
382 * is a link table. It's up to the calling code to test errno and increment
388 * This function is called by VCreateVolume to hide the implementation
389 * details of the inode numbers. This only allows for 7 volume special
390 * types, but if we get that far, this could should be dead by then.
392 Inode namei_MakeSpecIno(int volid, int type)
395 ino = NAMEI_INODESPECIAL;
396 type &= NAMEI_TAGMASK;
397 ino |= ((Inode)type) << NAMEI_TAGSHIFT;
398 ino |= ((Inode)volid) << NAMEI_UNIQSHIFT;
402 /* SetOGM - set owner group and mode bits from parm and tag
404 * owner - low 15 bits of parm.
405 * group - next 15 bits of parm.
406 * mode - 2 bits of parm, then lowest = 3 bits of tag.
408 static int SetOGM(int fd, int parm, int tag)
410 int owner, group, mode;
412 owner = parm & 0x7fff;
413 group = (parm >> 15) & 0x7fff;
414 if (fchown(fd, owner, group)<0)
417 mode = (parm >> 27) & 0x18;
419 if (fchmod(fd, mode)<0)
426 /* GetOGM - get parm and tag from owner, group and mode bits. */
427 static void GetOGMFromStat(struct stat *status, int *parm, int *tag)
429 *parm = status->st_uid | (status->st_gid << 15);
430 *parm |= (status->st_mode & 0x18) << 27;
431 *tag = status->st_mode & 0x7;
434 static int GetOGM(int fd, int *parm, int *tag)
437 if (fstat(fd, &status)<0)
440 GetOGMFromStat(&status, parm, tag);
444 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
446 /* Derive the name and create it O_EXCL. If that fails we have an error.
447 * Get the tag from a free column in the link table.
449 Inode namei_icreate(IHandle_t *lh, char *part, int p1, int p2, int p3, int p4)
462 memset((void*)&tmp, 0, sizeof(IHandle_t));
465 tmp.ih_dev = volutil_GetPartitionID(part);
466 if (tmp.ih_dev == -1) {
472 /* Parameters for special file:
473 * p1 - volume id - goes into owner/group/mode
476 * p4 - parent volume id
481 tmp.ih_vid = p4; /* Use parent volume id, where this file will be.*/
482 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
485 int vno = p2 & NAMEI_VNODEMASK;
486 /* Parameters for regular file:
498 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
499 tag = GetFreeTag(lh, p2);
503 /* name is <uniq(p3)><tag><vno(p2)> */
505 tmp.ih_ino = (Inode)p2;
506 tmp.ih_ino |= ((Inode)tag)<<NAMEI_TAGSHIFT;
507 tmp.ih_ino |= ((Inode)p3)<<NAMEI_UNIQSHIFT;
512 namei_HandleToName(&name, &tmp);
513 fd = open(name.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
515 if (errno == ENOTDIR || errno == ENOENT) {
516 if (namei_CreateDataDirectories(&name, &created_dir)<0)
518 fd = open(name.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
526 if (SetOGM(fd, ogm_parm, tag)<0) {
532 if (p2 == -1 && p3 == VI_LINKTABLE) {
533 /* hack at tmp to setup for set link count call. */
535 code = namei_SetLinkCount(&tfd, (Inode)0, 1, 0);
543 if (code || (fd < 0)) {
547 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
552 return (code || (fd<0)) ? (Inode)-1 : tmp.ih_ino;
557 int namei_iopen(IHandle_t *h)
562 /* Convert handle to file name. */
563 namei_HandleToName(&name, h);
564 fd = open(name.n_path, O_RDWR, 0666);
568 /* Need to detect vol special file and just unlink. In those cases, the
569 * handle passed in _is_ for the inode. We only check p1 for the special
572 int namei_dec(IHandle_t *ih, Inode ino, int p1)
579 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
582 int type = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
584 /* Verify this is the right file. */
585 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
594 if ((GetOGM(fdP->fd_fd, &inode_p1, &tag)<0) || (inode_p1 != p1)) {
595 FDH_REALLYCLOSE(fdP);
601 /* If it's the link table itself, decrement the link count. */
602 if (type == VI_LINKTABLE) {
603 if ((count = namei_GetLinkCount(fdP, (Inode)0, 1))<0) {
604 FDH_REALLYCLOSE(fdP);
610 if (namei_SetLinkCount(fdP, (Inode)0, count<0 ? 0 : count, 1)<0) {
611 FDH_REALLYCLOSE(fdP);
617 FDH_REALLYCLOSE(fdP);
623 namei_HandleToName(&name, tmp);
624 if ((code = unlink(name.n_path)) == 0) {
625 if (type == VI_LINKTABLE) {
626 /* Try to remove directory. If it fails, that's ok.
627 * Salvage will clean up.
629 (void) namei_RemoveDataDirectories(&name);
632 FDH_REALLYCLOSE(fdP);
636 /* Get a file descriptor handle for this Inode */
642 if ((count = namei_GetLinkCount(fdP, ino, 1))<0) {
643 FDH_REALLYCLOSE(fdP);
649 if (namei_SetLinkCount(fdP, ino, count, 1)<0) {
650 FDH_REALLYCLOSE(fdP);
657 namei_HandleToName(&name, &th);
658 code = unlink(name.n_path);
666 int namei_inc(IHandle_t *h, Inode ino, int p1)
672 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
673 int type = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
674 if (type != VI_LINKTABLE)
679 /* Get a file descriptor handle for this Inode */
685 if ((count = namei_GetLinkCount(fdP, ino, 1))<0)
694 if (namei_SetLinkCount(fdP, ino, count, 1)<0)
698 FDH_REALLYCLOSE(fdP);
705 /************************************************************************
706 * File Name Structure
707 ************************************************************************
709 * Each AFS file needs a unique name and it needs to be findable with
710 * minimal lookup time. Note that the constraint on the number of files and
711 * directories in a volume is the size of the vnode index files and the
712 * max file size AFS supports (for internal files) of 2^31. Since a record
713 * in the small vnode index file is 64 bytes long, we can have at most
714 * (2^31)/64 or 33554432 files. A record in the large index file is
715 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
716 * Another layout parameter is that there is roughly a 16 to 1 ratio between
717 * the number of files and the number of directories.
719 * Using this information we can see that a layout of 256 directories, each
720 * with 512 subdirectories and each of those having 512 files gives us
721 * 256*512*512 = 67108864 AFS files and directories.
723 * The volume, vnode, uniquifier and data version, as well as the tag
724 * are required, either for finding the file or for salvaging. It's best to
725 * restrict the name to something that can be mapped into 64 bits so the
726 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
727 * is used to distinguish between different versions of the same file
728 * which are currently in the RW and clones of a volume. See "Link Table
729 * Organization" below for more information on the tag. The tag is
730 * required in the name of the file to ensure a unique name.
732 * We can store data in the uid, gid and mode bits of the files, provided
733 * the directories have root only access. This gives us 15 bits for each
734 * of uid and gid (GNU chown considers 65535 to mean "don't change").
735 * There are 9 available mode bits. Adn we need to store a total of
736 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
737 * or 131 bits somewhere.
739 * The format of a file name for a regular file is:
740 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
741 * V1 - low 8 bits of RW volume id
742 * V2 - all bits of RW volume id
743 * AA - high 8 bits of vnode number.
744 * BB - next 9 bits of vnode number.
745 * <tag><uniq><vno> - file name
747 * Volume special files are stored in a separate directory:
748 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
751 * The vnode is hashed into the directory using the high bits of the
752 * vnode number. This is so that consecutively created vnodes are in
753 * roughly the same area on the disk. This will at least be optimal if
754 * the user is creating many files in the same AFS directory. The name
755 * should be formed so that the leading characters are different as quickly
756 * as possible, leading to faster discards of incorrect matches in the
762 /************************************************************************
763 * Link Table Organization
764 ************************************************************************
766 * The link table volume special file is used to hold the link counts that
767 * are held in the inodes in inode based AFS vice filesystems. For user
768 * space access, the link counts are being kept in a separate
769 * volume special file. The file begins with the usual version stamp
770 * information and is then followed by one row per vnode number. vnode 0
771 * is used to hold the link count of the link table itself. That is because
772 * the same link table is shared among all the volumes of the volume group
773 * and is deleted only when the last volume of a volume group is deleted.
775 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
776 * link count from 0 through 7. Each colume represents a unique instance of
777 * that vnode. Say we have a file shared between the RW and a RO and a
778 * different version of the file (or a different uniquifer) for the BU volume.
779 * Then one column would be holding the link count of 2 for the RW and RO
780 * and a different column would hold the link count of 1 for the BU volume.
781 * Note that we allow only 5 volumes per file, giving 15 bits used in the
784 #define LINKTABLE_WIDTH 2
785 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
787 static void namei_GetLCOffsetAndIndexFromIno(Inode ino, int *offset, int *index)
789 int toff = (int) (ino & NAMEI_VNODEMASK);
790 int tindex = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
792 *offset = (toff << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
793 *index = (tindex << 1) + tindex;
797 /* namei_GetLinkCount
798 * If lockit is set, lock the file and leave it locked upon a successful
801 int namei_GetLinkCount(FdHandle_t *h, Inode ino, int lockit)
803 unsigned short row = 0;
806 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
809 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
810 if (lockf(h->fd_fd, F_LOCK, 0) < 0)
812 if (flock(h->fd_fd, LOCK_EX)<0)
817 if (lseek(h->fd_fd, offset, SEEK_SET) == -1)
818 goto bad_getLinkByte;
820 if (read(h->fd_fd, (char*)&row, sizeof(row))!=sizeof(row)) {
821 goto bad_getLinkByte;
824 return (int) ((row >> index) & NAMEI_TAGMASK);
828 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
829 lockf(h->fd_fd, F_ULOCK, 0);
831 flock(h->fd_fd, LOCK_UN);
836 /* Return a free column index for this vnode. */
837 static int GetFreeTag(IHandle_t *ih, int vno)
851 /* Only one manipulates at a time. */
852 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
853 if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
855 if (flock(fdP->fd_fd, LOCK_EX)<0) {
857 FDH_REALLYCLOSE(fdP);
861 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
862 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
866 code = read(fdP->fd_fd, (char*)&row, sizeof(row));
867 if (code != sizeof(row)) {
873 /* Now find a free column in this row and claim it. */
874 for (col = 0; col<NAMEI_MAXVOLS; col++) {
875 coldata = 7 << (col * 3);
876 if ((row & coldata) == 0)
879 if (col >= NAMEI_MAXVOLS)
882 coldata = 1 << (col * 3);
885 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
888 if (write(fdP->fd_fd, (char*)&row, sizeof(row))!=sizeof(row)) {
892 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
893 lockf(fdP->fd_fd, F_ULOCK, 0);
895 flock(fdP->fd_fd, LOCK_UN);
897 FDH_REALLYCLOSE(fdP);
901 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
902 lockf(fdP->fd_fd, F_ULOCK, 0);
904 flock(fdP->fd_fd, LOCK_UN);
906 FDH_REALLYCLOSE(fdP);
912 /* namei_SetLinkCount
913 * If locked is set, assume file is locked. Otherwise, lock file before
914 * proceeding to modify it.
916 int namei_SetLinkCount(FdHandle_t *fdP, Inode ino, int count, int locked)
923 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
926 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
927 if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
929 if (flock(fdP->fd_fd, LOCK_EX)<0) {
934 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
936 goto bad_SetLinkCount;
940 code = read(fdP->fd_fd, (char*)&row, sizeof(row));
941 if (code != sizeof(row)) {
944 goto bad_SetLinkCount;
951 row &= (unsigned short)~junk;
952 row |= (unsigned short)count;
954 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
956 goto bad_SetLinkCount;
959 if (write(fdP->fd_fd, (char*)&row, sizeof(short)) != sizeof(short)) {
961 goto bad_SetLinkCount;
969 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
970 lockf(fdP->fd_fd, F_ULOCK, 0);
972 flock(fdP->fd_fd, LOCK_UN);
979 /* ListViceInodes - write inode data to a results file. */
980 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
982 static int DecodeVolumeName(char *name, int *vid);
983 static int namei_ListAFSSubDirs(IHandle_t *dirIH,
984 int (*write_fun)(FILE *, struct ViceInodeInfo *,
987 int (*judgeFun)(struct ViceInodeInfo *, int vid),
988 int singleVolumeNumber);
993 * Write the inode data to the results file.
995 * Returns -2 on error, 0 on success.
997 * This is written as a callback simply so that other listing routines
998 * can use the same inode reading code.
1000 static int WriteInodeInfo(FILE *fp, struct ViceInodeInfo *info, char *dir,
1004 n = fwrite(info, sizeof(*info), 1, fp);
1005 return (n == 1) ? 0 : -2;
1009 int mode_errors; /* Number of errors found in mode bits on directories. */
1010 void VerifyDirPerms(char *path)
1014 if (stat(path, &status)<0) {
1015 Log("Unable to stat %s. Please manually verify mode bits for this"
1016 " directory\n", path);
1019 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1025 * Fill the results file with the requested inode information.
1029 * -1 - complete failure, salvage should terminate.
1030 * -2 - not enough space on partition, salvager has error message for this.
1032 * This code optimizes single volume salvages by just looking at that one
1033 * volume's directory.
1035 * If the resultFile is NULL, then don't call the write routine.
1037 int ListViceInodes(char *devname, char *mountedOn, char *resultFile,
1038 int (*judgeInode)(struct ViceInodeInfo *info, int vid),
1039 int singleVolumeNumber, int *forcep,
1040 int forceR, char *wpath)
1042 FILE *fp = (FILE*)-1;
1047 fp = fopen(resultFile, "w");
1049 Log("Unable to create inode description file %s\n", resultFile);
1054 /* Verify protections on directories. */
1056 VerifyDirPerms(mountedOn);
1058 ninodes = namei_ListAFSFiles(mountedOn, WriteInodeInfo, fp,
1059 judgeInode, singleVolumeNumber);
1069 if (fflush(fp) == EOF) {
1070 Log("Unable to successfully flush inode file for %s\n", mountedOn);
1074 if (fsync(fileno(fp)) == -1) {
1075 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1079 if (fclose(fp) == EOF) {
1080 Log("Unable to successfully close inode file for %s\n", mountedOn);
1085 * Paranoia: check that the file is really the right size
1087 if (stat(resultFile, &status) == -1) {
1088 Log("Unable to successfully stat inode file for %s\n", mountedOn);
1091 if (status.st_size != ninodes * sizeof (struct ViceInodeInfo)) {
1092 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1093 status.st_size, ninodes * sizeof (struct ViceInodeInfo),
1101 /* namei_ListAFSFiles
1103 * Collect all the matching AFS files on the drive.
1104 * If singleVolumeNumber is non-zero, just return files for that volume.
1106 * Returns <0 on error, else number of files found to match.
1108 int namei_ListAFSFiles(char *dev,
1109 int (*writeFun)(FILE *, struct ViceInodeInfo *, char *,
1112 int (*judgeFun)(struct ViceInodeInfo *, int),
1113 int singleVolumeNumber)
1119 struct dirent *dp1, *dp2;
1122 static void FreeZLCList(void);
1125 memset((void*)&ih, 0, sizeof(IHandle_t));
1126 ih.ih_dev = volutil_GetPartitionID(dev);
1128 if (singleVolumeNumber) {
1129 ih.ih_vid = singleVolumeNumber;
1130 namei_HandleToVolDir(&name, &ih);
1131 ninodes = namei_ListAFSSubDirs(&ih, writeFun, fp,
1132 judgeFun, singleVolumeNumber);
1137 /* Find all volume data directories and descend through them. */
1138 namei_HandleToInodeDir(&name, &ih);
1140 dirp1 = opendir(name.n_path);
1143 while ((dp1 = readdir(dirp1))) {
1144 if (*dp1->d_name == '.') continue;
1145 (void) strcpy(path2, name.n_path);
1146 (void) strcat(path2, "/");
1147 (void) strcat(path2, dp1->d_name);
1148 dirp2 = opendir(path2);
1150 while ((dp2 = readdir(dirp2))) {
1151 if (*dp2->d_name == '.') continue;
1152 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1153 ninodes += namei_ListAFSSubDirs(&ih, writeFun, fp,
1170 /* namei_ListAFSSubDirs
1175 * > = 0 - number of AFS files found.
1177 static int namei_ListAFSSubDirs(IHandle_t *dirIH,
1178 int (*writeFun)(FILE *, struct ViceInodeInfo *,
1181 int (*judgeFun)(struct ViceInodeInfo *, int),
1182 int singleVolumeNumber)
1184 IHandle_t myIH = *dirIH;
1186 char path1[512], path2[512], path3[512];
1187 DIR *dirp1, *dirp2, *dirp3;
1188 struct dirent *dp1, *dp2, *dp3;
1189 struct ViceInodeInfo info;
1190 FdHandle_t linkHandle;
1194 static void AddToZLCDeleteList(char dir, char *name);
1195 static void DeleteZLCFiles(char *path);
1198 namei_HandleToVolDir(&name, &myIH);
1199 (void) strcpy(path1, name.n_path);
1201 /* Do the directory containing the special files first to pick up link
1204 (void) strcat(path1, "/");
1205 (void) strcat(path1, NAMEI_SPECDIR);
1207 linkHandle.fd_fd = -1;
1208 dirp1 = opendir(path1);
1210 while ((dp1 = readdir(dirp1))) {
1211 if (*dp1->d_name == '.') continue;
1212 if (DecodeInode(path1, dp1->d_name, &info, myIH.ih_vid)<0)
1214 if (info.u.param[2] != VI_LINKTABLE) {
1218 /* Open this handle */
1219 (void) sprintf(path2, "%s/%s", path1, dp1->d_name);
1220 linkHandle.fd_fd = open(path2, O_RDONLY, 0666);
1221 info.linkCount = namei_GetLinkCount(&linkHandle, (Inode)0, 0);
1223 if (judgeFun && !(*judgeFun)(&info, singleVolumeNumber))
1226 if ((*writeFun)(fp, &info, path1, dp1->d_name)<0) {
1227 if (linkHandle.fd_fd >= 0)
1228 close(linkHandle.fd_fd);
1237 /* Now run through all the other subdirs */
1238 namei_HandleToVolDir(&name, &myIH);
1239 (void) strcpy(path1, name.n_path);
1241 dirp1 = opendir(path1);
1243 while ((dp1 = readdir(dirp1))) {
1244 if (*dp1->d_name == '.') continue;
1245 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
1248 /* Now we've got a next level subdir. */
1249 (void) strcpy(path2, path1);
1250 (void) strcat(path2, "/");
1251 (void) strcat(path2, dp1->d_name);
1252 dirp2 = opendir(path2);
1254 while ((dp2 = readdir(dirp2))) {
1255 if (*dp2->d_name == '.') continue;
1257 /* Now we've got to the actual data */
1258 (void) strcpy(path3, path2);
1259 (void) strcat(path3, "/");
1260 (void) strcat(path3, dp2->d_name);
1261 dirp3 = opendir(path3);
1263 while ((dp3 = readdir(dirp3))) {
1264 if (*dp3->d_name == '.') continue;
1265 if (DecodeInode(path3, dp3->d_name, &info,
1268 info.linkCount = namei_GetLinkCount(&linkHandle,
1269 info.inodeNumber, 0);
1270 if (info.linkCount == 0) {
1272 Log("Found 0 link count file %s/%s, deleting it.\n",
1273 path3, dp3->d_name);
1274 AddToZLCDeleteList((char)i, dp3->d_name);
1276 Log("Found 0 link count file %s/%s.\n",
1277 path3, dp3->d_name);
1282 && !(*judgeFun)(&info, singleVolumeNumber))
1285 if ((*writeFun)(fp, &info, path3, dp3->d_name)<0) {
1286 close(linkHandle.fd_fd);
1303 if (linkHandle.fd_fd >= 0)
1304 close(linkHandle.fd_fd);
1306 /* Then why does this directory exist? Blow it away. */
1307 namei_HandleToVolDir(&name, dirIH);
1308 namei_RemoveDataDirectories(&name);
1314 static int DecodeVolumeName(char *name, int *vid)
1316 if (strlen(name) <= 2)
1318 *vid = (int) flipbase64_to_int64(name);
1325 * Get the inode number from the name.
1328 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1335 (void) strcpy(fpath, dpath);
1336 (void) strcat(fpath, "/");
1337 (void) strcat(fpath, name);
1339 if (stat(fpath, &status)<0) {
1343 info->byteCount = status.st_size;
1344 info->inodeNumber = flipbase64_to_int64(name);
1346 GetOGMFromStat(&status, &parm, &tag);
1347 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1348 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
1349 info->u.param[0] = parm;
1350 info->u.param[1] = -1;
1351 info->u.param[2] = tag;
1352 info->u.param[3] = volid;
1355 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
1356 info->u.param[0] = volid;
1357 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
1358 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
1359 & (Inode)NAMEI_UNIQMASK);
1360 info->u.param[3] = parm;
1368 * returns a static string used to print either 32 or 64 bit inode numbers.
1370 char * PrintInode(char *s, Inode ino)
1372 static afs_ino_str_t result;
1376 (void) sprintf((char*)s, "%Lu", ino);
1383 /* Routines to facilitate removing zero link count files. */
1384 #define MAX_ZLC_NAMES 32
1385 #define MAX_ZLC_NAMELEN 16
1386 typedef struct zlcList_s {
1387 struct zlcList_s *zlc_next;
1389 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1392 static zlcList_t *zlcAnchor = NULL;
1393 static zlcList_t *zlcCur = NULL;
1395 static void AddToZLCDeleteList(char dir, char *name)
1397 assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1399 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1400 if (zlcCur && zlcCur->zlc_next)
1401 zlcCur = zlcCur->zlc_next;
1403 zlcList_t *tmp = (zlcList_t*)malloc(sizeof(zlcList_t));
1410 zlcCur->zlc_next = tmp;
1414 zlcCur->zlc_next = NULL;
1418 (void) sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1422 static void DeleteZLCFiles(char *path)
1428 for (z = zlcAnchor; z; z = z->zlc_next) {
1429 for (i=0; i < z->zlc_n; i++) {
1430 (void) sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1431 if (namei_unlink(fname)<0) {
1432 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1436 z->zlc_n = 0; /* Can reuse space. */
1441 static void FreeZLCList(void)
1448 tnext = i->zlc_next;
1452 zlcCur = zlcAnchor = NULL;
1456 #endif /* AFS_NAMEI_ENV */