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);
174 namei_HandleToVolDir(name, ih);
176 if (vno == NAMEI_VNODESPECIAL) {
177 (void) strcpy(name->n_dir1, NAMEI_SPECDIR);
178 addtoname(name, name->n_dir1);
179 name->n_dir2[0] = '\0';
182 (void) int32_to_flipbase64(str, VNO_DIR1(vno));
183 (void) strcpy(name->n_dir1, str);
184 addtoname(name, name->n_dir1);
185 (void) int32_to_flipbase64(str, VNO_DIR2(vno));
186 (void) strcpy(name->n_dir2, str);
187 addtoname(name, name->n_dir2);
189 (void) int64_to_flipbase64(str, (int64_t)ih->ih_ino);
190 (void) strcpy(name->n_inode, str);
191 addtoname(name, name->n_inode);
194 /* The following is a warning to tell sys-admins to not muck about in this
197 #define VICE_README "These files and directories are a part of the AFS \
198 namespace. Modifying them\nin any way will result in loss of AFS data.\n"
199 int namei_ViceREADME(char *partition)
204 sprintf(filename, "%s/%s/README", partition, INODEDIR);
205 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
207 write(fd, VICE_README, strlen(VICE_README));
214 #define create_dir() \
216 if (mkdir(tmp, 0700)<0) { \
217 if (errno != EEXIST) \
225 #define create_nextdir(A) \
227 strcat(tmp, "/"); strcat(tmp, A); create_dir(); \
230 /* namei_CreateDataDirectories
232 * If creating the file failed because of ENOENT or ENOTDIR, try
233 * creating all the directories first.
235 static int namei_CreateDataDirectories(namei_t *name, int *created)
243 (void) strcpy(tmp, name->n_base);
246 create_nextdir(name->n_voldir1);
247 create_nextdir(name->n_voldir2);
248 create_nextdir(name->n_dir1);
249 if (name->n_dir2[0]) {
250 create_nextdir(name->n_dir2);
255 /* delTree(): Deletes an entire tree of directories (no files)
257 * root : Full path to the subtree. Should be big enough for PATH_MAX
258 * tree : the subtree to be deleted is rooted here. Specifies only the
259 * subtree beginning at tree (not the entire path). It should be
260 * a pointer into the "root" buffer.
262 * errp : errno of the first error encountered during the directory cleanup.
263 * *errp should have been initialized to 0.
266 * -1 : If errors were encountered during cleanup and error is set to
270 * If there are errors, we try to work around them and delete as many
271 * directories as possible. We don't attempt to remove directories that still
272 * have non-dir entries in them.
275 delTree(char *root, char *tree, int *errp)
284 /* delete the children first */
285 cp = strchr(tree, '/');
287 delTree(root, cp+1, errp);
291 cp = tree + strlen(tree); /* move cp to the end of string tree */
293 /* now delete all entries in this dir */
294 if ( (ds = opendir(root)) != (DIR *)NULL) {
296 while (dirp = readdir(ds)) {
297 /* ignore . and .. */
298 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
300 /* since root is big enough, we reuse the space to
301 * concatenate the dirname to the current tree
304 strcat(root, dirp->d_name);
305 if ( stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
306 /* delete this subtree */
307 delTree(root, cp+1, errp);
309 *errp = *errp ? *errp : errno;
311 /* recover path to our cur tree by truncating it to
320 /* finally axe the current dir */
322 *errp = *errp ? *errp : errno;
324 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
326 #endif /* !AFS_PTHREAD_ENV */
328 } /* if valid tree */
330 /* if we encountered errors during cleanup, we return a -1 */
338 /* namei_RemoveDataDirectories
340 * Returns 0 on success.
341 * Returns -1 on error. Typically, callers ignore this error bcause we
342 * can continue running if the removes fail. The salvage process will
343 * finish tidying up for us. We only use the n_base and n_voldir1 entries
344 * and only do rmdir's.
347 static int namei_RemoveDataDirectories(namei_t *name)
349 char pbuf[MAXPATHLEN], *path = pbuf;
350 int prefixlen = strlen(name->n_base), err = 0;
352 strcpy(path, name->n_path);
354 /* move past the prefix */
355 path = path+prefixlen+1; /* skip over the trailing / */
357 /* now delete all dirs upto path */
358 return delTree(pbuf, path, &err);
362 /* Create the file in the name space.
364 * Parameters stored as follows:
366 * p1 - volid - implied in containing directory.
367 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
368 * p3 - uniq -- bits 4-0 are in mode bits 4-0
369 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
371 * p1 - volid - creation time - dwHighDateTime
372 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
373 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
374 * p4 - parid - parent volume id - implied in containing directory.
376 * Return value is the inode number or (Inode)-1 if error.
377 * We "know" there is only one link table, so return EEXIST if there already
378 * is a link table. It's up to the calling code to test errno and increment
384 * This function is called by VCreateVolume to hide the implementation
385 * details of the inode numbers. This only allows for 7 volume special
386 * types, but if we get that far, this could should be dead by then.
388 Inode namei_MakeSpecIno(int volid, int type)
391 ino = NAMEI_INODESPECIAL;
392 type &= NAMEI_TAGMASK;
393 ino |= ((Inode)type) << NAMEI_TAGSHIFT;
394 ino |= ((Inode)volid) << NAMEI_UNIQSHIFT;
398 /* SetOGM - set owner group and mode bits from parm and tag
400 * owner - low 15 bits of parm.
401 * group - next 15 bits of parm.
402 * mode - 2 bits of parm, then lowest = 3 bits of tag.
404 static int SetOGM(int fd, int parm, int tag)
406 int owner, group, mode;
408 owner = parm & 0x7fff;
409 group = (parm >> 15) & 0x7fff;
410 if (fchown(fd, owner, group)<0)
413 mode = (parm >> 27) & 0x18;
415 if (fchmod(fd, mode)<0)
422 /* GetOGM - get parm and tag from owner, group and mode bits. */
423 static void GetOGMFromStat(struct stat *status, int *parm, int *tag)
427 *parm = status->st_uid | (status->st_gid << 15);
428 *parm |= (status->st_mode & 0x18) << 27;
429 *tag = status->st_mode & 0x7;
432 static int GetOGM(int fd, int *parm, int *tag)
435 if (fstat(fd, &status)<0)
438 GetOGMFromStat(&status, parm, tag);
442 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
444 /* Derive the name and create it O_EXCL. If that fails we have an error.
445 * Get the tag from a free column in the link table.
447 Inode namei_icreate(IHandle_t *lh, char *part, int p1, int p2, int p3, int p4)
465 memset((void*)&tmp, 0, sizeof(IHandle_t));
468 tmp.ih_dev = volutil_GetPartitionID(part);
469 if (tmp.ih_dev == -1) {
475 /* Parameters for special file:
476 * p1 - volume id - goes into owner/group/mode
479 * p4 - parent volume id
484 tmp.ih_vid = p4; /* Use parent volume id, where this file will be.*/
485 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
488 int vno = p2 & NAMEI_VNODEMASK;
489 /* Parameters for regular file:
501 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
502 tag = GetFreeTag(lh, p2);
506 /* name is <uniq(p3)><tag><vno(p2)> */
508 tmp.ih_ino = (Inode)p2;
509 tmp.ih_ino |= ((Inode)tag)<<NAMEI_TAGSHIFT;
510 tmp.ih_ino |= ((Inode)p3)<<NAMEI_UNIQSHIFT;
515 namei_HandleToName(&name, &tmp);
516 fd = open(name.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
518 if (errno == ENOTDIR || errno == ENOENT) {
519 if (namei_CreateDataDirectories(&name, &created_dir)<0)
521 fd = open(name.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
529 if (SetOGM(fd, ogm_parm, tag)<0) {
535 if (p2 == -1 && p3 == VI_LINKTABLE) {
536 /* hack at tmp to setup for set link count call. */
538 code = namei_SetLinkCount(&tfd, (Inode)0, 1, 0);
546 if (code || (fd < 0)) {
550 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
555 return (code || (fd<0)) ? (Inode)-1 : tmp.ih_ino;
560 int namei_iopen(IHandle_t *h)
565 /* Convert handle to file name. */
566 namei_HandleToName(&name, h);
567 fd = open(name.n_path, O_RDWR, 0666);
571 /* Need to detect vol special file and just unlink. In those cases, the
572 * handle passed in _is_ for the inode. We only check p1 for the special
575 int namei_dec(IHandle_t *ih, Inode ino, int p1)
582 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
586 int type = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
588 /* Verify this is the right file. */
589 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
598 if ((GetOGM(fdP->fd_fd, &inode_p1, &tag)<0) || (inode_p1 != p1)) {
599 FDH_REALLYCLOSE(fdP);
605 /* If it's the link table itself, decrement the link count. */
606 if (type == VI_LINKTABLE) {
607 if ((count = namei_GetLinkCount(fdP, (Inode)0, 1))<0) {
608 FDH_REALLYCLOSE(fdP);
614 if (namei_SetLinkCount(fdP, (Inode)0, count<0 ? 0 : count, 1)<0) {
615 FDH_REALLYCLOSE(fdP);
621 FDH_REALLYCLOSE(fdP);
627 namei_HandleToName(&name, tmp);
628 if ((code = unlink(name.n_path)) == 0) {
629 if (type == VI_LINKTABLE) {
630 /* Try to remove directory. If it fails, that's ok.
631 * Salvage will clean up.
633 (void) namei_RemoveDataDirectories(&name);
636 FDH_REALLYCLOSE(fdP);
640 /* Get a file descriptor handle for this Inode */
646 if ((count = namei_GetLinkCount(fdP, ino, 1))<0) {
647 FDH_REALLYCLOSE(fdP);
653 if (namei_SetLinkCount(fdP, ino, count, 1)<0) {
654 FDH_REALLYCLOSE(fdP);
661 namei_HandleToName(&name, &th);
662 code = unlink(name.n_path);
670 int namei_inc(IHandle_t *h, Inode ino, int p1)
676 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
677 int type = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
678 if (type != VI_LINKTABLE)
683 /* Get a file descriptor handle for this Inode */
689 if ((count = namei_GetLinkCount(fdP, ino, 1))<0)
698 if (namei_SetLinkCount(fdP, ino, count, 1)<0)
702 FDH_REALLYCLOSE(fdP);
709 /************************************************************************
710 * File Name Structure
711 ************************************************************************
713 * Each AFS file needs a unique name and it needs to be findable with
714 * minimal lookup time. Note that the constraint on the number of files and
715 * directories in a volume is the size of the vnode index files and the
716 * max file size AFS supports (for internal files) of 2^31. Since a record
717 * in the small vnode index file is 64 bytes long, we can have at most
718 * (2^31)/64 or 33554432 files. A record in the large index file is
719 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
720 * Another layout parameter is that there is roughly a 16 to 1 ratio between
721 * the number of files and the number of directories.
723 * Using this information we can see that a layout of 256 directories, each
724 * with 512 subdirectories and each of those having 512 files gives us
725 * 256*512*512 = 67108864 AFS files and directories.
727 * The volume, vnode, uniquifier and data version, as well as the tag
728 * are required, either for finding the file or for salvaging. It's best to
729 * restrict the name to something that can be mapped into 64 bits so the
730 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
731 * is used to distinguish between different versions of the same file
732 * which are currently in the RW and clones of a volume. See "Link Table
733 * Organization" below for more information on the tag. The tag is
734 * required in the name of the file to ensure a unique name.
736 * We can store data in the uid, gid and mode bits of the files, provided
737 * the directories have root only access. This gives us 15 bits for each
738 * of uid and gid (GNU chown considers 65535 to mean "don't change").
739 * There are 9 available mode bits. Adn we need to store a total of
740 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
741 * or 131 bits somewhere.
743 * The format of a file name for a regular file is:
744 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
745 * V1 - low 8 bits of RW volume id
746 * V2 - all bits of RW volume id
747 * AA - high 8 bits of vnode number.
748 * BB - next 9 bits of vnode number.
749 * <tag><uniq><vno> - file name
751 * Volume special files are stored in a separate directory:
752 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
755 * The vnode is hashed into the directory using the high bits of the
756 * vnode number. This is so that consecutively created vnodes are in
757 * roughly the same area on the disk. This will at least be optimal if
758 * the user is creating many files in the same AFS directory. The name
759 * should be formed so that the leading characters are different as quickly
760 * as possible, leading to faster discards of incorrect matches in the
766 /************************************************************************
767 * Link Table Organization
768 ************************************************************************
770 * The link table volume special file is used to hold the link counts that
771 * are held in the inodes in inode based AFS vice filesystems. For user
772 * space access, the link counts are being kept in a separate
773 * volume special file. The file begins with the usual version stamp
774 * information and is then followed by one row per vnode number. vnode 0
775 * is used to hold the link count of the link table itself. That is because
776 * the same link table is shared among all the volumes of the volume group
777 * and is deleted only when the last volume of a volume group is deleted.
779 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
780 * link count from 0 through 7. Each colume represents a unique instance of
781 * that vnode. Say we have a file shared between the RW and a RO and a
782 * different version of the file (or a different uniquifer) for the BU volume.
783 * Then one column would be holding the link count of 2 for the RW and RO
784 * and a different column would hold the link count of 1 for the BU volume.
785 * Note that we allow only 5 volumes per file, giving 15 bits used in the
788 #define LINKTABLE_WIDTH 2
789 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
791 static void namei_GetLCOffsetAndIndexFromIno(Inode ino, int *offset, int *index)
793 int toff = (int) (ino & NAMEI_VNODEMASK);
794 int tindex = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
796 *offset = (toff << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
797 *index = (tindex << 1) + tindex;
801 /* namei_GetLinkCount
802 * If lockit is set, lock the file and leave it locked upon a successful
805 int namei_GetLinkCount(FdHandle_t *h, Inode ino, int lockit)
807 unsigned short row = 0;
811 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
814 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
815 if (lockf(h->fd_fd, F_LOCK, 0) < 0)
817 if (flock(h->fd_fd, LOCK_EX)<0)
822 if (lseek(h->fd_fd, offset, SEEK_SET) == -1)
823 goto bad_getLinkByte;
825 if (read(h->fd_fd, (char*)&row, sizeof(row))!=sizeof(row)) {
826 goto bad_getLinkByte;
829 return (int) ((row >> index) & NAMEI_TAGMASK);
833 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
834 lockf(h->fd_fd, F_ULOCK, 0);
836 flock(h->fd_fd, LOCK_UN);
841 /* Return a free column index for this vnode. */
842 static int GetFreeTag(IHandle_t *ih, int vno)
856 /* Only one manipulates at a time. */
857 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
858 if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
860 if (flock(fdP->fd_fd, LOCK_EX)<0) {
862 FDH_REALLYCLOSE(fdP);
866 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
867 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
871 code = read(fdP->fd_fd, (char*)&row, sizeof(row));
872 if (code != sizeof(row)) {
878 /* Now find a free column in this row and claim it. */
879 for (col = 0; col<NAMEI_MAXVOLS; col++) {
880 coldata = 7 << (col * 3);
881 if ((row & coldata) == 0)
884 if (col >= NAMEI_MAXVOLS)
887 coldata = 1 << (col * 3);
890 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
893 if (write(fdP->fd_fd, (char*)&row, sizeof(row))!=sizeof(row)) {
897 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
898 lockf(fdP->fd_fd, F_ULOCK, 0);
900 flock(fdP->fd_fd, LOCK_UN);
902 FDH_REALLYCLOSE(fdP);
906 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
907 lockf(fdP->fd_fd, F_ULOCK, 0);
909 flock(fdP->fd_fd, LOCK_UN);
911 FDH_REALLYCLOSE(fdP);
917 /* namei_SetLinkCount
918 * If locked is set, assume file is locked. Otherwise, lock file before
919 * proceeding to modify it.
921 int namei_SetLinkCount(FdHandle_t *fdP, Inode ino, int count, int locked)
928 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
931 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
932 if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
934 if (flock(fdP->fd_fd, LOCK_EX)<0) {
939 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
941 goto bad_SetLinkCount;
945 code = read(fdP->fd_fd, (char*)&row, sizeof(row));
946 if (code != sizeof(row)) {
949 goto bad_SetLinkCount;
956 row &= (unsigned short)~junk;
957 row |= (unsigned short)count;
959 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
961 goto bad_SetLinkCount;
964 if (write(fdP->fd_fd, (char*)&row, sizeof(short)) != sizeof(short)) {
966 goto bad_SetLinkCount;
974 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
975 lockf(fdP->fd_fd, F_ULOCK, 0);
977 flock(fdP->fd_fd, LOCK_UN);
984 /* ListViceInodes - write inode data to a results file. */
985 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
987 static int DecodeVolumeName(char *name, int *vid);
988 static int namei_ListAFSSubDirs(IHandle_t *dirIH,
989 int (*write_fun)(FILE *, struct ViceInodeInfo *,
992 int (*judgeFun)(struct ViceInodeInfo *, int vid),
993 int singleVolumeNumber);
998 * Write the inode data to the results file.
1000 * Returns -2 on error, 0 on success.
1002 * This is written as a callback simply so that other listing routines
1003 * can use the same inode reading code.
1005 static int WriteInodeInfo(FILE *fp, struct ViceInodeInfo *info, char *dir,
1009 n = fwrite(info, sizeof(*info), 1, fp);
1010 return (n == 1) ? 0 : -2;
1014 int mode_errors; /* Number of errors found in mode bits on directories. */
1015 void VerifyDirPerms(char *path)
1019 if (stat(path, &status)<0) {
1020 Log("Unable to stat %s. Please manually verify mode bits for this"
1021 " directory\n", path);
1024 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1030 * Fill the results file with the requested inode information.
1034 * -1 - complete failure, salvage should terminate.
1035 * -2 - not enough space on partition, salvager has error message for this.
1037 * This code optimizes single volume salvages by just looking at that one
1038 * volume's directory.
1040 * If the resultFile is NULL, then don't call the write routine.
1042 int ListViceInodes(char *devname, char *mountedOn, char *resultFile,
1043 int (*judgeInode)(struct ViceInodeInfo *info, int vid),
1044 int singleVolumeNumber, int *forcep,
1045 int forceR, char *wpath)
1047 FILE *fp = (FILE*)-1;
1052 fp = fopen(resultFile, "w");
1054 Log("Unable to create inode description file %s\n", resultFile);
1059 /* Verify protections on directories. */
1061 VerifyDirPerms(mountedOn);
1063 ninodes = namei_ListAFSFiles(mountedOn, WriteInodeInfo, fp,
1064 judgeInode, singleVolumeNumber);
1074 if (fflush(fp) == EOF) {
1075 Log("Unable to successfully flush inode file for %s\n", mountedOn);
1079 if (fsync(fileno(fp)) == -1) {
1080 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1084 if (fclose(fp) == EOF) {
1085 Log("Unable to successfully close inode file for %s\n", mountedOn);
1090 * Paranoia: check that the file is really the right size
1092 if (stat(resultFile, &status) == -1) {
1093 Log("Unable to successfully stat inode file for %s\n", mountedOn);
1096 if (status.st_size != ninodes * sizeof (struct ViceInodeInfo)) {
1097 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1098 status.st_size, ninodes * sizeof (struct ViceInodeInfo),
1106 /* namei_ListAFSFiles
1108 * Collect all the matching AFS files on the drive.
1109 * If singleVolumeNumber is non-zero, just return files for that volume.
1111 * Returns <0 on error, else number of files found to match.
1113 int namei_ListAFSFiles(char *dev,
1114 int (*writeFun)(FILE *, struct ViceInodeInfo *, char *,
1117 int (*judgeFun)(struct ViceInodeInfo *, int),
1118 int singleVolumeNumber)
1124 struct dirent *dp1, *dp2;
1127 static void FreeZLCList(void);
1130 memset((void*)&ih, 0, sizeof(IHandle_t));
1131 ih.ih_dev = volutil_GetPartitionID(dev);
1133 if (singleVolumeNumber) {
1134 ih.ih_vid = singleVolumeNumber;
1135 namei_HandleToVolDir(&name, &ih);
1136 ninodes = namei_ListAFSSubDirs(&ih, writeFun, fp,
1137 judgeFun, singleVolumeNumber);
1142 /* Find all volume data directories and descend through them. */
1143 namei_HandleToInodeDir(&name, &ih);
1145 dirp1 = opendir(name.n_path);
1148 while (dp1 = readdir(dirp1)) {
1149 if (*dp1->d_name == '.') continue;
1150 (void) strcpy(path2, name.n_path);
1151 (void) strcat(path2, "/");
1152 (void) strcat(path2, dp1->d_name);
1153 dirp2 = opendir(path2);
1155 while (dp2 = readdir(dirp2)) {
1156 if (*dp2->d_name == '.') continue;
1157 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1158 ninodes += namei_ListAFSSubDirs(&ih, writeFun, fp,
1175 /* namei_ListAFSSubDirs
1180 * > = 0 - number of AFS files found.
1182 static int namei_ListAFSSubDirs(IHandle_t *dirIH,
1183 int (*writeFun)(FILE *, struct ViceInodeInfo *,
1186 int (*judgeFun)(struct ViceInodeInfo *, int),
1187 int singleVolumeNumber)
1190 IHandle_t myIH = *dirIH;
1192 char path1[512], path2[512], path3[512];
1193 DIR *dirp1, *dirp2, *dirp3;
1194 struct dirent *dp1, *dp2, *dp3;
1196 struct ViceInodeInfo info;
1198 FdHandle_t linkHandle;
1201 static void AddToZLCDeleteList(char dir, char *name);
1202 static void DeleteZLCFiles(char *path);
1205 namei_HandleToVolDir(&name, &myIH);
1206 (void) strcpy(path1, name.n_path);
1208 /* Do the directory containing the special files first to pick up link
1211 (void) strcat(path1, "/");
1212 (void) strcat(path1, NAMEI_SPECDIR);
1214 linkHandle.fd_fd = -1;
1215 dirp1 = opendir(path1);
1217 while (dp1 = readdir(dirp1)) {
1218 if (*dp1->d_name == '.') continue;
1219 if (DecodeInode(path1, dp1->d_name, &info, myIH.ih_vid)<0)
1221 if (info.u.param[2] != VI_LINKTABLE) {
1225 /* Open this handle */
1226 (void) sprintf(path2, "%s/%s", path1, dp1->d_name);
1227 linkHandle.fd_fd = open(path2, O_RDONLY, 0666);
1228 info.linkCount = namei_GetLinkCount(&linkHandle, (Inode)0, 0);
1230 if (judgeFun && !(*judgeFun)(&info, singleVolumeNumber))
1233 if ((*writeFun)(fp, &info, path1, dp1->d_name)<0) {
1234 if (linkHandle.fd_fd >= 0)
1235 close(linkHandle.fd_fd);
1244 /* Now run through all the other subdirs */
1245 namei_HandleToVolDir(&name, &myIH);
1246 (void) strcpy(path1, name.n_path);
1248 dirp1 = opendir(path1);
1250 while (dp1 = readdir(dirp1)) {
1251 if (*dp1->d_name == '.') continue;
1252 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
1255 /* Now we've got a next level subdir. */
1256 (void) strcpy(path2, path1);
1257 (void) strcat(path2, "/");
1258 (void) strcat(path2, dp1->d_name);
1259 dirp2 = opendir(path2);
1261 while (dp2 = readdir(dirp2)) {
1262 if (*dp2->d_name == '.') continue;
1264 /* Now we've got to the actual data */
1265 (void) strcpy(path3, path2);
1266 (void) strcat(path3, "/");
1267 (void) strcat(path3, dp2->d_name);
1268 dirp3 = opendir(path3);
1270 while (dp3 = readdir(dirp3)) {
1271 if (*dp3->d_name == '.') continue;
1272 if (DecodeInode(path3, dp3->d_name, &info,
1275 info.linkCount = namei_GetLinkCount(&linkHandle,
1276 info.inodeNumber, 0);
1277 if (info.linkCount == 0) {
1279 Log("Found 0 link count file %s/%s, deleting it.\n",
1280 path3, dp3->d_name);
1281 AddToZLCDeleteList((char)i, dp3->d_name);
1283 Log("Found 0 link count file %s/%s.\n",
1284 path3, dp3->d_name);
1289 && !(*judgeFun)(&info, singleVolumeNumber))
1292 if ((*writeFun)(fp, &info, path3, dp3->d_name)<0) {
1293 close(linkHandle.fd_fd);
1310 if (linkHandle.fd_fd >= 0)
1311 close(linkHandle.fd_fd);
1313 /* Then why does this directory exist? Blow it away. */
1314 namei_HandleToVolDir(&name, dirIH);
1315 namei_RemoveDataDirectories(&name);
1321 static int DecodeVolumeName(char *name, int *vid)
1323 if (strlen(name) <= 2)
1325 *vid = (int) flipbase64_to_int64(name);
1332 * Get the inode number from the name.
1335 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1342 (void) strcpy(fpath, dpath);
1343 (void) strcat(fpath, "/");
1344 (void) strcat(fpath, name);
1346 if (stat(fpath, &status)<0) {
1350 info->byteCount = status.st_size;
1351 info->inodeNumber = flipbase64_to_int64(name);
1353 GetOGMFromStat(&status, &parm, &tag);
1354 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1355 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
1356 info->u.param[0] = parm;
1357 info->u.param[1] = -1;
1358 info->u.param[2] = tag;
1359 info->u.param[3] = volid;
1362 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
1363 info->u.param[0] = volid;
1364 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
1365 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
1366 & (Inode)NAMEI_UNIQMASK);
1367 info->u.param[3] = parm;
1375 * returns a static string used to print either 32 or 64 bit inode numbers.
1377 char * PrintInode(char *s, Inode ino)
1379 static afs_ino_str_t result;
1383 (void) sprintf((char*)s, "%Lu", ino);
1390 /* Routines to facilitate removing zero link count files. */
1391 #define MAX_ZLC_NAMES 32
1392 #define MAX_ZLC_NAMELEN 16
1393 typedef struct zlcList_s {
1394 struct zlcList_s *zlc_next;
1396 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1399 static zlcList_t *zlcAnchor = NULL;
1400 static zlcList_t *zlcCur = NULL;
1402 static void AddToZLCDeleteList(char dir, char *name)
1404 assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1406 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1407 if (zlcCur && zlcCur->zlc_next)
1408 zlcCur = zlcCur->zlc_next;
1410 zlcList_t *tmp = (zlcList_t*)malloc(sizeof(zlcList_t));
1417 zlcCur->zlc_next = tmp;
1421 zlcCur->zlc_next = NULL;
1425 (void) sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1429 static void DeleteZLCFiles(char *path)
1435 for (z = zlcAnchor; z; z = z->zlc_next) {
1436 for (i=0; i < z->zlc_n; i++) {
1437 (void) sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1438 if (namei_unlink(fname)<0) {
1439 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1443 z->zlc_n = 0; /* Can reuse space. */
1448 static void FreeZLCList(void)
1455 tnext = i->zlc_next;
1459 zlcCur = zlcAnchor = NULL;
1463 #endif /* AFS_NAMEI_ENV */