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>
26 #include <sys/lockf.h>
31 #include <afs/afsutil.h>
34 #include <afs/afsint.h>
38 #include "viceinode.h"
40 #include "partition.h"
42 extern char *volutil_PartitionName_r(int volid, char *buf, int buflen);
44 int namei_iread(IHandle_t *h, int offset, char *buf, int size)
53 if (FDH_SEEK(fdP, offset, SEEK_SET)<0) {
58 nBytes = FDH_READ(fdP, buf, size);
63 int namei_iwrite(IHandle_t *h, int offset, char *buf, int size)
72 if (FDH_SEEK(fdP, offset, SEEK_SET)<0) {
76 nBytes = FDH_WRITE(fdP, buf, size);
83 /* Inode number format:
84 * low 26 bits - vnode number - all 1's if volume special file.
86 * next 3 bits spare (0's)
87 * high 32 bits - uniquifier (regular) or type if spare
89 #define NAMEI_VNODEMASK 0x003ffffff
90 #define NAMEI_TAGMASK 0x7
91 #define NAMEI_TAGSHIFT 26
92 #define NAMEI_UNIQMASK 0xffffffff
93 #define NAMEI_UNIQSHIFT 32
94 #define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
95 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
97 /* dir1 is the high 8 bits of the 26 bit vnode */
98 #define VNO_DIR1(vno) ((vno >> 14) & 0xff)
99 /* dir2 is the next 9 bits */
100 #define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
101 /* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
103 #define NAMEI_SPECDIR "special"
104 #define NAMEI_SPECDIRLEN (sizeof(NAMEI_SPECDIR)-1)
106 #define NAMEI_MAXVOLS 5 /* Maximum supported number of volumes per volume
107 * group, not counting temporary (move) volumes.
108 * This is the number of separate files, all having
109 * the same vnode number, which can occur in a volume
120 int namei_SetLinkCount(FdHandle_t *h, Inode ino, int count, int locked);
121 static int GetFreeTag(IHandle_t *ih, int vno);
123 /* namei_HandleToInodeDir
125 * Construct the path name of the directory holding the inode data.
126 * Format: /<vicepx>/INODEDIR
129 #define PNAME_BLEN 64
130 static void namei_HandleToInodeDir(namei_t *name, IHandle_t *ih)
132 char *tmp = name->n_base;
134 memset(name, '\0', sizeof(*name));
136 (void) volutil_PartitionName_r(ih->ih_dev, tmp, NAMEI_LCOMP_LEN);
137 tmp += VICE_PREFIX_SIZE;
138 tmp += ih->ih_dev > 25 ? 2 : 1;
140 (void) strcpy(tmp, INODEDIR);
141 (void) strcpy(name->n_path, name->n_base);
144 #define addtoname(N, C) \
146 strcat((N)->n_path, "/"); strcat((N)->n_path, C); \
150 static void namei_HandleToVolDir(namei_t *name, IHandle_t *ih)
154 namei_HandleToInodeDir(name, ih);
155 (void) int32_to_flipbase64(tmp, (int64_t)(ih->ih_vid & 0xff));
156 (void) strcpy(name->n_voldir1, tmp);
157 addtoname(name, name->n_voldir1);
158 (void) int32_to_flipbase64(tmp, (int64_t)ih->ih_vid);
159 (void) strcpy(name->n_voldir2, tmp);
160 addtoname(name, name->n_voldir2);
163 /* namei_HandleToName
165 * Constructs a file name for the fully qualified handle.
166 * Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special
168 void namei_HandleToName(namei_t *name, IHandle_t *ih)
171 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
173 namei_HandleToVolDir(name, ih);
175 if (vno == NAMEI_VNODESPECIAL) {
176 (void) strcpy(name->n_dir1, NAMEI_SPECDIR);
177 addtoname(name, name->n_dir1);
178 name->n_dir2[0] = '\0';
181 (void) int32_to_flipbase64(str, VNO_DIR1(vno));
182 (void) strcpy(name->n_dir1, str);
183 addtoname(name, name->n_dir1);
184 (void) int32_to_flipbase64(str, VNO_DIR2(vno));
185 (void) strcpy(name->n_dir2, str);
186 addtoname(name, name->n_dir2);
188 (void) int64_to_flipbase64(str, (int64_t)ih->ih_ino);
189 (void) strcpy(name->n_inode, str);
190 addtoname(name, name->n_inode);
193 /* The following is a warning to tell sys-admins to not muck about in this
196 #define VICE_README "These files and directories are a part of the AFS \
197 namespace. Modifying them\nin any way will result in loss of AFS data.\n"
198 int namei_ViceREADME(char *partition)
203 sprintf(filename, "%s/%s/README", partition, INODEDIR);
204 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
206 write(fd, VICE_README, strlen(VICE_README));
213 #define create_dir() \
215 if (mkdir(tmp, 0700)<0) { \
216 if (errno != EEXIST) \
224 #define create_nextdir(A) \
226 strcat(tmp, "/"); strcat(tmp, A); create_dir(); \
229 /* namei_CreateDataDirectories
231 * If creating the file failed because of ENOENT or ENOTDIR, try
232 * creating all the directories first.
234 static int namei_CreateDataDirectories(namei_t *name, int *created)
240 (void) strcpy(tmp, name->n_base);
243 create_nextdir(name->n_voldir1);
244 create_nextdir(name->n_voldir2);
245 create_nextdir(name->n_dir1);
246 if (name->n_dir2[0]) {
247 create_nextdir(name->n_dir2);
252 /* delTree(): Deletes an entire tree of directories (no files)
254 * root : Full path to the subtree. Should be big enough for PATH_MAX
255 * tree : the subtree to be deleted is rooted here. Specifies only the
256 * subtree beginning at tree (not the entire path). It should be
257 * a pointer into the "root" buffer.
259 * errp : errno of the first error encountered during the directory cleanup.
260 * *errp should have been initialized to 0.
263 * -1 : If errors were encountered during cleanup and error is set to
267 * If there are errors, we try to work around them and delete as many
268 * directories as possible. We don't attempt to remove directories that still
269 * have non-dir entries in them.
272 delTree(char *root, char *tree, int *errp)
280 /* delete the children first */
281 cp = strchr(tree, '/');
283 delTree(root, cp+1, errp);
287 cp = tree + strlen(tree); /* move cp to the end of string tree */
289 /* now delete all entries in this dir */
290 if ( (ds = opendir(root)) != (DIR *)NULL) {
292 while ((dirp = readdir(ds))) {
293 /* ignore . and .. */
294 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
296 /* since root is big enough, we reuse the space to
297 * concatenate the dirname to the current tree
300 strcat(root, dirp->d_name);
301 if ( stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
302 /* delete this subtree */
303 delTree(root, cp+1, errp);
305 *errp = *errp ? *errp : errno;
307 /* recover path to our cur tree by truncating it to
316 /* finally axe the current dir */
318 *errp = *errp ? *errp : errno;
320 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
322 #endif /* !AFS_PTHREAD_ENV */
324 } /* if valid tree */
326 /* if we encountered errors during cleanup, we return a -1 */
334 /* namei_RemoveDataDirectories
336 * Returns 0 on success.
337 * Returns -1 on error. Typically, callers ignore this error bcause we
338 * can continue running if the removes fail. The salvage process will
339 * finish tidying up for us. We only use the n_base and n_voldir1 entries
340 * and only do rmdir's.
343 static int namei_RemoveDataDirectories(namei_t *name)
345 char pbuf[MAXPATHLEN], *path = pbuf;
346 int prefixlen = strlen(name->n_base), err = 0;
348 strcpy(path, name->n_path);
350 /* move past the prefix */
351 path = path+prefixlen+1; /* skip over the trailing / */
353 /* now delete all dirs upto path */
354 return delTree(pbuf, path, &err);
358 /* Create the file in the name space.
360 * Parameters stored as follows:
362 * p1 - volid - implied in containing directory.
363 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
364 * p3 - uniq -- bits 4-0 are in mode bits 4-0
365 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
367 * p1 - volid - creation time - dwHighDateTime
368 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
369 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
370 * p4 - parid - parent volume id - implied in containing directory.
372 * Return value is the inode number or (Inode)-1 if error.
373 * We "know" there is only one link table, so return EEXIST if there already
374 * is a link table. It's up to the calling code to test errno and increment
380 * This function is called by VCreateVolume to hide the implementation
381 * details of the inode numbers. This only allows for 7 volume special
382 * types, but if we get that far, this could should be dead by then.
384 Inode namei_MakeSpecIno(int volid, int type)
387 ino = NAMEI_INODESPECIAL;
388 type &= NAMEI_TAGMASK;
389 ino |= ((Inode)type) << NAMEI_TAGSHIFT;
390 ino |= ((Inode)volid) << NAMEI_UNIQSHIFT;
394 /* SetOGM - set owner group and mode bits from parm and tag
396 * owner - low 15 bits of parm.
397 * group - next 15 bits of parm.
398 * mode - 2 bits of parm, then lowest = 3 bits of tag.
400 static int SetOGM(int fd, int parm, int tag)
402 int owner, group, mode;
404 owner = parm & 0x7fff;
405 group = (parm >> 15) & 0x7fff;
406 if (fchown(fd, owner, group)<0)
409 mode = (parm >> 27) & 0x18;
411 if (fchmod(fd, mode)<0)
418 /* GetOGM - get parm and tag from owner, group and mode bits. */
419 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)
454 memset((void*)&tmp, 0, sizeof(IHandle_t));
457 tmp.ih_dev = volutil_GetPartitionID(part);
458 if (tmp.ih_dev == -1) {
464 /* Parameters for special file:
465 * p1 - volume id - goes into owner/group/mode
468 * p4 - parent volume id
473 tmp.ih_vid = p4; /* Use parent volume id, where this file will be.*/
474 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
477 int vno = p2 & NAMEI_VNODEMASK;
478 /* Parameters for regular file:
490 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
491 tag = GetFreeTag(lh, p2);
495 /* name is <uniq(p3)><tag><vno(p2)> */
497 tmp.ih_ino = (Inode)p2;
498 tmp.ih_ino |= ((Inode)tag)<<NAMEI_TAGSHIFT;
499 tmp.ih_ino |= ((Inode)p3)<<NAMEI_UNIQSHIFT;
504 namei_HandleToName(&name, &tmp);
505 fd = open(name.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
507 if (errno == ENOTDIR || errno == ENOENT) {
508 if (namei_CreateDataDirectories(&name, &created_dir)<0)
510 fd = open(name.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
518 if (SetOGM(fd, ogm_parm, tag)<0) {
524 if (p2 == -1 && p3 == VI_LINKTABLE) {
525 /* hack at tmp to setup for set link count call. */
527 code = namei_SetLinkCount(&tfd, (Inode)0, 1, 0);
535 if (code || (fd < 0)) {
539 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
544 return (code || (fd<0)) ? (Inode)-1 : tmp.ih_ino;
549 int namei_iopen(IHandle_t *h)
554 /* Convert handle to file name. */
555 namei_HandleToName(&name, h);
556 fd = open(name.n_path, O_RDWR, 0666);
560 /* Need to detect vol special file and just unlink. In those cases, the
561 * handle passed in _is_ for the inode. We only check p1 for the special
564 int namei_dec(IHandle_t *ih, Inode ino, int p1)
571 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
574 int type = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
576 /* Verify this is the right file. */
577 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
586 if ((GetOGM(fdP->fd_fd, &inode_p1, &tag)<0) || (inode_p1 != p1)) {
587 FDH_REALLYCLOSE(fdP);
593 /* If it's the link table itself, decrement the link count. */
594 if (type == VI_LINKTABLE) {
595 if ((count = namei_GetLinkCount(fdP, (Inode)0, 1))<0) {
596 FDH_REALLYCLOSE(fdP);
602 if (namei_SetLinkCount(fdP, (Inode)0, count<0 ? 0 : count, 1)<0) {
603 FDH_REALLYCLOSE(fdP);
609 FDH_REALLYCLOSE(fdP);
615 namei_HandleToName(&name, tmp);
616 if ((code = unlink(name.n_path)) == 0) {
617 if (type == VI_LINKTABLE) {
618 /* Try to remove directory. If it fails, that's ok.
619 * Salvage will clean up.
621 (void) namei_RemoveDataDirectories(&name);
624 FDH_REALLYCLOSE(fdP);
628 /* Get a file descriptor handle for this Inode */
634 if ((count = namei_GetLinkCount(fdP, ino, 1))<0) {
635 FDH_REALLYCLOSE(fdP);
641 if (namei_SetLinkCount(fdP, ino, count, 1)<0) {
642 FDH_REALLYCLOSE(fdP);
649 namei_HandleToName(&name, &th);
650 code = unlink(name.n_path);
658 int namei_inc(IHandle_t *h, Inode ino, int p1)
664 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
665 int type = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
666 if (type != VI_LINKTABLE)
671 /* Get a file descriptor handle for this Inode */
677 if ((count = namei_GetLinkCount(fdP, ino, 1))<0)
686 if (namei_SetLinkCount(fdP, ino, count, 1)<0)
690 FDH_REALLYCLOSE(fdP);
697 /************************************************************************
698 * File Name Structure
699 ************************************************************************
701 * Each AFS file needs a unique name and it needs to be findable with
702 * minimal lookup time. Note that the constraint on the number of files and
703 * directories in a volume is the size of the vnode index files and the
704 * max file size AFS supports (for internal files) of 2^31. Since a record
705 * in the small vnode index file is 64 bytes long, we can have at most
706 * (2^31)/64 or 33554432 files. A record in the large index file is
707 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
708 * Another layout parameter is that there is roughly a 16 to 1 ratio between
709 * the number of files and the number of directories.
711 * Using this information we can see that a layout of 256 directories, each
712 * with 512 subdirectories and each of those having 512 files gives us
713 * 256*512*512 = 67108864 AFS files and directories.
715 * The volume, vnode, uniquifier and data version, as well as the tag
716 * are required, either for finding the file or for salvaging. It's best to
717 * restrict the name to something that can be mapped into 64 bits so the
718 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
719 * is used to distinguish between different versions of the same file
720 * which are currently in the RW and clones of a volume. See "Link Table
721 * Organization" below for more information on the tag. The tag is
722 * required in the name of the file to ensure a unique name.
724 * We can store data in the uid, gid and mode bits of the files, provided
725 * the directories have root only access. This gives us 15 bits for each
726 * of uid and gid (GNU chown considers 65535 to mean "don't change").
727 * There are 9 available mode bits. Adn we need to store a total of
728 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
729 * or 131 bits somewhere.
731 * The format of a file name for a regular file is:
732 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
733 * V1 - low 8 bits of RW volume id
734 * V2 - all bits of RW volume id
735 * AA - high 8 bits of vnode number.
736 * BB - next 9 bits of vnode number.
737 * <tag><uniq><vno> - file name
739 * Volume special files are stored in a separate directory:
740 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
743 * The vnode is hashed into the directory using the high bits of the
744 * vnode number. This is so that consecutively created vnodes are in
745 * roughly the same area on the disk. This will at least be optimal if
746 * the user is creating many files in the same AFS directory. The name
747 * should be formed so that the leading characters are different as quickly
748 * as possible, leading to faster discards of incorrect matches in the
754 /************************************************************************
755 * Link Table Organization
756 ************************************************************************
758 * The link table volume special file is used to hold the link counts that
759 * are held in the inodes in inode based AFS vice filesystems. For user
760 * space access, the link counts are being kept in a separate
761 * volume special file. The file begins with the usual version stamp
762 * information and is then followed by one row per vnode number. vnode 0
763 * is used to hold the link count of the link table itself. That is because
764 * the same link table is shared among all the volumes of the volume group
765 * and is deleted only when the last volume of a volume group is deleted.
767 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
768 * link count from 0 through 7. Each colume represents a unique instance of
769 * that vnode. Say we have a file shared between the RW and a RO and a
770 * different version of the file (or a different uniquifer) for the BU volume.
771 * Then one column would be holding the link count of 2 for the RW and RO
772 * and a different column would hold the link count of 1 for the BU volume.
773 * Note that we allow only 5 volumes per file, giving 15 bits used in the
776 #define LINKTABLE_WIDTH 2
777 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
779 static void namei_GetLCOffsetAndIndexFromIno(Inode ino, int *offset, int *index)
781 int toff = (int) (ino & NAMEI_VNODEMASK);
782 int tindex = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
784 *offset = (toff << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
785 *index = (tindex << 1) + tindex;
789 /* namei_GetLinkCount
790 * If lockit is set, lock the file and leave it locked upon a successful
793 int namei_GetLinkCount(FdHandle_t *h, Inode ino, int lockit)
795 unsigned short row = 0;
798 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
801 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
802 if (lockf(h->fd_fd, F_LOCK, 0) < 0)
804 if (flock(h->fd_fd, LOCK_EX)<0)
809 if (lseek(h->fd_fd, offset, SEEK_SET) == -1)
810 goto bad_getLinkByte;
812 if (read(h->fd_fd, (char*)&row, sizeof(row))!=sizeof(row)) {
813 goto bad_getLinkByte;
816 return (int) ((row >> index) & NAMEI_TAGMASK);
820 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
821 lockf(h->fd_fd, F_ULOCK, 0);
823 flock(h->fd_fd, LOCK_UN);
828 /* Return a free column index for this vnode. */
829 static int GetFreeTag(IHandle_t *ih, int vno)
843 /* Only one manipulates at a time. */
844 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
845 if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
847 if (flock(fdP->fd_fd, LOCK_EX)<0) {
849 FDH_REALLYCLOSE(fdP);
853 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
854 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
858 code = read(fdP->fd_fd, (char*)&row, sizeof(row));
859 if (code != sizeof(row)) {
865 /* Now find a free column in this row and claim it. */
866 for (col = 0; col<NAMEI_MAXVOLS; col++) {
867 coldata = 7 << (col * 3);
868 if ((row & coldata) == 0)
871 if (col >= NAMEI_MAXVOLS)
874 coldata = 1 << (col * 3);
877 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
880 if (write(fdP->fd_fd, (char*)&row, sizeof(row))!=sizeof(row)) {
884 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
885 lockf(fdP->fd_fd, F_ULOCK, 0);
887 flock(fdP->fd_fd, LOCK_UN);
889 FDH_REALLYCLOSE(fdP);
893 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
894 lockf(fdP->fd_fd, F_ULOCK, 0);
896 flock(fdP->fd_fd, LOCK_UN);
898 FDH_REALLYCLOSE(fdP);
904 /* namei_SetLinkCount
905 * If locked is set, assume file is locked. Otherwise, lock file before
906 * proceeding to modify it.
908 int namei_SetLinkCount(FdHandle_t *fdP, Inode ino, int count, int locked)
915 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
918 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
919 if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
921 if (flock(fdP->fd_fd, LOCK_EX)<0) {
926 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
928 goto bad_SetLinkCount;
932 code = read(fdP->fd_fd, (char*)&row, sizeof(row));
933 if (code != sizeof(row)) {
936 goto bad_SetLinkCount;
943 row &= (unsigned short)~junk;
944 row |= (unsigned short)count;
946 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
948 goto bad_SetLinkCount;
951 if (write(fdP->fd_fd, (char*)&row, sizeof(short)) != sizeof(short)) {
953 goto bad_SetLinkCount;
961 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
962 lockf(fdP->fd_fd, F_ULOCK, 0);
964 flock(fdP->fd_fd, LOCK_UN);
971 /* ListViceInodes - write inode data to a results file. */
972 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
974 static int DecodeVolumeName(char *name, int *vid);
975 static int namei_ListAFSSubDirs(IHandle_t *dirIH,
976 int (*write_fun)(FILE *, struct ViceInodeInfo *,
979 int (*judgeFun)(struct ViceInodeInfo *, int vid),
980 int singleVolumeNumber);
985 * Write the inode data to the results file.
987 * Returns -2 on error, 0 on success.
989 * This is written as a callback simply so that other listing routines
990 * can use the same inode reading code.
992 static int WriteInodeInfo(FILE *fp, struct ViceInodeInfo *info, char *dir,
996 n = fwrite(info, sizeof(*info), 1, fp);
997 return (n == 1) ? 0 : -2;
1001 int mode_errors; /* Number of errors found in mode bits on directories. */
1002 void VerifyDirPerms(char *path)
1006 if (stat(path, &status)<0) {
1007 Log("Unable to stat %s. Please manually verify mode bits for this"
1008 " directory\n", path);
1011 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1017 * Fill the results file with the requested inode information.
1021 * -1 - complete failure, salvage should terminate.
1022 * -2 - not enough space on partition, salvager has error message for this.
1024 * This code optimizes single volume salvages by just looking at that one
1025 * volume's directory.
1027 * If the resultFile is NULL, then don't call the write routine.
1029 int ListViceInodes(char *devname, char *mountedOn, char *resultFile,
1030 int (*judgeInode)(struct ViceInodeInfo *info, int vid),
1031 int singleVolumeNumber, int *forcep,
1032 int forceR, char *wpath)
1034 FILE *fp = (FILE*)-1;
1039 fp = fopen(resultFile, "w");
1041 Log("Unable to create inode description file %s\n", resultFile);
1046 /* Verify protections on directories. */
1048 VerifyDirPerms(mountedOn);
1050 ninodes = namei_ListAFSFiles(mountedOn, WriteInodeInfo, fp,
1051 judgeInode, singleVolumeNumber);
1061 if (fflush(fp) == EOF) {
1062 Log("Unable to successfully flush inode file for %s\n", mountedOn);
1066 if (fsync(fileno(fp)) == -1) {
1067 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1071 if (fclose(fp) == EOF) {
1072 Log("Unable to successfully close inode file for %s\n", mountedOn);
1077 * Paranoia: check that the file is really the right size
1079 if (stat(resultFile, &status) == -1) {
1080 Log("Unable to successfully stat inode file for %s\n", mountedOn);
1083 if (status.st_size != ninodes * sizeof (struct ViceInodeInfo)) {
1084 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1085 status.st_size, ninodes * sizeof (struct ViceInodeInfo),
1093 /* namei_ListAFSFiles
1095 * Collect all the matching AFS files on the drive.
1096 * If singleVolumeNumber is non-zero, just return files for that volume.
1098 * Returns <0 on error, else number of files found to match.
1100 int namei_ListAFSFiles(char *dev,
1101 int (*writeFun)(FILE *, struct ViceInodeInfo *, char *,
1104 int (*judgeFun)(struct ViceInodeInfo *, int),
1105 int singleVolumeNumber)
1111 struct dirent *dp1, *dp2;
1114 static void FreeZLCList(void);
1117 memset((void*)&ih, 0, sizeof(IHandle_t));
1118 ih.ih_dev = volutil_GetPartitionID(dev);
1120 if (singleVolumeNumber) {
1121 ih.ih_vid = singleVolumeNumber;
1122 namei_HandleToVolDir(&name, &ih);
1123 ninodes = namei_ListAFSSubDirs(&ih, writeFun, fp,
1124 judgeFun, singleVolumeNumber);
1129 /* Find all volume data directories and descend through them. */
1130 namei_HandleToInodeDir(&name, &ih);
1132 dirp1 = opendir(name.n_path);
1135 while ((dp1 = readdir(dirp1))) {
1136 if (*dp1->d_name == '.') continue;
1137 (void) strcpy(path2, name.n_path);
1138 (void) strcat(path2, "/");
1139 (void) strcat(path2, dp1->d_name);
1140 dirp2 = opendir(path2);
1142 while ((dp2 = readdir(dirp2))) {
1143 if (*dp2->d_name == '.') continue;
1144 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1145 ninodes += namei_ListAFSSubDirs(&ih, writeFun, fp,
1162 /* namei_ListAFSSubDirs
1167 * > = 0 - number of AFS files found.
1169 static int namei_ListAFSSubDirs(IHandle_t *dirIH,
1170 int (*writeFun)(FILE *, struct ViceInodeInfo *,
1173 int (*judgeFun)(struct ViceInodeInfo *, int),
1174 int singleVolumeNumber)
1176 IHandle_t myIH = *dirIH;
1178 char path1[512], path2[512], path3[512];
1179 DIR *dirp1, *dirp2, *dirp3;
1180 struct dirent *dp1, *dp2, *dp3;
1181 struct ViceInodeInfo info;
1182 FdHandle_t linkHandle;
1186 static void AddToZLCDeleteList(char dir, char *name);
1187 static void DeleteZLCFiles(char *path);
1190 namei_HandleToVolDir(&name, &myIH);
1191 (void) strcpy(path1, name.n_path);
1193 /* Do the directory containing the special files first to pick up link
1196 (void) strcat(path1, "/");
1197 (void) strcat(path1, NAMEI_SPECDIR);
1199 linkHandle.fd_fd = -1;
1200 dirp1 = opendir(path1);
1202 while ((dp1 = readdir(dirp1))) {
1203 if (*dp1->d_name == '.') continue;
1204 if (DecodeInode(path1, dp1->d_name, &info, myIH.ih_vid)<0)
1206 if (info.u.param[2] != VI_LINKTABLE) {
1210 /* Open this handle */
1211 (void) sprintf(path2, "%s/%s", path1, dp1->d_name);
1212 linkHandle.fd_fd = open(path2, O_RDONLY, 0666);
1213 info.linkCount = namei_GetLinkCount(&linkHandle, (Inode)0, 0);
1215 if (judgeFun && !(*judgeFun)(&info, singleVolumeNumber))
1218 if ((*writeFun)(fp, &info, path1, dp1->d_name)<0) {
1219 if (linkHandle.fd_fd >= 0)
1220 close(linkHandle.fd_fd);
1229 /* Now run through all the other subdirs */
1230 namei_HandleToVolDir(&name, &myIH);
1231 (void) strcpy(path1, name.n_path);
1233 dirp1 = opendir(path1);
1235 while ((dp1 = readdir(dirp1))) {
1236 if (*dp1->d_name == '.') continue;
1237 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
1240 /* Now we've got a next level subdir. */
1241 (void) strcpy(path2, path1);
1242 (void) strcat(path2, "/");
1243 (void) strcat(path2, dp1->d_name);
1244 dirp2 = opendir(path2);
1246 while ((dp2 = readdir(dirp2))) {
1247 if (*dp2->d_name == '.') continue;
1249 /* Now we've got to the actual data */
1250 (void) strcpy(path3, path2);
1251 (void) strcat(path3, "/");
1252 (void) strcat(path3, dp2->d_name);
1253 dirp3 = opendir(path3);
1255 while ((dp3 = readdir(dirp3))) {
1256 if (*dp3->d_name == '.') continue;
1257 if (DecodeInode(path3, dp3->d_name, &info,
1260 info.linkCount = namei_GetLinkCount(&linkHandle,
1261 info.inodeNumber, 0);
1262 if (info.linkCount == 0) {
1264 Log("Found 0 link count file %s/%s, deleting it.\n",
1265 path3, dp3->d_name);
1266 AddToZLCDeleteList((char)i, dp3->d_name);
1268 Log("Found 0 link count file %s/%s.\n",
1269 path3, dp3->d_name);
1274 && !(*judgeFun)(&info, singleVolumeNumber))
1277 if ((*writeFun)(fp, &info, path3, dp3->d_name)<0) {
1278 close(linkHandle.fd_fd);
1295 if (linkHandle.fd_fd >= 0)
1296 close(linkHandle.fd_fd);
1298 /* Then why does this directory exist? Blow it away. */
1299 namei_HandleToVolDir(&name, dirIH);
1300 namei_RemoveDataDirectories(&name);
1306 static int DecodeVolumeName(char *name, int *vid)
1308 if (strlen(name) <= 2)
1310 *vid = (int) flipbase64_to_int64(name);
1317 * Get the inode number from the name.
1320 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1327 (void) strcpy(fpath, dpath);
1328 (void) strcat(fpath, "/");
1329 (void) strcat(fpath, name);
1331 if (stat(fpath, &status)<0) {
1335 info->byteCount = status.st_size;
1336 info->inodeNumber = flipbase64_to_int64(name);
1338 GetOGMFromStat(&status, &parm, &tag);
1339 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1340 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
1341 info->u.param[0] = parm;
1342 info->u.param[1] = -1;
1343 info->u.param[2] = tag;
1344 info->u.param[3] = volid;
1347 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
1348 info->u.param[0] = volid;
1349 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
1350 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
1351 & (Inode)NAMEI_UNIQMASK);
1352 info->u.param[3] = parm;
1360 * returns a static string used to print either 32 or 64 bit inode numbers.
1362 char * PrintInode(char *s, Inode ino)
1364 static afs_ino_str_t result;
1368 (void) sprintf((char*)s, "%Lu", ino);
1375 /* Routines to facilitate removing zero link count files. */
1376 #define MAX_ZLC_NAMES 32
1377 #define MAX_ZLC_NAMELEN 16
1378 typedef struct zlcList_s {
1379 struct zlcList_s *zlc_next;
1381 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1384 static zlcList_t *zlcAnchor = NULL;
1385 static zlcList_t *zlcCur = NULL;
1387 static void AddToZLCDeleteList(char dir, char *name)
1389 assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1391 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1392 if (zlcCur && zlcCur->zlc_next)
1393 zlcCur = zlcCur->zlc_next;
1395 zlcList_t *tmp = (zlcList_t*)malloc(sizeof(zlcList_t));
1402 zlcCur->zlc_next = tmp;
1406 zlcCur->zlc_next = NULL;
1410 (void) sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1414 static void DeleteZLCFiles(char *path)
1420 for (z = zlcAnchor; z; z = z->zlc_next) {
1421 for (i=0; i < z->zlc_n; i++) {
1422 (void) sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1423 if (namei_unlink(fname)<0) {
1424 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1428 z->zlc_n = 0; /* Can reuse space. */
1433 static void FreeZLCList(void)
1440 tnext = i->zlc_next;
1444 zlcCur = zlcAnchor = NULL;
1448 #endif /* AFS_NAMEI_ENV */