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 sprintf(filename, "%s/%s/README", partition, INODEDIR);
208 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
210 write(fd, VICE_README, strlen(VICE_README));
217 #define create_dir() \
219 if (mkdir(tmp, 0700)<0) { \
220 if (errno != EEXIST) \
228 #define create_nextdir(A) \
230 strcat(tmp, "/"); strcat(tmp, A); create_dir(); \
233 /* namei_CreateDataDirectories
235 * If creating the file failed because of ENOENT or ENOTDIR, try
236 * creating all the directories first.
238 static int namei_CreateDataDirectories(namei_t *name, int *created)
244 (void) strcpy(tmp, name->n_base);
247 create_nextdir(name->n_voldir1);
248 create_nextdir(name->n_voldir2);
249 create_nextdir(name->n_dir1);
250 if (name->n_dir2[0]) {
251 create_nextdir(name->n_dir2);
256 /* delTree(): Deletes an entire tree of directories (no files)
258 * root : Full path to the subtree. Should be big enough for PATH_MAX
259 * tree : the subtree to be deleted is rooted here. Specifies only the
260 * subtree beginning at tree (not the entire path). It should be
261 * a pointer into the "root" buffer.
263 * errp : errno of the first error encountered during the directory cleanup.
264 * *errp should have been initialized to 0.
267 * -1 : If errors were encountered during cleanup and error is set to
271 * If there are errors, we try to work around them and delete as many
272 * directories as possible. We don't attempt to remove directories that still
273 * have non-dir entries in them.
276 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)
425 *parm = status->st_uid | (status->st_gid << 15);
426 *parm |= (status->st_mode & 0x18) << 27;
427 *tag = status->st_mode & 0x7;
430 static int GetOGM(int fd, int *parm, int *tag)
433 if (fstat(fd, &status)<0)
436 GetOGMFromStat(&status, parm, tag);
440 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
442 /* Derive the name and create it O_EXCL. If that fails we have an error.
443 * Get the tag from a free column in the link table.
445 Inode namei_icreate(IHandle_t *lh, char *part, int p1, int p2, int p3, int p4)
458 memset((void*)&tmp, 0, sizeof(IHandle_t));
461 tmp.ih_dev = volutil_GetPartitionID(part);
462 if (tmp.ih_dev == -1) {
468 /* Parameters for special file:
469 * p1 - volume id - goes into owner/group/mode
472 * p4 - parent volume id
477 tmp.ih_vid = p4; /* Use parent volume id, where this file will be.*/
478 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
481 int vno = p2 & NAMEI_VNODEMASK;
482 /* Parameters for regular file:
494 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
495 tag = GetFreeTag(lh, p2);
499 /* name is <uniq(p3)><tag><vno(p2)> */
501 tmp.ih_ino = (Inode)p2;
502 tmp.ih_ino |= ((Inode)tag)<<NAMEI_TAGSHIFT;
503 tmp.ih_ino |= ((Inode)p3)<<NAMEI_UNIQSHIFT;
508 namei_HandleToName(&name, &tmp);
509 fd = open(name.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
511 if (errno == ENOTDIR || errno == ENOENT) {
512 if (namei_CreateDataDirectories(&name, &created_dir)<0)
514 fd = open(name.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
522 if (SetOGM(fd, ogm_parm, tag)<0) {
528 if (p2 == -1 && p3 == VI_LINKTABLE) {
529 /* hack at tmp to setup for set link count call. */
531 code = namei_SetLinkCount(&tfd, (Inode)0, 1, 0);
539 if (code || (fd < 0)) {
543 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
548 return (code || (fd<0)) ? (Inode)-1 : tmp.ih_ino;
553 int namei_iopen(IHandle_t *h)
558 /* Convert handle to file name. */
559 namei_HandleToName(&name, h);
560 fd = open(name.n_path, O_RDWR, 0666);
564 /* Need to detect vol special file and just unlink. In those cases, the
565 * handle passed in _is_ for the inode. We only check p1 for the special
568 int namei_dec(IHandle_t *ih, Inode ino, int p1)
575 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
578 int type = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
580 /* Verify this is the right file. */
581 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
590 if ((GetOGM(fdP->fd_fd, &inode_p1, &tag)<0) || (inode_p1 != p1)) {
591 FDH_REALLYCLOSE(fdP);
597 /* If it's the link table itself, decrement the link count. */
598 if (type == VI_LINKTABLE) {
599 if ((count = namei_GetLinkCount(fdP, (Inode)0, 1))<0) {
600 FDH_REALLYCLOSE(fdP);
606 if (namei_SetLinkCount(fdP, (Inode)0, count<0 ? 0 : count, 1)<0) {
607 FDH_REALLYCLOSE(fdP);
613 FDH_REALLYCLOSE(fdP);
619 namei_HandleToName(&name, tmp);
620 if ((code = unlink(name.n_path)) == 0) {
621 if (type == VI_LINKTABLE) {
622 /* Try to remove directory. If it fails, that's ok.
623 * Salvage will clean up.
625 (void) namei_RemoveDataDirectories(&name);
628 FDH_REALLYCLOSE(fdP);
632 /* Get a file descriptor handle for this Inode */
638 if ((count = namei_GetLinkCount(fdP, ino, 1))<0) {
639 FDH_REALLYCLOSE(fdP);
645 if (namei_SetLinkCount(fdP, ino, count, 1)<0) {
646 FDH_REALLYCLOSE(fdP);
653 namei_HandleToName(&name, &th);
654 code = unlink(name.n_path);
662 int namei_inc(IHandle_t *h, Inode ino, int p1)
668 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
669 int type = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
670 if (type != VI_LINKTABLE)
675 /* Get a file descriptor handle for this Inode */
681 if ((count = namei_GetLinkCount(fdP, ino, 1))<0)
690 if (namei_SetLinkCount(fdP, ino, count, 1)<0)
694 FDH_REALLYCLOSE(fdP);
701 /************************************************************************
702 * File Name Structure
703 ************************************************************************
705 * Each AFS file needs a unique name and it needs to be findable with
706 * minimal lookup time. Note that the constraint on the number of files and
707 * directories in a volume is the size of the vnode index files and the
708 * max file size AFS supports (for internal files) of 2^31. Since a record
709 * in the small vnode index file is 64 bytes long, we can have at most
710 * (2^31)/64 or 33554432 files. A record in the large index file is
711 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
712 * Another layout parameter is that there is roughly a 16 to 1 ratio between
713 * the number of files and the number of directories.
715 * Using this information we can see that a layout of 256 directories, each
716 * with 512 subdirectories and each of those having 512 files gives us
717 * 256*512*512 = 67108864 AFS files and directories.
719 * The volume, vnode, uniquifier and data version, as well as the tag
720 * are required, either for finding the file or for salvaging. It's best to
721 * restrict the name to something that can be mapped into 64 bits so the
722 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
723 * is used to distinguish between different versions of the same file
724 * which are currently in the RW and clones of a volume. See "Link Table
725 * Organization" below for more information on the tag. The tag is
726 * required in the name of the file to ensure a unique name.
728 * We can store data in the uid, gid and mode bits of the files, provided
729 * the directories have root only access. This gives us 15 bits for each
730 * of uid and gid (GNU chown considers 65535 to mean "don't change").
731 * There are 9 available mode bits. Adn we need to store a total of
732 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
733 * or 131 bits somewhere.
735 * The format of a file name for a regular file is:
736 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
737 * V1 - low 8 bits of RW volume id
738 * V2 - all bits of RW volume id
739 * AA - high 8 bits of vnode number.
740 * BB - next 9 bits of vnode number.
741 * <tag><uniq><vno> - file name
743 * Volume special files are stored in a separate directory:
744 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
747 * The vnode is hashed into the directory using the high bits of the
748 * vnode number. This is so that consecutively created vnodes are in
749 * roughly the same area on the disk. This will at least be optimal if
750 * the user is creating many files in the same AFS directory. The name
751 * should be formed so that the leading characters are different as quickly
752 * as possible, leading to faster discards of incorrect matches in the
758 /************************************************************************
759 * Link Table Organization
760 ************************************************************************
762 * The link table volume special file is used to hold the link counts that
763 * are held in the inodes in inode based AFS vice filesystems. For user
764 * space access, the link counts are being kept in a separate
765 * volume special file. The file begins with the usual version stamp
766 * information and is then followed by one row per vnode number. vnode 0
767 * is used to hold the link count of the link table itself. That is because
768 * the same link table is shared among all the volumes of the volume group
769 * and is deleted only when the last volume of a volume group is deleted.
771 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
772 * link count from 0 through 7. Each colume represents a unique instance of
773 * that vnode. Say we have a file shared between the RW and a RO and a
774 * different version of the file (or a different uniquifer) for the BU volume.
775 * Then one column would be holding the link count of 2 for the RW and RO
776 * and a different column would hold the link count of 1 for the BU volume.
777 * Note that we allow only 5 volumes per file, giving 15 bits used in the
780 #define LINKTABLE_WIDTH 2
781 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
783 static void namei_GetLCOffsetAndIndexFromIno(Inode ino, int *offset, int *index)
785 int toff = (int) (ino & NAMEI_VNODEMASK);
786 int tindex = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
788 *offset = (toff << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
789 *index = (tindex << 1) + tindex;
793 /* namei_GetLinkCount
794 * If lockit is set, lock the file and leave it locked upon a successful
797 int namei_GetLinkCount(FdHandle_t *h, Inode ino, int lockit)
799 unsigned short row = 0;
802 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
805 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
806 if (lockf(h->fd_fd, F_LOCK, 0) < 0)
808 if (flock(h->fd_fd, LOCK_EX)<0)
813 if (lseek(h->fd_fd, offset, SEEK_SET) == -1)
814 goto bad_getLinkByte;
816 if (read(h->fd_fd, (char*)&row, sizeof(row))!=sizeof(row)) {
817 goto bad_getLinkByte;
820 return (int) ((row >> index) & NAMEI_TAGMASK);
824 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
825 lockf(h->fd_fd, F_ULOCK, 0);
827 flock(h->fd_fd, LOCK_UN);
832 /* Return a free column index for this vnode. */
833 static int GetFreeTag(IHandle_t *ih, int vno)
847 /* Only one manipulates at a time. */
848 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
849 if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
851 if (flock(fdP->fd_fd, LOCK_EX)<0) {
853 FDH_REALLYCLOSE(fdP);
857 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
858 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
862 code = read(fdP->fd_fd, (char*)&row, sizeof(row));
863 if (code != sizeof(row)) {
869 /* Now find a free column in this row and claim it. */
870 for (col = 0; col<NAMEI_MAXVOLS; col++) {
871 coldata = 7 << (col * 3);
872 if ((row & coldata) == 0)
875 if (col >= NAMEI_MAXVOLS)
878 coldata = 1 << (col * 3);
881 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
884 if (write(fdP->fd_fd, (char*)&row, sizeof(row))!=sizeof(row)) {
888 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
889 lockf(fdP->fd_fd, F_ULOCK, 0);
891 flock(fdP->fd_fd, LOCK_UN);
893 FDH_REALLYCLOSE(fdP);
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);
908 /* namei_SetLinkCount
909 * If locked is set, assume file is locked. Otherwise, lock file before
910 * proceeding to modify it.
912 int namei_SetLinkCount(FdHandle_t *fdP, Inode ino, int count, int locked)
919 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
922 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
923 if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
925 if (flock(fdP->fd_fd, LOCK_EX)<0) {
930 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
932 goto bad_SetLinkCount;
936 code = read(fdP->fd_fd, (char*)&row, sizeof(row));
937 if (code != sizeof(row)) {
940 goto bad_SetLinkCount;
947 row &= (unsigned short)~junk;
948 row |= (unsigned short)count;
950 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
952 goto bad_SetLinkCount;
955 if (write(fdP->fd_fd, (char*)&row, sizeof(short)) != sizeof(short)) {
957 goto bad_SetLinkCount;
965 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
966 lockf(fdP->fd_fd, F_ULOCK, 0);
968 flock(fdP->fd_fd, LOCK_UN);
975 /* ListViceInodes - write inode data to a results file. */
976 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
978 static int DecodeVolumeName(char *name, int *vid);
979 static int namei_ListAFSSubDirs(IHandle_t *dirIH,
980 int (*write_fun)(FILE *, struct ViceInodeInfo *,
983 int (*judgeFun)(struct ViceInodeInfo *, int vid),
984 int singleVolumeNumber);
989 * Write the inode data to the results file.
991 * Returns -2 on error, 0 on success.
993 * This is written as a callback simply so that other listing routines
994 * can use the same inode reading code.
996 static int WriteInodeInfo(FILE *fp, struct ViceInodeInfo *info, char *dir,
1000 n = fwrite(info, sizeof(*info), 1, fp);
1001 return (n == 1) ? 0 : -2;
1005 int mode_errors; /* Number of errors found in mode bits on directories. */
1006 void VerifyDirPerms(char *path)
1010 if (stat(path, &status)<0) {
1011 Log("Unable to stat %s. Please manually verify mode bits for this"
1012 " directory\n", path);
1015 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1021 * Fill the results file with the requested inode information.
1025 * -1 - complete failure, salvage should terminate.
1026 * -2 - not enough space on partition, salvager has error message for this.
1028 * This code optimizes single volume salvages by just looking at that one
1029 * volume's directory.
1031 * If the resultFile is NULL, then don't call the write routine.
1033 int ListViceInodes(char *devname, char *mountedOn, char *resultFile,
1034 int (*judgeInode)(struct ViceInodeInfo *info, int vid),
1035 int singleVolumeNumber, int *forcep,
1036 int forceR, char *wpath)
1038 FILE *fp = (FILE*)-1;
1043 fp = fopen(resultFile, "w");
1045 Log("Unable to create inode description file %s\n", resultFile);
1050 /* Verify protections on directories. */
1052 VerifyDirPerms(mountedOn);
1054 ninodes = namei_ListAFSFiles(mountedOn, WriteInodeInfo, fp,
1055 judgeInode, singleVolumeNumber);
1065 if (fflush(fp) == EOF) {
1066 Log("Unable to successfully flush inode file for %s\n", mountedOn);
1070 if (fsync(fileno(fp)) == -1) {
1071 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1075 if (fclose(fp) == EOF) {
1076 Log("Unable to successfully close inode file for %s\n", mountedOn);
1081 * Paranoia: check that the file is really the right size
1083 if (stat(resultFile, &status) == -1) {
1084 Log("Unable to successfully stat inode file for %s\n", mountedOn);
1087 if (status.st_size != ninodes * sizeof (struct ViceInodeInfo)) {
1088 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1089 status.st_size, ninodes * sizeof (struct ViceInodeInfo),
1097 /* namei_ListAFSFiles
1099 * Collect all the matching AFS files on the drive.
1100 * If singleVolumeNumber is non-zero, just return files for that volume.
1102 * Returns <0 on error, else number of files found to match.
1104 int namei_ListAFSFiles(char *dev,
1105 int (*writeFun)(FILE *, struct ViceInodeInfo *, char *,
1108 int (*judgeFun)(struct ViceInodeInfo *, int),
1109 int singleVolumeNumber)
1115 struct dirent *dp1, *dp2;
1118 static void FreeZLCList(void);
1121 memset((void*)&ih, 0, sizeof(IHandle_t));
1122 ih.ih_dev = volutil_GetPartitionID(dev);
1124 if (singleVolumeNumber) {
1125 ih.ih_vid = singleVolumeNumber;
1126 namei_HandleToVolDir(&name, &ih);
1127 ninodes = namei_ListAFSSubDirs(&ih, writeFun, fp,
1128 judgeFun, singleVolumeNumber);
1133 /* Find all volume data directories and descend through them. */
1134 namei_HandleToInodeDir(&name, &ih);
1136 dirp1 = opendir(name.n_path);
1139 while ((dp1 = readdir(dirp1))) {
1140 if (*dp1->d_name == '.') continue;
1141 (void) strcpy(path2, name.n_path);
1142 (void) strcat(path2, "/");
1143 (void) strcat(path2, dp1->d_name);
1144 dirp2 = opendir(path2);
1146 while ((dp2 = readdir(dirp2))) {
1147 if (*dp2->d_name == '.') continue;
1148 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1149 ninodes += namei_ListAFSSubDirs(&ih, writeFun, fp,
1166 /* namei_ListAFSSubDirs
1171 * > = 0 - number of AFS files found.
1173 static int namei_ListAFSSubDirs(IHandle_t *dirIH,
1174 int (*writeFun)(FILE *, struct ViceInodeInfo *,
1177 int (*judgeFun)(struct ViceInodeInfo *, int),
1178 int singleVolumeNumber)
1180 IHandle_t myIH = *dirIH;
1182 char path1[512], path2[512], path3[512];
1183 DIR *dirp1, *dirp2, *dirp3;
1184 struct dirent *dp1, *dp2, *dp3;
1185 struct ViceInodeInfo info;
1186 FdHandle_t linkHandle;
1190 static void AddToZLCDeleteList(char dir, char *name);
1191 static void DeleteZLCFiles(char *path);
1194 namei_HandleToVolDir(&name, &myIH);
1195 (void) strcpy(path1, name.n_path);
1197 /* Do the directory containing the special files first to pick up link
1200 (void) strcat(path1, "/");
1201 (void) strcat(path1, NAMEI_SPECDIR);
1203 linkHandle.fd_fd = -1;
1204 dirp1 = opendir(path1);
1206 while ((dp1 = readdir(dirp1))) {
1207 if (*dp1->d_name == '.') continue;
1208 if (DecodeInode(path1, dp1->d_name, &info, myIH.ih_vid)<0)
1210 if (info.u.param[2] != VI_LINKTABLE) {
1214 /* Open this handle */
1215 (void) sprintf(path2, "%s/%s", path1, dp1->d_name);
1216 linkHandle.fd_fd = open(path2, O_RDONLY, 0666);
1217 info.linkCount = namei_GetLinkCount(&linkHandle, (Inode)0, 0);
1219 if (judgeFun && !(*judgeFun)(&info, singleVolumeNumber))
1222 if ((*writeFun)(fp, &info, path1, dp1->d_name)<0) {
1223 if (linkHandle.fd_fd >= 0)
1224 close(linkHandle.fd_fd);
1233 /* Now run through all the other subdirs */
1234 namei_HandleToVolDir(&name, &myIH);
1235 (void) strcpy(path1, name.n_path);
1237 dirp1 = opendir(path1);
1239 while ((dp1 = readdir(dirp1))) {
1240 if (*dp1->d_name == '.') continue;
1241 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
1244 /* Now we've got a next level subdir. */
1245 (void) strcpy(path2, path1);
1246 (void) strcat(path2, "/");
1247 (void) strcat(path2, dp1->d_name);
1248 dirp2 = opendir(path2);
1250 while ((dp2 = readdir(dirp2))) {
1251 if (*dp2->d_name == '.') continue;
1253 /* Now we've got to the actual data */
1254 (void) strcpy(path3, path2);
1255 (void) strcat(path3, "/");
1256 (void) strcat(path3, dp2->d_name);
1257 dirp3 = opendir(path3);
1259 while ((dp3 = readdir(dirp3))) {
1260 if (*dp3->d_name == '.') continue;
1261 if (DecodeInode(path3, dp3->d_name, &info,
1264 info.linkCount = namei_GetLinkCount(&linkHandle,
1265 info.inodeNumber, 0);
1266 if (info.linkCount == 0) {
1268 Log("Found 0 link count file %s/%s, deleting it.\n",
1269 path3, dp3->d_name);
1270 AddToZLCDeleteList((char)i, dp3->d_name);
1272 Log("Found 0 link count file %s/%s.\n",
1273 path3, dp3->d_name);
1278 && !(*judgeFun)(&info, singleVolumeNumber))
1281 if ((*writeFun)(fp, &info, path3, dp3->d_name)<0) {
1282 close(linkHandle.fd_fd);
1299 if (linkHandle.fd_fd >= 0)
1300 close(linkHandle.fd_fd);
1302 /* Then why does this directory exist? Blow it away. */
1303 namei_HandleToVolDir(&name, dirIH);
1304 namei_RemoveDataDirectories(&name);
1310 static int DecodeVolumeName(char *name, int *vid)
1312 if (strlen(name) <= 2)
1314 *vid = (int) flipbase64_to_int64(name);
1321 * Get the inode number from the name.
1324 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1331 (void) strcpy(fpath, dpath);
1332 (void) strcat(fpath, "/");
1333 (void) strcat(fpath, name);
1335 if (stat(fpath, &status)<0) {
1339 info->byteCount = status.st_size;
1340 info->inodeNumber = flipbase64_to_int64(name);
1342 GetOGMFromStat(&status, &parm, &tag);
1343 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1344 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
1345 info->u.param[0] = parm;
1346 info->u.param[1] = -1;
1347 info->u.param[2] = tag;
1348 info->u.param[3] = volid;
1351 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
1352 info->u.param[0] = volid;
1353 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
1354 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
1355 & (Inode)NAMEI_UNIQMASK);
1356 info->u.param[3] = parm;
1364 * returns a static string used to print either 32 or 64 bit inode numbers.
1366 char * PrintInode(char *s, Inode ino)
1368 static afs_ino_str_t result;
1372 (void) sprintf((char*)s, "%Lu", ino);
1379 /* Routines to facilitate removing zero link count files. */
1380 #define MAX_ZLC_NAMES 32
1381 #define MAX_ZLC_NAMELEN 16
1382 typedef struct zlcList_s {
1383 struct zlcList_s *zlc_next;
1385 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1388 static zlcList_t *zlcAnchor = NULL;
1389 static zlcList_t *zlcCur = NULL;
1391 static void AddToZLCDeleteList(char dir, char *name)
1393 assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1395 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1396 if (zlcCur && zlcCur->zlc_next)
1397 zlcCur = zlcCur->zlc_next;
1399 zlcList_t *tmp = (zlcList_t*)malloc(sizeof(zlcList_t));
1406 zlcCur->zlc_next = tmp;
1410 zlcCur->zlc_next = NULL;
1414 (void) sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1418 static void DeleteZLCFiles(char *path)
1424 for (z = zlcAnchor; z; z = z->zlc_next) {
1425 for (i=0; i < z->zlc_n; i++) {
1426 (void) sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1427 if (namei_unlink(fname)<0) {
1428 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1432 z->zlc_n = 0; /* Can reuse space. */
1437 static void FreeZLCList(void)
1444 tnext = i->zlc_next;
1448 zlcCur = zlcAnchor = NULL;
1452 #endif /* AFS_NAMEI_ENV */