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"
45 #include <afs/errors.h>
47 extern char *volutil_PartitionName_r(int volid, char *buf, int buflen);
49 int namei_iread(IHandle_t *h, int offset, char *buf, int size)
58 if (FDH_SEEK(fdP, offset, SEEK_SET)<0) {
63 nBytes = FDH_READ(fdP, buf, size);
68 int namei_iwrite(IHandle_t *h, int offset, char *buf, int size)
77 if (FDH_SEEK(fdP, offset, SEEK_SET)<0) {
81 nBytes = FDH_WRITE(fdP, buf, size);
88 /* Inode number format:
89 * low 26 bits - vnode number - all 1's if volume special file.
91 * next 3 bits spare (0's)
92 * high 32 bits - uniquifier (regular) or type if spare
94 #define NAMEI_VNODEMASK 0x003ffffff
95 #define NAMEI_TAGMASK 0x7
96 #define NAMEI_TAGSHIFT 26
97 #define NAMEI_UNIQMASK 0xffffffff
98 #define NAMEI_UNIQSHIFT 32
99 #define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
100 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
102 /* dir1 is the high 8 bits of the 26 bit vnode */
103 #define VNO_DIR1(vno) ((vno >> 14) & 0xff)
104 /* dir2 is the next 9 bits */
105 #define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
106 /* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
108 #define NAMEI_SPECDIR "special"
109 #define NAMEI_SPECDIRLEN (sizeof(NAMEI_SPECDIR)-1)
111 #define NAMEI_MAXVOLS 5 /* Maximum supported number of volumes per volume
112 * group, not counting temporary (move) volumes.
113 * This is the number of separate files, all having
114 * the same vnode number, which can occur in a volume
125 int namei_SetLinkCount(FdHandle_t *h, Inode ino, int count, int locked);
126 static int GetFreeTag(IHandle_t *ih, int vno);
128 /* namei_HandleToInodeDir
130 * Construct the path name of the directory holding the inode data.
131 * Format: /<vicepx>/INODEDIR
134 #define PNAME_BLEN 64
135 static void namei_HandleToInodeDir(namei_t *name, IHandle_t *ih)
137 char *tmp = name->n_base;
139 memset(name, '\0', sizeof(*name));
141 (void) volutil_PartitionName_r(ih->ih_dev, tmp, NAMEI_LCOMP_LEN);
142 tmp += VICE_PREFIX_SIZE;
143 tmp += ih->ih_dev > 25 ? 2 : 1;
145 (void) strcpy(tmp, INODEDIR);
146 (void) strcpy(name->n_path, name->n_base);
149 #define addtoname(N, C) \
151 strcat((N)->n_path, "/"); strcat((N)->n_path, C); \
155 static void namei_HandleToVolDir(namei_t *name, IHandle_t *ih)
159 namei_HandleToInodeDir(name, ih);
160 (void) int32_to_flipbase64(tmp, (int64_t)(ih->ih_vid & 0xff));
161 (void) strcpy(name->n_voldir1, tmp);
162 addtoname(name, name->n_voldir1);
163 (void) int32_to_flipbase64(tmp, (int64_t)ih->ih_vid);
164 (void) strcpy(name->n_voldir2, tmp);
165 addtoname(name, name->n_voldir2);
168 /* namei_HandleToName
170 * Constructs a file name for the fully qualified handle.
171 * Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special
173 void namei_HandleToName(namei_t *name, IHandle_t *ih)
176 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
178 namei_HandleToVolDir(name, ih);
180 if (vno == NAMEI_VNODESPECIAL) {
181 (void) strcpy(name->n_dir1, NAMEI_SPECDIR);
182 addtoname(name, name->n_dir1);
183 name->n_dir2[0] = '\0';
186 (void) int32_to_flipbase64(str, VNO_DIR1(vno));
187 (void) strcpy(name->n_dir1, str);
188 addtoname(name, name->n_dir1);
189 (void) int32_to_flipbase64(str, VNO_DIR2(vno));
190 (void) strcpy(name->n_dir2, str);
191 addtoname(name, name->n_dir2);
193 (void) int64_to_flipbase64(str, (int64_t)ih->ih_ino);
194 (void) strcpy(name->n_inode, str);
195 addtoname(name, name->n_inode);
198 /* The following is a warning to tell sys-admins to not muck about in this
201 #define VICE_README "These files and directories are a part of the AFS \
202 namespace. Modifying them\nin any way will result in loss of AFS data.\n"
203 int namei_ViceREADME(char *partition)
208 /* Create the inode directory if we're starting for the first time */
209 sprintf(filename, "%s/%s", partition, INODEDIR);
210 mkdir(filename, 0700);
212 sprintf(filename, "%s/%s/README", partition, INODEDIR);
213 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
215 write(fd, VICE_README, strlen(VICE_README));
222 #define create_dir() \
224 if (mkdir(tmp, 0700)<0) { \
225 if (errno != EEXIST) \
233 #define create_nextdir(A) \
235 strcat(tmp, "/"); strcat(tmp, A); create_dir(); \
238 /* namei_CreateDataDirectories
240 * If creating the file failed because of ENOENT or ENOTDIR, try
241 * creating all the directories first.
243 static int namei_CreateDataDirectories(namei_t *name, int *created)
249 (void) strcpy(tmp, name->n_base);
252 create_nextdir(name->n_voldir1);
253 create_nextdir(name->n_voldir2);
254 create_nextdir(name->n_dir1);
255 if (name->n_dir2[0]) {
256 create_nextdir(name->n_dir2);
261 /* delTree(): Deletes an entire tree of directories (no files)
263 * root : Full path to the subtree. Should be big enough for PATH_MAX
264 * tree : the subtree to be deleted is rooted here. Specifies only the
265 * subtree beginning at tree (not the entire path). It should be
266 * a pointer into the "root" buffer.
268 * errp : errno of the first error encountered during the directory cleanup.
269 * *errp should have been initialized to 0.
272 * -1 : If errors were encountered during cleanup and error is set to
276 * If there are errors, we try to work around them and delete as many
277 * directories as possible. We don't attempt to remove directories that still
278 * have non-dir entries in them.
281 delTree(char *root, char *tree, int *errp)
289 /* delete the children first */
290 cp = strchr(tree, '/');
292 delTree(root, cp+1, errp);
296 cp = tree + strlen(tree); /* move cp to the end of string tree */
298 /* now delete all entries in this dir */
299 if ( (ds = opendir(root)) != (DIR *)NULL) {
301 while ((dirp = readdir(ds))) {
302 /* ignore . and .. */
303 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
305 /* since root is big enough, we reuse the space to
306 * concatenate the dirname to the current tree
309 strcat(root, dirp->d_name);
310 if ( stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
311 /* delete this subtree */
312 delTree(root, cp+1, errp);
314 *errp = *errp ? *errp : errno;
316 /* recover path to our cur tree by truncating it to
321 /* if (!errno) -- closedir not implicit if we got an error */
325 /* finally axe the current dir */
327 *errp = *errp ? *errp : errno;
329 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
331 #endif /* !AFS_PTHREAD_ENV */
333 } /* if valid tree */
335 /* if we encountered errors during cleanup, we return a -1 */
343 /* namei_RemoveDataDirectories
345 * Returns 0 on success.
346 * Returns -1 on error. Typically, callers ignore this error bcause we
347 * can continue running if the removes fail. The salvage process will
348 * finish tidying up for us. We only use the n_base and n_voldir1 entries
349 * and only do rmdir's.
352 static int namei_RemoveDataDirectories(namei_t *name)
354 char pbuf[MAXPATHLEN], *path = pbuf;
355 int prefixlen = strlen(name->n_base), err = 0;
357 strcpy(path, name->n_path);
359 /* move past the prefix */
360 path = path+prefixlen+1; /* skip over the trailing / */
362 /* now delete all dirs upto path */
363 return delTree(pbuf, path, &err);
367 /* Create the file in the name space.
369 * Parameters stored as follows:
371 * p1 - volid - implied in containing directory.
372 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
373 * p3 - uniq -- bits 4-0 are in mode bits 4-0
374 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
376 * p1 - volid - creation time - dwHighDateTime
377 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
378 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
379 * p4 - parid - parent volume id - implied in containing directory.
381 * Return value is the inode number or (Inode)-1 if error.
382 * We "know" there is only one link table, so return EEXIST if there already
383 * is a link table. It's up to the calling code to test errno and increment
389 * This function is called by VCreateVolume to hide the implementation
390 * details of the inode numbers. This only allows for 7 volume special
391 * types, but if we get that far, this could should be dead by then.
393 Inode namei_MakeSpecIno(int volid, int type)
396 ino = NAMEI_INODESPECIAL;
397 type &= NAMEI_TAGMASK;
398 ino |= ((Inode)type) << NAMEI_TAGSHIFT;
399 ino |= ((Inode)volid) << NAMEI_UNIQSHIFT;
403 /* SetOGM - set owner group and mode bits from parm and tag
405 * owner - low 15 bits of parm.
406 * group - next 15 bits of parm.
407 * mode - 2 bits of parm, then lowest = 3 bits of tag.
409 static int SetOGM(int fd, int parm, int tag)
411 int owner, group, mode;
413 owner = parm & 0x7fff;
414 group = (parm >> 15) & 0x7fff;
415 if (fchown(fd, owner, group)<0)
418 mode = (parm >> 27) & 0x18;
420 if (fchmod(fd, mode)<0)
427 /* GetOGM - get parm and tag from owner, group and mode bits. */
428 static void GetOGMFromStat(struct stat *status, int *parm, int *tag)
430 *parm = status->st_uid | (status->st_gid << 15);
431 *parm |= (status->st_mode & 0x18) << 27;
432 *tag = status->st_mode & 0x7;
435 static int GetOGM(int fd, int *parm, int *tag)
438 if (fstat(fd, &status)<0)
441 GetOGMFromStat(&status, parm, tag);
445 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
447 /* Derive the name and create it O_EXCL. If that fails we have an error.
448 * Get the tag from a free column in the link table.
450 Inode namei_icreate(IHandle_t *lh, char *part, int p1, int p2, int p3, int p4)
463 memset((void*)&tmp, 0, sizeof(IHandle_t));
466 tmp.ih_dev = volutil_GetPartitionID(part);
467 if (tmp.ih_dev == -1) {
473 /* Parameters for special file:
474 * p1 - volume id - goes into owner/group/mode
477 * p4 - parent volume id
482 tmp.ih_vid = p4; /* Use parent volume id, where this file will be.*/
483 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
486 int vno = p2 & NAMEI_VNODEMASK;
487 /* Parameters for regular file:
499 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
500 tag = GetFreeTag(lh, p2);
504 /* name is <uniq(p3)><tag><vno(p2)> */
506 tmp.ih_ino = (Inode)p2;
507 tmp.ih_ino |= ((Inode)tag)<<NAMEI_TAGSHIFT;
508 tmp.ih_ino |= ((Inode)p3)<<NAMEI_UNIQSHIFT;
513 namei_HandleToName(&name, &tmp);
514 fd = open(name.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
516 if (errno == ENOTDIR || errno == ENOENT) {
517 if (namei_CreateDataDirectories(&name, &created_dir)<0)
519 fd = open(name.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
527 if (SetOGM(fd, ogm_parm, tag)<0) {
533 if (p2 == -1 && p3 == VI_LINKTABLE) {
534 /* hack at tmp to setup for set link count call. */
536 code = namei_SetLinkCount(&tfd, (Inode)0, 1, 0);
544 if (code || (fd < 0)) {
548 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
553 return (code || (fd<0)) ? (Inode)-1 : tmp.ih_ino;
558 int namei_iopen(IHandle_t *h)
563 /* Convert handle to file name. */
564 namei_HandleToName(&name, h);
565 fd = open(name.n_path, O_RDWR, 0666);
569 /* Need to detect vol special file and just unlink. In those cases, the
570 * handle passed in _is_ for the inode. We only check p1 for the special
573 int namei_dec(IHandle_t *ih, Inode ino, int p1)
580 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
583 int type = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
585 /* Verify this is the right file. */
586 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
595 if ((GetOGM(fdP->fd_fd, &inode_p1, &tag)<0) || (inode_p1 != p1)) {
596 FDH_REALLYCLOSE(fdP);
602 /* If it's the link table itself, decrement the link count. */
603 if (type == VI_LINKTABLE) {
604 if ((count = namei_GetLinkCount(fdP, (Inode)0, 1))<0) {
605 FDH_REALLYCLOSE(fdP);
611 if (namei_SetLinkCount(fdP, (Inode)0, count<0 ? 0 : count, 1)<0) {
612 FDH_REALLYCLOSE(fdP);
618 FDH_REALLYCLOSE(fdP);
624 namei_HandleToName(&name, tmp);
625 if ((code = unlink(name.n_path)) == 0) {
626 if (type == VI_LINKTABLE) {
627 /* Try to remove directory. If it fails, that's ok.
628 * Salvage will clean up.
630 (void) namei_RemoveDataDirectories(&name);
633 FDH_REALLYCLOSE(fdP);
637 /* Get a file descriptor handle for this Inode */
643 if ((count = namei_GetLinkCount(fdP, ino, 1))<0) {
644 FDH_REALLYCLOSE(fdP);
650 if (namei_SetLinkCount(fdP, ino, count, 1)<0) {
651 FDH_REALLYCLOSE(fdP);
658 namei_HandleToName(&name, &th);
659 code = unlink(name.n_path);
667 int namei_inc(IHandle_t *h, Inode ino, int p1)
673 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
674 int type = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
675 if (type != VI_LINKTABLE)
680 /* Get a file descriptor handle for this Inode */
686 if ((count = namei_GetLinkCount(fdP, ino, 1))<0)
695 if (namei_SetLinkCount(fdP, ino, count, 1)<0)
699 FDH_REALLYCLOSE(fdP);
706 /************************************************************************
707 * File Name Structure
708 ************************************************************************
710 * Each AFS file needs a unique name and it needs to be findable with
711 * minimal lookup time. Note that the constraint on the number of files and
712 * directories in a volume is the size of the vnode index files and the
713 * max file size AFS supports (for internal files) of 2^31. Since a record
714 * in the small vnode index file is 64 bytes long, we can have at most
715 * (2^31)/64 or 33554432 files. A record in the large index file is
716 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
717 * Another layout parameter is that there is roughly a 16 to 1 ratio between
718 * the number of files and the number of directories.
720 * Using this information we can see that a layout of 256 directories, each
721 * with 512 subdirectories and each of those having 512 files gives us
722 * 256*512*512 = 67108864 AFS files and directories.
724 * The volume, vnode, uniquifier and data version, as well as the tag
725 * are required, either for finding the file or for salvaging. It's best to
726 * restrict the name to something that can be mapped into 64 bits so the
727 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
728 * is used to distinguish between different versions of the same file
729 * which are currently in the RW and clones of a volume. See "Link Table
730 * Organization" below for more information on the tag. The tag is
731 * required in the name of the file to ensure a unique name.
733 * We can store data in the uid, gid and mode bits of the files, provided
734 * the directories have root only access. This gives us 15 bits for each
735 * of uid and gid (GNU chown considers 65535 to mean "don't change").
736 * There are 9 available mode bits. Adn we need to store a total of
737 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
738 * or 131 bits somewhere.
740 * The format of a file name for a regular file is:
741 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
742 * V1 - low 8 bits of RW volume id
743 * V2 - all bits of RW volume id
744 * AA - high 8 bits of vnode number.
745 * BB - next 9 bits of vnode number.
746 * <tag><uniq><vno> - file name
748 * Volume special files are stored in a separate directory:
749 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
752 * The vnode is hashed into the directory using the high bits of the
753 * vnode number. This is so that consecutively created vnodes are in
754 * roughly the same area on the disk. This will at least be optimal if
755 * the user is creating many files in the same AFS directory. The name
756 * should be formed so that the leading characters are different as quickly
757 * as possible, leading to faster discards of incorrect matches in the
763 /************************************************************************
764 * Link Table Organization
765 ************************************************************************
767 * The link table volume special file is used to hold the link counts that
768 * are held in the inodes in inode based AFS vice filesystems. For user
769 * space access, the link counts are being kept in a separate
770 * volume special file. The file begins with the usual version stamp
771 * information and is then followed by one row per vnode number. vnode 0
772 * is used to hold the link count of the link table itself. That is because
773 * the same link table is shared among all the volumes of the volume group
774 * and is deleted only when the last volume of a volume group is deleted.
776 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
777 * link count from 0 through 7. Each colume represents a unique instance of
778 * that vnode. Say we have a file shared between the RW and a RO and a
779 * different version of the file (or a different uniquifer) for the BU volume.
780 * Then one column would be holding the link count of 2 for the RW and RO
781 * and a different column would hold the link count of 1 for the BU volume.
782 * Note that we allow only 5 volumes per file, giving 15 bits used in the
785 #define LINKTABLE_WIDTH 2
786 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
788 static void namei_GetLCOffsetAndIndexFromIno(Inode ino, int *offset, int *index)
790 int toff = (int) (ino & NAMEI_VNODEMASK);
791 int tindex = (int)((ino>>NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
793 *offset = (toff << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
794 *index = (tindex << 1) + tindex;
798 /* namei_GetLinkCount
799 * If lockit is set, lock the file and leave it locked upon a successful
802 int namei_GetLinkCount(FdHandle_t *h, Inode ino, int lockit)
804 unsigned short row = 0;
807 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
810 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
811 if (lockf(h->fd_fd, F_LOCK, 0) < 0)
813 if (flock(h->fd_fd, LOCK_EX)<0)
818 if (lseek(h->fd_fd, offset, SEEK_SET) == -1)
819 goto bad_getLinkByte;
821 if (read(h->fd_fd, (char*)&row, sizeof(row))!=sizeof(row)) {
822 goto bad_getLinkByte;
825 return (int) ((row >> index) & NAMEI_TAGMASK);
829 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
830 lockf(h->fd_fd, F_ULOCK, 0);
832 flock(h->fd_fd, LOCK_UN);
837 /* Return a free column index for this vnode. */
838 static int GetFreeTag(IHandle_t *ih, int vno)
852 /* Only one manipulates at a time. */
853 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
854 if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
856 if (flock(fdP->fd_fd, LOCK_EX)<0) {
858 FDH_REALLYCLOSE(fdP);
862 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
863 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
867 code = read(fdP->fd_fd, (char*)&row, sizeof(row));
868 if (code != sizeof(row)) {
874 /* Now find a free column in this row and claim it. */
875 for (col = 0; col<NAMEI_MAXVOLS; col++) {
876 coldata = 7 << (col * 3);
877 if ((row & coldata) == 0)
880 if (col >= NAMEI_MAXVOLS)
883 coldata = 1 << (col * 3);
886 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
889 if (write(fdP->fd_fd, (char*)&row, sizeof(row))!=sizeof(row)) {
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);
902 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
903 lockf(fdP->fd_fd, F_ULOCK, 0);
905 flock(fdP->fd_fd, LOCK_UN);
907 FDH_REALLYCLOSE(fdP);
913 /* namei_SetLinkCount
914 * If locked is set, assume file is locked. Otherwise, lock file before
915 * proceeding to modify it.
917 int namei_SetLinkCount(FdHandle_t *fdP, Inode ino, int count, int locked)
924 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
927 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
928 if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
930 if (flock(fdP->fd_fd, LOCK_EX)<0) {
935 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
937 goto bad_SetLinkCount;
941 code = read(fdP->fd_fd, (char*)&row, sizeof(row));
942 if (code != sizeof(row)) {
945 goto bad_SetLinkCount;
952 row &= (unsigned short)~junk;
953 row |= (unsigned short)count;
955 if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
957 goto bad_SetLinkCount;
960 if (write(fdP->fd_fd, (char*)&row, sizeof(short)) != sizeof(short)) {
962 goto bad_SetLinkCount;
970 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
971 lockf(fdP->fd_fd, F_ULOCK, 0);
973 flock(fdP->fd_fd, LOCK_UN);
980 /* ListViceInodes - write inode data to a results file. */
981 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
983 static int DecodeVolumeName(char *name, int *vid);
984 static int namei_ListAFSSubDirs(IHandle_t *dirIH,
985 int (*write_fun)(FILE *, struct ViceInodeInfo *,
988 int (*judgeFun)(struct ViceInodeInfo *, int vid),
989 int singleVolumeNumber);
994 * Write the inode data to the results file.
996 * Returns -2 on error, 0 on success.
998 * This is written as a callback simply so that other listing routines
999 * can use the same inode reading code.
1001 static int WriteInodeInfo(FILE *fp, struct ViceInodeInfo *info, char *dir,
1005 n = fwrite(info, sizeof(*info), 1, fp);
1006 return (n == 1) ? 0 : -2;
1010 int mode_errors; /* Number of errors found in mode bits on directories. */
1011 void VerifyDirPerms(char *path)
1015 if (stat(path, &status)<0) {
1016 Log("Unable to stat %s. Please manually verify mode bits for this"
1017 " directory\n", path);
1020 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1026 * Fill the results file with the requested inode information.
1030 * -1 - complete failure, salvage should terminate.
1031 * -2 - not enough space on partition, salvager has error message for this.
1033 * This code optimizes single volume salvages by just looking at that one
1034 * volume's directory.
1036 * If the resultFile is NULL, then don't call the write routine.
1038 int ListViceInodes(char *devname, char *mountedOn, char *resultFile,
1039 int (*judgeInode)(struct ViceInodeInfo *info, int vid),
1040 int singleVolumeNumber, int *forcep,
1041 int forceR, char *wpath)
1043 FILE *fp = (FILE*)-1;
1048 fp = fopen(resultFile, "w");
1050 Log("Unable to create inode description file %s\n", resultFile);
1055 /* Verify protections on directories. */
1057 VerifyDirPerms(mountedOn);
1059 ninodes = namei_ListAFSFiles(mountedOn, WriteInodeInfo, fp,
1060 judgeInode, singleVolumeNumber);
1070 if (fflush(fp) == EOF) {
1071 Log("Unable to successfully flush inode file for %s\n", mountedOn);
1075 if (fsync(fileno(fp)) == -1) {
1076 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1080 if (fclose(fp) == EOF) {
1081 Log("Unable to successfully close inode file for %s\n", mountedOn);
1086 * Paranoia: check that the file is really the right size
1088 if (stat(resultFile, &status) == -1) {
1089 Log("Unable to successfully stat inode file for %s\n", mountedOn);
1092 if (status.st_size != ninodes * sizeof (struct ViceInodeInfo)) {
1093 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1094 status.st_size, ninodes * sizeof (struct ViceInodeInfo),
1102 /* namei_ListAFSFiles
1104 * Collect all the matching AFS files on the drive.
1105 * If singleVolumeNumber is non-zero, just return files for that volume.
1107 * Returns <0 on error, else number of files found to match.
1109 int namei_ListAFSFiles(char *dev,
1110 int (*writeFun)(FILE *, struct ViceInodeInfo *, char *,
1113 int (*judgeFun)(struct ViceInodeInfo *, int),
1114 int singleVolumeNumber)
1120 struct dirent *dp1, *dp2;
1123 static void FreeZLCList(void);
1126 memset((void*)&ih, 0, sizeof(IHandle_t));
1127 ih.ih_dev = volutil_GetPartitionID(dev);
1129 if (singleVolumeNumber) {
1130 ih.ih_vid = singleVolumeNumber;
1131 namei_HandleToVolDir(&name, &ih);
1132 ninodes = namei_ListAFSSubDirs(&ih, writeFun, fp,
1133 judgeFun, singleVolumeNumber);
1138 /* Find all volume data directories and descend through them. */
1139 namei_HandleToInodeDir(&name, &ih);
1141 dirp1 = opendir(name.n_path);
1144 while ((dp1 = readdir(dirp1))) {
1145 if (*dp1->d_name == '.') continue;
1146 (void) strcpy(path2, name.n_path);
1147 (void) strcat(path2, "/");
1148 (void) strcat(path2, dp1->d_name);
1149 dirp2 = opendir(path2);
1151 while ((dp2 = readdir(dirp2))) {
1152 if (*dp2->d_name == '.') continue;
1153 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1154 ninodes += namei_ListAFSSubDirs(&ih, writeFun, fp,
1171 /* namei_ListAFSSubDirs
1176 * > = 0 - number of AFS files found.
1178 static int namei_ListAFSSubDirs(IHandle_t *dirIH,
1179 int (*writeFun)(FILE *, struct ViceInodeInfo *,
1182 int (*judgeFun)(struct ViceInodeInfo *, int),
1183 int singleVolumeNumber)
1185 IHandle_t myIH = *dirIH;
1187 char path1[512], path2[512], path3[512];
1188 DIR *dirp1, *dirp2, *dirp3;
1189 struct dirent *dp1, *dp2, *dp3;
1190 struct ViceInodeInfo info;
1191 FdHandle_t linkHandle;
1195 static void AddToZLCDeleteList(char dir, char *name);
1196 static void DeleteZLCFiles(char *path);
1199 namei_HandleToVolDir(&name, &myIH);
1200 (void) strcpy(path1, name.n_path);
1202 /* Do the directory containing the special files first to pick up link
1205 (void) strcat(path1, "/");
1206 (void) strcat(path1, NAMEI_SPECDIR);
1208 linkHandle.fd_fd = -1;
1209 dirp1 = opendir(path1);
1211 while ((dp1 = readdir(dirp1))) {
1212 if (*dp1->d_name == '.') continue;
1213 if (DecodeInode(path1, dp1->d_name, &info, myIH.ih_vid)<0)
1215 if (info.u.param[2] != VI_LINKTABLE) {
1219 /* Open this handle */
1220 (void) sprintf(path2, "%s/%s", path1, dp1->d_name);
1221 linkHandle.fd_fd = open(path2, O_RDONLY, 0666);
1222 info.linkCount = namei_GetLinkCount(&linkHandle, (Inode)0, 0);
1224 if (judgeFun && !(*judgeFun)(&info, singleVolumeNumber))
1227 if ((*writeFun)(fp, &info, path1, dp1->d_name)<0) {
1228 if (linkHandle.fd_fd >= 0)
1229 close(linkHandle.fd_fd);
1238 /* Now run through all the other subdirs */
1239 namei_HandleToVolDir(&name, &myIH);
1240 (void) strcpy(path1, name.n_path);
1242 dirp1 = opendir(path1);
1244 while ((dp1 = readdir(dirp1))) {
1245 if (*dp1->d_name == '.') continue;
1246 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
1249 /* Now we've got a next level subdir. */
1250 (void) strcpy(path2, path1);
1251 (void) strcat(path2, "/");
1252 (void) strcat(path2, dp1->d_name);
1253 dirp2 = opendir(path2);
1255 while ((dp2 = readdir(dirp2))) {
1256 if (*dp2->d_name == '.') continue;
1258 /* Now we've got to the actual data */
1259 (void) strcpy(path3, path2);
1260 (void) strcat(path3, "/");
1261 (void) strcat(path3, dp2->d_name);
1262 dirp3 = opendir(path3);
1264 while ((dp3 = readdir(dirp3))) {
1265 if (*dp3->d_name == '.') continue;
1266 if (DecodeInode(path3, dp3->d_name, &info,
1269 info.linkCount = namei_GetLinkCount(&linkHandle,
1270 info.inodeNumber, 0);
1271 if (info.linkCount == 0) {
1273 Log("Found 0 link count file %s/%s, deleting it.\n",
1274 path3, dp3->d_name);
1275 AddToZLCDeleteList((char)i, dp3->d_name);
1277 Log("Found 0 link count file %s/%s.\n",
1278 path3, dp3->d_name);
1283 && !(*judgeFun)(&info, singleVolumeNumber))
1286 if ((*writeFun)(fp, &info, path3, dp3->d_name)<0) {
1287 close(linkHandle.fd_fd);
1304 if (linkHandle.fd_fd >= 0)
1305 close(linkHandle.fd_fd);
1307 /* Then why does this directory exist? Blow it away. */
1308 namei_HandleToVolDir(&name, dirIH);
1309 namei_RemoveDataDirectories(&name);
1315 static int DecodeVolumeName(char *name, int *vid)
1317 if (strlen(name) <= 2)
1319 *vid = (int) flipbase64_to_int64(name);
1326 * Get the inode number from the name.
1329 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1336 (void) strcpy(fpath, dpath);
1337 (void) strcat(fpath, "/");
1338 (void) strcat(fpath, name);
1340 if (stat(fpath, &status)<0) {
1344 info->byteCount = status.st_size;
1345 info->inodeNumber = flipbase64_to_int64(name);
1347 GetOGMFromStat(&status, &parm, &tag);
1348 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1349 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
1350 info->u.param[0] = parm;
1351 info->u.param[1] = -1;
1352 info->u.param[2] = tag;
1353 info->u.param[3] = volid;
1356 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
1357 info->u.param[0] = volid;
1358 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
1359 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
1360 & (Inode)NAMEI_UNIQMASK);
1361 info->u.param[3] = parm;
1366 * Convert the VolumeInfo file from RO to RW
1367 * this routine is called by namei_convertROtoRWvolume()
1370 static afs_int32 convertVolumeInfo(fdr, fdw, vid)
1375 struct VolumeDiskData vd;
1378 if (read(fdr, &vd, sizeof(struct VolumeDiskData)) != sizeof(struct VolumeDiskData)) {
1379 Log("1 convertVolumeInfo: read failed for %lu with code %d\n",
1383 vd.restoredFromId = vd.id; /* remember the RO volume here */
1385 vd.id = vd.parentId;
1388 vd.uniquifier += 5000; /* just in case there are still file copies from
1389 the old RW volume around */
1390 p = strrchr(vd.name, '.');
1391 if (p && !strcmp(p, ".readonly")) {
1394 if (write(fdw, &vd, sizeof(struct VolumeDiskData)) != sizeof(struct VolumeDiskData)) {
1395 Log("1 convertVolumeInfo: write failed for %lu with code %d\n",
1403 * Convert a RO-volume into a RW-volume
1405 * This function allows to recover very fast from the loss of a partition
1406 * from RO-copies if all RO-Copies exist on another partition.
1407 * Then these RO-volumes can be made to the new RW-volumes.
1408 * Backup of RW-volumes then consists in "vos release".
1410 * We must make sure in this partition exists only the RO-volume which
1411 * is typical for remote replicas.
1413 * Then the linktable is already ok,
1414 * the vnode files need to be renamed
1415 * the volinfo file needs to be replaced by another one with
1416 * slightly different contents and new name.
1417 * The volume header file of the RO-volume in the /vicep<x> directory
1418 * is destroyed by this call. A new header file for the RW-volume must
1419 * be created after return from this routine.
1422 int namei_ConvertROtoRWvolume(IHandle_t * h, afs_uint32 vid)
1425 char dir_name[512], oldpath[512], newpath[512];
1438 struct ViceInodeInfo info;
1440 namei_HandleToName(&n, h);
1441 strcpy(dir_name, n.n_path);
1442 p = strrchr(dir_name, '/');
1444 dirp = opendir(dir_name);
1446 Log("1 namei_ConvertROtoRWvolume: Could not opendir(%s)\n",
1451 while (dp = readdir(dirp)) {
1452 /* struct ViceInodeInfo info; */
1454 if (*dp->d_name == '.') continue;
1455 if (DecodeInode(dir_name, dp->d_name, &info, h->ih_vid)<0) {
1456 Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s/%s\n",
1457 dir_name, dp->d_name);
1461 if (info.u.param[1] != -1) {
1462 Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s/%s\n",
1463 dir_name, dp->d_name);
1467 if (info.u.param[0] != vid) {
1468 if (info.u.param[0] == h->ih_vid) {
1469 if (info.u.param[2] == VI_LINKTABLE) { /* link table */
1474 Log("1 namei_ConvertROtoRWvolume: found special file %s/%s for volume %lu\n",
1475 dir_name, dp->d_name, info.u.param[0]);
1479 if (info.u.param[2] == VI_VOLINFO) { /* volume info file */
1480 strcpy(infoName, dp->d_name);
1483 else if (info.u.param[2] == VI_SMALLINDEX) { /* small vnodes file */
1484 strcpy(smallName, dp->d_name);
1487 else if (info.u.param[2] == VI_LARGEINDEX) { /* large vnodes file */
1488 strcpy(largeName, dp->d_name);
1493 Log("1 namei_ConvertROtoRWvolume: unknown type %d of special file found : %s/%s\n",
1494 info.u.param[2], dir_name, dp->d_name);
1500 if (!infoSeen || !smallSeen || !largeSeen || !linkSeen) {
1501 Log("1 namei_ConvertROtoRWvolume: not all special files found in %s\n",
1507 * If we come here then there was only a RO-volume and we can safely
1511 bzero(&t_ih, sizeof(t_ih));
1512 t_ih.ih_dev = h->ih_dev;
1513 t_ih.ih_vid = h->ih_vid;
1515 sprintf(oldpath, "%s/%s", dir_name, infoName);
1516 fd = open(oldpath, O_RDWR, 0);
1518 Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
1522 t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_VOLINFO);
1523 namei_HandleToName(&n, &t_ih);
1524 fd2 = open(n.n_path, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, 0);
1526 Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n",
1531 code = convertVolumeInfo(fd, fd2, h->ih_vid);
1538 SetOGM(fd2, h->ih_vid, 1);
1541 t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_SMALLINDEX);
1542 namei_HandleToName(&n, &t_ih);
1543 sprintf(newpath, "%s/%s", dir_name, smallName);
1544 fd = open(newpath, O_RDWR, 0);
1546 Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n",
1550 SetOGM(fd, h->ih_vid, 2);
1552 link(newpath, n.n_path);
1555 t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_LARGEINDEX);
1556 namei_HandleToName(&n, &t_ih);
1557 sprintf(newpath, "%s/%s", dir_name, largeName);
1558 fd = open(newpath, O_RDWR, 0);
1560 Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n",
1564 SetOGM(fd, h->ih_vid, 3);
1566 link(newpath, n.n_path);
1575 * returns a static string used to print either 32 or 64 bit inode numbers.
1577 char * PrintInode(char *s, Inode ino)
1579 static afs_ino_str_t result;
1583 (void) sprintf((char*)s, "%Lu", ino);
1590 /* Routines to facilitate removing zero link count files. */
1591 #define MAX_ZLC_NAMES 32
1592 #define MAX_ZLC_NAMELEN 16
1593 typedef struct zlcList_s {
1594 struct zlcList_s *zlc_next;
1596 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1599 static zlcList_t *zlcAnchor = NULL;
1600 static zlcList_t *zlcCur = NULL;
1602 static void AddToZLCDeleteList(char dir, char *name)
1604 assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1606 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1607 if (zlcCur && zlcCur->zlc_next)
1608 zlcCur = zlcCur->zlc_next;
1610 zlcList_t *tmp = (zlcList_t*)malloc(sizeof(zlcList_t));
1617 zlcCur->zlc_next = tmp;
1621 zlcCur->zlc_next = NULL;
1625 (void) sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1629 static void DeleteZLCFiles(char *path)
1635 for (z = zlcAnchor; z; z = z->zlc_next) {
1636 for (i=0; i < z->zlc_n; i++) {
1637 (void) sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1638 if (namei_unlink(fname)<0) {
1639 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1643 z->zlc_n = 0; /* Can reuse space. */
1648 static void FreeZLCList(void)
1655 tnext = i->zlc_next;
1659 zlcCur = zlcAnchor = NULL;
1663 #endif /* AFS_NAMEI_ENV */